@@ -24,6 +24,8 @@ import {
2424 SelectionWithEditor ,
2525 Target ,
2626 TypedSelection ,
27+ Position as TargetPosition ,
28+ InsideOutsideType ,
2729} from "./Types" ;
2830
2931export default function processTargets (
@@ -199,19 +201,15 @@ function getSelectionsFromMark(
199201 return context . sourceMark ;
200202
201203 case "cursorToken" : {
202- const tokens = context . currentSelections . map ( ( selection ) => {
203- const token = context . navigationMap . getTokenForRange (
204- selection . selection
205- ) ;
206- if ( token == null ) {
207- throw new Error ( "Couldn't find mark under cursor" ) ;
204+ const tokenSelections = context . currentSelections . map ( ( selection ) => {
205+ const tokenSelection =
206+ context . navigationMap . getTokenSelectionForSelection ( selection ) ;
207+ if ( tokenSelection == null ) {
208+ throw new Error ( "Couldn't find token in selection" ) ;
208209 }
209- return token ;
210+ return tokenSelection ;
210211 } ) ;
211- return tokens . map ( ( token ) => ( {
212- selection : new Selection ( token . range . start , token . range . end ) ,
213- editor : token . editor ,
214- } ) ) ;
212+ return tokenSelections ;
215213 }
216214
217215 case "decoratedSymbol" :
@@ -334,6 +332,15 @@ function transformSelection(
334332 const activeIndex =
335333 modifier . active < 0 ? modifier . active + pieces . length : modifier . active ;
336334
335+ if (
336+ anchorIndex < 0 ||
337+ activeIndex < 0 ||
338+ anchorIndex >= pieces . length ||
339+ activeIndex >= pieces . length
340+ ) {
341+ throw new Error ( "Subtoken index out of range" ) ;
342+ }
343+
337344 const isReversed = activeIndex < anchorIndex ;
338345
339346 const anchor = selection . selection . start . translate (
@@ -461,6 +468,8 @@ function createTypedSelection(
461468 selectionContext : getTokenSelectionContext (
462469 selection ,
463470 modifier ,
471+ position ,
472+ insideOutsideType ,
464473 selectionContext
465474 ) ,
466475 } ;
@@ -600,6 +609,8 @@ function performPositionAdjustment(
600609function getTokenSelectionContext (
601610 selection : SelectionWithEditor ,
602611 modifier : Modifier ,
612+ position : TargetPosition ,
613+ insideOutsideType : InsideOutsideType ,
603614 selectionContext : SelectionContext
604615) : SelectionContext {
605616 if ( ! isSelectionContextEmpty ( selectionContext ) ) {
@@ -611,32 +622,38 @@ function getTokenSelectionContext(
611622
612623 const document = selection . editor . document ;
613624 const { start, end } = selection . selection ;
614-
615- const startLine = document . lineAt ( start ) ;
616- const leadingText = startLine . text . slice ( 0 , start . character ) ;
617- const leadingDelimiters = leadingText . match ( / \s + $ / ) ;
618- const leadingDelimiterRange =
619- leadingDelimiters != null
620- ? new Range (
621- start . line ,
622- start . character - leadingDelimiters [ 0 ] . length ,
623- start . line ,
624- start . character
625- )
626- : null ;
627-
628625 const endLine = document . lineAt ( end ) ;
629- const trailingText = endLine . text . slice ( end . character ) ;
630- const trailingDelimiters = trailingText . match ( / ^ \s + / ) ;
631- const trailingDelimiterRange =
632- trailingDelimiters != null
633- ? new Range (
634- end . line ,
635- end . character ,
636- end . line ,
637- end . character + trailingDelimiters [ 0 ] . length
638- )
639- : null ;
626+ let leadingDelimiterRange , trailingDelimiterRange ;
627+
628+ // Position start/end of has no delimiter
629+ if ( position !== "before" || insideOutsideType !== "inside" ) {
630+ const startLine = document . lineAt ( start ) ;
631+ const leadingText = startLine . text . slice ( 0 , start . character ) ;
632+ const leadingDelimiters = leadingText . match ( / \s + $ / ) ;
633+ leadingDelimiterRange =
634+ leadingDelimiters != null
635+ ? new Range (
636+ start . line ,
637+ start . character - leadingDelimiters [ 0 ] . length ,
638+ start . line ,
639+ start . character
640+ )
641+ : null ;
642+ }
643+
644+ if ( position !== "after" || insideOutsideType !== "inside" ) {
645+ const trailingText = endLine . text . slice ( end . character ) ;
646+ const trailingDelimiters = trailingText . match ( / ^ \s + / ) ;
647+ trailingDelimiterRange =
648+ trailingDelimiters != null
649+ ? new Range (
650+ end . line ,
651+ end . character ,
652+ end . line ,
653+ end . character + trailingDelimiters [ 0 ] . length
654+ )
655+ : null ;
656+ }
640657
641658 const isInDelimitedList =
642659 ( leadingDelimiterRange != null || trailingDelimiterRange != null ) &&
0 commit comments