Skip to content

Commit

Permalink
[highlighter] Extract more complex numbers pattern matching on its ow…
Browse files Browse the repository at this point in the history
…n one, rather than being part of the `number` one
  • Loading branch information
olivierphi committed Apr 25, 2022
1 parent f67f426 commit 22be33e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions rich/default_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +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.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
3 changes: 2 additions & 1 deletion rich/highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ 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<number>(?<!\w)\-?[0-9]+\.?[0-9]*(e[-+]?\d+?)?(\s*[-+]\s*[0-9]+\.?[0-9]*j)?\b|0x[0-9a-fA-F]*)",
r"(?P<complex_number>(?<!\w)(\-?[0-9]+\.?[0-9]*(e[-+]?\d+?)?\b|0x[0-9a-fA-F]*)[-+]([0-9]+\.?[0-9]*(e[-+]?\d+)?|0x[0-9a-fA-F]*)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?\".*?(?<!\\)\")",
r"(?P<url>(file|https|http|ws|wss)://[-0-9a-zA-Z$_+!`(),.?/;:&=%#]*)",
Expand Down
35 changes: 32 additions & 3 deletions tests/test_highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +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")]),
(" 3.14 + 2.06j ", [Span(1, 13, "repr.number")]),
(" 3+2j ", [Span(1, 5, "repr.number")]),
(" 123456.4321 - 1234.5678j ", [Span(1, 25, "repr.number")]),
(
" (3.14+2.06j) ",
[
Span(1, 2, "repr.brace"),
Span(12, 13, "repr.brace"),
Span(2, 12, "repr.complex_number"),
],
),
(
" (3+2j) ",
[
Span(1, 2, "repr.brace"),
Span(6, 7, "repr.brace"),
Span(2, 6, "repr.complex_number"),
],
),
(
" (123456.4321-1234.5678j) ",
[
Span(1, 2, "repr.brace"),
Span(24, 25, "repr.brace"),
Span(2, 24, "repr.complex_number"),
],
),
(
" (-123123-2.1312342342423422e+25j) ",
[
Span(1, 2, "repr.brace"),
Span(33, 34, "repr.brace"),
Span(2, 33, "repr.complex_number"),
],
),
(" /foo ", [Span(1, 2, "repr.path"), Span(2, 5, "repr.filename")]),
(" /foo/bar.html ", [Span(1, 6, "repr.path"), Span(6, 14, "repr.filename")]),
("01-23-45-67-89-AB", [Span(0, 17, "repr.eui48")]), # 6x2 hyphen
Expand Down

0 comments on commit 22be33e

Please sign in to comment.