From 9eb067485dfd468e6442dcb902e6bbabe8eb74be Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 6 Oct 2023 15:23:01 -0400 Subject: [PATCH] Highlight multiline comments --- editor/document/document.go | 21 ++++++++++++++++----- editor/document/row.go | 29 ++++++++++++++++++++++------- editor/document/row_test.go | 8 ++++---- justfile | 2 +- 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/editor/document/document.go b/editor/document/document.go index 222c095..d4958cd 100644 --- a/editor/document/document.go +++ b/editor/document/document.go @@ -16,13 +16,11 @@ type Document struct { } func NewDocument() *Document { - var rows []*Row - return &Document{ false, "", nil, - rows, + []*Row{}, } } @@ -85,13 +83,15 @@ func (d *Document) GetRow(at int) *Row { } func (d *Document) AppendRow(s string) { - newRow := newRow(d, s) + at := d.RowCount() + newRow := newRow(at, d, s) newRow.update() d.rows = append(d.rows, newRow) d.dirty = true } +// InsertRow is the equivalent of editorInsertRow in kilo func (d *Document) InsertRow(at int, s string) { if at < 0 || at > d.RowCount() { return @@ -105,9 +105,14 @@ func (d *Document) InsertRow(at int, s string) { // Splice it back together newRows = append(newRows, start...) - newRows = append(newRows, newRow(d, s)) + newRows = append(newRows, newRow(at, d, s)) newRows = append(newRows, end...) + // Update indices + for j := at + 1; j <= len(newRows); j++ { + newRows[j].index += 1 + } + d.rows = newRows d.dirty = true } @@ -163,6 +168,7 @@ func (d *Document) IsDirty() bool { return d.dirty } +// delRow is the equivalent of kilo's editorDelRow func (d *Document) delRow(at int) { var newSlice []*Row @@ -174,6 +180,11 @@ func (d *Document) delRow(at int) { newSlice = append(newSlice, start...) newSlice = append(newSlice, end...) + // Update indices + for j := at - 1; j <= len(newSlice); j++ { + newSlice[j].index -= 1 + } + d.rows = newSlice d.dirty = true } diff --git a/editor/document/row.go b/editor/document/row.go index 097b318..dce2b08 100644 --- a/editor/document/row.go +++ b/editor/document/row.go @@ -8,13 +8,15 @@ import ( ) type Row struct { - parent *Document - chars []rune - render []rune - Hl []int + index int + parent *Document + chars []rune + render []rune + Hl []int + startsComment bool } -func newRow(parent *Document, s string) *Row { +func newRow(at int, parent *Document, s string) *Row { var chars []rune var render []rune @@ -23,7 +25,14 @@ func newRow(parent *Document, s string) *Row { render = append(render, ch) } - return &Row{parent, chars, render, []int{}} + return &Row{ + at, + parent, + chars, + render, + []int{}, + false, + } } func (r *Row) Size() int { @@ -154,7 +163,7 @@ func (r *Row) updateSyntax() { prevSep := true inString := '0' - inComment := false + inComment := r.index > 0 && r.parent.rows[r.index-1].startsComment for i < r.RenderSize() { ch := r.render[i] prevHl := highlight.Normal @@ -283,6 +292,12 @@ func (r *Row) updateSyntax() { prevSep = char.IsSeparator(ch) i++ } + + changed := r.startsComment != inComment + r.startsComment = inComment + if changed && (r.index+1) < r.parent.RowCount() { + r.parent.rows[r.index+1].updateSyntax() + } } func (r *Row) toString() string { diff --git a/editor/document/row_test.go b/editor/document/row_test.go index b9e448f..d46e4a9 100644 --- a/editor/document/row_test.go +++ b/editor/document/row_test.go @@ -6,7 +6,7 @@ var doc = NewDocument() func TestNewRow(t *testing.T) { str := "abcdefg" - row := newRow(doc, str) + row := newRow(1, doc, str) got := string(row.chars) want := str @@ -18,7 +18,7 @@ func TestNewRow(t *testing.T) { func TestRowSize(t *testing.T) { str := "abcdefg" - row := newRow(doc, str) + row := newRow(1, doc, str) got := row.Size() want := 7 @@ -30,7 +30,7 @@ func TestRowSize(t *testing.T) { func TestRenderSize(t *testing.T) { str := "\tabcdefg" - row := newRow(doc, str) + row := newRow(1, doc, str) row.update() got := row.RenderSize() @@ -57,7 +57,7 @@ var insertRuneTests = []insertRune{ func TestInsertRune(t *testing.T) { for _, test := range insertRuneTests { - row := newRow(doc, test.initial) + row := newRow(1, doc, test.initial) row.insertRune(test.ch, test.at) if row.Size() != 5 { diff --git a/justfile b/justfile index 2d80492..d88dea6 100644 --- a/justfile +++ b/justfile @@ -4,7 +4,7 @@ default: # Run all the tests test: - go test ./... -v + go test ./... _cover: go test ./... -coverprofile=coverage.out