X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2FlineArray.go;h=248603782d9a7de1c55c2bc91869c16f955abeec;hb=61baa73d70750fc3cb235a2a806a64709605542b;hp=b609837b214f61932eb7a6ceccd1029b288aeacb;hpb=18c419635413bd252608c84905f989541eca6dda;p=micro.git diff --git a/cmd/micro/lineArray.go b/cmd/micro/lineArray.go index b609837b..24860378 100644 --- a/cmd/micro/lineArray.go +++ b/cmd/micro/lineArray.go @@ -5,7 +5,7 @@ import ( "io" "unicode/utf8" - "github.com/zyedidia/highlight" + "github.com/zyedidia/micro/cmd/micro/highlight" ) func runeToByteIndex(n int, txt []byte) int { @@ -29,10 +29,14 @@ func runeToByteIndex(n int, txt []byte) int { return count } +// 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 type Line struct { data []byte - state highlight.State + state highlight.State + match highlight.LineMatch + rehighlight bool } // A LineArray simply stores and array of lines and makes it easy to insert @@ -41,24 +45,71 @@ type LineArray struct { lines []Line } +// 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) + // The copy function is predeclared and works for any slice type. + copy(newSlice, slice) + slice = newSlice + } + slice = slice[0 : l+len(data)] + for i, c := range data { + slice[l+i] = c + } + return slice +} + // NewLineArray returns a new line array from an array of bytes -func NewLineArray(reader io.Reader) *LineArray { +func NewLineArray(size int64, reader io.Reader) *LineArray { la := new(LineArray) + + la.lines = make([]Line, 0, 1000) + br := bufio.NewReader(reader) + var loaded int - i := 0 + n := 0 for { data, err := br.ReadBytes('\n') + if len(data) > 1 && data[len(data)-2] == '\r' { + data = append(data[:len(data)-2], '\n') + if fileformat == 0 { + fileformat = 2 + } + } else if len(data) > 0 { + if fileformat == 0 { + fileformat = 1 + } + } + + 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 + } + + if loaded >= 0 { + loaded += len(data) + } + if err != nil { if err == io.EOF { - la.lines = append(la.lines, Line{data[:len(data)], nil}) + la.lines = Append(la.lines, Line{data[:], nil, nil, false}) + // la.lines = Append(la.lines, Line{data[:len(data)]}) } // Last line was read break } else { - la.lines = append(la.lines, Line{data[:len(data)-1], nil}) + // la.lines = Append(la.lines, Line{data[:len(data)-1]}) + la.lines = Append(la.lines, Line{data[:len(data)-1], nil, nil, false}) } - i++ + n++ } return la @@ -76,11 +127,28 @@ func (la *LineArray) String() string { return str } +// SaveString returns the string that should be written to disk when +// the line array is saved +// It is the same as string but uses crlf or lf line endings depending +func (la *LineArray) SaveString(useCrlf bool) string { + str := "" + for i, l := range la.lines { + str += string(l.data) + if i != len(la.lines)-1 { + if useCrlf { + str += "\r" + } + str += "\n" + } + } + return str +} + // NewlineBelow adds a newline below the given line number func (la *LineArray) NewlineBelow(y int) { - la.lines = append(la.lines, Line{[]byte(" "), nil}) + la.lines = append(la.lines, Line{[]byte{' '}, nil, nil, false}) copy(la.lines[y+2:], la.lines[y+1:]) - la.lines[y+1] = Line{[]byte(""), nil} + la.lines[y+1] = Line{[]byte{}, la.lines[y].state, nil, false} } // inserts a byte array at a given location @@ -117,6 +185,10 @@ func (la *LineArray) Split(pos Loc) { la.NewlineBelow(pos.Y) 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 la.DeleteToEnd(Loc{pos.X, pos.Y}) } @@ -173,3 +245,23 @@ func (la *LineArray) Substr(start, end Loc) string { str += string(la.lines[end.Y].data[:endX]) return str } + +// State gets the highlight state for the given line number +func (la *LineArray) State(lineN int) highlight.State { + return la.lines[lineN].state +} + +// SetState sets the highlight state at the given line number +func (la *LineArray) SetState(lineN int, s highlight.State) { + la.lines[lineN].state = s +} + +// SetMatch sets the match at the given line number +func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) { + la.lines[lineN].match = m +} + +// Match retrieves the match for the given line number +func (la *LineArray) Match(lineN int) highlight.LineMatch { + return la.lines[lineN].match +}