feat(editor): colour Vue component tags apart from HTML elements (#7059)

Component tags in Vue templates (<Icon>, <el-input>) had the same colour as HTML elements. PascalCase or hyphenated tag names now use a new --syn-custom-tag token, the Custom tag name colour: #2fbaa3 in Islands Dark and #b21dbf in Islands Light, with values for high contrast, the palette themes and Harbor Night; other themes fall back to the tag colour.

Fixes #6894
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-30 21:59:52 +08:00
1 джерело 5bfefff5e1
коміт edd2e90b9b
9 files changed
+124 -2

No files matched your search

+26
Переглянути файл
@@ -0,0 +1,26 @@
import { describe, expect, it } from "vitest";
import { ensureSyntaxTree } from "@codemirror/language";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { isCustomTagName } from "./customTags";
import { vueLanguage } from "./vue";
describe("Vue custom tag names", () => {
it("are PascalCase or hyphenated names", () => {
expect(["Icon", "el-input", "router-view", "MyButton"].map(isCustomTagName)).toEqual([true, true, true, true]);
expect(["div", "template", "span", "slot"].map(isCustomTagName)).toEqual([false, false, false, false]);
});
it("get their own colour in templates, opening and closing tags alike", () => {
const doc = `<template>\n <div><Icon /><el-input v-model="x"></el-input></div>\n</template>\n<script setup>\nconst a = 1 < 2\n</script>\n`;
const view = new EditorView({
state: EditorState.create({ doc, extensions: vueLanguage() }),
parent: document.body,
});
ensureSyntaxTree(view.state, doc.length, 5000);
view.dispatch({}); // let the plugin see the finished tree
const marked = [...view.dom.querySelectorAll(".cm-customTagName")].map((el) => el.textContent);
view.destroy();
expect(marked).toEqual(["Icon", "el-input", "el-input"]);
});
});
+52
Переглянути файл
@@ -0,0 +1,52 @@
/**
* WebStorm's "Custom tag name" colour: component tags in Vue templates (`<Icon>`, `<el-input>`)
* stand apart from HTML elements (`--syn-custom-tag`, the tag colour when a theme has none).
*/
import { syntaxTree } from "@codemirror/language";
import { RangeSetBuilder } from "@codemirror/state";
import { Decoration, EditorView, ViewPlugin, type DecorationSet, type ViewUpdate } from "@codemirror/view";
const customTag = Decoration.mark({ class: "cm-customTagName" });
/** A component, not an HTML element: PascalCase (`Icon`), or a custom-element name with a hyphen. */
export function isCustomTagName(name: string): boolean {
return /^[A-Z]/.test(name) || name.includes("-");
}
function decorations(view: EditorView): DecorationSet {
const builder = new RangeSetBuilder<Decoration>();
const tree = syntaxTree(view.state);
for (const { from, to } of view.visibleRanges) {
tree.iterate({
from,
to,
enter: (node) => {
if (node.name !== "TagName") return;
if (isCustomTagName(view.state.sliceDoc(node.from, node.to))) builder.add(node.from, node.to, customTag);
},
});
}
return builder.finish();
}
const plugin = ViewPlugin.fromClass(
class {
decorations: DecorationSet;
constructor(view: EditorView) {
this.decorations = decorations(view);
}
update(update: ViewUpdate) {
if (update.docChanged || update.viewportChanged || syntaxTree(update.state) !== syntaxTree(update.startState))
this.decorations = decorations(update.view);
}
},
{ decorations: (p) => p.decorations },
);
/** Colours component tag names in a template (the syntax highlighting's tag span inside included). */
export const customTagNames = [
plugin,
EditorView.baseTheme({
".cm-customTagName, .cm-customTagName *": { color: "var(--syn-custom-tag, var(--syn-tag))" },
}),
];
+3 -1
Переглянути файл
@@ -9,6 +9,7 @@ import { html } from "@codemirror/lang-html";
import { lessLanguage } from "@codemirror/lang-less";
import { sassLanguage } from "@codemirror/lang-sass";
import { vue } from "@codemirror/lang-vue";
import { customTagNames } from "./customTags";
import { VERSIONED_SCRIPT_TYPES } from "./htmlScripts";
/**
@@ -41,5 +42,6 @@ export function vueLanguage(): LanguageSupport {
],
});
const language = (base.language as LRLanguage).configure({ props: [indentNodeProp.add({ Element: elementIndent })] });
return vue({ base: new LanguageSupport(language, base.support) });
const support = vue({ base: new LanguageSupport(language, base.support) });
return new LanguageSupport(support.language, [support.support, customTagNames]);
}
+5
Переглянути файл
@@ -202,6 +202,8 @@ html[data-os="windows"] {
--syn-escape: #cf8e6d;
--syn-regexp: #42c3d4;
--syn-tag: #d5b778;
/* Component tags of Vue templates (IDEA: HTML_CUSTOM_TAG_NAME). */
--syn-custom-tag: #2fbaa3;
--syn-attribute: #bcbec4;
--syn-todo: #8bb33d;
--syn-link: #548af7;
@@ -688,6 +690,7 @@ html[data-os="windows"] {
--syn-escape: #0037a6;
--syn-regexp: #264eff;
--syn-tag: #0033b3;
--syn-custom-tag: #b21dbf;
--syn-attribute: #174ad4;
--syn-todo: #008dde;
--syn-link: #006dcc;
@@ -991,6 +994,7 @@ html[data-os="windows"] {
--syn-escape: #e8a885;
--syn-regexp: #6ad8e6;
--syn-tag: #ecd39a;
--syn-custom-tag: #4fe0c8;
--syn-attribute: #e3e4e8;
--syn-todo: #aad35a;
--syn-link: #8fb3ff;
@@ -1383,6 +1387,7 @@ html[data-os="windows"] {
--syn-escape: var(--palette-cyan);
--syn-regexp: var(--palette-cyan);
--syn-tag: var(--palette-red);
--syn-custom-tag: var(--palette-cyan);
--syn-attribute: var(--palette-orange);
--syn-todo: var(--palette-magenta);
--syn-markup-heading: var(--palette-blue);