|
|
|
@ -2,6 +2,16 @@
|
|
|
|
|
local utils = require("go.utils")
|
|
|
|
|
|
|
|
|
|
local gorename = "gorename"
|
|
|
|
|
|
|
|
|
|
local lsprename = function(new_name)
|
|
|
|
|
if #new_name == 0 or new_name == vim.fn.expand("<cword>") then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
local params = vim.lsp.util.make_position_params()
|
|
|
|
|
params.newName = new_name
|
|
|
|
|
vim.lsp.buf_request(0, "textDocument/rename", params)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local run = function(to_identifier, ...)
|
|
|
|
|
require("go.install").install(gorename)
|
|
|
|
|
local fname = vim.fn.expand("%:p") -- %:p:h ? %:p
|
|
|
|
@ -21,6 +31,12 @@ local run = function(to_identifier, ...)
|
|
|
|
|
to_identifier = to_identifier or vim.fn.input(prompt, old_identifier)
|
|
|
|
|
local byte_offset = vim.fn.wordcount().cursor_bytes
|
|
|
|
|
|
|
|
|
|
local clients = vim.lsp.get_active_clients() or {}
|
|
|
|
|
if #clients > 0 then
|
|
|
|
|
-- TODO check gopls?
|
|
|
|
|
return lsprename(to_identifier)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local offset = string.format("%s:#%i", fname, byte_offset)
|
|
|
|
|
|
|
|
|
|
local setup = {gorename, "-offset", offset, "-to", to_identifier}
|
|
|
|
@ -33,20 +49,19 @@ local run = function(to_identifier, ...)
|
|
|
|
|
-- end
|
|
|
|
|
--
|
|
|
|
|
-- print(vim.inspect(setup))
|
|
|
|
|
local j =
|
|
|
|
|
vim.fn.jobstart(
|
|
|
|
|
setup,
|
|
|
|
|
{
|
|
|
|
|
on_stdout = function(jobid, data, event)
|
|
|
|
|
data = utils.handle_job_data(data)
|
|
|
|
|
if not data then return end
|
|
|
|
|
local result = vim.fn.json_decode(data)
|
|
|
|
|
if result.errors ~= nil or result.lines == nil or result["start"] == nil or result["start"] == 0 then
|
|
|
|
|
print("failed to rename" .. vim.inspect(result))
|
|
|
|
|
end
|
|
|
|
|
print("renamed to " .. to_identifier)
|
|
|
|
|
local j = vim.fn.jobstart(setup, {
|
|
|
|
|
on_stdout = function(jobid, data, event)
|
|
|
|
|
data = utils.handle_job_data(data)
|
|
|
|
|
if not data then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
-- local result = vim.fn.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
|
|
|
|
|
print("failed to rename" .. vim.inspect(result))
|
|
|
|
|
end
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
print("renamed to " .. to_identifier)
|
|
|
|
|
end
|
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
return {run = run}
|
|
|
|
|