@hydranium/data-server 1.0.0-next.38 → 1.0.0-next.381
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.
- package/README.md +57 -73
- package/lib/browser/index.d.ts +10 -0
- package/lib/browser/index.d.ts.map +1 -0
- package/lib/browser/index.js +15 -0
- package/lib/browser/index.js.map +1 -0
- package/lib/browser/start-data-server-in-worker.d.ts +23 -0
- package/lib/browser/start-data-server-in-worker.d.ts.map +1 -0
- package/lib/browser/start-data-server-in-worker.js +29 -0
- package/lib/browser/start-data-server-in-worker.js.map +1 -0
- package/lib/data-server.d.ts +439 -285
- package/lib/data-server.d.ts.map +1 -1
- package/lib/data-server.js +747 -501
- package/lib/data-server.js.map +1 -1
- package/lib/messages/index.d.ts +1 -1
- package/lib/messages/index.d.ts.map +1 -1
- package/lib/messages/index.js +1 -1
- package/lib/messages/index.js.map +1 -1
- package/lib/node/node-diagnostics-provider.d.ts.map +1 -1
- package/lib/node/node-diagnostics-provider.js +2 -1
- package/lib/node/node-diagnostics-provider.js.map +1 -1
- package/lib/testing/data-server-harness.d.ts +31 -14
- package/lib/testing/data-server-harness.d.ts.map +1 -1
- package/lib/testing/data-server-harness.js +8 -3
- package/lib/testing/data-server-harness.js.map +1 -1
- package/lib/testing/index.d.ts +1 -0
- package/lib/testing/index.d.ts.map +1 -1
- package/lib/testing/index.js.map +1 -1
- package/package.json +35 -43
- package/src/browser/index.ts +15 -0
- package/src/browser/start-data-server-in-worker.ts +35 -0
- package/src/data-server.ts +902 -539
- package/src/messages/index.ts +8 -1
- package/src/node/node-diagnostics-provider.ts +2 -8
- package/src/testing/data-server-harness.ts +55 -26
- package/src/testing/index.ts +5 -2
package/lib/data-server.js
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
*
|
|
7
7
|
* SPDX-License-Identifier: MIT
|
|
8
8
|
********************************************************************************/
|
|
9
|
-
import { createRpcProxy, defineMessage, DisposableCollection, isDocumentSource, isElementSource, isSyntheticSource, messageError, ReferenceSource } from '@hydranium/protocol';
|
|
9
|
+
import { asModelVersion, createRpcProxy, defineMessage, describeErrorWithStack, DisposableCollection, HYDRANIUM_ERROR_CODES, isDocumentSource, isElementSource, isResponseError, isSyntheticSource, messageData, messageError, ReferenceSource, SessionClosedError, TIMED_OUT, TransferDocument, UNKNOWN_CLIENT_ID, textHash } from '@hydranium/protocol';
|
|
10
10
|
import { DATA_SERVER_DIAGNOSTICS_METHODS, DATA_SERVER_PROTOCOL_METHODS, DATA_SERVER_WIRE_PREFIX } from '@hydranium/protocol/data';
|
|
11
|
-
import {
|
|
11
|
+
import { DOCUMENT_RELEASE_CLIENT_ID } from '@hydranium/core';
|
|
12
12
|
import { defaultDataServerDiagnostics } from './default-diagnostics.js';
|
|
13
13
|
/**
|
|
14
14
|
* Raised when a profiling command arrives with no capture running.
|
|
@@ -23,9 +23,26 @@ export const NO_ACTIVE_PROFILE = defineMessage('hydranium/data-server/no-active-
|
|
|
23
23
|
* The JSON-RPC code, unrelated to the catalogue code above: this one is numeric,
|
|
24
24
|
* survives reconstruction and is what a caller switches on.
|
|
25
25
|
*/
|
|
26
|
-
export const NO_ACTIVE_PROFILE_CODE =
|
|
26
|
+
export const NO_ACTIVE_PROFILE_CODE = HYDRANIUM_ERROR_CODES.noActiveProfile;
|
|
27
27
|
export const noActiveProfileError = () => messageError(NO_ACTIVE_PROFILE_CODE, NO_ACTIVE_PROFILE);
|
|
28
|
+
/**
|
|
29
|
+
* Raised when a reference question's wait for the build exceeds
|
|
30
|
+
* {@link DataServerOptions.referenceSettleTimeoutMs}.
|
|
31
|
+
*
|
|
32
|
+
* A `ResponseError` because a class name does not cross RPC: without the code a
|
|
33
|
+
* wire client cannot tell a wedged build from a reference that resolves to
|
|
34
|
+
* nothing, which is the distinction the wait exists to keep.
|
|
35
|
+
*/
|
|
36
|
+
export const REFERENCE_SETTLE_TIMEOUT = defineMessage('hydranium/data-server/reference-settle-timeout', 'The model is still being processed; try again in a moment.');
|
|
37
|
+
/** See {@link NO_ACTIVE_PROFILE_CODE} for why this is separate from the catalogue code. */
|
|
38
|
+
export const REFERENCE_SETTLE_TIMEOUT_CODE = HYDRANIUM_ERROR_CODES.referenceSettleTimeout;
|
|
39
|
+
/** The wait's length rides in `data` as `elapsedMs`, for the code that reacts rather than the reader. */
|
|
40
|
+
export const referenceSettleTimeoutError = (elapsedMs) => new ResponseError(REFERENCE_SETTLE_TIMEOUT_CODE, REFERENCE_SETTLE_TIMEOUT.format(), {
|
|
41
|
+
elapsedMs,
|
|
42
|
+
...messageData(REFERENCE_SETTLE_TIMEOUT)
|
|
43
|
+
});
|
|
28
44
|
import { DocumentState, UriUtils } from '@hydranium/langium';
|
|
45
|
+
import { ResponseError } from 'vscode-jsonrpc';
|
|
29
46
|
/**
|
|
30
47
|
* Domain separator between text and diagnostics inputs of
|
|
31
48
|
* {@link DataServer.computeDocumentFingerprint}'s hash. A single NUL byte is
|
|
@@ -34,47 +51,30 @@ import { DocumentState, UriUtils } from '@hydranium/langium';
|
|
|
34
51
|
*/
|
|
35
52
|
const FINGERPRINT_SEPARATOR = '\0';
|
|
36
53
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* **A cryptographic hash via `node:crypto` is the wrong trade**: the
|
|
41
|
-
* fingerprint only needs a stable signal that `(text, diagnostics)` changed
|
|
42
|
-
* between emissions, and a `node:*` import would cost
|
|
43
|
-
* `@hydranium/data-server` its browser-portability. Parts are fed
|
|
44
|
-
* incrementally, char by char, so multi-MB document text is never
|
|
45
|
-
* concatenated into one string.
|
|
54
|
+
* `LSPErrorCodes.ServerCancelled`, with which Langium's document wait rejects
|
|
55
|
+
* for a URI that has no document; `vscode-languageserver-protocol` is not a
|
|
56
|
+
* dependency of this package.
|
|
46
57
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
|
|
60
|
-
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
61
|
-
const high = (h2 >>> 0).toString(16).padStart(8, '0');
|
|
62
|
-
const low = (h1 >>> 0).toString(16).padStart(8, '0');
|
|
63
|
-
return high + low;
|
|
64
|
-
}
|
|
65
|
-
/** Hash a document's raw text + diagnostics — the `'text-diagnostics'` strategy. */
|
|
66
|
-
function textDiagnosticsFingerprint(document) {
|
|
67
|
-
return fingerprintHash([document.textDocument.getText(), FINGERPRINT_SEPARATOR, JSON.stringify(document.diagnostics ?? [])]);
|
|
58
|
+
const SERVER_CANCELLED = -32802;
|
|
59
|
+
/**
|
|
60
|
+
* Hash `text`, the text the model was parsed from, + `model`'s diagnostics — the `'text-diagnostics'` strategy.
|
|
61
|
+
* The text comes from the root's ledger record or CST, as the document's own may have moved on,
|
|
62
|
+
* and a repair in place leaves the CST on the unrepaired text;
|
|
63
|
+
* a root built without a CST hashes as `'transfer-document'`.
|
|
64
|
+
* Absent diagnostics must hash apart from `[]`, as in {@link transferDocumentFingerprint}.
|
|
65
|
+
*/
|
|
66
|
+
function textDiagnosticsFingerprint(text, model) {
|
|
67
|
+
return text === undefined
|
|
68
|
+
? transferDocumentFingerprint(model)
|
|
69
|
+
: textHash([text, FINGERPRINT_SEPARATOR, JSON.stringify(model.diagnostics ?? null)]);
|
|
68
70
|
}
|
|
69
|
-
/**
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
JSON.stringify(transferDocument.diagnostics)
|
|
77
|
-
]);
|
|
71
|
+
/**
|
|
72
|
+
* Hash an encoded model's root + diagnostics — the `'transfer-document'` strategy.
|
|
73
|
+
* Absent diagnostics must hash apart from `[]`: a client skipping a render on an
|
|
74
|
+
* equal hash would otherwise miss a validation that found nothing.
|
|
75
|
+
*/
|
|
76
|
+
function transferDocumentFingerprint(model) {
|
|
77
|
+
return textHash([JSON.stringify(model.root), FINGERPRINT_SEPARATOR, JSON.stringify(model.diagnostics ?? null)]);
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
80
|
* `logAfterMs` threshold passed to {@link Tracer.time} around
|
|
@@ -105,13 +105,19 @@ const FINGERPRINT_LOG_AFTER_MS = 5;
|
|
|
105
105
|
* adopters subclass ONLY when they need to decorate wire returns or
|
|
106
106
|
* notifications. The usual adoption path is DI rebinds alone.
|
|
107
107
|
*
|
|
108
|
+
* **`TTransfer` is not checked.** It declares the union of the transfer roots
|
|
109
|
+
* the workspace's languages produce. Which root a document has is known only
|
|
110
|
+
* at runtime, from its language, so a `TTransfer` missing a language compiles.
|
|
111
|
+
*
|
|
108
112
|
* Lifecycle: the constructor registers framework request handlers (plus
|
|
109
113
|
* any names supplied via {@link DataServerOptions.additionalMethods}) and
|
|
110
114
|
* builds the {@link DataClientProtocol} notification proxy on the same
|
|
111
115
|
* connection in one {@link createRpcProxy} call (binding `this` as its
|
|
112
|
-
* `localTarget`) under the configured namespace, and
|
|
113
|
-
* `
|
|
114
|
-
* dispatches
|
|
116
|
+
* `localTarget`) under the configured namespace, and follows documents
|
|
117
|
+
* through `ModelService`'s events: at the configured phase, `onModelUpdated`
|
|
118
|
+
* dispatches per-document `clientProxy.onDocumentUpdated` for watched URIs and
|
|
119
|
+
* `onModelsBuilt` a single `clientProxy.onDocumentsBuilt` naming the batch's
|
|
120
|
+
* unwatched documents.
|
|
115
121
|
*
|
|
116
122
|
* The data-server requires the full {@link ServerSharedServices}
|
|
117
123
|
* shape — `HydraniumTextDocuments` for client-attributed updates,
|
|
@@ -136,85 +142,40 @@ export class DataServer {
|
|
|
136
142
|
* extending rather than restate a value the framework may change.
|
|
137
143
|
*/
|
|
138
144
|
static DEFAULT_OPTIONS = {
|
|
139
|
-
subscriptionPhase: DocumentState.Validated
|
|
145
|
+
subscriptionPhase: DocumentState.Validated,
|
|
146
|
+
referenceSettleTimeoutMs: 60_000
|
|
140
147
|
};
|
|
141
148
|
options;
|
|
142
|
-
/**
|
|
143
|
-
|
|
149
|
+
/** This connection's {@link DataServerUriWatchRecord} per canonical URI; see there for when a record lives. */
|
|
150
|
+
uriWatchRecords = new Map();
|
|
144
151
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
* the document open forever — resident, and with its last-close revert
|
|
151
|
-
* suppressed. The store has no notion of which connection a clientId reached
|
|
152
|
-
* it over, so the connection-scoped set has to live here; teaching it a client
|
|
153
|
-
* identity is the more robust and much larger alternative.
|
|
154
|
-
*/
|
|
155
|
-
openedDocuments = new Map();
|
|
156
|
-
/** Typed client proxy — sends `data-server/on*` notifications back over the same wire. */
|
|
157
|
-
clientProxy;
|
|
158
|
-
disposables = new DisposableCollection();
|
|
152
|
+
* Per client id, the key each URI it watched resolved to when it watched.
|
|
153
|
+
* An unwatch looks the key up here: once a file is gone, a symlink to it
|
|
154
|
+
* no longer resolves to the key the watch is under.
|
|
155
|
+
*/
|
|
156
|
+
watchKeys = new Map();
|
|
159
157
|
/**
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
* `'deleted'`, otherwise `'rebuilt'` (cascade rebuild from a dependent
|
|
164
|
-
* URI's change). The same mechanism `AstDocumentManager.onUpdate` uses on
|
|
165
|
-
* the LSP side, so reason fidelity stays consistent between heads.
|
|
158
|
+
* The position of the last update or deletion event sent, which each
|
|
159
|
+
* such event carries, and a watch's answer carries as of the moment it
|
|
160
|
+
* took whether its document exists: an event past it was sent after.
|
|
166
161
|
*/
|
|
167
|
-
|
|
162
|
+
eventSequence = 0;
|
|
168
163
|
/**
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
* Without this filter, both scenarios reach subscribers as wire-side
|
|
185
|
-
* `'changed'` events. A subscriber that interprets `'changed'` as
|
|
186
|
-
* "another client wrote new content" and resets its in-memory root to
|
|
187
|
-
* the server view then misreads the spurious event as a concurrent
|
|
188
|
-
* third-party write and loses the user's edits.
|
|
189
|
-
*
|
|
190
|
-
* Initialised on {@link watchModelDocument} to the current
|
|
191
|
-
* document fingerprint so the FIRST phase event after a fresh
|
|
192
|
-
* subscription is also de-duplicated against the state subscribers
|
|
193
|
-
* obtained via `getModelDocument` — they do not need a redundant phase
|
|
194
|
-
* notification for the state they just fetched.
|
|
195
|
-
*
|
|
196
|
-
* Cleared in {@link unwatchModelDocument} when the last
|
|
197
|
-
* subscriber for a URI leaves so memory does not accumulate.
|
|
198
|
-
*
|
|
199
|
-
* Stored as a cyrb53 hex digest (16 chars per URI) rather than the raw
|
|
200
|
-
* text + diagnostics serialisation so the memory cost is constant in
|
|
201
|
-
* document size. See {@link computeDocumentFingerprint} for the inputs.
|
|
202
|
-
*/
|
|
203
|
-
lastEmittedFingerprint = new Map();
|
|
204
|
-
/**
|
|
205
|
-
* URIs whose LAST client just closed. The update handler rebuilds such a
|
|
206
|
-
* document from its disk content (discarding unsaved in-session edits),
|
|
207
|
-
* but {@link dispatchPhaseEvent} gates on the subscription map — and the
|
|
208
|
-
* last close typically also removed the last watcher, so consumers that
|
|
209
|
-
* only ever fetch via `getModelDocument` would keep showing the
|
|
210
|
-
* discarded state forever. Marked on the last-close transition (see
|
|
211
|
-
* {@link subscribeToTextDocumentCloses}) and consumed by
|
|
212
|
-
* {@link dispatchPhaseEvent}, which broadcasts the following rebuild's
|
|
213
|
-
* phase event even without a subscription — de-duplicated against
|
|
214
|
-
* {@link lastEmittedFingerprint} where an entry survives, so a close
|
|
215
|
-
* whose disk state equals the last emitted state stays silent.
|
|
216
|
-
*/
|
|
217
|
-
pendingRevertBroadcasts = new Set();
|
|
164
|
+
* The live client sessions this connection registered, by client id. A
|
|
165
|
+
* document request carrying one of these ids acts as that session; one
|
|
166
|
+
* carrying any other id fails. A session leaves once it ends, so a
|
|
167
|
+
* connection that registers many over its life does not accumulate them.
|
|
168
|
+
*/
|
|
169
|
+
clientSessions = new Map();
|
|
170
|
+
/**
|
|
171
|
+
* Typed client proxy — sends `data-server/on*` notifications back over the same wire.
|
|
172
|
+
* The proxy sends any `on[A-Z]*` name other than `onDidOpenConnection` and
|
|
173
|
+
* `onDidCloseConnection`, for which it returns its own events, so a subclass
|
|
174
|
+
* with notifications of its own narrows only the type:
|
|
175
|
+
* `declare protected readonly clientProxy: TheirClient`.
|
|
176
|
+
*/
|
|
177
|
+
clientProxy;
|
|
178
|
+
disposables = new DisposableCollection();
|
|
218
179
|
tracer;
|
|
219
180
|
/** The in-flight interactive profile capture, held between {@link startProfiling} and {@link stopProfiling}. */
|
|
220
181
|
activeProfile;
|
|
@@ -226,11 +187,8 @@ export class DataServer {
|
|
|
226
187
|
encoder;
|
|
227
188
|
/**
|
|
228
189
|
* In-process workspace facade — the lifecycle delegate `get` / `update` / `save` go through.
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
* while the wire shape stays `TDiagnostic extends TransferDiagnostic`. The encoder's
|
|
232
|
-
* `astDocumentToTransferDocument` accepts both shapes (wire-shape or LSP-shape) and projects
|
|
233
|
-
* to wire shape on the return — see `TransferEncoder.astDocumentToTransferDocument`.
|
|
190
|
+
* Its documents carry the AST-layer {@link AstDiagnostic}, never the wire `TDiagnostic`:
|
|
191
|
+
* {@link encoder} converts one into the other on the way out.
|
|
234
192
|
*/
|
|
235
193
|
modelService;
|
|
236
194
|
constructor(connection, services, options = {}) {
|
|
@@ -239,8 +197,7 @@ export class DataServer {
|
|
|
239
197
|
this.options = this.resolveOptions(options);
|
|
240
198
|
this.tracer = this.services.Tracer.for(options.logName ?? 'DataServer').trace('instantiated');
|
|
241
199
|
// DI-bound: adopters rebind `services.model.TransferEncoder` /
|
|
242
|
-
// `services.model.ModelService` with their own subclasses.
|
|
243
|
-
// framework upper bounds; the casts below narrow to this instance's generics.
|
|
200
|
+
// `services.model.ModelService` with their own subclasses.
|
|
244
201
|
const { TransferEncoder: encoder, ModelService: modelService } = this.services.model;
|
|
245
202
|
if (!encoder || !modelService) {
|
|
246
203
|
throw new Error('DataServer requires `services.model.TransferEncoder` and `services.model.ModelService` to be bound. ' +
|
|
@@ -269,33 +226,36 @@ export class DataServer {
|
|
|
269
226
|
// framework's own rejections.
|
|
270
227
|
renderErrorMessage: error => this.services.MessageRenderer.renderError(error)
|
|
271
228
|
});
|
|
272
|
-
this.disposables.push(this.
|
|
273
|
-
|
|
274
|
-
}));
|
|
275
|
-
this.subscribeToDocumentBuilder();
|
|
229
|
+
this.disposables.push(this.modelService.onModelDeleted(event => this.dispatchDeleteEvent(event)));
|
|
230
|
+
this.subscribeToModelUpdates();
|
|
276
231
|
this.subscribeToTextDocumentSaves();
|
|
277
|
-
this.
|
|
232
|
+
this.subscribeToDirtyChanges();
|
|
233
|
+
this.subscribeToModelReleases();
|
|
278
234
|
this.subscribeToProjectManager();
|
|
279
235
|
// Self-register teardown so an adopter that keeps no reference to the
|
|
280
236
|
// server still releases per-connection state, with no lifecycle hook of
|
|
281
|
-
// its own.
|
|
282
|
-
|
|
237
|
+
// its own. The client did not close anything itself, so its sessions end
|
|
238
|
+
// as lost and each document it was the last to have open waits out the
|
|
239
|
+
// store's release grace. See `dispose`.
|
|
240
|
+
this.disposables.push(connection.onClose(() => this.dispose('lost')));
|
|
283
241
|
}
|
|
284
242
|
/**
|
|
285
243
|
* Release everything the constructor wired up — the bound protocol
|
|
286
|
-
* handlers and every listener — and clear
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
* this server's and so outlives the connection unless released here.
|
|
244
|
+
* handlers and every listener — and clear {@link uriWatchRecords}, so a
|
|
245
|
+
* long-lived shared services bundle does not retain per-connection memory
|
|
246
|
+
* after the connection closes. Also ends every session this connection registered, which closes
|
|
247
|
+
* every document it has open: the SHARED store's state rather than this
|
|
248
|
+
* server's, which outlives the connection unless released here.
|
|
292
249
|
*
|
|
293
|
-
* Idempotent: subsequent calls are no-ops.
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
250
|
+
* Idempotent: subsequent calls are no-ops. Runs by itself when the
|
|
251
|
+
* connection closes, so adopters who don't hold a reference still get
|
|
252
|
+
* per-connection cleanup; adopters that DO hold a reference may call
|
|
253
|
+
* `dispose()` directly for early teardown. The sessions end with `cause`:
|
|
254
|
+
* `'closed'` releases every document whose last open they close at once,
|
|
255
|
+
* and `'lost'`, the connection's own close, lets each wait out the release
|
|
256
|
+
* grace.
|
|
297
257
|
*/
|
|
298
|
-
dispose() {
|
|
258
|
+
dispose(cause = 'closed') {
|
|
299
259
|
this.disposed = true;
|
|
300
260
|
// Release an in-flight interactive capture so the process-wide inspector
|
|
301
261
|
// singleton is not left active after the connection closes.
|
|
@@ -304,98 +264,137 @@ export class DataServer {
|
|
|
304
264
|
this.activeProfile = undefined;
|
|
305
265
|
void capture.stop({}).catch(() => undefined);
|
|
306
266
|
}
|
|
307
|
-
// AFTER `disposables.dispose()`, deliberately:
|
|
308
|
-
//
|
|
309
|
-
//
|
|
267
|
+
// AFTER `disposables.dispose()`, deliberately: ending a session releases
|
|
268
|
+
// each document it was the last to hold, and this server's own
|
|
269
|
+
// `onModelReleased` subscription would otherwise mark a release broadcast
|
|
270
|
+
// for a connection that is already gone.
|
|
310
271
|
this.disposables.dispose();
|
|
311
|
-
this.
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
this.
|
|
272
|
+
for (const [clientId, session] of this.clientSessions) {
|
|
273
|
+
this.endSession(clientId, session, cause);
|
|
274
|
+
}
|
|
275
|
+
this.uriWatchRecords.clear();
|
|
276
|
+
this.watchKeys.clear();
|
|
315
277
|
}
|
|
316
278
|
// ============================================================
|
|
317
|
-
// DataServerProtocol implementation
|
|
279
|
+
// DataServerProtocol implementation
|
|
318
280
|
// ============================================================
|
|
319
281
|
//
|
|
320
|
-
//
|
|
321
|
-
//
|
|
322
|
-
// the wire boundary via
|
|
323
|
-
//
|
|
324
|
-
//
|
|
325
|
-
//
|
|
326
|
-
async
|
|
327
|
-
//
|
|
328
|
-
//
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
// `baseVersion` against, so reporting the snapshot's would make the
|
|
348
|
-
// caller's first tagged write self-conflict. A genuine concurrent edit
|
|
349
|
-
// still trips the gate and is reconciled by the caller's replay rather
|
|
350
|
-
// than predicted here.
|
|
351
|
-
return { ...document, version: this.services.workspace.TextDocuments.version(args.uri) };
|
|
282
|
+
// Reads go to `ModelService`; a request that opens or writes a document
|
|
283
|
+
// goes to the session this connection registered under its `clientId`.
|
|
284
|
+
// Each answer is encoded on the wire boundary via
|
|
285
|
+
// `encoder.astDocumentToTransferDocument`. To change how documents are
|
|
286
|
+
// read, override on the ModelService subclass; to change how they are
|
|
287
|
+
// written, on the session class the bound `ClientSessionFactory` builds.
|
|
288
|
+
async createSession(args) {
|
|
289
|
+
// After teardown nothing would ever end a session registered now, and its
|
|
290
|
+
// id would stay taken for the life of the process.
|
|
291
|
+
if (this.disposed) {
|
|
292
|
+
throw new SessionClosedError(args.clientId);
|
|
293
|
+
}
|
|
294
|
+
const session = this.modelService.createSession(args.label, args.clientId, { resumeToken: args.resumeToken });
|
|
295
|
+
this.clientSessions.set(args.clientId, session);
|
|
296
|
+
// A session also ends without closeSession, as when a client registers
|
|
297
|
+
// again on another connection, and its watches and entry here would
|
|
298
|
+
// otherwise outlive it.
|
|
299
|
+
session.onDidDispose(cause => {
|
|
300
|
+
// A session class may report its end late, once a replacement holds
|
|
301
|
+
// the id here; the replacement's entry and watches stay.
|
|
302
|
+
if (this.clientSessions.get(args.clientId) !== session) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
this.dropWatches(args.clientId);
|
|
306
|
+
this.clientSessions.delete(args.clientId);
|
|
307
|
+
this.tracer.debug(`Forget session ${args.clientId}: it ended as ${cause}`);
|
|
308
|
+
});
|
|
352
309
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
310
|
+
/** End a session this connection registered. */
|
|
311
|
+
async closeSession(args) {
|
|
312
|
+
const session = this.clientSessions.get(args.clientId);
|
|
313
|
+
if (session) {
|
|
314
|
+
this.endSession(args.clientId, session);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/** End `session`, registered here under `clientId`, with `cause`, and drop its watches on this connection. */
|
|
318
|
+
endSession(clientId, session, cause = 'closed') {
|
|
319
|
+
this.dropWatches(clientId);
|
|
320
|
+
session.dispose(cause);
|
|
321
|
+
}
|
|
322
|
+
dropWatches(clientId) {
|
|
323
|
+
this.watchKeys.delete(clientId);
|
|
324
|
+
for (const [uri, record] of this.uriWatchRecords) {
|
|
325
|
+
if (record.watchers.delete(clientId)) {
|
|
326
|
+
this.pruneUriWatchRecord(uri);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
async createModelDocument(args) {
|
|
331
|
+
const session = this.requireSession(args.clientId);
|
|
332
|
+
const version = await session.create(args.uri, args.text);
|
|
333
|
+
const snapshot = await this.openedSnapshot(args.uri, () => session.close(args.uri));
|
|
334
|
+
return { ...snapshot, created: { version: asModelVersion(version) } };
|
|
335
|
+
}
|
|
336
|
+
async updateModelDocuments(args) {
|
|
337
|
+
const astDocuments = await this.requireSession(args.clientId).updateAll({
|
|
338
|
+
updates: args.updates.map(update => this.toSessionWrite(update))
|
|
339
|
+
});
|
|
340
|
+
// A page that reads unsaved work from its editor sees the writes only once their text reaches it.
|
|
341
|
+
await Promise.all(args.updates.map(update => this.modelService.settleLanguageClient(update.uri)));
|
|
342
|
+
return astDocuments.map(astDocument => this.encodeDocument(astDocument));
|
|
360
343
|
}
|
|
361
344
|
/**
|
|
362
|
-
*
|
|
363
|
-
*
|
|
345
|
+
* The session write a wire write request carries. Only the fields a session
|
|
346
|
+
* takes: a field an adopter adds to a request reaches the session only
|
|
347
|
+
* through an override of this.
|
|
364
348
|
*/
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
349
|
+
toSessionWrite(request) {
|
|
350
|
+
return { uri: request.uri, model: request.model, baseVersion: request.baseVersion, restore: request.restore };
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* The session this connection registered under `clientId`, which every
|
|
354
|
+
* document request acts as. An id that is not one fails with the
|
|
355
|
+
* closed-session code: opening or writing under it would leave an open that
|
|
356
|
+
* no session end ever closes.
|
|
357
|
+
*/
|
|
358
|
+
requireSession(clientId) {
|
|
359
|
+
const session = this.clientSessions.get(clientId);
|
|
360
|
+
if (!session) {
|
|
361
|
+
throw new SessionClosedError(clientId, 'The client session is not registered on this connection.');
|
|
373
362
|
}
|
|
363
|
+
return session;
|
|
364
|
+
}
|
|
365
|
+
async openModelDocument(args) {
|
|
366
|
+
const session = this.requireSession(args.clientId);
|
|
367
|
+
const wasOpen = this.services.workspace.TextDocuments.isOpenInClient(args.uri, args.clientId);
|
|
368
|
+
await session.open(args.uri, args.options);
|
|
369
|
+
// A repeat open keeps the earlier one, which is still in use.
|
|
370
|
+
return this.openedSnapshot(args.uri, wasOpen ? undefined : () => session.close(args.uri));
|
|
374
371
|
}
|
|
375
372
|
/**
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
*/
|
|
390
|
-
closeOpenDocuments() {
|
|
391
|
-
const held = [...this.openedDocuments];
|
|
392
|
-
this.openedDocuments.clear();
|
|
393
|
-
for (const [uri, clientIds] of held) {
|
|
394
|
-
for (const clientId of clientIds) {
|
|
395
|
-
void Promise.resolve(this.modelService.close({ uri, clientId })).catch(() => undefined);
|
|
396
|
-
}
|
|
373
|
+
* The snapshot a session's open or create answers with. When the read
|
|
374
|
+
* fails, `rollback` undoes the open this call made before the failure is
|
|
375
|
+
* rethrown: the caller sees no open, so nothing on its side would ever close
|
|
376
|
+
* it.
|
|
377
|
+
*/
|
|
378
|
+
async openedSnapshot(uri, rollback) {
|
|
379
|
+
try {
|
|
380
|
+
return await this.getModelDocument({ uri });
|
|
381
|
+
}
|
|
382
|
+
catch (error) {
|
|
383
|
+
// The open's failure is the one the caller needs, not the cleanup's.
|
|
384
|
+
await rollback?.().catch(() => undefined);
|
|
385
|
+
throw error;
|
|
397
386
|
}
|
|
398
387
|
}
|
|
388
|
+
async closeModelDocument(args) {
|
|
389
|
+
// Through the handle, so a close under an ended session fails as that
|
|
390
|
+
// session's calls do, and a session class's own close is honoured.
|
|
391
|
+
const session = this.requireSession(args.clientId);
|
|
392
|
+
// Closing also releases the watch for (uri, clientId) — a forgotten
|
|
393
|
+
// unwatch would otherwise leak phase-event dispatch until the connection
|
|
394
|
+
// closes. Idempotent: a close without a prior watch is a no-op.
|
|
395
|
+
await this.unwatchModelDocument({ uri: args.uri, clientId: args.clientId });
|
|
396
|
+
await session.close(args.uri);
|
|
397
|
+
}
|
|
399
398
|
async getModelDocument(args) {
|
|
400
399
|
// Smart dispatch: a warm document (already in LangiumDocuments) is
|
|
401
400
|
// returned at its settle phase without a redundant build; a cold URI
|
|
@@ -408,72 +407,142 @@ export class DataServer {
|
|
|
408
407
|
// unsubscribed caller that needs diagnostics inline passes
|
|
409
408
|
// `includeDiagnostics: true` to settle at `Validated` instead.
|
|
410
409
|
const state = args.includeDiagnostics ? DocumentState.Validated : undefined;
|
|
411
|
-
|
|
412
|
-
|
|
410
|
+
try {
|
|
411
|
+
return this.encodeDocument(await this.modelService.ensureDocumentState(args.uri, state));
|
|
412
|
+
}
|
|
413
|
+
catch (error) {
|
|
414
|
+
// The protocol answers a read of a URI with no document with an
|
|
415
|
+
// envelope that has no model.
|
|
416
|
+
if (this.isMissingDocument(error, args.uri)) {
|
|
417
|
+
return this.envelope(UriUtils.toUri(args.uri));
|
|
418
|
+
}
|
|
419
|
+
throw error;
|
|
420
|
+
}
|
|
413
421
|
}
|
|
414
422
|
async updateModelDocument(args) {
|
|
415
|
-
const astDocument = await this.
|
|
416
|
-
|
|
423
|
+
const astDocument = await this.requireSession(args.clientId).update(this.toSessionWrite(args));
|
|
424
|
+
// A page that reads unsaved work from its editor sees the write only once the text reaches it.
|
|
425
|
+
await this.modelService.settleLanguageClient(args.uri);
|
|
426
|
+
return this.encodeDocument(astDocument);
|
|
417
427
|
}
|
|
418
428
|
async saveModelDocument(args) {
|
|
419
|
-
const astDocument = await this.
|
|
420
|
-
|
|
429
|
+
const astDocument = await this.requireSession(args.clientId).save(this.toSessionWrite(args));
|
|
430
|
+
await this.modelService.settleLanguageClient(args.uri);
|
|
431
|
+
return { ...this.encodeDocument(astDocument), persisted: astDocument.persisted };
|
|
432
|
+
}
|
|
433
|
+
async persistModelDocument(args) {
|
|
434
|
+
const version = await this.requireSession(args.clientId).persist({ uri: args.uri, baseVersion: args.baseVersion });
|
|
435
|
+
await this.modelService.settleLanguageClient(args.uri);
|
|
436
|
+
const astDocument = await this.modelService.ensureDocumentState(args.uri, this.services.workspace.DocumentBuilder.finalBuildState());
|
|
437
|
+
return { ...this.encodeDocument(astDocument), persisted: { version } };
|
|
421
438
|
}
|
|
422
439
|
/**
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
|
|
440
|
+
* Delete the document through the session, then release the session's
|
|
441
|
+
* watch of it, as {@link closeModelDocument} does. Released only after the
|
|
442
|
+
* delete: one that fails leaves the session its open and its watch. Other
|
|
443
|
+
* sessions' watches survive, as for any deletion.
|
|
444
|
+
*/
|
|
445
|
+
async deleteModelDocument(args) {
|
|
446
|
+
const session = this.requireSession(args.clientId);
|
|
447
|
+
// Keyed before the delete: once the file is gone, a symlink to it no
|
|
448
|
+
// longer resolves to the key the watch is under.
|
|
449
|
+
const uri = this.canonicalKey(args.uri);
|
|
450
|
+
await session.delete({ uri: args.uri, baseVersion: args.baseVersion });
|
|
451
|
+
await this.unwatchModelDocument({ uri, clientId: args.clientId });
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Record a watch for `(uri, clientId)` in {@link uriWatchRecords}.
|
|
455
|
+
* Subsequent phase events on `uri` fan out to the wired
|
|
456
|
+
* `clientProxy.onDocumentUpdated`. The bidirectional pattern means the
|
|
457
|
+
* event channel is the client notification surface, not a returned
|
|
458
|
+
* handle.
|
|
459
|
+
*
|
|
460
|
+
* Also baselines the URI's emission fingerprint (see
|
|
461
|
+
* {@link dispatchPhaseEvent}) to the document's current state when the
|
|
462
|
+
* first watcher for the URI registers. This guarantees that any phase event firing immediately after
|
|
463
|
+
* the watch with no observable change is suppressed — watchers obtain
|
|
464
|
+
* initial state via `getModelDocument` (or {@link openModelDocument}) and
|
|
465
|
+
* do not need a redundant phase notification for that same state.
|
|
428
466
|
*
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
* phase event firing immediately after the watch with no observable
|
|
433
|
-
* change is suppressed — watchers obtain initial state via
|
|
434
|
-
* `getModelDocument` (or {@link openModelDocument}) and do not need a
|
|
435
|
-
* redundant phase notification for that same state.
|
|
467
|
+
* Answers once the document is loaded as a read loads it, with the key
|
|
468
|
+
* events name it by and whether a document exists, taken after the watch
|
|
469
|
+
* is in place and the load, so an event sent while it loads is older.
|
|
436
470
|
*/
|
|
437
471
|
async watchModelDocument(args) {
|
|
438
472
|
const uri = this.canonicalKey(args.uri);
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
473
|
+
const keys = this.watchKeys.get(args.clientId) ?? new Map();
|
|
474
|
+
this.watchKeys.set(args.clientId, keys.set(args.uri, uri));
|
|
475
|
+
const record = this.uriWatchRecords.get(uri) ?? { watchers: new Set() };
|
|
476
|
+
this.uriWatchRecords.set(uri, record);
|
|
477
|
+
record.watchers.add(args.clientId);
|
|
478
|
+
if (record.fingerprint === undefined) {
|
|
479
|
+
const document = this.modelService.getDocument(args.uri);
|
|
480
|
+
if (document && !this.services.workspace.ModelLedger.isPlaceholder(document.parseResult.value)) {
|
|
481
|
+
record.fingerprint = this.computeDocumentFingerprint(document.parseResult.value, this.encoder.toTransferDocument(document));
|
|
482
|
+
record.fingerprintVersion = this.services.workspace.ModelLedger.versionOf(document.parseResult.value);
|
|
449
483
|
}
|
|
450
484
|
}
|
|
485
|
+
// As for a read, only a missing document counts as none. A file briefly
|
|
486
|
+
// unreadable leaves the builder's placeholder, and is there all the same.
|
|
487
|
+
// Whether it exists, the text held and the position of the last event
|
|
488
|
+
// sent are read in one synchronous run, so an event sent after is past
|
|
489
|
+
// the position and moves the text past the state answered.
|
|
490
|
+
const answer = (exists) => {
|
|
491
|
+
const text = exists ? this.serverTextState(args.uri) : undefined;
|
|
492
|
+
return { uri, exists, sequence: this.eventSequence, ...(text ? { text } : {}) };
|
|
493
|
+
};
|
|
494
|
+
return this.modelService.ensureDocumentState(args.uri).then(() => answer(this.modelService.getDocument(args.uri) !== undefined), (error) => answer(!this.isMissingDocument(error, args.uri)));
|
|
451
495
|
}
|
|
452
496
|
/**
|
|
453
497
|
* Remove a watch for `(uri, clientId)` previously created by
|
|
454
498
|
* {@link watchModelDocument}. Idempotent — unwatching twice is a no-op.
|
|
455
|
-
* Dispatch for `uri` stops once no watchers remain,
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
*
|
|
499
|
+
* Dispatch for `uri` stops once no watchers remain, and the emission
|
|
500
|
+
* fingerprint goes with the last watcher (see {@link pruneUriWatchRecord}).
|
|
501
|
+
*
|
|
502
|
+
* A URI the client watched is unwatched under the key it resolved to then,
|
|
503
|
+
* from {@link watchKeys}, which still holds once its file is gone.
|
|
459
504
|
*/
|
|
460
505
|
async unwatchModelDocument(args) {
|
|
461
|
-
const
|
|
462
|
-
const
|
|
463
|
-
|
|
506
|
+
const keys = this.watchKeys.get(args.clientId);
|
|
507
|
+
const uri = keys?.get(args.uri) ?? this.canonicalKey(args.uri);
|
|
508
|
+
// Every spelling the client watched it under was this one watch.
|
|
509
|
+
keys?.forEach((key, spelling) => {
|
|
510
|
+
if (key === uri) {
|
|
511
|
+
keys.delete(spelling);
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
if (keys?.size === 0) {
|
|
515
|
+
this.watchKeys.delete(args.clientId);
|
|
516
|
+
}
|
|
517
|
+
if (this.uriWatchRecords.get(uri)?.watchers.delete(args.clientId)) {
|
|
518
|
+
this.pruneUriWatchRecord(uri);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Drop what `uri` no longer needs: the
|
|
523
|
+
* {@link DataServerUriWatchRecord.fingerprint} once no client watches it,
|
|
524
|
+
* and the whole record once it holds no
|
|
525
|
+
* {@link DataServerUriWatchRecord.releaseBroadcastPending} mark either.
|
|
526
|
+
* Every path that removes a watcher or consumes a mark ends here.
|
|
527
|
+
*/
|
|
528
|
+
pruneUriWatchRecord(uri) {
|
|
529
|
+
const record = this.uriWatchRecords.get(uri);
|
|
530
|
+
if (!record || record.watchers.size > 0) {
|
|
464
531
|
return;
|
|
465
532
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
533
|
+
if (record.releaseBroadcastPending) {
|
|
534
|
+
record.fingerprint = undefined;
|
|
535
|
+
record.fingerprintVersion = undefined;
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
this.uriWatchRecords.delete(uri);
|
|
470
539
|
}
|
|
471
540
|
}
|
|
472
541
|
/**
|
|
473
|
-
* Canonicalise a URI string for use as a
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
*
|
|
542
|
+
* Canonicalise a URI string for use as a {@link uriWatchRecords} key, via the
|
|
543
|
+
* shared `DocumentUriPolicy`. Callers may send non-canonical URIs
|
|
544
|
+
* (drive-letter casing, percent-encoding differences, or a symlink path)
|
|
545
|
+
* over the wire; the dispatch side keys by
|
|
477
546
|
* `document.uri.toString()` from `LangiumDocuments`, so writer keys must
|
|
478
547
|
* canonicalise to the same form or events silently fail to deliver.
|
|
479
548
|
* Routing through the seam (rather than a bare `UriUtils.normalize`)
|
|
@@ -483,24 +552,22 @@ export class DataServer {
|
|
|
483
552
|
* LSP head resolves.
|
|
484
553
|
*
|
|
485
554
|
* Adopters can still override for head-specific URI policy by subclassing.
|
|
555
|
+
* The result keys watches only; a document is looked up by its own URI.
|
|
486
556
|
*/
|
|
487
557
|
canonicalKey(uri) {
|
|
488
558
|
return this.services.workspace.DocumentUriPolicy.canonicalUri(uri);
|
|
489
559
|
}
|
|
490
560
|
async getProjects() {
|
|
491
|
-
// Pass-through: `ProjectManager<TProject>` already produces `TProject`
|
|
492
|
-
// (the shared services are parameterised over the same generic).
|
|
493
561
|
// The framework reads `id` for registry identity and `dependencies`
|
|
494
562
|
// for the visibility closure; every other field rides on the
|
|
495
563
|
// JSON-RPC envelope as adopter-defined wire metadata.
|
|
496
|
-
return this.services.workspace.ProjectManager.getProjects();
|
|
564
|
+
return this.services.workspace.ProjectManager.getProjects().map(project => this.encodeProject(project));
|
|
497
565
|
}
|
|
498
566
|
async getProjectForUri(args) {
|
|
499
|
-
//
|
|
500
|
-
//
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
return this.services.workspace.ProjectManager.getProject(UriUtils.toUri(args.uri));
|
|
567
|
+
// Membership logic belongs to the adopter's `ProjectManager`; the
|
|
568
|
+
// data-server forwards the URI without interpretation.
|
|
569
|
+
const project = this.services.workspace.ProjectManager.getProject(UriUtils.toUri(args.uri));
|
|
570
|
+
return project && this.encodeProject(project);
|
|
504
571
|
}
|
|
505
572
|
/**
|
|
506
573
|
* Resolve once the data-server is ready to serve requests. Delegates
|
|
@@ -517,26 +584,56 @@ export class DataServer {
|
|
|
517
584
|
// Resolve the language's reference services from each request's source
|
|
518
585
|
// (see resolveReferenceServices) and delegate.
|
|
519
586
|
// ============================================================
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
587
|
+
/**
|
|
588
|
+
* Settle the build before a reference question is answered from the index.
|
|
589
|
+
*
|
|
590
|
+
* A query issued right after a model update otherwise races the rebuild and
|
|
591
|
+
* reads an index missing the symbols it asks about. The cost is not confined
|
|
592
|
+
* to this call: a scope built inside that window is cached, and the build's
|
|
593
|
+
* remaining documents link against it.
|
|
594
|
+
*
|
|
595
|
+
* Waits the source document specifically, but ONLY when one is loaded at
|
|
596
|
+
* that URI: a synthetic source can address a URI with no document (a
|
|
597
|
+
* directory URI, typically), where a per-URI `waitUntil` throws "No document
|
|
598
|
+
* found". Falls back to a global settle, matching the id-based
|
|
599
|
+
* `ElementSource` path.
|
|
600
|
+
*
|
|
601
|
+
* Bounded by {@link DataServerOptions.referenceSettleTimeoutMs}.
|
|
602
|
+
*/
|
|
603
|
+
async awaitReferencesLinked(source) {
|
|
604
|
+
const uri = isDocumentSource(source) || isSyntheticSource(source) ? UriUtils.toUri(source.uri) : undefined;
|
|
529
605
|
const waitUri = uri && this.services.workspace.LangiumDocuments.hasDocument(uri) ? uri : undefined;
|
|
530
|
-
|
|
606
|
+
const stopwatch = this.services.Clock.stopwatch();
|
|
607
|
+
const linked = this.services.workspace.DocumentBuilder.waitUntil(DocumentState.Linked, waitUri);
|
|
608
|
+
if ((await this.services.Clock.raceTimer(linked, this.options.referenceSettleTimeoutMs)) !== TIMED_OUT) {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
const elapsedMs = Math.round(stopwatch.elapsedMs);
|
|
612
|
+
// A per-URI wait resolves off a document-phase notification, so a
|
|
613
|
+
// missed one strands a wait on a document that HAS reached the
|
|
614
|
+
// phase. Re-read before failing; the workspace-wide wait has no
|
|
615
|
+
// equivalent reading and can only reject.
|
|
616
|
+
const document = waitUri ? this.services.workspace.LangiumDocuments.getDocument(waitUri) : undefined;
|
|
617
|
+
if (waitUri && document && document.state >= DocumentState.Linked) {
|
|
618
|
+
this.tracer
|
|
619
|
+
.withUri(waitUri.toString())
|
|
620
|
+
.warn(`Missed the 'Linked' notification after ${elapsedMs}ms; the document already reached it`);
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
throw referenceSettleTimeoutError(elapsedMs);
|
|
624
|
+
}
|
|
625
|
+
async findReferenceCandidates(ctx) {
|
|
626
|
+
await this.awaitReferencesLinked(ctx.source);
|
|
531
627
|
return this.resolveReferenceServices(ctx.source).CandidateProvider.find(ctx);
|
|
532
628
|
}
|
|
533
629
|
async resolveReference(ref) {
|
|
630
|
+
await this.awaitReferencesLinked(ref.source);
|
|
534
631
|
const resolved = this.resolveReferenceServices(ref.source).CandidateProvider.resolveCandidate(ref);
|
|
535
632
|
if (!resolved) {
|
|
536
633
|
return undefined;
|
|
537
634
|
}
|
|
538
|
-
|
|
539
|
-
return { ...resolved.candidate, element };
|
|
635
|
+
// `TElement` is the caller's claim about the target's type; nothing checks it.
|
|
636
|
+
return { ...resolved.candidate, element: this.encoder.toTransfer(resolved.node) };
|
|
540
637
|
}
|
|
541
638
|
async findNextName(args) {
|
|
542
639
|
const uri = UriUtils.toUri(args.uri);
|
|
@@ -545,15 +642,32 @@ export class DataServer {
|
|
|
545
642
|
// synthetic source — and the create-element flow driving this method is
|
|
546
643
|
// the one that produces directory URIs, on which a bare
|
|
547
644
|
// `getServices(uri)` throws while `findReferenceCandidates` succeeds.
|
|
548
|
-
const
|
|
645
|
+
const source = ReferenceSource.synthetic(args.uri, args.type, args.language);
|
|
646
|
+
// Mid-rebuild the names of documents not yet re-indexed are absent from
|
|
647
|
+
// the taken set, and the caller PERSISTS what it is handed. Nothing
|
|
648
|
+
// reserves the answer either, so a name is free of the collisions the
|
|
649
|
+
// index knows about and of nothing else — two callers racing get the same
|
|
650
|
+
// proposal, which only uniqueness at the write can catch.
|
|
651
|
+
await this.awaitReferencesLinked(source);
|
|
652
|
+
const nameProvider = this.resolveReferenceServices(source).NameProvider;
|
|
549
653
|
const tier = args.tier ?? 'project';
|
|
550
654
|
if (tier === 'public') {
|
|
551
655
|
return nameProvider.findNextProjectQualifiedName(args.type, args.proposal);
|
|
552
656
|
}
|
|
553
657
|
if (tier === 'local') {
|
|
554
|
-
// Document-scoped uniqueness: the document root is the container.
|
|
555
|
-
|
|
556
|
-
|
|
658
|
+
// Document-scoped uniqueness: the document root is the container. A
|
|
659
|
+
// URI with no document has nothing to collide with, like a document
|
|
660
|
+
// with no root.
|
|
661
|
+
try {
|
|
662
|
+
const document = await this.modelService.ensureDocumentState(args.uri);
|
|
663
|
+
return document.root ? nameProvider.findNextName(args.type, args.proposal, document.root) : args.proposal;
|
|
664
|
+
}
|
|
665
|
+
catch (error) {
|
|
666
|
+
if (this.isMissingDocument(error, args.uri)) {
|
|
667
|
+
return args.proposal;
|
|
668
|
+
}
|
|
669
|
+
throw error;
|
|
670
|
+
}
|
|
557
671
|
}
|
|
558
672
|
const project = this.services.workspace.ProjectManager.getProject(uri);
|
|
559
673
|
if (!project) {
|
|
@@ -586,66 +700,30 @@ export class DataServer {
|
|
|
586
700
|
return language.references;
|
|
587
701
|
}
|
|
588
702
|
/**
|
|
589
|
-
* Pick the language that owns a reference source
|
|
703
|
+
* Pick the language that owns a reference source: the registry's
|
|
704
|
+
* `getLanguageFor` over the source's declared language, URI and AST type,
|
|
705
|
+
* with an {@link isElementSource} routed by the document that holds the
|
|
706
|
+
* element (see {@link findElementDocumentUri}) before its type, and
|
|
707
|
+
* {@link fallbackReferenceLanguage}, adopter policy, last.
|
|
590
708
|
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
* single-language adopters never reach the steps below, and never pay
|
|
595
|
-
* for them.
|
|
596
|
-
* 3. An {@link isElementSource} source carries no URI, so in a
|
|
597
|
-
* multi-language workspace it is routed via the document that holds the
|
|
598
|
-
* element (see {@link findElementDocumentUri}).
|
|
599
|
-
* 4. A source carrying an AST type is routed by that type when exactly one
|
|
600
|
-
* registered grammar can produce it (see
|
|
601
|
-
* {@link resolveReferenceLanguageByType}, over the registry's own type
|
|
602
|
-
* index). This is what resolves a create-element flow's synthetic source
|
|
603
|
-
* on a bare directory URI without asking the adopter.
|
|
604
|
-
* 5. Anything still unresolved falls to {@link fallbackReferenceLanguage},
|
|
605
|
-
* which is adopter policy.
|
|
709
|
+
* An unregistered declared language falls through rather than failing: the
|
|
710
|
+
* id crosses the wire, so a client built against a head with one more
|
|
711
|
+
* grammar would otherwise lose queries the other signals answer.
|
|
606
712
|
*/
|
|
607
713
|
resolveReferenceLanguage(source) {
|
|
608
714
|
const registry = this.services.ServiceRegistry;
|
|
609
|
-
// `getServicesFor` (non-throwing, one ladder walk) gates the URI lookup:
|
|
610
|
-
// an extensionless / unregistered URI falls through to the steps below
|
|
611
|
-
// rather than throwing "no services for the extension ''".
|
|
612
|
-
const uri = isDocumentSource(source) || isSyntheticSource(source) ? UriUtils.toUri(source.uri) : undefined;
|
|
613
|
-
const byUri = uri && registry.getServicesFor(uri);
|
|
614
|
-
if (byUri) {
|
|
615
|
-
return byUri;
|
|
616
|
-
}
|
|
617
|
-
const all = registry.all;
|
|
618
|
-
if (all.length === 1) {
|
|
619
|
-
return all[0];
|
|
620
|
-
}
|
|
621
715
|
if (isElementSource(source)) {
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
}
|
|
716
|
+
// The index lookup runs only once the cheaper signals abstained.
|
|
717
|
+
return (registry.getLanguageFor({}) ??
|
|
718
|
+
registry.getLanguageFor({ target: this.findElementDocumentUri(source), type: source.type }) ??
|
|
719
|
+
this.fallbackReferenceLanguage(source));
|
|
627
720
|
}
|
|
628
|
-
const
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
* {@link isElementSource}'s optional narrowing `type`.
|
|
635
|
-
*
|
|
636
|
-
* Answers only when EXACTLY ONE registered grammar can produce the type.
|
|
637
|
-
* Several can when the type comes from a grammar both import, and then the
|
|
638
|
-
* type genuinely does not identify a language — that is a fall-through to
|
|
639
|
-
* adopter policy, not a coin toss. Note this asks which grammar can *produce*
|
|
640
|
-
* the type, not which mentions it: a grammar that merely cross-references a
|
|
641
|
-
* type can never hold a node of it.
|
|
642
|
-
*/
|
|
643
|
-
resolveReferenceLanguageByType(source) {
|
|
644
|
-
const type = isSyntheticSource(source) ? source.type : isElementSource(source) ? source.type : undefined;
|
|
645
|
-
// The registry owns the type index — it is the one place that knows when
|
|
646
|
-
// the registered set changed, so unlike a private memo here it cannot go
|
|
647
|
-
// stale on a language registered after the first lookup.
|
|
648
|
-
return type ? this.services.ServiceRegistry.soleServicesByType(type) : undefined;
|
|
721
|
+
const hint = isSyntheticSource(source)
|
|
722
|
+
? { languageId: source.language, target: source.uri, type: source.type }
|
|
723
|
+
: isDocumentSource(source)
|
|
724
|
+
? { target: source.uri }
|
|
725
|
+
: {};
|
|
726
|
+
return registry.getLanguageFor(hint) ?? this.fallbackReferenceLanguage(source);
|
|
649
727
|
}
|
|
650
728
|
/**
|
|
651
729
|
* Locate the document that holds the element an {@link ElementSource}
|
|
@@ -654,15 +732,14 @@ export class DataServer {
|
|
|
654
732
|
* disambiguates names that repeat across types (honouring grammar
|
|
655
733
|
* subtyping through `AstReflection.isSubtype`).
|
|
656
734
|
*
|
|
657
|
-
* Only reached
|
|
658
|
-
*
|
|
659
|
-
* step 2.
|
|
735
|
+
* Only reached in a multi-language workspace, once the declared language
|
|
736
|
+
* and the URI have abstained.
|
|
660
737
|
*
|
|
661
738
|
* Abstains when the name matches elements in more than one DOCUMENT — the
|
|
662
739
|
* index spans every language and is filled in build order, so "the
|
|
663
|
-
* first match" would be file-watch order rather than an answer.
|
|
664
|
-
* then falls through to the type
|
|
665
|
-
*
|
|
740
|
+
* first match" would be file-watch order rather than an answer. Routing
|
|
741
|
+
* then falls through to the element's type, which abstains on ties in the
|
|
742
|
+
* same way, and finally to adopter policy.
|
|
666
743
|
*/
|
|
667
744
|
findElementDocumentUri(source) {
|
|
668
745
|
const matches = this.services.workspace.IndexManager.getElementsByName(source.name, source.type);
|
|
@@ -694,55 +771,152 @@ export class DataServer {
|
|
|
694
771
|
// ============================================================
|
|
695
772
|
// Internal plumbing
|
|
696
773
|
// ============================================================
|
|
774
|
+
/**
|
|
775
|
+
* Whether `error`, from waiting on `uri`'s document, means that `uri` has
|
|
776
|
+
* no document: a URI with neither a file nor text builds nothing, and
|
|
777
|
+
* Langium's wait after the build rejects for want of one, with a
|
|
778
|
+
* `ServerCancelled` response error. Any other failure, such as a read
|
|
779
|
+
* that fails before a document is registered, a cancelled wait or a
|
|
780
|
+
* `ReentrantWriteLockError`, is the caller's to see.
|
|
781
|
+
*/
|
|
782
|
+
isMissingDocument(error, uri) {
|
|
783
|
+
return isResponseError(error) && error.code === SERVER_CANCELLED && this.modelService.getDocument(uri) === undefined;
|
|
784
|
+
}
|
|
697
785
|
/**
|
|
698
786
|
* Build a {@link TransferDocument} envelope from the current document state,
|
|
699
787
|
* delegating root + diagnostic encoding to {@link encoder} (see
|
|
700
|
-
* `TransferEncoder.toTransferDocument` for the walk).
|
|
701
|
-
*
|
|
702
|
-
*
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
* its wire types. The cast on the return is where that invariant — the
|
|
706
|
-
* adopter's `TTransfer` matches its encoder's overlay — is asserted, at a
|
|
707
|
-
* single boundary point rather than spread across the callers.
|
|
708
|
-
*/
|
|
709
|
-
envelope(uri) {
|
|
788
|
+
* `TransferEncoder.toTransferDocument` for the walk). `fingerprint` is the
|
|
789
|
+
* caller's {@link computeDocumentFingerprint} of that same state, which
|
|
790
|
+
* spares hashing it twice.
|
|
791
|
+
*/
|
|
792
|
+
envelope(uri, fingerprint) {
|
|
710
793
|
// Resolve through the model service's canonicalizing gateway rather than
|
|
711
794
|
// reaching into `LangiumDocuments` directly, so a divergent (symlink) URI
|
|
712
795
|
// still finds the document the build keys by its real path — and so the
|
|
713
796
|
// data-server never has to remember to canonicalize this lookup itself.
|
|
714
797
|
const document = this.modelService.getDocument(uri.toString());
|
|
715
|
-
if (!document) {
|
|
798
|
+
if (!document || this.services.workspace.ModelLedger.isPlaceholder(document.parseResult.value)) {
|
|
716
799
|
// No document — a shaped envelope rather than a throw, so the caller
|
|
717
|
-
// decides policy at its own layer; `
|
|
800
|
+
// decides policy at its own layer; `model` is optional on the envelope
|
|
718
801
|
// so the compiler forces that decision. Adopters preferring to throw
|
|
719
|
-
// override `envelope`.
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
802
|
+
// override `envelope`. The builder's unparsed placeholder is no
|
|
803
|
+
// document either: its root parses no text.
|
|
804
|
+
return TransferDocument.absent(uri.toString());
|
|
805
|
+
}
|
|
806
|
+
const encoded = this.encoder.toTransferDocument(document);
|
|
807
|
+
return this.withServerState(encoded, fingerprint ?? this.computeDocumentFingerprint(document.parseResult.value, encoded));
|
|
808
|
+
}
|
|
809
|
+
/**
|
|
810
|
+
* The transfer document a request answers with for `astDocument`, its model
|
|
811
|
+
* hashed and the text stamped on it by {@link withServerState}. Every
|
|
812
|
+
* document a request answers with goes through here, so none goes out
|
|
813
|
+
* without them.
|
|
814
|
+
*/
|
|
815
|
+
encodeDocument(astDocument) {
|
|
816
|
+
const encoded = this.encoder.astDocumentToTransferDocument(astDocument);
|
|
817
|
+
return this.withServerState(encoded, this.computeDocumentFingerprint(astDocument.root, encoded));
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* The project a client receives for `project`, unchanged by default. Every
|
|
821
|
+
* project a request answers with or a change notification carries goes
|
|
822
|
+
* through here, so a field derived for clients cannot miss one path. The
|
|
823
|
+
* result crosses as JSON, so a value a client reads goes in a data field,
|
|
824
|
+
* not a getter.
|
|
825
|
+
*/
|
|
826
|
+
encodeProject(project) {
|
|
827
|
+
return project;
|
|
828
|
+
}
|
|
829
|
+
/**
|
|
830
|
+
* `encoded` with `hash` on its model and the text the server holds for it:
|
|
831
|
+
* the store's while it holds the document, the build's otherwise. The text
|
|
832
|
+
* is read when the document is sent rather than kept with the build: a save
|
|
833
|
+
* changes `text.dirty` without a rebuild, and the store's text moves on before
|
|
834
|
+
* the build that follows it.
|
|
835
|
+
*/
|
|
836
|
+
withServerState(encoded, hash) {
|
|
837
|
+
const text = this.serverTextState(encoded.uri);
|
|
838
|
+
if (text && text.version > encoded.model.version) {
|
|
839
|
+
this.tracer.withUri(encoded.uri).debug(`Send a model behind its text: model v${encoded.model.version} / text v${text.version}`);
|
|
726
840
|
}
|
|
727
|
-
return
|
|
841
|
+
return {
|
|
842
|
+
uri: encoded.uri,
|
|
843
|
+
// The encoder returns the structural base; `TTransfer` is the adopter's
|
|
844
|
+
// declaration of the roots its languages produce, and nothing checks it.
|
|
845
|
+
model: { ...encoded.model, hash },
|
|
846
|
+
...(text ? { text } : {})
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
/**
|
|
850
|
+
* The text the server holds for `uri`, as every envelope and a watch's
|
|
851
|
+
* answer carry it: the store's while it holds the document, the build's
|
|
852
|
+
* otherwise, and none for a document it has neither of.
|
|
853
|
+
*/
|
|
854
|
+
serverTextState(uri) {
|
|
855
|
+
return this.services.workspace.TextDocuments.textState(uri) ?? this.builtTextState(uri);
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* The text a build read for a document the store never held, which is
|
|
859
|
+
* clean: the store holds every text a client gave it.
|
|
860
|
+
*/
|
|
861
|
+
builtTextState(uri) {
|
|
862
|
+
const textDocument = this.modelService.getDocument(uri)?.textDocument;
|
|
863
|
+
return textDocument && { version: textDocument.version, hash: textHash(textDocument.getText()), dirty: false };
|
|
728
864
|
}
|
|
729
865
|
/**
|
|
730
|
-
*
|
|
731
|
-
*
|
|
732
|
-
*
|
|
733
|
-
* regardless of subscriber count.
|
|
866
|
+
* Follow every document at the configured
|
|
867
|
+
* {@link DataServerOptions.subscriptionPhase}, once per connection rather
|
|
868
|
+
* than once per watch, so the cost stays flat in the number of watches.
|
|
734
869
|
*/
|
|
735
|
-
|
|
736
|
-
this.
|
|
870
|
+
subscribeToModelUpdates() {
|
|
871
|
+
const phase = this.options.subscriptionPhase;
|
|
872
|
+
this.disposables.push(this.modelService.onModelUpdated(event => this.dispatchPhaseEvent(event), { phase }));
|
|
873
|
+
// One message per build rather than one per document.
|
|
874
|
+
this.disposables.push(this.modelService.onModelsBuilt(event => this.dispatchBuiltEvent(event.uris), { phase }));
|
|
737
875
|
}
|
|
738
876
|
/**
|
|
739
|
-
*
|
|
740
|
-
*
|
|
741
|
-
*
|
|
742
|
-
*
|
|
743
|
-
* or any other client writing through `notifyDidSaveTextDocument`.
|
|
877
|
+
* Report the documents that reached {@link DataServerOptions.subscriptionPhase}
|
|
878
|
+
* and that NOBODY on this
|
|
879
|
+
* connection is watching — the ones a client was not told about through
|
|
880
|
+
* {@link dispatchPhaseEvent}, which is gated per URI.
|
|
744
881
|
*
|
|
745
|
-
*
|
|
882
|
+
* **The gap this closes is the one no other source can observe.** A document
|
|
883
|
+
* the client watches, it hears about already. A file changed on disk, the
|
|
884
|
+
* host's own filesystem watcher reports — and on a browser host, where the
|
|
885
|
+
* workspace lives behind this head, an external change cannot happen at all.
|
|
886
|
+
* What is left, and what nothing outside the server can see, is a document
|
|
887
|
+
* rebuilt because something it DEPENDS ON changed: its file never changed, so
|
|
888
|
+
* a filesystem watcher is silent by construction, and it has no subscriber, so
|
|
889
|
+
* the update channel is silent by design. A consumer displaying data derived
|
|
890
|
+
* from that document — a tree label, a decorator — otherwise goes stale with
|
|
891
|
+
* no signal from any source, and repairs itself the moment someone opens the
|
|
892
|
+
* file to investigate, which is what makes it expensive to diagnose later.
|
|
893
|
+
*
|
|
894
|
+
* Payload-free on purpose: URIs only, so a client re-reads what it displays
|
|
895
|
+
* rather than being handed transfer documents it did not ask for. That is the
|
|
896
|
+
* property the watcher gate protects, and it is preserved here by
|
|
897
|
+
* carrying no document rather than by gating the message.
|
|
898
|
+
*
|
|
899
|
+
* Quiet in the common case. Editing a document that an editor has open leaves
|
|
900
|
+
* that URI watched and therefore out of this set, so a build with no
|
|
901
|
+
* dependents produces nothing at all, and workspace initialisation produces
|
|
902
|
+
* nothing because it does not build to this phase. The ceiling is a
|
|
903
|
+
* whole-workspace rebuild at the subscription phase: one message, URIs only.
|
|
904
|
+
*/
|
|
905
|
+
dispatchBuiltEvent(built) {
|
|
906
|
+
const uris = built.map(uri => this.canonicalKey(uri)).filter(uri => !this.uriWatchRecords.get(uri)?.watchers.size);
|
|
907
|
+
if (uris.length === 0) {
|
|
908
|
+
return;
|
|
909
|
+
}
|
|
910
|
+
this.tracer.debug(`Emit onDocumentsBuilt: ${uris.length} unwatched document(s)`);
|
|
911
|
+
this.clientProxy.onDocumentsBuilt({ uris });
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Follow every save so a single `onDocumentSaved` wire notification fires
|
|
915
|
+
* for ANY save of a subscribed URI — regardless of whether the save
|
|
916
|
+
* originated from the data-server's RPC `saveModelDocument`, the LSP head's
|
|
917
|
+
* text-editor save, or any other client.
|
|
918
|
+
*
|
|
919
|
+
* Architectural symmetry with {@link subscribeToModelUpdates}: every
|
|
746
920
|
* subscribed client sees every state change to documents they care about,
|
|
747
921
|
* regardless of which client triggered it. Firing `onDocumentSaved` only
|
|
748
922
|
* from the data-server's own RPC path is a bug, not an optimisation: an
|
|
@@ -750,37 +924,108 @@ export class DataServer {
|
|
|
750
924
|
* clients, which never clear their dirty state.
|
|
751
925
|
*/
|
|
752
926
|
subscribeToTextDocumentSaves() {
|
|
753
|
-
this.disposables.push(this.
|
|
927
|
+
this.disposables.push(this.modelService.onModelSaved(event => this.dispatchSaveEvent(event)));
|
|
928
|
+
}
|
|
929
|
+
/** Relay each change of a document's dirty state; see {@link dispatchDirtyEvent}. */
|
|
930
|
+
subscribeToDirtyChanges() {
|
|
931
|
+
this.disposables.push(this.modelService.onDirtyChanged(event => this.dispatchDirtyEvent(event)));
|
|
754
932
|
}
|
|
755
933
|
/**
|
|
756
|
-
*
|
|
757
|
-
* {@link
|
|
758
|
-
*
|
|
759
|
-
* hold, so a `false` answer means this close was the last one.
|
|
934
|
+
* Send a dirty flip to the connection, gated on the URI's watchers as
|
|
935
|
+
* {@link dispatchSaveEvent} is: the answer at any one moment travels on
|
|
936
|
+
* every document sent, so a client that watches nothing reads it there.
|
|
760
937
|
*/
|
|
761
|
-
|
|
762
|
-
const
|
|
763
|
-
this.
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
938
|
+
dispatchDirtyEvent(event) {
|
|
939
|
+
const uri = this.canonicalKey(event.uri);
|
|
940
|
+
if (this.uriWatchRecords.get(uri)?.watchers.size) {
|
|
941
|
+
this.clientProxy.onDocumentDirtyChanged({ uri, text: event.text });
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
/**
|
|
945
|
+
* Mark each document the store releases, so {@link dispatchPhaseEvent}
|
|
946
|
+
* broadcasts the build that follows even without a watcher. By default
|
|
947
|
+
* that build reverts the document to its disk content, discarding unsaved
|
|
948
|
+
* in-session edits, and the last close typically also
|
|
949
|
+
* removed the last watcher: without the broadcast, a consumer that only
|
|
950
|
+
* ever fetches via `getModelDocument` keeps showing the discarded state.
|
|
951
|
+
* The broadcast is de-duplicated against the fingerprint while a watcher
|
|
952
|
+
* holds one, so a close whose disk state equals the last emitted state
|
|
953
|
+
* stays silent; with no watcher there is no fingerprint, and every release
|
|
954
|
+
* build is sent. The first build after the release consumes the mark, and
|
|
955
|
+
* goes out as the release unless it is stamped past the version a client
|
|
956
|
+
* opened the document at again: a client's write or an integrity repair
|
|
957
|
+
* that rides it keeps its author.
|
|
958
|
+
*
|
|
959
|
+
* A release, not the close itself: a document whose last client lost its
|
|
960
|
+
* connection is released only once the release grace runs out, or when
|
|
961
|
+
* another client opens it meanwhile, and not at all when a client lost
|
|
962
|
+
* from it opens it again within its own grace.
|
|
963
|
+
*/
|
|
964
|
+
subscribeToModelReleases() {
|
|
965
|
+
this.disposables.push(this.modelService.onModelReleased(event => {
|
|
966
|
+
const uri = this.canonicalKey(event.uri);
|
|
967
|
+
const record = this.uriWatchRecords.get(uri) ?? { watchers: new Set() };
|
|
968
|
+
record.releaseBroadcastPending = true;
|
|
969
|
+
this.uriWatchRecords.set(uri, record);
|
|
970
|
+
this.tracer.withUri(uri).debug('Release marked: the first build after it is broadcast, watched or not');
|
|
767
971
|
}));
|
|
768
972
|
}
|
|
769
|
-
/**
|
|
973
|
+
/**
|
|
974
|
+
* Fan out a deletion for each removed URI, to EVERY client on the connection
|
|
975
|
+
* rather than only to the ones watching that URI.
|
|
976
|
+
*
|
|
977
|
+
* **Deliberately ungated, where {@link dispatchPhaseEvent} and
|
|
978
|
+
* {@link dispatchSaveEvent} are gated.** Those two carry a built document and
|
|
979
|
+
* fire on every build, so the watcher gate is what keeps bandwidth
|
|
980
|
+
* proportional to what a client asked for. A deletion is neither: it is one
|
|
981
|
+
* URI, it is rare, and it reports the workspace's STRUCTURE rather than a
|
|
982
|
+
* document's content. Gating it forces any consumer that displays the
|
|
983
|
+
* workspace — a model tree, a file decorator — to learn about disappearances
|
|
984
|
+
* from a filesystem watcher instead, which a client hosted in a browser has
|
|
985
|
+
* no way to run: its workspace lives behind the head. A client that does not
|
|
986
|
+
* care filters on the URI, which costs it a comparison.
|
|
987
|
+
*
|
|
988
|
+
* The precedent is the release broadcast (see
|
|
989
|
+
* {@link subscribeToModelReleases}), where the same judgement was already made
|
|
990
|
+
* in the other direction: a transition that matters enough is delivered
|
|
991
|
+
* without a subscription.
|
|
992
|
+
*
|
|
993
|
+
* A deletion is announced before the build that follows it, so this goes
|
|
994
|
+
* out ahead of the `'rebuilt'` events for the dependents whose references
|
|
995
|
+
* the deletion just broke.
|
|
996
|
+
*
|
|
997
|
+
* The URI's fingerprint and release mark are dropped: they describe a
|
|
998
|
+
* document that no longer exists. A kept fingerprint is adopted as the
|
|
999
|
+
* baseline and suppresses the first emit after the file returns with its
|
|
1000
|
+
* previous content.
|
|
1001
|
+
*
|
|
1002
|
+
* The watchers SURVIVE: a recreated file resumes delivering to them with no
|
|
1003
|
+
* re-subscription, and a client that answers the deletion by closing
|
|
1004
|
+
* releases the watch through `closeModelDocument` anyway. A session that
|
|
1005
|
+
* deleted the document has its own released by {@link deleteModelDocument}.
|
|
1006
|
+
*/
|
|
1007
|
+
dispatchDeleteEvent(event) {
|
|
1008
|
+
const uri = this.canonicalKey(event.uri);
|
|
1009
|
+
const record = this.uriWatchRecords.get(uri);
|
|
1010
|
+
if (record) {
|
|
1011
|
+
record.fingerprint = undefined;
|
|
1012
|
+
record.fingerprintVersion = undefined;
|
|
1013
|
+
record.releaseBroadcastPending = undefined;
|
|
1014
|
+
record.sentVersion = undefined;
|
|
1015
|
+
this.pruneUriWatchRecord(uri);
|
|
1016
|
+
}
|
|
1017
|
+
this.clientProxy.onDocumentDeleted({ uri, sourceClientId: event.sourceClientId, sequence: ++this.eventSequence });
|
|
1018
|
+
}
|
|
1019
|
+
/** Fan out a save event for the document's URI, gated on the URI's watchers. */
|
|
770
1020
|
dispatchSaveEvent(event) {
|
|
771
|
-
// The save event arrives under the CLIENT URI the text store keys by (e.g. a
|
|
772
|
-
// symlink path S); the subscription map and `dispatchPhaseEvent` key by the
|
|
773
|
-
// CANONICAL identity R. Canonicalize before both the gate and the envelope so
|
|
774
|
-
// a save of a symlinked file isn't silently dropped (and the envelope resolves
|
|
775
|
-
// the R-keyed document rather than missing into an empty one).
|
|
776
1021
|
const uri = this.canonicalKey(event.document.uri);
|
|
777
|
-
if (!this.
|
|
1022
|
+
if (!this.uriWatchRecords.get(uri)?.watchers.size) {
|
|
778
1023
|
return;
|
|
779
1024
|
}
|
|
780
|
-
const response = this.envelope(UriUtils.toUri(uri));
|
|
1025
|
+
const response = this.envelope(UriUtils.toUri(event.document.uri));
|
|
781
1026
|
const wireEvent = {
|
|
782
1027
|
document: response,
|
|
783
|
-
sourceClientId: event.
|
|
1028
|
+
sourceClientId: event.sourceClientId
|
|
784
1029
|
};
|
|
785
1030
|
this.clientProxy.onDocumentSaved(wireEvent);
|
|
786
1031
|
}
|
|
@@ -793,107 +1038,107 @@ export class DataServer {
|
|
|
793
1038
|
* out to multiple local subscribers with an `Emitter<T>` — the
|
|
794
1039
|
* framework deliberately does NOT promise multi-listener semantics.
|
|
795
1040
|
*
|
|
796
|
-
*
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
*
|
|
800
|
-
*
|
|
801
|
-
*
|
|
802
|
-
*
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
1041
|
+
* An event for a URI no client watches is NOT sent over the wire, so
|
|
1042
|
+
* bandwidth scales with watched URIs rather than with phase events; a
|
|
1043
|
+
* pending release mark is the exception (see
|
|
1044
|
+
* {@link subscribeToModelReleases}).
|
|
1045
|
+
*
|
|
1046
|
+
* An event for a rebuild with no observable change since the last emit is
|
|
1047
|
+
* suppressed against the URI's fingerprint and the version it was taken at;
|
|
1048
|
+
* a new version with the same fingerprint goes out with the same
|
|
1049
|
+
* `model.hash`, so a watcher still learns the version. Such rebuilds are routine: a
|
|
1050
|
+
* second client attaching to an open document makes
|
|
1051
|
+
* `HydraniumTextDocuments.refreshContent` fire `onDidChangeContent` purely
|
|
1052
|
+
* to re-trigger the build, and Langium rebuilds a document when a
|
|
1053
|
+
* dependency changes even if its diagnostics come out the same. Sent at a
|
|
1054
|
+
* version this head has not sent, they reach watchers as `'changed'`, and a
|
|
1055
|
+
* watcher that reads `'changed'` as another client's write and resets its
|
|
1056
|
+
* root to the server view loses the user's edits. The first {@link watchModelDocument} baselines the
|
|
1057
|
+
* fingerprint, so the first event after a watch is de-duplicated against
|
|
1058
|
+
* the state the watcher just fetched; it goes with the last watcher, so the
|
|
1059
|
+
* next first watch re-baselines rather than adopting a stale digest. A
|
|
1060
|
+
* digest keeps its memory constant in document size; see
|
|
1061
|
+
* {@link computeDocumentFingerprint} for the inputs.
|
|
1062
|
+
*/
|
|
1063
|
+
dispatchPhaseEvent(event) {
|
|
1064
|
+
const uri = this.canonicalKey(event.document.uri);
|
|
1065
|
+
const record = this.uriWatchRecords.get(uri);
|
|
1066
|
+
const afterRelease = record?.releaseBroadcastPending === true;
|
|
1067
|
+
if (!record || (record.watchers.size === 0 && !afterRelease)) {
|
|
811
1068
|
return;
|
|
812
1069
|
}
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
1070
|
+
if (afterRelease) {
|
|
1071
|
+
// Consumed even when watchers exist — the regular dispatch below
|
|
1072
|
+
// serves them, and the mark's attribution is more precise than the
|
|
1073
|
+
// post-close unknown client the manager's attribution would yield.
|
|
1074
|
+
// With no watcher the record goes now, so the fingerprint written
|
|
1075
|
+
// below lands on a detached record and the next first watch
|
|
1076
|
+
// re-baselines instead of adopting it.
|
|
1077
|
+
record.releaseBroadcastPending = undefined;
|
|
1078
|
+
this.pruneUriWatchRecord(uri);
|
|
1079
|
+
}
|
|
1080
|
+
// The encoder reads the built document, which the event's holds for this
|
|
1081
|
+
// synchronous run.
|
|
1082
|
+
const document = this.modelService.getDocument(event.document.uri);
|
|
1083
|
+
if (!document) {
|
|
820
1084
|
return;
|
|
821
1085
|
}
|
|
822
|
-
const fingerprint = this.computeDocumentFingerprint(document);
|
|
823
|
-
|
|
1086
|
+
const fingerprint = this.computeDocumentFingerprint(document.parseResult.value, this.encoder.toTransferDocument(document));
|
|
1087
|
+
const stamped = this.services.workspace.ModelLedger.versionOf(document.parseResult.value);
|
|
1088
|
+
if (record.fingerprint === fingerprint && record.fingerprintVersion === stamped) {
|
|
824
1089
|
// A rebuild that produced no observable change since the last emit —
|
|
825
1090
|
// suppressed so RPC subscribers don't see a no-op broadcast. Logged at
|
|
826
1091
|
// debug so a *needed* re-broadcast wrongly suppressed by this dedup
|
|
827
1092
|
// (the failure mode the fingerprint strategy must avoid) is visible.
|
|
828
|
-
this.tracer.withUri(uri).debug(`Suppress onDocumentUpdated v${
|
|
1093
|
+
this.tracer.withUri(uri).debug(`Suppress onDocumentUpdated v${stamped}: fingerprint and version unchanged`);
|
|
829
1094
|
return;
|
|
830
1095
|
}
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
1096
|
+
record.fingerprint = fingerprint;
|
|
1097
|
+
record.fingerprintVersion = stamped;
|
|
1098
|
+
// The event's attribution, so this head names the same client as the
|
|
1099
|
+
// in-process heads do for one build, except for a version this head
|
|
1100
|
+
// already sent; see `DataServerUriWatchRecord.sentVersion`.
|
|
1101
|
+
const version = this.modelService.isOpen(event.document.uri) ? document.textDocument.version : undefined;
|
|
1102
|
+
const { reason, sourceClientId } = version !== undefined && record.sentVersion === version
|
|
1103
|
+
? { reason: 'rebuilt', sourceClientId: UNKNOWN_CLIENT_ID }
|
|
1104
|
+
: event;
|
|
1105
|
+
record.sentVersion = version;
|
|
1106
|
+
// The stamp, not the live version: a write after the build's parse moves
|
|
1107
|
+
// only the latter. At the opened version too, which a reopen with the
|
|
1108
|
+
// released text keeps.
|
|
1109
|
+
const openedVersion = afterRelease ? this.modelService.openedVersion(event.document.uri) : undefined;
|
|
1110
|
+
const releaseBuild = afterRelease && (openedVersion === undefined || stamped <= openedVersion);
|
|
1111
|
+
const wireEvent = {
|
|
1112
|
+
document: this.envelope(document.uri, fingerprint),
|
|
1113
|
+
sourceClientId: releaseBuild ? DOCUMENT_RELEASE_CLIENT_ID : sourceClientId,
|
|
1114
|
+
reason,
|
|
1115
|
+
sequence: ++this.eventSequence
|
|
836
1116
|
};
|
|
1117
|
+
const mark = afterRelease ? `, release mark: opened v${openedVersion ?? '-'}, attributed ${sourceClientId}` : '';
|
|
837
1118
|
this.tracer
|
|
838
1119
|
.withUri(uri)
|
|
839
|
-
.debug(`Emit onDocumentUpdated v${
|
|
840
|
-
this.clientProxy.onDocumentUpdated(
|
|
1120
|
+
.debug(`Emit onDocumentUpdated v${stamped} (reason=${wireEvent.reason}, sourceClientId=${wireEvent.sourceClientId}${mark})`);
|
|
1121
|
+
this.clientProxy.onDocumentUpdated(wireEvent);
|
|
841
1122
|
}
|
|
842
1123
|
/**
|
|
843
|
-
* Fingerprint of the
|
|
844
|
-
* `onDocumentUpdated` emissions
|
|
845
|
-
*
|
|
846
|
-
* {@link
|
|
1124
|
+
* Fingerprint of the snapshot `root` and its encoding `encoded`, used to
|
|
1125
|
+
* de-dup `onDocumentUpdated` emissions and sent as every document's
|
|
1126
|
+
* `model.hash`, so it must not depend on the version or on live state. The
|
|
1127
|
+
* {@link FingerprintStrategy} option selects what is hashed (default
|
|
1128
|
+
* `'transfer-document'` — see the type).
|
|
847
1129
|
*
|
|
848
|
-
*
|
|
849
|
-
*
|
|
850
|
-
*
|
|
851
|
-
* `envelope` share a single encode walk.
|
|
1130
|
+
* {@link TransferEncoder.toTransferDocument} is cached per build, so within
|
|
1131
|
+
* one phase event the fingerprint and the subsequently-emitted `envelope`
|
|
1132
|
+
* share a single encode walk.
|
|
852
1133
|
*
|
|
853
1134
|
* Wrapped in {@link Tracer.time} against {@link FINGERPRINT_LOG_AFTER_MS}.
|
|
854
1135
|
*/
|
|
855
|
-
computeDocumentFingerprint(
|
|
856
|
-
return this.tracer
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
return extra.length === 0 ? base : fingerprintHash([base, FINGERPRINT_SEPARATOR, ...extra.map(value => JSON.stringify(value))]);
|
|
862
|
-
}, 'debug', { logAfterMs: FINGERPRINT_LOG_AFTER_MS });
|
|
863
|
-
}
|
|
864
|
-
/**
|
|
865
|
-
* Extra inputs folded into {@link computeDocumentFingerprint} alongside the
|
|
866
|
-
* chosen {@link FingerprintStrategy}. Default: none. Override to contribute a
|
|
867
|
-
* signal that lives outside the document's text / root / diagnostics — each
|
|
868
|
-
* entry need only be stable across equivalent emissions and
|
|
869
|
-
* JSON-serialisable.
|
|
870
|
-
*/
|
|
871
|
-
additionalFingerprintInputs(_document) {
|
|
872
|
-
return [];
|
|
873
|
-
}
|
|
874
|
-
/**
|
|
875
|
-
* Discriminate the reason for a phase-event-driven update notification.
|
|
876
|
-
* Uses the most recent `DocumentBuilder.onUpdate` snapshot:
|
|
877
|
-
* - URI in the `deleted` list → `'deleted'`
|
|
878
|
-
* - URI in the `changed` list → `'changed'` (the URI was passed to
|
|
879
|
-
* `documentBuilder.update(changed, deleted)`, which spans `didChange`
|
|
880
|
-
* text-document events and programmatic `update([uri], [])` calls).
|
|
881
|
-
* - Otherwise → `'rebuilt'` (cascade re-derivation: this URI was rebuilt
|
|
882
|
-
* because something it depends on changed; its own text wasn't flagged).
|
|
883
|
-
*
|
|
884
|
-
* `'saved'` is NOT emitted from this code path — saves take the dedicated
|
|
885
|
-
* `DataClientProtocol.onDocumentSaved` channel; adopters that want a
|
|
886
|
-
* unified update stream synthesise `'saved'` in their bridge layer.
|
|
887
|
-
*/
|
|
888
|
-
resolveUpdateReason(uri) {
|
|
889
|
-
const last = this.lastBuildUpdate;
|
|
890
|
-
if (last?.deleted.some(deleted => UriUtils.equals(deleted, uri))) {
|
|
891
|
-
return 'deleted';
|
|
892
|
-
}
|
|
893
|
-
if (last?.changed.some(changed => UriUtils.equals(changed, uri))) {
|
|
894
|
-
return 'changed';
|
|
895
|
-
}
|
|
896
|
-
return 'rebuilt';
|
|
1136
|
+
computeDocumentFingerprint(root, encoded) {
|
|
1137
|
+
return this.tracer
|
|
1138
|
+
.withUri(encoded.uri)
|
|
1139
|
+
.time('Compute fingerprint', () => this.options.fingerprintStrategy === 'text-diagnostics'
|
|
1140
|
+
? textDiagnosticsFingerprint(this.services.workspace.ModelLedger.textOf(root), encoded.model)
|
|
1141
|
+
: transferDocumentFingerprint(encoded.model), 'debug', { logAfterMs: FINGERPRINT_LOG_AFTER_MS });
|
|
897
1142
|
}
|
|
898
1143
|
/**
|
|
899
1144
|
* Subscribe to the project tier's change channel and re-fan registry
|
|
@@ -917,38 +1162,38 @@ export class DataServer {
|
|
|
917
1162
|
/**
|
|
918
1163
|
* Fan one internal {@link ProjectChangeEvent} out to per-project wire
|
|
919
1164
|
* notifications. Each `removed` entry pairs the id with the pre-removal
|
|
920
|
-
* snapshot needed for the `'removed'` wire payload.
|
|
1165
|
+
* snapshot needed for the `'removed'` wire payload. A project whose
|
|
1166
|
+
* {@link encodeProject} throws is logged and skipped, so the rest of the
|
|
1167
|
+
* batch still reaches the client.
|
|
921
1168
|
*/
|
|
922
1169
|
dispatchProjectChangeEvent(event) {
|
|
1170
|
+
const send = (project, reason) => {
|
|
1171
|
+
let encoded;
|
|
1172
|
+
try {
|
|
1173
|
+
encoded = this.encodeProject(project);
|
|
1174
|
+
}
|
|
1175
|
+
catch (error) {
|
|
1176
|
+
this.tracer.error(`Project ${project.id} not sent as ${reason}: encodeProject threw: ${describeErrorWithStack(error)}`);
|
|
1177
|
+
return;
|
|
1178
|
+
}
|
|
1179
|
+
this.clientProxy.onProjectsChanged({ project: encoded, reason });
|
|
1180
|
+
};
|
|
923
1181
|
for (const id of event.added) {
|
|
924
1182
|
const project = this.services.workspace.ProjectManager.getProjectById(id);
|
|
925
1183
|
if (project) {
|
|
926
|
-
|
|
1184
|
+
send(project, 'added');
|
|
927
1185
|
}
|
|
928
1186
|
}
|
|
929
1187
|
for (const id of event.updated) {
|
|
930
1188
|
const project = this.services.workspace.ProjectManager.getProjectById(id);
|
|
931
1189
|
if (project) {
|
|
932
|
-
|
|
1190
|
+
send(project, 'updated');
|
|
933
1191
|
}
|
|
934
1192
|
}
|
|
935
1193
|
for (const entry of event.removed) {
|
|
936
|
-
|
|
1194
|
+
send(entry.snapshot, 'removed');
|
|
937
1195
|
}
|
|
938
1196
|
}
|
|
939
|
-
/**
|
|
940
|
-
* Resolve the wire-level `sourceClientId` for `document`'s events — the client
|
|
941
|
-
* that authored its current version, from the version-author history on
|
|
942
|
-
* `HydraniumTextDocuments`. The protocol-level counterpart of the internal
|
|
943
|
-
* `AstDocumentManager.getAuthor`: a framework-internal rebuild has no author,
|
|
944
|
-
* so this surfaces the {@link UNKNOWN_CLIENT_ID} presentation default. Adopters
|
|
945
|
-
* that rebuild through non-text-document channels override to derive a
|
|
946
|
-
* source id of their own.
|
|
947
|
-
*/
|
|
948
|
-
resolveSourceClientId(document) {
|
|
949
|
-
const author = this.services.workspace.TextDocuments.getAuthor(document.textDocument.uri, document.textDocument.version);
|
|
950
|
-
return author ?? UNKNOWN_CLIENT_ID;
|
|
951
|
-
}
|
|
952
1197
|
// --- Diagnostics (DataServerDiagnosticsProtocol) ---------------------------
|
|
953
1198
|
// Computed in THIS process: the data-server child holds the model store, so
|
|
954
1199
|
// these snapshots reflect the heap that actually carries the workspace
|
|
@@ -1006,7 +1251,7 @@ export class DataServer {
|
|
|
1006
1251
|
const builtIn = [...DATA_SERVER_PROTOCOL_METHODS, ...DATA_SERVER_DIAGNOSTICS_METHODS];
|
|
1007
1252
|
const overlap = additionalMethods.filter(name => builtIn.includes(name));
|
|
1008
1253
|
if (overlap.length > 0) {
|
|
1009
|
-
throw new Error(`
|
|
1254
|
+
throw new Error(`DataServerOptions.additionalMethods overlaps with built-in data-server methods: ${overlap.join(', ')}. ` +
|
|
1010
1255
|
'Adopter protocols must not redeclare framework method names; rename the adopter method or remove it from additionalMethods.');
|
|
1011
1256
|
}
|
|
1012
1257
|
return {
|
|
@@ -1015,7 +1260,8 @@ export class DataServer {
|
|
|
1015
1260
|
methodNamespace: partial.methodNamespace ?? DATA_SERVER_WIRE_PREFIX,
|
|
1016
1261
|
additionalMethods,
|
|
1017
1262
|
excludedMethods: partial.excludedMethods ?? [],
|
|
1018
|
-
diagnostics: partial.diagnostics ?? defaultDataServerDiagnostics()
|
|
1263
|
+
diagnostics: partial.diagnostics ?? defaultDataServerDiagnostics(),
|
|
1264
|
+
referenceSettleTimeoutMs: partial.referenceSettleTimeoutMs ?? DataServer.DEFAULT_OPTIONS.referenceSettleTimeoutMs
|
|
1019
1265
|
};
|
|
1020
1266
|
}
|
|
1021
1267
|
}
|