Skip to content

Commit

Permalink
Implemented a first set of review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Jun 15, 2024
1 parent 8931941 commit 94ab736
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 56 deletions.
1 change: 0 additions & 1 deletion packages/generator-langium/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const PACKAGE_LANGUAGE = 'packages/language';
const PACKAGE_CLI = 'packages/cli';
const PACKAGE_WEB = 'packages/web';
const PACKAGE_EXTENSION = 'packages/extension';
// const TEMPLATE_TEST_DIR = '../templates/packages/language/test';
const USER_DIR = '.';

const EXTENSION_NAME = /<%= extension-name %>/g;
Expand Down
8 changes: 5 additions & 3 deletions packages/generator-langium/templates/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
"--extensionDevelopmentPath=${workspaceFolder}/packages/extension"
],
"sourceMaps": true,
"outFiles": [
"${workspaceFolder}/out/**/*.js"
"${workspaceFolder}/packages/language/out/**/*.js",
"${workspaceFolder}/packages/extension/out/**/*.js"
]
},
{
Expand All @@ -27,7 +28,8 @@
],
"sourceMaps": true,
"outFiles": [
"${workspaceFolder}/out/**/*.js",
"${workspaceFolder}/packages/language/out/**/*.js",
"${workspaceFolder}/packages/extension/out/**/*.js",
"${workspaceFolder}/node_modules/langium"
]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/generator-langium/templates/gitignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
!.vscode/tasks.json
node_modules/
out/
src/language/generated/
src/generated/
syntaxes/
9 changes: 2 additions & 7 deletions packages/generator-langium/templates/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
{
"name": "<%= extension-name %>-base",
"name": "<%= extension-name %>-workspace",
"description": "Base workspace package",
"version": "0.0.1",
"type": "module",
"private": true,
"files": [
"out",
"src"
],
"scripts": {
"compile": "tsc -b tsconfig.build.json",
"watch": "tsc -b tsconfig.build.json --watch",
"build": "npm run compile && npm run build --workspaces",
"build": "tsc -b tsconfig.build.json && npm run build --workspaces",
"lint": "eslint src --ext ts",
"langium:generate": "npm run --workspace packages/language langium:generate",
"langium:watch": "npm run --workspace packages/language langium:watch"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Model } from '<%= language-id %>-language';
import { expandToNode, joinToNode, toString } from 'langium/generate';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { extractDestinationAndName } from './cli-util.js';
import { extractDestinationAndName } from './util.js';

export function generateJavaScript(model: Model, filePath: string, destination: string | undefined): string {
const data = extractDestinationAndName(filePath, destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Model } from '<%= language-id %>-language';
import { create<%= LanguageName %>Services, <%= LanguageName %>LanguageMetaData } from '<%= language-id %>-language';
import chalk from 'chalk';
import { Command } from 'commander';
import { extractAstNode } from './cli-util.js';
import { extractAstNode } from './util.js';
import { generateJavaScript } from './generator.js';
import { NodeFileSystem } from 'langium/node';
import * as url from 'node:url';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
.vscode/**
.vscode-test/**
.gitignore
langium-quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@
## What's in the folder

This folder contains all necessary files for your language extension.
* `package.json` - the manifest file in which you declare your language support.
* `language-configuration.json` - the language configuration used in the VS Code editor, defining the tokens that are used for comments and brackets.
* `src/extension/main.ts` - the main code of the extension, which is responsible for launching a language server and client.
* `src/language/<%= language-id %>.langium` - the grammar definition of your language.
* `src/language/main.ts` - the entry point of the language server process.
* `src/language/<%= language-id %>-module.ts` - the dependency injection module of your language implementation. Use this to register overridden and added services.
* `src/language/<%= language-id %>-validator.ts` - an example validator. You should change it to reflect the semantics of your language.
* `src/cli/main.ts` - the entry point of the command line interface (CLI) of your language.
* `src/cli/generator.ts` - the code generator used by the CLI to write output files from DSL documents.
* `src/cli/cli-util.ts` - utility code for the CLI.

* `package.json` - the manifest file in which you declare your language support.
* `language-configuration.json` - the language configuration used in the VS Code editor, defining the tokens that are used for comments and brackets.
* `src/extension/main.ts` - the main code of the extension, which is responsible for launching a language server and client.
* `src/language/<%= language-id %>.langium` - the grammar definition of your language.
* `src/language/main.ts` - the entry point of the language server process.
* `src/language/<%= language-id %>-module.ts` - the dependency injection module of your language implementation. Use this to register overridden and added services.
* `src/language/<%= language-id %>-validator.ts` - an example validator. You should change it to reflect the semantics of your language.
* `src/cli/main.ts` - the entry point of the command line interface (CLI) of your language.
* `src/cli/generator.ts` - the code generator used by the CLI to write output files from DSL documents.
* `src/cli/util.ts` - utility code for the CLI.

## Get up and running straight away

* Run `npm run langium:generate` to generate TypeScript code from the grammar definition.
* Run `npm run build` to compile all TypeScript code.
* Press `F5` to open a new window with your extension loaded.
* Create a new file with a file name suffix matching your language.
* Verify that syntax highlighting, validation, completion etc. are working as expected.
* Run `node ./bin/cli` to see options for the CLI; `node ./bin/cli generate <file>` generates code for a given DSL file.
* Run `npm run langium:generate` to generate TypeScript code from the grammar definition.
* Run `npm run build` to compile all TypeScript code.
* Press `F5` to open a new window with your extension loaded.
* Create a new file with a file name suffix matching your language.
* Verify that syntax highlighting, validation, completion etc. are working as expected.
* Run `node ./bin/cli` to see options for the CLI; `node ./bin/cli generate <file>` generates code for a given DSL file.

## Make changes

* Run `npm run watch` to have the TypeScript compiler run automatically after every change of the source files.
* Run `npm run langium:watch` to have the Langium generator run automatically after every change of the grammar declaration.
* You can relaunch the extension from the debug toolbar after making changes to the files listed above.
* You can also reload (`Ctrl+R` or `Cmd+R` on Mac) the VS Code window with your extension to load your changes.
* Run `npm run watch` to have the TypeScript compiler run automatically after every change of the source files.
* Run `npm run langium:watch` to have the Langium generator run automatically after every change of the grammar declaration.
* You can relaunch the extension from the debug toolbar after making changes to the files listed above.
* You can also reload (`Ctrl+R` or `Cmd+R` on Mac) the VS Code window with your extension to load your changes.

## Install your extension

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { beforeAll, describe, expect, test } from "vitest";
import { EmptyFileSystem, type LangiumDocument } from "langium";
import { expandToString as s } from "langium/generate";
import { parseHelper } from "langium/test";
import { create<%= LanguageName %>Services, isModel, Model } from "<%= language-id %>-language";
import type { Model } from "<%= language-id %>-language";
import { create<%= LanguageName %>Services, isModel } from "<%= language-id %>-language";

let services: ReturnType<typeof create<%= LanguageName %>Services>;
let parse: ReturnType<typeof parseHelper<Model>>;
Expand Down Expand Up @@ -54,6 +55,6 @@ function checkDocumentValid(document: LangiumDocument): string | undefined {
${document.parseResult.parserErrors.map(e => e.message).join('\n ')}
`
|| document.parseResult.value === undefined && `ParseResult is 'undefined'.`
|| !isModel(document.parseResult.value) && `Root AST object is a ${document.parseResult.value.$type}, expected a '${Model}'.`
|| !isModel(document.parseResult.value) && `Root AST object is a ${document.parseResult.value.$type}, expected a Model'.`
|| undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { EmptyFileSystem, type LangiumDocument } from "langium";
import { expandToString as s } from "langium/generate";
import { parseHelper } from "langium/test";
import type { Diagnostic } from "vscode-languageserver-types";
import { create<%= LanguageName %>Services, isModel, Model } from "<%= language-id %>-language";
import type { Model } from "<%= language-id %>-language";
import { create<%= LanguageName %>Services, isModel } from "<%= language-id %>-language";

let services: ReturnType<typeof create<%= LanguageName %>Services>;
let parse: ReturnType<typeof parseHelper<Model>>;
Expand All @@ -19,7 +20,7 @@ beforeAll(async () => {
});

describe('Validating', () => {

test('check no errors', async () => {
document = await parse(`
person Langium
Expand Down Expand Up @@ -56,7 +57,7 @@ function checkDocumentValid(document: LangiumDocument): string | undefined {
${document.parseResult.parserErrors.map(e => e.message).join('\n ')}
`
|| document.parseResult.value === undefined && `ParseResult is 'undefined'.`
|| !isModel(document.parseResult.value) && `Root AST object is a ${document.parseResult.value.$type}, expected a '${Model}'.`
|| !isModel(document.parseResult.value) && `Root AST object is a ${document.parseResult.value.$type}, expected a 'Model'.`
|| undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "out",
"declarationDir": "out",
"outDir": "out"
},
"include": [
"src/**/*.ts",
]
}

