expr-lang / expr

Expression language and expression evaluation for Go
https://expr-lang.org
MIT License
6.33k stars 406 forks source link

Language support for VSCode? #690

Open manterfield opened 4 months ago

manterfield commented 4 months ago

Thanks for the awesome project!

I'm considering using expr embedded within a product, however I'd want to ensure my end users get syntax highlighting within their editor (VSCode support as a minimum). Without this end users aren't going to have the best possible experience.

I can't really expect them to use the standalone expr editor given that by its nature this is embedded within other syntaxes and contexts (so users pulling out the expression to work on in another editor is too much friction).

I can see on the docs site you've got expr blocks highlighted and you're using prism, but I couldn't track down how that was being implemented.

Do you already have a TextMate grammar, language server or VSCode extension? If not, can something along these lines be added?

antonmedv commented 4 months ago

VSCode extention for Expr is in the works. 😉

But difficult comes with providing Env completions and embedding in other languages. For now it will be more like a separate highlighter for separate files.

How do you use Expr? How do you think highlighter should work?

manterfield commented 4 months ago

For us, expressions will appear within a yaml block which is in turn within markdown frontmatter.

Completions etc would be lovely, but the main things are highlighting (followed by diagnostics).

For highlighting - if there's a TextMate language/grammar spec created for your VSCode extension, then that can also be embedded in other language extensions created by users adding expr to their tools. Language only extensions are pretty straightforward to create - HCL is one example here.

For diagnostics - there's a few ways to skin this cat, but as long as we can run validation of an expression without choking on the absence of runtime variables then users can create their own diagnostics and a lightweight language server.

Of the two, diagnostics is likely the easier to solve as a user of expr, but the highlighting really isn't something I'd want to be implementing from the outside without any formal spec for the language.

Would you be able to share/open source whatever code you have that enables highlighting of expr on the docs site? I imagine that would massively speed up any attempts to create embedded highlighting.

antonmedv commented 4 months ago

Sure. Will try to shape it into some sort of a package.

But how you will embed one grammar into YAML? This is actually possible with Expr Editor, btw.

manterfield commented 4 months ago

Perhaps I've misunderstood Expr Editor, but I thought it was a standalone tool.

If that's the case, then I couldn't really make use of it in the context Expr is used. My end users will be editing my config syntax within their own IDEs, with Expr being one part of that wider syntax. It'd be a big ask to have them change their local dev flow and tooling in order to make best use of the expression syntax I've chosen.

You can embed languages by matching the key and defining it as using a specific grammar. GitHub Actions do this with the GitHub Actions VSCode extension (also on their expressions). If you check that out, you'll notice that whilst it's YAML, the if statements still highlight correctly rather than as strings with that extension installed.

There's a few ways to support embedded languages though, this VSCode doc goes into more detail.

antonmedv commented 4 months ago

Cool. Will definitely check this out.