Example code is shown below:
M.setup = function(setup_config)
if setup_config.base_dir then
error("'base_dir' is no longer a valid value for setup. See 'base_dirs'")
end
base_dirs = setup_config.base_dirs or nil
hidden_files = setup_config.hidden_files or false
--bufferline = setup_config.bufferline
--if bufferline then
--local bl = require 'bufferline'
_git.update_git_repos(base_dirs)
-- set auto command to automatically detect the project root directory
vim.cmd "au BufEnter * lua require'telescope._extensions.project.main'.set_cwd_if_project(vim.api.nvim_buf_get_name(0))"
end
M.set_cwd_if_project = function(full_filepath)
local project_path = _utils.get_project_path_from_filepath(full_filepath)
--local pid = vim.fn.getpid()
print("Triggered", full_filepath)
vim.fn.chdir("/home")
--print(pid)
--vim.cmd("echo pid")
--print(project_path)
--_utils.change_project_dir(project_path)
--print(cd_successful)
end
I set the autocommand (au BufEnter) in the setup function.
I checked the result from print("Triggered", full_filepath), and it works correctly with correct filepath.
However, the problem is that the command vim.fn.chdir("/home") does not work. I already tried another command, vim.cmd("cd /home"). But, it does not work.
It seems that while the function is called, the directory is not changed. This is because other commands (e.g., vim.fn.getpid()) work correctly.
The weird point is that if I directly use lua require'telescope._extensions.project.main'.set_cwd_if_project(vim.api.nvim_buf_get_name(0))" from the vim, the chdir works correctly.
Why the problem arrives?