Skip to content

Commit 376f9f5

Browse files
saidelikeCedric Halbronnpokeypre-commit-ci-lite[bot]AndreasArvidsson
authored
Migrate python scopes map/list (#1874)
## Checklist - [/] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [/] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [/] I have not broken the cheatsheet - [x] I have executed all existing tests (Extension tests - 6277 passing) - [x] I have tested the `visualize list` and `visualize map` commands visualize list: ![image](https://github.com/cursorless-dev/cursorless/assets/387346/9e160f0d-2af7-4d75-a611-7ae54518fbaf) visualize map: ![image](https://github.com/cursorless-dev/cursorless/assets/387346/cf4dc273-2ec7-4be8-a0ab-e291ff6e9450) --------- Co-authored-by: Cedric Halbronn <[email protected]> Co-authored-by: Pokey Rule <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Andreas Arvidsson <[email protected]>
1 parent d00e7c0 commit 376f9f5

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
3+
one = 1
4+
5+
6+
def mapsAndLists():
7+
# immutable can serve as dictionary keys
8+
key1 = "a"
9+
key2 = False
10+
key3 = 1.5
11+
12+
# Python "tuple" is "pair"/"round" scope
13+
_ = (1, 2, 3)
14+
15+
# Python "list" is "list" scope
16+
_ = [1, 2, 3]
17+
_ = [sys.path[0], sys.path[1]]
18+
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
19+
_ = [x for x in fruits if "a" in x]
20+
21+
# Python "set" is "list" scope
22+
_ = {1, 2, 3}
23+
24+
# Python "dictionary" is "map" scope
25+
d1 = {"a": 1, "b": 2, "c": 3}
26+
_ = {key1: 1, key2: 2, key3: 3}
27+
_ = {value: key for (key, value) in d1.items()}
28+
29+
# complex ones
30+
_ = [[1, 2, 3], 4, 5, 6]
31+
_ = [[1, 2, 3], {1, 2, 3}, {"a": 1, "b": 2, "c": 3}]
32+
_ = {{"a": 1, "b": 2, "c": 3}: 1, d1: 2}
33+
_ = {{1, 2, 3}: 1, {2, 3, 4}: 2}
34+
_ = ({1, 2, 3}, {1, 2, 3}, {1, 2, 3})

packages/cursorless-engine/src/languages/python.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ import { ternaryBranchMatcher } from "./ternaryBranchMatcher";
2323
export const getTypeNode = (node: SyntaxNode) =>
2424
node.children.find((child) => child.type === "type") ?? null;
2525

26-
const dictionaryTypes = ["dictionary", "dictionary_comprehension"];
27-
const listTypes = ["list", "list_comprehension", "set"];
28-
2926
function itemNodeFinder(
3027
parentType: string,
3128
childType: string,
@@ -48,8 +45,6 @@ function itemNodeFinder(
4845
const nodeMatchers: Partial<
4946
Record<SimpleScopeTypeType, NodeMatcherAlternative>
5047
> = {
51-
map: dictionaryTypes,
52-
list: listTypes,
5348
string: "string",
5449
collectionItem: cascadingMatcher(
5550
matcher(

queries/python.scm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727
(with_statement)
2828
] @statement
2929

30+
[
31+
(dictionary)
32+
(dictionary_comprehension)
33+
] @map
34+
35+
[
36+
(list)
37+
(list_comprehension)
38+
(set)
39+
] @list
40+
3041
(
3142
(function_definition
3243
name: (_) @functionName

0 commit comments

Comments
 (0)