Skip to content
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

Add Separate Export for LSP Services #1258

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@
"block",
{ "pattern": "MIT License|DO NOT EDIT MANUALLY!" }
],
"no-restricted-imports": ["error", {
"paths": [{
"name": "vscode-jsonrpc",
"importNames": [ "CancellationToken" ],
"message": "Import 'CancellationToken' via 'Cancellation.CancellationToken' from 'langium', or directly from './utils/cancellation.ts' within Langium."
}, {
"name": "vscode-jsonrpc/",
"importNames": [ "CancellationToken"],
"message": "Import 'CancellationToken' via 'Cancellation.CancellationToken' from 'langium', or directly from './utils/cancellation.ts' within Langium."
}],
"patterns": [ {
"group": [ "vscode-jsonrpc" ],
"importNamePattern": "^(?!CancellationToken)",
"message": "Don't import types or symbols from 'vscode-jsonrpc' (package index), as that brings a large overhead in bundle size. Import from 'vscode-jsonrpc/lib/common/...js' and add a // eslint-disable..., if really necessary."
}]
}],
// List of [@typescript-eslint rules](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#supported-rules)
"@typescript-eslint/adjacent-overload-signatures": "error", // grouping same method names
"@typescript-eslint/array-type": ["error", { // string[] instead of Array<string>
Expand Down
6 changes: 3 additions & 3 deletions examples/arithmetics/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import type { AstNode, LangiumDocument, LangiumServices } from 'langium';
import type { AstNode, LangiumDocument, LangiumCoreServices } from 'langium';
import { URI } from 'langium';
import * as fs from 'node:fs';
import * as path from 'node:path';
import chalk from 'chalk';

export async function extractDocument<T extends AstNode>(fileName: string, extensions: readonly string[], services: LangiumServices): Promise<LangiumDocument<T>> {
export async function extractDocument<T extends AstNode>(fileName: string, extensions: readonly string[], services: LangiumCoreServices): Promise<LangiumDocument<T>> {
if (!extensions.includes(path.extname(fileName))) {
console.error(chalk.yellow(`Please, choose a file with one of these extensions: ${extensions}.`));
process.exit(1);
Expand Down Expand Up @@ -38,6 +38,6 @@ export async function extractDocument<T extends AstNode>(fileName: string, exten
return document as LangiumDocument<T>;
}

export async function extractAstNode<T extends AstNode>(fileName: string, extensions: string[], services: LangiumServices): Promise<T> {
export async function extractAstNode<T extends AstNode>(fileName: string, extensions: string[], services: LangiumCoreServices): Promise<T> {
return (await extractDocument(fileName, extensions, services)).parseResult.value as T;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import type { DefaultSharedModuleContext, LangiumServices, LangiumSharedServices, Module, PartialLangiumServices } from 'langium';
import { createDefaultModule, createDefaultSharedModule, inject } from 'langium';
import { ArithmeticsGeneratedModule, ArithmeticsGeneratedSharedModule } from './generated/module.js';
import { ArithmeticsValidator, registerValidationChecks } from './arithmetics-validator.js';
import { type Module, inject } from 'langium';
import { createDefaultModule, createDefaultSharedModule, type DefaultSharedModuleContext, type LangiumServices, type LangiumSharedServices, type PartialLangiumServices } from 'langium/lsp';
import { ArithmeticsScopeProvider } from './arithmetics-scope-provider.js';
import { ArithmeticsValidator, registerValidationChecks } from './arithmetics-validator.js';
import { ArithmeticsGeneratedModule, ArithmeticsGeneratedSharedModule } from './generated/module.js';

/**
* Declaration of custom services - add your own service classes here.
Expand Down
7 changes: 4 additions & 3 deletions examples/arithmetics/src/language-server/generated/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* DO NOT EDIT MANUALLY!
******************************************************************************/

import type { LangiumGeneratedServices, LangiumGeneratedSharedServices, LangiumSharedServices, LangiumServices, LanguageMetaData, Module } from 'langium';
import type { LangiumGeneratedCoreServices, LangiumGeneratedSharedCoreServices, LanguageMetaData, Module } from 'langium';
import type { LangiumSharedServices, LangiumServices } from 'langium/lsp';
import { ArithmeticsAstReflection } from './ast.js';
import { ArithmeticsGrammar } from './grammar.js';

Expand All @@ -13,11 +14,11 @@ export const ArithmeticsLanguageMetaData = {
caseInsensitive: true
} as const satisfies LanguageMetaData;

export const ArithmeticsGeneratedSharedModule: Module<LangiumSharedServices, LangiumGeneratedSharedServices> = {
export const ArithmeticsGeneratedSharedModule: Module<LangiumSharedServices, LangiumGeneratedSharedCoreServices> = {
AstReflection: () => new ArithmeticsAstReflection()
};

export const ArithmeticsGeneratedModule: Module<LangiumServices, LangiumGeneratedServices> = {
export const ArithmeticsGeneratedModule: Module<LangiumServices, LangiumGeneratedCoreServices> = {
Grammar: () => ArithmeticsGrammar(),
LanguageMetaData: () => ArithmeticsLanguageMetaData,
parser: {}
Expand Down
5 changes: 3 additions & 2 deletions examples/arithmetics/src/language-server/main-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
******************************************************************************/

import type { Diagnostic, Range } from 'vscode-languageserver/browser.js';
import { startLanguageServer, EmptyFileSystem, DocumentState } from 'langium';
import { EmptyFileSystem, DocumentState } from 'langium';
import { startLanguageServer } from 'langium/lsp';
import { BrowserMessageReader, BrowserMessageWriter, createConnection, NotificationType } from 'vscode-languageserver/browser.js';
import { createArithmeticsServices } from './arithmetics-module.js';
import { interpretEvaluations } from './arithmetics-evaluator.js';
Expand Down Expand Up @@ -53,4 +54,4 @@ shared.workspace.DocumentBuilder.onBuildPhase(DocumentState.Validated, documents
diagnostics: document.diagnostics ?? []
});
}
});
});
2 changes: 1 addition & 1 deletion examples/arithmetics/src/language-server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import { startLanguageServer } from 'langium';
import { startLanguageServer } from 'langium/lsp';
import { NodeFileSystem } from 'langium/node';
import { createConnection, ProposedFeatures } from 'vscode-languageserver/node.js';
import { createArithmeticsServices } from './arithmetics-module.js';
Expand Down
13 changes: 6 additions & 7 deletions examples/domainmodel/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import type { AstNode, LangiumDocument, LangiumServices } from 'langium';
import type { WorkspaceFolder } from 'vscode-languageserver';
import * as fs from 'node:fs';
import * as path from 'node:path';
import chalk from 'chalk';
import type { AstNode, LangiumCoreServices, LangiumDocument, WorkspaceFolder } from 'langium';
import { URI } from 'langium';
import * as fs from 'node:fs';
import * as path from 'node:path';

export async function extractDocument<T extends AstNode>(fileName: string, extensions: readonly string[], services: LangiumServices): Promise<LangiumDocument<T>> {
export async function extractDocument<T extends AstNode>(fileName: string, extensions: readonly string[], services: LangiumCoreServices): Promise<LangiumDocument<T>> {
if (!extensions.includes(path.extname(fileName))) {
console.error(chalk.yellow(`Please, choose a file with one of these extensions: ${extensions}.`));
process.exit(1);
Expand Down Expand Up @@ -39,11 +38,11 @@ export async function extractDocument<T extends AstNode>(fileName: string, exten
return document as LangiumDocument<T>;
}

export async function extractAstNode<T extends AstNode>(fileName: string, extensions: readonly string[], services: LangiumServices): Promise<T> {
export async function extractAstNode<T extends AstNode>(fileName: string, extensions: readonly string[], services: LangiumCoreServices): Promise<T> {
return (await extractDocument(fileName, extensions, services)).parseResult.value as T;
}

export async function setRootFolder(fileName: string, services: LangiumServices, root?: string): Promise<void> {
export async function setRootFolder(fileName: string, services: LangiumCoreServices, root?: string): Promise<void> {
if (!root) {
root = path.dirname(fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
******************************************************************************/

import type { AstNode } from 'langium';
import { AbstractFormatter, Formatting } from 'langium';
import { AbstractFormatter, Formatting } from 'langium/lsp';
import * as ast from './generated/ast.js';

export class DomainModelFormatter extends AbstractFormatter {
Expand Down
15 changes: 8 additions & 7 deletions examples/domainmodel/src/language-server/domain-model-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import type { LangiumServices, Module, PartialLangiumServices, LangiumSharedServices, DefaultSharedModuleContext } from 'langium';
import { inject, createDefaultSharedModule, createDefaultModule } from 'langium';
import { DomainModelGeneratedModule, DomainModelGeneratedSharedModule } from './generated/module.js';
import { DomainModelValidator, registerValidationChecks } from './domain-model-validator.js';
import { DomainModelScopeComputation } from './domain-model-scope.js';
import { QualifiedNameProvider } from './domain-model-naming.js';
import { type Module, inject } from 'langium';
import type { LangiumServices, LangiumSharedServices, PartialLangiumServices } from 'langium/lsp';
import { createDefaultModule, createDefaultSharedModule, type DefaultSharedModuleContext } from 'langium/lsp';
import { DomainModelFormatter } from './domain-model-formatter.js';
import { QualifiedNameProvider } from './domain-model-naming.js';
import { DomainModelRenameProvider } from './domain-model-rename-refactoring.js';
import { DomainModelScopeComputation } from './domain-model-scope.js';
import { DomainModelValidator, registerValidationChecks } from './domain-model-validator.js';
import { DomainModelGeneratedModule, DomainModelGeneratedSharedModule } from './generated/module.js';

export type DomainModelAddedServices = {
references: {
Expand All @@ -22,7 +23,7 @@ export type DomainModelAddedServices = {
}
}

export type DomainModelServices = LangiumServices & DomainModelAddedServices
export type DomainModelServices = LangiumServices & DomainModelAddedServices;

export const DomainModelModule: Module<DomainModelServices, PartialLangiumServices & DomainModelAddedServices> = {
references: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
******************************************************************************/

import type { AstNode, LangiumDocument, LangiumDocuments, ReferenceDescription, URI } from 'langium';
import type { Range, WorkspaceEdit } from 'vscode-languageserver';
import { AstUtils, CstUtils, isNamed } from 'langium';
import { DefaultRenameProvider } from 'langium/lsp';
import type { RenameParams } from 'vscode-languageserver-protocol';
import { Location, TextEdit, type Range, type WorkspaceEdit } from 'vscode-languageserver-types';
import type { DomainModelServices } from './domain-model-module.js';
import type { QualifiedNameProvider } from './domain-model-naming.js';
import { AstUtils, CstUtils, DefaultRenameProvider, isNamed } from 'langium';
import { Location } from 'vscode-languageserver';
import { TextEdit } from 'vscode-languageserver-types';
import { isPackageDeclaration } from './generated/ast.js';

export class DomainModelRenameProvider extends DefaultRenameProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import type { AstNode, AstNodeDescription, LangiumDocument, PrecomputedScopes }
import type { DomainModelServices } from './domain-model-module.js';
import type { QualifiedNameProvider } from './domain-model-naming.js';
import type { Domainmodel, PackageDeclaration } from './generated/ast.js';
import { AstUtils, DefaultScopeComputation, interruptAndCheck, MultiMap } from 'langium';
import { CancellationToken } from 'vscode-jsonrpc';
import { AstUtils, Cancellation, DefaultScopeComputation, interruptAndCheck, MultiMap } from 'langium';
import { isType, isPackageDeclaration } from './generated/ast.js';

export class DomainModelScopeComputation extends DefaultScopeComputation {
Expand All @@ -24,7 +23,7 @@ export class DomainModelScopeComputation extends DefaultScopeComputation {
/**
* Exports only types (`DataType or `Entity`) with their qualified names.
*/
override async computeExports(document: LangiumDocument, cancelToken = CancellationToken.None): Promise<AstNodeDescription[]> {
override async computeExports(document: LangiumDocument, cancelToken = Cancellation.CancellationToken.None): Promise<AstNodeDescription[]> {
spoenemann marked this conversation as resolved.
Show resolved Hide resolved
const descr: AstNodeDescription[] = [];
for (const modelNode of AstUtils.streamAllContents(document.parseResult.value)) {
await interruptAndCheck(cancelToken);
Expand All @@ -41,14 +40,14 @@ export class DomainModelScopeComputation extends DefaultScopeComputation {
return descr;
}

override async computeLocalScopes(document: LangiumDocument, cancelToken = CancellationToken.None): Promise<PrecomputedScopes> {
override async computeLocalScopes(document: LangiumDocument, cancelToken = Cancellation.CancellationToken.None): Promise<PrecomputedScopes> {
const model = document.parseResult.value as Domainmodel;
const scopes = new MultiMap<AstNode, AstNodeDescription>();
await this.processContainer(model, scopes, document, cancelToken);
return scopes;
}

protected async processContainer(container: Domainmodel | PackageDeclaration, scopes: PrecomputedScopes, document: LangiumDocument, cancelToken: CancellationToken): Promise<AstNodeDescription[]> {
protected async processContainer(container: Domainmodel | PackageDeclaration, scopes: PrecomputedScopes, document: LangiumDocument, cancelToken: Cancellation.CancellationToken): Promise<AstNodeDescription[]> {
const localDescriptions: AstNodeDescription[] = [];
for (const element of container.elements) {
await interruptAndCheck(cancelToken);
Expand Down
7 changes: 4 additions & 3 deletions examples/domainmodel/src/language-server/generated/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* DO NOT EDIT MANUALLY!
******************************************************************************/

import type { LangiumGeneratedServices, LangiumGeneratedSharedServices, LangiumSharedServices, LangiumServices, LanguageMetaData, Module, IParserConfig } from 'langium';
import type { LangiumGeneratedCoreServices, LangiumGeneratedSharedCoreServices, LanguageMetaData, Module, IParserConfig } from 'langium';
import type { LangiumSharedServices, LangiumServices } from 'langium/lsp';
import { DomainModelAstReflection } from './ast.js';
import { DomainModelGrammar } from './grammar.js';

Expand All @@ -19,11 +20,11 @@ export const parserConfig: IParserConfig = {
maxLookahead: 3,
};

export const DomainModelGeneratedSharedModule: Module<LangiumSharedServices, LangiumGeneratedSharedServices> = {
export const DomainModelGeneratedSharedModule: Module<LangiumSharedServices, LangiumGeneratedSharedCoreServices> = {
AstReflection: () => new DomainModelAstReflection()
};

export const DomainModelGeneratedModule: Module<LangiumServices, LangiumGeneratedServices> = {
export const DomainModelGeneratedModule: Module<LangiumServices, LangiumGeneratedCoreServices> = {
Grammar: () => DomainModelGrammar(),
LanguageMetaData: () => DomainModelLanguageMetaData,
parser: {
Expand Down
3 changes: 2 additions & 1 deletion examples/domainmodel/src/language-server/main-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
******************************************************************************/

import type { Diagnostic } from 'vscode-languageserver/browser.js';
import { startLanguageServer, EmptyFileSystem, DocumentState } from 'langium';
import { EmptyFileSystem, DocumentState } from 'langium';
import { startLanguageServer } from 'langium/lsp';
import { BrowserMessageReader, BrowserMessageWriter, createConnection, NotificationType } from 'vscode-languageserver/browser.js';
import { createDomainModelServices } from './domain-model-module.js';

Expand Down
2 changes: 1 addition & 1 deletion examples/domainmodel/src/language-server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import { startLanguageServer } from 'langium';
import { startLanguageServer } from 'langium/lsp';
import { NodeFileSystem } from 'langium/node';
import { createConnection, ProposedFeatures } from 'vscode-languageserver/node.js';
import { createDomainModelServices } from './domain-model-module.js';
Expand Down
Loading
Loading