Add Dart syntax highlighting (#1326)

This commit is contained in:
Mohamed Boudra
2026-06-04 18:26:23 +08:00
committed by GitHub
parent de9d796567
commit 1daa4587e5
3 changed files with 21 additions and 0 deletions
@@ -46,6 +46,22 @@ describe("highlightCode", () => {
expect(stringToken?.style).toBe("string");
});
it("highlights Dart code", () => {
const code = 'class Greeter {\n final String message = "hello";\n}';
const result = highlightCode(code, "test.dart");
expect(result).toHaveLength(3);
const classToken = result[0].find((t) => t.text === "class");
expect(classToken?.style).toBe("keyword");
const finalToken = result[1].find((t) => t.text === "final");
expect(finalToken?.style).toBe("keyword");
const stringToken = result[1].find((t) => t.text.includes("hello"));
expect(stringToken?.style).toBe("string");
});
it("highlights TSX code with correct dialect", () => {
const code = 'const el = <div className="test">hello</div>;';
const result = highlightCode(code, "test.tsx");
@@ -14,6 +14,7 @@ describe("isLanguageSupported", () => {
expect(isLanguageSupported("test.html")).toBe(true);
expect(isLanguageSupported("test.java")).toBe(true);
expect(isLanguageSupported("test.swift")).toBe(true);
expect(isLanguageSupported("test.dart")).toBe(true);
expect(isLanguageSupported("test.ex")).toBe(true);
});
@@ -51,6 +52,7 @@ describe("getSupportedExtensions", () => {
expect(extensions).toContain("go");
expect(extensions).toContain("rs");
expect(extensions).toContain("swift");
expect(extensions).toContain("dart");
expect(extensions).toContain("json");
});
});
+3
View File
@@ -1,4 +1,5 @@
import { StreamLanguage } from "@codemirror/language";
import { dart } from "@codemirror/legacy-modes/mode/clike";
import { swift } from "@codemirror/legacy-modes/mode/swift";
import { parser as jsParser } from "@lezer/javascript";
import { parser as jsonParser } from "@lezer/json";
@@ -59,6 +60,8 @@ const parsersByExtension: Record<string, Parser> = {
rs: rustParser,
// Swift
swift: StreamLanguage.define(swift).parser,
// Dart
dart: StreamLanguage.define(dart).parser,
// Elixir
ex: elixirParser,
exs: elixirParser,