feature: Add a public API to reorder tags
- Dominant language
- Lua
- Stars
- 716
- Forks
- 33
- PR merge metrics
- No merged PRs in 30d
Description
### Did you check the docs?
- [x] I have read the Grapple docs
### Is your feature request related to a problem? Please describe.
I know it is possible to reorder tags via grapple UI, but I am not really using that UI, I am using grapple API via fuzzy finders to perform the same actions the UI provide, and currently I need a way to reorder tags by just using keybindings, something like `` move the current tag at index 1, I didn't find a way to do it.
### Describe the solution you'd like
I need an API like `required("grapple").move(current_index, destination_index)` or something like that in order to reorder tags.
### Describe alternatives you've considered
The only alternative I have now is to open grapple UI, perhaps I can keep the ordering state inside a table in a custom module and try to manually reorder tags using that state?
### Additional context
Here is what I am trying to achieve, It may help to understand what I am looking for. Thanks a lot for this great plugin.
```lua
local grapple = require("grapple")
local fzf = require("fzf-lua")
local function format_result(item)
return string.format("[%s] - %s", item[1], item[2])
end
local function extract_path(repr)
return repr:match("-%s*(.+)")
end
local function delete_tag(selected)
if #selected > 0 then
grapple.untag({ path = extract_path(selected[1]) })
end
end
local function yank_path(value)
require("clipboard").copy_to_clipboard(extract_path(value))
end
local function list_tags()
-- Get the list of tags (default scope)
local tags = grapple.tags()
-- Map tags to display strings for fzf
local items = {}
for i, tag in ipairs(tags) do
local result = {
i,
tag.path,
(tag.cursor or { 1, 0 })[1],
(tag.cursor or { 1, 0 })[2],
}
table.insert(items, result)
end
return items
end
local function open_grapple_tags()
fzf.fzf_exec(
-- https://github.com/ibhagwan/fzf-lua/wiki/Advanced
function (fzf_cb)
for _, item in ipairs(list_tags()) do
fzf_cb(format_result(item))
end
fzf_cb() -- EOF
end,
{
prompt = "Grapple Tags❯ ",
-- On selection open the file
actions = {
["default"] = function(selected)
-- Select tag by index and open
if #selected > 0 then
grapple.select({ path = extract_path(selected[1]) })
end
end,
["ctrl-d"] = { fn = delete_tag, reload = true },
["ctrl-y"] = { fn = yank_path , reload = true },
-- ["ctrl-v"] = { fn = yank_path , reload = true },
-- ["ctrl-s"] = { fn = yank_path , reload = true },
-- ["ctrl-v"] = { fn = yank_path , reload = true },
["ctrl-q"] = grapple.quickfix,
},
}
)
end
-- Map keys to open grapple tags picker with fzf-lua
vim.keymap.set("n", "", open_grapple_tags, { desc = "[P]roject [A]rgs" })
vim.keymap.set("n", "a", grapple.tag)
vim.keymap.set("n", "", function() grapple.select({ index = 1 }) end)
vim.keymap.set("n", "", function() grapple.select({ index = 2 }) end)
vim.keymap.set("n", "", function() grapple.select({ index = 3 }) end)
vim.keymap.set("n", "", function() grapple.select({ index = 4 }) end)
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start from the public require("grapple") API and the existing tags(), tag(), untag(), and select() calls shown in the issue. Determine where tag ordering is managed and define an API that can move a tag from one index to another; done means callers can reorder tags without opening the Grapple UI and can use the result from keybindings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100