Skip to content

Change surrounding delimiter api #225

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 5 additions & 1 deletion src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
87 changes: 43 additions & 44 deletions src/languages/surroundingPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Position, Selection } from "vscode";
import { Point, SyntaxNode } from "web-tree-sitter";
import {
Delimiter,
DelimiterInclusion,
NodeMatcher,
NodeMatcherValue,
SelectionWithEditor,
Expand All @@ -28,7 +29,7 @@ const leftToRightMap: Record<string, string> = Object.fromEntries(

export function createSurroundingPairMatcher(
delimiter: Delimiter | null,
delimitersOnly: boolean
delimiterInclusion: DelimiterInclusion
): NodeMatcher {
return function nodeMatcher(
selection: SelectionWithEditor,
Expand Down Expand Up @@ -68,56 +69,54 @@ export function createSurroundingPairMatcher(
return extractSelection(
leftDelimiterNode,
rightDelimiterNode,
delimitersOnly
delimiterInclusion
);
};
}

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: {},
},
}));
}
11 changes: 3 additions & 8 deletions src/processTargets.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -427,7 +427,7 @@ function transformSelection(

const nodeMatcher = createSurroundingPairMatcher(
modifier.delimiter,
modifier.delimitersOnly
modifier.delimiterInclusion
);
let result = findNearestContainingAncestorNode(
node,
Expand Down Expand Up @@ -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(
Expand Down