Skip to content

Commit

Permalink
Merge pull request #2429 from bhcleek/lsp/build-tags
Browse files Browse the repository at this point in the history
lsp: support build tags
  • Loading branch information
bhcleek committed Jul 29, 2019
2 parents 09247ac + 28a3099 commit 3a373cf
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 25 deletions.
2 changes: 2 additions & 0 deletions autoload/go/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ endfunction
function! go#config#SetBuildTags(value) abort
if a:value is ''
silent! unlet g:go_build_tags
call go#lsp#ResetWorkspaceDirectories()
return
endif

let g:go_build_tags = a:value
call go#lsp#ResetWorkspaceDirectories()
endfunction

function! go#config#TestTimeout() abort
Expand Down
16 changes: 8 additions & 8 deletions autoload/go/def_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func! Test_DefJump_gopls_simple_first() abort
try
let g:go_def_mode = 'gopls'

let l:tmp = gotest#write_file('simple/firstposition/position.go', [
\ 'package position',
let l:tmp = gotest#write_file('simple/firstposition/firstposition.go', [
\ 'package firstposition',
\ '',
\ 'func Example() {',
\ "\tid := " . '"foo"',
Expand Down Expand Up @@ -120,8 +120,8 @@ func! Test_DefJump_gopls_simple_last() abort
try
let g:go_def_mode = 'gopls'

let l:tmp = gotest#write_file('simple/lastposition/position.go', [
\ 'package position',
let l:tmp = gotest#write_file('simple/lastposition/lastposition.go', [
\ 'package lastposition',
\ '',
\ 'func Example() {',
\ "\tid := " . '"foo"',
Expand Down Expand Up @@ -155,8 +155,8 @@ func! Test_DefJump_gopls_MultipleCodeUnit_first() abort
try
let g:go_def_mode = 'gopls'

let l:tmp = gotest#write_file('multiplecodeunit/firstposition/position.go', [
\ 'package position',
let l:tmp = gotest#write_file('multiplecodeunit/firstposition/firstposition.go', [
\ 'package firstposition',
\ '',
\ 'func Example() {',
\ "\t𐐀, id := " . '"foo", "bar"',
Expand Down Expand Up @@ -190,8 +190,8 @@ func! Test_DefJump_gopls_MultipleCodeUnit_last() abort
try
let g:go_def_mode = 'gopls'

let l:tmp = gotest#write_file('multiplecodeunit/lastposition/position.go', [
\ 'package position',
let l:tmp = gotest#write_file('multiplecodeunit/lastposition/lastposition.go', [
\ 'package lastposition',
\ '',
\ 'func Example() {',
\ "\t𐐀, id := " . '"foo", "bar"',
Expand Down
57 changes: 53 additions & 4 deletions autoload/go/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,14 @@ function! s:newlsp() abort

function! l:lsp.handleRequest(req) dict abort
if a:req.method == 'workspace/workspaceFolders'
let l:resp = go#lsp#message#workspaceFolders(self.workspaceDirectories)
let l:resp = go#lsp#message#WorkspaceFoldersResult(self.workspaceDirectories)
elseif a:req.method == 'workspace/configuration' && has_key(a:req, 'params') && has_key(a:req.params, 'items')
let l:resp = go#lsp#message#ConfigurationResult(a:req.params.items)
else
return
endif

let l:msg = self.newResponse(l:resp)
let l:msg = self.newResponse(a:req.id, l:resp)
call self.write(l:msg)
endfunction

Expand Down Expand Up @@ -281,6 +285,8 @@ function! s:newlsp() abort
\ 'id': a:id,
\ 'result': a:result,
\ }

return l:msg
endfunction

function! l:lsp.write(msg) dict abort
Expand Down Expand Up @@ -683,11 +689,13 @@ function! s:infoFromHoverContent(content) abort
return l:content
endfunction

function! go#lsp#AddWorkspace(...) abort
function! go#lsp#AddWorkspaceDirectory(...) abort
if a:0 == 0
return
endif

call go#lsp#CleanWorkspaces()

let l:workspaces = []
for l:dir in a:000
let l:dir = fnamemodify(l:dir, ':p')
Expand All @@ -702,7 +710,48 @@ function! go#lsp#AddWorkspace(...) abort
let l:state = s:newHandlerState('')
let l:state.handleResult = funcref('s:noop')
let l:lsp.workspaceDirectories = extend(l:lsp.workspaceDirectories, l:workspaces)
let l:msg = go#lsp#message#AddWorkspaces(l:workspaces)
let l:msg = go#lsp#message#ChangeWorkspaceFolders(l:workspaces, [])
call l:lsp.sendMessage(l:msg, l:state)

return 0
endfunction

function! go#lsp#CleanWorkspaces() abort
let l:workspaces = []

let l:lsp = s:lspfactory.get()

let l:i = 0
let l:missing = []
for l:dir in l:lsp.workspaceDirectories
if !isdirectory(l:dir)
let l:dir = add(l:missing, l:dir)
call remove(l:lsp.workspaceDirectories, l:i)
continue
endif
let l:i += 1
endfor

let l:state = s:newHandlerState('')
let l:state.handleResult = funcref('s:noop')
let l:msg = go#lsp#message#ChangeWorkspaceFolders([], l:missing)
call l:lsp.sendMessage(l:msg, l:state)

return 0
endfunction

