Skip to content

Commit 686167f

Browse files
authored
Merge pull request #2210 from bhcleek/lsp/synchronization
synchronize with gopls on certain events
2 parents f040988 + c2fc185 commit 686167f

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

autoload/go/lsp.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ function! go#lsp#DidOpen(fname)
368368
return
369369
endif
370370

371+
if !filereadable(a:fname)
372+
return
373+
endif
374+
371375
let l:lsp = s:lspfactory.get()
372376
let l:msg = go#lsp#message#DidOpen(fnamemodify(a:fname, ':p'), join(go#util#GetLines(), "\n") . "\n")
373377
let l:state = s:newHandlerState('')
@@ -382,6 +386,10 @@ function! go#lsp#DidChange(fname)
382386
return go#lsp#DidOpen(a:fname)
383387
endif
384388

389+
if !filereadable(a:fname)
390+
return
391+
endif
392+
385393
let l:lsp = s:lspfactory.get()
386394
let l:msg = go#lsp#message#DidChange(fnamemodify(a:fname, ':p'), join(go#util#GetLines(), "\n") . "\n")
387395
let l:state = s:newHandlerState('')
@@ -390,6 +398,10 @@ function! go#lsp#DidChange(fname)
390398
endfunction
391399

392400
function! go#lsp#DidClose(fname)
401+
if !filereadable(a:fname)
402+
return
403+
endif
404+
393405
if !get(b:, 'go_lsp_did_open', 0)
394406
return
395407
endif

ftplugin/go.vim

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,16 @@ endif
8282
augroup vim-go-buffer
8383
autocmd! * <buffer>
8484

85-
" TODO(bc): notify gopls about changes on CursorHold when the buffer is
86-
" modified.
87-
" TODO(bc): notify gopls that the file on disk is correct on BufWritePost
85+
" The file is registered (textDocument/DidOpen) with gopls in in
86+
" plugin/go.vim on the FileType event.
87+
" TODO(bc): handle all the other events that may be of interest to gopls,
88+
" too (e.g. BufFilePost , CursorHold , CursorHoldI, FileReadPost,
89+
" StdinReadPre, BufWritePost, TextChange, TextChangedI)
90+
if go#util#has_job()
91+
autocmd BufWritePost <buffer> call go#lsp#DidChange(expand('<afile>:p'))
92+
autocmd FileChangedShell <buffer> call go#lsp#DidChange(expand('<afile>:p'))
93+
autocmd BufDelete <buffer> call go#lsp#DidClose(expand('<afile>:p'))
94+
endif
8895

8996
autocmd CursorHold <buffer> call go#auto#auto_type_info()
9097
autocmd CursorHold <buffer> call go#auto#auto_sameids()

plugin/go.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ function! s:gofiletype_post()
234234
let &g:fileencodings = s:current_fileencodings
235235
endfunction
236236

237+
function! s:register()
238+
if !(&modifiable && expand('<amatch>') ==# 'go')
239+
return
240+
endif
241+
242+
call go#lsp#DidOpen(expand('<afile>:p'))
243+
endfunction
244+
237245
augroup vim-go
238246
autocmd!
239247

@@ -245,6 +253,10 @@ augroup vim-go
245253
autocmd BufNewFile *.s if &modifiable | setlocal fileencoding=utf-8 fileformat=unix | endif
246254
autocmd BufRead *.s call s:gofiletype_pre()
247255
autocmd BufReadPost *.s call s:gofiletype_post()
256+
257+
if go#util#has_job()
258+
autocmd FileType * call s:register()
259+
endif
248260
augroup end
249261

250262
" restore Vi compatibility settings

0 commit comments

Comments
 (0)