feat: add C# syntax highlighting (#1651)
Wire the @replit/codemirror-lang-csharp Lezer grammar into @getpaseo/highlight following the existing per-language pattern (Java/Rust/Swift). Map the cs extension to the parser and alias the csharp and c# markdown fence names so fenced code blocks highlight. Register csharpLanguage.parser rather than the package's raw parser export: the highlight styleTags are applied only inside csharpLanguage (via parser.configure), so the raw parser would parse but render unstyled. Closes #1527 Co-authored-by: Clemens Wagner <wagner.clemens@gmx.de> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Clemens Wagner
Claude Opus 4.8
parent
85322c5968
commit
6277ba1ff9
Generated
+16
@@ -11166,6 +11166,21 @@
|
|||||||
"nanoid": "^3.3.11"
|
"nanoid": "^3.3.11"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@replit/codemirror-lang-csharp": {
|
||||||
|
"version": "6.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@replit/codemirror-lang-csharp/-/codemirror-lang-csharp-6.2.0.tgz",
|
||||||
|
"integrity": "sha512-6utbaWkoymhoAXj051mkRp+VIJlpwUgCX9Toevz3YatiZsz512fw3OVCedXQx+WcR0wb6zVHjChnuxqfCLtFVQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"peerDependencies": {
|
||||||
|
"@codemirror/autocomplete": "^6.0.0",
|
||||||
|
"@codemirror/language": "^6.0.0",
|
||||||
|
"@codemirror/state": "^6.0.0",
|
||||||
|
"@codemirror/view": "^6.0.0",
|
||||||
|
"@lezer/common": "^1.0.0",
|
||||||
|
"@lezer/highlight": "^1.0.0",
|
||||||
|
"@lezer/lr": "^1.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@rollup/pluginutils": {
|
"node_modules/@rollup/pluginutils": {
|
||||||
"version": "5.3.0",
|
"version": "5.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.3.0.tgz",
|
||||||
@@ -36989,6 +37004,7 @@
|
|||||||
"@lezer/rust": "^1.0.2",
|
"@lezer/rust": "^1.0.2",
|
||||||
"@lezer/xml": "^1.0.6",
|
"@lezer/xml": "^1.0.6",
|
||||||
"@lezer/yaml": "^1.0.4",
|
"@lezer/yaml": "^1.0.4",
|
||||||
|
"@replit/codemirror-lang-csharp": "^6.2.0",
|
||||||
"lezer-elixir": "^1.1.2"
|
"lezer-elixir": "^1.1.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ const LANGUAGE_ALIASES: Record<string, string> = {
|
|||||||
rust: "rs",
|
rust: "rs",
|
||||||
golang: "go",
|
golang: "go",
|
||||||
"c++": "cpp",
|
"c++": "cpp",
|
||||||
|
csharp: "cs",
|
||||||
|
"c#": "cs",
|
||||||
objc: "m",
|
objc: "m",
|
||||||
"objective-c": "m",
|
"objective-c": "m",
|
||||||
markdown: "md",
|
markdown: "md",
|
||||||
|
|||||||
@@ -44,6 +44,7 @@
|
|||||||
"@lezer/rust": "^1.0.2",
|
"@lezer/rust": "^1.0.2",
|
||||||
"@lezer/xml": "^1.0.6",
|
"@lezer/xml": "^1.0.6",
|
||||||
"@lezer/yaml": "^1.0.4",
|
"@lezer/yaml": "^1.0.4",
|
||||||
|
"@replit/codemirror-lang-csharp": "^6.2.0",
|
||||||
"lezer-elixir": "^1.1.2"
|
"lezer-elixir": "^1.1.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -62,6 +62,19 @@ describe("highlightCode", () => {
|
|||||||
expect(stringToken?.style).toBe("string");
|
expect(stringToken?.style).toBe("string");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("highlights C# code", () => {
|
||||||
|
const code = 'class Greeter {\n string message = "hello";\n}';
|
||||||
|
const result = highlightCode(code, "test.cs");
|
||||||
|
|
||||||
|
expect(result).toHaveLength(3);
|
||||||
|
|
||||||
|
const classToken = result[0].find((t) => t.text === "class");
|
||||||
|
expect(classToken?.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", () => {
|
it("highlights TSX code with correct dialect", () => {
|
||||||
const code = 'const el = <div className="test">hello</div>;';
|
const code = 'const el = <div className="test">hello</div>;';
|
||||||
const result = highlightCode(code, "test.tsx");
|
const result = highlightCode(code, "test.tsx");
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ describe("isLanguageSupported", () => {
|
|||||||
expect(isLanguageSupported("test.java")).toBe(true);
|
expect(isLanguageSupported("test.java")).toBe(true);
|
||||||
expect(isLanguageSupported("test.swift")).toBe(true);
|
expect(isLanguageSupported("test.swift")).toBe(true);
|
||||||
expect(isLanguageSupported("test.dart")).toBe(true);
|
expect(isLanguageSupported("test.dart")).toBe(true);
|
||||||
|
expect(isLanguageSupported("test.cs")).toBe(true);
|
||||||
expect(isLanguageSupported("test.ex")).toBe(true);
|
expect(isLanguageSupported("test.ex")).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -53,6 +54,7 @@ describe("getSupportedExtensions", () => {
|
|||||||
expect(extensions).toContain("rs");
|
expect(extensions).toContain("rs");
|
||||||
expect(extensions).toContain("swift");
|
expect(extensions).toContain("swift");
|
||||||
expect(extensions).toContain("dart");
|
expect(extensions).toContain("dart");
|
||||||
|
expect(extensions).toContain("cs");
|
||||||
expect(extensions).toContain("json");
|
expect(extensions).toContain("json");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import { parser as phpParser } from "@lezer/php";
|
|||||||
import { parser as rustParser } from "@lezer/rust";
|
import { parser as rustParser } from "@lezer/rust";
|
||||||
import { parser as xmlParser } from "@lezer/xml";
|
import { parser as xmlParser } from "@lezer/xml";
|
||||||
import { parser as yamlParser } from "@lezer/yaml";
|
import { parser as yamlParser } from "@lezer/yaml";
|
||||||
|
import { csharpLanguage } from "@replit/codemirror-lang-csharp";
|
||||||
import { parser as elixirParser } from "lezer-elixir";
|
import { parser as elixirParser } from "lezer-elixir";
|
||||||
import type { Parser } from "@lezer/common";
|
import type { Parser } from "@lezer/common";
|
||||||
|
|
||||||
@@ -62,6 +63,8 @@ const parsersByExtension: Record<string, Parser> = {
|
|||||||
swift: StreamLanguage.define(swift).parser,
|
swift: StreamLanguage.define(swift).parser,
|
||||||
// Dart
|
// Dart
|
||||||
dart: StreamLanguage.define(dart).parser,
|
dart: StreamLanguage.define(dart).parser,
|
||||||
|
// C#
|
||||||
|
cs: csharpLanguage.parser,
|
||||||
// Elixir
|
// Elixir
|
||||||
ex: elixirParser,
|
ex: elixirParser,
|
||||||
exs: elixirParser,
|
exs: elixirParser,
|
||||||
|
|||||||
Reference in New Issue
Block a user