Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 2ab0d55c80 fix(vue): don't indent code in top-level <script> and <style> blocks
Pressing Enter in <script setup> indented the new line one level, because
the HTML base indents every element's children. WebStorm's Vue code style
indents the children of the top-level <template> only, so code in
<script> and <style> starts at the margin, as npm create vue writes it.

Fixes #3982
2026-11-17 21:10:26 +08:00
2 змінених файлів з 57 додано та 14 видалено

No files matched your search

+29
Переглянути файл
@@ -1,4 +1,6 @@
import { describe, expect, it } from "vitest";
import { getIndentation } from "@codemirror/language";
import { EditorState } from "@codemirror/state";
import { findLanguageDescription, loadSupport } from "./languages";
describe("vue language mode", () => {
@@ -23,4 +25,31 @@ describe("vue language mode", () => {
expect(tree).toContain("Declaration");
expect(tree).not.toContain("SassVariableName");
});
it("indents the top-level <template>'s children only, not <script setup> or <style> code", async () => {
const support = await loadSupport(findLanguageDescription("Vue", "/p/a.vue")!);
const doc = [
"<template>",
"<div>",
"</div>",
"</template>",
'<script setup lang="ts">',
"const a = 1",
"function f() {",
"return a",
"}",
"</script>",
"<style>",
".a { color: red; }",
"</style>",
].join("\n");
const state = EditorState.create({ doc, extensions: [support!, EditorState.tabSize.of(2)] });
const indent = (line: number) => getIndentation(state, state.doc.line(line).from);
expect(indent(2)).toBe(2);
expect(indent(6)).toBe(0);
expect(indent(7)).toBe(0);
expect(indent(8)).toBe(2);
expect(indent(10)).toBe(0);
expect(indent(12)).toBe(0);
});
});
+28 -14
Переглянути файл
@@ -4,24 +4,38 @@
* stock `vue()` parses every `<style>` as plain CSS, which loses the SCSS
* colours.
*/
import { LanguageSupport } from "@codemirror/language";
import { indentNodeProp, LanguageSupport, type LRLanguage, type TreeIndentContext } from "@codemirror/language";
import { html } from "@codemirror/lang-html";
import { sassLanguage } from "@codemirror/lang-sass";
import { vue } from "@codemirror/lang-vue";
/**
* WebStorm's Vue code style indents the children of the top-level `<template>` only: code in
* `<script setup>` or `<style>` starts at the margin, as `npm create vue` writes it.
*/
function elementIndent(context: TreeIndentContext): number | null {
const after = /^(\s*)(<\/)?/.exec(context.textAfter)!;
const node = context.node;
if (node.to <= context.pos + after[0].length) return context.continue();
const tag = node.firstChild?.getChild("TagName");
const flat =
node.parent?.name === "Document" && !!tag && context.state.sliceDoc(tag.from, tag.to).toLowerCase() !== "template";
return context.lineIndent(node.from) + (after[2] || flat ? 0 : context.unit);
}
export function vueLanguage(): LanguageSupport {
return vue({
base: html({
selfClosingTags: true,
// `<style>` without `lang` (or with `lang="css"`) keeps HTML's own CSS
// nesting; only an explicit SCSS/Sass dialect re-parses it.
nestedLanguages: [
{
tag: "style",
attrs: (a) => a.lang === "scss" || a.lang === "sass",
parser: sassLanguage.parser,
},
],
}),
const base = html({
selfClosingTags: true,
// `<style>` without `lang` (or with `lang="css"`) keeps HTML's own CSS
// nesting; only an explicit SCSS/Sass dialect re-parses it.
nestedLanguages: [
{
tag: "style",
attrs: (a) => a.lang === "scss" || a.lang === "sass",
parser: sassLanguage.parser,
},
],
});
const language = (base.language as LRLanguage).configure({ props: [indentNodeProp.add({ Element: elementIndent })] });
return vue({ base: new LanguageSupport(language, base.support) });
}