rouge-ruby / rouge

A pure Ruby code highlighter that is compatible with Pygments
https://rouge.jneen.net/
Other
3.3k stars 732 forks source link

Improve Python lexer #1919

Closed dunkmann00 closed 1 year ago

dunkmann00 commented 1 year ago

This includes three changes to the Python lexer:

1) Move the decorator rule before the operator rule.

Before this change, decorators were never getting recognized. This was because if there was ever an @, it was always satisfying the operator rule. This remedies that by first checking for the decorator pattern and then the operator pattern.

2) Recognize functions and classes when they are called.

Previously, functions and classes were only recognized when they were defined. With this change, they will also be recognized and styled when they are called.

3) Don't recognize imported modules as Namespaces.

It is not desirable to highlight imported modules like namespaces. With this change, they are simply recognized as the general Name token.

4) Remove self from @builtins_pseudo to prevent it from getting styled

self is not a built-in special variable in Python. It is just a local variable that is customarily used to indicate the calling object on an instance method. As such, it shouldn't be styled the same as actual built-in objects.

dunkmann00 commented 1 year ago

Hi @tancnle! Any thoughts on this or #1920?

tancnle commented 1 year ago

Hi @tancnle! Any thoughts on this or #1920?

@dunkmann00 I will have a look at the PR sometime today. Sorry for the delay 🙇🏼‍♂️

tancnle commented 1 year ago

@dunkmann00 Thank you for the contribution. The PR looks great to me.

  1. See the visual comparison before and after ✨ 🤩
Before After
Screenshot 2023-01-31 at 4 48 02 pm Screenshot 2023-01-31 at 4 48 31 pm
  1. I have added some visual samples to the PR.
  2. I have left a comment on the removal of self.

    Please let me know what you think about 2 and 3. 🙏🏼

dunkmann00 commented 1 year ago

@tancnle The added visual samples look great! I added one more to show that classes also get highlighted when they are created.

Before After
image image
tancnle commented 1 year ago

Thank you for your effort on this @dunkmann00 🙇🏼 🚀

dunkmann00 commented 1 year ago

Thanks for reviewing, helping, and merging @tancnle!