mjbvz / vscode-lit-html

Adds syntax highlighting for html inside of JavaScript and TypeScript tagged template strings
https://marketplace.visualstudio.com/items?itemName=bierner.lit-html
MIT License
318 stars 75 forks source link

Support passing generic type parameters to html function? #96

Open karlhorky opened 2 years ago

karlhorky commented 2 years ago

Hi @mjbvz, first of all, thanks for your effort creating this extension, very useful!

I wanted to find out if you'd be open to supporting passing TypeScript generic type parameters to the html function? Eg. both of:

  1. Single-line generic type parameters
  2. Multi-line generic type parameters

The extension currently doesn't highlight with either of these:

Screen Shot 2022-04-02 at 16 11 43

Code:

const withHighlight = html`<div></div>`;
const noHighlight = html<string>` <div></div> `;
const alsoNoHighlight = html<
  // longer generic type parameter
  ButtonHTMLAttributes<HTMLButtonElement>
>` <div></div> `;
mjbvz commented 2 years ago

I'm open to it (if someone makes a PR :)

karlhorky commented 1 year ago

Looking at similar issues across other extensions, one problem with the example above is that the longer generic type argument spans multiple lines.

The root cause for this is a bit more of a problem, since TextMate grammars have limitations, as @sheetalkamat is familiar with:

VS Code itself somehow gets around the limitations and can do correct syntax highlighting over multiple lines, maybe with Semantic Highlighting by @aeschli and @alexdima.

I also see some changes with regular expressions, which @sheetalkamat has also been maintaining:

So maybe there is a way for a VS Code extension to also achieve this, by also using this Semantic Highlighting / these regular expressions.

The TextMate grammar limitations have also been explored in the following SQL tagged template literals VS Code extension, but no solution has yet appeared:

karlhorky commented 1 year ago

Workaround (/* html */ comment)

Add a /* html */ comment after the multi-line generic

const alsoNoHighlight = html<
  // longer generic type parameter
  ButtonHTMLAttributes<HTMLButtonElement>
->` <div></div> `;
+>/* html */ ` <div></div> `;

Credit: @Net in https://github.com/thebearingedge/vscode-sql-lit/issues/13#issuecomment-1550486134