Skip to content

Commit 2bca925

Browse files
committed
perf(syntax): wrap syntax text directly
1 parent dd6e32e commit 2bca925

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

benchmarks/benchmarks.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,22 @@ def time_text_thin_terminal_heavy_wrapping(self):
110110
self.console.print(self.syntax, width=30)
111111

112112

113+
class SyntaxLargeWrappingSuite:
114+
def setup(self):
115+
self.console = Console(
116+
file=StringIO(), color_system="truecolor", legacy_windows=False
117+
)
118+
self.syntax = Syntax(
119+
code=snippets.PYTHON_SNIPPET * 120,
120+
lexer="python",
121+
word_wrap=True,
122+
line_numbers=False,
123+
)
124+
125+
def time_text_thin_terminal_heavy_wrapping(self):
126+
self.console.print(self.syntax, width=30)
127+
128+
113129
class TableSuite:
114130
def time_table_no_wrapping(self):
115131
self._print_table(width=100)

rich/syntax.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -745,15 +745,25 @@ def _get_syntax(
745745

746746
if self.word_wrap and not self.line_numbers:
747747
text = Text("\n").join(lines)
748-
syntax_lines = console.render_lines(
749-
text,
750-
render_options.update(height=None, justify="left"),
751-
style=background_style,
752-
pad=not transparent_background,
753-
new_lines=True,
754-
)
755-
for syntax_line in syntax_lines:
756-
yield from syntax_line
748+
for wrapped_line in text.wrap(
749+
console,
750+
render_options.max_width,
751+
justify="left",
752+
overflow=render_options.overflow,
753+
tab_size=self.tab_size,
754+
no_wrap=render_options.no_wrap,
755+
):
756+
yield from _Segment.adjust_line_length(
757+
list(
758+
_Segment.apply_style(
759+
wrapped_line.render(console), background_style
760+
)
761+
),
762+
render_options.max_width,
763+
style=background_style,
764+
pad=not transparent_background,
765+
)
766+
yield new_line
757767
return
758768

759769
for line_no, line in enumerate(lines, self.start_line + line_offset):

0 commit comments

Comments
 (0)