Skip to content

Commit

Permalink
highlights(python): add support for pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
theHamsta committed Dec 3, 2021
1 parent d6a0a26 commit a37830c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion queries/python/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 40 additions & 0 deletions tests/query/highlights/python/pattern_matching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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

0 comments on commit a37830c

Please sign in to comment.