Skip to content

Commit

Permalink
cmd/govim: implement LSP DidSave (#618)
Browse files Browse the repository at this point in the history
Closes #287
  • Loading branch information
myitcv committed Dec 10, 2019
1 parent 8f8ccc4 commit d1c7a12
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cmd/govim/buffer_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,25 @@ func (v *vimstate) deleteCurrentBuffer(args ...json.RawMessage) error {
return nil
}

func (v *vimstate) bufWritePost(args ...json.RawMessage) error {
currBufNr := v.ParseInt(args[0])
cb, ok := v.buffers[currBufNr]
if !ok {
return fmt.Errorf("tried to handle BufWritePost for buffer %v; but we have no record of it", currBufNr)
}
params := &protocol.DidSaveTextDocumentParams{
TextDocument: protocol.VersionedTextDocumentIdentifier{
TextDocumentIdentifier: cb.ToTextDocumentIdentifier(),
Version: float64(cb.Version),
},
Text: string(cb.Contents()),
}
if err := v.server.DidSave(context.Background(), params); err != nil {
return fmt.Errorf("failed to call gopls.DidSave on %v: %v", cb.Name, err)
}
return nil
}

type bufferUpdate struct {
buffer *types.Buffer
wait chan bool
Expand Down
1 change: 1 addition & 0 deletions cmd/govim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func (g *govimplugin) Init(gg govim.Govim, errCh chan error) error {
g.DefineAutoCommand("", govim.Events{govim.EventBufUnload}, govim.Patterns{"*.go"}, false, g.vimstate.bufUnload, "eval(expand('<abuf>'))")
g.DefineAutoCommand("", govim.Events{govim.EventBufRead, govim.EventBufNewFile}, govim.Patterns{"*.go"}, false, g.vimstate.bufReadPost, exprAutocmdCurrBufInfo)
g.DefineAutoCommand("", govim.Events{govim.EventBufWritePre}, govim.Patterns{"*.go"}, false, g.vimstate.formatCurrentBuffer, "eval(expand('<abuf>'))")
g.DefineAutoCommand("", govim.Events{govim.EventBufWritePost}, govim.Patterns{"*.go"}, false, g.vimstate.bufWritePost, "eval(expand('<abuf>'))")
g.DefineFunction(string(config.FunctionComplete), []string{"findarg", "base"}, g.vimstate.complete)
g.DefineCommand(string(config.CommandGoToDef), g.vimstate.gotoDef, govim.NArgsZeroOrOne)
g.DefineCommand(string(config.CommandSuggestedFixes), g.vimstate.suggestFixes, govim.NArgsZeroOrOne)
Expand Down

0 comments on commit d1c7a12

Please sign in to comment.