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

How can we highlight template literals as JavaScript? #140

Closed
kireerik opened this issue May 24, 2023 · 14 comments
Closed

How can we highlight template literals as JavaScript? #140

kireerik opened this issue May 24, 2023 · 14 comments

Comments

@kireerik
Copy link

To highlight as HTML it works like this:

"custom_template_tags": {
	"": "scope:text.html.basic"
}

 
To highlight as JavaScript I tried the following for example:

"custom_template_tags": {
	"": "scope:source.js"
}
@Thom1729
Copy link
Owner

It's not clear to me what you're asking. You might be looking for the default configuration option.

@kireerik
Copy link
Author

It works like that. Thank you!

"custom_templates": {
	"default": "scope:source.js"
}

@kireerik
Copy link
Author

kireerik commented Jun 8, 2023

How can I restore the default highlighting for template strings (in lookaheads)?

I know how to set lookaheads, but I don't know what should the value be to reset to the default (in a js file).

@Thom1729
Copy link
Owner

Thom1729 commented Jun 8, 2023

You want e.g. templates to be highlighted as HTML by default, but as plain template strings with a lookahead? I'm having trouble envisioning the use case; can you provide an example?

If that's what you mean, then I'm not sure there's a clean solution at the moment. You could do it as follows:

"custom_templates": {
  "default": "scope:text.html.basic",
  "lookaheads": {
    "foo": "string-content"
  }
},

But a) you won't get the proper string scope, and b) this is kind of hacky and brittle. If this is a real use case for you, then it might not be too much of a code change to support it.

@kireerik
Copy link
Author

kireerik commented Jun 9, 2023

Yes, but not as HTML (but as JavaScript), yes.

I have tried what you suggest, but it is not working. So yes it would be nice to have this.

This is how I am trying:

"custom_templates": {
	"default": "scope:source.js"
	, "lookaheads": {
		"\\<": "scope:text.html.basic"
		, ".": "string-content"
	}
}

I would like to restore the default highlighting for these type of dynamic imports for example:

@Thom1729
Copy link
Owner

Thom1729 commented Jun 9, 2023

Rather than ".": "string-content", you need "\\.": "string-content". The key there is interpreted as a regexp, so the dot will match anything; you need a backslash to escape the dot, and then another backslash for JSON escaping. Without that, any string will match the lookahead, so you'll never see the default.

This seems to work for me, with the caveat that (as mentioned above) e.g. ./foo won't get the normal string scope (and color).

Alternatively, what about:

"custom_templates": {
  "lookaheads": {
    "\\<": "scope:text.html.basic",
    "import": "scope:source.js",
  },
},

@kireerik
Copy link
Author

The alternative is not working.

@kireerik
Copy link
Author

Seems like the solution is to not define the default at all. Like this for example:

"custom_templates": {
	"lookaheads": {
		"\\<|.\\>": "scope:text.html.basic"

		, "var": "scope:source.js"
	}
}

@Thom1729
Copy link
Owner

Yeah, that's what I meant. Sorry it wasn't clear. Does that work for your use case? If not I can create a feature ticket.

@kireerik
Copy link
Author

Yes, it works like this for me. Thank you!

@kireerik
Copy link
Author

Similarly to template strings can I set (custom) highlighting (using lookaheads) for regular strings?

@Thom1729
Copy link
Owner

That is not currently a feature. Given that you can use template strings anywhere, I suspect that custom embedding for single/double-quoted strings would not be useful enough to justify the complexity. In addition, there might be substantial performance implications. What is your use case for this?

@kireerik
Copy link
Author

Yes, it would be just nice to have. I have some single line html strings and ' looks simpler and it is easier to type in these cases compared to `.

@kireerik
Copy link
Author

I am implementing JavaScript Extensible Markup Language usage for some parts that are currently modules and will become (SolidJS) components in the Refo static site example.

So for example the following line:

will look like this:

{template(`<!DOCTYPE HTML>`)}

and unfortunately this line too for example:

{template(`<head>`)}

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

2 participants