diff --git a/src/Types.ts b/src/Types.ts index c50b68d7a0..2b9a9eb3b5 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -96,11 +96,15 @@ export type ScopeType = | "xmlEndTag" | "xmlStartTag"; export type PieceType = "word" | "character"; +export type DelimiterInclusion = + | "excludeDelimiters" + | "includeDelimiters" + | "delimitersOnly"; export interface SurroundingPairModifier { type: "surroundingPair"; delimiter: Delimiter | null; - delimitersOnly: boolean; + delimiterInclusion: DelimiterInclusion; } export interface ContainingScopeModifier { type: "containingScope"; diff --git a/src/languages/surroundingPair.ts b/src/languages/surroundingPair.ts index 6611d301ae..7dec3cf1fc 100644 --- a/src/languages/surroundingPair.ts +++ b/src/languages/surroundingPair.ts @@ -3,6 +3,7 @@ import { Position, Selection } from "vscode"; import { Point, SyntaxNode } from "web-tree-sitter"; import { Delimiter, + DelimiterInclusion, NodeMatcher, NodeMatcherValue, SelectionWithEditor, @@ -28,7 +29,7 @@ const leftToRightMap: Record = Object.fromEntries( export function createSurroundingPairMatcher( delimiter: Delimiter | null, - delimitersOnly: boolean + delimiterInclusion: DelimiterInclusion ): NodeMatcher { return function nodeMatcher( selection: SelectionWithEditor, @@ -68,7 +69,7 @@ export function createSurroundingPairMatcher( return extractSelection( leftDelimiterNode, rightDelimiterNode, - delimitersOnly + delimiterInclusion ); }; } @@ -76,48 +77,46 @@ export function createSurroundingPairMatcher( function extractSelection( leftDelimiterNode: SyntaxNode, rightDelimiterNode: SyntaxNode, - delimitersOnly: boolean + delimiterInclusion: DelimiterInclusion ): NodeMatcherValue[] { - if (delimitersOnly === false) { - return [ - { - node: leftDelimiterNode, - selection: { - selection: new Selection( - positionFromPoint(leftDelimiterNode.endPosition), - positionFromPoint(rightDelimiterNode.startPosition) - ), - context: { - outerSelection: new Selection( - positionFromPoint(leftDelimiterNode.startPosition), - positionFromPoint(rightDelimiterNode.endPosition) - ), - }, - }, - }, - ]; - } else { - return [ - { - node: leftDelimiterNode, - selection: { - selection: new Selection( - positionFromPoint(leftDelimiterNode.startPosition), - positionFromPoint(leftDelimiterNode.endPosition) - ), - context: {}, - }, - }, - { - node: rightDelimiterNode, - selection: { - selection: new Selection( - positionFromPoint(rightDelimiterNode.startPosition), - positionFromPoint(rightDelimiterNode.endPosition) - ), - context: {}, - }, - }, - ]; + var selections: Selection[]; + + switch (delimiterInclusion) { + case "includeDelimiters": + selections = [ + new Selection( + positionFromPoint(leftDelimiterNode.startPosition), + positionFromPoint(rightDelimiterNode.endPosition) + ), + ]; + break; + case "excludeDelimiters": + selections = [ + new Selection( + positionFromPoint(leftDelimiterNode.endPosition), + positionFromPoint(rightDelimiterNode.startPosition) + ), + ]; + break; + case "delimitersOnly": + selections = [ + new Selection( + positionFromPoint(leftDelimiterNode.startPosition), + positionFromPoint(leftDelimiterNode.endPosition) + ), + new Selection( + positionFromPoint(rightDelimiterNode.startPosition), + positionFromPoint(rightDelimiterNode.endPosition) + ), + ]; + break; } + + return selections.map((selection) => ({ + node: leftDelimiterNode, + selection: { + selection, + context: {}, + }, + })); } diff --git a/src/processTargets.ts b/src/processTargets.ts index 17fa27a03d..c9088e36da 100644 --- a/src/processTargets.ts +++ b/src/processTargets.ts @@ -1,5 +1,5 @@ import update from "immutability-helper"; -import { concat, range, zip } from "lodash"; +import { concat, isEqual, range, zip } from "lodash"; import * as vscode from "vscode"; import { Location, Position, Range, Selection, TextDocument } from "vscode"; import { SyntaxNode } from "web-tree-sitter"; @@ -427,7 +427,7 @@ function transformSelection( const nodeMatcher = createSurroundingPairMatcher( modifier.delimiter, - modifier.delimitersOnly + modifier.delimiterInclusion ); let result = findNearestContainingAncestorNode( node, @@ -656,12 +656,7 @@ function getTokenSelectionContext( // TODO Clean this up once we have rich targets and better polymorphic // selection contexts that indicate their type function isSelectionContextEmpty(selectionContext: SelectionContext) { - return ( - selectionContext.isInDelimitedList == null && - selectionContext.containingListDelimiter == null && - selectionContext.leadingDelimiterRange == null && - selectionContext.trailingDelimiterRange == null - ); + return isEqual(selectionContext, {}); } function getLineSelectionContext(