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
package/dist/lsp/hover.js CHANGED
@@ -1,22 +1,905 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { dirname, resolve } from "node:path";
3
+ import { fileURLToPath, pathToFileURL } from "node:url";
1
4
  import * as Effect from "effect/Effect";
2
- import { fileURLToPath } from "node:url";
3
5
  import ts from "typescript";
4
6
  import { MarkupKind, } from "vscode-languageserver/node";
5
7
  import { compileString } from "../compiler.js";
6
- import { resolveImportedSchemas } from "../compiler/schemas/imported-schemas.js";
8
+ import { pipelineCompanionCandidates } from "../compiler/pipelines/catalog.js";
7
9
  import { inferPipelineCompanions } from "../compiler/pipelines/companions.js";
10
+ import { resolveImportedSchemas } from "../compiler/schemas/imported-schemas.js";
8
11
  import { TypeScriptCompilationContext } from "../compiler/typescript-context.js";
9
12
  import { Emitter } from "../emitter/emitter.js";
10
- import { pipelineCompanionCandidates } from "../compiler/pipelines/catalog.js";
13
+ import { FunScriptError, isExpectedSyntaxError, isExpectedUriError, isFileNotFoundError, } from "../errors.js";
11
14
  import { Lexer } from "../lexer/lexer.js";
12
15
  import { BANNED_KEYWORDS, BANNED_SYMBOLS, TokenType, } from "../lexer/tokens.js";
13
16
  import { Parser } from "../parser/parser.js";
14
- import { spanToRange } from "./definition.js";
17
+ import { detectJsxContext } from "./completion.js";
18
+ import { DefinitionFinder, findDefinition, isPositionInSpan, spanToRange, } from "./definition.js";
15
19
  import { getHoverDocumentation } from "./docs.js";
16
- import { FunScriptError } from "../errors.js";
17
20
  function isExpectedHoverError(error) {
18
21
  return error instanceof FunScriptError || error instanceof URIError;
19
22
  }
