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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue