fix(editor): highlight <style lang="less"> in Vue files with the Less grammar (#5818)

A Vue file's <style> block switched grammar only for lang="scss" and
lang="sass"; lang="less" was parsed as plain CSS, so Less variables,
nested rules and mixin calls lost their highlighting. It now uses
@codemirror/lang-less, which was already in the lockfile through
@codemirror/language-data and is now a direct dependency.

Fixes #5745
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-25 16:03:55 +08:00
1 джерело 89fa6cb02c
коміт 9e6c29a7b7
4 files changed
+20 -4

No files matched your search

+1
Переглянути файл
@@ -52,6 +52,7 @@
"@codemirror/lang-html": "^6.4.12",
"@codemirror/lang-java": "^6.0.2",
"@codemirror/lang-javascript": "^6.2.5",
"@codemirror/lang-less": "^6.0.2",
"@codemirror/lang-markdown": "^6.5.2",
"@codemirror/lang-sass": "^6.0.2",
"@codemirror/lang-sql": "^6.10.0",
Створено
+3
Переглянути файл
@@ -31,6 +31,9 @@ importers:
'@codemirror/lang-javascript':
specifier: ^6.2.5
version: 6.2.5
'@codemirror/lang-less':
specifier: ^6.0.2
version: 6.0.2
'@codemirror/lang-markdown':
specifier: ^6.5.2
version: 6.5.2
+10
Переглянути файл
@@ -16,6 +16,16 @@ describe("vue language mode", () => {
expect(tree).toMatch(/RuleSet\(ClassSelector.*RuleSet\(ClassSelector/s);
});
it('highlights Less inside <style lang="less">', async () => {
const support = await loadSupport(findLanguageDescription("Vue", "/p/a.vue")!);
const src = `<template><div/></template>\n<style lang="less">\n@c: red;\n.a { .b { color: @c; } .m(); }\n</style>`;
const tree = support!.language.parser.parse(src).toString();
// Less-only syntax: a top-level @-variable declaration, a mixin call and a nested rule.
expect(tree).toContain("StyleSheet(Declaration(AtKeyword");
expect(tree).toContain("Inclusion(ClassSelector");
expect(tree).toMatch(/RuleSet\(ClassSelector.*RuleSet\(ClassSelector/s);
});
it("still parses a plain <style> block as CSS", async () => {
const support = await loadSupport(findLanguageDescription("Vue", "/p/a.vue")!);
const tree = support!.language.parser
+6 -4
Переглянути файл
@@ -1,11 +1,12 @@
/**
* Vue single-file components: `@codemirror/lang-vue`'s parser over an HTML
* base that also highlights `<style lang="scss|sass">` with the Sass grammar —
* stock `vue()` parses every `<style>` as plain CSS, which loses the SCSS
* colours.
* base that also highlights `<style lang="scss|sass">` with the Sass grammar
* and `<style lang="less">` with the Less one — stock `vue()` parses every
* `<style>` as plain CSS, which loses their variables, nesting and mixins.
*/
import { indentNodeProp, LanguageSupport, type LRLanguage, type TreeIndentContext } from "@codemirror/language";
import { html } from "@codemirror/lang-html";
import { lessLanguage } from "@codemirror/lang-less";
import { sassLanguage } from "@codemirror/lang-sass";
import { vue } from "@codemirror/lang-vue";
@@ -27,13 +28,14 @@ export function vueLanguage(): LanguageSupport {
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.
// nesting; only an explicit SCSS/Sass/Less dialect re-parses it.
nestedLanguages: [
{
tag: "style",
attrs: (a) => a.lang === "scss" || a.lang === "sass",
parser: sassLanguage.parser,
},
{ tag: "style", attrs: (a) => a.lang === "less", parser: lessLanguage.parser },
],
});
const language = (base.language as LRLanguage).configure({ props: [indentNodeProp.add({ Element: elementIndent })] });