cloudhead / neovim-fuzzy

Minimalistic fuzzy file finding for neovim
BSD 3-Clause "New" or "Revised" License
111 stars 17 forks source link

When opening a new file using neovim-fuzzy the cwd for the "current" tab is changed to the git root #46

Open insidewhy opened 4 years ago

insidewhy commented 4 years ago

This happens when fuzzy_opencmd is set to tabe (or tab drop or any other command that opens the result in a new tab).

e.g. say I am in a project called project inside subdirectory subdir which contains two files, file1 and file2.

When I open the first file with neovim-fuzzy, say file1 then the file1 tab corrrectly has project/subdir as its cwd. Now if I open a second file while the file1 tab is focussed, say file2 then the new tab with file2 in it has the correct cwd of project/subdir but the file1 tab now has a cwd of project.

Then if I were to open a third file with neovim-fuzzy, while looking at the file2 tab, now that tab will also have a cwd of the project root. It always sets the cwd of the "current" tab to the project root.

insidewhy commented 4 years ago

BTW tested this with neovim-fuzzy as the only plugin enabled, and it still happened.

insidewhy commented 4 years ago

Here is my workaround:

fun s:FuzzyOpener(val)
  let prev_tabpage = tabpagenr()
  exe "tabe" a:val
  let next_tabpage = tabpagenr()
  exe "tabnext" prev_tabpage
  lcd -
  let dir = getcwd()
  exe "tabnext" next_tabpage
  exe "lcd" dir
  " neovim-fuzzy will execute "lcd -" so ensure the previous directory is the
  " one we want
  lcd /tmp
endfun

command! -nargs=1 FuzzyOpener call s:FuzzyOpener(<q-args>)

let g:fuzzy_opencmd = 'FuzzyOpener'  

It is dirty but it works.

insidewhy commented 4 years ago

The correct fix would be to change this bit of code: https://github.com/cloudhead/neovim-fuzzy/blob/53383395befafce802c902c21b54847074454491/plugin/neovim-fuzzy.vim#L221

It would have to store the current tab before running the open command, and then check if the tab has changed. When it has, then it should switch to the previous tab and run lcd to the directory of the new tab, before switching back to the new tab.

insidewhy commented 4 years ago

Or alternatively it could just not lcd at all and instead resolve the path to open with respect to self.root.