feat: add gomodifytags support
This commit is contained in:
parent
ca1bc4bd06
commit
303b539475
4 changed files with 144 additions and 1 deletions
22
lua/gopher/_utils/init.lua
Normal file
22
lua/gopher/_utils/init.lua
Normal file
|
|
@ -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,
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
|
local tags = require "gopher.struct_tags"
|
||||||
local gopher = {}
|
local gopher = {}
|
||||||
|
|
||||||
gopher.install_deps = require("gopher.installer").install_all
|
gopher.install_deps = require("gopher.installer").install_all
|
||||||
|
gopher.tags_add = tags.add
|
||||||
|
gopher.tags_rm = tags.remove
|
||||||
|
|
||||||
return gopher
|
return gopher
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
local Job = require "plenary.job"
|
local Job = require "plenary.job"
|
||||||
local M = {
|
local M = {
|
||||||
urls = {},
|
urls = {
|
||||||
|
gomodifytags = "github.com/fatih/gomodifytags",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
local function install(pkg)
|
local function install(pkg)
|
||||||
|
|
|
||||||
116
lua/gopher/struct_tags.lua
Normal file
116
lua/gopher/struct_tags.lua
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue