MCPcopy
hub / github.com/Textualize/rich / _apply_stylized_ranges

Method _apply_stylized_ranges

rich/syntax.py:790–821  ·  view source on GitHub ↗

Apply stylized ranges to a text instance, using the given code to determine the right portion to apply the style to. Args: text (Text): Text instance to apply the style to.

(self, text: Text)

Source from the content-addressed store, hash-verified

788 yield new_line
789
790 def _apply_stylized_ranges(self, text: Text) -> None:
791 """
792 Apply stylized ranges to a text instance,
793 using the given code to determine the right portion to apply the style to.
794
795 Args:
796 text (Text): Text instance to apply the style to.
797 """
798 code = text.plain
799 newlines_offsets = [
800 # Let's add outer boundaries at each side of the list:
801 0,
802 # N.B. using "\n" here is much faster than using metacharacters such as "^" or "\Z":
803 *[
804 match.start() + 1
805 for match in re.finditer("\n", code, flags=re.MULTILINE)
806 ],
807 len(code) + 1,
808 ]
809
810 for stylized_range in self._stylized_ranges:
811 start = _get_code_index_for_syntax_position(
812 newlines_offsets, stylized_range.start
813 )
814 end = _get_code_index_for_syntax_position(
815 newlines_offsets, stylized_range.end
816 )
817 if start is not None and end is not None:
818 if stylized_range.style_before:
819 text.stylize_before(stylized_range.style, start, end)
820 else:
821 text.stylize(stylized_range.style, start, end)
822
823 def _process_code(self, code: str) -> Tuple[bool, str]:
824 """

Callers 1

highlightMethod · 0.95

Calls 4

stylize_beforeMethod · 0.80
stylizeMethod · 0.80
startMethod · 0.45

Tested by

no test coverage detected