From 4d3fa34df01dd0741303c57f51c588fdd23ae441 Mon Sep 17 00:00:00 2001 From: ray-x Date: Wed, 27 Mar 2024 11:11:41 +1100 Subject: [PATCH] allow ui.input/ui.select to be configurable issue #444 --- README.md | 3 +++ lua/go.lua | 14 +++++++++++++- lua/go/codeaction.lua | 15 ++++----------- lua/go/codelens.lua | 23 ++++++----------------- lua/go/dap.lua | 6 +----- lua/go/gomvp.lua | 5 +---- lua/go/gotest.lua | 5 +---- lua/go/inlay.lua | 12 +----------- lua/go/lsp.lua | 39 +++++++++++++++------------------------ lua/go/rename.lua | 36 ++++++++++++++++++------------------ lua/go/utils.lua | 8 +++----- 11 files changed, 66 insertions(+), 100 deletions(-) diff --git a/README.md b/README.md index 33a1ec2..efdf68a 100644 --- a/README.md +++ b/README.md @@ -788,6 +788,9 @@ require('go').setup({ signs = true, update_in_insert = false, }, + -- if you need to setup your ui for input and select, you can do it here + -- go_input = require('guihua.input').input -- set to vim.ui.input to disable guihua input + -- go_select = require('guihua.select').select -- vim.ui.select to disable guihua select lsp_document_formatting = true, -- set to true: use gopls to format -- false if you want to use other formatter tool(e.g. efm, nulls) diff --git a/lua/go.lua b/lua/go.lua index b98b938..b00a558 100644 --- a/lua/go.lua +++ b/lua/go.lua @@ -50,6 +50,18 @@ _GO_NVIM_CFG = { update_in_insert = false, signs = true, }, + go_input = function() + if require('go.utils').load_plugin('guihua.lua', 'guihua.gui') then + return require('guihua.input').input + end + return vim.ui.input + end, + go_select = function() + if require('go.utils').load_plugin('guihua.lua', 'guihua.gui') then + return require('guihua.gui').select + end + return vim.ui.select + end, -- deprecated setups -- lsp_diag_hdlr = true, -- hook lsp diag handler -- lsp_diag_underline = true, @@ -58,7 +70,7 @@ _GO_NVIM_CFG = { -- lsp_diag_signs = true, lsp_inlay_hints = { enable = true, - style = 'inlay', -- 'default: inlay', 'eol': show at end of line, 'inlay': show in the middle of the line + style = 'inlay', -- 'default: inlay', 'eol': show at end of line, 'inlay': show in the middle of the line -- Note: following setup only for for style == 'eol' -- Only show inlay hints for the current line diff --git a/lua/go/codeaction.lua b/lua/go/codeaction.lua index 7421e74..ee248d4 100644 --- a/lua/go/codeaction.lua +++ b/lua/go/codeaction.lua @@ -14,11 +14,8 @@ function M.run_range_code_action(t) local original_select = vim.ui.select local original_input = vim.ui.input - local guihua = utils.load_plugin('guihua.lua', 'guihua.gui') - if guihua then - vim.ui.select = require('guihua.gui').select - vim.ui.input = require('guihua.input').input - end + vim.ui.select = _GO_NVIM_CFG.go_select() + vim.ui.input = _GO_NVIM_CFG.go_input() vim.lsp.buf.code_action({ context = context, range = t.range, @@ -30,14 +27,10 @@ function M.run_range_code_action(t) end function M.run_code_action() - local guihua = utils.load_plugin('guihua.lua', 'guihua.gui') - local original_select = vim.ui.select local original_input = vim.ui.input - if guihua then - vim.ui.select = require('guihua.gui').select - vim.ui.input = require('guihua.input').input - end + vim.ui.select = _GO_NVIM_CFG.go_select() + vim.ui.input = _GO_NVIM_CFG.go_input() log('codeaction') if vim.api.nvim_get_mode().mode ~= 'v' then diff --git a/lua/go/codelens.lua b/lua/go/codelens.lua index f991c4b..ed4b460 100644 --- a/lua/go/codelens.lua +++ b/lua/go/codelens.lua @@ -24,12 +24,9 @@ function M.setup() end function M.run_action() - local guihua = utils.load_plugin('guihua.lua', 'guihua.gui') local original_select = vim.ui.select - if guihua then - vim.ui.select = require('guihua.gui').select - end + vim.ui.select = _GO_NVIM_CFG.go_select() codelens.run() vim.defer_fn(function() @@ -50,20 +47,12 @@ function M.toggle() end function M.refresh() + local gopls = require('go.lsp').client() + log('refresh codelens') + if not gopls then -- and gopls.server_capabilities.codeLensProvider then + return + end if _GO_NVIM_CFG.lsp_codelens == true then - local found = false - if not found then - for _, lsp in pairs(vim.lsp.get_active_clients()) do - if lsp.name == 'gopls' then - found = true - break - end - end - end - if not found then - return - end - log('refresh codelens') vim.lsp.codelens.refresh() else log('refresh codelens') diff --git a/lua/go/dap.lua b/lua/go/dap.lua index 47be13c..ee27c85 100644 --- a/lua/go/dap.lua +++ b/lua/go/dap.lua @@ -314,12 +314,8 @@ M.run = function(...) return require('dap').toggle_breakpoint() end - local guihua = utils.load_plugin('guihua.lua', 'guihua.gui') - local original_select = vim.ui.select - if guihua then - vim.ui.select = require('guihua.gui').select - end + vim.ui.select = _GO_NVIM_CFG.go_select() -- testopts = {"test", "nearest", "file", "stop", "restart"} log('plugin loaded', mode, optarg) diff --git a/lua/go/gomvp.lua b/lua/go/gomvp.lua index 6605a8f..e90ef9a 100644 --- a/lua/go/gomvp.lua +++ b/lua/go/gomvp.lua @@ -9,10 +9,7 @@ function M.run(args) local input = vim.ui.input - local guihua = utils.load_plugin("guihua.lua", "guihua.input") - if guihua then - input = guihua.input - end + vim.ui.input = _GO_NVIM_CFG.go_input() local cmd = { "gomvp" } local old_mod = require('go.ts.go').get_module_at_pos() if old_mod == nil then diff --git a/lua/go/gotest.lua b/lua/go/gotest.lua index 9032dbd..681aa34 100644 --- a/lua/go/gotest.lua +++ b/lua/go/gotest.lua @@ -719,12 +719,9 @@ end -- GUI to select test? M.select_tests = function() - local guihua = utils.load_plugin('guihua.lua', 'guihua.gui') local original_select = vim.ui.select - if guihua then - vim.ui.select = require('guihua.gui').select - end + vim.ui.select = _GO_NVIM_CFG.go_select() vim.defer_fn(function() vim.ui.select = original_select diff --git a/lua/go/inlay.lua b/lua/go/inlay.lua index 0667f98..411a1bc 100644 --- a/lua/go/inlay.lua +++ b/lua/go/inlay.lua @@ -305,7 +305,6 @@ function M.disable_inlay_hints(update, bufnr) end end -local found = false -- Sends the request to gopls to get the inlay hints and handle them function M.set_inlay_hints() if vim.wo.diff then @@ -313,16 +312,7 @@ function M.set_inlay_hints() end local bufnr = vim.api.nvim_get_current_buf() -- check if lsp is ready - if not found then - for _, lsp in pairs(vim.lsp.get_active_clients({ bufnr = bufnr })) do - if lsp.name == 'gopls' then - found = true - break - end - end - end - if not found then - log('gopls not found') + if not require('go.lsp').client() then return end local fname = fn.expand('%:p') diff --git a/lua/go/lsp.lua b/lua/go/lsp.lua index de18b1f..d1cf192 100644 --- a/lua/go/lsp.lua +++ b/lua/go/lsp.lua @@ -179,12 +179,11 @@ end local M = {} function M.client() - local clients = vim.lsp.get_active_clients() - for _, cl in pairs(clients) do - if cl.name == 'gopls' then - return cl - end - end + local clients = vim.lsp.get_active_clients({ + bufnr = vim.api.nvim_get_current_buf(), + name = 'gopls', + }) or {} + return clients[1] end function M.config() @@ -266,16 +265,7 @@ M.codeaction = function(gopls_cmd, only, hdlr) params.context = { only = { only } } end local bufnr = vim.api.nvim_get_current_buf() - local clients = vim.lsp.get_active_clients() - local gopls - -- find gopls client - for cid, c in pairs(clients) do - if c.name == 'gopls' then - log('gopls found', cid) - gopls = c - break - end - end + local gopls = M.client() if gopls == nil then log('gopls not found') return hdlr() @@ -530,14 +520,15 @@ function M.document_symbols(opts) 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 - symbols = - client.request_sync('textDocument/documentSymbol', params, opts.timeout or 1000, _bufnr) - return symbols - end - end) - return symbols + local c = M.client() + if c ~= nil then + return c.request_sync( + 'textDocument/documentSymbol', + params, + opts.timeout or 1000, + vim.api.nvim_get_current_buf() + ) + end end local change_type = { diff --git a/lua/go/rename.lua b/lua/go/rename.lua index 1856ca9..15cb148 100644 --- a/lua/go/rename.lua +++ b/lua/go/rename.lua @@ -1,42 +1,37 @@ -local utils = require("go.utils") +local utils = require('go.utils') -local gorename = "gorename" +local gorename = 'gorename' local vfn = vim.fn local lsprename = function() - - local guihua = utils.load_plugin("guihua.lua", "guihua.floating") local input = vim.ui.input - if guihua then - vim.ui.input = require('guihua.input').input - end + vim.ui.input = _GO_NVIM_CFG.go_input() vim.lsp.buf.rename() return vim.defer_fn(function() vim.ui.input = input end, 1000) - end local run = function(to_identifier, ...) - require("go.install").install(gorename) - local fname = vfn.expand("%:p") -- %:p:h ? %:p + require('go.install').install(gorename) + local fname = vfn.expand('%:p') -- %:p:h ? %:p - local old_identifier = vfn.expand("") + local old_identifier = vfn.expand('') local prompt = vfn.printf("GoRename '%s' to (may take a while) :", old_identifier) to_identifier = to_identifier or vfn.input(prompt, old_identifier) local byte_offset = vfn.wordcount().cursor_bytes - local clients = vim.lsp.get_active_clients() or {} - if #clients > 0 then + local client = require('go.lsp').client() + if client then -- TODO check gopls? return lsprename() end - local offset = string.format("%s:#%i", fname, byte_offset) + local offset = string.format('%s:#%i', fname, byte_offset) - local setup = { gorename, "-offset", offset, "-to", to_identifier } + local setup = { gorename, '-offset', offset, '-to', to_identifier } vfn.jobstart(setup, { on_stdout = function(_, data, _) @@ -46,10 +41,15 @@ local run = function(to_identifier, ...) end -- local result = vfn.json_decode(data) local result = vim.json.decode(data) - if result.errors ~= nil or result.lines == nil or result["start"] == nil or result["start"] == 0 then - vim.notify("failed to rename" .. vim.inspect(result), vim.log.levels.ERROR) + if + result.errors ~= nil + or result.lines == nil + or result['start'] == nil + or result['start'] == 0 + then + vim.notify('failed to rename' .. vim.inspect(result), vim.log.levels.ERROR) end - vim.notify("renamed to " .. to_identifier, vim.log.levels.DEBUG) + vim.notify('renamed to ' .. to_identifier, vim.log.levels.DEBUG) end, }) end diff --git a/lua/go/utils.lua b/lua/go/utils.lua index 2314ffe..c3d2d70 100644 --- a/lua/go/utils.lua +++ b/lua/go/utils.lua @@ -618,12 +618,10 @@ function util.read_file(path) end function util.restart(cmd_args) - local old_lsp_clients = vim.lsp.get_active_clients() + local old_lsp_client = require('go.lsp').client() local configs = require('lspconfig.configs') - for _, client in pairs(old_lsp_clients) do - if client.name == 'gopls' then - vim.lsp.stop_client(client.id) - end + if old_lsp_client then + vim.lsp.stop_client(old_lsp_client.id) end if configs['gopls'] ~= nil then