@mrclrchtr/supi-code-intelligence 11.3.0 → 12.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 (132) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +15 -9
  3. package/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  4. package/node_modules/@mrclrchtr/supi-code-runtime/src/api.ts +3 -1
  5. package/node_modules/@mrclrchtr/supi-code-runtime/src/declaration-level.ts +34 -0
  6. package/node_modules/@mrclrchtr/supi-code-runtime/src/types.ts +64 -28
  7. package/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  8. package/node_modules/@mrclrchtr/supi-lsp/README.md +22 -17
  9. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  10. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/src/api.ts +3 -1
  11. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/src/declaration-level.ts +34 -0
  12. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/src/types.ts +64 -28
  13. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  14. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/classes/range.js +2 -2
  15. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/internal/constants.js +30 -0
  16. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/internal/trim-comparator-whitespace.js +438 -0
  17. package/node_modules/@mrclrchtr/supi-lsp/node_modules/semver/package.json +1 -1
  18. package/node_modules/@mrclrchtr/supi-lsp/package.json +3 -3
  19. package/node_modules/@mrclrchtr/supi-lsp/src/api.ts +2 -2
  20. package/node_modules/@mrclrchtr/supi-lsp/src/client/client-diagnostic-capabilities.ts +13 -3
  21. package/node_modules/@mrclrchtr/supi-lsp/src/client/client-diagnostics.ts +25 -9
  22. package/node_modules/@mrclrchtr/supi-lsp/src/client/client-document-sync.ts +7 -5
  23. package/node_modules/@mrclrchtr/supi-lsp/src/client/client-edit-capture.ts +258 -0
  24. package/node_modules/@mrclrchtr/supi-lsp/src/client/client.ts +64 -11
  25. package/node_modules/@mrclrchtr/supi-lsp/src/config/capabilities.ts +8 -0
  26. package/node_modules/@mrclrchtr/supi-lsp/src/config/operation-support.ts +48 -79
  27. package/node_modules/@mrclrchtr/supi-lsp/src/config/tsconfig-scope.ts +10 -0
  28. package/node_modules/@mrclrchtr/supi-lsp/src/manager/manager-typescript-coverage.ts +118 -0
  29. package/node_modules/@mrclrchtr/supi-lsp/src/manager/manager.ts +67 -3
  30. package/node_modules/@mrclrchtr/supi-lsp/src/manager/typescript-file-local.ts +87 -0
  31. package/node_modules/@mrclrchtr/supi-lsp/src/provider/lsp-refactor-provider.ts +21 -35
  32. package/node_modules/@mrclrchtr/supi-lsp/src/provider/refactor-command-capture.ts +139 -0
  33. package/node_modules/@mrclrchtr/supi-lsp/src/provider/refactor-planning.ts +202 -47
  34. package/node_modules/@mrclrchtr/supi-lsp/src/provider/semantic-symbol-mapper.ts +22 -17
  35. package/node_modules/@mrclrchtr/supi-lsp/src/session/route-boundary.ts +26 -0
  36. package/node_modules/@mrclrchtr/supi-lsp/src/session/runtime-registry.ts +106 -16
  37. package/node_modules/@mrclrchtr/supi-lsp/src/session/runtime-rename.ts +3 -0
  38. package/node_modules/@mrclrchtr/supi-lsp/src/session/workspace-lsp-runtime.ts +22 -1
  39. package/node_modules/@mrclrchtr/supi-tree-sitter/README.md +1 -1
  40. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  41. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/src/api.ts +3 -1
  42. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/src/declaration-level.ts +34 -0
  43. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/src/types.ts +64 -28
  44. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  45. package/node_modules/@mrclrchtr/supi-tree-sitter/package.json +3 -3
  46. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/tsx/tree-sitter-tsx.wasm +0 -0
  47. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/tsx/tree-sitter-tsx.wasm.json +5 -1
  48. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/typescript/tree-sitter-typescript.wasm +0 -0
  49. package/node_modules/@mrclrchtr/supi-tree-sitter/resources/grammars/typescript/tree-sitter-typescript.wasm.json +5 -1
  50. package/node_modules/@mrclrchtr/supi-tree-sitter/src/coordinates.ts +16 -2
  51. package/node_modules/@mrclrchtr/supi-tree-sitter/src/session/structural-worker-client.ts +3 -2
  52. package/node_modules/@mrclrchtr/supi-tree-sitter/src/syntax-errors.ts +2 -0
  53. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/call-range.ts +94 -0
  54. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/call-sites.ts +26 -6
  55. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/callees.ts +6 -3
  56. package/node_modules/@mrclrchtr/supi-tree-sitter/src/tool/exports.ts +13 -4
  57. package/node_modules/@mrclrchtr/supi-tree-sitter/src/worker/runtime.ts +1 -9
  58. package/package.json +5 -5
  59. package/src/analysis/health/server-status.ts +41 -12
  60. package/src/analysis/helpers.ts +0 -7
  61. package/src/analysis/relations/implementations.ts +5 -0
  62. package/src/analysis/relations/references.ts +5 -0
  63. package/src/analysis/target/anchored-structural.ts +4 -1
  64. package/src/analysis/target/file-merge.ts +162 -0
  65. package/src/analysis/target/file-outline-omissions.ts +146 -0
  66. package/src/analysis/target/file-refinement.ts +7 -7
  67. package/src/analysis/target/file.ts +44 -156
  68. package/src/analysis/target/symbol.ts +5 -1
  69. package/src/analysis/target/types.ts +32 -6
  70. package/src/api.ts +1 -1
  71. package/src/app/app.ts +9 -3
  72. package/src/session/find-types.ts +8 -4
  73. package/src/session/find-workflow.ts +11 -16
  74. package/src/session/graph/collect.ts +2 -0
  75. package/src/session/graph-types.ts +3 -1
  76. package/src/session/graph-workflow.ts +5 -4
  77. package/src/session/input/health-refactor.ts +48 -34
  78. package/src/session/input/workflows.ts +45 -46
  79. package/src/session/inspect/file-point.ts +2 -2
  80. package/src/session/inspect-types.ts +1 -1
  81. package/src/session/inspect-workflow.ts +3 -3
  82. package/src/session/orientation/workspace-context.ts +3 -0
  83. package/src/session/orientation-workflow.ts +6 -3
  84. package/src/session/refactor-file-actions.ts +122 -0
  85. package/src/session/refactor-plan-pipeline.ts +152 -0
  86. package/src/session/refactor-plans.ts +18 -15
  87. package/src/session/refactor-types.ts +24 -12
  88. package/src/session/refactor-workflow.ts +70 -244
  89. package/src/session/session.ts +87 -12
  90. package/src/session/target-workflow.ts +23 -21
  91. package/src/tool/code_find/execute.ts +6 -7
  92. package/src/tool/code_find/spec.ts +9 -16
  93. package/src/tool/code_find/tui.ts +12 -6
  94. package/src/tool/code_graph/section.ts +4 -0
  95. package/src/tool/code_graph/spec.ts +6 -0
  96. package/src/tool/code_health/spec.ts +6 -0
  97. package/src/tool/code_health/structured-schema.ts +4 -4
  98. package/src/tool/code_health/structured.ts +15 -17
  99. package/src/tool/code_inspect/execute.ts +1 -1
  100. package/src/tool/code_inspect/markdown.ts +33 -6
  101. package/src/tool/code_inspect/result.ts +1 -1
  102. package/src/tool/code_inspect/spec.ts +14 -6
  103. package/src/tool/code_inspect/structured.ts +2 -2
  104. package/src/tool/code_inspect/tui.ts +2 -2
  105. package/src/tool/code_orientation/directory-navigation.ts +20 -31
  106. package/src/tool/code_orientation/spec.ts +8 -0
  107. package/src/tool/code_orientation/workspace-output.ts +40 -13
  108. package/src/tool/code_orientation/workspace-sections.ts +45 -2
  109. package/src/tool/code_refactor_apply/result.ts +2 -1
  110. package/src/tool/code_refactor_apply/spec.ts +7 -0
  111. package/src/tool/code_refactor_plan/execute.ts +0 -2
  112. package/src/tool/code_refactor_plan/result.ts +23 -13
  113. package/src/tool/code_refactor_plan/spec.ts +8 -2
  114. package/src/tool/code_refactor_plan/structured.ts +59 -12
  115. package/src/tool/code_refactor_plan/tui.ts +14 -17
  116. package/src/tool/code_resolve/guidance.ts +1 -1
  117. package/src/tool/code_resolve/markdown.ts +121 -34
  118. package/src/tool/code_resolve/result.ts +18 -15
  119. package/src/tool/code_resolve/spec.ts +6 -0
  120. package/src/tool/code_resolve/structured.ts +43 -10
  121. package/src/tool/namespace.ts +14 -0
  122. package/src/tool/refactor-markdown.ts +55 -15
  123. package/src/tool/register.ts +3 -0
  124. package/src/tool/result/model-budget.ts +2 -2
  125. package/src/tool/result/refactor-preview.ts +118 -0
  126. package/src/tool/result/refactor-unavailable.ts +16 -0
  127. package/src/tool/result/refactor.ts +8 -2
  128. package/src/tool/result/types.ts +13 -10
  129. package/src/tool/schemas.ts +63 -20
  130. package/src/tool/specs.ts +3 -0
  131. package/src/ui/status-overlay.ts +1 -1
  132. package/src/ui/tui/common.ts +10 -10
