feat(processor): add chroma dep, lexer resolution and drop rules
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/chroma/v2"
|
||||
"github.com/alecthomas/chroma/v2/lexers"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Inline bool
|
||||
Block bool
|
||||
PreserveLines bool
|
||||
InRange func(line int) bool
|
||||
}
|
||||
|
||||
type LineChange struct {
|
||||
Num int
|
||||
Original string
|
||||
Result string
|
||||
Action string
|
||||
}
|
||||
|
||||
func resolveLexer(filename string) chroma.Lexer {
|
||||
ext := strings.TrimPrefix(strings.ToLower(filepath.Ext(filename)), ".")
|
||||
var l chroma.Lexer
|
||||
if ext == "less" {
|
||||
l = lexers.Get("SCSS")
|
||||
} else {
|
||||
l = lexers.Match("f." + ext)
|
||||
}
|
||||
if l == nil {
|
||||
return nil
|
||||
}
|
||||
return chroma.Coalesce(l)
|
||||
}
|
||||
|
||||
func shouldDrop(tt chroma.TokenType, opts Options) bool {
|
||||
if tt == chroma.CommentPreproc || tt == chroma.CommentPreprocFile {
|
||||
return false
|
||||
}
|
||||
if opts.Inline {
|
||||
return tt == chroma.CommentSingle || tt == chroma.CommentHashbang || tt == chroma.CommentSpecial
|
||||
}
|
||||
if opts.Block {
|
||||
return tt == chroma.CommentMultiline
|
||||
}
|
||||
return tt.Category() == chroma.Comment
|
||||
}
|
||||
|
||||
func isCommentType(tt chroma.TokenType) bool {
|
||||
return tt.Category() == chroma.Comment && tt != chroma.CommentPreproc && tt != chroma.CommentPreprocFile
|
||||
}
|
||||
|
||||
func leadingWhitespace(s string) string {
|
||||
for i, r := range s {
|
||||
if r != ' ' && r != '\t' {
|
||||
return s[:i]
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alecthomas/chroma/v2"
|
||||
)
|
||||
|
||||
func TestResolveLexer(t *testing.T) {
|
||||
cases := []struct {
|
||||
file string
|
||||
want string
|
||||
}{
|
||||
{"a.c", "C"},
|
||||
{"a.go", "Go"},
|
||||
{"a.js", "JavaScript"},
|
||||
{"a.rs", "Rust"},
|
||||
{"a.html", "HTML"},
|
||||
{"a.less", "SCSS"},
|
||||
{"a.conf", ""},
|
||||
}
|
||||
for _, c := range cases {
|
||||
l := resolveLexer(c.file)
|
||||
got := ""
|
||||
if l != nil {
|
||||
got = l.Config().Name
|
||||
}
|
||||
if got != c.want {
|
||||
t.Errorf("resolveLexer(%q) = %q, want %q", c.file, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldDrop(t *testing.T) {
|
||||
def := Options{}
|
||||
inline := Options{Inline: true}
|
||||
block := Options{Block: true}
|
||||
cases := []struct {
|
||||
tt chroma.TokenType
|
||||
opts Options
|
||||
want bool
|
||||
}{
|
||||
{chroma.CommentSingle, def, true},
|
||||
{chroma.CommentMultiline, def, true},
|
||||
{chroma.CommentPreproc, def, false},
|
||||
{chroma.CommentPreprocFile, def, false},
|
||||
{chroma.CommentSingle, inline, true},
|
||||
{chroma.CommentMultiline, inline, false},
|
||||
{chroma.CommentSingle, block, false},
|
||||
{chroma.CommentMultiline, block, true},
|
||||
{chroma.KeywordType, def, false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := shouldDrop(c.tt, c.opts); got != c.want {
|
||||
t.Errorf("shouldDrop(%v, %+v) = %v, want %v", c.tt, c.opts, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user