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

Add python support for match/case #1981

Open fcheung opened 11 months ago

fcheung commented 11 months ago

This adds match & case as keywords, used by python 3.10 structural pattern matching ( https://peps.python.org/pep-0634/ )

Screenshot 2023-07-28 at 10 44 42

(screenshot from rouge's visual test page)

tancnle commented 10 months ago

Thank you for your contribution @fcheung 🙇🏼‍♂️ match and case are a bit special in Python. They are called soft keywords. So in the use case highlighted above, they are keywords but not for other, e.g. as variables.

# Keywords
match point:
    case Point(x, y) if x == y:
        print(f"The point is located on the diagonal Y=X at {x}.")
    case Point(x, y):
        print(f"Point is not on the diagonal.")

# Non-keywords
case = 1
match = 1
match if True else bar

We can port these behaviours from Pygments (see related PR).