" go#lsp#ResetWorkspaceDiretories removes and then re-adds all workspace
" folders to cause gopls to send configuration requests for all of them again.
" This is useful, for instance, when build tags have been added and gopls
" needs to use them.
function! go#lsp#ResetWorkspaceDirectories() abort
call go#lsp#CleanWorkspaces()

let l:lsp = s:lspfactory.get()

let l:state = s:newHandlerState('')
let l:state.handleResult = funcref('s:noop')
let l:msg = go#lsp#message#ChangeWorkspaceFolders(l:lsp.workspaceDirectories, l:lsp.workspaceDirectories)
call l:lsp.sendMessage(l:msg, l:state)

return 0
Expand Down
39 changes: 29 additions & 10 deletions autoload/go/lsp/message.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function! go#lsp#message#Initialize(wd) abort
\ 'capabilities': {
\ 'workspace': {
\ 'workspaceFolders': v:true,
\ 'configuration': v:true,
\ },
\ 'textDocument': {
\ 'hover': {
Expand All @@ -31,12 +32,8 @@ function! go#lsp#message#Initialized() abort
\ }
endfunction

function! go#lsp#message#workspaceFolders(dirs) abort
return map(copy(a:dirs), function('s:workspaceFolderToURI', []))
endfunction

function s:workspaceFolderToURI(key, val) abort
return go#path#ToURI(a:val)
function! go#lsp#message#WorkspaceFoldersResult(dirs) abort
return map(copy(a:dirs), function('s:workspaceFolder', []))
endfunction

function! go#lsp#message#Definition(file, line, col) abort
Expand Down Expand Up @@ -134,22 +131,44 @@ function! go#lsp#message#Hover(file, line, col) abort
\ }
endfunction

function! go#lsp#message#AddWorkspaces(dirs) abort
let l:dirs = map(copy(a:dirs), function('s:workspaceFolderToAddURI', []))
function! go#lsp#message#ChangeWorkspaceFolders(add, remove) abort
let l:addDirs = map(copy(a:add), function('s:workspaceFolder', []))
let l:removeDirs = map(copy(a:add), function('s:workspaceFolder', []))

return {
\ 'notification': 1,
\ 'method': 'workspace/didChangeWorkspaceFolders',
\ 'params': {
\ 'event': {
\ 'added': l:dirs,
\ 'removed': l:removeDirs,
\ 'added': l:addDirs,
\ },
\ }
\ }

endfunction

function s:workspaceFolderToAddURI(key, val) abort
function! go#lsp#message#ConfigurationResult(items) abort
let l:result = []

" results must be in the same order as the items
for l:item in a:items
let l:config = {
\ 'buildFlags': [],
\ 'hoverKind': 'NoDocumentation',
\ }
let l:buildtags = go#config#BuildTags()
if buildtags isnot ''
let l:config.buildFlags = extend(l:config.buildFlags, ['-tags', go#config#BuildTags()])
endif

let l:result = add(l:result, l:config)
endfor

return l:result
endfunction

function s:workspaceFolder(key, val) abort
return {'uri': go#path#ToURI(a:val), 'name': a:val}
endfunction

Expand Down
10 changes: 8 additions & 2 deletions autoload/gotest.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ set cpo&vim
" The full path to the created directory is returned, it is the caller's
" responsibility to clean that up!
fun! gotest#write_file(path, contents) abort
if go#util#has_job()
call go#lsp#CleanWorkspaces()
endif
let l:dir = go#util#tempdir("vim-go-test/testrun/")
let $GOPATH .= ':' . l:dir
let l:full_path = l:dir . '/src/' . a:path
Expand All @@ -21,7 +24,7 @@ fun! gotest#write_file(path, contents) abort
exe 'cd ' . l:dir . '/src'

if go#util#has_job()
call go#lsp#AddWorkspace(fnamemodify(l:full_path, ':p:h'))
call go#lsp#AddWorkspaceDirectory(fnamemodify(l:full_path, ':p:h'))
endif

silent exe 'e! ' . a:path
Expand Down Expand Up @@ -50,6 +53,9 @@ endfun
" The file will be copied to a new GOPATH-compliant temporary directory and
" loaded as the current buffer.
fun! gotest#load_fixture(path) abort
if go#util#has_job()
call go#lsp#CleanWorkspaces()
endif
let l:dir = go#util#tempdir("vim-go-test/testrun/")
let $GOPATH .= ':' . l:dir
let l:full_path = l:dir . '/src/' . a:path
Expand All @@ -60,7 +66,7 @@ fun! gotest#load_fixture(path) abort
silent exe printf('read %s/test-fixtures/%s', g:vim_go_root, a:path)
silent noautocmd w!
if go#util#has_job()
call go#lsp#AddWorkspace(fnamemodify(l:full_path, ':p:h'))
call go#lsp#AddWorkspaceDirectory(fnamemodify(l:full_path, ':p:h'))
endif

return l:dir
Expand Down
2 changes: 1 addition & 1 deletion ftplugin/go/commands.vim
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ command! -nargs=0 GoReportGitHubIssue call go#issue#New()
command! -nargs=0 GoIfErr call go#iferr#Generate()

" -- lsp
command! -nargs=+ -complete=dir GoAddWorkspace call go#lsp#AddWorkspace(<f-args>)
command! -nargs=+ -complete=dir GoAddWorkspace call go#lsp#AddWorkspaceDirectory(<f-args>)

" vim: sw=2 ts=2 et

0 comments on commit 3a373cf

Please sign in to comment.