-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Editor support #1
Comments
Thanks @hpx7! I was pretty surprised too that there aren't more options in the space. That file looks exactly like the problem I was working on that prompted me to write this: templating for code generation. I also tried template literals and came to the same conclusion as you. Completely agree on the IDE integration. I almost tried tackling that in December, but ultimately held off to see if anyone else would be interested in this project. I'd love to hear your thoughts. A TypeScript language server plugin might work? If not, writing a language server is another option. It looks like https://github.com/lifeart/els-addon-typed-templates achieves this in the type system: lifeart/els-addon-typed-templates#11 (comment). |
If this project had good VSCode integration and we posted it around a bit, I think it would immediately get popular. I don't have any experience with language servers or ts language server plugins, but my first thought is that this may be difficult to implement as a plugin because we're fundamentally operating on text files, not typescript files. That els-addon-typed-templates looks like exactly what we want to achieve -- but looks like it's building on top of https://github.com/ember-tooling/ember-language-server. Have you seen https://code.visualstudio.com/api/language-extensions/embedded-languages? |
Yeah I think so too.
Awesome, thanks for sharing this. I like that the language service implementation would enable integrations with other editors as well. |
Hi @tatethurston just curious if you had any more recent thoughts on this? |
Hey @hpx7 I've been heads down at work and haven't had a chance to build this yet. Have you seen any open source implementations of a visual studio language extension you like? I poked around a bit but I couldn't find anything concrete -- lots of packages wrapping packages. |
The Vue one is quite good: https://github.com/vuejs/vetur |
Another good reference: https://github.com/bash-lsp/bash-language-server |
I released a very MVP of visual studio code editor support: https://marketplace.visualstudio.com/items?itemName=embedded-typescript.embedded-typescript-vsc Right now, only errors from the ets compiler appear in editor, and there isn't any completion. Read: none of DX from TypeScript yet, but the scaffolding is at least in place. Next steps will be:
Related to the first point above, the compiler should probably use the TS compiler as a peer dependency and type check templates so that users do not encounter TS errors in the generated files. |
This is awesome! As soon as there is typescript integration I'm going to replace handlebars with this in my project. How hard would it be to skip the |
My understanding is that this is not possible with the current TypeScript compiler API. The compiler would need to expose a plugin API that enabled provided handlers to run and return a TS AST, similar to webpack loaders https://webpack.js.org/contribute/writing-a-loader. A Webpack loader would get close, but AFAICT we wouldn't have the necessary type information available in the importing file (because TS wouldn't know the contents). Would a |
Oh I didn't realize you needed to act on the AST, so then I suppose tools like esbuild wouldn't help either? A |
I may be misunderstanding, but right now
I opted for appending an extension (
The later may be clearer, and would clean up some funniness with jest that I don't love right now -- jest has it's own module resolution system that doesn't follow TS semantics above where '.ts' is appended to all import paths. |
It would be pretty straightforward to export For clarity, this wouldn't be the same as Before your application ran you would need Could you point me to some code where you're programmatically invoking handlebars compiler to generate |
This is the code where I do codegen using handlebars templates: https://github.com/hpx7/rtag/blob/67d907187e2adaac2134b85a0839bc781be03fc2/generate.ts#L199-L200 I guess if you expose a |
Are clients supplying templates to your codegen tool? Or does your tool define the templates and clients supply the values? |
The templates are bundled with my tool (see https://github.com/hpx7/rtag/tree/develop/templates/base/server/.rtag for example). Users have to write a |
The general workflow I have in mind is:
If I'm understanding correctly, in your case it looks like Instead of bundling the uncompiled templates with your tool, you would bundle the compiled templates with your tool. Then you can drop Does that make sense? Let me know if you have a constraint that I'm missing. |
Thanks for walking me through that, yes that would work! The only real downside I see is for folks who are using a locally checked out version of my tool rather than the packaged one in npm. I would have to publish additional instructions for them (as well as for potential contributors) on the workflow, but not the end of the world. |
Awesome!
If you commit the generated
This would require the addition of a watch mode to the ets compiler |
We've just picked this library up for code generation and it's great! Thank you so much for making it! If there's any chance of editor support, we'd be v happy :) |
Hey, cool project! I was looking for typesafe templating solutions and came across your project via google. I find it crazy that there isn't more in this space...
I'm working on a project where I'm using templating as a form of code generation. As an example, see this file which generates an auth module based on some user configuration: https://github.com/hpx7/rtag/blob/be5b3c365b415aa7888152f4fc4f4472959ef022/templates/base/auth.ts.hbs
I've been thinking about how it would be nice to have type-safe templating so I could do things like validate exhaustive checking of enums/unions etc. I first thought of switching to using template literals but quickly realized they don't work great for this use case (precise text formatting, lots of control flow).
ETS seems like a great option, but obviously the huge advantage of template literals is the editor support -- you get in your IDE (1) compile errors and (2) autocomplete. I've been brainstorming ideas for how to achieve the best of both worlds, just dropping this issue in case you had any thoughts on this topic.
The text was updated successfully, but these errors were encountered: