1
0
Fork 0

Highlight multiline comments
timw4mail/gilo/pipeline/head This commit looks good Details

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 {
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
}

View File

@ -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 {

View File

@ -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 {

View File

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