|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/alecthomas/chroma/v2" |
| 8 | + "github.com/alecthomas/chroma/v2/lexers" |
| 9 | + "github.com/alecthomas/chroma/v2/styles" |
| 10 | + "github.com/nhost/lazyreview/diff" |
| 11 | +) |
| 12 | + |
| 13 | +// highlighter tokenizes diff lines using chroma and caches the results per file. |
| 14 | +type highlighter struct { |
| 15 | + cache map[string][]string // file path -> highlighted lines |
| 16 | + style *chroma.Style |
| 17 | +} |
| 18 | + |
| 19 | +// newHighlighter creates a highlighter with the dracula chroma style. |
| 20 | +func newHighlighter() *highlighter { |
| 21 | + return &highlighter{ |
| 22 | + cache: make(map[string][]string), |
| 23 | + style: styles.Get("dracula"), |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// clear invalidates the entire highlight cache. |
| 28 | +func (h *highlighter) clear() { |
| 29 | + h.cache = make(map[string][]string) |
| 30 | +} |
| 31 | + |
| 32 | +// highlightFile tokenizes all lines in a diff file and returns cached results. |
| 33 | +// Returns nil if no lexer matches the file path. |
| 34 | +func (h *highlighter) highlightFile(f *diff.File, path string) []string { |
| 35 | + if f == nil { |
| 36 | + return nil |
| 37 | + } |
| 38 | + |
| 39 | + if cached, ok := h.cache[path]; ok { |
| 40 | + return cached |
| 41 | + } |
| 42 | + |
| 43 | + lexer := lexers.Match(path) |
| 44 | + if lexer == nil { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + lexer = chroma.Coalesce(lexer) |
| 49 | + |
| 50 | + var lines []string |
| 51 | + for _, hunk := range f.Hunks { |
| 52 | + // blank entry for the hunk header line |
| 53 | + lines = append(lines, "") |
| 54 | + |
| 55 | + for _, line := range hunk.Lines { |
| 56 | + lines = append(lines, h.highlightLine(lexer, line.Content)) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + h.cache[path] = lines |
| 61 | + |
| 62 | + return lines |
| 63 | +} |
| 64 | + |
| 65 | +// highlightLine tokenizes a single line and returns an ANSI-colored string. |
| 66 | +func (h *highlighter) highlightLine(lexer chroma.Lexer, content string) string { |
| 67 | + // Strip the diff prefix (+/-/ ) for tokenization |
| 68 | + raw := content |
| 69 | + if len(raw) > 0 { |
| 70 | + switch raw[0] { |
| 71 | + case '+', '-', ' ': |
| 72 | + raw = raw[1:] |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + iter, err := lexer.Tokenise(nil, raw) |
| 77 | + if err != nil { |
| 78 | + return "" |
| 79 | + } |
| 80 | + |
| 81 | + var sb strings.Builder |
| 82 | + |
| 83 | + // Write the diff prefix character without syntax coloring |
| 84 | + if len(content) > 0 { |
| 85 | + sb.WriteByte(content[0]) |
| 86 | + } |
| 87 | + |
| 88 | + for _, token := range iter.Tokens() { |
| 89 | + entry := h.style.Get(token.Type) |
| 90 | + fg := entry.Colour |
| 91 | + |
| 92 | + if !fg.IsSet() { |
| 93 | + sb.WriteString(token.Value) |
| 94 | + |
| 95 | + continue |
| 96 | + } |
| 97 | + |
| 98 | + r, g, b := fg.Red(), fg.Green(), fg.Blue() |
| 99 | + sb.WriteString(fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[39m", r, g, b, token.Value)) |
| 100 | + } |
| 101 | + |
| 102 | + return sb.String() |
| 103 | +} |
| 104 | + |
| 105 | +// styleLine combines a cached syntax-highlighted foreground with a diff |
| 106 | +// background color. Falls back to plain diff styling when highlight is empty. |
| 107 | +func (h *highlighter) styleLine( |
| 108 | + line diff.Line, |
| 109 | + highlight string, |
| 110 | + plainFallback string, |
| 111 | +) string { |
| 112 | + if highlight == "" { |
| 113 | + return plainFallback |
| 114 | + } |
| 115 | + |
| 116 | + switch line.Type { |
| 117 | + case diff.Added: |
| 118 | + return addedBgANSI + highlight + resetANSI |
| 119 | + case diff.Removed: |
| 120 | + return removedBgANSI + highlight + resetANSI |
| 121 | + case diff.Context: |
| 122 | + return highlight |
| 123 | + } |
| 124 | + |
| 125 | + return highlight |
| 126 | +} |
0 commit comments