@hydranium/core 1.0.0-next.8 → 1.0.0-next.85

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 (306) hide show
  1. package/lib/documents/ast-document-manager.d.ts +75 -16
  2. package/lib/documents/ast-document-manager.d.ts.map +1 -1
  3. package/lib/documents/ast-document-manager.js +57 -19
  4. package/lib/documents/ast-document-manager.js.map +1 -1
  5. package/lib/documents/client-ids.d.ts +5 -19
  6. package/lib/documents/client-ids.d.ts.map +1 -1
  7. package/lib/documents/client-ids.js +5 -19
  8. package/lib/documents/client-ids.js.map +1 -1
  9. package/lib/documents/hydranium-text-documents.d.ts +55 -21
  10. package/lib/documents/hydranium-text-documents.d.ts.map +1 -1
  11. package/lib/documents/hydranium-text-documents.js +142 -46
  12. package/lib/documents/hydranium-text-documents.js.map +1 -1
  13. package/lib/documents/language-client-text-shadow.d.ts +41 -9
  14. package/lib/documents/language-client-text-shadow.d.ts.map +1 -1
  15. package/lib/documents/language-client-text-shadow.js +56 -11
  16. package/lib/documents/language-client-text-shadow.js.map +1 -1
  17. package/lib/documents/self-save-registry.d.ts +37 -6
  18. package/lib/documents/self-save-registry.d.ts.map +1 -1
  19. package/lib/documents/self-save-registry.js +28 -15
  20. package/lib/documents/self-save-registry.js.map +1 -1
  21. package/lib/index.d.ts +2 -0
  22. package/lib/index.d.ts.map +1 -1
  23. package/lib/index.js +5 -0
  24. package/lib/index.js.map +1 -1
  25. package/lib/langium/ast-extension/ast-node-builder.d.ts +32 -9
  26. package/lib/langium/ast-extension/ast-node-builder.d.ts.map +1 -1
  27. package/lib/langium/ast-extension/ast-node-builder.js +60 -22
  28. package/lib/langium/ast-extension/ast-node-builder.js.map +1 -1
  29. package/lib/langium/bootstrap.d.ts.map +1 -1
  30. package/lib/langium/bootstrap.js +16 -9
  31. package/lib/langium/bootstrap.js.map +1 -1
  32. package/lib/langium/config/configuration-provider.d.ts +81 -0
  33. package/lib/langium/config/configuration-provider.d.ts.map +1 -0
  34. package/lib/langium/config/configuration-provider.js +112 -0
  35. package/lib/langium/config/configuration-provider.js.map +1 -0
  36. package/lib/langium/config/index.d.ts +1 -0
  37. package/lib/langium/config/index.d.ts.map +1 -1
  38. package/lib/langium/config/index.js +1 -0
  39. package/lib/langium/config/index.js.map +1 -1
  40. package/lib/langium/diagnostics/lsp-logger.d.ts +17 -0
  41. package/lib/langium/diagnostics/lsp-logger.d.ts.map +1 -1
  42. package/lib/langium/diagnostics/lsp-logger.js +40 -6
  43. package/lib/langium/diagnostics/lsp-logger.js.map +1 -1
  44. package/lib/langium/document-builder/build-pipeline-integration.d.ts +13 -1
  45. package/lib/langium/document-builder/build-pipeline-integration.d.ts.map +1 -1
  46. package/lib/langium/document-builder/build-pipeline-integration.js +1 -1
  47. package/lib/langium/document-builder/build-pipeline-integration.js.map +1 -1
  48. package/lib/langium/document-builder/build-session.d.ts +93 -0
  49. package/lib/langium/document-builder/build-session.d.ts.map +1 -0
  50. package/lib/langium/document-builder/build-session.js +72 -0
  51. package/lib/langium/document-builder/build-session.js.map +1 -0
  52. package/lib/langium/document-builder/document-builder.d.ts +150 -7
  53. package/lib/langium/document-builder/document-builder.d.ts.map +1 -1
  54. package/lib/langium/document-builder/document-builder.js +298 -11
  55. package/lib/langium/document-builder/document-builder.js.map +1 -1
  56. package/lib/langium/document-builder/index.d.ts +1 -0
  57. package/lib/langium/document-builder/index.d.ts.map +1 -1
  58. package/lib/langium/document-builder/index.js +1 -0
  59. package/lib/langium/document-builder/index.js.map +1 -1
  60. package/lib/langium/integration-services.d.ts +45 -1
  61. package/lib/langium/integration-services.d.ts.map +1 -1
  62. package/lib/langium/integration-services.js +10 -5
  63. package/lib/langium/integration-services.js.map +1 -1
  64. package/lib/langium/integrity/integrity-service.d.ts +5 -0
  65. package/lib/langium/integrity/integrity-service.d.ts.map +1 -1
  66. package/lib/langium/integrity/integrity-service.js +34 -1
  67. package/lib/langium/integrity/integrity-service.js.map +1 -1
  68. package/lib/langium/keys/containment.d.ts +64 -0
  69. package/lib/langium/keys/containment.d.ts.map +1 -0
  70. package/lib/langium/keys/containment.js +53 -0
  71. package/lib/langium/keys/containment.js.map +1 -0
  72. package/lib/langium/keys/index.d.ts +1 -0
  73. package/lib/langium/keys/index.d.ts.map +1 -1
  74. package/lib/langium/keys/index.js +1 -0
  75. package/lib/langium/keys/index.js.map +1 -1
  76. package/lib/langium/keys/name-based-key-provider.d.ts +4 -0
  77. package/lib/langium/keys/name-based-key-provider.d.ts.map +1 -1
  78. package/lib/langium/keys/name-based-key-provider.js +4 -0
  79. package/lib/langium/keys/name-based-key-provider.js.map +1 -1
  80. package/lib/langium/model-service/model-service.d.ts +149 -10
  81. package/lib/langium/model-service/model-service.d.ts.map +1 -1
  82. package/lib/langium/model-service/model-service.js +120 -93
  83. package/lib/langium/model-service/model-service.js.map +1 -1
  84. package/lib/langium/module.d.ts +84 -15
  85. package/lib/langium/module.d.ts.map +1 -1
  86. package/lib/langium/module.js +26 -12
  87. package/lib/langium/module.js.map +1 -1
  88. package/lib/langium/naming/name-provider.d.ts +25 -4
  89. package/lib/langium/naming/name-provider.d.ts.map +1 -1
  90. package/lib/langium/naming/name-provider.js +2 -1
  91. package/lib/langium/naming/name-provider.js.map +1 -1
  92. package/lib/langium/naming/name-separator-validation.d.ts +23 -0
  93. package/lib/langium/naming/name-separator-validation.d.ts.map +1 -1
  94. package/lib/langium/naming/name-separator-validation.js +30 -1
  95. package/lib/langium/naming/name-separator-validation.js.map +1 -1
  96. package/lib/langium/residency/cst-residency-service.d.ts +31 -9
  97. package/lib/langium/residency/cst-residency-service.d.ts.map +1 -1
  98. package/lib/langium/residency/cst-residency-service.js +13 -56
  99. package/lib/langium/residency/cst-residency-service.js.map +1 -1
  100. package/lib/langium/scope/hydranium-scope-provider.d.ts +32 -13
  101. package/lib/langium/scope/hydranium-scope-provider.d.ts.map +1 -1
  102. package/lib/langium/scope/hydranium-scope-provider.js +37 -20
  103. package/lib/langium/scope/hydranium-scope-provider.js.map +1 -1
  104. package/lib/langium/shared-services.d.ts +15 -2
  105. package/lib/langium/shared-services.d.ts.map +1 -1
  106. package/lib/langium/shared-services.js.map +1 -1
  107. package/lib/langium/transfer/transfer-encoder.d.ts +60 -17
  108. package/lib/langium/transfer/transfer-encoder.d.ts.map +1 -1
  109. package/lib/langium/transfer/transfer-encoder.js +38 -16
  110. package/lib/langium/transfer/transfer-encoder.js.map +1 -1
  111. package/lib/langium/validation/document-validator.d.ts +294 -10
  112. package/lib/langium/validation/document-validator.d.ts.map +1 -1
  113. package/lib/langium/validation/document-validator.js +448 -5
  114. package/lib/langium/validation/document-validator.js.map +1 -1
  115. package/lib/langium/workspace/document-uri-policy.d.ts +3 -4
  116. package/lib/langium/workspace/document-uri-policy.d.ts.map +1 -1
  117. package/lib/langium/workspace/document-uri-policy.js +3 -4
  118. package/lib/langium/workspace/document-uri-policy.js.map +1 -1
  119. package/lib/langium/workspace/hydranium-langium-document-factory.d.ts +25 -0
  120. package/lib/langium/workspace/hydranium-langium-document-factory.d.ts.map +1 -1
  121. package/lib/langium/workspace/hydranium-langium-document-factory.js +25 -0
  122. package/lib/langium/workspace/hydranium-langium-document-factory.js.map +1 -1
  123. package/lib/langium/workspace/hydranium-workspace-manager.d.ts +21 -1
  124. package/lib/langium/workspace/hydranium-workspace-manager.d.ts.map +1 -1
  125. package/lib/langium/workspace/hydranium-workspace-manager.js +28 -0
  126. package/lib/langium/workspace/hydranium-workspace-manager.js.map +1 -1
  127. package/lib/langium/workspace/in-memory-file-system-provider.d.ts +8 -0
  128. package/lib/langium/workspace/in-memory-file-system-provider.d.ts.map +1 -1
  129. package/lib/langium/workspace/in-memory-file-system-provider.js +11 -2
  130. package/lib/langium/workspace/in-memory-file-system-provider.js.map +1 -1
  131. package/lib/langium/workspace/initialize-workspace.d.ts +22 -2
  132. package/lib/langium/workspace/initialize-workspace.d.ts.map +1 -1
  133. package/lib/langium/workspace/initialize-workspace.js +13 -5
  134. package/lib/langium/workspace/initialize-workspace.js.map +1 -1
  135. package/lib/langium/workspace/langium-documents.d.ts +79 -17
  136. package/lib/langium/workspace/langium-documents.d.ts.map +1 -1
  137. package/lib/langium/workspace/langium-documents.js +83 -28
  138. package/lib/langium/workspace/langium-documents.js.map +1 -1
  139. package/lib/locale/index.d.ts +10 -0
  140. package/lib/locale/index.d.ts.map +1 -0
  141. package/lib/locale/index.js +10 -0
  142. package/lib/locale/index.js.map +1 -0
  143. package/lib/locale/server-locale.d.ts +73 -0
  144. package/lib/locale/server-locale.d.ts.map +1 -0
  145. package/lib/locale/server-locale.js +61 -0
  146. package/lib/locale/server-locale.js.map +1 -0
  147. package/lib/lsp/hydranium-document-update-handler.js +1 -1
  148. package/lib/lsp/hydranium-document-update-handler.js.map +1 -1
  149. package/lib/lsp/index.d.ts +1 -1
  150. package/lib/lsp/index.d.ts.map +1 -1
  151. package/lib/lsp/index.js +1 -1
  152. package/lib/lsp/index.js.map +1 -1
  153. package/lib/lsp/lsp-latency.d.ts +39 -0
  154. package/lib/lsp/lsp-latency.d.ts.map +1 -0
  155. package/lib/lsp/lsp-latency.js +58 -0
  156. package/lib/lsp/lsp-latency.js.map +1 -0
  157. package/lib/lsp/semantic-token-provider.d.ts +34 -1
  158. package/lib/lsp/semantic-token-provider.d.ts.map +1 -1
  159. package/lib/lsp/semantic-token-provider.js +55 -8
  160. package/lib/lsp/semantic-token-provider.js.map +1 -1
  161. package/lib/messages/carriers.d.ts +54 -0
  162. package/lib/messages/carriers.d.ts.map +1 -0
  163. package/lib/messages/carriers.js +62 -0
  164. package/lib/messages/carriers.js.map +1 -0
  165. package/lib/messages/index.d.ts +25 -0
  166. package/lib/messages/index.d.ts.map +1 -0
  167. package/lib/messages/index.js +25 -0
  168. package/lib/messages/index.js.map +1 -0
  169. package/lib/messages/renderer.d.ts +136 -0
  170. package/lib/messages/renderer.d.ts.map +1 -0
  171. package/lib/messages/renderer.js +159 -0
  172. package/lib/messages/renderer.js.map +1 -0
  173. package/lib/node/heap-ceiling.d.ts +61 -0
  174. package/lib/node/heap-ceiling.d.ts.map +1 -0
  175. package/lib/node/heap-ceiling.js +72 -0
  176. package/lib/node/heap-ceiling.js.map +1 -0
  177. package/lib/node/index.d.ts +1 -0
  178. package/lib/node/index.d.ts.map +1 -1
  179. package/lib/node/index.js +1 -0
  180. package/lib/node/index.js.map +1 -1
  181. package/lib/node/latency-from-env.d.ts +1 -1
  182. package/lib/node/latency-from-env.js +1 -1
  183. package/lib/node/node-file-system-provider.d.ts.map +1 -1
  184. package/lib/node/node-file-system-provider.js +1 -40
  185. package/lib/node/node-file-system-provider.js.map +1 -1
  186. package/lib/node/profiling-run.d.ts +2 -2
  187. package/lib/node/profiling-run.js +1 -1
  188. package/lib/node/profiling-run.js.map +1 -1
  189. package/lib/node/rename-over-open-readers.d.ts +52 -0
  190. package/lib/node/rename-over-open-readers.d.ts.map +1 -0
  191. package/lib/node/rename-over-open-readers.js +66 -0
  192. package/lib/node/rename-over-open-readers.js.map +1 -0
  193. package/lib/testing/fake-document.d.ts +16 -4
  194. package/lib/testing/fake-document.d.ts.map +1 -1
  195. package/lib/testing/fake-document.js +18 -3
  196. package/lib/testing/fake-document.js.map +1 -1
  197. package/lib/testing/make-noop-shared-services.d.ts +14 -0
  198. package/lib/testing/make-noop-shared-services.d.ts.map +1 -1
  199. package/lib/testing/make-noop-shared-services.js +28 -2
  200. package/lib/testing/make-noop-shared-services.js.map +1 -1
  201. package/lib/testing/make-test-services.d.ts +44 -8
  202. package/lib/testing/make-test-services.d.ts.map +1 -1
  203. package/lib/testing/make-test-services.js +19 -3
  204. package/lib/testing/make-test-services.js.map +1 -1
  205. package/lib/testing/node/spawned-server.d.ts +14 -0
  206. package/lib/testing/node/spawned-server.d.ts.map +1 -1
  207. package/lib/testing/node/spawned-server.js +33 -1
  208. package/lib/testing/node/spawned-server.js.map +1 -1
  209. package/lib/testing/playwright/flaky-network-proxy.d.ts +76 -0
  210. package/lib/testing/playwright/flaky-network-proxy.d.ts.map +1 -0
  211. package/lib/testing/playwright/flaky-network-proxy.js +164 -0
  212. package/lib/testing/playwright/flaky-network-proxy.js.map +1 -0
  213. package/lib/testing/playwright/index.d.ts +1 -0
  214. package/lib/testing/playwright/index.d.ts.map +1 -1
  215. package/lib/testing/playwright/index.js +1 -0
  216. package/lib/testing/playwright/index.js.map +1 -1
  217. package/lib/testing/playwright/server-log-capture.d.ts +14 -4
  218. package/lib/testing/playwright/server-log-capture.d.ts.map +1 -1
  219. package/lib/testing/playwright/server-log-capture.js +21 -4
  220. package/lib/testing/playwright/server-log-capture.js.map +1 -1
  221. package/lib/testing/playwright/server-log-rename-reporter.d.ts.map +1 -1
  222. package/lib/testing/playwright/server-log-rename-reporter.js +10 -1
  223. package/lib/testing/playwright/server-log-rename-reporter.js.map +1 -1
  224. package/lib/testing/stub-ast-document-manager.d.ts +5 -5
  225. package/lib/testing/stub-ast-document-manager.d.ts.map +1 -1
  226. package/lib/testing/stub-ast-document-manager.js +4 -6
  227. package/lib/testing/stub-ast-document-manager.js.map +1 -1
  228. package/lib/testing/stub-document-builder.d.ts +8 -0
  229. package/lib/testing/stub-document-builder.d.ts.map +1 -1
  230. package/lib/testing/stub-document-builder.js +22 -3
  231. package/lib/testing/stub-document-builder.js.map +1 -1
  232. package/lib/testing/stub-langium-documents.d.ts +3 -2
  233. package/lib/testing/stub-langium-documents.d.ts.map +1 -1
  234. package/lib/testing/stub-langium-documents.js.map +1 -1
  235. package/lib/testing/stub-model-service.d.ts +5 -4
  236. package/lib/testing/stub-model-service.d.ts.map +1 -1
  237. package/lib/testing/stub-model-service.js +2 -2
  238. package/lib/testing/stub-model-service.js.map +1 -1
  239. package/package.json +17 -8
  240. package/src/documents/ast-document-manager.ts +129 -36
  241. package/src/documents/client-ids.ts +5 -22
  242. package/src/documents/hydranium-text-documents.ts +167 -61
  243. package/src/documents/language-client-text-shadow.ts +59 -11
  244. package/src/documents/self-save-registry.ts +52 -13
  245. package/src/index.ts +5 -0
  246. package/src/langium/ast-extension/ast-node-builder.ts +66 -22
  247. package/src/langium/bootstrap.ts +16 -9
  248. package/src/langium/config/configuration-provider.ts +119 -0
  249. package/src/langium/config/index.ts +1 -0
  250. package/src/langium/diagnostics/lsp-logger.ts +41 -6
  251. package/src/langium/document-builder/build-pipeline-integration.ts +14 -1
  252. package/src/langium/document-builder/build-session.ts +87 -0
  253. package/src/langium/document-builder/document-builder.ts +339 -11
  254. package/src/langium/document-builder/index.ts +1 -0
  255. package/src/langium/integration-services.ts +67 -6
  256. package/src/langium/integrity/integrity-service.ts +35 -1
  257. package/src/langium/keys/containment.ts +87 -0
  258. package/src/langium/keys/index.ts +1 -0
  259. package/src/langium/keys/name-based-key-provider.ts +4 -0
  260. package/src/langium/model-service/model-service.ts +211 -30
  261. package/src/langium/module.ts +106 -21
  262. package/src/langium/naming/name-provider.ts +27 -5
  263. package/src/langium/naming/name-separator-validation.ts +34 -5
  264. package/src/langium/residency/cst-residency-service.ts +36 -12
  265. package/src/langium/scope/hydranium-scope-provider.ts +40 -22
  266. package/src/langium/shared-services.ts +19 -2
  267. package/src/langium/transfer/transfer-encoder.ts +85 -24
  268. package/src/langium/validation/document-validator.ts +537 -11
  269. package/src/langium/workspace/document-uri-policy.ts +3 -4
  270. package/src/langium/workspace/hydranium-langium-document-factory.ts +34 -0
  271. package/src/langium/workspace/hydranium-workspace-manager.ts +30 -1
  272. package/src/langium/workspace/in-memory-file-system-provider.ts +13 -3
  273. package/src/langium/workspace/initialize-workspace.ts +34 -5
  274. package/src/langium/workspace/langium-documents.ts +106 -33
  275. package/src/locale/index.ts +10 -0
  276. package/src/locale/server-locale.ts +86 -0
  277. package/src/lsp/hydranium-document-update-handler.ts +1 -1
  278. package/src/lsp/index.ts +1 -1
  279. package/src/lsp/lsp-latency.ts +62 -0
  280. package/src/lsp/semantic-token-provider.ts +74 -7
  281. package/src/messages/carriers.ts +78 -0
  282. package/src/messages/index.ts +36 -0
  283. package/src/messages/renderer.ts +204 -0
  284. package/src/node/heap-ceiling.ts +108 -0
  285. package/src/node/index.ts +1 -0
  286. package/src/node/latency-from-env.ts +1 -1
  287. package/src/node/node-file-system-provider.ts +1 -41
  288. package/src/node/profiling-run.ts +3 -3
  289. package/src/node/rename-over-open-readers.ts +78 -0
  290. package/src/testing/fake-document.ts +24 -5
  291. package/src/testing/make-noop-shared-services.ts +50 -2
  292. package/src/testing/make-test-services.ts +73 -18
  293. package/src/testing/node/spawned-server.ts +35 -1
  294. package/src/testing/playwright/flaky-network-proxy.ts +240 -0
  295. package/src/testing/playwright/index.ts +1 -0
  296. package/src/testing/playwright/server-log-capture.ts +24 -5
  297. package/src/testing/playwright/server-log-rename-reporter.ts +12 -1
  298. package/src/testing/stub-ast-document-manager.ts +10 -14
  299. package/src/testing/stub-document-builder.ts +32 -3
  300. package/src/testing/stub-langium-documents.ts +3 -2
  301. package/src/testing/stub-model-service.ts +7 -6
  302. package/lib/lsp/instrument-connection.d.ts +0 -36
  303. package/lib/lsp/instrument-connection.d.ts.map +0 -1
  304. package/lib/lsp/instrument-connection.js +0 -62
  305. package/lib/lsp/instrument-connection.js.map +0 -1
  306. package/src/lsp/instrument-connection.ts +0 -66
