Skip to content

Commit 0818816

Browse files
committed
Add "identifier" scope
1 parent f21c8f5 commit 0818816

File tree

9 files changed

+135
-3
lines changed

9 files changed

+135
-3
lines changed

cursorless-talon/src/modifiers/scopes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
# Text-based scope types
5858
"char": "character",
5959
"word": "word",
60+
"identifier": "identifier",
6061
"block": "paragraph",
6162
"cell": "notebookCell",
6263
"file": "document",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { NestedScopeHandler } from ".";
2+
import { getMatcher } from "../../../core/tokenizer";
3+
import type { ScopeType } from "../../../typings/targetDescriptor.types";
4+
import { getMatchesInRange } from "../../../util/regex";
5+
import { TokenTarget } from "../../targets";
6+
import type { TargetScope } from "./scope.types";
7+
8+
export default class IdentifierScopeHandler extends NestedScopeHandler {
9+
public readonly scopeType: ScopeType = { type: "identifier" };
10+
public readonly iterationScopeType: ScopeType = { type: "line" };
11+
12+
private regex: RegExp;
13+
14+
constructor(languageId: string) {
15+
super(languageId);
16+
this.regex = getMatcher(languageId).identifierMatcher;
17+
}
18+
19+
protected getScopesInIterationScope({
20+
editor,
21+
domain,
22+
}: TargetScope): TargetScope[] {
23+
return getMatchesInRange(this.regex, editor, domain).map((range) => ({
24+
editor,
25+
domain: range,
26+
getTarget: (isReversed) =>
27+
new TokenTarget({
28+
editor,
29+
contentRange: range,
30+
isReversed,
31+
}),
32+
}));
33+
}
34+
}

src/processTargets/modifiers/scopeHandlers/getScopeHandler.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LineScopeHandler, TokenScopeHandler } from ".";
1+
import { IdentifierScopeHandler, LineScopeHandler, TokenScopeHandler } from ".";
22
import type { ScopeType } from "../../../typings/targetDescriptor.types";
33
import type { ScopeHandler } from "./scopeHandler.types";
44

@@ -13,21 +13,23 @@ import type { ScopeHandler } from "./scopeHandler.types";
1313
* function.
1414
*
1515
* @param scopeType The scope type for which to get a scope handler
16-
* @param _languageId The language id of the document where the scope handler
16+
* @param languageId The language id of the document where the scope handler
1717
* will be used
1818
* @returns A scope handler for the given scope type and language id, or
1919
* undefined if the given scope type / language id combination is still using
2020
* legacy pathways
2121
*/
2222
export default function getScopeHandler(
2323
scopeType: ScopeType,
24-
_languageId?: string
24+
languageId?: string
2525
): ScopeHandler | undefined {
2626
switch (scopeType.type) {
2727
case "token":
2828
return new TokenScopeHandler();
2929
case "line":
3030
return new LineScopeHandler();
31+
case "identifier":
32+
return new IdentifierScopeHandler(languageId!);
3133
default:
3234
return undefined;
3335
}

src/processTargets/modifiers/scopeHandlers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export * from "./NestedScopeHandler";
22
export { default as NestedScopeHandler } from "./NestedScopeHandler";
33
export * from "./LineScopeHandler";
44
export { default as LineScopeHandler } from "./LineScopeHandler";
5+
export * from "./IdentifierScopeHandler";
6+
export { default as IdentifierScopeHandler } from "./IdentifierScopeHandler";
57
export * from "./TokenScopeHandler";
68
export { default as TokenScopeHandler } from "./TokenScopeHandler";
79
export * from "./getScopeHandler";
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
languageId: plaintext
2+
command:
3+
spokenForm: clear every identifier
4+
version: 3
5+
targets:
6+
- type: primitive
7+
modifiers:
8+
- type: everyScope
9+
scopeType: {type: identifier}
10+
usePrePhraseSnapshot: true
11+
action: {name: clearAndSetSelection}
12+
initialState:
13+
documentContents: aaa bbb.
14+
selections:
15+
- anchor: {line: 0, character: 0}
16+
active: {line: 0, character: 0}
17+
marks: {}
18+
finalState:
19+
documentContents: " ."
20+
selections:
21+
- anchor: {line: 0, character: 0}
22+
active: {line: 0, character: 0}
23+
- anchor: {line: 0, character: 1}
24+
active: {line: 0, character: 1}
25+
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: everyScope, scopeType: {type: identifier}}]}]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
languageId: plaintext
2+
command:
3+
spokenForm: clear identifier
4+
version: 3
5+
targets:
6+
- type: primitive
7+
modifiers:
8+
- type: containingScope
9+
scopeType: {type: identifier}
10+
usePrePhraseSnapshot: true
11+
action: {name: clearAndSetSelection}
12+
initialState:
13+
documentContents: foo.
14+
selections:
15+
- anchor: {line: 0, character: 3}
16+
active: {line: 0, character: 3}
17+
marks: {}
18+
finalState:
19+
documentContents: .
20+
selections:
21+
- anchor: {line: 0, character: 0}
22+
active: {line: 0, character: 0}
23+
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: identifier}}]}]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
languageId: plaintext
2+
command:
3+
spokenForm: clear identifier
4+
version: 3
5+
targets:
6+
- type: primitive
7+
modifiers:
8+
- type: containingScope
9+
scopeType: {type: identifier}
10+
usePrePhraseSnapshot: true
11+
action: {name: clearAndSetSelection}
12+
initialState:
13+
documentContents: .
14+
selections:
15+
- anchor: {line: 0, character: 0}
16+
active: {line: 0, character: 0}
17+
marks: {}
18+
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: containingScope, scopeType: {type: identifier}}]}]
19+
thrownError: {name: NoContainingScopeError}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
languageId: plaintext
2+
command:
3+
spokenForm: clear last identifier
4+
version: 3
5+
targets:
6+
- type: primitive
7+
modifiers:
8+
- type: ordinalScope
9+
scopeType: {type: identifier}
10+
start: -1
11+
length: 1
12+
usePrePhraseSnapshot: true
13+
action: {name: clearAndSetSelection}
14+
initialState:
15+
documentContents: aaa bbb.
16+
selections:
17+
- anchor: {line: 0, character: 0}
18+
active: {line: 0, character: 0}
19+
marks: {}
20+
finalState:
21+
documentContents: aaa .
22+
selections:
23+
- anchor: {line: 0, character: 4}
24+
active: {line: 0, character: 4}
25+
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: ordinalScope, scopeType: {type: identifier}, start: -1, length: 1}]}]

src/typings/targetDescriptor.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export type SimpleScopeTypeType =
128128
| "document"
129129
| "character"
130130
| "word"
131+
| "identifier"
131132
| "nonWhitespaceSequence"
132133
| "boundedNonWhitespaceSequence"
133134
| "url";

0 commit comments

Comments
 (0)