mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-17 22:37:10 +09:00
Handle initialization and didOpen properly
This commit is contained in:
@@ -166,7 +166,14 @@ func (b *SharedBuffer) lspDidChange(start, end Loc, text string) {
|
||||
Text: text,
|
||||
}
|
||||
|
||||
b.server.DidChange(b.AbsPath, b.version, []lspt.TextDocumentContentChangeEvent{change})
|
||||
if b.HasLSP() {
|
||||
b.server.DidChange(b.AbsPath, b.version, []lspt.TextDocumentContentChangeEvent{change})
|
||||
}
|
||||
}
|
||||
|
||||
// HasLSP returns whether this buffer is communicating with an LSP server
|
||||
func (b *SharedBuffer) HasLSP() bool {
|
||||
return b.server != nil && b.server.Active
|
||||
}
|
||||
|
||||
// MarkModified marks the buffer as modified for this frame
|
||||
@@ -400,23 +407,37 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
|
||||
|
||||
if !found {
|
||||
if btype == BTDefault {
|
||||
ft := b.Settings["filetype"].(string)
|
||||
l, ok := lsp.GetLanguage(ft)
|
||||
if ok && l.Installed() {
|
||||
b.server, _ = lsp.StartServer(l)
|
||||
b.server.Initialize(gopath.Dir(b.AbsPath))
|
||||
bytes := b.Bytes()
|
||||
if len(bytes) == 0 {
|
||||
bytes = []byte{'\n'}
|
||||
}
|
||||
b.server.DidOpen(b.AbsPath, ft, string(bytes), b.version)
|
||||
}
|
||||
b.lspInit()
|
||||
}
|
||||
}
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
// initializes an LSP server if possible, or calls didOpen on an existing
|
||||
// LSP server in this workspace
|
||||
func (b *Buffer) lspInit() {
|
||||
ft := b.Settings["filetype"].(string)
|
||||
l, ok := lsp.GetLanguage(ft)
|
||||
if ok && l.Installed() {
|
||||
b.server = lsp.GetServer(l, gopath.Dir(b.AbsPath))
|
||||
if b.server == nil {
|
||||
var err error
|
||||
b.server, err = lsp.StartServer(l)
|
||||
if err == nil {
|
||||
b.server.Initialize(gopath.Dir(b.AbsPath))
|
||||
}
|
||||
}
|
||||
if b.HasLSP() {
|
||||
bytes := b.Bytes()
|
||||
if len(bytes) == 0 {
|
||||
bytes = []byte{'\n'}
|
||||
}
|
||||
b.server.DidOpen(b.AbsPath, ft, string(bytes), b.version)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close removes this buffer from the list of open buffers
|
||||
func (b *Buffer) Close() {
|
||||
for i, buf := range OpenBuffers {
|
||||
@@ -442,7 +463,9 @@ func (b *Buffer) Fini() {
|
||||
fmt.Fprint(util.Stdout, string(b.Bytes()))
|
||||
}
|
||||
|
||||
b.server.DidClose(b.AbsPath)
|
||||
if b.HasLSP() {
|
||||
b.server.DidClose(b.AbsPath)
|
||||
}
|
||||
}
|
||||
|
||||
// GetName returns the name that should be displayed in the statusline
|
||||
|
||||
@@ -196,7 +196,9 @@ func (b *Buffer) saveToFile(filename string, withSudo bool) error {
|
||||
b.isModified = false
|
||||
b.UpdateRules()
|
||||
|
||||
b.server.DidSave(b.AbsPath)
|
||||
if b.HasLSP() {
|
||||
b.server.DidSave(b.AbsPath)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user