refactor: unite struct_tags util with main logic
This commit is contained in:
parent
a67bf39c06
commit
bef7b88379
3 changed files with 54 additions and 53 deletions
|
|
@ -1,43 +0,0 @@
|
|||
local u = require "gopher._utils"
|
||||
|
||||
---@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
|
||||
|
||||
return {
|
||||
tags = table.concat(u.list_unique(tags), ","),
|
||||
options = table.concat(u.list_unique(options), ","),
|
||||
}
|
||||
end
|
||||
|
||||
return struct_tags
|
||||
|
|
@ -38,7 +38,6 @@ 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
|
||||
|
|
@ -106,6 +105,51 @@ local function handle_tags(fpath, bufnr, range, user_args)
|
|||
)
|
||||
end
|
||||
|
||||
---@dochide
|
||||
---@param option string
|
||||
local function option_to_tag(option)
|
||||
return option:match "^(.-)="
|
||||
end
|
||||
|
||||
---@dochide
|
||||
---@param args string[]
|
||||
local function unwrap_if_needed(args)
|
||||
local out = {}
|
||||
for _, v in pairs(args) do
|
||||
for _, p in pairs(vim.split(v, ",")) do
|
||||
table.insert(out, p)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
---@dochide
|
||||
---@class gopher.StructTagsArgs
|
||||
---@field tags string
|
||||
---@field options string
|
||||
|
||||
---@dochide
|
||||
---@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
|
||||
|
||||
return {
|
||||
tags = table.concat(u.list_unique(tags), ","),
|
||||
options = table.concat(u.list_unique(options), ","),
|
||||
}
|
||||
end
|
||||
|
||||
-- Adds tags to a struct under the cursor
|
||||
-- See `:h gopher.nvim-struct-tags`
|
||||
---@param opts gopher.StructTagInput
|
||||
|
|
@ -116,7 +160,7 @@ function struct_tags.add(opts)
|
|||
local fpath = vim.fn.expand "%"
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
|
||||
local user_args = st.parse_args(opts.input)
|
||||
local user_args = struct_tags.parse_args(opts.input)
|
||||
local a = {
|
||||
"-add-tags",
|
||||
(user_args.tags ~= "") and user_args.tags or c.gotag.default_tag,
|
||||
|
|
@ -137,7 +181,7 @@ function struct_tags.remove(opts)
|
|||
local fpath = vim.fn.expand "%"
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
|
||||
local user_args = st.parse_args(opts.input)
|
||||
local user_args = struct_tags.parse_args(opts.input)
|
||||
handle_tags(fpath, bufnr, opts.range, {
|
||||
"-remove-tags",
|
||||
(user_args.tags ~= "") and user_args.tags or c.gotag.default_tag,
|
||||
|
|
|
|||
|
|
@ -2,14 +2,14 @@ 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" }
|
||||
local out = require("gopher.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" }
|
||||
local out = require("gopher.struct_tags").parse_args { "json,yaml,etc" }
|
||||
|
||||
t.eq(out.tags, "json,yaml,etc")
|
||||
t.eq(out.options, "")
|
||||
|
|
@ -27,7 +27,7 @@ st["should parse tags separated by command and spaces"] = function()
|
|||
end
|
||||
|
||||
st["should parse tag with an option"] = function()
|
||||
local out = require("gopher._utils.struct_tags").parse_args {
|
||||
local out = require("gopher.struct_tags").parse_args {
|
||||
"json=omitempty",
|
||||
"xml",
|
||||
"xml=theoption",
|
||||
|
|
@ -38,28 +38,28 @@ st["should parse tag with an option"] = function()
|
|||
end
|
||||
|
||||
st["should parse tags with an option"] = function()
|
||||
local out = require("gopher._utils.struct_tags").parse_args { "json=omitempty", "yaml" }
|
||||
local out = require("gopher.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" }
|
||||
local out = require("gopher.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" }
|
||||
local out = require("gopher.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" }
|
||||
local out = require("gopher.struct_tags").parse_args { "json,yaml,json=omitempty" }
|
||||
|
||||
t.eq(out.tags, "json,yaml")
|
||||
t.eq(out.options, "json=omitempty")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue