Description
Overview
Languages like Razor (and I imagine HTML for custom attributes) typically have scenarios where portions of the document are semantically a different language.
In Razor this happens frequently through the use of TagHelpers or in Blazor:
<form asp-antiforgery="ViewBag.ShouldRenderAntiforgery">
...
</form>
In this example we'd expect that ViewBag.ShouldRenderAntiforgery
would be C#. The way TagHelpers (things that apply to HTML and change the semantic language of the right hand side of an attribute) can be customized by users is limitless so we need to have full control over telling the IDE what things are C# and what things aren't.
Ideas on how to implement
Over in the issue where the discussion of general semantic colorization the proposal was to have an API similar to:
interface SemanticHighlightRangeProvider {
provideHighlightRanges(doc,...): [ Range, TokenType, TokenModifier[] ][];
}
The proposed approach can also be used to enable semantic language colorization without enabling an entire language's extensions for a subset of a document.
To do this one could expand on the ThemeDefinition
and add a language
parameter:
interface TokenStyle {
foreground?: Color | ColorFunction
style?: bold | italic | underline
language?: string
}
This would enable LanguageServers to mark a chunk of text in an editor as a Token that associates with a specific language. This would work similarly to how tooltips/completion descriptions etc. work when specifying pieces of text that should be colorized as a certain language.
So in the first example asp-antiforgery="ViewBag.ShouldRenderAntiforgery"
Razor's language server would indicate that the entire ViewBag.ShouldRenderAntiforgery
was of a Razor specific theme that had a token Style of:
{
"language": "csharp"
}
This would enable future scenarios where if Razor wanted to go above and beyond to provide semantic colorization of specific tokens it could do it in an additive fashion on top of existing theme.