Method: rich.syntax.Syntax._apply_stylized_ranges
Calls: 1, Exceptions: 0, Paths: 1Back
Path 1: 1 calls (1.0)
Text (1)
1def _apply_stylized_ranges(self, text: Text) -> None:
2 """
3 Apply stylized ranges to a text instance,
4 using the given code to determine the right portion to apply the style to.
5
6 Args:
7 text (Text): Text instance to apply the style to.
8 """
9 code = text.plain
10 newlines_offsets = [
11 # Let's add outer boundaries at each side of the list:
12 0,
13 # N.B. using "\n" here is much faster than using metacharacters such as "^" or "\Z":
14 *[
15 match.start() + 1
16 for match in re.finditer("\n", code, flags=re.MULTILINE)
17 ],
18 len(code) + 1,
19 ]
20
21 for stylized_range in self._stylized_ranges:
22 start = _get_code_index_for_syntax_position(
23 newlines_offsets, stylized_range.start
24 )
25 end = _get_code_index_for_syntax_position(
26 newlines_offsets, stylized_range.end
27 )
28 if start is not None and end is not None:
29 text.stylize(stylized_range.style, start, end)