chore: remove old and unused code
This commit is contained in:
60
highlight.go
60
highlight.go
@@ -6,18 +6,67 @@ import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/alecthomas/chroma/v2/lexers"
|
||||
"github.com/alecthomas/chroma/v2/quick"
|
||||
)
|
||||
|
||||
const reviewContextLines = 3
|
||||
const highlightedDiffCacheLimit = 256
|
||||
|
||||
var codeHighlightTheme = "github-dark"
|
||||
var colorEnabled = true
|
||||
|
||||
var hunkHeaderPattern = regexp.MustCompile(`^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@`)
|
||||
|
||||
type highlightedDiffCacheKey struct {
|
||||
path, hunk, side, theme string
|
||||
startLine, endLine int
|
||||
color bool
|
||||
}
|
||||
|
||||
type highlightedDiffCache struct {
|
||||
mu sync.Mutex
|
||||
entries map[highlightedDiffCacheKey][]highlightedDiffLine
|
||||
order []highlightedDiffCacheKey
|
||||
}
|
||||
|
||||
var highlightedDiffs = highlightedDiffCache{
|
||||
entries: make(map[highlightedDiffCacheKey][]highlightedDiffLine),
|
||||
}
|
||||
|
||||
func (c *highlightedDiffCache) get(key highlightedDiffCacheKey) ([]highlightedDiffLine, bool) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
lines, ok := c.entries[key]
|
||||
return append([]highlightedDiffLine(nil), lines...), ok
|
||||
}
|
||||
|
||||
func (c *highlightedDiffCache) put(
|
||||
key highlightedDiffCacheKey, lines []highlightedDiffLine,
|
||||
) []highlightedDiffLine {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if cached, ok := c.entries[key]; ok {
|
||||
return append([]highlightedDiffLine(nil), cached...)
|
||||
}
|
||||
if len(c.entries) >= highlightedDiffCacheLimit {
|
||||
delete(c.entries, c.order[0])
|
||||
c.order = c.order[1:]
|
||||
}
|
||||
c.entries[key] = append([]highlightedDiffLine(nil), lines...)
|
||||
c.order = append(c.order, key)
|
||||
return append([]highlightedDiffLine(nil), lines...)
|
||||
}
|
||||
|
||||
func (c *highlightedDiffCache) clear() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.entries = make(map[highlightedDiffCacheKey][]highlightedDiffLine)
|
||||
c.order = nil
|
||||
}
|
||||
|
||||
type highlightedDiffLine struct {
|
||||
gutter string
|
||||
code string
|
||||
@@ -34,6 +83,17 @@ type parsedDiffLine struct {
|
||||
}
|
||||
|
||||
func highlightDiff(path, hunk string, startLine, endLine int, side string) []highlightedDiffLine {
|
||||
key := highlightedDiffCacheKey{
|
||||
path: path, hunk: hunk, side: side, theme: codeHighlightTheme,
|
||||
startLine: startLine, endLine: endLine, color: colorEnabled,
|
||||
}
|
||||
if cached, ok := highlightedDiffs.get(key); ok {
|
||||
return cached
|
||||
}
|
||||
return highlightedDiffs.put(key, highlightDiffUncached(path, hunk, startLine, endLine, side))
|
||||
}
|
||||
|
||||
func highlightDiffUncached(path, hunk string, startLine, endLine int, side string) []highlightedDiffLine {
|
||||
if hunk == "" {
|
||||
return []highlightedDiffLine{{code: "(GitHub did not return a diff hunk)"}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user