23
+ function isWordChar(ch) {
24
+ return /[A-Za-z0-9_]/.test(ch);
25
+ }
26
+ function tryParseProgram(source, uri, existingTokens) {
27
+ try {
28
+ const tokens = existingTokens ?? new Lexer(source, uri).tokenize();
29
+ return new Parser(tokens, uri, source).parse();
30
+ }
31
+ catch (error) {
32
+ if (!isExpectedSyntaxError(error))
33
+ throw error;
34
+ return null;
35
+ }
36
+ }
37
+ function formatBindingTarget(target) {
38
+ if (target.kind === "Identifier" || target.kind === "UpperIdentifier") {
39
+ return target.name;
40
+ }
41
+ if (target.kind === "RecordPattern") {
42
+ return `{ ${target.fields
43
+ .map((f) => f.pattern
44
+ ? `${f.key.name}: ${formatBindingTarget(f.pattern)}`
45
+ : f.key.name)
46
+ .join(", ")} }`;
47
+ }
48
+ return `[${target.elements.map((e) => ("name" in e && typeof e.name === "object" && e.name && "name" in e.name ? String(e.name.name) : "_")).join(", ")}]`;
49
+ }
50
+ function formatTypeNode(typeNode) {
51
+ switch (typeNode.kind) {
52
+ case "TypeReferenceNode": {
53
+ const args = typeNode.typeArgs && typeNode.typeArgs.length > 0
54
+ ? `<${typeNode.typeArgs.map(formatTypeNode).join(", ")}>`
55
+ : "";
56
+ return `${typeNode.name.name}${args}`;
57
+ }
58
+ case "QualifiedTypeReferenceNode": {
59
+ const args = typeNode.typeArgs && typeNode.typeArgs.length > 0
60
+ ? `<${typeNode.typeArgs.map(formatTypeNode).join(", ")}>`
61
+ : "";
62
+ return `${typeNode.parts.map((p) => p.name).join(".")}${args}`;
63
+ }
64
+ case "FunctionTypeNode": {
65
+ const tp = typeNode.typeParams && typeNode.typeParams.length > 0
66
+ ? `<${typeNode.typeParams.map((p) => p.name.name).join(", ")}>`
67
+ : "";
68
+ const params = typeNode.params
69
+ .map((p) => p.typeAnnotation
70
+ ? `${formatBindingTarget(p.name)}: ${formatTypeNode(p.typeAnnotation)}`
71
+ : formatBindingTarget(p.name))
72
+ .join(", ");
73
+ return `${tp}(${params}) => ${formatTypeNode(typeNode.returnType)}`;
74
+ }
75
+ case "EffectTypeNode": {
76
+ const parts = [formatTypeNode(typeNode.success)];
77
+ if (typeNode.error)
78
+ parts.push(formatTypeNode(typeNode.error));
79
+ if (typeNode.requirements) {
80
+ if (!typeNode.error)
81
+ parts.push("never");
82
+ parts.push(formatTypeNode(typeNode.requirements));
83
+ }
84
+ return `Effect<${parts.join(", ")}>`;
85
+ }
86
+ case "RecordTypeNode": {
87
+ if (typeNode.fields.length === 0)
88
+ return "{}";
89
+ const fields = typeNode.fields
90
+ .map((f) => `${f.name.name}${f.optional ? "?" : ""}: ${formatTypeNode(f.typeAnnotation)}`)
91
+ .join(", ");
92
+ return `{ ${fields} }`;
93
+ }
94
+ case "ArrayTypeNode":
95
+ return `Array<${formatTypeNode(typeNode.elementType)}>`;
96
+ case "UnionTypeNode":
97
+ return typeNode.types.map(formatTypeNode).join(" | ");
98
+ case "IntersectionTypeNode":
99
+ return typeNode.types.map(formatTypeNode).join(" & ");
100
+ case "LiteralTypeNode":
101
+ return typeof typeNode.value === "string"
102
+ ? JSON.stringify(typeNode.value)
103
+ : String(typeNode.value);
104
+ case "ParenthesizedTypeNode":
105
+ return `(${formatTypeNode(typeNode.typeNode)})`;
106
+ case "TypeQueryNode":
107
+ return "typeof ...";
108
+ case "IndexedAccessTypeNode":
109
+ return `${formatTypeNode(typeNode.objectType)}[${formatTypeNode(typeNode.indexType)}]`;
110
+ case "TsEscapeTypeNode":
111
+ return `ts { ${typeNode.code.trim()} }`;
112
+ }
113
+ }
114
+ function inferQuickExprType(expr) {
115
+ switch (expr.kind) {
116
+ case "NumberLiteral":
117
+ return "number";
118
+ case "StringLiteral":
119
+ return "string";
120
+ case "BooleanLiteral":
121
+ return "boolean";
122
+ case "ArrayExpression":
123
+ return "Array<unknown>";
124
+ case "RecordExpression":
125
+ return "{ ... }";
126
+ case "EffectBlockExpression":
127
+ return "Effect<...>";
128
+ case "JSXElement":
129
+ case "JSXSelfClosingElement":
130
+ return "JSX.Element";
131
+ case "FunctionExpression":
132
+ return formatFunctionSignature("fn", expr).replace(/^fn = /, "");
133
+ default:
134
+ return "unknown";
135
+ }
136
+ }
137
+ function formatFunctionSignature(name, fn, explicitType) {
138
+ if (explicitType) {
139
+ return `${name}: ${formatTypeNode(explicitType)}`;
140
+ }
141
+ const tp = fn.typeParams && fn.typeParams.length > 0
142
+ ? `<${fn.typeParams.map((p) => p.name.name).join(", ")}>`
143
+ : "";
144
+ const params = fn.params
145
+ .map((p) => p.typeAnnotation
146
+ ? `${formatBindingTarget(p.name)}: ${formatTypeNode(p.typeAnnotation)}`
147
+ : formatBindingTarget(p.name))
148
+ .join(", ");
149
+ const ret = fn.returnType
150
+ ? formatTypeNode(fn.returnType)
151
+ : fn.body.kind === "BlockStatement"
152
+ ? "unknown"
153
+ : inferQuickExprType(fn.body);
154
+ return `${name} = ${tp}(${params}) => ${ret}`;
155
+ }
156
+ function extractLeadingDocComment(source, oneBasedLine) {
157
+ const lines = source.split(/\r?\n/);
158
+ const commentLines = [];
159
+ let idx = oneBasedLine - 2;
160
+ while (idx >= 0) {
161
+ const trimmed = lines[idx].trim();
162
+ if (trimmed.startsWith("//")) {
163
+ commentLines.push(trimmed.replace(/^\/\/\/?\s?/, ""));
164
+ idx--;
165
+ continue;
166
+ }
167
+ break;
168
+ }
169
+ if (commentLines.length === 0)
170
+ return null;
171
+ return commentLines.reverse().join("\n").trim() || null;
172
+ }
173
+ function formatMarkdownHover(signature, kindLabel, name, docComment, originInfo) {
174
+ const parts = [`\`\`\`funscript\n${signature}\n\`\`\``, "***"];
175
+ const header = originInfo
176
+ ? `**${kindLabel}** \`${name}\` — ${originInfo}`
177
+ : `**${kindLabel}** \`${name}\``;
178
+ parts.push(header);
179
+ if (docComment) {
180
+ parts.push(docComment);
181
+ }
182
+ return parts.join("\n\n");
183
+ }
184
+ function resolveRelativeModule(fromUri, modulePath, documents) {
185
+ if (!modulePath.startsWith("."))
186
+ return null;
187
+ let base;
188
+ try {
189
+ base = resolve(dirname(fileURLToPath(fromUri)), modulePath);
190
+ }
191
+ catch (error) {
192
+ if (!isExpectedUriError(error))
193
+ throw error;
194
+ return null;
195
+ }
196
+ const candidates = [
197
+ base,
198
+ `${base}.fun`,
199
+ `${base}.funs`,
200
+ `${base}.funx`,
201
+ `${base}.ts`,
202
+ `${base}.tsx`,
203
+ resolve(base, "index.fun"),
204
+ resolve(base, "index.funs"),
205
+ resolve(base, "index.funx"),
206
+ ];
207
+ if (documents) {
208
+ for (const doc of documents) {
209
+ try {
210
+ if (candidates.includes(fileURLToPath(doc.uri))) {
211
+ return doc;
212
+ }
213
+ }
214
+ catch (error) {
215
+ if (!isExpectedUriError(error))
216
+ throw error;
217
+ }
218
+ }
219
+ }
220
+ for (const candidate of candidates) {
221
+ try {
222
+ if (existsSync(candidate)) {
223
+ return {
224
+ uri: pathToFileURL(candidate).href,
225
+ text: readFileSync(candidate, "utf8"),
226
+ };
227
+ }
228
+ }
229
+ catch (error) {
230
+ if (!isFileNotFoundError(error) && !isExpectedUriError(error))
231
+ throw error;
232
+ }
233
+ }
234
+ return null;
235
+ }
236
+ function findExportedDeclaration(program, name) {
237
+ for (const entry of program.body) {
238
+ if (entry.kind !== "ExportDeclaration")
239
+ continue;
240
+ if ("id" in entry.declaration &&
241
+ "name" in entry.declaration.id &&
242
+ entry.declaration.id.name === name) {
243
+ return { outer: entry, decl: entry.declaration };
244
+ }
245
+ }
246
+ return null;
247
+ }
248
+ function findImportedDeclaration(program, uri, localName, documents) {
249
+ for (const item of program.body) {
250
+ if (item.kind !== "ImportDeclaration")
251
+ continue;
252
+ const spec = item.specifiers.find((s) => (s.local ?? s.imported).name === localName);
253
+ if (!spec)
254
+ continue;
255
+ const modulePath = item.source.parts
256
+ .map((part) => (part.kind === "StringRawPart" ? part.value : ""))
257
+ .join("");
258
+ const targetDoc = resolveRelativeModule(uri, modulePath, documents);
259
+ if (!targetDoc)
260
+ continue;
261
+ const targetAst = tryParseProgram(targetDoc.text, targetDoc.uri);
262
+ if (!targetAst)
263
+ continue;
264
+ const found = findExportedDeclaration(targetAst, spec.imported.name);
265
+ if (found) {
266
+ return {
267
+ doc: targetDoc,
268
+ modulePath,
269
+ outer: found.outer,
270
+ decl: found.decl,
271
+ };
272
+ }
273
+ }
274
+ return null;
275
+ }
276
+ function formatSchemaSignature(decl, isExported) {
277
+ const prefix = isExported ? "export " : "";
278
+ const tp = decl.typeParams && decl.typeParams.length > 0
279
+ ? `<${decl.typeParams.map((p) => p.name.name).join(", ")}>`
280
+ : "";
281
+ const header = `${prefix}schema ${decl.id.name}${tp}`;
282
+ switch (decl.body.kind) {
283
+ case "RecordSchema": {
284
+ if (decl.body.fields.length === 0)
285
+ return `${header} = {}`;
286
+ const fields = decl.body.fields
287
+ .map((f) => ` ${f.name.name}: ${formatTypeNode(f.typeAnnotation)}`)
288
+ .join("\n");
289
+ return `${header} = {\n${fields}\n}`;
290
+ }
291
+ case "TaggedUnionSchema": {
292
+ const variants = decl.body.variants
293
+ .map((v) => {
294
+ if (v.fields.length === 0)
295
+ return ` | ${v.name.name}`;
296
+ const fields = v.fields
297
+ .map((f) => `${f.name.name}: ${formatTypeNode(f.typeAnnotation)}`)
298
+ .join(", ");
299
+ return ` | ${v.name.name}(${fields})`;
300
+ })
301
+ .join("\n");
302
+ return `${header} =\n${variants}`;
303
+ }
304
+ case "StructuralUnionSchema":
305
+ return `${header} = ${decl.body.members.map(formatTypeNode).join(" | ")}`;
306
+ case "RefinementSchema":
307
+ return `${header} = ${formatTypeNode(decl.body.baseType)} |> ...`;
308
+ case "TypeSignatureSchema":
309
+ return `${header} = ${formatTypeNode(decl.body.signature)}`;
310
+ case "TsDerivedSchema":
311
+ return `${header} = ts ${formatTypeNode(decl.body.targetType)}`;
312
+ case "ImportedTsSchema":
313
+ return `${header} = from ts<${formatTypeNode(decl.body.typeNode)}>`;
314
+ }
315
+ }
316
+ function formatErrorSignature(decl, isExported) {
317
+ const prefix = isExported ? "export " : "";
318
+ if (decl.fields.length === 0) {
319
+ return `${prefix}error ${decl.id.name}`;
320
+ }
321
+ const fields = decl.fields
322
+ .map((f) => `${f.name.name}: ${formatTypeNode(f.typeAnnotation)}`)
323
+ .join(", ");
324
+ return `${prefix}error ${decl.id.name}(${fields})`;
325
+ }
326
+ function formatServiceSignature(decl, isExported) {
327
+ const prefix = isExported ? "export " : "";
328
+ if (decl.members.length === 0) {
329
+ return `${prefix}service ${decl.id.name}`;
330
+ }
331
+ const members = decl.members
332
+ .map((m) => ` ${m.name.name}: ${formatTypeNode(m.signature)}`)
333
+ .join("\n");
334
+ return `${prefix}service ${decl.id.name}\n${members}`;
335
+ }
336
+ function formatTopLevelDeclarationHover(decl, outer, source, originInfo) {
337
+ const isExported = outer.kind === "ExportDeclaration";
338
+ const docComment = extractLeadingDocComment(source, outer.span.start.line);
339
+ switch (decl.kind) {
340
+ case "SchemaDeclaration":
341
+ return formatMarkdownHover(formatSchemaSignature(decl, isExported), "schema", decl.id.name, docComment, originInfo);
342
+ case "ErrorDeclaration":
343
+ return formatMarkdownHover(formatErrorSignature(decl, isExported), "error", decl.id.name, docComment, originInfo);
344
+ case "ServiceDeclaration":
345
+ return formatMarkdownHover(formatServiceSignature(decl, isExported), "service", decl.id.name, docComment, originInfo);
346
+ case "BindingDeclaration": {
347
+ const bindingName = formatBindingTarget(decl.id);
348
+ const prefix = `${isExported ? "export " : ""}${decl.isMut ? "mut " : ""}`;
349
+ if (decl.init.kind === "FunctionExpression") {
350
+ const sig = `${prefix}${formatFunctionSignature(bindingName, decl.init, decl.typeAnnotation)}`;
351
+ return formatMarkdownHover(sig, "function", bindingName, docComment, originInfo);
352
+ }
353
+ const typeStr = decl.typeAnnotation
354
+ ? `: ${formatTypeNode(decl.typeAnnotation)}`
355
+ : ` = ${inferQuickExprType(decl.init)}`;
356
+ return formatMarkdownHover(`${prefix}${bindingName}${typeStr}`, "binding", bindingName, docComment, originInfo);
357
+ }
358
+ }
359
+ }
360
+ function findHoverInAst(source, uri, posLine, posCol, tokens, documents) {
361
+ const program = tryParseProgram(source, uri, tokens);
362
+ if (!program)
363
+ return null;
364
+ const finder = new DefinitionFinder(posLine, posCol);
365
+ const match = finder.find(program);
366
+ if (!match)
367
+ return null;
368
+ const range = spanToRange(match.originNode.span);
369
+ const declNode = match.declarationNode;
370
+ if (declNode.kind === "ExportDeclaration") {
371
+ return {
372
+ contents: {
373
+ kind: MarkupKind.Markdown,
374
+ value: formatTopLevelDeclarationHover(declNode.declaration, declNode, source),
375
+ },
376
+ range,
377
+ };
378
+ }
379
+ if (declNode.kind === "SchemaDeclaration" ||
380
+ declNode.kind === "ErrorDeclaration" ||
381
+ declNode.kind === "ServiceDeclaration" ||
382
+ declNode.kind === "BindingDeclaration") {
383
+ return {
384
+ contents: {
385
+ kind: MarkupKind.Markdown,
386
+ value: formatTopLevelDeclarationHover(declNode, declNode, source),
387
+ },
388
+ range,
389
+ };
390
+ }
391
+ if (declNode.kind === "ImportSpecifier") {
392
+ const spec = declNode;
393
+ const importDecl = program.body.find((entry) => entry.kind === "ImportDeclaration" && entry.specifiers.includes(spec));
394
+ if (!importDecl)
395
+ return null;
396
+ const localName = (spec.local ?? spec.imported).name;
397
+ const imported = findImportedDeclaration(program, uri, localName, documents);
398
+ if (!imported)
399
+ return null;
400
+ return {
401
+ contents: {
402
+ kind: MarkupKind.Markdown,
403
+ value: formatTopLevelDeclarationHover(imported.decl, imported.outer, imported.doc.text, `imported from \`${imported.modulePath}\``),
404
+ },
405
+ range,
406
+ };
407
+ }
408
+ return null;
409
+ }
410
+ function findTokenAtPosition(tokens, posLine, posCol) {
411
+ const lineTokens = [];
412
+ for (let i = 0; i < tokens.length; i++) {
413
+ const t = tokens[i];
414
+ if (t.span.start.line === posLine) {
415
+ lineTokens.push({ token: t, index: i });
416
+ }
417
+ }
418
+ if (lineTokens.length === 0)
419
+ return null;
420
+ for (const entry of lineTokens) {
421
+ if (posCol >= entry.token.span.start.column &&
422
+ posCol < entry.token.span.end.column) {
423
+ return entry;
424
+ }
425
+ }
426
+ for (const entry of lineTokens) {
427
+ if (posCol === entry.token.span.end.column) {
428
+ return entry;
429
+ }
430
+ }
431
+ return null;
432
+ }
433
+ function isJsxPunctuationOrTextToken(type) {
434
+ return (type === TokenType.JSX_TEXT ||
435
+ type === TokenType.JSX_TAG_OPEN ||
436
+ type === TokenType.JSX_TAG_CLOSE ||
437
+ type === TokenType.JSX_SLASH_CLOSE ||
438
+ type === TokenType.JSX_CLOSING_TAG_OPEN ||
439
+ type === TokenType.JSX_ATTRIBUTE_NAME);
440
+ }
441
+ function isJsxTagNameToken(tokens, index) {
442
+ const tok = tokens[index];
443
+ if (tok.type !== TokenType.IDENTIFIER &&
444
+ tok.type !== TokenType.UPPER_IDENTIFIER) {
445
+ return false;
446
+ }
447
+ let i = index - 1;
448
+ while (i >= 1 && tokens[i].type === TokenType.DOT) {
449
+ const prev = tokens[i - 1];
450
+ if (prev.type !== TokenType.IDENTIFIER &&
451
+ prev.type !== TokenType.UPPER_IDENTIFIER) {
452
+ return false;
453
+ }
454
+ i -= 2;
455
+ }
456
+ if (i < 0)
457
+ return false;
458
+ return (tokens[i].type === TokenType.JSX_TAG_OPEN ||
459
+ tokens[i].type === TokenType.JSX_CLOSING_TAG_OPEN);
460
+ }
461
+ function formatTagName(tagName) {
462
+ if (tagName.kind === "Identifier" || tagName.kind === "UpperIdentifier") {
463
+ return tagName.name;
464
+ }
465
+ return `${formatTagName(tagName.object)}.${tagName.property.name}`;
466
+ }
467
+ function findJsxAttributeInList(tagName, attributes, posLine, posCol) {
468
+ for (const attr of attributes) {
469
+ if (attr.kind === "JSXSpreadAttribute") {
470
+ const nested = findJsxAttributeInExpr(attr.argument, posLine, posCol);
471
+ if (nested)
472
+ return nested;
473
+ continue;
474
+ }
475
+ if (isPositionInSpan(posLine, posCol, attr.name.span)) {
476
+ return { tagName, attr };
477
+ }
478
+ if (attr.value?.kind === "JSXExpressionContainer") {
479
+ const nested = findJsxAttributeInExpr(attr.value.expression, posLine, posCol);
480
+ if (nested)
481
+ return nested;
482
+ }
483
+ }
484
+ return null;
485
+ }
486
+ function findJsxAttributeInExpr(expr, posLine, posCol) {
487
+ switch (expr.kind) {
488
+ case "JSXElement": {
489
+ const inAttrs = findJsxAttributeInList(expr.openingElement.name, expr.openingElement.attributes, posLine, posCol);
490
+ if (inAttrs)
491
+ return inAttrs;
492
+ for (const child of expr.children) {
493
+ if (child.kind === "JSXElement" ||
494
+ child.kind === "JSXSelfClosingElement") {
495
+ const nested = findJsxAttributeInExpr(child, posLine, posCol);
496
+ if (nested)
497
+ return nested;
498
+ }
499
+ else if (child.kind === "JSXExpressionContainer") {
500
+ const nested = findJsxAttributeInExpr(child.expression, posLine, posCol);
501
+ if (nested)
502
+ return nested;
503
+ }
504
+ }
505
+ return null;
506
+ }
507
+ case "JSXSelfClosingElement":
508
+ return findJsxAttributeInList(expr.name, expr.attributes, posLine, posCol);
509
+ case "FunctionExpression":
510
+ case "MutBlockExpression":
511
+ return expr.body.kind === "BlockStatement"
512
+ ? findJsxAttributeInBlock(expr.body, posLine, posCol)
513
+ : findJsxAttributeInExpr(expr.body, posLine, posCol);
514
+ case "PipelineExpression":
515
+ case "BinaryExpression":
516
+ return (findJsxAttributeInExpr(expr.left, posLine, posCol) ??
517
+ findJsxAttributeInExpr(expr.right, posLine, posCol));
518
+ case "UnaryExpression":
519
+ return findJsxAttributeInExpr(expr.argument, posLine, posCol);
520
+ case "CallExpression": {
521
+ const inCallee = findJsxAttributeInExpr(expr.callee, posLine, posCol);
522
+ if (inCallee)
523
+ return inCallee;
524
+ for (const arg of expr.args) {
525
+ const inArg = findJsxAttributeInExpr(arg, posLine, posCol);
526
+ if (inArg)
527
+ return inArg;
528
+ }
529
+ return null;
530
+ }
531
+ case "MemberExpression":
532
+ return findJsxAttributeInExpr(expr.object, posLine, posCol);
533
+ case "IndexExpression":
534
+ return (findJsxAttributeInExpr(expr.object, posLine, posCol) ??
535
+ findJsxAttributeInExpr(expr.index, posLine, posCol));
536
+ case "RecordExpression": {
537
+ for (const entry of expr.entries) {
538
+ if (entry.kind === "ShorthandEntry")
539
+ continue;
540
+ const inner = entry.kind === "SpreadEntry" ? entry.argument : entry.value;
541
+ const found = findJsxAttributeInExpr(inner, posLine, posCol);
542
+ if (found)
543
+ return found;
544
+ }
545
+ return null;
546
+ }
547
+ case "ArrayExpression": {
548
+ for (const el of expr.elements) {
549
+ const found = findJsxAttributeInExpr(el, posLine, posCol);
550
+ if (found)
551
+ return found;
552
+ }
553
+ return null;
554
+ }
555
+ case "MatchExpression": {
556
+ const inSubj = expr.scrutinee
557
+ ? findJsxAttributeInExpr(expr.scrutinee, posLine, posCol)
558
+ : null;
559
+ if (inSubj)
560
+ return inSubj;
561
+ for (const arm of expr.arms) {
562
+ if (arm.guard) {
563
+ const inGuard = findJsxAttributeInExpr(arm.guard, posLine, posCol);
564
+ if (inGuard)
565
+ return inGuard;
566
+ }
567
+ const inBody = arm.body.kind === "BlockStatement"
568
+ ? findJsxAttributeInBlock(arm.body, posLine, posCol)
569
+ : findJsxAttributeInExpr(arm.body, posLine, posCol);
570
+ if (inBody)
571
+ return inBody;
572
+ }
573
+ return null;
574
+ }
575
+ case "EffectBlockExpression": {
576
+ for (const stmt of expr.body) {
577
+ if (stmt.kind === "EffectBindStatement" ||
578
+ stmt.kind === "EffectDiscardStatement") {
579
+ const found = findJsxAttributeInExpr(stmt.effect, posLine, posCol);
580
+ if (found)
581
+ return found;
582
+ }
583
+ else if (stmt.kind === "EffectControlFlowStatement") {
584
+ const found = findJsxAttributeInExpr(stmt.statement, posLine, posCol);
585
+ if (found)
586
+ return found;
587
+ }
588
+ else if (stmt.kind === "BindingDeclaration") {
589
+ const found = findJsxAttributeInExpr(stmt.init, posLine, posCol);
590
+ if (found)
591
+ return found;
592
+ }
593
+ else if (stmt.kind === "AssignmentStatement") {
594
+ const found = findJsxAttributeInExpr(stmt.value, posLine, posCol);
595
+ if (found)
596
+ return found;
597
+ }
598
+ else if (stmt.kind === "WhenStatement") {
599
+ const inCond = findJsxAttributeInExpr(stmt.condition, posLine, posCol);
600
+ if (inCond)
601
+ return inCond;
602
+ const inCons = stmt.isEarlyReturn
603
+ ? findJsxAttributeInExpr(stmt.consequent, posLine, posCol)
604
+ : findJsxAttributeInBlock(stmt.consequent, posLine, posCol);
605
+ if (inCons)
606
+ return inCons;
607
+ }
608
+ }
609
+ return findJsxAttributeInExpr(expr.finalExpression, posLine, posCol);
610
+ }
611
+ case "StringLiteral":
612
+ case "TemplateLiteral": {
613
+ for (const part of expr.parts) {
614
+ if (part.kind === "StringInterpolationPart") {
615
+ const found = findJsxAttributeInExpr(part.expression, posLine, posCol);
616
+ if (found)
617
+ return found;
618
+ }
619
+ }
620
+ return null;
621
+ }
622
+ default:
623
+ return null;
624
+ }
625
+ }
626
+ function findJsxAttributeInBlock(block, posLine, posCol) {
627
+ for (const stmt of block.statements) {
628
+ if (stmt.kind === "BindingDeclaration") {
629
+ const found = findJsxAttributeInExpr(stmt.init, posLine, posCol);
630
+ if (found)
631
+ return found;
632
+ }
633
+ else if (stmt.kind === "AssignmentStatement") {
634
+ const found = findJsxAttributeInExpr(stmt.value, posLine, posCol);
635
+ if (found)
636
+ return found;
637
+ }
638
+ else if (stmt.kind === "ExpressionStatement") {
639
+ const found = findJsxAttributeInExpr(stmt.expression, posLine, posCol);
640
+ if (found)
641
+ return found;
642
+ }
643
+ else if (stmt.kind === "ReturnStatement") {
644
+ const found = findJsxAttributeInExpr(stmt.argument, posLine, posCol);
645
+ if (found)
646
+ return found;
647
+ }
648
+ else if (stmt.kind === "WhenStatement") {
649
+ const inCond = findJsxAttributeInExpr(stmt.condition, posLine, posCol);
650
+ if (inCond)
651
+ return inCond;
652
+ const inCons = stmt.isEarlyReturn
653
+ ? findJsxAttributeInExpr(stmt.consequent, posLine, posCol)
654
+ : findJsxAttributeInBlock(stmt.consequent, posLine, posCol);
655
+ if (inCons)
656
+ return inCons;
657
+ }
658
+ }
659
+ return block.finalExpression
660
+ ? findJsxAttributeInExpr(block.finalExpression, posLine, posCol)
661
+ : null;
662
+ }
663
+ function findJsxAttributeInProgram(program, posLine, posCol) {
664
+ for (const top of program.body) {
665
+ const inner = top.kind === "ExportDeclaration" ? top.declaration : top;
666
+ if (inner.kind === "BindingDeclaration") {
667
+ const found = findJsxAttributeInExpr(inner.init, posLine, posCol);
668
+ if (found)
669
+ return found;
670
+ }
671
+ else if (inner.kind === "ExportDefaultDeclaration") {
672
+ const found = findJsxAttributeInExpr(inner.expression, posLine, posCol);
673
+ if (found)
674
+ return found;
675
+ }
676
+ }
677
+ return null;
678
+ }
679
+ function resolveBindingDeclaration(program, source, uri, name, usageLine, usageCol, documents) {
680
+ const def = new DefinitionFinder(usageLine, usageCol).find(program);
681
+ if (def) {
682
+ const declNode = def.declarationNode;
683
+ const raw = declNode.kind === "ExportDeclaration" ? declNode.declaration : declNode;
684
+ if (raw.kind === "BindingDeclaration") {
685
+ return {
686
+ decl: raw,
687
+ ownerProgram: program,
688
+ ownerSource: source,
689
+ ownerUri: uri,
690
+ };
691
+ }
692
+ if (raw.kind === "ImportSpecifier") {
693
+ const localName = (raw.local ?? raw.imported).name;
694
+ const imported = findImportedDeclaration(program, uri, localName, documents);
695
+ if (imported && imported.decl.kind === "BindingDeclaration") {
696
+ const ownerProgram = tryParseProgram(imported.doc.text, imported.doc.uri);
697
+ if (ownerProgram) {
698
+ return {
699
+ decl: imported.decl,
700
+ ownerProgram,
701
+ ownerSource: imported.doc.text,
702
+ ownerUri: imported.doc.uri,
703
+ originInfo: `imported from \`${imported.modulePath}\``,
704
+ };
705
+ }
706
+ }
707
+ }
708
+ }
709
+ for (const top of program.body) {
710
+ const inner = top.kind === "ExportDeclaration" ? top.declaration : top;
711
+ if (inner.kind === "BindingDeclaration" &&
712
+ "name" in inner.id &&
713
+ inner.id.name === name) {
714
+ return {
715
+ decl: inner,
716
+ ownerProgram: program,
717
+ ownerSource: source,
718
+ ownerUri: uri,
719
+ };
720
+ }
721
+ }
722
+ const imported = findImportedDeclaration(program, uri, name, documents);
723
+ if (imported && imported.decl.kind === "BindingDeclaration") {
724
+ const ownerProgram = tryParseProgram(imported.doc.text, imported.doc.uri);
725
+ if (ownerProgram) {
726
+ return {
727
+ decl: imported.decl,
728
+ ownerProgram,
729
+ ownerSource: imported.doc.text,
730
+ ownerUri: imported.doc.uri,
731
+ originInfo: `imported from \`${imported.modulePath}\``,
732
+ };
733
+ }
734
+ }
735
+ return null;
736
+ }
737
+ function resolveRecordMemberEntry(program, source, uri, objectName, memberName, usageLine, usageCol, documents) {
738
+ const resolved = resolveBindingDeclaration(program, source, uri, objectName, usageLine, usageCol, documents);
739
+ if (!resolved || resolved.decl.init.kind !== "RecordExpression") {
740
+ return null;
741
+ }
742
+ const rec = resolved.decl.init;
743
+ const field = rec.entries.find((e) => e.kind === "FieldEntry" && e.key.name === memberName);
744
+ if (!field || field.kind !== "FieldEntry") {
745
+ return null;
746
+ }
747
+ return {
748
+ expr: field.value,
749
+ ownerProgram: resolved.ownerProgram,
750
+ ownerSource: resolved.ownerSource,
751
+ ownerUri: resolved.ownerUri,
752
+ originInfo: resolved.originInfo,
753
+ };
754
+ }
755
+ function findPropInTypeNode(typeNode, propName, ownerProgram, ownerSource, ownerUri, documents) {
756
+ if (typeNode.kind === "RecordTypeNode") {
757
+ const field = typeNode.fields.find((f) => f.name.name === propName);
758
+ if (!field)
759
+ return null;
760
+ return {
761
+ typeText: formatTypeNode(field.typeAnnotation),
762
+ docComment: extractLeadingDocComment(ownerSource, field.span.start.line),
763
+ };
764
+ }
765
+ if (typeNode.kind === "TypeReferenceNode") {
766
+ const schemaName = typeNode.name.name;
767
+ let targetSchema = null;
768
+ let schemaSource = ownerSource;
769
+ for (const top of ownerProgram.body) {
770
+ const inner = top.kind === "ExportDeclaration" ? top.declaration : top;
771
+ if (inner.kind === "SchemaDeclaration" && inner.id.name === schemaName) {
772
+ targetSchema = inner;
773
+ break;
774
+ }
775
+ }
776
+ if (!targetSchema) {
777
+ const imported = findImportedDeclaration(ownerProgram, ownerUri, schemaName, documents);
778
+ if (imported && imported.decl.kind === "SchemaDeclaration") {
779
+ targetSchema = imported.decl;
780
+ schemaSource = imported.doc.text;
781
+ }
782
+ }
783
+ if (!targetSchema)
784
+ return null;
785
+ if (targetSchema.body.kind === "RecordSchema") {
786
+ const field = targetSchema.body.fields.find((f) => f.name.name === propName);
787
+ if (!field)
788
+ return null;
789
+ return {
790
+ typeText: formatTypeNode(field.typeAnnotation),
791
+ docComment: extractLeadingDocComment(schemaSource, field.span.start.line),
792
+ };
793
+ }
794
+ }
795
+ return null;
796
+ }
797
+ function findJsxAttributeHover(source, uri, posLine, posCol, tokens, documents) {
798
+ const program = tryParseProgram(source, uri, tokens);
799
+ if (!program)
800
+ return null;
801
+ const hit = findJsxAttributeInProgram(program, posLine, posCol);
802
+ if (!hit || hit.attr.kind !== "JSXAttribute")
803
+ return null;
804
+ const { tagName, attr } = hit;
805
+ let paramType;
806
+ let ownerProgram = null;
807
+ let ownerSource = source;
808
+ let ownerUri = uri;
809
+ let originInfo;
810
+ if (tagName.kind === "MemberExpression" &&
811
+ (tagName.object.kind === "Identifier" ||
812
+ tagName.object.kind === "UpperIdentifier")) {
813
+ const member = resolveRecordMemberEntry(program, source, uri, tagName.object.name, tagName.property.name, tagName.object.span.start.line, tagName.object.span.start.column, documents);
814
+ if (member && member.expr.kind === "FunctionExpression") {
815
+ paramType = member.expr.params[0]?.typeAnnotation;
816
+ ownerProgram = member.ownerProgram;
817
+ ownerSource = member.ownerSource;
818
+ ownerUri = member.ownerUri;
819
+ originInfo = member.originInfo;
820
+ }
821
+ }
822
+ else if (tagName.kind === "Identifier" ||
823
+ tagName.kind === "UpperIdentifier") {
824
+ const resolved = resolveBindingDeclaration(program, source, uri, tagName.name, tagName.span.start.line, tagName.span.start.column, documents);
825
+ if (resolved) {
826
+ ownerProgram = resolved.ownerProgram;
827
+ ownerSource = resolved.ownerSource;
828
+ ownerUri = resolved.ownerUri;
829
+ originInfo = resolved.originInfo;
830
+ if (resolved.decl.init.kind === "FunctionExpression") {
831
+ paramType = resolved.decl.init.params[0]?.typeAnnotation;
832
+ }
833
+ else if (resolved.decl.typeAnnotation?.kind === "FunctionTypeNode") {
834
+ paramType = resolved.decl.typeAnnotation.params[0]?.typeAnnotation;
835
+ }
836
+ }
837
+ }
838
+ if (!paramType || !ownerProgram)
839
+ return null;
840
+ const prop = findPropInTypeNode(paramType, attr.name.name, ownerProgram, ownerSource, ownerUri, documents);
841
+ if (!prop)
842
+ return null;
843
+ const fullTag = formatTagName(tagName);
844
+ return {
845
+ contents: {
846
+ kind: MarkupKind.Markdown,
847
+ value: formatMarkdownHover(`${fullTag}.${attr.name.name}: ${prop.typeText}`, "prop", attr.name.name, prop.docComment, originInfo),
848
+ },
849
+ range: spanToRange(attr.name.span),
850
+ };
851
+ }
852
+ function formatRecordMemberHover(tokens, matchedIndex, source, uri, documents) {
853
+ if (matchedIndex < 2 ||
854
+ tokens[matchedIndex - 1].type !== TokenType.DOT ||
855
+ (tokens[matchedIndex - 2].type !== TokenType.UPPER_IDENTIFIER &&
856
+ tokens[matchedIndex - 2].type !== TokenType.IDENTIFIER)) {
857
+ return null;
858
+ }
859
+ const program = tryParseProgram(source, uri, tokens);
860
+ if (!program)
861
+ return null;
862
+ const objTok = tokens[matchedIndex - 2];
863
+ const token = tokens[matchedIndex];
864
+ const recMember = resolveRecordMemberEntry(program, source, uri, objTok.value, token.value, objTok.span.start.line, objTok.span.start.column, documents);
865
+ if (!recMember)
866
+ return null;
867
+ const sig = recMember.expr.kind === "FunctionExpression"
868
+ ? formatFunctionSignature(`${objTok.value}.${token.value}`, recMember.expr)
869
+ : `${objTok.value}.${token.value}: ${inferQuickExprType(recMember.expr)}`;
870
+ const docComment = extractLeadingDocComment(recMember.ownerSource, recMember.expr.span.start.line);
871
+ return {
872
+ contents: {
873
+ kind: MarkupKind.Markdown,
874
+ value: formatMarkdownHover(sig, recMember.expr.kind === "FunctionExpression" ? "function" : "property", `${objTok.value}.${token.value}`, docComment, recMember.originInfo),
875
+ },
876
+ range: spanToRange(token.span),
877
+ };
878
+ }
879
+ function findJsxTagHover(tokens, matchedIndex, posLine, posCol, source, uri, skipSymbol, documents) {
880
+ const token = tokens[matchedIndex];
881
+ const isMemberSegment = matchedIndex >= 2 && tokens[matchedIndex - 1].type === TokenType.DOT;
882
+ const hasMemberSuffix = matchedIndex + 2 < tokens.length &&
883
+ tokens[matchedIndex + 1].type === TokenType.DOT;
884
+ if (isMemberSegment) {
885
+ const memberHover = formatRecordMemberHover(tokens, matchedIndex, source, uri, documents);
886
+ if (memberHover)
887
+ return memberHover;
888
+ }
889
+ else if (token.type === TokenType.UPPER_IDENTIFIER || hasMemberSuffix) {
890
+ const astHover = findHoverInAst(source, uri, posLine, posCol, tokens, documents);
891
+ if (astHover)
892
+ return astHover;
893
+ }
894
+ else {
895
+ // Lowercase intrinsic JSX tag (e.g. `<div>`, `<span>`)
896
+ return null;
897
+ }
898
+ if (!skipSymbol && posCol < token.span.end.column) {
899
+ return symbolHover(source, uri, token);
900
+ }
901
+ return null;
902
+ }
20
903
  function symbolHover(source, uri, token, companion) {
21
904
  if (token.type !== TokenType.IDENTIFIER &&
22
905
  token.type !== TokenType.UPPER_IDENTIFIER)
@@ -35,6 +918,26 @@ function symbolHover(source, uri, token, companion) {
35
918
  }
36
919
  function symbolHoverFromResult(source, token, context, result, companion) {
37
920
  try {
921
+ const schemaDeclaration = result.ast.body.flatMap((entry) => {
922
+ const decl = entry.kind === "ExportDeclaration" ? entry.declaration : entry;
923
+ return decl.kind === "SchemaDeclaration" &&
924
+ decl.body.kind !== "RecordSchema" &&
925
+ decl.body.kind !== "ImportedTsSchema" &&
926
+ decl.id.span.start.offset === token.span.start.offset
927
+ ? [decl]
928
+ : [];
929
+ });
930
+ if (schemaDeclaration.length === 1) {
931
+ const decl = schemaDeclaration[0];
932
+ const signature = source.slice(decl.id.span.start.offset, decl.body.span.end.offset);
933
+ return {
934
+ contents: {
935
+ kind: MarkupKind.Markdown,
936
+ value: `\`\`\`fun\nschema ${signature}\n\`\`\``,
937
+ },
938
+ range: spanToRange(token.span),
939
+ };
940
+ }
38
941
  const schema = result.ast.body.flatMap((entry) => {
39
942
  const decl = entry.kind === "ExportDeclaration" ? entry.declaration : entry;
40
943
  return decl.kind === "SchemaDeclaration" &&
@@ -113,7 +1016,45 @@ function symbolHoverFromResult(source, token, context, result, companion) {
113
1016
  symbol = candidates[0].symbol;
114
1017
  node = candidates[0].node;
115
1018
  }
116
- const type = checked.checker.typeToString(checked.checker.getTypeOfSymbolAtLocation(symbol, node), node, ts.TypeFormatFlags.NoTruncation);
1019
+ const valueType = checked.checker.getTypeOfSymbolAtLocation(symbol, node);
1020
+ const resolvedType = valueType.flags & ts.TypeFlags.Any &&
1021
+ symbol.flags & ts.SymbolFlags.TypeAlias
1022
+ ? checked.checker.getDeclaredTypeOfSymbol(symbol)
1023
+ : valueType;
1024
+ if (resolvedType.flags & ts.TypeFlags.Any ||
1025
+ checked.checker.typeToString(resolvedType, node) === token.value ||
1026
+ checked.checker
1027
+ .typeToString(resolvedType, node)
1028
+ .startsWith(`${token.value}<`)) {
1029
+ const location = findDefinition(source, "file:///hover.fun", {
1030
+ line: token.span.start.line - 1,
1031
+ character: token.span.start.column - 1,
1032
+ }, true);
1033
+ const target = Array.isArray(location)
1034
+ ? location[0]?.targetSelectionRange.start
1035
+ : undefined;
1036
+ const declaration = result.ast.body.flatMap((entry) => {
1037
+ const decl = entry.kind === "ExportDeclaration" ? entry.declaration : entry;
1038
+ return decl.kind === "SchemaDeclaration" &&
1039
+ target &&
1040
+ decl.id.span.start.line - 1 === target.line &&
1041
+ decl.id.span.start.column - 1 === target.character
1042
+ ? [decl]
1043
+ : [];
1044
+ });
1045
+ if (declaration.length === 1) {
1046
+ const decl = declaration[0];
1047
+ const signature = source.slice(decl.id.span.start.offset, decl.body.span.end.offset);
1048
+ return {
1049
+ contents: {
1050
+ kind: MarkupKind.Markdown,
1051
+ value: `\`\`\`fun\nschema ${signature}\n\`\`\``,
1052
+ },
1053
+ range: spanToRange(token.span),
1054
+ };
1055
+ }
1056
+ }
1057
+ const type = checked.checker.typeToString(resolvedType, node, ts.TypeFormatFlags.NoTruncation);
117
1058
  const documented = symbol.flags & ts.SymbolFlags.Alias
118
1059
  ? checked.checker.getAliasedSymbol(symbol)
119
1060
  : symbol;
@@ -217,42 +1158,21 @@ function importedSchemaHover(source, uri, position) {
217
1158
  return null;
218
1159
  }
219
1160
  }
220
- function isWordChar(ch) {
221
- return /[A-Za-z0-9_]/.test(ch);
222
- }
223
- function findHoverInTokens(tokens, posLine, posCol, currentLine, source, uri, skipSymbol = false) {
224
- // Find token on posLine that strictly or best matches posCol
225
- const lineTokens = [];
226
- for (let i = 0; i < tokens.length; i++) {
227
- const t = tokens[i];
228
- if (t.span.start.line === posLine) {
229
- lineTokens.push({ token: t, index: i });
230
- }
231
- }
232
- if (lineTokens.length === 0) {
1161
+ function findHoverInTokens(tokens, posLine, posCol, currentLine, source, uri, skipSymbol = false, documents) {
1162
+ const matched = findTokenAtPosition(tokens, posLine, posCol);
1163
+ if (!matched) {
233
1164
  return null;
234
1165
  }
235
- // 1. Check for token that strictly encompasses posCol
236
- let matchedIndex = -1;
237
- for (const { token, index } of lineTokens) {
238
- if (posCol >= token.span.start.column && posCol < token.span.end.column) {
239
- matchedIndex = index;
240
- break;
241
- }
1166
+ const { token, index: matchedIndex } = matched;
1167
+ if (token.type === TokenType.JSX_ATTRIBUTE_NAME) {
1168
+ return findJsxAttributeHover(source, uri, posLine, posCol, tokens, documents);
242
1169
  }
243
- // 2. If no strict match, check if cursor is at token end boundary
244
- if (matchedIndex === -1) {
245
- for (const { token, index } of lineTokens) {
246
- if (posCol === token.span.end.column) {
247
- matchedIndex = index;
248
- break;
249
- }
250
- }
251
- }
252
- if (matchedIndex === -1) {
1170
+ if (isJsxPunctuationOrTextToken(token.type)) {
253
1171
  return null;
254
1172
  }
255
- const token = tokens[matchedIndex];
1173
+ if (isJsxTagNameToken(tokens, matchedIndex)) {
1174
+ return findJsxTagHover(tokens, matchedIndex, posLine, posCol, source, uri, skipSymbol, documents);
1175
+ }
256
1176
  // Case A: token is a member after a dot (e.g. `succeed` in `Effect.succeed` or `scoped` in `Layer.scoped`)
257
1177
  if (matchedIndex >= 2 &&
258
1178
  tokens[matchedIndex - 1].type === TokenType.DOT &&
@@ -412,7 +1332,34 @@ function findHoverInTokens(tokens, posLine, posCol, currentLine, source, uri, sk
412
1332
  }
413
1333
  return null;
414
1334
  }
415
- function findHoverInLineText(currentLine, lineIndex, charIndex) {
1335
+ function isSuppressedInFunxLineFallback(lines, lineIndex, charIndex, uri) {
1336
+ if (!uri.endsWith(".funx"))
1337
+ return false;
1338
+ const currentLine = lines[lineIndex] ?? "";
1339
+ const upToCursor = lines
1340
+ .slice(0, lineIndex)
1341
+ .concat(currentLine.slice(0, charIndex))
1342
+ .join("\n");
1343
+ const ctx = detectJsxContext(upToCursor);
1344
+ if (ctx.kind !== "normal")
1345
+ return true;
1346
+ if (charIndex < currentLine.length) {
1347
+ const throughChar = lines
1348
+ .slice(0, lineIndex)
1349
+ .concat(currentLine.slice(0, charIndex + 1))
1350
+ .join("\n");
1351
+ if (detectJsxContext(throughChar).kind !== "normal") {
1352
+ return true;
1353
+ }
1354
+ }
1355
+ return false;
1356
+ }
1357
+ function findHoverInLineText(currentLine, lineIndex, charIndex, lines, uri) {
1358
+ if (lines &&
1359
+ uri &&
1360
+ isSuppressedInFunxLineFallback(lines, lineIndex, charIndex, uri)) {
1361
+ return null;
1362
+ }
416
1363
  // 1. Check multi-character operators first (e.g. `???`, `|>`, `<-`, `=>`, and banned symbols)
417
1364
  const bannedOps = Object.keys(BANNED_SYMBOLS);
418
1365
  const operators = [
@@ -593,10 +1540,14 @@ function findHoverInLineText(currentLine, lineIndex, charIndex) {
593
1540
  }
594
1541
  return null;
595
1542
  }
596
- export function findHover(source, uri, position, skipSymbol = false) {
1543
+ export function findHover(source, uri, position, skipSymbolOrDocuments = false, documents) {
597
1544
  if (!source || source.trim().length === 0) {
598
1545
  return null;
599
1546
  }
1547
+ const skipSymbol = typeof skipSymbolOrDocuments === "boolean" ? skipSymbolOrDocuments : false;
1548
+ const resolvedDocuments = Array.isArray(skipSymbolOrDocuments)
1549
+ ? skipSymbolOrDocuments
1550
+ : documents;
600
1551
  const lines = source.split("\n");
601
1552
  const lineIndex = position.line;
602
1553
  const charIndex = position.character;
@@ -624,13 +1575,21 @@ export function findHover(source, uri, position, skipSymbol = false) {
624
1575
  const importedHover = importedSchemaHover(source, uri, position);
625
1576
  if (importedHover)
626
1577
  return importedHover;
627
- const hoverFromTokens = findHoverInTokens(tokens, posLine, posCol, currentLine, source, uri, skipSymbol);
1578
+ const hoverFromTokens = findHoverInTokens(tokens, posLine, posCol, currentLine, source, uri, skipSymbol, resolvedDocuments);
628
1579
  if (hoverFromTokens) {
629
1580
  return hoverFromTokens;
630
1581
  }
1582
+ const matched = findTokenAtPosition(tokens, posLine, posCol);
1583
+ if (matched &&
1584
+ (isJsxPunctuationOrTextToken(matched.token.type) ||
1585
+ isJsxTagNameToken(tokens, matched.index) ||
1586
+ matched.token.type === TokenType.STRING ||
1587
+ matched.token.type === TokenType.STRING_PART)) {
1588
+ return null;
1589
+ }
631
1590
  }
632
1591
  // 2. Fallback to line text-based inspection (e.g. if lexer threw or comments/unparsed code)
633
- return findHoverInLineText(currentLine, lineIndex, charIndex);
1592
+ return findHoverInLineText(currentLine, lineIndex, charIndex, lines, uri);
634
1593
  }
635
1594
  /** Async hover retains synchronous behavior for ordinary sources and compiles Wasm blocks when needed. */
636
1595
  export async function findHoverAsync(source, uri, position) {