Skip to content

Commit e23ca4d

Browse files
authored
Merge pull request #2558 from Textualize/syntax-measure
fix for syntax measure
2 parents 13dd9c2 + fe1ed53 commit e23ca4d

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
- Fix missing `mode` property on file wrapper breaking uploads via `requests` https://github.com/Textualize/rich/pull/2495
2626
- Fix mismatching default value of parameter `ensure_ascii` https://github.com/Textualize/rich/pull/2538
2727
- Remove unused height parameter in `Layout` class https://github.com/Textualize/rich/pull/2540
28+
- Fixed exception in Syntax.__rich_measure__ for empty files
2829

2930

3031
### Changed

rich/syntax.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,11 @@ def __rich_measure__(
593593
if self.code_width is not None:
594594
width = self.code_width + self._numbers_column_width + padding + 1
595595
return Measurement(self._numbers_column_width, width)
596+
lines = self.code.splitlines()
596597
width = (
597598
self._numbers_column_width
598599
+ padding
599-
+ max(cell_len(line) for line in self.code.splitlines())
600+
+ (max(cell_len(line) for line in lines) if lines else 0)
600601
)
601602
if self.line_numbers:
602603
width += 1

tests/test_syntax.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ def test_syntax_measure():
392392
code = Syntax("Hello, World", "python", code_width=20, line_numbers=True)
393393
assert code.__rich_measure__(console, console.options) == Measurement(3, 24)
394394

395+
code = Syntax("", "python", code_width=20, line_numbers=True)
396+
assert code.__rich_measure__(console, console.options) == Measurement(3, 24)
397+
395398

396399
if __name__ == "__main__":
397400
syntax = Panel.fit(

0 commit comments

Comments
 (0)