Skip to content

Commit 78cddd4

Browse files
committed
feat: add assembly file highlighting and language selection
1 parent d2d1e4f commit 78cddd4

7 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎apps/mobile/src/lib/highlightLanguage.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import "@t3tools/client-runtime/assembly-highlighting";
12
import { getFiletypeFromFileName } from "@pierre/diffs/utils/getFiletypeFromFileName";
23

34
import { HIGHLIGHTS_LANGUAGE_IDS } from "./highlights.generated";

‎apps/mobile/src/lib/highlights.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { codeToTokens } from "@pierre/highlights";
22
import { pierreDark, pierreLight } from "@pierre/highlights/themes";
33
import { describe, expect, it, vi } from "vite-plus/test";
44

5+
import { resolveHighlightLanguage } from "./highlightLanguage";
56
import { highlightCode, tokenizeCode } from "./highlights";
67

78
vi.mock("expo", async () => {
@@ -57,3 +58,20 @@ it("normalizes only requested lines from full-source records, including CRLF and
5758
expect(source.highlightLine(2, "dark")?.[0]?.color).toBe("#737373");
5859
expect(source.highlightLine(100, "dark")).toBeUndefined();
5960
});
61+
62+
it.each([
63+
"asm",
64+
"assembly",
65+
"s",
66+
...["asm", "ASM", "s", "S", "nasm", "NASM"].map((ext) => `src/start.${ext}`),
67+
])("highlights assembly in %s", (hint) => {
68+
const language = resolveHighlightLanguage(
69+
hint.includes("/") ? { path: hint } : { language: hint },
70+
);
71+
const code = "mov eax, 42 ; answer\nret";
72+
for (const scheme of ["light", "dark"] as const) {
73+
const tokens = highlightCode(code, language, scheme);
74+
expect(tokens.map((line) => line.map((token) => token.content).join("")).join("\n")).toBe(code);
75+
expect(new Set(tokens.flat().map((token) => token.color)).size).toBeGreaterThan(1);
76+
}
77+
});

‎apps/web/src/composer-code-languages.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const CODE_BLOCK_LANGUAGES = [
22
{ id: "", label: "Plain text" },
3+
{ id: "asm", label: "Assembly" },
34
{ id: "bash", label: "Bash" },
45
{ id: "c", label: "C" },
56
{ id: "cpp", label: "C++" },
@@ -43,6 +44,7 @@ export const CODE_BLOCK_LANGUAGES = [
4344
export type CodeBlockLanguage = { readonly id: string; readonly label: string };
4445

4546
const LANGUAGE_ALIASES: Record<string, string> = {
47+
assembly: "asm",
4648
cs: "csharp",
4749
"c++": "cpp",
4850
js: "javascript",
@@ -51,6 +53,7 @@ const LANGUAGE_ALIASES: Record<string, string> = {
5153
py: "python",
5254
rb: "ruby",
5355
rs: "rust",
56+
s: "asm",
5457
sh: "bash",
5558
shell: "bash",
5659
text: "",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getFiletypeFromFileName } from "@pierre/diffs";
2+
import { expect, it } from "vite-plus/test";
3+
4+
import { getSyntaxHighlighterPromise } from "./syntaxHighlighting";
5+
import { DIFF_THEME_NAMES } from "./diffRendering";
6+
7+
it.each([
8+
"asm",
9+
"assembly",
10+
"s",
11+
...["asm", "ASM", "s", "S", "nasm", "NASM"].map((ext) => `src/start.${ext}`),
12+
])("highlights assembly in %s", async (hint) => {
13+
const language = hint.includes("/") ? getFiletypeFromFileName(hint) : hint;
14+
const highlighter = await getSyntaxHighlighterPromise(language);
15+
const code = "mov eax, 42 ; answer\nret";
16+
for (const theme of Object.values(DIFF_THEME_NAMES)) {
17+
const { tokens } = highlighter.codeToTokens(code, { lang: language, theme });
18+
expect(tokens.map((line) => line.map((token) => token.content).join("")).join("\n")).toBe(code);
19+
expect(new Set(tokens.flat().map((token) => token.color)).size).toBeGreaterThan(1);
20+
}
21+
});

‎apps/web/src/lib/syntaxHighlighting.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import "@t3tools/client-runtime/assembly-highlighting";
12
import {
23
getSharedHighlighter,
34
type DiffsHighlighter,

‎packages/client-runtime/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"private": true,
44
"type": "module",
55
"exports": {
6+
"./assembly-highlighting": {
7+
"types": "./src/assemblyHighlighting.ts",
8+
"default": "./src/assemblyHighlighting.ts"
9+
},
610
"./zip/bounded-zip": {
711
"types": "./src/zip/boundedZip.ts",
812
"default": "./src/zip/boundedZip.ts"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { setCustomExtension } from "@pierre/diffs/utils/getFiletypeFromFileName";
2+
3+
// The default .s mapping is riscv, which the Highlights backend does not support.
4+
for (const extension of ["asm", "ASM", "s", "S", "nasm", "NASM"]) {
5+
setCustomExtension(extension, "asm");
6+
}

0 commit comments

Comments
 (0)