]> git.lizzy.rs Git - micro.git/commitdiff
Minor optimizations
authorZachary Yedidia <zyedidia@gmail.com>
Mon, 29 Jan 2018 21:02:15 +0000 (16:02 -0500)
committerZachary Yedidia <zyedidia@gmail.com>
Mon, 29 Jan 2018 21:02:15 +0000 (16:02 -0500)
cmd/micro/actions.go
cmd/micro/cellview.go
cmd/micro/command.go
cmd/micro/lineArray.go
cmd/micro/util.go

index a9a34aa01a6e458d64aebab47594e4de23bc0d7e..41c89e7103ed6d482462a3e0ca89e0e68d66121b 100644 (file)
@@ -7,6 +7,7 @@ import (
        "strconv"
        "strings"
        "time"
+       "unicode/utf8"
 
        "github.com/yuin/gopher-lua"
        "github.com/zyedidia/clipboard"
@@ -742,9 +743,9 @@ func (v *View) Backspace(usePlugin bool) bool {
                // If the user is using spaces instead of tabs and they are deleting
                // whitespace at the start of the line, we should delete as if it's a
                // tab (tabSize number of spaces)
-               lineStart := v.Buf.Line(v.Cursor.Y)[:v.Cursor.X]
+               lineStart := sliceEnd(v.Buf.LineBytes(v.Cursor.Y), v.Cursor.X)
                tabSize := int(v.Buf.Settings["tabsize"].(float64))
-               if v.Buf.Settings["tabstospaces"].(bool) && IsSpaces(lineStart) && len(lineStart) != 0 && len(lineStart)%tabSize == 0 {
+               if v.Buf.Settings["tabstospaces"].(bool) && IsSpaces(lineStart) && utf8.RuneCount(lineStart) != 0 && utf8.RuneCount(lineStart)%tabSize == 0 {
                        loc := v.Cursor.Loc
                        v.Buf.Remove(loc.Move(-tabSize, v.Buf), loc)
                } else {
index 7a6c13cdf6c1a6ea4dddf6aa43800984c9c2ac43..c33d29257b5856e89f12e1b229e92efdb6f83bb2 100644 (file)
@@ -85,7 +85,7 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) {
        indentrunes := []rune(buf.Settings["indentchar"].(string))
        // if empty indentchar settings, use space
        if indentrunes == nil || len(indentrunes) == 0 {
-               indentrunes = []rune(" ")
+               indentrunes = []rune{' '}
        }
        indentchar := indentrunes[0]
 
index ade999cdddba7f27c46f6a0c43bf8374cf3c60af..ebb7936551d82353829d5c5fdbed19fe892d9510 100644 (file)
@@ -176,7 +176,7 @@ func PluginCmd(args []string) {
                                        continue
                                }
                        }
-                       if !IsSpaces(removed) {
+                       if !IsSpaces([]byte(removed)) {
                                messenger.Message("Removed ", removed)
                        } else {
                                messenger.Error("The requested plugins do not exist")
index 20fde5859dc2d38e0a46a7452bc2d2480ddbf3f9..248603782d9a7de1c55c2bc91869c16f955abeec 100644 (file)
@@ -89,7 +89,6 @@ func NewLineArray(size int64, reader io.Reader) *LineArray {
                if n >= 1000 && loaded >= 0 {
                        totalLinesNum := int(float64(size) * (float64(n) / float64(loaded)))
                        newSlice := make([]Line, len(la.lines), totalLinesNum+10000)
-                       // The copy function is predeclared and works for any slice type.
                        copy(newSlice, la.lines)
                        la.lines = newSlice
                        loaded = -1
@@ -147,9 +146,9 @@ func (la *LineArray) SaveString(useCrlf bool) string {
 
 // NewlineBelow adds a newline below the given line number
 func (la *LineArray) NewlineBelow(y int) {
-       la.lines = append(la.lines, Line{[]byte(" "), nil, nil, false})
+       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(""), la.lines[y].state, nil, false}
+       la.lines[y+1] = Line{[]byte{}, la.lines[y].state, nil, false}
 }
 
 // inserts a byte array at a given location
index 878ddb2517a4dceb7be5503b0dde8a8036d9ef3f..b8a8e92a30e54b2daf13d2dcb7c51c37b6f6251f 100644 (file)
@@ -37,6 +37,40 @@ func toRunes(b []byte) []rune {
        return runes
 }
 
+func sliceStart(slc []byte, index int) []byte {
+       len := len(slc)
+       i := 0
+       totalSize := 0
+       for totalSize < len {
+               if i >= index {
+                       return slc[totalSize:]
+               }
+
+               _, size := utf8.DecodeRune(slc[totalSize:])
+               totalSize += size
+               i++
+       }
+
+       return slc[totalSize:]
+}
+
+func sliceEnd(slc []byte, index int) []byte {
+       len := len(slc)
+       i := 0
+       totalSize := 0
+       for totalSize < len {
+               if i >= index {
+                       return slc[:totalSize]
+               }
+
+               _, size := utf8.DecodeRune(slc[totalSize:])
+               totalSize += size
+               i++
+       }
+
+       return slc[:totalSize]
+}
+
 // NumOccurrences counts the number of occurrences of a byte in a string
 func NumOccurrences(s string, c byte) int {
        var n int
@@ -144,7 +178,7 @@ func GetLeadingWhitespace(str string) string {
 }
 
 // IsSpaces checks if a given string is only spaces
-func IsSpaces(str string) bool {
+func IsSpaces(str []byte) bool {
        for _, c := range str {
                if c != ' ' {
                        return false