1
0
Fork 0

Properly highlight numbers
timw4mail/gilo/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2021-04-13 14:43:31 -04:00
parent fe1c54317e
commit 9735d9b252
3 changed files with 22 additions and 8 deletions

View File

@ -3,7 +3,8 @@ package document
import ( import (
"strings" "strings"
"timshome.page/gilo/editor/highlight" "timshome.page/gilo/editor/highlight"
gilo2 "timshome.page/gilo/internal/gilo" "timshome.page/gilo/internal/gilo"
"timshome.page/gilo/key"
"unicode" "unicode"
) )
@ -33,7 +34,7 @@ func (r *Row) RenderSize() int {
return len(r.render) return len(r.render)
} }
func (r *Row) Render(at *gilo2.Point) string { func (r *Row) Render(at *gilo.Point) string {
return string(r.render[at.X:]) return string(r.render[at.X:])
} }
@ -95,7 +96,7 @@ func (r *Row) deleteRune(at int) {
func (r *Row) update() { func (r *Row) update() {
r.render = r.render[:0] r.render = r.render[:0]
replacement := strings.Repeat(" ", gilo2.TabSize) replacement := strings.Repeat(" ", gilo.TabSize)
str := strings.ReplaceAll(string(r.chars), "\t", replacement) str := strings.ReplaceAll(string(r.chars), "\t", replacement)
for _, ch := range str { for _, ch := range str {
@ -107,17 +108,30 @@ func (r *Row) update() {
func (r *Row) updateSyntax() { func (r *Row) updateSyntax() {
i := 0 i := 0
prevSep := true
r.Hl = make([]int, r.RenderSize()) r.Hl = make([]int, r.RenderSize())
for i < r.RenderSize() { for i < r.RenderSize() {
ch := r.render[i] ch := r.render[i]
if unicode.IsDigit(ch) { prevHl := highlight.Normal
if i > 0 {
prevHl = r.Hl[i - 1]
}
if (unicode.IsDigit(ch) && (prevSep || prevHl == highlight.Number)) ||
(ch == '.' && prevHl == highlight.Number) {
r.Hl[i] = highlight.Number r.Hl[i] = highlight.Number
i += 1
prevSep = false
continue
} else { } else {
r.Hl[i] = highlight.Normal r.Hl[i] = highlight.Normal
} }
prevSep = key.IsSeparator(ch)
i++ i++
} }
} }
@ -131,7 +145,7 @@ func (r *Row) CursorXToRenderX(cursorX int) (renderX int) {
for i := 0; i < cursorX; i++ { for i := 0; i < cursorX; i++ {
if r.chars[i] == '\t' { if r.chars[i] == '\t' {
renderX += (gilo2.TabSize - 1) - (renderX % gilo2.TabSize) renderX += (gilo.TabSize - 1) - (renderX % gilo.TabSize)
} }
renderX += 1 renderX += 1
@ -146,7 +160,7 @@ func (r *Row) RenderXtoCursorX(renderX int) (cursorX int) {
for cursorX = 0; cursorX < r.Size(); cursorX++ { for cursorX = 0; cursorX < r.Size(); cursorX++ {
if r.chars[cursorX] == '\t' { if r.chars[cursorX] == '\t' {
currentRenderX += (gilo2.TabSize - 1) - (currentRenderX % gilo2.TabSize) currentRenderX += (gilo.TabSize - 1) - (currentRenderX % gilo.TabSize)
} else { } else {
currentRenderX += 1 currentRenderX += 1
} }

View File

@ -1,4 +1,4 @@
// The main interface/implementation of the editor object // Package editor The main interface/implementation of the editor object
package editor package editor
import ( import (

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"golang.org/x/term"
"fmt" "fmt"
"golang.org/x/term"
"os" "os"
"timshome.page/gilo/editor" "timshome.page/gilo/editor"
"timshome.page/gilo/terminal" "timshome.page/gilo/terminal"