@@ -4,10 +4,8 @@ import type { ServerCapabilities } from "vscode-languageserver-protocol";
4
4
  export type ServerOperationName =
5
5
  | "rename_symbol"
6
6
  | "prepare_rename"
7
- | "extract_function"
8
- | "extract_variable"
9
- | "update_imports"
10
- | "delete_dead_code"
7
+ | "code_action"
8
+ | "source_action"
11
9
  | "code_action_resolve";
12
10
 
13
11
  /** Whether a server advertises one operation in its negotiated capabilities. */
@@ -19,6 +17,13 @@ export interface ServerOperationSupport {
19
17
  readonly supi: "supported" | "limited" | "unsupported";
20
18
  /** A stable explanation for a SuPi planning limitation or deferral. */
21
19
  readonly reason?: string;
20
+ /** Advertised CodeActionKinds in this operation's family (`refactor*` or `source*`). */
21
+ readonly kinds?: readonly string[];
22
+ /**
23
+ * The server may return kinds beyond `kinds`: it advertises CodeActionKind.Empty
24
+ * (`""`) or does not narrow `codeActionKinds` at all.
25
+ */
26
+ readonly anyKind?: boolean;
22
27
  }
23
28
 
24
29
  /** Operation support projected onto a project-server health entry. */
@@ -26,23 +31,6 @@ export type ServerOperationSupportMap = Partial<
26
31
  Record<ServerOperationName, ServerOperationSupport>
27
32
  >;
28
33
 
29
- const REMOVE_UNUSED_KIND = "source.removeUnused";
30
- const REMOVE_UNUSED_IMPORTS_KIND = "source.removeUnusedImports";
31
-
32
- /** Code-action kinds requested by each operation-specific planner. */
33
- export const REFACTOR_CODE_ACTION_KINDS = {
34
- extract_function: ["refactor.extract.function"],
35
- extract_variable: ["refactor.extract.constant", "refactor.extract.variable"],
36
- update_imports: ["source.organizeImports"],
37
- delete_dead_code: [REMOVE_UNUSED_KIND, REMOVE_UNUSED_IMPORTS_KIND],
38
- } as const satisfies Record<
39
- "extract_function" | "extract_variable" | "update_imports" | "delete_dead_code",
40
- readonly string[]
41
- >;
42
-
43
- const EXTRACTION_NAME_LIMITATION =
44
- "Standard LSP code actions do not provide a safe exact extraction-name contract.";
45
-
46
34
  function isRecord(value: unknown): value is Record<string, unknown> {
47
35
  return typeof value === "object" && value !== null && !Array.isArray(value);
48
36
  }
@@ -95,6 +83,22 @@ export function supportsCodeActionResolve(capabilities: ServerCapabilities | nul
95
83
  return codeActionOptions(capabilities)?.resolveProvider === true;
96
84
  }
97
85
 
86
+ /**
87
+ * Test whether a returned CodeActionKind falls under a requested kind: the same
88
+ * kind or a dotted descendant. The empty kind requests every kind.
89
+ */
90
+ export function isCodeActionKindWithin(kind: unknown, requested: string): boolean {
91
+ if (requested === "") return true;
92
+ return typeof kind === "string" && (kind === requested || kind.startsWith(`${requested}.`));
93
+ }
94
+
95
+ /** Return the negotiated `codeActionKinds`, or undefined when the server did not narrow them. */
96
+ export function advertisedCodeActionKinds(
97
+ capabilities: ServerCapabilities | null,
98
+ ): readonly string[] | undefined {
99
+ return codeActionOptions(capabilities)?.kinds;
100
+ }
101
+
98
102
  /** Test the documented hierarchical relationship between two code-action kinds. */
99
103
  export function isCodeActionKindInRange(kind: string, requested: string): boolean {
100
104
  if (kind === "" || requested === "") return true;
@@ -118,46 +122,30 @@ export function supportsRequestedCodeActionKinds(
118
122
  );
119
123
  }
120
124
 
