refactor(struct_tags): renaming, change way of error reporting

This commit is contained in:
Smirnov Oleksandr 2023-02-07 16:34:52 +02:00
parent 298fc24cab
commit 7ca1c039fe

View file

@ -1,12 +1,11 @@
local M = {} local ts_utils = require "gopher._utils.ts"
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
local struct_tags = {}
local function modify(...) local function modify(...)
local ts_utils = require "gopher._utils.ts" ---@diagnostic disable-next-line: param-type-mismatch
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil then if ns == nil then
return return
@ -15,7 +14,7 @@ local function modify(...)
-- stylua: ignore -- stylua: ignore
local cmd_args = { local cmd_args = {
"-format", "json", "-format", "json",
"-file", fpath, "-file", vim.fn.expand "%",
"-w" "-w"
} }
@ -47,7 +46,7 @@ local function modify(...)
args = cmd_args, args = cmd_args,
on_exit = function(data, retval) on_exit = function(data, retval)
if retval ~= 0 then if retval ~= 0 then
u.notify("command 'gomodifytags " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") error("gomodifytags failed: " .. retval)
return return
end end
@ -57,8 +56,12 @@ local function modify(...)
-- decode goted value -- decode goted value
local tagged = vim.json.decode(table.concat(res_data)) local tagged = vim.json.decode(table.concat(res_data))
if tagged == nil then
error "faild to get tags"
end
if tagged.errors ~= nil or tagged.lines == nil or tagged["start"] == nil or tagged["start"] == 0 then if tagged.errors ~= nil or tagged.lines == nil or tagged["start"] == nil or tagged["start"] == 0 then
u.notify("failed to set tags " .. vim.inspect(tagged), "error") error("failed to set tags " .. vim.inspect(tagged))
end end
for i, v in ipairs(tagged.lines) do for i, v in ipairs(tagged.lines) do
@ -72,7 +75,7 @@ end
---add tags to struct under cursor ---add tags to struct under cursor
---@param ... unknown ---@param ... unknown
function M.add(...) function struct_tags.add(...)
local arg = { ... } local arg = { ... }
if #arg == nil or arg == "" then if #arg == nil or arg == "" then
arg = { "json" } arg = { "json" }
@ -88,7 +91,7 @@ end
---remove tags to struct under cursor ---remove tags to struct under cursor
---@param ... unknown ---@param ... unknown
function M.remove(...) function struct_tags.remove(...)
local arg = { ... } local arg = { ... }
if #arg == nil or arg == "" then if #arg == nil or arg == "" then
arg = { "json" } arg = { "json" }
@ -102,4 +105,4 @@ function M.remove(...)
modify(unpack(cmd_args)) modify(unpack(cmd_args))
end end
return M return struct_tags