Skip to content
Open
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
27 changes: 19 additions & 8 deletions hugo/content/playground/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { render } from './Tree.js';
import { overlay, throttle } from "./utils.js";
import { addMonacoStyles, createUserConfig, MonacoEditorLanguageClientWrapper } from "langium-website-core/bundle";
import { DocumentChangeResponse } from "langium-ast-helper";
import { DefaultAstNodeLocator } from "langium";
import { DefaultAstNodeLocator, LanguageMetaData, URI } from "langium";
import { createServicesForGrammar } from "langium/grammar";
import { generateTextMate } from "langium-cli/textmate";
export { share, overlay } from './utils.js';
Expand Down Expand Up @@ -155,13 +155,13 @@ export async function setupPlayground(
// disposed successfully, setup & clear overlay
await setupDSLWrapper();
overlay(false, false);

}).catch(async (e: any) => {
// failed to dispose, report & discard this error
// can happen when a previous editor was not started correctly
console.error('DSL editor disposal error: ' + e);
overlay(true, true);

});
}
});
Expand All @@ -172,7 +172,7 @@ export async function setupPlayground(
*/
async function setupDSLWrapper(): Promise<void> {
// get a fresh DSL wrapper
dslWrapper = await getFreshDSLWrapper(rightEditor, nextId(), currentDSLContent, currentGrammarContent);
dslWrapper = await getFreshDSLWrapper(rightEditor, "txt", currentDSLContent, currentGrammarContent);

// get a fresh client
dslClient = dslWrapper?.getLanguageClient();
Expand Down Expand Up @@ -209,8 +209,10 @@ async function getFreshDSLWrapper(
code: string,
grammarText: string
): Promise<MonacoEditorLanguageClientWrapper | undefined> {
const languageMetaData = getLanguageMetaData(languageId);

// construct and set a new monarch syntax onto the editor
const { Grammar } = await createServicesForGrammar({ grammar: grammarText });
const { Grammar, shared } = await createServicesForGrammar({ grammar: grammarText, languageMetaData });
const worker = await getLSWorkerForGrammar(grammarText);
const wrapper = new MonacoEditorLanguageClientWrapper();
const textmateGrammar = JSON.parse(generateTextMate(Grammar, { id: languageId, grammar: 'UserGrammar' }));
Expand All @@ -231,8 +233,8 @@ async function getFreshDSLWrapper(
// particularly due to a stuck LC, which can cause this to fail part-ways through
try {
await wrapper.dispose();
} catch (e) {}
return undefined as MonacoEditorLanguageClientWrapper|undefined;
} catch (e) { }
return undefined as MonacoEditorLanguageClientWrapper | undefined;
});
}

Expand Down Expand Up @@ -274,7 +276,7 @@ function registerForDocumentChanges(dslClient: any | undefined) {

// retrieve existing code from the model
currentDSLContent = dslWrapper?.getModel()?.getValue() as string;

// delay changes by 200ms, to avoid getting too many intermediate states
throttle(2, languageUpdateDelay, () => {
// render the AST in the far-right window
Expand Down Expand Up @@ -315,3 +317,12 @@ async function getLSWorkerForGrammar(grammar: string): Promise<Worker> {

});
}

export function getLanguageMetaData(languageId: string): LanguageMetaData {
return {
caseInsensitive: false,
fileExtensions: [`.${languageId}`],
languageId: languageId,
mode: 'development'
};
}
15 changes: 8 additions & 7 deletions hugo/content/playground/user-validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AstNode, AstNodeDescription, AstUtils, DefaultDocumentValidator, DefaultLinker, DiagnosticInfo, DocumentState, DocumentValidator, LangiumDocument, LinkingError, LinkingErrorData, ReferenceInfo, ValidationOptions } from "langium";
import { AstNode, DefaultDocumentValidator, DiagnosticInfo, DocumentValidator, LangiumDocument, LinkingErrorData, ValidationOptions } from "langium";
import { LangiumServices } from "langium/lsp";
import { Diagnostic } from "vscode-languageserver";

Expand All @@ -11,14 +11,15 @@ export class PlaygroundValidator extends DefaultDocumentValidator {
const linkingError = reference.error;
if (linkingError) {
const info: DiagnosticInfo<AstNode, string> = {
node: linkingError.container,
property: linkingError.property,
index: linkingError.index,
node: linkingError.info.container,
range: reference.$refNode?.range,
property: linkingError.info.property,
index: linkingError.info.index,
data: {
code: DocumentValidator.LinkingError,
containerType: linkingError.container.$type,
property: linkingError.property,
refText: linkingError.reference.$refText
containerType: linkingError.info.container.$type,
property: linkingError.info.property,
refText: linkingError.info.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));
Expand Down
11 changes: 6 additions & 5 deletions hugo/content/playground/user-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,22 @@ async function startWithGrammar(grammarText: string): Promise<void> {
// create a fresh connection for the LS
const connection = createServerConnection();

// create shared services & serializer for the given grammar grammar
const { shared, serializer } = await createServicesForGrammar({
const module = {
grammar: grammarText,
module: {
validation: {
DocumentValidator: (services: LangiumServices) => new PlaygroundValidator(services)
}
},
sharedModule: {

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

// create shared services & serializer for the given grammar grammar
const { shared, serializer } = await createServicesForGrammar(module);

// listen for validated documents, and send the AST back to the language client
shared.workspace.DocumentBuilder.onBuildPhase(DocumentState.Validated, documents => {
Expand All @@ -61,7 +62,7 @@ async function startWithGrammar(grammarText: string): Promise<void> {
});
}
});

// start the LS
startLanguageServer(shared);

Expand Down
12 changes: 6 additions & 6 deletions hugo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
"clsx": "^1.2.1",
"d3": "^7.8.5",
"hugo-extended": "~0.89.4",
"langium": "^3.0.0",
"langium-arithmetics-dsl": "^3.0.0",
"langium": "^4.1.0",
"langium-arithmetics-dsl": "^4.1.0",
"langium-ast-helper": "0.1.2",
"langium-cli": "^3.0.3",
"langium-domainmodel-dsl": "^3.0.0",
"langium-minilogo": "^3.0.0",
"langium-cli": "^4.1.0",
"langium-domainmodel-dsl": "^4.1.0",
"langium-minilogo": "^3.0.1",
"langium-sql": "^0.3.2",
"langium-statemachine-dsl": "^3.0.0",
"langium-statemachine-dsl": "^4.1.0",
"langium-website-core": "~1.0.0",
"lz-string": "^1.4.4",
"monaco-editor-workers": "~0.44.0",
Expand Down
Loading