121
- function codeActionAdvertisement(
122
- capabilities: ServerCapabilities | null,
123
- requested: readonly string[],
124
- ): ServerAdvertisement {
125
- if (capabilities === null) return "unknown";
126
- const options = codeActionOptions(capabilities);
127
- if (!options) return "not-advertised";
128
- if (requested.length === 0 || !options.kinds) return "unknown";
129
- if (!supportsRequestedCodeActionKinds(capabilities, requested)) return "not-advertised";
130
-
131
- // A generic parent or the empty kind proves eligibility, but it does not
132
- // prove that this exact operation is implemented by the server.
133
- const exactOrChild = requested.some((wanted) =>
134
- options.kinds?.some(
135
- (kind) => kind === wanted || (wanted !== "" && kind.startsWith(`${wanted}.`)),
136
- ),
137
- );
138
- return exactOrChild ? "advertised" : "unknown";
125
+ function supportedOperation(server: ServerAdvertisement): ServerOperationSupport {
126
+ return { server, supi: "supported" };
139
127
  }
140
128
 
141
- function supportedOperation(
142
- server: ServerAdvertisement,
143
- supi: ServerOperationSupport["supi"] = "supported",
144
- reason?: string,
129
+ /** Project advertised kinds for one CodeActionKind family, such as `refactor` or `source`. */
130
+ function codeActionFamilySupport(
131
+ capabilities: ServerCapabilities | null,
132
+ family: "refactor" | "source",
145
133
  ): ServerOperationSupport {
146
- return { server, supi, ...(reason ? { reason } : {}) };
147
- }
148
-
149
- function deleteDeadCodeSupport(capabilities: ServerCapabilities | null): ServerOperationSupport {
150
- const server = codeActionAdvertisement(capabilities, REFACTOR_CODE_ACTION_KINDS.delete_dead_code);
151
- const importsOnly =
152
- codeActionAdvertisement(capabilities, [REMOVE_UNUSED_KIND]) !== "advertised" &&
153
- codeActionAdvertisement(capabilities, [REMOVE_UNUSED_IMPORTS_KIND]) === "advertised";
154
- return importsOnly
155
- ? supportedOperation(
156
- server,
157
- "limited",
158
- "Only unused-import removal is supported by this mapping, not arbitrary unused declarations.",
159
- )
160
- : supportedOperation(server);
134
+ if (capabilities === null) return supportedOperation("unknown");
135
+ const options = codeActionOptions(capabilities);
136
+ if (!options) return supportedOperation("not-advertised");
137
+ // An unnarrowed provider or the empty kind may return kinds it does not list.
138
+ const anyKind = !options.kinds || options.kinds.includes("");
139
+ const kinds = (options.kinds ?? []).filter(
140
+ (kind) => kind !== "" && isCodeActionKindWithin(kind, family),
141
+ );
142
+ if (kinds.length === 0 && !anyKind) return supportedOperation("not-advertised");
143
+ return {
144
+ server: "advertised",
145
+ supi: "supported",
146
+ ...(kinds.length > 0 ? { kinds } : {}),
147
+ ...(anyKind ? { anyKind } : {}),
148
+ };
161
149
  }
162
150
 
163
151
  /** Build the honest operation-support projection for one negotiated server. */
@@ -185,27 +173,8 @@ export function getServerOperationSupport(
185
173
  return {
186
174
  rename_symbol: supportedOperation(renameServer),
187
175
  prepare_rename: supportedOperation(prepareServer),
188
- extract_function: supportedOperation(
189
- codeActionAdvertisement(capabilities, REFACTOR_CODE_ACTION_KINDS.extract_function),
190
- "limited",
191
- EXTRACTION_NAME_LIMITATION,
192
- ),
193
- extract_variable: supportedOperation(
194
- codeActionAdvertisement(capabilities, REFACTOR_CODE_ACTION_KINDS.extract_variable),
195
- "limited",
196
- EXTRACTION_NAME_LIMITATION,
197
- ),
198
- update_imports: supportedOperation(
199
- codeActionAdvertisement(capabilities, REFACTOR_CODE_ACTION_KINDS.update_imports),
200
- ),
201
- delete_dead_code: deleteDeadCodeSupport(capabilities),
176
+ code_action: codeActionFamilySupport(capabilities, "refactor"),
177
+ source_action: codeActionFamilySupport(capabilities, "source"),
202
178
  code_action_resolve: supportedOperation(resolveServer),
203
179
  };
204
180
  }
205
-
206
- /** Get the exact operation-specific kinds used in a code-action request. */
207
- export function codeActionKindsForOperation(
208
- operation: keyof typeof REFACTOR_CODE_ACTION_KINDS,
209
- ): readonly string[] {
210
- return REFACTOR_CODE_ACTION_KINDS[operation];
211
- }
@@ -162,6 +162,16 @@ export function getFileScopeDecision(filePath: string, cwd: string): FileScopeDe
162
162
  };
163
163
  }
164
164
 
