Bounds check delete regions in rope buffer
This commit is contained in:
parent
296fa8b4ac
commit
9133ab55d0
@ -114,7 +114,17 @@ func (b *RopeBuffer) Insert(line, col int, value []byte) {
|
|||||||
// Remove deletes any characters between startLine, startCol, and endLine,
|
// Remove deletes any characters between startLine, startCol, and endLine,
|
||||||
// endCol, inclusive bounds.
|
// endCol, inclusive bounds.
|
||||||
func (b *RopeBuffer) Remove(startLine, startCol, endLine, endCol int) {
|
func (b *RopeBuffer) Remove(startLine, startCol, endLine, endCol int) {
|
||||||
(*rope.Node)(b).Remove(b.pos(startLine, startCol), b.pos(endLine, endCol)+1)
|
start := b.pos(startLine, startCol)
|
||||||
|
end := b.pos(endLine, endCol) + 1
|
||||||
|
|
||||||
|
if len := (*rope.Node)(b).Len(); end >= len {
|
||||||
|
end = len
|
||||||
|
if start > end {
|
||||||
|
start = end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(*rope.Node)(b).Remove(start, end)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of occurrences of 'sequence' in the buffer, within the range
|
// Returns the number of occurrences of 'sequence' in the buffer, within the range
|
||||||
|
@ -4,16 +4,29 @@ import "testing"
|
|||||||
|
|
||||||
func TestRopeInserting(t *testing.T) {
|
func TestRopeInserting(t *testing.T) {
|
||||||
var buf Buffer = NewRopeBuffer([]byte("some"))
|
var buf Buffer = NewRopeBuffer([]byte("some"))
|
||||||
buf.Insert(0, 4, []byte(" text")) // Insert " text" after "some"
|
buf.Insert(0, 4, []byte(" text\n")) // Insert " text" after "some"
|
||||||
buf.Insert(0, 0, []byte("with\n\t"))
|
buf.Insert(0, 0, []byte("with\n\t"))
|
||||||
// "with\n\tsome text"
|
//with
|
||||||
|
// some text
|
||||||
|
//
|
||||||
|
|
||||||
buf.Remove(0, 4, 1, 5) // Delete from line 0, col 4, to line 1, col 6 "\n\tsome "
|
buf.Remove(0, 4, 1, 5) // Delete from line 0, col 4, to line 1, col 6 "\n\tsome "
|
||||||
|
|
||||||
if str := string(buf.Bytes()); str != "withtext" {
|
if str := string(buf.Bytes()); str != "withtext\n" {
|
||||||
t.Errorf("string does not match \"withtext\", got %#v", str)
|
t.Errorf("string does not match \"withtext\", got %#v", str)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//withtext
|
||||||
|
//
|
||||||
|
|
||||||
|
// Note the inclusive bounds and pointing to last line, first column.
|
||||||
|
buf.Remove(0, 0, 1, 0) // Delete all of the buffer
|
||||||
|
|
||||||
|
if str := string(buf.Bytes()); str != "" {
|
||||||
|
t.Errorf("string does not math \"\", got %#v", str)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRopeBounds(t *testing.T) {
|
func TestRopeBounds(t *testing.T) {
|
||||||
|
@ -302,6 +302,7 @@ func (t *TextEdit) getColumnWidth() int {
|
|||||||
// If the returned string is empty, then nothing was selected. The slice returned may or may not
|
// If the returned string is empty, then nothing was selected. The slice returned may or may not
|
||||||
// be a copy of the buffer, so do not write to it.
|
// be a copy of the buffer, so do not write to it.
|
||||||
func (t *TextEdit) GetSelectedBytes() []byte {
|
func (t *TextEdit) GetSelectedBytes() []byte {
|
||||||
|
// TODO: there's a bug with copying text
|
||||||
if t.selectMode {
|
if t.selectMode {
|
||||||
return t.Buffer.Slice(t.selection.StartLine, t.selection.StartCol, t.selection.EndLine, t.selection.EndCol)
|
return t.Buffer.Slice(t.selection.StartLine, t.selection.StartCol, t.selection.EndLine, t.selection.EndCol)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user