Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for syntax measure #2558

Merged
merged 1 commit into from Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix missing `mode` property on file wrapper breaking uploads via `requests` https://github.com/Textualize/rich/pull/2495
- Fix mismatching default value of parameter `ensure_ascii` https://github.com/Textualize/rich/pull/2538
- Remove unused height parameter in `Layout` class https://github.com/Textualize/rich/pull/2540
- Fixed exception in Syntax.__rich_measure__ for empty files


### Changed
Expand Down
3 changes: 2 additions & 1 deletion rich/syntax.py
Expand Up @@ -593,10 +593,11 @@ def __rich_measure__(
if self.code_width is not None:
width = self.code_width + self._numbers_column_width + padding + 1
return Measurement(self._numbers_column_width, width)
lines = self.code.splitlines()
width = (
self._numbers_column_width
+ padding
+ max(cell_len(line) for line in self.code.splitlines())
+ (max(cell_len(line) for line in lines) if lines else 0)
)
if self.line_numbers:
width += 1
Expand Down
3 changes: 3 additions & 0 deletions tests/test_syntax.py
Expand Up @@ -392,6 +392,9 @@ def test_syntax_measure():
code = Syntax("Hello, World", "python", code_width=20, line_numbers=True)
assert code.__rich_measure__(console, console.options) == Measurement(3, 24)

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


if __name__ == "__main__":
syntax = Panel.fit(
Expand Down