@@ -7,34 +7,244 @@
7
7
  * SPDX-License-Identifier: MIT
8
8
  ********************************************************************************/
9
9
 
10
- import { type LogThreshold, type MaybeObservableValue, ObservableValue, type Tracer, TransferDiagnostic } from '@hydranium/protocol';
10
+ import {
11
+ defineMessage,
12
+ describeError,
13
+ interpolate,
14
+ type MessageDefinition,
15
+ type MessageParams,
16
+ messageData,
17
+ type ParamsArg,
18
+ type ParamsOf,
19
+ type LogThreshold,
20
+ type MaybeObservableValue,
21
+ ObservableValue,
22
+ type Tracer,
23
+ TransferDiagnostic
24
+ } from '@hydranium/protocol';
11
25
  import {
12
26
  type AstNode,
13
27
  type AstNodeLocator,
28
+ type AstReflection,
14
29
  DefaultDocumentValidator,
15
30
  type DiagnosticInfo,
31
+ DocumentValidator,
16
32
  type LangiumDocument,
17
33
  type LangiumCoreServices,
34
+ type Lexer,
18
35
  type ValidateSingleNodeOptions,
19
36
  type ValidationOptions,
20
37
  type ValidationSeverity
21
38
  } from '@hydranium/langium';
22
39
  import type { CancellationToken } from 'vscode-languageserver-protocol';
