@hydranium/data-server 1.0.0-next.22 → 1.0.0-next.220
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 +20 -6
- package/lib/data-server.d.ts +416 -284
- package/lib/data-server.d.ts.map +1 -1
- package/lib/data-server.js +652 -484
- package/lib/data-server.js.map +1 -1
- package/lib/messages/index.d.ts +18 -0
- package/lib/messages/index.d.ts.map +1 -0
- package/lib/messages/index.js +18 -0
- package/lib/messages/index.js.map +1 -0
- 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 -39
- package/src/data-server.ts +808 -525
- package/src/messages/index.ts +26 -0
- 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/src/data-server.ts
CHANGED
|
@@ -9,12 +9,22 @@
|
|
|
9
9
|
|
|
10
10
|
import {
|
|
11
11
|
createRpcProxy,
|
|
12
|
+
defineMessage,
|
|
12
13
|
DisposableCollection,
|
|
14
|
+
HYDRANIUM_ERROR_CODES,
|
|
13
15
|
isDocumentSource,
|
|
14
16
|
isElementSource,
|
|
17
|
+
isResponseError,
|
|
15
18
|
isSyntheticSource,
|
|
19
|
+
messageData,
|
|
20
|
+
messageError,
|
|
16
21
|
ReferenceSource,
|
|
22
|
+
SessionClosedError,
|
|
23
|
+
TIMED_OUT,
|
|
17
24
|
type CloseModelArgs,
|
|
25
|
+
type HydraniumMessageData,
|
|
26
|
+
type HydraniumResponseError,
|
|
27
|
+
type CanonicalUri,
|
|
18
28
|
type Disposable,
|
|
19
29
|
type ElementSource,
|
|
20
30
|
type FindNextNameArgs,
|
|
@@ -26,10 +36,16 @@ import {
|
|
|
26
36
|
type ReferenceContext,
|
|
27
37
|
type ReferenceRequest,
|
|
28
38
|
type ReferenceTarget,
|
|
39
|
+
type TransferModelSnapshot,
|
|
40
|
+
type ModelVersion,
|
|
41
|
+
type TextState,
|
|
29
42
|
type Tracer,
|
|
30
43
|
type TransferDiagnostic,
|
|
31
|
-
|
|
32
|
-
type
|
|
44
|
+
TransferDocument,
|
|
45
|
+
type TransferSavedDocument,
|
|
46
|
+
type TransferElement,
|
|
47
|
+
UNKNOWN_CLIENT_ID,
|
|
48
|
+
textHash
|
|
33
49
|
} from '@hydranium/protocol';
|
|
34
50
|
import {
|
|
35
51
|
DATA_SERVER_DIAGNOSTICS_METHODS,
|
|
@@ -42,29 +58,87 @@ import {
|
|
|
42
58
|
type StartProfilingArgs,
|
|
43
59
|
type StopProfilingArgs,
|
|
44
60
|
type WriteServerHeapSnapshotArgs,
|
|
61
|
+
type CloseSessionArgs,
|
|
62
|
+
type CreateModelDocumentArgs,
|
|
63
|
+
type CreateSessionArgs,
|
|
45
64
|
type GetModelDocumentArgs,
|
|
46
65
|
type GetProjectForUriArgs,
|
|
66
|
+
type TransferPersistDocumentArgs,
|
|
47
67
|
type TransferSaveDocumentArgs,
|
|
48
68
|
type WatchModelDocumentArgs,
|
|
49
69
|
type TransferDocumentSavedEvent,
|
|
50
70
|
type TransferDocumentUpdatedEvent,
|
|
51
|
-
type TransferUpdateDocumentArgs
|
|
71
|
+
type TransferUpdateDocumentArgs,
|
|
72
|
+
type TransferUpdateDocumentsArgs
|
|
52
73
|
} from '@hydranium/protocol/data';
|
|
53
|
-
import {
|
|
74
|
+
import { DOCUMENT_RELEASE_CLIENT_ID } from '@hydranium/core';
|
|
54
75
|
import { defaultDataServerDiagnostics } from './default-diagnostics.js';
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Raised when a profiling command arrives with no capture running.
|
|
79
|
+
*
|
|
80
|
+
* A `ResponseError` rather than a plain `Error` so the identity survives to the
|
|
81
|
+
* response boundary, which is where the message is rendered. An unwrapped throw
|
|
82
|
+
* reaches the client as a generic internal error instead: no code for a caller
|
|
83
|
+
* to switch on, and nothing for the boundary to render from.
|
|
84
|
+
*/
|
|
85
|
+
export const NO_ACTIVE_PROFILE = defineMessage(
|
|
86
|
+
'hydranium/data-server/no-active-profile',
|
|
87
|
+
'No profiling capture is active; call startProfiling first.'
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The JSON-RPC code, unrelated to the catalogue code above: this one is numeric,
|
|
92
|
+
* survives reconstruction and is what a caller switches on.
|
|
93
|
+
*/
|
|
94
|
+
export const NO_ACTIVE_PROFILE_CODE = HYDRANIUM_ERROR_CODES.noActiveProfile;
|
|
95
|
+
|
|
96
|
+
export const noActiveProfileError = (): HydraniumResponseError => messageError(NO_ACTIVE_PROFILE_CODE, NO_ACTIVE_PROFILE);
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Raised when a reference question's wait for the build exceeds
|
|
100
|
+
* {@link DataServerOptions.referenceSettleTimeoutMs}.
|
|
101
|
+
*
|
|
102
|
+
* A `ResponseError` because a class name does not cross RPC: without the code a
|
|
103
|
+
* wire client cannot tell a wedged build from a reference that resolves to
|
|
104
|
+
* nothing, which is the distinction the wait exists to keep.
|
|
105
|
+
*/
|
|
106
|
+
export const REFERENCE_SETTLE_TIMEOUT = defineMessage(
|
|
107
|
+
'hydranium/data-server/reference-settle-timeout',
|
|
108
|
+
'The model is still being processed; try again in a moment.'
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
/** See {@link NO_ACTIVE_PROFILE_CODE} for why this is separate from the catalogue code. */
|
|
112
|
+
export const REFERENCE_SETTLE_TIMEOUT_CODE = HYDRANIUM_ERROR_CODES.referenceSettleTimeout;
|
|
113
|
+
|
|
114
|
+
/** The wait's length rides in `data` as `elapsedMs`, for the code that reacts rather than the reader. */
|
|
115
|
+
export const referenceSettleTimeoutError = (elapsedMs: number): ResponseError<HydraniumMessageData & { readonly elapsedMs: number }> =>
|
|
116
|
+
new ResponseError(REFERENCE_SETTLE_TIMEOUT_CODE, REFERENCE_SETTLE_TIMEOUT.format(), {
|
|
117
|
+
elapsedMs,
|
|
118
|
+
...messageData(REFERENCE_SETTLE_TIMEOUT)
|
|
119
|
+
});
|
|
120
|
+
|
|
55
121
|
import type { DataServerDiagnosticsProvider, DataServerProfileCapture } from './diagnostics-provider.js';
|
|
56
122
|
import type {
|
|
57
|
-
|
|
123
|
+
ClientSession,
|
|
124
|
+
ClientSessionWriteArgs,
|
|
58
125
|
HydraniumLanguageServices,
|
|
59
126
|
LogNameOptions,
|
|
127
|
+
AstDiagnostic,
|
|
128
|
+
AstDocument,
|
|
129
|
+
AstDocumentSavedEvent,
|
|
130
|
+
EncodedTransferDocument,
|
|
131
|
+
ModelDeletedEvent,
|
|
132
|
+
ModelDirtyChangedEvent,
|
|
60
133
|
ModelService,
|
|
134
|
+
ModelUpdatedEvent,
|
|
61
135
|
ProjectChangeEvent,
|
|
62
136
|
ServerSharedServices,
|
|
137
|
+
SessionEndCause,
|
|
63
138
|
TransferEncoder
|
|
64
139
|
} from '@hydranium/core';
|
|
65
|
-
import type
|
|
66
|
-
import { type
|
|
67
|
-
import type { CancellationToken, MessageConnection } from 'vscode-jsonrpc';
|
|
140
|
+
import { type AstNode, DocumentState, UriUtils, type URI } from '@hydranium/langium';
|
|
141
|
+
import { type MessageConnection, ResponseError } from 'vscode-jsonrpc';
|
|
68
142
|
|
|
69
143
|
/**
|
|
70
144
|
* Domain separator between text and diagnostics inputs of
|
|
@@ -75,34 +149,11 @@ import type { CancellationToken, MessageConnection } from 'vscode-jsonrpc';
|
|
|
75
149
|
const FINGERPRINT_SEPARATOR = '\0';
|
|
76
150
|
|
|
77
151
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* **A cryptographic hash via `node:crypto` is the wrong trade**: the
|
|
82
|
-
* fingerprint only needs a stable signal that `(text, diagnostics)` changed
|
|
83
|
-
* between emissions, and a `node:*` import would cost
|
|
84
|
-
* `@hydranium/data-server` its browser-portability. Parts are fed
|
|
85
|
-
* incrementally, char by char, so multi-MB document text is never
|
|
86
|
-
* concatenated into one string.
|
|
152
|
+
* `LSPErrorCodes.ServerCancelled`, with which Langium's document wait rejects
|
|
153
|
+
* for a URI that has no document; `vscode-languageserver-protocol` is not a
|
|
154
|
+
* dependency of this package.
|
|
87
155
|
*/
|
|
88
|
-
|
|
89
|
-
let h1 = 0xdeadbeef;
|
|
90
|
-
let h2 = 0x41c6ce57;
|
|
91
|
-
for (const part of parts) {
|
|
92
|
-
for (let i = 0; i < part.length; i++) {
|
|
93
|
-
const ch = part.charCodeAt(i);
|
|
94
|
-
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
95
|
-
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
|
|
99
|
-
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
100
|
-
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
|
|
101
|
-
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
102
|
-
const high = (h2 >>> 0).toString(16).padStart(8, '0');
|
|
103
|
-
const low = (h1 >>> 0).toString(16).padStart(8, '0');
|
|
104
|
-
return high + low;
|
|
105
|
-
}
|
|
156
|
+
const SERVER_CANCELLED = -32802;
|
|
106
157
|
|
|
107
158
|
/**
|
|
108
159
|
* Which observable state {@link DataServer.computeDocumentFingerprint} hashes to
|
|
@@ -115,26 +166,32 @@ function fingerprintHash(parts: readonly string[]): string {
|
|
|
115
166
|
* folded in on a cascade rebuild that leaves the document's own text
|
|
116
167
|
* untouched. Safe for any adopter whose AST extensions fold derived state
|
|
117
168
|
* into the transfer projection (the framework norm).
|
|
118
|
-
* - `'text-diagnostics'` — the cheaper
|
|
119
|
-
* opt-DOWN for adopters that fold no derived state and want to avoid
|
|
120
|
-
* transfer
|
|
169
|
+
* - `'text-diagnostics'` — the cheaper parsed-text + diagnostics hash. An
|
|
170
|
+
* opt-DOWN for adopters that fold no derived state and want to avoid
|
|
171
|
+
* stringifying the transfer root per phase event.
|
|
121
172
|
*/
|
|
122
173
|
export type FingerprintStrategy = 'transfer-document' | 'text-diagnostics';
|
|
123
174
|
|
|
124
|
-
/**
|
|
125
|
-
|
|
126
|
-
|
|
175
|
+
/**
|
|
176
|
+
* Hash `text`, the text the model was parsed from, + `model`'s diagnostics — the `'text-diagnostics'` strategy.
|
|
177
|
+
* The text comes from the root's ledger record or CST, as the document's own may have moved on,
|
|
178
|
+
* and a repair in place leaves the CST on the unrepaired text;
|
|
179
|
+
* a root built without a CST hashes as `'transfer-document'`.
|
|
180
|
+
* Absent diagnostics must hash apart from `[]`, as in {@link transferDocumentFingerprint}.
|
|
181
|
+
*/
|
|
182
|
+
function textDiagnosticsFingerprint(text: string | undefined, model: EncodedTransferDocument<TransferElement, unknown>['model']): string {
|
|
183
|
+
return text === undefined
|
|
184
|
+
? transferDocumentFingerprint(model)
|
|
185
|
+
: textHash([text, FINGERPRINT_SEPARATOR, JSON.stringify(model.diagnostics ?? null)]);
|
|
127
186
|
}
|
|
128
187
|
|
|
129
|
-
/**
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
JSON.stringify(transferDocument.diagnostics)
|
|
137
|
-
]);
|
|
188
|
+
/**
|
|
189
|
+
* Hash an encoded model's root + diagnostics — the `'transfer-document'` strategy.
|
|
190
|
+
* Absent diagnostics must hash apart from `[]`: a client skipping a render on an
|
|
191
|
+
* equal hash would otherwise miss a validation that found nothing.
|
|
192
|
+
*/
|
|
193
|
+
function transferDocumentFingerprint(model: EncodedTransferDocument<TransferElement, unknown>['model']): string {
|
|
194
|
+
return textHash([JSON.stringify(model.root), FINGERPRINT_SEPARATOR, JSON.stringify(model.diagnostics ?? null)]);
|
|
138
195
|
}
|
|
139
196
|
/**
|
|
140
197
|
* `logAfterMs` threshold passed to {@link Tracer.time} around
|
|
@@ -155,12 +212,13 @@ export interface DataServerOptions extends LogNameOptions {
|
|
|
155
212
|
* (`DataClientProtocol.onDocumentUpdated`).
|
|
156
213
|
*
|
|
157
214
|
* **Subscription dispatch only** — distinct from the synchronous RPC
|
|
158
|
-
* response phase.
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
* controls only the async notification
|
|
215
|
+
* response phase. A read (`getModelDocument`) settles at the
|
|
216
|
+
* integrity-settled landmark (`IntegrityService.SettledState`), or at
|
|
217
|
+
* `Validated` when {@link GetModelDocumentArgs.includeDiagnostics} is set.
|
|
218
|
+
* A write (`updateModelDocument` / `updateModelDocuments` /
|
|
219
|
+
* `saveModelDocument`) answers once its document is validated, as a
|
|
220
|
+
* session's write does. This option controls only the async notification
|
|
221
|
+
* phase.
|
|
164
222
|
*
|
|
165
223
|
* Default: `DocumentState.Validated` — validation is the last builder
|
|
166
224
|
* phase and produces the full diagnostic set, so subscription events fired
|
|
@@ -201,6 +259,22 @@ export interface DataServerOptions extends LogNameOptions {
|
|
|
201
259
|
*/
|
|
202
260
|
readonly fingerprintStrategy?: FingerprintStrategy;
|
|
203
261
|
|
|
262
|
+
/**
|
|
263
|
+
* How long a reference question waits for the build to reach `Linked` before
|
|
264
|
+
* failing with {@link REFERENCE_SETTLE_TIMEOUT_CODE}. Bounded because the
|
|
265
|
+
* wait resolves off a build-phase notification, so a build cancelled below
|
|
266
|
+
* `Linked` with nothing after it leaves the request unanswered for the life
|
|
267
|
+
* of the server.
|
|
268
|
+
*
|
|
269
|
+
* Expiry rejects rather than reading the index anyway: the unsettled index
|
|
270
|
+
* is what the wait keeps the caller away from, so falling through to it on a
|
|
271
|
+
* slow build reinstates the fault — and on a name proposal persists it.
|
|
272
|
+
*
|
|
273
|
+
* Default {@link DataServer.DEFAULT_OPTIONS}, sized for a cold workspace
|
|
274
|
+
* still reaching `Linked` for the first time.
|
|
275
|
+
*/
|
|
276
|
+
readonly referenceSettleTimeoutMs?: number;
|
|
277
|
+
|
|
204
278
|
/**
|
|
205
279
|
* Wire-method namespace for both inbound request handlers and the
|
|
206
280
|
* outbound notification client proxy. Defaults to
|
|
@@ -258,15 +332,20 @@ export interface DataServerOptions extends LogNameOptions {
|
|
|
258
332
|
* When supplied, every inbound data-server RPC is timed into this collector
|
|
259
333
|
* (via the `createRpcProxy` binding) and exposed through
|
|
260
334
|
* {@link DataServerDiagnosticsProtocol.getLatency}. A head that also runs an
|
|
261
|
-
* LSP connection can pass the SAME collector to
|
|
262
|
-
*
|
|
263
|
-
*
|
|
335
|
+
* LSP connection can pass the SAME collector through `lspLatencyOptions` to
|
|
336
|
+
* that connection, so one report covers both heads. Absent by default (no
|
|
337
|
+
* timing overhead).
|
|
264
338
|
*/
|
|
265
339
|
readonly latency?: LatencyCollector;
|
|
266
340
|
}
|
|
267
341
|
|
|
268
|
-
/**
|
|
269
|
-
|
|
342
|
+
/**
|
|
343
|
+
* Fully-resolved variant of {@link DataServerOptions} — every field set, after
|
|
344
|
+
* merging defaults. Returned by the `protected`
|
|
345
|
+
* {@link DataServer.resolveOptions} and held on {@link DataServer.options}, so
|
|
346
|
+
* an adopter changing how defaults resolve has to name it.
|
|
347
|
+
*/
|
|
348
|
+
export interface ResolvedDataServerOptions {
|
|
270
349
|
readonly subscriptionPhase: DocumentState;
|
|
271
350
|
readonly fingerprintStrategy: FingerprintStrategy;
|
|
272
351
|
readonly methodNamespace: string;
|
|
@@ -274,6 +353,41 @@ interface ResolvedDataServerOptions {
|
|
|
274
353
|
readonly excludedMethods: readonly string[];
|
|
275
354
|
/** Always resolved — to the caller's, or to the platform default. */
|
|
276
355
|
readonly diagnostics: DataServerDiagnosticsProvider;
|
|
356
|
+
readonly referenceSettleTimeoutMs: number;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* What a {@link DataServer} keeps per canonical URI, in the `protected`
|
|
361
|
+
* {@link DataServer.uriWatchRecords}. A record lives while it has a watcher
|
|
362
|
+
* or a release mark; {@link DataServer.pruneUriWatchRecord} enforces that on
|
|
363
|
+
* every path that removes either, so a record with no watcher has a release
|
|
364
|
+
* broadcast pending. Test the size of {@link DataServerUriWatchRecord.watchers}, not the
|
|
365
|
+
* record's presence, for "watched".
|
|
366
|
+
*/
|
|
367
|
+
export interface DataServerUriWatchRecord {
|
|
368
|
+
/** The client ids watching the URI; save, dirty and phase events go out only while it is non-empty, a pending release mark aside. */
|
|
369
|
+
readonly watchers: Set<string>;
|
|
370
|
+
/** Digest of the last emitted state, or of the state a first watch found; see {@link DataServer.dispatchPhaseEvent} for why it de-duplicates, {@link DataServer.computeDocumentFingerprint} for its inputs. */
|
|
371
|
+
fingerprint?: string;
|
|
372
|
+
/**
|
|
373
|
+
* The version {@link DataServerUriWatchRecord.fingerprint} was taken at; an
|
|
374
|
+
* event goes out when either moves. Not {@link DataServerUriWatchRecord.sentVersion}:
|
|
375
|
+
* a first watch sets this and sends nothing, so the next event at its
|
|
376
|
+
* version still carries the manager's attribution.
|
|
377
|
+
*/
|
|
378
|
+
fingerprintVersion?: ModelVersion;
|
|
379
|
+
/** Set by {@link DataServer.subscribeToModelReleases} once a release happened: its broadcast, the next phase event, goes out even without a watcher. */
|
|
380
|
+
releaseBroadcastPending?: boolean;
|
|
381
|
+
/**
|
|
382
|
+
* The version of an open document the last event sent was built at. The
|
|
383
|
+
* version sent again goes out as `'rebuilt'` from no client: the manager
|
|
384
|
+
* counts a version delivered only once its `Validated` listeners ran, so a
|
|
385
|
+
* build cancelled after the subscription phase would send it as a change
|
|
386
|
+
* twice. Only while the store has the document open: without it, a
|
|
387
|
+
* document can keep one version through changes of its file, so its events
|
|
388
|
+
* keep the manager's attribution.
|
|
389
|
+
*/
|
|
390
|
+
sentVersion?: number;
|
|
277
391
|
}
|
|
278
392
|
|
|
279
393
|
/**
|
|
@@ -297,13 +411,19 @@ interface ResolvedDataServerOptions {
|
|
|
297
411
|
* adopters subclass ONLY when they need to decorate wire returns or
|
|
298
412
|
* notifications. The usual adoption path is DI rebinds alone.
|
|
299
413
|
*
|
|
414
|
+
* **`TTransfer` is not checked.** It declares the union of the transfer roots
|
|
415
|
+
* the workspace's languages produce. Which root a document has is known only
|
|
416
|
+
* at runtime, from its language, so a `TTransfer` missing a language compiles.
|
|
417
|
+
*
|
|
300
418
|
* Lifecycle: the constructor registers framework request handlers (plus
|
|
301
419
|
* any names supplied via {@link DataServerOptions.additionalMethods}) and
|
|
302
420
|
* builds the {@link DataClientProtocol} notification proxy on the same
|
|
303
421
|
* connection in one {@link createRpcProxy} call (binding `this` as its
|
|
304
|
-
* `localTarget`) under the configured namespace, and
|
|
305
|
-
* `
|
|
306
|
-
* dispatches
|
|
422
|
+
* `localTarget`) under the configured namespace, and follows documents
|
|
423
|
+
* through `ModelService`'s events: at the configured phase, `onModelUpdated`
|
|
424
|
+
* dispatches per-document `clientProxy.onDocumentUpdated` for watched URIs and
|
|
425
|
+
* `onModelsBuilt` a single `clientProxy.onDocumentsBuilt` naming the batch's
|
|
426
|
+
* unwatched documents.
|
|
307
427
|
*
|
|
308
428
|
* The data-server requires the full {@link ServerSharedServices}
|
|
309
429
|
* shape — `HydraniumTextDocuments` for client-attributed updates,
|
|
@@ -331,87 +451,33 @@ export class DataServer<
|
|
|
331
451
|
* Framework defaults, exposed so an adopter can spread them when
|
|
332
452
|
* extending rather than restate a value the framework may change.
|
|
333
453
|
*/
|
|
334
|
-
static readonly DEFAULT_OPTIONS: Required<Pick<DataServerOptions, 'subscriptionPhase'>> = {
|
|
335
|
-
subscriptionPhase: DocumentState.Validated
|
|
454
|
+
static readonly DEFAULT_OPTIONS: Required<Pick<DataServerOptions, 'subscriptionPhase' | 'referenceSettleTimeoutMs'>> = {
|
|
455
|
+
subscriptionPhase: DocumentState.Validated,
|
|
456
|
+
referenceSettleTimeoutMs: 60_000
|
|
336
457
|
};
|
|
337
458
|
|
|
338
459
|
protected readonly options: ResolvedDataServerOptions;
|
|
339
|
-
/**
|
|
340
|
-
protected readonly
|
|
341
|
-
/**
|
|
342
|
-
* Open-document bookkeeping: URI → set of clientIds that opened it over THIS
|
|
343
|
-
* connection.
|
|
344
|
-
*
|
|
345
|
-
* Kept because the document store releases a client's hold only from an
|
|
346
|
-
* explicit close, and a client that dies without closing would otherwise keep
|
|
347
|
-
* the document open forever — resident, and with its last-close revert
|
|
348
|
-
* suppressed. The store has no notion of which connection a clientId reached
|
|
349
|
-
* it over, so the connection-scoped set has to live here; teaching it a client
|
|
350
|
-
* identity is the more robust and much larger alternative.
|
|
351
|
-
*/
|
|
352
|
-
protected readonly openedDocuments = new Map<string, Set<string>>();
|
|
353
|
-
/** Typed client proxy — sends `data-server/on*` notifications back over the same wire. */
|
|
354
|
-
protected readonly clientProxy: DataClientProtocol<TTransfer, TDiagnostic, TProject>;
|
|
355
|
-
protected readonly disposables = new DisposableCollection();
|
|
460
|
+
/** This connection's {@link DataServerUriWatchRecord} per canonical URI; see there for when a record lives. */
|
|
461
|
+
protected readonly uriWatchRecords = new Map<string, DataServerUriWatchRecord>();
|
|
356
462
|
/**
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
* `'deleted'`, otherwise `'rebuilt'` (cascade rebuild from a dependent
|
|
361
|
-
* URI's change). The same mechanism `AstDocumentManager.onUpdate` uses on
|
|
362
|
-
* the LSP side, so reason fidelity stays consistent between heads.
|
|
363
|
-
*/
|
|
364
|
-
protected lastBuildUpdate?: { changed: readonly URI[]; deleted: readonly URI[] };
|
|
365
|
-
/**
|
|
366
|
-
* Per-URI hash of the last emitted (text + diagnostics) state,
|
|
367
|
-
* used by {@link dispatchPhaseEvent} to suppress duplicate
|
|
368
|
-
* `onDocumentUpdated` notifications for rebuilds that produce no
|
|
369
|
-
* observable change since the last emit.
|
|
370
|
-
*
|
|
371
|
-
* Two scenarios routinely produce such rebuilds:
|
|
372
|
-
* 1. **Refresh-triggered rebuilds.** When a second client attaches to a
|
|
373
|
-
* document already open in another client,
|
|
374
|
-
* `HydraniumTextDocuments.refreshContent` fires `onDidChangeContent`
|
|
375
|
-
* purely to re-trigger the build pipeline — the text and diagnostics
|
|
376
|
-
* are identical to the prior emit.
|
|
377
|
-
* 2. **Cascade rebuilds with no diagnostic delta.** Langium rebuilds a
|
|
378
|
-
* document when a dependency changes; if the rebuild produces the
|
|
379
|
-
* same diagnostics, there is nothing new to communicate to subscribers.
|
|
380
|
-
*
|
|
381
|
-
* Without this filter, both scenarios reach subscribers as wire-side
|
|
382
|
-
* `'changed'` events. A subscriber that interprets `'changed'` as
|
|
383
|
-
* "another client wrote new content" and resets its in-memory root to
|
|
384
|
-
* the server view then misreads the spurious event as a concurrent
|
|
385
|
-
* third-party write and loses the user's edits.
|
|
386
|
-
*
|
|
387
|
-
* Initialised on {@link watchModelDocument} to the current
|
|
388
|
-
* document fingerprint so the FIRST phase event after a fresh
|
|
389
|
-
* subscription is also de-duplicated against the state subscribers
|
|
390
|
-
* obtained via `getModelDocument` — they do not need a redundant phase
|
|
391
|
-
* notification for the state they just fetched.
|
|
392
|
-
*
|
|
393
|
-
* Cleared in {@link unwatchModelDocument} when the last
|
|
394
|
-
* subscriber for a URI leaves so memory does not accumulate.
|
|
463
|
+
* The client sessions this connection registered, by client id. A document
|
|
464
|
+
* request carrying one of these ids acts as that session; one carrying any
|
|
465
|
+
* other id fails.
|
|
395
466
|
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
*
|
|
467
|
+
* Entries stay after {@link dispose} has ended their sessions, so a request
|
|
468
|
+
* still running from before the connection closed reaches the ended handle
|
|
469
|
+
* and fails with the session's own error.
|
|
399
470
|
*/
|
|
400
|
-
protected readonly
|
|
471
|
+
protected readonly clientSessions = new Map<string, ClientSession<AstNode, AstDiagnostic, TTransfer>>();
|
|
401
472
|
/**
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
*
|
|
406
|
-
*
|
|
407
|
-
* discarded state forever. Marked on the last-close transition (see
|
|
408
|
-
* {@link subscribeToTextDocumentCloses}) and consumed by
|
|
409
|
-
* {@link dispatchPhaseEvent}, which broadcasts the following rebuild's
|
|
410
|
-
* phase event even without a subscription — de-duplicated against
|
|
411
|
-
* {@link lastEmittedFingerprint} where an entry survives, so a close
|
|
412
|
-
* whose disk state equals the last emitted state stays silent.
|
|
473
|
+
* Typed client proxy — sends `data-server/on*` notifications back over the same wire.
|
|
474
|
+
* The proxy sends any `on[A-Z]*` name other than `onDidOpenConnection` and
|
|
475
|
+
* `onDidCloseConnection`, for which it returns its own events, so a subclass
|
|
476
|
+
* with notifications of its own narrows only the type:
|
|
477
|
+
* `declare protected readonly clientProxy: TheirClient`.
|
|
413
478
|
*/
|
|
414
|
-
protected readonly
|
|
479
|
+
protected readonly clientProxy: DataClientProtocol<TTransfer, TDiagnostic, TProject>;
|
|
480
|
+
protected readonly disposables = new DisposableCollection();
|
|
415
481
|
protected readonly tracer: Tracer;
|
|
416
482
|
/** The in-flight interactive profile capture, held between {@link startProfiling} and {@link stopProfiling}. */
|
|
417
483
|
protected activeProfile?: DataServerProfileCapture;
|
|
@@ -420,27 +486,23 @@ export class DataServer<
|
|
|
420
486
|
/** Per-method RPC latency collector, when the head opted in via {@link DataServerOptions.latency}. */
|
|
421
487
|
protected readonly latency?: LatencyCollector;
|
|
422
488
|
/** Encoder pulled from DI. Adopters rebind `services.model.TransferEncoder` to a typed-overlay subclass. */
|
|
423
|
-
protected readonly encoder: TransferEncoder<
|
|
489
|
+
protected readonly encoder: TransferEncoder<TDiagnostic>;
|
|
424
490
|
/**
|
|
425
491
|
* In-process workspace facade — the lifecycle delegate `get` / `update` / `save` go through.
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
* while the wire shape stays `TDiagnostic extends TransferDiagnostic`. The encoder's
|
|
429
|
-
* `astDocumentToTransferDocument` accepts both shapes (wire-shape or LSP-shape) and projects
|
|
430
|
-
* to wire shape on the return — see `TransferEncoder.astDocumentToTransferDocument`.
|
|
492
|
+
* Its documents carry the AST-layer {@link AstDiagnostic}, never the wire `TDiagnostic`:
|
|
493
|
+
* {@link encoder} converts one into the other on the way out.
|
|
431
494
|
*/
|
|
432
|
-
protected readonly modelService: ModelService<AstNode,
|
|
495
|
+
protected readonly modelService: ModelService<AstNode, AstDiagnostic, TTransfer>;
|
|
433
496
|
|
|
434
497
|
constructor(
|
|
435
498
|
protected readonly connection: MessageConnection,
|
|
436
|
-
protected readonly services: ServerSharedServices<TProject>,
|
|
499
|
+
protected readonly services: ServerSharedServices<TProject, TDiagnostic>,
|
|
437
500
|
options: DataServerOptions = {}
|
|
438
501
|
) {
|
|
439
502
|
this.options = this.resolveOptions(options);
|
|
440
503
|
this.tracer = this.services.Tracer.for(options.logName ?? 'DataServer').trace('instantiated');
|
|
441
504
|
// DI-bound: adopters rebind `services.model.TransferEncoder` /
|
|
442
|
-
// `services.model.ModelService` with their own subclasses.
|
|
443
|
-
// framework upper bounds; the casts below narrow to this instance's generics.
|
|
505
|
+
// `services.model.ModelService` with their own subclasses.
|
|
444
506
|
const { TransferEncoder: encoder, ModelService: modelService } = this.services.model;
|
|
445
507
|
if (!encoder || !modelService) {
|
|
446
508
|
throw new Error(
|
|
@@ -448,8 +510,8 @@ export class DataServer<
|
|
|
448
510
|
'Did you compose `createServerSharedModule(ctx)` into your shared module?'
|
|
449
511
|
);
|
|
450
512
|
}
|
|
451
|
-
this.encoder = encoder
|
|
452
|
-
this.modelService = modelService
|
|
513
|
+
this.encoder = encoder;
|
|
514
|
+
this.modelService = modelService;
|
|
453
515
|
const excluded = new Set<string>(this.options.excludedMethods);
|
|
454
516
|
const registeredMethods = [
|
|
455
517
|
...DATA_SERVER_PROTOCOL_METHODS,
|
|
@@ -465,38 +527,43 @@ export class DataServer<
|
|
|
465
527
|
methodNamespace: this.options.methodNamespace,
|
|
466
528
|
localTarget: this,
|
|
467
529
|
localMethods: registeredMethods as readonly (keyof this & string)[],
|
|
468
|
-
latency: this.latency
|
|
530
|
+
latency: this.latency,
|
|
531
|
+
// Bound at the chokepoint rather than per handler, so an adopter's
|
|
532
|
+
// `additionalMethods` are rendered on the same terms as the
|
|
533
|
+
// framework's own rejections.
|
|
534
|
+
renderErrorMessage: error => this.services.MessageRenderer.renderError(error)
|
|
469
535
|
});
|
|
470
|
-
this.disposables.push(
|
|
471
|
-
|
|
472
|
-
this.lastBuildUpdate = { changed, deleted };
|
|
473
|
-
})
|
|
474
|
-
);
|
|
475
|
-
this.subscribeToDocumentBuilder();
|
|
536
|
+
this.disposables.push(this.modelService.onModelDeleted(event => this.dispatchDeleteEvent(event)));
|
|
537
|
+
this.subscribeToModelUpdates();
|
|
476
538
|
this.subscribeToTextDocumentSaves();
|
|
477
|
-
this.
|
|
539
|
+
this.subscribeToDirtyChanges();
|
|
540
|
+
this.subscribeToModelReleases();
|
|
478
541
|
this.subscribeToProjectManager();
|
|
479
542
|
// Self-register teardown so an adopter that keeps no reference to the
|
|
480
543
|
// server still releases per-connection state, with no lifecycle hook of
|
|
481
|
-
// its own.
|
|
482
|
-
|
|
544
|
+
// its own. The client did not close anything itself, so its sessions end
|
|
545
|
+
// as lost and each document it was the last to have open waits out the
|
|
546
|
+
// store's release grace. See `dispose`.
|
|
547
|
+
this.disposables.push(connection.onClose(() => this.dispose('lost')));
|
|
483
548
|
}
|
|
484
549
|
|
|
485
550
|
/**
|
|
486
551
|
* Release everything the constructor wired up — the bound protocol
|
|
487
|
-
* handlers and every listener — and clear
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
* this server's and so outlives the connection unless released here.
|
|
552
|
+
* handlers and every listener — and clear {@link uriWatchRecords}, so a
|
|
553
|
+
* long-lived shared services bundle does not retain per-connection memory
|
|
554
|
+
* after the connection closes. Also ends every session this connection registered, which closes
|
|
555
|
+
* every document it has open: the SHARED store's state rather than this
|
|
556
|
+
* server's, which outlives the connection unless released here.
|
|
493
557
|
*
|
|
494
|
-
* Idempotent: subsequent calls are no-ops.
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
*
|
|
558
|
+
* Idempotent: subsequent calls are no-ops. Runs by itself when the
|
|
559
|
+
* connection closes, so adopters who don't hold a reference still get
|
|
560
|
+
* per-connection cleanup; adopters that DO hold a reference may call
|
|
561
|
+
* `dispose()` directly for early teardown. The sessions end with `cause`:
|
|
562
|
+
* `'closed'` releases every document whose last open they close at once,
|
|
563
|
+
* and `'lost'`, the connection's own close, lets each wait out the release
|
|
564
|
+
* grace.
|
|
498
565
|
*/
|
|
499
|
-
dispose(): void {
|
|
566
|
+
dispose(cause: SessionEndCause = 'closed'): void {
|
|
500
567
|
this.disposed = true;
|
|
501
568
|
// Release an in-flight interactive capture so the process-wide inspector
|
|
502
569
|
// singleton is not left active after the connection closes.
|
|
@@ -505,104 +572,142 @@ export class DataServer<
|
|
|
505
572
|
this.activeProfile = undefined;
|
|
506
573
|
void capture.stop({}).catch(() => undefined);
|
|
507
574
|
}
|
|
508
|
-
// AFTER `disposables.dispose()`, deliberately:
|
|
509
|
-
//
|
|
510
|
-
//
|
|
575
|
+
// AFTER `disposables.dispose()`, deliberately: ending a session releases
|
|
576
|
+
// each document it was the last to hold, and this server's own
|
|
577
|
+
// `onModelReleased` subscription would otherwise mark a release broadcast
|
|
578
|
+
// for a connection that is already gone.
|
|
511
579
|
this.disposables.dispose();
|
|
512
|
-
this.
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
this.
|
|
580
|
+
for (const [clientId, session] of this.clientSessions) {
|
|
581
|
+
this.endSession(clientId, session, cause);
|
|
582
|
+
}
|
|
583
|
+
this.uriWatchRecords.clear();
|
|
516
584
|
}
|
|
517
585
|
|
|
518
586
|
// ============================================================
|
|
519
|
-
// DataServerProtocol implementation
|
|
587
|
+
// DataServerProtocol implementation
|
|
520
588
|
// ============================================================
|
|
521
589
|
//
|
|
522
|
-
//
|
|
523
|
-
//
|
|
524
|
-
// the wire boundary via
|
|
525
|
-
//
|
|
526
|
-
//
|
|
527
|
-
//
|
|
528
|
-
|
|
529
|
-
async
|
|
530
|
-
//
|
|
531
|
-
//
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
await this.modelService.open(args);
|
|
535
|
-
// Record the hold so `dispose` can release it for a client that never
|
|
536
|
-
// closes. Keyed by the URI as given, because that is what `close` takes.
|
|
537
|
-
let holders = this.openedDocuments.get(args.uri);
|
|
538
|
-
if (!holders) {
|
|
539
|
-
holders = new Set<string>();
|
|
540
|
-
this.openedDocuments.set(args.uri, holders);
|
|
590
|
+
// Reads go to `ModelService`; a request that opens or writes a document
|
|
591
|
+
// goes to the session this connection registered under its `clientId`.
|
|
592
|
+
// Each answer is encoded on the wire boundary via
|
|
593
|
+
// `encoder.astDocumentToTransferDocument`. To change how documents are
|
|
594
|
+
// read, override on the ModelService subclass; to change how they are
|
|
595
|
+
// written, on the session class the bound `ClientSessionFactory` builds.
|
|
596
|
+
|
|
597
|
+
async createSession(args: CreateSessionArgs): Promise<void> {
|
|
598
|
+
// After teardown nothing would ever end a session registered now, and its
|
|
599
|
+
// id would stay taken for the life of the process.
|
|
600
|
+
if (this.disposed) {
|
|
601
|
+
throw new SessionClosedError(args.clientId);
|
|
541
602
|
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
//
|
|
545
|
-
//
|
|
546
|
-
|
|
547
|
-
// multi-client synced version whenever the open seeded the synced document
|
|
548
|
-
// with a caller-supplied version id — and the synced version is what
|
|
549
|
-
// `ModelService.update`'s optimistic-concurrency gate compares
|
|
550
|
-
// `baseVersion` against, so reporting the snapshot's would make the
|
|
551
|
-
// caller's first tagged write self-conflict. A genuine concurrent edit
|
|
552
|
-
// still trips the gate and is reconciled by the caller's replay rather
|
|
553
|
-
// than predicted here.
|
|
554
|
-
return { ...document, version: this.services.workspace.TextDocuments.version(args.uri) };
|
|
603
|
+
const session = this.modelService.createSession(args.label, args.clientId, { resumeToken: args.resumeToken });
|
|
604
|
+
this.clientSessions.set(args.clientId, session);
|
|
605
|
+
// A client registering again on another connection ends this session
|
|
606
|
+
// there, and its watches here would otherwise outlive it.
|
|
607
|
+
session.onDidDispose(() => this.dropWatches(args.clientId));
|
|
555
608
|
}
|
|
556
609
|
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
610
|
+
/**
|
|
611
|
+
* End a session this connection registered. The ended handle stays in
|
|
612
|
+
* {@link clientSessions}, as it does after {@link dispose}, so a request for
|
|
613
|
+
* the id still arriving fails with the ended session's own error.
|
|
614
|
+
*/
|
|
615
|
+
async closeSession(args: CloseSessionArgs): Promise<void> {
|
|
616
|
+
const session = this.clientSessions.get(args.clientId);
|
|
617
|
+
if (session) {
|
|
618
|
+
this.endSession(args.clientId, session);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/** End `session`, registered here under `clientId`, with `cause`, and drop its watches on this connection. */
|
|
623
|
+
protected endSession(
|
|
624
|
+
clientId: string,
|
|
625
|
+
session: ClientSession<AstNode, AstDiagnostic, TTransfer>,
|
|
626
|
+
cause: SessionEndCause = 'closed'
|
|
627
|
+
): void {
|
|
628
|
+
this.dropWatches(clientId);
|
|
629
|
+
session.dispose(cause);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
protected dropWatches(clientId: string): void {
|
|
633
|
+
for (const [uri, record] of this.uriWatchRecords) {
|
|
634
|
+
if (record.watchers.delete(clientId)) {
|
|
635
|
+
this.pruneUriWatchRecord(uri);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
async createModelDocument(args: CreateModelDocumentArgs): Promise<TransferDocument<TTransfer, TDiagnostic>> {
|
|
641
|
+
const session = this.requireSession(args.clientId);
|
|
642
|
+
await session.create(args.uri, args.text);
|
|
643
|
+
return this.openedSnapshot(args.uri, () => session.close(args.uri));
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
async updateModelDocuments(args: TransferUpdateDocumentsArgs<TTransfer>): Promise<TransferDocument<TTransfer, TDiagnostic>[]> {
|
|
647
|
+
const astDocuments = await this.requireSession(args.clientId).updateAll({
|
|
648
|
+
updates: args.updates.map(update => this.toSessionWrite(update))
|
|
649
|
+
});
|
|
650
|
+
return astDocuments.map(astDocument => this.encodeDocument(astDocument));
|
|
564
651
|
}
|
|
565
652
|
|
|
566
653
|
/**
|
|
567
|
-
*
|
|
568
|
-
*
|
|
654
|
+
* The session write a wire write request carries. Only the fields a session
|
|
655
|
+
* takes: a field an adopter adds to a request reaches the session only
|
|
656
|
+
* through an override of this.
|
|
569
657
|
*/
|
|
570
|
-
protected
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
658
|
+
protected toSessionWrite(request: Omit<TransferUpdateDocumentArgs<TTransfer>, 'clientId'>): ClientSessionWriteArgs<TTransfer> {
|
|
659
|
+
return { uri: request.uri, model: request.model, baseVersion: request.baseVersion };
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* The session this connection registered under `clientId`, which every
|
|
664
|
+
* document request acts as. An id that is not one fails with the
|
|
665
|
+
* closed-session code: opening or writing under it would leave an open that
|
|
666
|
+
* no session end ever closes.
|
|
667
|
+
*/
|
|
668
|
+
protected requireSession(clientId: string): ClientSession<AstNode, AstDiagnostic, TTransfer> {
|
|
669
|
+
const session = this.clientSessions.get(clientId);
|
|
670
|
+
if (!session) {
|
|
671
|
+
throw new SessionClosedError(clientId, 'The client session was never registered on this connection.');
|
|
578
672
|
}
|
|
673
|
+
return session;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
async openModelDocument(args: Pick<OpenModelArgs, 'uri' | 'clientId' | 'options'>): Promise<TransferDocument<TTransfer, TDiagnostic>> {
|
|
677
|
+
const session = this.requireSession(args.clientId);
|
|
678
|
+
const wasOpen = this.services.workspace.TextDocuments.isOpenInClient(args.uri, args.clientId);
|
|
679
|
+
await session.open(args.uri, args.options);
|
|
680
|
+
// A repeat open keeps the earlier one, which is still in use.
|
|
681
|
+
return this.openedSnapshot(args.uri, wasOpen ? undefined : () => session.close(args.uri));
|
|
579
682
|
}
|
|
580
683
|
|
|
581
684
|
/**
|
|
582
|
-
*
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
* without this a client that dies mid-session keeps its documents open for the
|
|
587
|
-
* lifetime of the process: `isOpenInAnyClient` stays true, the document stays
|
|
588
|
-
* resident, and the last-close revert never runs. A long-lived multi-client
|
|
589
|
-
* head is the configuration where a dead client is normal rather than
|
|
590
|
-
* exceptional, so the leak accumulates there.
|
|
591
|
-
*
|
|
592
|
-
* Runs from {@link dispose}, which is synchronous, so each close is fired and
|
|
593
|
-
* its failure swallowed — a teardown must not reject, and a URI whose close
|
|
594
|
-
* fails is no worse off than it was before this drain existed.
|
|
685
|
+
* The snapshot a session's open or create answers with. When the read
|
|
686
|
+
* fails, `rollback` undoes the open this call made before the failure is
|
|
687
|
+
* rethrown: the caller sees no open, so nothing on its side would ever close
|
|
688
|
+
* it.
|
|
595
689
|
*/
|
|
596
|
-
protected
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
690
|
+
protected async openedSnapshot(uri: string, rollback?: () => Promise<void>): Promise<TransferDocument<TTransfer, TDiagnostic>> {
|
|
691
|
+
try {
|
|
692
|
+
return await this.getModelDocument({ uri });
|
|
693
|
+
} catch (error: unknown) {
|
|
694
|
+
// The open's failure is the one the caller needs, not the cleanup's.
|
|
695
|
+
await rollback?.().catch(() => undefined);
|
|
696
|
+
throw error;
|
|
603
697
|
}
|
|
604
698
|
}
|
|
605
699
|
|
|
700
|
+
async closeModelDocument(args: CloseModelArgs): Promise<void> {
|
|
701
|
+
// Through the handle, so a close under an ended session fails as that
|
|
702
|
+
// session's calls do, and a session class's own close is honoured.
|
|
703
|
+
const session = this.requireSession(args.clientId);
|
|
704
|
+
// Closing also releases the watch for (uri, clientId) — a forgotten
|
|
705
|
+
// unwatch would otherwise leak phase-event dispatch until the connection
|
|
706
|
+
// closes. Idempotent: a close without a prior watch is a no-op.
|
|
707
|
+
await this.unwatchModelDocument({ uri: args.uri, clientId: args.clientId });
|
|
708
|
+
await session.close(args.uri);
|
|
709
|
+
}
|
|
710
|
+
|
|
606
711
|
async getModelDocument(args: GetModelDocumentArgs): Promise<TransferDocument<TTransfer, TDiagnostic>> {
|
|
607
712
|
// Smart dispatch: a warm document (already in LangiumDocuments) is
|
|
608
713
|
// returned at its settle phase without a redundant build; a cold URI
|
|
@@ -615,18 +720,32 @@ export class DataServer<
|
|
|
615
720
|
// unsubscribed caller that needs diagnostics inline passes
|
|
616
721
|
// `includeDiagnostics: true` to settle at `Validated` instead.
|
|
617
722
|
const state = args.includeDiagnostics ? DocumentState.Validated : undefined;
|
|
618
|
-
|
|
619
|
-
|
|
723
|
+
try {
|
|
724
|
+
return this.encodeDocument(await this.modelService.ensureDocumentState(args.uri, state));
|
|
725
|
+
} catch (error: unknown) {
|
|
726
|
+
// The protocol answers a read of a URI with no document with an
|
|
727
|
+
// envelope that has no model.
|
|
728
|
+
if (this.isMissingDocument(error, args.uri)) {
|
|
729
|
+
return this.envelope(UriUtils.toUri(args.uri));
|
|
730
|
+
}
|
|
731
|
+
throw error;
|
|
732
|
+
}
|
|
620
733
|
}
|
|
621
734
|
|
|
622
735
|
async updateModelDocument(args: TransferUpdateDocumentArgs<TTransfer>): Promise<TransferDocument<TTransfer, TDiagnostic>> {
|
|
623
|
-
const astDocument = await this.
|
|
624
|
-
return this.
|
|
736
|
+
const astDocument = await this.requireSession(args.clientId).update(this.toSessionWrite(args));
|
|
737
|
+
return this.encodeDocument(astDocument);
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
async saveModelDocument(args: TransferSaveDocumentArgs<TTransfer>): Promise<TransferSavedDocument<TTransfer, TDiagnostic>> {
|
|
741
|
+
const astDocument = await this.requireSession(args.clientId).save(this.toSessionWrite(args));
|
|
742
|
+
return { ...this.encodeDocument(astDocument), persisted: astDocument.persisted };
|
|
625
743
|
}
|
|
626
744
|
|
|
627
|
-
async
|
|
628
|
-
const
|
|
629
|
-
|
|
745
|
+
async persistModelDocument(args: TransferPersistDocumentArgs): Promise<TransferSavedDocument<TTransfer, TDiagnostic>> {
|
|
746
|
+
const version = await this.requireSession(args.clientId).persist({ uri: args.uri, baseVersion: args.baseVersion });
|
|
747
|
+
const astDocument = await this.modelService.ensureDocumentState(args.uri, this.services.workspace.DocumentBuilder.finalBuildState());
|
|
748
|
+
return { ...this.encodeDocument(astDocument), persisted: { version } };
|
|
630
749
|
}
|
|
631
750
|
|
|
632
751
|
/**
|
|
@@ -634,28 +753,25 @@ export class DataServer<
|
|
|
634
753
|
* fan out to the wired `clientProxy.onDocumentUpdated`. The
|
|
635
754
|
* bidirectional pattern means the event channel is the client
|
|
636
755
|
* notification surface, not a returned handle — this method only
|
|
637
|
-
* registers the
|
|
756
|
+
* registers the watcher in {@link uriWatchRecords}.
|
|
638
757
|
*
|
|
639
|
-
* Also baselines the
|
|
640
|
-
* {@link
|
|
641
|
-
*
|
|
642
|
-
*
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
* redundant phase notification for that same state.
|
|
758
|
+
* Also baselines the URI's emission fingerprint (see
|
|
759
|
+
* {@link dispatchPhaseEvent}) to the document's current state when the
|
|
760
|
+
* first watcher for the URI registers. This guarantees that any phase event firing immediately after
|
|
761
|
+
* the watch with no observable change is suppressed — watchers obtain
|
|
762
|
+
* initial state via `getModelDocument` (or {@link openModelDocument}) and
|
|
763
|
+
* do not need a redundant phase notification for that same state.
|
|
646
764
|
*/
|
|
647
765
|
async watchModelDocument(args: WatchModelDocumentArgs): Promise<void> {
|
|
648
766
|
const uri = this.canonicalKey(args.uri);
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
if (document) {
|
|
658
|
-
this.lastEmittedFingerprint.set(uri, this.computeDocumentFingerprint(document));
|
|
767
|
+
const record = this.uriWatchRecords.get(uri) ?? { watchers: new Set<string>() };
|
|
768
|
+
this.uriWatchRecords.set(uri, record);
|
|
769
|
+
record.watchers.add(args.clientId);
|
|
770
|
+
if (record.fingerprint === undefined) {
|
|
771
|
+
const document = this.modelService.getDocument(args.uri);
|
|
772
|
+
if (document && !this.services.workspace.ModelLedger.isPlaceholder(document.parseResult.value)) {
|
|
773
|
+
record.fingerprint = this.computeDocumentFingerprint(document.parseResult.value, this.encoder.toTransferDocument(document));
|
|
774
|
+
record.fingerprintVersion = this.services.workspace.ModelLedger.versionOf(document.parseResult.value);
|
|
659
775
|
}
|
|
660
776
|
}
|
|
661
777
|
}
|
|
@@ -663,29 +779,41 @@ export class DataServer<
|
|
|
663
779
|
/**
|
|
664
780
|
* Remove a watch for `(uri, clientId)` previously created by
|
|
665
781
|
* {@link watchModelDocument}. Idempotent — unwatching twice is a no-op.
|
|
666
|
-
* Dispatch for `uri` stops once no watchers remain,
|
|
667
|
-
*
|
|
668
|
-
* re-baselines against the then-current document state rather than a
|
|
669
|
-
* stale snapshot from the previous watch.
|
|
782
|
+
* Dispatch for `uri` stops once no watchers remain, and the emission
|
|
783
|
+
* fingerprint goes with the last watcher (see {@link pruneUriWatchRecord}).
|
|
670
784
|
*/
|
|
671
785
|
async unwatchModelDocument(args: WatchModelDocumentArgs): Promise<void> {
|
|
672
786
|
const uri = this.canonicalKey(args.uri);
|
|
673
|
-
|
|
674
|
-
|
|
787
|
+
if (this.uriWatchRecords.get(uri)?.watchers.delete(args.clientId)) {
|
|
788
|
+
this.pruneUriWatchRecord(uri);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* Drop what `uri` no longer needs: the
|
|
794
|
+
* {@link DataServerUriWatchRecord.fingerprint} once no client watches it,
|
|
795
|
+
* and the whole record once it holds no
|
|
796
|
+
* {@link DataServerUriWatchRecord.releaseBroadcastPending} mark either.
|
|
797
|
+
* Every path that removes a watcher or consumes a mark ends here.
|
|
798
|
+
*/
|
|
799
|
+
protected pruneUriWatchRecord(uri: string): void {
|
|
800
|
+
const record = this.uriWatchRecords.get(uri);
|
|
801
|
+
if (!record || record.watchers.size > 0) {
|
|
675
802
|
return;
|
|
676
803
|
}
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
804
|
+
if (record.releaseBroadcastPending) {
|
|
805
|
+
record.fingerprint = undefined;
|
|
806
|
+
record.fingerprintVersion = undefined;
|
|
807
|
+
} else {
|
|
808
|
+
this.uriWatchRecords.delete(uri);
|
|
681
809
|
}
|
|
682
810
|
}
|
|
683
811
|
|
|
684
812
|
/**
|
|
685
|
-
* Canonicalise a URI string for use as a
|
|
686
|
-
*
|
|
687
|
-
*
|
|
688
|
-
*
|
|
813
|
+
* Canonicalise a URI string for use as a {@link uriWatchRecords} key, via the
|
|
814
|
+
* shared `DocumentUriPolicy`. Callers may send non-canonical URIs
|
|
815
|
+
* (drive-letter casing, percent-encoding differences, or a symlink path)
|
|
816
|
+
* over the wire; the dispatch side keys by
|
|
689
817
|
* `document.uri.toString()` from `LangiumDocuments`, so writer keys must
|
|
690
818
|
* canonicalise to the same form or events silently fail to deliver.
|
|
691
819
|
* Routing through the seam (rather than a bare `UriUtils.normalize`)
|
|
@@ -695,6 +823,7 @@ export class DataServer<
|
|
|
695
823
|
* LSP head resolves.
|
|
696
824
|
*
|
|
697
825
|
* Adopters can still override for head-specific URI policy by subclassing.
|
|
826
|
+
* The result keys watches only; a document is looked up by its own URI.
|
|
698
827
|
*/
|
|
699
828
|
protected canonicalKey(uri: string): string {
|
|
700
829
|
return this.services.workspace.DocumentUriPolicy.canonicalUri(uri);
|
|
@@ -734,27 +863,60 @@ export class DataServer<
|
|
|
734
863
|
// (see resolveReferenceServices) and delegate.
|
|
735
864
|
// ============================================================
|
|
736
865
|
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
866
|
+
/**
|
|
867
|
+
* Settle the build before a reference question is answered from the index.
|
|
868
|
+
*
|
|
869
|
+
* A query issued right after a model update otherwise races the rebuild and
|
|
870
|
+
* reads an index missing the symbols it asks about. The cost is not confined
|
|
871
|
+
* to this call: a scope built inside that window is cached, and the build's
|
|
872
|
+
* remaining documents link against it.
|
|
873
|
+
*
|
|
874
|
+
* Waits the source document specifically, but ONLY when one is loaded at
|
|
875
|
+
* that URI: a synthetic source can address a URI with no document (a
|
|
876
|
+
* directory URI, typically), where a per-URI `waitUntil` throws "No document
|
|
877
|
+
* found". Falls back to a global settle, matching the id-based
|
|
878
|
+
* `ElementSource` path.
|
|
879
|
+
*
|
|
880
|
+
* Bounded by {@link DataServerOptions.referenceSettleTimeoutMs}.
|
|
881
|
+
*/
|
|
882
|
+
protected async awaitReferencesLinked(source: ReferenceSource): Promise<void> {
|
|
883
|
+
const uri = isDocumentSource(source) || isSyntheticSource(source) ? UriUtils.toUri(source.uri) : undefined;
|
|
746
884
|
const waitUri = uri && this.services.workspace.LangiumDocuments.hasDocument(uri) ? uri : undefined;
|
|
747
|
-
|
|
885
|
+
const stopwatch = this.services.Clock.stopwatch();
|
|
886
|
+
const linked = this.services.workspace.DocumentBuilder.waitUntil(DocumentState.Linked, waitUri);
|
|
887
|
+
if ((await this.services.Clock.raceTimer(linked, this.options.referenceSettleTimeoutMs)) !== TIMED_OUT) {
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
const elapsedMs = Math.round(stopwatch.elapsedMs);
|
|
891
|
+
// A per-URI wait resolves off a document-phase notification, so a
|
|
892
|
+
// missed one strands a wait on a document that HAS reached the
|
|
893
|
+
// phase. Re-read before failing; the workspace-wide wait has no
|
|
894
|
+
// equivalent reading and can only reject.
|
|
895
|
+
const document = waitUri ? this.services.workspace.LangiumDocuments.getDocument(waitUri) : undefined;
|
|
896
|
+
if (waitUri && document && document.state >= DocumentState.Linked) {
|
|
897
|
+
this.tracer
|
|
898
|
+
.withUri(waitUri.toString())
|
|
899
|
+
.warn(`Missed the 'Linked' notification after ${elapsedMs}ms; the document already reached it`);
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
throw referenceSettleTimeoutError(elapsedMs);
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
async findReferenceCandidates(ctx: ReferenceContext): Promise<ReferenceCandidate[]> {
|
|
906
|
+
await this.awaitReferencesLinked(ctx.source);
|
|
748
907
|
return this.resolveReferenceServices(ctx.source).CandidateProvider.find(ctx);
|
|
749
908
|
}
|
|
750
909
|
|
|
751
|
-
async resolveReference
|
|
910
|
+
async resolveReference<TElement extends TransferElement = TransferElement>(
|
|
911
|
+
ref: ReferenceRequest
|
|
912
|
+
): Promise<ReferenceTarget<TElement> | undefined> {
|
|
913
|
+
await this.awaitReferencesLinked(ref.source);
|
|
752
914
|
const resolved = this.resolveReferenceServices(ref.source).CandidateProvider.resolveCandidate(ref);
|
|
753
915
|
if (!resolved) {
|
|
754
916
|
return undefined;
|
|
755
917
|
}
|
|
756
|
-
|
|
757
|
-
return { ...resolved.candidate, element };
|
|
918
|
+
// `TElement` is the caller's claim about the target's type; nothing checks it.
|
|
919
|
+
return { ...resolved.candidate, element: this.encoder.toTransfer(resolved.node) as TElement };
|
|
758
920
|
}
|
|
759
921
|
|
|
760
922
|
async findNextName(args: FindNextNameArgs): Promise<string> {
|
|
@@ -764,15 +926,31 @@ export class DataServer<
|
|
|
764
926
|
// synthetic source — and the create-element flow driving this method is
|
|
765
927
|
// the one that produces directory URIs, on which a bare
|
|
766
928
|
// `getServices(uri)` throws while `findReferenceCandidates` succeeds.
|
|
767
|
-
const
|
|
929
|
+
const source = ReferenceSource.synthetic(args.uri, args.type, args.language);
|
|
930
|
+
// Mid-rebuild the names of documents not yet re-indexed are absent from
|
|
931
|
+
// the taken set, and the caller PERSISTS what it is handed. Nothing
|
|
932
|
+
// reserves the answer either, so a name is free of the collisions the
|
|
933
|
+
// index knows about and of nothing else — two callers racing get the same
|
|
934
|
+
// proposal, which only uniqueness at the write can catch.
|
|
935
|
+
await this.awaitReferencesLinked(source);
|
|
936
|
+
const nameProvider = this.resolveReferenceServices(source).NameProvider;
|
|
768
937
|
const tier = args.tier ?? 'project';
|
|
769
938
|
if (tier === 'public') {
|
|
770
939
|
return nameProvider.findNextProjectQualifiedName(args.type, args.proposal);
|
|
771
940
|
}
|
|
772
941
|
if (tier === 'local') {
|
|
773
|
-
// Document-scoped uniqueness: the document root is the container.
|
|
774
|
-
|
|
775
|
-
|
|
942
|
+
// Document-scoped uniqueness: the document root is the container. A
|
|
943
|
+
// URI with no document has nothing to collide with, like a document
|
|
944
|
+
// with no root.
|
|
945
|
+
try {
|
|
946
|
+
const document = await this.modelService.ensureDocumentState(args.uri);
|
|
947
|
+
return document.root ? nameProvider.findNextName(args.type, args.proposal, document.root) : args.proposal;
|
|
948
|
+
} catch (error: unknown) {
|
|
949
|
+
if (this.isMissingDocument(error, args.uri)) {
|
|
950
|
+
return args.proposal;
|
|
951
|
+
}
|
|
952
|
+
throw error;
|
|
953
|
+
}
|
|
776
954
|
}
|
|
777
955
|
const project = this.services.workspace.ProjectManager.getProject(uri);
|
|
778
956
|
if (!project) {
|
|
@@ -809,67 +987,32 @@ export class DataServer<
|
|
|
809
987
|
}
|
|
810
988
|
|
|
811
989
|
/**
|
|
812
|
-
* Pick the language that owns a reference source
|
|
990
|
+
* Pick the language that owns a reference source: the registry's
|
|
991
|
+
* `getLanguageFor` over the source's declared language, URI and AST type,
|
|
992
|
+
* with an {@link isElementSource} routed by the document that holds the
|
|
993
|
+
* element (see {@link findElementDocumentUri}) before its type, and
|
|
994
|
+
* {@link fallbackReferenceLanguage}, adopter policy, last.
|
|
813
995
|
*
|
|
814
|
-
*
|
|
815
|
-
*
|
|
816
|
-
*
|
|
817
|
-
* single-language adopters never reach the steps below, and never pay
|
|
818
|
-
* for them.
|
|
819
|
-
* 3. An {@link isElementSource} source carries no URI, so in a
|
|
820
|
-
* multi-language workspace it is routed via the document that holds the
|
|
821
|
-
* element (see {@link findElementDocumentUri}).
|
|
822
|
-
* 4. A source carrying an AST type is routed by that type when exactly one
|
|
823
|
-
* registered grammar can produce it (see
|
|
824
|
-
* {@link resolveReferenceLanguageByType}, over the registry's own type
|
|
825
|
-
* index). This is what resolves a create-element flow's synthetic source
|
|
826
|
-
* on a bare directory URI without asking the adopter.
|
|
827
|
-
* 5. Anything still unresolved falls to {@link fallbackReferenceLanguage},
|
|
828
|
-
* which is adopter policy.
|
|
996
|
+
* An unregistered declared language falls through rather than failing: the
|
|
997
|
+
* id crosses the wire, so a client built against a head with one more
|
|
998
|
+
* grammar would otherwise lose queries the other signals answer.
|
|
829
999
|
*/
|
|
830
1000
|
protected resolveReferenceLanguage(source: ReferenceSource): HydraniumLanguageServices | undefined {
|
|
831
1001
|
const registry = this.services.ServiceRegistry;
|
|
832
|
-
// `getServicesFor` (non-throwing, one ladder walk) gates the URI lookup:
|
|
833
|
-
// an extensionless / unregistered URI falls through to the steps below
|
|
834
|
-
// rather than throwing "no services for the extension ''".
|
|
835
|
-
const uri = isDocumentSource(source) || isSyntheticSource(source) ? UriUtils.toUri(source.uri) : undefined;
|
|
836
|
-
const byUri = uri && registry.getServicesFor(uri);
|
|
837
|
-
if (byUri) {
|
|
838
|
-
return byUri;
|
|
839
|
-
}
|
|
840
|
-
const all = registry.all;
|
|
841
|
-
if (all.length === 1) {
|
|
842
|
-
return all[0] as HydraniumLanguageServices;
|
|
843
|
-
}
|
|
844
1002
|
if (isElementSource(source)) {
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
1003
|
+
// The index lookup runs only once the cheaper signals abstained.
|
|
1004
|
+
return (
|
|
1005
|
+
registry.getLanguageFor({}) ??
|
|
1006
|
+
registry.getLanguageFor({ target: this.findElementDocumentUri(source), type: source.type }) ??
|
|
1007
|
+
this.fallbackReferenceLanguage(source)
|
|
1008
|
+
);
|
|
850
1009
|
}
|
|
851
|
-
const
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
* {@link isSyntheticSource}'s `type` (the transient node being created) or an
|
|
858
|
-
* {@link isElementSource}'s optional narrowing `type`.
|
|
859
|
-
*
|
|
860
|
-
* Answers only when EXACTLY ONE registered grammar can produce the type.
|
|
861
|
-
* Several can when the type comes from a grammar both import, and then the
|
|
862
|
-
* type genuinely does not identify a language — that is a fall-through to
|
|
863
|
-
* adopter policy, not a coin toss. Note this asks which grammar can *produce*
|
|
864
|
-
* the type, not which mentions it: a grammar that merely cross-references a
|
|
865
|
-
* type can never hold a node of it.
|
|
866
|
-
*/
|
|
867
|
-
protected resolveReferenceLanguageByType(source: ReferenceSource): HydraniumLanguageServices | undefined {
|
|
868
|
-
const type = isSyntheticSource(source) ? source.type : isElementSource(source) ? source.type : undefined;
|
|
869
|
-
// The registry owns the type index — it is the one place that knows when
|
|
870
|
-
// the registered set changed, so unlike a private memo here it cannot go
|
|
871
|
-
// stale on a language registered after the first lookup.
|
|
872
|
-
return type ? this.services.ServiceRegistry.soleServicesByType(type) : undefined;
|
|
1010
|
+
const hint = isSyntheticSource(source)
|
|
1011
|
+
? { languageId: source.language, target: source.uri, type: source.type }
|
|
1012
|
+
: isDocumentSource(source)
|
|
1013
|
+
? { target: source.uri }
|
|
1014
|
+
: {};
|
|
1015
|
+
return registry.getLanguageFor(hint) ?? this.fallbackReferenceLanguage(source);
|
|
873
1016
|
}
|
|
874
1017
|
|
|
875
1018
|
/**
|
|
@@ -879,15 +1022,14 @@ export class DataServer<
|
|
|
879
1022
|
* disambiguates names that repeat across types (honouring grammar
|
|
880
1023
|
* subtyping through `AstReflection.isSubtype`).
|
|
881
1024
|
*
|
|
882
|
-
* Only reached
|
|
883
|
-
*
|
|
884
|
-
* step 2.
|
|
1025
|
+
* Only reached in a multi-language workspace, once the declared language
|
|
1026
|
+
* and the URI have abstained.
|
|
885
1027
|
*
|
|
886
1028
|
* Abstains when the name matches elements in more than one DOCUMENT — the
|
|
887
1029
|
* index spans every language and is filled in build order, so "the
|
|
888
|
-
* first match" would be file-watch order rather than an answer.
|
|
889
|
-
* then falls through to the type
|
|
890
|
-
*
|
|
1030
|
+
* first match" would be file-watch order rather than an answer. Routing
|
|
1031
|
+
* then falls through to the element's type, which abstains on ties in the
|
|
1032
|
+
* same way, and finally to adopter policy.
|
|
891
1033
|
*/
|
|
892
1034
|
protected findElementDocumentUri(source: ElementSource): URI | undefined {
|
|
893
1035
|
const matches = this.services.workspace.IndexManager.getElementsByName(source.name, source.type);
|
|
@@ -922,61 +1064,144 @@ export class DataServer<
|
|
|
922
1064
|
// Internal plumbing
|
|
923
1065
|
// ============================================================
|
|
924
1066
|
|
|
1067
|
+
/**
|
|
1068
|
+
* Whether `error`, from waiting on `uri`'s document, means that `uri` has
|
|
1069
|
+
* no document: a URI with neither a file nor text builds nothing, and
|
|
1070
|
+
* Langium's wait after the build rejects for want of one, with a
|
|
1071
|
+
* `ServerCancelled` response error. Any other failure, such as a read
|
|
1072
|
+
* that fails before a document is registered, a cancelled wait or a
|
|
1073
|
+
* `ReentrantWriteLockError`, is the caller's to see.
|
|
1074
|
+
*/
|
|
1075
|
+
protected isMissingDocument(error: unknown, uri: string): boolean {
|
|
1076
|
+
return isResponseError(error) && error.code === SERVER_CANCELLED && this.modelService.getDocument(uri) === undefined;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
925
1079
|
/**
|
|
926
1080
|
* Build a {@link TransferDocument} envelope from the current document state,
|
|
927
1081
|
* delegating root + diagnostic encoding to {@link encoder} (see
|
|
928
|
-
* `TransferEncoder.toTransferDocument` for the walk).
|
|
929
|
-
*
|
|
930
|
-
*
|
|
931
|
-
* `Record<string, TransferElement>` at the framework-default level, and an
|
|
932
|
-
* adopter supplying a typed-overlay encoder narrows the runtime shape to
|
|
933
|
-
* its wire types. The cast on the return is where that invariant — the
|
|
934
|
-
* adopter's `TTransfer` matches its encoder's overlay — is asserted, at a
|
|
935
|
-
* single boundary point rather than spread across the callers.
|
|
1082
|
+
* `TransferEncoder.toTransferDocument` for the walk). `fingerprint` is the
|
|
1083
|
+
* caller's {@link computeDocumentFingerprint} of that same state, which
|
|
1084
|
+
* spares hashing it twice.
|
|
936
1085
|
*/
|
|
937
|
-
protected envelope(uri: URI): TransferDocument<TTransfer, TDiagnostic> {
|
|
1086
|
+
protected envelope(uri: URI, fingerprint?: string): TransferDocument<TTransfer, TDiagnostic> {
|
|
938
1087
|
// Resolve through the model service's canonicalizing gateway rather than
|
|
939
1088
|
// reaching into `LangiumDocuments` directly, so a divergent (symlink) URI
|
|
940
1089
|
// still finds the document the build keys by its real path — and so the
|
|
941
1090
|
// data-server never has to remember to canonicalize this lookup itself.
|
|
942
1091
|
const document = this.modelService.getDocument(uri.toString());
|
|
943
|
-
if (!document) {
|
|
1092
|
+
if (!document || this.services.workspace.ModelLedger.isPlaceholder(document.parseResult.value)) {
|
|
944
1093
|
// No document — a shaped envelope rather than a throw, so the caller
|
|
945
|
-
// decides policy at its own layer; `
|
|
1094
|
+
// decides policy at its own layer; `model` is optional on the envelope
|
|
946
1095
|
// so the compiler forces that decision. Adopters preferring to throw
|
|
947
|
-
// override `envelope`.
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
version: 0,
|
|
951
|
-
root: undefined,
|
|
952
|
-
diagnostics: [] as TDiagnostic[]
|
|
953
|
-
};
|
|
1096
|
+
// override `envelope`. The builder's unparsed placeholder is no
|
|
1097
|
+
// document either: its root parses no text.
|
|
1098
|
+
return TransferDocument.absent<TTransfer, TDiagnostic>(uri.toString());
|
|
954
1099
|
}
|
|
955
|
-
|
|
1100
|
+
const encoded = this.encoder.toTransferDocument(document);
|
|
1101
|
+
return this.withServerState(encoded, fingerprint ?? this.computeDocumentFingerprint(document.parseResult.value, encoded));
|
|
956
1102
|
}
|
|
957
1103
|
|
|
958
1104
|
/**
|
|
959
|
-
*
|
|
960
|
-
*
|
|
961
|
-
*
|
|
962
|
-
*
|
|
1105
|
+
* The transfer document a request answers with for `astDocument`, its model
|
|
1106
|
+
* hashed and the text stamped on it by {@link withServerState}. Every
|
|
1107
|
+
* document a request answers with goes through here, so none goes out
|
|
1108
|
+
* without them.
|
|
963
1109
|
*/
|
|
964
|
-
protected
|
|
965
|
-
this.
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
1110
|
+
protected encodeDocument(astDocument: AstDocument<AstNode, AstDiagnostic>): TransferDocument<TTransfer, TDiagnostic> {
|
|
1111
|
+
const encoded = this.encoder.astDocumentToTransferDocument(astDocument);
|
|
1112
|
+
return this.withServerState(encoded, this.computeDocumentFingerprint(astDocument.root, encoded));
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
/**
|
|
1116
|
+
* `encoded` with `hash` on its model and the text the server holds for it:
|
|
1117
|
+
* the store's while it holds the document, the build's otherwise. The text
|
|
1118
|
+
* is read when the document is sent rather than kept with the build: a save
|
|
1119
|
+
* changes `text.dirty` without a rebuild, and the store's text moves on before
|
|
1120
|
+
* the build that follows it.
|
|
1121
|
+
*/
|
|
1122
|
+
protected withServerState(
|
|
1123
|
+
encoded: EncodedTransferDocument<TransferElement, TDiagnostic>,
|
|
1124
|
+
hash: string
|
|
1125
|
+
): TransferDocument<TTransfer, TDiagnostic> {
|
|
1126
|
+
const text = this.services.workspace.TextDocuments.textState(encoded.uri) ?? this.builtTextState(encoded.uri);
|
|
1127
|
+
if (text && text.version > encoded.model.version) {
|
|
1128
|
+
this.tracer.withUri(encoded.uri).debug(`Send a model behind its text: model v${encoded.model.version} / text v${text.version}`);
|
|
1129
|
+
}
|
|
1130
|
+
return {
|
|
1131
|
+
uri: encoded.uri,
|
|
1132
|
+
// The encoder returns the structural base; `TTransfer` is the adopter's
|
|
1133
|
+
// declaration of the roots its languages produce, and nothing checks it.
|
|
1134
|
+
model: { ...encoded.model, hash } as TransferModelSnapshot<TTransfer, TDiagnostic>,
|
|
1135
|
+
...(text ? { text } : {})
|
|
1136
|
+
};
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/**
|
|
1140
|
+
* The text a build read for a document the store never held, which is
|
|
1141
|
+
* clean: the store holds every text a client gave it.
|
|
1142
|
+
*/
|
|
1143
|
+
protected builtTextState(uri: string): TextState | undefined {
|
|
1144
|
+
const textDocument = this.modelService.getDocument(uri)?.textDocument;
|
|
1145
|
+
return textDocument && { version: textDocument.version, hash: textHash(textDocument.getText()), dirty: false };
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
/**
|
|
1149
|
+
* Follow every document at the configured
|
|
1150
|
+
* {@link DataServerOptions.subscriptionPhase}, once per connection rather
|
|
1151
|
+
* than once per watch, so the cost stays flat in the number of watches.
|
|
1152
|
+
*/
|
|
1153
|
+
protected subscribeToModelUpdates(): void {
|
|
1154
|
+
const phase = this.options.subscriptionPhase;
|
|
1155
|
+
this.disposables.push(this.modelService.onModelUpdated(event => this.dispatchPhaseEvent(event), { phase }));
|
|
1156
|
+
// One message per build rather than one per document.
|
|
1157
|
+
this.disposables.push(this.modelService.onModelsBuilt(event => this.dispatchBuiltEvent(event.uris), { phase }));
|
|
970
1158
|
}
|
|
971
1159
|
|
|
972
1160
|
/**
|
|
973
|
-
*
|
|
974
|
-
*
|
|
975
|
-
*
|
|
976
|
-
*
|
|
977
|
-
* or any other client writing through `notifyDidSaveTextDocument`.
|
|
1161
|
+
* Report the documents that reached {@link DataServerOptions.subscriptionPhase}
|
|
1162
|
+
* and that NOBODY on this
|
|
1163
|
+
* connection is watching — the ones a client was not told about through
|
|
1164
|
+
* {@link dispatchPhaseEvent}, which is gated per URI.
|
|
978
1165
|
*
|
|
979
|
-
*
|
|
1166
|
+
* **The gap this closes is the one no other source can observe.** A document
|
|
1167
|
+
* the client watches, it hears about already. A file changed on disk, the
|
|
1168
|
+
* host's own filesystem watcher reports — and on a browser host, where the
|
|
1169
|
+
* workspace lives behind this head, an external change cannot happen at all.
|
|
1170
|
+
* What is left, and what nothing outside the server can see, is a document
|
|
1171
|
+
* rebuilt because something it DEPENDS ON changed: its file never changed, so
|
|
1172
|
+
* a filesystem watcher is silent by construction, and it has no subscriber, so
|
|
1173
|
+
* the update channel is silent by design. A consumer displaying data derived
|
|
1174
|
+
* from that document — a tree label, a decorator — otherwise goes stale with
|
|
1175
|
+
* no signal from any source, and repairs itself the moment someone opens the
|
|
1176
|
+
* file to investigate, which is what makes it expensive to diagnose later.
|
|
1177
|
+
*
|
|
1178
|
+
* Payload-free on purpose: URIs only, so a client re-reads what it displays
|
|
1179
|
+
* rather than being handed transfer documents it did not ask for. That is the
|
|
1180
|
+
* property the watcher gate protects, and it is preserved here by
|
|
1181
|
+
* carrying no document rather than by gating the message.
|
|
1182
|
+
*
|
|
1183
|
+
* Quiet in the common case. Editing a document that an editor has open leaves
|
|
1184
|
+
* that URI watched and therefore out of this set, so a build with no
|
|
1185
|
+
* dependents produces nothing at all, and workspace initialisation produces
|
|
1186
|
+
* nothing because it does not build to this phase. The ceiling is a
|
|
1187
|
+
* whole-workspace rebuild at the subscription phase: one message, URIs only.
|
|
1188
|
+
*/
|
|
1189
|
+
protected dispatchBuiltEvent(built: readonly CanonicalUri[]): void {
|
|
1190
|
+
const uris = built.map(uri => this.canonicalKey(uri)).filter(uri => !this.uriWatchRecords.get(uri)?.watchers.size);
|
|
1191
|
+
if (uris.length === 0) {
|
|
1192
|
+
return;
|
|
1193
|
+
}
|
|
1194
|
+
this.tracer.debug(`Emit onDocumentsBuilt: ${uris.length} unwatched document(s)`);
|
|
1195
|
+
this.clientProxy.onDocumentsBuilt({ uris });
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
/**
|
|
1199
|
+
* Follow every save so a single `onDocumentSaved` wire notification fires
|
|
1200
|
+
* for ANY save of a subscribed URI — regardless of whether the save
|
|
1201
|
+
* originated from the data-server's RPC `saveModelDocument`, the LSP head's
|
|
1202
|
+
* text-editor save, or any other client.
|
|
1203
|
+
*
|
|
1204
|
+
* Architectural symmetry with {@link subscribeToModelUpdates}: every
|
|
980
1205
|
* subscribed client sees every state change to documents they care about,
|
|
981
1206
|
* regardless of which client triggered it. Firing `onDocumentSaved` only
|
|
982
1207
|
* from the data-server's own RPC path is a bug, not an optimisation: an
|
|
@@ -984,41 +1209,114 @@ export class DataServer<
|
|
|
984
1209
|
* clients, which never clear their dirty state.
|
|
985
1210
|
*/
|
|
986
1211
|
protected subscribeToTextDocumentSaves(): void {
|
|
987
|
-
this.disposables.push(this.
|
|
1212
|
+
this.disposables.push(this.modelService.onModelSaved(event => this.dispatchSaveEvent(event)));
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/** Relay each change of a document's dirty state; see {@link dispatchDirtyEvent}. */
|
|
1216
|
+
protected subscribeToDirtyChanges(): void {
|
|
1217
|
+
this.disposables.push(this.modelService.onDirtyChanged(event => this.dispatchDirtyEvent(event)));
|
|
988
1218
|
}
|
|
989
1219
|
|
|
990
1220
|
/**
|
|
991
|
-
*
|
|
992
|
-
* {@link
|
|
993
|
-
*
|
|
994
|
-
* hold, so a `false` answer means this close was the last one.
|
|
1221
|
+
* Send a dirty flip to the connection, gated on the URI's watchers as
|
|
1222
|
+
* {@link dispatchSaveEvent} is: the answer at any one moment travels on
|
|
1223
|
+
* every document sent, so a client that watches nothing reads it there.
|
|
995
1224
|
*/
|
|
996
|
-
protected
|
|
997
|
-
const
|
|
1225
|
+
protected dispatchDirtyEvent(event: ModelDirtyChangedEvent): void {
|
|
1226
|
+
const uri = this.canonicalKey(event.uri);
|
|
1227
|
+
if (this.uriWatchRecords.get(uri)?.watchers.size) {
|
|
1228
|
+
this.clientProxy.onDocumentDirtyChanged({ uri, text: event.text });
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
/**
|
|
1233
|
+
* Mark each document the store releases, so {@link dispatchPhaseEvent}
|
|
1234
|
+
* broadcasts the build that follows even without a watcher. By default
|
|
1235
|
+
* that build reverts the document to its disk content, discarding unsaved
|
|
1236
|
+
* in-session edits, and the last close typically also
|
|
1237
|
+
* removed the last watcher: without the broadcast, a consumer that only
|
|
1238
|
+
* ever fetches via `getModelDocument` keeps showing the discarded state.
|
|
1239
|
+
* The broadcast is de-duplicated against the fingerprint while a watcher
|
|
1240
|
+
* holds one, so a close whose disk state equals the last emitted state
|
|
1241
|
+
* stays silent; with no watcher there is no fingerprint, and every release
|
|
1242
|
+
* build is sent. The first build after the release consumes the mark, and
|
|
1243
|
+
* goes out as the release unless it is stamped past the version a client
|
|
1244
|
+
* opened the document at again: a client's write or an integrity repair
|
|
1245
|
+
* that rides it keeps its author.
|
|
1246
|
+
*
|
|
1247
|
+
* A release, not the close itself: a document whose last client lost its
|
|
1248
|
+
* connection is released only once the release grace runs out, or when
|
|
1249
|
+
* another client opens it meanwhile, and not at all when a client lost
|
|
1250
|
+
* from it opens it again within its own grace.
|
|
1251
|
+
*/
|
|
1252
|
+
protected subscribeToModelReleases(): void {
|
|
998
1253
|
this.disposables.push(
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1254
|
+
this.modelService.onModelReleased(event => {
|
|
1255
|
+
const uri = this.canonicalKey(event.uri);
|
|
1256
|
+
const record = this.uriWatchRecords.get(uri) ?? { watchers: new Set<string>() };
|
|
1257
|
+
record.releaseBroadcastPending = true;
|
|
1258
|
+
this.uriWatchRecords.set(uri, record);
|
|
1259
|
+
this.tracer.withUri(uri).debug('Release marked: the first build after it is broadcast, watched or not');
|
|
1003
1260
|
})
|
|
1004
1261
|
);
|
|
1005
1262
|
}
|
|
1006
1263
|
|
|
1007
|
-
/**
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1264
|
+
/**
|
|
1265
|
+
* Fan out a deletion for each removed URI, to EVERY client on the connection
|
|
1266
|
+
* rather than only to the ones watching that URI.
|
|
1267
|
+
*
|
|
1268
|
+
* **Deliberately ungated, where {@link dispatchPhaseEvent} and
|
|
1269
|
+
* {@link dispatchSaveEvent} are gated.** Those two carry a built document and
|
|
1270
|
+
* fire on every build, so the watcher gate is what keeps bandwidth
|
|
1271
|
+
* proportional to what a client asked for. A deletion is neither: it is one
|
|
1272
|
+
* URI, it is rare, and it reports the workspace's STRUCTURE rather than a
|
|
1273
|
+
* document's content. Gating it forces any consumer that displays the
|
|
1274
|
+
* workspace — a model tree, a file decorator — to learn about disappearances
|
|
1275
|
+
* from a filesystem watcher instead, which a client hosted in a browser has
|
|
1276
|
+
* no way to run: its workspace lives behind the head. A client that does not
|
|
1277
|
+
* care filters on the URI, which costs it a comparison.
|
|
1278
|
+
*
|
|
1279
|
+
* The precedent is the release broadcast (see
|
|
1280
|
+
* {@link subscribeToModelReleases}), where the same judgement was already made
|
|
1281
|
+
* in the other direction: a transition that matters enough is delivered
|
|
1282
|
+
* without a subscription.
|
|
1283
|
+
*
|
|
1284
|
+
* A deletion is announced before the build that follows it, so this goes
|
|
1285
|
+
* out ahead of the `'rebuilt'` events for the dependents whose references
|
|
1286
|
+
* the deletion just broke.
|
|
1287
|
+
*
|
|
1288
|
+
* The URI's fingerprint and release mark are dropped: they describe a
|
|
1289
|
+
* document that no longer exists. A kept fingerprint is adopted as the
|
|
1290
|
+
* baseline and suppresses the first emit after the file returns with its
|
|
1291
|
+
* previous content.
|
|
1292
|
+
*
|
|
1293
|
+
* The watchers SURVIVE: a recreated file resumes delivering to them with no
|
|
1294
|
+
* re-subscription, and a client that answers the deletion by closing
|
|
1295
|
+
* releases the watch through `closeModelDocument` anyway.
|
|
1296
|
+
*/
|
|
1297
|
+
protected dispatchDeleteEvent(event: ModelDeletedEvent): void {
|
|
1298
|
+
const uri = this.canonicalKey(event.uri);
|
|
1299
|
+
const record = this.uriWatchRecords.get(uri);
|
|
1300
|
+
if (record) {
|
|
1301
|
+
record.fingerprint = undefined;
|
|
1302
|
+
record.fingerprintVersion = undefined;
|
|
1303
|
+
record.releaseBroadcastPending = undefined;
|
|
1304
|
+
record.sentVersion = undefined;
|
|
1305
|
+
this.pruneUriWatchRecord(uri);
|
|
1306
|
+
}
|
|
1307
|
+
this.clientProxy.onDocumentDeleted({ uri });
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
/** Fan out a save event for the document's URI, gated on the URI's watchers. */
|
|
1311
|
+
protected dispatchSaveEvent(event: AstDocumentSavedEvent<AstNode>): void {
|
|
1014
1312
|
const uri = this.canonicalKey(event.document.uri);
|
|
1015
|
-
if (!this.
|
|
1313
|
+
if (!this.uriWatchRecords.get(uri)?.watchers.size) {
|
|
1016
1314
|
return;
|
|
1017
1315
|
}
|
|
1018
|
-
const response = this.envelope(UriUtils.toUri(uri));
|
|
1316
|
+
const response = this.envelope(UriUtils.toUri(event.document.uri));
|
|
1019
1317
|
const wireEvent: TransferDocumentSavedEvent<TTransfer, TDiagnostic> = {
|
|
1020
1318
|
document: response,
|
|
1021
|
-
sourceClientId: event.
|
|
1319
|
+
sourceClientId: event.sourceClientId
|
|
1022
1320
|
};
|
|
1023
1321
|
this.clientProxy.onDocumentSaved(wireEvent);
|
|
1024
1322
|
}
|
|
@@ -1032,116 +1330,114 @@ export class DataServer<
|
|
|
1032
1330
|
* out to multiple local subscribers with an `Emitter<T>` — the
|
|
1033
1331
|
* framework deliberately does NOT promise multi-listener semantics.
|
|
1034
1332
|
*
|
|
1035
|
-
*
|
|
1036
|
-
*
|
|
1037
|
-
*
|
|
1038
|
-
*
|
|
1039
|
-
*
|
|
1040
|
-
*
|
|
1041
|
-
*
|
|
1333
|
+
* An event for a URI no client watches is NOT sent over the wire, so
|
|
1334
|
+
* bandwidth scales with watched URIs rather than with phase events; a
|
|
1335
|
+
* pending release mark is the exception (see
|
|
1336
|
+
* {@link subscribeToModelReleases}).
|
|
1337
|
+
*
|
|
1338
|
+
* An event for a rebuild with no observable change since the last emit is
|
|
1339
|
+
* suppressed against the URI's fingerprint and the version it was taken at;
|
|
1340
|
+
* a new version with the same fingerprint goes out with the same
|
|
1341
|
+
* `model.hash`, so a watcher still learns the version. Such rebuilds are routine: a
|
|
1342
|
+
* second client attaching to an open document makes
|
|
1343
|
+
* `HydraniumTextDocuments.refreshContent` fire `onDidChangeContent` purely
|
|
1344
|
+
* to re-trigger the build, and Langium rebuilds a document when a
|
|
1345
|
+
* dependency changes even if its diagnostics come out the same. Sent at a
|
|
1346
|
+
* version this head has not sent, they reach watchers as `'changed'`, and a
|
|
1347
|
+
* watcher that reads `'changed'` as another client's write and resets its
|
|
1348
|
+
* root to the server view loses the user's edits. The first {@link watchModelDocument} baselines the
|
|
1349
|
+
* fingerprint, so the first event after a watch is de-duplicated against
|
|
1350
|
+
* the state the watcher just fetched; it goes with the last watcher, so the
|
|
1351
|
+
* next first watch re-baselines rather than adopting a stale digest. A
|
|
1352
|
+
* digest keeps its memory constant in document size; see
|
|
1353
|
+
* {@link computeDocumentFingerprint} for the inputs.
|
|
1042
1354
|
*/
|
|
1043
|
-
protected dispatchPhaseEvent(
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
// A pending revert mark is deliberately NOT consumed here: the
|
|
1049
|
-
// follow-up build re-fires this phase and broadcasts then.
|
|
1355
|
+
protected dispatchPhaseEvent(event: ModelUpdatedEvent<AstNode>): void {
|
|
1356
|
+
const uri = this.canonicalKey(event.document.uri);
|
|
1357
|
+
const record = this.uriWatchRecords.get(uri);
|
|
1358
|
+
const afterRelease = record?.releaseBroadcastPending === true;
|
|
1359
|
+
if (!record || (record.watchers.size === 0 && !afterRelease)) {
|
|
1050
1360
|
return;
|
|
1051
1361
|
}
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1362
|
+
if (afterRelease) {
|
|
1363
|
+
// Consumed even when watchers exist — the regular dispatch below
|
|
1364
|
+
// serves them, and the mark's attribution is more precise than the
|
|
1365
|
+
// post-close unknown client the manager's attribution would yield.
|
|
1366
|
+
// With no watcher the record goes now, so the fingerprint written
|
|
1367
|
+
// below lands on a detached record and the next first watch
|
|
1368
|
+
// re-baselines instead of adopting it.
|
|
1369
|
+
record.releaseBroadcastPending = undefined;
|
|
1370
|
+
this.pruneUriWatchRecord(uri);
|
|
1371
|
+
}
|
|
1372
|
+
// The encoder reads the built document, which the event's holds for this
|
|
1373
|
+
// synchronous run.
|
|
1374
|
+
const document = this.modelService.getDocument(event.document.uri);
|
|
1375
|
+
if (!document) {
|
|
1059
1376
|
return;
|
|
1060
1377
|
}
|
|
1061
|
-
const fingerprint = this.computeDocumentFingerprint(document);
|
|
1062
|
-
|
|
1378
|
+
const fingerprint = this.computeDocumentFingerprint(document.parseResult.value, this.encoder.toTransferDocument(document));
|
|
1379
|
+
const stamped = this.services.workspace.ModelLedger.versionOf(document.parseResult.value);
|
|
1380
|
+
if (record.fingerprint === fingerprint && record.fingerprintVersion === stamped) {
|
|
1063
1381
|
// A rebuild that produced no observable change since the last emit —
|
|
1064
1382
|
// suppressed so RPC subscribers don't see a no-op broadcast. Logged at
|
|
1065
1383
|
// debug so a *needed* re-broadcast wrongly suppressed by this dedup
|
|
1066
1384
|
// (the failure mode the fingerprint strategy must avoid) is visible.
|
|
1067
|
-
this.tracer.withUri(uri).debug(`Suppress onDocumentUpdated v${
|
|
1385
|
+
this.tracer.withUri(uri).debug(`Suppress onDocumentUpdated v${stamped}: fingerprint and version unchanged`);
|
|
1068
1386
|
return;
|
|
1069
1387
|
}
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1388
|
+
record.fingerprint = fingerprint;
|
|
1389
|
+
record.fingerprintVersion = stamped;
|
|
1390
|
+
// The event's attribution, so this head names the same client as the
|
|
1391
|
+
// in-process heads do for one build, except for a version this head
|
|
1392
|
+
// already sent; see `DataServerUriWatchRecord.sentVersion`.
|
|
1393
|
+
const version = this.modelService.isOpen(event.document.uri) ? document.textDocument.version : undefined;
|
|
1394
|
+
const { reason, sourceClientId } =
|
|
1395
|
+
version !== undefined && record.sentVersion === version
|
|
1396
|
+
? { reason: 'rebuilt' as const, sourceClientId: UNKNOWN_CLIENT_ID }
|
|
1397
|
+
: event;
|
|
1398
|
+
record.sentVersion = version;
|
|
1399
|
+
// The stamp, not the live version: a write after the build's parse moves
|
|
1400
|
+
// only the latter. At the opened version too, which a reopen with the
|
|
1401
|
+
// released text keeps.
|
|
1402
|
+
const openedVersion = afterRelease ? this.modelService.openedVersion(event.document.uri) : undefined;
|
|
1403
|
+
const releaseBuild = afterRelease && (openedVersion === undefined || stamped <= openedVersion);
|
|
1404
|
+
const wireEvent: TransferDocumentUpdatedEvent<TTransfer, TDiagnostic> = {
|
|
1405
|
+
document: this.envelope(document.uri, fingerprint),
|
|
1406
|
+
sourceClientId: releaseBuild ? DOCUMENT_RELEASE_CLIENT_ID : sourceClientId,
|
|
1407
|
+
reason
|
|
1075
1408
|
};
|
|
1409
|
+
const mark = afterRelease ? `, release mark: opened v${openedVersion ?? '-'}, attributed ${sourceClientId}` : '';
|
|
1076
1410
|
this.tracer
|
|
1077
1411
|
.withUri(uri)
|
|
1078
|
-
.debug(`Emit onDocumentUpdated v${
|
|
1079
|
-
this.clientProxy.onDocumentUpdated(
|
|
1412
|
+
.debug(`Emit onDocumentUpdated v${stamped} (reason=${wireEvent.reason}, sourceClientId=${wireEvent.sourceClientId}${mark})`);
|
|
1413
|
+
this.clientProxy.onDocumentUpdated(wireEvent);
|
|
1080
1414
|
}
|
|
1081
1415
|
|
|
1082
1416
|
/**
|
|
1083
|
-
* Fingerprint of the
|
|
1084
|
-
* `onDocumentUpdated` emissions
|
|
1085
|
-
*
|
|
1086
|
-
* {@link
|
|
1417
|
+
* Fingerprint of the snapshot `root` and its encoding `encoded`, used to
|
|
1418
|
+
* de-dup `onDocumentUpdated` emissions and sent as every document's
|
|
1419
|
+
* `model.hash`, so it must not depend on the version or on live state. The
|
|
1420
|
+
* {@link FingerprintStrategy} option selects what is hashed (default
|
|
1421
|
+
* `'transfer-document'` — see the type).
|
|
1087
1422
|
*
|
|
1088
|
-
*
|
|
1089
|
-
*
|
|
1090
|
-
*
|
|
1091
|
-
* `envelope` share a single encode walk.
|
|
1423
|
+
* {@link TransferEncoder.toTransferDocument} is cached per build, so within
|
|
1424
|
+
* one phase event the fingerprint and the subsequently-emitted `envelope`
|
|
1425
|
+
* share a single encode walk.
|
|
1092
1426
|
*
|
|
1093
1427
|
* Wrapped in {@link Tracer.time} against {@link FINGERPRINT_LOG_AFTER_MS}.
|
|
1094
1428
|
*/
|
|
1095
|
-
protected computeDocumentFingerprint(
|
|
1096
|
-
return this.tracer
|
|
1097
|
-
|
|
1098
|
-
(
|
|
1099
|
-
|
|
1429
|
+
protected computeDocumentFingerprint(root: AstNode, encoded: EncodedTransferDocument<TransferElement, TDiagnostic>): string {
|
|
1430
|
+
return this.tracer
|
|
1431
|
+
.withUri(encoded.uri)
|
|
1432
|
+
.time(
|
|
1433
|
+
'Compute fingerprint',
|
|
1434
|
+
() =>
|
|
1100
1435
|
this.options.fingerprintStrategy === 'text-diagnostics'
|
|
1101
|
-
? textDiagnosticsFingerprint(
|
|
1102
|
-
: transferDocumentFingerprint(
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
'debug',
|
|
1107
|
-
{ logAfterMs: FINGERPRINT_LOG_AFTER_MS }
|
|
1108
|
-
);
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
/**
|
|
1112
|
-
* Extra inputs folded into {@link computeDocumentFingerprint} alongside the
|
|
1113
|
-
* chosen {@link FingerprintStrategy}. Default: none. Override to contribute a
|
|
1114
|
-
* signal that lives outside the document's text / root / diagnostics — each
|
|
1115
|
-
* entry need only be stable across equivalent emissions and
|
|
1116
|
-
* JSON-serialisable.
|
|
1117
|
-
*/
|
|
1118
|
-
protected additionalFingerprintInputs(_document: LangiumDocument): readonly unknown[] {
|
|
1119
|
-
return [];
|
|
1120
|
-
}
|
|
1121
|
-
|
|
1122
|
-
/**
|
|
1123
|
-
* Discriminate the reason for a phase-event-driven update notification.
|
|
1124
|
-
* Uses the most recent `DocumentBuilder.onUpdate` snapshot:
|
|
1125
|
-
* - URI in the `deleted` list → `'deleted'`
|
|
1126
|
-
* - URI in the `changed` list → `'changed'` (the URI was passed to
|
|
1127
|
-
* `documentBuilder.update(changed, deleted)`, which spans `didChange`
|
|
1128
|
-
* text-document events and programmatic `update([uri], [])` calls).
|
|
1129
|
-
* - Otherwise → `'rebuilt'` (cascade re-derivation: this URI was rebuilt
|
|
1130
|
-
* because something it depends on changed; its own text wasn't flagged).
|
|
1131
|
-
*
|
|
1132
|
-
* `'saved'` is NOT emitted from this code path — saves take the dedicated
|
|
1133
|
-
* `DataClientProtocol.onDocumentSaved` channel; adopters that want a
|
|
1134
|
-
* unified update stream synthesise `'saved'` in their bridge layer.
|
|
1135
|
-
*/
|
|
1136
|
-
protected resolveUpdateReason(uri: URI): TransferDocumentUpdatedEvent<TTransfer, TDiagnostic>['reason'] {
|
|
1137
|
-
const last = this.lastBuildUpdate;
|
|
1138
|
-
if (last?.deleted.some(deleted => UriUtils.equals(deleted, uri))) {
|
|
1139
|
-
return 'deleted';
|
|
1140
|
-
}
|
|
1141
|
-
if (last?.changed.some(changed => UriUtils.equals(changed, uri))) {
|
|
1142
|
-
return 'changed';
|
|
1143
|
-
}
|
|
1144
|
-
return 'rebuilt';
|
|
1436
|
+
? textDiagnosticsFingerprint(this.services.workspace.ModelLedger.textOf(root), encoded.model)
|
|
1437
|
+
: transferDocumentFingerprint(encoded.model),
|
|
1438
|
+
'debug',
|
|
1439
|
+
{ logAfterMs: FINGERPRINT_LOG_AFTER_MS }
|
|
1440
|
+
);
|
|
1145
1441
|
}
|
|
1146
1442
|
|
|
1147
1443
|
/**
|
|
@@ -1187,20 +1483,6 @@ export class DataServer<
|
|
|
1187
1483
|
}
|
|
1188
1484
|
}
|
|
1189
1485
|
|
|
1190
|
-
/**
|
|
1191
|
-
* Resolve the wire-level `sourceClientId` for `document`'s events — the client
|
|
1192
|
-
* that authored its current version, from the version-author history on
|
|
1193
|
-
* `HydraniumTextDocuments`. The protocol-level counterpart of the internal
|
|
1194
|
-
* `AstDocumentManager.getAuthor`: a framework-internal rebuild has no author,
|
|
1195
|
-
* so this surfaces the {@link UNKNOWN_CLIENT_ID} presentation default. Adopters
|
|
1196
|
-
* that rebuild through non-text-document channels override to derive a
|
|
1197
|
-
* source id of their own.
|
|
1198
|
-
*/
|
|
1199
|
-
protected resolveSourceClientId(document: LangiumDocument): string {
|
|
1200
|
-
const author = this.services.workspace.TextDocuments.getAuthor(document.textDocument.uri, document.textDocument.version);
|
|
1201
|
-
return author ?? UNKNOWN_CLIENT_ID;
|
|
1202
|
-
}
|
|
1203
|
-
|
|
1204
1486
|
// --- Diagnostics (DataServerDiagnosticsProtocol) ---------------------------
|
|
1205
1487
|
// Computed in THIS process: the data-server child holds the model store, so
|
|
1206
1488
|
// these snapshots reflect the heap that actually carries the workspace
|
|
@@ -1243,7 +1525,7 @@ export class DataServer<
|
|
|
1243
1525
|
|
|
1244
1526
|
async stopProfiling(args: StopProfilingArgs): Promise<string> {
|
|
1245
1527
|
if (!this.activeProfile) {
|
|
1246
|
-
throw
|
|
1528
|
+
throw noActiveProfileError();
|
|
1247
1529
|
}
|
|
1248
1530
|
const capture = this.activeProfile;
|
|
1249
1531
|
this.activeProfile = undefined;
|
|
@@ -1262,7 +1544,7 @@ export class DataServer<
|
|
|
1262
1544
|
/** Resolve a partial options object into a fully-defaulted form. */
|
|
1263
1545
|
protected resolveOptions(partial: DataServerOptions): ResolvedDataServerOptions {
|
|
1264
1546
|
const additionalMethods = partial.additionalMethods ?? [];
|
|
1265
|
-
const builtIn = [...DATA_SERVER_PROTOCOL_METHODS, ...DATA_SERVER_DIAGNOSTICS_METHODS]
|
|
1547
|
+
const builtIn: readonly string[] = [...DATA_SERVER_PROTOCOL_METHODS, ...DATA_SERVER_DIAGNOSTICS_METHODS];
|
|
1266
1548
|
const overlap = additionalMethods.filter(name => builtIn.includes(name));
|
|
1267
1549
|
if (overlap.length > 0) {
|
|
1268
1550
|
throw new Error(
|
|
@@ -1276,7 +1558,8 @@ export class DataServer<
|
|
|
1276
1558
|
methodNamespace: partial.methodNamespace ?? DATA_SERVER_WIRE_PREFIX,
|
|
1277
1559
|
additionalMethods,
|
|
1278
1560
|
excludedMethods: partial.excludedMethods ?? [],
|
|
1279
|
-
diagnostics: partial.diagnostics ?? defaultDataServerDiagnostics()
|
|
1561
|
+
diagnostics: partial.diagnostics ?? defaultDataServerDiagnostics(),
|
|
1562
|
+
referenceSettleTimeoutMs: partial.referenceSettleTimeoutMs ?? DataServer.DEFAULT_OPTIONS.referenceSettleTimeoutMs
|
|
1280
1563
|
};
|
|
1281
1564
|
}
|
|
1282
1565
|
}
|