@mrclrchtr/supi-code-intelligence 8.0.1 → 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,51 @@
1
+ import type { DiscoveredTargetData } from "./file.ts";
2
+ import { canonicalFileDeclarationKind } from "./identity.ts";
3
+
4
+ /** Refine a LaTeX section anchor with exact structural title evidence. */
5
+ export function refineLatexSectionNameAnchor(
6
+ semantic: DiscoveredTargetData,
7
+ structural: DiscoveredTargetData,
8
+ ): DiscoveredTargetData {
9
+ if (
10
+ canonicalFileDeclarationKind(semantic.file, semantic.kind, semantic.name) !== "latex-section" ||
11
+ canonicalFileDeclarationKind(structural.file, structural.kind, structural.name) !==
12
+ "latex-section" ||
13
+ semantic.name !== structural.name
14
+ ) {
15
+ return semantic;
16
+ }
17
+
18
+ return {
19
+ ...semantic,
20
+ position: { ...structural.position },
21
+ displayLine: structural.displayLine,
22
+ displayCharacter: structural.displayCharacter,
23
+ anchorKind: structural.anchorKind,
24
+ };
25
+ }
26
+
27
+ /** Prefer known hierarchy, but keep conflicts unknown. */
28
+ export function reconcileNesting(
29
+ semantic: DiscoveredTargetData["nesting"],
30
+ structural: DiscoveredTargetData["nesting"],
31
+ ): DiscoveredTargetData["nesting"] {
32
+ if (semantic === structural) return semantic;
33
+ if (semantic === "unknown") return structural;
34
+ if (structural === "unknown") return semantic;
35
+ return "unknown";
36
+ }
37
+
38
+ /** Keep container metadata unless both providers report conflicting known facts. */
39
+ export function reconcileContainer(
40
+ semantic: DiscoveredTargetData,
41
+ structural: DiscoveredTargetData,
42
+ ): string | null {
43
+ if (semantic.container === structural.container) return semantic.container;
44
+ if (semantic.nesting === "unknown" && semantic.container === null) {
45
+ return structural.container;
46
+ }
47
+ if (structural.nesting === "unknown" && structural.container === null) {
48
+ return semantic.container;
49
+ }
50
+ return null;
51
+ }
@@ -20,7 +20,18 @@ import {
20
20
  } from "@mrclrchtr/supi-code-runtime/api";
21
21
  import type { AnchorKind } from "../../session/target-store.ts";
22
22
  import { normalizePath } from "../search/paths.ts";
23
- import { canonicalDeclarationKind, refineTypeAliasIdentity } from "./identity.ts";
23
+ import {
24
+ reconcileContainer,
25
+ reconcileNesting,
26
+ refineLatexSectionNameAnchor,
27
+ } from "./file-refinement.ts";
28
+ import {
29
+ canonicalDeclarationKind,
30
+ canonicalFileDeclarationKind,
31
+ canonicalFileDeclarationName,
32
+ declarationOccurrencesFor,
33
+ refineTypeAliasIdentity,
34
+ } from "./identity.ts";
24
35
  import type { ResolvedTargetData, ResolvedTargetGroupData } from "./types.ts";
25
36
 