23
- import type { Diagnostic } from 'vscode-languageserver-types';
40
+ // A VALUE import, for `Diagnostic.getMessageString`: `message` is
41
+ // `string | MarkupContent` in LSP 3.17+, so upstream's own reader is what
42
+ // narrows it rather than a hand-rolled union check here.
43
+ import { Diagnostic } from 'vscode-languageserver-types';
24
44
  import { type LogNameOptions } from '../diagnostics/logger.js';
25
45
  import { isSyntheticNode } from '../workspace/synthetic.js';
26
46
  import { isVirtualUri } from '../workspace/virtual-document.js';
27
47
 
28
48
  /**
29
- * Diagnostic shape produced by {@link HydraniumDocumentValidator}: an LSP
30
- * {@link Diagnostic} extended with the protocol-level `element` path and
31
- * optional `property` name from {@link TransferDiagnostic}.
49
+ * The AST-layer diagnostic: what the build left on `LangiumDocument`, and what
50
+ * an `AstDocument` carries. An LSP {@link Diagnostic} that MAY also carry the
51
+ * `element` path and `property` name from {@link TransferDiagnostic}.
52
+ *
53
+ * **Not interchangeable with {@link TransferDiagnostic}, which is the other end
54
+ * of the same conversion.** They disagree on field types as well as on which
55
+ * fields exist — `severity` is LSP's numeric enum here and a string union there
56
+ * — so a slot typed with the wrong one still accepts the value and then reads
57
+ * `undefined`, or compares equal to nothing.
58
+ *
59
+ * `element` is optional because a document's diagnostics do not all come from
60
+ * this validator. {@link HydraniumDocumentValidator.toDiagnostic} always sets
61
+ * one, but Langium pushes lexer and parser errors straight onto the document
62
+ * without routing them through it, so a document that fails to parse carries
63
+ * diagnostics with no path at all. Treat it as absent, not empty.
64
+ *
65
+ * Narrowing `LangiumDocument.diagnostics` to this type instead is unavailable:
66
+ * declaration merging may add a member but not retype one, and augmenting the
67
+ * LSP `Diagnostic` reaches only one of the two declaration files its package
68
+ * ships, since the `exports` map splits `import` from `default` with no `types`
69
+ * condition. Consumers therefore cast at the read, and the cast is sound only
70
+ * because this field is optional.
32
71
  */
