Highlight multiline comments
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2023-10-06 15:23:01 -04:00
parent 79846e372a
commit 9eb067485d
4 changed files with 43 additions and 17 deletions

View File

@ -16,13 +16,11 @@ type Document struct {
} }
func NewDocument() *Document { func NewDocument() *Document {
var rows []*Row
return &Document{ return &Document{
false, false,
"", "",
nil, nil,
rows, []*Row{},
} }
} }
@ -85,13 +83,15 @@ func (d *Document) GetRow(at int) *Row {
} }
func (d *Document) AppendRow(s string) { func (d *Document) AppendRow(s string) {
newRow := newRow(d, s) at := d.RowCount()
newRow := newRow(at, d, s)
newRow.update() newRow.update()
d.rows = append(d.rows, newRow) d.rows = append(d.rows, newRow)
d.dirty = true d.dirty = true
} }
// InsertRow is the equivalent of editorInsertRow in kilo
func (d *Document) InsertRow(at int, s string) { func (d *Document) InsertRow(at int, s string) {
if at < 0 || at > d.RowCount() { if at < 0 || at > d.RowCount() {
return return
@ -105,9 +105,14 @@ func (d *Document) InsertRow(at int, s string) {
// Splice it back together // Splice it back together
newRows = append(newRows, start...) newRows = append(newRows, start...)
newRows = append(newRows, newRow(d, s)) newRows = append(newRows, newRow(at, d, s))
newRows = append(newRows, end...) newRows = append(newRows, end...)
// Update indices
for j := at + 1; j <= len(newRows); j++ {
newRows[j].index += 1
}
d.rows = newRows d.rows = newRows
d.dirty = true d.dirty = true
} }
@ -163,6 +168,7 @@ func (d *Document) IsDirty() bool {
return d.dirty return d.dirty
} }
// delRow is the equivalent of kilo's editorDelRow
func (d *Document) delRow(at int) { func (d *Document) delRow(at int) {
var newSlice []*Row var newSlice []*Row
@ -174,6 +180,11 @@ func (d *Document) delRow(at int) {
newSlice = append(newSlice, start...) newSlice = append(newSlice, start...)
newSlice = append(newSlice, end...) newSlice = append(newSlice, end...)
// Update indices
for j := at - 1; j <= len(newSlice); j++ {
newSlice[j].index -= 1
}
d.rows = newSlice d.rows = newSlice
d.dirty = true d.dirty = true
} }

View File

@ -8,13 +8,15 @@ import (
) )
type Row struct { type Row struct {
parent *Document index int
chars []rune parent *Document
render []rune chars []rune
Hl []int 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 chars []rune
var render []rune var render []rune
@ -23,7 +25,14 @@ func newRow(parent *Document, s string) *Row {
render = append(render, ch) render = append(render, ch)
} }
return &Row{parent, chars, render, []int{}} return &Row{
at,
parent,
chars,
render,
[]int{},
false,
}
} }
func (r *Row) Size() int { func (r *Row) Size() int {
@ -154,7 +163,7 @@ func (r *Row) updateSyntax() {
prevSep := true prevSep := true
inString := '0' inString := '0'
inComment := false inComment := r.index > 0 && r.parent.rows[r.index-1].startsComment
for i < r.RenderSize() { for i < r.RenderSize() {
ch := r.render[i] ch := r.render[i]
prevHl := highlight.Normal prevHl := highlight.Normal
@ -283,6 +292,12 @@ func (r *Row) updateSyntax() {
prevSep = char.IsSeparator(ch) prevSep = char.IsSeparator(ch)
i++ 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 { func (r *Row) toString() string {

View File

@ -6,7 +6,7 @@ var doc = NewDocument()
func TestNewRow(t *testing.T) { func TestNewRow(t *testing.T) {
str := "abcdefg" str := "abcdefg"
row := newRow(doc, str) row := newRow(1, doc, str)
got := string(row.chars) got := string(row.chars)
want := str want := str
@ -18,7 +18,7 @@ func TestNewRow(t *testing.T) {
func TestRowSize(t *testing.T) { func TestRowSize(t *testing.T) {
str := "abcdefg" str := "abcdefg"
row := newRow(doc, str) row := newRow(1, doc, str)
got := row.Size() got := row.Size()
want := 7 want := 7
@ -30,7 +30,7 @@ func TestRowSize(t *testing.T) {
func TestRenderSize(t *testing.T) { func TestRenderSize(t *testing.T) {
str := "\tabcdefg" str := "\tabcdefg"
row := newRow(doc, str) row := newRow(1, doc, str)
row.update() row.update()
got := row.RenderSize() got := row.RenderSize()
@ -57,7 +57,7 @@ var insertRuneTests = []insertRune{
func TestInsertRune(t *testing.T) { func TestInsertRune(t *testing.T) {
for _, test := range insertRuneTests { for _, test := range insertRuneTests {
row := newRow(doc, test.initial) row := newRow(1, doc, test.initial)
row.insertRune(test.ch, test.at) row.insertRune(test.ch, test.at)
if row.Size() != 5 { if row.Size() != 5 {

View File

@ -4,7 +4,7 @@ default:
# Run all the tests # Run all the tests
test: test:
go test ./... -v go test ./...
_cover: _cover:
go test ./... -coverprofile=coverage.out go test ./... -coverprofile=coverage.out