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
49 changes: 49 additions & 0 deletions docs/source/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------

Expand Down
27 changes: 27 additions & 0 deletions examples/sintaxy_with_styles.py
Original file line number Diff line number Diff line change
@@ -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)