Skip to content

Commit

Permalink
switched to Langium v3.3, simplified API, removed duplicated code for…
Browse files Browse the repository at this point in the history
… a fix which is part of Langium 3.3 now
  • Loading branch information
JohannesMeierSE committed Dec 8, 2024
1 parent 5389732 commit 429c996
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 75 deletions.
4 changes: 2 additions & 2 deletions examples/lox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
},
"dependencies": {
"commander": "~12.1.0",
"langium": "~3.2.0",
"langium": "~3.3.0",
"typir-langium": "~0.0.2",
"vscode-languageclient": "~9.0.1",
"vscode-languageserver": "~9.0.1"
},
"devDependencies": {
"@types/vscode": "~1.94.0",
"langium-cli": "~3.2.0"
"langium-cli": "~3.3.0"
},
"files": [
"bin",
Expand Down
4 changes: 2 additions & 2 deletions examples/ox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
},
"dependencies": {
"commander": "~12.1.0",
"langium": "~3.2.0",
"langium": "^3.3.0",
"typir-langium": "~0.0.2",
"vscode-languageclient": "~9.0.1",
"vscode-languageserver": "~9.0.1"
},
"devDependencies": {
"@types/vscode": "~1.94.0",
"langium-cli": "~3.2.0"
"langium-cli": "^3.3.0"
},
"files": [
"bin",
Expand Down
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/typir-langium/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"bugs": "https://github.com/TypeFox/typir/issues",
"dependencies": {
"langium": "~3.2.0",
"langium": "~3.3.0",
"typir": "~0.0.2"
}
}
44 changes: 1 addition & 43 deletions packages/typir-langium/src/features/langium-caching.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 { AstNode, ContextCache, DocumentState, LangiumSharedCoreServices, URI } from 'langium';
import { AstNode, DocumentCache, DocumentState, LangiumSharedCoreServices } from 'langium';
import { CachePending, DomainElementInferenceCaching, Type } from 'typir';
import { getDocumentKey } from '../utils/typir-langium-utils.js';

Expand Down Expand Up @@ -47,45 +47,3 @@ export class LangiumDomainElementInferenceCaching implements DomainElementInfere
return this.cache.has(key, domainElement) && this.cache.get(key, domainElement) === CachePending;
}
}


// TODO this is copied from Langium, since the introducing PR #1659 will be included in the upcoming Langium version 3.3 (+ PR #1712), after releasing v3.3 this class can be removed completely!
/**
* Every key/value pair in this cache is scoped to a document.
* If this document is changed or deleted, all associated key/value pairs are deleted.
*/
export class DocumentCache<K, V> extends ContextCache<URI | string, K, V, string> {

/**
* Creates a new document cache.
*
* @param sharedServices Service container instance to hook into document lifecycle events.
* @param state Optional document state on which the cache should evict.
* If not provided, the cache will evict on `DocumentBuilder#onUpdate`.
* *Deleted* documents are considered in both cases.
*
* Providing a state here will use `DocumentBuilder#onDocumentPhase` instead,
* which triggers on all documents that have been affected by this change, assuming that the
* state is `DocumentState.Linked` or a later state.
*/
constructor(sharedServices: LangiumSharedCoreServices, state?: DocumentState) {
super(uri => uri.toString());
if (state) {
this.toDispose.push(sharedServices.workspace.DocumentBuilder.onDocumentPhase(state, document => {
this.clear(document.uri.toString());
}));
this.toDispose.push(sharedServices.workspace.DocumentBuilder.onUpdate((_changed, deleted) => {
for (const uri of deleted) { // react only on deleted documents
this.clear(uri);
}
}));
} else {
this.toDispose.push(sharedServices.workspace.DocumentBuilder.onUpdate((changed, deleted) => {
const allUris = changed.concat(deleted); // react on both changed and deleted documents
for (const uri of allUris) {
this.clear(uri);
}
}));
}
}
}
35 changes: 25 additions & 10 deletions packages/typir-langium/src/features/langium-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import { AstNode, AstUtils, LangiumDefaultCoreServices, ValidationAcceptor, ValidationChecks } from 'langium';
import { AstNode, LangiumDefaultCoreServices, ValidationAcceptor, ValidationChecks } from 'langium';
import { TypirServices, ValidationProblem } from 'typir';
import { LangiumServicesForTypirBinding } from '../typir-langium.js';

export function registerTypirValidationChecks(services: LangiumDefaultCoreServices & LangiumServicesForTypirBinding) {
const registry = services.validation.ValidationRegistry;
const validator = services.TypeValidation;
registry.registerBeforeDocument(validator.checkTypingProblemsWithTypirBeforeDocument, validator);
const checks: ValidationChecks<object> = {
AstNode: validator.checkTypingProblemsWithTypir, // checking each node is not performant, improve the API, see below!
};
registry.register(checks, validator);
registry.registerAfterDocument(validator.checkTypingProblemsWithTypirAfterDocument, validator);
}

/*
Expand All @@ -38,13 +40,27 @@ export function registerTypirValidationChecks(services: LangiumDefaultCoreServic
*/

export interface LangiumTypirValidator {
/**
* Will be called once before starting the validation of a LangiumDocument.
* @param rootNode the root node of the current document
* @param accept receives the found validation hints
*/
checkTypingProblemsWithTypirBeforeDocument(rootNode: AstNode, accept: ValidationAcceptor): void;

/**
* Executes all checks, which are directly derived from the current Typir configuration,
* i.e. checks that arguments fit to parameters for function calls (including operands for operators).
* @param node the current AST node to check regarding typing issues
* @param accept receives the found validation hints
*/
checkTypingProblemsWithTypir(node: AstNode, accept: ValidationAcceptor): void;

/**
* Will be called once after finishing the validation of a LangiumDocument.
* @param rootNode the root node of the current document
* @param accept receives the found validation hints
*/
checkTypingProblemsWithTypirAfterDocument(rootNode: AstNode, accept: ValidationAcceptor): void;
}

export class DefaultLangiumTypirValidator implements LangiumTypirValidator {
Expand All @@ -54,17 +70,16 @@ export class DefaultLangiumTypirValidator implements LangiumTypirValidator {
this.services = services;
}

checkTypingProblemsWithTypir(node: AstNode, accept: ValidationAcceptor) {
// TODO use the new validation registry API in Langium v3.3 instead!
if (node.$container === undefined) {
this.report(this.services.validation.collector.validateBefore(node), node, accept);
checkTypingProblemsWithTypirBeforeDocument(rootNode: AstNode, accept: ValidationAcceptor): void {
this.report(this.services.validation.collector.validateBefore(rootNode), rootNode, accept);
}

AstUtils.streamAst(node).forEach(child => {
this.report(this.services.validation.collector.validate(child), child, accept);
});
checkTypingProblemsWithTypir(node: AstNode, accept: ValidationAcceptor) {
this.report(this.services.validation.collector.validate(node), node, accept);
}

this.report(this.services.validation.collector.validateAfter(node), node, accept);
}
checkTypingProblemsWithTypirAfterDocument(rootNode: AstNode, accept: ValidationAcceptor): void {
this.report(this.services.validation.collector.validateAfter(rootNode), rootNode, accept);
}

protected report(problems: ValidationProblem[], node: AstNode, accept: ValidationAcceptor): void {
Expand Down

0 comments on commit 429c996

Please sign in to comment.