diff --git a/packages/langium-cli/src/generator/grammar-serializer.ts b/packages/langium-cli/src/generator/grammar-serializer.ts index 856126533..e94b5c010 100644 --- a/packages/langium-cli/src/generator/grammar-serializer.ts +++ b/packages/langium-cli/src/generator/grammar-serializer.ts @@ -36,6 +36,7 @@ export function serializeGrammar(services: LangiumServices, grammars: Grammar[], }; const serializedGrammar = services.serializer.JsonSerializer.serialize(grammar, { space: production ? undefined : 2, + comments: true, uriConverter }); // The json serializer returns strings with \n line delimiter by default diff --git a/packages/langium-cli/test/generator/grammar-serializer.test.ts b/packages/langium-cli/test/generator/grammar-serializer.test.ts new file mode 100644 index 000000000..fcbee7134 --- /dev/null +++ b/packages/langium-cli/test/generator/grammar-serializer.test.ts @@ -0,0 +1,48 @@ +/****************************************************************************** + * Copyright 2021 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 { LangiumConfig } from '../../src/package.js'; +import { EmptyFileSystem, URI, type Grammar } from 'langium'; +import { describe, expect, test } from 'vitest'; +import { RelativePath } from '../../src/package.js'; +import { serializeGrammar } from '../../src/generator/grammar-serializer.js'; +import { createLangiumGrammarServices } from 'langium/grammar'; +import { expandToString } from 'langium/generate'; + +const grammarServices = createLangiumGrammarServices(EmptyFileSystem); + +describe('Grammar serializer', () => { + test('should include comments of AST elements', async () => { + // arrange + const config: LangiumConfig = { + projectName: 'Magic', + languages: [], + [RelativePath]: '/path/to/magic', + }; + + const grammarText = expandToString` + grammar Test + + /** + * This is the entry rule + */ + entry Model: /** This is the name assignment */ name='test'; + `; + + const document = grammarServices.shared.workspace.LangiumDocumentFactory.fromString(grammarText, URI.file('test.langium')); + grammarServices.shared.workspace.LangiumDocuments.addDocument(document); + await grammarServices.shared.workspace.DocumentBuilder.build([document]); + const grammar = document.parseResult.value; + + // act + const moduleString = serializeGrammar(grammarServices.grammar, [grammar], config); + + // assert + expect(moduleString).toMatch('"$comment": "/** This is the name assignment */"'); + expect(moduleString).toMatch('"$comment": "/**\\\\n * This is the entry rule\\\\n */"'); + }); + +}); diff --git a/packages/langium/src/grammar/langium-grammar.langium b/packages/langium/src/grammar/langium-grammar.langium index 1a5abf1c7..b964d8636 100644 --- a/packages/langium/src/grammar/langium-grammar.langium +++ b/packages/langium/src/grammar/langium-grammar.langium @@ -1,8 +1,9 @@ -/****************************************************************************** - * Copyright 2021 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. - ******************************************************************************/ +// ****************************************************************************** +// Copyright 2021 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. +// ***************************************************************************** + grammar LangiumGrammar entry Grammar: diff --git a/packages/langium/src/serializer/json-serializer.ts b/packages/langium/src/serializer/json-serializer.ts index 068f94df7..4e4867a22 100644 --- a/packages/langium/src/serializer/json-serializer.ts +++ b/packages/langium/src/serializer/json-serializer.ts @@ -188,7 +188,10 @@ export class DefaultJsonSerializer implements JsonSerializer { } if (comments) { astNode ??= { ...value }; - (astNode as AstNodeWithComment).$comment = this.commentProvider.getComment(value); + const comment = this.commentProvider.getComment(value); + if (comment) { + (astNode as AstNodeWithComment).$comment = comment.replace(/\r/g, ''); + } } return astNode ?? value; } else {