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

Configure Feed

Select the types of activity you want to include in your feed.

feat(struct_tags): add range support (#117)

* feat(struct_tags): add range support

* refactor: use `start`, and `end_` naming for ranges

authored by

Oleksandr Smirnov and committed by
Oleksandr Smirnov
b6d3815f 8c879529

+129 -26
+4 -4
lua/gopher/_utils/ts.lua
··· 65 65 66 66 ---@class gopher.TsResult 67 67 ---@field name string 68 - ---@field start_line integer 69 - ---@field end_line integer 68 + ---@field start integer 69 + ---@field end_ integer 70 70 ---@field is_varstruct boolean 71 71 72 72 ---@param bufnr integer ··· 95 95 assert(res.name ~= nil, "No capture name found") 96 96 97 97 local start_row, _, end_row, _ = parent_node:range() 98 - res["start_line"] = start_row + 1 99 - res["end_line"] = end_row + 1 98 + res["start"] = start_row + 1 99 + res["end_"] = end_row + 1 100 100 101 101 return res 102 102 end
+29 -15
lua/gopher/struct_tags.lua
··· 37 37 local log = require "gopher._utils.log" 38 38 local struct_tags = {} 39 39 40 + ---@dochide 41 + ---@class gopher.StructTagInput 42 + ---@field tags string[] User provided tags 43 + ---@field range? gopher.StructTagRange (optional) 44 + 45 + ---@dochide 46 + ---@class gopher.StructTagRange 47 + ---@field start number 48 + ---@field end_ number 49 + 40 50 ---@param fpath string 41 51 ---@param bufnr integer 52 + ---@param range? gopher.StructTagRange 42 53 ---@param user_args string[] 43 54 ---@dochide 44 - local function handle_tags(fpath, bufnr, user_args) 55 + local function handle_tags(fpath, bufnr, range, user_args) 45 56 local st = ts.get_struct_under_cursor(bufnr) 46 57 47 58 -- stylua: ignore ··· 53 64 "-w", 54 65 } 55 66 56 - if st.is_varstruct then 67 + -- `-struct` and `-line` cannot be combined, setting them separately 68 + if range or st.is_varstruct then 57 69 table.insert(cmd, "-line") 58 - table.insert(cmd, string.format("%d,%d", st.start_line, st.end_line)) 70 + table.insert(cmd, string.format("%d,%d", (range or st).start, (range or st).end_)) 59 71 else 60 72 table.insert(cmd, "-struct") 61 73 table.insert(cmd, st.name) ··· 93 105 ---@param args string[] 94 106 ---@return string 95 107 ---@dochide 96 - local function handler_user_args(args) 108 + local function handler_user_tags(args) 97 109 if #args == 0 then 98 110 return c.gotag.default_tag 99 111 end ··· 102 114 103 115 -- Adds tags to a struct under the cursor 104 116 -- See |gopher.nvim-struct-tags| 105 - ---@param ... string Tags to add to the struct fields. If not provided, it will use [config.gotag.default_tag] 117 + ---@param opts gopher.StructTagInput 106 118 ---@dochide 107 - function struct_tags.add(...) 108 - local args = { ... } 119 + function struct_tags.add(opts) 120 + log.debug("adding tags", opts) 121 + 109 122 local fpath = vim.fn.expand "%" 110 123 local bufnr = vim.api.nvim_get_current_buf() 111 124 112 - local user_tags = handler_user_args(args) 113 - handle_tags(fpath, bufnr, { "-add-tags", user_tags }) 125 + local user_tags = handler_user_tags(opts.tags) 126 + handle_tags(fpath, bufnr, opts.range, { "-add-tags", user_tags }) 114 127 end 115 128 116 129 -- Removes tags from a struct under the cursor 117 130 -- See `:h gopher.nvim-struct-tags` 118 131 ---@dochide 119 - ---@param ... string Tags to add to the struct fields. If not provided, it will use [config.gotag.default_tag] 120 - function struct_tags.remove(...) 121 - local args = { ... } 132 + ---@param opts gopher.StructTagInput 133 + function struct_tags.remove(opts) 134 + log.debug("removing tags", opts) 135 + 122 136 local fpath = vim.fn.expand "%" 123 137 local bufnr = vim.api.nvim_get_current_buf() 124 138 125 - local user_tags = handler_user_args(args) 126 - handle_tags(fpath, bufnr, { "-remove-tags", user_tags }) 139 + local user_tags = handler_user_tags(opts.tags) 140 + handle_tags(fpath, bufnr, opts.range, { "-remove-tags", user_tags }) 127 141 end 128 142 129 143 -- Removes all tags from a struct under the cursor ··· 132 146 function struct_tags.clear() 133 147 local fpath = vim.fn.expand "%" 134 148 local bufnr = vim.api.nvim_get_current_buf() 135 - handle_tags(fpath, bufnr, { "-clear-tags" }) 149 + handle_tags(fpath, bufnr, nil, { "-clear-tags" }) 136 150 end 137 151 138 152 return struct_tags
+22 -7
plugin/gopher.lua
··· 11 11 ---@param name string 12 12 ---@param fn fun(args: table) 13 13 ---@param nargs? number|"*"|"?" 14 + ---@param range? boolean 14 15 ---@private 15 - local function cmd(name, fn, nargs) 16 - nargs = nargs or 0 17 - vim.api.nvim_create_user_command(name, fn, { nargs = nargs }) 16 + local function cmd(name, fn, nargs, range) 17 + vim.api.nvim_create_user_command(name, fn, { 18 + nargs = nargs or 0, 19 + range = range or false, 20 + }) 18 21 end 19 22 20 23 cmd("GopherLog", function() ··· 44 47 45 48 -- :GoTag 46 49 cmd("GoTagAdd", function(opts) 47 - require("gopher").tags.add(unpack(opts.fargs)) 48 - end, "*") 50 + require("gopher").tags.add { 51 + tags = opts.fargs, 52 + range = (opts.count ~= -1) and { 53 + start = opts.line1, 54 + end_ = opts.line2, 55 + } or nil, 56 + } 57 + end, "*", true) 49 58 50 59 cmd("GoTagRm", function(opts) 51 - require("gopher").tags.rm(unpack(opts.fargs)) 52 - end, "*") 60 + require("gopher").tags.rm { 61 + tags = opts.fargs, 62 + range = (opts.count ~= -1) and { 63 + start = opts.line1, 64 + end_ = opts.line2, 65 + } or nil, 66 + } 67 + end, "*", true) 53 68 54 69 cmd("GoTagClear", function() 55 70 require("gopher").tags.clear()
+14
spec/fixtures/tags/add_range_input.go
··· 1 + package main 2 + 3 + type Test struct { 4 + ID int 5 + Name string 6 + Num int64 7 + Cost int 8 + Thingy []string 9 + Testing int 10 + Another struct { 11 + First int 12 + Second string 13 + } 14 + }
+14
spec/fixtures/tags/add_range_output.go
··· 1 + package main 2 + 3 + type Test struct { 4 + ID int 5 + Name string `gopher:"name"` 6 + Num int64 `gopher:"num"` 7 + Cost int `gopher:"cost"` 8 + Thingy []string 9 + Testing int 10 + Another struct { 11 + First int 12 + Second string 13 + } 14 + }
+14
spec/fixtures/tags/remove_range_input.go
··· 1 + package main 2 + 3 + type Test struct { 4 + ID int `asdf:"id"` 5 + Name string `asdf:"name"` 6 + Num int64 `asdf:"num"` 7 + Cost int `asdf:"cost"` 8 + Thingy []string `asdf:"thingy"` 9 + Testing int `asdf:"testing"` 10 + Another struct { 11 + First int `asdf:"first"` 12 + Second string `asdf:"second"` 13 + } `asdf:"another"` 14 + }
+14
spec/fixtures/tags/remove_range_output.go
··· 1 + package main 2 + 3 + type Test struct { 4 + ID int `asdf:"id"` 5 + Name string `asdf:"name"` 6 + Num int64 7 + Cost int 8 + Thingy []string 9 + Testing int `asdf:"testing"` 10 + Another struct { 11 + First int `asdf:"first"` 12 + Second string `asdf:"second"` 13 + } `asdf:"another"` 14 + }
+18
spec/integration/struct_tags_test.lua
··· 78 78 t.cleanup(rs) 79 79 end 80 80 81 + struct_tags["should add tag with range"] = function() 82 + local rs = t.setup_test("tags/add_range", child, { 5, 1 }) 83 + child.cmd ".,+2GoTagAdd gopher" 84 + child.cmd "write" 85 + 86 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 87 + t.cleanup(rs) 88 + end 89 + 90 + struct_tags["should remove tag with range"] = function() 91 + local rs = t.setup_test("tags/remove_range", child, { 6, 1 }) 92 + child.cmd ".,+2GoTagRm asdf" 93 + child.cmd "write" 94 + 95 + t.eq(t.readfile(rs.tmp), rs.fixtures.output) 96 + t.cleanup(rs) 97 + end 98 + 81 99 return T