diff --git a/lua/astrocore/config.lua b/lua/astrocore/config.lua index c393c22..dc41ac6 100644 --- a/lua/astrocore/config.lua +++ b/lua/astrocore/config.lua @@ -249,17 +249,6 @@ local M = { notifications = true, }, git_worktrees = nil, - -- enable by default once tested - -- rooter = { - -- detector = { "lsp", { ".git" } }, - -- ignore = { - -- dirs = {}, - -- servers = {}, - -- }, - -- scope = "global", - -- autochdir = true, - -- notify = false, - -- }, rooter = false, sessions = { autosave = { last = true, cwd = true }, diff --git a/lua/astrocore/init.lua b/lua/astrocore/init.lua index f7e8ade..3fd04c1 100644 --- a/lua/astrocore/init.lua +++ b/lua/astrocore/init.lua @@ -374,6 +374,7 @@ function M.setup(opts) end end + -- initialize rooter if M.config.rooter then local root_config = M.config.rooter --[[@as AstroCoreRooterOpts]] vim.api.nvim_create_user_command( @@ -388,26 +389,28 @@ function M.setup(opts) ) local group = vim.api.nvim_create_augroup("rooter", { clear = true }) -- clear the augroup no matter what - if root_config.autochdir then - vim.api.nvim_create_autocmd({ "VimEnter", "BufEnter" }, { + vim.api.nvim_create_autocmd({ "VimEnter", "BufEnter" }, { + nested = true, + group = group, + desc = "Root detection when entering a buffer", + callback = function(args) + if root_config.autochdir then require("astrocore.rooter").root(args.buf) end + end, + }) + if vim.tbl_contains(root_config.detector or {}, "lsp") then + vim.api.nvim_create_autocmd("LspAttach", { nested = true, group = group, - desc = "Root detection when entering a buffer", - callback = function(args) require("astrocore.rooter").root(args.buf) end, - }) - if vim.tbl_contains(root_config.detector or {}, "lsp") then - vim.api.nvim_create_autocmd("LspAttach", { - nested = true, - group = group, - desc = "Root detection on LSP attach", - callback = function(args) + desc = "Root detection on LSP attach", + callback = function(args) + if root_config.autochdir then local server = assert(vim.lsp.get_client_by_id(args.data.client_id)).name if not vim.tbl_contains(vim.tbl_get(root_config, "ignore", "servers") or {}, server) then require("astrocore.rooter").root(args.buf) end - end, - }) - end + end + end, + }) end end end diff --git a/lua/astrocore/rooter.lua b/lua/astrocore/rooter.lua index a54dbfc..5f4d070 100644 --- a/lua/astrocore/rooter.lua +++ b/lua/astrocore/rooter.lua @@ -11,6 +11,11 @@ local M = { detectors = {} } ---@type AstroCoreRooterOpts local config = require("astrocore").config.rooter +local vim_autochdir + +local notify = function(msg, level) + require("astrocore").notify(msg, level or vim.log.levels.INFO, { title = "AstroNvim Rooter" }) +end ---@class AstroCoreRooterRoot ---@field paths string[] @@ -119,13 +124,13 @@ function M.detect(bufnr, all) end --- Get information information about the current root ----@param silent integer? whether or not to notify with verbose details ----@return string the currently detected root -function M.info(silent) - local roots = M.detect(0, true) - if not silent then +function M.info() + local lines = {} + if vim_autochdir then + table.insert(lines, "Rooting disabled when `autochdir` is set") + else + local roots = M.detect(0, true) local first = true - local lines = {} for _, root in ipairs(roots) do for _, path in ipairs(root.paths) do local surround = first and "**" or "" @@ -154,9 +159,8 @@ function M.info(silent) if setting then table.insert(lines, key .. " = " .. vim.inspect(setting)) end end table.insert(lines, "```") - require("astrocore").notify(table.concat(lines, "\n"), vim.log.levels.INFO, { title = "AstroNvim Rooter" }) end - return roots[1] and roots[1].paths[1] or vim.loop.cwd() + notify(table.concat(lines, "\n")) end --- Set the current directory to a given root @@ -177,7 +181,7 @@ function M.set_pwd(root) vim.api.nvim_err_writeln(("Unable to parse scope: %s"):format(config.scope)) end - if config.notify then vim.notify("Set CWD to " .. path .. " using " .. vim.inspect(root.spec)) end + if config.notify then notify(("Set CWD to `%s`"):format(path)) end end return true end @@ -205,17 +209,16 @@ function M.exists(path) return vim.fn.empty(vim.fn.glob(path)) == 0 end function M.root(bufnr) -- add `autochdir` protection local autochdir = vim.opt.autochdir:get() - if not M.disabled and autochdir then - require("astrocore").notify("AstroNvim's rooter disabled, unset `autochdir` to re-enable", vim.log.levels.WARN) - M.disabled = true - elseif M.disabled and not autochdir then - M.disabled = false + if not vim_autochdir and autochdir then + notify("Rooting disabled, unset `autochdir` to re-enable", vim.log.levels.WARN) + vim_autochdir = true + elseif vim_autochdir and not autochdir then + vim_autochdir = false end - if M.disabled or vim.v.vim_did_enter == 0 then return end + if vim_autochdir or vim.v.vim_did_enter == 0 then return end if not bufnr or bufnr == 0 then bufnr = vim.api.nvim_get_current_buf() end - local root = M.detect(bufnr)[1] if root then M.set_pwd(root) end end diff --git a/lua/astrocore/toggles.lua b/lua/astrocore/toggles.lua index 76511bc..69c8fd2 100644 --- a/lua/astrocore/toggles.lua +++ b/lua/astrocore/toggles.lua @@ -12,6 +12,18 @@ local M = {} local function bool2str(bool) return bool and "on" or "off" end local function ui_notify(silent, ...) return not silent and require("astrocore").notify(...) end +--- Toggle rooter autochdir +---@param silent? boolean if true then don't sent a notification +function M.autochdir(silent) + local root_config = require("astrocore").config.rooter + if not root_config then + ui_notify(silent, "Rooter not configured") + else + root_config.autochdir = not root_config.autochdir + ui_notify(silent, ("Rooter autochdir %s"):format(bool2str(root_config.autochdir))) + end +end + --- Toggle notifications for UI toggles ---@param silent? boolean if true then don't sent a notification function M.notifications(silent)