22 changes: 10 additions & 12 deletions packages/generator-langium/test/yeoman-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ describe('Check yeoman generator works', () => {
targetRoot + '/packages/language/src/syntaxes/hello-world.monarch.ts',
targetRoot + '/packages/language/src/generated/ast.ts',
targetRoot + '/packages/language/src/generated/grammar.ts',
targetRoot + '/packages/language/src/generated/module.ts',
targetRoot + '/packages/language/src/generated/module.ts'
];

const filesTest = (targetRoot: string) => [
targetRoot + '/packages/language/tsconfig.test.json',
targetRoot + '/packages/language/test/linking/linking.test.ts',
targetRoot + '/packages/language/test/parsing/parsing.test.ts',
targetRoot + '/packages/language/test/validating/validating.test.ts',
targetRoot + '/packages/language/test/linking.test.ts',
targetRoot + '/packages/language/test/parsing.test.ts',
targetRoot + '/packages/language/test/validating.test.ts'
];

const filesCli = (targetRoot: string) => [
targetRoot + '/packages/cli/bin/cli.js',
targetRoot + '/packages/cli/src/cli-util.ts',
targetRoot + '/packages/cli/src/util.ts',
targetRoot + '/packages/cli/src/generator.ts',
targetRoot + '/packages/cli/src/main.ts',
targetRoot + '/packages/cli/package.json',
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('Check yeoman generator works', () => {
targetRoot + '/packages/extension/tsconfig.json'
];

test('1 Should produce files for Core', async () => {
test('1 Should produce files for workspace and language (no test)', async () => {
const context = createHelpers({}).run(path.join(moduleRoot));

// generate in examples
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('Check yeoman generator works', () => {
});
}, 120_000);

test('2 Should produce files for Core & CLI & test', async () => {
test('2 Should produce files for workspace and languag (plus test) and cli', async () => {
const context = createHelpers({}).run<LangiumGenerator>(path.join(moduleRoot));

// generate in examples
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('Check yeoman generator works', () => {
});
}, 120_000);

test('3 Should produce files for Core & CLI & web & extension without tests', async () => {
test('3 Should produce files for workspace, language (no test), cli, web and extension', async () => {
const context = createHelpers({}).run<LangiumGenerator>(path.join(moduleRoot));

// generate in examples
Expand Down Expand Up @@ -232,16 +232,14 @@ describe('Check yeoman generator works', () => {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const PACKAGE_JSON_EXPECTATION: Record<string, any> = {
name: 'hello-world-base',
name: 'hello-world-workspace',
description: 'Base workspace package',
version: '0.0.1',
type: 'module',
private: true,
files: ['out', 'src'],
scripts: {
'compile': 'tsc -b tsconfig.build.json',
'watch': 'tsc -b tsconfig.build.json --watch',
'build': 'npm run compile && npm run build --workspaces',
'build': 'tsc -b tsconfig.build.json && npm run build --workspaces',
'lint': 'eslint src --ext ts',
'langium:generate': 'npm run --workspace packages/language langium:generate',
'langium:watch': 'npm run --workspace packages/language langium:watch'
Expand Down

0 comments on commit 94ab736

Please sign in to comment.