Skip to content

Added support to exclude start or end token in ranges #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export interface PartialRangeTarget {
type: "range";
start: PartialPrimitiveTarget;
end: PartialPrimitiveTarget;
excludeStart?: boolean;
excludeEnd?: boolean;
}

export interface PartialListTarget {
Expand All @@ -142,6 +144,8 @@ export interface RangeTarget {
type: "range";
start: PrimitiveTarget;
end: PrimitiveTarget;
excludeStart: boolean;
excludeEnd: boolean;
}

export interface ListTarget {
Expand Down
2 changes: 2 additions & 0 deletions src/inferFullTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ export function inferSingleNonListTarget(
);
return {
type: "range",
excludeStart: target.excludeStart ?? false,
excludeEnd: target.excludeEnd ?? false,
start,
end: inferRangeEndTarget(
context,
Expand Down
77 changes: 63 additions & 14 deletions src/processTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,44 @@ function processSingleRangeTarget(
endSelection.start
);

const anchor = isStartBeforeEnd ? startSelection.start : startSelection.end;
const active = isStartBeforeEnd ? endSelection.end : endSelection.start;
const anchor = getRangePosition(
startTarget!,
isStartBeforeEnd,
target.excludeStart
);
const active = getRangePosition(
endTarget!,
!isStartBeforeEnd,
target.excludeEnd
);

const outerAnchor = target.excludeStart
? null
: isStartBeforeEnd
? startTarget!.selectionContext.outerSelection?.start
: startTarget!.selectionContext.outerSelection?.end;
const outerActive = target.excludeEnd
? null
: isStartBeforeEnd
? 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
: startTarget!.selectionContext;
const endSelectionContext = target.excludeEnd
? null
: endTarget!.selectionContext;
const leadingDelimiterRange = isStartBeforeEnd
? startTarget!.selectionContext.leadingDelimiterRange
: endTarget!.selectionContext.leadingDelimiterRange;
? startSelectionContext?.leadingDelimiterRange
: endSelectionContext?.leadingDelimiterRange;
const trailingDelimiterRange = 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);
? endSelectionContext?.trailingDelimiterRange
: startSelectionContext?.trailingDelimiterRange;

return {
selection: {
Expand All @@ -106,6 +128,33 @@ 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,
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
Expand Down