8
8
next ,
9
9
} from "@cursorless/common" ;
10
10
import { ScopeHandlerFactory } from "." ;
11
- import type { Target } from "../../../typings/target.types " ;
11
+ import { createContinuousRangeTarget } from "../../createContinuousRangeTarget " ;
12
12
import { BaseScopeHandler } from "./BaseScopeHandler" ;
13
13
import type { TargetScope } from "./scope.types" ;
14
14
import type {
@@ -67,32 +67,32 @@ export class ContiguousScopeHandler extends BaseScopeHandler {
67
67
targetRangeOpposite != null &&
68
68
isAdjacent ( targetRangeOpposite . proximal , targetRange . proximal )
69
69
) {
70
- yield targetsToScope ( targetRangeOpposite . distal , targetRange . distal ) ;
70
+ yield combineScopes ( targetRangeOpposite . distal , targetRange . distal ) ;
71
71
targetRangeOpposite = undefined ;
72
72
} else {
73
- yield targetsToScope ( targetRange . proximal , targetRange . distal ) ;
73
+ yield combineScopes ( targetRange . proximal , targetRange . distal ) ;
74
74
}
75
75
}
76
76
}
77
77
}
78
78
79
- function targetsToScope (
80
- leadingTarget : Target ,
81
- trailingTarget : Target ,
82
- ) : TargetScope {
83
- if ( leadingTarget . contentRange . isRangeEqual ( trailingTarget . contentRange ) ) {
84
- return {
85
- editor : leadingTarget . editor ,
86
- domain : leadingTarget . contentRange ,
87
- getTargets : ( ) => [ leadingTarget ] ,
88
- } ;
79
+ function combineScopes ( scope1 : TargetScope , scope2 : TargetScope ) : TargetScope {
80
+ if ( scope1 . domain . isRangeEqual ( scope2 . domain ) ) {
81
+ return scope1 ;
89
82
}
90
83
91
- const range = leadingTarget . contentRange . union ( trailingTarget . contentRange ) ;
92
84
return {
93
- editor : leadingTarget . editor ,
94
- domain : range ,
95
- getTargets : ( ) => [ leadingTarget . withContentRange ( range ) ] ,
85
+ editor : scope1 . editor ,
86
+ domain : scope1 . domain . union ( scope2 . domain ) ,
87
+ getTargets : ( isReversed ) => [
88
+ createContinuousRangeTarget (
89
+ isReversed ,
90
+ scope1 . getTargets ( false ) [ 0 ] ,
91
+ scope2 . getTargets ( false ) [ 0 ] ,
92
+ true ,
93
+ true ,
94
+ ) ,
95
+ ] ,
96
96
} ;
97
97
}
98
98
@@ -101,41 +101,42 @@ function* generateTargetRangesInDirection(
101
101
editor : TextEditor ,
102
102
position : Position ,
103
103
direction : Direction ,
104
- ) : Iterable < { proximal : Target ; distal : Target } > {
105
- let proximal , distal : Target | undefined ;
104
+ ) : Iterable < { proximal : TargetScope ; distal : TargetScope } > {
105
+ let proximal , distal : TargetScope | undefined ;
106
106
107
107
const generator = scopeHandler . generateScopes ( editor , position , direction , {
108
108
allowAdjacentScopes : true ,
109
109
skipAncestorScopes : true ,
110
110
} ) ;
111
111
112
112
for ( const scope of generator ) {
113
- for ( const target of scope . getTargets ( false ) ) {
114
- if ( proximal == null ) {
115
- proximal = target ;
116
- }
113
+ if ( proximal == null ) {
114
+ proximal = scope ;
115
+ }
117
116
118
- if ( distal != null ) {
119
- if ( ! isAdjacent ( distal , target ) ) {
120
- yield { proximal, distal } ;
121
- proximal = target ;
122
- }
117
+ if ( distal != null ) {
118
+ if ( ! isAdjacent ( distal , scope ) ) {
119
+ yield { proximal, distal } ;
120
+ proximal = scope ;
123
121
}
124
-
125
- distal = target ;
126
122
}
123
+
124
+ distal = scope ;
127
125
}
128
126
129
127
if ( proximal != null && distal != null ) {
130
128
yield { proximal, distal } ;
131
129
}
132
130
}
133
131
134
- function isAdjacent ( target1 : Target , target2 : Target ) : boolean {
135
- if ( target1 . contentRange . isRangeEqual ( target2 . contentRange ) ) {
132
+ function isAdjacent ( scope1 : TargetScope , scope2 : TargetScope ) : boolean {
133
+ if ( scope1 . domain . isRangeEqual ( scope2 . domain ) ) {
136
134
return true ;
137
135
}
138
136
137
+ const target1 = scope1 . getTargets ( false ) [ 0 ] ;
138
+ const target2 = scope2 . getTargets ( false ) [ 0 ] ;
139
+
139
140
const [ leadingTarget , trailingTarget ] = target1 . contentRange . start . isBefore (
140
141
target2 . contentRange . start ,
141
142
)
0 commit comments