26
37
  const BINARY_EXTENSIONS = new Set([
@@ -50,7 +61,7 @@ const BINARY_EXTENSIONS = new Set([
50
61
  ".node",
51
62
  ]);
52
63
 
53
- interface DiscoveredTargetData extends ResolvedTargetData {
64
+ export interface DiscoveredTargetData extends ResolvedTargetData {
54
65
  /** Provider-backed hierarchy used only for file-result presentation ranking. */
55
66
  readonly nesting: DeclarationNesting;
56
67
  }
@@ -175,6 +186,7 @@ async function discoverSemantic(
175
186
 
176
187
  function targetFromSymbol(file: string, symbol: DocumentCodeSymbol): DiscoveredTargetData {
177
188
  const anchor = symbol.nameAnchor ?? symbol.declarationAnchor;
189
+ const identityKind = canonicalFileDeclarationKind(file, symbol.kind, symbol.name);
178
190
  return {
179
191
  file,
180
192
  position: { line: anchor.line - 1, character: anchor.character - 1 },
@@ -184,6 +196,7 @@ function targetFromSymbol(file: string, symbol: DocumentCodeSymbol): DiscoveredT
184
196
  declarationOccurrence: 0,
185
197
  name: symbol.name,
186
198
  kind: symbol.kind,
199
+ ...(identityKind === undefined ? {} : { identityKind }),
187
200
  confidence: "semantic",
188
201
  provenance: ["semantic"],
189
202
  anchorKind: (symbol.nameAnchor ? "name" : "declaration") as AnchorKind,
@@ -221,19 +234,25 @@ function flattenOutline(
221
234
  nesting: DeclarationNesting = "top-level",
222
235
  ): DiscoveredTargetData[] {
223
236
  return items.flatMap((item) => {
237
+ const nameAnchor = item.nameAnchor ?? {
238
+ line: item.startLine,
239
+ character: item.startCharacter,
240
+ };
224
241
  const target: DiscoveredTargetData = {
225
242
  file,
226
- position: { line: item.startLine - 1, character: item.startCharacter - 1 },
227
- displayLine: item.startLine,
228
- displayCharacter: item.startCharacter,
243
+ position: { line: nameAnchor.line - 1, character: nameAnchor.character - 1 },
244
+ displayLine: nameAnchor.line,
245
+ displayCharacter: nameAnchor.character,
229
246
  declarationAnchor: { line: item.startLine, character: item.startCharacter },
230
247
  declarationOccurrence: 0,
231
248
  name: item.name,
232
249
  kind: item.kind,
233
- identityKind: canonicalDeclarationKind(item.kind),
250
+ identityKind:
251
+ canonicalFileDeclarationKind(file, item.kind, item.name) ??
252
+ canonicalDeclarationKind(item.kind),
234
253
  confidence: "structural",
235
254
  provenance: ["structural"],
236
- anchorKind: "declaration",
255
+ anchorKind: item.nameAnchor ? "name" : "declaration",
237
256
  container,
238
257
  nesting,
239
258
  };
@@ -255,9 +274,10 @@ function mergeDiscoveries(
255
274
  if (semanticIndex === null) return [structuralTarget];
256
275
  unmatchedSemantic.delete(semanticIndex);
257
276
  const semanticTarget = semantic[semanticIndex];
277
+ const refinedSemantic = refineLatexSectionNameAnchor(semanticTarget, structuralTarget);
258
278
  return [
259
279
  {
260
- ...semanticTarget,
280
+ ...refinedSemantic,
261
281
  provenance: ["semantic", "structural"] as const,
262
282
  container: reconcileContainer(semanticTarget, structuralTarget),
263
283
  nesting: reconcileNesting(semanticTarget.nesting, structuralTarget.nesting),
@@ -270,32 +290,6 @@ function mergeDiscoveries(
270
290
  return identified.sort(compareFileOrientationDeclarations);
271
291
  }
272
292
 
273
- /** Let provider-backed known hierarchy refine unknown without guessing through a conflict. */
274
- function reconcileNesting(
275
- semantic: DeclarationNesting,
276
- structural: DeclarationNesting,
277
- ): DeclarationNesting {
278
- if (semantic === structural) return semantic;
279
- if (semantic === "unknown") return structural;
280
- if (structural === "unknown") return semantic;
281
- return "unknown";
282
- }
283
-
284
- /** Preserve known container evidence while clearing explicit provider conflicts. */
285
- function reconcileContainer(
286
- semantic: DiscoveredTargetData,
287
- structural: DiscoveredTargetData,
288
- ): string | null {
289
- if (semantic.container === structural.container) return semantic.container;
290
- if (semantic.nesting === "unknown" && semantic.container === null) {
291
- return structural.container;
292
- }
293
- if (structural.nesting === "unknown" && structural.container === null) {
294
- return semantic.container;
295
- }
296
- return null;
297
- }
298
-
299
293
  function findSemanticMatch(
300
294
  structural: DiscoveredTargetData,
301
295
  semantic: readonly DiscoveredTargetData[],
@@ -321,7 +315,8 @@ function findSemanticMatch(
321
315
 
322
316
  function sameDeclaration(left: DiscoveredTargetData, right: DiscoveredTargetData): boolean {
323
317
  return (
324
- left.name === right.name &&
318
+ canonicalFileDeclarationName(left.file, left.kind, left.name) ===
319
+ canonicalFileDeclarationName(right.file, right.kind, right.name) &&
325
320
  compatibleContainerEvidence(left, right) &&
326
321
  left.declarationAnchor.line === right.declarationAnchor.line &&
327
322
  declarationIdentityKind(left) === declarationIdentityKind(right)
@@ -342,22 +337,29 @@ function compatibleContainerEvidence(
342
337
  function assignDeclarationOccurrences(
343
338
  targets: readonly DiscoveredTargetData[],
344
339
  ): DiscoveredTargetData[] {
345
- const occurrences = new Map<string, number>();
346
- return targets.map((target) => {
347
- const key = [
348
- target.declarationAnchor.line,
349
- target.name ?? "",
350
- declarationIdentityKind(target),
351
- target.container ?? "",
352
- ].join("\0");
353
- const occurrence = occurrences.get(key) ?? 0;
354
- occurrences.set(key, occurrence + 1);
355
- return { ...target, declarationOccurrence: occurrence };
340
+ const candidates = targets.map((target) => ({
341
+ name: target.name,
342
+ identityKind: declarationIdentityKind(target),
343
+ declarationAnchor: target.declarationAnchor,
344
+ nameAnchor: { line: target.displayLine, character: target.displayCharacter },
345
+ container: target.container,
346
+ }));
347
+ const occurrences = declarationOccurrencesFor(candidates);
348
+ return targets.map((target, index) => {
349
+ const declarationOccurrence = occurrences[index];
350
+ if (declarationOccurrence === undefined) {
351
+ throw new Error("The declaration occurrence was not assigned.");
352
+ }
353
+ return { ...target, declarationOccurrence };
356
354
  });
357
355
  }
358
356
 
359
357
  function declarationIdentityKind(target: ResolvedTargetData): string {
360
- return target.identityKind ?? canonicalDeclarationKind(target.kind);
358
+ return (
359
+ target.identityKind ??
360
+ canonicalFileDeclarationKind(target.file, target.kind, target.name) ??
361
+ canonicalDeclarationKind(target.kind)
362
+ );
361
363
  }
362
364
 
363
365
  function compareFileOrientationDeclarations(
@@ -1,4 +1,4 @@
1
- import { resolve } from "node:path";
1
+ import { extname, resolve } from "node:path";
2
2
  import {
3
3
  type CodeRequestControl,
4
4
  type CodeSymbol,
@@ -23,6 +23,46 @@ export function canonicalDeclarationKind(kind: string | null): string {
23
23
  return normalized;
24
24
  }
25
25
 
26
+ const LATEX_EXTENSIONS = new Set([".tex", ".sty", ".cls"]);
27
+ const LATEX_SECTION_KINDS = new Set([
28
+ "module",
29
+ "part",
30
+ "chapter",
31
+ "section",
32
+ "subsection",
33
+ "subsubsection",
34
+ "paragraph",
35
+ "subparagraph",
36
+ ]);
37
+ const TEXLAB_COMMAND_NAME = /^define\s+(\\\S+)$/;
38
+
39
+ /** Map LaTeX outline and Texlab kinds to file-local identities. */
40
+ export function canonicalFileDeclarationKind(
41
+ file: string,
42
+ kind: string | null,
43
+ name: string | null,
44
+ ): string | undefined {
45
+ if (!LATEX_EXTENSIONS.has(extname(file).toLowerCase())) return undefined;
46
+
47
+ const normalized = (kind ?? "").toLowerCase();
48
+ if (LATEX_SECTION_KINDS.has(normalized)) return "latex-section";
49
+ if (normalized === "command") return "latex-command";
50
+ if (normalized === "key" && TEXLAB_COMMAND_NAME.test(name ?? "")) return "latex-command";
51
+ return undefined;
52
+ }
53
+
54
+ /** Match Texlab's `define \\command` name to the grammar's command name. */
55
+ export function canonicalFileDeclarationName(
56
+ file: string,
57
+ kind: string | null,
58
+ name: string | null,
59
+ ): string | null {
60
+ if (LATEX_EXTENSIONS.has(extname(file).toLowerCase()) && kind?.toLowerCase() === "key") {
61
+ return TEXLAB_COMMAND_NAME.exec(name ?? "")?.[1] ?? name;
62
+ }
63
+ return name;
64
+ }
65
+
26
66
  /** Exact declaration-name observation used to derive provider-independent identity. */
27
67
  export interface DeclarationIdentityObservation {
28
68
  readonly file: string;
@@ -42,6 +82,71 @@ export interface DeclarationOccurrenceIdentity extends DeclarationIdentityResult
42
82
  readonly declarationOccurrence: number;
43
83
  }
44
84
 
85
+ /** Identity fields used to order otherwise-equal declarations on one line. */
86
+ export interface DeclarationOccurrenceCandidate {
87
+ readonly name: string | null;
88
+ readonly identityKind: string;
89
+ readonly declarationAnchor: { readonly line: number; readonly character: number };
90
+ readonly nameAnchor?: { readonly line: number; readonly character: number } | null;
91
+ readonly container: string | null;
92
+ }
93
+
94
+ /** Assign source-order occurrences after one sort for each identity group. */
95
+ export function declarationOccurrencesFor(
96
+ candidates: readonly DeclarationOccurrenceCandidate[],
97
+ ): number[] {
98
+ const groups = new Map<
99
+ string,
100
+ Array<{ candidate: DeclarationOccurrenceCandidate; index: number }>
101
+ >();
102
+ candidates.forEach((candidate, index) => {
103
+ const key = JSON.stringify([
104
+ candidate.name,
105
+ candidate.identityKind,
106
+ candidate.container,
107
+ candidate.declarationAnchor.line,
108
+ ]);
109
+ const group = groups.get(key);
110
+ if (group) group.push({ candidate, index });
111
+ else groups.set(key, [{ candidate, index }]);
112
+ });
113
+
114
+ const occurrences = new Array<number>(candidates.length);
115
+ for (const group of groups.values()) {
116
+ group.sort(
117
+ (left, right) =>
118
+ compareDeclarationOccurrenceCandidates(left.candidate, right.candidate) ||
119
+ left.index - right.index,
120
+ );
121
+ group.forEach(({ index }, occurrence) => {
122
+ occurrences[index] = occurrence;
123
+ });
124
+ }
125
+ return occurrences;
126
+ }
127
+
128
+ /** Return the shared source-order occurrence for one selected declaration. */
129
+ export function declarationOccurrenceFor(
130
+ selected: DeclarationOccurrenceCandidate,
131
+ candidates: readonly DeclarationOccurrenceCandidate[],
132
+ ): number {
133
+ const index = candidates.indexOf(selected);
134
+ if (index < 0) throw new Error("The selected declaration is not in the occurrence set.");
135
+ const occurrence = declarationOccurrencesFor(candidates)[index];
136
+ if (occurrence === undefined) throw new Error("The declaration occurrence was not assigned.");
137
+ return occurrence;
138
+ }
139
+
140
+ function compareDeclarationOccurrenceCandidates(
141
+ left: DeclarationOccurrenceCandidate,
142
+ right: DeclarationOccurrenceCandidate,
143
+ ): number {
144
+ return (
145
+ left.declarationAnchor.character - right.declarationAnchor.character ||
146
+ (left.nameAnchor?.character ?? 0) - (right.nameAnchor?.character ?? 0)
147
+ );
148
+ }
149
+
45
150
  /**
46
151
  * Resolve an identity kind without rewriting the provider-reported display kind.
47
152
  * An LSP `Variable` becomes `type` only when Tree-sitter reports the exact name
@@ -93,6 +198,10 @@ export function createCodeSymbolIdentityResolver(
93
198
  ): (symbol: CodeSymbol) => Promise<DeclarationOccurrenceIdentity> {
94
199
  const identities = new Map<CodeSymbol, Promise<DeclarationIdentityResult>>();
95
200
  const occurrences = new Map<CodeSymbol, Promise<DeclarationOccurrenceIdentity>>();
201
+ const occurrenceSets = new Map<
202
+ CodeSymbol,
203
+ Promise<Map<CodeSymbol, DeclarationOccurrenceIdentity>>
204
+ >();
96
205
 
97
206
  const resolveIdentity = (symbol: CodeSymbol): Promise<DeclarationIdentityResult> => {
98
207
  const existing = identities.get(symbol);
@@ -114,42 +223,51 @@ export function createCodeSymbolIdentityResolver(
114
223
  return (symbol) => {
115
224
  const existing = occurrences.get(symbol);
116
225
  if (existing !== undefined) return existing;
117
- const occurrence = resolveCodeSymbolOccurrence(symbol, allSymbols, resolveIdentity);
226
+ let occurrenceSet = occurrenceSets.get(symbol);
227
+ if (!occurrenceSet) {
228
+ const peers = allSymbols.filter(
229
+ (candidate) =>
230
+ candidate.name === symbol.name &&
231
+ (candidate.container ?? null) === (symbol.container ?? null) &&
232
+ candidate.declarationAnchor.line === symbol.declarationAnchor.line,
233
+ );
234
+ occurrenceSet = resolveCodeSymbolOccurrences(peers, resolveIdentity);
235
+ for (const peer of peers) occurrenceSets.set(peer, occurrenceSet);
236
+ }
237
+ const occurrence = occurrenceSet.then((resolved) => {
238
+ const identity = resolved.get(symbol);
239
+ if (!identity) throw new Error("The symbol is not in its declaration set.");
240
+ return identity;
241
+ });
118
242
  occurrences.set(symbol, occurrence);
119
243
  return occurrence;
120
244
  };
121
245
  }
122
246
 
123
- async function resolveCodeSymbolOccurrence(
124
- symbol: CodeSymbol,
125
- allSymbols: readonly CodeSymbol[],
247
+ async function resolveCodeSymbolOccurrences(
248
+ symbols: readonly CodeSymbol[],
126
249
  resolveIdentity: (symbol: CodeSymbol) => Promise<DeclarationIdentityResult>,
127
- ): Promise<DeclarationOccurrenceIdentity> {
128
- const peers = allSymbols.filter(
129
- (candidate) =>
130
- candidate.name === symbol.name &&
131
- (candidate.container ?? null) === (symbol.container ?? null) &&
132
- candidate.declarationAnchor.line === symbol.declarationAnchor.line,
133
- );
250
+ ): Promise<Map<CodeSymbol, DeclarationOccurrenceIdentity>> {
134
251
  const observed = await Promise.all(
135
- peers.map(async (candidate) => ({ candidate, identity: await resolveIdentity(candidate) })),
136
- );
137
- const selectedIdentity = await resolveIdentity(symbol);
138
- const matching = observed
139
- .filter(({ identity }) => identity.identityKind === selectedIdentity.identityKind)
140
- .map(({ candidate }) => candidate)
141
- .sort(compareCodeSymbolDeclarations);
142
- return {
143
- ...selectedIdentity,
144
- declarationOccurrence: Math.max(0, matching.indexOf(symbol)),
145
- };
146
- }
147
-
148
- function compareCodeSymbolDeclarations(left: CodeSymbol, right: CodeSymbol): number {
149
- return (
150
- left.declarationAnchor.character - right.declarationAnchor.character ||
151
- (left.nameAnchor?.character ?? 0) - (right.nameAnchor?.character ?? 0)
252
+ symbols.map(async (symbol) => ({ symbol, identity: await resolveIdentity(symbol) })),
152
253
  );
254
+ const candidates = observed.map(({ symbol, identity }) => ({
255
+ name: symbol.name,
256
+ identityKind: identity.identityKind,
257
+ declarationAnchor: symbol.declarationAnchor,
258
+ nameAnchor: symbol.nameAnchor ?? null,
259
+ container: symbol.container ?? null,
260
+ }));
261
+ const occurrences = declarationOccurrencesFor(candidates);
262
+ const resolved = new Map<CodeSymbol, DeclarationOccurrenceIdentity>();
263
+ observed.forEach(({ symbol, identity }, index) => {
264
+ const declarationOccurrence = occurrences[index];
265
+ if (declarationOccurrence === undefined) {
266
+ throw new Error("The declaration occurrence was not assigned.");
267
+ }
268
+ resolved.set(symbol, { ...identity, declarationOccurrence });
269
+ });
270
+ return resolved;
153
271
  }
154
272
 
155
273
  /** Refine one semantic target with exact structural type-alias evidence. */
@@ -0,0 +1,137 @@
1
+ import { extname } from "node:path";
2
+ import {
3
+ type CodeRequestControl,
4
+ isCodeRequestInterruption,
5
+ type OutlineData,
6
+ type StructuralProvider,
7
+ } from "@mrclrchtr/supi-code-runtime/api";
8
+ import {
9
+ canonicalDeclarationKind,
10
+ canonicalFileDeclarationKind,
11
+ type DeclarationOccurrenceCandidate,
12
+ declarationOccurrenceFor,
13
+ } from "./identity.ts";
14
+ import type { ResolvedTargetData, TargetOutcome } from "./types.ts";
15
+
16
+ type LabelOutlineCandidate = DeclarationOccurrenceCandidate & {
17
+ readonly name: string;
18
+ readonly nameAnchor: { readonly line: number; readonly character: number } | null;
19
+ };
20
+
21
+ export interface LatexLabelIdentity {
22
+ readonly name: string;
23
+ readonly declarationAnchor: { readonly line: number; readonly character: number };
24
+ readonly nameAnchor: { readonly line: number; readonly character: number };
25
+ readonly container: string | null;
26
+ readonly declarationOccurrence: number;
27
+ }
28
+
29
+ function collectLabelCandidates(
30
+ items: readonly OutlineData[],
31
+ container: string | null = null,
32
+ ): LabelOutlineCandidate[] {
33
+ return items.flatMap((item) => {
34
+ const isLabel = item.kind.toLowerCase() === "label";
35
+ const candidates: LabelOutlineCandidate[] = isLabel
36
+ ? [
37
+ {
38
+ name: item.name,
39
+ identityKind: canonicalDeclarationKind(item.kind),
40
+ declarationAnchor: { line: item.startLine, character: item.startCharacter },
41
+ nameAnchor: item.nameAnchor ?? null,
42
+ container,
43
+ },
44
+ ]
45
+ : [];
46
+ return [...candidates, ...collectLabelCandidates(item.children ?? [], item.name)];
47
+ });
48
+ }
49
+
50
+ /** Find a complete label key at one source coordinate. */
51
+ export async function findLatexLabelAtAnchor(input: {
52
+ file: string;
53
+ line: number;
54
+ character: number;
55
+ outline?: StructuralProvider["outline"];
56
+ control?: CodeRequestControl;
57
+ }): Promise<LatexLabelIdentity | null> {
58
+ if (!input.outline || ![".tex", ".sty", ".cls"].includes(extname(input.file).toLowerCase())) {
59
+ return null;
60
+ }
61
+ try {
62
+ const result = await input.outline(input.file, input.control);
63
+ if (result.kind !== "success") return null;
64
+ const candidates = collectLabelCandidates(result.data);
65
+ const selected = candidates.find((candidate) => {
66
+ const anchor = candidate.nameAnchor;
67
+ return (
68
+ anchor !== null &&
69
+ anchor.line === input.line &&
70
+ input.character >= anchor.character &&
71
+ input.character < anchor.character + candidate.name.length
72
+ );
73
+ });
74
+ if (!selected?.nameAnchor) return null;
75
+ return {
76
+ name: selected.name,
77
+ declarationAnchor: selected.declarationAnchor,
78
+ nameAnchor: selected.nameAnchor,
79
+ container: selected.container,
80
+ declarationOccurrence: declarationOccurrenceFor(selected, candidates),
81
+ };
82
+ } catch (error) {
83
+ if (isCodeRequestInterruption(error, input.control)) throw error;
84
+ return null;
85
+ }
86
+ }
87
+
88
+ /** Resolve a label key at an exact source character. */
89
+ export async function resolveLatexLabelTargetAtAnchor(input: {
90
+ file: string;
91
+ line: number;
92
+ character: number;
93
+ outline?: StructuralProvider["outline"];
94
+ control?: CodeRequestControl;
95
+ }): Promise<ResolvedTargetData | null> {
96
+ const label = await findLatexLabelAtAnchor(input);
97
+ if (!label) return null;
98
+ const anchor = label.nameAnchor;
99
+ return {
100
+ file: input.file,
101
+ position: { line: anchor.line - 1, character: anchor.character - 1 },
102
+ displayLine: anchor.line,
103
+ displayCharacter: anchor.character,
104
+ declarationAnchor: label.declarationAnchor,
105
+ declarationOccurrence: label.declarationOccurrence,
106
+ name: label.name,
107
+ kind: "label",
108
+ identityKind: canonicalDeclarationKind("label"),
109
+ confidence: "structural",
110
+ provenance: ["structural"],
111
+ anchorKind: "name",
112
+ container: label.container,
113
+ resolution: {
114
+ requested: { line: input.line, character: input.character },
115
+ resolved: { line: anchor.line, character: anchor.character },
116
+ snapped: anchor.line !== input.line || anchor.character !== input.character,
117
+ source: "structural-identifier",
118
+ },
119
+ };
120
+ }
121
+
122
+ /** Accept a structural label only when it disproves a Texlab section-name hit. */
123
+ export async function resolveTexlabSectionLabelCollision(
124
+ file: string,
125
+ semantic: TargetOutcome,
126
+ resolveStructural: () => Promise<TargetOutcome | null>,
127
+ ): Promise<TargetOutcome | null> {
128
+ if (
129
+ semantic.kind !== "resolved" ||
130
+ canonicalFileDeclarationKind(file, semantic.target.kind, semantic.target.name) !==
131
+ "latex-section"
132
+ ) {
133
+ return null;
134
+ }
135
+ const structural = await resolveStructural();
136
+ return structural?.kind === "resolved" && structural.target.kind === "label" ? structural : null;
137
+ }
@@ -0,0 +1,116 @@
1
+ import { extname } from "node:path";
2
+ import {
3
+ type CodeRequestControl,
4
+ isCodeRequestInterruption,
5
+ type OutlineData,
6
+ type StructuralProvider,
7
+ } from "@mrclrchtr/supi-code-runtime/api";
8
+ import {
9
+ canonicalFileDeclarationKind,
10
+ type DeclarationOccurrenceCandidate,
11
+ declarationOccurrenceFor,
12
+ } from "./identity.ts";
13
+ import type { ResolvedTargetData } from "./types.ts";
14
+
15
+ interface SectionCandidate extends DeclarationOccurrenceCandidate {
16
+ readonly item: OutlineData;
17
+ }
18
+
19
+ /** Resolve an exact LaTeX section title from structural outline evidence. */
20
+ export async function resolveLatexSectionTargetAtAnchor(input: {
21
+ readonly file: string;
22
+ readonly line: number;
23
+ readonly character: number;
24
+ readonly outline?: StructuralProvider["outline"];
25
+ readonly control?: CodeRequestControl;
26
+ }): Promise<ResolvedTargetData | null> {
27
+ if (!input.outline || !isLatexFile(input.file)) return null;
28
+ try {
29
+ const result = await input.outline(input.file, input.control);
30
+ if (result.kind !== "success") return null;
31
+ const candidates = collectSectionCandidates(input.file, result.data);
32
+ const matches = candidates.filter(({ item }) => {
33
+ const anchor = item.nameAnchor;
34
+ if (!anchor) return false;
35
+ const end = item.nameEndAnchor;
36
+ if (!end) {
37
+ return (
38
+ anchor.line === input.line &&
39
+ input.character >= anchor.character &&
40
+ input.character < anchor.character + item.name.length
41
+ );
42
+ }
43
+ return (
44
+ compareAnchor({ line: input.line, character: input.character }, anchor) >= 0 &&
45
+ compareAnchor({ line: input.line, character: input.character }, end) < 0
46
+ );
47
+ });
48
+ const selected = matches[0];
49
+ if (matches.length !== 1 || !selected?.item.nameAnchor) return null;
50
+
51
+ const occurrence = declarationOccurrenceFor(selected, candidates);
52
+ const anchor = selected.item.nameAnchor;
53
+ return {
54
+ file: input.file,
55
+ position: { line: anchor.line - 1, character: anchor.character - 1 },
56
+ displayLine: anchor.line,
57
+ displayCharacter: anchor.character,
58
+ declarationAnchor: {
59
+ line: selected.item.startLine,
60
+ character: selected.item.startCharacter,
61
+ },
62
+ declarationOccurrence: occurrence,
63
+ name: selected.item.name,
64
+ kind: selected.item.kind,
65
+ identityKind: "latex-section",
66
+ confidence: "structural",
67
+ provenance: ["structural"],
68
+ anchorKind: "name",
69
+ container: selected.container,
70
+ resolution: {
71
+ requested: { line: input.line, character: input.character },
72
+ resolved: { line: anchor.line, character: anchor.character },
73
+ snapped: anchor.line !== input.line || anchor.character !== input.character,
74
+ source: "structural",
75
+ },
76
+ };
77
+ } catch (error) {
78
+ if (isCodeRequestInterruption(error, input.control)) throw error;
79
+ return null;
80
+ }
81
+ }
82
+
83
+ function collectSectionCandidates(
84
+ file: string,
85
+ items: readonly OutlineData[],
86
+ container: string | null = null,
87
+ ): SectionCandidate[] {
88
+ return items.flatMap((item) => {
89
+ const identityKind = canonicalFileDeclarationKind(file, item.kind, item.name);
90
+ const current: SectionCandidate[] =
91
+ identityKind === "latex-section"
92
+ ? [
93
+ {
94
+ item,
95
+ name: item.name,
96
+ identityKind,
97
+ declarationAnchor: { line: item.startLine, character: item.startCharacter },
98
+ nameAnchor: item.nameAnchor ?? null,
99
+ container,
100
+ },
101
+ ]
102
+ : [];
103
+ return [...current, ...collectSectionCandidates(file, item.children ?? [], item.name)];
104
+ });
105
+ }
106
+
107
+ function compareAnchor(
108
+ left: { readonly line: number; readonly character: number },
109
+ right: { readonly line: number; readonly character: number },
110
+ ): number {
111
+ return left.line - right.line || left.character - right.character;
112
+ }
113
+
114
+ function isLatexFile(file: string): boolean {
115
+ return [".tex", ".sty", ".cls"].includes(extname(file).toLowerCase());
116
+ }