return gopls setup so we can use in nvim-lsp-installer, issue #56 escape '-'

pull/59/head
ray-x 3 years ago
parent 8669783860
commit 2d7e23b72b

@ -156,6 +156,8 @@ M.test_fun = function(...)
-- end
local fpath = vim.fn.expand("%:p:h")
fpath:gsub(" ", [[\ ]])
fpath:gsub("-", [[\-]])
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row, col = row, col + 1
local ns = require("go.ts.go").get_func_method_node_at_pos(row, col)

@ -1,8 +1,8 @@
local vim, api = vim, vim.api
local utils = require('go.utils')
local utils = require("go.utils")
local log = utils.log
local diagnostic_map = function(bufnr)
local opts = {noremap = true, silent = true}
local opts = { noremap = true, silent = true }
api.nvim_buf_set_keymap(bufnr, "n", "]O", ":lua vim.lsp.diagnostic.set_loclist()<CR>", opts)
end
@ -21,44 +21,43 @@ local on_attach = function(client, bufnr)
end
local uri = vim.uri_from_bufnr(bufnr)
if uri == "file://" or uri == "file:///" or #uri < 11 then
return {error = "invalid file", result = nil}
return { error = "invalid file", result = nil }
end
diagnostic_map(bufnr)
-- add highlight for Lspxxx
api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
local opts = {noremap = true, silent = true}
buf_set_keymap('n', 'gD', '<Cmd>lua vim.lsp.buf.declaration()<CR>', opts)
buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)
buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts)
buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
buf_set_keymap('n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
buf_set_keymap('n', '<space>ca', '<cmd>lua require"go.lsp".telescope_code_actions()<CR>', opts)
buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
buf_set_keymap('n', '<space>e', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', opts)
buf_set_keymap('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)
buf_set_keymap('n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts)
buf_set_keymap('n', '<space>q', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>', opts)
local opts = { noremap = true, silent = true }
buf_set_keymap("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
buf_set_keymap("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
buf_set_keymap("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>", opts)
buf_set_keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
buf_set_keymap("n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
buf_set_keymap("n", "<space>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
buf_set_keymap("n", "<space>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
buf_set_keymap("n", "<space>wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
buf_set_keymap("n", "<space>D", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
buf_set_keymap("n", "<space>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
buf_set_keymap("n", "<space>ca", '<cmd>lua require"go.lsp".telescope_code_actions()<CR>', opts)
buf_set_keymap("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
buf_set_keymap("n", "<space>e", "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", opts)
buf_set_keymap("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
buf_set_keymap("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
buf_set_keymap("n", "<space>q", "<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>", opts)
buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
buf_set_keymap("n", "<space>ff", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
end
local gopls = {
-- capabilities = cap,
filetypes = {"go", "gomod"},
filetypes = { "go", "gomod" },
message_level = vim.lsp.protocol.MessageType.Error,
cmd = {
"gopls", -- share the gopls instance if there is one already
"-remote.debug=:0"
"-remote.debug=:0",
},
root_dir = function(fname)
local has_lsp, lspconfig = pcall(require, "lspconfig")
@ -67,18 +66,18 @@ local gopls = {
return util.root_pattern("go.mod", ".git")(fname) or util.path.dirname(fname)
end
end,
flags = {allow_incremental_sync = true, debounce_text_changes = 500},
flags = { allow_incremental_sync = true, debounce_text_changes = 500 },
settings = {
gopls = {
-- more settings: https://github.com/golang/tools/blob/master/gopls/doc/settings.md
-- flags = {allow_incremental_sync = true, debounce_text_changes = 500},
-- not supported
analyses = {unusedparams = true, unreachable = false},
analyses = { unusedparams = true, unreachable = false },
codelenses = {
generate = true, -- show the `go generate` lens.
gc_details = true, -- // Show a code lens toggling the display of gc's choices.
test = true,
tidy = true
tidy = true,
},
usePlaceholders = true,
completeUnimported = true,
@ -88,12 +87,12 @@ local gopls = {
diagnosticsDelay = "500ms",
experimentalWatchedFileDelay = "100ms",
symbolMatcher = "fuzzy",
['local'] = "",
["local"] = "",
gofumpt = false, -- true, -- turn on for new repos, gofmpt is good but also create code turmoils
buildFlags = {"-tags", "integration"}
buildFlags = { "-tags", "integration" },
-- buildFlags = {"-tags", "functional"}
}
}
},
},
}
local extend_config = function(opts)
@ -114,7 +113,8 @@ end
local M = {}
function M.setup()
function M.config()
if _GO_NVIM_CFG.lsp_on_attach then
if _GO_NVIM_CFG.lsp_on_attach == true then
gopls.on_attach = on_attach
@ -126,7 +126,7 @@ function M.setup()
if _GO_NVIM_CFG.gopls_cmd then
gopls.cmd = _GO_NVIM_CFG.gopls_cmd
else
gopls.cmd = {'gopls'}
gopls.cmd = { "gopls" }
end
if _GO_NVIM_CFG.lsp_gofumpt then
@ -140,8 +140,12 @@ function M.setup()
if type(_GO_NVIM_CFG.lsp_cfg) == "table" then
extend_config(_GO_NVIM_CFG.lsp_cfg)
end
log(gopls)
require'lspconfig'.gopls.setup(gopls)
return gopls
end
function M.setup()
local gopls = M.config()
require("lspconfig").gopls.setup(gopls)
end
--[[
@ -161,7 +165,7 @@ M.codeaction = function(action, only, wait_ms)
local params = vim.lsp.util.make_range_params()
log(action, only)
if only then
params.context = {only = {only}}
params.context = { only = { only } }
end
local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, wait_ms)
if not result or next(result) == nil then
@ -178,7 +182,7 @@ M.codeaction = function(action, only, wait_ms)
if type(r.command) == "table" then
if type(r.command) == "table" and r.command.arguments then
for _, arg in pairs(r.command.arguments) do
if action == nil or arg['Fix'] == action then
if action == nil or arg["Fix"] == action then
vim.lsp.buf.execute_command(r.command)
return
end
@ -190,18 +194,23 @@ M.codeaction = function(action, only, wait_ms)
end
function M.telescope_code_actions()
local ok, _ = utils.load_plugin('telescope', "builtin")
local ok, _ = utils.load_plugin("telescope", "builtin")
if ok then
local themes = require('telescope.themes')
local opts = themes.get_dropdown {
local themes = require("telescope.themes")
local opts = themes.get_dropdown({
winblend = 10,
border = true,
previewer = false,
shorten_path = false
}
require('telescope.builtin').lsp_code_actions(opts)
shorten_path = false,
})
require("telescope.builtin").lsp_code_actions(opts)
else
vim.lsp.buf.code_action()
end
end
M.gopls_cfg = gopls
M.gopls_on_attach = on_attach
return M

Loading…
Cancel
Save