Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
splits up config, docs, and workspace services into lsp/non-lsp versions
- Loading branch information
Showing
14 changed files
with
411 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/langium/src/lsp/workspace/default-configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/****************************************************************************** | ||
* Copyright 2023 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. | ||
******************************************************************************/ | ||
|
||
import type { Connection, DidChangeConfigurationParams } from 'vscode-languageserver'; | ||
import type { ConfigurationItem } from 'vscode-languageserver-protocol'; | ||
import type { ServiceRegistry } from '../../service-registry.js'; | ||
import type { LangiumSharedServices } from '../../services.js'; | ||
import { DidChangeConfigurationNotification } from 'vscode-languageserver-protocol'; | ||
import type { ConfigurationProvider } from '../../workspace/configuration.js'; | ||
import type { LangiumSharedLSPServices } from '../lsp-services.js'; | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
// TODO needs a block still | ||
export class LSPConfigurationProvider implements ConfigurationProvider { | ||
|
||
protected settings: Record<string, Record<string, any>> = {}; | ||
protected workspaceConfig = false; | ||
protected initialized = false; | ||
protected readonly serviceRegistry: ServiceRegistry; | ||
protected readonly connection: Connection | undefined; | ||
|
||
constructor(services: LangiumSharedServices & LangiumSharedLSPServices) { | ||
this.serviceRegistry = services.ServiceRegistry; | ||
this.connection = services.lsp.Connection; | ||
services.lsp.LanguageServer.onInitialize(params => { | ||
this.workspaceConfig = params.capabilities.workspace?.configuration ?? false; | ||
}); | ||
services.lsp.LanguageServer.onInitialized(_params => { | ||
const languages = this.serviceRegistry.all; | ||
services.lsp.Connection?.client.register(DidChangeConfigurationNotification.type, { | ||
// Listen to configuration changes for all languages | ||
section: languages.map(lang => this.toSectionName(lang.LanguageMetaData.languageId)) | ||
}); | ||
}); | ||
} | ||
|
||
protected async initialize(): Promise<void> { | ||
if (this.workspaceConfig && this.connection) { | ||
const languages = this.serviceRegistry.all; | ||
const configToUpdate: ConfigurationItem[] = languages.map(lang => { return { section: this.toSectionName(lang.LanguageMetaData.languageId) }; }); | ||
// get workspace configurations (default scope URI) | ||
const configs = await this.connection.workspace.getConfiguration(configToUpdate); | ||
configToUpdate.forEach((conf, idx) => { | ||
this.updateSectionConfiguration(conf.section!, configs[idx]); | ||
}); | ||
} | ||
this.initialized = true; | ||
} | ||
|
||
updateConfiguration(change: DidChangeConfigurationParams): void { | ||
if (!change.settings) { | ||
return; | ||
} | ||
Object.keys(change.settings).forEach(section => { | ||
this.updateSectionConfiguration(section, change.settings[section]); | ||
}); | ||
} | ||
|
||
protected updateSectionConfiguration(section: string, configuration: any): void { | ||
this.settings[section] = configuration; | ||
} | ||
|
||
async getConfiguration(language: string, configuration: string): Promise<any> { | ||
if (!this.initialized) { | ||
await this.initialize(); | ||
} | ||
const sectionName = this.toSectionName(language); | ||
if (this.settings[sectionName]) { | ||
return this.settings[sectionName][configuration]; | ||
} | ||
} | ||
|
||
protected toSectionName(languageId: string): string { | ||
return `${languageId}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/****************************************************************************** | ||
* Copyright 2023 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. | ||
******************************************************************************/ | ||
|
||
import type { TextDocuments } from 'vscode-languageserver'; | ||
import type { ParseResult } from '../../parser/langium-parser.js'; | ||
import type { ServiceRegistry } from '../../service-registry.js'; | ||
import type { LangiumSharedServices } from '../../services.js'; | ||
import type { AstNode } from '../../syntax-tree.js'; | ||
import type { Mutable } from '../../utils/ast-util.js'; | ||
import type { FileSystemProvider } from './../../workspace/file-system-provider.js'; | ||
import { DocumentState, type LangiumDocument, type LangiumDocumentFactory } from '../../workspace/documents.js'; | ||
import { URI } from '../../utils/uri-util.js'; | ||
import { TextDocument } from 'vscode-languageserver-textdocument'; | ||
import type { LangiumSharedLSPServices } from '../lsp-services.js'; | ||
|
||
// TODO needs a block still | ||
export class LSPLangiumDocumentFactory implements LangiumDocumentFactory { | ||
|
||
protected readonly serviceRegistry: ServiceRegistry; | ||
protected readonly textDocuments: TextDocuments<TextDocument>; | ||
protected readonly fileSystemProvider: FileSystemProvider; | ||
|
||
constructor(services: LangiumSharedServices & LangiumSharedLSPServices) { | ||
this.serviceRegistry = services.ServiceRegistry; | ||
this.textDocuments = services.workspace.TextDocuments; | ||
this.fileSystemProvider = services.workspace.FileSystemProvider; | ||
} | ||
|
||
fromTextDocument<T extends AstNode = AstNode>(textDocument: TextDocument, uri?: URI): LangiumDocument<T> { | ||
return this.create<T>(uri ?? URI.parse(textDocument.uri), textDocument); | ||
} | ||
|
||
fromString<T extends AstNode = AstNode>(text: string, uri: URI): LangiumDocument<T> { | ||
return this.create<T>(uri, text); | ||
} | ||
|
||
fromModel<T extends AstNode = AstNode>(model: T, uri: URI): LangiumDocument<T> { | ||
return this.create<T>(uri, { $model: model }); | ||
} | ||
|
||
create<T extends AstNode = AstNode>(uri: URI, content?: string | TextDocument | { $model: T }): LangiumDocument<T> { | ||
// if no document is given, check the textDocuments service first, it maintains documents being opened in an editor | ||
content ??= this.textDocuments.get(uri.toString()); | ||
// if still no document is found try to load it from the file system | ||
content ??= this.getContentFromFileSystem(uri); | ||
|
||
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); | ||
} | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
update<T extends AstNode = AstNode>(document: Mutable<LangiumDocument<T>>): LangiumDocument<T> { | ||
const textDocument = this.textDocuments.get(document.uri.toString()); | ||
const text = textDocument ? textDocument.getText() : this.getContentFromFileSystem(document.uri); | ||
|
||
if (textDocument) { | ||
Object.defineProperty( | ||
document, 'textDocument', | ||
{ | ||
value: textDocument | ||
} | ||
); | ||
} else { | ||
const textDocumentGetter = this.createTextDocumentGetter(document.uri, text); | ||
Object.defineProperty( | ||
document, 'textDocument', | ||
{ | ||
get: textDocumentGetter | ||
} | ||
); | ||
} | ||
|
||
document.parseResult = this.parse(document.uri, text); | ||
(document.parseResult.value as Mutable<AstNode>).$document = document; | ||
return document; | ||
} | ||
|
||
protected getContentFromFileSystem(uri: URI): string { | ||
return this.fileSystemProvider.readFileSync(uri); | ||
} | ||
|
||
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 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 ?? '' | ||
); | ||
}; | ||
} | ||
} |
Oops, something went wrong.