7814
Ta
tiny-c-agent / files
tiny-c-agent / src / tui / highlight.c
12.6 KB
1#include "highlight.h" 2 3#include <stdio.h> 4#include <stdlib.h> 5#include <string.h> 6 7#include <tree_sitter/api.h> 8 9#include <tree_sitter/tree-sitter-c.h> 10#include <tree_sitter/tree-sitter-python.h> 11#include <tree_sitter/tree-sitter-javascript.h> 12#include <tree_sitter/tree-sitter-bash.h> 13#include <tree_sitter/tree-sitter-markdown.h> 14#include <tree_sitter/tree-sitter-markdown-inline.h> 15 16#include "tree-sitter-make.h" 17#include "tree-sitter-typescript.h" 18#include "tree-sitter-tsx.h" 19 20#include "queries.h" 21#include "util.h" 22#include "theme.h" 23 24typedef struct { 25 TSParser *parser; 26 TSQuery *query; 27} syntax; 28 29static syntax *syntax_open(const TSLanguage *lang, const char *name) { 30 if (!lang) return NULL; 31 TSParser *parser = ts_parser_new(); 32 if (!ts_parser_set_language(parser, lang)) { 33 ts_parser_delete(parser); 34 return NULL; 35 } 36 const char *src = tiny_query_for(name); 37 if (!src) { 38 ts_parser_delete(parser); 39 return NULL; 40 } 41 uint32_t error_offset = 0; 42 TSQueryError error_type = TSQueryErrorNone; 43 TSQuery *query = ts_query_new(lang, src, (uint32_t)strlen(src), &error_offset, &error_type); 44 if (!query) { 45 ts_parser_delete(parser); 46 return NULL; 47 } 48 syntax *s = malloc(sizeof *s); 49 if (!s) abort(); 50 s->parser = parser; 51 s->query = query; 52 return s; 53} 54 55// Lazily built grammars; `seen` records that we tried, so a missing grammar is 56// not re-probed on every block. 57static syntax *C, *PY, *JS, *BASH, *MAKE, *TS, *TSX, *MD, *MD_INLINE; 58static bool c_seen, py_seen, js_seen, bash_seen, make_seen, ts_seen, tsx_seen, md_seen, md_inline_seen; 59 60static syntax *grammar_c(void) { 61 if (!c_seen) { 62 c_seen = true; 63 C = syntax_open(tree_sitter_c(), "c"); 64 } 65 return C; 66} 67 68static syntax *grammar_python(void) { 69 if (!py_seen) { 70 py_seen = true; 71 PY = syntax_open(tree_sitter_python(), "python"); 72 } 73 return PY; 74} 75 76static syntax *grammar_js(void) { 77 if (!js_seen) { 78 js_seen = true; 79 JS = syntax_open(tree_sitter_javascript(), "javascript"); 80 } 81 return JS; 82} 83 84static syntax *grammar_bash(void) { 85 if (!bash_seen) { 86 bash_seen = true; 87 BASH = syntax_open(tree_sitter_bash(), "bash"); 88 } 89 return BASH; 90} 91 92static syntax *grammar_make(void) { 93 if (!make_seen) { 94 make_seen = true; 95 MAKE = syntax_open(tree_sitter_make(), "make"); 96 } 97 return MAKE; 98} 99 100static syntax *grammar_ts(void) { 101 if (!ts_seen) { 102 ts_seen = true; 103 TS = syntax_open(tree_sitter_typescript(), "typescript"); 104 } 105 return TS; 106} 107 108static syntax *grammar_tsx(void) { 109 if (!tsx_seen) { 110 tsx_seen = true; 111 TSX = syntax_open(tree_sitter_tsx(), "typescript"); 112 } 113 return TSX; 114} 115 116static syntax *grammar_md(void) { 117 if (!md_seen) { 118 md_seen = true; 119 MD = syntax_open(tree_sitter_markdown(), "markdown"); 120 } 121 return MD; 122} 123 124static syntax *grammar_md_inline(void) { 125 if (!md_inline_seen) { 126 md_inline_seen = true; 127 MD_INLINE = syntax_open(tree_sitter_markdown_inline(), "markdown_inline"); 128 } 129 return MD_INLINE; 130} 131 132// ---- spans ----------------------------------------------------------------- 133 134typedef struct { 135 size_t start; 136 size_t end; 137 const tiny_style *style; 138} span; 139 140typedef struct { 141 span *v; 142 size_t n; 143 size_t cap; 144} spanlist; 145 146static void span_push(spanlist *l, size_t start, size_t end, const tiny_style *style) { 147 if (l->n == l->cap) { 148 l->cap = l->cap ? l->cap * 2 : 16; 149 l->v = realloc(l->v, l->cap * sizeof *l->v); 150 if (!l->v) abort(); 151 } 152 l->v[l->n++] = (span){start, end, style}; 153} 154 155static void spans_of(syntax *s, TSNode node, size_t offset, spanlist *out) { 156 TSQueryCursor *cur = ts_query_cursor_new(); 157 ts_query_cursor_exec(cur, s->query, node); 158 TSQueryMatch m; 159 while (ts_query_cursor_next_match(cur, &m)) { 160 for (uint16_t i = 0; i < m.capture_count; i++) { 161 TSQueryCapture cap = m.captures[i]; 162 uint32_t len = 0; 163 const char *name = ts_query_capture_name_for_id(s->query, cap.index, &len); 164 char namebuf[128]; 165 if (len >= sizeof namebuf) len = sizeof namebuf - 1; 166 memcpy(namebuf, name, len); 167 namebuf[len] = '\0'; 168 const tiny_style *style = tiny_capture_style(namebuf); 169 if (!style) continue; 170 span_push(out, offset + ts_node_start_byte(cap.node), offset + ts_node_end_byte(cap.node), style); 171 } 172 } 173 ts_query_cursor_delete(cur); 174} 175 176// Descendant nodes whose type is one of `types`, appended to `out`. 177static void collect_types(TSNode n, const char *const *types, size_t ntypes, 178 TSNode **out, size_t *len, size_t *cap) { 179 const char *t = ts_node_type(n); 180 for (size_t i = 0; i < ntypes; i++) { 181 if (strcmp(t, types[i]) == 0) { 182 if (*len == *cap) { 183 *cap = *cap ? *cap * 2 : 8; 184 *out = realloc(*out, *cap * sizeof **out); 185 if (!*out) abort(); 186 } 187 (*out)[(*len)++] = n; 188 break; 189 } 190 } 191 uint32_t count = ts_node_child_count(n); 192 for (uint32_t i = 0; i < count; i++) 193 collect_types(ts_node_child(n, i), types, ntypes, out, len, cap); 194} 195 196// Parses `text` and hands the caller the tree to free once every node from it 197// is done being used. 198static TSNode parse(syntax *s, const char *text, size_t len, TSTree **tree_out) { 199 TSTree *tree = ts_parser_parse_string(s->parser, NULL, text, (uint32_t)len); 200 *tree_out = tree; 201 return ts_tree_root_node(tree); 202} 203 204static void sgr_of(tui_buf *b, const tiny_style *style) { 205 if (style) sgr_style(b, style); 206 else sgr_plain(b); 207} 208 209// Writes text[at, end) with the innermost active span's colour, re-asserting it 210// after each newline, and advances `at`. 211static void paint_emit(tui_buf *out, const char *text, size_t text_len, size_t *at, size_t end, 212 const span *const *active, size_t nactive, bool *plain) { 213 if (end <= *at) return; 214 const tiny_style *style = nactive ? active[nactive - 1]->style : NULL; 215 if (!style) { 216 if (!*plain) sgr_plain(out); 217 *plain = true; 218 } else { 219 sgr_of(out, style); 220 *plain = false; 221 } 222 for (size_t i = *at; i < end && i < text_len; i++) { 223 buf_addc(out, text[i]); 224 if (text[i] == '\n' && style) sgr_of(out, style); 225 } 226 *at = end; 227} 228 229// Wraps every span in its colour, innermost wins, text outside falls back to 230// Normal. A span may cross a line break and re-asserts itself after each one. 231static char *paint(const char *text, span *spans, size_t nspans, size_t text_len) { 232 typedef struct { 233 size_t at; 234 bool open; 235 size_t idx; 236 } event; 237 238 event *ev = malloc((nspans ? nspans : 1) * 2 * sizeof *ev); 239 if (!ev) abort(); 240 size_t nev = 0; 241 for (size_t i = 0; i < nspans; i++) { 242 if (spans[i].end <= spans[i].start) continue; 243 ev[nev++] = (event){spans[i].start, true, i}; 244 ev[nev++] = (event){spans[i].end, false, i}; 245 } 246 // Insertion sort by position, closing before opening at the same offset. 247 for (size_t i = 1; i < nev; i++) { 248 event key = ev[i]; 249 size_t j = i; 250 while (j > 0 && 251 (ev[j - 1].at > key.at || (ev[j - 1].at == key.at && ev[j - 1].open && !key.open))) { 252 ev[j] = ev[j - 1]; 253 j--; 254 } 255 ev[j] = key; 256 } 257 258 const span *active[64]; 259 size_t nactive = 0; 260 tui_buf out; 261 buf_init(&out); 262 bool plain = true; 263 size_t at = 0; 264 265 for (size_t e = 0; e < nev; e++) { 266 paint_emit(&out, text, text_len, &at, ev[e].at, active, nactive, &plain); 267 if (ev[e].open) { 268 if (nactive < sizeof active / sizeof active[0]) active[nactive++] = &spans[ev[e].idx]; 269 } else { 270 for (size_t i = 0; i < nactive; i++) { 271 if (active[i] == &spans[ev[e].idx]) { 272 memmove(&active[i], &active[i + 1], (nactive - i - 1) * sizeof active[0]); 273 nactive--; 274 break; 275 } 276 } 277 } 278 } 279 paint_emit(&out, text, text_len, &at, text_len, active, nactive, &plain); 280 free(ev); 281 if (!plain) sgr_plain(&out); 282 if (!out.data) return strdup(""); 283 return buf_steal(&out); 284} 285 286// Replaces every span entirely inside [start, end) with one covering span. 287static void recolor(spanlist *l, size_t start, size_t end, const tiny_style *style) { 288 size_t w = 0; 289 for (size_t i = 0; i < l->n; i++) { 290 if (l->v[i].start >= start && l->v[i].end <= end) continue; 291 l->v[w++] = l->v[i]; 292 } 293 l->n = w; 294 span_push(l, start, end, style); 295} 296 297static int heading_level(TSNode heading) { 298 uint32_t count = ts_node_named_child_count(heading); 299 for (uint32_t i = 0; i < count; i++) { 300 const char *t = ts_node_type(ts_node_named_child(heading, i)); 301 const char *p = NULL; 302 if (strncmp(t, "atx_h", 5) == 0) p = t + 5; 303 else if (strncmp(t, "setext_h", 8) == 0) p = t + 8; 304 if (p && *p) return atoi(p); 305 } 306 return 1; 307} 308 309char *tiny_highlight_markdown(const char *text) { 310 syntax *md = grammar_md(); 311 if (!md) return strdup(text); 312 size_t len = strlen(text); 313 TSTree *md_tree = NULL; 314 TSNode root = parse(md, text, len, &md_tree); 315 spanlist spans = {0}; 316 spans_of(md, root, 0, &spans); 317 318 syntax *inl = grammar_md_inline(); 319 TSTree **trees = NULL; 320 size_t ntrees = 0, trees_cap = 0; 321 if (inl) { 322 static const char *inline_types[] = {"inline"}; 323 TSNode *inlines = NULL; 324 size_t ninline = 0, cap = 0; 325 collect_types(root, inline_types, 1, &inlines, &ninline, &cap); 326 for (size_t i = 0; i < ninline; i++) { 327 size_t off = ts_node_start_byte(inlines[i]); 328 size_t ilen = ts_node_end_byte(inlines[i]) - off; 329 char *sub = malloc(ilen + 1); 330 if (!sub) abort(); 331 memcpy(sub, text + off, ilen); 332 sub[ilen] = '\0'; 333 TSTree *itree = NULL; 334 TSNode iroot = parse(inl, sub, ilen, &itree); 335 if (ntrees == trees_cap) { 336 trees_cap = trees_cap ? trees_cap * 2 : 8; 337 trees = realloc(trees, trees_cap * sizeof *trees); 338 if (!trees) abort(); 339 } 340 trees[ntrees++] = itree; 341 spans_of(inl, iroot, off, &spans); 342 static const char *code_types[] = {"code_span"}; 343 TSNode *codes = NULL; 344 size_t ncodes = 0, ccap = 0; 345 collect_types(iroot, code_types, 1, &codes, &ncodes, &ccap); 346 for (size_t c = 0; c < ncodes; c++) 347 recolor(&spans, off + ts_node_start_byte(codes[c]), 348 off + ts_node_end_byte(codes[c]), tiny_inline_code_style()); 349 free(codes); 350 free(sub); 351 } 352 free(inlines); 353 } 354 355 static const char *heading_types[] = {"atx_heading", "setext_heading"}; 356 TSNode *headings = NULL; 357 size_t nhead = 0, hcap = 0; 358 collect_types(root, heading_types, 2, &headings, &nhead, &hcap); 359 for (size_t i = 0; i < nhead; i++) 360 recolor(&spans, ts_node_start_byte(headings[i]), ts_node_end_byte(headings[i]), 361 tiny_heading_style(heading_level(headings[i]))); 362 free(headings); 363 364 char *out = paint(text, spans.v, spans.n, len); 365 free(spans.v); 366 for (size_t i = 0; i < ntrees; i++) ts_tree_delete(trees[i]); 367 free(trees); 368 ts_tree_delete(md_tree); 369 return out; 370} 371 372char *tiny_highlight_code(const char *info, const char *text) { 373 syntax *s = NULL; 374 char lang[32]; 375 size_t i = 0; 376 while (info[i] && i < sizeof lang - 1 && info[i] != ' ' && info[i] != '\t') { 377 lang[i] = (char)(info[i] >= 'A' && info[i] <= 'Z' ? info[i] - 'A' + 'a' : info[i]); 378 i++; 379 } 380 lang[i] = '\0'; 381 if (strcmp(lang, "c") == 0) s = grammar_c(); 382 else if (strcmp(lang, "python") == 0 || strcmp(lang, "py") == 0) s = grammar_python(); 383 else if (strcmp(lang, "js") == 0 || strcmp(lang, "javascript") == 0 || strcmp(lang, "jsx") == 0) s = grammar_js(); 384 else if (strcmp(lang, "ts") == 0 || strcmp(lang, "typescript") == 0) s = grammar_ts(); 385 else if (strcmp(lang, "tsx") == 0) s = grammar_tsx(); 386 else if (strcmp(lang, "make") == 0 || strcmp(lang, "makefile") == 0) s = grammar_make(); 387 else if (strcmp(lang, "bash") == 0 || strcmp(lang, "sh") == 0 || strcmp(lang, "shell") == 0) s = grammar_bash(); 388 if (!s) return strdup(text); 389 size_t len = strlen(text); 390 TSTree *tree = NULL; 391 TSNode root = parse(s, text, len, &tree); 392 spanlist spans = {0}; 393 spans_of(s, root, 0, &spans); 394 char *out = paint(text, spans.v, spans.n, len); 395 free(spans.v); 396 ts_tree_delete(tree); 397 return out; 398}