From 2a808a4d8be4e37677b301390803a715be10ac57 Mon Sep 17 00:00:00 2001 From: wenlongcai1995 <1106086524@qq.com> Date: Fri, 1 May 2026 13:17:36 +0800 Subject: [PATCH] docs: document Syntax.stylize_range with working examples This adds documentation for the stylize_range method of the Syntax class, along with a demonstrative example script. The method allows users to apply custom styling to specific portions of syntax-highlighted code. Closes #2842 Co-authored-by: GenericAgent (AI assistant) --- docs/source/syntax.rst | 49 +++++++++++++++++++++++++++++++++ examples/sintaxy_with_styles.py | 27 ++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 examples/sintaxy_with_styles.py diff --git a/docs/source/syntax.rst b/docs/source/syntax.rst index 7f6c8c83af..7ec16971a8 100644 --- a/docs/source/syntax.rst +++ b/docs/source/syntax.rst @@ -44,6 +44,55 @@ Background color You can override the background color from the theme by supplying a ``background_color`` argument to the constructor. This should be a string in the same format a style definition accepts, e.g. "red", "#ff0000", "rgb(255,0,0)" etc. You may also set the special value "default" which will use the default background color set in the terminal. +Stylize Range +------------- + +You can apply custom styling to specific portions of syntax highlighted code +using the :meth:`~rich.syntax.Syntax.stylize_range` method:: + + from rich.console import Console + from rich.syntax import Syntax + from rich.style import Style + + console = Console() + + code = """\ +def fibonacci(n): + if n <= 1: + return n + return fibonacci(n - 1) + fibonacci(n - 2)""" + + syntax = Syntax(code, "python", line_numbers=True) + syntax.stylize_range(Style(bgcolor="bright_yellow"), (3, 4), (3, 13)) + console.print(syntax) + +The ``stylize_range`` method accepts a :class:`~rich.style.Style` object and +two positional tuples: ``start`` and ``end``. + +Positions are specified as ``(line, column)`` tuples where: + +- ``line`` is 1-based (the first line is 1) +- ``column`` is 0-based (the first character on a line is 0) + +You can also pass ``style_before=True`` to apply the style underneath +any existing styles, instead of on top:: + + syntax.stylize_range( + Style(bold=True), + (1, 0), (1, 3), + style_before=True, + ) + +.. note:: + + Line numbers are based on the original code, not the displayed output. + If your code contains newline characters (``\\n``), each newline + increments the line number. + +For a complete working example, see +``examples/sintaxy_with_styles.py``. + + Syntax CLI ---------- diff --git a/examples/sintaxy_with_styles.py b/examples/sintaxy_with_styles.py new file mode 100644 index 0000000000..87711e89d2 --- /dev/null +++ b/examples/sintaxy_with_styles.py @@ -0,0 +1,27 @@ +""" +Demonstration of Syntax.stylize_range +""" + +from rich.console import Console +from rich.style import Style +from rich.syntax import Syntax + +console = Console() + +# Example 1: Single-line code +code1 = "example = True" +syntax1 = Syntax(code1, "python") +syntax1.stylize_range(Style(bgcolor="bright_yellow"), (1, 10), (1, 14)) +console.print("Example 1 - Highlight 'True':") +console.print(syntax1) + +# Example 2: Multi-line code with line numbers +code2 = """def fibonacci(n): + if n <= 1: + return n + return fibonacci(n - 1) + fibonacci(n - 2)""" +syntax2 = Syntax(code2, "python", line_numbers=True) +# Highlight "return n" on line 3 +syntax2.stylize_range(Style(bgcolor="deep_sky_blue4", bold=True), (3, 4), (3, 13)) +console.print("\nExample 2 - Highlight 'return n' (with line numbers):") +console.print(syntax2)