Skip to content

Commit b3e5138

Browse files
committed
Change surrounding delimiter api
1 parent 698f45c commit b3e5138

File tree

3 files changed

+51
-53
lines changed

3 files changed

+51
-53
lines changed

src/Types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,15 @@ export type ScopeType =
9595
| "xmlEndTag"
9696
| "xmlStartTag";
9797
export type PieceType = "word" | "character";
98+
export type DelimiterInclusion =
99+
| "excludeDelimiters"
100+
| "includeDelimiters"
101+
| "delimitersOnly";
98102

99103
export interface SurroundingPairModifier {
100104
type: "surroundingPair";
101105
delimiter: Delimiter | null;
102-
delimitersOnly: boolean;
106+
delimiterInclusion: DelimiterInclusion;
103107
}
104108
export interface ContainingScopeModifier {
105109
type: "containingScope";

src/languages/surroundingPair.ts

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Position, Selection } from "vscode";
33
import { Point, SyntaxNode } from "web-tree-sitter";
44
import {
55
Delimiter,
6+
DelimiterInclusion,
67
NodeMatcher,
78
NodeMatcherValue,
89
SelectionWithEditor,
@@ -27,7 +28,7 @@ const leftToRightMap: Record<string, string> = Object.fromEntries(
2728

2829
export function createSurroundingPairMatcher(
2930
delimiter: Delimiter | null,
30-
delimitersOnly: boolean
31+
delimiterInclusion: DelimiterInclusion
3132
): NodeMatcher {
3233
return function nodeMatcher(
3334
selection: SelectionWithEditor,
@@ -67,56 +68,54 @@ export function createSurroundingPairMatcher(
6768
return extractSelection(
6869
leftDelimiterNode,
6970
rightDelimiterNode,
70-
delimitersOnly
71+
delimiterInclusion
7172
);
7273
};
7374
}
7475

7576
function extractSelection(
7677
leftDelimiterNode: SyntaxNode,
7778
rightDelimiterNode: SyntaxNode,
78-
delimitersOnly: boolean
79+
delimiterInclusion: DelimiterInclusion
7980
): NodeMatcherValue[] {
80-
if (delimitersOnly === false) {
81-
return [
82-
{
83-
node: leftDelimiterNode,
84-
selection: {
85-
selection: new Selection(
86-
positionFromPoint(leftDelimiterNode.endPosition),
87-
positionFromPoint(rightDelimiterNode.startPosition)
88-
),
89-
context: {
90-
outerSelection: new Selection(
91-
positionFromPoint(leftDelimiterNode.startPosition),
92-
positionFromPoint(rightDelimiterNode.endPosition)
93-
),
94-
},
95-
},
96-
},
97-
];
98-
} else {
99-
return [
100-
{
101-
node: leftDelimiterNode,
102-
selection: {
103-
selection: new Selection(
104-
positionFromPoint(leftDelimiterNode.startPosition),
105-
positionFromPoint(leftDelimiterNode.endPosition)
106-
),
107-
context: {},
108-
},
109-
},
110-
{
111-
node: rightDelimiterNode,
112-
selection: {
113-
selection: new Selection(
114-
positionFromPoint(rightDelimiterNode.startPosition),
115-
positionFromPoint(rightDelimiterNode.endPosition)
116-
),
117-
context: {},
118-
},
119-
},
120-
];
81+
var selections: Selection[];
82+
83+
switch (delimiterInclusion) {
84+
case "includeDelimiters":
85+
selections = [
86+
new Selection(
87+
positionFromPoint(leftDelimiterNode.startPosition),
88+
positionFromPoint(rightDelimiterNode.endPosition)
89+
),
90+
];
91+
break;
92+
case "excludeDelimiters":
93+
selections = [
94+
new Selection(
95+
positionFromPoint(leftDelimiterNode.endPosition),
96+
positionFromPoint(rightDelimiterNode.startPosition)
97+
),
98+
];
99+
break;
100+
case "delimitersOnly":
101+
selections = [
102+
new Selection(
103+
positionFromPoint(leftDelimiterNode.startPosition),
104+
positionFromPoint(leftDelimiterNode.endPosition)
105+
),
106+
new Selection(
107+
positionFromPoint(rightDelimiterNode.startPosition),
108+
positionFromPoint(rightDelimiterNode.endPosition)
109+
),
110+
];
111+
break;
121112
}
113+
114+
return selections.map((selection) => ({
115+
node: leftDelimiterNode,
116+
selection: {
117+
selection,
118+
context: {},
119+
},
120+
}));
122121
}

src/processTargets.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import update from "immutability-helper";
2-
import { concat, range, zip } from "lodash";
2+
import { concat, isEqual, range, zip } from "lodash";
33
import * as vscode from "vscode";
44
import { Location, Position, Range, Selection, TextDocument } from "vscode";
55
import { SyntaxNode } from "web-tree-sitter";
@@ -427,7 +427,7 @@ function transformSelection(
427427

428428
const nodeMatcher = createSurroundingPairMatcher(
429429
modifier.delimiter,
430-
modifier.delimitersOnly
430+
modifier.delimiterInclusion
431431
);
432432
let result = findNearestContainingAncestorNode(
433433
node,
@@ -656,12 +656,7 @@ function getTokenSelectionContext(
656656
// TODO Clean this up once we have rich targets and better polymorphic
657657
// selection contexts that indicate their type
658658
function isSelectionContextEmpty(selectionContext: SelectionContext) {
659-
return (
660-
selectionContext.isInDelimitedList == null &&
661-
selectionContext.containingListDelimiter == null &&
662-
selectionContext.leadingDelimiterRange == null &&
663-
selectionContext.trailingDelimiterRange == null
664-
);
659+
return isEqual(selectionContext, {});
665660
}
666661

667662
function getLineSelectionContext(

0 commit comments

Comments
 (0)