From fa03fa4bf768495c33cc9aee9d4ff701851f3334 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Wed, 21 Jul 2021 21:57:11 +0200 Subject: [PATCH 1/4] added support to exclude start or end token in ranges --- src/Types.ts | 4 ++++ src/inferFullTargets.ts | 2 ++ src/processTargets.ts | 40 +++++++++++++++++++++++++++------------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/Types.ts b/src/Types.ts index d828441e14..2156d1b6d5 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -117,6 +117,8 @@ export interface PartialRangeTarget { type: "range"; start: PartialPrimitiveTarget; end: PartialPrimitiveTarget; + excludeStart?: boolean; + excludeEnd?: boolean; } export interface PartialListTarget { @@ -142,6 +144,8 @@ export interface RangeTarget { type: "range"; start: PrimitiveTarget; end: PrimitiveTarget; + excludeStart?: boolean; + excludeEnd?: boolean; } export interface ListTarget { diff --git a/src/inferFullTargets.ts b/src/inferFullTargets.ts index b0a41fd792..54eb928af2 100644 --- a/src/inferFullTargets.ts +++ b/src/inferFullTargets.ts @@ -269,6 +269,8 @@ export function inferSingleNonListTarget( ); return { type: "range", + excludeStart: target.excludeStart, + excludeEnd: target.excludeEnd, start, end: inferRangeEndTarget( context, diff --git a/src/processTargets.ts b/src/processTargets.ts index daf3a9834f..c21c9ccfeb 100644 --- a/src/processTargets.ts +++ b/src/processTargets.ts @@ -69,23 +69,37 @@ function processSingleRangeTarget( endSelection.start ); - const anchor = isStartBeforeEnd ? startSelection.start : startSelection.end; - const active = isStartBeforeEnd ? endSelection.end : endSelection.start; - const leadingDelimiterRange = isStartBeforeEnd + const anchor = + isStartBeforeEnd !== target.excludeStart + ? startSelection.start + : startSelection.end; + const active = + isStartBeforeEnd !== target.excludeEnd + ? endSelection.end + : endSelection.start; + + const outerAnchor = target.excludeStart + ? anchor + : isStartBeforeEnd + ? startTarget!.selectionContext.outerSelection?.start ?? anchor + : startTarget!.selectionContext.outerSelection?.end ?? anchor; + const outerActive = target.excludeEnd + ? active + : isStartBeforeEnd + ? endTarget!.selectionContext.outerSelection?.end ?? active + : endTarget!.selectionContext.outerSelection?.start ?? active; + + const leadingDelimiterRange = target.excludeStart + ? null + : isStartBeforeEnd ? startTarget!.selectionContext.leadingDelimiterRange : endTarget!.selectionContext.leadingDelimiterRange; - const trailingDelimiterRange = isStartBeforeEnd + const trailingDelimiterRange = target.excludeEnd + ? null + : isStartBeforeEnd ? endTarget!.selectionContext.trailingDelimiterRange : startTarget!.selectionContext.trailingDelimiterRange; - const startOuterSelection = - startTarget!.selectionContext.outerSelection ?? startSelection; - const endOuterSelection = - endTarget!.selectionContext.outerSelection ?? endSelection; - const outerSelection = isStartBeforeEnd - ? new Selection(startOuterSelection.start, endOuterSelection.end) - : new Selection(endOuterSelection.start, startOuterSelection.end); - return { selection: { selection: new Selection(anchor, active), @@ -98,7 +112,7 @@ function processSingleRangeTarget( isInDelimitedList: startTarget!.selectionContext.isInDelimitedList, leadingDelimiterRange, trailingDelimiterRange, - outerSelection, + outerSelection: new Selection(outerAnchor, outerActive), }, insideOutsideType: startTarget!.insideOutsideType, position: "contents", From 9aa3bdc829229a72d7c28b5c29a9528d9894f199 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Thu, 22 Jul 2021 11:39:20 +0200 Subject: [PATCH 2/4] handle delimiters when excluding ranges --- src/Types.ts | 4 +-- src/inferFullTargets.ts | 4 +-- src/processTargets.ts | 78 ++++++++++++++++++++++++++++------------- 3 files changed, 58 insertions(+), 28 deletions(-) diff --git a/src/Types.ts b/src/Types.ts index 2156d1b6d5..94aa01412f 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -144,8 +144,8 @@ export interface RangeTarget { type: "range"; start: PrimitiveTarget; end: PrimitiveTarget; - excludeStart?: boolean; - excludeEnd?: boolean; + excludeStart: boolean; + excludeEnd: boolean; } export interface ListTarget { diff --git a/src/inferFullTargets.ts b/src/inferFullTargets.ts index 54eb928af2..3ccd30bd8e 100644 --- a/src/inferFullTargets.ts +++ b/src/inferFullTargets.ts @@ -269,8 +269,8 @@ export function inferSingleNonListTarget( ); return { type: "range", - excludeStart: target.excludeStart, - excludeEnd: target.excludeEnd, + excludeStart: !!target.excludeStart, + excludeEnd: !!target.excludeEnd, start, end: inferRangeEndTarget( context, diff --git a/src/processTargets.ts b/src/processTargets.ts index c21c9ccfeb..debc13e5e0 100644 --- a/src/processTargets.ts +++ b/src/processTargets.ts @@ -69,36 +69,44 @@ function processSingleRangeTarget( endSelection.start ); - const anchor = - isStartBeforeEnd !== target.excludeStart - ? startSelection.start - : startSelection.end; - const active = - isStartBeforeEnd !== target.excludeEnd - ? endSelection.end - : endSelection.start; + const anchor = getRangePosition( + startTarget!, + isStartBeforeEnd, + target.excludeStart + ); + const active = getRangePosition( + endTarget!, + !isStartBeforeEnd, + target.excludeEnd + ); const outerAnchor = target.excludeStart - ? anchor + ? null : isStartBeforeEnd - ? startTarget!.selectionContext.outerSelection?.start ?? anchor - : startTarget!.selectionContext.outerSelection?.end ?? anchor; + ? startTarget!.selectionContext.outerSelection?.start + : startTarget!.selectionContext.outerSelection?.end; const outerActive = target.excludeEnd - ? active - : isStartBeforeEnd - ? endTarget!.selectionContext.outerSelection?.end ?? active - : endTarget!.selectionContext.outerSelection?.start ?? active; - - const leadingDelimiterRange = target.excludeStart ? null : isStartBeforeEnd - ? startTarget!.selectionContext.leadingDelimiterRange - : endTarget!.selectionContext.leadingDelimiterRange; - const trailingDelimiterRange = target.excludeEnd + ? endTarget!.selectionContext.outerSelection?.end + : endTarget!.selectionContext.outerSelection?.start; + const outerSelection = + outerAnchor != null || outerActive != null + ? new Selection(outerAnchor ?? anchor, outerActive ?? active) + : null; + + const startSelectionContext = target.excludeStart ? null - : isStartBeforeEnd - ? endTarget!.selectionContext.trailingDelimiterRange - : startTarget!.selectionContext.trailingDelimiterRange; + : startTarget!.selectionContext; + const endSelectionContext = target.excludeEnd + ? null + : endTarget!.selectionContext; + const leadingDelimiterRange = isStartBeforeEnd + ? startSelectionContext?.leadingDelimiterRange + : endSelectionContext?.leadingDelimiterRange; + const trailingDelimiterRange = isStartBeforeEnd + ? endSelectionContext?.trailingDelimiterRange + : startSelectionContext?.trailingDelimiterRange; return { selection: { @@ -112,7 +120,7 @@ function processSingleRangeTarget( isInDelimitedList: startTarget!.selectionContext.isInDelimitedList, leadingDelimiterRange, trailingDelimiterRange, - outerSelection: new Selection(outerAnchor, outerActive), + outerSelection, }, insideOutsideType: startTarget!.insideOutsideType, position: "contents", @@ -120,6 +128,28 @@ function processSingleRangeTarget( }); } +function getRangePosition( + target: TypedSelection, + isStart: boolean, + exclude: boolean +) { + const selection = target.selection.selection; + if (exclude) { + const outerSelection = target!.selectionContext.outerSelection; + const delimiterPosition = isStart + ? target!.selectionContext.trailingDelimiterRange?.end + : target!.selectionContext.leadingDelimiterRange?.start; + if (outerSelection != null) { + if (delimiterPosition != null) { + return delimiterPosition; + } + return isStart ? outerSelection.end : outerSelection.start; + } + return isStart ? selection.end : selection.start; + } + return isStart ? selection.start : selection.end; +} + function processSinglePrimitiveTarget( context: ProcessedTargetsContext, target: PrimitiveTarget From 126b1112c1233987270cc360e01fe95d64959fc2 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sun, 25 Jul 2021 00:45:52 +0200 Subject: [PATCH 3/4] Update src/inferFullTargets.ts Co-authored-by: Pokey Rule --- src/inferFullTargets.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/inferFullTargets.ts b/src/inferFullTargets.ts index 3ccd30bd8e..95261b2586 100644 --- a/src/inferFullTargets.ts +++ b/src/inferFullTargets.ts @@ -269,8 +269,8 @@ export function inferSingleNonListTarget( ); return { type: "range", - excludeStart: !!target.excludeStart, - excludeEnd: !!target.excludeEnd, + excludeStart: target.excludeStart ?? false, + excludeEnd: target.excludeEnd ?? false, start, end: inferRangeEndTarget( context, From 41f64e2483c9d139fcdb3ce9367b7d3cbbc11769 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sun, 25 Jul 2021 01:24:37 +0200 Subject: [PATCH 4/4] added dock string --- src/processTargets.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/processTargets.ts b/src/processTargets.ts index d3093f8280..677420f75b 100644 --- a/src/processTargets.ts +++ b/src/processTargets.ts @@ -128,6 +128,11 @@ function processSingleRangeTarget( }); } +/** +* @param target The target to get position from +* @param isStart If true disposition is the start of the range +* @param exclude If true the content of this position should be excluded +*/ function getRangePosition( target: TypedSelection, isStart: boolean, @@ -137,8 +142,8 @@ function getRangePosition( if (exclude) { const outerSelection = target!.selectionContext.outerSelection; const delimiterPosition = isStart - ? target!.selectionContext.trailingDelimiterRange?.end - : target!.selectionContext.leadingDelimiterRange?.start; + ? target.selectionContext.trailingDelimiterRange?.end + : target.selectionContext.leadingDelimiterRange?.start; if (outerSelection != null) { if (delimiterPosition != null) { return delimiterPosition;