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

* feat(struct_tags): add range support

* refactor: use `start`, and `end_` naming for ranges
This commit is contained in:
Oleksandr Smirnov 2025-08-29 19:46:45 +03:00 committed by Oleksandr Smirnov
parent 8c87952964
commit b6d3815f9b
8 changed files with 129 additions and 26 deletions

View file

@ -65,8 +65,8 @@ end
---@class gopher.TsResult
---@field name string
---@field start_line integer
---@field end_line integer
---@field start integer
---@field end_ integer
---@field is_varstruct boolean
---@param bufnr integer
@ -95,8 +95,8 @@ local function do_stuff(bufnr, parent_type, query)
assert(res.name ~= nil, "No capture name found")
local start_row, _, end_row, _ = parent_node:range()
res["start_line"] = start_row + 1
res["end_line"] = end_row + 1
res["start"] = start_row + 1
res["end_"] = end_row + 1
return res
end

View file

@ -37,11 +37,22 @@ local u = require "gopher._utils"
local log = require "gopher._utils.log"
local struct_tags = {}
---@dochide
---@class gopher.StructTagInput
---@field tags string[] User provided tags
---@field range? gopher.StructTagRange (optional)
---@dochide
---@class gopher.StructTagRange
---@field start number
---@field end_ number
---@param fpath string
---@param bufnr integer
---@param range? gopher.StructTagRange
---@param user_args string[]
---@dochide
local function handle_tags(fpath, bufnr, user_args)
local function handle_tags(fpath, bufnr, range, user_args)
local st = ts.get_struct_under_cursor(bufnr)
-- stylua: ignore
@ -53,9 +64,10 @@ local function handle_tags(fpath, bufnr, user_args)
"-w",
}
if st.is_varstruct then
-- `-struct` and `-line` cannot be combined, setting them separately
if range or st.is_varstruct then
table.insert(cmd, "-line")
table.insert(cmd, string.format("%d,%d", st.start_line, st.end_line))
table.insert(cmd, string.format("%d,%d", (range or st).start, (range or st).end_))
else
table.insert(cmd, "-struct")
table.insert(cmd, st.name)
@ -93,7 +105,7 @@ end
---@param args string[]
---@return string
---@dochide
local function handler_user_args(args)
local function handler_user_tags(args)
if #args == 0 then
return c.gotag.default_tag
end
@ -102,28 +114,30 @@ end
-- Adds tags to a struct under the cursor
-- See |gopher.nvim-struct-tags|
---@param ... string Tags to add to the struct fields. If not provided, it will use [config.gotag.default_tag]
---@param opts gopher.StructTagInput
---@dochide
function struct_tags.add(...)
local args = { ... }
function struct_tags.add(opts)
log.debug("adding tags", opts)
local fpath = vim.fn.expand "%"
local bufnr = vim.api.nvim_get_current_buf()
local user_tags = handler_user_args(args)
handle_tags(fpath, bufnr, { "-add-tags", user_tags })
local user_tags = handler_user_tags(opts.tags)
handle_tags(fpath, bufnr, opts.range, { "-add-tags", user_tags })
end
-- Removes tags from a struct under the cursor
-- See `:h gopher.nvim-struct-tags`
---@dochide
---@param ... string Tags to add to the struct fields. If not provided, it will use [config.gotag.default_tag]
function struct_tags.remove(...)
local args = { ... }
---@param opts gopher.StructTagInput
function struct_tags.remove(opts)
log.debug("removing tags", opts)
local fpath = vim.fn.expand "%"
local bufnr = vim.api.nvim_get_current_buf()
local user_tags = handler_user_args(args)
handle_tags(fpath, bufnr, { "-remove-tags", user_tags })
local user_tags = handler_user_tags(opts.tags)
handle_tags(fpath, bufnr, opts.range, { "-remove-tags", user_tags })
end
-- Removes all tags from a struct under the cursor
@ -132,7 +146,7 @@ end
function struct_tags.clear()
local fpath = vim.fn.expand "%"
local bufnr = vim.api.nvim_get_current_buf()
handle_tags(fpath, bufnr, { "-clear-tags" })
handle_tags(fpath, bufnr, nil, { "-clear-tags" })
end
return struct_tags