@hydranium/core 1.0.0-next.39 → 1.0.0-next.391

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 (805) hide show
  1. package/README.md +78 -82
  2. package/lib/browser/host-heads-in-worker.d.ts +77 -0
  3. package/lib/browser/host-heads-in-worker.d.ts.map +1 -0
  4. package/lib/browser/host-heads-in-worker.js +169 -0
  5. package/lib/browser/host-heads-in-worker.js.map +1 -0
  6. package/lib/browser/index.d.ts +12 -0
  7. package/lib/browser/index.d.ts.map +1 -0
  8. package/lib/browser/index.js +17 -0
  9. package/lib/browser/index.js.map +1 -0
  10. package/lib/browser/indexeddb-file-system-store.d.ts +82 -0
  11. package/lib/browser/indexeddb-file-system-store.d.ts.map +1 -0
  12. package/lib/browser/indexeddb-file-system-store.js +181 -0
  13. package/lib/browser/indexeddb-file-system-store.js.map +1 -0
  14. package/lib/browser/start-language-server-in-worker.d.ts +32 -0
  15. package/lib/browser/start-language-server-in-worker.d.ts.map +1 -0
  16. package/lib/browser/start-language-server-in-worker.js +40 -0
  17. package/lib/browser/start-language-server-in-worker.js.map +1 -0
  18. package/lib/documents/ast-document-manager.d.ts +254 -140
  19. package/lib/documents/ast-document-manager.d.ts.map +1 -1
  20. package/lib/documents/ast-document-manager.js +311 -128
  21. package/lib/documents/ast-document-manager.js.map +1 -1
  22. package/lib/documents/client-ids.d.ts +5 -19
  23. package/lib/documents/client-ids.d.ts.map +1 -1
  24. package/lib/documents/client-ids.js +5 -19
  25. package/lib/documents/client-ids.js.map +1 -1
  26. package/lib/documents/client-session-errors.d.ts +15 -0
  27. package/lib/documents/client-session-errors.d.ts.map +1 -0
  28. package/lib/documents/client-session-errors.js +15 -0
  29. package/lib/documents/client-session-errors.js.map +1 -0
  30. package/lib/documents/client-session-registry.d.ts +146 -0
  31. package/lib/documents/client-session-registry.d.ts.map +1 -0
  32. package/lib/documents/client-session-registry.js +207 -0
  33. package/lib/documents/client-session-registry.js.map +1 -0
  34. package/lib/documents/dirty-state-tracker.d.ts +101 -0
  35. package/lib/documents/dirty-state-tracker.d.ts.map +1 -0
  36. package/lib/documents/dirty-state-tracker.js +85 -0
  37. package/lib/documents/dirty-state-tracker.js.map +1 -0
  38. package/lib/documents/document-release-handler.d.ts +204 -0
  39. package/lib/documents/document-release-handler.d.ts.map +1 -0
  40. package/lib/documents/document-release-handler.js +317 -0
  41. package/lib/documents/document-release-handler.js.map +1 -0
  42. package/lib/documents/document-release-scheduler.d.ts +67 -0
  43. package/lib/documents/document-release-scheduler.d.ts.map +1 -0
  44. package/lib/documents/document-release-scheduler.js +80 -0
  45. package/lib/documents/document-release-scheduler.js.map +1 -0
  46. package/lib/documents/file-system-task-queue.d.ts +52 -0
  47. package/lib/documents/file-system-task-queue.d.ts.map +1 -0
  48. package/lib/documents/file-system-task-queue.js +40 -0
  49. package/lib/documents/file-system-task-queue.js.map +1 -0
  50. package/lib/documents/hydranium-text-documents.d.ts +453 -282
  51. package/lib/documents/hydranium-text-documents.d.ts.map +1 -1
  52. package/lib/documents/hydranium-text-documents.js +918 -504
  53. package/lib/documents/hydranium-text-documents.js.map +1 -1
  54. package/lib/documents/index.d.ts +10 -2
  55. package/lib/documents/index.d.ts.map +1 -1
  56. package/lib/documents/index.js +10 -2
  57. package/lib/documents/index.js.map +1 -1
  58. package/lib/documents/language-client-shadow.d.ts +234 -0
  59. package/lib/documents/language-client-shadow.d.ts.map +1 -0
  60. package/lib/documents/language-client-shadow.js +306 -0
  61. package/lib/documents/language-client-shadow.js.map +1 -0
  62. package/lib/documents/model-ledger.d.ts +47 -0
  63. package/lib/documents/model-ledger.d.ts.map +1 -0
  64. package/lib/documents/model-ledger.js +30 -0
  65. package/lib/documents/model-ledger.js.map +1 -0
  66. package/lib/documents/text-ledger.d.ts +64 -0
  67. package/lib/documents/text-ledger.d.ts.map +1 -0
  68. package/lib/documents/text-ledger.js +63 -0
  69. package/lib/documents/text-ledger.js.map +1 -0
  70. package/lib/documents/version-sync-service.d.ts +153 -0
  71. package/lib/documents/version-sync-service.d.ts.map +1 -0
  72. package/lib/documents/version-sync-service.js +186 -0
  73. package/lib/documents/version-sync-service.js.map +1 -0
  74. package/lib/index.d.ts +3 -0
  75. package/lib/index.d.ts.map +1 -1
  76. package/lib/index.js +3 -0
  77. package/lib/index.js.map +1 -1
  78. package/lib/langium/ast-extension/ast-extension-service.d.ts +42 -31
  79. package/lib/langium/ast-extension/ast-extension-service.d.ts.map +1 -1
  80. package/lib/langium/ast-extension/ast-extension-service.js +45 -19
  81. package/lib/langium/ast-extension/ast-extension-service.js.map +1 -1
  82. package/lib/langium/ast-extension/ast-node-builder.d.ts +12 -7
  83. package/lib/langium/ast-extension/ast-node-builder.d.ts.map +1 -1
  84. package/lib/langium/ast-extension/ast-node-builder.js +25 -9
  85. package/lib/langium/ast-extension/ast-node-builder.js.map +1 -1
  86. package/lib/langium/bootstrap.d.ts +49 -36
  87. package/lib/langium/bootstrap.d.ts.map +1 -1
  88. package/lib/langium/bootstrap.js +75 -36
  89. package/lib/langium/bootstrap.js.map +1 -1
  90. package/lib/langium/build-phase-pass/build-phase-pass-service.d.ts +9 -9
  91. package/lib/langium/build-phase-pass/build-phase-pass-service.d.ts.map +1 -1
  92. package/lib/langium/build-phase-pass/build-phase-pass-service.js +25 -16
  93. package/lib/langium/build-phase-pass/build-phase-pass-service.js.map +1 -1
  94. package/lib/langium/build-phase-pass/build-phase-pass.d.ts +20 -8
  95. package/lib/langium/build-phase-pass/build-phase-pass.d.ts.map +1 -1
  96. package/lib/langium/build-phase-pass/build-phase-pass.js +20 -0
  97. package/lib/langium/build-phase-pass/build-phase-pass.js.map +1 -1
  98. package/lib/langium/composite-ast-reflection.d.ts +46 -0
  99. package/lib/langium/composite-ast-reflection.d.ts.map +1 -0
  100. package/lib/langium/composite-ast-reflection.js +97 -0
  101. package/lib/langium/composite-ast-reflection.js.map +1 -0
  102. package/lib/langium/config/configuration-provider.d.ts +81 -0
  103. package/lib/langium/config/configuration-provider.d.ts.map +1 -0
  104. package/lib/langium/config/configuration-provider.js +112 -0
  105. package/lib/langium/config/configuration-provider.js.map +1 -0
  106. package/lib/langium/config/index.d.ts +1 -0
  107. package/lib/langium/config/index.d.ts.map +1 -1
  108. package/lib/langium/config/index.js +1 -0
  109. package/lib/langium/config/index.js.map +1 -1
  110. package/lib/langium/config/settings.js.map +1 -1
  111. package/lib/langium/diagnostics/console-tee.d.ts +27 -0
  112. package/lib/langium/diagnostics/console-tee.d.ts.map +1 -0
  113. package/lib/langium/diagnostics/console-tee.js +141 -0
  114. package/lib/langium/diagnostics/console-tee.js.map +1 -0
  115. package/lib/langium/diagnostics/hydranium-langium-profiler.d.ts +9 -20
  116. package/lib/langium/diagnostics/hydranium-langium-profiler.d.ts.map +1 -1
  117. package/lib/langium/diagnostics/hydranium-langium-profiler.js +15 -24
  118. package/lib/langium/diagnostics/hydranium-langium-profiler.js.map +1 -1
  119. package/lib/langium/diagnostics/log-preamble.js +1 -1
  120. package/lib/langium/diagnostics/log-preamble.js.map +1 -1
  121. package/lib/langium/diagnostics/logger.d.ts +25 -6
  122. package/lib/langium/diagnostics/logger.d.ts.map +1 -1
  123. package/lib/langium/diagnostics/logger.js +49 -17
  124. package/lib/langium/diagnostics/logger.js.map +1 -1
  125. package/lib/langium/diagnostics/lsp-logger.d.ts +25 -0
  126. package/lib/langium/diagnostics/lsp-logger.d.ts.map +1 -1
  127. package/lib/langium/diagnostics/lsp-logger.js +81 -46
  128. package/lib/langium/diagnostics/lsp-logger.js.map +1 -1
  129. package/lib/langium/diagnostics/server-tracer.d.ts.map +1 -1
  130. package/lib/langium/diagnostics/server-tracer.js.map +1 -1
  131. package/lib/langium/document-builder/build-pipeline-integration.d.ts +21 -1
  132. package/lib/langium/document-builder/build-pipeline-integration.d.ts.map +1 -1
  133. package/lib/langium/document-builder/build-pipeline-integration.js +43 -30
  134. package/lib/langium/document-builder/build-pipeline-integration.js.map +1 -1
  135. package/lib/langium/document-builder/build-session.d.ts +6 -5
  136. package/lib/langium/document-builder/build-session.d.ts.map +1 -1
  137. package/lib/langium/document-builder/build-session.js +5 -5
  138. package/lib/langium/document-builder/build-session.js.map +1 -1
  139. package/lib/langium/document-builder/document-builder.d.ts +390 -40
  140. package/lib/langium/document-builder/document-builder.d.ts.map +1 -1
  141. package/lib/langium/document-builder/document-builder.js +635 -102
  142. package/lib/langium/document-builder/document-builder.js.map +1 -1
  143. package/lib/langium/documentation/comment-provider.d.ts.map +1 -1
  144. package/lib/langium/documentation/comment-provider.js.map +1 -1
  145. package/lib/langium/integration-services.d.ts +20 -23
  146. package/lib/langium/integration-services.d.ts.map +1 -1
  147. package/lib/langium/integration-services.js.map +1 -1
  148. package/lib/langium/integrity/integrity-rule.d.ts +22 -8
  149. package/lib/langium/integrity/integrity-rule.d.ts.map +1 -1
  150. package/lib/langium/integrity/integrity-rule.js.map +1 -1
  151. package/lib/langium/integrity/integrity-service.d.ts +217 -29
  152. package/lib/langium/integrity/integrity-service.d.ts.map +1 -1
  153. package/lib/langium/integrity/integrity-service.js +588 -84
  154. package/lib/langium/integrity/integrity-service.js.map +1 -1
  155. package/lib/langium/keys/containment.d.ts +3 -2
  156. package/lib/langium/keys/containment.d.ts.map +1 -1
  157. package/lib/langium/keys/containment.js.map +1 -1
  158. package/lib/langium/keys/name-based-key-provider.d.ts +17 -10
  159. package/lib/langium/keys/name-based-key-provider.d.ts.map +1 -1
  160. package/lib/langium/keys/name-based-key-provider.js +24 -12
  161. package/lib/langium/keys/name-based-key-provider.js.map +1 -1
  162. package/lib/langium/keys/positional-key-provider.d.ts.map +1 -1
  163. package/lib/langium/keys/positional-key-provider.js.map +1 -1
  164. package/lib/langium/labeling/label-provider.d.ts.map +1 -1
  165. package/lib/langium/labeling/label-provider.js.map +1 -1
  166. package/lib/langium/language-module.d.ts +30 -5
  167. package/lib/langium/language-module.d.ts.map +1 -1
  168. package/lib/langium/language-module.js +21 -5
  169. package/lib/langium/language-module.js.map +1 -1
  170. package/lib/langium/model-service/client-session.d.ts +405 -0
  171. package/lib/langium/model-service/client-session.d.ts.map +1 -0
  172. package/lib/langium/model-service/client-session.js +479 -0
  173. package/lib/langium/model-service/client-session.js.map +1 -0
  174. package/lib/langium/model-service/index.d.ts +2 -0
  175. package/lib/langium/model-service/index.d.ts.map +1 -1
  176. package/lib/langium/model-service/index.js +2 -0
  177. package/lib/langium/model-service/index.js.map +1 -1
  178. package/lib/langium/model-service/model-events.d.ts +68 -0
  179. package/lib/langium/model-service/model-events.d.ts.map +1 -0
  180. package/lib/langium/model-service/model-events.js +10 -0
  181. package/lib/langium/model-service/model-events.js.map +1 -0
  182. package/lib/langium/model-service/model-service.d.ts +441 -323
  183. package/lib/langium/model-service/model-service.d.ts.map +1 -1
  184. package/lib/langium/model-service/model-service.js +602 -407
  185. package/lib/langium/model-service/model-service.js.map +1 -1
  186. package/lib/langium/module.d.ts +96 -37
  187. package/lib/langium/module.d.ts.map +1 -1
  188. package/lib/langium/module.js +41 -29
  189. package/lib/langium/module.js.map +1 -1
  190. package/lib/langium/naming/name-provider.d.ts +32 -7
  191. package/lib/langium/naming/name-provider.d.ts.map +1 -1
  192. package/lib/langium/naming/name-provider.js +10 -4
  193. package/lib/langium/naming/name-provider.js.map +1 -1
  194. package/lib/langium/naming/name-separator-validation.d.ts.map +1 -1
  195. package/lib/langium/project/abstract-project-manager.d.ts +184 -13
  196. package/lib/langium/project/abstract-project-manager.d.ts.map +1 -1
  197. package/lib/langium/project/abstract-project-manager.js +336 -16
  198. package/lib/langium/project/abstract-project-manager.js.map +1 -1
  199. package/lib/langium/project/project-change-event.d.ts +22 -3
  200. package/lib/langium/project/project-change-event.d.ts.map +1 -1
  201. package/lib/langium/project/project-manager.d.ts +28 -2
  202. package/lib/langium/project/project-manager.d.ts.map +1 -1
  203. package/lib/langium/project/single-project-manager.d.ts +7 -1
  204. package/lib/langium/project/single-project-manager.d.ts.map +1 -1
  205. package/lib/langium/project/single-project-manager.js +12 -0
  206. package/lib/langium/project/single-project-manager.js.map +1 -1
  207. package/lib/langium/residency/cst-residency-service.d.ts +92 -52
  208. package/lib/langium/residency/cst-residency-service.d.ts.map +1 -1
  209. package/lib/langium/residency/cst-residency-service.js +154 -58
  210. package/lib/langium/residency/cst-residency-service.js.map +1 -1
  211. package/lib/langium/scope/ast-node-description-provider.d.ts +46 -10
  212. package/lib/langium/scope/ast-node-description-provider.d.ts.map +1 -1
  213. package/lib/langium/scope/ast-node-description-provider.js +88 -15
  214. package/lib/langium/scope/ast-node-description-provider.js.map +1 -1
  215. package/lib/langium/scope/built-local-symbols.d.ts +12 -0
  216. package/lib/langium/scope/built-local-symbols.d.ts.map +1 -0
  217. package/lib/langium/scope/built-local-symbols.js +11 -0
  218. package/lib/langium/scope/built-local-symbols.js.map +1 -0
  219. package/lib/langium/scope/fallback-scope.d.ts +27 -0
  220. package/lib/langium/scope/fallback-scope.d.ts.map +1 -0
  221. package/lib/langium/scope/fallback-scope.js +35 -0
  222. package/lib/langium/scope/fallback-scope.js.map +1 -0
  223. package/lib/langium/scope/hydranium-scope-computation.d.ts +21 -18
  224. package/lib/langium/scope/hydranium-scope-computation.d.ts.map +1 -1
  225. package/lib/langium/scope/hydranium-scope-computation.js +38 -39
  226. package/lib/langium/scope/hydranium-scope-computation.js.map +1 -1
  227. package/lib/langium/scope/hydranium-scope-provider.d.ts +122 -32
  228. package/lib/langium/scope/hydranium-scope-provider.d.ts.map +1 -1
  229. package/lib/langium/scope/hydranium-scope-provider.js +217 -39
  230. package/lib/langium/scope/hydranium-scope-provider.js.map +1 -1
  231. package/lib/langium/scope/index.d.ts +1 -0
  232. package/lib/langium/scope/index.d.ts.map +1 -1
  233. package/lib/langium/scope/index.js +1 -0
  234. package/lib/langium/scope/index.js.map +1 -1
  235. package/lib/langium/scope/reference-builder.d.ts.map +1 -1
  236. package/lib/langium/scope/reference-builder.js.map +1 -1
  237. package/lib/langium/scope/reference-candidate-provider.d.ts +6 -5
  238. package/lib/langium/scope/reference-candidate-provider.d.ts.map +1 -1
  239. package/lib/langium/scope/reference-candidate-provider.js +3 -2
  240. package/lib/langium/scope/reference-candidate-provider.js.map +1 -1
  241. package/lib/langium/scope/scope-extension-service.d.ts +69 -37
  242. package/lib/langium/scope/scope-extension-service.d.ts.map +1 -1
  243. package/lib/langium/scope/scope-extension-service.js +102 -29
  244. package/lib/langium/scope/scope-extension-service.js.map +1 -1
  245. package/lib/langium/scope/scoped-ast-node-description.d.ts +1 -3
  246. package/lib/langium/scope/scoped-ast-node-description.d.ts.map +1 -1
  247. package/lib/langium/scope/scoped-ast-node-description.js +1 -3
  248. package/lib/langium/scope/scoped-ast-node-description.js.map +1 -1
  249. package/lib/langium/serialization/abstract-serializer.d.ts +38 -27
  250. package/lib/langium/serialization/abstract-serializer.d.ts.map +1 -1
  251. package/lib/langium/serialization/abstract-serializer.js +54 -30
  252. package/lib/langium/serialization/abstract-serializer.js.map +1 -1
  253. package/lib/langium/serialization/json-serializer.d.ts +8 -1
  254. package/lib/langium/serialization/json-serializer.d.ts.map +1 -1
  255. package/lib/langium/serialization/json-serializer.js +13 -3
  256. package/lib/langium/serialization/json-serializer.js.map +1 -1
  257. package/lib/langium/serialization/serializer.d.ts +1 -1
  258. package/lib/langium/serialization/yaml-serializer.d.ts +12 -0
  259. package/lib/langium/serialization/yaml-serializer.d.ts.map +1 -1
  260. package/lib/langium/serialization/yaml-serializer.js +24 -4
  261. package/lib/langium/serialization/yaml-serializer.js.map +1 -1
  262. package/lib/langium/service-registry.d.ts +29 -1
  263. package/lib/langium/service-registry.d.ts.map +1 -1
  264. package/lib/langium/service-registry.js +37 -0
  265. package/lib/langium/service-registry.js.map +1 -1
  266. package/lib/langium/shared-services.d.ts +10 -4
  267. package/lib/langium/shared-services.d.ts.map +1 -1
  268. package/lib/langium/shared-services.js.map +1 -1
  269. package/lib/langium/single-langium-copy.d.ts +22 -0
  270. package/lib/langium/single-langium-copy.d.ts.map +1 -0
  271. package/lib/langium/single-langium-copy.js +36 -0
  272. package/lib/langium/single-langium-copy.js.map +1 -0
  273. package/lib/langium/transfer/transfer-encoder.d.ts +60 -39
  274. package/lib/langium/transfer/transfer-encoder.d.ts.map +1 -1
  275. package/lib/langium/transfer/transfer-encoder.js +35 -24
  276. package/lib/langium/transfer/transfer-encoder.js.map +1 -1
  277. package/lib/langium/trivia/comment-preserver.d.ts +543 -0
  278. package/lib/langium/trivia/comment-preserver.d.ts.map +1 -0
  279. package/lib/langium/trivia/comment-preserver.js +1165 -0
  280. package/lib/langium/trivia/comment-preserver.js.map +1 -0
  281. package/lib/langium/trivia/document-ending-preserver.d.ts +51 -0
  282. package/lib/langium/trivia/document-ending-preserver.d.ts.map +1 -0
  283. package/lib/langium/trivia/document-ending-preserver.js +62 -0
  284. package/lib/langium/trivia/document-ending-preserver.js.map +1 -0
  285. package/lib/langium/trivia/index.d.ts +14 -0
  286. package/lib/langium/trivia/index.d.ts.map +1 -0
  287. package/lib/langium/trivia/index.js +14 -0
  288. package/lib/langium/trivia/index.js.map +1 -0
  289. package/lib/langium/trivia/trivia-contribution.d.ts +37 -0
  290. package/lib/langium/trivia/trivia-contribution.d.ts.map +1 -0
  291. package/lib/langium/trivia/trivia-contribution.js +10 -0
  292. package/lib/langium/trivia/trivia-contribution.js.map +1 -0
  293. package/lib/langium/trivia/trivia-preserver.d.ts +52 -0
  294. package/lib/langium/trivia/trivia-preserver.d.ts.map +1 -0
  295. package/lib/langium/trivia/trivia-preserver.js +10 -0
  296. package/lib/langium/trivia/trivia-preserver.js.map +1 -0
  297. package/lib/langium/trivia/trivia-service.d.ts +70 -0
  298. package/lib/langium/trivia/trivia-service.d.ts.map +1 -0
  299. package/lib/langium/trivia/trivia-service.js +56 -0
  300. package/lib/langium/trivia/trivia-service.js.map +1 -0
  301. package/lib/langium/update-rewrite/normalize-empty-strings.d.ts.map +1 -1
  302. package/lib/langium/update-rewrite/update-rewrite-service.d.ts +1 -1
  303. package/lib/langium/update-rewrite/update-rewrite-service.d.ts.map +1 -1
  304. package/lib/langium/update-rewrite/update-rewrite-service.js.map +1 -1
  305. package/lib/langium/update-rewrite/update-rewrite.d.ts +1 -1
  306. package/lib/langium/validation/document-validator.d.ts +168 -23
  307. package/lib/langium/validation/document-validator.d.ts.map +1 -1
  308. package/lib/langium/validation/document-validator.js +264 -26
  309. package/lib/langium/validation/document-validator.js.map +1 -1
  310. package/lib/langium/validation/validation-contribution-collector.d.ts +9 -1
  311. package/lib/langium/validation/validation-contribution-collector.d.ts.map +1 -1
  312. package/lib/langium/validation/validation-contribution-collector.js +1 -1
  313. package/lib/langium/validation/validation-contribution-collector.js.map +1 -1
  314. package/lib/langium/workspace/ast-node-locator.d.ts +24 -0
  315. package/lib/langium/workspace/ast-node-locator.d.ts.map +1 -0
  316. package/lib/langium/workspace/ast-node-locator.js +32 -0
  317. package/lib/langium/workspace/ast-node-locator.js.map +1 -0
  318. package/lib/langium/workspace/document-uri-policy.d.ts +24 -15
  319. package/lib/langium/workspace/document-uri-policy.d.ts.map +1 -1
  320. package/lib/langium/workspace/document-uri-policy.js +22 -13
  321. package/lib/langium/workspace/document-uri-policy.js.map +1 -1
  322. package/lib/langium/workspace/file-not-found.d.ts +23 -0
  323. package/lib/langium/workspace/file-not-found.d.ts.map +1 -0
  324. package/lib/langium/workspace/file-not-found.js +31 -0
  325. package/lib/langium/workspace/file-not-found.js.map +1 -0
  326. package/lib/langium/workspace/file-system-provider.d.ts +167 -8
  327. package/lib/langium/workspace/file-system-provider.d.ts.map +1 -1
  328. package/lib/langium/workspace/file-system-provider.js +115 -13
  329. package/lib/langium/workspace/file-system-provider.js.map +1 -1
  330. package/lib/langium/workspace/hydranium-langium-document-factory.d.ts +78 -4
  331. package/lib/langium/workspace/hydranium-langium-document-factory.d.ts.map +1 -1
  332. package/lib/langium/workspace/hydranium-langium-document-factory.js +188 -5
  333. package/lib/langium/workspace/hydranium-langium-document-factory.js.map +1 -1
  334. package/lib/langium/workspace/hydranium-workspace-lock.d.ts +50 -1
  335. package/lib/langium/workspace/hydranium-workspace-lock.d.ts.map +1 -1
  336. package/lib/langium/workspace/hydranium-workspace-lock.js +73 -1
  337. package/lib/langium/workspace/hydranium-workspace-lock.js.map +1 -1
  338. package/lib/langium/workspace/hydranium-workspace-manager.d.ts +105 -21
  339. package/lib/langium/workspace/hydranium-workspace-manager.d.ts.map +1 -1
  340. package/lib/langium/workspace/hydranium-workspace-manager.js +186 -31
  341. package/lib/langium/workspace/hydranium-workspace-manager.js.map +1 -1
  342. package/lib/langium/workspace/in-memory-file-system-provider.d.ts +26 -9
  343. package/lib/langium/workspace/in-memory-file-system-provider.d.ts.map +1 -1
  344. package/lib/langium/workspace/in-memory-file-system-provider.js +45 -22
  345. package/lib/langium/workspace/in-memory-file-system-provider.js.map +1 -1
  346. package/lib/langium/workspace/index-manager.d.ts.map +1 -1
  347. package/lib/langium/workspace/index-manager.js.map +1 -1
  348. package/lib/langium/workspace/index.d.ts +3 -0
  349. package/lib/langium/workspace/index.d.ts.map +1 -1
  350. package/lib/langium/workspace/index.js +3 -0
  351. package/lib/langium/workspace/index.js.map +1 -1
  352. package/lib/langium/workspace/initialize-workspace.d.ts +40 -3
  353. package/lib/langium/workspace/initialize-workspace.d.ts.map +1 -1
  354. package/lib/langium/workspace/initialize-workspace.js +68 -3
  355. package/lib/langium/workspace/initialize-workspace.js.map +1 -1
  356. package/lib/langium/workspace/langium-documents.d.ts +7 -0
  357. package/lib/langium/workspace/langium-documents.d.ts.map +1 -1
  358. package/lib/langium/workspace/langium-documents.js +18 -1
  359. package/lib/langium/workspace/langium-documents.js.map +1 -1
  360. package/lib/langium/workspace/persistent-file-system-provider.d.ts +16 -10
  361. package/lib/langium/workspace/persistent-file-system-provider.d.ts.map +1 -1
  362. package/lib/langium/workspace/persistent-file-system-provider.js +25 -13
  363. package/lib/langium/workspace/persistent-file-system-provider.js.map +1 -1
  364. package/lib/{documents → langium/workspace}/self-save-registry.d.ts +7 -9
  365. package/lib/langium/workspace/self-save-registry.d.ts.map +1 -0
  366. package/lib/{documents → langium/workspace}/self-save-registry.js +9 -11
  367. package/lib/langium/workspace/self-save-registry.js.map +1 -0
  368. package/lib/langium/workspace/syntax-errors.d.ts +23 -0
  369. package/lib/langium/workspace/syntax-errors.d.ts.map +1 -0
  370. package/lib/langium/workspace/syntax-errors.js +29 -0
  371. package/lib/langium/workspace/syntax-errors.js.map +1 -0
  372. package/lib/langium/workspace/virtual-document.d.ts +55 -21
  373. package/lib/langium/workspace/virtual-document.d.ts.map +1 -1
  374. package/lib/langium/workspace/virtual-document.js +87 -24
  375. package/lib/langium/workspace/virtual-document.js.map +1 -1
  376. package/lib/langium/workspace/write-lock-scope.d.ts +22 -10
  377. package/lib/langium/workspace/write-lock-scope.d.ts.map +1 -1
  378. package/lib/langium/workspace/write-lock-scope.js +15 -5
  379. package/lib/langium/workspace/write-lock-scope.js.map +1 -1
  380. package/lib/launcher/integrated-server.d.ts +3 -1
  381. package/lib/launcher/integrated-server.d.ts.map +1 -1
  382. package/lib/locale/server-locale.d.ts +6 -6
  383. package/lib/locale/server-locale.d.ts.map +1 -1
  384. package/lib/locale/server-locale.js.map +1 -1
  385. package/lib/lsp/completion/hydranium-completion-provider.d.ts +44 -2
  386. package/lib/lsp/completion/hydranium-completion-provider.d.ts.map +1 -1
  387. package/lib/lsp/completion/hydranium-completion-provider.js +68 -0
  388. package/lib/lsp/completion/hydranium-completion-provider.js.map +1 -1
  389. package/lib/lsp/connection-features.d.ts +23 -0
  390. package/lib/lsp/connection-features.d.ts.map +1 -0
  391. package/lib/lsp/connection-features.js +60 -0
  392. package/lib/lsp/connection-features.js.map +1 -0
  393. package/lib/lsp/diagnostics-connection.d.ts +27 -0
  394. package/lib/lsp/diagnostics-connection.d.ts.map +1 -0
  395. package/lib/lsp/diagnostics-connection.js +53 -0
  396. package/lib/lsp/diagnostics-connection.js.map +1 -0
  397. package/lib/lsp/edit-requests-guard.d.ts +31 -0
  398. package/lib/lsp/edit-requests-guard.d.ts.map +1 -0
  399. package/lib/lsp/edit-requests-guard.js +125 -0
  400. package/lib/lsp/edit-requests-guard.js.map +1 -0
  401. package/lib/lsp/hydranium-document-update-handler.d.ts +126 -82
  402. package/lib/lsp/hydranium-document-update-handler.d.ts.map +1 -1
  403. package/lib/lsp/hydranium-document-update-handler.js +234 -101
  404. package/lib/lsp/hydranium-document-update-handler.js.map +1 -1
  405. package/lib/lsp/index.d.ts +6 -2
  406. package/lib/lsp/index.d.ts.map +1 -1
  407. package/lib/lsp/index.js +6 -2
  408. package/lib/lsp/index.js.map +1 -1
  409. package/lib/lsp/lsp-latency.d.ts +39 -0
  410. package/lib/lsp/lsp-latency.d.ts.map +1 -0
  411. package/lib/lsp/lsp-latency.js +58 -0
  412. package/lib/lsp/lsp-latency.js.map +1 -0
  413. package/lib/lsp/save-text-document.d.ts +31 -0
  414. package/lib/lsp/save-text-document.d.ts.map +1 -0
  415. package/lib/lsp/save-text-document.js +52 -0
  416. package/lib/lsp/save-text-document.js.map +1 -0
  417. package/lib/lsp/semantic-token-provider.d.ts +58 -2
  418. package/lib/lsp/semantic-token-provider.d.ts.map +1 -1
  419. package/lib/lsp/semantic-token-provider.js +98 -9
  420. package/lib/lsp/semantic-token-provider.js.map +1 -1
  421. package/lib/lsp/shared-module.d.ts +4 -8
  422. package/lib/lsp/shared-module.d.ts.map +1 -1
  423. package/lib/lsp/shared-module.js.map +1 -1
  424. package/lib/lsp/start-language-server.d.ts +12 -7
  425. package/lib/lsp/start-language-server.d.ts.map +1 -1
  426. package/lib/lsp/start-language-server.js +21 -7
  427. package/lib/lsp/start-language-server.js.map +1 -1
  428. package/lib/messages/carriers.d.ts +4 -6
  429. package/lib/messages/carriers.d.ts.map +1 -1
  430. package/lib/messages/carriers.js +4 -6
  431. package/lib/messages/carriers.js.map +1 -1
  432. package/lib/messages/index.d.ts +2 -2
  433. package/lib/messages/index.d.ts.map +1 -1
  434. package/lib/messages/index.js +2 -2
  435. package/lib/messages/index.js.map +1 -1
  436. package/lib/messages/renderer.d.ts +10 -4
  437. package/lib/messages/renderer.d.ts.map +1 -1
  438. package/lib/messages/renderer.js +6 -0
  439. package/lib/messages/renderer.js.map +1 -1
  440. package/lib/node/event-loop-monitor.d.ts.map +1 -1
  441. package/lib/node/event-loop-monitor.js +3 -2
  442. package/lib/node/event-loop-monitor.js.map +1 -1
  443. package/lib/node/heap-ceiling.d.ts +61 -0
  444. package/lib/node/heap-ceiling.d.ts.map +1 -0
  445. package/lib/node/heap-ceiling.js +72 -0
  446. package/lib/node/heap-ceiling.js.map +1 -0
  447. package/lib/node/index.d.ts +1 -1
  448. package/lib/node/index.d.ts.map +1 -1
  449. package/lib/node/index.js +5 -1
  450. package/lib/node/index.js.map +1 -1
  451. package/lib/node/latency-from-env.d.ts +1 -1
  452. package/lib/node/latency-from-env.js +1 -1
  453. package/lib/node/lint-grammar.js.map +1 -1
  454. package/lib/node/log-file-sink.d.ts +3 -2
  455. package/lib/node/log-file-sink.d.ts.map +1 -1
  456. package/lib/node/log-file-sink.js +6 -2
  457. package/lib/node/log-file-sink.js.map +1 -1
  458. package/lib/node/log-preamble-node.d.ts +5 -3
  459. package/lib/node/log-preamble-node.d.ts.map +1 -1
  460. package/lib/node/log-preamble-node.js +13 -10
  461. package/lib/node/log-preamble-node.js.map +1 -1
  462. package/lib/node/measure-memory.d.ts.map +1 -1
  463. package/lib/node/measure-memory.js +5 -4
  464. package/lib/node/measure-memory.js.map +1 -1
  465. package/lib/node/memory-monitor.d.ts +2 -2
  466. package/lib/node/memory-monitor.d.ts.map +1 -1
  467. package/lib/node/memory-monitor.js +9 -10
  468. package/lib/node/memory-monitor.js.map +1 -1
  469. package/lib/node/node-file-system-provider.d.ts +67 -9
  470. package/lib/node/node-file-system-provider.d.ts.map +1 -1
  471. package/lib/node/node-file-system-provider.js +198 -53
  472. package/lib/node/node-file-system-provider.js.map +1 -1
  473. package/lib/node/profile-capture.d.ts +12 -4
  474. package/lib/node/profile-capture.d.ts.map +1 -1
  475. package/lib/node/profile-capture.js +8 -6
  476. package/lib/node/profile-capture.js.map +1 -1
  477. package/lib/node/profile-digest.js.map +1 -1
  478. package/lib/node/profiling-run.d.ts +13 -3
  479. package/lib/node/profiling-run.d.ts.map +1 -1
  480. package/lib/node/profiling-run.js +17 -8
  481. package/lib/node/profiling-run.js.map +1 -1
  482. package/lib/node/rename-over-open-readers.d.ts +61 -0
  483. package/lib/node/rename-over-open-readers.d.ts.map +1 -0
  484. package/lib/node/rename-over-open-readers.js +80 -0
  485. package/lib/node/rename-over-open-readers.js.map +1 -0
  486. package/lib/node/server-diagnostics.js +1 -1
  487. package/lib/node/server-diagnostics.js.map +1 -1
  488. package/lib/node/server-state-snapshot.d.ts +6 -5
  489. package/lib/node/server-state-snapshot.d.ts.map +1 -1
  490. package/lib/node/server-state-snapshot.js +20 -17
  491. package/lib/node/server-state-snapshot.js.map +1 -1
  492. package/lib/node/socket-launcher.d.ts +43 -7
  493. package/lib/node/socket-launcher.d.ts.map +1 -1
  494. package/lib/node/socket-launcher.js +42 -7
  495. package/lib/node/socket-launcher.js.map +1 -1
  496. package/lib/node/startup-cpu-profile.d.ts +29 -0
  497. package/lib/node/startup-cpu-profile.d.ts.map +1 -0
  498. package/lib/node/startup-cpu-profile.js +91 -0
  499. package/lib/node/startup-cpu-profile.js.map +1 -0
  500. package/lib/node/stdio-launcher.d.ts +13 -13
  501. package/lib/node/stdio-launcher.d.ts.map +1 -1
  502. package/lib/node/stdio-launcher.js +21 -11
  503. package/lib/node/stdio-launcher.js.map +1 -1
  504. package/lib/node/validate-workspace.d.ts +1 -1
  505. package/lib/node/validate-workspace.d.ts.map +1 -1
  506. package/lib/node/validate-workspace.js +5 -1
  507. package/lib/node/validate-workspace.js.map +1 -1
  508. package/lib/testing/document-uri-policy-conformance.js +1 -1
  509. package/lib/testing/document-uri-policy-conformance.js.map +1 -1
  510. package/lib/testing/fake-description.js.map +1 -1
  511. package/lib/testing/fake-document.d.ts +2 -1
  512. package/lib/testing/fake-document.d.ts.map +1 -1
  513. package/lib/testing/fake-document.js.map +1 -1
  514. package/lib/testing/index.d.ts +1 -1
  515. package/lib/testing/index.d.ts.map +1 -1
  516. package/lib/testing/index.js +2 -1
  517. package/lib/testing/index.js.map +1 -1
  518. package/lib/testing/langium-test-helpers.d.ts.map +1 -1
  519. package/lib/testing/langium-test-helpers.js +1 -0
  520. package/lib/testing/langium-test-helpers.js.map +1 -1
  521. package/lib/testing/make-noop-language-services.js.map +1 -1
  522. package/lib/testing/make-noop-shared-services.js.map +1 -1
  523. package/lib/testing/make-test-services.d.ts +45 -14
  524. package/lib/testing/make-test-services.d.ts.map +1 -1
  525. package/lib/testing/make-test-services.js +23 -7
  526. package/lib/testing/make-test-services.js.map +1 -1
  527. package/lib/testing/make-test-tracer.d.ts +12 -0
  528. package/lib/testing/make-test-tracer.d.ts.map +1 -1
  529. package/lib/testing/make-test-tracer.js +13 -0
  530. package/lib/testing/make-test-tracer.js.map +1 -1
  531. package/lib/testing/node/applied-edit-capture.d.ts +29 -0
  532. package/lib/testing/node/applied-edit-capture.d.ts.map +1 -0
  533. package/lib/testing/node/applied-edit-capture.js +117 -0
  534. package/lib/testing/node/applied-edit-capture.js.map +1 -0
  535. package/lib/testing/node/golden-corpus.js.map +1 -1
  536. package/lib/testing/node/index.d.ts +4 -1
  537. package/lib/testing/node/index.d.ts.map +1 -1
  538. package/lib/testing/node/index.js +4 -1
  539. package/lib/testing/node/index.js.map +1 -1
  540. package/lib/testing/node/lsp-harness.d.ts.map +1 -1
  541. package/lib/testing/node/lsp-harness.js +2 -0
  542. package/lib/testing/node/lsp-harness.js.map +1 -1
  543. package/lib/testing/node/lsp-server-connection.d.ts +21 -6
  544. package/lib/testing/node/lsp-server-connection.d.ts.map +1 -1
  545. package/lib/testing/node/lsp-server-connection.js +49 -106
  546. package/lib/testing/node/lsp-server-connection.js.map +1 -1
  547. package/lib/testing/node/scratch-workspace.d.ts +2 -1
  548. package/lib/testing/node/scratch-workspace.d.ts.map +1 -1
  549. package/lib/testing/node/scratch-workspace.js +12 -6
  550. package/lib/testing/node/scratch-workspace.js.map +1 -1
  551. package/lib/testing/node/settle-server.d.ts +33 -0
  552. package/lib/testing/node/settle-server.d.ts.map +1 -0
  553. package/lib/testing/node/settle-server.js +84 -0
  554. package/lib/testing/node/settle-server.js.map +1 -0
  555. package/lib/testing/node/spawned-server.d.ts +30 -1
  556. package/lib/testing/node/spawned-server.d.ts.map +1 -1
  557. package/lib/testing/node/spawned-server.js +39 -2
  558. package/lib/testing/node/spawned-server.js.map +1 -1
  559. package/lib/testing/playwright/browser-capture-bridge.js.map +1 -1
  560. package/lib/testing/playwright/e2e-profiling.js.map +1 -1
  561. package/lib/testing/playwright/flaky-network-proxy.d.ts +76 -0
  562. package/lib/testing/playwright/flaky-network-proxy.d.ts.map +1 -0
  563. package/lib/testing/playwright/flaky-network-proxy.js +164 -0
  564. package/lib/testing/playwright/flaky-network-proxy.js.map +1 -0
  565. package/lib/testing/playwright/index.d.ts +1 -0
  566. package/lib/testing/playwright/index.d.ts.map +1 -1
  567. package/lib/testing/playwright/index.js +1 -0
  568. package/lib/testing/playwright/index.js.map +1 -1
  569. package/lib/testing/playwright/server-log-capture.d.ts +46 -16
  570. package/lib/testing/playwright/server-log-capture.d.ts.map +1 -1
  571. package/lib/testing/playwright/server-log-capture.js +111 -40
  572. package/lib/testing/playwright/server-log-capture.js.map +1 -1
  573. package/lib/testing/playwright/server-log-fixture.d.ts +16 -0
  574. package/lib/testing/playwright/server-log-fixture.d.ts.map +1 -1
  575. package/lib/testing/playwright/server-log-fixture.js +25 -6
  576. package/lib/testing/playwright/server-log-fixture.js.map +1 -1
  577. package/lib/testing/playwright/server-log-region.d.ts +15 -0
  578. package/lib/testing/playwright/server-log-region.d.ts.map +1 -0
  579. package/lib/testing/playwright/server-log-region.js +30 -0
  580. package/lib/testing/playwright/server-log-region.js.map +1 -0
  581. package/lib/testing/playwright/server-log-rename-reporter.d.ts.map +1 -1
  582. package/lib/testing/playwright/server-log-rename-reporter.js +4 -3
  583. package/lib/testing/playwright/server-log-rename-reporter.js.map +1 -1
  584. package/lib/testing/run-update-pipeline.d.ts +8 -5
  585. package/lib/testing/run-update-pipeline.d.ts.map +1 -1
  586. package/lib/testing/run-update-pipeline.js.map +1 -1
  587. package/lib/testing/stub-ast-document-manager.d.ts +11 -51
  588. package/lib/testing/stub-ast-document-manager.d.ts.map +1 -1
  589. package/lib/testing/stub-ast-document-manager.js +31 -59
  590. package/lib/testing/stub-ast-document-manager.js.map +1 -1
  591. package/lib/testing/stub-document-builder.d.ts +25 -11
  592. package/lib/testing/stub-document-builder.d.ts.map +1 -1
  593. package/lib/testing/stub-document-builder.js +91 -2
  594. package/lib/testing/stub-document-builder.js.map +1 -1
  595. package/lib/testing/stub-hydranium-text-documents.d.ts +33 -10
  596. package/lib/testing/stub-hydranium-text-documents.d.ts.map +1 -1
  597. package/lib/testing/stub-hydranium-text-documents.js +132 -16
  598. package/lib/testing/stub-hydranium-text-documents.js.map +1 -1
  599. package/lib/testing/stub-index-manager.js.map +1 -1
  600. package/lib/testing/stub-langium-documents.d.ts +3 -2
  601. package/lib/testing/stub-langium-documents.d.ts.map +1 -1
  602. package/lib/testing/stub-langium-documents.js.map +1 -1
  603. package/lib/testing/stub-model-service.d.ts +8 -7
  604. package/lib/testing/stub-model-service.d.ts.map +1 -1
  605. package/lib/testing/stub-model-service.js +4 -4
  606. package/lib/testing/stub-model-service.js.map +1 -1
  607. package/lib/testing/stub-project-manager.d.ts.map +1 -1
  608. package/lib/testing/stub-project-manager.js +10 -0
  609. package/lib/testing/stub-project-manager.js.map +1 -1
  610. package/lib/testing/stub-self-save-registry.d.ts +1 -1
  611. package/lib/testing/stub-self-save-registry.d.ts.map +1 -1
  612. package/lib/testing/stub-service-registry.d.ts +1 -1
  613. package/lib/testing/stub-service-registry.js.map +1 -1
  614. package/lib/testing/stub-writable-file-system.d.ts +1 -1
  615. package/lib/testing/stub-writable-file-system.d.ts.map +1 -1
  616. package/lib/testing/stub-writable-file-system.js +2 -1
  617. package/lib/testing/stub-writable-file-system.js.map +1 -1
  618. package/lib/util/connection-liveness.d.ts +22 -6
  619. package/lib/util/connection-liveness.d.ts.map +1 -1
  620. package/lib/util/connection-liveness.js +75 -15
  621. package/lib/util/connection-liveness.js.map +1 -1
  622. package/lib/util/environment.d.ts +7 -0
  623. package/lib/util/environment.d.ts.map +1 -1
  624. package/lib/util/environment.js +15 -1
  625. package/lib/util/environment.js.map +1 -1
  626. package/lib/util/registry.d.ts.map +1 -1
  627. package/package.json +41 -60
  628. package/src/browser/host-heads-in-worker.ts +218 -0
  629. package/src/browser/index.ts +17 -0
  630. package/src/browser/indexeddb-file-system-store.ts +192 -0
  631. package/src/browser/start-language-server-in-worker.ts +56 -0
  632. package/src/documents/ast-document-manager.ts +477 -250
  633. package/src/documents/client-ids.ts +5 -22
  634. package/src/documents/client-session-errors.ts +26 -0
  635. package/src/documents/client-session-registry.ts +262 -0
  636. package/src/documents/dirty-state-tracker.ts +167 -0
  637. package/src/documents/document-release-handler.ts +404 -0
  638. package/src/documents/document-release-scheduler.ts +121 -0
  639. package/src/documents/file-system-task-queue.ts +77 -0
  640. package/src/documents/hydranium-text-documents.ts +1044 -615
  641. package/src/documents/index.ts +10 -2
  642. package/src/documents/language-client-shadow.ts +501 -0
  643. package/src/documents/model-ledger.ts +66 -0
  644. package/src/documents/text-ledger.ts +112 -0
  645. package/src/documents/version-sync-service.ts +304 -0
  646. package/src/index.ts +3 -0
  647. package/src/langium/ast-extension/ast-extension-service.ts +77 -41
  648. package/src/langium/ast-extension/ast-node-builder.ts +35 -13
  649. package/src/langium/bootstrap.ts +79 -43
  650. package/src/langium/build-phase-pass/build-phase-pass-service.ts +29 -19
  651. package/src/langium/build-phase-pass/build-phase-pass.ts +37 -8
  652. package/src/langium/composite-ast-reflection.ts +112 -0
  653. package/src/langium/config/configuration-provider.ts +119 -0
  654. package/src/langium/config/index.ts +1 -0
  655. package/src/langium/diagnostics/console-tee.ts +150 -0
  656. package/src/langium/diagnostics/hydranium-langium-profiler.ts +15 -39
  657. package/src/langium/diagnostics/log-preamble.ts +1 -1
  658. package/src/langium/diagnostics/logger.ts +51 -17
  659. package/src/langium/diagnostics/lsp-logger.ts +83 -45
  660. package/src/langium/document-builder/build-pipeline-integration.ts +57 -32
  661. package/src/langium/document-builder/build-session.ts +4 -3
  662. package/src/langium/document-builder/document-builder.ts +773 -106
  663. package/src/langium/integration-services.ts +20 -23
  664. package/src/langium/integrity/integrity-rule.ts +22 -8
  665. package/src/langium/integrity/integrity-service.ts +656 -90
  666. package/src/langium/keys/containment.ts +3 -2
  667. package/src/langium/keys/name-based-key-provider.ts +24 -12
  668. package/src/langium/language-module.ts +52 -9
  669. package/src/langium/model-service/client-session.ts +769 -0
  670. package/src/langium/model-service/index.ts +2 -0
  671. package/src/langium/model-service/model-events.ts +82 -0
  672. package/src/langium/model-service/model-service.ts +885 -532
  673. package/src/langium/module.ts +141 -65
  674. package/src/langium/naming/name-provider.ts +34 -8
  675. package/src/langium/project/abstract-project-manager.ts +409 -20
  676. package/src/langium/project/project-change-event.ts +25 -3
  677. package/src/langium/project/project-manager.ts +31 -2
  678. package/src/langium/project/single-project-manager.ts +16 -1
  679. package/src/langium/residency/cst-residency-service.ts +210 -87
  680. package/src/langium/scope/ast-node-description-provider.ts +111 -16
  681. package/src/langium/scope/built-local-symbols.ts +13 -0
  682. package/src/langium/scope/fallback-scope.ts +38 -0
  683. package/src/langium/scope/hydranium-scope-computation.ts +45 -44
  684. package/src/langium/scope/hydranium-scope-provider.ts +245 -41
  685. package/src/langium/scope/index.ts +1 -0
  686. package/src/langium/scope/reference-candidate-provider.ts +6 -5
  687. package/src/langium/scope/scope-extension-service.ts +148 -56
  688. package/src/langium/scope/scoped-ast-node-description.ts +1 -3
  689. package/src/langium/serialization/abstract-serializer.ts +57 -41
  690. package/src/langium/serialization/json-serializer.ts +14 -3
  691. package/src/langium/serialization/serializer.ts +1 -1
  692. package/src/langium/serialization/yaml-serializer.ts +26 -4
  693. package/src/langium/service-registry.ts +41 -2
  694. package/src/langium/shared-services.ts +13 -4
  695. package/src/langium/single-langium-copy.ts +42 -0
  696. package/src/langium/transfer/transfer-encoder.ts +76 -50
  697. package/src/langium/trivia/comment-preserver.ts +1410 -0
  698. package/src/langium/trivia/document-ending-preserver.ts +71 -0
  699. package/src/langium/trivia/index.ts +14 -0
  700. package/src/langium/trivia/trivia-contribution.ts +39 -0
  701. package/src/langium/trivia/trivia-preserver.ts +56 -0
  702. package/src/langium/trivia/trivia-service.ts +99 -0
  703. package/src/langium/update-rewrite/update-rewrite-service.ts +1 -1
  704. package/src/langium/update-rewrite/update-rewrite.ts +1 -1
  705. package/src/langium/validation/document-validator.ts +312 -31
  706. package/src/langium/validation/validation-contribution-collector.ts +10 -1
  707. package/src/langium/workspace/ast-node-locator.ts +33 -0
  708. package/src/langium/workspace/document-uri-policy.ts +32 -18
  709. package/src/langium/workspace/file-not-found.ts +35 -0
  710. package/src/langium/workspace/file-system-provider.ts +258 -16
  711. package/src/langium/workspace/hydranium-langium-document-factory.ts +239 -4
  712. package/src/langium/workspace/hydranium-workspace-lock.ts +101 -2
  713. package/src/langium/workspace/hydranium-workspace-manager.ts +214 -35
  714. package/src/langium/workspace/in-memory-file-system-provider.ts +54 -23
  715. package/src/langium/workspace/index.ts +3 -0
  716. package/src/langium/workspace/initialize-workspace.ts +96 -4
  717. package/src/langium/workspace/langium-documents.ts +19 -1
  718. package/src/langium/workspace/persistent-file-system-provider.ts +27 -15
  719. package/src/{documents → langium/workspace}/self-save-registry.ts +12 -14
  720. package/src/langium/workspace/syntax-errors.ts +34 -0
  721. package/src/langium/workspace/virtual-document.ts +106 -25
  722. package/src/langium/workspace/write-lock-scope.ts +27 -12
  723. package/src/launcher/integrated-server.ts +3 -1
  724. package/src/locale/server-locale.ts +6 -6
  725. package/src/lsp/completion/hydranium-completion-provider.ts +82 -2
  726. package/src/lsp/connection-features.ts +67 -0
  727. package/src/lsp/diagnostics-connection.ts +56 -0
  728. package/src/lsp/edit-requests-guard.ts +152 -0
  729. package/src/lsp/hydranium-document-update-handler.ts +284 -110
  730. package/src/lsp/index.ts +6 -2
  731. package/src/lsp/lsp-latency.ts +62 -0
  732. package/src/lsp/save-text-document.ts +63 -0
  733. package/src/lsp/semantic-token-provider.ts +146 -8
  734. package/src/lsp/shared-module.ts +4 -8
  735. package/src/lsp/start-language-server.ts +21 -7
  736. package/src/messages/carriers.ts +4 -6
  737. package/src/messages/index.ts +11 -2
  738. package/src/messages/renderer.ts +11 -3
  739. package/src/node/event-loop-monitor.ts +3 -4
  740. package/src/node/heap-ceiling.ts +108 -0
  741. package/src/node/index.ts +5 -1
  742. package/src/node/latency-from-env.ts +1 -1
  743. package/src/node/log-file-sink.ts +6 -2
  744. package/src/node/log-preamble-node.ts +20 -10
  745. package/src/node/measure-memory.ts +5 -4
  746. package/src/node/memory-monitor.ts +10 -10
  747. package/src/node/node-file-system-provider.ts +214 -57
  748. package/src/node/profile-capture.ts +16 -15
  749. package/src/node/profiling-run.ts +30 -8
  750. package/src/node/rename-over-open-readers.ts +94 -0
  751. package/src/node/server-diagnostics.ts +1 -1
  752. package/src/node/server-state-snapshot.ts +28 -20
  753. package/src/node/socket-launcher.ts +78 -14
  754. package/src/node/startup-cpu-profile.ts +94 -0
  755. package/src/node/stdio-launcher.ts +29 -19
  756. package/src/node/validate-workspace.ts +8 -2
  757. package/src/testing/document-uri-policy-conformance.ts +1 -1
  758. package/src/testing/fake-document.ts +2 -1
  759. package/src/testing/index.ts +2 -1
  760. package/src/testing/langium-test-helpers.ts +1 -0
  761. package/src/testing/make-test-services.ts +83 -31
  762. package/src/testing/make-test-tracer.ts +26 -0
  763. package/src/testing/node/applied-edit-capture.ts +141 -0
  764. package/src/testing/node/index.ts +4 -1
  765. package/src/testing/node/lsp-harness.ts +2 -0
  766. package/src/testing/node/lsp-server-connection.ts +83 -119
  767. package/src/testing/node/scratch-workspace.ts +12 -6
  768. package/src/testing/node/settle-server.ts +89 -0
  769. package/src/testing/node/spawned-server.ts +57 -3
  770. package/src/testing/parse-semantic-root.ts +1 -1
  771. package/src/testing/playwright/flaky-network-proxy.ts +240 -0
  772. package/src/testing/playwright/index.ts +1 -0
  773. package/src/testing/playwright/server-log-capture.ts +137 -47
  774. package/src/testing/playwright/server-log-fixture.ts +46 -6
  775. package/src/testing/playwright/server-log-region.ts +32 -0
  776. package/src/testing/playwright/server-log-rename-reporter.ts +4 -3
  777. package/src/testing/run-update-pipeline.ts +8 -5
  778. package/src/testing/stub-ast-document-manager.ts +47 -123
  779. package/src/testing/stub-document-builder.ts +128 -15
  780. package/src/testing/stub-hydranium-text-documents.ts +184 -27
  781. package/src/testing/stub-langium-documents.ts +3 -2
  782. package/src/testing/stub-model-service.ts +9 -8
  783. package/src/testing/stub-project-manager.ts +10 -0
  784. package/src/testing/stub-self-save-registry.ts +1 -1
  785. package/src/testing/stub-service-registry.ts +1 -1
  786. package/src/testing/stub-writable-file-system.ts +3 -2
  787. package/src/util/connection-liveness.ts +81 -15
  788. package/src/util/environment.ts +24 -2
  789. package/lib/documents/language-client-text-shadow.d.ts +0 -124
  790. package/lib/documents/language-client-text-shadow.d.ts.map +0 -1
  791. package/lib/documents/language-client-text-shadow.js +0 -190
  792. package/lib/documents/language-client-text-shadow.js.map +0 -1
  793. package/lib/documents/self-save-registry.d.ts.map +0 -1
  794. package/lib/documents/self-save-registry.js.map +0 -1
  795. package/lib/lsp/instrument-connection.d.ts +0 -36
  796. package/lib/lsp/instrument-connection.d.ts.map +0 -1
  797. package/lib/lsp/instrument-connection.js +0 -62
  798. package/lib/lsp/instrument-connection.js.map +0 -1
  799. package/lib/node/process-memory.d.ts +0 -66
  800. package/lib/node/process-memory.d.ts.map +0 -1
  801. package/lib/node/process-memory.js +0 -252
  802. package/lib/node/process-memory.js.map +0 -1
  803. package/src/documents/language-client-text-shadow.ts +0 -211
  804. package/src/lsp/instrument-connection.ts +0 -66
  805. package/src/node/process-memory.ts +0 -299
@@ -0,0 +1,1410 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2026 CrossBreeze, EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the MIT License which is available in the project root.
6
+ *
7
+ * SPDX-License-Identifier: MIT
8
+ ********************************************************************************/
9
+
10
+ import { type Tracer } from '@hydranium/protocol';
11
+ import {
12
+ type AstNode,
13
+ AstUtils,
14
+ type CompositeCstNode,
15
+ type CstNode,
16
+ GrammarAST,
17
+ GrammarUtils,
18
+ IndentationAwareTokenBuilder,
19
+ isAstNode,
20
+ type LangiumDocument,
21
+ type URI
22
+ } from '@hydranium/langium';
23
+ import { type LogNameOptions } from '../diagnostics/logger.js';
24
+ import { type HydraniumLanguageServices } from '../language-module.js';
25
+ import { hasSyntaxErrors } from '../workspace/syntax-errors.js';
26
+ import { type TriviaContribution, type TriviaRegistry } from './trivia-contribution.js';
27
+ import { type TriviaPreserver } from './trivia-preserver.js';
28
+
29
+ /**
30
+ * Where a comment sat relative to the node it belongs to.
31
+ *
32
+ * The CST records source order but not ownership, so getting this wrong
33
+ * relocates a comment rather than losing it.
34
+ *
35
+ * - `atContainerStart` — before everything the container holds.
36
+ * - `leading` — on its own line(s) above the node that follows it.
37
+ * - `trailing` — on the same line the previous node ended on.
38
+ * - `trailingOnContainer` — on the container's own header line, or amongst its
39
+ * syntax with more of the container still to come.
40
+ * - `afterNode` — on its own line after the last node, still inside the
41
+ * container.
42
+ * - `atContainerEnd` — after everything the container holds.
43
+ */
44
+ export type CommentPlacement = 'atContainerStart' | 'leading' | 'trailing' | 'trailingOnContainer' | 'afterNode' | 'atContainerEnd';
45
+
46
+ /** One comment, bound to the AST node it hangs off rather than to an offset. */
47
+ export interface DocumentComment {
48
+ /** The comment's source text, delimiters included. */
49
+ readonly text: string;
50
+ /**
51
+ * The node the comment hangs off, held as the AST OBJECT rather than a path
52
+ * or an offset, so a write that mutates the tree IN PLACE — an integrity
53
+ * repair, a diagram gesture — is reflected without re-extracting.
54
+ *
55
+ * A transfer-model write supplies a separate object graph. A unique rename
56
+ * can still be matched when its type, parent slot and other properties agree;
57
+ * ambiguous matches are dropped.
58
+ */
59
+ readonly anchor: AstNode;
60
+ readonly placement: CommentPlacement;
61
+ /**
62
+ * Blank lines between the comment and what it leads, for the placements that
63
+ * emit it BEFORE its anchor.
64
+ *
65
+ * Captured for the placements that emit it after as well, where nothing reads
66
+ * it: there the spacing an author means is the gap above the comment, which
67
+ * {@link blankLinesBefore} carries.
68
+ */
69
+ readonly blankLinesAfter: number;
70
+ /**
71
+ * Blank lines between the comment and what PRECEDES it, for the placements
72
+ * that emit it after their anchor. A comment separated from the closing brace
73
+ * above it by a blank line reads as a new thought; re-emitting it flush
74
+ * against that brace silently rewrites the author's spacing.
75
+ */
76
+ readonly blankLinesBefore: number;
77
+ /**
78
+ * The indentation the comment sat at in the source, when it owned its line —
79
+ * `undefined` for one sharing a line with code, which is never reindented.
80
+ *
81
+ * A MULTI-LINE comment needs it to shift its continuation lines, which carry
82
+ * their original indentation inside {@link text}: emitting the block at a new
83
+ * indentation without shifting them leaves it hanging off its old column.
84
+ *
85
+ * A single-line one needs it whenever its anchor turns out to share a line,
86
+ * because the indentation read off a mid-line offset is the code's and not
87
+ * the comment's.
88
+ */
89
+ readonly sourceIndent?: string;
90
+ }
91
+
92
+ /** What {@link CommentPreserver} takes off a document. */
93
+ export interface CommentTrivia {
94
+ readonly comments: readonly DocumentComment[];
95
+ /** Original tree, retained to detect ambiguous or removed anchors at apply time. */
96
+ readonly sourceRoot: AstNode;
97
+ }
98
+
99
+ /** Construction options for {@link CommentPreserver}. */
100
+ export interface CommentPreserverOptions extends LogNameOptions {
101
+ /**
102
+ * Registry id, defaulting to `comments`. Set it only to run a second comment
103
+ * preserver beside the framework's; a subclass that REPLACES it keeps the
104
+ * default so the contribution sub-key and the id stay in step.
105
+ */
106
+ readonly id?: string;
107
+ /**
108
+ * How many comments spliced into shared lines are worth blaming one at a
109
+ * time when the result will not parse. Default {@link DEFAULT_ISOLATED_EDITS}.
110
+ *
111
+ * Isolating means re-splicing and re-parsing the whole document once per
112
+ * candidate, so the search costs the document's length times their number.
113
+ * Past this many the write drops them together instead — the same text it
114
+ * would reach anyway if every one of them were at fault, without spending
115
+ * that search on a build's write lock.
116
+ */
117
+ readonly maxIsolatedEdits?: number;
118
+ }
119
+
120
+ function isComposite(node: CstNode): node is CompositeCstNode {
121
+ return 'content' in node;
122
+ }
123
+
124
+ function tokenNameOf(node: CstNode): string | undefined {
125
+ return 'tokenType' in node ? (node as CstNode & { tokenType: { name: string } }).tokenType.name : undefined;
126
+ }
127
+
128
+ /**
129
+ * Where a node begins and ends in the text it was located in, and which node
130
+ * that is.
131
+ *
132
+ * Exported because {@link CommentPreserver.editFor} takes one, and an override
133
+ * that cannot name its own parameter type has to restate the shape — which then
134
+ * stops compiling the moment a field is added here.
135
+ */
136
+ export interface AnchorSpan {
137
+ readonly offset: number;
138
+ readonly end: number;
139
+ readonly owner: AstNode;
140
+ }
141
+
142
+ export interface IndentationTokenNames {
143
+ readonly indent: string;
144
+ readonly dedent: string;
145
+ }
146
+
147
+ /**
148
+ * A container a run of comment lines can be placed in: the run's own, or one
149
+ * it is lifted into when a block's DEDENT follows the run. `last` is the index
150
+ * in its content the run is placed after, `closed` the block the run is lifted
151
+ * out of, and `liftBelow` the column a comment line has to start left of to be
152
+ * lifted this far.
153
+ */
154
+ export interface CommentLiftTarget {
155
+ readonly container: CompositeCstNode;
156
+ readonly last: number;
157
+ readonly closed?: CompositeCstNode;
158
+ readonly liftBelow?: number;
159
+ }
160
+
161
+ /**
162
+ * A run of hidden nodes in `content`, from `start` to `end`, with the syntax
163
+ * before it and the containers its comments can be placed in, innermost first.
164
+ */
165
+ export interface CommentRun {
166
+ readonly content: readonly CstNode[];
167
+ readonly start: number;
168
+ readonly end: number;
169
+ readonly previous?: { readonly end: number };
170
+ readonly targets: readonly CommentLiftTarget[];
171
+ /** The nodes {@link CommentPreserver.neighbours} finds before the run, in order. */
172
+ readonly before: readonly CstNode[];
173
+ /** Per target, the nodes {@link CommentPreserver.neighbours} finds after its `last`. */
174
+ readonly after: readonly (readonly CstNode[])[];
175
+ }
176
+
177
+ /**
178
+ * One splice, with what it takes to check the risky kind afterwards.
179
+ *
180
+ * `inline` is set only for an edit that opens a line inside syntax the
181
+ * serializer emitted whole; it carries what the comment must look like when the
182
+ * result is read back, since that edit is the one that can hand a comment to
183
+ * the wrong declaration.
184
+ *
185
+ * Exported for the same reason {@link AnchorSpan} is: the members that take one
186
+ * are protected, so an override has to be able to name it.
187
+ */
188
+ export interface InlineAwareEdit {
189
+ readonly at: number;
190
+ readonly text: string;
191
+ /** Capture order, so several comments on one anchor keep the order written. */
192
+ readonly order: number;
193
+ readonly inline?: { readonly text: string; readonly key: string };
194
+ }
195
+
196
+ /**
197
+ * Carries a document's comments across a write that re-serializes it from the
198
+ * AST: extract against the node each comment hangs off, then splice into the
199
+ * serializer's output, located by re-parsing it.
200
+ *
201
+ * **Emitting comments from inside a serializer instead drops them silently for
202
+ * most nodes.** A hand-written concrete-syntax serializer reaches its children
203
+ * through its own per-`$type` emitters, not through
204
+ * `AbstractSerializer.serializeNode`, so a hook on that seam is reached by some
205
+ * nodes and bypassed by the rest — and the ones it misses fail as absence,
206
+ * which no test notices unless it asserts on exact text.
207
+ */
208
+ export class CommentPreserver implements TriviaPreserver<CommentTrivia> {
209
+ /**
210
+ * **Registering a second preserver under this id throws.** A subclass added
211
+ * ALONGSIDE the framework's — rather than replacing it by rebinding the
212
+ * `comments` contribution sub-key — collides here, and the throw surfaces
213
+ * from the first write rather than from server start, because the service is
214
+ * constructed lazily. Pass an `id` to run both.
215
+ */
216
+ readonly id: string;
217
+ readonly label = 'Comments';
218
+
219
+ /** Default {@link CommentPreserverOptions.maxIsolatedEdits}. */
220
+ static readonly DEFAULT_ISOLATED_EDITS = 24;
221
+
222
+ protected readonly tracer: Tracer;
223
+ protected readonly maxIsolatedEdits: number;
224
+ protected commentTokenNames?: readonly string[];
225
+ protected indentationTokenNames?: IndentationTokenNames | null;
226
+ /**
227
+ * Set for the duration of one {@link extract}: the run
228
+ * {@link resolveCommentRun} resolved last, so the run's next comment reuses
229
+ * it.
230
+ */
231
+ protected commentRun?: CommentRun;
232
+ /**
233
+ * Set for the duration of one {@link extract}: {@link syntaxSpan} per node,
234
+ * as a block a run is lifted out of would otherwise be measured once per
235
+ * comment.
236
+ */
237
+ protected spanMemo?: Map<CstNode, { readonly offset: number; readonly end: number } | undefined>;
238
+ /** Set for the duration of one {@link apply}; see {@link cachedAnchorKey}. */
239
+ protected keyMemo?: Map<AstNode, string | undefined>;
240
+ /** Set for the duration of one {@link apply}; see {@link ambiguousRetainedKey}. */
241
+ protected listVerdicts?: Map<AstNode, Map<string, boolean>>;
242
+ /** Set for the duration of one {@link apply}; see {@link parse}. */
243
+ protected lastParse?: { text?: string; uri?: string; document?: LangiumDocument };
244
+
245
+ constructor(
246
+ protected readonly services: HydraniumLanguageServices,
247
+ options: CommentPreserverOptions = {}
248
+ ) {
249
+ this.id = options.id ?? 'comments';
250
+ this.maxIsolatedEdits = options.maxIsolatedEdits ?? CommentPreserver.DEFAULT_ISOLATED_EDITS;
251
+ this.tracer = services.shared.Tracer.for(options.logName ?? 'CommentPreserver');
252
+ }
253
+
254
+ /**
255
+ * Every comment terminal the grammar declares, by token name.
256
+ *
257
+ * Deliberately NOT `GrammarConfig.multilineCommentRules`, which Langium
258
+ * populates only with terminals whose regex spans lines — so a grammar that
259
+ * comments with `//` answers an empty list there and every one of its
260
+ * comments would be invisible to a capture keyed on it.
261
+ */
262
+ protected getCommentTokenNames(): readonly string[] {
263
+ if (!this.commentTokenNames) {
264
+ this.commentTokenNames = this.services.Grammar.rules
265
+ .filter(GrammarAST.isTerminalRule)
266
+ .filter(rule => GrammarUtils.isCommentTerminal(rule))
267
+ .map(rule => rule.name);
268
+ }
269
+ return this.commentTokenNames;
270
+ }
271
+
272
+ /**
273
+ * The names of the INDENT and DEDENT tokens the language's
274
+ * `IndentationAwareTokenBuilder` emits, `undefined` for a lexer that emits
275
+ * none. Override it for a lexer of your own that emits such tokens.
276
+ *
277
+ * They are syntax to the parser only. A DEDENT is zero-width at the start of
278
+ * the line after its block, and an INDENT covers the whitespace in front of
279
+ * the block's first line, so a comment line beside either reads as sharing a
280
+ * line with the block, and moves onto the block's last line or its header.
281
+ */
282
+ protected getIndentationTokenNames(): IndentationTokenNames | undefined {
283
+ if (this.indentationTokenNames === undefined) {
284
+ const builder = this.services.parser.TokenBuilder;
285
+ this.indentationTokenNames =
286
+ builder instanceof IndentationAwareTokenBuilder
287
+ ? { indent: builder.indentTokenType.name, dedent: builder.dedentTokenType.name }
288
+ : null;
289
+ }
290
+ return this.indentationTokenNames ?? undefined;
291
+ }
292
+
293
+ protected isIndentation(node: CstNode): boolean {
294
+ const names = this.getIndentationTokenNames();
295
+ const name = tokenNameOf(node);
296
+ return names !== undefined && (name === names.indent || name === names.dedent);
297
+ }
298
+
299
+ /**
300
+ * Where the syntax of `node` begins and ends without the indentation tokens
301
+ * at its edges.
302
+ */
303
+ protected syntaxSpan(node: CstNode): { readonly offset: number; readonly end: number } | undefined {
304
+ if (this.getIndentationTokenNames() === undefined) {
305
+ return node;
306
+ }
307
+ if (this.spanMemo?.has(node)) {
308
+ return this.spanMemo.get(node);
309
+ }
310
+ const first = this.edgeToken(node, 1);
311
+ const last = this.edgeToken(node, -1);
312
+ const span = first && last && { offset: first.offset, end: last.end };
313
+ this.spanMemo?.set(node, span);
314
+ return span;
315
+ }
316
+
317
+ /** The first token of `node` for a `step` of 1, the last for -1, that is neither hidden nor indentation. */
318
+ protected edgeToken(node: CstNode, step: 1 | -1): CstNode | undefined {
319
+ if (!isComposite(node)) {
320
+ return node.hidden || this.isIndentation(node) ? undefined : node;
321
+ }
322
+ const content = node.content;
323
+ for (let index = step > 0 ? 0 : content.length - 1; index >= 0 && index < content.length; index += step) {
324
+ const found = this.edgeToken(content[index], step);
325
+ if (found !== undefined) {
326
+ return found;
327
+ }
328
+ }
329
+ return undefined;
330
+ }
331
+
332
+ /**
333
+ * Stable identity for one AST node, used to find it again in the
334
+ * re-serialized text — or `undefined` when no stable identity exists.
335
+ *
336
+ * **Read through the `NameProvider`, so the anchor is what the grammar treats
337
+ * as IDENTITY rather than whatever sits on `name`.** A grammar whose `name`
338
+ * is a display label, carrying its identifier on another property, would
339
+ * otherwise anchor comments to a value users retitle freely. A simple
340
+ * retitle can be matched, but the real identifier remains the safer key.
341
+ * `NameProviderOptions.nameProperties` names that property.
342
+ *
343
+ * **Whatever an override returns must survive a sibling being inserted into
344
+ * the same list.** A positional key does not: every comment after the
345
+ * insertion point is then written against the following declaration — a move,
346
+ * which reads as text the author wrote there and which nothing downstream can
347
+ * detect. That is why an unidentified node answers `undefined` here rather
348
+ * than falling back to a container index, and why this cannot delegate to
349
+ * `ElementKeyProvider`, whose name-based strategy makes exactly that fallback:
350
+ * its keys are derived and consumed within one snapshot, where no position can
351
+ * have shifted underneath them.
352
+ *
353
+ * Overriding is the supported route for a grammar that identifies some nodes
354
+ * outside the naming surface altogether — by a cross-reference that is unique
355
+ * among its siblings, say. Capture and lookup both read this one definition,
356
+ * so an override cannot make those two disagree.
357
+ *
358
+ * **Matching a rename does not read it.** That comparison asks the
359
+ * `NameProvider` what changed, so a node identified only by an override is
360
+ * carried across a write that leaves its identity alone and dropped by one
361
+ * that rewrites it — never misplaced, but never followed either.
362
+ */
363
+ protected anchorKey(node: AstNode): string | undefined {
364
+ const nameProvider = this.services.references.NameProvider;
365
+ const segments: string[] = [];
366
+ let current: AstNode | undefined = node;
367
+ while (current?.$container) {
368
+ const name = nameProvider.getOwnName(current);
369
+ if (name === undefined || name.length === 0) {
370
+ return undefined;
371
+ }
372
+ segments.unshift(`${current.$type}#${this.escapeKeySegment(name)}`);
373
+ current = current.$container;
374
+ }
375
+ return segments.length === 0 ? `@root:${node.$type}` : segments.join('/');
376
+ }
377
+
378
+ /**
379
+ * Escape the characters {@link anchorKey} builds its keys out of, so a name
380
+ * containing one cannot spell a key another node also answers to — which
381
+ * would make two distinct nodes indistinguishable to the lookup.
382
+ */
383
+ protected escapeKeySegment(name: string): string {
384
+ return name.replace(/[\\#/]/g, character => `\\${character}`);
385
+ }
386
+
387
+ /**
388
+ * Collect every comment in `document`, each bound to the node it hangs off.
389
+ *
390
+ * Call this BEFORE the mutation that prompts the write. The anchors are AST
391
+ * objects, so a rule that renames one in place is reflected automatically;
392
+ * capturing afterwards would work too, but capturing first is what keeps the
393
+ * CST and the comments describing the same state.
394
+ */
395
+ extract(document: LangiumDocument): CommentTrivia {
396
+ // The capture reads the CST, which a residency policy may have shed.
397
+ // Restored rather than skipped: skipping would lose the whole file's
398
+ // comments for a document that had gone idle, silently and only under a
399
+ // shedding strategy.
400
+ this.services.shared.workspace.CstResidencyService.rehydrate(document);
401
+ const root = document.parseResult.value.$cstNode;
402
+ const commentTokens = this.getCommentTokenNames();
403
+ if (!root || commentTokens.length === 0) {
404
+ return { comments: [], sourceRoot: document.parseResult.value };
405
+ }
406
+ const source = document.textDocument.getText();
407
+ const comments: DocumentComment[] = [];
408
+ this.commentRun = undefined;
409
+ this.spanMemo = new Map();
410
+ try {
411
+ this.captureFrom(root, source, commentTokens, comments);
412
+ } finally {
413
+ this.commentRun = undefined;
414
+ this.spanMemo = undefined;
415
+ }
416
+ return { comments, sourceRoot: document.parseResult.value };
417
+ }
418
+
419
+ /** Recursive half of {@link extract}: one composite node's content, then its children. */
420
+ protected captureFrom(node: CstNode, source: string, commentTokens: readonly string[], into: DocumentComment[]): void {
421
+ if (!isComposite(node)) {
422
+ return;
423
+ }
424
+ const content = node.content;
425
+ content.forEach((child, index) => {
426
+ const tokenName = tokenNameOf(child);
427
+ if (child.hidden && tokenName !== undefined && commentTokens.includes(tokenName)) {
428
+ into.push(this.classify(child, index, node, source));
429
+ }
430
+ this.captureFrom(child, source, commentTokens, into);
431
+ });
432
+ }
433
+
434
+ /**
435
+ * Decide which node a comment belongs to, and how it sat against it.
436
+ *
437
+ * A comment on the same line the previous node ENDED on is that node's
438
+ * trailing comment — checked first, because by source order it also precedes
439
+ * whatever comes next, and reading it as the NEXT node's leading comment is
440
+ * what relocates an end-of-line note onto the following declaration.
441
+ */
442
+ protected classify(comment: CstNode, index: number, container: CompositeCstNode, source: string): DocumentComment {
443
+ const placed = this.placeAmongSyntax(comment, index, container, source);
444
+ const owner = placed.container;
445
+ const content = placed.content;
446
+ const at = placed.index;
447
+ const ownsOwnNode = (candidate: CstNode | undefined): boolean =>
448
+ candidate?.astNode !== undefined && candidate.astNode !== owner.astNode;
449
+ const realSibling = (from: number, step: number): CstNode | undefined => {
450
+ for (let scan = from; scan >= 0 && scan < content.length; scan += step) {
451
+ if (!content[scan].hidden) {
452
+ return content[scan];
453
+ }
454
+ }
455
+ return undefined;
456
+ };
457
+ const offsetOf = (node: CstNode): number => this.syntaxSpan(node)?.offset ?? node.offset;
458
+ const endOf = (node: CstNode): number => this.syntaxSpan(node)?.end ?? node.end;
459
+
460
+ const previous = realSibling(at - 1, -1);
461
+ const next = realSibling(at + 1, 1);
462
+
463
+ // Measured to whatever comes NEXT IN SOURCE, comment or not — never to the
464
+ // next real node, which would skip the rest of a comment block and make
465
+ // every line of it re-emit the whole gap that follows the block.
466
+ const following = content[at + 1];
467
+ const blankLinesAfter = following === undefined ? 0 : this.countBlankLines(source.slice(comment.end, offsetOf(following)));
468
+ const preceding = at > 0 ? content[at - 1] : undefined;
469
+ // From the last text before the comment: a block it is lifted out of can
470
+ // end with comment lines of its own, after the block's syntax.
471
+ const precedingEnd = preceding === undefined ? comment.offset : endOf(preceding);
472
+ let gapStart = comment.offset;
473
+ while (gapStart > precedingEnd && /\s/.test(source[gapStart - 1])) {
474
+ gapStart--;
475
+ }
476
+ const blankLinesBefore = preceding === undefined ? 0 : this.countBlankLines(source.slice(gapStart, comment.offset));
477
+
478
+ // Only a comment that OWNS its line can be reindented — one sharing a line
479
+ // with code keeps whatever spacing that line gives it.
480
+ const lineStart = source.lastIndexOf('\n', comment.offset - 1) + 1;
481
+ const beforeOnLine = source.slice(lineStart, comment.offset);
482
+ const sourceIndent = beforeOnLine.trim().length === 0 ? beforeOnLine : undefined;
483
+
484
+ const text = this.normalizeLineEndings(comment.text);
485
+ const sharesLineWithPrevious = previous !== undefined && !this.breaksLine(source, endOf(previous), comment.offset);
486
+
487
+ if (sharesLineWithPrevious) {
488
+ // On the same line as whatever precedes it, so it annotates that line.
489
+ // When the preceding sibling is the CONTAINER's own syntax — an opening
490
+ // brace, a name token, a keyword — the line is the container's header
491
+ // and the comment belongs to the container, NOT to the first member
492
+ // that happens to follow. Reading it as that member's leading comment
493
+ // moves a note about the declaration onto its first child.
494
+ return ownsOwnNode(previous)
495
+ ? { text, anchor: previous.astNode!, placement: 'trailing', blankLinesAfter: 0, blankLinesBefore: 0 }
496
+ : { text, anchor: owner.astNode!, placement: 'trailingOnContainer', blankLinesAfter: 0, blankLinesBefore: 0 };
497
+ }
498
+ if (ownsOwnNode(next)) {
499
+ return { text, anchor: next!.astNode!, placement: 'leading', blankLinesAfter, blankLinesBefore: 0, sourceIndent };
500
+ }
501
+ if (ownsOwnNode(previous)) {
502
+ return { text, anchor: previous!.astNode!, placement: 'afterNode', blankLinesAfter: 0, blankLinesBefore, sourceIndent };
503
+ }
504
+ if (previous === undefined) {
505
+ return { text, anchor: owner.astNode!, placement: 'atContainerStart', blankLinesAfter, blankLinesBefore: 0, sourceIndent };
506
+ }
507
+ // Surrounded by the container's own syntax on both sides. A further
508
+ // sibling after it means the comment sits INSIDE the construct — between
509
+ // the braces of an empty body, or partway through a header — so it stays
510
+ // with the container rather than being pushed past its closing syntax,
511
+ // which would move it out to the enclosing scope. Only a comment with
512
+ // nothing after it at all actually trails the container.
513
+ const placement: CommentPlacement = next === undefined ? 'atContainerEnd' : 'trailingOnContainer';
514
+ return { text, anchor: owner.astNode!, placement, blankLinesAfter, blankLinesBefore, sourceIndent };
515
+ }
516
+
517
+ /**
518
+ * Resolves the container {@link classify} anchors `comment` in, and the
519
+ * comment's adjacent siblings there and the nearest syntax on each side,
520
+ * without INDENT and DEDENT tokens. The content is that window, not all of
521
+ * the container's siblings.
522
+ *
523
+ * An indentation-aware lexer can emit a block's DEDENT after the comment
524
+ * lines that follow the block, so the parser nests them in it. An own-line
525
+ * comment left of the column a block's INDENT opened is lifted out of each
526
+ * block that ends right after it, with its adjacent hidden nodes, and placed
527
+ * after that block in the enclosing container.
528
+ */
529
+ protected placeAmongSyntax(
530
+ comment: CstNode,
531
+ index: number,
532
+ container: CompositeCstNode,
533
+ source: string
534
+ ): { readonly container: CompositeCstNode; readonly content: readonly CstNode[]; readonly index: number } {
535
+ const content = container.content;
536
+ const run = this.resolveCommentRun(index, container, source);
537
+ let placed = 0;
538
+ if (run.targets.length > 1 && (run.previous === undefined || this.breaksLine(source, run.previous.end, comment.offset))) {
539
+ const column = this.columnAt(source, comment.offset);
540
+ while (placed + 1 < run.targets.length && column < (run.targets[placed + 1].liftBelow ?? 0)) {
541
+ placed++;
542
+ }
543
+ }
544
+ const { container: parent, closed } = run.targets[placed];
545
+ const before = closed ? [closed] : run.before;
546
+ const around = content.slice(Math.max(index - 1, run.start), Math.min(index + 1, run.end) + 1);
547
+ const view = [...before, ...around, ...run.after[placed]];
548
+ return { container: parent, content: view, index: before.length + (index > run.start ? 1 : 0) };
549
+ }
550
+
551
+ /**
552
+ * The run of hidden nodes around `container.content[index]`, and the blocks
553
+ * it can be lifted out of: each that ends right after the run and whose
554
+ * INDENT opened a column, up to the first that does not.
555
+ */
556
+ protected resolveCommentRun(index: number, container: CompositeCstNode, source: string): CommentRun {
557
+ const content = container.content;
558
+ const cached = this.commentRun;
559
+ // Resolved once per run, not per comment: a run of k comment lines would
560
+ // otherwise cost k scans of the run.
561
+ if (cached?.content === content && index >= cached.start && index <= cached.end) {
562
+ return cached;
563
+ }
564
+ let start = index;
565
+ while (start > 0 && content[start - 1].hidden) {
566
+ start--;
567
+ }
568
+ let end = index;
569
+ while (end + 1 < content.length && content[end + 1].hidden) {
570
+ end++;
571
+ }
572
+ let previous: { readonly end: number } | undefined;
573
+ const targets: CommentLiftTarget[] = [{ container, last: end }];
574
+ if (this.getIndentationTokenNames() !== undefined) {
575
+ for (let scan = start - 1; scan >= 0 && previous === undefined; scan--) {
576
+ previous = content[scan].hidden ? undefined : this.syntaxSpan(content[scan]);
577
+ }
578
+ let target = targets[0];
579
+ while (target.container.container && this.closesAfter(target.container.content, target.last)) {
580
+ const opened = this.openedColumn(target.container.content, target.closed ? target.last : start, source) ?? 0;
581
+ if (opened === 0) {
582
+ break;
583
+ }
584
+ const parent = target.container.container;
585
+ target = {
586
+ container: parent,
587
+ last: this.indexIn(parent.content, target.container),
588
+ closed: target.container,
589
+ liftBelow: opened
590
+ };
591
+ targets.push(target);
592
+ }
593
+ }
594
+ const before = this.neighbours(content, start - 1, -1).reverse();
595
+ const after = targets.map(target => this.neighbours(target.container.content, target.last + 1, 1));
596
+ return (this.commentRun = { content, start, end, previous, targets, before, after });
597
+ }
598
+
599
+ /** The nodes of `content` from `from` in `step` direction up to the first that is syntax, without indentation tokens. */
600
+ protected neighbours(content: readonly CstNode[], from: number, step: 1 | -1): CstNode[] {
601
+ const found: CstNode[] = [];
602
+ for (let scan = from; scan >= 0 && scan < content.length; scan += step) {
603
+ const node = content[scan];
604
+ if (!this.isIndentation(node)) {
605
+ found.push(node);
606
+ if (!node.hidden) {
607
+ break;
608
+ }
609
+ }
610
+ }
611
+ return found;
612
+ }
613
+
614
+ /** Whether only hidden and indentation tokens follow `index` in `content`. */
615
+ protected closesAfter(content: readonly CstNode[], index: number): boolean {
616
+ for (let scan = index + 1; scan < content.length; scan++) {
617
+ if (!content[scan].hidden && !this.isIndentation(content[scan])) {
618
+ return false;
619
+ }
620
+ }
621
+ return true;
622
+ }
623
+
624
+ /** The column the last INDENT before `content[index]` opens, `undefined` without one. */
625
+ protected openedColumn(content: readonly CstNode[], index: number, source: string): number | undefined {
626
+ const indent = this.getIndentationTokenNames()?.indent;
627
+ for (let scan = index - 1; scan >= 0; scan--) {
628
+ if (tokenNameOf(content[scan]) === indent) {
629
+ return this.columnAt(source, content[scan].end);
630
+ }
631
+ }
632
+ return undefined;
633
+ }
634
+
635
+ /**
636
+ * The index of `node` in `content`, found by offset: `indexOf` per lifted
637
+ * block makes extraction quadratic in a block's siblings.
638
+ */
639
+ protected indexIn(content: readonly CstNode[], node: CstNode): number {
640
+ let low = 0;
641
+ let high = content.length;
642
+ while (low < high) {
643
+ const middle = (low + high) >> 1;
644
+ if (content[middle].offset < node.offset) {
645
+ low = middle + 1;
646
+ } else {
647
+ high = middle;
648
+ }
649
+ }
650
+ // Zero-width tokens can share the node's offset.
651
+ for (let index = low; index < content.length && content[index].offset === node.offset; index++) {
652
+ if (content[index] === node) {
653
+ return index;
654
+ }
655
+ }
656
+ return content.indexOf(node);
657
+ }
658
+
659
+ /** Whether `text` has a line break from `from` up to `to`, without copying the span. */
660
+ protected breaksLine(text: string, from: number, to: number): boolean {
661
+ // Not indexOf, which scans on to the next line break past `to`.
662
+ for (let offset = from; offset < to; offset++) {
663
+ if (text.charCodeAt(offset) === 10) {
664
+ return true;
665
+ }
666
+ }
667
+ return false;
668
+ }
669
+
670
+ /** The column of `offset` in `text`. */
671
+ protected columnAt(text: string, offset: number): number {
672
+ return offset - (text.lastIndexOf('\n', offset - 1) + 1);
673
+ }
674
+
675
+ /**
676
+ * Comment text with CRLF reduced to LF.
677
+ *
678
+ * Serializers emit LF, so splicing a comment captured from a CRLF document
679
+ * verbatim puts lone CR bytes in the middle of LF-terminated lines.
680
+ */
681
+ protected normalizeLineEndings(text: string): string {
682
+ return text.includes('\r') ? text.replace(/\r\n/g, '\n') : text;
683
+ }
684
+
685
+ /** Blank lines in a run of whitespace — one fewer than its newlines. */
686
+ protected countBlankLines(gap: string): number {
687
+ return Math.max(0, (gap.match(/\n/g)?.length ?? 0) - 1);
688
+ }
689
+
690
+ /**
691
+ * Splice the captured comments back into `serialized`.
692
+ *
693
+ * A comment whose anchor the write deleted, or whose anchor has no stable
694
+ * key, is dropped — counted at `debug`, because a silent drop is the failure
695
+ * this preserver exists to remove and an unexplained one just moves it.
696
+ */
697
+ apply(serialized: string, trivia: CommentTrivia, uri: URI): string {
698
+ if (trivia.comments.length === 0) {
699
+ return serialized;
700
+ }
701
+ // The memos are scoped to this call and not to the instance, because an
702
+ // in-place write changes what a node's key IS: a repair that renames a
703
+ // node between two writes would otherwise be answered from the first.
704
+ this.keyMemo = new Map();
705
+ this.listVerdicts = new Map();
706
+ this.lastParse = {};
707
+ try {
708
+ return this.applyComments(serialized, trivia, uri);
709
+ } finally {
710
+ this.keyMemo = undefined;
711
+ this.listVerdicts = undefined;
712
+ this.lastParse = undefined;
713
+ }
714
+ }
715
+
716
+ /** {@link apply}'s body, inside the per-write memos it sets up. */
717
+ protected applyComments(serialized: string, trivia: CommentTrivia, uri: URI): string {
718
+ const spliced = this.spliceComments(serialized, trivia, uri);
719
+ // **Never hand back text the grammar cannot read.** A splice edits syntax
720
+ // it did not produce, and a line comment in particular ends whatever
721
+ // shares its line — so a placement that is merely wrong becomes a file
722
+ // that no longer parses, written to disk by an integrity repair with no
723
+ // user gesture. Dropping the comments is recoverable; corrupting the
724
+ // document is not.
725
+ if (spliced !== serialized && this.parse(spliced, uri) === undefined) {
726
+ this.tracer.withUri(uri.toString()).warn('Reattaching comments produced text that does not parse; writing without them');
727
+ return serialized;
728
+ }
729
+ return spliced;
730
+ }
731
+
732
+ /**
733
+ * {@link anchorKey}, answered once per node per write.
734
+ *
735
+ * The key walks `$container` to the root building a segment each step, and
736
+ * every stage of a write asks about the same nodes — locating spans, matching
737
+ * a rename, judging a key the output kept. Recomputing makes each of those
738
+ * stages cost nodes × depth, and the stages that scan a sibling list do it
739
+ * once per comment.
740
+ */
741
+ protected cachedAnchorKey(node: AstNode): string | undefined {
742
+ const memo = this.keyMemo;
743
+ if (memo === undefined) {
744
+ return this.anchorKey(node);
745
+ }
746
+ if (!memo.has(node)) {
747
+ memo.set(node, this.anchorKey(node));
748
+ }
749
+ return memo.get(node);
750
+ }
751
+
752
+ /**
753
+ * Parse `text` as an unregistered document, or `undefined` when it is not
754
+ * readable — by reported error, or by a throw from a URI that routes to no
755
+ * services. Does NOT register the result, so `LangiumDocuments` is untouched.
756
+ *
757
+ * Within one {@link apply}, the text and URI parsed last are answered with
758
+ * the same document rather than parsed again, so a caller must not change
759
+ * it. One entry, so no document outlives the next parse.
760
+ */
761
+ protected parse(text: string, uri: URI): LangiumDocument | undefined {
762
+ const memo = this.lastParse;
763
+ if (memo !== undefined && memo.text === text && memo.uri === uri.toString()) {
764
+ return memo.document;
765
+ }
766
+ let document: LangiumDocument | undefined;
767
+ try {
768
+ document = this.services.shared.workspace.LangiumDocumentFactory.fromString(text, uri);
769
+ } catch {
770
+ document = undefined;
771
+ }
772
+ if (document !== undefined && hasSyntaxErrors(document.parseResult)) {
773
+ document = undefined;
774
+ }
775
+ if (memo !== undefined) {
776
+ memo.text = text;
777
+ memo.uri = uri.toString();
778
+ memo.document = document;
779
+ }
780
+ return document;
781
+ }
782
+
783
+ /** The splice itself, run before {@link apply}'s parse check. */
784
+ protected spliceComments(serialized: string, trivia: CommentTrivia, uri: URI): string {
785
+ const located = this.locate(serialized, uri);
786
+ if (located === undefined) {
787
+ return serialized;
788
+ }
789
+
790
+ const sourceOwners = new Map<string, AstNode>();
791
+ const sourceCollisions = new Set<string>();
792
+ const liveAnchors = new Set<AstNode>();
793
+ for (const node of [trivia.sourceRoot, ...AstUtils.streamAllContents(trivia.sourceRoot)]) {
794
+ liveAnchors.add(node);
795
+ const key = this.cachedAnchorKey(node);
796
+ if (key === undefined) {
797
+ continue;
798
+ }
799
+ const owner = sourceOwners.get(key);
800
+ if (owner !== undefined && owner !== node) {
801
+ sourceCollisions.add(key);
802
+ } else {
803
+ sourceOwners.set(key, node);
804
+ }
805
+ }
806
+ const needsRename = trivia.comments.some(comment => {
807
+ const key = this.cachedAnchorKey(comment.anchor);
808
+ return key !== undefined && !located.has(key);
809
+ });
810
+ const renamed = needsRename
811
+ ? this.matchRenamedAnchors(trivia.sourceRoot, serialized, uri, located, sourceOwners, sourceCollisions)
812
+ : new Map<AstNode, string>();
813
+
814
+ const edits: InlineAwareEdit[] = [];
815
+ let dropped = 0;
816
+ trivia.comments.forEach((comment, order) => {
817
+ const key = this.cachedAnchorKey(comment.anchor);
818
+ const destination = key === undefined ? undefined : located.has(key) ? key : this.renamedKey(comment.anchor, key, renamed);
819
+ const retained = destination === key ? located.get(key!) : undefined;
820
+ const span =
821
+ key === undefined ||
822
+ !liveAnchors.has(comment.anchor) ||
823
+ sourceCollisions.has(key) ||
824
+ destination === undefined ||
825
+ (retained !== undefined && this.ambiguousRetainedKey(comment.anchor, retained.owner))
826
+ ? undefined
827
+ : located.get(destination);
828
+ if (span === undefined) {
829
+ dropped++;
830
+ return;
831
+ }
832
+ const edit = this.editFor(comment, span, serialized);
833
+ edits.push({
834
+ ...edit,
835
+ order,
836
+ inline: this.needsReadBack(comment, span, serialized) ? { text: comment.text, key: destination! } : undefined
837
+ });
838
+ });
839
+
840
+ if (dropped > 0) {
841
+ this.tracer.withUri(uri.toString()).debug(`Dropped ${dropped} comment(s): anchor deleted by the write, or carrying no stable key`);
842
+ }
843
+
844
+ // Stable by offset, then by capture order, so several comments landing on
845
+ // one anchor keep the order the author wrote them in.
846
+ edits.sort((left, right) => left.at - right.at || left.order - right.order);
847
+ // **This is what licenses the mid-line split** `editFor` makes for a
848
+ // `leading` comment whose anchor does not start its line. Parsing alone
849
+ // cannot license it: text can parse perfectly well having handed the
850
+ // comment to the following declaration instead. Re-extracting and
851
+ // requiring the same anchor back is the only evidence the comment still
852
+ // belongs where its author put it.
853
+ //
854
+ // A failed edit is dropped and the rest reassembled, since removing one
855
+ // changes what the others land in; two rounds, then the inline edits go
856
+ // as a set rather than iterating towards a text nothing has verified.
857
+ let remaining: readonly InlineAwareEdit[] = edits;
858
+ let result = this.assembleComments(serialized, remaining);
859
+ for (let attempt = 0; attempt < 2; attempt++) {
860
+ const failed = this.unverifiedInlineEdits(result, remaining, uri, serialized);
861
+ if (failed.size === 0) {
862
+ return result;
863
+ }
864
+ remaining = remaining.filter(edit => !failed.has(edit));
865
+ result = this.assembleComments(serialized, remaining);
866
+ }
867
+ return this.assembleComments(
868
+ serialized,
869
+ remaining.filter(edit => edit.inline === undefined)
870
+ );
871
+ }
872
+
873
+ /**
874
+ * Whether this comment's edit has to be read back before it can be trusted.
875
+ *
876
+ * Both cases are the serializer having put the anchor on a line it shares.
877
+ * A comment that wants the line above has to open one mid-construct; a
878
+ * comment that wants the end of the anchor's line gets the end of a line that
879
+ * may belong to a later sibling, and a second such comment lands inside the
880
+ * first one's text and stops being a comment at all. Neither can be judged
881
+ * from the offsets alone — only from what the text says once re-read.
882
+ */
883
+ protected needsReadBack(comment: DocumentComment, span: AnchorSpan, serialized: string): boolean {
884
+ if (comment.placement === 'leading' || comment.placement === 'atContainerStart') {
885
+ return !this.startOfLineIsBlank(serialized, span.offset);
886
+ }
887
+ return comment.placement === 'trailing' && this.endOfLineAt(serialized, span.end) !== span.end;
888
+ }
889
+
890
+ /**
891
+ * Splice `edits` into `serialized`, in the order given.
892
+ *
893
+ * Two inline edits landing on one offset collapse into a single opened line,
894
+ * so a stack of comments above one inline member comes back as a stack rather
895
+ * than with a blank line between each pair.
896
+ */
897
+ protected assembleComments(serialized: string, edits: readonly InlineAwareEdit[]): string {
898
+ let result = '';
899
+ let cursor = 0;
900
+ let previousInlineAt: number | undefined;
901
+ for (const edit of edits) {
902
+ const text = edit.inline !== undefined && edit.at === previousInlineAt ? edit.text.replace(/^\n[ \t]*/, '') : edit.text;
903
+ result += serialized.slice(cursor, edit.at) + text;
904
+ cursor = edit.at;
905
+ previousInlineAt = edit.inline !== undefined ? edit.at : undefined;
906
+ }
907
+ return result + serialized.slice(cursor);
908
+ }
909
+
910
+ /**
911
+ * Whether the output node now answering to `source`'s key is too likely to be
912
+ * a DIFFERENT node for the comment to follow it.
913
+ *
914
+ * A key surviving the write normally means the node did. It can also mean a
915
+ * sibling took the name over, which reads as the author having written the
916
+ * comment about a declaration they never saw.
917
+ *
918
+ * **What is detectable is a list whose members changed places; what is not is
919
+ * a payload that renames a node and gives its old name to another.** Those
920
+ * two writes produce the same text and the same keys, and the node carrying
921
+ * the old name afterwards is byte-identical under both readings, so no rule
922
+ * over this evidence separates them. A caller that knows which node it
923
+ * renamed is the only thing that could, and the transfer model carries no
924
+ * such record.
925
+ *
926
+ * **The evidence is the ORDER of the keys the two lists share.** Adding or
927
+ * removing siblings shifts the rest but leaves them in the same relative
928
+ * order; only names changing places can put two shared keys out of order. A
929
+ * list whose shared keys invert is therefore read as exchanged identities and
930
+ * those siblings lose their comments — which costs a deliberate reorder its
931
+ * comments, the conservative half of a trade whose other half is that a bare
932
+ * swap cannot move one.
933
+ *
934
+ * **Comparing counts or key SETS instead misses cases each way.** A swap
935
+ * alongside an insertion leaves the counts differing and the key set whole,
936
+ * so neither test sees it; a deletion alongside an insertion leaves the counts
937
+ * equal while every surviving sibling is still itself, so a count test drops
938
+ * comments that were never in doubt.
939
+ */
940
+ protected ambiguousRetainedKey(source: AstNode, output: AstNode): boolean {
941
+ const sourceParent = source.$container;
942
+ const outputParent = output.$container;
943
+ if (!sourceParent || !outputParent) {
944
+ return false;
945
+ }
946
+ const parentKey = this.cachedAnchorKey(sourceParent);
947
+ if (parentKey !== this.cachedAnchorKey(outputParent) || source.$containerProperty !== output.$containerProperty) {
948
+ return true;
949
+ }
950
+ if (source.$containerIndex === output.$containerIndex) {
951
+ return false;
952
+ }
953
+ const property = source.$containerProperty;
954
+ if (property === undefined) {
955
+ return true;
956
+ }
957
+ // Answered once per list rather than once per comment. One insertion moves
958
+ // every later sibling, so each of their comments asks the same question of
959
+ // the same two lists — and scanning both per comment costs the list's
960
+ // length squared.
961
+ //
962
+ // Memoised against the parent NODE rather than its key, because two
963
+ // same-named parents answer to one key and would otherwise share a verdict
964
+ // about lists that have nothing to do with each other.
965
+ const cached = this.listVerdicts?.get(sourceParent)?.get(property);
966
+ if (cached !== undefined) {
967
+ return cached;
968
+ }
969
+ const verdict = this.reidentifiedList(sourceParent, outputParent, property);
970
+ if (this.listVerdicts !== undefined) {
971
+ const byProperty = this.listVerdicts.get(sourceParent) ?? new Map<string, boolean>();
972
+ byProperty.set(property, verdict);
973
+ this.listVerdicts.set(sourceParent, byProperty);
974
+ }
975
+ return verdict;
976
+ }
977
+
978
+ /**
979
+ * Whether the two lists differ in a way a plain insertion or deletion cannot
980
+ * explain — the expensive half of {@link ambiguousRetainedKey}, split out so
981
+ * it can be answered once per list.
982
+ *
983
+ * What says the members may have swapped names rather than moved:
984
+ *
985
+ * - **The lists are the same length.** Every rename chain over a fixed set of
986
+ * slots looks exactly like the shift a deletion plus an insertion produces,
987
+ * and for members carrying nothing but a name the properties match under
988
+ * both readings. Neither this nor anything downstream can separate them.
989
+ * - **Keys present on both sides have changed places.** Adding or removing
990
+ * siblings shifts the rest but never reorders them, so an inversion is
991
+ * evidence no insertion or deletion can account for — and it is evidence
992
+ * the lengths alone miss, since a swap alongside an insertion leaves the
993
+ * counts differing and the key set whole.
994
+ */
995
+ protected reidentifiedList(sourceParent: AstNode, outputParent: AstNode, property: string): boolean {
996
+ const before = (sourceParent as unknown as Record<string, unknown>)[property];
997
+ const after = (outputParent as unknown as Record<string, unknown>)[property];
998
+ if (!Array.isArray(before) || !Array.isArray(after)) {
999
+ return true;
1000
+ }
1001
+ if (before.length === after.length) {
1002
+ return true;
1003
+ }
1004
+ const positions = new Map<string, number>();
1005
+ after.filter(isAstNode).forEach((node: AstNode, index: number) => {
1006
+ const key = this.cachedAnchorKey(node);
1007
+ if (key !== undefined && !positions.has(key)) {
1008
+ positions.set(key, index);
1009
+ }
1010
+ });
1011
+ let furthest = -1;
1012
+ for (const node of before.filter(isAstNode) as AstNode[]) {
1013
+ const key = this.cachedAnchorKey(node);
1014
+ const position = key === undefined ? undefined : positions.get(key);
1015
+ if (position === undefined) {
1016
+ continue;
1017
+ }
1018
+ if (position < furthest) {
1019
+ return true;
1020
+ }
1021
+ furthest = position;
1022
+ }
1023
+ return false;
1024
+ }
1025
+
1026
+ /**
1027
+ * The inline edits `candidate` did not round-trip: each one whose comment came
1028
+ * back on a different anchor, and — when `candidate` does not parse at all —
1029
+ * each one that cannot be put back beside the others without breaking it
1030
+ * again. `serialized` is the unspliced text those trials are rebuilt from,
1031
+ * since finding the single edit at fault means assembling the rest without it.
1032
+ *
1033
+ * Costs a parse and an extract, so it answers empty without either when
1034
+ * nothing was spliced inline — which is every write into a document the
1035
+ * serializer lays out one declaration per line.
1036
+ */
1037
+ protected unverifiedInlineEdits(
1038
+ candidate: string,
1039
+ edits: readonly InlineAwareEdit[],
1040
+ uri: URI,
1041
+ serialized: string
1042
+ ): Set<InlineAwareEdit> {
1043
+ const inlineEdits = edits.filter(edit => edit.inline !== undefined);
1044
+ if (inlineEdits.length === 0) {
1045
+ return new Set();
1046
+ }
1047
+ const reparsed = this.parse(candidate, uri);
1048
+ if (reparsed === undefined) {
1049
+ if (inlineEdits.length > this.maxIsolatedEdits) {
1050
+ this.tracer
1051
+ .withUri(uri.toString())
1052
+ .debug(`Spliced text did not parse; dropping ${inlineEdits.length} shared-line comments without isolating`);
1053
+ return new Set(inlineEdits);
1054
+ }
1055
+ const retained = edits.filter(edit => edit.inline === undefined);
1056
+ const failed = new Set<InlineAwareEdit>();
1057
+ for (const edit of inlineEdits) {
1058
+ const trial = [...retained, edit].sort((left, right) => left.at - right.at || left.order - right.order);
1059
+ const text = this.assembleComments(serialized, trial);
1060
+ if (this.parse(text, uri) !== undefined && this.unverifiedInlineEdits(text, trial, uri, serialized).size === 0) {
1061
+ retained.push(edit);
1062
+ } else {
1063
+ failed.add(edit);
1064
+ }
1065
+ }
1066
+ return failed;
1067
+ }
1068
+ // Counted rather than scanned per edit: several comments can share an
1069
+ // anchor and a text, and each edit must consume one of them.
1070
+ const captured = new Map<string, number>();
1071
+ for (const comment of this.extract(reparsed).comments) {
1072
+ const seen = `${this.cachedAnchorKey(comment.anchor) ?? ''}\u0000${comment.text}`;
1073
+ captured.set(seen, (captured.get(seen) ?? 0) + 1);
1074
+ }
1075
+ const failed = new Set<InlineAwareEdit>();
1076
+ for (const edit of inlineEdits) {
1077
+ const expected = edit.inline!;
1078
+ const wanted = `${expected.key}\u0000${expected.text}`;
1079
+ const remaining = captured.get(wanted) ?? 0;
1080
+ if (remaining === 0) {
1081
+ failed.add(edit);
1082
+ } else {
1083
+ captured.set(wanted, remaining - 1);
1084
+ }
1085
+ }
1086
+ return failed;
1087
+ }
1088
+
1089
+ protected renamedKey(anchor: AstNode, oldKey: string, renamed: Map<AstNode, string>): string | undefined {
1090
+ let current: AstNode | undefined = anchor;
1091
+ while (current !== undefined) {
1092
+ const newPrefix = renamed.get(current);
1093
+ const oldPrefix = this.cachedAnchorKey(current);
1094
+ if (newPrefix !== undefined && oldPrefix !== undefined && (oldKey === oldPrefix || oldKey.startsWith(`${oldPrefix}/`))) {
1095
+ return newPrefix + oldKey.slice(oldPrefix.length);
1096
+ }
1097
+ current = current.$container;
1098
+ }
1099
+ return undefined;
1100
+ }
1101
+
1102
+ protected matchRenamedAnchors(
1103
+ sourceRoot: AstNode,
1104
+ serialized: string,
1105
+ uri: URI,
1106
+ located: Map<string, AnchorSpan>,
1107
+ sourceOwners: Map<string, AstNode>,
1108
+ sourceCollisions: Set<string>
1109
+ ): Map<AstNode, string> {
1110
+ const matches = new Map<AstNode, string>();
1111
+ const output = this.parse(serialized, uri);
1112
+ if (output === undefined) {
1113
+ return matches;
1114
+ }
1115
+ const outputNodes = [output.parseResult.value, ...AstUtils.streamAllContents(output.parseResult.value)];
1116
+ const unmatchedBySlot = new Map<string, AstNode[]>();
1117
+ for (const node of outputNodes) {
1118
+ const key = this.cachedAnchorKey(node);
1119
+ const parent = node.$container && this.cachedAnchorKey(node.$container);
1120
+ if (key === undefined || !located.has(key) || sourceOwners.has(key) || parent === undefined) {
1121
+ continue;
1122
+ }
1123
+ const slot = JSON.stringify([parent, node.$containerProperty, node.$containerIndex, node.$type]);
1124
+ const candidates = unmatchedBySlot.get(slot) ?? [];
1125
+ candidates.push(node);
1126
+ unmatchedBySlot.set(slot, candidates);
1127
+ }
1128
+ const claimed = new Map<AstNode, AstNode>();
1129
+ const ambiguous = new Set<AstNode>();
1130
+ for (const source of [sourceRoot, ...AstUtils.streamAllContents(sourceRoot)]) {
1131
+ const key = this.cachedAnchorKey(source);
1132
+ if (key === undefined || sourceCollisions.has(key) || located.has(key) || !source.$container) {
1133
+ continue;
1134
+ }
1135
+ const parentKey = this.cachedAnchorKey(source.$container);
1136
+ if (parentKey === undefined) {
1137
+ continue;
1138
+ }
1139
+ const slot = JSON.stringify([parentKey, source.$containerProperty, source.$containerIndex, source.$type]);
1140
+ const candidates = (unmatchedBySlot.get(slot) ?? []).filter(candidate => this.sameAsideFromName(source, candidate));
1141
+ if (candidates.length === 1) {
1142
+ const candidate = candidates[0];
1143
+ if (ambiguous.has(candidate)) {
1144
+ continue;
1145
+ }
1146
+ if (claimed.has(candidate)) {
1147
+ matches.delete(claimed.get(candidate)!);
1148
+ ambiguous.add(candidate);
1149
+ } else {
1150
+ matches.set(source, this.cachedAnchorKey(candidate)!);
1151
+ }
1152
+ claimed.set(candidate, source);
1153
+ }
1154
+ }
1155
+ return matches;
1156
+ }
1157
+
1158
+ protected sameAsideFromName(source: AstNode, target: AstNode): boolean {
1159
+ const nameProvider = this.services.references.NameProvider;
1160
+ const before = nameProvider.getOwnName(source);
1161
+ const after = nameProvider.getOwnName(target);
1162
+ if (before === undefined || after === undefined || before === after) {
1163
+ return false;
1164
+ }
1165
+ let renamed = false;
1166
+ for (const key of this.comparableProperties(source.$type)) {
1167
+ const oldValue = (source as unknown as Record<string, unknown>)[key];
1168
+ const newValue = (target as unknown as Record<string, unknown>)[key];
1169
+ if (oldValue === before && newValue === after) {
1170
+ renamed = true;
1171
+ } else if (!this.sameValue(oldValue, newValue)) {
1172
+ return false;
1173
+ }
1174
+ }
1175
+ return renamed;
1176
+ }
1177
+
1178
+ /**
1179
+ * The properties `type` declares in the grammar — everything a rename match
1180
+ * may compare, and nothing else.
1181
+ *
1182
+ * **Comparing own keys instead compares derived state, and matches nothing.**
1183
+ * A built document carries whatever `ast.extensions` computed onto its nodes;
1184
+ * the re-parsed serializer output is never built and carries none of it. Every
1185
+ * candidate then differs on a property the grammar never mentioned, so no
1186
+ * rename is ever matched — on the real write path only, because a document a
1187
+ * test parses from a string has no computed state to disagree about.
1188
+ */
1189
+ protected comparableProperties(type: string): readonly string[] {
1190
+ return Object.keys(this.services.shared.AstReflection.getTypeMetaData(type).properties);
1191
+ }
1192
+
1193
+ protected sameValue(left: unknown, right: unknown): boolean {
1194
+ if (left === right) {
1195
+ return true;
1196
+ }
1197
+ if (Array.isArray(left) && Array.isArray(right)) {
1198
+ return left.length === right.length && left.every((value, index) => this.sameValue(value, right[index]));
1199
+ }
1200
+ if (isAstNode(left) && isAstNode(right)) {
1201
+ if (left.$type !== right.$type) {
1202
+ return false;
1203
+ }
1204
+ return this.comparableProperties(left.$type).every(key =>
1205
+ this.sameValue((left as unknown as Record<string, unknown>)[key], (right as unknown as Record<string, unknown>)[key])
1206
+ );
1207
+ }
1208
+ if (left !== null && right !== null && typeof left === 'object' && typeof right === 'object') {
1209
+ if ('$refText' in left && '$refText' in right) {
1210
+ return left.$refText === right.$refText;
1211
+ }
1212
+ }
1213
+ return false;
1214
+ }
1215
+
1216
+ /** The insertion this comment's placement implies, against its anchor's span. */
1217
+ protected editFor(comment: DocumentComment, span: AnchorSpan, serialized: string): { at: number; text: string } {
1218
+ const blanks = '\n'.repeat(comment.blankLinesAfter);
1219
+ const blanksBefore = '\n'.repeat(comment.blankLinesBefore);
1220
+ switch (comment.placement) {
1221
+ case 'trailing':
1222
+ return { at: this.endOfLineAt(serialized, span.end), text: ` ${comment.text}` };
1223
+ // Appended to the container's FIRST line, which is its header. The
1224
+ // comment may have sat further along that line in the source — mid
1225
+ // header, or between braces the serializer has since collapsed to `{}`
1226
+ // — and the exact column is not recoverable from a re-emitted
1227
+ // document. The line is, and that is what keeps it on its own
1228
+ // declaration instead of on a neighbour.
1229
+ case 'trailingOnContainer':
1230
+ return { at: this.endOfLineAt(serialized, span.offset), text: ` ${comment.text}` };
1231
+ // Both of these open a NEW line after the anchor, which is only safe
1232
+ // while nothing else shares the anchor's line. When the serializer put
1233
+ // the anchor and its container's closing syntax together — an inline
1234
+ // enum body, say — splitting there pushes that syntax onto the comment's
1235
+ // line, and a line comment then ends the construct. Appending to the
1236
+ // line instead keeps the comment on the same declaration and the
1237
+ // document readable.
1238
+ case 'afterNode': {
1239
+ if (!this.restOfLineIsBlank(serialized, span.end)) {
1240
+ return { at: this.endOfLineAt(serialized, span.end), text: ` ${comment.text}` };
1241
+ }
1242
+ const indent = this.indentAt(serialized, span.offset);
1243
+ return { at: span.end, text: `\n${blanksBefore}${indent}${this.reindent(comment, indent)}` };
1244
+ }
1245
+ case 'atContainerEnd':
1246
+ return this.restOfLineIsBlank(serialized, span.end)
1247
+ ? { at: span.end, text: `\n${blanksBefore}${comment.text}` }
1248
+ : { at: this.endOfLineAt(serialized, span.end), text: ` ${comment.text}` };
1249
+ case 'atContainerStart':
1250
+ case 'leading':
1251
+ default: {
1252
+ // `leading` means "on the line above", and the anchor may sit
1253
+ // mid-line — a member of a body the serializer emitted inline.
1254
+ // **Opening a line there is sound only because the caller reads the
1255
+ // result back and requires this comment on this same anchor**,
1256
+ // dropping the edit when it is not. Unguarded, the split strands the
1257
+ // rest of the construct at column zero and lands somewhere different
1258
+ // again on the next write.
1259
+ if (!this.startOfLineIsBlank(serialized, span.offset)) {
1260
+ const indent = comment.sourceIndent ?? this.indentAt(serialized, span.offset);
1261
+ return { at: span.offset, text: `\n${indent}${this.reindent(comment, indent)}\n${blanks}${indent}` };
1262
+ }
1263
+ const indent = this.indentAt(serialized, span.offset);
1264
+ return { at: span.offset, text: `${this.reindent(comment, indent)}\n${blanks}${indent}` };
1265
+ }
1266
+ }
1267
+ }
1268
+
1269
+ /**
1270
+ * A multi-line comment's continuation lines, shifted by the same delta its
1271
+ * first line moves — so a block emitted at a new indentation stays square
1272
+ * instead of trailing its original column.
1273
+ *
1274
+ * **Only the leading whitespace run is touched, and an outdent removes at
1275
+ * most what is there** — shifting further would eat the comment's own text.
1276
+ *
1277
+ * Single-line comments and ones sharing a line with code are returned
1278
+ * unchanged.
1279
+ */
1280
+ protected reindent(comment: DocumentComment, targetIndent: string): string {
1281
+ if (comment.sourceIndent === undefined || !comment.text.includes('\n')) {
1282
+ return comment.text;
1283
+ }
1284
+ // Indentation is compared as characters, which only means anything while
1285
+ // both sides use the SAME whitespace character. A tab-indented source
1286
+ // re-emitted with spaces has no meaningful delta, and shifting by one
1287
+ // anyway prepends spaces in front of tabs.
1288
+ const sourceUnit = /^\t*$/.test(comment.sourceIndent) ? '\t' : ' ';
1289
+ const targetUnit = /^\t*$/.test(targetIndent) ? '\t' : ' ';
1290
+ const delta = targetIndent.length - comment.sourceIndent.length;
1291
+ if (delta === 0 || sourceUnit !== targetUnit) {
1292
+ return comment.text;
1293
+ }
1294
+ const [first, ...rest] = comment.text.split('\n');
1295
+ const shifted = rest.map(line => {
1296
+ if (delta > 0) {
1297
+ return targetUnit.repeat(delta) + line;
1298
+ }
1299
+ const removable = /^[ \t]*/.exec(line)?.[0].length ?? 0;
1300
+ return line.slice(Math.min(-delta, removable));
1301
+ });
1302
+ return [first, ...shifted].join('\n');
1303
+ }
1304
+
1305
+ /** Whether everything from `offset` to the end of its line is whitespace. */
1306
+ protected restOfLineIsBlank(text: string, offset: number): boolean {
1307
+ return text.slice(offset, this.endOfLineAt(text, offset)).trim().length === 0;
1308
+ }
1309
+
1310
+ /** Whether `offset` is preceded on its own line by whitespace alone. */
1311
+ protected startOfLineIsBlank(text: string, offset: number): boolean {
1312
+ return text.slice(text.lastIndexOf('\n', offset - 1) + 1, offset).trim().length === 0;
1313
+ }
1314
+
1315
+ /**
1316
+ * End of the line `offset` sits on, as an insertion point for a trailing
1317
+ * comment. Stops before a CR so an insertion into CRLF text lands inside the
1318
+ * line rather than between its two terminator bytes.
1319
+ */
1320
+ protected endOfLineAt(text: string, offset: number): number {
1321
+ const newline = text.indexOf('\n', offset);
1322
+ const lineEnd = newline < 0 ? text.length : newline;
1323
+ return lineEnd > 0 && text[lineEnd - 1] === '\r' ? lineEnd - 1 : lineEnd;
1324
+ }
1325
+
1326
+ /** Leading whitespace of the line `offset` sits on, so an insertion lines up with it. */
1327
+ protected indentAt(text: string, offset: number): string {
1328
+ const lineStart = text.lastIndexOf('\n', offset - 1) + 1;
1329
+ return /^\s*/.exec(text.slice(lineStart, offset))?.[0] ?? '';
1330
+ }
1331
+
1332
+ /**
1333
+ * Where every node landed in the serializer's output, keyed by
1334
+ * {@link anchorKey}.
1335
+ *
1336
+ * Re-parses `serialized` through the document factory, which does NOT
1337
+ * register the result, so this leaves `LangiumDocuments` alone. Output the
1338
+ * grammar cannot read, by reported error or by a throw from a URI that
1339
+ * routes to no services, yields `undefined`: the caller then writes the
1340
+ * serializer's text unchanged rather than splicing into text already wrong.
1341
+ *
1342
+ * **A key claimed by two DIFFERENT nodes is removed, not merged.** A repeated
1343
+ * identity is exactly what the integrity tier exists to repair, so collisions
1344
+ * reach this method routinely; merging their spans puts every one of their
1345
+ * comments on whichever came first. Removing the key drops those instead,
1346
+ * which is the only outcome here that keeps a comment off a declaration its
1347
+ * author did not write it on.
1348
+ */
1349
+ protected locate(serialized: string, uri: URI): Map<string, AnchorSpan> | undefined {
1350
+ const document = this.parse(serialized, uri);
1351
+ if (document === undefined) {
1352
+ this.tracer.withUri(uri.toString()).warn('Serialized output did not re-parse; writing it without comments');
1353
+ return undefined;
1354
+ }
1355
+ const root = document.parseResult.value.$cstNode;
1356
+ if (!root) {
1357
+ return undefined;
1358
+ }
1359
+ const found = new Map<string, AnchorSpan>();
1360
+ // One AST node owns many CST nodes — its composite plus every token under
1361
+ // it — so a repeated key is only a collision when a DIFFERENT node claims
1362
+ // it. Tracking the owner is what separates the two.
1363
+ const owners = new Map<string, AstNode>();
1364
+ const collided = new Set<string>();
1365
+ // One AST node owns every CST node beneath it, so this asks for the same
1366
+ // key once per token; `cachedAnchorKey` is what keeps the pass from
1367
+ // costing nodes x depth.
1368
+ const visit = (node: CstNode): void => {
1369
+ const key = node.astNode !== undefined && !node.hidden ? this.cachedAnchorKey(node.astNode) : undefined;
1370
+ // Without the indentation tokens, whose DEDENT would end a block at
1371
+ // the start of the line after it.
1372
+ const span = key === undefined ? undefined : this.syntaxSpan(node);
1373
+ if (key !== undefined && span !== undefined) {
1374
+ const owner = owners.get(key);
1375
+ if (owner === undefined) {
1376
+ owners.set(key, node.astNode!);
1377
+ found.set(key, { offset: span.offset, end: span.end, owner: node.astNode! });
1378
+ } else if (owner === node.astNode) {
1379
+ // Widest span per node: its first CST node gives the start, a
1380
+ // later token contributing to the same node extends the end.
1381
+ const existing = found.get(key)!;
1382
+ found.set(key, {
1383
+ offset: Math.min(existing.offset, span.offset),
1384
+ end: Math.max(existing.end, span.end),
1385
+ owner: existing.owner
1386
+ });
1387
+ } else {
1388
+ collided.add(key);
1389
+ }
1390
+ }
1391
+ if (isComposite(node)) {
1392
+ node.content.forEach(visit);
1393
+ }
1394
+ };
1395
+ visit(root);
1396
+ for (const key of collided) {
1397
+ found.delete(key);
1398
+ }
1399
+ return found;
1400
+ }
1401
+ }
1402
+
1403
+ /** Registers {@link CommentPreserver}; bound at `trivia.preservers.comments`. */
1404
+ export class CommentPreserverContribution implements TriviaContribution {
1405
+ constructor(protected readonly services: HydraniumLanguageServices) {}
1406
+
1407
+ registerTriviaPreservers(registry: TriviaRegistry): void {
1408
+ registry.register(new CommentPreserver(this.services));
1409
+ }
1410
+ }