@mrclrchtr/supi-code-intelligence 8.0.0 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +3 -2
  2. package/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  3. package/node_modules/@mrclrchtr/supi-code-runtime/src/types.ts +10 -1
  4. package/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  5. package/node_modules/@mrclrchtr/supi-core/src/debug-registry.ts +9 -4
  6. package/node_modules/@mrclrchtr/supi-core/src/session-capabilities.ts +55 -1
  7. package/node_modules/@mrclrchtr/supi-core/src/session-capability-profiles.ts +240 -0
  8. package/node_modules/@mrclrchtr/supi-core/src/session.ts +22 -0
  9. package/node_modules/@mrclrchtr/supi-lsp/README.md +1 -0
  10. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  11. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/src/types.ts +10 -1
  12. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  13. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-core/src/debug-registry.ts +9 -4
  14. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-core/src/session-capabilities.ts +55 -1
  15. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-core/src/session-capability-profiles.ts +240 -0
  16. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-core/src/session.ts +22 -0
  17. package/node_modules/@mrclrchtr/supi-lsp/node_modules/ignore/README.md +6 -9
  18. package/node_modules/@mrclrchtr/supi-lsp/node_modules/ignore/index.js +202 -106
  19. package/node_modules/@mrclrchtr/supi-lsp/node_modules/ignore/legacy.js +164 -88
  20. package/node_modules/@mrclrchtr/supi-lsp/node_modules/ignore/package.json +1 -1
  21. package/node_modules/@mrclrchtr/supi-lsp/package.json +3 -3
  22. package/node_modules/@mrclrchtr/supi-lsp/src/client/client.ts +14 -3
  23. package/node_modules/@mrclrchtr/supi-lsp/src/config/defaults.json +6 -0
  24. package/node_modules/@mrclrchtr/supi-lsp/src/diagnostics/evidence.ts +1 -1
  25. package/node_modules/@mrclrchtr/supi-lsp/src/manager/manager.ts +1 -1
  26. package/node_modules/@mrclrchtr/supi-lsp/src/provider/refactor-planning.ts +1 -0
  27. package/node_modules/@mrclrchtr/supi-lsp/src/provider/semantic-edit-normalizer.ts +1 -1
  28. package/node_modules/@mrclrchtr/supi-lsp/src/utils.ts +2 -0
  29. package/node_modules/@mrclrchtr/supi-tree-sitter/README.md +12 -3
  30. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  31. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/src/types.ts +10 -1
  32. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  33. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-core/src/debug-registry.ts +9 -4
  34. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-core/src/session-capabilities.ts +55 -1
  35. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-core/src/session-capability-profiles.ts +240 -0
  36. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-core/src/session.ts +22 -0
  37. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/web-tree-sitter/debug/web-tree-sitter.wasm +0 -0
  38. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/web-tree-sitter/debug/web-tree-sitter.wasm.map +7 -7
  39. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/web-tree-sitter/package.json +1 -1
  40. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/web-tree-sitter/web-tree-sitter.wasm +0 -0
  41. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/web-tree-sitter/web-tree-sitter.wasm.map +7 -7
  42. package/node_modules/@mrclrchtr/supi-tree-sitter/package.json +6 -4
  43. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/kotlin/tree-sitter-kotlin.wasm.json +1 -1
  44. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/latex/LICENSE +21 -0
  45. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/latex/tree-sitter-latex.wasm +0 -0
  46. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/latex/tree-sitter-latex.wasm.json +17 -0
  47. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/sql/tree-sitter-sql.wasm.json +1 -1
  48. package/node_modules/@mrclrchtr/supi-tree-sitter/src/language.ts +5 -1
  49. package/node_modules/@mrclrchtr/supi-tree-sitter/src/operation-support.ts +6 -0
  50. package/node_modules/@mrclrchtr/supi-tree-sitter/src/provider/tree-sitter-provider.ts +2 -0
  51. package/node_modules/@mrclrchtr/supi-tree-sitter/src/session/structural-worker-client.ts +45 -34
  52. package/node_modules/@mrclrchtr/supi-tree-sitter/src/session/structural-worker-process.ts +122 -0
  53. package/node_modules/@mrclrchtr/supi-tree-sitter/src/session/structural-worker-protocol.ts +1 -1
  54. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/callees.ts +1 -0
  55. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/imports.ts +55 -1
  56. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/outline-latex.ts +288 -0
  57. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/outline-polyglot.ts +8 -1
  58. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/outline.ts +18 -4
  59. package/node_modules/@mrclrchtr/supi-tree-sitter/src/types.ts +11 -3
  60. package/node_modules/@mrclrchtr/supi-tree-sitter/src/worker/process-host.mjs +92 -0
  61. package/package.json +5 -5
  62. package/src/analysis/refactor/latex-section-rename.ts +343 -0
  63. package/src/analysis/target/anchored-structural.ts +148 -0
  64. package/src/analysis/target/anchored.ts +15 -124
  65. package/src/analysis/target/file-refinement.ts +51 -0
  66. package/src/analysis/target/file.ts +49 -47
  67. package/src/analysis/target/identity.ts +147 -29
  68. package/src/analysis/target/latex-label-container.ts +137 -0
  69. package/src/analysis/target/latex-section-anchor.ts +116 -0
  70. package/src/analysis/target/latex-target-anchor.ts +17 -0
  71. package/src/overview/overview-data.ts +1 -0
  72. package/src/session/inspect/collect.ts +3 -1
  73. package/src/session/refactor-plans.ts +3 -1
  74. package/src/session/refactor-workflow.ts +127 -49
  75. package/src/tool/code_inspect/markdown.ts +1 -1
  76. package/src/tool/result/refactor.ts +24 -6
