Basic non-compliant autocompletion via LSP

This commit is contained in:
Zachary Yedidia
2020-08-10 18:19:13 -04:00
parent f6ba76424a
commit 053134af1c
5 changed files with 193 additions and 58 deletions

View File

@@ -7,6 +7,7 @@ import (
"sort"
"strings"
lspt "github.com/sourcegraph/go-lsp"
"github.com/zyedidia/micro/v2/internal/util"
)
@@ -203,3 +204,34 @@ func BufferComplete(b *Buffer) ([]string, []string) {
return completions, suggestions
}
func LSPComplete(b *Buffer) ([]string, []string) {
c := b.GetActiveCursor()
_, argstart := GetWord(b)
if argstart == -1 {
return []string{}, []string{}
}
pos := lspt.Position{
Line: c.Y,
Character: c.X,
}
items, err := b.server.Completion(b.AbsPath, pos)
if err != nil {
return []string{}, []string{}
}
suggestions := make([]string, len(items))
for i, item := range items {
suggestions[i] = item.Label
}
completions := make([]string, len(suggestions))
for i := range suggestions {
completions[i] = util.SliceEndStr(suggestions[i], c.X-argstart)
}
return completions, suggestions
}

View File

@@ -138,7 +138,9 @@ func (b *SharedBuffer) insert(pos Loc, value []byte) {
inslines := bytes.Count(value, []byte{'\n'})
b.MarkModified(pos.Y, pos.Y+inslines)
b.lspDidChange(pos, pos.MoveLA(util.CharacterCount(value), b.LineArray), string(value))
p := pos
p.X += util.CharacterCount(value)
b.lspDidChange(pos, p, string(value))
}
func (b *SharedBuffer) remove(start, end Loc) []byte {
b.isModified = true
@@ -405,7 +407,11 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
if ok && l.Installed() {
b.server, _ = lsp.StartServer(l)
b.server.Initialize(gopath.Dir(b.AbsPath))
b.server.DidOpen(b.AbsPath, ft, string(b.Bytes()), b.version)
bytes := b.Bytes()
if len(bytes) == 0 {
bytes = []byte{'\n'}
}
b.server.DidOpen(b.AbsPath, ft, string(bytes), b.version)
}
}
}