176
177
public getFirstBracketAfter(position: Position): IFoundBracket | null {
179
>
180
>
const node = this.initialAstWithoutTokens || this.astWithTokens!;
181
>
return getFirstBracketAfter(node, lengthZero, node.length, positionToLength(position));
182
>
}
183
184
public getFirstBracketBefore(position: Position): IFoundBracket | null {
186
>
187
>
const node = this.initialAstWithoutTokens || this.astWithTokens!;
188
>
return getFirstBracketBefore(node, lengthZero, node.length, positionToLength(position));
189
>
}
190
}
191
192
>
function getFirstBracketBefore(node: AstNode, nodeOffsetStart: Length, nodeOffsetEnd: Length, position: Length): IFoundBracket | null {
bracketPairsTree.ts
193
>
if (node.kind === AstNodeKind.List || node.kind === AstNodeKind.Pair) {
194
>
const lengths: { nodeOffsetStart: Length; nodeOffsetEnd: Length }[] = [];
195
>
for (const child of node.children) {
196
>
nodeOffsetEnd = lengthAdd(nodeOffsetStart, child.length);
197
>
lengths.push({ nodeOffsetStart, nodeOffsetEnd });
198
>
nodeOffsetStart = nodeOffsetEnd;
199
>
}
200
>
for (let i = lengths.length - 1; i >= 0; i--) {
201
>
const { nodeOffsetStart, nodeOffsetEnd } = lengths[i];
202
>
if (lengthLessThan(nodeOffsetStart, position)) {
203
>
const result = getFirstBracketBefore(node.children[i], nodeOffsetStart, nodeOffsetEnd, position);
204
>
if (result) {
205
>
return result;
206
>
}
207
>
}
208
>
}
209
>
return null;
210
>
} else if (node.kind === AstNodeKind.UnexpectedClosingBracket) {
211
return null;
213
>
const range = lengthsToRange(nodeOffsetStart, nodeOffsetEnd);
214
>
return {
215
>
bracketInfo: node.bracketInfo,
216
>
range
217
>
};
218
>
}
219
>
return null;
220
>
}
221
222
>
function getFirstBracketAfter(node: AstNode, nodeOffsetStart: Length, nodeOffsetEnd: Length, position: Length): IFoundBracket | null {
bracketPairsTree.ts
223
>
if (node.kind === AstNodeKind.List || node.kind === AstNodeKind.Pair) {
224
>
for (const child of node.children) {
225
>
nodeOffsetEnd = lengthAdd(nodeOffsetStart, child.length);
226
>
if (lengthLessThan(position, nodeOffsetEnd)) {
227
>
const result = getFirstBracketAfter(child, nodeOffsetStart, nodeOffsetEnd, position);
228
>
if (result) {
229
>
return result;
230
>
}
231
>
}
232
>
nodeOffsetStart = nodeOffsetEnd;
233
>
}
234
>
return null;
235
>
} else if (node.kind === AstNodeKind.UnexpectedClosingBracket) {
236
return null;
238
>
const range = lengthsToRange(nodeOffsetStart, nodeOffsetEnd);
239
>
return {
240
>
bracketInfo: node.bracketInfo,
241
>
range
242
>
};
243
>
}
244
>
return null;
245
>
}
246
247
function collectBrackets(