Skip to content

Commit

Permalink
[highlighter] Fine-tuning on complex numbers pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierphi committed Apr 26, 2022
1 parent 5e67e22 commit 69003a7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion rich/default_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"repr.attrib_equal": Style(bold=True),
"repr.attrib_value": Style(color="magenta", italic=False),
"repr.number": Style(color="cyan", bold=True, italic=False),
"repr.complex_number": Style(color="magenta", bold=True, italic=False),
"repr.number_complex": Style(color="cyan", bold=True, italic=False), # same
"repr.bool_true": Style(color="bright_green", italic=True),
"repr.bool_false": Style(color="bright_red", italic=True),
"repr.none": Style(color="magenta", italic=True),
Expand Down
2 changes: 1 addition & 1 deletion rich/highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class ReprHighlighter(RegexHighlighter):
r"(?P<call>[\w.]*?)\(",
r"\b(?P<bool_true>True)\b|\b(?P<bool_false>False)\b|\b(?P<none>None)\b",
r"(?P<ellipsis>\.\.\.)",
r"(?P<complex_number>(?<!\w)(?:\-?[0-9]+\.?[0-9]*(?:e[-+]?\d+?)?|0x[0-9a-fA-F]*)(?:[-+](?:[0-9]+\.?[0-9]*(?:e[-+]?\d+)?|0x[0-9a-fA-F]*))?j)",
r"(?P<number_complex>(?<!\w)(?:\-?[0-9]+\.?[0-9]*(?:e[-+]?\d+?)?)(?:[-+](?:[0-9]+\.?[0-9]*(?:e[-+]?\d+)?))?j)",
r"(?P<number>(?<!\w)\-?[0-9]+\.?[0-9]*(e[-+]?\d+?)?\b|0x[0-9a-fA-F]*)",
r"(?P<path>\B(/[-\w._+]+)*\/)(?P<filename>[-\w._+]*)?",
r"(?<![\\\w])(?P<str>b?'''.*?(?<!\\)'''|b?'.*?(?<!\\)'|b?\"\"\".*?(?<!\\)\"\"\"|b?\".*?(?<!\\)\")",
Expand Down
12 changes: 6 additions & 6 deletions tests/test_highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,38 +59,38 @@ def test_wrong_type():
(" 1.2 ", [Span(1, 4, "repr.number")]),
(" 0xff ", [Span(1, 5, "repr.number")]),
(" 1e10 ", [Span(1, 5, "repr.number")]),
(" 1j ", [Span(1, 3, "repr.complex_number")]),
(" 3.14j ", [Span(1, 6, "repr.complex_number")]),
(" 1j ", [Span(1, 3, "repr.number_complex")]),
(" 3.14j ", [Span(1, 6, "repr.number_complex")]),
(
" (3.14+2.06j) ",
[
Span(1, 2, "repr.brace"),
Span(12, 13, "repr.brace"),
Span(2, 12, "repr.complex_number"),
Span(2, 12, "repr.number_complex"),
],
),
(
" (3+2j) ",
[
Span(1, 2, "repr.brace"),
Span(6, 7, "repr.brace"),
Span(2, 6, "repr.complex_number"),
Span(2, 6, "repr.number_complex"),
],
),
(
" (123456.4321-1234.5678j) ",
[
Span(1, 2, "repr.brace"),
Span(24, 25, "repr.brace"),
Span(2, 24, "repr.complex_number"),
Span(2, 24, "repr.number_complex"),
],
),
(
" (-123123-2.1312342342423422e+25j) ",
[
Span(1, 2, "repr.brace"),
Span(33, 34, "repr.brace"),
Span(2, 33, "repr.complex_number"),
Span(2, 33, "repr.number_complex"),
],
),
(" /foo ", [Span(1, 2, "repr.path"), Span(2, 5, "repr.filename")]),
Expand Down

0 comments on commit 69003a7

Please sign in to comment.