Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rich/highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ReprHighlighter(RegexHighlighter):
r"\b(?P<bool_true>True)\b|\b(?P<bool_false>False)\b|\b(?P<none>None)\b",
r"(?P<ellipsis>\.\.\.)",
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<number>(?<!\w)\-?[0-9]+\.?[0-9]*(e[-+]?\d+?)?\b|(?<![0-9A-Za-z_])[+-]?0[xX][0-9A-Fa-f](?:_?[0-9A-Fa-f])*(?![0-9A-Za-z_]))",
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
36 changes: 36 additions & 0 deletions tests/test_highlighter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Tests for the highlighter classes."""

import json
import re
from typing import List

import pytest
Expand All @@ -12,6 +14,8 @@
)
from rich.text import Span, Text

HEX_LITERAL_RE = re.compile(r"[+-]?0x[0-9a-f](?:_?[0-9a-f])*$", re.IGNORECASE)


def test_wrong_type():
highlighter = NullHighlighter()
Expand Down Expand Up @@ -167,6 +171,38 @@ def test_highlight_regex(test: str, spans: List[Span]):
assert text.spans == spans


@pytest.mark.parametrize(
"literal, should_highlight",
[
("0xFF", True),
("0Xff", True),
("-0x1A", True),
("+0x2a", True),
("0xdead_beef", True),
("0xDEAD_BEEF", True),
("(0x2A)", True),
(",0x2A", True),
("1920x1080", False),
("abc0x123", False),
("eth0x1", False),
("foo0xFFbar", False),
("0xdead__beef", False),
("0x_deadbeef", False),
("0x123_", False),
],
)
def test_hex_boundaries(literal: str, should_highlight: bool) -> None:
text = Text(literal)
highlighter = ReprHighlighter()
highlighter.highlight(text)
has_hex_highlight = any(
span.style == "repr.number"
and HEX_LITERAL_RE.fullmatch(text.plain[span.start : span.end])
for span in text.spans
)
assert has_hex_highlight is should_highlight


def test_highlight_json_with_indent():
json_string = json.dumps({"name": "apple", "count": 1}, indent=4)
text = Text(json_string)
Expand Down