feat: added better lexer selection handling
All checks were successful
Run Test Suite / test (push) Successful in 18s
All checks were successful
Run Test Suite / test (push) Successful in 18s
This commit is contained in:
parent
e362c9f118
commit
0e2867b948
@ -6,8 +6,6 @@ import (
|
||||
|
||||
"git.gophernest.net/azpect/TextEditor/internal/core"
|
||||
"git.gophernest.net/azpect/TextEditor/internal/style"
|
||||
"github.com/alecthomas/chroma/v2"
|
||||
"github.com/alecthomas/chroma/v2/lexers"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
@ -52,12 +50,7 @@ func viewWindow(w *core.Window, styles style.Styles, options core.WinOptions, mo
|
||||
end := w.ScrollY + w.ViewportHeight()
|
||||
|
||||
// Chroma stuff
|
||||
name := strings.ReplaceAll(buf.Filetype, ".", "")
|
||||
lexer := lexers.Get(name)
|
||||
if lexer == nil {
|
||||
lexer = lexers.Fallback
|
||||
}
|
||||
lexer = chroma.Coalesce(lexer) // Merge tokens together
|
||||
lexer := style.GetLexer(buf)
|
||||
|
||||
// Draw buffer lines
|
||||
for lineNum := start; lineNum < end; lineNum++ {
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
package style
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.gophernest.net/azpect/TextEditor/internal/core"
|
||||
"github.com/alecthomas/chroma/v2"
|
||||
"github.com/alecthomas/chroma/v2/lexers"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
@ -233,3 +236,33 @@ func darkenColor(c chroma.Colour, factor float64) chroma.Colour {
|
||||
b := uint8(float64(c.Blue()) * factor)
|
||||
return chroma.NewColour(r, g, b)
|
||||
}
|
||||
|
||||
// GetLexer: Uses buffer meta data or content to pick a lexer for use in applying
|
||||
// highlights.
|
||||
func GetLexer(buf *core.Buffer) chroma.Lexer {
|
||||
var lexer chroma.Lexer
|
||||
|
||||
if buf.Filetype != "" {
|
||||
lexer = lexers.Get(strings.TrimPrefix(buf.Filetype, "."))
|
||||
}
|
||||
|
||||
if lexer == nil && buf.Filename != "" {
|
||||
lexer = lexers.Match(buf.Filename)
|
||||
}
|
||||
|
||||
if lexer == nil && len(buf.Lines) > 0 {
|
||||
// Get first few lines for content analysis
|
||||
var content strings.Builder
|
||||
for i := 0; i < min(len(buf.Lines), 10); i++ {
|
||||
content.WriteString(buf.Lines[i].String() + "\n")
|
||||
}
|
||||
lexer = lexers.Analyse(content.String())
|
||||
}
|
||||
|
||||
if lexer == nil {
|
||||
lexer = lexers.Fallback
|
||||
}
|
||||
|
||||
lexer = chroma.Coalesce(lexer) // Merge tokens together
|
||||
return lexer
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user