[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang

feat: add gomodifytags support

Changed files
+144 -1
lua
+22
lua/gopher/_utils/init.lua
···
··· 1 + return { 2 + ---@param t table 3 + ---@return boolean 4 + empty = function(t) 5 + if t == nil then 6 + return true 7 + end 8 + 9 + return next(t) == nil 10 + end, 11 + 12 + ---@param s string 13 + ---@return string 14 + rtrim = function(s) 15 + local n = #s 16 + while n > 0 and s:find("^%s", n) do 17 + n = n - 1 18 + end 19 + 20 + return s:sub(1, n) 21 + end, 22 + }
+3
lua/gopher/init.lua
··· 1 local gopher = {} 2 3 gopher.install_deps = require("gopher.installer").install_all 4 5 return gopher
··· 1 + local tags = require "gopher.struct_tags" 2 local gopher = {} 3 4 gopher.install_deps = require("gopher.installer").install_all 5 + gopher.tags_add = tags.add 6 + gopher.tags_rm = tags.remove 7 8 return gopher
+3 -1
lua/gopher/installer.lua
··· 1 local Job = require "plenary.job" 2 local M = { 3 - urls = {}, 4 } 5 6 local function install(pkg)
··· 1 local Job = require "plenary.job" 2 local M = { 3 + urls = { 4 + gomodifytags = "github.com/fatih/gomodifytags", 5 + }, 6 } 7 8 local function install(pkg)
+116
lua/gopher/struct_tags.lua
···
··· 1 + local Job = require "plenary.job" 2 + local ts_utils = require "gopher._utils.ts" 3 + local utils = require "gopher._utils" 4 + local M = {} 5 + 6 + local function modify(...) 7 + local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter 8 + local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) 9 + if ns == nil or ns == {} then 10 + return 11 + end 12 + 13 + -- stylua: ignore 14 + local cmd_args = { 15 + "-format", "json", 16 + "-file", fpath, 17 + "-w" 18 + } 19 + 20 + -- by struct name of line pos 21 + if ns.name == nil then 22 + local _, csrow, _, _ = unpack(vim.fn.getpos ".") 23 + table.insert(cmd_args, "-line") 24 + table.insert(cmd_args, csrow) 25 + else 26 + table.insert(cmd_args, "-struct") 27 + table.insert(cmd_args, ns.name) 28 + end 29 + 30 + -- set user args for cmd 31 + local arg = { ... } 32 + for _, v in ipairs(arg) do 33 + table.insert(cmd_args, v) 34 + end 35 + 36 + -- set default tag for "clear tags" 37 + if #arg == 1 and arg[1] ~= "-clear-tags" then 38 + table.insert(cmd_args, "json") 39 + end 40 + 41 + -- get result of "gomodifytags" works 42 + local res_data 43 + Job 44 + :new({ 45 + command = "gomodifytags", 46 + args = cmd_args, 47 + on_exit = function(data, retval) 48 + if retval ~= 0 then 49 + print("command exited with code " .. retval) 50 + return 51 + end 52 + 53 + res_data = data:result() 54 + end, 55 + }) 56 + :sync() 57 + 58 + -- decode goted value 59 + local tagged = vim.json.decode(table.concat(res_data)) 60 + if 61 + tagged.errors ~= nil 62 + or tagged.lines == nil 63 + or tagged["start"] == nil 64 + or tagged["start"] == 0 65 + then 66 + print("failed to set tags " .. vim.inspect(tagged)) 67 + end 68 + 69 + for i, v in ipairs(tagged.lines) do 70 + tagged.lines[i] = utils.rtrim(v) 71 + end 72 + 73 + -- write goted tags 74 + vim.api.nvim_buf_set_lines( 75 + 0, 76 + tagged.start - 1, 77 + tagged.start - 1 + #tagged.lines, 78 + false, 79 + tagged.lines 80 + ) 81 + vim.cmd "write" 82 + end 83 + 84 + ---add tags to struct under cursor 85 + ---@param ... unknown 86 + function M.add(...) 87 + local arg = { ... } 88 + if #arg == nil or arg == "" then 89 + arg = { "json" } 90 + end 91 + 92 + local cmd_args = { "-add-tags" } 93 + for _, v in ipairs(arg) do 94 + table.insert(cmd_args, v) 95 + end 96 + 97 + modify(unpack(cmd_args)) 98 + end 99 + 100 + ---remove tags to struct under cursor 101 + ---@param ... unknown 102 + function M.remove(...) 103 + local arg = { ... } 104 + if #arg == nil or arg == "" then 105 + arg = { "json" } 106 + end 107 + 108 + local cmd_args = { "-remove-tags" } 109 + for _, v in ipairs(arg) do 110 + table.insert(cmd_args, v) 111 + end 112 + 113 + modify(unpack(cmd_args)) 114 + end 115 + 116 + return M