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:
parent
ca1d4fda88
commit
d46461d232
8 changed files with 129 additions and 26 deletions
|
|
@ -65,8 +65,8 @@ end
|
||||||
|
|
||||||
---@class gopher.TsResult
|
---@class gopher.TsResult
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field start_line integer
|
---@field start integer
|
||||||
---@field end_line integer
|
---@field end_ integer
|
||||||
---@field is_varstruct boolean
|
---@field is_varstruct boolean
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
|
|
@ -95,8 +95,8 @@ local function do_stuff(bufnr, parent_type, query)
|
||||||
assert(res.name ~= nil, "No capture name found")
|
assert(res.name ~= nil, "No capture name found")
|
||||||
|
|
||||||
local start_row, _, end_row, _ = parent_node:range()
|
local start_row, _, end_row, _ = parent_node:range()
|
||||||
res["start_line"] = start_row + 1
|
res["start"] = start_row + 1
|
||||||
res["end_line"] = end_row + 1
|
res["end_"] = end_row + 1
|
||||||
|
|
||||||
return res
|
return res
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -37,11 +37,22 @@ local u = require "gopher._utils"
|
||||||
local log = require "gopher._utils.log"
|
local log = require "gopher._utils.log"
|
||||||
local struct_tags = {}
|
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 fpath string
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
|
---@param range? gopher.StructTagRange
|
||||||
---@param user_args string[]
|
---@param user_args string[]
|
||||||
---@dochide
|
---@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)
|
local st = ts.get_struct_under_cursor(bufnr)
|
||||||
|
|
||||||
-- stylua: ignore
|
-- stylua: ignore
|
||||||
|
|
@ -53,9 +64,10 @@ local function handle_tags(fpath, bufnr, user_args)
|
||||||
"-w",
|
"-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, "-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
|
else
|
||||||
table.insert(cmd, "-struct")
|
table.insert(cmd, "-struct")
|
||||||
table.insert(cmd, st.name)
|
table.insert(cmd, st.name)
|
||||||
|
|
@ -93,7 +105,7 @@ end
|
||||||
---@param args string[]
|
---@param args string[]
|
||||||
---@return string
|
---@return string
|
||||||
---@dochide
|
---@dochide
|
||||||
local function handler_user_args(args)
|
local function handler_user_tags(args)
|
||||||
if #args == 0 then
|
if #args == 0 then
|
||||||
return c.gotag.default_tag
|
return c.gotag.default_tag
|
||||||
end
|
end
|
||||||
|
|
@ -102,28 +114,30 @@ end
|
||||||
|
|
||||||
-- Adds tags to a struct under the cursor
|
-- Adds tags to a struct under the cursor
|
||||||
-- See |gopher.nvim-struct-tags|
|
-- 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
|
---@dochide
|
||||||
function struct_tags.add(...)
|
function struct_tags.add(opts)
|
||||||
local args = { ... }
|
log.debug("adding tags", opts)
|
||||||
|
|
||||||
local fpath = vim.fn.expand "%"
|
local fpath = vim.fn.expand "%"
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
|
|
||||||
local user_tags = handler_user_args(args)
|
local user_tags = handler_user_tags(opts.tags)
|
||||||
handle_tags(fpath, bufnr, { "-add-tags", user_tags })
|
handle_tags(fpath, bufnr, opts.range, { "-add-tags", user_tags })
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Removes tags from a struct under the cursor
|
-- Removes tags from a struct under the cursor
|
||||||
-- See `:h gopher.nvim-struct-tags`
|
-- See `:h gopher.nvim-struct-tags`
|
||||||
---@dochide
|
---@dochide
|
||||||
---@param ... string Tags to add to the struct fields. If not provided, it will use [config.gotag.default_tag]
|
---@param opts gopher.StructTagInput
|
||||||
function struct_tags.remove(...)
|
function struct_tags.remove(opts)
|
||||||
local args = { ... }
|
log.debug("removing tags", opts)
|
||||||
|
|
||||||
local fpath = vim.fn.expand "%"
|
local fpath = vim.fn.expand "%"
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
|
|
||||||
local user_tags = handler_user_args(args)
|
local user_tags = handler_user_tags(opts.tags)
|
||||||
handle_tags(fpath, bufnr, { "-remove-tags", user_tags })
|
handle_tags(fpath, bufnr, opts.range, { "-remove-tags", user_tags })
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Removes all tags from a struct under the cursor
|
-- Removes all tags from a struct under the cursor
|
||||||
|
|
@ -132,7 +146,7 @@ end
|
||||||
function struct_tags.clear()
|
function struct_tags.clear()
|
||||||
local fpath = vim.fn.expand "%"
|
local fpath = vim.fn.expand "%"
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
handle_tags(fpath, bufnr, { "-clear-tags" })
|
handle_tags(fpath, bufnr, nil, { "-clear-tags" })
|
||||||
end
|
end
|
||||||
|
|
||||||
return struct_tags
|
return struct_tags
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,13 @@ end
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param fn fun(args: table)
|
---@param fn fun(args: table)
|
||||||
---@param nargs? number|"*"|"?"
|
---@param nargs? number|"*"|"?"
|
||||||
|
---@param range? boolean
|
||||||
---@private
|
---@private
|
||||||
local function cmd(name, fn, nargs)
|
local function cmd(name, fn, nargs, range)
|
||||||
nargs = nargs or 0
|
vim.api.nvim_create_user_command(name, fn, {
|
||||||
vim.api.nvim_create_user_command(name, fn, { nargs = nargs })
|
nargs = nargs or 0,
|
||||||
|
range = range or false,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
cmd("GopherLog", function()
|
cmd("GopherLog", function()
|
||||||
|
|
@ -44,12 +47,24 @@ end)
|
||||||
|
|
||||||
-- :GoTag
|
-- :GoTag
|
||||||
cmd("GoTagAdd", function(opts)
|
cmd("GoTagAdd", function(opts)
|
||||||
require("gopher").tags.add(unpack(opts.fargs))
|
require("gopher").tags.add {
|
||||||
end, "*")
|
tags = opts.fargs,
|
||||||
|
range = (opts.count ~= -1) and {
|
||||||
|
start = opts.line1,
|
||||||
|
end_ = opts.line2,
|
||||||
|
} or nil,
|
||||||
|
}
|
||||||
|
end, "*", true)
|
||||||
|
|
||||||
cmd("GoTagRm", function(opts)
|
cmd("GoTagRm", function(opts)
|
||||||
require("gopher").tags.rm(unpack(opts.fargs))
|
require("gopher").tags.rm {
|
||||||
end, "*")
|
tags = opts.fargs,
|
||||||
|
range = (opts.count ~= -1) and {
|
||||||
|
start = opts.line1,
|
||||||
|
end_ = opts.line2,
|
||||||
|
} or nil,
|
||||||
|
}
|
||||||
|
end, "*", true)
|
||||||
|
|
||||||
cmd("GoTagClear", function()
|
cmd("GoTagClear", function()
|
||||||
require("gopher").tags.clear()
|
require("gopher").tags.clear()
|
||||||
|
|
|
||||||
14
spec/fixtures/tags/add_range_input.go
vendored
Normal file
14
spec/fixtures/tags/add_range_input.go
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int
|
||||||
|
Name string
|
||||||
|
Num int64
|
||||||
|
Cost int
|
||||||
|
Thingy []string
|
||||||
|
Testing int
|
||||||
|
Another struct {
|
||||||
|
First int
|
||||||
|
Second string
|
||||||
|
}
|
||||||
|
}
|
||||||
14
spec/fixtures/tags/add_range_output.go
vendored
Normal file
14
spec/fixtures/tags/add_range_output.go
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int
|
||||||
|
Name string `gopher:"name"`
|
||||||
|
Num int64 `gopher:"num"`
|
||||||
|
Cost int `gopher:"cost"`
|
||||||
|
Thingy []string
|
||||||
|
Testing int
|
||||||
|
Another struct {
|
||||||
|
First int
|
||||||
|
Second string
|
||||||
|
}
|
||||||
|
}
|
||||||
14
spec/fixtures/tags/remove_range_input.go
vendored
Normal file
14
spec/fixtures/tags/remove_range_input.go
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int `asdf:"id"`
|
||||||
|
Name string `asdf:"name"`
|
||||||
|
Num int64 `asdf:"num"`
|
||||||
|
Cost int `asdf:"cost"`
|
||||||
|
Thingy []string `asdf:"thingy"`
|
||||||
|
Testing int `asdf:"testing"`
|
||||||
|
Another struct {
|
||||||
|
First int `asdf:"first"`
|
||||||
|
Second string `asdf:"second"`
|
||||||
|
} `asdf:"another"`
|
||||||
|
}
|
||||||
14
spec/fixtures/tags/remove_range_output.go
vendored
Normal file
14
spec/fixtures/tags/remove_range_output.go
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int `asdf:"id"`
|
||||||
|
Name string `asdf:"name"`
|
||||||
|
Num int64
|
||||||
|
Cost int
|
||||||
|
Thingy []string
|
||||||
|
Testing int `asdf:"testing"`
|
||||||
|
Another struct {
|
||||||
|
First int `asdf:"first"`
|
||||||
|
Second string `asdf:"second"`
|
||||||
|
} `asdf:"another"`
|
||||||
|
}
|
||||||
|
|
@ -78,4 +78,22 @@ struct_tags["should add tags on short declr var"] = function()
|
||||||
t.cleanup(rs)
|
t.cleanup(rs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
struct_tags["should add tag with range"] = function()
|
||||||
|
local rs = t.setup_test("tags/add_range", child, { 5, 1 })
|
||||||
|
child.cmd ".,+2GoTagAdd gopher"
|
||||||
|
child.cmd "write"
|
||||||
|
|
||||||
|
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||||
|
t.cleanup(rs)
|
||||||
|
end
|
||||||
|
|
||||||
|
struct_tags["should remove tag with range"] = function()
|
||||||
|
local rs = t.setup_test("tags/remove_range", child, { 6, 1 })
|
||||||
|
child.cmd ".,+2GoTagRm asdf"
|
||||||
|
child.cmd "write"
|
||||||
|
|
||||||
|
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||||
|
t.cleanup(rs)
|
||||||
|
end
|
||||||
|
|
||||||
return T
|
return T
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue