mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-18 06:47:14 +09:00
Basic non-compliant autocompletion via LSP
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user