Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(snipe): add snipe select backend #186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ require("dressing").setup({
enabled = true,

-- Priority list of preferred vim.select implementations
backend = { "telescope", "fzf_lua", "fzf", "builtin", "nui" },
backend = { "telescope", "fzf_lua", "fzf", "builtin", "nui", "snipe" },

-- Trim trailing `:` from prompt
trim_prompt = true,
Expand Down Expand Up @@ -243,6 +243,19 @@ require("dressing").setup({
min_height = 10,
},

-- Options for snipe selector
snipe = {
-- Default options come from your snipe.nvim configuration,
-- but you can override them here.
-- See available options for snipe.menu in the snipe.nvim repository
options = {},

--- Callback function that runs when a new snipe buffer is created
--- Useful to override the default keymaps logic
--- @type fun(menu: table, on_choice: fun(item: any, idx: number|nil))
add_new_buffer_callback = nil,
},

-- Options for built-in selector
builtin = {
-- Display numbers for options and set up keymaps
Expand Down
15 changes: 14 additions & 1 deletion lua/dressing/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ local default_config = {
enabled = true,

-- Priority list of preferred vim.select implementations
backend = { "telescope", "fzf_lua", "fzf", "builtin", "nui" },
backend = { "telescope", "fzf_lua", "fzf", "builtin", "nui", "snipe" },

-- Trim trailing `:` from prompt
trim_prompt = true,
Expand Down Expand Up @@ -114,6 +114,19 @@ local default_config = {
min_height = 10,
},

-- Options for snipe selector
snipe = {
-- Default options come from your snipe.nvim configuration,
-- but you can override them here.
-- See available options for snipe.menu in the snipe.nvim repository
options = {},

--- Callback function that runs when a new snipe buffer is created
--- Useful to override the default keymaps logic
--- @type fun(menu: table, on_choice: fun(item: any, idx: number|nil))
add_new_buffer_callback = nil,
},

-- Options for built-in selector
builtin = {
-- Display numbers for options and set up keymaps
Expand Down
56 changes: 56 additions & 0 deletions lua/dressing/select/snipe.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
local M = {}

local snipe = require("snipe")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No top-level requires here. This module needs to be able to be required even if snipe is not installed (because we need to check the is_supported method below)


M.is_supported = function()
return pcall(require, "snipe")
end

M._init_snipe_menu = function(config, on_choice)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason this needs to be on the module? If not, this could just be a local function.

config = config or {}

local default_opts = {
default_keymaps = {
auto_setup = true,
},
}

local opts = vim.tbl_deep_extend("keep", default_opts, config.options or {})

local snipe_menu = require("snipe.menu"):new(opts)

-- Run user callback if provided
if config.add_new_buffer_callback then
snipe_menu:add_new_buffer_callback(function(m)
config.add_new_buffer_callback(m, on_choice)
end)
end
Comment on lines +23 to +27
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems odd. Why are we passing on_choice here? That's the callback for the vim.ui.select call, but this is only going to be called once on startup?


snipe.ui_select_menu = snipe_menu
end

M.select = function(config, items, opts, on_choice)
-- Ensure the snipe menu is initialized
-- It is enough to initialize once
if not snipe.ui_select_menu then
M._init_snipe_menu(config, on_choice)
end

snipe.ui_select(items, opts, on_choice)
snipe.ui_select_menu.page = 1
-- Set cursor to the first item
vim.api.nvim_win_set_cursor(snipe.ui_select_menu.win, { 1, 0 })
Comment on lines +41 to +42
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the default behavior without these lines?


vim.api.nvim_create_autocmd("BufLeave", {
desc = "Cancel vim.ui.select",
once = true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also pass buffer = 0? We want to make sure this is only called when leaving the snipe buffer

callback = function()
on_choice(nil, nil)
snipe.ui_select_menu:close()
Comment on lines +48 to +49
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for on_choice to get double-called here? Naively I would expect that if you use snipe to select an item it will call on_choice, then close the buffer, which will trigger BufLeave and call on_choice again.

-- Reset the title to avoid the title being set for the next menu
snipe.ui_select_menu.config.open_win_override.title = nil
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very odd...the snipe vim.ui.select wrapper stores the window title and re-uses it?

end,
})
end

return M
Loading