Run notifications in background to hide latency

This commit is contained in:
Zachary Yedidia
2020-08-10 22:49:29 -04:00
parent c344f1bfce
commit f0b1158ab6
5 changed files with 34 additions and 23 deletions

View File

@@ -227,7 +227,7 @@ func LSPComplete(b *Buffer) ([]string, []string) {
for i, item := range items {
suggestions[i] = item.Label
if len(item.TextEdit.NewText) > 0 {
if item.TextEdit != nil && len(item.TextEdit.NewText) > 0 {
completions[i] = util.SliceEndStr(item.TextEdit.NewText, c.X-argstart)
} else if len(item.InsertText) > 0 {
completions[i] = util.SliceEndStr(item.InsertText, c.X-argstart)

View File

@@ -443,6 +443,8 @@ func (b *Buffer) Fini() {
if b.Type == BTStdout {
fmt.Fprint(util.Stdout, string(b.Bytes()))
}
b.server.DidClose(b.AbsPath)
}
// GetName returns the name that should be displayed in the statusline

View File

@@ -195,5 +195,8 @@ func (b *Buffer) saveToFile(filename string, withSudo bool) error {
b.AbsPath = absPath
b.isModified = false
b.UpdateRules()
b.server.DidSave(b.AbsPath)
return err
}

View File

@@ -14,7 +14,7 @@ func (s *Server) DidOpen(filename, language, text string, version int) {
TextDocument: doc,
}
s.sendNotification("textDocument/didOpen", params)
go s.sendNotification("textDocument/didOpen", params)
}
func (s *Server) DidSave(filename string) {
@@ -25,7 +25,7 @@ func (s *Server) DidSave(filename string) {
params := lsp.DidSaveTextDocumentParams{
TextDocument: doc,
}
s.sendNotification("textDocument/didSave", params)
go s.sendNotification("textDocument/didSave", params)
}
func (s *Server) DidChange(filename string, version int, changes []lsp.TextDocumentContentChangeEvent) {
@@ -40,7 +40,7 @@ func (s *Server) DidChange(filename string, version int, changes []lsp.TextDocum
TextDocument: doc,
ContentChanges: changes,
}
s.sendNotification("textDocument/didChange", params)
go s.sendNotification("textDocument/didChange", params)
}
func (s *Server) DidClose(filename string) {
@@ -51,5 +51,5 @@ func (s *Server) DidClose(filename string) {
params := lsp.DidCloseTextDocumentParams{
TextDocument: doc,
}
s.sendNotification("textDocument/didClose", params)
go s.sendNotification("textDocument/didClose", params)
}

View File

@@ -90,8 +90,6 @@ func StartServer(l Language) (*Server, error) {
s.language = &l
s.responses = make(map[int]chan []byte)
// activeServers[l.Command] = s
return s, nil
}
@@ -154,29 +152,34 @@ func (s *Server) Initialize(directory string) {
activeServers[s.language.Command+"-"+directory] = s
s.active = true
s.root = directory
go s.receive()
resp, err := s.sendRequest("initialize", params)
if err != nil {
log.Println("[micro-lsp]", err)
return
}
s.lock.Lock()
go func() {
resp, err := s.sendRequest("initialize", params)
if err != nil {
log.Println("[micro-lsp]", err)
s.active = false
s.lock.Unlock()
return
}
// todo parse capabilities
log.Println("[micro-lsp] <<<", string(resp))
// todo parse capabilities
log.Println("[micro-lsp] <<<", string(resp))
var r RPCInit
json.Unmarshal(resp, &r)
var r RPCInit
json.Unmarshal(resp, &r)
err = s.sendNotification("initialized", struct{}{})
if err != nil {
log.Println("[micro-lsp]", err)
return
}
s.lock.Unlock()
err = s.sendNotification("initialized", struct{}{})
if err != nil {
log.Println("[micro-lsp]", err)
}
s.capabilities = r.Result.Capabilities
s.root = directory
s.capabilities = r.Result.Capabilities
}()
}
func (s *Server) receive() {
@@ -203,6 +206,7 @@ func (s *Server) receive() {
case "":
// Response
if _, ok := s.responses[r.ID]; ok {
log.Println("[micro-lsp] Got response for", r.ID)
s.responses[r.ID] <- resp
}
}
@@ -251,6 +255,8 @@ func (s *Server) sendNotification(method string, params interface{}) error {
Params: params,
}
s.lock.Lock()
defer s.lock.Unlock()
return s.sendMessage(m)
}