-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add lexer parameter to Syntax #1748
Add lexer parameter to Syntax #1748
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. A couple of minor requests.
rich/syntax.py
Outdated
@@ -374,8 +381,8 @@ def highlight( | |||
) | |||
_get_theme_style = self._theme.get_style_for_token | |||
try: | |||
lexer = get_lexer_by_name( | |||
self.lexer_name, | |||
lexer = self.lexer or get_lexer_by_name( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Breaking this in to an if
rather than an expression would allow test coverage to span both branches, and avoid that cast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda prefer the previous version 😊 ideally the if would live outside the try/except, but there's logic using the lexer inside the else statement, so I've kept it there. We still need a cast (or an asser as I've done now) in this case :)
Just thinking this through. I think your comment about not being 100% happy with having That could be construed as a breaking change, but it was always intended to be a positional argument. So it might warrant a major version bump. Let's go with the single |
I think this looks much better! Let me know what you think 😊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. Just a request for a docstring and this is good to go.
@@ -348,6 +349,20 @@ def _get_token_color(self, token_type: TokenType) -> Optional[Color]: | |||
style = self._theme.get_style_for_token(token_type) | |||
return style.color | |||
|
|||
@property | |||
def lexer(self) -> Optional[Lexer]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring please!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, hope it is good!
Codecov Report
@@ Coverage Diff @@
## master #1748 +/- ##
=======================================
Coverage 99.82% 99.82%
=======================================
Files 71 71
Lines 6914 6951 +37
=======================================
+ Hits 6902 6939 +37
Misses 12 12
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
Thanks! |
11.0.0 Added Added max_depth arg to pretty printing Textualize/rich#1585 Added vertical_align to Table.add_row Textualize/rich#1590 Fixed Fixed issue with pretty repr in jupyter notebook Textualize/rich#1717 Fix Traceback theme defaults override user supplied styles Textualize/rich#1786 Changed breaking Deprecated rich.console.RenderGroup, now named rich.console.Group breaking Syntax.__init__ parameter lexer_name renamed to lexer Syntax constructor accepts both str and now a pygments lexer Textualize/rich#1748
Type of changes
Checklist
Description
I was working on some code that needed to show highlighted code and I needed to use a lexer instance as I was using some custom Pygment filters and I noticed that rich doesn't allow to pass an instance of lexer, so I decided to see if I can add support for that 😊
I'm not 100% happy with having both
lexer
andlexer_name
as arguments, but that's the easiest way to maintain compatibilty. Another option is renamelexer_name
tolexer
and acceptUnion[str, Lexer]
, but that would be a breaking change (which might be ok), let me know what you think 😊Related discussion: #985