funkscript 0.2.6 → 0.2.7

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 (171) hide show
  1. package/README.md +1 -1
  2. package/dist/cli/build-watch.d.ts.map +1 -1
  3. package/dist/cli/build-watch.js +4 -24
  4. package/dist/cli/build-watch.js.map +1 -1
  5. package/dist/cli/build.d.ts.map +1 -1
  6. package/dist/cli/build.js +3 -58
  7. package/dist/cli/build.js.map +1 -1
  8. package/dist/cli/check-policy.d.ts +9 -0
  9. package/dist/cli/check-policy.d.ts.map +1 -0
  10. package/dist/cli/check-policy.js +39 -0
  11. package/dist/cli/check-policy.js.map +1 -0
  12. package/dist/cli/check.d.ts.map +1 -1
  13. package/dist/cli/check.js +16 -30
  14. package/dist/cli/check.js.map +1 -1
  15. package/dist/cli/commands.d.ts.map +1 -1
  16. package/dist/cli/commands.js +11 -0
  17. package/dist/cli/commands.js.map +1 -1
  18. package/dist/cli/doctor.d.ts.map +1 -1
  19. package/dist/cli/doctor.js +2 -6
  20. package/dist/cli/doctor.js.map +1 -1
  21. package/dist/cli/index.js +3 -0
  22. package/dist/cli/index.js.map +1 -1
  23. package/dist/cli/query.d.ts +3 -0
  24. package/dist/cli/query.d.ts.map +1 -0
  25. package/dist/cli/query.js +152 -0
  26. package/dist/cli/query.js.map +1 -0
  27. package/dist/cli/run.d.ts.map +1 -1
  28. package/dist/cli/run.js +2 -29
  29. package/dist/cli/run.js.map +1 -1
  30. package/dist/cli/tsc.d.ts.map +1 -1
  31. package/dist/cli/tsc.js +2 -6
  32. package/dist/cli/tsc.js.map +1 -1
  33. package/dist/compiler/check.d.ts.map +1 -1
  34. package/dist/compiler/check.js +18 -28
  35. package/dist/compiler/check.js.map +1 -1
  36. package/dist/compiler/diagnostics-json.d.ts +73 -0
  37. package/dist/compiler/diagnostics-json.d.ts.map +1 -0
  38. package/dist/compiler/diagnostics-json.js +957 -0
  39. package/dist/compiler/diagnostics-json.js.map +1 -0
  40. package/dist/compiler/escape-audit.d.ts +46 -0
  41. package/dist/compiler/escape-audit.d.ts.map +1 -0
  42. package/dist/compiler/escape-audit.js +682 -0
  43. package/dist/compiler/escape-audit.js.map +1 -0
  44. package/dist/compiler/modules/output-plan.d.ts.map +1 -1
  45. package/dist/compiler/modules/output-plan.js +10 -5
  46. package/dist/compiler/modules/output-plan.js.map +1 -1
  47. package/dist/compiler/modules/source-files.d.ts +2 -0
  48. package/dist/compiler/modules/source-files.d.ts.map +1 -1
  49. package/dist/compiler/modules/source-files.js +8 -0
  50. package/dist/compiler/modules/source-files.js.map +1 -1
  51. package/dist/compiler/numeric-comparisons.d.ts.map +1 -1
  52. package/dist/compiler/numeric-comparisons.js +13 -7
  53. package/dist/compiler/numeric-comparisons.js.map +1 -1
  54. package/dist/compiler/pipelines/resolution.d.ts.map +1 -1
  55. package/dist/compiler/pipelines/resolution.js +24 -23
  56. package/dist/compiler/pipelines/resolution.js.map +1 -1
  57. package/dist/compiler/query.d.ts +53 -0
  58. package/dist/compiler/query.d.ts.map +1 -0
  59. package/dist/compiler/query.js +781 -0
  60. package/dist/compiler/query.js.map +1 -0
  61. package/dist/compiler/source-map.d.ts.map +1 -1
  62. package/dist/compiler/source-map.js +2 -7
  63. package/dist/compiler/source-map.js.map +1 -1
  64. package/dist/compiler/type-view-paths.d.ts +2 -0
  65. package/dist/compiler/type-view-paths.d.ts.map +1 -1
  66. package/dist/compiler/type-view-paths.js +8 -0
  67. package/dist/compiler/type-view-paths.js.map +1 -1
  68. package/dist/compiler/typescript-context.d.ts.map +1 -1
  69. package/dist/compiler/typescript-context.js +5 -7
  70. package/dist/compiler/typescript-context.js.map +1 -1
  71. package/dist/emitter/assemblyscript.d.ts.map +1 -1
  72. package/dist/emitter/assemblyscript.js +120 -136
  73. package/dist/emitter/assemblyscript.js.map +1 -1
  74. package/dist/formatter/index.d.ts +9 -0
  75. package/dist/formatter/index.d.ts.map +1 -1
  76. package/dist/formatter/index.js +158 -2
  77. package/dist/formatter/index.js.map +1 -1
  78. package/dist/index.d.ts +3 -0
  79. package/dist/index.d.ts.map +1 -1
  80. package/dist/index.js +3 -0
  81. package/dist/index.js.map +1 -1
  82. package/dist/lsp/completion.d.ts +25 -0
  83. package/dist/lsp/completion.d.ts.map +1 -1
  84. package/dist/lsp/completion.js +1114 -51
  85. package/dist/lsp/completion.js.map +1 -1
  86. package/dist/lsp/definition.d.ts +2 -0
  87. package/dist/lsp/definition.d.ts.map +1 -1
  88. package/dist/lsp/definition.js +52 -58
  89. package/dist/lsp/definition.js.map +1 -1
  90. package/dist/lsp/hover.d.ts +5 -1
  91. package/dist/lsp/hover.d.ts.map +1 -1
  92. package/dist/lsp/hover.js +1000 -41
  93. package/dist/lsp/hover.js.map +1 -1
  94. package/dist/lsp/imports.d.ts +2 -0
  95. package/dist/lsp/imports.d.ts.map +1 -1
  96. package/dist/lsp/imports.js +35 -20
  97. package/dist/lsp/imports.js.map +1 -1
  98. package/dist/lsp/navigation.d.ts +1 -1
  99. package/dist/lsp/navigation.d.ts.map +1 -1
  100. package/dist/lsp/navigation.js +30 -12
  101. package/dist/lsp/navigation.js.map +1 -1
  102. package/dist/lsp/range-format.d.ts +4 -0
  103. package/dist/lsp/range-format.d.ts.map +1 -0
  104. package/dist/lsp/range-format.js +88 -0
  105. package/dist/lsp/range-format.js.map +1 -0
  106. package/dist/lsp/schema-import-hover.d.ts +8 -0
  107. package/dist/lsp/schema-import-hover.d.ts.map +1 -0
  108. package/dist/lsp/schema-import-hover.js +52 -0
  109. package/dist/lsp/schema-import-hover.js.map +1 -0
  110. package/dist/lsp/server.d.ts +1 -0
  111. package/dist/lsp/server.d.ts.map +1 -1
  112. package/dist/lsp/server.js +53 -48
  113. package/dist/lsp/server.js.map +1 -1
  114. package/dist/lsp/workspace.d.ts +3 -1
  115. package/dist/lsp/workspace.d.ts.map +1 -1
  116. package/dist/lsp/workspace.js +66 -10
  117. package/dist/lsp/workspace.js.map +1 -1
  118. package/dist/plugins/bun.d.ts +2 -18
  119. package/dist/plugins/bun.d.ts.map +1 -1
  120. package/dist/plugins/bun.js +5 -30
  121. package/dist/plugins/bun.js.map +1 -1
  122. package/dist/plugins/esbuild.d.ts +1 -1
  123. package/dist/plugins/esbuild.d.ts.map +1 -1
  124. package/dist/plugins/esbuild.js +10 -36
  125. package/dist/plugins/esbuild.js.map +1 -1
  126. package/dist/plugins/server-boundary.d.ts +14 -0
  127. package/dist/plugins/server-boundary.d.ts.map +1 -0
  128. package/dist/plugins/server-boundary.js +25 -0
  129. package/dist/plugins/server-boundary.js.map +1 -0
  130. package/dist/plugins/vite.d.ts +2 -18
  131. package/dist/plugins/vite.d.ts.map +1 -1
  132. package/dist/plugins/vite.js +4 -29
  133. package/dist/plugins/vite.js.map +1 -1
  134. package/package.json +1 -1
  135. package/src/cli/build-watch.ts +4 -26
  136. package/src/cli/build.ts +9 -60
  137. package/src/cli/check-policy.ts +54 -0
  138. package/src/cli/check.ts +19 -31
  139. package/src/cli/commands.ts +13 -0
  140. package/src/cli/doctor.ts +2 -5
  141. package/src/cli/index.ts +3 -0
  142. package/src/cli/query.ts +191 -0
  143. package/src/cli/run.ts +5 -30
  144. package/src/cli/tsc.ts +2 -5
  145. package/src/compiler/check.ts +28 -48
  146. package/src/compiler/diagnostics-json.ts +1205 -0
  147. package/src/compiler/escape-audit.ts +913 -0
  148. package/src/compiler/modules/output-plan.ts +11 -9
  149. package/src/compiler/modules/source-files.ts +9 -0
  150. package/src/compiler/numeric-comparisons.ts +14 -8
  151. package/src/compiler/pipelines/resolution.ts +30 -26
  152. package/src/compiler/query.ts +1177 -0
  153. package/src/compiler/source-map.ts +3 -7
  154. package/src/compiler/type-view-paths.ts +7 -0
  155. package/src/compiler/typescript-context.ts +6 -7
  156. package/src/emitter/assemblyscript.ts +101 -147
  157. package/src/formatter/index.ts +229 -2
  158. package/src/index.ts +3 -0
  159. package/src/lsp/completion.ts +1452 -66
  160. package/src/lsp/definition.ts +50 -58
  161. package/src/lsp/hover.ts +1442 -40
  162. package/src/lsp/imports.ts +55 -21
  163. package/src/lsp/navigation.ts +41 -22
  164. package/src/lsp/range-format.ts +102 -0
  165. package/src/lsp/schema-import-hover.ts +76 -0
  166. package/src/lsp/server.ts +69 -69
  167. package/src/lsp/workspace.ts +63 -9
  168. package/src/plugins/bun.ts +14 -66
  169. package/src/plugins/esbuild.ts +16 -55
  170. package/src/plugins/server-boundary.ts +41 -0
  171. package/src/plugins/vite.ts +12 -62
