2018-08-27 22:53:10 +03:00
|
|
|
package buffer
|
2016-06-07 18:43:28 +03:00
|
|
|
|
|
|
|
import (
|
2016-11-29 21:44:30 +03:00
|
|
|
"bufio"
|
|
|
|
"io"
|
2020-01-29 04:54:14 +03:00
|
|
|
"sync"
|
2016-06-07 18:43:28 +03:00
|
|
|
"unicode/utf8"
|
2017-02-18 23:45:49 +03:00
|
|
|
|
2019-02-04 07:17:24 +03:00
|
|
|
"github.com/zyedidia/micro/pkg/highlight"
|
2016-06-07 18:43:28 +03:00
|
|
|
)
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// Finds the byte index of the nth rune in a byte slice
|
2016-06-07 18:43:28 +03:00
|
|
|
func runeToByteIndex(n int, txt []byte) int {
|
|
|
|
if n == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
i := 0
|
|
|
|
for len(txt) > 0 {
|
|
|
|
_, size := utf8.DecodeRune(txt)
|
|
|
|
|
|
|
|
txt = txt[size:]
|
|
|
|
count += size
|
|
|
|
i++
|
|
|
|
|
|
|
|
if i == n {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2018-01-05 01:03:08 +03:00
|
|
|
// A Line contains the data in bytes as well as a highlight state, match
|
|
|
|
// and a flag for whether the highlighting needs to be updated
|
2017-02-18 23:45:49 +03:00
|
|
|
type Line struct {
|
|
|
|
data []byte
|
|
|
|
|
2017-02-19 19:16:01 +03:00
|
|
|
state highlight.State
|
|
|
|
match highlight.LineMatch
|
|
|
|
rehighlight bool
|
2020-01-29 04:54:14 +03:00
|
|
|
lock sync.Mutex
|
2017-02-18 23:45:49 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
const (
|
|
|
|
// Line ending file formats
|
|
|
|
FFAuto = 0 // Autodetect format
|
|
|
|
FFUnix = 1 // LF line endings (unix style '\n')
|
|
|
|
FFDos = 2 // CRLF line endings (dos style '\r\n')
|
|
|
|
)
|
|
|
|
|
|
|
|
type FileFormat byte
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// A LineArray simply stores and array of lines and makes it easy to insert
|
|
|
|
// and delete in it
|
2016-06-07 18:43:28 +03:00
|
|
|
type LineArray struct {
|
2019-01-15 00:52:25 +03:00
|
|
|
lines []Line
|
2019-01-15 07:24:49 +03:00
|
|
|
Endings FileFormat
|
2019-01-15 00:52:25 +03:00
|
|
|
initsize uint64
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
|
2018-01-05 01:03:08 +03:00
|
|
|
// Append efficiently appends lines together
|
|
|
|
// It allocates an additional 10000 lines if the original estimate
|
|
|
|
// is incorrect
|
2017-04-29 21:12:00 +03:00
|
|
|
func Append(slice []Line, data ...Line) []Line {
|
|
|
|
l := len(slice)
|
|
|
|
if l+len(data) > cap(slice) { // reallocate
|
|
|
|
newSlice := make([]Line, (l+len(data))+10000)
|
|
|
|
copy(newSlice, slice)
|
|
|
|
slice = newSlice
|
|
|
|
}
|
|
|
|
slice = slice[0 : l+len(data)]
|
|
|
|
for i, c := range data {
|
|
|
|
slice[l+i] = c
|
|
|
|
}
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// NewLineArray returns a new line array from an array of bytes
|
2018-08-26 06:06:44 +03:00
|
|
|
func NewLineArray(size uint64, endings FileFormat, reader io.Reader) *LineArray {
|
2016-06-07 18:43:28 +03:00
|
|
|
la := new(LineArray)
|
2016-11-29 21:44:30 +03:00
|
|
|
|
2017-04-29 21:12:00 +03:00
|
|
|
la.lines = make([]Line, 0, 1000)
|
2018-08-26 06:06:44 +03:00
|
|
|
la.initsize = size
|
2017-03-26 22:23:32 +03:00
|
|
|
|
2017-04-29 21:12:00 +03:00
|
|
|
br := bufio.NewReader(reader)
|
|
|
|
var loaded int
|
2017-03-26 22:23:32 +03:00
|
|
|
|
2017-04-29 21:12:00 +03:00
|
|
|
n := 0
|
|
|
|
for {
|
2016-11-29 21:44:30 +03:00
|
|
|
data, err := br.ReadBytes('\n')
|
2018-08-26 06:06:44 +03:00
|
|
|
// Detect the line ending by checking to see if there is a '\r' char
|
|
|
|
// before the '\n'
|
|
|
|
// Even if the file format is set to DOS, the '\r' is removed so
|
|
|
|
// that all lines end with '\n'
|
|
|
|
dlen := len(data)
|
|
|
|
if dlen > 1 && data[dlen-2] == '\r' {
|
|
|
|
data = append(data[:dlen-2], '\n')
|
|
|
|
if endings == FFAuto {
|
2019-01-15 07:24:49 +03:00
|
|
|
la.Endings = FFDos
|
2017-08-24 21:56:46 +03:00
|
|
|
}
|
2019-01-15 07:24:49 +03:00
|
|
|
dlen = len(data)
|
2018-08-26 06:06:44 +03:00
|
|
|
} else if dlen > 0 {
|
|
|
|
if endings == FFAuto {
|
2019-01-15 07:24:49 +03:00
|
|
|
la.Endings = FFUnix
|
2017-08-24 21:56:46 +03:00
|
|
|
}
|
2017-08-24 20:13:14 +03:00
|
|
|
}
|
2017-04-29 21:12:00 +03:00
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// If we are loading a large file (greater than 1000) we use the file
|
|
|
|
// size and the length of the first 1000 lines to try to estimate
|
|
|
|
// how many lines will need to be allocated for the rest of the file
|
|
|
|
// We add an extra 10000 to the original estimate to be safe and give
|
|
|
|
// plenty of room for expansion
|
2017-04-29 21:12:00 +03:00
|
|
|
if n >= 1000 && loaded >= 0 {
|
|
|
|
totalLinesNum := int(float64(size) * (float64(n) / float64(loaded)))
|
|
|
|
newSlice := make([]Line, len(la.lines), totalLinesNum+10000)
|
|
|
|
copy(newSlice, la.lines)
|
|
|
|
la.lines = newSlice
|
|
|
|
loaded = -1
|
|
|
|
}
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// Counter for the number of bytes in the first 1000 lines
|
2017-04-29 21:12:00 +03:00
|
|
|
if loaded >= 0 {
|
2018-08-26 06:06:44 +03:00
|
|
|
loaded += dlen
|
2017-04-29 21:12:00 +03:00
|
|
|
}
|
|
|
|
|
2016-11-29 21:44:30 +03:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
2020-01-29 04:54:14 +03:00
|
|
|
la.lines = Append(la.lines, Line{
|
|
|
|
data: data[:],
|
|
|
|
state: nil,
|
|
|
|
match: nil,
|
|
|
|
rehighlight: false,
|
|
|
|
})
|
2016-11-29 21:44:30 +03:00
|
|
|
}
|
|
|
|
// Last line was read
|
|
|
|
break
|
|
|
|
} else {
|
2020-01-29 04:54:14 +03:00
|
|
|
la.lines = Append(la.lines, Line{
|
|
|
|
data: data[:dlen-1],
|
|
|
|
state: nil,
|
|
|
|
match: nil,
|
|
|
|
rehighlight: false,
|
|
|
|
})
|
2016-11-29 21:44:30 +03:00
|
|
|
}
|
2017-04-29 21:12:00 +03:00
|
|
|
n++
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return la
|
|
|
|
}
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// Bytes returns the string that should be written to disk when
|
2017-08-24 20:13:14 +03:00
|
|
|
// the line array is saved
|
2018-08-26 06:06:44 +03:00
|
|
|
func (la *LineArray) Bytes() []byte {
|
|
|
|
str := make([]byte, 0, la.initsize+1000) // initsize should provide a good estimate
|
2017-08-24 20:13:14 +03:00
|
|
|
for i, l := range la.lines {
|
2018-08-26 06:06:44 +03:00
|
|
|
str = append(str, l.data...)
|
2017-08-24 20:13:14 +03:00
|
|
|
if i != len(la.lines)-1 {
|
2019-01-15 07:24:49 +03:00
|
|
|
if la.Endings == FFDos {
|
2018-08-26 06:06:44 +03:00
|
|
|
str = append(str, '\r')
|
2017-08-24 20:13:14 +03:00
|
|
|
}
|
2018-08-26 06:06:44 +03:00
|
|
|
str = append(str, '\n')
|
2017-08-24 20:13:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// newlineBelow adds a newline below the given line number
|
|
|
|
func (la *LineArray) newlineBelow(y int) {
|
2020-01-29 04:54:14 +03:00
|
|
|
la.lines = append(la.lines, Line{
|
|
|
|
data: []byte{' '},
|
|
|
|
state: nil,
|
|
|
|
match: nil,
|
|
|
|
rehighlight: false,
|
|
|
|
})
|
2016-06-07 18:43:28 +03:00
|
|
|
copy(la.lines[y+2:], la.lines[y+1:])
|
2020-01-29 04:54:14 +03:00
|
|
|
la.lines[y+1] = Line{
|
|
|
|
data: []byte{},
|
|
|
|
state: la.lines[y].state,
|
|
|
|
match: nil,
|
|
|
|
rehighlight: false,
|
|
|
|
}
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// Inserts a byte array at a given location
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) insert(pos Loc, value []byte) {
|
2017-02-18 23:45:49 +03:00
|
|
|
x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
|
2016-06-07 18:43:28 +03:00
|
|
|
for i := 0; i < len(value); i++ {
|
|
|
|
if value[i] == '\n' {
|
2018-08-26 06:06:44 +03:00
|
|
|
la.split(Loc{x, y})
|
2016-06-07 18:43:28 +03:00
|
|
|
x = 0
|
|
|
|
y++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
la.insertByte(Loc{x, y}, value[i])
|
|
|
|
x++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// InsertByte inserts a byte at a given location
|
2016-06-07 18:43:28 +03:00
|
|
|
func (la *LineArray) insertByte(pos Loc, value byte) {
|
2017-02-18 23:45:49 +03:00
|
|
|
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
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// joinLines joins the two lines a and b
|
|
|
|
func (la *LineArray) joinLines(a, b int) {
|
2017-02-18 23:45:49 +03:00
|
|
|
la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
|
2018-08-26 06:06:44 +03:00
|
|
|
la.deleteLine(b)
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// split splits a line at a given position
|
|
|
|
func (la *LineArray) split(pos Loc) {
|
|
|
|
la.newlineBelow(pos.Y)
|
2017-02-18 23:45:49 +03:00
|
|
|
la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
|
|
|
|
la.lines[pos.Y+1].state = la.lines[pos.Y].state
|
2017-02-19 19:16:01 +03:00
|
|
|
la.lines[pos.Y].state = nil
|
|
|
|
la.lines[pos.Y].match = nil
|
|
|
|
la.lines[pos.Y+1].match = nil
|
|
|
|
la.lines[pos.Y].rehighlight = true
|
2018-08-26 06:06:44 +03:00
|
|
|
la.deleteToEnd(Loc{pos.X, pos.Y})
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// removes from start to end
|
2018-08-26 06:06:44 +03:00
|
|
|
func (la *LineArray) remove(start, end Loc) []byte {
|
2020-02-09 22:58:37 +03:00
|
|
|
// sub := la.Substr(start, end)
|
2017-02-18 23:45:49 +03:00
|
|
|
startX := runeToByteIndex(start.X, la.lines[start.Y].data)
|
|
|
|
endX := runeToByteIndex(end.X, la.lines[end.Y].data)
|
2016-06-07 18:43:28 +03:00
|
|
|
if start.Y == end.Y {
|
2017-02-18 23:45:49 +03:00
|
|
|
la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
|
2016-06-07 18:43:28 +03:00
|
|
|
} else {
|
2020-02-09 22:58:37 +03:00
|
|
|
la.deleteLines(start.Y, end.Y-1)
|
2018-08-26 06:06:44 +03:00
|
|
|
la.deleteToEnd(Loc{startX, start.Y})
|
|
|
|
la.deleteFromStart(Loc{endX - 1, start.Y + 1})
|
|
|
|
la.joinLines(start.Y, start.Y+1)
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
2020-02-09 22:58:37 +03:00
|
|
|
return []byte{}
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// deleteToEnd deletes from the end of a line to the position
|
|
|
|
func (la *LineArray) deleteToEnd(pos Loc) {
|
2017-02-18 23:45:49 +03:00
|
|
|
la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// deleteFromStart deletes from the start of a line to the position
|
|
|
|
func (la *LineArray) deleteFromStart(pos Loc) {
|
2017-02-18 23:45:49 +03:00
|
|
|
la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// deleteLine deletes the line number
|
|
|
|
func (la *LineArray) deleteLine(y int) {
|
2016-06-07 18:43:28 +03:00
|
|
|
la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:58:37 +03:00
|
|
|
func (la *LineArray) deleteLines(y1, y2 int) {
|
|
|
|
la.lines = la.lines[:y1+copy(la.lines[y1:], la.lines[y2:])]
|
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// DeleteByte deletes the byte at a position
|
2018-08-26 06:06:44 +03:00
|
|
|
func (la *LineArray) deleteByte(pos Loc) {
|
2017-02-18 23:45:49 +03:00
|
|
|
la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X+copy(la.lines[pos.Y].data[pos.X:], la.lines[pos.Y].data[pos.X+1:])]
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 20:26:05 +03:00
|
|
|
// Substr returns the string representation between two locations
|
2018-08-26 06:06:44 +03:00
|
|
|
func (la *LineArray) Substr(start, end Loc) []byte {
|
2017-02-18 23:45:49 +03:00
|
|
|
startX := runeToByteIndex(start.X, la.lines[start.Y].data)
|
|
|
|
endX := runeToByteIndex(end.X, la.lines[end.Y].data)
|
2016-06-07 18:43:28 +03:00
|
|
|
if start.Y == end.Y {
|
2019-01-03 01:48:50 +03:00
|
|
|
src := la.lines[start.Y].data[startX:endX]
|
|
|
|
dest := make([]byte, len(src))
|
|
|
|
copy(dest, src)
|
|
|
|
return dest
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
2018-08-26 06:06:44 +03:00
|
|
|
str := make([]byte, 0, len(la.lines[start.Y+1].data)*(end.Y-start.Y))
|
|
|
|
str = append(str, la.lines[start.Y].data[startX:]...)
|
|
|
|
str = append(str, '\n')
|
2016-06-07 18:43:28 +03:00
|
|
|
for i := start.Y + 1; i <= end.Y-1; i++ {
|
2018-08-26 06:06:44 +03:00
|
|
|
str = append(str, la.lines[i].data...)
|
|
|
|
str = append(str, '\n')
|
2016-06-07 18:43:28 +03:00
|
|
|
}
|
2018-08-26 06:06:44 +03:00
|
|
|
str = append(str, la.lines[end.Y].data[:endX]...)
|
2016-06-07 18:43:28 +03:00
|
|
|
return str
|
|
|
|
}
|
2017-02-19 05:28:45 +03:00
|
|
|
|
2019-01-14 08:18:49 +03:00
|
|
|
// LinesNum returns the number of lines in the buffer
|
|
|
|
func (la *LineArray) LinesNum() int {
|
|
|
|
return len(la.lines)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start returns the start of the buffer
|
|
|
|
func (la *LineArray) Start() Loc {
|
|
|
|
return Loc{0, 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
// End returns the location of the last character in the buffer
|
|
|
|
func (la *LineArray) End() Loc {
|
|
|
|
numlines := len(la.lines)
|
|
|
|
return Loc{utf8.RuneCount(la.lines[numlines-1].data), numlines - 1}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LineBytes returns line n as an array of bytes
|
|
|
|
func (la *LineArray) LineBytes(n int) []byte {
|
|
|
|
if n >= len(la.lines) || n < 0 {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
return la.lines[n].data
|
|
|
|
}
|
|
|
|
|
2018-01-05 01:03:08 +03:00
|
|
|
// State gets the highlight state for the given line number
|
2017-02-19 05:28:45 +03:00
|
|
|
func (la *LineArray) State(lineN int) highlight.State {
|
2020-01-29 04:54:14 +03:00
|
|
|
la.lines[lineN].lock.Lock()
|
|
|
|
defer la.lines[lineN].lock.Unlock()
|
2017-02-19 05:28:45 +03:00
|
|
|
return la.lines[lineN].state
|
|
|
|
}
|
|
|
|
|
2018-01-05 01:03:08 +03:00
|
|
|
// SetState sets the highlight state at the given line number
|
2017-02-19 05:28:45 +03:00
|
|
|
func (la *LineArray) SetState(lineN int, s highlight.State) {
|
2020-01-29 04:54:14 +03:00
|
|
|
la.lines[lineN].lock.Lock()
|
|
|
|
defer la.lines[lineN].lock.Unlock()
|
2017-02-19 05:28:45 +03:00
|
|
|
la.lines[lineN].state = s
|
|
|
|
}
|
2017-02-19 19:16:01 +03:00
|
|
|
|
2018-01-05 01:03:08 +03:00
|
|
|
// SetMatch sets the match at the given line number
|
2017-02-19 19:16:01 +03:00
|
|
|
func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
|
2020-01-29 04:54:14 +03:00
|
|
|
la.lines[lineN].lock.Lock()
|
|
|
|
defer la.lines[lineN].lock.Unlock()
|
2017-02-19 19:16:01 +03:00
|
|
|
la.lines[lineN].match = m
|
|
|
|
}
|
|
|
|
|
2018-01-05 01:03:08 +03:00
|
|
|
// Match retrieves the match for the given line number
|
2017-02-19 19:16:01 +03:00
|
|
|
func (la *LineArray) Match(lineN int) highlight.LineMatch {
|
2020-01-29 04:54:14 +03:00
|
|
|
la.lines[lineN].lock.Lock()
|
|
|
|
defer la.lines[lineN].lock.Unlock()
|
2017-02-19 19:16:01 +03:00
|
|
|
return la.lines[lineN].match
|
|
|
|
}
|
2018-09-09 00:04:26 +03:00
|
|
|
|
|
|
|
func (la *LineArray) Rehighlight(lineN int) bool {
|
2020-01-29 04:54:14 +03:00
|
|
|
la.lines[lineN].lock.Lock()
|
|
|
|
defer la.lines[lineN].lock.Unlock()
|
2018-09-09 00:04:26 +03:00
|
|
|
return la.lines[lineN].rehighlight
|
|
|
|
}
|
|
|
|
|
|
|
|
func (la *LineArray) SetRehighlight(lineN int, on bool) {
|
2020-01-29 04:54:14 +03:00
|
|
|
la.lines[lineN].lock.Lock()
|
|
|
|
defer la.lines[lineN].lock.Unlock()
|
2018-09-09 00:04:26 +03:00
|
|
|
la.lines[lineN].rehighlight = on
|
|
|
|
}
|