Skip to content
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

Feature: Enable language server features in tagged template literals #83

Open
Fuzzyma opened this issue Nov 14, 2023 · 2 comments
Open

Comments

@Fuzzyma
Copy link
Contributor

Fuzzyma commented Nov 14, 2023

Sometimes I have code in a string:

const js = `
  const msg = 'this doesnt have any language server features';
  console.log(msg);
`

There is a vscode extension that allows syntax highlighting for such template literals by prefixing it was a comment or use a tagged template:

// with comment
const js = /* javascript */ `
  const msg = 'this code is highlighted';
  console.log(msg)
`
// via tagged template
const js = javascript`
  const msg = 'this code is highlighted';
  console.log(msg)
`

Syntax highlighting alone is pretty cool. However, I run into bugs all the time because I have spelling errors or other easy to catch mistakes if those templates would also have language server support.

So I wanted to extend the ts language server to add this feature but I am not sure if extending a language server is even possible yet.

In a dream world you probably would use it like this:

const outerMsg = 'hello from outside'
const ts = javascript`
  // inherits the scope of the sorrounding code
  console.log(outerMsg); // outerMsg is "string"
`

const ts = javascript<Window>`
  // scope is Window
  window.console.log('much wow!')
`

Obviously you would still need to define the javascript function in this case. So maybe its not the best solution but its a start.

@Fuzzyma Fuzzyma changed the title Feature: Enable language server fetures in tagged template literals Feature: Enable language server features in tagged template literals Nov 14, 2023
@TylorS167
Copy link

I have a similar use case, but with the use of html in template strings. I'm also curious if it's possible to do this kind of thing with Volar

@remcohaszing
Copy link
Member

Yes, this is possible. You will need to create a Volar language plugin. My recommended approach is to parse the TypeScript code into an AST using the TypeScript library. Then look for any template expressions, and create embedded code for each template expression found.

The following links may be useful as a reference implementation:

Some embedded languages are harded to deal with than others. TypeScript requires an entire project context, HTML/CSS/JSON does not. I’m positive this should be fairly simple to implement for embedded HTML, but you may run into issues implementing this for JavaScript/TypeScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants