Skip to content

Commit

Permalink
Playground: Customize linking errors (#230)
Browse files Browse the repository at this point in the history
* Add warnings instead of errors when linking has failed
* Adjust wording
  • Loading branch information
Lotes authored May 23, 2024
1 parent 9baef3d commit 454c8ce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
28 changes: 28 additions & 0 deletions hugo/content/playground/user-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { AstNode, AstNodeDescription, AstUtils, DefaultDocumentValidator, DefaultLinker, DiagnosticInfo, DocumentState, DocumentValidator, LangiumDocument, LinkingError, LinkingErrorData, ReferenceInfo, ValidationOptions } from "langium";
import { LangiumServices } from "langium/lsp";
import { Diagnostic } from "vscode-languageserver";

export class PlaygroundValidator extends DefaultDocumentValidator {
constructor(services: LangiumServices) {
super(services);
}
protected override processLinkingErrors(document: LangiumDocument, diagnostics: Diagnostic[], _options: ValidationOptions): void {
for (const reference of document.references) {
const linkingError = reference.error;
if (linkingError) {
const info: DiagnosticInfo<AstNode, string> = {
node: linkingError.container,
property: linkingError.property,
index: linkingError.index,
data: {
code: DocumentValidator.LinkingError,
containerType: linkingError.container.$type,
property: linkingError.property,
refText: linkingError.reference.$refText
} satisfies LinkingErrorData
};
diagnostics.push(this.toDiagnostic('warning', `${linkingError.message}\nIn case you want to adjust the linking rules, please consult the learning section in the Langium documentation.`, info));
}
}
}
};
11 changes: 9 additions & 2 deletions hugo/content/playground/user-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import { NotificationType } from 'vscode-languageserver/browser.js';
import { DocumentChange, createServerConnection } from './worker-utils.js';
import { startLanguageServer } from 'langium/lsp';
import { LangiumServices, startLanguageServer } from 'langium/lsp';
import { DocumentState } from 'langium';
import { createServicesForGrammar } from 'langium/grammar';
import { PlaygroundValidator } from './user-validator.js';

// listen for messages to trigger starting the LS with a given grammar
addEventListener('message', async (event) => {
Expand Down Expand Up @@ -36,11 +37,17 @@ async function startWithGrammar(grammarText: string): Promise<void> {
// create shared services & serializer for the given grammar grammar
const { shared, serializer } = await createServicesForGrammar({
grammar: grammarText,
module: {
validation: {
DocumentValidator: (services: LangiumServices) => new PlaygroundValidator(services)
}
},
sharedModule: {

lsp: {
Connection: () => connection,
}
}
},
});

// listen for validated documents, and send the AST back to the language client
Expand Down

0 comments on commit 454c8ce

Please sign in to comment.