fix(files): stop nvim.dir from showing if files is default explorer#2493
fix(files): stop nvim.dir from showing if files is default explorer#2493abeldekat wants to merge 1 commit into
nvim.dir from showing if files is default explorer#2493Conversation
Details: - See PR 40507 in neovim/neovim
|
In my config,
|
|
I am skeptical of this. I'd wait for that to merge and simply set the flag instead. |
|
I apologize for my short-sightedness. Basically, we're trying to figure out a new way to handle builtin neovim plugins more ergonomically and dir.lua is kind of the guinea pig.
I believe so? If you're talking about "dynamically" disabling dir.lua after startup, that should work... |
The to-be-maybe-restored documentation about
I.e. this will only work if 'mini.files' is loaded during startup. This might not be the case if it is lazy loaded (i.e. after startup). What is needed for 'mini.files' is the way to disable 'dir.lua' from hijacking directory buffers. The |
|
Working on a solution for this now. I will comment on this thread when it's done. Disabling Thanks for your patience. |
Details: - There is some work being done upstream on better default explorer (instead of `netrw`). Reacting to each its change is a bit too much for now. Temporarily disable tests for 'mini.files' as default explorer, but check on the upstream progress regularly to adjust 'mini.files' behavior and re-enable tests as soon as reasonably possible. Related to #2493
|
I've temporarily disabled the default explorer 'mini.files' tests on Nightly. Hopefully for not too long. |
|
@barrettruth, there seems to be more than one back-and-fourth upstream PRs regarding this issue. Is it resolved yet? |
|
After neovim/neovim#40529, overriding if config.options.use_as_default_explorer then
vim.api.nvim_create_autocmd('FileType', {
group = gr,
pattern = 'directory',
nested = true,
callback = function(args)
vim.bo[args.buf].bufhidden = 'wipe'
vim.b[args.buf].minifiles_processed_dir = true
MiniFiles.open(vim.api.nvim_buf_get_name(args.buf), false)
vim.schedule(function()
vim.api.nvim_set_current_win(MiniFiles.get_explorer_state().windows[1].win_id)
end)
end,
})
endStay tuned - just leaving this here as a progress update. |
To be clear, I am not asking to tweak 'mini.files'. The more appropriate question for The current approach of silently deleting explicitly documented augroup is fine and the same can be done for |
|
Disable netrw & dir.lua and everything works fine right now on master: vim.g.loaded_nvim_dir_plugin = 1
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
require('mini.files').setup({ options = { use_as_default_explorer = true } })This is pretty clear in the documentation now, looking at it from an objective context rather than with all this poorly-managed chaos I've created XD. Sorry for the confusion. Just to give you more context, the "right" way to handle this will be to override dir.lua and hook into This is because the idea of "is this buffer a directory" is being lifted off of mini.files and other file managers so as to simplify them. |
This only works if this code is executed during startup, but does not work if executed after startup (as it is documented). So with the following 'init.lua' ... vim.schedule(function() vim.g.loaded_nvim_dir_plugin = 1 end)... starting The reason this is needed is because I don't want users to have to manually set some |
|
Regarding this comment:
I think it's very nice that in the new situation The code below is tested on current nightly and v0.12.4: MiniFiles using FileType directorydiff --git a/lua/mini/files.lua b/lua/mini/files.lua
index 4728a338..68e49586 100644
--- a/lua/mini/files.lua
+++ b/lua/mini/files.lua
@@ -1366,19 +1366,35 @@ H.create_autocommands = function(config)
vim.api.nvim_create_autocmd(event, { group = gr, pattern = pattern, callback = callback, desc = desc })
end
- if config.options.use_as_default_explorer then
- -- Stop 'netrw' from showing. Needs `VimEnter` event autocommand if
- -- this is called prior 'netrw' is set up
- vim.cmd('silent! autocmd! FileExplorer *')
- vim.cmd('autocmd VimEnter * ++once silent! autocmd! FileExplorer *')
-
- -- - Use `nested` to allow other events (`BufWinEnter` for 'mini.clue')
- local opts = { nested = true, group = gr, callback = H.track_dir_edit, desc = 'Track directory edit' }
- vim.api.nvim_create_autocmd('BufEnter', opts)
- end
-
au('VimResized', '*', MiniFiles.refresh, 'Refresh on resize')
au('ColorScheme', '*', H.create_default_hl, 'Ensure colors')
+
+ if not config.options.use_as_default_explorer then return end
+
+ local set_as_default_explorer = vim.fn.has('nvim-0.13') == 0 and H.replace_netrw or H.replace_nvim_dir
+ set_as_default_explorer(gr)
+end
+
+-- TODO: Remove when support for nvim-v0.12 is dropped
+H.replace_netrw = function(gr)
+ -- Stop 'netrw' from showing. Needs `VimEnter` event autocommand if
+ -- this is called prior 'netrw' is set up
+ vim.cmd('silent! autocmd! FileExplorer *')
+ vim.cmd('autocmd VimEnter * ++once silent! autocmd! FileExplorer *')
+
+ -- - Use `nested` to allow other events (`BufWinEnter` for 'mini.clue')
+ local opts = { nested = true, group = gr, callback = H.track_dir_edit, desc = 'Track directory edit' }
+ vim.api.nvim_create_autocmd('BufEnter', opts)
+end
+
+H.replace_nvim_dir = function(gr)
+ -- Stop 'nvim.dir' from showing. Needs `VimEnter` event autocommand if
+ -- this is called prior 'nvim.dir' is set up
+ vim.cmd('silent! autocmd! nvim.dir *')
+ vim.cmd('autocmd VimEnter * ++once silent! autocmd! nvim.dir *')
+
+ local opts = { pattern = 'directory', group = gr, callback = H.track_dir_edit_by_ft, desc = 'Track directory edit' }
+ vim.api.nvim_create_autocmd('FileType', opts)
end
--stylua: ignore
@@ -1413,20 +1429,28 @@ H.normalize_opts = function(explorer_opts, opts)
end
-- Autocommands ---------------------------------------------------------------
+H.track_dir_edit_by_ft = function(data)
+ local buf_id = data.buf
+
+ -- Make directory buffer disappear when it is not needed
+ vim.bo[buf_id].bufhidden = 'wipe'
+
+ vim.schedule(function()
+ -- Open directory without history
+ MiniFiles.open(data.file, false)
+ -- Smartly delete directory buffer if already visited
+ local opts = { once = true, nested = true, buf = buf_id, callback = H.wipe_directory_buffer }
+ vim.api.nvim_create_autocmd('BufEnter', opts)
+ end)
+end
+
+-- TODO: Remove when support for nvim-v0.12 is dropped
H.track_dir_edit = function(data)
-- Make early returns
if vim.api.nvim_get_current_buf() ~= data.buf then return end
- if vim.b.minifiles_processed_dir then
- -- Smartly delete directory buffer if already visited
- local alt_buf = vim.fn.bufnr('#')
- -- - Setting alternative buffer is enough for the "directory buffer" to be
- -- wiped out, as it has `bufhidden=wipe`. Forcing delete after showing alt
- -- buffer might result in hard-to-track errors (like when opening directory
- -- in 'mini.pick' when there is an altbuf for the buffer in target window).
- if alt_buf ~= data.buf and vim.fn.buflisted(alt_buf) == 1 then return vim.api.nvim_win_set_buf(0, alt_buf) end
- return vim.api.nvim_buf_delete(data.buf, { force = true })
- end
+ -- Smartly delete directory buffer if already visited
+ if vim.b.minifiles_processed_dir then return H.wipe_directory_buffer(data) end
local path = vim.api.nvim_buf_get_name(0)
if vim.fn.isdirectory(path) ~= 1 then return end
@@ -1439,6 +1463,16 @@ H.track_dir_edit = function(data)
vim.schedule(function() MiniFiles.open(path, false) end)
end
+H.wipe_directory_buffer = function(data)
+ local alt_buf = vim.fn.bufnr('#')
+ -- - Setting alternative buffer is enough for the "directory buffer" to be
+ -- wiped out, as it has `bufhidden=wipe`. Forcing delete after showing alt
+ -- buffer might result in hard-to-track errors (like when opening directory
+ -- in 'mini.pick' when there is an altbuf for the buffer in target window).
+ if alt_buf ~= data.buf and vim.fn.buflisted(alt_buf) == 1 then return vim.api.nvim_win_set_buf(0, alt_buf) end
+ return vim.api.nvim_buf_delete(data.buf, { force = true })
+end
+
-- Explorers ------------------------------------------------------------------
---@class Explorer
---
diff --git a/tests/test_files.lua b/tests/test_files.lua
index c18eb85e..f90814b4 100644
--- a/tests/test_files.lua
+++ b/tests/test_files.lua
@@ -6289,15 +6289,7 @@ T['LSP']['respects `options.lsp_timeout`'] = function()
eq(child.lua_get('_G.lsp_requests'), {})
end
-T['Default explorer'] = new_set({
- hooks = {
- pre_case = function()
- if child.fn.has('nvim-0.13') == 1 then
- MiniTest.skip('Default explorer is being reworked on Nightly, so tests are not stable')
- end
- end,
- },
-})
+T['Default explorer'] = new_set()
T['Default explorer']['works on startup'] = function()
vim.loop.os_setenv('USE_AS_DEFAULT_EXPLORER', 'true') |
It is nice, but it is also too early to take advantage of that. The Coincidentally, I am just finishing dealing with this locally. |
Resolve #2493 Co-authored-by: abeldekat <58370433+abeldekat@users.noreply.github.com>
|
I've decided to go ahead and apply the similar in spirit change and enabled the tests on Nightly. Hopefully, the "delete |


Details:
Tests that failed:
Default explorer | works on startup:Default explorer | works in:edit .`Default explorer | works in:tabfind .`Default explorer | handles close without opening fileSurprisingly, test
Default explorer works in :vsplitdid not fail.Probablybecause of thevsplit...)Before the fix,
vim.fn.getbufinfo(1):... variables = { changedtick = 4, current_syntax = "directory", minifiles_processed_dir = true, nvim_dir = "/home/user/.config/repro" }, ...After:
... variables = { changedtick = 3, minifiles_processed_dir = true }, ...