feat: migrate autoroot.fnl

This commit is contained in:
Youwen Wu 2025-02-01 13:56:22 -08:00
parent 782836bc7d
commit fa1b2b49a1
Signed by: youwen5
GPG key ID: 865658ED1FE61EC3
2 changed files with 37 additions and 25 deletions

21
fnl/scripts/autoroot.fnl Normal file
View file

@ -0,0 +1,21 @@
{:setup (fn []
(let [root-names [:.envrc :.git :Makefile :.svn :.hg]
root-cache {}]
(fn set-root []
(var path (vim.api.nvim_buf_get_name 0))
(when (= path "") (lua "return "))
(set path (vim.fs.dirname path))
(var root (. root-cache path))
(when (= root nil)
(local root-file (. (vim.fs.find root-names
{: path :upward true})
1))
(when (= root-file nil) (lua "return "))
(set root (vim.fs.dirname root-file))
(tset root-cache path root))
(vim.fn.chdir root))
(local root-augroup (vim.api.nvim_create_augroup :MyAutoRoot {}))
(vim.api.nvim_create_autocmd :BufEnter
{:callback set-root
:group root-augroup})))}

View file

@ -1,37 +1,28 @@
local M = {} -- [nfnl] Compiled from ./fnl/scripts/autoroot.fnl by https://github.com/Olical/nfnl, do not edit.
local function _1_()
M.setup = function() local root_names = {".envrc", ".git", "Makefile", ".svn", ".hg"}
-- Array of file names indicating root directory. Modify to your liking.
local root_names = { ".envrc", ".git", "Makefile", ".svn", ".hg" }
-- Cache to use for speed up (at cost of possibly outdated results)
local root_cache = {} local root_cache = {}
local function set_root()
local set_root = function()
-- Get directory path to start search from
local path = vim.api.nvim_buf_get_name(0) local path = vim.api.nvim_buf_get_name(0)
if path == "" then if (path == "") then
return return
else
end end
path = vim.fs.dirname(path) path = vim.fs.dirname(path)
-- Try cache and resort to searching upward for root directory
local root = root_cache[path] local root = root_cache[path]
if root == nil then if (root == nil) then
local root_file = vim.fs.find(root_names, { path = path, upward = true })[1] local root_file = vim.fs.find(root_names, {path = path, upward = true})[1]
if root_file == nil then if (root_file == nil) then
return return
else
end end
root = vim.fs.dirname(root_file) root = vim.fs.dirname(root_file)
root_cache[path] = root root_cache[path] = root
else
end end
return vim.fn.chdir(root)
-- Set current directory
vim.fn.chdir(root)
end end
local root_augroup = vim.api.nvim_create_augroup("MyAutoRoot", {}) local root_augroup = vim.api.nvim_create_augroup("MyAutoRoot", {})
vim.api.nvim_create_autocmd("BufEnter", { group = root_augroup, callback = set_root }) return vim.api.nvim_create_autocmd("BufEnter", {callback = set_root, group = root_augroup})
end end
return {setup = _1_}
return M