Complete step 164 in Chapter 7 of Kilo tutorial
Some checks failed
timw4mail/gilo/pipeline/head There was a failure building this commit

This commit is contained in:
Timothy Warren 2023-10-04 10:37:47 -04:00
parent 3e8a08e454
commit 469679cf6f
6 changed files with 90 additions and 11 deletions

View File

@ -74,9 +74,13 @@ func (e *Editor) save() {
e.doc.Filename = e.prompt("Save as: %s (ESC to cancel)", nil) e.doc.Filename = e.prompt("Save as: %s (ESC to cancel)", nil)
if e.doc.Filename == "" { if e.doc.Filename == "" {
e.SetStatusMessage("Save aborted") e.SetStatusMessage("Save aborted")
return
} }
} }
// Set the syntax highlighting type, now that we now the file extension
e.doc.SelectSyntaxHighlight()
size := e.doc.Save() size := e.doc.Save()
if size > 0 { if size > 0 {

View File

@ -34,6 +34,8 @@ func (d *Document) Open(filename string) {
defer file.Close() defer file.Close()
d.Filename = filename d.Filename = filename
d.SelectSyntaxHighlight()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines) scanner.Split(bufio.ScanLines)
@ -175,3 +177,12 @@ func (d *Document) delRow(at int) {
d.rows = newSlice d.rows = newSlice
d.dirty = true d.dirty = true
} }
func (d *Document) SelectSyntaxHighlight() {
d.Syntax = highlight.GetSyntaxByFilename(d.Filename)
// Update syntax highlighting when syntax type changes
for _, row := range d.rows {
row.updateSyntax()
}
}

View File

@ -1,6 +1,9 @@
package document package document
import "testing" import (
"testing"
"timshome.page/gilo/editor/highlight"
)
func TestNewDocument(t *testing.T) { func TestNewDocument(t *testing.T) {
d := NewDocument() d := NewDocument()

View File

@ -10,13 +10,3 @@ const (
) )
const HighlightNumbers = (1 << 0) const HighlightNumbers = (1 << 0)
var HLDB = []*Syntax{{
"c",
[]string{".c", ".h", ".cpp"},
HighlightNumbers,
}, {
"go",
[]string{".go"},
HighlightNumbers,
}}

View File

@ -1,7 +1,44 @@
package highlight package highlight
import "strings"
type Syntax struct { type Syntax struct {
FileType string FileType string
FileMatch []string FileMatch []string
Flags int Flags int
} }
// HLDB - The "database" of syntax types
var HLDB = []*Syntax{{
"c",
[]string{".c", ".h", ".cpp"},
HighlightNumbers,
}, {
"go",
[]string{".go", "go.mod"},
HighlightNumbers,
}, {
"makefile",
[]string{"Makefile", "makefile"},
0,
}}
func GetSyntaxByFilename(filename string) *Syntax {
if filename == "" {
return nil
}
extInd := strings.LastIndex(filename, ".")
ext := filename[extInd:len(filename)]
for i := 0; i < len(HLDB); i++ {
s := HLDB[i]
for j := range s.FileMatch {
if s.FileMatch[j] == ext || strings.Contains(filename, s.FileMatch[j]) {
return s
}
}
}
return nil
}

View File

@ -0,0 +1,34 @@
package highlight
import "testing"
func TestGetSyntaxByFilename(t *testing.T) {
var s *Syntax = nil
t.Run("returns nil on empty filename", func(t *testing.T) {
got := GetSyntaxByFilename("")
want := s
if got != want {
t.Errorf("got %q want 'nil'", got.FileType)
}
})
t.Run("returns nil on undefined file type", func(t *testing.T) {
got := GetSyntaxByFilename("foo.bar")
want := s
if got != want {
t.Errorf("got %q want 'nil'", got.FileType)
}
})
t.Run("returns c on .c file", func(t *testing.T) {
got := GetSyntaxByFilename("kilo.c")
want := "c"
if got.FileType != want {
t.Errorf("got %q want %q", got.FileType, want)
}
})
}