@@ -93,23 +93,57 @@ function formatImportSpecifier(spec: ImportSpecifier): string {
93
93
  return `${typePrefix}${binding}`;
94
94
  }
95
95
 
96
- export function findAutoImportCodeActions(
96
+ type ImportAnalysis = { tokens: Token[]; program: Program };
97
+
98
+ function analyzeImports(
97
99
  source: string,
98
100
  uri: string,
99
- range: Range,
100
- ): CodeAction[] {
101
- if (!source || !source.trim()) return [];
102
-
103
- let tokens: Token[];
104
- let program: Program;
101
+ ): ImportAnalysis | undefined {
102
+ if (!source.trim()) return undefined;
105
103
  try {
106
- tokens = new Lexer(source, uri).tokenize();
107
- program = new Parser(tokens, uri, source).parse();
104
+ const tokens = new Lexer(source, uri).tokenize();
105
+ const program = new Parser(tokens, uri, source).parse();
106
+ return { tokens, program };
108
107
  } catch (error) {
109
108
  if (!isExpectedSyntaxError(error)) throw error;
110
- return [];
109
+ return undefined;
111
110
  }
111
+ }
112
112
 
113
+ /** Compute both import actions from a single request-scoped parse. */
114
+ export function findImportCodeActions(
115
+ source: string,
116
+ uri: string,
117
+ range: Range,
118
+ ): CodeAction[] {
119
+ const analysis = analyzeImports(source, uri);
120
+ if (!analysis) return [];
121
+ const actions: CodeAction[] = [];
122
+ for (const find of [autoImportActions, removeUnusedImportActions]) {
123
+ try {
124
+ actions.push(...find(source, uri, range, analysis));
125
+ } catch (error) {
126
+ if (!isExpectedSyntaxError(error)) throw error;
127
+ }
128
+ }
129
+ return actions;
130
+ }
131
+
132
+ export function findAutoImportCodeActions(
133
+ source: string,
134
+ uri: string,
135
+ range: Range,
136
+ ): CodeAction[] {
137
+ const analysis = analyzeImports(source, uri);
138
+ return analysis ? autoImportActions(source, uri, range, analysis) : [];
139
+ }
140
+
141
+ function autoImportActions(
142
+ source: string,
143
+ uri: string,
144
+ range: Range,
145
+ { tokens, program }: ImportAnalysis,
146
+ ): CodeAction[] {
113
147
  // Find identifier token under cursor/range
114
148
  const targetToken = tokens.find((t) => {
115
149
  if (
@@ -265,18 +299,18 @@ export function findRemoveUnusedImportsCodeActions(
265
299
  uri: string,
266
300
  range: Range,
267
301
  ): CodeAction[] {
268
- if (!source || !source.trim()) return [];
269
-
270
- let tokens: Token[];
271
- let program: Program;
272
- try {
273
- tokens = new Lexer(source, uri).tokenize();
274
- program = new Parser(tokens, uri, source).parse();
275
- } catch (error) {
276
- if (!isExpectedSyntaxError(error)) throw error;
277
- return [];
278
- }
302
+ const analysis = analyzeImports(source, uri);
303
+ return analysis
304
+ ? removeUnusedImportActions(source, uri, range, analysis)
305
+ : [];
306
+ }
279
307
 
308
+ function removeUnusedImportActions(
309
+ source: string,
310
+ uri: string,
311
+ range: Range,
312
+ { tokens, program }: ImportAnalysis,
313
+ ): CodeAction[] {
280
314
  const importDecls: ImportDeclaration[] = [];
281
315
  for (const entry of program.body) {
282
316
  if (entry.kind === "ImportDeclaration") {
@@ -1,14 +1,16 @@
1
1
  import { dirname, resolve } from "node:path";
2
- import { fileURLToPath } from "node:url";
3
- import type {
4
- DocumentSymbol,
5
- Location,
6
- LocationLink,
7
- PrepareRenameResult,
8
- SymbolInformation,
9
- TextDocumentItem,
10
- TextEdit,
11
- WorkspaceEdit,
2
+ import { realpathSync } from "node:fs";
3
+ import { fileURLToPath, pathToFileURL } from "node:url";
4
+ import {
5
+ SymbolKind,
6
+ type DocumentSymbol,
7
+ type Location,
8
+ type LocationLink,
9
+ type PrepareRenameResult,
10
+ type SymbolInformation,
11
+ type TextDocumentItem,
12
+ type TextEdit,
13
+ type WorkspaceEdit,
12
14
  } from "vscode-languageserver/node";
13
15
  import type { Program } from "../ast/types.js";
14
16
  import { isExpectedSyntaxError, isExpectedUriError } from "../errors.js";
@@ -25,6 +27,16 @@ import {
25
27
  type Doc = Pick<TextDocumentItem, "uri" | "text">;
26
28
  type AnalyzedDocument = ParsedDocument;
27
29
 
30
+ /** Treat symlink aliases of a file as one document when resolving symbols. */
31
+ function documentKey(uri: string): string {
32
+ if (!uri.startsWith("file:")) return uri;
33
+ try {
34
+ return realpathSync(fileURLToPath(uri));
35
+ } catch {
36
+ return uri;
37
+ }
38
+ }
39
+
28
40
  function parse(source: string, uri: string): Program | null {
29
41
  try {
30
42
  return new Parser(new Lexer(source, uri).tokenize(), uri, source).parse();
@@ -47,7 +59,7 @@ class NavigationAnalysis {
47
59
  private readonly cache = new ParsedDocumentCache(),
48
60
  ) {
49
61
  this.documents = [
50
- ...docs.filter((doc) => doc.uri !== uri),
62
+ ...docs.filter((doc) => documentKey(doc.uri) !== documentKey(uri)),
51
63
  { uri, text: source },
52
64
  ];
53
65
  }
@@ -97,6 +109,8 @@ class NavigationAnalysis {
97
109
  if (!candidate.uri.startsWith("file:")) continue;
98
110
  try {
99
111
  available.set(fileURLToPath(candidate.uri), candidate);
112
+ const canonical = documentKey(candidate.uri);
113
+ available.set(canonical, candidate);
100
114
  } catch (error) {
101
115
  if (!isExpectedUriError(error)) throw error;
102
116
  }
@@ -104,10 +118,16 @@ class NavigationAnalysis {
104
118
  const funTarget = resolveFunModule(
105
119
  fileURLToPath(doc.uri),
106
120
  modulePath,
107
- (file) => available.has(file),
121
+ (file) =>
122
+ available.has(file) ||
123
+ available.has(documentKey(pathToFileURL(file).href)),
108
124
  { directories: true },
109
125
  );
110
- if (funTarget) return available.get(funTarget);
126
+ if (funTarget)
127
+ return (
128
+ available.get(funTarget) ??
129
+ available.get(documentKey(pathToFileURL(funTarget).href))
130
+ );
111
131
  if (/\.funx?$/.test(modulePath)) return undefined;
112
132
  const stem = base.endsWith(".js") ? base.slice(0, -3) : base;
113
133
  const candidates = new Set([
@@ -424,7 +444,10 @@ export function rename(
424
444
  const current = { uri, text: source };
425
445
  const key = analysis.renameIdentity(current, selected);
426
446
  if (!key) return null;
427
- const allDocs = [...docs.filter((doc) => doc.uri !== uri), current];
447
+ const allDocs = [
448
+ ...docs.filter((doc) => documentKey(doc.uri) !== documentKey(uri)),
449
+ current,
450
+ ];
428
451
  const locations: Location[] = [];
429
452
  for (const doc of allDocs) {
430
453
  const analyzed = analysis.document(doc);
@@ -484,14 +507,10 @@ export function documentSymbols(source: string, uri: string): DocumentSymbol[] {
484
507
  const decl = top.kind === "ExportDeclaration" ? top.declaration : top;
485
508
  if (!("id" in decl) || !("name" in decl.id)) continue;
486
509
  const id = decl.id;
487
- const kind =
488
- decl.kind === "SchemaDeclaration"
489
- ? 5
490
- : decl.kind === "ServiceDeclaration"
491
- ? 11
492
- : decl.kind === "ErrorDeclaration"
493
- ? 5
494
- : 12;
510
+ let kind: DocumentSymbol["kind"] = SymbolKind.Function;
511
+ if (decl.kind === "SchemaDeclaration" || decl.kind === "ErrorDeclaration")
512
+ kind = SymbolKind.Class;
513
+ else if (decl.kind === "ServiceDeclaration") kind = SymbolKind.Interface;
495
514
  symbols.push({
496
515
  name: id.name,
497
516
  kind,
@@ -0,0 +1,102 @@
1
+ import { diffChars } from "diff";
2
+ import type { Range, TextEdit } from "vscode-languageserver/node";
3
+ import { extractComments } from "../formatter/comments.js";
4
+ import { validateSyntax } from "./validation.js";
5
+
6
+ /** Limit full-document formatter changes to the selection, rejecting semantic changes. */
7
+ export function formatSelection(
8
+ source: string,
9
+ formatted: string,
10
+ uri: string,
11
+ range: Range,
12
+ ): TextEdit[] | null {
13
+ const lines = source.split("\n");
14
+ const offset = (position: Range["start"]): number | null => {
15
+ if (
16
+ !Number.isInteger(position.line) ||
17
+ !Number.isInteger(position.character) ||
18
+ position.line < 0 ||
19
+ position.line >= lines.length ||
20
+ position.character < 0 ||
21
+ position.character > lines[position.line].length
22
+ )
23
+ return null;
24
+ let result = position.character;
25
+ for (let i = 0; i < position.line; i++) result += lines[i].length + 1;
26
+ return result;
27
+ };
28
+ if (!range?.start || !range?.end) return null;
29
+ const start = offset(range.start);
30
+ const end = offset(range.end);
31
+ if (start === null || end === null || start > end) return null;
32
+ if (start === end) return [];
33
+
34
+ const edits: Array<{ start: number; end: number; text: string }> = [];
35
+ let originalOffset = 0;
36
+ let pending: { start: number; end: number; text: string } | undefined;
37
+ const flush = (): void => {
38
+ if (pending) edits.push(pending);
39
+ pending = undefined;
40
+ };
41
+ const changes = diffChars(source, formatted, { timeout: 150 });
42
+ if (!changes) return null;
43
+ for (const change of changes) {
44
+ if (!change.added && !change.removed) {
45
+ flush();
46
+ originalOffset += change.value.length;
47
+ continue;
48
+ }
49
+ pending ??= { start: originalOffset, end: originalOffset, text: "" };
50
+ if (change.removed) {
51
+ originalOffset += change.value.length;
52
+ pending.end = originalOffset;
53
+ } else {
54
+ pending.text += change.value;
55
+ }
56
+ }
57
+ flush();
58
+ const selected = edits.filter(
59
+ (edit) => edit.start >= start && edit.end <= end && edit.start < end,
60
+ );
61
+ if (!selected.length) return [];
62
+ if (
63
+ selected.some(
64
+ (edit) =>
65
+ /\S/.test(source.slice(edit.start, edit.end)) || /\S/.test(edit.text),
66
+ )
67
+ )
68
+ return null;
69
+ let replacement = "";
70
+ let cursor = start;
71
+ for (const edit of selected) {
72
+ replacement += source.slice(cursor, edit.start) + edit.text;
73
+ cursor = edit.end;
74
+ }
75
+ replacement += source.slice(cursor, end);
76
+ const candidate = source.slice(0, start) + replacement + source.slice(end);
77
+ const before = validateSyntax(source, uri);
78
+ const after = validateSyntax(candidate, uri);
79
+ if (
80
+ before.diagnostics.length ||
81
+ after.diagnostics.length ||
82
+ !before.ast ||
83
+ !after.ast
84
+ )
85
+ return null;
86
+ const comments = (text: string) =>
87
+ extractComments(text).map(({ text, line, isTrailing }) => ({
88
+ text,
89
+ line,
90
+ isTrailing,
91
+ }));
92
+ if (JSON.stringify(comments(source)) !== JSON.stringify(comments(candidate)))
93
+ return null;
94
+ const withoutSpans = (_key: string, value: unknown): unknown =>
95
+ _key === "span" ? undefined : value;
96
+ if (
97
+ JSON.stringify(before.ast, withoutSpans) !==
98
+ JSON.stringify(after.ast, withoutSpans)
99
+ )
100
+ return null;
101
+ return [{ range, newText: replacement }];
102
+ }
@@ -0,0 +1,76 @@
1
+ import type {
2
+ Hover,
3
+ Position,
4
+ TextDocumentItem,
5
+ } from "vscode-languageserver/node";
6
+ import { Lexer } from "../lexer/lexer.js";
7
+ import { Parser } from "../parser/parser.js";
8
+ import { isExpectedSyntaxError } from "../errors.js";
9
+ import { findWorkspaceDefinition } from "./navigation.js";
10
+ import { findHoverAsync } from "./hover.js";
11
+
12
+ type Doc = Pick<TextDocumentItem, "uri" | "text">;
13
+
14
+ /** Avoid scanning the workspace unless the selected name could be a local import. */
15
+ export function hasRelativeImportedName(
16
+ source: string,
17
+ uri: string,
18
+ name: string,
19
+ ): boolean {
20
+ try {
21
+ const ast = new Parser(
22
+ new Lexer(source, uri).tokenize(),
23
+ uri,
24
+ source,
25
+ ).parse();
26
+ return ast.body.some(
27
+ (entry) =>
28
+ entry.kind === "ImportDeclaration" &&
29
+ entry.source.parts[0]?.kind === "StringRawPart" &&
30
+ entry.source.parts[0].value.startsWith(".") &&
31
+ entry.specifiers.some((spec) => spec.local.name === name),
32
+ );
33
+ } catch (error) {
34
+ if (isExpectedSyntaxError(error)) return false;
35
+ throw error;
36
+ }
37
+ }
38
+
39
+ /** Resolve only imported schema symbols, leaving ordinary hover and shadowing intact. */
40
+ export async function importedFunSchemaHover(
41
+ source: string,
42
+ uri: string,
43
+ position: Position,
44
+ docs: Doc[],
45
+ ): Promise<Hover | null> {
46
+ const links = findWorkspaceDefinition(source, uri, position, docs, true);
47
+ if (!Array.isArray(links) || links.length !== 1) return null;
48
+ const link = links[0];
49
+ if (link.targetUri === uri) return null;
50
+ const target = docs.find((doc) => doc.uri === link.targetUri);
51
+ if (!target) return null;
52
+
53
+ try {
54
+ const ast = new Parser(
55
+ new Lexer(target.text, target.uri).tokenize(),
56
+ target.uri,
57
+ target.text,
58
+ ).parse();
59
+ const selected = link.targetSelectionRange.start;
60
+ const schema = ast.body.some((entry) => {
61
+ const decl =
62
+ entry.kind === "ExportDeclaration" ? entry.declaration : entry;
63
+ return (
64
+ decl.kind === "SchemaDeclaration" &&
65
+ decl.id.span.start.line - 1 === selected.line &&
66
+ decl.id.span.start.column - 1 === selected.character
67
+ );
68
+ });
69
+ if (!schema) return null;
70
+ const hover = await findHoverAsync(target.text, target.uri, selected);
71
+ return hover ? { ...hover, range: link.originSelectionRange } : null;
72
+ } catch (error) {
73
+ if (isExpectedSyntaxError(error)) return null;
74
+ throw error;
75
+ }
76
+ }
package/src/lsp/server.ts CHANGED
@@ -72,10 +72,12 @@ import { convertPipelineStyle } from "../tooling/pipeline-style.js";
72
72
  import { FUNSCRIPT_VERSION } from "../version.js";
73
73
  import { complete } from "./completion.js";
74
74
  import { findHoverAsync } from "./hover.js";
75
+ import { formatSelection } from "./range-format.js";
75
76
  import {
76
- findAutoImportCodeActions,
77
- findRemoveUnusedImportsCodeActions,
78
- } from "./imports.js";
77
+ hasRelativeImportedName,
78
+ importedFunSchemaHover,
79
+ } from "./schema-import-hover.js";
80
+ import { findImportCodeActions } from "./imports.js";
79
81
  import { findInlayHints } from "./inlay-hints.js";
80
82
  import { findSignatureHelp } from "./signature-help.js";
81
83
  import { InteropRenameSession, renameInterop } from "./interop-rename.js";
@@ -270,7 +272,6 @@ export class FunScriptLspServer extends EventEmitter {
270
272
  capabilities: {
271
273
  textDocumentSync: TextDocumentSyncKind.Full,
272
274
  documentFormattingProvider: true,
273
- documentRangeFormattingProvider: true,
274
275
  definitionProvider: true,
275
276
  documentSymbolProvider: true,
276
277
  workspaceSymbolProvider: true,
@@ -313,20 +314,7 @@ export class FunScriptLspServer extends EventEmitter {
313
314
  if (params?.textDocument) {
314
315
  this.documentRevision++;
315
316
  this.cancelCodeActionsFor(params.textDocument.uri);
316
- this.documents.set(params.textDocument.uri, params.textDocument);
317
- this.syncProjectConfigWatchers();
318
- const diagnostics = validateSyntax(
319
- params.textDocument.text,
320
- params.textDocument.uri,
321
- ).diagnostics;
322
- if (diagnostics.length) this.syntaxErrors.add(params.textDocument.uri);
323
- else this.syntaxErrors.delete(params.textDocument.uri);
324
- this.publishDiagnostics(
325
- params.textDocument.uri,
326
- params.textDocument.version,
327
- diagnostics,
328
- );
329
- this.scheduleTypeDiagnostics();
317
+ this.updateDocument(params.textDocument);
330
318
  }
331
319
  }
332
320
 
@@ -346,23 +334,20 @@ export class FunScriptLspServer extends EventEmitter {
346
334
  version: updatedVersion,
347
335
  text: newText,
348
336
  };
349
- this.documents.set(params.textDocument.uri, updatedDoc);
350
- this.syncProjectConfigWatchers();
351
- const diagnostics = validateSyntax(
352
- newText,
353
- params.textDocument.uri,
354
- ).diagnostics;
355
- if (diagnostics.length) this.syntaxErrors.add(updatedDoc.uri);
356
- else this.syntaxErrors.delete(updatedDoc.uri);
357
- this.publishDiagnostics(
358
- params.textDocument.uri,
359
- updatedVersion,
360
- diagnostics,
361
- );
362
- this.scheduleTypeDiagnostics();
337
+ this.updateDocument(updatedDoc);
363
338
  }
364
339
  }
365
340
 
341
+ private updateDocument(document: TextDocumentItem): void {
342
+ this.documents.set(document.uri, document);
343
+ this.syncProjectConfigWatchers();
344
+ const diagnostics = validateSyntax(document.text, document.uri).diagnostics;
345
+ if (diagnostics.length) this.syntaxErrors.add(document.uri);
346
+ else this.syntaxErrors.delete(document.uri);
347
+ this.publishDiagnostics(document.uri, document.version, diagnostics);
348
+ this.scheduleTypeDiagnostics();
349
+ }
350
+
366
351
  public onDidCloseHandler(params: DidCloseTextDocumentParams): void {
367
352
  if (params?.textDocument) {
368
353
  this.documentRevision++;
@@ -395,7 +380,7 @@ export class FunScriptLspServer extends EventEmitter {
395
380
  params.options.tabSize > 0
396
381
  ? params.options.tabSize
397
382
  : undefined;
398
- const filename = uri ? new URL(uri).pathname : undefined;
383
+ const filename = uri.startsWith("file:") ? fileURLToPath(uri) : undefined;
399
384
  const projectFormat = filename
400
385
  ? loadFormatConfigForFile(filename)
401
386
  : undefined;
@@ -549,26 +534,9 @@ export class FunScriptLspServer extends EventEmitter {
549
534
  }
550
535
 
551
536
  try {
552
- const autoImportActions = findAutoImportCodeActions(
553
- doc.text,
554
- uri,
555
- params.range,
556
- );
557
- actions.push(...autoImportActions);
537
+ actions.push(...findImportCodeActions(doc.text, uri, params.range));
558
538
  } catch (error) {
559
- /* Auto import gracefully declines. */
560
- if (!isExpectedSyntaxError(error)) throw error;
561
- }
562
-
563
- try {
564
- const unusedImportActions = findRemoveUnusedImportsCodeActions(
565
- doc.text,
566
- uri,
567
- params.range,
568
- );
569
- actions.push(...unusedImportActions);
570
- } catch (error) {
571
- /* Remove unused imports gracefully declines. */
539
+ /* Import actions gracefully decline on invalid syntax. */
572
540
  if (!isExpectedSyntaxError(error)) throw error;
573
541
  }
574
542
 
@@ -647,26 +615,25 @@ export class FunScriptLspServer extends EventEmitter {
647
615
  const doc = uri ? this.documents.get(uri) : undefined;
648
616
  if (!doc) return null;
649
617
 
650
- // The formatter currently operates on complete syntax trees. Only accept a
651
- // range that covers the full document; partial edits could alter unrelated code.
652
618
  const lines = doc.text.split("\n");
653
619
  const lastLine = lines.length - 1;
654
620
  const end = params.range?.end;
655
621
  const start = params.range?.start;
656
- if (
657
- !start ||
658
- !end ||
659
- start.line !== 0 ||
660
- start.character !== 0 ||
661
- end.line < lastLine ||
662
- (end.line === lastLine && end.character < lines[lastLine].length)
663
- ) {
664
- return null;
665
- }
666
- return this.onFormattingHandler({
622
+ if (!start || !end) return null;
623
+ const formatting = this.onFormattingHandler({
667
624
  textDocument: params.textDocument,
668
625
  options: params.options,
669
626
  });
627
+ if (!formatting) return null;
628
+ if (!formatting.length) return [];
629
+ if (
630
+ start.line === 0 &&
631
+ start.character === 0 &&
632
+ (end.line > lastLine ||
633
+ (end.line === lastLine && end.character >= lines[lastLine].length))
634
+ )
635
+ return formatting;
636
+ return formatSelection(doc.text, formatting[0].newText, uri, params.range);
670
637
  }
671
638
 
672
639
  public onDefinitionHandler(
@@ -686,7 +653,7 @@ export class FunScriptLspServer extends EventEmitter {
686
653
  doc.text,
687
654
  uri,
688
655
  params.position,
689
- this.workspaceSnapshot()?.fun ?? this.getDocuments(),
656
+ this.workspaceSnapshot(true)?.fun ?? this.getDocuments(),
690
657
  clientSupportsLink,
691
658
  );
692
659
  }
@@ -698,6 +665,35 @@ export class FunScriptLspServer extends EventEmitter {
698
665
  return null;
699
666
  }
700
667
 
668
+ const line = doc.text.split("\n")[params.position.line] ?? "";
669
+ const before = line.slice(0, params.position.character);
670
+ const after = line.slice(params.position.character);
671
+ const word =
672
+ (before.match(/[A-Za-z0-9_]*$/)?.[0] ?? "") +
673
+ (after.match(/^[A-Za-z0-9_]*/)?.[0] ?? "");
674
+ if (
675
+ /^[A-Z]/.test(word) &&
676
+ /\bimport\b/.test(doc.text) &&
677
+ hasRelativeImportedName(doc.text, uri, word)
678
+ ) {
679
+ let imported = await importedFunSchemaHover(
680
+ doc.text,
681
+ uri,
682
+ params.position,
683
+ this.getDocuments(),
684
+ );
685
+ if (!imported) {
686
+ const snapshot = this.workspaceSnapshot(true);
687
+ if (snapshot)
688
+ imported = await importedFunSchemaHover(
689
+ doc.text,
690
+ uri,
691
+ params.position,
692
+ snapshot.fun,
693
+ );
694
+ }
695
+ if (imported) return imported;
696
+ }
701
697
  return findHoverAsync(doc.text, uri, params.position);
702
698
  }
703
699
 
@@ -746,7 +742,10 @@ export class FunScriptLspServer extends EventEmitter {
746
742
  public onWorkspaceSymbolHandler(
747
743
  params: WorkspaceSymbolParams,
748
744
  ): SymbolInformation[] {
749
- return workspaceSymbols(this.getDocuments(), params?.query ?? "");
745
+ return workspaceSymbols(
746
+ this.workspaceSnapshot(true)?.fun ?? this.getDocuments(),
747
+ params?.query ?? "",
748
+ );
750
749
  }
751
750
 
752
751
  public onReferencesHandler(params: ReferenceParams): Location[] {
@@ -757,7 +756,7 @@ export class FunScriptLspServer extends EventEmitter {
757
756
  doc.text,
758
757
  uri,
759
758
  params.position,
760
- this.workspaceSnapshot()?.fun ?? this.getDocuments(),
759
+ this.workspaceSnapshot(true)?.fun ?? this.getDocuments(),
761
760
  params.context.includeDeclaration,
762
761
  )
763
762
  : [];
@@ -1013,11 +1012,12 @@ export class FunScriptLspServer extends EventEmitter {
1013
1012
  });
1014
1013
  }
1015
1014
 
1016
- private workspaceSnapshot() {
1015
+ private workspaceSnapshot(allowIncomplete = false) {
1017
1016
  return workspaceSnapshot(
1018
1017
  this.workspaceRoots,
1019
1018
  this.documents.values(),
1020
1019
  this.interopDocuments.values(),
1020
+ { allowIncomplete },
1021
1021
  );
1022
1022
  }
1023
1023