-
Notifications
You must be signed in to change notification settings - Fork 86
Support discarding the CST to preserve memory #1733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ import type { GrammarConfig } from '../languages/grammar-config.js'; | |
| import type { NameProvider } from '../references/name-provider.js'; | ||
| import type { References } from '../references/references.js'; | ||
| import type { LangiumServices } from './lsp-services.js'; | ||
| import type { CstNode } from '../syntax-tree.js'; | ||
| import type { AstNode, CstNode } from '../syntax-tree.js'; | ||
| import type { MaybePromise } from '../utils/promise-utils.js'; | ||
| import type { LangiumDocument } from '../workspace/documents.js'; | ||
| import { LocationLink } from 'vscode-languageserver'; | ||
|
|
@@ -37,7 +37,7 @@ export interface DefinitionProvider { | |
|
|
||
| export interface GoToLink { | ||
| source: CstNode | ||
| target: CstNode | ||
| target: AstNode | ||
| targetDocument: LangiumDocument | ||
| } | ||
|
|
||
|
|
@@ -67,21 +67,25 @@ export class DefaultDefinitionProvider implements DefinitionProvider { | |
|
|
||
| protected collectLocationLinks(sourceCstNode: CstNode, _params: DefinitionParams): MaybePromise<LocationLink[] | undefined> { | ||
| const goToLink = this.findLink(sourceCstNode); | ||
| if (goToLink) { | ||
| return [LocationLink.create( | ||
| goToLink.targetDocument.textDocument.uri, | ||
| (goToLink.target.astNode.$cstNode ?? goToLink.target).range, | ||
| goToLink.target.range, | ||
| goToLink.source.range | ||
| )]; | ||
| if (goToLink && goToLink.target.$segments) { | ||
| const name = this.nameProvider.getNameProperty(goToLink.target); | ||
| if (name) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we use the whole composite node as target in case a name is not available? |
||
| const nameSegment = goToLink.target.$segments.properties.get(name); | ||
| return nameSegment.map(segment => LocationLink.create( | ||
| goToLink.targetDocument.textDocument.uri, | ||
| goToLink.target.$segments!.full.range, | ||
| segment.range, | ||
| goToLink.source.range | ||
| )); | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| protected findLink(source: CstNode): GoToLink | undefined { | ||
| const target = this.references.findDeclarationNode(source); | ||
| if (target?.astNode) { | ||
| const targetDocument = getDocument(target.astNode); | ||
| const target = this.references.findDeclaration(source); | ||
| if (target) { | ||
| const targetDocument = getDocument(target); | ||
| if (target && targetDocument) { | ||
| return { source, target, targetDocument }; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,17 +4,18 @@ | |
| * terms of the MIT License, which is available in the project root. | ||
| ******************************************************************************/ | ||
|
|
||
| import type { TextDocumentWillSaveEvent, DidChangeWatchedFilesParams, DidChangeWatchedFilesRegistrationOptions, TextDocumentChangeEvent, TextEdit } from 'vscode-languageserver'; | ||
| import type { TextDocumentWillSaveEvent, DidChangeWatchedFilesParams, DidChangeWatchedFilesRegistrationOptions, TextDocumentChangeEvent, TextEdit, Connection } from 'vscode-languageserver'; | ||
| import { DidChangeWatchedFilesNotification, FileChangeType } from 'vscode-languageserver'; | ||
| import { stream } from '../utils/stream.js'; | ||
| import { URI } from '../utils/uri-utils.js'; | ||
| import type { DocumentBuilder } from '../workspace/document-builder.js'; | ||
| import type { TextDocument } from '../workspace/documents.js'; | ||
| import type { LangiumDocuments, TextDocument } from '../workspace/documents.js'; | ||
| import type { WorkspaceLock } from '../workspace/workspace-lock.js'; | ||
| import type { LangiumSharedServices } from './lsp-services.js'; | ||
| import type { WorkspaceManager } from '../workspace/workspace-manager.js'; | ||
| import type { ServiceRegistry } from '../service-registry.js'; | ||
| import type { MaybePromise } from '../utils/promise-utils.js'; | ||
| import { discardCst } from '../utils/cst-utils.js'; | ||
|
|
||
| /** | ||
| * Shared service for handling text document changes and watching relevant files. | ||
|
|
@@ -71,13 +72,17 @@ export class DefaultDocumentUpdateHandler implements DocumentUpdateHandler { | |
| protected readonly workspaceManager: WorkspaceManager; | ||
| protected readonly documentBuilder: DocumentBuilder; | ||
| protected readonly workspaceLock: WorkspaceLock; | ||
| protected readonly documents: LangiumDocuments; | ||
| protected readonly connection: Connection | undefined; | ||
| protected readonly serviceRegistry: ServiceRegistry; | ||
|
|
||
| constructor(services: LangiumSharedServices) { | ||
| this.workspaceManager = services.workspace.WorkspaceManager; | ||
| this.documentBuilder = services.workspace.DocumentBuilder; | ||
| this.workspaceLock = services.workspace.WorkspaceLock; | ||
| this.serviceRegistry = services.ServiceRegistry; | ||
| this.documents = services.workspace.LangiumDocuments; | ||
| this.connection = services.lsp.Connection; | ||
|
|
||
| let canRegisterFileWatcher = false; | ||
| services.lsp.LanguageServer.onInitialize(params => { | ||
|
|
@@ -98,15 +103,14 @@ export class DefaultDocumentUpdateHandler implements DocumentUpdateHandler { | |
| .distinct() | ||
| .toArray(); | ||
| if (fileExtensions.length > 0) { | ||
| const connection = services.lsp.Connection; | ||
| const options: DidChangeWatchedFilesRegistrationOptions = { | ||
| watchers: [{ | ||
| globPattern: fileExtensions.length === 1 | ||
| ? `**/*.${fileExtensions[0]}` | ||
| : `**/*.{${fileExtensions.join(',')}}` | ||
| }] | ||
| }; | ||
| connection?.client.register(DidChangeWatchedFilesNotification.type, options); | ||
| this.connection?.client.register(DidChangeWatchedFilesNotification.type, options); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -141,4 +145,18 @@ export class DefaultDocumentUpdateHandler implements DocumentUpdateHandler { | |
| .toArray(); | ||
| this.fireDocumentUpdate(changedUris, deletedUris); | ||
| } | ||
|
|
||
| didCloseDocument(event: TextDocumentChangeEvent<TextDocument>): void { | ||
| const document = this.documents.getDocument(URI.parse(event.document.uri)); | ||
| if (document) { | ||
| // Preserve memory by discarding the CST of the document | ||
| // Whenever the user reopens the document, the CST will be rebuilt | ||
| discardCst(document.parseResult.value); | ||
| } | ||
| // Discard the diagnostics for the closed document | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we make this configurable (without method overriding)? It could be simply a public property on the service class to turn this behavior on or off? I imagine there are users/language implementors who would like to keep all problem markers even for closed documents. |
||
| this.connection?.sendDiagnostics({ | ||
| uri: event.document.uri, | ||
| diagnostics: [] | ||
| }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.