33
- export interface TransferLspDiagnostic extends Diagnostic {
34
- element: string;
72
+ export interface AstDiagnostic extends Diagnostic {
73
+ element?: string;
35
74
  property?: string;
36
75
  }
37
76
 
77
+ /**
78
+ * A reference resolved to nothing.
79
+ *
80
+ * **The English is byte-identical to Langium's `DefaultLinker.createLinkingError`
81
+ * sentence, and must stay so.** The identity is attached to a diagnostic Langium
82
+ * already worded, so a divergence here would silently change the text every
83
+ * adopter without a catalogue sees. {@link HydraniumDocumentValidator.processLinkingErrors}
84
+ * attaches it only when the two match exactly, which is also what keeps an
85
+ * adopter's own reworded linking error from being mislabelled as this one.
86
+ *
87
+ * It exists because this is the most-seen validation error in any language built
88
+ * on the framework, and Langium puts its own code in `data.code` rather than on
89
+ * `Diagnostic.code` — so without a framework identity every adopter wanting to
90
+ * render it has to special-case Langium's shape.
91
+ */
92
+ export const UNRESOLVED_REFERENCE = defineMessage(
93
+ 'hydranium/core/unresolved-reference',
94
+ "Could not resolve reference to {referenceType} named '{refText}'."
95
+ );
96
+
97
+ /**
98
+ * A character no token of the grammar can start with.
99
+ *
100
+ * **The English is byte-identical to CHEVROTAIN's
101
+ * `defaultLexerErrorProvider.buildUnexpectedCharactersMessage`, and must stay
102
+ * so** — one layer further out than {@link UNRESOLVED_REFERENCE}, whose sentence
103
+ * is Langium's. Langium words neither: `processLexingErrors` copies
104
+ * `lexerDiagnostic.message` through untouched, so the text an adopter sees today
105
+ * is produced two dependencies down and is the only thing this identity may
106
+ * claim to be. {@link HydraniumDocumentValidator.identifyLexingError} attaches
107
+ * it only when the two match exactly, which is also what keeps a custom lexer's
108
+ * own diagnostics — Langium admits any of them through `lexerReport` — from
109
+ * being mislabelled as this one.
110
+ *
111
+ * It exists because a lexing error is the FIRST message a user of a new language
112
+ * sees and the one an adopter cannot reach: every other diagnostic worth
113
+ * translating either carries an identity already or is raised by adopter code,
114
+ * while this one arrives with a Langium `data.code` naming a KIND and no
115
+ * parameters at all — so a catalogue had nothing to key on and nothing to
116
+ * interpolate.
117
+ *
118
+ * `skipped` rather than `length`, matching what the sentence says the number
119
+ * means: chevrotain reports how many characters the lexer discarded to recover,
120
+ * which for a single stray character is one and for a run of them is the run.
121
+ */
122
+ export const UNEXPECTED_CHARACTER = defineMessage(
123
+ 'hydranium/core/unexpected-character',
124
+ 'unexpected character: ->{character}<- at offset: {offset}, skipped {skipped} characters.'
125
+ );
126
+
127
+ /**
128
+ * A token the grammar does not admit at this position.
129
+ *
130
+ * **The English is byte-identical to Langium's
131
+ * `LangiumParserErrorMessageProvider.buildMismatchTokenMessage`, and must stay
132
+ * so**, on the same rule as {@link UNRESOLVED_REFERENCE}.
133
+ *
134
+ * `expected` is a token type name, which is grammar vocabulary rather than
135
+ * translatable text — only the frame around it is. Promoting it into the code
136
+ * instead would make the catalogue as large as the grammar.
137
+ */
138
+ export const UNEXPECTED_TOKEN = defineMessage(
139
+ 'hydranium/core/unexpected-token',
140
+ "Expecting token of type '{expected}' but found `{found}`."
141
+ );
142
+
143
+ /**
144
+ * Input left over once the entry rule had matched.
145
+ *
146
+ * **The English is byte-identical to Langium's
147
+ * `LangiumParserErrorMessageProvider.buildNotAllInputParsedMessage`, and must
148
+ * stay so.**
149
+ */
150
+ export const TRAILING_INPUT = defineMessage('hydranium/core/trailing-input', 'Expecting end of file but found `{found}`.');
151
+
152
+ /**
153
+ * A mode-popping token reached with nothing on the lexer's mode stack.
154
+ *
155
+ * **The English is byte-identical to CHEVROTAIN's
156
+ * `defaultLexerErrorProvider.buildUnableToPopLexerModeMessage`, and must stay
157
+ * so**, the closing clause running on without punctuation exactly as upstream
158
+ * writes it.
159
+ *
160
+ * Reachable only from a multi-mode lexer, which a grammar opts into through a
161
+ * custom token builder.
162
+ */
163
+ export const UNPOPPABLE_LEXER_MODE = defineMessage(
164
+ 'hydranium/core/unpoppable-lexer-mode',
165
+ 'Unable to pop Lexer Mode after encountering Token ->{image}<- The Mode Stack is empty'
166
+ );
167
+
168
+ /**
169
+ * A dedent that lines up with no enclosing indentation level.
170
+ *
171
+ * **The English is byte-identical to LANGIUM's `IndentationAwareTokenBuilder`,
172
+ * and must stay so** — the one lexing sentence Langium words itself rather than
173
+ * relaying from chevrotain, which is why it renders through no
174
+ * `ILexerErrorMessageProvider` and an adopter cannot reach it by replacing one.
175
+ *
176
+ * `stack` is the indentation stack as the template literal upstream stringifies
177
+ * it, comma-joined: structure would have to survive {@link MessageParams}, which
178
+ * holds scalars, and a translation has nothing to say about the numbers anyway.
179
+ */
180
+ export const INVALID_DEDENT = defineMessage(
181
+ 'hydranium/core/invalid-dedent',
182
+ 'Invalid dedent level {level} at offset: {offset}. Current indentation stack: {stack}'
183
+ );
184
+
185
+ /**
186
+ * A position where none of a rule's alternatives can start.
187
+ *
188
+ * **The English is byte-identical to CHEVROTAIN's
189
+ * `defaultParserErrorProvider.buildNoViableAltMessage`, and must stay so** —
190
+ * one layer further out than {@link UNEXPECTED_TOKEN}, because Langium overrides
191
+ * only two of the four parser sentences and passes this one straight through.
192
+ *
193
+ * `sequences` is the token-sequence list chevrotain generated, carried whole
194
+ * rather than as structure: {@link MessageParams} holds scalars, and the list is
195
+ * token names in any case, so only the frame around it is translatable.
196
+ */
197
+ export const NO_VIABLE_ALTERNATIVE = defineMessage(
198
+ 'hydranium/core/no-viable-alternative',
199
+ "Expecting: one of these possible Token sequences:\n{sequences}\nbut found: '{found}'"
200
+ );
201
+
202
+ /**
203
+ * A repetition that had to match at least once and matched nothing.
204
+ *
205
+ * **The English is byte-identical to CHEVROTAIN's
206
+ * `defaultParserErrorProvider.buildEarlyExitMessage`, and must stay so**,
207
+ * including the doubled colon after `sequences::`, which is upstream's and not a
208
+ * typo to repair here — a divergence would silently stop the identity attaching.
209
+ *
210
+ * Named for the empty repetition rather than for chevrotain's `EarlyExit`, which
211
+ * describes its own control flow rather than the reader's problem.
212
+ */
213
+ export const MISSING_ITERATION = defineMessage(
214
+ 'hydranium/core/missing-iteration',
215
+ "Expecting: expecting at least one iteration which starts with one of these possible Token sequences::\n <{sequences}>\nbut found: '{found}'"
216
+ );
217
+
218
+ /**
219
+ * The two sentences whose only free parameter is a generated list, in the order
220
+ * {@link HydraniumDocumentValidator.identifyParsingError} tries them. Order is
221
+ * free: their frames share a prefix but diverge before the list begins, so at
222
+ * most one can match.
223
+ */
224
+ const SEQUENCE_LISTING_MESSAGES = [NO_VIABLE_ALTERNATIVE, MISSING_ITERATION];
225
+
226
+ /**
227
+ * Splits a template into alternating frame and placeholder NAME, for
228
+ * {@link HydraniumDocumentValidator.sliceFramed}. The capturing group is what
229
+ * keeps the names in the result rather than discarding them.
230
+ */
231
+ const PLACEHOLDER_CAPTURE = /\{([^}]+)\}/;
232
+
233
+ /**
234
+ * Langium's own `data.code` values for a diagnostic that came out of the lexer.
235
+ *
236
+ * All four severities, not just the error one: `lexerReport` admits warnings and
237
+ * below, and a token builder that downgrades a stray character still produces
238
+ * the same sentence. Gating on the error code alone would leave the identity off
239
+ * a message that is word-for-word the one it names.
240
+ */
241
+ const LEXING_CODES: ReadonlySet<unknown> = new Set([
242
+ DocumentValidator.LexingError,
243
+ DocumentValidator.LexingWarning,
244
+ DocumentValidator.LexingInfo,
245
+ DocumentValidator.LexingHint
246
+ ]);
247
+
38
248
  export interface DocumentValidatorOptions extends LogNameOptions {
39
249
  /**
40
250
  * Level at which the `validateDocument` timing line is emitted, or `'off'`
@@ -73,7 +283,7 @@ export interface DocumentValidatorOptions extends LogNameOptions {
73
283
  * - **Skip-validation hook** via the virtual {@link shouldSkipValidation}
74
284
  * predicate, which adopters override to compose additional skip reasons.
75
285
  * - **Diagnostic mapping** to {@link TransferDiagnostic}-shaped
76
- * {@link TransferLspDiagnostic}: every emitted diagnostic carries the AST
286
+ * {@link AstDiagnostic}: every emitted diagnostic carries the AST
77
287
  * node's path (built via Langium's `AstNodeLocator`) plus the optional
78
288
  * offending property name.
79
289
  * - **Optional timing wrap** on `validateDocument`, on by default.
@@ -81,6 +291,13 @@ export interface DocumentValidatorOptions extends LogNameOptions {
81
291
  export class HydraniumDocumentValidator extends DefaultDocumentValidator {
82
292
  protected readonly tracer: Tracer;
83
293
  protected readonly astNodeLocator: AstNodeLocator;
294
+ protected readonly reflection: AstReflection;
295
+ protected readonly lexer: Lexer;
296
+ /**
297
+ * {@link tokenTypeNames}' answer. Nothing invalidates it: the vocabulary
298
+ * comes from the grammar, which is fixed once the DI tree is composed.
299
+ */
300
+ protected expectedNames?: readonly string[];
84
301
  protected readonly logLevel: ObservableValue<LogThreshold>;
85
302
  protected readonly logAfterMs: ObservableValue<number>;
86
303
  protected readonly validateVirtualDocuments: ObservableValue<boolean>;
@@ -90,6 +307,8 @@ export class HydraniumDocumentValidator extends DefaultDocumentValidator {
90
307
  super(services);
91
308
  this.tracer = services.shared.Tracer.for(options.logName ?? 'DocumentValidator').trace('instantiated');
92
309
  this.astNodeLocator = services.workspace.AstNodeLocator;
310
+ this.reflection = services.shared.AstReflection;
311
+ this.lexer = services.parser.Lexer;
93
312
  this.logLevel = ObservableValue.from(options.logLevel ?? 'debug');
94
313
  this.logAfterMs = ObservableValue.from(options.logAfterMs ?? 20);
95
314
  this.validateVirtualDocuments = ObservableValue.from(options.validateVirtualDocuments ?? false);
@@ -111,16 +330,239 @@ export class HydraniumDocumentValidator extends DefaultDocumentValidator {
111
330
  }
112
331
  const level = this.logLevel.value;
113
332
  if (level === 'off') {
114
- return super.validateDocument(document, options, cancelToken);
333
+ return this.validateAndIdentify(document, options, cancelToken);
115
334
  }
116
335
  // `level` is narrowed to `LogLevel` past the early-return.
117
336
  return this.tracer
118
337
  .withUri(document.uri.toString())
119
- .time('validateDocument', () => super.validateDocument(document, options, cancelToken), level, {
338
+ .time('validateDocument', () => this.validateAndIdentify(document, options, cancelToken), level, {
120
339
  logAfterMs: this.logAfterMs.value
121
340
  });
122
341
  }
123
342
 
343
+ /**
344
+ * Langium's validation pass, plus the framework identity on the lexing and
345
+ * parsing diagnostics it produced.
346
+ *
347
+ * **Here rather than in an override of `processLexingErrors` /
348
+ * `processParsingErrors`, and the reason is that neither seam can see what
349
+ * the identity needs.** Langium hands them a `ParseResult`, which carries the
350
+ * AST and the error lists but not the source text — and the offending
351
+ * CHARACTER, or the offending TOKEN, is the parameter a translation exists to
352
+ * interpolate. The document does carry it, and this is the innermost point
353
+ * that still holds one.
354
+ *
355
+ * Correlating the finished diagnostics back to the `ParseResult`'s error
356
+ * lists positionally is the alternative, and it is unsound for the parsing
357
+ * half: `processParsingErrors` DROPS an error whose token offsets are `NaN`
358
+ * and whose exception carries no `previousToken`, so the two lists differ in
359
+ * length exactly when recovery has been at work.
360
+ *
361
+ * Inside the timing span rather than around it, so the line accounts for the
362
+ * whole pass.
363
+ */
364
+ protected async validateAndIdentify(
365
+ document: LangiumDocument,
366
+ options?: ValidationOptions,
367
+ cancelToken?: CancellationToken
368
+ ): Promise<Diagnostic[]> {
369
+ const diagnostics = await super.validateDocument(document, options, cancelToken);
370
+ // Mapped in place over the array Langium built, rather than filtered and
371
+ // re-concatenated: the publish order is the diagnostic order and a
372
+ // reordering would move a squiggle's entry in the problems list.
373
+ // Each pass guards on its own `data.code`, so at most one can claim a
374
+ // given diagnostic and the order between them carries no meaning.
375
+ for (let index = 0; index < diagnostics.length; index++) {
376
+ diagnostics[index] = this.identifyLexingError(document, diagnostics[index]);
377
+ diagnostics[index] = this.identifyParsingError(document, diagnostics[index]);
378
+ }
379
+ return diagnostics;
380
+ }
381
+
382
+ /**
383
+ * Add whichever lexing identity renders `diagnostic`'s sentence, or return it
384
+ * untouched when none of them does — a custom lexer's own report, which
385
+ * Langium admits through `lexerReport`, being the case that reaches the end.
386
+ *
387
+ * The parameters come from the diagnostic's own RANGE, not from
388
+ * `parseResult.lexerErrors`. Both hold the same numbers, and the range is the
389
+ * one that needs no assumption about upstream: correlating the two lists
390
+ * would depend on Langium appending one diagnostic per lexer error in order,
391
+ * which is true today and is an internal of the method being wrapped.
392
+ *
393
+ * **The format-and-compare is the whole discriminator, and it validates the
394
+ * reconstruction as well as the identity.** A range that did not round-trip
395
+ * to the offset chevrotain reported would produce a different sentence and be
396
+ * declined, so a wrong parameter set can never be attached to a right-looking
397
+ * message.
398
+ */
399
+ protected identifyLexingError(document: LangiumDocument, diagnostic: Diagnostic): Diagnostic {
400
+ const data = diagnostic.data as { code?: unknown } | undefined;
401
+ if (!LEXING_CODES.has(data?.code)) {
402
+ return diagnostic;
403
+ }
404
+ const text = document.textDocument;
405
+ const message = Diagnostic.getMessageString(diagnostic);
406
+ const offset = text.offsetAt(diagnostic.range.start);
407
+ const stray = {
408
+ character: text.getText().charAt(offset),
409
+ offset,
410
+ skipped: text.offsetAt(diagnostic.range.end) - offset
411
+ };
412
+ if (message === UNEXPECTED_CHARACTER.format(stray)) {
413
+ return this.identified(diagnostic, UNEXPECTED_CHARACTER, stray);
414
+ }
415
+ // The mode-pop error's range spans the offending token exactly, upstream
416
+ // reporting its `startOffset` with the image's own length.
417
+ const image = text.getText(diagnostic.range);
418
+ if (message === UNPOPPABLE_LEXER_MODE.format({ image })) {
419
+ return this.identified(diagnostic, UNPOPPABLE_LEXER_MODE, { image });
420
+ }
421
+ // `level` and `stack` are lexer state that reaches no field here, so they
422
+ // are sliced; `offset` is given, which is what anchors the slice.
423
+ const dedent = this.sliceFramed(message, INVALID_DEDENT, { offset });
424
+ return dedent === undefined ? diagnostic : this.identified(diagnostic, INVALID_DEDENT, dedent);
425
+ }
426
+
427
+ /**
428
+ * Add whichever parser identity renders `diagnostic`'s sentence, or return it
429
+ * untouched when none of them does — an adopter that replaced the parser
430
+ * error-message provider being the case that reaches the last branch.
431
+ *
432
+ * `found` comes from the diagnostic's own RANGE, for the same reason
433
+ * {@link identifyLexingError} reads its parameters there. A token whose
434
+ * offsets chevrotain could not report collapses to a zero-width range, which
435
+ * yields the empty string the sentence already renders for it.
436
+ *
437
+ * **`expected` is recovered by SEARCHING the vocabulary, because nothing
438
+ * carries it.** Chevrotain hands the expected token type to the message
439
+ * provider and keeps it on neither the exception nor the diagnostic, so the
440
+ * only structured source left is the grammar's own vocabulary: every token
441
+ * type is offered to the same `format` the identity renders with, and the one
442
+ * that reproduces the message supplies the parameter. Matching it out of the
443
+ * prose instead would derive a parameter from a sentence that a Langium
444
+ * reword can change without warning. Two distinct names cannot render one
445
+ * sentence, so the match is unique where it exists.
446
+ */
447
+ protected identifyParsingError(document: LangiumDocument, diagnostic: Diagnostic): Diagnostic {
448
+ const data = diagnostic.data as { code?: unknown } | undefined;
449
+ if (data?.code !== DocumentValidator.ParsingError) {
450
+ return diagnostic;
451
+ }
452
+ const message = Diagnostic.getMessageString(diagnostic);
453
+ const found = document.textDocument.getText(diagnostic.range);
454
+ if (message === TRAILING_INPUT.format({ found })) {
455
+ return this.identified(diagnostic, TRAILING_INPUT, { found });
456
+ }
457
+ const expected = this.tokenTypeNames().find(name => UNEXPECTED_TOKEN.format({ expected: name, found }) === message);
458
+ if (expected !== undefined) {
459
+ return this.identified(diagnostic, UNEXPECTED_TOKEN, { expected, found });
460
+ }
461
+ for (const definition of SEQUENCE_LISTING_MESSAGES) {
462
+ const params = this.sliceFramed(message, definition, { found });
463
+ if (params !== undefined) {
464
+ return this.identified(diagnostic, definition, params);
465
+ }
466
+ }
467
+ return diagnostic;
468
+ }
469
+
470
+ /** `diagnostic` carrying `definition`'s identity, over whatever `data` it already had. */
471
+ protected identified<S extends string>(diagnostic: Diagnostic, definition: MessageDefinition<S>, ...params: ParamsArg<S>): Diagnostic {
472
+ // Merged OVER the existing data, so Langium's `code` survives for the
473
+ // readers that switch on it — `stopAfterParsingErrors` is one, and the
474
+ // GLSP head's read-only decision is another.
475
+ const data = diagnostic.data as object | undefined;
476
+ return { ...diagnostic, code: definition.code, data: { ...data, ...messageData(definition, ...params) } };
477
+ }
478
+
479
+ /**
480
+ * `definition`'s parameters as they appear in `message`, taking the ones in
481
+ * `known` as given, or `undefined` when `message` is not that sentence.
482
+ *
483
+ * **The only parameters in this file taken OUT of the prose rather than
484
+ * derived and checked against it, and the bound on that is what `known`
485
+ * is for.** A generated token-sequence list, a lexer's indentation stack: for
486
+ * these there is no finite candidate set to offer to `format` the way a token
487
+ * type name can be, and no structured field carries them. What keeps the read
488
+ * honest is that everything else in the sentence is either a fixed literal or
489
+ * a parameter the caller derived independently, so the frame either matches
490
+ * exactly or the sentence is declined.
491
+ *
492
+ * **The trailing format-and-compare is load-bearing once more than one
493
+ * parameter is unknown.** Each is captured up to the NEXT fixed separator, so
494
+ * a separator that also occurs inside a value would cut early — re-rendering
495
+ * catches that, where for a single unknown between two affixes it could not
496
+ * fail. Two adjacent placeholders are declined outright: nothing marks the
497
+ * boundary, so no capture is recoverable.
498
+ *
499
+ * The frame is split off the declaration's own `text`, so it cannot drift
500
+ * from the sentence `format` renders.
501
+ */
502
+ protected sliceFramed<S extends string>(
503
+ message: string,
504
+ definition: MessageDefinition<S>,
505
+ known: MessageParams
506
+ ): ParamsOf<S> | undefined {
507
+ // `split` with a capturing group interleaves literals and placeholder
508
+ // names, so even indices are frame and odd ones are parameters.
509
+ const parts = definition.text.split(PLACEHOLDER_CAPTURE);
510
+ const params: Record<string, string | number> = { ...known };
511
+ let cursor = 0;
512
+ for (let index = 0; index < parts.length; index += 2) {
513
+ const literal = parts[index];
514
+ if (!message.startsWith(literal, cursor)) {
515
+ return undefined;
516
+ }
517
+ cursor += literal.length;
518
+ const name = parts[index + 1];
519
+ if (name === undefined) {
520
+ break;
521
+ }
522
+ const given = params[name];
523
+ if (given !== undefined) {
524
+ const rendered = String(given);
525
+ if (!message.startsWith(rendered, cursor)) {
526
+ return undefined;
527
+ }
528
+ cursor += rendered.length;
529
+ continue;
530
+ }
531
+ const separator = parts[index + 2];
532
+ const trailing = parts[index + 3] === undefined;
533
+ if (separator === '' && !trailing) {
534
+ return undefined;
535
+ }
536
+ const at = trailing && separator === '' ? message.length : message.indexOf(separator, cursor);
537
+ if (at < cursor) {
538
+ return undefined;
539
+ }
540
+ params[name] = message.slice(cursor, at);
541
+ cursor = at;
542
+ }
543
+ if (cursor !== message.length || interpolate(definition.text, params) !== message) {
544
+ return undefined;
545
+ }
546
+ // Narrowing only: every placeholder the template names was either given or
547
+ // captured above, which is precisely what `ParamsOf` requires.
548
+ return params as ParamsOf<S>;
549
+ }
550
+
551
+ /**
552
+ * Every token type name the grammar declares, as
553
+ * {@link identifyParsingError} candidates.
554
+ *
555
+ * Read off the lexer, whose `definition` the `Lexer` INTERFACE declares —
556
+ * the parser holds the same vocabulary only behind Langium's default class,
557
+ * where an adopter's replacement need not keep it. A token type the parser
558
+ * expects but the lexer does not declare yields no match, which declines the
559
+ * identity rather than attaching a wrong one.
560
+ */
561
+ protected tokenTypeNames(): readonly string[] {
562
+ this.expectedNames ??= Object.keys(this.lexer.definition);
563
+ return this.expectedNames;
564
+ }
565
+
124
566
  /**
125
567
  * Skip validation for a node **and its children** when
126
568
  * {@link shouldSkipValidation} returns true.
@@ -144,11 +586,95 @@ export class HydraniumDocumentValidator extends DefaultDocumentValidator {
144
586
  return !this.validateSyntheticNodes.value && isSyntheticNode(node);
145
587
  }
146
588
 
589
+ /**
590
+ * Langium's linking-error pass, plus the framework identity on each
591
+ * diagnostic it produced.
592
+ *
593
+ * Langium puts its own `linking-error` marker in `data.code` and leaves
594
+ * `Diagnostic.code` unset, so nothing identifies the message on a surface
595
+ * that drops `data` — which is every editor surface. This adds the framework
596
+ * identity to both `code` and `data.hydranium` while LEAVING `data.code` in
597
+ * place: `stopAfterLinkingErrors` and Langium's code-action dispatch both
598
+ * read it.
599
+ *
600
+ * **The identity is attached only when the message is the one
601
+ * {@link UNRESOLVED_REFERENCE} renders.** Two other sentences reach this
602
+ * list: the linker's exception form, raised when resolution itself throws,
603
+ * and whatever an adopter overriding `createLinkingError` chose. Labelling
604
+ * either would make a catalogue render the wrong sentence — and for the
605
+ * exception form it would discard the underlying cause. Comparing against
606
+ * the locally rendered English is what discriminates; there is no structural
607
+ * field that does.
608
+ */
609
+ protected override processLinkingErrors(document: LangiumDocument, diagnostics: Diagnostic[], options: ValidationOptions): void {
610
+ const from = diagnostics.length;
611
+ super.processLinkingErrors(document, diagnostics, options);
612
+ for (let index = from; index < diagnostics.length; index++) {
613
+ diagnostics[index] = this.identifyLinkingError(diagnostics[index]);
614
+ }
615
+ }
616
+
617
+ /**
618
+ * Add the {@link UNRESOLVED_REFERENCE} identity to one linking diagnostic,
619
+ * or return it untouched when its message is not the sentence that identity
620
+ * renders.
621
+ *
622
+ * `refText` comes from the `data` Langium populated; `referenceType` is not
623
+ * in it, so it is recovered the same way `createLinkingError` produced it —
624
+ * through the reflection, from the container type and property.
625
+ */
626
+ protected identifyLinkingError(diagnostic: Diagnostic): Diagnostic {
627
+ const data = diagnostic.data as { code?: unknown; refText?: unknown; containerType?: unknown; property?: unknown } | undefined;
628
+ if (typeof data?.refText !== 'string' || typeof data.containerType !== 'string' || typeof data.property !== 'string') {
629
+ return diagnostic;
630
+ }
631
+ const referenceType = this.referenceTypeOf(data.containerType, data.property, data.refText);
632
+ if (referenceType === undefined) {
633
+ return diagnostic;
634
+ }
635
+ const params = { referenceType, refText: data.refText };
636
+ if (diagnostic.message !== UNRESOLVED_REFERENCE.format(params)) {
637
+ return diagnostic;
638
+ }
639
+ // Merged OVER Langium's data rather than replacing it, so `data.code`
640
+ // survives for the readers that switch on it.
641
+ return { ...diagnostic, code: UNRESOLVED_REFERENCE.code, data: { ...data, ...messageData(UNRESOLVED_REFERENCE, params) } };
642
+ }
643
+
644
+ /**
645
+ * The declared target type of the reference this diagnostic came from, or
646
+ * `undefined` when the reflection cannot name one.
647
+ *
648
+ * **The lookup can throw, and that is the reason this is a method rather
649
+ * than an inline call.** `AbstractAstReflection.getReferenceType` raises on
650
+ * an unknown container `$type` and on a property that is not a reference —
651
+ * and the linking error whose type it cannot name is the one most likely to
652
+ * reach here, because `DefaultLinker` catches its OWN failed lookup and
653
+ * turns it into the exception-form message. Rethrowing would take
654
+ * `validateDocument` with it, since Langium wraps `processLinkingErrors` in
655
+ * no try: a diagnostic Langium degraded gracefully would become a failed
656
+ * build. Declining is also the right answer on the merits — a reference
657
+ * whose type cannot be named is not the message
658
+ * {@link UNRESOLVED_REFERENCE} claims.
659
+ */
660
+ protected referenceTypeOf(containerType: string, property: string, refText: string): string | undefined {
661
+ try {
662
+ return this.reflection.getReferenceType({
663
+ container: { $type: containerType } as AstNode,
664
+ property,
665
+ reference: { $refText: refText }
666
+ } as Parameters<AstReflection['getReferenceType']>[0]);
667
+ } catch (err: unknown) {
668
+ this.tracer.debug(`cannot name the reference type for ${containerType}.${property}: ${describeError(err)}`);
669
+ return undefined;
670
+ }
671
+ }
672
+
147
673
  protected override toDiagnostic<N extends AstNode>(
148
674
  severity: ValidationSeverity,
149
675
  message: string,
150
676
  info: DiagnosticInfo<N, string>
151
- ): TransferLspDiagnostic {
677
+ ): AstDiagnostic {
152
678
  const base = super.toDiagnostic(severity, message, info);
153
679
  const node = info.node;
154
680
  if (!node) {
@@ -105,10 +105,9 @@ export class DefaultDocumentUriPolicy implements DocumentUriPolicy {
105
105
  /**
106
106
  * No filesystem access, so existence cannot be checked — every URI is
107
107
  * treated as loadable and returned unchanged. A later load that misses
108
- * therefore reaches the filesystem and throws, which
109
- * `AbstractHydraniumLangiumDocuments.getOrCreateDocument` absorbs into its
110
- * empty-document fallback: under this policy that fallback, not this method,
111
- * is what makes a missing file a placeholder rather than an error.
108
+ * therefore reaches the filesystem and throws, which reports the miss with
109
+ * the reason the read failed. Under a policy that CAN check, the miss is
110
+ * caught earlier and reported without one.
112
111
  */
113
112
  loadUri(uri: URI | string): URI | undefined {
114
113
  return UriUtils.toUri(uri);