-
Notifications
You must be signed in to change notification settings - Fork 35
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
local M = {} | ||
|
||
local snipe = require("snipe") | ||
|
||
M.is_supported = function() | ||
return pcall(require, "snipe") | ||
end | ||
|
||
M._init_snipe_menu = function(config, on_choice) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems odd. Why are we passing |
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also pass |
||
callback = function() | ||
on_choice(nil, nil) | ||
snipe.ui_select_menu:close() | ||
Comment on lines
+48
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible for |
||
-- Reset the title to avoid the title being set for the next menu | ||
snipe.ui_select_menu.config.open_win_override.title = nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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)