Skip to content

Commit 8cf3464

Browse files
Add item scope to JS (#2675)
```js const value = [1, 2]; ``` This example is problematic. Currently we test the language specific implementation before the text based one. This means that if your cursor is inside the curly brackets `item` is `value = [1, 2]`. I don't think this can be solved until we have a better `oneOf` implementation of collection item. Same problem with Java #2674 To fix that above I made a change so that the text based item can be used even if there is a syntax tree one available if it's smaller. This needs to merge before the java pr. ## 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 d6cc6f9 commit 8cf3464

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

data/fixtures/recorded/languages/typescript/takeItem.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ finalState:
2323
2424
const value = { a: 1, b: 2, c: 3 };
2525
selections:
26-
- anchor: {line: 1, character: 0}
27-
active: {line: 1, character: 35}
26+
- anchor: {line: 1, character: 6}
27+
active: {line: 1, character: 34}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
let foo, bar;
2+
---
3+
4+
[#1 Content] =
5+
[#1 Domain] = 0:4-0:7
6+
>---<
7+
0| let foo, bar;
8+
9+
[#1 Removal] = 0:4-0:9
10+
>-----<
11+
0| let foo, bar;
12+
13+
[#1 Trailing delimiter] = 0:7-0:9
14+
>--<
15+
0| let foo, bar;
16+
17+
[#1 Insertion delimiter] = ", "
18+
19+
20+
[#2 Content] =
21+
[#2 Domain] = 0:9-0:12
22+
>---<
23+
0| let foo, bar;
24+
25+
[#2 Removal] = 0:7-0:12
26+
>-----<
27+
0| let foo, bar;
28+
29+
[#2 Leading delimiter] = 0:7-0:9
30+
>--<
31+
0| let foo, bar;
32+
33+
[#2 Insertion delimiter] = ", "

packages/common/src/scopeSupportFacets/javascript.ts

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ export const javascriptCoreScopeSupport: LanguageScopeSupportFacetMap = {
112112
"value.return": supported,
113113
"value.return.lambda": supported,
114114
"value.field": supported,
115+
116+
"collectionItem.unenclosed": supported,
115117
};
116118

117119
export const javascriptJsxScopeSupport: LanguageScopeSupportFacetMap = {

packages/cursorless-engine/src/processTargets/modifiers/ContainingScopeStage.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,39 @@ export class ContainingScopeStage implements ModifierStage {
4545
.run(target);
4646
}
4747

48-
const containingScope = getContainingScopeTarget(
48+
const containingScopes = getContainingScopeTarget(
4949
target,
5050
scopeHandler,
5151
ancestorIndex,
5252
);
53-
54-
if (containingScope == null) {
55-
if (scopeType.type === "collectionItem") {
56-
// For `collectionItem`, fall back to generic implementation
57-
return this.modifierStageFactory
53+
if (scopeType.type === "collectionItem") {
54+
// For `collectionItem`, combine with generic implementation
55+
try {
56+
const legacyScopes = this.modifierStageFactory
5857
.getLegacyScopeStage(this.modifier)
5958
.run(target);
59+
if (containingScopes == null) {
60+
return legacyScopes;
61+
}
62+
if (containingScopes.length === 1 && legacyScopes.length === 1) {
63+
const containingRange = containingScopes[0].contentRange;
64+
const legacyRange = legacyScopes[0].contentRange;
65+
if (
66+
containingRange.contains(legacyRange) &&
67+
!containingRange.isRangeEqual(legacyRange)
68+
) {
69+
return legacyScopes;
70+
}
71+
}
72+
} catch (_ex) {
73+
// Do nothing
6074
}
75+
}
6176

77+
if (containingScopes == null) {
6278
throw new NoContainingScopeError(this.modifier.scopeType.type);
6379
}
6480

65-
return containingScope;
81+
return containingScopes;
6682
}
6783
}

queries/javascript.core.scm

+13
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,19 @@
272272
(#has-multiple-children-of-type? @_dummy variable_declarator)
273273
)
274274

275+
;;!! let foo, bar;
276+
;;! ^^^ ^^^
277+
(
278+
(lexical_declaration
279+
(variable_declarator)? @_.leading.endOf
280+
.
281+
(variable_declarator) @collectionItem
282+
.
283+
(variable_declarator)? @_.trailing.startOf
284+
)
285+
(#insertion-delimiter! @collectionItem ", ")
286+
)
287+
275288
(expression_statement
276289
[
277290
;; name:

0 commit comments

Comments
 (0)