Code Generation #328
Replies: 3 comments 3 replies
-
The reason I ask is because the Langium generated extension does not provide code gen automatically when changes made to the model file. A filesystemwatcher is created as part of the language client options but I don't see how I hook in the code gen to run when the language sever is told about file changes? |
Beta Was this translation helpful? Give feedback.
-
For Langium, we aligned with the JS ecosystem in regards to code-generation: Building a CLI tool to generate code without the hassle of going through the language server. In theory that's still possible, for example, you could simply hook a generator to the end of the document-builder lifecycle, but we didn't want to include that by default. You can override this code: With this: protected async buildDocuments(documents: LangiumDocument[], cancelToken: CancellationToken): Promise<void> {
await super.buildDocuments(documents, cancelToken);
await this.codeGenerator.run(documents, cancelToken); // This is some injected service that you use for code generation
} However, we would generally recommend to build a CLI, like the |
Beta Was this translation helpful? Give feedback.
-
@msujew, I'm not too sure that I understand this workflow either. I can see that the files are created in the /cli folder when I use Yeoman to scaffold my project. In my case, I'd want to commence generation when a VS Code command is invoked, expecting to pass a valid instance of the AST root to the generator. However, I'm not sure how to do this. Another approach that I could foresee is letting generation be an injected service in my language server's module file. I suppose I could get an instance of the generator through the DI's injector and invoke generation that way (maybe this is the 'going through the language server' approach that you mention above). What I don't really understand is how to invoke a CLI that performs generation, given the scenario that I described with a command. I'm sure it's pretty simple, I just don't know how to go about it. |
Beta Was this translation helpful? Give feedback.
-
I was looking at the examples and don't understand how to use code generation in the real world. I'm not a VSCode extension guru and so I'm probably missing something.
The Langium examples given say you can install the package globally and then use the command defined in the bin property in the package.json. That's all fine but that does not make sense to me in respect to a VScode extension. A user will install your extension and then expect to be able to perform code gen on their model file (based on your langium grammar).
So am I correct the examples are not demonstrating code gen in a VScode extension and that I need to hook that in myself via a VScode command or context menu? Any guidance would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions