Refactor highlighting to use highlight array

This commit is contained in:
Timothy Warren 2021-04-07 12:32:22 -04:00
parent c3b2900f42
commit c18181c064
5 changed files with 55 additions and 9 deletions

View File

@ -2,12 +2,15 @@ package document
import ( import (
"strings" "strings"
"timshome.page/gilo/internal/editor/highlight"
"timshome.page/gilo/internal/gilo" "timshome.page/gilo/internal/gilo"
"unicode"
) )
type Row struct { type Row struct {
chars []rune chars []rune
render []rune render []rune
Hl []int
} }
func newRow(s string) *Row { func newRow(s string) *Row {
@ -19,7 +22,7 @@ func newRow(s string) *Row {
render = append(render, ch) render = append(render, ch)
} }
return &Row{chars, render} return &Row{chars, render, []int{}}
} }
func (r *Row) Size() int { func (r *Row) Size() int {
@ -98,6 +101,18 @@ func (r *Row) update() {
for _, ch := range str { for _, ch := range str {
r.render = append(r.render, ch) r.render = append(r.render, ch)
} }
r.updateSyntax()
}
func (r *Row) updateSyntax() {
for _, ch := range r.render {
if unicode.IsDigit(ch) {
r.Hl = append(r.Hl, highlight.Number)
} else {
r.Hl = append(r.Hl, highlight.Normal)
}
}
} }
func (r *Row) toString() string { func (r *Row) toString() string {

View File

@ -5,9 +5,9 @@ import (
"fmt" "fmt"
"strings" "strings"
"time" "time"
"timshome.page/gilo/internal/editor/highlight"
"timshome.page/gilo/internal/gilo" "timshome.page/gilo/internal/gilo"
"timshome.page/gilo/internal/terminal" "timshome.page/gilo/internal/terminal"
"unicode"
) )
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -74,20 +74,29 @@ func (e *editor) drawRows(ab *gilo.Buffer) {
continue continue
} }
// rowLen := e.document.GetRow(fileRow).RenderSize() - e.offset.X currentColor := terminal.DefaultFGColor
row := e.document.GetRow(fileRow)
for _, ch := range e.document.GetRow(fileRow).Render(e.offset) { for i, ch := range row.Render(e.offset) {
if unicode.IsDigit(ch) { if row.Hl[i] == highlight.Normal {
ab.Append(terminal.FGRed) if currentColor != terminal.DefaultFGColor {
ab.AppendRune(ch)
ab.Append(terminal.DefaultFGColor) ab.Append(terminal.DefaultFGColor)
currentColor = terminal.DefaultFGColor
}
ab.AppendRune(ch)
} else { } else {
color := highlight.SyntaxToColor(row.Hl[i])
if color != currentColor {
currentColor = color
ab.Append(color)
}
ab.AppendRune(ch) ab.AppendRune(ch)
} }
} }
// outputRow := gilo.Truncate(e.document.GetRow(fileRow).Render(e.offset), rowLen) ab.Append(terminal.DefaultFGColor)
// ab.Append(outputRow)
} }
ab.AppendLn(terminal.ClearLine) ab.AppendLn(terminal.ClearLine)

View File

@ -0,0 +1,9 @@
package highlight
// ----------------------------------------------------------------------------
// !Syntax Highlighting Constants
// ----------------------------------------------------------------------------
const (
Normal = iota
Number
)

View File

@ -0,0 +1,12 @@
package highlight
import "timshome.page/gilo/internal/terminal"
func SyntaxToColor(hl int) string {
switch hl {
case Number:
return terminal.FGRed
default:
return terminal.DefaultFGColor
}
}

View File

@ -9,3 +9,4 @@ const (
TabSize = 4 TabSize = 4
QuitTimes = 3 QuitTimes = 3
) )