From de96d840182e530bb61f59eaa187f18cdab76e51 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 22 Aug 2022 03:12:45 +0200 Subject: [PATCH] add fallback_buffer_dir option Change root directory to buffer directory if file is outside a project --- README.md | 3 +++ lua/project_nvim/config.lua | 3 +++ lua/project_nvim/project.lua | 19 +++++++++++-------- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3218f47..e4f9e21 100644 --- a/README.md +++ b/README.md @@ -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"), diff --git a/lua/project_nvim/config.lua b/lua/project_nvim/config.lua index 431c3d4..c6cc199 100644 --- a/lua/project_nvim/config.lua +++ b/lua/project_nvim/config.lua @@ -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"), diff --git a/lua/project_nvim/project.lua b/lua/project_nvim/project.lua index 28fa4b7..b06740c 100644 --- a/lua/project_nvim/project.lua +++ b/lua/project_nvim/project.lua @@ -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()