i think this works now

master
Stephen Searles 8 years ago
parent 98d79d60a8
commit 432138f52f
  1. 7
      acedoc.go
  2. 31
      delta.go

@ -74,8 +74,11 @@ func (d Delta) rows() int {
} }
func (d Delta) cols() int { func (d Delta) cols() int {
r := int(d.End.Column) - int(d.Start.Column) if d.Start.Row == d.End.Row {
return r return int(d.End.Column) - int(d.Start.Column)
} else {
return int(d.End.Column)
}
} }
func (d *Document) Contents() string { func (d *Document) Contents() string {

@ -112,24 +112,33 @@ func (d Delta) Equal(other Delta) bool {
} }
func Remove(row, col uint, str string) Delta { func Remove(row, col uint, str string) Delta {
lines := strings.Split(str, "\n") dl := Insert(row, col, str)
nlines := len(lines) - 1 dl.Action = DeltaRemove
return Delta{ return dl
Action: DeltaRemove,
Lines: lines,
Start: Position{row, col},
End: Position{row + uint(nlines), uint(len(lines[nlines]))},
}
} }
func Insert(row, col uint, str string) Delta { func Insert(row, col uint, str string) Delta {
lines := strings.Split(str, "\n") lines := strings.Split(str, "\n")
nlines := uint(len(lines) - 1)
var endcol uint
addedLines := uint(len(lines) - 1)
if len(lines) == 0 {
addedLines = 1
} else {
lastLine := lines[len(lines)-1]
endcol += uint(len(lastLine))
}
if addedLines == 0 {
endcol += col
}
return Delta{ return Delta{
Action: DeltaInsert, Action: DeltaInsert,
Lines: lines, Lines: lines,
Start: Position{row, col}, Start: Position{row, col},
End: Position{row + nlines, uint(len(lines[nlines]))}, End: Position{row + addedLines, endcol},
} }
} }
@ -215,7 +224,7 @@ func (d *Document) validate(dl Delta) error {
} }
if dl.Action == DeltaRemove && !d.InDocument(dl.End) { if dl.Action == DeltaRemove && !d.InDocument(dl.End) {
return fmt.Errorf("end is not in document for remove") return fmt.Errorf("end %v is not in document %q for remove", dl.End, d.contents())
} }
if dl.nLines() != dl.nRows() { if dl.nLines() != dl.nRows() {

Loading…
Cancel
Save