From 303b5394750d212ec0c0a93a27091a55ed7bd65d Mon Sep 17 00:00:00 2001 From: Smirnov Olexander Date: Mon, 30 May 2022 23:34:09 +0300 Subject: [PATCH] feat: add gomodifytags support --- lua/gopher/_utils/init.lua | 22 +++++++ lua/gopher/init.lua | 3 + lua/gopher/installer.lua | 4 +- lua/gopher/struct_tags.lua | 116 +++++++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 lua/gopher/_utils/init.lua create mode 100644 lua/gopher/struct_tags.lua diff --git a/lua/gopher/_utils/init.lua b/lua/gopher/_utils/init.lua new file mode 100644 index 0000000..e2c4d07 --- /dev/null +++ b/lua/gopher/_utils/init.lua @@ -0,0 +1,22 @@ +return { + ---@param t table + ---@return boolean + empty = function(t) + if t == nil then + return true + end + + return next(t) == nil + end, + + ---@param s string + ---@return string + rtrim = function(s) + local n = #s + while n > 0 and s:find("^%s", n) do + n = n - 1 + end + + return s:sub(1, n) + end, +} diff --git a/lua/gopher/init.lua b/lua/gopher/init.lua index 3a52b96..93047ed 100644 --- a/lua/gopher/init.lua +++ b/lua/gopher/init.lua @@ -1,5 +1,8 @@ +local tags = require "gopher.struct_tags" local gopher = {} gopher.install_deps = require("gopher.installer").install_all +gopher.tags_add = tags.add +gopher.tags_rm = tags.remove return gopher diff --git a/lua/gopher/installer.lua b/lua/gopher/installer.lua index d70fea6..c1213c5 100644 --- a/lua/gopher/installer.lua +++ b/lua/gopher/installer.lua @@ -1,6 +1,8 @@ local Job = require "plenary.job" local M = { - urls = {}, + urls = { + gomodifytags = "github.com/fatih/gomodifytags", + }, } local function install(pkg) diff --git a/lua/gopher/struct_tags.lua b/lua/gopher/struct_tags.lua new file mode 100644 index 0000000..3e64dc6 --- /dev/null +++ b/lua/gopher/struct_tags.lua @@ -0,0 +1,116 @@ +local Job = require "plenary.job" +local ts_utils = require "gopher._utils.ts" +local utils = require "gopher._utils" +local M = {} + +local function modify(...) + 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))) + if ns == nil or ns == {} then + return + end + + -- stylua: ignore + local cmd_args = { + "-format", "json", + "-file", fpath, + "-w" + } + + -- by struct name of line pos + if ns.name == nil then + local _, csrow, _, _ = unpack(vim.fn.getpos ".") + table.insert(cmd_args, "-line") + table.insert(cmd_args, csrow) + else + table.insert(cmd_args, "-struct") + table.insert(cmd_args, ns.name) + end + + -- set user args for cmd + local arg = { ... } + for _, v in ipairs(arg) do + table.insert(cmd_args, v) + end + + -- set default tag for "clear tags" + if #arg == 1 and arg[1] ~= "-clear-tags" then + table.insert(cmd_args, "json") + end + + -- get result of "gomodifytags" works + local res_data + Job + :new({ + command = "gomodifytags", + args = cmd_args, + on_exit = function(data, retval) + if retval ~= 0 then + print("command exited with code " .. retval) + return + end + + res_data = data:result() + end, + }) + :sync() + + -- decode goted value + local tagged = vim.json.decode(table.concat(res_data)) + if + tagged.errors ~= nil + or tagged.lines == nil + or tagged["start"] == nil + or tagged["start"] == 0 + then + print("failed to set tags " .. vim.inspect(tagged)) + end + + for i, v in ipairs(tagged.lines) do + tagged.lines[i] = utils.rtrim(v) + end + + -- write goted tags + vim.api.nvim_buf_set_lines( + 0, + tagged.start - 1, + tagged.start - 1 + #tagged.lines, + false, + tagged.lines + ) + vim.cmd "write" +end + +---add tags to struct under cursor +---@param ... unknown +function M.add(...) + local arg = { ... } + if #arg == nil or arg == "" then + arg = { "json" } + end + + local cmd_args = { "-add-tags" } + for _, v in ipairs(arg) do + table.insert(cmd_args, v) + end + + modify(unpack(cmd_args)) +end + +---remove tags to struct under cursor +---@param ... unknown +function M.remove(...) + local arg = { ... } + if #arg == nil or arg == "" then + arg = { "json" } + end + + local cmd_args = { "-remove-tags" } + for _, v in ipairs(arg) do + table.insert(cmd_args, v) + end + + modify(unpack(cmd_args)) +end + +return M