From 6d3bf3dfd4989abff2f423e5b79994c1a2872e2f Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Tue, 10 Sep 2024 16:54:53 +0200 Subject: [PATCH] Auto detect JSON for highlighting Resolves #240. --- m/reader.go | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/m/reader.go b/m/reader.go index 00ceb91..e753eed 100644 --- a/m/reader.go +++ b/m/reader.go @@ -3,6 +3,7 @@ package m import ( "bufio" "bytes" + "encoding/json" "fmt" "io" "os" @@ -112,11 +113,9 @@ func (reader *Reader) preAllocLines() { func (reader *Reader) readStream(stream io.Reader, formatter chroma.Formatter, lexer chroma.Lexer) { reader.consumeLinesFromStream(stream) - if lexer != nil { - t0 := time.Now() - highlightFromMemory(reader, <-reader.highlightingStyle, formatter, lexer) - log.Debug("highlightFromMemory() took ", time.Since(t0)) - } + t0 := time.Now() + highlightFromMemory(reader, <-reader.highlightingStyle, formatter, lexer) + log.Debug("highlightFromMemory() took ", time.Since(t0)) reader.done.Store(true) select { @@ -534,10 +533,6 @@ func NewReaderFromFilename(filename string, style chroma.Style, formatter chroma // We expect this to be executed in a goroutine func highlightFromMemory(reader *Reader, style chroma.Style, formatter chroma.Formatter, lexer chroma.Lexer) { - if lexer == nil { - return - } - defer func() { reader.highlightingDone.Store(true) select { @@ -546,18 +541,20 @@ func highlightFromMemory(reader *Reader, style chroma.Style, formatter chroma.Fo } }() + // Is the buffer small enough? var byteCount int64 reader.Lock() for _, line := range reader.lines { byteCount += int64(len(line.raw)) + + if byteCount > MAX_HIGHLIGHT_SIZE { + log.Debug("File too large for highlighting: ", byteCount) + reader.Unlock() + return + } } reader.Unlock() - if byteCount > MAX_HIGHLIGHT_SIZE { - log.Debug("File too large for highlighting: ", byteCount) - return - } - textBuilder := strings.Builder{} reader.Lock() for _, line := range reader.lines { @@ -565,8 +562,19 @@ func highlightFromMemory(reader *Reader, style chroma.Style, formatter chroma.Fo textBuilder.WriteString("\n") } reader.Unlock() + text := textBuilder.String() - highlighted, err := highlight(textBuilder.String(), style, formatter, lexer) + if lexer == nil && json.Valid([]byte(text)) { + log.Debug("Buffer is valid JSON, highlighting as JSON") + lexer = lexers.Get("json") + } + + if lexer == nil { + log.Debug("No lexer set for highlighting") + return + } + + highlighted, err := highlight(text, style, formatter, lexer) if err != nil { log.Warn("Highlighting failed: ", err) return