mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-17 06:17:12 +09:00
Fix edit application in formatting
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user