Skip to content

Commit

Permalink
feat: clean up rooter and add toggle function
Browse files Browse the repository at this point in the history
  • Loading branch information
mehalter committed Jan 26, 2024
1 parent 346a133 commit 83a48bb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 41 deletions.
11 changes: 0 additions & 11 deletions lua/astrocore/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
31 changes: 17 additions & 14 deletions lua/astrocore/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
35 changes: 19 additions & 16 deletions lua/astrocore/rooter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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 ""
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lua/astrocore/toggles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 83a48bb

Please sign in to comment.