Skip to content

Commit fff171f

Browse files
Added iteration scope for js statements (#2144)
## Checklist - [x] 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
1 parent 86266ce commit fff171f

File tree

7 files changed

+95
-10
lines changed

7 files changed

+95
-10
lines changed

packages/common/src/scopeSupportFacets/javascript.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ const { supported, notApplicable } = ScopeSupportFacetLevel;
1010
export const javascriptScopeSupport: LanguageScopeSupportFacetMap = {
1111
list: supported,
1212
map: supported,
13-
statement: supported,
1413
ifStatement: supported,
1514
regularExpression: supported,
1615
switchStatementSubject: supported,
1716
fieldAccess: supported,
1817

18+
statement: supported,
19+
"statement.iteration.document": supported,
20+
"statement.iteration.block": supported,
21+
1922
class: supported,
2023
className: supported,
2124

packages/common/src/scopeSupportFacets/scopeSupportFacetInfos.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ export const scopeSupportFacetInfos: Record<
4343
description: "A map/dictionary",
4444
scopeType: "map",
4545
},
46-
statement: {
47-
description: "A statement, eg assignment, for loop, etc",
48-
scopeType: "statement",
49-
},
5046
ifStatement: {
5147
description: "An if statement",
5248
scopeType: "ifStatement",
@@ -64,6 +60,22 @@ export const scopeSupportFacetInfos: Record<
6460
scopeType: "private.fieldAccess",
6561
},
6662

63+
statement: {
64+
description: "A statement, eg assignment, for loop, etc",
65+
scopeType: "statement",
66+
},
67+
"statement.iteration.document": {
68+
description: "Iteration scope for statements. The entire document.",
69+
scopeType: "statement",
70+
isIteration: true,
71+
},
72+
"statement.iteration.block": {
73+
description:
74+
"Iteration scope for statements. Statement blocks(body of functions/if statements/for loops/etc).",
75+
scopeType: "statement",
76+
isIteration: true,
77+
},
78+
6779
class: {
6880
description: "A class in an object-oriented language",
6981
scopeType: "class",

packages/common/src/scopeSupportFacets/scopeSupportFacets.types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ const scopeSupportFacets = [
1111

1212
"list",
1313
"map",
14-
"statement",
1514
"ifStatement",
1615
"regularExpression",
1716
"switchStatementSubject",
1817
"fieldAccess",
1918

19+
"statement",
20+
"statement.iteration.document",
21+
"statement.iteration.block",
22+
2023
"class",
2124
"className",
2225
"namedFunction",

packages/common/src/testUtil/getFixturePaths.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ export function getRecordedTestPaths() {
3232
return walkFilesSync(directory)
3333
.filter((p) => p.endsWith(".yml") || p.endsWith(".yaml"))
3434
.map((p) => ({
35-
name: path
36-
.relative(relativeDir, p.substring(0, p.lastIndexOf(".")))
37-
.replaceAll("\\", "/"),
3835
path: p,
36+
name: pathToName(relativeDir, p),
3937
}));
4038
}
4139

@@ -47,8 +45,14 @@ export function getScopeTestPaths() {
4745
.filter((p) => p.endsWith(".scope"))
4846
.map((p) => ({
4947
path: p,
50-
name: path.relative(relativeDir, p.substring(0, p.lastIndexOf("."))),
48+
name: pathToName(relativeDir, p),
5149
languageId: path.dirname(path.relative(directory, p)).split(path.sep)[0],
5250
facet: path.basename(p).match(/([a-zA-Z.]+)\d*\.scope/)![1],
5351
}));
5452
}
53+
54+
function pathToName(relativeDir: string, filePath: string) {
55+
return path
56+
.relative(relativeDir, filePath.substring(0, filePath.lastIndexOf(".")))
57+
.replaceAll("\\", "/");
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function foo() {
2+
3+
}
4+
---
5+
6+
[#1 Range] =
7+
[#1 Domain] = 0:0-2:1
8+
0| function foo() {
9+
>----------------
10+
1|
11+
12+
2| }
13+
-<
14+
15+
16+
[#2 Range] =
17+
[#2 Domain] = 0:16-2:0
18+
0| function foo() {
19+
>
20+
1|
21+
22+
2| }
23+
<
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Start
2+
3+
function foo() {
4+
5+
}
6+
---
7+
8+
[#1 Range] =
9+
[#1 Domain] = 0:0-4:1
10+
0| // Start
11+
>--------
12+
1|
13+
14+
2| function foo() {
15+
----------------
16+
3|
17+
18+
4| }
19+
-<
20+
21+
22+
[#2 Range] =
23+
[#2 Domain] = 2:16-4:0
24+
0| // Start
25+
26+
1|
27+
28+
2| function foo() {
29+
>
30+
3|
31+
32+
4| }
33+
<

queries/javascript.core.scm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,13 @@
680680
(#not-parent-type? @statement export_statement)
681681
)
682682

683+
(program) @statement.iteration
684+
685+
(statement_block
686+
"{" @statement.iteration.start.endOf
687+
"}" @statement.iteration.end.startOf
688+
)
689+
683690
;;!! foo(name: string) {}
684691
;;! ^^^^^^^^^^^^
685692
(

0 commit comments

Comments
 (0)