go.nvim/lua/go/rename.lua

57 lines
1.6 KiB
Lua
Raw Normal View History

2021-07-07 05:56:22 +00:00
local utils = require("go.utils")
2021-04-21 11:11:44 +00:00
local gorename = "gorename"
2022-06-01 11:29:13 +00:00
local vfn = vim.fn
local lsprename = function()
local guihua = utils.load_plugin("guihua.lua", "guihua.floating")
local input = vim.ui.input
if guihua then
2022-06-16 04:10:49 +00:00
vim.ui.input = require('guihua.input').input
end
vim.lsp.buf.rename()
return vim.defer_fn(function()
vim.ui.input = input
end, 1000)
end
2021-04-21 11:11:44 +00:00
local run = function(to_identifier, ...)
require("go.install").install(gorename)
2022-06-01 11:29:13 +00:00
local fname = vfn.expand("%:p") -- %:p:h ? %:p
2021-04-21 11:11:44 +00:00
2022-06-01 11:29:13 +00:00
local old_identifier = vfn.expand("<cword>")
2021-04-21 11:11:44 +00:00
2022-06-01 11:29:13 +00:00
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
2021-04-21 11:11:44 +00:00
local clients = vim.lsp.get_active_clients() or {}
if #clients > 0 then
-- TODO check gopls?
2022-06-01 11:29:13 +00:00
return lsprename()
end
2021-04-21 11:11:44 +00:00
local offset = string.format("%s:#%i", fname, byte_offset)
2022-04-18 14:22:08 +00:00
local setup = { gorename, "-offset", offset, "-to", to_identifier }
2021-04-21 11:11:44 +00:00
2022-06-01 11:29:13 +00:00
vfn.jobstart(setup, {
on_stdout = function(_, data, _)
data = utils.handle_job_data(data)
if not data then
return
end
2022-06-01 11:29:13 +00:00
-- 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)
2021-04-21 11:11:44 +00:00
end
vim.notify("renamed to " .. to_identifier, vim.log.levels.DEBUG)
2022-04-18 14:22:08 +00:00
end,
})
2021-04-21 11:11:44 +00:00
end
return { run = run, lsprename = lsprename }