Something went wrong. Try again.
A code review TUI that tries to reduce how many red and green squiggles you have to squint at
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400package ui
import ( "image/color" "strings"
"github.com/alecthomas/chroma/v2" "github.com/charmbracelet/x/ansi")
const styledSyntaxCacheLimit = 256
type styledSyntaxCache struct { earlier map[int][]reviewSpan later map[int][]reviewSpan}
func (cache *styledSyntaxCache) code( rowIndex int, row documentRow, earlier bool, syntax reviewSyntax, changes []rowChange, palette reviewPalette,) []reviewSpan { entries := cache.later if earlier { entries = cache.earlier } if styled, exists := entries[rowIndex]; exists { return styled } if len(cache.earlier)+len(cache.later) >= styledSyntaxCacheLimit { cache.clear() } if cache.earlier == nil { cache.earlier = make(map[int][]reviewSpan, styledSyntaxCacheLimit/2) cache.later = make(map[int][]reviewSpan, styledSyntaxCacheLimit/2) } tokens := syntax.later[rowIndex] change := changes[rowIndex].later entries = cache.later if earlier { tokens = syntax.earlier[rowIndex] change = changes[rowIndex].earlier entries = cache.earlier } foreground, strongBackground := syntaxRowColors(row, palette) styled := styleSyntaxCode( tokens, displayDiffRowText(row), foreground, strongBackground, change, palette, ) entries[rowIndex] = styled return styled}
func (model *ReviewModel) styledReviewCode( rowIndex int, row documentRow, earlier bool, palette reviewPalette,) []reviewSpan { matches, current := model.search.rowMatches(rowIndex) if len(matches) == 0 { styled := model.styledSyntax.code( rowIndex, row, earlier, model.syntax, model.changes, palette, ) return styleFileHeaderChangedCounts(row, styled, palette) } tokens := model.syntax.later[rowIndex] change := model.changes[rowIndex].later if earlier { tokens = model.syntax.earlier[rowIndex] change = model.changes[rowIndex].earlier } foreground, strongBackground := syntaxRowColors(row, palette) styled := styleSyntaxCodeWithSearch( tokens, displayDiffRowText(row), foreground, strongBackground, change, matches, current, palette, ) return styleFileHeaderChangedCounts(row, styled, palette)}
func styleFileHeaderChangedCounts( row documentRow, spans []reviewSpan, palette reviewPalette,) []reviewSpan { if row.role != documentRowFileHeader || len(row.text) > reviewRowPreviewBytes { return spans } text := cleanDiffRowText(row) if row.additionCountRange.last > row.additionCountRange.first { spans = reviewSpansWithForeground(spans, text, row.additionCountRange, palette.addition) } if row.deletionCountRange.last > row.deletionCountRange.first { spans = reviewSpansWithForeground(spans, text, row.deletionCountRange, palette.deletion) } return spans}
func reviewSpansWithForeground( spans []reviewSpan, text string, rangeToColor textRange, foreground color.Color,) []reviewSpan { first := ansi.StringWidth(text[:rangeToColor.first]) last := first + ansi.StringWidth(text[rangeToColor.first:rangeToColor.last]) colored := cutReviewSpans(spans, first, last) for index := range colored { colored[index].foreground = foreground } result := cutReviewSpans(spans, 0, first) result = append(result, colored...) result = append(result, cutReviewSpans(spans, last, ansi.StringWidth(text))...) return result}
func (cache *styledSyntaxCache) clear() { cache.earlier = nil cache.later = nil}
func syntaxRowColors( row documentRow, palette reviewPalette,) (color.Color, color.Color) { foreground := palette.foreground strongBackground := palette.background switch row.polarity { case documentPolarityNeutral: case documentPolarityAddition: foreground = palette.addition strongBackground = palette.additionStrong case documentPolarityDeletion: foreground = palette.deletion strongBackground = palette.deletionStrong } if row.role != documentRowSource { foreground = palette.muted } if row.role == documentRowFileHeader || row.role == documentRowHunkHeader { foreground = palette.accent } if row.provenance == documentProvenanceElision || row.provenance == documentProvenanceSynthetic { foreground = palette.accent } return foreground, strongBackground}
func styleSyntaxCode( tokens []syntaxToken, raw string, base color.Color, strongBackground color.Color, change textRange, palette reviewPalette,) []reviewSpan { return styleSyntaxCodeWithSearch( tokens, raw, base, strongBackground, change, nil, -1, palette, )}
func styleSyntaxCodeWithSearch( tokens []syntaxToken, raw string, base color.Color, strongBackground color.Color, change textRange, matches []reviewSearchMatch, current int, palette reviewPalette,) []reviewSpan { if syntaxText(tokens) != raw { tokens = []syntaxToken{{kind: chroma.Text, text: raw}} } result := make([]reviewSpan, 0, len(tokens)*2) offset := 0 searchIndex := 0 for _, token := range tokens { if len(matches) == 0 { appendStyledSyntaxToken( &result, token, offset, base, strongBackground, change, palette, ) } else { appendSearchStyledSyntaxToken( &result, token, offset, base, strongBackground, change, matches, &searchIndex, current, palette, ) } offset += len(token.text) } for index := range result { result[index].text = expandReviewDisplayTabs(result[index].text) } return result}
func appendSearchStyledSyntaxToken( result *[]reviewSpan, token syntaxToken, offset int, base color.Color, strongBackground color.Color, change textRange, matches []reviewSearchMatch, searchIndex *int, current int, palette reviewPalette,) { tokenEnd := offset + len(token.text) for cursor := offset; cursor < tokenEnd; { for *searchIndex < len(matches) && matches[*searchIndex].last <= cursor { *searchIndex++ } next, background := nextSearchSyntaxSegment( cursor, tokenEnd, strongBackground, change, matches, *searchIndex, current, palette, ) *result = append(*result, syntaxReviewSpan( token.kind, token.text[cursor-offset:next-offset], base, background, palette, )) cursor = next }}
func nextSearchSyntaxSegment( cursor int, tokenEnd int, strongBackground color.Color, change textRange, matches []reviewSearchMatch, searchIndex int, current int, palette reviewPalette,) (int, color.Color) { next := tokenEnd var background color.Color if cursor < change.first { next = min(next, change.first) } else if cursor < change.last { next = min(next, change.last) background = strongBackground } if searchIndex >= len(matches) { return next, background } match := matches[searchIndex] if cursor < match.first { return min(next, match.first), background } if cursor >= match.last { return next, background } next = min(next, match.last) background = palette.searchMatch if searchIndex == current { background = palette.searchCurrent } return next, background}
func appendStyledSyntaxToken( result *[]reviewSpan, token syntaxToken, offset int, base color.Color, strongBackground color.Color, change textRange, palette reviewPalette,) { tokenEnd := offset + len(token.text) overlapFirst := max(offset, change.first) overlapLast := min(tokenEnd, change.last) if overlapLast <= overlapFirst { *result = append(*result, syntaxReviewSpan(token.kind, token.text, base, nil, palette)) return } if overlapFirst > offset { *result = append(*result, syntaxReviewSpan( token.kind, token.text[:overlapFirst-offset], base, nil, palette, )) } *result = append(*result, syntaxReviewSpan( token.kind, token.text[overlapFirst-offset:overlapLast-offset], base, strongBackground, palette, )) if overlapLast < tokenEnd { *result = append(*result, syntaxReviewSpan( token.kind, token.text[overlapLast-offset:], base, nil, palette, )) }}
func syntaxReviewSpan( kind chroma.TokenType, text string, base color.Color, background color.Color, palette reviewPalette,) reviewSpan { foreground := syntaxTokenColor(kind, base, palette) if background != nil { foreground = palette.foreground } return reviewSpan{ text: text, foreground: foreground, background: background, italic: kind.InCategory(chroma.Comment), }}
func syntaxTokenColor(kind chroma.TokenType, base color.Color, palette reviewPalette) color.Color { switch { case kind.InCategory(chroma.Keyword): return palette.syntaxKeyword case kind.InCategory(chroma.NameFunction), kind.InCategory(chroma.NameClass), kind.InCategory(chroma.NameDecorator): return palette.syntaxName case kind.InCategory(chroma.LiteralString): return palette.syntaxString case kind.InCategory(chroma.LiteralNumber): return palette.syntaxNumber case kind.InCategory(chroma.Comment): return palette.syntaxComment case kind.InCategory(chroma.Operator): return palette.syntaxOperator default: return base }}
func syntaxText(tokens []syntaxToken) string { var text strings.Builder for _, token := range tokens { text.WriteString(token.text) } return text.String()}