highlights(python): add support for pattern matching

Fixes #2080
Depends on https://github.com/tree-sitter/tree-sitter-python/pull/140
This commit is contained in:
Stephan Seitz 2021-12-03 19:10:11 +01:00
parent 542d098e14
commit c4e3564ea3
4 changed files with 54 additions and 7 deletions

View file

@ -207,7 +207,7 @@
"revision": "5875f9a7d94836708119b0a1102bb5792e8bf673"
},
"python": {
"revision": "e979351ec7b033fc2515ba9b573e3ad0fee8d1c3"
"revision": "ed0fe62e55dc617ed9dec8817ebf771aa7cf3c42"
},
"ql": {
"revision": "8e7fd7e638d4a0ec7a792ee16b19dbc6407aa810"

View file

@ -230,7 +230,7 @@
["from" "import"] @include
(aliased_import "as" @include)
["if" "elif" "else"] @conditional
["if" "elif" "else" "match" "case"] @conditional
["for" "while" "break" "continue"] @repeat

View file

@ -34,11 +34,6 @@
(typed_default_parameter
(identifier) @definition.parameter)
(with_statement
(with_clause
(with_item
alias: (identifier) @definition.var)))
; *args parameter
(parameters
(list_splat_pattern
@ -113,6 +108,8 @@
(named_expression
(identifier) @definition.var)
(as_pattern
alias: (identifier) @definition.var)
;;; REFERENCES
(identifier) @reference

View file

@ -0,0 +1,50 @@
match command.split():
# ^ conditional
case ["quit"]:
# ^ conditional
print("Goodbye!")
quit_game()
case ["look"]:
# ^ conditional
current_room.describe()
case ["get", obj]:
# ^ conditional
character.get(obj, current_room)
case ["go", direction]:
# ^ conditional
current_room = current_room.neighbor(direction)
# The rest of your commands go here
match command.split():
# ^ conditional
case ["drop", *objects]:
# ^ conditional
for obj in objects:
character.drop(obj, current_room)
match command.split():
# ^ conditional
case ["quit"]: ... # Code omitted for brevity
case ["go", direction]: pass
case ["drop", *objects]: pass
case _:
print(f"Sorry, I couldn't understand {command!r}")
match command.split():
# ^ conditional
case ["north"] | ["go", "north"]:
# ^ conditional
current_room = current_room.neighbor("north")
case ["get", obj] | ["pick", "up", obj] | ["pick", obj, "up"]:
# ^ conditional
pass
match = 2
# ^ variable
match, a = 2, 3
# ^ variable
match: int = secret
# ^ variable
x, match: str = 2, "hey, what's up?"
# <- variable
# ^ variable