Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fallback_buffer_dir option #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ use {
-- directory.
silent_chdir = true,

-- Change root directory to the buffer directory if it is outside a project
fallback_buffer_dir = false,

-- Path where project.nvim will store the project history for use in
-- telescope
datapath = vim.fn.stdpath("data"),
Expand Down
3 changes: 3 additions & 0 deletions lua/project_nvim/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ M.defaults = {
-- directory.
silent_chdir = true,

-- Change root directory to the buffer directory if it is outside a project
fallback_buffer_dir = false,

-- Path where project.nvim will store the project history for use in
-- telescope
datapath = vim.fn.stdpath("data"),
Expand Down
19 changes: 11 additions & 8 deletions lua/project_nvim/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,21 @@ function M.set_pwd(dir, method)
if dir ~= nil then
M.last_project = dir
table.insert(history.session_projects, dir)
elseif config.options.fallback_buffer_dir then
dir = vim.fn.expand("%:p:h", true)
method = "buffer directory fallback"
else
return false
end

if vim.fn.getcwd() ~= dir then
vim.api.nvim_set_current_dir(dir)
if vim.fn.getcwd() ~= dir then
vim.api.nvim_set_current_dir(dir)

if config.options.silent_chdir == false then
vim.notify("Set CWD to " .. dir .. " using " .. method)
end
if config.options.silent_chdir == false then
vim.notify("Set CWD to " .. dir .. " using " .. method)
end
return true
end

return false
return true
end

function M.get_project_root()
Expand Down