project.nvim icon indicating copy to clipboard operation
project.nvim copied to clipboard

feat: add patterns fallback

Open younger-1 opened this issue 3 years ago • 4 comments

This pr add a setup option patterns_fallback which is false by default. When it is set to ture and all patterns fail to match, project.nvim will change the directory to current buffer's location. vim-rooter also support this kind of setting

see also #56

younger-1 avatar Feb 28 '22 05:02 younger-1

@ahmedkhalf Any thought on the fallback behavior?

zhengpd avatar May 09 '22 06:05 zhengpd

Here's the non-intrusive workaround I'using before project.nvim got an option to fallback to non-project directory:

require'project_nvim'.setup { manual_mode = true }

local fix_project_root = function()
  local prj = require'project_nvim.project'
  local root = prj.get_project_root()
  if root then
    prj.on_buf_enter()
  else
    vim.cmd('lcd ' .. vim.fn.fnameescape(vim.fn.expand('%:p:h')))
  end
end
vim.api.nvim_create_user_command('ProjectRoot', fix_project_root, { force = true})
vim.cmd('autocmd BufEnter * ProjectRoot')

zhengpd avatar May 09 '22 06:05 zhengpd

Thanks for your sharing. Your workaround is a bit overhead when switch buffers in different project very often as it would call get_project_root twice if root could be found.

This inspires me that I want to add a cache which map each buffer to it's root and each time switch to buffer, lookup cache first and then call get_project_root to set cache entry. I thinks it could have better performance.

younger-1 avatar May 09 '22 18:05 younger-1

The option could be called autochdir (bc it does exactly what autochdir does) and it seems more reasonable to specify it in the detection_methods list:

{
  -- Manual mode doesn't automatically change your root directory, so you have
  -- the option to manually do so using `:ProjectRoot` command.
  manual_mode = false,

  -- Methods of detecting the root directory. **"lsp"** uses the native neovim
  -- lsp, while **"pattern"** uses vim-rooter like glob pattern matching. Here
  -- order matters: if one is not detected, the other is used as fallback. You
  -- can also delete or rearangne the detection methods.
  detection_methods = { "lsp", "pattern", "autochdir" },

myarcana avatar Mar 24 '23 21:03 myarcana