Add lexer parameter to Syntax#1748
Conversation
willmcgugan
left a comment
There was a problem hiding this comment.
Looks good. A couple of minor requests.
rich/syntax.py
Outdated
| try: | ||
| lexer = get_lexer_by_name( | ||
| self.lexer_name, | ||
| lexer = self.lexer or get_lexer_by_name( |
There was a problem hiding this comment.
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.
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 😊 |
willmcgugan
left a comment
There was a problem hiding this comment.
Great. Just a request for a docstring and this is good to go.
| return style.color | ||
|
|
||
| @property | ||
| def lexer(self) -> Optional[Lexer]: |
There was a problem hiding this comment.
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
lexerandlexer_nameas arguments, but that's the easiest way to maintain compatibilty. Another option is renamelexer_nametolexerand acceptUnion[str, Lexer], but that would be a breaking change (which might be ok), let me know what you think 😊Related discussion: #985