From 7ca1c039fe0be416869954745285d6465040b193 Mon Sep 17 00:00:00 2001 From: Smirnov Oleksandr Date: Tue, 7 Feb 2023 16:34:52 +0200 Subject: [PATCH] refactor(struct_tags): renaming, change way of error reporting --- lua/gopher/struct_tags.lua | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/lua/gopher/struct_tags.lua b/lua/gopher/struct_tags.lua index a0844a0..1deb9fa 100644 --- a/lua/gopher/struct_tags.lua +++ b/lua/gopher/struct_tags.lua @@ -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 ts_utils = require "gopher._utils.ts" - 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 + ---@diagnostic disable-next-line: param-type-mismatch local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) if ns == nil then return @@ -15,7 +14,7 @@ local function modify(...) -- stylua: ignore local cmd_args = { "-format", "json", - "-file", fpath, + "-file", vim.fn.expand "%", "-w" } @@ -47,7 +46,7 @@ local function modify(...) args = cmd_args, on_exit = function(data, retval) if retval ~= 0 then - u.notify("command 'gomodifytags " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") + error("gomodifytags failed: " .. retval) return end @@ -57,8 +56,12 @@ local function modify(...) -- decode goted value 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 - u.notify("failed to set tags " .. vim.inspect(tagged), "error") + error("failed to set tags " .. vim.inspect(tagged)) end for i, v in ipairs(tagged.lines) do @@ -72,7 +75,7 @@ end ---add tags to struct under cursor ---@param ... unknown -function M.add(...) +function struct_tags.add(...) local arg = { ... } if #arg == nil or arg == "" then arg = { "json" } @@ -88,7 +91,7 @@ end ---remove tags to struct under cursor ---@param ... unknown -function M.remove(...) +function struct_tags.remove(...) local arg = { ... } if #arg == nil or arg == "" then arg = { "json" } @@ -102,4 +105,4 @@ function M.remove(...) modify(unpack(cmd_args)) end -return M +return struct_tags