1
0
Fork 0

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 (
"strings"
"timshome.page/gilo/internal/editor/highlight"
"timshome.page/gilo/internal/gilo"
"unicode"
)
type Row struct {
chars []rune
render []rune
Hl []int
}
func newRow(s string) *Row {
@ -19,7 +22,7 @@ func newRow(s string) *Row {
render = append(render, ch)
}
return &Row{chars, render}
return &Row{chars, render, []int{}}
}
func (r *Row) Size() int {
@ -98,6 +101,18 @@ func (r *Row) update() {
for _, ch := range str {
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 {

View File

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