Hello!
I have a question regarding nested highlighting elements in CM6.
I have read the docs and see that one can use various “path” endings (/, !, *, or …
) to control how nested StyleTags
are dealt with (e.g. whether a variable name inside a parameter list gets the class, the parameter list gets the class, or both).
However I believe that there is an important case that is missed by the (/, !, *, or …
) syntax and the current implementation: there doesn’t seem to be a way to tell the system that we want a separate DOM element for the outer rule (in this case parameter list).
Say we add the following to the StyleTags
definition for the @codemirror/lang-javascript
extension and have a HighlightStyle
which maps tags.custom_parameters
to class="cmt-parameters"
:
"ArgList/...": tags.custom_parameters,
"TypeParamList/...": tags.custom_parameters,
"ParamList/...": tags.custom_parameters,
This results in all the matches inside the parameter list (e.g. variable name and parentheses) to be wrapped in individual DOM elements with two classes (e.g. class="cmt-parameters cmt-variableName”
) rather than for them to have a single cmt-variableName
class and to then wrap the whole parameter list in another element with class="cmt-parameters”
.
While this may be desired for some use cases, in others it is not. A simple example is adding a subtle background color change when hovering over a parameter list. As is with CSS alone (without a JS hack), we can only change the background on hover of a section of the parameter list.
It seems the Decoration
system can deal with this case, as the Underline example here will add such a wrapper element around the parameter list’s contents, but this option just isn’t exposed in the StyleTags
definition syntax:
Also I know wrapper DOM elements created by the Decoration
system will never be in/on more than one line but this is already built into the API and handled automatically.
With all that said, assuming I haven’t missed an undocumented way of already doing this, would you be open to a PR with an option to allow nested decoration elements rather than adding multiple classes to all the logically nested elements?
Something like <…>
to indicate a separate element is created for both the enclosing and nested matches:
"ArgList/<…>": tags.custom_parameters,
"TypeParamList/<…>": tags.custom_parameters,
"ParamList//<…>": tags.custom_parameters,
Thanks for your consideration on this or any pointers to how this can already be done with the highlight extension!
-Ed