allow ui.input/ui.select to be configurable issue #444

pull/450/head
ray-x 2 months ago
parent 8b28f98877
commit 4d3fa34df0

@ -788,6 +788,9 @@ require('go').setup({
signs = true, signs = true,
update_in_insert = false, 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, lsp_document_formatting = true,
-- set to true: use gopls to format -- set to true: use gopls to format
-- false if you want to use other formatter tool(e.g. efm, nulls) -- false if you want to use other formatter tool(e.g. efm, nulls)

@ -50,6 +50,18 @@ _GO_NVIM_CFG = {
update_in_insert = false, update_in_insert = false,
signs = true, 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 -- deprecated setups
-- lsp_diag_hdlr = true, -- hook lsp diag handler -- lsp_diag_hdlr = true, -- hook lsp diag handler
-- lsp_diag_underline = true, -- lsp_diag_underline = true,
@ -58,7 +70,7 @@ _GO_NVIM_CFG = {
-- lsp_diag_signs = true, -- lsp_diag_signs = true,
lsp_inlay_hints = { lsp_inlay_hints = {
enable = true, 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' -- Note: following setup only for for style == 'eol'
-- Only show inlay hints for the current line -- Only show inlay hints for the current line

@ -14,11 +14,8 @@ function M.run_range_code_action(t)
local original_select = vim.ui.select local original_select = vim.ui.select
local original_input = vim.ui.input local original_input = vim.ui.input
local guihua = utils.load_plugin('guihua.lua', 'guihua.gui') vim.ui.select = _GO_NVIM_CFG.go_select()
if guihua then vim.ui.input = _GO_NVIM_CFG.go_input()
vim.ui.select = require('guihua.gui').select
vim.ui.input = require('guihua.input').input
end
vim.lsp.buf.code_action({ vim.lsp.buf.code_action({
context = context, context = context,
range = t.range, range = t.range,
@ -30,14 +27,10 @@ function M.run_range_code_action(t)
end end
function M.run_code_action() function M.run_code_action()
local guihua = utils.load_plugin('guihua.lua', 'guihua.gui')
local original_select = vim.ui.select local original_select = vim.ui.select
local original_input = vim.ui.input local original_input = vim.ui.input
if guihua then vim.ui.select = _GO_NVIM_CFG.go_select()
vim.ui.select = require('guihua.gui').select vim.ui.input = _GO_NVIM_CFG.go_input()
vim.ui.input = require('guihua.input').input
end
log('codeaction') log('codeaction')
if vim.api.nvim_get_mode().mode ~= 'v' then if vim.api.nvim_get_mode().mode ~= 'v' then

@ -24,12 +24,9 @@ function M.setup()
end end
function M.run_action() function M.run_action()
local guihua = utils.load_plugin('guihua.lua', 'guihua.gui')
local original_select = vim.ui.select local original_select = vim.ui.select
if guihua then vim.ui.select = _GO_NVIM_CFG.go_select()
vim.ui.select = require('guihua.gui').select
end
codelens.run() codelens.run()
vim.defer_fn(function() vim.defer_fn(function()
@ -50,20 +47,12 @@ function M.toggle()
end end
function M.refresh() 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 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() vim.lsp.codelens.refresh()
else else
log('refresh codelens') log('refresh codelens')

@ -314,12 +314,8 @@ M.run = function(...)
return require('dap').toggle_breakpoint() return require('dap').toggle_breakpoint()
end end
local guihua = utils.load_plugin('guihua.lua', 'guihua.gui')
local original_select = vim.ui.select local original_select = vim.ui.select
if guihua then vim.ui.select = _GO_NVIM_CFG.go_select()
vim.ui.select = require('guihua.gui').select
end
-- testopts = {"test", "nearest", "file", "stop", "restart"} -- testopts = {"test", "nearest", "file", "stop", "restart"}
log('plugin loaded', mode, optarg) log('plugin loaded', mode, optarg)

@ -9,10 +9,7 @@ function M.run(args)
local input = vim.ui.input local input = vim.ui.input
local guihua = utils.load_plugin("guihua.lua", "guihua.input") vim.ui.input = _GO_NVIM_CFG.go_input()
if guihua then
input = guihua.input
end
local cmd = { "gomvp" } local cmd = { "gomvp" }
local old_mod = require('go.ts.go').get_module_at_pos() local old_mod = require('go.ts.go').get_module_at_pos()
if old_mod == nil then if old_mod == nil then

@ -719,12 +719,9 @@ end
-- GUI to select test? -- GUI to select test?
M.select_tests = function() M.select_tests = function()
local guihua = utils.load_plugin('guihua.lua', 'guihua.gui')
local original_select = vim.ui.select local original_select = vim.ui.select
if guihua then vim.ui.select = _GO_NVIM_CFG.go_select()
vim.ui.select = require('guihua.gui').select
end
vim.defer_fn(function() vim.defer_fn(function()
vim.ui.select = original_select vim.ui.select = original_select

@ -305,7 +305,6 @@ function M.disable_inlay_hints(update, bufnr)
end end
end end
local found = false
-- Sends the request to gopls to get the inlay hints and handle them -- Sends the request to gopls to get the inlay hints and handle them
function M.set_inlay_hints() function M.set_inlay_hints()
if vim.wo.diff then if vim.wo.diff then
@ -313,16 +312,7 @@ function M.set_inlay_hints()
end end
local bufnr = vim.api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
-- check if lsp is ready -- check if lsp is ready
if not found then if not require('go.lsp').client() 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')
return return
end end
local fname = fn.expand('%:p') local fname = fn.expand('%:p')

@ -179,12 +179,11 @@ end
local M = {} local M = {}
function M.client() function M.client()
local clients = vim.lsp.get_active_clients() local clients = vim.lsp.get_active_clients({
for _, cl in pairs(clients) do bufnr = vim.api.nvim_get_current_buf(),
if cl.name == 'gopls' then name = 'gopls',
return cl }) or {}
end return clients[1]
end
end end
function M.config() function M.config()
@ -266,16 +265,7 @@ M.codeaction = function(gopls_cmd, only, hdlr)
params.context = { only = { only } } params.context = { only = { only } }
end end
local bufnr = vim.api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
local clients = vim.lsp.get_active_clients() local gopls = M.client()
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
if gopls == nil then if gopls == nil then
log('gopls not found') log('gopls not found')
return hdlr() return hdlr()
@ -530,14 +520,15 @@ function M.document_symbols(opts)
params.context = { includeDeclaration = true } params.context = { includeDeclaration = true }
params.query = opts.prompt or '' params.query = opts.prompt or ''
local symbols local symbols
vim.lsp.for_each_buffer_client(bufnr, function(client, _, _bufnr) local c = M.client()
if client.name == 'gopls' then if c ~= nil then
symbols = return c.request_sync(
client.request_sync('textDocument/documentSymbol', params, opts.timeout or 1000, _bufnr) 'textDocument/documentSymbol',
return symbols params,
end opts.timeout or 1000,
end) vim.api.nvim_get_current_buf()
return symbols )
end
end end
local change_type = { local change_type = {

@ -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 vfn = vim.fn
local lsprename = function() local lsprename = function()
local guihua = utils.load_plugin("guihua.lua", "guihua.floating")
local input = vim.ui.input local input = vim.ui.input
if guihua then vim.ui.input = _GO_NVIM_CFG.go_input()
vim.ui.input = require('guihua.input').input
end
vim.lsp.buf.rename() vim.lsp.buf.rename()
return vim.defer_fn(function() return vim.defer_fn(function()
vim.ui.input = input vim.ui.input = input
end, 1000) end, 1000)
end end
local run = function(to_identifier, ...) local run = function(to_identifier, ...)
require("go.install").install(gorename) require('go.install').install(gorename)
local fname = vfn.expand("%:p") -- %:p:h ? %:p local fname = vfn.expand('%:p') -- %:p:h ? %:p
local old_identifier = vfn.expand("<cword>") local old_identifier = vfn.expand('<cword>')
local prompt = vfn.printf("GoRename '%s' to (may take a while) :", old_identifier) local prompt = vfn.printf("GoRename '%s' to (may take a while) :", old_identifier)
to_identifier = to_identifier or vfn.input(prompt, old_identifier) to_identifier = to_identifier or vfn.input(prompt, old_identifier)
local byte_offset = vfn.wordcount().cursor_bytes local byte_offset = vfn.wordcount().cursor_bytes
local clients = vim.lsp.get_active_clients() or {} local client = require('go.lsp').client()
if #clients > 0 then if client then
-- TODO check gopls? -- TODO check gopls?
return lsprename() return lsprename()
end 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, { vfn.jobstart(setup, {
on_stdout = function(_, data, _) on_stdout = function(_, data, _)
@ -46,10 +41,15 @@ local run = function(to_identifier, ...)
end end
-- local result = vfn.json_decode(data) -- local result = vfn.json_decode(data)
local result = vim.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 if
vim.notify("failed to rename" .. vim.inspect(result), vim.log.levels.ERROR) 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 end
vim.notify("renamed to " .. to_identifier, vim.log.levels.DEBUG) vim.notify('renamed to ' .. to_identifier, vim.log.levels.DEBUG)
end, end,
}) })
end end

@ -618,12 +618,10 @@ function util.read_file(path)
end end
function util.restart(cmd_args) 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') local configs = require('lspconfig.configs')
for _, client in pairs(old_lsp_clients) do if old_lsp_client then
if client.name == 'gopls' then vim.lsp.stop_client(old_lsp_client.id)
vim.lsp.stop_client(client.id)
end
end end
if configs['gopls'] ~= nil then if configs['gopls'] ~= nil then

Loading…
Cancel
Save