2021-07-11 11:33:22 +00:00
|
|
|
local vim, api = vim, vim.api
|
2022-07-28 10:53:19 +00:00
|
|
|
local utils = require('go.utils')
|
2021-09-01 01:22:05 +00:00
|
|
|
local log = utils.log
|
2022-11-03 01:53:45 +00:00
|
|
|
local trace = utils.trace
|
2021-07-11 11:33:22 +00:00
|
|
|
local diagnostic_map = function(bufnr)
|
2021-12-16 01:23:43 +00:00
|
|
|
local opts = { noremap = true, silent = true }
|
2022-07-28 10:53:19 +00:00
|
|
|
api.nvim_buf_set_keymap(bufnr, 'n', ']O', ':lua vim.lsp.diagnostic.set_loclist()<CR>', opts)
|
2021-07-11 11:33:22 +00:00
|
|
|
end
|
2022-05-04 05:15:49 +00:00
|
|
|
|
|
|
|
if vim.lsp.buf.format == nil then
|
2022-08-01 07:02:21 +00:00
|
|
|
-- neovim < 0.8 only
|
2022-07-31 22:30:35 +00:00
|
|
|
vim.lsp.buf.format = function(options)
|
2022-07-31 23:04:28 +00:00
|
|
|
if options.async then
|
|
|
|
vim.lsp.buf.formatting()
|
|
|
|
else
|
|
|
|
vim.lsp.buf.formatting_sync()
|
|
|
|
end
|
2022-07-31 22:30:35 +00:00
|
|
|
end
|
2022-05-04 05:15:49 +00:00
|
|
|
end
|
|
|
|
|
2022-04-28 04:23:18 +00:00
|
|
|
local codelens_enabled = false
|
2022-05-27 17:20:33 +00:00
|
|
|
|
2021-07-11 11:33:22 +00:00
|
|
|
local on_attach = function(client, bufnr)
|
2023-02-11 00:44:44 +00:00
|
|
|
log('go.nvim on_on_attach', client, bufnr)
|
2021-07-11 11:33:22 +00:00
|
|
|
local function buf_set_keymap(...)
|
|
|
|
vim.api.nvim_buf_set_keymap(bufnr, ...)
|
|
|
|
end
|
|
|
|
local uri = vim.uri_from_bufnr(bufnr)
|
2022-07-28 10:53:19 +00:00
|
|
|
if uri == 'file://' or uri == 'file:///' or #uri < 11 then
|
|
|
|
return { error = 'invalid file', result = nil }
|
2021-07-11 11:33:22 +00:00
|
|
|
end
|
|
|
|
diagnostic_map(bufnr)
|
|
|
|
-- add highlight for Lspxxx
|
|
|
|
|
2022-07-28 10:53:19 +00:00
|
|
|
api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
|
2021-07-11 11:33:22 +00:00
|
|
|
|
2021-12-16 01:23:43 +00:00
|
|
|
local opts = { noremap = true, silent = true }
|
2022-01-17 04:29:38 +00:00
|
|
|
if _GO_NVIM_CFG.lsp_document_formatting == false then
|
2022-04-30 09:55:07 +00:00
|
|
|
-- client.resolved_capabilities.document_formatting = false
|
|
|
|
client.server_capabilities.documentFormattingProvider = false
|
2022-01-17 04:29:38 +00:00
|
|
|
end
|
2021-12-16 01:23:43 +00:00
|
|
|
|
2022-04-30 01:57:51 +00:00
|
|
|
if _GO_NVIM_CFG.lsp_codelens then
|
2022-05-04 05:15:49 +00:00
|
|
|
codelens_enabled = (client.server_capabilities.codeLensProvider ~= false)
|
2022-04-30 01:57:51 +00:00
|
|
|
if not codelens_enabled then
|
2023-02-12 20:55:00 +00:00
|
|
|
vim.notify('codelens not support by your gopls', vim.log.levels.WARN)
|
2022-04-30 01:57:51 +00:00
|
|
|
end
|
2022-07-31 11:27:27 +00:00
|
|
|
vim.lsp.codelens.refresh()
|
2022-04-24 11:03:38 +00:00
|
|
|
end
|
2022-07-31 11:27:27 +00:00
|
|
|
|
2022-04-20 11:16:09 +00:00
|
|
|
if _GO_NVIM_CFG.lsp_keymaps == true then
|
2023-02-11 00:44:44 +00:00
|
|
|
log('go.nvim lsp_keymaps', client, bufnr)
|
2023-03-23 07:13:49 +00:00
|
|
|
buf_set_keymap('n', '<Leader>ff', '<Cmd>lua vim.lsp.buf.format({async = true}))<CR>', opts)
|
2022-07-28 10:53:19 +00:00
|
|
|
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)
|
2023-03-12 22:56:55 +00:00
|
|
|
buf_set_keymap(
|
|
|
|
'n',
|
|
|
|
'<space>wl',
|
|
|
|
'<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>',
|
|
|
|
opts
|
|
|
|
)
|
2023-03-23 07:13:49 +00:00
|
|
|
buf_set_keymap('n', 'gD', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
|
2022-07-28 10:53:19 +00:00
|
|
|
buf_set_keymap('n', '<space>rn', "<cmd>lua require('go.rename').run()<CR>", opts)
|
2023-03-12 22:56:55 +00:00
|
|
|
buf_set_keymap(
|
|
|
|
'n',
|
|
|
|
'<space>ca',
|
|
|
|
"<cmd>lua require('go.codeaction').run_code_action()<CR>",
|
|
|
|
opts
|
|
|
|
)
|
|
|
|
buf_set_keymap(
|
|
|
|
'v',
|
|
|
|
'<space>ca',
|
|
|
|
"<cmd>lua require('go.codeaction').run_range_code_action()<CR>",
|
|
|
|
opts
|
|
|
|
)
|
2022-07-28 10:53:19 +00:00
|
|
|
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)
|
2021-07-11 11:33:22 +00:00
|
|
|
|
2022-04-30 09:55:07 +00:00
|
|
|
if client.server_capabilities.documentFormattingProvider then
|
2022-07-28 10:53:19 +00:00
|
|
|
buf_set_keymap('n', '<space>ff', '<cmd>lua vim.lsp.buf.format({async = true})<CR>', opts)
|
2022-04-30 09:55:07 +00:00
|
|
|
end
|
2022-05-05 05:37:59 +00:00
|
|
|
|
2022-07-31 01:29:38 +00:00
|
|
|
-- local vim_version = vim.version().major * 100 + vim.version().minor * 10 + vim.version().patch
|
2022-07-28 10:53:19 +00:00
|
|
|
elseif type(_GO_NVIM_CFG.lsp_keymaps) == 'function' then
|
2022-04-20 11:24:07 +00:00
|
|
|
_GO_NVIM_CFG.lsp_keymaps(bufnr)
|
2022-04-20 11:16:09 +00:00
|
|
|
end
|
2023-04-18 09:20:54 +00:00
|
|
|
if client.name == 'gopls' and vim.fn.has('nvim-0.8.3') == 1 then
|
2023-03-12 22:56:55 +00:00
|
|
|
local semantic = client.config.capabilities.textDocument.semanticTokens
|
2023-04-18 09:20:54 +00:00
|
|
|
local provider = client.server_capabilities.semanticTokensProvider
|
2023-03-14 12:12:38 +00:00
|
|
|
if semantic then
|
2023-04-18 09:20:54 +00:00
|
|
|
client.server_capabilities.semanticTokensProvider =
|
|
|
|
vim.tbl_deep_extend('force', provider or {}, {
|
|
|
|
full = true,
|
|
|
|
legend = {
|
|
|
|
tokenTypes = {
|
|
|
|
'namespace',
|
|
|
|
'type',
|
|
|
|
'class',
|
|
|
|
'enum',
|
|
|
|
'interface',
|
|
|
|
'struct',
|
|
|
|
'typeParameter',
|
|
|
|
'parameter',
|
|
|
|
'variable',
|
|
|
|
'property',
|
|
|
|
'enumMember',
|
|
|
|
'event',
|
|
|
|
'function',
|
|
|
|
'method',
|
|
|
|
'macro',
|
|
|
|
'keyword',
|
|
|
|
'modifier',
|
|
|
|
'comment',
|
|
|
|
'string',
|
|
|
|
'number',
|
|
|
|
'regexp',
|
|
|
|
'operator',
|
|
|
|
},
|
|
|
|
tokenModifiers = {
|
|
|
|
'declaration',
|
|
|
|
'definition',
|
|
|
|
'readonly',
|
|
|
|
'static',
|
|
|
|
'deprecated',
|
|
|
|
'abstract',
|
|
|
|
'async',
|
|
|
|
'modification',
|
|
|
|
'documentation',
|
|
|
|
'defaultLibrary',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
range = true,
|
|
|
|
})
|
2023-03-14 12:12:38 +00:00
|
|
|
end
|
2023-03-12 22:56:55 +00:00
|
|
|
end
|
2021-07-11 11:33:22 +00:00
|
|
|
end
|
|
|
|
|
2022-07-29 12:23:52 +00:00
|
|
|
local extend_config = function(gopls, opts)
|
2022-04-30 01:57:51 +00:00
|
|
|
if next(opts) == nil or gopls == nil then
|
|
|
|
return gopls
|
2021-09-01 01:22:05 +00:00
|
|
|
end
|
|
|
|
for key, value in pairs(opts) do
|
2022-07-28 10:53:19 +00:00
|
|
|
if type(gopls[key]) == 'table' and type(value) == 'table' then
|
2022-07-29 12:23:52 +00:00
|
|
|
gopls[key] = vim.tbl_deep_extend('force', gopls[key], value)
|
2021-09-01 01:22:05 +00:00
|
|
|
else
|
2022-07-31 23:08:52 +00:00
|
|
|
if type(gopls[key]) ~= type(value) and key ~= 'handlers' then
|
2022-07-28 10:53:19 +00:00
|
|
|
vim.notify('gopls setup for ' .. key .. ' is not ' .. type(value))
|
|
|
|
end
|
2021-09-01 01:22:05 +00:00
|
|
|
gopls[key] = value
|
|
|
|
end
|
|
|
|
end
|
2022-07-28 10:53:19 +00:00
|
|
|
return gopls
|
2021-09-01 01:22:05 +00:00
|
|
|
end
|
|
|
|
|
2021-08-23 02:24:48 +00:00
|
|
|
local M = {}
|
|
|
|
|
2022-01-16 04:26:25 +00:00
|
|
|
function M.client()
|
2022-05-27 17:20:33 +00:00
|
|
|
local clients = vim.lsp.get_active_clients()
|
2022-01-16 04:26:25 +00:00
|
|
|
for _, cl in pairs(clients) do
|
2022-07-28 10:53:19 +00:00
|
|
|
if cl.name == 'gopls' then
|
2022-01-16 04:26:25 +00:00
|
|
|
return cl
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-16 01:23:43 +00:00
|
|
|
function M.config()
|
2022-07-29 12:23:52 +00:00
|
|
|
local gopls = require('go.gopls').setups()
|
2022-04-30 01:57:51 +00:00
|
|
|
if gopls == nil then
|
2022-08-10 05:12:47 +00:00
|
|
|
return {}
|
2022-04-30 01:57:51 +00:00
|
|
|
end
|
2022-05-03 22:22:38 +00:00
|
|
|
if _GO_NVIM_CFG == nil then
|
2023-02-12 20:55:00 +00:00
|
|
|
return vim.notify('please setup go.nvim', vim.log.levels.WARN)
|
2022-05-03 22:22:38 +00:00
|
|
|
end
|
2021-12-21 23:58:06 +00:00
|
|
|
gopls.on_attach = on_attach
|
2022-07-28 10:53:19 +00:00
|
|
|
if type(_GO_NVIM_CFG.lsp_on_attach) == 'function' then
|
2021-12-21 23:58:06 +00:00
|
|
|
gopls.on_attach = _GO_NVIM_CFG.lsp_on_attach
|
2021-07-12 04:25:07 +00:00
|
|
|
end
|
2022-07-28 10:53:19 +00:00
|
|
|
if _GO_NVIM_CFG.lsp_on_client_start and type(_GO_NVIM_CFG.lsp_on_client_start) == 'function' then
|
2022-05-13 09:27:02 +00:00
|
|
|
gopls.on_attach = function(client, bufnr)
|
2022-07-08 10:44:19 +00:00
|
|
|
on_attach(client, bufnr)
|
2022-05-13 09:27:02 +00:00
|
|
|
_GO_NVIM_CFG.lsp_on_client_start(client, bufnr)
|
|
|
|
end
|
|
|
|
end
|
2021-07-12 04:25:07 +00:00
|
|
|
|
2021-08-23 02:24:48 +00:00
|
|
|
if _GO_NVIM_CFG.gopls_cmd then
|
|
|
|
gopls.cmd = _GO_NVIM_CFG.gopls_cmd
|
2021-09-02 11:07:32 +00:00
|
|
|
else
|
2022-07-28 10:53:19 +00:00
|
|
|
gopls.cmd = { 'gopls' }
|
|
|
|
require('go.install').install('gopls')
|
2021-08-23 02:24:48 +00:00
|
|
|
end
|
2021-07-26 00:35:58 +00:00
|
|
|
|
2021-08-23 02:24:48 +00:00
|
|
|
if _GO_NVIM_CFG.lsp_gofumpt then
|
|
|
|
gopls.settings.gopls.gofumpt = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if _GO_NVIM_CFG.gopls_remote_auto then
|
2022-07-28 10:53:19 +00:00
|
|
|
table.insert(gopls.cmd, '-remote=auto')
|
2021-08-23 02:24:48 +00:00
|
|
|
end
|
|
|
|
|
2022-07-28 10:53:19 +00:00
|
|
|
if type(_GO_NVIM_CFG.lsp_cfg) == 'table' then
|
2022-07-29 12:23:52 +00:00
|
|
|
return extend_config(gopls, _GO_NVIM_CFG.lsp_cfg)
|
2021-09-01 01:22:05 +00:00
|
|
|
end
|
2021-12-16 01:23:43 +00:00
|
|
|
return gopls
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.setup()
|
2022-05-27 17:20:33 +00:00
|
|
|
local goplscfg = M.config()
|
2022-07-28 10:53:19 +00:00
|
|
|
local lspconfig = utils.load_plugin('nvim-lspconfig', 'lspconfig')
|
2022-01-17 04:29:38 +00:00
|
|
|
if lspconfig == nil then
|
2023-02-12 20:55:00 +00:00
|
|
|
vim.notify('failed to load lspconfig', vim.log.levels.WARN)
|
2022-01-17 04:29:38 +00:00
|
|
|
return
|
|
|
|
end
|
2022-05-01 23:06:31 +00:00
|
|
|
|
2022-05-05 05:37:59 +00:00
|
|
|
local vim_version = vim.version().major * 100 + vim.version().minor * 10 + vim.version().patch
|
2022-05-01 23:06:31 +00:00
|
|
|
|
|
|
|
if vim_version < 61 then
|
2022-07-28 10:53:19 +00:00
|
|
|
vim.notify('LSP: go.nvim requires neovim 0.6.1 or later', vim.log.levels.WARN)
|
2022-05-01 23:06:31 +00:00
|
|
|
end
|
2022-07-19 23:27:59 +00:00
|
|
|
log(goplscfg)
|
2022-05-27 17:20:33 +00:00
|
|
|
lspconfig.gopls.setup(goplscfg)
|
2021-07-11 11:33:22 +00:00
|
|
|
end
|
2021-08-23 02:24:48 +00:00
|
|
|
|
2021-09-20 09:05:04 +00:00
|
|
|
--[[
|
|
|
|
FillStruct = "fill_struct"
|
|
|
|
UndeclaredName = "undeclared_name"
|
|
|
|
ExtractVariable = "extract_variable"
|
|
|
|
ExtractFunction = "extract_function"
|
|
|
|
ExtractMethod = "extract_method"
|
2021-09-25 02:44:52 +00:00
|
|
|
valueSet = { "", "Empty", "QuickFix", "Refactor", "RefactorExtract", "RefactorInline", "RefactorRewrite", "Source", "SourceOrganizeImports", "quickfix", "refactor", "refactor.extract", "refactor.inline", "refactor.re
|
|
|
|
write", "source", "source.organizeImports" }
|
2021-09-20 09:05:04 +00:00
|
|
|
]]
|
|
|
|
|
|
|
|
-- action / fix to take
|
|
|
|
-- only this action 'refactor.rewrite' source.organizeImports
|
|
|
|
M.codeaction = function(action, only, wait_ms)
|
|
|
|
wait_ms = wait_ms or 1000
|
|
|
|
local params = vim.lsp.util.make_range_params()
|
|
|
|
log(action, only)
|
|
|
|
if only then
|
2021-12-16 01:23:43 +00:00
|
|
|
params.context = { only = { only } }
|
2021-09-20 09:05:04 +00:00
|
|
|
end
|
2022-07-28 10:53:19 +00:00
|
|
|
local result = vim.lsp.buf_request_sync(0, 'textDocument/codeAction', params, wait_ms)
|
2021-09-20 09:05:04 +00:00
|
|
|
if not result or next(result) == nil then
|
2022-07-28 10:53:19 +00:00
|
|
|
log('nil result')
|
2021-09-20 09:05:04 +00:00
|
|
|
return
|
|
|
|
end
|
2022-07-28 10:53:19 +00:00
|
|
|
log('code action result', result)
|
2022-01-16 04:26:25 +00:00
|
|
|
local c = M.client()
|
2021-09-20 09:05:04 +00:00
|
|
|
for _, res in pairs(result) do
|
|
|
|
for _, r in pairs(res.result or {}) do
|
|
|
|
if r.edit and not vim.tbl_isempty(r.edit) then
|
2022-01-16 04:26:25 +00:00
|
|
|
local re = vim.lsp.util.apply_workspace_edit(r.edit, c.offset_encoding)
|
2022-07-28 10:53:19 +00:00
|
|
|
log('workspace edit', r, re)
|
2021-09-20 09:05:04 +00:00
|
|
|
end
|
2022-07-28 10:53:19 +00:00
|
|
|
if type(r.command) == 'table' then
|
|
|
|
if type(r.command) == 'table' and r.command.arguments then
|
2021-09-20 09:05:04 +00:00
|
|
|
for _, arg in pairs(r.command.arguments) do
|
2022-07-28 10:53:19 +00:00
|
|
|
if action == nil or arg['Fix'] == action then
|
2021-09-20 09:05:04 +00:00
|
|
|
vim.lsp.buf.execute_command(r.command)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-16 01:23:43 +00:00
|
|
|
M.gopls_on_attach = on_attach
|
2022-04-28 04:23:18 +00:00
|
|
|
M.codelens_enabled = function()
|
|
|
|
return codelens_enabled
|
|
|
|
end
|
2021-12-16 01:23:43 +00:00
|
|
|
|
2022-08-23 09:31:13 +00:00
|
|
|
local function request(method, params, handler)
|
|
|
|
return vim.lsp.buf_request(0, method, params, handler)
|
|
|
|
end
|
|
|
|
|
2023-03-03 09:43:47 +00:00
|
|
|
local function request_sync(method, params, timeout_ms)
|
|
|
|
return vim.lsp.buf_request_sync(0, method, params, timeout_ms)
|
|
|
|
end
|
|
|
|
|
2022-08-23 09:31:13 +00:00
|
|
|
function M.gen_return(lsp_result)
|
|
|
|
if not lsp_result or not lsp_result.contents then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local contents = vim.split(lsp_result.contents.value, '\n')
|
|
|
|
local func
|
|
|
|
for _, line in ipairs(contents) do
|
|
|
|
if line:match('^func') then
|
|
|
|
func = line
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not func then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local ret_list, err = M.find_ret(func)
|
|
|
|
if ret_list == nil or next(ret_list) == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local header = ret_list[1]
|
|
|
|
for i = 2, #ret_list do
|
|
|
|
header = header .. ', ' .. ret_list[i]
|
|
|
|
end
|
|
|
|
local current_line = vim.api.nvim_get_current_line()
|
|
|
|
local ss, se = string.find(current_line, '%s+')
|
|
|
|
local leading_space = ''
|
|
|
|
if ss then
|
|
|
|
leading_space = current_line:sub(ss, se)
|
|
|
|
end
|
|
|
|
header = leading_space .. header .. ' := ' .. vim.trim(current_line)
|
|
|
|
|
|
|
|
local row, col = unpack(api.nvim_win_get_cursor(0))
|
|
|
|
vim.api.nvim_buf_set_lines(0, row - 1, row, true, { header })
|
|
|
|
vim.cmd('write')
|
|
|
|
if err then
|
|
|
|
require('go.iferr').run()
|
|
|
|
end
|
|
|
|
return header
|
|
|
|
end
|
|
|
|
|
|
|
|
local name_map = {
|
|
|
|
error = 'err',
|
|
|
|
int = 'i',
|
|
|
|
int64 = 'i',
|
|
|
|
uint = 'i',
|
|
|
|
uint64 = 'i',
|
|
|
|
float = 'f',
|
|
|
|
float64 = 'f',
|
|
|
|
string = 's',
|
|
|
|
rune = 'r',
|
|
|
|
bool = 'b',
|
|
|
|
channel = 'ch',
|
|
|
|
byte = 'b',
|
|
|
|
}
|
|
|
|
|
2022-09-12 01:22:21 +00:00
|
|
|
local function gen_name(types)
|
2022-08-23 09:31:13 +00:00
|
|
|
local rets = {}
|
|
|
|
local used = {}
|
|
|
|
for _, t in pairs(types) do
|
|
|
|
if name_map[t] then
|
|
|
|
if not used[name_map[t]] then
|
|
|
|
rets[#rets + 1] = name_map[t]
|
|
|
|
used[name_map[t]] = 1
|
|
|
|
else
|
|
|
|
rets[#rets + 1] = name_map[t] .. tostring(used[name_map[t]])
|
|
|
|
used[name_map[t]] = used[name_map[t]] + 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local f = t:sub(1, 1)
|
|
|
|
if f == f:upper() then
|
|
|
|
name_map[t] = f:lower() .. t:sub(2)
|
|
|
|
table.insert(rets, name_map[t])
|
|
|
|
used[name_map[t]] = (used[name_map[t]] or 0) + 1
|
|
|
|
else
|
|
|
|
name_map[t] = f
|
|
|
|
table.insert(rets, name_map[t])
|
|
|
|
used[name_map[t]] = (used[name_map[t]] or 0) + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
log(rets)
|
|
|
|
return rets
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.find_ret(str)
|
|
|
|
str = vim.trim(str)
|
2022-11-11 16:13:43 +00:00
|
|
|
local pat = [[\v^func\s+%(\w|\.|\*|\)|\()+\(%(\w|\_s|[*\.\[\],{}<>-])*\)\s+]]
|
2022-08-23 09:31:13 +00:00
|
|
|
local regex = vim.regex(pat)
|
|
|
|
local start, endpos = regex:match_str(str)
|
|
|
|
if start == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local ret = vim.trim(str:sub(endpos + 1))
|
|
|
|
if ret == '' then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
pat = [[\v\(%(\w|\_s|[*\.\[\],{}<>-])*\)]]
|
|
|
|
regex = vim.regex(pat)
|
|
|
|
|
|
|
|
start, endpos = regex:match_str(ret)
|
|
|
|
-- handle return type in bracket
|
|
|
|
local retlist = {}
|
|
|
|
if start ~= nil then
|
|
|
|
ret = ret:sub(2, #ret - 1) -- remove ( and )
|
|
|
|
local ret_types = vim.split(ret, ',%s*')
|
|
|
|
local need_convert = true
|
|
|
|
for _, t in pairs(ret_types) do
|
|
|
|
t = vim.trim(t)
|
|
|
|
local m = vim.split(t, '%s+')
|
|
|
|
if #m > 1 then
|
|
|
|
need_convert = false
|
|
|
|
end
|
|
|
|
table.insert(retlist, m[1])
|
|
|
|
end
|
|
|
|
if need_convert then
|
|
|
|
retlist = gen_name(ret_types)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
retlist = gen_name({ ret })
|
|
|
|
end
|
|
|
|
local includes_err = vim.tbl_contains(retlist, 'err')
|
|
|
|
return retlist, includes_err
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.hover_returns()
|
|
|
|
local util = require('vim.lsp.util')
|
|
|
|
|
|
|
|
local current_line = vim.api.nvim_get_current_line()
|
|
|
|
local row, col = unpack(api.nvim_win_get_cursor(0))
|
|
|
|
local pat = [[\w\+(]]
|
|
|
|
local r = vim.regex(pat)
|
|
|
|
local s, e = r:match_str(current_line)
|
|
|
|
log(s, e)
|
|
|
|
if s == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local params = util.make_position_params()
|
|
|
|
params.position.character = e - 1
|
|
|
|
log(params)
|
|
|
|
request('textDocument/hover', params, function(err, result, ctx)
|
|
|
|
if err ~= nil then
|
|
|
|
log(err)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if result == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
M.gen_return(result)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2023-03-03 09:43:47 +00:00
|
|
|
function M.document_symbols(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
|
|
|
|
local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()
|
|
|
|
local params = vim.lsp.util.make_position_params()
|
|
|
|
params.context = { includeDeclaration = true }
|
|
|
|
params.query = opts.prompt or ''
|
|
|
|
local symbols
|
|
|
|
vim.lsp.for_each_buffer_client(bufnr, function(client, _, _bufnr)
|
|
|
|
if client.name == 'gopls' then
|
2023-03-12 22:56:55 +00:00
|
|
|
symbols =
|
|
|
|
client.request_sync('textDocument/documentSymbol', params, opts.timeout or 1000, _bufnr)
|
2023-03-03 09:43:47 +00:00
|
|
|
return symbols
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
return symbols
|
|
|
|
end
|
|
|
|
|
2022-11-03 01:53:45 +00:00
|
|
|
local change_type = {
|
|
|
|
Created = 1,
|
|
|
|
Changed = 2,
|
|
|
|
Deleted = 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
function M.watchFileChanged(fname, params)
|
|
|
|
params = params or vim.lsp.util.make_workspace_params()
|
|
|
|
fname = fname or vim.api.nvim_buf_get_name(0)
|
2023-03-12 22:56:55 +00:00
|
|
|
-- \ 'method': 'workspace/didChangeWatchedFiles',
|
|
|
|
params.changes = params.changes
|
|
|
|
or {
|
|
|
|
{ uri = params.uri or vim.uri_from_fname(fname), type = params.type or change_type.Changed },
|
|
|
|
}
|
|
|
|
vim.lsp.buf_request(
|
|
|
|
vim.api.nvim_get_current_buf(),
|
|
|
|
'workspace/didChangeWatchedFiles',
|
|
|
|
params,
|
|
|
|
function(err, result, ctx)
|
|
|
|
vim.defer_fn(function()
|
|
|
|
-- log(err, result, ctx)
|
|
|
|
if err then
|
|
|
|
-- the request was send to all clients and some may not support
|
|
|
|
log(
|
|
|
|
'failed to workspace reloaded:'
|
|
|
|
.. vim.inspect(err)
|
|
|
|
.. vim.inspect(ctx)
|
|
|
|
.. vim.inspect(result)
|
|
|
|
)
|
|
|
|
else
|
|
|
|
vim.notify('workspace reloaded')
|
|
|
|
end
|
|
|
|
end, 200)
|
|
|
|
end
|
|
|
|
)
|
2022-11-03 01:53:45 +00:00
|
|
|
end
|
|
|
|
|
2021-08-23 02:24:48 +00:00
|
|
|
return M
|