-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlight.go
93 lines (78 loc) · 2.79 KB
/
highlight.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Package vimtea provides a Vim-like text editor component for terminal applications
package vimtea
import (
"bytes"
"strings"
"time"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/quick"
)
// syntaxHighlighter provides syntax highlighting functionality for the editor
// using the Chroma library for language detection and highlighting
type syntaxHighlighter struct {
filename string // File name used to determine language
language string // Detected language
syntaxTheme string // Chroma theme to use for highlighting
enabled bool // Whether highlighting is enabled
cache map[string]string // Cache of highlighted lines for performance
}
// yankHighlight provides visual feedback for yanked (copied) text
// by temporarily highlighting the yanked region
type yankHighlight struct {
Active bool // Whether the highlight is currently active
Start Cursor // Start position of highlighted region
End Cursor // End position of highlighted region
StartTime time.Time // When the highlight was activated
Duration time.Duration // How long the highlight should remain visible
IsLinewise bool // Whether this is a line-wise operation
}
// newSyntaxHighlighter creates a new syntax highlighter with the specified theme and filename
// The filename is used to determine the language for syntax highlighting
func newSyntaxHighlighter(syntaxTheme string, fileName string) *syntaxHighlighter {
return &syntaxHighlighter{
syntaxTheme: syntaxTheme,
enabled: true,
filename: fileName,
cache: make(map[string]string),
}
}
// newYankHighlight creates a new inactive yank highlight
// with a default duration of 100ms
func newYankHighlight() yankHighlight {
return yankHighlight{
Active: false,
Duration: time.Millisecond * 100,
}
}
// HighlightLine applies syntax highlighting to a single line of text
// It uses caching to improve performance for repeated lines
func (sh *syntaxHighlighter) HighlightLine(line string) string {
// Skip highlighting if disabled or no filename is set
if !sh.enabled || sh.filename == "" {
return line
}
// Check cache for already highlighted lines
cacheKey := line
if cached, ok := sh.cache[cacheKey]; ok {
return cached
}
// Skip empty lines
if len(line) == 0 {
return line
}
lexer := lexers.Match(sh.filename)
if lexer == nil {
return line
}
// Apply syntax highlighting
buf := new(bytes.Buffer)
err := quick.Highlight(buf, line, lexer.Config().Name, "terminal16m", sh.syntaxTheme)
if err != nil {
return line
}
// Clean up the result by removing newlines
highlighted := strings.ReplaceAll(buf.String(), "\n", "")
// Cache the result for future use
sh.cache[cacheKey] = highlighted
return highlighted
}