micro/internal/buffer/line_array.go

452 lines
12 KiB
Go
Raw Normal View History

2018-08-27 22:53:10 +03:00
package buffer
2016-06-07 18:43:28 +03:00
import (
"bufio"
2020-02-27 19:27:00 +03:00
"bytes"
"io"
2020-01-29 04:54:14 +03:00
"sync"
2017-02-18 23:45:49 +03:00
2020-05-20 23:43:12 +03:00
"github.com/zyedidia/micro/v2/internal/util"
2020-05-20 23:47:08 +03:00
"github.com/zyedidia/micro/v2/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 {
2020-05-20 23:43:12 +03:00
_, _, size := util.DecodeCharacter(txt)
2016-06-07 18:43:28 +03:00
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
useRegex bool
ignorecase bool
match [][2]int
done bool
}
// 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
state highlight.State
match highlight.LineMatch
rehighlight bool
2020-01-29 04:54:14 +03:00
lock sync.Mutex
// The search states for the line, used for highlighting of search matches,
// separately from the syntax highlighting.
// A map is used because the line array may be shared between multiple buffers
// (multiple instances of the same file opened in different edit panes)
// which have distinct searches, so in the general case there are multiple
// searches per a line, one search per a Buffer containing this line.
search map[*Buffer]*searchState
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
}
// Append efficiently appends lines together
// It allocates an additional 10000 lines if the original estimate
// is incorrect
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)
la.lines = make([]Line, 0, 1000)
2018-08-26 06:06:44 +03:00
la.initsize = size
br := bufio.NewReader(reader)
var loaded int
la.Endings = endings
n := 0
for {
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 la.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 la.Endings == FFAuto {
2019-01-15 07:24:49 +03:00
la.Endings = FFUnix
2017-08-24 21:56:46 +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
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
if loaded >= 0 {
2018-08-26 06:06:44 +03:00
loaded += dlen
}
if err != nil {
if err == io.EOF {
2020-01-29 04:54:14 +03:00
la.lines = Append(la.lines, Line{
data: data,
2020-01-29 04:54:14 +03:00
state: nil,
match: nil,
rehighlight: false,
})
}
// 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,
})
}
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
// the line array is saved
2018-08-26 06:06:44 +03:00
func (la *LineArray) Bytes() []byte {
2020-02-27 19:27:00 +03:00
b := new(bytes.Buffer)
// initsize should provide a good estimate
b.Grow(int(la.initsize + 4096))
for i, l := range la.lines {
2020-02-27 19:27:00 +03:00
b.Write(l.data)
if i != len(la.lines)-1 {
2019-01-15 07:24:49 +03:00
if la.Endings == FFDos {
2020-02-27 19:27:00 +03:00
b.WriteByte('\r')
}
2020-02-27 19:27:00 +03:00
b.WriteByte('\n')
}
}
2020-02-27 19:27:00 +03:00
return b.Bytes()
}
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' || (value[i] == '\r' && i < len(value)-1 && value[i+1] == '\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++
if value[i] == '\r' {
i++
}
2016-06-07 18:43:28 +03:00
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
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 {
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 {
la.deleteLines(start.Y+1, 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
}
return sub
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+1:])]
2020-02-09 22:58:37 +03:00
}
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
}
// 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)
2020-05-20 23:47:08 +03:00
return Loc{util.CharacterCount(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
}
// State gets the highlight state for the given line number
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()
return la.lines[lineN].state
}
// SetState sets the highlight state at the given line number
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()
la.lines[lineN].state = s
}
// SetMatch sets the match at the given line number
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()
la.lines[lineN].match = m
}
// Match retrieves the match for the given line number
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()
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
}
// SearchMatch returns true if the location `pos` is within a match
// of the last search for the buffer `b`.
// It is used for efficient highlighting of search matches (separately
// from the syntax highlighting).
// SearchMatch searches for the matches if it is called first time
// for the given line or if the line was modified. Otherwise the
// previously found matches are used.
//
// The buffer `b` needs to be passed because the line array may be shared
// between multiple buffers (multiple instances of the same file opened
// in different edit panes) which have distinct searches, so SearchMatch
// needs to know which search to match against.
func (la *LineArray) SearchMatch(b *Buffer, pos Loc) bool {
if b.LastSearch == "" {
return false
}
lineN := pos.Y
if la.lines[lineN].search == nil {
la.lines[lineN].search = make(map[*Buffer]*searchState)
}
s, ok := la.lines[lineN].search[b]
if !ok {
// Note: here is a small harmless leak: when the buffer `b` is closed,
// `s` is not deleted from the map. It means that the buffer
// will not be garbage-collected until the line array is garbage-collected,
// i.e. until all the buffers sharing this file are closed.
s = new(searchState)
la.lines[lineN].search[b] = s
}
if !ok || s.search != b.LastSearch || s.useRegex != b.LastSearchRegex ||
s.ignorecase != b.Settings["ignorecase"].(bool) {
s.search = b.LastSearch
s.useRegex = b.LastSearchRegex
s.ignorecase = b.Settings["ignorecase"].(bool)
s.done = false
}
if !s.done {
s.match = nil
start := Loc{0, lineN}
end := Loc{util.CharacterCount(la.lines[lineN].data), lineN}
for start.X < end.X {
m, found, _ := b.FindNext(b.LastSearch, start, end, start, true, b.LastSearchRegex)
if !found {
break
}
s.match = append(s.match, [2]int{m[0].X, m[1].X})
start.X = m[1].X
if m[1].X == m[0].X {
start.X = m[1].X + 1
}
}
s.done = true
}
for _, m := range s.match {
if pos.X >= m[0] && pos.X < m[1] {
return true
}
}
return false
}
// invalidateSearchMatches marks search matches for the given line as outdated.
// It is called when the line is modified.
func (la *LineArray) invalidateSearchMatches(lineN int) {
if la.lines[lineN].search != nil {
for _, s := range la.lines[lineN].search {
s.done = false
}
}
}