@mrclrchtr/supi-code-intelligence 9.1.1 → 10.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 (94) hide show
  1. package/README.md +24 -8
  2. package/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  3. package/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  4. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  5. package/node_modules/@mrclrchtr/supi-lsp/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  6. package/node_modules/@mrclrchtr/supi-lsp/package.json +3 -3
  7. package/node_modules/@mrclrchtr/supi-lsp/src/manager/manager-workspace-symbol.ts +29 -0
  8. package/node_modules/@mrclrchtr/supi-lsp/src/manager/manager.ts +25 -22
  9. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-code-runtime/package.json +1 -1
  10. package/node_modules/@mrclrchtr/supi-tree-sitter/node_modules/@mrclrchtr/supi-core/package.json +1 -1
  11. package/node_modules/@mrclrchtr/supi-tree-sitter/package.json +3 -3
  12. package/node_modules/ignore/LICENSE-MIT +21 -0
  13. package/node_modules/ignore/README.md +501 -0
  14. package/node_modules/ignore/index.d.ts +81 -0
  15. package/node_modules/ignore/index.js +1566 -0
  16. package/node_modules/ignore/legacy.js +1346 -0
  17. package/node_modules/ignore/package.json +93 -0
  18. package/node_modules/smol-toml/LICENSE +24 -0
  19. package/node_modules/smol-toml/README.md +605 -0
  20. package/node_modules/smol-toml/dist/date.d.ts +41 -0
  21. package/node_modules/smol-toml/dist/date.js +156 -0
  22. package/node_modules/smol-toml/dist/error.d.ts +37 -0
  23. package/node_modules/smol-toml/dist/error.js +67 -0
  24. package/node_modules/smol-toml/dist/extract.js +293 -0
  25. package/node_modules/smol-toml/dist/index.cjs +771 -0
  26. package/node_modules/smol-toml/dist/index.d.ts +44 -0
  27. package/node_modules/smol-toml/dist/index.js +34 -0
  28. package/node_modules/smol-toml/dist/parse.d.ts +73 -0
  29. package/node_modules/smol-toml/dist/parse.js +164 -0
  30. package/node_modules/smol-toml/dist/primitive.js +151 -0
  31. package/node_modules/smol-toml/dist/stringify.d.ts +32 -0
  32. package/node_modules/smol-toml/dist/stringify.js +219 -0
  33. package/node_modules/smol-toml/dist/struct.js +156 -0
  34. package/node_modules/smol-toml/dist/util.d.ts +43 -0
  35. package/node_modules/smol-toml/dist/util.js +57 -0
  36. package/node_modules/smol-toml/package.json +68 -0
  37. package/package.json +9 -5
  38. package/src/analysis/evidence.ts +2 -0
  39. package/src/analysis/target/symbol.ts +19 -8
  40. package/src/analysis/target/types.ts +2 -0
  41. package/src/analysis/workspace/exclusions.ts +35 -0
  42. package/src/analysis/workspace/git.ts +130 -0
  43. package/src/analysis/workspace/go-parser.ts +330 -0
  44. package/src/analysis/workspace/go.ts +154 -0
  45. package/src/analysis/workspace/index.ts +28 -0
  46. package/src/analysis/workspace/inventory.ts +316 -0
  47. package/src/analysis/workspace/membership.ts +223 -0
  48. package/src/analysis/workspace/metadata-types.ts +56 -0
  49. package/src/analysis/workspace/metadata-utils.ts +60 -0
  50. package/src/analysis/workspace/metadata.ts +148 -0
  51. package/src/analysis/workspace/node.ts +214 -0
  52. package/src/analysis/workspace/observe.ts +161 -0
  53. package/src/analysis/workspace/paths.ts +65 -0
  54. package/src/analysis/workspace/python.ts +158 -0
  55. package/src/analysis/workspace/relationships.ts +87 -0
  56. package/src/analysis/workspace/rust.ts +238 -0
  57. package/src/analysis/workspace/safe-path.ts +20 -0
  58. package/src/analysis/workspace/types.ts +245 -0
  59. package/src/app/app.ts +1 -0
  60. package/src/config.ts +4 -4
  61. package/src/extension.ts +16 -49
  62. package/src/overview/budget.ts +23 -0
  63. package/src/overview/collect.ts +22 -0
  64. package/src/session/orientation/workspace-context.ts +166 -0
  65. package/src/session/orientation-types.ts +8 -1
  66. package/src/session/orientation-workflow.ts +16 -3
  67. package/src/session/session.ts +2 -3
  68. package/src/session/target-workflow.ts +2 -0
  69. package/src/tool/code_find/execute.ts +1 -1
  70. package/src/tool/code_find/result.ts +10 -3
  71. package/src/tool/code_graph/result.ts +7 -2
  72. package/src/tool/code_orientation/execute.ts +8 -1
  73. package/src/tool/code_orientation/markdown.ts +58 -8
  74. package/src/tool/code_orientation/result.ts +17 -5
  75. package/src/tool/code_orientation/workspace-format.ts +74 -0
  76. package/src/tool/code_orientation/workspace-instructions.ts +105 -0
  77. package/src/tool/code_orientation/workspace-output.ts +118 -0
  78. package/src/tool/code_orientation/workspace-projects.ts +177 -0
  79. package/src/tool/code_orientation/workspace-sections.ts +217 -0
  80. package/src/tool/code_resolve/execute.ts +1 -1
  81. package/src/tool/code_resolve/markdown.ts +4 -1
  82. package/src/tool/code_resolve/result.ts +14 -2
  83. package/src/tool/register.ts +2 -0
  84. package/src/tool/result/candidate-row.ts +22 -0
  85. package/src/tool/result/display.ts +6 -0
  86. package/src/tool/result/errors.ts +12 -0
  87. package/src/tool/result/types.ts +2 -0
  88. package/src/types/execution.ts +2 -0
  89. package/src/ui/tui/candidate-disclosure.ts +43 -0
  90. package/src/ui/tui/candidate-selection.ts +24 -5
  91. package/src/ui/tui/common.ts +3 -0
  92. package/src/overview/overview-data.ts +0 -154
  93. package/src/overview/overview.ts +0 -91
  94. package/src/overview/types.ts +0 -23
