/
documents.ts
464 lines (412 loc) · 19.7 KB
/
documents.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/******************************************************************************
* Copyright 2021 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
// re-export 'TextDocument' & friends from 'vscode-languageserver-textdocument' for convenience,
// the overhead is very small, just a few kilobytes
export * from 'vscode-languageserver-textdocument';
import type { Diagnostic, Range } from 'vscode-languageserver-types';
import type { ParseResult } from '../parser/langium-parser.js';
import type { ServiceRegistry } from '../service-registry.js';
import type { LangiumSharedCoreServices } from '../services.js';
import type { AstNode, AstNodeDescription, Mutable, Reference } from '../syntax-tree.js';
import type { MultiMap } from '../utils/collections.js';
import type { Stream } from '../utils/stream.js';
import { CancellationToken } from '../utils/cancellation.js';
import { stream } from '../utils/stream.js';
import { URI } from '../utils/uri-utils.js';
import { TextDocument } from './documents.js';
import type { FileSystemProvider } from './file-system-provider.js';
/**
* A Langium document holds the parse result (AST and CST) and any additional state that is derived
* from the AST, e.g. the result of scope precomputation.
*/
export interface LangiumDocument<T extends AstNode = AstNode> {
/** The Uniform Resource Identifier (URI) of the document */
readonly uri: URI;
/** The text document used to convert between offsets and positions */
readonly textDocument: TextDocument;
/** The current state of the document */
state: DocumentState;
/** The parse result holds the Abstract Syntax Tree (AST) and potentially also parser / lexer errors */
parseResult: ParseResult<T>;
/** Result of the scope precomputation phase */
precomputedScopes?: PrecomputedScopes;
/** An array of all cross-references found in the AST while linking */
references: Reference[];
/** Result of the validation phase */
diagnostics?: Diagnostic[]
}
/**
* A document is subject to several phases that are run in predefined order. Any state value implies that
* smaller state values are finished as well.
*/
export enum DocumentState {
/**
* The text content has changed and needs to be parsed again. The AST held by this outdated
* document instance is no longer valid.
*/
Changed = 0,
/**
* An AST has been created from the text content. The document structure can be traversed,
* but cross-references cannot be resolved yet. If necessary, the structure can be manipulated
* at this stage as a preprocessing step.
*/
Parsed = 1,
/**
* The `IndexManager` service has processed AST nodes of this document. This means the
* exported symbols are available in the global scope and can be resolved from other documents.
*/
IndexedContent = 2,
/**
* The `ScopeComputation` service has processed this document. This means the local symbols
* are stored in a MultiMap so they can be looked up by the `ScopeProvider` service.
* Once a document has reached this state, you may follow every reference - it will lazily
* resolve its `ref` property and yield either the target AST node or `undefined` in case
* the target is not in scope.
*/
ComputedScopes = 3,
/**
* The `Linker` service has processed this document. All outgoing references have been
* resolved or marked as erroneous.
*/
Linked = 4,
/**
* The `IndexManager` service has processed AST node references of this document. This is
* necessary to determine which documents are affected by a change in one of the workspace
* documents.
*/
IndexedReferences = 5,
/**
* The `DocumentValidator` service has processed this document. The language server listens
* to the results of this phase and sends diagnostics to the client.
*/
Validated = 6
}
/**
* Result of the scope precomputation phase (`ScopeComputation` service).
* It maps every AST node to the set of symbols that are visible in the subtree of that node.
*/
export type PrecomputedScopes = MultiMap<AstNode, AstNodeDescription>
export interface DocumentSegment {
readonly range: Range
readonly offset: number
readonly length: number
readonly end: number
}
/**
* Surrogate definition of the `TextDocuments` interface from the `vscode-languageserver` package.
* No implementation object is expected to be offered by `LangiumCoreServices`, but only by `LangiumLSPServices`.
*/
export type LangiumCoreTextDocuments = {
get(uri: string): TextDocument | undefined
}
/**
* Shared service for creating `LangiumDocument` instances.
*
* Register a custom implementation if special (additional) behavior is required for your language(s).
* Note: If you specialize {@link fromString} or {@link fromTextDocument} you probably might want to
* specialize {@link update}, too!
*/
export interface LangiumDocumentFactory {
/**
* Create a Langium document from a `TextDocument` (usually associated with a file).
*/
fromTextDocument<T extends AstNode = AstNode>(textDocument: TextDocument, uri?: URI): LangiumDocument<T>;
/**
* Create a Langium document from a `TextDocument` asynchronously. This action can be cancelled if a cancellable parser implementation has been provided.
*/
fromTextDocument<T extends AstNode = AstNode>(textDocument: TextDocument, uri: URI | undefined, cancellationToken: CancellationToken): Promise<LangiumDocument<T>>;
/**
* Create an Langium document from an in-memory string.
*/
fromString<T extends AstNode = AstNode>(text: string, uri: URI): LangiumDocument<T>;
/**
* Create a Langium document from an in-memory string asynchronously. This action can be cancelled if a cancellable parser implementation has been provided.
*/
fromString<T extends AstNode = AstNode>(text: string, uri: URI, cancellationToken: CancellationToken): Promise<LangiumDocument<T>>;
/**
* Create an Langium document from a model that has been constructed in memory.
*/
fromModel<T extends AstNode = AstNode>(model: T, uri: URI): LangiumDocument<T>;
/**
* Create an Langium document from a specified `URI`. The factory will use the `FileSystemAccess` service to read the file.
*/
fromUri<T extends AstNode = AstNode>(uri: URI, cancellationToken?: CancellationToken): Promise<LangiumDocument<T>>;
/**
* Update the given document after changes in the corresponding textual representation.
* Method is called by the document builder after it has been requested to build an existing
* document and the document's state is {@link DocumentState.Changed}.
* The text parsing is expected to be done the same way as in {@link fromTextDocument}
* and {@link fromString}.
*/
update<T extends AstNode = AstNode>(document: LangiumDocument<T>, cancellationToken: CancellationToken): Promise<LangiumDocument<T>>
}
export class DefaultLangiumDocumentFactory implements LangiumDocumentFactory {
protected readonly serviceRegistry: ServiceRegistry;
protected readonly textDocuments?: LangiumCoreTextDocuments;
protected readonly fileSystemProvider: FileSystemProvider;
constructor(services: LangiumSharedCoreServices) {
this.serviceRegistry = services.ServiceRegistry;
this.textDocuments = services.workspace.TextDocuments;
this.fileSystemProvider = services.workspace.FileSystemProvider;
}
async fromUri<T extends AstNode = AstNode>(uri: URI, cancellationToken = CancellationToken.None): Promise<LangiumDocument<T>> {
const content = await this.fileSystemProvider.readFile(uri);
return this.createAsync<T>(uri, content, cancellationToken);
}
fromTextDocument<T extends AstNode = AstNode>(textDocument: TextDocument, uri?: URI): LangiumDocument<T>;
fromTextDocument<T extends AstNode = AstNode>(textDocument: TextDocument, uri: URI | undefined, cancellationToken: CancellationToken): Promise<LangiumDocument<T>>;
fromTextDocument<T extends AstNode = AstNode>(textDocument: TextDocument, uri?: URI, cancellationToken?: CancellationToken): LangiumDocument<T> | Promise<LangiumDocument<T>> {
uri = uri ?? URI.parse(textDocument.uri);
if (cancellationToken) {
return this.createAsync<T>(uri, textDocument, cancellationToken);
} else {
return this.create<T>(uri, textDocument);
}
}
fromString<T extends AstNode = AstNode>(text: string, uri: URI): LangiumDocument<T>;
fromString<T extends AstNode = AstNode>(text: string, uri: URI, cancellationToken: CancellationToken): Promise<LangiumDocument<T>>;
fromString<T extends AstNode = AstNode>(text: string, uri: URI, cancellationToken?: CancellationToken): LangiumDocument<T> | Promise<LangiumDocument<T>> {
if (cancellationToken) {
return this.createAsync<T>(uri, text, cancellationToken);
} else {
return this.create<T>(uri, text);
}
}
fromModel<T extends AstNode = AstNode>(model: T, uri: URI): LangiumDocument<T> {
return this.create<T>(uri, { $model: model });
}
protected create<T extends AstNode = AstNode>(uri: URI, content: string | TextDocument | { $model: T }): LangiumDocument<T> {
if (typeof content === 'string') {
const parseResult = this.parse<T>(uri, content);
return this.createLangiumDocument<T>(parseResult, uri, undefined, content);
} else if ('$model' in content) {
const parseResult = { value: content.$model, parserErrors: [], lexerErrors: [] };
return this.createLangiumDocument<T>(parseResult, uri);
} else {
const parseResult = this.parse<T>(uri, content.getText());
return this.createLangiumDocument(parseResult, uri, content);
}
}
protected async createAsync<T extends AstNode = AstNode>(uri: URI, content: string | TextDocument, cancelToken: CancellationToken): Promise<LangiumDocument<T>> {
if (typeof content === 'string') {
const parseResult = await this.parseAsync<T>(uri, content, cancelToken);
return this.createLangiumDocument<T>(parseResult, uri, undefined, content);
} else {
const parseResult = await this.parseAsync<T>(uri, content.getText(), cancelToken);
return this.createLangiumDocument(parseResult, uri, content);
}
}
/**
* Create a LangiumDocument from a given parse result.
*
* A TextDocument is created on demand if it is not provided as argument here. Usually this
* should not be necessary because the main purpose of the TextDocument is to convert between
* text ranges and offsets, which is done solely in LSP request handling.
*
* With the introduction of {@link update} below this method is supposed to be mainly called
* during workspace initialization and on addition/recognition of new files, while changes in
* existing documents are processed via {@link update}.
*/
protected createLangiumDocument<T extends AstNode = AstNode>(parseResult: ParseResult<T>, uri: URI, textDocument?: TextDocument, text?: string): LangiumDocument<T> {
let document: LangiumDocument<T>;
if (textDocument) {
document = {
parseResult,
uri,
state: DocumentState.Parsed,
references: [],
textDocument
};
} else {
const textDocumentGetter = this.createTextDocumentGetter(uri, text);
document = {
parseResult,
uri,
state: DocumentState.Parsed,
references: [],
get textDocument() {
return textDocumentGetter();
}
};
}
(parseResult.value as Mutable<AstNode>).$document = document;
return document;
}
async update<T extends AstNode = AstNode>(document: Mutable<LangiumDocument<T>>, cancellationToken: CancellationToken): Promise<LangiumDocument<T>> {
// The CST full text property contains the original text that was used to create the AST.
const oldText = document.parseResult.value.$cstNode?.root.fullText;
const textDocument = this.textDocuments?.get(document.uri.toString());
const text = textDocument ? textDocument.getText() : await this.fileSystemProvider.readFile(document.uri);
if (textDocument) {
Object.defineProperty(
document,
'textDocument',
{
value: textDocument
}
);
} else {
const textDocumentGetter = this.createTextDocumentGetter(document.uri, text);
Object.defineProperty(
document,
'textDocument',
{
get: textDocumentGetter
}
);
}
// Some of these documents can be pretty large, so parsing them again can be quite expensive.
// Therefore, we only parse if the text has actually changed.
if (oldText !== text) {
document.parseResult = await this.parseAsync(document.uri, text, cancellationToken);
(document.parseResult.value as Mutable<AstNode>).$document = document;
}
document.state = DocumentState.Parsed;
return document;
}
protected parse<T extends AstNode>(uri: URI, text: string): ParseResult<T> {
const services = this.serviceRegistry.getServices(uri);
return services.parser.LangiumParser.parse<T>(text);
}
protected parseAsync<T extends AstNode>(uri: URI, text: string, cancellationToken: CancellationToken): Promise<ParseResult<T>> {
const services = this.serviceRegistry.getServices(uri);
return services.parser.AsyncParser.parse<T>(text, cancellationToken);
}
protected createTextDocumentGetter(uri: URI, text?: string): () => TextDocument {
const serviceRegistry = this.serviceRegistry;
let textDoc: TextDocument | undefined = undefined;
return () => {
return textDoc ??= TextDocument.create(
uri.toString(), serviceRegistry.getServices(uri).LanguageMetaData.languageId, 0, text ?? ''
);
};
}
}
/**
* Shared service for managing Langium documents.
*/
export interface LangiumDocuments {
/**
* A stream of all documents managed under this service.
*/
readonly all: Stream<LangiumDocument>
/**
* Manage a new document under this service.
* @throws an error if a document with the same URI is already present.
*/
addDocument(document: LangiumDocument): void;
/**
* Retrieve the document with the given URI, if present. Otherwise returns `undefined`.
*/
getDocument(uri: URI): LangiumDocument | undefined;
/**
* Retrieve the document with the given URI. If not present, a new one will be created using the file system access.
* The new document will be added to the list of documents managed under this service.
*/
getOrCreateDocument(uri: URI, cancellationToken?: CancellationToken): Promise<LangiumDocument>;
/**
* Creates a new document with the given URI and text content.
* The new document is automatically added to this service and can be retrieved using {@link getDocument}.
*
* @throws an error if a document with the same URI is already present.
*/
createDocument(uri: URI, text: string): LangiumDocument;
/**
* Creates a new document with the given URI and text content asynchronously.
* The process can be interrupted with a cancellation token.
* The new document is automatically added to this service and can be retrieved using {@link getDocument}.
*
* @throws an error if a document with the same URI is already present.
*/
createDocument(uri: URI, text: string, cancellationToken: CancellationToken): Promise<LangiumDocument>;
/**
* Returns `true` if a document with the given URI is managed under this service.
*/
hasDocument(uri: URI): boolean;
/**
* Flag the document with the given URI as `Changed`, if present, meaning that its content
* is no longer valid. The content (parseResult) stays untouched, while internal data may
* be dropped to reduce memory footprint.
*
* @returns the affected {@link LangiumDocument} if existing for convenience
*/
invalidateDocument(uri: URI): LangiumDocument | undefined;
/**
* Remove the document with the given URI, if present, and mark it as `Changed`, meaning
* that its content is no longer valid. The next call to `getOrCreateDocument` with the same
* URI will create a new document instance.
*
* @returns the affected {@link LangiumDocument} if existing for convenience
*/
deleteDocument(uri: URI): LangiumDocument | undefined;
}
export class DefaultLangiumDocuments implements LangiumDocuments {
protected readonly langiumDocumentFactory: LangiumDocumentFactory;
protected readonly documentMap: Map<string, LangiumDocument> = new Map();
constructor(services: LangiumSharedCoreServices) {
this.langiumDocumentFactory = services.workspace.LangiumDocumentFactory;
}
get all(): Stream<LangiumDocument> {
return stream(this.documentMap.values());
}
addDocument(document: LangiumDocument): void {
const uriString = document.uri.toString();
if (this.documentMap.has(uriString)) {
throw new Error(`A document with the URI '${uriString}' is already present.`);
}
this.documentMap.set(uriString, document);
}
getDocument(uri: URI): LangiumDocument | undefined {
const uriString = uri.toString();
return this.documentMap.get(uriString);
}
async getOrCreateDocument(uri: URI, cancellationToken?: CancellationToken): Promise<LangiumDocument> {
let document = this.getDocument(uri);
if (document) {
return document;
}
document = await this.langiumDocumentFactory.fromUri(uri, cancellationToken);
this.addDocument(document);
return document;
}
createDocument(uri: URI, text: string): LangiumDocument;
createDocument(uri: URI, text: string, cancellationToken: CancellationToken): Promise<LangiumDocument>;
createDocument(uri: URI, text: string, cancellationToken?: CancellationToken): LangiumDocument | Promise<LangiumDocument> {
if (cancellationToken) {
return this.langiumDocumentFactory.fromString(text, uri, cancellationToken).then(document => {
this.addDocument(document);
return document;
});
} else {
const document = this.langiumDocumentFactory.fromString(text, uri);
this.addDocument(document);
return document;
}
}
hasDocument(uri: URI): boolean {
return this.documentMap.has(uri.toString());
}
invalidateDocument(uri: URI): LangiumDocument | undefined {
const uriString = uri.toString();
const langiumDoc = this.documentMap.get(uriString);
if (langiumDoc) {
langiumDoc.state = DocumentState.Changed;
langiumDoc.precomputedScopes = undefined;
langiumDoc.references = [];
langiumDoc.diagnostics = undefined;
}
return langiumDoc;
}
deleteDocument(uri: URI): LangiumDocument | undefined {
const uriString = uri.toString();
const langiumDoc = this.documentMap.get(uriString);
if (langiumDoc) {
langiumDoc.state = DocumentState.Changed;
this.documentMap.delete(uriString);
}
return langiumDoc;
}
}