TypeScript
feat: add assembly file highlighting and language selection (#27899)
Lunabe5a887b939d
diff --git a/apps/mobile/src/lib/highlightLanguage.ts b/apps/mobile/src/lib/highlightLanguage.tsindex 6178f2470b9..f6065eea80f 100644--- a/apps/mobile/src/lib/highlightLanguage.ts+++ b/apps/mobile/src/lib/highlightLanguage.ts@@ -1,3 +1,4 @@+import "@t3tools/client-runtime/assembly-highlighting";import { getFiletypeFromFileName } from "@pierre/diffs/utils/getFiletypeFromFileName";import { HIGHLIGHTS_LANGUAGE_IDS } from "./highlights.generated";diff --git a/apps/mobile/src/lib/highlights.test.ts b/apps/mobile/src/lib/highlights.test.tsindex 83469ef745e..0867b293846 100644--- a/apps/mobile/src/lib/highlights.test.ts+++ b/apps/mobile/src/lib/highlights.test.ts@@ -2,6 +2,7 @@ import { codeToTokens } from "@pierre/highlights";import { pierreDark, pierreLight } from "@pierre/highlights/themes";import { describe, expect, it, vi } from "vite-plus/test";+import { resolveHighlightLanguage } from "./highlightLanguage";import { highlightCode, tokenizeCode } from "./highlights";vi.mock("expo", async () => {@@ -57,3 +58,20 @@ it("normalizes only requested lines from full-source records, including CRLF andexpect(source.highlightLine(2, "dark")?.[0]?.color).toBe("#737373");expect(source.highlightLine(100, "dark")).toBeUndefined();});++it.each([+ "asm",+ "assembly",+ "s",+ ...["asm", "ASM", "s", "S", "nasm", "NASM"].map((ext) => `src/start.${ext}`),+])("highlights assembly in %s", (hint) => {+ const language = resolveHighlightLanguage(+ hint.includes("/") ? { path: hint } : { language: hint },+ );+ const code = "mov eax, 42 ; answer\nret";+ for (const scheme of ["light", "dark"] as const) {+ const tokens = highlightCode(code, language, scheme);+ expect(tokens.map((line) => line.map((token) => token.content).join("")).join("\n")).toBe(code);+ expect(new Set(tokens.flat().map((token) => token.color)).size).toBeGreaterThan(1);+ }+});diff --git a/apps/web/src/composer-code-languages.ts b/apps/web/src/composer-code-languages.tsindex 641b75b6926..827a4f8faaf 100644--- a/apps/web/src/composer-code-languages.ts+++ b/apps/web/src/composer-code-languages.ts@@ -1,5 +1,6 @@export const CODE_BLOCK_LANGUAGES = [{ id: "", label: "Plain text" },+ { id: "asm", label: "Assembly" },{ id: "bash", label: "Bash" },{ id: "c", label: "C" },{ id: "cpp", label: "C++" },@@ -43,6 +44,7 @@ export const CODE_BLOCK_LANGUAGES = [export type CodeBlockLanguage = { readonly id: string; readonly label: string };const LANGUAGE_ALIASES: Record<string, string> = {+ assembly: "asm",cs: "csharp","c++": "cpp",js: "javascript",@@ -51,6 +53,7 @@ const LANGUAGE_ALIASES: Record<string, string> = {py: "python",rb: "ruby",rs: "rust",+ s: "asm",sh: "bash",shell: "bash",text: "",diff --git a/apps/web/src/lib/syntaxHighlighting.test.ts b/apps/web/src/lib/syntaxHighlighting.test.tsnew file mode 100644index 00000000000..f97a73f106b--- /dev/null+++ b/apps/web/src/lib/syntaxHighlighting.test.ts@@ -0,0 +1,21 @@+import { getFiletypeFromFileName } from "@pierre/diffs";+import { expect, it } from "vite-plus/test";++import { getSyntaxHighlighterPromise } from "./syntaxHighlighting";+import { DIFF_THEME_NAMES } from "./diffRendering";++it.each([+ "asm",+ "assembly",+ "s",+ ...["asm", "ASM", "s", "S", "nasm", "NASM"].map((ext) => `src/start.${ext}`),+])("highlights assembly in %s", async (hint) => {+ const language = hint.includes("/") ? getFiletypeFromFileName(hint) : hint;+ const highlighter = await getSyntaxHighlighterPromise(language);+ const code = "mov eax, 42 ; answer\nret";+ for (const theme of Object.values(DIFF_THEME_NAMES)) {+ const { tokens } = highlighter.codeToTokens(code, { lang: language, theme });+ expect(tokens.map((line) => line.map((token) => token.content).join("")).join("\n")).toBe(code);+ expect(new Set(tokens.flat().map((token) => token.color)).size).toBeGreaterThan(1);+ }+});diff --git a/apps/web/src/lib/syntaxHighlighting.ts b/apps/web/src/lib/syntaxHighlighting.tsindex 9d1c8841054..423312efa59 100644--- a/apps/web/src/lib/syntaxHighlighting.ts+++ b/apps/web/src/lib/syntaxHighlighting.ts@@ -1,3 +1,4 @@+import "@t3tools/client-runtime/assembly-highlighting";import {getSharedHighlighter,type DiffsHighlighter,diff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.jsonindex 9600ad7ad35..d1b48e1d76f 100644--- a/packages/client-runtime/package.json+++ b/packages/client-runtime/package.json@@ -3,6 +3,10 @@"private": true,"type": "module","exports": {+ "./assembly-highlighting": {+ "types": "./src/assemblyHighlighting.ts",+ "default": "./src/assemblyHighlighting.ts"+ },"./zip/bounded-zip": {"types": "./src/zip/boundedZip.ts","default": "./src/zip/boundedZip.ts"diff --git a/packages/client-runtime/src/assemblyHighlighting.ts b/packages/client-runtime/src/assemblyHighlighting.tsnew file mode 100644index 00000000000..88dd6fe0dd8--- /dev/null+++ b/packages/client-runtime/src/assemblyHighlighting.ts@@ -0,0 +1,6 @@+import { setCustomExtension } from "@pierre/diffs/utils/getFiletypeFromFileName";++// The default .s mapping is riscv, which the Highlights backend does not support.+for (const extension of ["asm", "ASM", "s", "S", "nasm", "NASM"]) {+ setCustomExtension(extension, "asm");+}