2021-04-23 06:31:00 +00:00
|
|
|
local reftool = {}
|
2021-09-20 09:05:04 +00:00
|
|
|
|
2022-04-24 10:30:41 +00:00
|
|
|
local utils = require("go.utils")
|
2021-09-20 09:05:04 +00:00
|
|
|
local log = utils.log
|
2022-06-01 11:29:13 +00:00
|
|
|
local vfn = vim.fn
|
2021-04-23 06:31:00 +00:00
|
|
|
|
2021-09-20 09:05:04 +00:00
|
|
|
local function insert_result(result)
|
2022-06-01 11:29:13 +00:00
|
|
|
local curpos = vfn.getcurpos()
|
2022-04-24 10:30:41 +00:00
|
|
|
local goto_l = string.format("goto %d", result["start"] + 1)
|
2021-09-20 09:05:04 +00:00
|
|
|
vim.cmd(goto_l)
|
|
|
|
local inserts = result.code
|
2022-04-24 10:30:41 +00:00
|
|
|
inserts = vim.split(inserts, "\n")
|
|
|
|
local change = string.format("normal! %ds%s", result["end"] - result.start, inserts[1])
|
2021-09-20 09:05:04 +00:00
|
|
|
vim.cmd(change)
|
2022-04-24 10:30:41 +00:00
|
|
|
vim.cmd("startinsert!")
|
2021-09-20 09:05:04 +00:00
|
|
|
log(change)
|
|
|
|
local curline = curpos[2]
|
|
|
|
for i = 2, #inserts do
|
|
|
|
log("append ", curline, inserts[i])
|
2022-06-01 11:29:13 +00:00
|
|
|
vfn.append(curline, inserts[i])
|
2021-09-20 09:05:04 +00:00
|
|
|
curline = curline + 1
|
2021-04-23 06:31:00 +00:00
|
|
|
end
|
2021-09-20 09:05:04 +00:00
|
|
|
|
2022-04-24 10:30:41 +00:00
|
|
|
vim.cmd("stopinsert!")
|
|
|
|
vim.cmd("write")
|
2021-09-20 09:05:04 +00:00
|
|
|
-- format(#inserts, curpos)
|
2022-06-01 11:29:13 +00:00
|
|
|
vfn.setpos(".", curpos)
|
2022-07-31 23:04:28 +00:00
|
|
|
require('go.format').gofmt()
|
2021-04-23 06:31:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- can only be fillstruct and fillswitch
|
|
|
|
local function fill(cmd)
|
2022-04-24 10:30:41 +00:00
|
|
|
if cmd ~= "fillstruct" and cmd ~= "fillswitch" then
|
2021-09-20 09:05:04 +00:00
|
|
|
log(cmd, "not found")
|
2022-04-24 10:30:41 +00:00
|
|
|
error("cmd not supported by go.nvim", cmd)
|
2021-09-20 09:05:04 +00:00
|
|
|
end
|
|
|
|
require("go.install").install(cmd)
|
2021-04-23 06:31:00 +00:00
|
|
|
|
2021-09-30 12:23:36 +00:00
|
|
|
log(cmd)
|
2022-06-01 11:29:13 +00:00
|
|
|
local file = vfn.expand("%:p")
|
|
|
|
local line = vfn.line(".")
|
|
|
|
-- local run = string.format("%s -file=%s -line=%d 2>/dev/null", cmd, file, line)
|
2022-04-24 10:30:41 +00:00
|
|
|
local farg = string.format("-file=%s", file)
|
|
|
|
local larg = string.format("-line=%d", line)
|
|
|
|
local args = { cmd, farg, larg, "2>/dev/null" }
|
2021-09-20 09:05:04 +00:00
|
|
|
log(args)
|
2022-06-01 11:29:13 +00:00
|
|
|
vfn.jobstart(args, {
|
|
|
|
on_stdout = function(_, str, _)
|
2021-09-20 09:05:04 +00:00
|
|
|
log(str)
|
|
|
|
if #str < 2 then
|
2022-04-24 10:30:41 +00:00
|
|
|
log("reftools", cmd, "finished with no result")
|
2021-09-20 09:05:04 +00:00
|
|
|
return
|
|
|
|
end
|
2022-06-01 11:29:13 +00:00
|
|
|
local json = vfn.json_decode(str)
|
2021-09-20 09:05:04 +00:00
|
|
|
if #json == 0 then
|
2023-02-12 20:55:00 +00:00
|
|
|
vim.notify("reftools " .. cmd .. " finished with no result", vim.log.levels.DEBUG)
|
2021-04-23 06:31:00 +00:00
|
|
|
end
|
2021-09-20 09:05:04 +00:00
|
|
|
|
|
|
|
local result = json[1]
|
|
|
|
insert_result(result)
|
2022-04-24 10:30:41 +00:00
|
|
|
end,
|
2021-09-20 09:05:04 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
local function gopls_fillstruct(timeout_ms)
|
2022-04-24 10:30:41 +00:00
|
|
|
log("fill struct with gopls")
|
|
|
|
local codeaction = require("go.lsp").codeaction
|
|
|
|
codeaction("fill_struct", "refactor.rewrite", timeout_ms)
|
2021-09-20 09:05:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function reftool.fillstruct()
|
2022-04-24 10:30:41 +00:00
|
|
|
if _GO_NVIM_CFG.fillstruct == "gopls" then
|
2021-09-20 09:05:04 +00:00
|
|
|
gopls_fillstruct(1000)
|
|
|
|
else
|
2022-04-24 10:30:41 +00:00
|
|
|
log("fillstruct")
|
|
|
|
fill("fillstruct")
|
2021-04-23 06:31:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-30 12:23:36 +00:00
|
|
|
reftool.fillswitch = function()
|
2022-04-24 10:30:41 +00:00
|
|
|
fill("fillswitch")
|
2021-09-30 12:23:36 +00:00
|
|
|
end
|
2021-04-23 06:31:00 +00:00
|
|
|
|
|
|
|
return reftool
|