From 469679cf6f1680832b68a58e4405a7e8b564e843 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 4 Oct 2023 10:37:47 -0400 Subject: [PATCH] Complete step 164 in Chapter 7 of Kilo tutorial --- editor/Editor.go | 4 ++++ editor/document/document.go | 11 ++++++++++ editor/document/document_test.go | 5 ++++- editor/highlight/constants.go | 10 --------- editor/highlight/syntax.go | 37 ++++++++++++++++++++++++++++++++ editor/highlight/syntax_test.go | 34 +++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 editor/highlight/syntax_test.go diff --git a/editor/Editor.go b/editor/Editor.go index 7131b06..8ab1caf 100644 --- a/editor/Editor.go +++ b/editor/Editor.go @@ -74,9 +74,13 @@ func (e *Editor) save() { e.doc.Filename = e.prompt("Save as: %s (ESC to cancel)", nil) if e.doc.Filename == "" { e.SetStatusMessage("Save aborted") + return } } + // Set the syntax highlighting type, now that we now the file extension + e.doc.SelectSyntaxHighlight() + size := e.doc.Save() if size > 0 { diff --git a/editor/document/document.go b/editor/document/document.go index 8c9b15a..222c095 100644 --- a/editor/document/document.go +++ b/editor/document/document.go @@ -34,6 +34,8 @@ func (d *Document) Open(filename string) { defer file.Close() d.Filename = filename + d.SelectSyntaxHighlight() + scanner := bufio.NewScanner(file) scanner.Split(bufio.ScanLines) @@ -175,3 +177,12 @@ func (d *Document) delRow(at int) { d.rows = newSlice 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() + } +} diff --git a/editor/document/document_test.go b/editor/document/document_test.go index fbc23a5..0a2307d 100644 --- a/editor/document/document_test.go +++ b/editor/document/document_test.go @@ -1,6 +1,9 @@ package document -import "testing" +import ( + "testing" + "timshome.page/gilo/editor/highlight" +) func TestNewDocument(t *testing.T) { d := NewDocument() diff --git a/editor/highlight/constants.go b/editor/highlight/constants.go index 78256e8..f07a536 100644 --- a/editor/highlight/constants.go +++ b/editor/highlight/constants.go @@ -10,13 +10,3 @@ const ( ) const HighlightNumbers = (1 << 0) - -var HLDB = []*Syntax{{ - "c", - []string{".c", ".h", ".cpp"}, - HighlightNumbers, -}, { - "go", - []string{".go"}, - HighlightNumbers, -}} diff --git a/editor/highlight/syntax.go b/editor/highlight/syntax.go index a95f059..b8b6530 100644 --- a/editor/highlight/syntax.go +++ b/editor/highlight/syntax.go @@ -1,7 +1,44 @@ package highlight +import "strings" + type Syntax struct { FileType string FileMatch []string 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 +} diff --git a/editor/highlight/syntax_test.go b/editor/highlight/syntax_test.go new file mode 100644 index 0000000..f4fdaf9 --- /dev/null +++ b/editor/highlight/syntax_test.go @@ -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) + } + }) +}