@@ -91,12 +91,14 @@ export function selectRight(doc: EditableDocument) {
91
91
}
92
92
93
93
export function selectForwardSexpOrUp ( doc : EditableDocument ) {
94
- const rangeFn =
95
- doc . selection . active >= doc . selection . anchor
96
- ? forwardSexpOrUpRange
97
- : ( doc : EditableDocument ) => forwardSexpOrUpRange ( doc , doc . selection . active , true ) ;
98
-
99
- selectRangeForward ( doc , rangeFn ( doc ) ) ;
94
+ const ranges = doc . selections . map ( ( selection ) => {
95
+ const rangeFn =
96
+ selection . active >= selection . anchor
97
+ ? ( doc ) => forwardSexpOrUpRange ( doc , selection . end )
98
+ : ( doc : EditableDocument ) => forwardSexpOrUpRange ( doc , selection . active , true ) ;
99
+ return rangeFn ( doc ) ;
100
+ } ) ;
101
+ selectRangeForward ( doc , ranges ) ;
100
102
}
101
103
102
104
export function selectBackwardSexp ( doc : EditableDocument ) {
@@ -147,11 +149,14 @@ export function selectBackwardUpSexp(doc: EditableDocument) {
147
149
}
148
150
149
151
export function selectBackwardSexpOrUp ( doc : EditableDocument ) {
150
- const rangeFn =
151
- doc . selection . active <= doc . selection . anchor
152
- ? ( doc : EditableDocument ) => backwardSexpOrUpRange ( doc , doc . selection . active , false )
153
- : ( doc : EditableDocument ) => backwardSexpOrUpRange ( doc , doc . selection . active , false ) ;
154
- selectRangeBackward ( doc , rangeFn ( doc ) ) ;
152
+ const ranges = doc . selections . map ( ( selection ) => {
153
+ const rangeFn =
154
+ selection . active <= selection . anchor
155
+ ? ( doc : EditableDocument ) => backwardSexpOrUpRange ( doc , selection . active , false )
156
+ : ( doc : EditableDocument ) => backwardSexpOrUpRange ( doc , selection . active , false ) ;
157
+ return rangeFn ( doc ) ;
158
+ } ) ;
159
+ selectRangeBackward ( doc , ranges ) ;
155
160
}
156
161
157
162
export function selectCloseList ( doc : EditableDocument ) {
@@ -197,92 +202,120 @@ enum GoUpSexpOption {
197
202
*/
198
203
function _forwardSexpRange (
199
204
doc : EditableDocument ,
200
- offset = Math . max ( doc . selection . anchor , doc . selection . active ) ,
205
+ offsets = doc . selections . map ( ( s ) => s . end ) ,
201
206
goUpSexp : GoUpSexpOption ,
202
207
goPastWhitespace = false
203
- ) : [ number , number ] {
204
- const cursor = doc . getTokenCursor ( offset ) ;
208
+ ) : Array < [ number , number ] > {
209
+ return offsets . map ( ( offset ) => {
210
+ const cursor = doc . getTokenCursor ( offset ) ;
205
211
206
- if ( goUpSexp == GoUpSexpOption . Never || goUpSexp == GoUpSexpOption . WhenAtLimit ) {
207
- // Normalize our position by scooting to the beginning of the closest sexp
208
- cursor . forwardWhitespace ( ) ;
212
+ if ( goUpSexp == GoUpSexpOption . Never || goUpSexp == GoUpSexpOption . WhenAtLimit ) {
213
+ // Normalize our position by scooting to the beginning of the closest sexp
214
+ cursor . forwardWhitespace ( ) ;
209
215
210
- if ( cursor . forwardSexp ( true , true ) ) {
211
- if ( goPastWhitespace ) {
212
- cursor . forwardWhitespace ( ) ;
216
+ if ( cursor . forwardSexp ( true , true ) ) {
217
+ if ( goPastWhitespace ) {
218
+ cursor . forwardWhitespace ( ) ;
219
+ }
220
+ return [ offset , cursor . offsetStart ] ;
213
221
}
214
- return [ offset , cursor . offsetStart ] ;
215
222
}
216
- }
217
223
218
- if ( goUpSexp == GoUpSexpOption . Required || goUpSexp == GoUpSexpOption . WhenAtLimit ) {
219
- cursor . forwardList ( ) ;
220
- if ( cursor . upList ( ) ) {
221
- if ( goPastWhitespace ) {
222
- cursor . forwardWhitespace ( ) ;
224
+ if ( goUpSexp == GoUpSexpOption . Required || goUpSexp == GoUpSexpOption . WhenAtLimit ) {
225
+ cursor . forwardList ( ) ;
226
+ if ( cursor . upList ( ) ) {
227
+ if ( goPastWhitespace ) {
228
+ cursor . forwardWhitespace ( ) ;
229
+ }
230
+ return [ offset , cursor . offsetStart ] ;
223
231
}
224
- return [ offset , cursor . offsetStart ] ;
225
232
}
226
- }
227
- return [ offset , offset ] ;
233
+ return [ offset , offset ] ;
234
+ } ) ;
228
235
}
229
236
230
237
/**
231
238
* Return a modified selection range on doc. Moves the left limit around sexps, potentially moving up.
232
239
*/
233
240
function _backwardSexpRange (
234
241
doc : EditableDocument ,
235
- offset : number = Math . min ( doc . selection . anchor , doc . selection . active ) ,
242
+ offsets : number [ ] = doc . selections . map ( ( s ) => s . start ) ,
236
243
goUpSexp : GoUpSexpOption ,
237
244
goPastWhitespace = false
238
- ) : [ number , number ] {
239
- const cursor = doc . getTokenCursor ( offset ) ;
240
-
241
- if ( goUpSexp == GoUpSexpOption . Never || goUpSexp == GoUpSexpOption . WhenAtLimit ) {
242
- if ( ! cursor . isWhiteSpace ( ) && cursor . offsetStart < offset ) {
243
- // This is because cursor.backwardSexp() can't move backwards when "on" the first sexp inside a list
244
- // TODO: Try to fix this in LispTokenCursor instead.
245
- cursor . forwardSexp ( ) ;
246
- }
247
- cursor . backwardWhitespace ( ) ;
245
+ ) : Array < [ number , number ] > {
246
+ return offsets . map ( ( offset ) => {
247
+ const cursor = doc . getTokenCursor ( offset ) ;
248
+
249
+ if ( goUpSexp == GoUpSexpOption . Never || goUpSexp == GoUpSexpOption . WhenAtLimit ) {
250
+ if ( ! cursor . isWhiteSpace ( ) && cursor . offsetStart < offset ) {
251
+ // This is because cursor.backwardSexp() can't move backwards when "on" the first sexp inside a list
252
+ // TODO: Try to fix this in LispTokenCursor instead.
253
+ cursor . forwardSexp ( ) ;
254
+ }
255
+ cursor . backwardWhitespace ( ) ;
248
256
249
- if ( cursor . backwardSexp ( true , true ) ) {
250
- if ( goPastWhitespace ) {
251
- cursor . backwardWhitespace ( ) ;
257
+ if ( cursor . backwardSexp ( true , true ) ) {
258
+ if ( goPastWhitespace ) {
259
+ cursor . backwardWhitespace ( ) ;
260
+ }
261
+ return [ cursor . offsetStart , offset ] ;
252
262
}
253
- return [ cursor . offsetStart , offset ] ;
254
263
}
255
- }
256
264
257
- if ( goUpSexp == GoUpSexpOption . Required || goUpSexp == GoUpSexpOption . WhenAtLimit ) {
258
- cursor . backwardList ( ) ;
259
- if ( cursor . backwardUpList ( ) ) {
260
- cursor . forwardSexp ( true , true ) ;
261
- cursor . backwardSexp ( true , true ) ;
262
- if ( goPastWhitespace ) {
263
- cursor . backwardWhitespace ( ) ;
265
+ if ( goUpSexp == GoUpSexpOption . Required || goUpSexp == GoUpSexpOption . WhenAtLimit ) {
266
+ cursor . backwardList ( ) ;
267
+ if ( cursor . backwardUpList ( ) ) {
268
+ cursor . forwardSexp ( true , true ) ;
269
+ cursor . backwardSexp ( true , true ) ;
270
+ if ( goPastWhitespace ) {
271
+ cursor . backwardWhitespace ( ) ;
272
+ }
273
+ return [ cursor . offsetStart , offset ] ;
264
274
}
265
- return [ cursor . offsetStart , offset ] ;
266
275
}
267
- }
268
276
269
- return [ offset , offset ] ;
277
+ return [ offset , offset ] ;
278
+ } ) ;
270
279
}
271
280
272
281
export function forwardSexpRange (
273
282
doc : EditableDocument ,
274
- offset = Math . max ( doc . selection . anchor , doc . selection . active ) ,
283
+ offsets ?: number [ ] ,
284
+ goPastWhitespace ?: boolean
285
+ ) : Array < [ number , number ] > ;
286
+ export function forwardSexpRange (
287
+ doc : EditableDocument ,
288
+ offset ?: number ,
289
+ goPastWhitespace ?: boolean
290
+ ) : [ number , number ] ;
291
+ export function forwardSexpRange (
292
+ doc : EditableDocument ,
293
+ oneOrMoreOffsets : number [ ] | number = doc . selections . map ( ( s ) => s . end ) ,
275
294
goPastWhitespace = false
276
- ) : [ number , number ] {
277
- return _forwardSexpRange ( doc , offset , GoUpSexpOption . Never , goPastWhitespace ) ;
295
+ ) : Array < [ number , number ] > | [ number , number ] {
296
+ const offsets = Array . isArray ( oneOrMoreOffsets ) ? oneOrMoreOffsets : [ oneOrMoreOffsets ] ;
297
+ const ranges = _forwardSexpRange ( doc , offsets , GoUpSexpOption . Never , goPastWhitespace ) ;
298
+ return Array . isArray ( oneOrMoreOffsets ) ? ranges : ranges [ 0 ] ;
278
299
}
279
300
280
301
export function backwardSexpRange (
281
302
doc : EditableDocument ,
282
- offset : number = Math . min ( doc . selection . anchor , doc . selection . active ) ,
303
+ offsets ?: number [ ] ,
304
+ goPastWhitespace ?: boolean
305
+ ) : Array < [ number , number ] > ;
306
+ export function backwardSexpRange (
307
+ doc : EditableDocument ,
308
+ offset ?: number ,
309
+ goPastWhitespace ?: boolean
310
+ ) : [ number , number ] ;
311
+ export function backwardSexpRange (
312
+ doc : EditableDocument ,
313
+ oneOrMoreOffsets : number [ ] | number = doc . selections . map ( ( s ) => s . start ) ,
283
314
goPastWhitespace = false
284
- ) : [ number , number ] {
285
- return _backwardSexpRange ( doc , offset , GoUpSexpOption . Never , goPastWhitespace ) ;
315
+ ) : Array < [ number , number ] > | [ number , number ] {
316
+ const offsets = Array . isArray ( oneOrMoreOffsets ) ? oneOrMoreOffsets : [ oneOrMoreOffsets ] ;
317
+ const ranges = _backwardSexpRange ( doc , offsets , GoUpSexpOption . Never , goPastWhitespace ) ;
318
+ return Array . isArray ( oneOrMoreOffsets ) ? ranges : ranges [ 0 ] ;
286
319
}
287
320
288
321
export function forwardListRange (
@@ -418,7 +451,7 @@ export function rangeToForwardUpList(
418
451
offset : number = doc . selections [ 0 ] . end ,
419
452
goPastWhitespace = false
420
453
) : [ number , number ] {
421
- return _forwardSexpRange ( doc , offset , GoUpSexpOption . Required , goPastWhitespace ) ;
454
+ return _forwardSexpRange ( doc , [ offset ] , GoUpSexpOption . Required , goPastWhitespace ) [ 0 ] ;
422
455
}
423
456
424
457
export function rangeToBackwardUpList (
@@ -427,23 +460,47 @@ export function rangeToBackwardUpList(
427
460
offset : number = doc . selections [ 0 ] . start ,
428
461
goPastWhitespace = false
429
462
) : [ number , number ] {
430
- return _backwardSexpRange ( doc , offset , GoUpSexpOption . Required , goPastWhitespace ) ;
463
+ return _backwardSexpRange ( doc , [ offset ] , GoUpSexpOption . Required , goPastWhitespace ) [ 0 ] ;
431
464
}
432
465
433
466
export function forwardSexpOrUpRange (
434
467
doc : EditableDocument ,
435
- offset = Math . max ( doc . selection . anchor , doc . selection . active ) ,
468
+ offsets ?: number [ ] ,
469
+ goPastWhitespace ?: boolean
470
+ ) : Array < [ number , number ] > ;
471
+ export function forwardSexpOrUpRange (
472
+ doc : EditableDocument ,
473
+ offset ?: number ,
474
+ goPastWhitespace ?: boolean
475
+ ) : [ number , number ] ;
476
+ export function forwardSexpOrUpRange (
477
+ doc : EditableDocument ,
478
+ oneOrMoreOffsets : number [ ] | number = doc . selections . map ( ( s ) => s . end ) ,
436
479
goPastWhitespace = false
437
- ) : [ number , number ] {
438
- return _forwardSexpRange ( doc , offset , GoUpSexpOption . WhenAtLimit , goPastWhitespace ) ;
480
+ ) : Array < [ number , number ] > | [ number , number ] {
481
+ const offsets = isNumber ( oneOrMoreOffsets ) ? [ oneOrMoreOffsets ] : oneOrMoreOffsets ;
482
+ const ranges = _forwardSexpRange ( doc , offsets , GoUpSexpOption . WhenAtLimit , goPastWhitespace ) ;
483
+ return isNumber ( oneOrMoreOffsets ) ? ranges [ 0 ] : ranges ;
439
484
}
440
485
441
486
export function backwardSexpOrUpRange (
442
487
doc : EditableDocument ,
443
- offset : number = Math . min ( doc . selection . anchor , doc . selection . active ) ,
488
+ offsets ?: number [ ] ,
489
+ goPastWhitespace ?: boolean
490
+ ) : Array < [ number , number ] > ;
491
+ export function backwardSexpOrUpRange (
492
+ doc : EditableDocument ,
493
+ offset ?: number ,
494
+ goPastWhitespace ?: boolean
495
+ ) : [ number , number ] ;
496
+ export function backwardSexpOrUpRange (
497
+ doc : EditableDocument ,
498
+ oneOrMoreOffsets : number [ ] | number = doc . selections . map ( ( s ) => s . start ) ,
444
499
goPastWhitespace = false
445
- ) : [ number , number ] {
446
- return _backwardSexpRange ( doc , offset , GoUpSexpOption . WhenAtLimit , goPastWhitespace ) ;
500
+ ) : Array < [ number , number ] > | [ number , number ] {
501
+ const offsets = isNumber ( oneOrMoreOffsets ) ? [ oneOrMoreOffsets ] : oneOrMoreOffsets ;
502
+ const ranges = _backwardSexpRange ( doc , offsets , GoUpSexpOption . WhenAtLimit , goPastWhitespace ) ;
503
+ return isNumber ( oneOrMoreOffsets ) ? ranges [ 0 ] : ranges ;
447
504
}
448
505
449
506
export function rangeToForwardDownList (
0 commit comments