feat(struct_tags): add options support
This commit is contained in:
parent
0de1892ca9
commit
92625cc6e3
6 changed files with 158 additions and 14 deletions
44
lua/gopher/_utils/struct_tags.lua
Normal file
44
lua/gopher/_utils/struct_tags.lua
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
---@param option string
|
||||
local function option_to_tag(option)
|
||||
return option:match "^(.-)="
|
||||
end
|
||||
|
||||
---@param args string[]
|
||||
local function unwrap_if_needed(args)
|
||||
if #args == 1 then
|
||||
return vim.split(args[1], ",")
|
||||
end
|
||||
return args
|
||||
end
|
||||
|
||||
local struct_tags = {}
|
||||
|
||||
---@class gopher.StructTagsArgs
|
||||
---@field tags string
|
||||
---@field options string
|
||||
|
||||
---@param args string[]
|
||||
---@return gopher.StructTagsArgs
|
||||
function struct_tags.parse_args(args)
|
||||
args = unwrap_if_needed(args)
|
||||
|
||||
local tags, options = {}, {}
|
||||
for _, v in pairs(args) do
|
||||
if string.find(v, "=") then
|
||||
table.insert(options, v)
|
||||
table.insert(tags, option_to_tag(v))
|
||||
else
|
||||
table.insert(tags, v)
|
||||
end
|
||||
end
|
||||
|
||||
-- todo: it's incompatible with vim lower 0.12
|
||||
tags = vim.list.unique(tags)
|
||||
options = vim.list.unique(options)
|
||||
return {
|
||||
tags = table.concat(tags, ","),
|
||||
options = table.concat(options, ","),
|
||||
}
|
||||
end
|
||||
|
||||
return struct_tags
|
||||
|
|
@ -35,6 +35,7 @@ local r = require "gopher._utils.runner"
|
|||
local c = require "gopher.config"
|
||||
local u = require "gopher._utils"
|
||||
local log = require "gopher._utils.log"
|
||||
local st = require "gopher._utils.struct_tags"
|
||||
local struct_tags = {}
|
||||
|
||||
---@dochide
|
||||
|
|
@ -102,16 +103,6 @@ local function handle_tags(fpath, bufnr, range, user_args)
|
|||
)
|
||||
end
|
||||
|
||||
---@param args string[]
|
||||
---@return string
|
||||
---@dochide
|
||||
local function handler_user_tags(args)
|
||||
if #args == 0 then
|
||||
return c.gotag.default_tag
|
||||
end
|
||||
return table.concat(args, ",")
|
||||
end
|
||||
|
||||
-- Adds tags to a struct under the cursor
|
||||
-- See |gopher.nvim-struct-tags|
|
||||
---@param opts gopher.StructTagInput
|
||||
|
|
@ -122,8 +113,13 @@ function struct_tags.add(opts)
|
|||
local fpath = vim.fn.expand "%"
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
|
||||
local user_tags = handler_user_tags(opts.tags)
|
||||
handle_tags(fpath, bufnr, opts.range, { "-add-tags", user_tags })
|
||||
local user_args = st.parse_args(opts.tags)
|
||||
handle_tags(fpath, bufnr, opts.range, {
|
||||
"-add-tags",
|
||||
(user_args.tags ~= "") and user_args.tags or c.gotag.default_tag,
|
||||
(#user_args.options ~= 0) and "-add-options" or nil,
|
||||
(#user_args.options ~= 0) and user_args.options or nil,
|
||||
})
|
||||
end
|
||||
|
||||
-- Removes tags from a struct under the cursor
|
||||
|
|
@ -136,8 +132,13 @@ function struct_tags.remove(opts)
|
|||
local fpath = vim.fn.expand "%"
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
|
||||
local user_tags = handler_user_tags(opts.tags)
|
||||
handle_tags(fpath, bufnr, opts.range, { "-remove-tags", user_tags })
|
||||
local user_args = st.parse_args(opts.tags)
|
||||
handle_tags(fpath, bufnr, opts.range, {
|
||||
"-remove-tags",
|
||||
(user_args.tags ~= "") and user_args.tags or c.gotag.default_tag,
|
||||
(#user_args.options ~= 0) and "-remove-options" or nil,
|
||||
(#user_args.options ~= 0) and user_args.options or nil,
|
||||
})
|
||||
end
|
||||
|
||||
-- Removes all tags from a struct under the cursor
|
||||
|
|
|
|||
11
spec/fixtures/tags/with_option_input.go
vendored
Normal file
11
spec/fixtures/tags/with_option_input.go
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package main
|
||||
|
||||
type Test struct {
|
||||
ID int
|
||||
Name string
|
||||
Num int64
|
||||
Another struct {
|
||||
First int
|
||||
Second string
|
||||
}
|
||||
}
|
||||
11
spec/fixtures/tags/with_option_output.go
vendored
Normal file
11
spec/fixtures/tags/with_option_output.go
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package main
|
||||
|
||||
type Test struct {
|
||||
ID int `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Num int64 `json:"num,omitempty"`
|
||||
Another struct {
|
||||
First int `json:"first,omitempty"`
|
||||
Second string `json:"second,omitempty"`
|
||||
} `json:"another,omitempty"`
|
||||
}
|
||||
|
|
@ -96,4 +96,13 @@ struct_tags["should remove tag with range"] = function()
|
|||
t.cleanup(rs)
|
||||
end
|
||||
|
||||
struct_tags["should add tags with option"] = function()
|
||||
local rs = t.setup_test("tags/with_option", child, { 3, 6 })
|
||||
child.cmd "GoTagAdd json=omitempty"
|
||||
child.cmd "write"
|
||||
|
||||
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||
t.cleanup(rs)
|
||||
end
|
||||
|
||||
return T
|
||||
|
|
|
|||
68
spec/unit/struct_tag_test.lua
Normal file
68
spec/unit/struct_tag_test.lua
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
local t = require "spec.testutils"
|
||||
local _, T, st = t.setup "struct_tags"
|
||||
|
||||
st["should parse tags"] = function()
|
||||
local out = require("gopher._utils.struct_tags").parse_args { "json", "yaml", "etc" }
|
||||
|
||||
t.eq(out.tags, "json,yaml,etc")
|
||||
t.eq(out.options, "")
|
||||
end
|
||||
|
||||
st["should parse tags separated by commas"] = function()
|
||||
local out = require("gopher._utils.struct_tags").parse_args { "json,yaml,etc" }
|
||||
|
||||
t.eq(out.tags, "json,yaml,etc")
|
||||
t.eq(out.options, "")
|
||||
end
|
||||
|
||||
st["should parse tags separated by command and spaces"] = function()
|
||||
local out = require("gopher.struct_tags").parse_args {
|
||||
"json,yaml",
|
||||
"json=omitempty",
|
||||
"xml=something",
|
||||
}
|
||||
|
||||
t.eq(out.tags, "json,yaml,xml")
|
||||
t.eq(out.options, "json=omitempty,xml=something")
|
||||
end
|
||||
|
||||
st["should parse tag with an option"] = function()
|
||||
local out = require("gopher._utils.struct_tags").parse_args {
|
||||
"json=omitempty",
|
||||
"xml",
|
||||
"xml=theoption",
|
||||
}
|
||||
|
||||
t.eq(out.tags, "json,xml")
|
||||
t.eq(out.options, "json=omitempty,xml=theoption")
|
||||
end
|
||||
|
||||
st["should parse tags with an option"] = function()
|
||||
local out = require("gopher._utils.struct_tags").parse_args { "json=omitempty", "yaml" }
|
||||
|
||||
t.eq(out.tags, "json,yaml")
|
||||
t.eq(out.options, "json=omitempty")
|
||||
end
|
||||
|
||||
st["should parse tags with an option separated with comma"] = function()
|
||||
local out = require("gopher._utils.struct_tags").parse_args { "json=omitempty,yaml" }
|
||||
|
||||
t.eq(out.tags, "json,yaml")
|
||||
t.eq(out.options, "json=omitempty")
|
||||
end
|
||||
|
||||
st["should parse tags with options specified separately"] = function()
|
||||
local out = require("gopher._utils.struct_tags").parse_args { "json", "yaml", "json=omitempty" }
|
||||
|
||||
t.eq(out.tags, "json,yaml")
|
||||
t.eq(out.options, "json=omitempty")
|
||||
end
|
||||
|
||||
st["should parse tags with options specified separately and separated by comma"] = function()
|
||||
local out = require("gopher._utils.struct_tags").parse_args { "json,yaml,json=omitempty" }
|
||||
|
||||
t.eq(out.tags, "json,yaml")
|
||||
t.eq(out.options, "json=omitempty")
|
||||
end
|
||||
|
||||
return T
|
||||
Loading…
Add table
Add a link
Reference in a new issue