Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added grand scope #2130

Merged
merged 8 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/2023-12.addedGrandScopeModifier.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
tags: [enhancement]
pullRequest: 2130
---

- Added `grand` modifier. This modifier will take the containing grandparent scope. eg `"take grand statement air"`
11 changes: 11 additions & 0 deletions cursorless-talon/src/cheatsheet/sections/modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def get_modifiers():
"extendThroughStartOf",
"extendThroughEndOf",
"every",
"ancestor",
"first",
"last",
"previous",
Expand Down Expand Up @@ -101,6 +102,16 @@ def get_modifiers():
},
],
},
{
"id": "ancestor",
"type": "modifier",
"variations": [
{
"spokenForm": f"{complex_modifiers['ancestor']} <scope>",
"description": "Grandparent containing instance of <scope>",
},
],
},
{
"id": "relativeScope",
"type": "modifier",
Expand Down
18 changes: 17 additions & 1 deletion cursorless-talon/src/modifiers/simple_scope_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,23 @@
)
def cursorless_simple_scope_modifier(m) -> dict[str, Any]:
"""Containing scope, every scope, etc"""
if hasattr(m, "cursorless_simple_scope_modifier"):
modifier = m.cursorless_simple_scope_modifier

if modifier == "every":
return {
"type": "everyScope",
"scopeType": m.cursorless_scope_type,
}

if modifier == "ancestor":
return {
"type": "containingScope",
"scopeType": m.cursorless_scope_type,
"ancestorIndex": 1,
}

return {
"type": "everyScope" if m[0] == "every" else "containingScope",
"type": "containingScope",
"scopeType": m.cursorless_scope_type,
}
2 changes: 1 addition & 1 deletion cursorless-talon/src/spoken_forms.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"its": "inferPreviousMark",
"visible": "visible"
},
"simple_scope_modifier": { "every": "every" },
"simple_scope_modifier": { "every": "every", "grand": "ancestor" },
"interior_modifier": {
"inside": "interiorOnly"
},
Expand Down
9 changes: 9 additions & 0 deletions docs/user/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ The command `"every"` can be used to select a syntactic element and all of its m

For example, the command `take every key [blue] air` will select every key in the map/object/dict including the token with a blue hat over the letter 'a'.

##### `"grand"`

The command `"grand"` can be used to select the grand parent of the containing syntactic element.

- `"take grand statement air"`
- `"take grand funk air"`

For example, the command `take grand statement [blue] air` will select that parent statement of the statement containing the token with a blue hat over the letter 'a'.

##### Sub-token modifiers

###### `"sub"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,18 @@ export class PrimitiveTargetSpokenFormGenerator {
throw new NoSpokenFormError(`Modifier '${modifier.type}'`);

case "containingScope":
return [this.handleScopeType(modifier.scopeType)];
if (modifier.ancestorIndex == null || modifier.ancestorIndex === 0) {
return this.handleScopeType(modifier.scopeType);
}
if (modifier.ancestorIndex === 1) {
return [
this.spokenFormMap.modifierExtra.ancestor,
this.handleScopeType(modifier.scopeType),
];
}
throw new NoSpokenFormError(
`Modifier '${modifier.type}' with ancestor index ${modifier.ancestorIndex}`,
);

case "everyScope":
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export function getContainingScopeTarget(
editor,
scope.domain.end,
"forward",
ancestorIndex - 1,
ancestorIndex,
true,
);
}

Expand Down Expand Up @@ -86,10 +87,12 @@ function expandFromPosition(
position: Position,
direction: Direction,
ancestorIndex: number,
allowAdjacentScopes: boolean = false,
): TargetScope | undefined {
let nextAncestorIndex = 0;
for (const scope of scopeHandler.generateScopes(editor, position, direction, {
containment: "required",
allowAdjacentScopes,
})) {
if (nextAncestorIndex === ancestorIndex) {
return scope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type ModifierExtra =
| "previous"
| "next"
| "forward"
| "backward";
| "backward"
| "ancestor";

export type SpokenFormType = keyof SpokenFormMapKeyTypes;
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export const defaultSpokenFormMapCore: DefaultSpokenFormMapDefinition = {
next: "next",
forward: "forward",
backward: "backward",
ancestor: "grand",
},

customRegex: {},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: javascript
command:
version: 6
spokenForm: change grand state
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: statement}
ancestorIndex: 1
usePrePhraseSnapshot: true
initialState:
documentContents: |-
function myFunk() {
const value = 2
}
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
marks: {}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
languageId: python
command:
version: 6
spokenForm: change grand state
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: statement}
ancestorIndex: 1
usePrePhraseSnapshot: true
initialState:
documentContents: |-
def my_funk():
value = 2
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
marks: {}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
languageId: javascript
command:
version: 6
spokenForm: change grand state
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: statement}
ancestorIndex: 1
usePrePhraseSnapshot: true
initialState:
documentContents: |-
class MyClass {
myFunk() {

}
}
selections:
- anchor: {line: 2, character: 8}
active: {line: 2, character: 8}
marks: {}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}