@@ -0,0 +1,35 @@
1
+ /** Gitignore-style exclusion matching for workspace observation. */
2
+
3
+ import ignore from "ignore";
4
+ import { errorMessage } from "./paths.ts";
5
+ import type { WorkspaceObservationError } from "./types.ts";
6
+
7
+ /** Compiled caller exclusions. Matching is case-sensitive and root-relative. */
8
+ export interface ExclusionMatcher {
9
+ matches(relativePath: string, directory: boolean): boolean;
10
+ }
11
+
12
+ /** Compile caller patterns one at a time so one invalid pattern cannot erase valid exclusions. */
13
+ export function createExclusionMatcher(
14
+ patterns: readonly string[],
15
+ errors: WorkspaceObservationError[],
16
+ ): ExclusionMatcher {
17
+ const matcher = ignore({ ignorecase: false });
18
+ for (const pattern of patterns) {
19
+ try {
20
+ matcher.add(pattern);
21
+ } catch (error) {
22
+ errors.push({
23
+ stage: "inventory",
24
+ path: null,
25
+ reason: `Invalid exclude pattern \`${pattern}\`: ${errorMessage(error)}`,
26
+ });
27
+ }
28
+ }
29
+ return {
30
+ matches(relativePath, directory) {
31
+ if (relativePath === ".") return false;
32
+ return matcher.ignores(directory ? `${relativePath}/` : relativePath);
33
+ },
34
+ };
35
+ }
@@ -0,0 +1,130 @@
1
+ /** Git inventory command boundary. This module never interprets project metadata. */
2
+
3
+ import { execFile } from "node:child_process";
4
+ import * as path from "node:path";
5
+ import { promisify } from "node:util";
6
+ import type { CodeRequestControl } from "@mrclrchtr/supi-code-runtime/api";
7
+ import { errorMessage } from "./paths.ts";
8
+
9
+ const execFileAsync = promisify(execFile);
10
+ const MAX_GIT_OUTPUT_BYTES = 64 * 1024 * 1024;
11
+
12
+ /** Git path list outcome before filesystem classification and exclusions. */
13
+ export type GitPathResult =
14
+ | { readonly kind: "git"; readonly paths: readonly string[] }
15
+ | { readonly kind: "not-git" }
16
+ | { readonly kind: "deadline" }
17
+ | { readonly kind: "unavailable"; readonly reason: string }
18
+ | { readonly kind: "missing-command"; readonly reason: string };
19
+
20
+ type GitCommandResult =
21
+ | { readonly kind: "ok"; readonly stdout: string }
22
+ | { readonly kind: "deadline" }
23
+ | { readonly kind: "error"; readonly reason: string; readonly missing?: boolean };
24
+
25
+ /**
26
+ * Read tracked and untracked paths under `root`.
27
+ *
28
+ * `git ls-files` runs from the requested root, so paths stay inside that root
29
+ * even when it is a repository subdirectory. Without `--exclude-standard`,
30
+ * ignored untracked paths stay visible for explicit focus inspection.
31
+ */
32
+ export async function readGitPaths(
33
+ root: string,
34
+ includeIgnored: boolean,
35
+ control: CodeRequestControl | undefined,
36
+ ): Promise<GitPathResult> {
37
+ const inside = await runGit(root, ["rev-parse", "--is-inside-work-tree"], control);
38
+ if (inside.kind === "deadline") return inside;
39
+ if (inside.kind === "error") {
40
+ if (inside.missing) return { kind: "missing-command", reason: inside.reason };
41
+ if (isNotRepository(inside.reason)) return { kind: "not-git" };
42
+ return { kind: "unavailable", reason: inside.reason };
43
+ }
44
+ if (inside.stdout.trim() !== "true") return { kind: "not-git" };
45
+
46
+ const args = ["ls-files", "-z", "--cached", "--others"];
47
+ if (!includeIgnored) args.push("--exclude-standard");
48
+ args.push("--", ".");
49
+ const listed = await runGit(root, args, control);
50
+ if (listed.kind === "deadline") return listed;
51
+ if (listed.kind === "error") {
52
+ if (isNotRepository(listed.reason)) return { kind: "not-git" };
53
+ return { kind: "unavailable", reason: listed.reason };
54
+ }
55
+ return {
56
+ kind: "git",
57
+ paths: [...new Set(listed.stdout.split("\0").filter((path) => path.length > 0))],
58
+ };
59
+ }
60
+
61
+ /** Check the explicit directory against Git's own ignore rules, without name heuristics. */
62
+ export async function isGitIgnoredDirectory(
63
+ directory: string,
64
+ control?: CodeRequestControl,
65
+ ): Promise<boolean> {
66
+ const result = await runGit(
67
+ path.dirname(directory),
68
+ ["check-ignore", "--no-index", "-q", "--", path.basename(directory)],
69
+ control,
70
+ );
71
+ return result.kind === "ok";
72
+ }
73
+
74
+ function isNotRepository(reason: string): boolean {
75
+ return /not a git repository|not a git work tree|not a work tree/i.test(reason);
76
+ }
77
+
78
+ async function runGit(
79
+ root: string,
80
+ args: readonly string[],
81
+ control: CodeRequestControl | undefined,
82
+ ): Promise<GitCommandResult> {
83
+ control?.signal?.throwIfAborted();
84
+ const deadline = finiteDeadline(control);
85
+ if (deadline !== null && Date.now() >= deadline) return { kind: "deadline" };
86
+
87
+ const controller = new AbortController();
88
+ const forwardAbort = () => controller.abort(control?.signal?.reason);
89
+ control?.signal?.addEventListener("abort", forwardAbort, { once: true });
90
+ const timer =
91
+ deadline === null
92
+ ? null
93
+ : setTimeout(() => controller.abort(), Math.max(0, deadline - Date.now()));
94
+
95
+ try {
96
+ const { stdout } = await execFileAsync("git", ["-c", "core.fsmonitor=false", ...args], {
97
+ env: {
98
+ ...process.env,
99
+ LC_ALL: "C",
100
+ GIT_OPTIONAL_LOCKS: "0",
101
+ GIT_DIR: undefined,
102
+ GIT_WORK_TREE: undefined,
103
+ GIT_INDEX_FILE: undefined,
104
+ },
105
+ cwd: root,
106
+ encoding: "utf8",
107
+ maxBuffer: MAX_GIT_OUTPUT_BYTES,
108
+ signal: controller.signal,
109
+ });
110
+ if (deadline !== null && Date.now() >= deadline) return { kind: "deadline" };
111
+ return { kind: "ok", stdout };
112
+ } catch (error) {
113
+ control?.signal?.throwIfAborted();
114
+ if (deadline !== null && Date.now() >= deadline) return { kind: "deadline" };
115
+ return {
116
+ kind: "error",
117
+ reason: errorMessage(error),
118
+ missing:
119
+ typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT",
120
+ };
121
+ } finally {
122
+ if (timer) clearTimeout(timer);
123
+ control?.signal?.removeEventListener("abort", forwardAbort);
124
+ }
125
+ }
126
+
127
+ function finiteDeadline(control: CodeRequestControl | undefined): number | null {
128
+ const deadline = control?.deadline;
129
+ return typeof deadline === "number" && Number.isFinite(deadline) ? deadline : null;
130
+ }
@@ -0,0 +1,330 @@
1
+ /** Grammar-aware tokenizer and parser for supported go.mod and go.work directives. */
2
+
3
+ /** One parsed directive entry. Block entries repeat the block directive name. */
4
+ export interface GoDirective {
5
+ readonly name: string;
6
+ readonly args: readonly string[];
7
+ readonly line: number;
8
+ readonly block: boolean;
9
+ }
10
+
11
+ /** A tokenizer or grammar limitation with its 1-based source line. */
12
+ export interface GoParseIssue {
13
+ readonly line: number;
14
+ readonly reason: string;
15
+ }
16
+
17
+ /** Parsed directives and syntax issues; one malformed line does not discard earlier lines. */
18
+ export interface GoParseResult {
19
+ readonly directives: readonly GoDirective[];
20
+ readonly issues: readonly GoParseIssue[];
21
+ }
22
+
23
+ type TokenKind = "word" | "newline" | "lparen" | "rparen" | "arrow" | "punct";
24
+
25
+ interface GoToken {
26
+ readonly kind: TokenKind;
27
+ readonly value: string;
28
+ readonly line: number;
29
+ }
30
+
31
+ interface TokenizeResult {
32
+ readonly tokens: readonly GoToken[];
33
+ readonly issues: readonly GoParseIssue[];
34
+ }
35
+
36
+ interface TokenizerState {
37
+ readonly tokens: GoToken[];
38
+ readonly issues: GoParseIssue[];
39
+ index: number;
40
+ line: number;
41
+ readonly text: string;
42
+ }
43
+
44
+ /** Parse go.mod or go.work text into directive entries without assigning project roles. */
45
+ export function parseGoFile(text: string): GoParseResult {
46
+ const tokenized = tokenizeGoFile(text);
47
+ return parseGoTokens(tokenized.tokens, tokenized.issues);
48
+ }
49
+
50
+ function tokenizeGoFile(text: string): TokenizeResult {
51
+ const state: TokenizerState = { text, tokens: [], issues: [], index: 0, line: 1 };
52
+ while (state.index < text.length) {
53
+ if (!consumeNext(state)) break;
54
+ }
55
+ return { tokens: state.tokens, issues: state.issues };
56
+ }
57
+
58
+ function consumeNext(state: TokenizerState): boolean {
59
+ const character = state.text[state.index] ?? "";
60
+ if (character === "\n") {
61
+ pushNewline(state);
62
+ return true;
63
+ }
64
+ if (/\s/.test(character)) {
65
+ state.index++;
66
+ return true;
67
+ }
68
+ if (startsComment(state, "//")) {
69
+ skipLineComment(state);
70
+ return true;
71
+ }
72
+ if (startsComment(state, "/*")) return skipBlockComment(state);
73
+ if (character === '"' || character === "`") return consumeQuoted(state);
74
+ return consumePunctuationOrWord(state);
75
+ }
76
+
77
+ function pushNewline(state: TokenizerState): void {
78
+ state.tokens.push({ kind: "newline", value: "\n", line: state.line });
79
+ state.line++;
80
+ state.index++;
81
+ }
82
+
83
+ function startsComment(state: TokenizerState, prefix: string): boolean {
84
+ return state.text.slice(state.index, state.index + prefix.length) === prefix;
85
+ }
86
+
87
+ function skipLineComment(state: TokenizerState): void {
88
+ state.index += 2;
89
+ while (state.index < state.text.length && state.text[state.index] !== "\n") state.index++;
90
+ }
91
+
92
+ function skipBlockComment(state: TokenizerState): boolean {
93
+ const end = state.text.indexOf("*/", state.index + 2);
94
+ if (end < 0) {
95
+ state.issues.push({ line: state.line, reason: "Unterminated block comment." });
96
+ return false;
97
+ }
98
+ state.line += countNewlines(state.text.slice(state.index + 2, end));
99
+ state.index = end + 2;
100
+ return true;
101
+ }
102
+
103
+ function consumeQuoted(state: TokenizerState): boolean {
104
+ const quoted = readQuoted(state.text, state.index, state.line, state.issues);
105
+ if (!quoted) return false;
106
+ state.tokens.push({ kind: "word", value: quoted.value, line: state.line });
107
+ state.line += quoted.newlines;
108
+ state.index = quoted.end;
109
+ return true;
110
+ }
111
+
112
+ function consumePunctuationOrWord(state: TokenizerState): boolean {
113
+ const punctuation = readPunctuation(state.text, state.index);
114
+ if (punctuation) {
115
+ state.tokens.push({ kind: punctuation.kind, value: punctuation.value, line: state.line });
116
+ state.index += punctuation.value.length;
117
+ return true;
118
+ }
119
+ const start = state.index;
120
+ while (
121
+ state.index < state.text.length &&
122
+ !isWordBoundary(state.text[state.index] ?? "") &&
123
+ !startsComment(state, "//") &&
124
+ !startsComment(state, "/*")
125
+ ) {
126
+ state.index++;
127
+ }
128
+ if (state.index === start) return false;
129
+ state.tokens.push({
130
+ kind: "word",
131
+ value: state.text.slice(start, state.index),
132
+ line: state.line,
133
+ });
134
+ return true;
135
+ }
136
+
137
+ function readQuoted(
138
+ text: string,
139
+ start: number,
140
+ line: number,
141
+ issues: GoParseIssue[],
142
+ ): { value: string; end: number; newlines: number } | null {
143
+ const quote = text[start];
144
+ let value = "";
145
+ let index = start + 1;
146
+ let newlines = 0;
147
+ while (index < text.length) {
148
+ const character = text[index] ?? "";
149
+ if (character === quote) return { value, end: index + 1, newlines };
150
+ if (character === "\n") newlines++;
151
+ if (quote === '"' && character === "\\") {
152
+ const escaped = readEscape(text, index);
153
+ if (!escaped) {
154
+ issues.push({
155
+ line,
156
+ reason: "Unsupported quoted-string escape; the value is not interpreted.",
157
+ });
158
+ return null;
159
+ }
160
+ value += escaped.value;
161
+ index = escaped.next;
162
+ continue;
163
+ }
164
+ value += character;
165
+ index++;
166
+ }
167
+ issues.push({ line, reason: "Unterminated quoted string." });
168
+ return null;
169
+ }
170
+
171
+ function readEscape(text: string, index: number): { value: string; next: number } | null {
172
+ const values: Record<string, string> = {
173
+ a: "\u0007",
174
+ b: "\b",
175
+ f: "\f",
176
+ n: "\n",
177
+ r: "\r",
178
+ t: "\t",
179
+ v: "\v",
180
+ '"': '"',
181
+ "\\": "\\",
182
+ };
183
+ const value = values[text[index + 1] ?? ""];
184
+ return value === undefined ? null : { value, next: index + 2 };
185
+ }
186
+
187
+ function readPunctuation(text: string, index: number): { kind: TokenKind; value: string } | null {
188
+ const character = text[index] ?? "";
189
+ if (character === "=" && text[index + 1] === ">") return { kind: "arrow", value: "=>" };
190
+ if (character === "(") return { kind: "lparen", value: "(" };
191
+ if (character === ")") return { kind: "rparen", value: ")" };
192
+ if (character === "=") return { kind: "punct", value: "=" };
193
+ if (character === "[" || character === "]" || character === ",") {
194
+ return { kind: "punct", value: character };
195
+ }
196
+ return null;
197
+ }
198
+
199
+ function isWordBoundary(character: string): boolean {
200
+ return /\s/.test(character) || character === "(" || character === ")" || character === "=";
201
+ }
202
+
203
+ function countNewlines(value: string): number {
204
+ let count = 0;
205
+ for (const character of value) {
206
+ if (character === "\n") count++;
207
+ }
208
+ return count;
209
+ }
210
+
211
+ interface Cursor {
212
+ index: number;
213
+ readonly tokens: readonly GoToken[];
214
+ readonly issues: GoParseIssue[];
215
+ }
216
+
217
+ function parseGoTokens(
218
+ tokens: readonly GoToken[],
219
+ initialIssues: readonly GoParseIssue[],
220
+ ): GoParseResult {
221
+ const cursor: Cursor = { index: 0, tokens, issues: [...initialIssues] };
222
+ const directives: GoDirective[] = [];
223
+ while (true) {
224
+ skipNewlines(cursor);
225
+ const head = cursor.tokens[cursor.index];
226
+ if (!head) break;
227
+ if (head.kind !== "word") {
228
+ malformed(cursor, head, "Expected a directive name.");
229
+ skipLine(cursor);
230
+ continue;
231
+ }
232
+ cursor.index++;
233
+ appendDirective(cursor, head, directives);
234
+ }
235
+ return { directives, issues: cursor.issues };
236
+ }
237
+
238
+ function appendDirective(cursor: Cursor, head: GoToken, directives: GoDirective[]): void {
239
+ const first = cursor.tokens[cursor.index];
240
+ if (first?.kind === "lparen" && first.line === head.line) {
241
+ cursor.index++;
242
+ parseBlock(cursor, head, directives);
243
+ return;
244
+ }
245
+ const lineEnd = nextLineEnd(cursor.tokens, cursor.index);
246
+ const args = tokensUntilLineEnd(cursor.tokens, cursor.index, lineEnd);
247
+ directives.push({
248
+ name: head.value,
249
+ args: args.map((token) => token.value),
250
+ line: head.line,
251
+ block: false,
252
+ });
253
+ cursor.index = lineEnd;
254
+ }
255
+
256
+ function parseBlock(cursor: Cursor, head: GoToken, directives: GoDirective[]): void {
257
+ while (true) {
258
+ skipNewlines(cursor);
259
+ const token = cursor.tokens[cursor.index];
260
+ if (!token) {
261
+ malformed(cursor, head, `Unterminated ${head.value} block.`);
262
+ return;
263
+ }
264
+ if (finishBlock(cursor, head, token)) return;
265
+ appendBlockEntry(cursor, head, token, directives);
266
+ }
267
+ }
268
+
269
+ function finishBlock(cursor: Cursor, head: GoToken, token: GoToken): boolean {
270
+ if (token.kind !== "rparen") return false;
271
+ cursor.index++;
272
+ const after = cursor.tokens[cursor.index];
273
+ if (after && after.kind !== "newline") {
274
+ malformed(cursor, after, `Unexpected token after ${head.value} block.`);
275
+ skipLine(cursor);
276
+ }
277
+ return true;
278
+ }
279
+
280
+ function appendBlockEntry(
281
+ cursor: Cursor,
282
+ head: GoToken,
283
+ token: GoToken,
284
+ directives: GoDirective[],
285
+ ): void {
286
+ const lineEnd = nextLineEnd(cursor.tokens, cursor.index);
287
+ const closeIndex = findClosingParen(cursor.tokens, cursor.index, lineEnd);
288
+ const entryEnd = closeIndex ?? lineEnd;
289
+ const args = tokensUntilLineEnd(cursor.tokens, cursor.index, entryEnd);
290
+ if (args.some((entry) => entry.kind === "lparen" || entry.kind === "rparen")) {
291
+ malformed(cursor, args[0] ?? token, `Nested parentheses are not supported in ${head.value}.`);
292
+ } else if (args.length > 0) {
293
+ directives.push({
294
+ name: head.value,
295
+ args: args.map((entry) => entry.value),
296
+ line: token.line,
297
+ block: true,
298
+ });
299
+ }
300
+ cursor.index = entryEnd;
301
+ }
302
+
303
+ function findClosingParen(tokens: readonly GoToken[], start: number, end: number): number | null {
304
+ for (let index = start; index < end; index++) {
305
+ if (tokens[index]?.kind === "rparen") return index;
306
+ }
307
+ return null;
308
+ }
309
+
310
+ function tokensUntilLineEnd(tokens: readonly GoToken[], start: number, end: number): GoToken[] {
311
+ return tokens.slice(start, end).filter((token) => token.kind !== "newline");
312
+ }
313
+
314
+ function nextLineEnd(tokens: readonly GoToken[], start: number): number {
315
+ let index = start;
316
+ while (index < tokens.length && tokens[index]?.kind !== "newline") index++;
317
+ return index;
318
+ }
319
+
320
+ function skipNewlines(cursor: Cursor): void {
321
+ while (cursor.tokens[cursor.index]?.kind === "newline") cursor.index++;
322
+ }
323
+
324
+ function skipLine(cursor: Cursor): void {
325
+ cursor.index = nextLineEnd(cursor.tokens, cursor.index);
326
+ }
327
+
328
+ function malformed(cursor: Cursor, token: GoToken, reason: string): void {
329
+ cursor.issues.push({ line: token.line, reason });
330
+ }
@@ -0,0 +1,154 @@
1
+ /** go.mod and go.work supported-directive readers. */
2
+
3
+ import type { GoDirective, GoParseIssue } from "./go-parser.ts";
4
+ import { parseGoFile } from "./go-parser.ts";
5
+ import type { DocumentContribution, ParsedDocument, WorkspaceConfig } from "./metadata-types.ts";
6
+ import { baseProject, issue } from "./metadata-utils.ts";
7
+ import type { DeclaredDependency, MetadataIssue, ProjectDeclaration } from "./types.ts";
8
+
9
+ const GO_MOD_KNOWN = new Set([
10
+ "module",
11
+ "go",
12
+ "toolchain",
13
+ "require",
14
+ "replace",
15
+ "exclude",
16
+ "retract",
17
+ "tool",
18
+ "godebug",
19
+ "ignore",
20
+ ]);
21
+ const GO_WORK_KNOWN = new Set(["go", "toolchain", "use", "replace", "godebug"]);
22
+
23
+ /** Read one go.mod or go.work document with grammar-aware directive parsing. */
24
+ export function readGoDocument(document: ParsedDocument): DocumentContribution {
25
+ if (document.kind !== "go-mod" && document.kind !== "go-work") {
26
+ return { project: null, workspace: null, issues: [] };
27
+ }
28
+ if (typeof document.data !== "string") {
29
+ return {
30
+ project: null,
31
+ workspace: null,
32
+ issues: [issue(document.path, null, "malformed", "Go metadata must be text.")],
33
+ };
34
+ }
35
+ const parsed = parseGoFile(document.data);
36
+ const issues = parsed.issues.map((entry) => parseIssue(document.path, entry));
37
+ if (document.kind === "go-work") return readGoWork(document, parsed.directives, issues);
38
+ return readGoModule(document, parsed.directives, issues);
39
+ }
40
+
41
+ function readGoModule(
42
+ document: ParsedDocument,
43
+ directives: readonly GoDirective[],
44
+ issues: MetadataIssue[],
45
+ ): DocumentContribution {
46
+ const module = directives.find((directive) => directive.name === "module");
47
+ if (module?.args.length !== 1) {
48
+ issues.push(
49
+ issue(
50
+ document.path,
51
+ "module",
52
+ "malformed",
53
+ module ? "module directive requires exactly one path." : "go.mod has no module directive.",
54
+ ),
55
+ );
56
+ }
57
+ const dependencies: DeclaredDependency[] = [];
58
+ for (const directive of directives) {
59
+ if (directive.name === "require") {
60
+ appendGoRequirement(document.path, directive, dependencies, issues);
61
+ continue;
62
+ }
63
+ if (directive.name !== "go" && directive.name !== "toolchain" && directive.name !== "module") {
64
+ discloseGoDirective(document.path, directive, issues, GO_MOD_KNOWN);
65
+ }
66
+ }
67
+ const name = module?.args.length === 1 ? (module.args[0] ?? null) : null;
68
+ const project: ProjectDeclaration | null =
69
+ name === null
70
+ ? null
71
+ : {
72
+ ...baseProject({
73
+ manifestPath: document.path,
74
+ kind: "go-module",
75
+ directory: document.directory,
76
+ name,
77
+ nameField: "module",
78
+ description: null,
79
+ descriptionField: null,
80
+ }),
81
+ dependencies,
82
+ };
83
+ return { project, workspace: null, issues };
84
+ }
85
+
86
+ function readGoWork(
87
+ document: ParsedDocument,
88
+ directives: readonly GoDirective[],
89
+ issues: MetadataIssue[],
90
+ ): DocumentContribution {
91
+ const uses: string[] = [];
92
+ for (const directive of directives) {
93
+ if (directive.name === "use") {
94
+ if (directive.args.length === 1) uses.push(directive.args[0] ?? "");
95
+ else {
96
+ issues.push(
97
+ issue(document.path, `use line ${directive.line}`, "malformed", "use requires one path."),
98
+ );
99
+ }
100
+ continue;
101
+ }
102
+ if (directive.name !== "go" && directive.name !== "toolchain") {
103
+ discloseGoDirective(document.path, directive, issues, GO_WORK_KNOWN);
104
+ }
105
+ }
106
+ const workspace: WorkspaceConfig = {
107
+ kind: "go-work",
108
+ manifestPath: document.path,
109
+ directory: document.directory,
110
+ field: "use",
111
+ patterns: uses,
112
+ excludeField: null,
113
+ excludes: [],
114
+ };
115
+ return { project: null, workspace, issues };
116
+ }
117
+
118
+ function appendGoRequirement(
119
+ path: string,
120
+ directive: GoDirective,
121
+ dependencies: DeclaredDependency[],
122
+ issues: MetadataIssue[],
123
+ ): void {
124
+ if (directive.args.length !== 2) {
125
+ issues.push(
126
+ issue(
127
+ path,
128
+ `require line ${directive.line}`,
129
+ "malformed",
130
+ "require needs a module path and version.",
131
+ ),
132
+ );
133
+ return;
134
+ }
135
+ const [name, value] = directive.args;
136
+ if (!name || !value) return;
137
+ dependencies.push({ field: "require", name, value });
138
+ }
139
+
140
+ function discloseGoDirective(
141
+ path: string,
142
+ directive: GoDirective,
143
+ issues: MetadataIssue[],
144
+ known: ReadonlySet<string>,
145
+ ): void {
146
+ const reason = known.has(directive.name)
147
+ ? `${directive.name} is parsed but not converted into a project fact.`
148
+ : `Unsupported Go directive: ${directive.name}.`;
149
+ issues.push(issue(path, directive.name, "unsupported", reason));
150
+ }
151
+
152
+ function parseIssue(path: string, issueValue: GoParseIssue): MetadataIssue {
153
+ return issue(path, `line ${issueValue.line}`, "malformed", issueValue.reason);
154
+ }
@@ -0,0 +1,28 @@
1
+ /** Shared workspace-observation interface for overview and Orientation collection. */
2
+
3
+ export { observeWorkspace } from "./observe.ts";
4
+ export type {
5
+ CollectionLimits,
6
+ DeclaredDependency,
7
+ DeclaredPath,
8
+ InventoryEntry,
9
+ InventoryEntryKind,
10
+ InventorySource,
11
+ InventoryStopReason,
12
+ MetadataDocument,
13
+ MetadataDocumentKind,
14
+ MetadataIssue,
15
+ MetadataIssueKind,
16
+ ObserveWorkspaceOptions,
17
+ ProjectDeclaration,
18
+ ProjectKind,
19
+ ProjectMembership,
20
+ ProjectRelationship,
21
+ WorkspaceCollection,
22
+ WorkspaceDeclaration,
23
+ WorkspaceInclusionPolicy,
24
+ WorkspaceInventory,
25
+ WorkspaceMetadataObservation,
26
+ WorkspaceObservation,
27
+ WorkspaceObservationError,
28
+ } from "./types.ts";