Skip to content

Commit a7e8a6b

Browse files
authored
Merge pull request #2219 from Textualize/bugfix-inconsistent-coloring-of-complex-numbers
[highlighter] Add complex numbers pattern to our highlighter's `number` matching
2 parents ed6ff7e + a02e5d6 commit a7e8a6b

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- Fixed markup escaping issue https://github.com/Textualize/rich/issues/2187
1919
- Safari - Box appearing around SVG export https://github.com/Textualize/rich/pull/2201
2020
- Fixed recursion error in Jupyter progress bars https://github.com/Textualize/rich/issues/2047
21+
- Complex numbers are now identified by the highlighter https://github.com/Textualize/rich/issues/2214
2122
- Fix crash on IDLE and forced is_terminal detection to False because IDLE can't do escape codes https://github.com/Textualize/rich/issues/2222
2223

2324
### Changed

rich/default_styles.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"repr.attrib_equal": Style(bold=True),
7979
"repr.attrib_value": Style(color="magenta", italic=False),
8080
"repr.number": Style(color="cyan", bold=True, italic=False),
81+
"repr.number_complex": Style(color="cyan", bold=True, italic=False), # same
8182
"repr.bool_true": Style(color="bright_green", italic=True),
8283
"repr.bool_false": Style(color="bright_red", italic=True),
8384
"repr.none": Style(color="magenta", italic=True),

rich/highlighter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class ReprHighlighter(RegexHighlighter):
9494
r"(?P<call>[\w.]*?)\(",
9595
r"\b(?P<bool_true>True)\b|\b(?P<bool_false>False)\b|\b(?P<none>None)\b",
9696
r"(?P<ellipsis>\.\.\.)",
97+
r"(?P<number_complex>(?<!\w)(?:\-?[0-9]+\.?[0-9]*(?:e[-+]?\d+?)?)(?:[-+](?:[0-9]+\.?[0-9]*(?:e[-+]?\d+)?))?j)",
9798
r"(?P<number>(?<!\w)\-?[0-9]+\.?[0-9]*(e[-+]?\d+?)?\b|0x[0-9a-fA-F]*)",
9899
r"(?P<path>\B(/[-\w._+]+)*\/)(?P<filename>[-\w._+]*)?",
99100
r"(?<![\\\w])(?P<str>b?'''.*?(?<!\\)'''|b?'.*?(?<!\\)'|b?\"\"\".*?(?<!\\)\"\"\"|b?\".*?(?<!\\)\")",

tests/test_highlighter.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,40 @@ def test_wrong_type():
5959
(" 1.2 ", [Span(1, 4, "repr.number")]),
6060
(" 0xff ", [Span(1, 5, "repr.number")]),
6161
(" 1e10 ", [Span(1, 5, "repr.number")]),
62+
(" 1j ", [Span(1, 3, "repr.number_complex")]),
63+
(" 3.14j ", [Span(1, 6, "repr.number_complex")]),
64+
(
65+
" (3.14+2.06j) ",
66+
[
67+
Span(1, 2, "repr.brace"),
68+
Span(12, 13, "repr.brace"),
69+
Span(2, 12, "repr.number_complex"),
70+
],
71+
),
72+
(
73+
" (3+2j) ",
74+
[
75+
Span(1, 2, "repr.brace"),
76+
Span(6, 7, "repr.brace"),
77+
Span(2, 6, "repr.number_complex"),
78+
],
79+
),
80+
(
81+
" (123456.4321-1234.5678j) ",
82+
[
83+
Span(1, 2, "repr.brace"),
84+
Span(24, 25, "repr.brace"),
85+
Span(2, 24, "repr.number_complex"),
86+
],
87+
),
88+
(
89+
" (-123123-2.1312342342423422e+25j) ",
90+
[
91+
Span(1, 2, "repr.brace"),
92+
Span(33, 34, "repr.brace"),
93+
Span(2, 33, "repr.number_complex"),
94+
],
95+
),
6296
(" /foo ", [Span(1, 2, "repr.path"), Span(2, 5, "repr.filename")]),
6397
(" /foo/bar.html ", [Span(1, 6, "repr.path"), Span(6, 14, "repr.filename")]),
6498
("01-23-45-67-89-AB", [Span(0, 17, "repr.eui48")]), # 6x2 hyphen

0 commit comments

Comments
 (0)