-
-
Notifications
You must be signed in to change notification settings - Fork 889
feat: delete entry from the picker without closing telescope #828
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: delete entry from the picker without closing telescope #828
Conversation
There is this problem:
|
Hi, @Conni2461 if you don't mind, can you help me out here, thanks if you do :) There is one problem here: We cannot reliably find out the index in the results table for the current selection. I have tried the following methods:
If there are any other ways to get the info, please do tell. Another option as done by |
I didn't know that I have some other thoughts i need to evaluate and maybe test it but i am busy today. I try to find some time for it tomorrow :) |
No problem, take your time and thanks for taking a look at it :) I am using refs to find out the index of the selection in the results table and also provided an option actions.delete_buffer = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:delete_selection(function(selection)
vim.api.nvim_buf_delete(selection.bufnr, { force = true })
end)
end
-- could do the same for file_browser where we delete a file/directory Also, we can think of doing multiple deletes using multi-select once this is well tested. |
This looks really cool! I recently implemented something similar for my telescope-vim-bookmarks extension where I have an action that either deletes all currently multi-selected bookmarks or the bookmark under the cursor (if there's no multi selection). There I used the But this here seems much more convenient, looking forward to it being merged, especially with multi-selection support. |
can we use |
I believe it could be used but it closes the finder here: https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/pickers.lua#L642 On that note, why are we closing the finder if the new finder is optional? Edit: closing the finder only if the new finder is given will give us the ability to use the function |
fe4bbc4
to
54eb3a7
Compare
Yeah, we can probably make that change here if you want. I'd prefer to re-use refresh (we can extend refresh in whatever ways we need to make that happen) |
I am also not liking the way of finding the selection entry in the results, as mentioned here I tried a bunch of ways but comparing it directly in a for loop seems the only way to get the correct index. It's fine for single selection but in case of multi-selection, we would have to do compare each result entry with every selection from multi-select. Is there any possibility where we add a special field for each entry in the result table identifying its position like Edit: |
Just bad programming by me 🤣 Sorry that i haven't looked at it earlier, it was a crazy week and then i wanted to look at it Saturday and the whole Great work here. I just tested it. (Also i thought we could change the Multi selection seems like a good idea, (maybe as option that can be enabled/disabled or as section function). Maybe someone has a use case in the future where that person doesn't want it to be the whole multi selection. |
I tested with We could reset the callback table, but then what about any existing implementation, extensions and user configs which are using this? Something like this: { <function 1> }
{ <function 1>, <function 2> }
{ <function 1>, <function 2>, <function 3> }
{ <function 1>, <function 2>, <function 3>, <function 4> }
{ <function 1>, <function 2>, <function 3>, <function 4>, <function 5> } Also, I am brainstorming a bit on how to do multi-selection and here's what I'm trying:
I've been using this in the buffers picker to delete buffers right when I opened this PR Screen.Recording.2021-05-17.at.22.48.19.mov |
Yeah i meant adding something that would pop the function after executing but we can just keep Do you want to integrate both your prs together? or whats your plan? |
Yeah, I will close my other PR and add the same here. |
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.
LGTM Thanks :)
But there are merge conflicts. Sorry :)
5ca0b7b
to
66dc7f0
Compare
This will allow users to delete entries from the picker without closing telescope.
Selection index does not necessarily mean the actual index in the results table as while searching we get the filtered lists and the selection index is according to that list. Also, entry.index value does not change ever, so we have to loop through the results to actually find the right index to remove.
Using reference comparison to find the actual index of the selection in the results entry.
What about realizing #582 now? To make everything simpler for end users and we can be confident in not mapping things. I just did a super simple prototype that works, missing theme and mappings. Can anyone think of a better interface than tamis proposal? (Also i am not sure about the |
Re: I think this would make the experience of setting a new action for a specific picker pretty good: #883 |
Yeah that sounds good. Should I add the documentation to the
This is really good work and would make configuring Telescope simple and intuitive! |
I added documentation for #883. I would ask tj what he thinks and then we can merge yours either before mine or after. Doesn't really matter but this way we only really need to document the new way :) |
Sounds good to me 👍 |
I'll test it with my configuration PR that i just merged and then i'll merge this and update the comment on how to set it up. WORKS require("telescope").setup {
pickers = {
buffers = {
show_all_buffers = true,
sort_lastused = true,
theme = "dropdown",
previewer = false,
mappings = {
i = {
["<c-d>"] = "delete_buffer",
}
}
}
}
} |
Thank you amazing feature :) and thanks everyone for being patient that long |
Awesome, thanks everyone for their guidance and feedback! :) Now, onto formatting the config in the new way! :D |
Hi @dhruvmanila. Thank you for this, it's awesome! I was really missing an easy way of deleting buffers. There is only one minor thing that is a bit annoying: if you delete the last entry, then the selection moves back to the first entry, while it would be much better if it stayed in the last. Should I open an issue for this? |
Not needed, you can do something like this (just tested it, works) local actions = require "telescope.actions"
require("telescope").setup {
pickers = {
buffers = {
mappings = {
i = {
["<c-d>"] = actions.delete_buffer + actions.move_to_top,
}
}
}
}
} |
I think you misunderstood. The default behavior is great: when you delete an entry, the selection stays where it is. The problem is that this rule is broken whenever you delete the last entry in the menu. In this case, it actually jumps back to the first entry while (in my opinion) it should stay in the last. |
@mroavi If I understand what you want correctly, you can achieve this by setting your |
@l-kershaw you are completely right. Didn't know about the possibility to configure scrolling behavior. Please ignore my suggestion. |
No worries 🙂 |
Yes, on second thought what I suggested is pretty off 😆 I took last too literally, cool you've got it solved! |
Thanks, this however doesn't delete the current buffer even if you select to delete it. |
For info, I was happy to find that mapping this to
|
this check added after feature landing seems breaking stuff, at least for telescope.nvim/lua/telescope/actions/init.lua Line 1103 in 2d92125
copied the function to my local setup, commented callback content and works well |
@sleewoo I think you're probably trying to delete an entry that doesn't exist as a buffer. The This action is probably best used with something like the Probably could do with better error handling but considering the purpose of this action and the way you're using it, I think this is low-priority. Also, by commenting out the contents of the callback function, you've broken the intended use case of the action -- deleting an open buffer. |
@jamestrew yes, reverted my dirty hack as realized i try to delete buffers that does not exists. |
Hello @dhruvmanila, My extension is to list out all the terminal buffers created using toggleterm. By pressing [CR], the selected terminal buffer(s) will be opened or focused. This part works fine. I also added a keymap [C-d] to remove(closed) the terminals. The terminals can be removed(closed) successfully, however, the telescope window we be closed every time I press [C-d] to perform the action. I would like the remove action performs just like the actions.delete_buffer, which will not close telescope. The code below is my implementation. I wrote my code based on your implementation of actions.delete_buffer, but still didn't get the desired result. Did I miss anything or this is a limitation for handling terminal buffers? Thank you so much!
|
I've confirmed the issue of telescope closing after a terminal buffer is deleted (using I've eliminated some possibilities for this problem. The issue doesn't seem to be in the I also tried to determine if this is related to I then assumed it had to do with neovim's handling of the deletion of terminal buffers. To test that assumption, I opened a terminal buffer by running the It must have to do with What makes it more difficult to diagnose is that telescope isn't always closed after deleting a terminal buffer that was created with |
This will allow users to delete entries from the picker without closing telescope.
Discussion ref: #803 (review)
fixes: #689
fixes: #621
fixes: #672
This function in and of itself will just remove the entry, what I'm proposing next is:
Idea: Should we provide a callback parameter which will be called with the removed entry, so that the user or the specific picker can do exactly what needs to be done outside of telescope? Eg., delete the buffer, delete the file, etc.
The selection strategy is temporarily set to "row" as that will help keep the selection in the same position instead of resetting to the top. If there is any other way to achieve this, do suggest :)
Thanks @Conni2461 for helping!
delete_selection.mov