Skip to content

Commit

Permalink
Add item scope to JS (#2675)
Browse files Browse the repository at this point in the history
```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
  • Loading branch information
AndreasArvidsson authored Nov 18, 2024
1 parent d6cc6f9 commit 8cf3464
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 9 deletions.
4 changes: 2 additions & 2 deletions data/fixtures/recorded/languages/typescript/takeItem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ finalState:
const value = { a: 1, b: 2, c: 3 };
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 35}
- anchor: {line: 1, character: 6}
active: {line: 1, character: 34}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
let foo, bar;
---

[#1 Content] =
[#1 Domain] = 0:4-0:7
>---<
0| let foo, bar;

[#1 Removal] = 0:4-0:9
>-----<
0| let foo, bar;

[#1 Trailing delimiter] = 0:7-0:9
>--<
0| let foo, bar;

[#1 Insertion delimiter] = ", "


[#2 Content] =
[#2 Domain] = 0:9-0:12
>---<
0| let foo, bar;

[#2 Removal] = 0:7-0:12
>-----<
0| let foo, bar;

[#2 Leading delimiter] = 0:7-0:9
>--<
0| let foo, bar;

[#2 Insertion delimiter] = ", "
2 changes: 2 additions & 0 deletions packages/common/src/scopeSupportFacets/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export const javascriptCoreScopeSupport: LanguageScopeSupportFacetMap = {
"value.return": supported,
"value.return.lambda": supported,
"value.field": supported,

"collectionItem.unenclosed": supported,
};

export const javascriptJsxScopeSupport: LanguageScopeSupportFacetMap = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,39 @@ export class ContainingScopeStage implements ModifierStage {
.run(target);
}

const containingScope = getContainingScopeTarget(
const containingScopes = getContainingScopeTarget(
target,
scopeHandler,
ancestorIndex,
);

if (containingScope == null) {
if (scopeType.type === "collectionItem") {
// For `collectionItem`, fall back to generic implementation
return this.modifierStageFactory
if (scopeType.type === "collectionItem") {
// For `collectionItem`, combine with generic implementation
try {
const legacyScopes = this.modifierStageFactory
.getLegacyScopeStage(this.modifier)
.run(target);
if (containingScopes == null) {
return legacyScopes;
}
if (containingScopes.length === 1 && legacyScopes.length === 1) {
const containingRange = containingScopes[0].contentRange;
const legacyRange = legacyScopes[0].contentRange;
if (
containingRange.contains(legacyRange) &&
!containingRange.isRangeEqual(legacyRange)
) {
return legacyScopes;
}
}
} catch (_ex) {
// Do nothing
}
}

if (containingScopes == null) {
throw new NoContainingScopeError(this.modifier.scopeType.type);
}

return containingScope;
return containingScopes;
}
}
13 changes: 13 additions & 0 deletions queries/javascript.core.scm
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,19 @@
(#has-multiple-children-of-type? @_dummy variable_declarator)
)

;;!! let foo, bar;
;;! ^^^ ^^^
(
(lexical_declaration
(variable_declarator)? @_.leading.endOf
.
(variable_declarator) @collectionItem
.
(variable_declarator)? @_.trailing.startOf
)
(#insertion-delimiter! @collectionItem ", ")
)

(expression_statement
[
;; name:
Expand Down

0 comments on commit 8cf3464

Please sign in to comment.