From 8f55b02cec5250b9ba0ed70acda86d42388d7982 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sat, 17 Jul 2021 01:20:14 +0200 Subject: [PATCH 1/4] fixed small offset error on the negative end index --- src/processTargets.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/processTargets.ts b/src/processTargets.ts index 912f956728..398e1c9359 100644 --- a/src/processTargets.ts +++ b/src/processTargets.ts @@ -224,7 +224,7 @@ function transformSelection( modifier.endIndex == null ? pieces.length : modifier.endIndex <= 0 - ? modifier.endIndex + pieces.length + ? modifier.endIndex + pieces.length + 1 : modifier.endIndex; const startIndex = From 089e4c9c579d52a72becb4e68d4e867b976b1838 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sat, 17 Jul 2021 02:31:33 +0200 Subject: [PATCH 2/4] Handle reverse order sub token --- src/processTargets.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/processTargets.ts b/src/processTargets.ts index 398e1c9359..d02a8488dc 100644 --- a/src/processTargets.ts +++ b/src/processTargets.ts @@ -234,11 +234,15 @@ function transformSelection( const start = selection.selection.start.translate( undefined, - pieces[startIndex].start + startIndex <= endIndex - 1 + ? pieces[startIndex].start + : pieces[endIndex - 1].start ); const end = selection.selection.start.translate( undefined, - pieces[endIndex - 1].end + startIndex <= endIndex - 1 + ? pieces[endIndex - 1].end + : pieces[startIndex].end ); return [ From bd5dd5317919e274cfe79efa184e7472adee1b07 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sat, 17 Jul 2021 22:06:31 +0200 Subject: [PATCH 3/4] excluding end index for negative index --- src/processTargets.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/processTargets.ts b/src/processTargets.ts index d02a8488dc..ef8161d5c9 100644 --- a/src/processTargets.ts +++ b/src/processTargets.ts @@ -224,7 +224,7 @@ function transformSelection( modifier.endIndex == null ? pieces.length : modifier.endIndex <= 0 - ? modifier.endIndex + pieces.length + 1 + ? modifier.endIndex + pieces.length : modifier.endIndex; const startIndex = From 026b66c5d26a421592f9f07edf24facc5d4fe2d1 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Mon, 19 Jul 2021 09:48:20 +0200 Subject: [PATCH 4/4] use anchor and active instead of start and end --- src/Types.ts | 4 ++-- src/processTargets.ts | 44 ++++++++++++++----------------------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/Types.ts b/src/Types.ts index 6da3f80f19..5fddbfe681 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -78,8 +78,8 @@ export interface ContainingScopeModifier { export interface SubpieceModifier { type: "subpiece"; pieceType: PieceType; - startIndex: number; - endIndex: number | null; + anchor: number; + active: number; } export interface MatchingPairSymbolModifier { type: "matchingPairSymbol"; diff --git a/src/processTargets.ts b/src/processTargets.ts index ef8161d5c9..960e4a596b 100644 --- a/src/processTargets.ts +++ b/src/processTargets.ts @@ -205,13 +205,10 @@ function transformSelection( let pieces: { start: number; end: number }[] = []; if (modifier.pieceType === "word") { - const matches = token.matchAll(SUBWORD_MATCHER); - for (const match of matches) { - pieces.push({ - start: match.index!, - end: match.index! + match[0].length, - }); - } + pieces = [...token.matchAll(SUBWORD_MATCHER)].map((match) => ({ + start: match.index!, + end: match.index! + match[0].length, + })); } else if (modifier.pieceType === "character") { pieces = range(token.length).map((index) => ({ start: index, @@ -219,39 +216,26 @@ function transformSelection( })); } - // NB: We use the modulo here to handle negative offsets - const endIndex = - modifier.endIndex == null - ? pieces.length - : modifier.endIndex <= 0 - ? modifier.endIndex + pieces.length - : modifier.endIndex; + const anchorIndex = + modifier.anchor < 0 ? modifier.anchor + pieces.length : modifier.anchor; + const activeIndex = + modifier.active < 0 ? modifier.active + pieces.length : modifier.active; - const startIndex = - modifier.startIndex < 0 - ? modifier.startIndex + pieces.length - : modifier.startIndex; + const isReversed = activeIndex < anchorIndex; - const start = selection.selection.start.translate( + const anchor = selection.selection.start.translate( undefined, - startIndex <= endIndex - 1 - ? pieces[startIndex].start - : pieces[endIndex - 1].start + isReversed ? pieces[anchorIndex].end : pieces[anchorIndex].start ); - const end = selection.selection.start.translate( + const active = selection.selection.start.translate( undefined, - startIndex <= endIndex - 1 - ? pieces[endIndex - 1].end - : pieces[startIndex].end + isReversed ? pieces[activeIndex].start : pieces[activeIndex].end ); return [ { selection: update(selection, { - selection: (s) => - s.isReversed - ? new Selection(end, start) - : new Selection(start, end), + selection: () => new Selection(anchor, active), }), context: {}, },