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
|
2021-11-11 23:30:45 +00:00
|
|
|
|
2022-05-27 17:20:33 +00:00
|
|
|
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
|
2021-11-11 23:30:45 +00:00
|
|
|
end
|
2022-05-27 17:20:33 +00:00
|
|
|
vim.lsp.buf.rename()
|
|
|
|
return vim.defer_fn(function()
|
|
|
|
vim.ui.input = input
|
|
|
|
end, 1000)
|
|
|
|
|
2021-11-11 23:30:45 +00:00
|
|
|
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
|
|
|
|
2021-11-11 23:30:45 +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()
|
2021-11-11 23:30:45 +00:00
|
|
|
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, _)
|
2021-11-11 23:30:45 +00:00
|
|
|
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)
|
2021-11-11 23:30:45 +00:00
|
|
|
local result = vim.json.decode(data)
|
|
|
|
if result.errors ~= nil or result.lines == nil or result["start"] == nil or result["start"] == 0 then
|
2023-02-12 20:55:00 +00:00
|
|
|
vim.notify("failed to rename" .. vim.inspect(result), vim.log.levels.ERROR)
|
2021-04-21 11:11:44 +00:00
|
|
|
end
|
2023-02-12 20:55:00 +00:00
|
|
|
vim.notify("renamed to " .. to_identifier, vim.log.levels.DEBUG)
|
2022-04-18 14:22:08 +00:00
|
|
|
end,
|
2021-11-11 23:30:45 +00:00
|
|
|
})
|
2021-04-21 11:11:44 +00:00
|
|
|
end
|
2022-05-27 17:20:33 +00:00
|
|
|
return { run = run, lsprename = lsprename }
|