@@ -40,79 +40,37 @@ export class ContiguousScopeHandler extends BaseScopeHandler {
40
40
return this . scopeHandler . iterationScopeType ;
41
41
}
42
42
43
- generateScopeCandidates (
43
+ * generateScopeCandidates (
44
44
editor : TextEditor ,
45
45
position : Position ,
46
46
direction : Direction ,
47
47
_hints : ScopeIteratorRequirements ,
48
48
) : Iterable < TargetScope > {
49
- return direction === "backward"
50
- ? this . generateScopeCandidatesBackward ( editor , position )
51
- : this . generateScopeCandidatesForward ( editor , position ) ;
52
- }
53
-
54
- private * generateScopeCandidatesBackward (
55
- editor : TextEditor ,
56
- position : Position ,
57
- ) : Iterable < TargetScope > {
58
- let targetRangeForward = next (
49
+ let targetRangeOpposite = next (
59
50
generateTargetRangesInDirection (
60
51
this . scopeHandler ,
61
52
editor ,
62
53
position ,
63
- "forward" ,
54
+ direction === "forward" ? "backward" : "forward" ,
64
55
) ,
65
56
) ;
66
57
67
- const targetRangesBackwardIter = generateTargetRangesInDirection (
58
+ const targetRangesIter = generateTargetRangesInDirection (
68
59
this . scopeHandler ,
69
60
editor ,
70
61
position ,
71
- "backward" ,
62
+ direction ,
72
63
) ;
73
64
74
- for ( const targetRange of targetRangesBackwardIter ) {
65
+ for ( const targetRange of targetRangesIter ) {
75
66
if (
76
- targetRangeForward != null &&
77
- isAdjacent ( targetRange [ 1 ] , targetRangeForward [ 0 ] )
67
+ targetRangeOpposite != null &&
68
+ isAdjacent ( targetRangeOpposite . proximal , targetRange . proximal )
78
69
) {
79
- yield targetsToScope ( targetRange [ 0 ] , targetRangeForward [ 1 ] ) ;
80
- targetRangeForward = undefined ;
70
+ yield targetsToScope ( targetRangeOpposite . distal , targetRange . distal ) ;
71
+ targetRangeOpposite = undefined ;
81
72
} else {
82
- yield targetsToScope ( ...targetRange ) ;
83
- }
84
- }
85
- }
86
-
87
- private * generateScopeCandidatesForward (
88
- editor : TextEditor ,
89
- position : Position ,
90
- ) : Iterable < TargetScope > {
91
- let targetRangeBackward = next (
92
- generateTargetRangesInDirection (
93
- this . scopeHandler ,
94
- editor ,
95
- position ,
96
- "backward" ,
97
- ) ,
98
- ) ;
99
-
100
- const targetRangesForwardIter = generateTargetRangesInDirection (
101
- this . scopeHandler ,
102
- editor ,
103
- position ,
104
- "forward" ,
105
- ) ;
106
-
107
- for ( const targetRange of targetRangesForwardIter ) {
108
- if (
109
- targetRangeBackward != null &&
110
- isAdjacent ( targetRangeBackward [ 1 ] , targetRange [ 0 ] )
111
- ) {
112
- yield targetsToScope ( targetRangeBackward [ 0 ] , targetRange [ 1 ] ) ;
113
- targetRangeBackward = undefined ;
114
- } else {
115
- yield targetsToScope ( ...targetRange ) ;
73
+ yield targetsToScope ( targetRange . proximal , targetRange . distal ) ;
116
74
}
117
75
}
118
76
}
@@ -143,9 +101,8 @@ function* generateTargetRangesInDirection(
143
101
editor : TextEditor ,
144
102
position : Position ,
145
103
direction : Direction ,
146
- ) : Iterable < [ Target , Target ] > {
147
- const isForward = direction === "forward" ;
148
- let first , last : Target | undefined ;
104
+ ) : Iterable < { proximal : Target ; distal : Target } > {
105
+ let proximal , distal : Target | undefined ;
149
106
150
107
const generator = scopeHandler . generateScopes ( editor , position , direction , {
151
108
allowAdjacentScopes : true ,
@@ -154,37 +111,37 @@ function* generateTargetRangesInDirection(
154
111
155
112
for ( const scope of generator ) {
156
113
for ( const target of scope . getTargets ( false ) ) {
157
- if ( first == null ) {
158
- first = target ;
114
+ if ( proximal == null ) {
115
+ proximal = target ;
159
116
}
160
117
161
- if ( last != null ) {
162
- const [ leadingTarget , trailingTarget ] = isForward
163
- ? [ last , target ]
164
- : [ target , last ] ;
165
-
166
- if ( ! isAdjacent ( leadingTarget , trailingTarget ) ) {
167
- yield isForward ? [ first , last ] : [ last , first ] ;
168
- first = target ;
118
+ if ( distal != null ) {
119
+ if ( ! isAdjacent ( distal , target ) ) {
120
+ yield { proximal, distal } ;
121
+ proximal = target ;
169
122
}
170
123
}
171
124
172
- last = target ;
125
+ distal = target ;
173
126
}
174
127
}
175
128
176
- if ( first != null && last != null ) {
177
- yield isForward ? [ first , last ] : [ last , first ] ;
129
+ if ( proximal != null && distal != null ) {
130
+ yield { proximal , distal } ;
178
131
}
179
132
}
180
133
181
- function isAdjacent ( leadingTarget : Target , trailingTarget : Target ) : boolean {
182
- if (
183
- leadingTarget . contentRange . intersection ( trailingTarget . contentRange ) != null
184
- ) {
134
+ function isAdjacent ( target1 : Target , target2 : Target ) : boolean {
135
+ if ( target1 . contentRange . isRangeEqual ( target2 . contentRange ) ) {
185
136
return true ;
186
137
}
187
138
139
+ const [ leadingTarget , trailingTarget ] = target1 . contentRange . start . isBefore (
140
+ target2 . contentRange . start ,
141
+ )
142
+ ? [ target1 , target2 ]
143
+ : [ target2 , target1 ] ;
144
+
188
145
const leadingRange =
189
146
leadingTarget . getTrailingDelimiterTarget ( ) ?. contentRange ??
190
147
leadingTarget . contentRange ;
0 commit comments