дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-01 06:42:54 +02:00
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
Цей коміт міститься в:
1 джерело
5bfefff5e1
коміт
edd2e90b9b
9 files changed
+124
-2
No files matched your search
@@ -0,0 +1,12 @@
|
||||
import "../src/styles/tokens.css";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { syntaxHighlighting } from "@codemirror/language";
|
||||
import { openideaHighlightStyle } from "../src/editor/core/cm/highlight";
|
||||
import { vueLanguage } from "../src/editor/core/cm/vue";
|
||||
|
||||
document.documentElement.dataset.theme = new URLSearchParams(location.search).get("theme") ?? "dark";
|
||||
new EditorView({
|
||||
parent: document.body,
|
||||
doc: '<template>\n <div><Icon /><el-input v-model="x"></el-input></div>\n</template>\n',
|
||||
extensions: [vueLanguage(), syntaxHighlighting(openideaHighlightStyle)],
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
</head>
|
||||
<body>
|
||||
<script type="module" src="./vueHighlight.fixture.ts"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,14 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
|
||||
for (const [theme, custom, tag] of [
|
||||
["dark", "rgb(47, 186, 163)", "rgb(213, 183, 120)"],
|
||||
["light", "rgb(178, 29, 191)", "rgb(0, 51, 179)"],
|
||||
] as const) {
|
||||
test(`Vue component tags have the custom tag colour (${theme})`, async ({ page }) => {
|
||||
await page.goto(`/e2e/vueHighlight.html?theme=${theme}`);
|
||||
const line = page.locator(".cm-line").nth(1);
|
||||
await expect(line.getByText("Icon", { exact: true })).toHaveCSS("color", custom);
|
||||
await expect(line.getByText("el-input", { exact: true }).first()).toHaveCSS("color", custom);
|
||||
await expect(line.getByText("div", { exact: true }).first()).toHaveCSS("color", tag);
|
||||
});
|
||||
}
|
||||
@@ -60,6 +60,7 @@
|
||||
"--syn-escape": "#7fd1c4",
|
||||
"--syn-regexp": "#7fd1c4",
|
||||
"--syn-tag": "#e0a46e",
|
||||
"--syn-custom-tag": "#8fd4c9",
|
||||
"--syn-attribute": "#9fc0e8",
|
||||
"--syn-todo": "#7fd1c4",
|
||||
"--syn-link": "#6fc3df",
|
||||
|
||||
@@ -333,7 +333,7 @@ a token.
|
||||
|
||||
<!-- BEGIN GENERATED TOKEN REFERENCE: pnpm gen:theme-docs -->
|
||||
|
||||
272 themeable tokens: 126 in the UI theme file, 146 in the editor colour scheme file.
|
||||
273 themeable tokens: 126 in the UI theme file, 147 in the editor colour scheme file.
|
||||
|
||||
### Surfaces (UI theme)
|
||||
|
||||
@@ -482,6 +482,7 @@ a token.
|
||||
| `--syn-escape` | `#cf8e6d` | `#0037a6` |
|
||||
| `--syn-regexp` | `#42c3d4` | `#264eff` |
|
||||
| `--syn-tag` | `#d5b778` | `#0033b3` |
|
||||
| `--syn-custom-tag` | `#2fbaa3` | `#b21dbf` |
|
||||
| `--syn-attribute` | `#bcbec4` | `#174ad4` |
|
||||
| `--syn-todo` | `#8bb33d` | `#008dde` |
|
||||
| `--syn-link` | `#548af7` | `#006dcc` |
|
||||
|
||||
@@ -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"]);
|
||||
});
|
||||
});
|
||||
@@ -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))" },
|
||||
}),
|
||||
];
|
||||
@@ -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]);
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Заблокувати користувача