-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmiscCode.txt
More file actions
405 lines (337 loc) · 13.4 KB
/
miscCode.txt
File metadata and controls
405 lines (337 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// const match2 = getLastMatchFromLastChildUp(previousSymbol.children.sort(compareRanges), vscode.SymbolKind.Function);
// const match1 = getFirstDeepestMatchFromLastUp(previousSymbol.children.sort(compareRanges), vscode.SymbolKind.Function);
// const match = getFirstMatchFromLastUp(previousSymbol.children.sort(compareRanges), vscode.SymbolKind.Function);
// const symbolBest = getDeepestMatchFromLastUp(previousSymbol.children.sort(compareRanges), vscode.SymbolKind.Function);
// deepest from last child forward, but doesn't start at first
// const deepestSymbol = getDeepestKindFlexible(previousSymbol.children.sort(compareRanges), vscode.SymbolKind.Function);
// only examines last child
// const deepest = getDeepestKindFromLastChild(previousSymbol.children.sort(compareRanges), vscode.SymbolKind.Function);
// ``;
// let {symbol, depth} = findDeepestSymbol(previousSymbol.children.sort(compareRanges).toReversed(), vscode.SymbolKind.Function);
// /**
// * Find the deepest matching DocumentSymbol by kind.
// * @param {vscode.DocumentSymbol[]} symbols
// * @param {vscode.SymbolKind} targetKind
// * @param {number} depth - Used internally to track current depth
// * @returns {{ symbol: vscode.DocumentSymbol|null, depth: number }}
// */
// function findDeepestSymbol(symbols, targetKind, depth = 0) {
// let deepest = {symbol: null, depth: -1};
// for (const symbol of symbols) {
// if (symbol.kind === targetKind && depth > deepest.depth) {
// deepest = {symbol, depth};
// }
// if (symbol.children && symbol.children.length > 0) {
// const childResult = findDeepestSymbol(symbol.children, targetKind, depth + 1);
// if (childResult.depth > deepest.depth) {
// deepest = childResult;
// }
// }
// }
// return deepest;
// }
// /**
// * Performs depth-prioritized search for a symbol of specific kind.
// * Always returns the deepest match in this subtree.
// * @param {vscode.DocumentSymbol} symbol
// * @param {vscode.SymbolKind} kind
// * @param {number} depth
// * @returns {{ symbol: vscode.DocumentSymbol | null, depth: number }}
// */
// function findDeepestInSubtree(symbol, kind, depth = 0) {
// let best = {symbol: null, depth: -1};
// for (const child of symbol.children ?? []) {
// const result = findDeepestInSubtree(child, kind, depth + 1);
// if (result.depth > best.depth) {
// best = result;
// }
// }
// if (symbol.kind === kind && depth > best.depth) {
// best = {symbol, depth};
// }
// return best;
// }
// /**
// * Starts at the last top-level symbol and moves upward.
// * Returns the first deepest match found from any subtree.
// * @param {vscode.DocumentSymbol[]} symbols
// * @param {vscode.SymbolKind} kind
// * @returns {vscode.DocumentSymbol | null}
// */
// function getFirstDeepestMatchFromLastUp(symbols, kind) {
// for (let i = symbols.length - 1; i >= 0; i--) {
// const result = findDeepestInSubtree(symbols[i], kind);
// if (result.symbol) {
// return result.symbol; // ⛳ First deepest match found
// }
// }
// return null;
// }
/**
* Recursively finds the last match of a given kind within a symbol's subtree,
* prioritizing reverse-order traversal of children.
* @param {vscode.DocumentSymbol} symbol
* @param {vscode.SymbolKind} kind
* @returns {vscode.DocumentSymbol | null}
*/
function findLastMatchInSubtree(symbol, kind) {
const children = symbol.children ?? [];
// Search children in reverse
for (let i = children.length - 1; i >= 0; i--) {
const match = findLastMatchInSubtree(children[i], kind);
if (match) return match;
}
// Check current symbol last
return symbol.kind === kind ? symbol : null;
}
/**
* Searches top-level symbols from last to first, inspecting children in reverse order.
* Returns the first match it finds during that sweep.
* @param {vscode.DocumentSymbol[]} symbols
* @param {vscode.SymbolKind} kind
* @returns {vscode.DocumentSymbol | null}
*/
function getLastMatchFromLastChildUp(symbols, kind) {
for (let i = symbols.length - 1; i >= 0; i--) {
const match = findLastMatchInSubtree(symbols[i], kind);
if (match) return match;
}
return null;
}
// /**
// * Get the deepest match of a given kind starting from the last child path.
// * @param {vscode.DocumentSymbol[]} symbols - The top-level symbols.
// * @param {vscode.SymbolKind} targetKind - The SymbolKind to match.
// * @returns {vscode.DocumentSymbol | null}
// */
// function getDeepestKindFromLastChild(symbols, targetKind) {
// let match = null;
// function recurse(symbol) {
// if (symbol.kind === targetKind) {
// match = symbol; // Store latest match along the descent
// }
// const children = symbol.children ?? [];
// if (children.length > 0) {
// recurse(children[children.length - 1]); // Only follow the last child
// }
// }
// if (symbols.length > 0) {
// recurse(symbols[symbols.length - 1]); // Start from the last top-level symbol
// }
// return match;
// }
// /**
// * Get the deepest match of a specific kind, favoring last-child branches,
// * with fallback to earlier siblings if needed.
// * @param {vscode.DocumentSymbol[]} symbols
// * @param {vscode.SymbolKind} targetKind
// * @returns {vscode.DocumentSymbol | null}
// */
// function getDeepestKindFlexible(symbols, targetKind) {
// let bestMatch = null;
// let bestDepth = -1;
// /**
// * @param {vscode.DocumentSymbol} symbol
// * @param {number} depth
// */
// function recurse(symbol, depth) {
// if (symbol.kind === targetKind && depth > bestDepth) {
// bestMatch = symbol;
// bestDepth = depth;
// }
// const children = symbol.children ?? [];
// for (let i = children.length - 1; i >= 0; i--) {
// recurse(children[i], depth + 1);
// }
// }
// for (let i = symbols.length - 1; i >= 0; i--) {
// recurse(symbols[i], 0);
// }
// return bestMatch;
// }
// /**
// * Recursively searches for the deepest kind match in a subtree.
// * @param {vscode.DocumentSymbol} symbol - The root symbol.
// * @param {vscode.SymbolKind} targetKind - The kind to search for.
// * @param {number} depth - Current recursion depth.
// * @returns {{ symbol: vscode.DocumentSymbol | null, depth: number }}
// */
// function findDeepestInSubtree(symbol, targetKind, depth = 0) {
// let best = {symbol: null, depth: -1};
// if (symbol.kind === targetKind) {
// best = {symbol, depth};
// }
// const children = symbol.children ?? [];
// for (const child of children) {
// const result = findDeepestInSubtree(child, targetKind, depth + 1);
// if (result.depth > best.depth) {
// best = result;
// }
// }
// return best;
// }
// /**
// * Searches top-level symbols in reverse for deepest kind match.
// * @param {vscode.DocumentSymbol[]} symbols - The top-level symbols.
// * @param {vscode.SymbolKind} targetKind - The kind to find.
// * @returns {vscode.DocumentSymbol | null}
// */
// function getDeepestMatchFromLastUp(symbols, targetKind) {
// for (let i = symbols.length - 1; i >= 0; i--) {
// const result = findDeepestInSubtree(symbols[i], targetKind);
// if (result.symbol) {
// return result.symbol; // 🎯 Return the first deepest match found from the last symbol upward
// }
// }
// return null;
// }
// /**
// * Recursively find the first match of a specific SymbolKind within a subtree.
// * Stops immediately once a match is found.
// * @param {vscode.DocumentSymbol} symbol
// * @param {vscode.SymbolKind} kind
// * @returns {vscode.DocumentSymbol | null}
// */
// function findFirstInSubtree(symbol, kind) {
// if (symbol.kind === kind) return symbol;
// const children = symbol.children ?? [];
// for (const child of children) {
// const result = findFirstInSubtree(child, kind);
// if (result) return result;
// }
// return null;
// }
// /**
// * Traverse top-level symbols in reverse, finding the first match from the last symbol upward.
// * Stops entirely after the first match.
// * @param {vscode.DocumentSymbol[]} symbols
// * @param {vscode.SymbolKind} kind
// * @returns {vscode.DocumentSymbol | null}
// */
// function getFirstMatchFromLastUp(symbols, kind) {
// for (let i = symbols.length - 1; i >= 0; i--) {
// const match = findFirstInSubtree(symbols[i], kind);
// if (match) return match;
// }
// return null;
// }
/**
* @param {vscode.DocumentSymbol} parent
* @param {string} kbWhere - nextStart, currentStart, etc.
* @param {SymMap} symMap
* @param {SymMapKey[]} kbSymbol
* @param {vscode.Selection} selection
* @param {vscode.DocumentSymbol[]} result
* @param {vscode.TextDocument} document
* @returns {Promise<vscode.DocumentSymbol[]>}
*/
async function previousRecursion(parent, kbWhere, symMap, kbSymbol, selection, result, document) {
result.push(parent);
/** @type {vscode.DocumentSymbol[] } */
// const sortedChildren = parent.children.sort(compareRanges);
const sortedChildren = parent.children.sort(compareRanges).toReversed();
for await (const child of sortedChildren) {
// sortedChildren.forEach(child => {
if (child.children.length) {
const extendedTargetRange = extendSelection(child, kbWhere, document);
// if (extendedTargetRange.contains(selection.active)) {
if (extendedTargetRange.start.isBefore(selection.active)) {
// result.push(parent);
// result.push(child);
await previousRecursion(child, kbWhere, symMap, kbSymbol, selection, result, document);
}
}
// rest are leaf nodes
// else if (kbWhere.startsWith("previous")) {
// if (kbWhere.startsWith("previous")) {
// else if (!child.children.length) {
else {
// const previousMatchingSymbol = sortedChildren.findLast(({kind, range}) => {
// const previousMatchingSymbol = sortedChildren.find(child => {
let previousMatchingSymbol;
if (Object.values(symMap).includes(child.kind)) previousMatchingSymbol = child;
// if (Object.values(symMap).includes(child.kind)) return true;
else if (arrowFunctionRanges) {
const isArrowFunction = globalThis.usesArrowFunctions ?
!!arrowFunctionRanges.find(arrowRange => arrowRange.isEqual(child.range)) : false;
// return isArrowFunction;
if (isArrowFunction) previousMatchingSymbol = child;
}
// });
// if (previousMatchingSymbol) result.push(previousMatchingSymbol);
if (previousMatchingSymbol) {
// result.push(parent);
// result.push(parent);
// result.push(child);
result.push(previousMatchingSymbol);
// return true;
}
// }
// });
}
}
// });
return result;
}
// if (Object.values(symMap).includes(symbol.kind)) result.push(symbol);
// else if (arrowFunctionRanges) {
// let isArrowFunction = globalThis.usesArrowFunctions ?
// !!arrowFunctionRanges.find(arrowRange => {
// return arrowRange.isEqual(symbol.range);
// }) : false;
// if (isArrowFunction) result.push(symbol);
// }
// /** @type { any } */
// const bad = checkArgs(structuredClone(args));
// if (bad && Object.keys(bad).length) { // not empty
// console.log(bad);
// let message = "";
// if (bad.symbol.length === 1)
// message += `The "symbol" option "${bad.symbol[0]}" is an error. `;
// else if (bad.symbol.length > 1)
// message += `The "symbol" options "${bad.symbol.join('" and "')}" are errors. `;
// if (bad.where)
// message += `The "where" option "${bad.where}" is an error. `;
// if (bad.select)
// message += `The "select" option "${bad.select}" is not allowed. "select" must be a boolean.`;
// return await window.showErrorMessage(
// message,
// {modal: true},
// ...['Go to keybindings']) // two buttons - Cancel will be added
// .then(selected => {
// if (selected === 'Go to keybindings') commands.executeCommand('workbench.action.openGlobalKeybindingsFile');
// // any way to navigate to this particular keybinding?
// // Opening a file at a specific line and column: vscode:;//file/{full-path-to-file}:{line}:{column}
// else commands.executeCommand('leaveEditorMessage');
// });
// }
// /**
// *
// * @param { Object } args
// * @param { string | string[] } args.symbol
// * @param { string } args.where
// * @param { boolean } args.select
// *
// * @returns { Object } bad
// * @property { string[] } [symbol]
// * @property { string } [where]
// * @property { string } [select]
// */
// function checkArgs(args) {
// /** @type {{ symbol: string[], where: string, select: boolean|string }} */
// let bad = {};
// if (!Array.isArray(args.symbol))
// args.symbol = [args.symbol];
// const symbols = ["class", "method", "function"];
// const wheres = [
// "previousStart", "previousEnd", "currentStart", "currentEnd", "nextStart",
// "nextEnd", "parentStart", "parentEnd", "childStart", "childEnd",
// "topScopeStart", "topScopeEnd"
// ];
// let /** @type { string[] } */ badSymbol = [];
// if (!!args.symbol && args.symbol[0] !== undefined) badSymbol = args.symbol.filter(kbSymbol => !symbols.includes(kbSymbol));
// if (badSymbol.length) bad.symbol = badSymbol;
// if (!!args.where && !wheres.includes(args.where)) bad.where = args.where;
// if (!!args.select && typeof args.select !== "boolean") bad.select = args.select;
// return bad;
// }