-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add file browser #11285
base: master
Are you sure you want to change the base?
Add file browser #11285
Conversation
Would it be possible to implement a file browser with this methodology instead? https://github.com/stevearc/oil.nvim It uses a buffer/pop to navigate and edit files like you're inside a buffer. |
@drybalka this is awesome.. thanks for the effort. Can you please post a screenshot as to how it looks ? Basically I am interested to show how nested directories/files are being presented. |
I am not a maintainer of helix, but in my opinion this is rather a plugin functionality. First of all, it would be hard-ish to implement and therefore to maintain. Secondly, buffers are primarily used for text editing and one does not usually need to create/delete/rename files so much. |
Well, the idea was to make it look and behave like the original telescope-file-browser, so you may look at the showcase video there (just without all pretty-niceness as this is just a proof-of-concept). As for a real screenshot it looks (quite bare bones) like this: In other words, both the picker and the preview (if a dir is selected) show the contents at depth 1, similar to how |
I kind of like just how simple this change is! It reuses existing UI components and allows exploring the file tree without adding any of the heavier features. |
helix-term/src/ui/mod.rs
Outdated
if let Ok(files) = directory_content { | ||
for file in files { | ||
if injector.push(file).is_err() { | ||
break; | ||
} | ||
} | ||
} |
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.
This style is used by the file picker because finding files is a potentially long-running iterator and we might use the injector to push some files asynchronously after a timeout. Since you've already collected the directory contents above you should pass that vec as the third argument to Picker::new
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.
Also I believe this function should return a result and pass up the error from directory_content
. Currently if you fail to list the directory contents a blank picker opens
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.
Great suggestions, simplified the code, thank you!
I think it would be useful to have the equivalent version of It could be something like this: fn file_browser_in_current_buffer_directory(cx: &mut Context) {
let doc_dir = doc!(cx.editor)
.path()
.and_then(|path| path.parent().map(|path| path.to_path_buf()));
let path = match doc_dir {
Some(path) => path,
None => {
cx.editor.set_error("current buffer has no path or parent");
return;
}
};
if let Ok(picker) = ui::file_browser(path) {
cx.push_layer(Box::new(overlaid(picker)));
}
} |
I think this can even be the default behavior, thanks for suggesting! Not sure when I would even need to open a file browser in the project root. I deliberately wanted to keep this PR simple and feature-poor, but your suggestion is quite simple and I think it is worth it. |
@drybalka This is perfect with the change to the current buffer directory. It's similar to good ol' netrw for quick navigation and covers 95% of the needs for me. The more advanced stuff (copy, paste, create, delete, etc) can be covered by a mix of #11164, shell scripts, wezterm, and the yazi file explorer. Thanks for your work! |
@drybalka found a small issue when testing: if the cursor is on a binary file, the preview will mess up the view and leave random characters everywhere. Maybe binary files can be excluded from preview somehow? |
@zegervdv I am using the same file previewer as file_picker, so the problem is probably in the previewer itself. Although as far as I tested the usual .jpg, .pdf, and executables are all correctly previewed as |
It would be nice to add support for going straight to the root of the project if the current buffer hasn't been saved yet. I went to test the file browser functionality by escaping the file picker when calling Screen.Recording.2024-09-10.at.08.18.51.mov |
Actually, I can't get this to work at all? Running on MacOS. Screen.Recording.2024-09-10.at.08.47.35.movHere is the log from |
Well, this is actually the behavior I get when using the command palette for the file_picker as well, I guess the palette is somehow buggy in this regard. I just mapped it to some keymap and tested like that. But anyway, even if you map it correctly and then try to open the file_browser after |
Actually in current latest version of helix, no file picker can open from the command pallete (tried in nixos and archlinux). It seems like another bug. |
I have the same reported issue when trying to open the browser from the command palette. Works great when opened from a keymap though. I do have a couple of suggestions to consider
|
So stoked to see this @drybalka! 🫶 Probably not worth it at this stage as merging the base feature is higher priority, but it'd be cool be be able to classify the entries with icons. Having a trailing slash on dirs makes sense, but hugely useful IMO to when something is linked, and being able to visually filter on filetypes. Kinda like I could probably take a look at doing this when this is in if you aren't feeling it :) |
5695166
to
b743ff3
Compare
@summersz Adding slash to dirs is a great idea, thank you! Using other keys in a picker is harder. The current design does not allow picker-specific keymaps, and even remapping the existing keys is not allowed yet. I will leave this idea for the future, as pickers refactor should come at some point. @m0ar I also really like the idea with icons! The file_browser picker shares a lot of code with the file_picker, so if the icons would work there then most probably they will also work here. I don't want to complicate this PR with icons at this point, but you may already try implementing them for the file_picker. |
bb00c5a
to
68b252a
Compare
To use this, you can add the following keybinding to your config ( [keys.normal.space.space]
f = "file_browser" Now, you can double-tap Note that you cannot open the file browser from the command palette due to #4508 (as discussed above). |
I love this feature. Could it also change the working-directory too, if for example i navigate to completely different project? Though that is probably for something like |
That's #10215 |
Alternatively if you want to use the file_browser as default, you can replace |
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.
Simple and relatively clean impl. Also tested it, and other than #4508, which is a separate issue, looks good to me!
It may just be personal preference to me, but I actually preferred having it open in the root of the cwd, as opposed to the directory of the currently open buffer. I'd be fine having any hierarchical file browser over none though, so that's not a blocker for me! Perhaps in the future, having an "open file browser at current working directory" would be a nice-to-have.
It may also be worth considering adding a default keybinding for the file browser, but that might be worth doing in a separate PR to not prolong this one
I tested your branch and it solves a large problem for me in a very large repo!! I was planning to recommend using the right/left arrows on dir's like @summersz comment but accept your reply on this matter. Im not sure why but there is a flicker when navigating directories, its almost like the window opens and closes quickly which looks jaring but apart form that I think you got a really nice branch here, hope this one makes it in! |
I just tried this out and it's great, nice work! 👏 @drybalka
This would be useful for me when I'm working with a large project, especially one that I'm not familiar with. e.g. I'm looking at some file deep in the tree and I want to reorient myself and start navigating again from the root.
I noticed this too when trying it out with |
Squashed version of helix-editor#11285 68b252a
This is a minimal implementation of the file browser, which would probably cover a lot of requirements in #200. The whole thing works analogous to the https://github.com/nvim-telescope/telescope-file-browser.nvim as suggested in this comment. Even though the resolution of the discussion seems to be "file tree/browser is too hard, it should be implemented as a plugin", I feel like my changes are quite small and natural to be considered for adding to the core nonetheless.
The implementation simply builds on the existing file picker and only modifies 3 files, so the added maintenance burden should be quite small. The code itself is not particularly elegant (in my rather inexperienced opinion), but I did not want to over-complicate things. This is also the reason why some features might be missing.
Feel free to modify this PR or simply make suggestions, I'd be happy to improve it. This is also my first PR here, so sorry if I miss anything.