From f67f426a6b7deb35fd8345edec0397562fb3b91b Mon Sep 17 00:00:00 2001 From: Olivier Philippon Date: Mon, 25 Apr 2022 12:18:11 +0100 Subject: [PATCH 1/4] [highlighter] Add complex numbers pattern to our highlighter's `number` matching --- rich/highlighter.py | 2 +- tests/test_highlighter.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/rich/highlighter.py b/rich/highlighter.py index 7bee4167e..ef2f3fedb 100644 --- a/rich/highlighter.py +++ b/rich/highlighter.py @@ -94,7 +94,7 @@ class ReprHighlighter(RegexHighlighter): r"(?P[\w.]*?)\(", r"\b(?PTrue)\b|\b(?PFalse)\b|\b(?PNone)\b", r"(?P\.\.\.)", - r"(?P(?(?\B(/[-\w._+]+)*\/)(?P[-\w._+]*)?", r"(?b?'''.*?(?(file|https|http|ws|wss)://[-0-9a-zA-Z$_+!`(),.?/;:&=%#]*)", diff --git a/tests/test_highlighter.py b/tests/test_highlighter.py index 930b340e5..ea6120beb 100644 --- a/tests/test_highlighter.py +++ b/tests/test_highlighter.py @@ -59,6 +59,9 @@ 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")]), (" /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 From 5e67e22ba48a0d7378713b80b1a91400647daa73 Mon Sep 17 00:00:00 2001 From: Olivier Philippon Date: Mon, 25 Apr 2022 16:47:43 +0100 Subject: [PATCH 2/4] [highlighter] Extract more complex numbers pattern matching on its own one, rather than being part of the `number` one --- rich/default_styles.py | 1 + rich/highlighter.py | 3 ++- tests/test_highlighter.py | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/rich/default_styles.py b/rich/default_styles.py index d803eec91..69a180c64 100644 --- a/rich/default_styles.py +++ b/rich/default_styles.py @@ -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), diff --git a/rich/highlighter.py b/rich/highlighter.py index ef2f3fedb..d235bf1f8 100644 --- a/rich/highlighter.py +++ b/rich/highlighter.py @@ -94,7 +94,8 @@ class ReprHighlighter(RegexHighlighter): r"(?P[\w.]*?)\(", r"\b(?PTrue)\b|\b(?PFalse)\b|\b(?PNone)\b", r"(?P\.\.\.)", - r"(?P(?(?(?\B(/[-\w._+]+)*\/)(?P[-\w._+]*)?", r"(?b?'''.*?(?(file|https|http|ws|wss)://[-0-9a-zA-Z$_+!`(),.?/;:&=%#]*)", diff --git a/tests/test_highlighter.py b/tests/test_highlighter.py index ea6120beb..b9cf9c398 100644 --- a/tests/test_highlighter.py +++ b/tests/test_highlighter.py @@ -59,9 +59,40 @@ 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")]), + (" 1j ", [Span(1, 3, "repr.complex_number")]), + (" 3.14j ", [Span(1, 6, "repr.complex_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 From 69003a772c2d0840820952b0c25dea57eb3ed714 Mon Sep 17 00:00:00 2001 From: Olivier Philippon Date: Tue, 26 Apr 2022 09:17:36 +0100 Subject: [PATCH 3/4] [highlighter] Fine-tuning on complex numbers pattern matching --- rich/default_styles.py | 2 +- rich/highlighter.py | 2 +- tests/test_highlighter.py | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/rich/default_styles.py b/rich/default_styles.py index 69a180c64..1969bb787 100644 --- a/rich/default_styles.py +++ b/rich/default_styles.py @@ -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), diff --git a/rich/highlighter.py b/rich/highlighter.py index d235bf1f8..193261c4c 100644 --- a/rich/highlighter.py +++ b/rich/highlighter.py @@ -94,7 +94,7 @@ class ReprHighlighter(RegexHighlighter): r"(?P[\w.]*?)\(", r"\b(?PTrue)\b|\b(?PFalse)\b|\b(?PNone)\b", r"(?P\.\.\.)", - r"(?P(?(?(?\B(/[-\w._+]+)*\/)(?P[-\w._+]*)?", r"(?b?'''.*?(? Date: Tue, 26 Apr 2022 11:29:28 +0100 Subject: [PATCH 4/4] [CHANGELOG] Complex numbers are now identified by the highlighter --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b82884a77..1a10aebb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed markup escaping issue https://github.com/Textualize/rich/issues/2187 - Safari - Box appearing around SVG export https://github.com/Textualize/rich/pull/2201 - Fixed recursion error in Jupyter progress bars https://github.com/Textualize/rich/issues/2047 +- Complex numbers are now identified by the highlighter https://github.com/Textualize/rich/issues/2214 ### Changed