xonecas.io
64875
Mc

mini-coder / files

mini-coder / src / tui / highlight.ts

7.0 KB
1import { readFileSync } from "node:fs";
2import { createRequire } from "node:module";
3import { dirname, join } from "node:path";
4import Parser from "tree-sitter";
5import JavaScript from "tree-sitter-javascript";
6import TypeScript from "tree-sitter-typescript";
7import Markdown from "@tree-sitter-grammars/tree-sitter-markdown";
8import { HEADINGS, INLINE_CODE, NORMAL_BG, NORMAL_FG, STYLES, channels, sgrPlain, type Style } from "./theme.ts";
9
10/**
11 * Syntax highlighting for committed scrollback, once, over the whole block.
12 * Grammar queries ship with the grammar packages and are consumed as data, so
13 * a capture name is the only thing that reaches the palette. The colours come
14 * from the TokyoNight `night` palette in `theme.ts`, mapped the way
15 * `folke/tokyonight.nvim` maps capture names to highlight groups. Truecolor only.
16 */
17
18function styleFor(capture: string): Style | undefined {
19  for (let name = capture; ; name = name.slice(0, name.lastIndexOf("."))) {
20    const style = STYLES[name];
21    if (style !== undefined) return style;
22    if (!name.includes(".")) return undefined;
23  }
24}
25
26const require = createRequire(import.meta.url);
27
28/** A grammar package keeps its queries next to its sources. */
29function querySource(pkg: string, ...path: string[]): string {
30  return readFileSync(join(dirname(require.resolve(`${pkg}/package.json`)), ...path), "utf8");
31}
32
33interface Syntax {
34  parser: Parser;
35  query: Parser.Query;
36}
37
38function syntax(language: Parser.Language, ...sources: string[]): Syntax {
39  const parser = new Parser();
40  parser.setLanguage(language);
41  return { parser, query: new Parser.Query(language, sources.join("\n")) };
42}
43
44const JS_QUERIES = querySource("tree-sitter-javascript", "queries", "highlights.scm");
45const TS_QUERIES = querySource("tree-sitter-typescript", "queries", "highlights.scm");
46
47const JAVASCRIPT = syntax(JavaScript, JS_QUERIES);
48/** TypeScript's own query only covers what it adds to JavaScript. */
49const TYPESCRIPT = syntax(TypeScript.typescript, JS_QUERIES, TS_QUERIES);
50const TSX = syntax(TypeScript.tsx, JS_QUERIES, TS_QUERIES);
51
52/** Fence info string to grammar, for the languages we carry. */
53const CODE: Record<string, Syntax | undefined> = {
54  js: JAVASCRIPT,
55  javascript: JAVASCRIPT,
56  jsx: JAVASCRIPT,
57  ts: TYPESCRIPT,
58  typescript: TYPESCRIPT,
59  tsx: TSX,
60};
61
62const MARKDOWN = syntax(
63  Markdown,
64  querySource("@tree-sitter-grammars/tree-sitter-markdown", "tree-sitter-markdown", "queries", "highlights.scm"),
65);
66const MARKDOWN_INLINE = syntax(
67  Markdown.inline,
68  querySource(
69    "@tree-sitter-grammars/tree-sitter-markdown",
70    "tree-sitter-markdown-inline",
71    "queries",
72    "highlights.scm",
73  ),
74);
75
76/** One styled range of a line, in byte offsets into that line. */
77interface Span {
78  start: number;
79  end: number;
80  style: Style;
81}
82
83function spansOf(syntax: Syntax, node: Parser.SyntaxNode, offset = 0): Span[] {
84  const spans: Span[] = [];
85  for (const capture of syntax.query.captures(node)) {
86    const style = styleFor(capture.name);
87    if (style === undefined) continue;
88    spans.push({ start: offset + capture.node.startIndex, end: offset + capture.node.endIndex, style });
89  }
90  return spans;
91}
92
93function parse(syntax: Syntax, text: string): Parser.SyntaxNode {
94  return syntax.parser.parse(text).rootNode;
95}
96
97/**
98 * A style's own attributes, with the defaults spelled out: a span that names no
99 * color must not inherit the color of the span or token before it, and the
100 * fallback is the palette's `Normal`, never the terminal's own pair.
101 */
102function sgr(style: Style): string {
103  let out = "\x1b[";
104  if (style.bold) out += "1;";
105  if (style.italic) out += "3;";
106  if (style.underline) out += "4;";
107  out += `38;2;${channels(style.fg ?? NORMAL_FG)};48;2;${channels(style.bg ?? NORMAL_BG)}m`;
108  return out;
109}
110
111/**
112 * Wraps every span in its color. Spans nest, so the innermost one wins; text
113 * outside any span falls back to `Normal`. The trailing close restores the same
114 * pair, so a span may cross a line break and the last line still ends plain —
115 * and no cell is ever left to the terminal. Because each row is painted on its
116 * own, a span that outlives a line break re-asserts itself on the next line.
117 */
118function paint(text: string, spans: Span[]): string {
119  const events: { at: number; open: boolean; span: Span }[] = [];
120  for (const span of spans) {
121    if (span.end <= span.start) continue;
122    events.push({ at: span.start, open: true, span }, { at: span.end, open: false, span });
123  }
124  events.sort((a, b) => a.at - b.at || Number(a.open) - Number(b.open));
125
126  const active: Span[] = [];
127  let out = "";
128  let plain = true;
129  let at = 0;
130  const emit = (end: number): void => {
131    if (end <= at) return;
132    const style = active.length === 0 ? undefined : active[active.length - 1].style;
133    if (style === undefined) {
134      if (!plain) out += sgrPlain();
135      plain = true;
136    } else {
137      out += sgr(style);
138      plain = false;
139    }
140    const chunk = text.slice(at, end);
141    out += style === undefined ? chunk : chunk.replace(/\n/g, `\n${sgr(style)}`);
142    at = end;
143  };
144  for (const event of events) {
145    emit(event.at);
146    if (event.open) active.push(event.span);
147    else active.splice(active.indexOf(event.span), 1);
148  }
149  emit(text.length);
150  return plain ? out : `${out}${sgrPlain()}`;
151}
152
153/**
154 * Colors one committed block of Markdown. Inline markup is a separate grammar,
155 * run over the ranges the block grammar hands over, as its injections do.
156 */
157export function highlightMarkdown(text: string): string {
158  const root = parse(MARKDOWN, text);
159  let spans = spansOf(MARKDOWN, root);
160  // Two things the grammar's queries predate: a whole heading is colored by its
161  // level, and inline code carries a background. Both replace what covers them.
162  for (const inline of root.descendantsOfType("inline")) {
163    const inlineRoot = parse(MARKDOWN_INLINE, inline.text);
164    spans.push(...spansOf(MARKDOWN_INLINE, inlineRoot, inline.startIndex));
165    for (const code of inlineRoot.descendantsOfType("code_span")) {
166      spans = recolor(spans, code, INLINE_CODE, inline.startIndex);
167    }
168  }
169  for (const heading of root.descendantsOfType(["atx_heading", "setext_heading"])) {
170    const marker = heading.namedChildren.find((child) => /^(atx|setext)_h/.test(child.type))?.type;
171    const level = marker === undefined ? 1 : Number(marker.replace(/\D/g, ""));
172    spans = recolor(spans, heading, HEADINGS[Math.min(level, HEADINGS.length) - 1]);
173  }
174  return paint(text, spans);
175}
176
177/** Replaces every span inside `node` with one span covering the whole node. */
178function recolor(spans: Span[], node: Parser.SyntaxNode, style: Style, offset = 0): Span[] {
179  const start = offset + node.startIndex;
180  const end = offset + node.endIndex;
181  const kept = spans.filter((span) => span.start < start || span.end > end);
182  return [...kept, { start, end, style }];
183}
184
185/** Colors a fenced code block's body, by its fence info string. */
186export function highlightCode(info: string, text: string): string {
187  const grammar = CODE[info.trim().toLowerCase()];
188  return grammar === undefined ? text : paint(text, spansOf(grammar, parse(grammar, text)));
189}