1
- import { type Position , type PreferredScopeModifier } from "@cursorless/common" ;
1
+ import {
2
+ NoContainingScopeError ,
3
+ type Position ,
4
+ type PreferredScopeModifier ,
5
+ } from "@cursorless/common" ;
2
6
import type { Target } from "../../typings/target.types" ;
3
7
import type { ModifierStageFactory } from "../ModifierStageFactory" ;
4
8
import type { ModifierStage } from "../PipelineStages.types" ;
5
- import { getContainingScopeTarget } from "./getContainingScopeTarget " ;
9
+ import { ContainingScopeStage } from "./ContainingScopeStage " ;
6
10
import type { TargetScope } from "./scopeHandlers/scope.types" ;
7
11
import type { ScopeHandler } from "./scopeHandlers/scopeHandler.types" ;
8
12
import type { ScopeHandlerFactory } from "./scopeHandlers/ScopeHandlerFactory" ;
@@ -22,6 +26,21 @@ export class PreferredScopeStage implements ModifierStage {
22
26
run ( target : Target ) : Target [ ] {
23
27
const { scopeType } = this . modifier ;
24
28
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
+
25
44
const scopeHandler = this . scopeHandlerFactory . create (
26
45
this . modifier . scopeType ,
27
46
target . editor . document . languageId ,
@@ -31,17 +50,13 @@ export class PreferredScopeStage implements ModifierStage {
31
50
throw Error ( `Couldn't create scope handler for: ${ scopeType . type } ` ) ;
32
51
}
33
52
34
- const containingTargets = getContainingScopeTarget ( target , scopeHandler ) ;
35
- if ( containingTargets != null ) {
36
- return containingTargets ;
37
- }
38
-
39
53
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 } ` ) ;
42
57
}
43
58
44
- throw Error ( `No scopes found for scope type: ${ scopeType . type } ` ) ;
59
+ return closestTargets ;
45
60
}
46
61
}
47
62
0 commit comments