Files
openidea/e2e/templateLiterals.spec.ts
T
LU XIANGRUIandwildcatDownstairs a67b27c33a feat(editor): highlight CSS, GraphQL, SQL and HTML in template literals (#5160)
In JavaScript and TypeScript files, embedded code in template literals showed as one string. The body of a tagged template literal is now parsed in the language its tag names: CSS for styled-components / emotion / Lit (`styled.div`, `styled(Comp)`, `.attrs(…)`, `css`, `createGlobalStyle`, `keyframes`, `injectGlobal`, parsed as declarations plus nested rules), GraphQL for `gql` / `graphql` (also the `graphql(`…`)` call form), SQL for `sql` and HTML for `html`; a `/* GraphQL */`-style comment before a plain literal works too. Substitutions stay JavaScript. CSS, SQL and HTML use the same modes as files in those languages; GraphQL gets a small stream mode used only inside literals (`.graphql` files keep tree-sitter highlighting). The embedded mode loads lazily, comment toggling and bracket matching follow it, and Angular TypeScript files and Markdown `ts` fences get the same injection.

Closes #5150

Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
2026-11-22 14:24:24 +08:00

30 lines
1.5 KiB
TypeScript

import { expect, test } from "./fixtures";
test("template literals highlight and comment in their embedded language", async ({ workbench: page }) => {
await page.evaluate(async () => {
const backendPath = "/src/editor/core/backend.ts";
const contributionsPath = "/src/workbench/contributions.ts";
const { docBackend } = await import(backendPath);
const { editorService } = await import(contributionsPath);
docBackend().files.set(
"/dev/acme-api/styles.ts",
"const Button = styled.button`\n padding: 4px;\n`;\nconst QUERY = gql`\n query Cart { total }\n`;\nconst plain = `margin: 0`;\n",
);
await editorService().openFile("/dev/acme-api/styles.ts");
});
const content = page.locator('.cm-editor[data-language="TypeScript"] .cm-content');
await expect(content).toBeVisible();
// Each word of an embedded language is a token of its own; a plain literal stays one string.
await expect(content.locator("span").filter({ hasText: /^padding$/ })).toBeVisible();
await expect(content.locator("span").filter({ hasText: /^query$/ })).toBeVisible();
await expect(content.locator("span").filter({ hasText: /^`margin: 0`$/ })).toBeVisible();
await content.evaluate((element) => {
const view = (element as HTMLElement & { cmTile: { view: import("@codemirror/view").EditorView } }).cmTile.view;
view.dispatch({ selection: { anchor: view.state.doc.toString().indexOf("query") } });
view.focus();
});
await page.keyboard.press("Meta+/");
await expect(content).toContainText("# query Cart { total }");
});