From c18181c064d708c9df8433dfa4c58df349dfc76d Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 7 Apr 2021 12:32:22 -0400 Subject: [PATCH] Refactor highlighting to use highlight array --- internal/editor/document/row.go | 17 ++++++++++++++++- internal/editor/draw.go | 25 +++++++++++++++++-------- internal/editor/highlight/constants.go | 9 +++++++++ internal/editor/highlight/fn.go | 12 ++++++++++++ internal/gilo/constants.go | 1 + 5 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 internal/editor/highlight/constants.go create mode 100644 internal/editor/highlight/fn.go diff --git a/internal/editor/document/row.go b/internal/editor/document/row.go index 3ffe87a..1fe40a7 100644 --- a/internal/editor/document/row.go +++ b/internal/editor/document/row.go @@ -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 { diff --git a/internal/editor/draw.go b/internal/editor/draw.go index 5531f5e..ca2be71 100644 --- a/internal/editor/draw.go +++ b/internal/editor/draw.go @@ -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) diff --git a/internal/editor/highlight/constants.go b/internal/editor/highlight/constants.go new file mode 100644 index 0000000..a78d120 --- /dev/null +++ b/internal/editor/highlight/constants.go @@ -0,0 +1,9 @@ +package highlight + +// ---------------------------------------------------------------------------- +// !Syntax Highlighting Constants +// ---------------------------------------------------------------------------- +const ( + Normal = iota + Number +) \ No newline at end of file diff --git a/internal/editor/highlight/fn.go b/internal/editor/highlight/fn.go new file mode 100644 index 0000000..33e88c2 --- /dev/null +++ b/internal/editor/highlight/fn.go @@ -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 + } +} diff --git a/internal/gilo/constants.go b/internal/gilo/constants.go index b2e9bcf..04b4c50 100644 --- a/internal/gilo/constants.go +++ b/internal/gilo/constants.go @@ -9,3 +9,4 @@ const ( TabSize = 4 QuitTimes = 3 ) +