|
|
|
@ -112,24 +112,33 @@ func (d Delta) Equal(other Delta) bool { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func Remove(row, col uint, str string) Delta { |
|
|
|
|
lines := strings.Split(str, "\n") |
|
|
|
|
nlines := len(lines) - 1 |
|
|
|
|
return Delta{ |
|
|
|
|
Action: DeltaRemove, |
|
|
|
|
Lines: lines, |
|
|
|
|
Start: Position{row, col}, |
|
|
|
|
End: Position{row + uint(nlines), uint(len(lines[nlines]))}, |
|
|
|
|
} |
|
|
|
|
dl := Insert(row, col, str) |
|
|
|
|
dl.Action = DeltaRemove |
|
|
|
|
return dl |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func Insert(row, col uint, str string) Delta { |
|
|
|
|
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{ |
|
|
|
|
Action: DeltaInsert, |
|
|
|
|
Lines: lines, |
|
|
|
|
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) { |
|
|
|
|
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() { |
|
|
|
|