From a37830cfe03564a5848875f41e5213d11fcbc126 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Fri, 3 Dec 2021 19:10:11 +0100 Subject: [PATCH] highlights(python): add support for pattern matching Fixes #2080 Depends on https://github.com/tree-sitter/tree-sitter-python/pull/140 --- queries/python/highlights.scm | 2 +- .../highlights/python/pattern_matching.py | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/query/highlights/python/pattern_matching.py diff --git a/queries/python/highlights.scm b/queries/python/highlights.scm index 423948e51a2..220ec0d74b4 100644 --- a/queries/python/highlights.scm +++ b/queries/python/highlights.scm @@ -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 diff --git a/tests/query/highlights/python/pattern_matching.py b/tests/query/highlights/python/pattern_matching.py new file mode 100644 index 00000000000..d1d3722aeab --- /dev/null +++ b/tests/query/highlights/python/pattern_matching.py @@ -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