mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-10 22:52:51 +09:00
buffer: Remove data as structure element of Line
This commit is contained in:
@@ -10,28 +10,6 @@ import (
|
||||
"github.com/micro-editor/micro/v2/pkg/highlight"
|
||||
)
|
||||
|
||||
// Finds the byte index of the nth rune in a byte slice
|
||||
func runeToByteIndex(n int, txt []byte) int {
|
||||
if n == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
count := 0
|
||||
i := 0
|
||||
for len(txt) > 0 {
|
||||
_, _, size := util.DecodeCharacter(txt)
|
||||
|
||||
txt = txt[size:]
|
||||
count += size
|
||||
i++
|
||||
|
||||
if i == n {
|
||||
break
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// A searchState contains the search match info for a single line
|
||||
type searchState struct {
|
||||
search string
|
||||
@@ -41,10 +19,9 @@ type searchState struct {
|
||||
done bool
|
||||
}
|
||||
|
||||
// A Line contains the data in bytes as well as a highlight state, match
|
||||
// A Line contains the slice of runes as well as a highlight state, match
|
||||
// and a flag for whether the highlighting needs to be updated
|
||||
type Line struct {
|
||||
data []byte
|
||||
runes []rune
|
||||
|
||||
state highlight.State
|
||||
@@ -60,6 +37,10 @@ type Line struct {
|
||||
search map[*Buffer]*searchState
|
||||
}
|
||||
|
||||
func (l Line) data() []byte {
|
||||
return []byte(string(l.runes))
|
||||
}
|
||||
|
||||
const (
|
||||
// Line ending file formats
|
||||
FFAuto = 0 // Autodetect format
|
||||
@@ -95,7 +76,7 @@ func Append(slice []Line, data ...Line) []Line {
|
||||
return slice
|
||||
}
|
||||
|
||||
// NewLineArray returns a new line array from an array of bytes
|
||||
// NewLineArray returns a new line array from an array of runes
|
||||
func NewLineArray(size uint64, endings FileFormat, reader io.Reader) *LineArray {
|
||||
la := new(LineArray)
|
||||
|
||||
@@ -149,7 +130,6 @@ func NewLineArray(size uint64, endings FileFormat, reader io.Reader) *LineArray
|
||||
if err == io.EOF {
|
||||
runes, _ := util.DecodeCharacters(data)
|
||||
la.lines = Append(la.lines, Line{
|
||||
data: data,
|
||||
runes: runes,
|
||||
state: nil,
|
||||
match: nil,
|
||||
@@ -160,7 +140,6 @@ func NewLineArray(size uint64, endings FileFormat, reader io.Reader) *LineArray
|
||||
} else {
|
||||
runes, _ := util.DecodeCharacters(data[:dlen-1])
|
||||
la.lines = Append(la.lines, Line{
|
||||
data: data[:dlen-1],
|
||||
runes: runes,
|
||||
state: nil,
|
||||
match: nil,
|
||||
@@ -179,7 +158,7 @@ func (la *LineArray) Bytes() []byte {
|
||||
// initsize should provide a good estimate
|
||||
b.Grow(int(la.initsize + 4096))
|
||||
for i, l := range la.lines {
|
||||
b.Write(l.data)
|
||||
b.Write(l.data())
|
||||
if i != len(la.lines)-1 {
|
||||
if la.Endings == FFDos {
|
||||
b.WriteByte('\r')
|
||||
@@ -193,14 +172,12 @@ func (la *LineArray) Bytes() []byte {
|
||||
// newlineBelow adds a newline below the given line number
|
||||
func (la *LineArray) newlineBelow(y int) {
|
||||
la.lines = append(la.lines, Line{
|
||||
data: []byte{' '},
|
||||
runes: []rune{},
|
||||
state: nil,
|
||||
match: nil,
|
||||
})
|
||||
copy(la.lines[y+2:], la.lines[y+1:])
|
||||
la.lines[y+1] = Line{
|
||||
data: []byte{},
|
||||
runes: []rune{},
|
||||
state: la.lines[y].state,
|
||||
match: nil,
|
||||
@@ -212,41 +189,57 @@ func (la *LineArray) insert(pos Loc, value []byte) {
|
||||
la.lock.Lock()
|
||||
defer la.lock.Unlock()
|
||||
|
||||
x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
|
||||
for i := 0; i < len(value); i++ {
|
||||
if value[i] == '\n' || (value[i] == '\r' && i < len(value)-1 && value[i+1] == '\n') {
|
||||
runes, _ := util.DecodeCharacters(value)
|
||||
x, y := util.Min(pos.X, len(la.lines[pos.Y].runes)), pos.Y
|
||||
start := -1
|
||||
for i := 0; i < len(runes); i++ {
|
||||
if runes[i] == '\n' || (runes[i] == '\r' && i < len(runes)-1 && runes[i+1] == '\n') {
|
||||
la.split(Loc{x, y})
|
||||
if i > 0 && start < len(runes) && start < i {
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
la.insertRunes(Loc{x, y}, runes[start:i])
|
||||
}
|
||||
|
||||
x = 0
|
||||
y++
|
||||
|
||||
if value[i] == '\r' {
|
||||
if runes[i] == '\r' {
|
||||
i++
|
||||
}
|
||||
if i+1 <= len(runes) {
|
||||
|
||||
start = i + 1
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
la.insertByte(Loc{x, y}, value[i])
|
||||
x++
|
||||
}
|
||||
if start < 0 {
|
||||
la.insertRunes(Loc{x, y}, runes)
|
||||
} else if start < len(runes) {
|
||||
la.insertRunes(Loc{x, y}, runes[start:])
|
||||
}
|
||||
}
|
||||
|
||||
// InsertByte inserts a byte at a given location
|
||||
func (la *LineArray) insertByte(pos Loc, value byte) {
|
||||
la.lines[pos.Y].data = append(la.lines[pos.Y].data, 0)
|
||||
copy(la.lines[pos.Y].data[pos.X+1:], la.lines[pos.Y].data[pos.X:])
|
||||
la.lines[pos.Y].data[pos.X] = value
|
||||
// Inserts a rune array at a given location
|
||||
func (la *LineArray) insertRunes(pos Loc, runes []rune) {
|
||||
la.lines[pos.Y].runes = append(la.lines[pos.Y].runes, runes...)
|
||||
copy(la.lines[pos.Y].runes[pos.X+len(runes):], la.lines[pos.Y].runes[pos.X:])
|
||||
copy(la.lines[pos.Y].runes[pos.X:], runes)
|
||||
}
|
||||
|
||||
// joinLines joins the two lines a and b
|
||||
func (la *LineArray) joinLines(a, b int) {
|
||||
la.lines[a].data = append(la.lines[a].data, la.lines[b].data...)
|
||||
la.insertRunes(Loc{len(la.lines[a].runes), a}, la.lines[b].runes)
|
||||
la.deleteLine(b)
|
||||
}
|
||||
|
||||
// split splits a line at a given position
|
||||
func (la *LineArray) split(pos Loc) {
|
||||
la.newlineBelow(pos.Y)
|
||||
la.lines[pos.Y+1].data = append(la.lines[pos.Y+1].data, la.lines[pos.Y].data[pos.X:]...)
|
||||
la.insertRunes(Loc{0, pos.Y + 1}, la.lines[pos.Y].runes[pos.X:])
|
||||
la.lines[pos.Y+1].state = la.lines[pos.Y].state
|
||||
la.lines[pos.Y].state = nil
|
||||
la.lines[pos.Y].match = nil
|
||||
@@ -260,10 +253,10 @@ func (la *LineArray) remove(start, end Loc) []byte {
|
||||
defer la.lock.Unlock()
|
||||
|
||||
sub := la.Substr(start, end)
|
||||
startX := runeToByteIndex(start.X, la.lines[start.Y].data)
|
||||
endX := runeToByteIndex(end.X, la.lines[end.Y].data)
|
||||
startX := util.Min(start.X, len(la.lines[start.Y].runes))
|
||||
endX := util.Min(end.X, len(la.lines[end.Y].runes))
|
||||
if start.Y == end.Y {
|
||||
la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
|
||||
la.lines[start.Y].runes = append(la.lines[start.Y].runes[:startX], la.lines[start.Y].runes[endX:]...)
|
||||
} else {
|
||||
la.deleteLines(start.Y+1, end.Y-1)
|
||||
la.deleteToEnd(Loc{startX, start.Y})
|
||||
@@ -275,12 +268,12 @@ func (la *LineArray) remove(start, end Loc) []byte {
|
||||
|
||||
// deleteToEnd deletes from the end of a line to the position
|
||||
func (la *LineArray) deleteToEnd(pos Loc) {
|
||||
la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
|
||||
la.lines[pos.Y].runes = la.lines[pos.Y].runes[:pos.X]
|
||||
}
|
||||
|
||||
// deleteFromStart deletes from the start of a line to the position
|
||||
func (la *LineArray) deleteFromStart(pos Loc) {
|
||||
la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
|
||||
la.lines[pos.Y].runes = la.lines[pos.Y].runes[pos.X+1:]
|
||||
}
|
||||
|
||||
// deleteLine deletes the line number
|
||||
@@ -294,22 +287,19 @@ func (la *LineArray) deleteLines(y1, y2 int) {
|
||||
|
||||
// Substr returns the string representation between two locations
|
||||
func (la *LineArray) Substr(start, end Loc) []byte {
|
||||
startX := runeToByteIndex(start.X, la.lines[start.Y].data)
|
||||
endX := runeToByteIndex(end.X, la.lines[end.Y].data)
|
||||
if start.Y == end.Y {
|
||||
src := la.lines[start.Y].data[startX:endX]
|
||||
dest := make([]byte, len(src))
|
||||
copy(dest, src)
|
||||
return dest
|
||||
startX := util.Min(start.X, len(la.lines[start.Y].runes))
|
||||
endX := util.Min(end.X, len(la.lines[end.Y].runes))
|
||||
if start.Y == end.Y && startX <= endX {
|
||||
return []byte(string(la.lines[start.Y].runes[startX:endX]))
|
||||
}
|
||||
str := make([]byte, 0, len(la.lines[start.Y+1].data)*(end.Y-start.Y))
|
||||
str = append(str, la.lines[start.Y].data[startX:]...)
|
||||
var str []byte
|
||||
str = append(str, []byte(string(la.lines[start.Y].runes[startX:]))...)
|
||||
str = append(str, '\n')
|
||||
for i := start.Y + 1; i <= end.Y-1; i++ {
|
||||
str = append(str, la.lines[i].data...)
|
||||
str = append(str, []byte(string(la.lines[i].runes))...)
|
||||
str = append(str, '\n')
|
||||
}
|
||||
str = append(str, la.lines[end.Y].data[:endX]...)
|
||||
str = append(str, []byte(string(la.lines[end.Y].runes[:endX]))...)
|
||||
return str
|
||||
}
|
||||
|
||||
@@ -326,15 +316,31 @@ func (la *LineArray) Start() Loc {
|
||||
// End returns the location of the last character in the buffer
|
||||
func (la *LineArray) End() Loc {
|
||||
numlines := len(la.lines)
|
||||
return Loc{util.CharacterCount(la.lines[numlines-1].data), numlines - 1}
|
||||
return Loc{len(la.lines[numlines-1].runes), numlines - 1}
|
||||
}
|
||||
|
||||
// Line returns line n as an array of runes
|
||||
func (la *LineArray) Line(n int) []rune {
|
||||
if n >= len(la.lines) || n < 0 {
|
||||
return []rune{}
|
||||
}
|
||||
return la.lines[n].runes
|
||||
}
|
||||
|
||||
// LineBytes returns line n as an array of bytes
|
||||
func (la *LineArray) LineBytes(lineN int) []byte {
|
||||
if lineN >= len(la.lines) || lineN < 0 {
|
||||
func (la *LineArray) LineBytes(n int) []byte {
|
||||
if n >= len(la.lines) || n < 0 {
|
||||
return []byte{}
|
||||
}
|
||||
return la.lines[lineN].data
|
||||
return []byte(string(la.lines[n].runes))
|
||||
}
|
||||
|
||||
// LineString returns line n as an string
|
||||
func (la *LineArray) LineString(n int) string {
|
||||
if n >= len(la.lines) || n < 0 {
|
||||
return string("")
|
||||
}
|
||||
return string(la.lines[n].runes)
|
||||
}
|
||||
|
||||
// State gets the highlight state for the given line number
|
||||
@@ -416,7 +422,7 @@ func (la *LineArray) SearchMatch(b *Buffer, pos Loc) bool {
|
||||
if !s.done {
|
||||
s.match = nil
|
||||
start := Loc{0, lineN}
|
||||
end := Loc{util.CharacterCount(la.lines[lineN].data), lineN}
|
||||
end := Loc{len(la.lines[lineN].runes), lineN}
|
||||
for start.X < end.X {
|
||||
m, found, _ := b.FindNext(b.LastSearch, start, end, start, true, b.LastSearchRegex)
|
||||
if !found {
|
||||
|
||||
Reference in New Issue
Block a user