refactor(struct_tags): finally my hands got to this

This commit is contained in:
Oleksandr Smirnov 2025-03-19 15:35:46 +02:00
parent e9f2eef5e7
commit 59c150259d
No known key found for this signature in database

View file

@ -26,90 +26,87 @@
--- } --- }
--- < --- <
local ts_utils = require "gopher._utils.ts" local ts = require "gopher._utils.ts"
local r = require "gopher._utils.runner" local r = require "gopher._utils.runner"
local c = require "gopher.config" local c = require "gopher.config"
local log = require "gopher._utils.log"
local struct_tags = {} local struct_tags = {}
local function modify(...) local function handle_tags(fpath, bufnr, user_args)
local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter local st = ts.get_struct_under_cursor(bufnr)
local bufnr = vim.api.nvim_get_current_buf()
local struct = ts_utils.get_struct_under_cursor(bufnr)
-- set user args for cmd -- stylua: ignore
local cmd_args = {} local cmd = {
local arg = { ... }
for _, v in ipairs(arg) do
table.insert(cmd_args, v)
end
local rs = r.sync {
c.commands.gomodifytags, c.commands.gomodifytags,
"-transform", "-transform", c.gotag.transform,
c.gotag.transform, "-format", "json",
"-format", "-struct", st.name,
"json", "-file", fpath,
"-struct",
struct.name,
"-w", "-w",
"-file",
fpath,
unpack(cmd_args),
} }
if rs.code ~= 0 then for _, v in ipairs(user_args) do
error("failed to set tags " .. rs.stderr) table.insert(cmd, v)
end end
-- decode value local rs = r.sync(cmd)
local tagged = vim.json.decode(rs.stdout) if rs.code ~= 0 then
if log.error("tags: failed to set tags " .. rs.stderr)
tagged.errors ~= nil error("failed to set tags " .. rs.stdout)
or tagged.lines == nil end
or tagged["start"] == nil
or tagged["start"] == 0 local res = vim.json.decode(rs.stdout)
then
error("failed to set tags " .. vim.inspect(tagged)) if res["errors"] then
log.error("tags: got an error " .. vim.inspect(res))
error("failed to set tags " .. vim.inspect(res["errors"]))
end end
vim.api.nvim_buf_set_lines( vim.api.nvim_buf_set_lines(
0, bufnr,
tagged.start - 1, res["start"] - 1,
tagged.start - 1 + #tagged.lines, res["start"] - 1 + #res["lines"],
false, false,
tagged.lines res["lines"]
) )
vim.cmd "write" vim.cmd "write"
end end
local function handler_user_args(args)
local res = {}
if #args == 0 then
table.insert(res, c.gotag.default_tag)
else
for _, v in ipairs(args) do
table.insert(res, v)
end
end
return res
end
-- add tags to struct under cursor -- add tags to struct under cursor
function struct_tags.add(...) function struct_tags.add(...)
local user_tags = { ... } local args = { ... }
if #user_tags == 0 then local fpath = vim.fn.expand "%"
user_tags = { c.gotag.default_tag } local bufnr = vim.api.nvim_get_current_buf()
end
local cmd_args = { "-add-tags" } local user_args = handler_user_args(args)
for _, v in ipairs(user_tags) do table.insert(user_args, 1, "-add-tags")
table.insert(cmd_args, v)
end
modify(unpack(cmd_args)) handle_tags(fpath, bufnr, user_args)
end end
-- remove tags to struct under cursor -- remove tags to struct under cursor
function struct_tags.remove(...) function struct_tags.remove(...)
local user_tags = { ... } local args = { ... }
if #user_tags == 0 then local fpath = vim.fn.expand "%"
user_tags = { c.gotag.default_tag } local bufnr = vim.api.nvim_get_current_buf()
end
local cmd_args = { "-remove-tags" } local user_args = handler_user_args(args)
for _, v in ipairs(user_tags) do table.insert(user_args, 1, "-remove-tags")
table.insert(cmd_args, v)
end
modify(unpack(cmd_args)) handle_tags(fpath, bufnr, user_args)
end end
return struct_tags return struct_tags