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

Conversation

muratoffalex
Copy link

add snipe select backend

Description

Snipe can be used as vim.ui.select, an example is given in the snipe documentation

@github-actions github-actions bot requested a review from stevearc December 14, 2024 10:01
Copy link
Owner

@stevearc stevearc left a comment

Choose a reason for hiding this comment

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

Some nits and Qs

@@ -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)

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.

Comment on lines +23 to +27
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
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?

Comment on lines +41 to +42
-- Set cursor to the first item
vim.api.nvim_win_set_cursor(snipe.ui_select_menu.win, { 1, 0 })
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

Comment on lines +48 to +49
on_choice(nil, nil)
snipe.ui_select_menu:close()
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.

on_choice(nil, nil)
snipe.ui_select_menu:close()
-- 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants