telescope-zf-native.nvim
telescope-zf-native.nvim copied to clipboard
Sorting based on proximity to open buffer
Hey, thanks so much for this plugin, it has greatly improved my telescope experience :)
This is somewhat similar to #10 requests but based on a different criteria. If say I'm working in a large repo with a large amount of files that have the same name:
dir_a/
- file_1
- file_2
dir_b/
- file_1
- file_2
I would love to configure the sorting such that if I'm in dir_b/file_1 and I open Telescope and type file_2, the closest selection in Telescope is dir_b/file_2 even though alphabetically dir_a/ might be sooner.
Is that something achievable in the current sorting configuration or will I need to drop down and fiddle with Telescope comps myself? Thanks! :)
Hey, thanks so much for this plugin, it has greatly improved my telescope experience :)
Great to hear!
If I understand correctly, this isn't yet possible. Though with some code on the telescope side you could probably get something that works. Let me get some clarification first
if I'm in dir_b/file_1...
By this do you mean your working directory (:pwd) is dir_b? Or do you mean the open buffer is dir_b/file_1?
As a note, zf (the code powering this plugin) is pretty good at filtering directories: https://github.com/natecraddock/zf/?tab=readme-ov-file#strict-path-matching
So you could type b/ file as your query and the search would probably be limited to b. As a more specific example
src/file1
src/file2
src/file3
test/file1
test/file2
test/file3
With this, a search for t/ 3 would get the test/file3. Maybe in your specific use case the combination of tokens and strict path matching isn't enough, but I figured I might as well point that out.
By this do you mean your working directory (:pwd) is dir_b? Or do you mean the open buffer is dir_b/file_1?
Yeah I meant the file in the open buffer. I have a temp workaround with a keymap bound to <leader>. to show files only in the same dir as the buffer require("telescope.builtin").find_files({ cwd = vim.fn.expand("%:p:h") }) but would prefer simply have the list be ranked with those being the closest.
So you could type b/ file as your query and the search would probably be limited to b. As a more specific example
Thank you! Yeah I found zf is overall pretty good with dirs so this is more of a quality of life improvement not a breaking thing.
In that case, I have a couple of ideas:
- I could add a new optional function to this plugin. Something like this
function custom_rank(rank: number, line: string): number
the custom_rank function would be passed the line, and the rank that was given by zf. Then you could inspect the line and then return a different rank if needed. This way you could see if the beginning of the line matches the current directory and give a better rank
- "Subclass"/create your own sorter that builds on top of zf
Another issue last week referenced this comment that may give you some ideas: https://github.com/nvim-telescope/telescope.nvim/issues/2905#issuecomment-1922729686
I'm not sure on the best approach. In either case, there would be a performance impact as some of the ranking is now in Lua.. though it likely wouldn't be too bad
Personally I find the 1st option a bit more straightforward but up to you! I'll muck around what's possible with the Telescope API
I actually just found out about proximity-sort which is a small sorter util in a Rust binary that does exactly the kind of thing I want to achieve. Long shot, but if there was a way to stitch zf together with this it would be ideal as I won't need to reimplement this in Lua but completely understand if this is quite out of scope :)
Hey! So sorry for leaving this abandoned for a long time. I'm not really sure where to take this. I'm going to close this, but please feel free to reply if you think there is anything else I can do to help