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

delete netrw FileExplorer function instead of fully disabling #18

Open
life00 opened this issue Feb 20, 2024 · 4 comments
Open

delete netrw FileExplorer function instead of fully disabling #18

life00 opened this issue Feb 20, 2024 · 4 comments

Comments

@life00
Copy link

life00 commented Feb 20, 2024

I recently was trying to figure out why I am not able to use open URL under cursor default neovim functionality. At some point I realized that I have set the option replace_netrw = true. When setting back to false the gx mapping functionality is as expected.

Does ranger.nvim fully disable netrw when set to replace it? Does it replace all netrw functionality? It doesn't seem so, gx mapping doesn't error, it just does nothing.

Maybe it would be possible for ranger.nvim to also replace this functionality of netrw (also hook up rifle for opening files)? Or maybe not disable netrw? It should be possible because plugins like neo-tree.nvim replace netrw directory browsing functionality without disabling it.

I actually really like this replace_netrw option, so I would not prefer to set it to false, but I still want gx mapping functionality.

@life00
Copy link
Author

life00 commented Feb 20, 2024

I found the code responsible for disabling netrw. When commenting it out it seems fine (gx mapping works), but when actually opening a folder it spawns both netrw and ranger (on top). Maybe just quickly close netrw window instead of fully disabling it? Again it seems ranger.nvim doesn't replace gx functionality so fully disabling netrw doesn't seem right.

@life00 life00 changed the title replace_netrw option and open URL under cursor functionality hijack netrw instead of replacing Feb 26, 2024
@life00
Copy link
Author

life00 commented Feb 26, 2024

I believe the most optimal solution would be to hijack netrw FileExplorer instead of fully disabling it. You may find useful this reddit thread. I am not a lua or vimscript dev, so sorry I can't even make that work because its in vimscript.

@life00
Copy link
Author

life00 commented Feb 26, 2024

I tried to convert some of that code into lua and I got the functionality I expect.

vim.api.nvim_create_autocmd("VimEnter", {
  pattern = { "*" },
  command = "silent! autocmd! FileExplorer",
})

Essentially while vim.g.loaded_netrw and vim.g.loaded_netrwPlugin are not set this deletes the FileExplorer autocmd for netrw on every VimEnter. This disables netrw FileExplorer functionality, so allows only ranger.nvim to start, and it does not disable gx file opening netrw functionality.

The only thing is that idk how to convert autocmd deletion to lua. I am also not sure if it works in all the cases?

@life00
Copy link
Author

life00 commented Mar 1, 2024

diff --git a/lua/ranger-nvim.lua b/lua/ranger-nvim.lua
index 3b18880..2e3bdb1 100644
--- a/lua/ranger-nvim.lua
+++ b/lua/ranger-nvim.lua
@@ -213,11 +213,12 @@ end

 ---Disable and replace netrw with ranger.
 local function replace_netrw()
-       vim.g.loaded_netrw = 1
-       vim.g.loaded_netrwPlugin = 1
        vim.api.nvim_create_autocmd("VimEnter", {
                pattern = "*",
                callback = function()
+                       if vim.fn.exists("#FileExplorer") then
+                               vim.api.nvim_create_augroup("FileExplorer", { clear = true })
+                       end
                        if vim.fn.isdirectory(vim.fn.argv(0)) == 1 then
                                M.open(false)
                        end

There you go. Got from stevearc/oil.nvim#182 (comment)
Add this patch and issue is fixed, however you may later revert it because neovim 0.10 implements gx mapping separate from netrw, so it will also be fixed when neovim 0.10 is released.

@life00 life00 changed the title hijack netrw instead of replacing delete netrw FileExplorer function instead of fully disabling Mar 1, 2024
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

No branches or pull requests

1 participant