kelly-lin/ranger.nvim

delete netrw FileExplorer function instead of fully disabling

life00 opened this issue · 4 comments

life00 commented

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 commented

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.

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.

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?

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.