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 = {}
M.setup = function()
-- 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)
-- [nfnl] Compiled from ./fnl/scripts/autoroot.fnl by https://github.com/Olical/nfnl, do not edit.
local function _1_()
local root_names = {".envrc", ".git", "Makefile", ".svn", ".hg"}
local root_cache = {}
local set_root = function()
-- Get directory path to start search from
local function set_root()
local path = vim.api.nvim_buf_get_name(0)
if path == "" then
return
if (path == "") then
return
else
end
path = vim.fs.dirname(path)
-- Try cache and resort to searching upward for root directory
local root = root_cache[path]
if root == nil then
local root_file = vim.fs.find(root_names, { path = path, upward = true })[1]
if root_file == nil then
return
if (root == nil) then
local root_file = vim.fs.find(root_names, {path = path, upward = true})[1]
if (root_file == nil) then
return
else
end
root = vim.fs.dirname(root_file)
root_cache[path] = root
else
end
-- Set current directory
vim.fn.chdir(root)
return vim.fn.chdir(root)
end
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
return M
return {setup = _1_}