echasnovski / mini.nvim

Library of 40+ independent Lua modules improving overall Neovim (version 0.8 and higher) experience with minimal effort
MIT License
4.84k stars 183 forks source link

Error when opening popup with current path of not saved buffer #592

Closed Susensio closed 9 months ago

Susensio commented 9 months ago

Contributing guidelines

Module(s)

mini.files

Description

I have a keymap set to MiniFiles.open(vim.api.nvim_buf_get_name(0)) as stated on the docs. https://github.com/echasnovski/mini.nvim/blob/964fab7fecd14f66a69a96162f8aa816480690fd/doc/mini-files.txt#L633-L634 It does not work if current buffer is a recently created file and not saved.

Neovim version

0.9.4

Steps to reproduce

  1. open nvim
  2. edit new file :e test.txt
  3. open MiniFiles with focus on current file (this is configured as a mapping) :lua MiniFiles.open(vim.api.nvim_buf_get_name(0))
  4. get an error

Expected behavior

I would expect MiniFiles to handle unsaved buffers gracefully.

Actual behavior

Error is shown, and no popup is shown

E5108: Error executing lua (mini.files) `path` is not a valid path ("/home/susensio/.config/nvim/text.txt")
stack traceback:
        [C]: in function 'error'
        ...nsio/.local/share/nvim/lazy/mini.nvim/lua/mini/files.lua:2530: in function 'error'
        ...nsio/.local/share/nvim/lazy/mini.nvim/lua/mini/files.lua:670: in function 'open'
        [string ":lua"]:1: in main chunk
echasnovski commented 9 months ago

Thanks for the issue!

This seems to work as expected. The reason is that this is not a "not save buffer" but rather a "not existing path". When you use MiniFiles.open(), its value should be a valid path that actually exist. If it doesn't exist, then it is not really clear what explorer should show.

If it is actually an existing file but in modified buffer, then 'mini.files' works as expected.

Susensio commented 9 months ago

Maybe a fallback would be nice. But I agree with you, it is not clear what to show. The cwd? The parent directory (if exists)?

I usually edit new files in existing directories, so showing the parent works better for me.

This is what I came up with. It shows the nearest parent directory.

local get_parent = vim.fs.dirname
local exists = function(path) return vim.loop.fs_stat(path) ~= nil end
local path = vim.api.nvim_buf_get_name(0)

while not exists(path) do
  path = get_parent(path)
end

MiniFiles.open(path)