docstring and test

This commit is contained in:
Will McGugan
2025-08-06 16:35:28 +01:00
parent 6b0c6e045d
commit ef4db84f24
2 changed files with 14 additions and 4 deletions

View File

@@ -396,12 +396,14 @@ class Content(Visual):
return cls("".join(text), spans)
def simplify(self) -> Content:
"""Simplify spans in place.
"""Simplify spans by joining contiguous spans together.
This joins contiguous spans together which can produce faster renders.
This can produce faster renders but typically only worth it if you have appended a
large number of Content instances together.
Note that this is only typically worth it if you have appended a large number of Content instances together,
and it only needs to be done once.
Note that this this modifies the Content instance in-place, which might appear
to violate the immutability constraints, but it will not change the rendered output,
nor its hash.
Returns:
Self.

View File

@@ -286,3 +286,11 @@ def test_split_and_tabs():
content = Content("--- hello.py\t2024-01-15 10:30:00.000000000 -0800", spans=spans)
widget = Widget()
content.render_strips(0, None, Style(), RenderOptions(widget._get_style, {}))
def test_simplify():
"""Test simplify joins spans."""
content = Content.from_markup("[bold]Foo[/][bold]Bar[/]")
assert content.spans == [Span(0, 3, "bold"), Span(3, 6, "bold")]
content.simplify()
assert content.spans == [Span(0, 6, "bold")]