@@ -0,0 +1,343 @@
1
+ import { createHash } from "node:crypto";
2
+ import { readFileSync } from "node:fs";
3
+ import type {
4
+ CodeRequestControl,
5
+ FileEdit,
6
+ OutlineData,
7
+ RefactorOperation,
8
+ RefactorResult,
9
+ StructuralProvider,
10
+ WorkspaceEdit,
11
+ } from "@mrclrchtr/supi-code-runtime/api";
12
+ import { isCodeRequestInterruption } from "@mrclrchtr/supi-code-runtime/api";
13
+ import type { TargetStoreEntry } from "../../session/target-store.ts";
14
+ import { canonicalFileDeclarationKind } from "../target/identity.ts";
15
+ import { createLogicalLineIndex } from "./position.ts";
16
+
17
+ export interface LatexSectionTitleLocation {
18
+ readonly file: string;
19
+ readonly name: string;
20
+ readonly range: FileEdit["range"];
21
+ readonly sourceFingerprint: string;
22
+ }
23
+
24
+ const UNSUPPORTED_SECTION_RENAME_REASON =
25
+ "LSP request textDocument/rename failed: Rename is unavailable at the requested position.";
26
+ const SHA256_PATTERN = /^[a-f0-9]{64}$/u;
27
+ const UNSUPPORTED_TITLE_CHARACTERS = /[\\{}%$#&^~_[\]()=]/u;
28
+
29
+ /** Check whether a target is a LaTeX section symbol. */
30
+ export function isLatexSectionTarget(
31
+ target: Pick<TargetStoreEntry, "file" | "kind" | "name">,
32
+ ): boolean {
33
+ return canonicalFileDeclarationKind(target.file, target.kind, target.name) === "latex-section";
34
+ }
35
+
36
+ /** Find a safe literal title that still matches the resolved source snapshot. */
37
+ export async function findLatexSectionTitleLocation(input: {
38
+ readonly target: TargetStoreEntry;
39
+ readonly structural: Pick<StructuralProvider, "outline"> | null;
40
+ readonly control?: CodeRequestControl;
41
+ }): Promise<LatexSectionTitleLocation | { readonly reason: string }> {
42
+ const { target, structural, control } = input;
43
+ const targetReason = validateSectionTarget(target);
44
+ if (targetReason) return { reason: targetReason };
45
+ if (!structural)
46
+ return { reason: "Tree-sitter section evidence is not available for this target." };
47
+
48
+ const before = readSourceSnapshot(target.file);
49
+ if (!before) return { reason: "The LaTeX source file could not be read." };
50
+ if (before.fingerprint !== target.fileFingerprint) {
51
+ return { reason: "The LaTeX source changed after the target was resolved." };
52
+ }
53
+
54
+ try {
55
+ const checked = await readStableSectionOutline(target, structural.outline, before, control);
56
+ if ("reason" in checked) return checked;
57
+ return createTitleLocation(target, checked.outline, checked.snapshot);
58
+ } catch (error) {
59
+ if (isCodeRequestInterruption(error, control)) throw error;
60
+ return { reason: "Tree-sitter could not verify the current LaTeX section title." };
61
+ }
62
+ }
63
+
64
+ /** Check whether a provider edit changes only the exact section title. */
65
+ export function isExactLatexSectionTitleEdit(
66
+ edits: WorkspaceEdit,
67
+ location: LatexSectionTitleLocation,
68
+ newName: string,
69
+ ): boolean {
70
+ if (edits.edits.length !== 1) return false;
71
+ const [edit] = edits.edits;
72
+ return (
73
+ edit !== undefined &&
74
+ edit.file === location.file &&
75
+ edit.newText === newName &&
76
+ edit.range.start.line === location.range.start.line &&
77
+ edit.range.start.character === location.range.start.character &&
78
+ edit.range.end.line === location.range.end.line &&
79
+ edit.range.end.character === location.range.end.character
80
+ );
81
+ }
82
+
83
+ /** Build a local title-only plan from the routed LSP authority. */
84
+ export function createLatexSectionTitleEdit(input: {
85
+ readonly location: LatexSectionTitleLocation;
86
+ readonly newName: string;
87
+ readonly documentVersion: number | null;
88
+ readonly authorizedMutationRoots: readonly string[];
89
+ }):
90
+ | {
91
+ readonly edits: WorkspaceEdit;
92
+ readonly authorizedMutationRoots: string[];
93
+ }
94
+ | { readonly reason: string } {
95
+ const { location, newName, documentVersion, authorizedMutationRoots } = input;
96
+ if (newName === location.name || !isSafeNewTitleText(newName)) {
97
+ return {
98
+ reason: "The local LaTeX rename accepts one plain-text title supported by the parser.",
99
+ };
100
+ }
101
+ if (authorizedMutationRoots.length === 0) {
102
+ return {
103
+ reason: "The routed LSP client did not authorize a mutation root for this title edit.",
104
+ };
105
+ }
106
+
107
+ const edit: FileEdit = {
108
+ file: location.file,
109
+ range: location.range,
110
+ newText: newName,
111
+ };
112
+ return {
113
+ edits: {
114
+ edits: [edit],
115
+ documentPreconditions: [
116
+ documentVersion === null
117
+ ? { file: location.file, kind: "disk-content" }
118
+ : { file: location.file, kind: "open-document-version", version: documentVersion },
119
+ ],
120
+ },
121
+ authorizedMutationRoots: [...authorizedMutationRoots],
122
+ };
123
+ }
124
+
125
+ /** Check and refine one LaTeX section rename result. */
126
+ export async function refineLatexSectionRenameResult(input: {
127
+ readonly operation: RefactorOperation;
128
+ readonly result: RefactorResult;
129
+ readonly target: TargetStoreEntry;
130
+ readonly file: string;
131
+ readonly newName: string | undefined;
132
+ readonly structural: Pick<StructuralProvider, "outline"> | null;
133
+ readonly documentVersionReader?: (file: string) => number | null;
134
+ readonly control?: CodeRequestControl;
135
+ }): Promise<{
136
+ result: RefactorResult;
137
+ evidenceSource: "semantic" | "structural";
138
+ sourceFingerprint?: string;
139
+ }> {
140
+ const semantic = { result: input.result, evidenceSource: "semantic" as const };
141
+ if (
142
+ input.operation !== "rename_symbol" ||
143
+ !isLatexSectionTarget(input.target) ||
144
+ input.result.kind === "ambiguous" ||
145
+ (input.result.kind === "unavailable" &&
146
+ input.result.reason !== UNSUPPORTED_SECTION_RENAME_REASON)
147
+ ) {
148
+ return semantic;
149
+ }
150
+
151
+ const location = await findLatexSectionTitleLocation({
152
+ target: input.target,
153
+ structural: input.structural,
154
+ control: input.control,
155
+ });
156
+ if ("reason" in location) return unavailableSectionRename(location.reason);
157
+ if (input.result.kind === "precise") {
158
+ return isExactLatexSectionTitleEdit(input.result.edits, location, input.newName ?? "")
159
+ ? { ...semantic, sourceFingerprint: location.sourceFingerprint }
160
+ : unavailableSectionRename(
161
+ "The LSP rename did not return one edit for the exact section title. No rename was planned.",
162
+ );
163
+ }
164
+
165
+ const versionReader = input.documentVersionReader;
166
+ if (!versionReader) {
167
+ return unavailableSectionRename(
168
+ "The current document state cannot be checked for a safe title edit.",
169
+ );
170
+ }
171
+ const localEdit = createLatexSectionTitleEdit({
172
+ location,
173
+ newName: input.newName ?? "",
174
+ documentVersion: versionReader(input.file),
175
+ authorizedMutationRoots: input.result.authorizedMutationRoots ?? [],
176
+ });
177
+ if ("reason" in localEdit) return unavailableSectionRename(localEdit.reason);
178
+ return {
179
+ result: {
180
+ kind: "precise",
181
+ edits: localEdit.edits,
182
+ authorizedMutationRoots: localEdit.authorizedMutationRoots,
183
+ },
184
+ evidenceSource: "structural",
185
+ sourceFingerprint: location.sourceFingerprint,
186
+ };
187
+ }
188
+
189
+ function unavailableSectionRename(reason: string): {
190
+ result: RefactorResult;
191
+ evidenceSource: "semantic";
192
+ } {
193
+ return { result: { kind: "unavailable", reason }, evidenceSource: "semantic" };
194
+ }
195
+
196
+ function validateSectionTarget(target: TargetStoreEntry): string | null {
197
+ if (!isLatexSectionTarget(target) || target.name === null || target.anchorKind !== "name") {
198
+ return "The target is not an anchored LaTeX section title.";
199
+ }
200
+ if (!target.declarationPosition) return "The target has no verified section declaration anchor.";
201
+ if (!SHA256_PATTERN.test(target.fileFingerprint)) {
202
+ return "The resolved LaTeX target has no verified source fingerprint.";
203
+ }
204
+ return null;
205
+ }
206
+
207
+ async function readStableSectionOutline(
208
+ target: TargetStoreEntry,
209
+ outline: StructuralProvider["outline"],
210
+ before: { readonly source: string; readonly fingerprint: string },
211
+ control?: CodeRequestControl,
212
+ ): Promise<
213
+ | {
214
+ readonly outline: readonly OutlineData[];
215
+ readonly snapshot: { readonly source: string; readonly fingerprint: string };
216
+ }
217
+ | { readonly reason: string }
218
+ > {
219
+ const result = await outline(target.file, control);
220
+ const after = readSourceSnapshot(target.file);
221
+ if (!after) return { reason: "The LaTeX source file could not be read." };
222
+ if (after.fingerprint !== before.fingerprint) {
223
+ return { reason: "The LaTeX source changed while Tree-sitter checked the section." };
224
+ }
225
+ if (result.kind !== "success") {
226
+ return { reason: "Tree-sitter could not inspect the LaTeX section." };
227
+ }
228
+ return { outline: result.data, snapshot: after };
229
+ }
230
+
231
+ function createTitleLocation(
232
+ target: TargetStoreEntry,
233
+ outline: readonly OutlineData[],
234
+ snapshot: { readonly source: string; readonly fingerprint: string },
235
+ ): LatexSectionTitleLocation | { readonly reason: string } {
236
+ const matches = findSectionMatches(outline, target);
237
+ if (matches.length !== 1) {
238
+ return { reason: "Tree-sitter did not find one exact title for this LaTeX section." };
239
+ }
240
+ const item = matches[0];
241
+ if (!item?.nameAnchor || !item.nameEndAnchor) {
242
+ return { reason: "The LaTeX section title contains TeX commands or mixed content." };
243
+ }
244
+
245
+ const start = { line: item.nameAnchor.line - 1, character: item.nameAnchor.character - 1 };
246
+ const end = { line: item.nameEndAnchor.line - 1, character: item.nameEndAnchor.character - 1 };
247
+ const literalTitle = readSourceRange(snapshot.source, start, end);
248
+ if (
249
+ !literalTitle ||
250
+ !isSafeExistingTitleText(literalTitle) ||
251
+ normalizeTitleWhitespace(literalTitle) !== target.name
252
+ ) {
253
+ return { reason: "The current source does not match one plain literal section title." };
254
+ }
255
+ return {
256
+ file: target.file,
257
+ name: target.name,
258
+ range: { start, end },
259
+ sourceFingerprint: snapshot.fingerprint,
260
+ };
261
+ }
262
+
263
+ function findSectionMatches(
264
+ items: readonly OutlineData[],
265
+ target: TargetStoreEntry,
266
+ ): OutlineData[] {
267
+ const declarationPosition = target.declarationPosition;
268
+ if (!declarationPosition) return [];
269
+ const matches: OutlineData[] = [];
270
+ for (const item of items) {
271
+ if (
272
+ canonicalFileDeclarationKind(target.file, item.kind, item.name) === "latex-section" &&
273
+ item.name === target.name &&
274
+ item.startLine === declarationPosition.line + 1 &&
275
+ item.startCharacter === declarationPosition.character + 1 &&
276
+ item.nameAnchor?.line === target.displayLine &&
277
+ item.nameAnchor?.character === target.displayCharacter &&
278
+ item.nameEndAnchor !== undefined
279
+ ) {
280
+ matches.push(item);
281
+ }
282
+ matches.push(...findSectionMatches(item.children ?? [], target));
283
+ }
284
+ return matches;
285
+ }
286
+
287
+ function readSourceRange(
288
+ source: string,
289
+ start: { readonly line: number; readonly character: number },
290
+ end: { readonly line: number; readonly character: number },
291
+ ): string | null {
292
+ try {
293
+ const lineIndex = createLogicalLineIndex(source);
294
+ return source.slice(lineIndex.offsetAt(start), lineIndex.offsetAt(end));
295
+ } catch {
296
+ return null;
297
+ }
298
+ }
299
+
300
+ function readSourceSnapshot(file: string): { source: string; fingerprint: string } | null {
301
+ try {
302
+ const bytes = readFileSync(file);
303
+ return {
304
+ source: bytes.toString("utf8"),
305
+ fingerprint: createHash("sha256").update(bytes).digest("hex"),
306
+ };
307
+ } catch {
308
+ return null;
309
+ }
310
+ }
311
+
312
+ function normalizeTitleWhitespace(value: string): string {
313
+ return value.replace(/\s+/gu, " ").trim();
314
+ }
315
+
316
+ function isSafeExistingTitleText(value: string): boolean {
317
+ return (
318
+ value.length > 0 &&
319
+ value.trim() === value &&
320
+ !UNSUPPORTED_TITLE_CHARACTERS.test(value) &&
321
+ !hasUnsafeControlCharacter(value, true)
322
+ );
323
+ }
324
+
325
+ function isSafeNewTitleText(value: string): boolean {
326
+ return (
327
+ value.length > 0 &&
328
+ value.trim() === value &&
329
+ !UNSUPPORTED_TITLE_CHARACTERS.test(value) &&
330
+ !hasUnsafeControlCharacter(value, false)
331
+ );
332
+ }
333
+
334
+ function hasUnsafeControlCharacter(value: string, allowWhitespace: boolean): boolean {
335
+ for (const character of value) {
336
+ const code = character.codePointAt(0);
337
+ if (!allowWhitespace && /\s/u.test(character) && character !== " ") return true;
338
+ if (code === undefined || (code >= 0x20 && code !== 0x7f)) continue;
339
+ if (allowWhitespace && (code === 0x09 || code === 0x0a || code === 0x0d)) continue;
340
+ return true;
341
+ }
342
+ return false;
343
+ }
@@ -0,0 +1,148 @@
1
+ import { basename } from "node:path";
2
+ import {
3
+ type CodeRequestControl,
4
+ type CodeResult,
5
+ isCodeRequestInterruption,
6
+ type NodeAtData,
7
+ type StructuralProvider,
8
+ } from "@mrclrchtr/supi-code-runtime/api";
9
+ import type { AnchoredResolutionMetadata } from "../../types/index.ts";
10
+ import { resolveLatexTargetAtAnchor } from "./latex-target-anchor.ts";
11
+ import type { TargetOutcome } from "./types.ts";
12
+
13
+ type Anchor = { line: number; character: number };
14
+
15
+ /** Tree-sitter declaration nodes with names. */
16
+ const DECLARATION_NODE_TYPES = new Set([
17
+ "function_declaration",
18
+ "generator_function_declaration",
19
+ "function_signature",
20
+ "method_definition",
21
+ "method_declaration",
22
+ "method_signature",
23
+ "class_declaration",
24
+ "interface_declaration",
25
+ "enum_declaration",
26
+ "type_alias_declaration",
27
+ "lexical_declaration",
28
+ "variable_declaration",
29
+ "export_statement",
30
+ ]);
31
+
32
+ /** Tree-sitter nodes that cannot identify a declaration name. */
33
+ const NON_SYMBOL_NODE_TYPES = new Set([
34
+ "comment",
35
+ "line_comment",
36
+ "block_comment",
37
+ "string",
38
+ "string_fragment",
39
+ "template_string",
40
+ "template_literal_type",
41
+ "regex",
42
+ "regex_pattern",
43
+ "number",
44
+ "escape_sequence",
45
+ ]);
46
+
47
+ function kindFromDeclarationType(nodeType: string): string | null {
48
+ if (nodeType.includes("function")) return "Function";
49
+ if (nodeType.includes("method")) return "Method";
50
+ if (nodeType.includes("class")) return "Class";
51
+ if (nodeType.includes("interface")) return "Interface";
52
+ if (nodeType.includes("enum")) return "Enum";
53
+ if (nodeType.includes("type_alias")) return "Type";
54
+ if (nodeType.includes("lexical") || nodeType.includes("variable")) return "Variable";
55
+ return null;
56
+ }
57
+
58
+ /** Resolve a structural declaration name at one source position. */
59
+ export async function resolveFromStructural(
60
+ file: string,
61
+ requested: Anchor,
62
+ provider: Partial<Pick<StructuralProvider, "nodeAt" | "outline">> | null,
63
+ control?: CodeRequestControl,
64
+ ): Promise<TargetOutcome | null> {
65
+ if (!provider) return null;
66
+ const latexTarget = await resolveLatexTargetAtAnchor({
67
+ file,
68
+ line: requested.line,
69
+ character: requested.character,
70
+ outline: provider.outline,
71
+ control,
72
+ });
73
+ if (latexTarget) return { kind: "resolved", target: latexTarget };
74
+ if (!provider.nodeAt) return null;
75
+
76
+ let nodeResult: CodeResult<NodeAtData> | null = null;
77
+ try {
78
+ nodeResult = await provider.nodeAt(file, requested.line, requested.character, control);
79
+ } catch (error) {
80
+ if (isCodeRequestInterruption(error, control)) throw error;
81
+ }
82
+ if (nodeResult?.kind !== "success") return null;
83
+
84
+ const node = nodeResult.data;
85
+ const isLabelKey = node.ancestry.some((ancestor) => ancestor.type === "label_definition");
86
+ if (isLabelKey || NON_SYMBOL_NODE_TYPES.has(node.type)) {
87
+ return {
88
+ kind: "error",
89
+ message: coordinateNotOnSymbolMessage(
90
+ file,
91
+ requested,
92
+ isLabelKey ? "unverified LaTeX label key" : node.type,
93
+ ),
94
+ };
95
+ }
96
+ if (node.type !== "identifier") {
97
+ return {
98
+ kind: "error",
99
+ message: coordinateNotOnSymbolMessage(file, requested, node.type),
100
+ };
101
+ }
102
+
103
+ const declaration = node.ancestry.find((ancestor) => DECLARATION_NODE_TYPES.has(ancestor.type));
104
+ if (!declaration) {
105
+ return {
106
+ kind: "error",
107
+ message: coordinateNotOnSymbolMessage(file, requested, "identifier usage"),
108
+ };
109
+ }
110
+
111
+ const anchor = { line: node.startLine, character: node.startCharacter };
112
+ const declarationAnchor = {
113
+ line: declaration.startLine,
114
+ character: declaration.startCharacter,
115
+ };
116
+ return {
117
+ kind: "resolved",
118
+ target: {
119
+ file,
120
+ position: { line: anchor.line - 1, character: anchor.character - 1 },
121
+ displayLine: anchor.line,
122
+ displayCharacter: anchor.character,
123
+ declarationAnchor,
124
+ declarationOccurrence: 0,
125
+ name: node.text,
126
+ kind: kindFromDeclarationType(declaration.type),
127
+ confidence: "structural",
128
+ provenance: ["structural"],
129
+ anchorKind: "name",
130
+ container: null,
131
+ resolution: {
132
+ requested: { ...requested },
133
+ resolved: anchor,
134
+ snapped: anchor.line !== requested.line || anchor.character !== requested.character,
135
+ source: "structural-identifier",
136
+ } satisfies AnchoredResolutionMetadata,
137
+ },
138
+ };
139
+ }
140
+
141
+ function coordinateNotOnSymbolMessage(file: string, requested: Anchor, detail: string): string {
142
+ const at = `${basename(file)}:${requested.line}:${requested.character}`;
143
+ return (
144
+ `No symbol target resolved at \`${at}\` (on \`${detail}\`). ` +
145
+ "`code_resolve` resolves real symbol targets from provider-backed evidence; " +
146
+ "use `code_inspect` for point-level facts at this coordinate, or pass the identifier coordinate of a declaration."
147
+ );
148
+ }
@@ -11,19 +11,20 @@ import * as fs from "node:fs";
11
11
  import * as path from "node:path";
12
12
  import {
13
13
  type CodeRequestControl,
14
- type CodeResult,
15
14
  type CodeSymbol,
16
15
  isCodeRequestInterruption,
17
- type NodeAtData,
18
16
  type SemanticProvider,
19
17
  type StructuralProvider,
20
18
  } from "@mrclrchtr/supi-code-runtime/api";
21
19
  import type { AnchorKind } from "../../session/target-store.ts";
22
20
  import type { AnchoredResolutionMetadata, AnchoredResolutionSource } from "../../types/index.ts";
21
+ import { resolveFromStructural } from "./anchored-structural.ts";
23
22
  import {
23
+ canonicalFileDeclarationKind,
24
24
  createCodeSymbolIdentityResolver,
25
25
  type DeclarationOccurrenceIdentity,
26
26
  } from "./identity.ts";
27
+ import { resolveTexlabSectionLabelCollision } from "./latex-label-container.ts";
27
28
  import type { DisambiguationCandidateData, ResolvedTargetData, TargetOutcome } from "./types.ts";
28
29
 
29
30
  /** 1-based symbol anchor position (mirrors the runtime `SymbolAnchor`). */
@@ -62,57 +63,11 @@ function isBinaryFile(filePath: string): boolean {
62
63
 
63
64
  // ── Provider-backed anchored symbol resolution ────────────────────────
64
65
 
65
- /**
66
- * Minimal provider surface used by anchored symbol resolution — only the
67
- * methods the resolver actually consults. The composite `CodeProvider` from
68
- * `request-context.ts` satisfies this shape, as do focused test doubles.
69
- */
66
+ /** Provider methods used by anchored resolution. */
70
67
  export interface AnchoredResolverProvider {
71
68
  documentSymbols?: SemanticProvider["documentSymbols"];
72
69
  nodeAt?: StructuralProvider["nodeAt"];
73
- }
74
-
75
- /** Tree-sitter node types that introduce a named declaration. */
76
- const DECLARATION_NODE_TYPES = new Set([
77
- "function_declaration",
78
- "generator_function_declaration",
79
- "function_signature",
80
- "method_definition",
81
- "method_declaration",
82
- "method_signature",
83
- "class_declaration",
84
- "interface_declaration",
85
- "enum_declaration",
86
- "type_alias_declaration",
87
- "lexical_declaration",
88
- "variable_declaration",
89
- "export_statement",
90
- ]);
91
-
92
- /** Tree-sitter node types that represent non-symbol tokens. */
93
- const NON_SYMBOL_NODE_TYPES = new Set([
94
- "comment",
95
- "line_comment",
96
- "block_comment",
97
- "string",
98
- "string_fragment",
99
- "template_string",
100
- "template_literal_type",
101
- "regex",
102
- "regex_pattern",
103
- "number",
104
- "escape_sequence",
105
- ]);
106
-
107
- function kindFromDeclarationType(nodeType: string): string | null {
108
- if (nodeType.includes("function")) return "Function";
109
- if (nodeType.includes("method")) return "Method";
110
- if (nodeType.includes("class")) return "Class";
111
- if (nodeType.includes("interface")) return "Interface";
112
- if (nodeType.includes("enum")) return "Enum";
113
- if (nodeType.includes("type_alias")) return "Type";
114
- if (nodeType.includes("lexical") || nodeType.includes("variable")) return "Variable";
115
- return null;
70
+ outline?: StructuralProvider["outline"];
116
71
  }
117
72
 
118
73
  /**
@@ -174,7 +129,8 @@ function resolvedFromSymbol(
174
129
  declarationOccurrence: opts.identity.declarationOccurrence,
175
130
  name: s.name,
176
131
  kind: s.kind,
177
- identityKind: opts.identity.identityKind,
132
+ identityKind:
133
+ canonicalFileDeclarationKind(file, s.kind, s.name) ?? opts.identity.identityKind,
178
134
  confidence: "semantic",
179
135
  provenance: opts.identity.structuralEvidence ? ["semantic", "structural"] : ["semantic"],
180
136
  anchorKind: (s.nameAnchor ? "name" : "declaration") as AnchorKind,
@@ -202,7 +158,7 @@ async function candidatesFromSymbols(
202
158
  return {
203
159
  name: s.name,
204
160
  kind: s.kind,
205
- identityKind: identity.identityKind,
161
+ identityKind: canonicalFileDeclarationKind(file, s.kind, s.name) ?? identity.identityKind,
206
162
  provenance: identity.structuralEvidence
207
163
  ? (["semantic", "structural"] as const)
208
164
  : (["semantic"] as const),
@@ -276,77 +232,6 @@ async function resolveFromSemantic(
276
232
  return null;
277
233
  }
278
234
 
279
- /** Layer 2: classify the coordinate via tree-sitter `nodeAt`. Returns null to fall through. */
280
- async function resolveFromStructural(
281
- file: string,
282
- requested: Anchor,
283
- provider: AnchoredResolverProvider,
284
- control?: CodeRequestControl,
285
- ): Promise<TargetOutcome | null> {
286
- if (!provider.nodeAt) return null;
287
- let nodeResult: CodeResult<NodeAtData> | null = null;
288
- try {
289
- nodeResult = await provider.nodeAt(file, requested.line, requested.character);
290
- } catch (error) {
291
- if (isCodeRequestInterruption(error, control)) throw error;
292
- nodeResult = null;
293
- }
294
- if (nodeResult?.kind !== "success") return null;
295
-
296
- const node = nodeResult.data;
297
- if (NON_SYMBOL_NODE_TYPES.has(node.type)) {
298
- return {
299
- kind: "error",
300
- message: coordinateNotOnSymbolMessage(file, requested.line, requested.character, node.type),
301
- };
302
- }
303
- if (node.type !== "identifier") {
304
- // Keyword/modifier/operator etc. — no LSP header snap available.
305
- return {
306
- kind: "error",
307
- message: coordinateNotOnSymbolMessage(file, requested.line, requested.character, node.type),
308
- };
309
- }
310
-
311
- const declAncestor = node.ancestry.find((a) => DECLARATION_NODE_TYPES.has(a.type));
312
- if (!declAncestor) {
313
- // Identifier is a usage, not a declaration name.
314
- return {
315
- kind: "error",
316
- message: coordinateNotOnSymbolMessage(
317
- file,
318
- requested.line,
319
- requested.character,
320
- "identifier usage",
321
- ),
322
- };
323
- }
324
- const nameAnchor: Anchor = { line: node.startLine, character: node.startCharacter };
325
- const snapped =
326
- nameAnchor.line !== requested.line || nameAnchor.character !== requested.character;
327
- return {
328
- kind: "resolved",
329
- target: {
330
- file,
331
- position: { line: nameAnchor.line - 1, character: nameAnchor.character - 1 },
332
- displayLine: nameAnchor.line,
333
- displayCharacter: nameAnchor.character,
334
- declarationAnchor: {
335
- line: declAncestor.startLine,
336
- character: declAncestor.startCharacter,
337
- },
338
- declarationOccurrence: 0,
339
- name: node.text,
340
- kind: kindFromDeclarationType(declAncestor.type),
341
- confidence: "structural",
342
- provenance: ["structural"],
343
- anchorKind: "name",
344
- container: null,
345
- resolution: buildResolution(requested, nameAnchor, snapped, "structural-identifier"),
346
- },
347
- };
348
- }
349
-
350
235
  /**
351
236
  * Resolve a real symbol target from anchored coordinates using provider
352
237
  * evidence. Replaces the anonymous point-target behavior for `code_resolve`
@@ -387,7 +272,13 @@ export async function resolveAnchoredSymbolTarget(
387
272
  const requested: Anchor = { line, character };
388
273
  if (provider) {
389
274
  const semantic = await resolveFromSemantic(file, requested, provider, control);
390
- if (semantic) return semantic;
275
+ if (semantic) {
276
+ return (
277
+ (await resolveTexlabSectionLabelCollision(file, semantic, () =>
278
+ resolveFromStructural(file, requested, provider, control),
279
+ )) ?? semantic
280
+ );
281
+ }
391
282
  const structural = await resolveFromStructural(file, requested, provider, control);
392
283
  if (structural) return structural;
393
284
  }