165
+ /**
166
+ * Whether a project config's program includes a file, by its own file set or
167
+ * include/exclude rules. Unlike {@link getFileScopeDecision}, the config need
168
+ * not be the file's nearest one (e.g. a test config including `../src`).
169
+ */
170
+ export function projectConfigIncludesFile(configPath: string, filePath: string): boolean {
171
+ const parsed = parseProjectConfig(configPath);
172
+ return parsed !== null && decideFileScope(parsed, path.resolve(filePath)).included;
173
+ }
174
+
165
175
  function isOutOfTree(cwd: string, absolutePath: string): boolean {
166
176
  const relative = path.relative(path.resolve(cwd), absolutePath);
167
177
  return relative.startsWith(`..${path.sep}`) || relative === "..";
@@ -0,0 +1,118 @@
1
+ import * as path from "node:path";
2
+ import { collectExtendedProjectConfigs } from "../config/tsconfig-extends.ts";
3
+ import { normalizeTsconfigPath } from "../config/tsconfig-path.ts";
4
+ import { getFileScopeDecision, projectConfigIncludesFile } from "../config/tsconfig-scope.ts";
5
+ import { type AutomaticLspPathPolicy, walkAutomaticLspTree } from "../workspace-path-policy.ts";
6
+
7
+ /** Directory depth searched for project configs below a TypeScript route root. */
8
+ const PROJECT_CONFIG_SCAN_DEPTH = 8;
9
+ /** Project config paths named in one coverage reason. */
10
+ const MAX_NAMED_PROJECTS = 3;
11
+ /** File names tsserver loads as projects; `tsconfig.*.json` variants are never auto-loaded. */
12
+ const LOADABLE_PROJECT_CONFIGS = new Set(["tsconfig.json", "jsconfig.json"]);
13
+
14
+ /** Find loadable project configs under a route root, honoring the automatic path policy. */
15
+ export function discoverTypeScriptProjectConfigs(
16
+ root: string,
17
+ policy: AutomaticLspPathPolicy,
18
+ ): string[] {
19
+ const configs: string[] = [];
20
+ walkAutomaticLspTree(policy, root, PROJECT_CONFIG_SCAN_DEPTH, (directory, entries) => {
21
+ for (const entry of entries) {
22
+ if (entry.isFile() && LOADABLE_PROJECT_CONFIGS.has(entry.name)) {
23
+ configs.push(path.join(directory, entry.name));
24
+ }
25
+ }
26
+ return false;
27
+ });
28
+ return configs;
29
+ }
30
+
31
+ /**
32
+ * Project configs whose program includes `file` but show no sign of being loaded.
33
+ *
34
+ * tsserver answers project-wide requests such as references and
35
+ * implementations only from loaded projects, and loads a project only when one
36
+ * of its files is open. An unloaded including project (typically a test
37
+ * config that includes `../src`) can hold matches the answer omits.
38
+ *
39
+ * A project counts as loaded when it is the nearest config of an open file or
40
+ * of a file in the answer (`resultFiles`): tsserver also loads projects on its
41
+ * own during a search, and a match from a project's own file proves it was searched.
42
+ *
43
+ * Base configs extended by another discovered config are skipped: their
44
+ * default include usually spans the whole tree while they own only root
45
+ * tooling files, so counting them would mark nearly every answer partial.
46
+ */
47
+ export function findUnloadedTypeScriptProjects(options: {
48
+ readonly file: string;
49
+ readonly cwd: string;
50
+ readonly openFiles: readonly string[];
51
+ readonly resultFiles?: readonly string[];
52
+ readonly configs: readonly string[];
53
+ }): string[] {
54
+ const baseConfigs = new Set(
55
+ options.configs.flatMap((config) => [...collectExtendedProjectConfigs(config)]),
56
+ );
57
+ const loaded = new Set<string>();
58
+ for (const loadedFile of [...options.openFiles, ...(options.resultFiles ?? [])]) {
59
+ const decision = getFileScopeDecision(loadedFile, options.cwd);
60
+ if (decision.status === "included" && decision.configPath) {
61
+ loaded.add(normalizeTsconfigPath(decision.configPath));
62
+ }
63
+ }
64
+ return options.configs.filter((config) => {
65
+ const key = normalizeTsconfigPath(config);
66
+ return (
67
+ !loaded.has(key) && !baseConfigs.has(key) && projectConfigIncludesFile(config, options.file)
68
+ );
69
+ });
70
+ }
71
+
72
+ /**
73
+ * Other projects that tsserver's implementation search skips for `file`.
74
+ *
75
+ * tsserver 6 and earlier answer `textDocument/implementation` from the file's
76
+ * default project only, unlike references, which span loaded projects. Any
77
+ * other non-base project may implement the target through an import, and
78
+ * opening its files does not change the answer.
79
+ */
80
+ export function findSkippedImplementationProjects(options: {
81
+ readonly file: string;
82
+ readonly cwd: string;
83
+ readonly configs: readonly string[];
84
+ }): string[] {
85
+ const baseConfigs = new Set(
86
+ options.configs.flatMap((config) => [...collectExtendedProjectConfigs(config)]),
87
+ );
88
+ const decision = getFileScopeDecision(options.file, options.cwd);
89
+ const own = decision.configPath ? normalizeTsconfigPath(decision.configPath) : undefined;
90
+ return options.configs.filter((config) => {
91
+ const key = normalizeTsconfigPath(config);
92
+ return key !== own && !baseConfigs.has(key);
93
+ });
94
+ }
95
+
96
+ /** Partial-result reason for tsserver's single-project implementation search. */
97
+ export function singleProjectImplementationReason(skipped: readonly string[]): string {
98
+ const noun = skipped.length === 1 ? "1 other project is" : `${skipped.length} other projects are`;
99
+ return (
100
+ "TypeScript coverage is limited: the TypeScript 6 or earlier server (tsserver) " +
101
+ `searches implementations only in this file's project; ${noun} not searched. ` +
102
+ "Use references to find uses in other projects, or use TypeScript 7."
103
+ );
104
+ }
105
+
106
+ /** Actionable partial-result reason naming a bounded set of unloaded projects. */
107
+ export function typeScriptCoverageGapReason(unloaded: readonly string[], cwd: string): string {
108
+ const named = unloaded
109
+ .slice(0, MAX_NAMED_PROJECTS)
110
+ .map((config) => path.relative(cwd, config) || path.basename(config));
111
+ const more = unloaded.length > named.length ? `, +${unloaded.length - named.length} more` : "";
112
+ const noun = unloaded.length === 1 ? "project that includes" : "projects that include";
113
+ return (
114
+ `TypeScript coverage is limited: ${unloaded.length} ${noun} this file ` +
115
+ `${unloaded.length === 1 ? "is" : "are"} not loaded (${named.join(", ")}${more}). ` +
116
+ "Open a file from each project and retry."
117
+ );
118
+ }
@@ -128,6 +128,13 @@ import type {
128
128
  OutstandingDiagnosticSummaryEntry,
129
129
  ServerStatus,
130
130
  } from "./manager-types.ts";
131
+ import {
132
+ discoverTypeScriptProjectConfigs,
133
+ findSkippedImplementationProjects,
134
+ findUnloadedTypeScriptProjects,
135
+ singleProjectImplementationReason,
136
+ typeScriptCoverageGapReason,
137
+ } from "./manager-typescript-coverage.ts";
131
138
  import {
132
139
  recoverWorkspaceDiagnostics as recoverWorkspaceDiagnosticsImpl,
133
140
  type WorkspaceRecoveryResult,
@@ -141,6 +148,7 @@ import {
141
148
  workspaceSymbolCollectionResult,
142
149
  workspaceSymbolCoverageGap,
143
150
  } from "./manager-workspace-symbol.ts";
151
+ import { isFileLocalTopLevelDeclaration } from "./typescript-file-local.ts";
144
152
 
145
153
  type UnavailableReason = "missing-command" | "start-failed" | "runtime-error";
146
154
 
@@ -412,6 +420,8 @@ export class LspManager {
412
420
  private warmedSemanticProjects = new Set<string>();
413
421
  /** In-flight warm-up probes keyed by project key so concurrent callers share one probe. */
414
422
  private pendingWarmProbes = new Map<string, Promise<void>>();
423
+ /** Loadable TypeScript project configs per route root; cleared when a config changes. */
424
+ private typeScriptProjectConfigs = new Map<string, string[]>();
415
425
  /** One process-crash recovery budget and shared replacement per route. */
416
426
  private processCrashRecoveries = new Map<string, ProcessCrashRecoveryState>();
417
427
  /** Client generations that completed the initialize handshake. */
@@ -572,6 +582,53 @@ export class LspManager {
572
582
  );
573
583
  }
574
584
 
585
+ /**
586
+ * Coverage gap for a project-wide TypeScript request answered by `client`.
587
+ *
588
+ * Returns a partial-result reason when a TypeScript project that includes
589
+ * `filePath` owns neither an open file nor a file in `resultFiles`, so
590
+ * tsserver may not have searched it; otherwise null. Non-TypeScript routes
591
+ * always return null.
592
+ *
593
+ * Implementation requests not known to use the native backend report every
594
+ * other project instead: tsserver searches only the file's own project.
595
+ *
596
+ * No gap applies when `position` (zero-based) names a top-level declaration
597
+ * its module does not export: no other project can reference it.
598
+ */
599
+ typeScriptProjectCoverageGap(
600
+ client: LspClient,
601
+ filePath: string,
602
+ request: "references" | "implementation",
603
+ evidence: {
604
+ readonly resultFiles?: readonly string[];
605
+ readonly position?: { readonly line: number; readonly character: number };
606
+ } = {},
607
+ ): string | null {
608
+ const { resultFiles = [], position } = evidence;
609
+ if (client.name !== "typescript") return null;
610
+ const rootKey = path.resolve(client.root);
611
+ let configs = this.typeScriptProjectConfigs.get(rootKey);
612
+ if (!configs) {
613
+ configs = discoverTypeScriptProjectConfigs(rootKey, this.automaticPathPolicy);
614
+ this.typeScriptProjectConfigs.set(rootKey, configs);
615
+ }
616
+ const file = resolveSessionPath(this.cwd, filePath);
617
+ if (position && isFileLocalTopLevelDeclaration(file, position)) return null;
618
+ if (request === "implementation" && client.typescriptSelection?.backend !== "native") {
619
+ const skipped = findSkippedImplementationProjects({ file, cwd: this.cwd, configs });
620
+ return skipped.length > 0 ? singleProjectImplementationReason(skipped) : null;
621
+ }
622
+ const unloaded = findUnloadedTypeScriptProjects({
623
+ file,
624
+ cwd: this.cwd,
625
+ openFiles: client.openFiles,
626
+ resultFiles,
627
+ configs,
628
+ });
629
+ return unloaded.length > 0 ? typeScriptCoverageGapReason(unloaded, this.cwd) : null;
630
+ }
631
+
575
632
  /** Get or create an LSP client for the given file. */
576
633
  async getClientForFile(
577
634
  filePath: string,
@@ -1050,7 +1107,9 @@ export class LspManager {
1050
1107
  if (client.status !== "running" || !client.ready) return;
1051
1108
  const key = clientKey(client.name, client.root);
1052
1109
  const recovery = this.processCrashRecoveries.get(key);
1053
- if (recovery) {
1110
+ // An in-flight recovery owns its record until it settles; removing it here
1111
+ // would make that recovery report cancellation for a live replacement.
1112
+ if (recovery && !recovery.pending) {
1054
1113
  recovery.attemptConsumed = false;
1055
1114
  recovery.statusReason = undefined;
1056
1115
  recovery.failureMessage = undefined;
@@ -2397,7 +2456,7 @@ export class LspManager {
2397
2456
  */
2398
2457
  noteWorkspaceChanges(changes: FileEvent[]): void {
2399
2458
  if (changes.length > 0) this.invalidationEpoch++;
2400
- invalidateProjectConfigCaches(changes);
2459
+ if (invalidateProjectConfigCaches(changes)) this.typeScriptProjectConfigs.clear();
2401
2460
  this.notifyWorkspaceFileChanges(changes);
2402
2461
  }
2403
2462
 
@@ -2586,6 +2645,7 @@ export class LspManager {
2586
2645
  this.warmedWorkspaceSymbolFiles.clear();
2587
2646
  this.warmedSemanticProjects.clear();
2588
2647
  this.pendingWarmProbes.clear();
2648
+ this.typeScriptProjectConfigs.clear();
2589
2649
  this.pendingRestarts.clear();
2590
2650
  }
2591
2651
  /** Get status of all servers. */
@@ -3437,15 +3497,19 @@ export class LspManager {
3437
3497
  }
3438
3498
  }
3439
3499
 
3440
- function invalidateProjectConfigCaches(changes: readonly FileEvent[]): void {
3500
+ /** Invalidate parsed project configs; returns whether any config changed. */
3501
+ function invalidateProjectConfigCaches(changes: readonly FileEvent[]): boolean {
3502
+ let changed = false;
3441
3503
  for (const change of changes) {
3442
3504
  const configPath = uriToFile(change.uri);
3443
3505
  if (!isProjectConfigFileName(path.basename(configPath))) continue;
3506
+ changed = true;
3444
3507
  invalidateTsconfigCacheForConfig(configPath);
3445
3508
  if (change.type === FileChangeType.Created) {
3446
3509
  invalidateTsconfigCacheForConfigDir(path.dirname(configPath));
3447
3510
  }
3448
3511
  }
3512
+ return changed;
3449
3513
  }
3450
3514
 
3451
3515
  function collectClientDiagnosticEvidenceDocuments(
@@ -0,0 +1,87 @@
1
+ import * as fs from "node:fs";
2
+ import ts from "typescript";
3
+
4
+ /**
5
+ * Whether the identifier at `position` names a top-level declaration that only
6
+ * its own file can reference: the file is an ES module and the declaration is
7
+ * neither exported by a modifier nor by an export clause or assignment.
8
+ *
9
+ * Other TypeScript projects can then hold no references or implementations,
10
+ * so a project coverage gap cannot apply. Script files (no import or export),
11
+ * members, unreadable files, and non-declaration positions return false.
12
+ *
13
+ * @param position Zero-based LSP position.
14
+ */
15
+ export function isFileLocalTopLevelDeclaration(
16
+ filePath: string,
17
+ position: { readonly line: number; readonly character: number },
18
+ ): boolean {
19
+ let text: string;
20
+ try {
21
+ text = fs.readFileSync(filePath, "utf8");
22
+ } catch {
23
+ return false;
24
+ }
25
+ const sourceFile = ts.createSourceFile(filePath, text, ts.ScriptTarget.Latest, true);
26
+ if (!ts.isExternalModule(sourceFile)) return false;
27
+ let offset: number;
28
+ try {
29
+ offset = ts.getPositionOfLineAndCharacter(sourceFile, position.line, position.character);
30
+ } catch {
31
+ return false;
32
+ }
33
+ const statement = sourceFile.statements.find(
34
+ (candidate) => candidate.getStart(sourceFile) <= offset && offset < candidate.end,
35
+ );
36
+ if (!statement) return false;
37
+ const name = topLevelDeclarationNameAt(statement, offset, sourceFile);
38
+ return name !== null && !hasExportModifier(statement) && !exportedByName(sourceFile, name);
39
+ }
40
+
41
+ /** Name of the declaration `statement` declares at `offset`, or null. */
42
+ function topLevelDeclarationNameAt(
43
+ statement: ts.Statement,
44
+ offset: number,
45
+ sourceFile: ts.SourceFile,
46
+ ): string | null {
47
+ const names: ts.Identifier[] = [];
48
+ if (
49
+ ts.isFunctionDeclaration(statement) ||
50
+ ts.isClassDeclaration(statement) ||
51
+ ts.isInterfaceDeclaration(statement) ||
52
+ ts.isTypeAliasDeclaration(statement) ||
53
+ ts.isEnumDeclaration(statement)
54
+ ) {
55
+ if (statement.name) names.push(statement.name);
56
+ } else if (ts.isVariableStatement(statement)) {
57
+ for (const declaration of statement.declarationList.declarations) {
58
+ if (ts.isIdentifier(declaration.name)) names.push(declaration.name);
59
+ }
60
+ }
61
+ const hit = names.find((name) => name.getStart(sourceFile) <= offset && offset <= name.end);
62
+ return hit ? hit.text : null;
63
+ }
64
+
65
+ function hasExportModifier(statement: ts.Statement): boolean {
66
+ return (
67
+ ts.canHaveModifiers(statement) &&
68
+ (ts.getModifiers(statement) ?? []).some(
69
+ (modifier) =>
70
+ modifier.kind === ts.SyntaxKind.ExportKeyword ||
71
+ modifier.kind === ts.SyntaxKind.DeclareKeyword,
72
+ )
73
+ );
74
+ }
75
+
76
+ /** `export { name }`, `export default name`, or `export = name` in this file. */
77
+ function exportedByName(sourceFile: ts.SourceFile, name: string): boolean {
78
+ return sourceFile.statements.some((statement) => {
79
+ if (ts.isExportAssignment(statement)) {
80
+ return ts.isIdentifier(statement.expression) && statement.expression.text === name;
81
+ }
82
+ if (!ts.isExportDeclaration(statement) || statement.moduleSpecifier) return false;
83
+ const clause = statement.exportClause;
84
+ if (!clause || !ts.isNamedExports(clause)) return false;
85
+ return clause.elements.some((element) => (element.propertyName ?? element.name).text === name);
86
+ });
87
+ }
@@ -7,10 +7,10 @@ import type {
7
7
  import type { WorkspaceLspRuntime } from "../session/runtime-registry.ts";
8
8
  import {
9
9
  collectCodeActionResults,
10
- isDeleteDeadCodeCodeAction,
11
- isUpdateImportsCodeAction,
12
- runFilteredCodeActionRefactor,
10
+ runCodeActionRefactor,
13
11
  runRenameRefactor,
12
+ runSourceActionRefactor,
13
+ wholeDocumentRange,
14
14
  } from "./refactor-planning.ts";
15
15
 
16
16
  /** Build optional refactor methods for the semantic provider adapter. */
@@ -45,12 +45,6 @@ async function planRefactor(
45
45
  ): Promise<RefactorResult> {
46
46
  switch (request.operation) {
47
47
  case "rename_symbol":
48
- if (!request.newName) {
49
- return {
50
- kind: "unavailable",
51
- reason: 'Refactor operation "rename_symbol" requires `newName`.',
52
- };
53
- }
54
48
  return runRenameRefactor({
55
49
  lsp,
56
50
  file: request.file,
@@ -58,39 +52,31 @@ async function planRefactor(
58
52
  newName: request.newName,
59
53
  control,
60
54
  });
61
- case "extract_function":
62
- case "extract_variable":
63
- return {
64
- kind: "unavailable",
65
- reason: `Refactor operation "${request.operation}" is unavailable: standard LSP code actions do not provide a safe exact extraction-name contract.`,
66
- };
67
- case "update_imports":
68
- return runFilteredCodeActionRefactor({
55
+ case "code_action":
56
+ return runCodeActionRefactor({
69
57
  lsp,
70
58
  file: request.file,
71
- position: request.position,
72
- operation: "update_imports",
73
- matches: isUpdateImportsCodeAction,
59
+ range: request.range,
60
+ kind: request.kind,
61
+ title: request.title,
62
+ label: "Code action",
74
63
  control,
75
64
  });
76
- case "delete_dead_code":
77
- return runFilteredCodeActionRefactor({
65
+ case "source_action": {
66
+ // LSP source actions apply to the entire file, so request the whole document.
67
+ const range = wholeDocumentRange(request.file);
68
+ if (!range) {
69
+ return { kind: "unavailable", reason: `File ${request.file} could not be read.` };
70
+ }
71
+ return runSourceActionRefactor({
78
72
  lsp,
79
73
  file: request.file,
80
- position: request.position,
81
- operation: "delete_dead_code",
82
- matches: isDeleteDeadCodeCodeAction,
74
+ range,
75
+ kind: request.kind,
76
+ title: request.title,
77
+ label: "Source action",
83
78
  control,
84
79
  });
85
- case "rename_file":
86
- case "move_file":
87
- return {
88
- kind: "unavailable",
89
- reason: `Refactor operation "${request.operation}" is not supported yet. File/resource operations are deferred.`,
90
- };
80
+ }
91
81
  }
92
- return {
93
- kind: "unavailable",
94
- reason: `Refactor operation "${request.operation}" is not supported by the active semantic provider.`,
95
- };
96
82
  }