Fix edit application in formatting

This commit is contained in:
Zachary Yedidia
2020-08-12 16:21:05 -04:00
parent c1621086a2
commit 3c50ac1666
2 changed files with 21 additions and 3 deletions

View File

@@ -1844,9 +1844,7 @@ func (h *BufPane) AutoFormat() bool {
return false
}
for _, e := range edits {
h.Buf.ApplyEdit(e)
}
h.Buf.ApplyEdits(edits)
return true
}

View File

@@ -12,6 +12,7 @@ import (
"path"
gopath "path"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
@@ -517,6 +518,25 @@ func (b *Buffer) ApplyEdit(e lspt.TextEdit) {
}
}
func (b *Buffer) ApplyEdits(edits []lspt.TextEdit) {
deltas := make([]Delta, len(edits))
for i, e := range edits {
deltas[i] = Delta{
Text: []byte(e.NewText),
Start: toLoc(e.Range.Start),
End: toLoc(e.Range.End),
}
}
// Since edit ranges are guaranteed by LSP to never overlap we can sort
// by last edit first and apply each edit in order
// Perhaps in the future we should make this more robust to a non-conforming
// server that sends overlapping ranges
sort.Slice(deltas, func(i, j int) bool {
return deltas[i].Start.GreaterThan(deltas[j].Start)
})
b.MultipleReplace(deltas)
}
// FileType returns the buffer's filetype
func (b *Buffer) FileType() string {
return b.Settings["filetype"].(string)