highlightCode applies syntax highlighting to code based on file extension.
(code, filePath string)
| 457 | |
| 458 | // highlightCode applies syntax highlighting to code based on file extension. |
| 459 | func (l *LogViewer) highlightCode(code, filePath string) string { |
| 460 | // Strip line number prefixes from Read tool output (format: " 1→" or " 1\t") |
| 461 | code = stripLineNumbers(code) |
| 462 | |
| 463 | // Get lexer based on file extension |
| 464 | ext := filepath.Ext(filePath) |
| 465 | lexer := lexers.Match(filePath) |
| 466 | if lexer == nil { |
| 467 | lexer = lexers.Get(ext) |
| 468 | } |
| 469 | if lexer == nil { |
| 470 | lexer = lexers.Fallback |
| 471 | } |
| 472 | lexer = chroma.Coalesce(lexer) |
| 473 | |
| 474 | // Use Tokyo Night theme for syntax highlighting |
| 475 | style := styles.Get("tokyonight-night") |
| 476 | if style == nil { |
| 477 | style = styles.Fallback |
| 478 | } |
| 479 | |
| 480 | // Use terminal256 formatter for ANSI color output |
| 481 | formatter := formatters.Get("terminal256") |
| 482 | if formatter == nil { |
| 483 | formatter = formatters.Fallback |
| 484 | } |
| 485 | |
| 486 | // Tokenize and format |
| 487 | iterator, err := lexer.Tokenise(nil, code) |
| 488 | if err != nil { |
| 489 | return "" |
| 490 | } |
| 491 | |
| 492 | var buf bytes.Buffer |
| 493 | if err := formatter.Format(&buf, style, iterator); err != nil { |
| 494 | return "" |
| 495 | } |
| 496 | |
| 497 | return buf.String() |
| 498 | } |
| 499 | |
| 500 | // stripLineNumbers removes line number prefixes from Read tool output. |
| 501 | // The format is: optional spaces + line number + → or tab + content |
no test coverage detected