Skip to content

Commit a6d5556

Browse files
Use containing scope stage in preferred scope stage (#2639)
ContainingScopeStage have some logic around how to treat legacy scopes that we were missing from PreferredScopeStage. ## 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 --------- Co-authored-by: Phil Cohen <[email protected]>
1 parent ab208a3 commit a6d5556

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
languageId: plaintext
2+
command:
3+
version: 7
4+
spokenForm: clear item
5+
action:
6+
name: clearAndSetSelection
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: preferredScope
11+
scopeType: {type: collectionItem}
12+
usePrePhraseSnapshot: true
13+
spokenFormError: Modifier 'preferredScope'
14+
initialState:
15+
documentContents: foo, bar
16+
selections:
17+
- anchor: {line: 0, character: 0}
18+
active: {line: 0, character: 0}
19+
marks: {}
20+
finalState:
21+
documentContents: ", bar"
22+
selections:
23+
- anchor: {line: 0, character: 0}
24+
active: {line: 0, character: 0}

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { type Position, type PreferredScopeModifier } from "@cursorless/common";
1+
import {
2+
NoContainingScopeError,
3+
type Position,
4+
type PreferredScopeModifier,
5+
} from "@cursorless/common";
26
import type { Target } from "../../typings/target.types";
37
import type { ModifierStageFactory } from "../ModifierStageFactory";
48
import type { ModifierStage } from "../PipelineStages.types";
5-
import { getContainingScopeTarget } from "./getContainingScopeTarget";
9+
import { ContainingScopeStage } from "./ContainingScopeStage";
610
import type { TargetScope } from "./scopeHandlers/scope.types";
711
import type { ScopeHandler } from "./scopeHandlers/scopeHandler.types";
812
import type { ScopeHandlerFactory } from "./scopeHandlers/ScopeHandlerFactory";
@@ -22,6 +26,21 @@ export class PreferredScopeStage implements ModifierStage {
2226
run(target: Target): Target[] {
2327
const { scopeType } = this.modifier;
2428

29+
const containingScopeStage = new ContainingScopeStage(
30+
this.modifierStageFactory,
31+
this.scopeHandlerFactory,
32+
{ type: "containingScope", scopeType },
33+
);
34+
35+
try {
36+
return containingScopeStage.run(target);
37+
} catch (ex) {
38+
// NoContainingScopeError is thrown if no containing scope was found, which is fine.
39+
if (!(ex instanceof NoContainingScopeError)) {
40+
throw ex;
41+
}
42+
}
43+
2544
const scopeHandler = this.scopeHandlerFactory.create(
2645
this.modifier.scopeType,
2746
target.editor.document.languageId,
@@ -31,17 +50,13 @@ export class PreferredScopeStage implements ModifierStage {
3150
throw Error(`Couldn't create scope handler for: ${scopeType.type}`);
3251
}
3352

34-
const containingTargets = getContainingScopeTarget(target, scopeHandler);
35-
if (containingTargets != null) {
36-
return containingTargets;
37-
}
38-
3953
const closestTargets = getClosestScopeTargets(target, scopeHandler);
40-
if (closestTargets != null) {
41-
return closestTargets;
54+
55+
if (closestTargets == null) {
56+
throw Error(`No scopes found for scope type: ${scopeType.type}`);
4257
}
4358

44-
throw Error(`No scopes found for scope type: ${scopeType.type}`);
59+
return closestTargets;
4560
}
4661
}
4762

0 commit comments

Comments
 (0)