]> git.lizzy.rs Git - micro.git/commitdiff
Fix buffer.RuneAt (#1895)
authorDmitry Maluka <dmitrymaluka@gmail.com>
Tue, 20 Oct 2020 00:36:14 +0000 (02:36 +0200)
committerGitHub <noreply@github.com>
Tue, 20 Oct 2020 00:36:14 +0000 (20:36 -0400)
Fix buffer.RuneAt returning the rune not at the given location (as the
documentation claims) but just before it.

internal/buffer/autocomplete.go
internal/buffer/buffer.go
internal/buffer/save.go

index b1c061cdd0f3388bbbc98b2bfbb24a85038c100d..414e30601285d9825f3afa7539eff13b0b139fa2 100644 (file)
@@ -69,11 +69,11 @@ func GetWord(b *Buffer) ([]byte, int) {
        l := b.LineBytes(c.Y)
        l = util.SliceStart(l, c.X)
 
-       if c.X == 0 || util.IsWhitespace(b.RuneAt(c.Loc)) {
+       if c.X == 0 || util.IsWhitespace(b.RuneAt(c.Loc.Move(-1, b))) {
                return []byte{}, -1
        }
 
-       if util.IsNonAlphaNumeric(b.RuneAt(c.Loc)) {
+       if util.IsNonAlphaNumeric(b.RuneAt(c.Loc.Move(-1, b))) {
                return []byte{}, c.X
        }
 
index 43be4a81ef689292930b02721edf9d07e147b1b4..387b041ca769f80bda406dd03cbc668ccda95179 100644 (file)
@@ -511,11 +511,12 @@ func (b *Buffer) RuneAt(loc Loc) rune {
                for len(line) > 0 {
                        r, _, size := util.DecodeCharacter(line)
                        line = line[size:]
-                       i++
 
                        if i == loc.X {
                                return r
                        }
+
+                       i++
                }
        }
        return '\n'
index d037fb9250c472c69497de51e294ec5274483e82..e912055dd1cbe246f70491d578abb34d6ea6d29c 100644 (file)
@@ -109,7 +109,7 @@ func (b *Buffer) saveToFile(filename string, withSudo bool) error {
 
        if b.Settings["eofnewline"].(bool) {
                end := b.End()
-               if b.RuneAt(Loc{end.X, end.Y}) != '\n' {
+               if b.RuneAt(Loc{end.X - 1, end.Y}) != '\n' {
                        b.insert(end, []byte{'\n'})
                }
        }