Skip to content

Commit 95acb52

Browse files
author
Andreas Arvidsson
committed
Added complex surrounding pair scope type
1 parent 9db3da3 commit 95acb52

File tree

7 files changed

+46
-84
lines changed

7 files changed

+46
-84
lines changed

src/processTargets/modifiers/ItemStage/getIterationScope.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,16 @@ function getSurroundingPair(
7575
editor: TextEditor,
7676
contentRange: Range
7777
) {
78-
return processSurroundingPairForDelimiters(
79-
context,
80-
editor,
81-
contentRange,
82-
{
83-
type: "surroundingPair",
84-
delimiter: "any",
85-
requireStrongContainment: true,
86-
},
87-
["parentheses", "squareBrackets", "curlyBrackets", "angleBrackets"]
88-
);
78+
return processSurroundingPairForDelimiters(context, editor, contentRange, {
79+
type: "surroundingPair",
80+
requireStrongContainment: true,
81+
delimiters: [
82+
"parentheses",
83+
"squareBrackets",
84+
"curlyBrackets",
85+
"angleBrackets",
86+
],
87+
});
8988
}
9089

9190
function getStringSurroundingPair(

src/processTargets/modifiers/surroundingPair/findDelimiterPairAdjacentToSelection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SurroundingPairScopeType } from "../../../typings/targetDescriptor.types";
1+
import { SurroundingPairComplexScopeType } from "../../../typings/targetDescriptor.types";
22
import { findOppositeDelimiter } from "./findOppositeDelimiter";
33
import { getSurroundingPairOffsets } from "./getSurroundingPairOffsets";
44
import {
@@ -30,7 +30,7 @@ export function findDelimiterPairAdjacentToSelection(
3030
initialIndex: number,
3131
delimiterOccurrences: PossibleDelimiterOccurrence[],
3232
selectionOffsets: Offsets,
33-
scopeType: SurroundingPairScopeType,
33+
scopeType: SurroundingPairComplexScopeType,
3434
bailOnUnmatchedAdjacent: boolean = false
3535
): SurroundingPairOffsets | null {
3636
const indicesToTry = [initialIndex + 1, initialIndex];

src/processTargets/modifiers/surroundingPair/findSurroundingPairCore.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { sortedIndexBy } from "lodash";
2-
import {
3-
SimpleSurroundingPairName,
4-
SurroundingPairScopeType,
5-
} from "../../../typings/targetDescriptor.types";
2+
import { SurroundingPairComplexScopeType } from "../../../typings/targetDescriptor.types";
63
import { findDelimiterPairAdjacentToSelection } from "./findDelimiterPairAdjacentToSelection";
74
import { findDelimiterPairContainingSelection } from "./findDelimiterPairContainingSelection";
85
import {
9-
SurroundingPairOffsets,
106
Offsets,
117
PossibleDelimiterOccurrence,
8+
SurroundingPairOffsets,
129
} from "./types";
1310

1411
/**
@@ -23,7 +20,6 @@ import {
2320
* delimiter pair that contains the selection.
2421
*
2522
* @param delimiterOccurrences A list of delimiter occurrences. Expected to be sorted by offsets
26-
* @param acceptableDelimiters A list of names of acceptable delimiters to look for
2723
* @param selectionOffsets The offsets of the selection
2824
* @param bailOnUnmatchedAdjacent If `true`, immediately return null if we find
2925
* an adjacent delimiter that we can't find a match for. This variable will
@@ -33,9 +29,8 @@ import {
3329
* @returns
3430
*/
3531
export function findSurroundingPairCore(
36-
scopeType: SurroundingPairScopeType,
32+
scopeType: SurroundingPairComplexScopeType,
3733
delimiterOccurrences: PossibleDelimiterOccurrence[],
38-
acceptableDelimiters: SimpleSurroundingPairName[],
3934
selectionOffsets: Offsets,
4035
bailOnUnmatchedAdjacent: boolean = false
4136
): SurroundingPairOffsets | null {
@@ -72,7 +67,7 @@ export function findSurroundingPairCore(
7267
return findDelimiterPairContainingSelection(
7368
initialIndex,
7469
delimiterOccurrences,
75-
acceptableDelimiters,
70+
scopeType.delimiters,
7671
selectionOffsets
7772
);
7873
}

src/processTargets/modifiers/surroundingPair/findSurroundingPairParseTreeBased.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { Range, TextDocument, TextEditor } from "vscode";
22
import { SyntaxNode } from "web-tree-sitter";
3-
import {
4-
SimpleSurroundingPairName,
5-
SurroundingPairScopeType,
6-
} from "../../../typings/targetDescriptor.types";
3+
import { SurroundingPairComplexScopeType } from "../../../typings/targetDescriptor.types";
74
import { getNodeRange } from "../../../util/nodeSelectors";
85
import { isContainedInErrorNode } from "../../../util/treeSitterUtils";
96
import { extractSelectionFromSurroundingPairOffsets } from "./extractSelectionFromSurroundingPairOffsets";
@@ -61,12 +58,11 @@ export function findSurroundingPairParseTreeBased(
6158
editor: TextEditor,
6259
selection: Range,
6360
node: SyntaxNode,
64-
delimiters: SimpleSurroundingPairName[],
65-
scopeType: SurroundingPairScopeType
61+
scopeType: SurroundingPairComplexScopeType
6662
) {
6763
const document: TextDocument = editor.document;
6864

69-
const individualDelimiters = getIndividualDelimiters(delimiters);
65+
const individualDelimiters = getIndividualDelimiters(scopeType.delimiters);
7066

7167
const delimiterTextToDelimiterInfoMap = Object.fromEntries(
7268
individualDelimiters.map((individualDelimiter) => [
@@ -86,7 +82,6 @@ export function findSurroundingPairParseTreeBased(
8682
const context: Context = {
8783
delimiterTextToDelimiterInfoMap,
8884
individualDelimiters,
89-
delimiters,
9085
selectionOffsets,
9186
scopeType,
9287
};
@@ -139,17 +134,12 @@ interface Context {
139134
*/
140135
individualDelimiters: IndividualDelimiter[];
141136

142-
/**
143-
* The names of the delimiters that we're considering
144-
*/
145-
delimiters: SimpleSurroundingPairName[];
146-
147137
/**
148138
* The offsets of the selection
149139
*/
150140
selectionOffsets: Offsets;
151141

152-
scopeType: SurroundingPairScopeType;
142+
scopeType: SurroundingPairComplexScopeType;
153143
}
154144

155145
/**
@@ -168,7 +158,6 @@ function findSurroundingPairContainedInNode(
168158
const {
169159
delimiterTextToDelimiterInfoMap,
170160
individualDelimiters,
171-
delimiters,
172161
selectionOffsets,
173162
scopeType,
174163
} = context;
@@ -229,7 +218,6 @@ function findSurroundingPairContainedInNode(
229218
return findSurroundingPairCore(
230219
scopeType,
231220
delimiterOccurrences,
232-
delimiters,
233221
selectionOffsets,
234222

235223
// If we're not the root node of the parse tree (ie `node.parent !=

src/processTargets/modifiers/surroundingPair/findSurroundingPairTextBased.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { escapeRegExp, findLast, uniq } from "lodash";
22
import { Range, TextDocument, TextEditor } from "vscode";
33
import {
4-
SimpleSurroundingPairName,
4+
SurroundingPairComplexScopeType,
55
SurroundingPairName,
6-
SurroundingPairScopeType,
76
} from "../../../typings/targetDescriptor.types";
87
import { getDocumentRange } from "../../../util/range";
98
import { matchAll } from "../../../util/regex";
@@ -69,13 +68,12 @@ export function findSurroundingPairTextBased(
6968
editor: TextEditor,
7069
range: Range,
7170
allowableRange: Range | null,
72-
delimiters: SimpleSurroundingPairName[],
73-
scopeType: SurroundingPairScopeType
71+
scopeType: SurroundingPairComplexScopeType
7472
) {
7573
const document: TextDocument = editor.document;
7674
const fullRange = allowableRange ?? getDocumentRange(document);
7775

78-
const individualDelimiters = getIndividualDelimiters(delimiters);
76+
const individualDelimiters = getIndividualDelimiters(scopeType.delimiters);
7977

8078
const delimiterTextToDelimiterInfoMap = Object.fromEntries(
8179
individualDelimiters.map((individualDelimiter) => [
@@ -108,7 +106,6 @@ export function findSurroundingPairTextBased(
108106
const context: Context = {
109107
scopeType,
110108
delimiterRegex,
111-
delimiters,
112109
delimiterTextToDelimiterInfoMap,
113110
};
114111

@@ -196,16 +193,11 @@ function getDelimiterRegex(individualDelimiters: IndividualDelimiter[]) {
196193
* Context to pass to nested call
197194
*/
198195
interface Context {
199-
scopeType: SurroundingPairScopeType;
196+
scopeType: SurroundingPairComplexScopeType;
200197
delimiterTextToDelimiterInfoMap: {
201198
[k: string]: IndividualDelimiter;
202199
};
203200
delimiterRegex: RegExp;
204-
205-
/**
206-
* The allowable delimiter names
207-
*/
208-
delimiters: SimpleSurroundingPairName[];
209201
}
210202

211203
/**
@@ -228,12 +220,8 @@ function getDelimiterPairOffsets(
228220
isAtStartOfFullRange: boolean,
229221
isAtEndOfFullRange: boolean
230222
): SurroundingPairOffsets | null {
231-
const {
232-
scopeType,
233-
delimiterTextToDelimiterInfoMap,
234-
delimiterRegex,
235-
delimiters,
236-
} = context;
223+
const { scopeType, delimiterTextToDelimiterInfoMap, delimiterRegex } =
224+
context;
237225
const { forceDirection } = scopeType;
238226

239227
// XXX: The below is a bit wasteful when there are multiple targets, because
@@ -293,7 +281,6 @@ function getDelimiterPairOffsets(
293281
const surroundingPair = findSurroundingPairCore(
294282
scopeType,
295283
delimiterOccurrences,
296-
delimiters,
297284
selectionOffsets,
298285
!isAtStartOfFullRange || !isAtEndOfFullRange
299286
);

src/processTargets/modifiers/surroundingPair/index.ts

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import getTextFragmentExtractor, {
55
} from "../../../languages/getTextFragmentExtractor";
66
import {
77
ComplexSurroundingPairName,
8-
SimpleSurroundingPairName,
8+
SurroundingPairComplexScopeType,
99
SurroundingPairScopeType,
1010
} from "../../../typings/targetDescriptor.types";
1111
import { ProcessedTargetsContext } from "../../../typings/Types";
@@ -34,16 +34,14 @@ export function processSurroundingPair(
3434
range: Range,
3535
scopeType: SurroundingPairScopeType
3636
): SurroundingPairInfo | null {
37+
const { delimiter, ...rest } = scopeType;
3738
const delimiters = complexDelimiterMap[
38-
scopeType.delimiter as ComplexSurroundingPairName
39-
] ?? [scopeType.delimiter];
40-
return processSurroundingPairForDelimiters(
41-
context,
42-
editor,
43-
range,
44-
scopeType,
45-
delimiters
46-
);
39+
delimiter as ComplexSurroundingPairName
40+
] ?? [delimiter];
41+
return processSurroundingPairForDelimiters(context, editor, range, {
42+
...rest,
43+
delimiters,
44+
});
4745
}
4846

4947
/**
@@ -64,8 +62,7 @@ export function processSurroundingPairForDelimiters(
6462
context: ProcessedTargetsContext,
6563
editor: TextEditor,
6664
range: Range,
67-
scopeType: SurroundingPairScopeType,
68-
delimiters: SimpleSurroundingPairName[]
65+
scopeType: SurroundingPairComplexScopeType
6966
): SurroundingPairInfo | null {
7067
const document = editor.document;
7168
let node: SyntaxNode | null;
@@ -79,13 +76,7 @@ export function processSurroundingPairForDelimiters(
7976
if ((err as Error).name === "UnsupportedLanguageError") {
8077
// If we're in a language where we don't have a parse tree we use the text
8178
// based algorithm
82-
return findSurroundingPairTextBased(
83-
editor,
84-
range,
85-
null,
86-
delimiters,
87-
scopeType
88-
);
79+
return findSurroundingPairTextBased(editor, range, null, scopeType);
8980
} else {
9081
throw err;
9182
}
@@ -103,7 +94,6 @@ export function processSurroundingPairForDelimiters(
10394
editor,
10495
range,
10596
textFragmentRange,
106-
delimiters,
10797
scopeType
10898
);
10999

@@ -115,11 +105,5 @@ export function processSurroundingPairForDelimiters(
115105
// If we have a parse tree and either we are not in a string or comment or we
116106
// couldn't find a surrounding pair within a string or comment, we use the
117107
// parse tree-based algorithm
118-
return findSurroundingPairParseTreeBased(
119-
editor,
120-
range,
121-
node,
122-
delimiters,
123-
scopeType
124-
);
108+
return findSurroundingPairParseTreeBased(editor, range, node, scopeType);
125109
}

src/typings/targetDescriptor.types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ export interface SurroundingPairScopeType {
129129
requireStrongContainment?: boolean;
130130
}
131131

132+
/** The same as `SurroundingPairScopeType` but with multiple delimiters */
133+
export interface SurroundingPairComplexScopeType
134+
extends Omit<SurroundingPairScopeType, "delimiter"> {
135+
/**
136+
* The allowable delimiter names
137+
*/
138+
delimiters: SimpleSurroundingPairName[];
139+
}
140+
132141
export type ScopeType = SimpleScopeType | SurroundingPairScopeType;
133142

134143
export interface ContainingSurroundingPairModifier

0 commit comments

Comments
 (0)