micro/cmd/micro/lineArray.go

226 lines
5.3 KiB
Go
Raw Normal View History

2016-06-07 18:43:28 +03:00
package main
import (
"bufio"
"bytes"
"io"
2016-06-07 18:43:28 +03:00
"unicode/utf8"
2017-02-18 23:45:49 +03:00
"github.com/zyedidia/micro/cmd/micro/highlight"
2016-06-07 18:43:28 +03:00
)
func lineCounter(r io.Reader) (int, error) {
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
for {
c, err := r.Read(buf)
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
return count, nil
case err != nil:
return count, err
}
}
}
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
}
2017-02-18 23:45:49 +03:00
type Line struct {
data []byte
state highlight.State
match highlight.LineMatch
rehighlight bool
2017-02-18 23:45:49 +03:00
}
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 {
2017-02-18 23:45:49 +03:00
lines []Line
2016-06-07 18:43:28 +03:00
}
2016-07-10 20:26:05 +03:00
// NewLineArray returns a new line array from an array of bytes
func NewLineArray(reader io.Reader) *LineArray {
2016-06-07 18:43:28 +03:00
la := new(LineArray)
var buf bytes.Buffer
tee := io.TeeReader(reader, &buf)
numlines, _ := lineCounter(tee)
2017-03-29 19:01:01 +03:00
numlines++
2017-03-26 22:27:53 +03:00
la.lines = make([]Line, numlines)
br := bufio.NewReader(&buf)
2017-03-29 19:01:01 +03:00
for i := 0; i < numlines; i++ {
data, err := br.ReadBytes('\n')
if err != nil {
if err == io.EOF {
// la.lines[i] = Line{data[:len(data)], nil, nil, false}
2017-03-29 19:01:01 +03:00
la.lines[i].data = data
}
// Last line was read
break
} else {
la.lines[i].data = data[:len(data)-1]
// la.lines[i] = Line{data[:len(data)-1], nil, nil, false}
}
2016-06-07 18:43:28 +03:00
}
return la
}
2016-07-10 20:26:05 +03:00
// Returns the String representation of the LineArray
2016-06-07 18:43:28 +03:00
func (la *LineArray) String() string {
2017-02-18 23:45:49 +03:00
str := ""
for i, l := range la.lines {
str += string(l.data)
if i != len(la.lines)-1 {
str += "\n"
}
}
return str
2016-06-07 18:43:28 +03:00
}
2016-07-10 20:26:05 +03:00
// NewlineBelow adds a newline below the given line number
2016-06-07 18:43:28 +03:00
func (la *LineArray) NewlineBelow(y int) {
la.lines = append(la.lines, Line{[]byte(" "), nil, nil, false})
2016-06-07 18:43:28 +03:00
copy(la.lines[y+2:], la.lines[y+1:])
2017-02-19 19:35:51 +03:00
la.lines[y+1] = Line{[]byte(""), la.lines[y].state, nil, false}
2016-06-07 18:43:28 +03:00
}
2016-07-10 20:26:05 +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
// x, y := pos.x, pos.y
for i := 0; i < len(value); i++ {
if value[i] == '\n' {
la.Split(Loc{x, y})
x = 0
y++
continue
}
la.insertByte(Loc{x, y}, value[i])
x++
}
}
2016-07-10 20:26:05 +03:00
// 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
}
2016-07-10 20:26:05 +03:00
// JoinLines joins the two lines a and b
2016-06-07 18:43:28 +03:00
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)
2016-06-07 18:43:28 +03:00
la.DeleteLine(b)
}
2016-07-10 20:26:05 +03:00
// Split splits a line at a given position
2016-06-07 18:43:28 +03:00
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
2016-06-07 18:43:28 +03:00
la.DeleteToEnd(Loc{pos.X, pos.Y})
}
2016-07-10 20:26:05 +03:00
// removes from start to end
2016-06-07 18:43:28 +03:00
func (la *LineArray) remove(start, end Loc) string {
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 {
for i := start.Y + 1; i <= end.Y-1; i++ {
la.DeleteLine(start.Y + 1)
2016-06-07 18:43:28 +03:00
}
la.DeleteToEnd(Loc{startX, start.Y})
la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
la.JoinLines(start.Y, start.Y+1)
}
return sub
}
2016-07-10 20:26:05 +03:00
// DeleteToEnd deletes from the end of a line to the position
2016-06-07 18:43:28 +03:00
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
}
2016-07-10 20:26:05 +03:00
// DeleteFromStart deletes from the start of a line to the position
2016-06-07 18:43:28 +03:00
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
}
2016-07-10 20:26:05 +03:00
// DeleteLine deletes the line number
2016-06-07 18:43:28 +03:00
func (la *LineArray) DeleteLine(y int) {
la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
}
2016-07-10 20:26:05 +03:00
// DeleteByte deletes the byte at a position
2016-06-07 18:43:28 +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
2016-06-07 18:43:28 +03:00
func (la *LineArray) Substr(start, end Loc) string {
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
return string(la.lines[start.Y].data[startX:endX])
2016-06-07 18:43:28 +03:00
}
var str string
2017-02-18 23:45:49 +03:00
str += string(la.lines[start.Y].data[startX:]) + "\n"
2016-06-07 18:43:28 +03:00
for i := start.Y + 1; i <= end.Y-1; i++ {
2017-02-18 23:45:49 +03:00
str += string(la.lines[i].data) + "\n"
2016-06-07 18:43:28 +03:00
}
2017-02-18 23:45:49 +03:00
str += string(la.lines[end.Y].data[:endX])
2016-06-07 18:43:28 +03:00
return str
}
func (la *LineArray) State(lineN int) highlight.State {
return la.lines[lineN].state
}
func (la *LineArray) SetState(lineN int, s highlight.State) {
la.lines[lineN].state = s
}
func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
la.lines[lineN].match = m
}
func (la *LineArray) Match(lineN int) highlight.LineMatch {
return la.lines[lineN].match
}