Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lua/go/enum.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function M.run(args)
local new_name = vfn.expand('%:p:r') .. "_enum.go"

vim.list_extend(cmd, args)
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
local opts = {
update_buffer = true,
on_exit = function()
Expand Down
2 changes: 1 addition & 1 deletion lua/go/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function M.envfile(f)
if vfn.filereadable(f) then
return f
end
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
local workfolder = util.get_gopls_workspace_folders()[1] or vfn.getcwd()
local goenv = workfolder .. sep .. (f or '.env')

if vfn.filereadable(goenv) == 1 then
Expand Down
2 changes: 1 addition & 1 deletion lua/go/goget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function M.run(args)

utils.log(cmd)

local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
local workfolder = utils.get_gopls_workspace_folders()[1] or vfn.getcwd()
local modfile = workfolder .. utils.sep() .. 'go.mod'
local opts = {
update_buffer = true,
Expand Down
2 changes: 1 addition & 1 deletion lua/go/gotool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function M.run(args)
vim.list_extend(cmd, args)
utils.log(cmd)

local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
local workfolder = utils.get_gopls_workspace_folders()[1] or vfn.getcwd()
local modfile = workfolder .. utils.sep() .. 'go.mod'
local opts = {
update_buffer = true,
Expand Down
4 changes: 2 additions & 2 deletions lua/go/null_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ return {
multiple_files = true,
format = 'raw',
cwd = h.cache.by_bufnr(function(params)
local ws = vim.lsp.buf.list_workspace_folders()
local ws = utils.get_gopls_workspace_folders()
if #ws > 0 then
return ws[1]
end
Expand Down Expand Up @@ -353,7 +353,7 @@ return {
filetypes = { 'go' },

cwd = h.cache.by_bufnr(function(params)
local ws = vim.lsp.buf.list_workspace_folders()
local ws = utils.get_gopls_workspace_folders()
if #ws > 0 then
return ws[1]
end
Expand Down
6 changes: 3 additions & 3 deletions lua/go/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local M = {}
local sep = require("go.utils").sep()

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

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

Expand All @@ -52,7 +52,7 @@ function M.setup()
end

function M.load_project()
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
local workfolder = util.get_gopls_workspace_folders()[1] or vfn.getcwd()
local gocfg = workfolder .. sep .. ".gonvim" .. sep .. "init.lua"
if _GO_NVIM_CFG.disable_per_project_cfg then
log("project setup existed but disabled")
Expand Down
12 changes: 11 additions & 1 deletion lua/go/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,16 @@ function utils.debug(msg)
end)
end

function utils.get_gopls_workspace_folders()
local workspace_folders = {}
for _, client in pairs(vim.lsp.get_clients({ bufnr = 0, name = 'gopls' })) do
for _, folder in pairs(client.workspace_folders or {}) do
table.insert(workspace_folders, folder.name)
end
end
return workspace_folders
end

function utils.rel_path(folder)
-- maybe expand('%:p:h:t')
local mod = '%:p'
Expand All @@ -537,7 +547,7 @@ function utils.rel_path(folder)
end
local fpath = fn.expand(mod)
-- workfolders does not work if user does not setup neovim to follow workspace
local workfolders = vim.lsp.buf.list_workspace_folders()
local workfolders = utils.get_gopls_workspace_folders()
local pwd = fn.getcwd()

if fn.empty(workfolders) == 0 then
Expand Down
4 changes: 2 additions & 2 deletions lua/tests/go_gopls_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local eq = assert.are.same
local cur_dir = vim.fn.expand('%:p:h')
local busted = require('plenary/busted')
local utils = require('go.utils')

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