diff options
| author | Stephan Seitz <stephan.seitz@fau.de> | 2021-12-03 19:10:11 +0100 |
|---|---|---|
| committer | Stephan Seitz <stephan.seitz@fau.de> | 2022-01-28 19:11:20 +0100 |
| commit | c4e3564ea31183ddcd00d6192efb2879977b420e (patch) | |
| tree | 0de3067dfe736560a5fd40c263bada51f4ba0cb6 | |
| parent | highlights(go): highlight `package_identifier` as `@namespace` (#2371) (diff) | |
| download | nvim-treesitter-c4e3564ea31183ddcd00d6192efb2879977b420e.tar nvim-treesitter-c4e3564ea31183ddcd00d6192efb2879977b420e.tar.gz nvim-treesitter-c4e3564ea31183ddcd00d6192efb2879977b420e.tar.bz2 nvim-treesitter-c4e3564ea31183ddcd00d6192efb2879977b420e.tar.lz nvim-treesitter-c4e3564ea31183ddcd00d6192efb2879977b420e.tar.xz nvim-treesitter-c4e3564ea31183ddcd00d6192efb2879977b420e.tar.zst nvim-treesitter-c4e3564ea31183ddcd00d6192efb2879977b420e.zip | |
highlights(python): add support for pattern matching
Fixes #2080
Depends on https://github.com/tree-sitter/tree-sitter-python/pull/140
| -rw-r--r-- | lockfile.json | 2 | ||||
| -rw-r--r-- | queries/python/highlights.scm | 2 | ||||
| -rw-r--r-- | queries/python/locals.scm | 7 | ||||
| -rw-r--r-- | tests/query/highlights/python/pattern_matching.py | 50 |
4 files changed, 54 insertions, 7 deletions
diff --git a/lockfile.json b/lockfile.json index b4b6668b2..bf3841e49 100644 --- a/lockfile.json +++ b/lockfile.json @@ -207,7 +207,7 @@ "revision": "5875f9a7d94836708119b0a1102bb5792e8bf673" }, "python": { - "revision": "e979351ec7b033fc2515ba9b573e3ad0fee8d1c3" + "revision": "ed0fe62e55dc617ed9dec8817ebf771aa7cf3c42" }, "ql": { "revision": "8e7fd7e638d4a0ec7a792ee16b19dbc6407aa810" diff --git a/queries/python/highlights.scm b/queries/python/highlights.scm index 0813c960b..ccbb2e79d 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/queries/python/locals.scm b/queries/python/locals.scm index 5cfd35435..7699a512e 100644 --- a/queries/python/locals.scm +++ b/queries/python/locals.scm @@ -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 diff --git a/tests/query/highlights/python/pattern_matching.py b/tests/query/highlights/python/pattern_matching.py new file mode 100644 index 000000000..38622cf4b --- /dev/null +++ b/tests/query/highlights/python/pattern_matching.py @@ -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 |
