Skip to content

Commit d89ea7a

Browse files
authored
improve get workspace folders (#607)
only return workspace folders from gopls. It's different when there are other lsp active clients (e.g. copilot) taking precedent, whose workspace folder could be different (e.g. repo root, instead of go.mod)
1 parent 41a18f0 commit d89ea7a

8 files changed

Lines changed: 21 additions & 12 deletions

File tree

lua/go/enum.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ function M.run(args)
1818
local new_name = vfn.expand('%:p:r') .. "_enum.go"
1919

2020
vim.list_extend(cmd, args)
21-
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
2221
local opts = {
2322
update_buffer = true,
2423
on_exit = function()

lua/go/env.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function M.envfile(f)
1010
if vfn.filereadable(f) then
1111
return f
1212
end
13-
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
13+
local workfolder = util.get_gopls_workspace_folders()[1] or vfn.getcwd()
1414
local goenv = workfolder .. sep .. (f or '.env')
1515

1616
if vfn.filereadable(goenv) == 1 then

lua/go/goget.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function M.run(args)
2626

2727
utils.log(cmd)
2828

29-
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
29+
local workfolder = utils.get_gopls_workspace_folders()[1] or vfn.getcwd()
3030
local modfile = workfolder .. utils.sep() .. 'go.mod'
3131
local opts = {
3232
update_buffer = true,

lua/go/gotool.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function M.run(args)
4040
vim.list_extend(cmd, args)
4141
utils.log(cmd)
4242

43-
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
43+
local workfolder = utils.get_gopls_workspace_folders()[1] or vfn.getcwd()
4444
local modfile = workfolder .. utils.sep() .. 'go.mod'
4545
local opts = {
4646
update_buffer = true,

lua/go/null_ls.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ return {
197197
multiple_files = true,
198198
format = 'raw',
199199
cwd = h.cache.by_bufnr(function(params)
200-
local ws = vim.lsp.buf.list_workspace_folders()
200+
local ws = utils.get_gopls_workspace_folders()
201201
if #ws > 0 then
202202
return ws[1]
203203
end
@@ -353,7 +353,7 @@ return {
353353
filetypes = { 'go' },
354354

355355
cwd = h.cache.by_bufnr(function(params)
356-
local ws = vim.lsp.buf.list_workspace_folders()
356+
local ws = utils.get_gopls_workspace_folders()
357357
if #ws > 0 then
358358
return ws[1]
359359
end

lua/go/project.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ local M = {}
2525
local sep = require("go.utils").sep()
2626

2727
function M.project_existed()
28-
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
28+
local workfolder = util.get_gopls_workspace_folders()[1] or vfn.getcwd()
2929
local gocfgfd = workfolder .. sep .. ".gonvim"
3030
local gocfgbrks = gocfgfd .. sep .. "breakpoints.lua"
3131
local gocfg = gocfgfd .. sep .. "init.lua"
@@ -36,7 +36,7 @@ function M.project_existed()
3636
end
3737

3838
function M.setup()
39-
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
39+
local workfolder = util.get_gopls_workspace_folders()[1] or vfn.getcwd()
4040
local gocfgfd = workfolder .. sep .. ".gonvim"
4141
local gocfg = gocfgfd .. sep .. "init.lua"
4242

@@ -52,7 +52,7 @@ function M.setup()
5252
end
5353

5454
function M.load_project()
55-
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
55+
local workfolder = util.get_gopls_workspace_folders()[1] or vfn.getcwd()
5656
local gocfg = workfolder .. sep .. ".gonvim" .. sep .. "init.lua"
5757
if _GO_NVIM_CFG.disable_per_project_cfg then
5858
log("project setup existed but disabled")

lua/go/utils.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,16 @@ function utils.debug(msg)
529529
end)
530530
end
531531

532+
function utils.get_gopls_workspace_folders()
533+
local workspace_folders = {}
534+
for _, client in pairs(vim.lsp.get_clients({ bufnr = 0, name = 'gopls' })) do
535+
for _, folder in pairs(client.workspace_folders or {}) do
536+
table.insert(workspace_folders, folder.name)
537+
end
538+
end
539+
return workspace_folders
540+
end
541+
532542
function utils.rel_path(folder)
533543
-- maybe expand('%:p:h:t')
534544
local mod = '%:p'
@@ -537,7 +547,7 @@ function utils.rel_path(folder)
537547
end
538548
local fpath = fn.expand(mod)
539549
-- workfolders does not work if user does not setup neovim to follow workspace
540-
local workfolders = vim.lsp.buf.list_workspace_folders()
550+
local workfolders = utils.get_gopls_workspace_folders()
541551
local pwd = fn.getcwd()
542552

543553
if fn.empty(workfolders) == 0 then

lua/tests/go_gopls_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local eq = assert.are.same
22
local cur_dir = vim.fn.expand('%:p:h')
3-
local busted = require('plenary/busted')
3+
local utils = require('go.utils')
44

55
local godir = cur_dir .. '/lua/tests/fixtures'
66
describe('should run gopls related functions', function()
@@ -29,7 +29,7 @@ describe('should run gopls related functions', function()
2929
end, 100)
3030
require('go.format').goimports()
3131
local fmt
32-
require('go.utils').log('workspaces:', vim.inspect(vim.lsp.buf.list_workspace_folders()))
32+
require('go.utils').log('workspaces:', vim.inspect(utils.get_gopls_workspace_folders()))
3333
vim.wait(4000, function()
3434
vim.cmd([[wa]])
3535
fmt = vim.fn.join(vim.fn.readfile(path), '\n')

0 commit comments

Comments
 (0)