feat: migrate autoroot.fnl
This commit is contained in:
parent
782836bc7d
commit
fa1b2b49a1
2 changed files with 37 additions and 25 deletions
21
fnl/scripts/autoroot.fnl
Normal file
21
fnl/scripts/autoroot.fnl
Normal 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})))}
|
33
lua/scripts/autoroot.lua
generated
33
lua/scripts/autoroot.lua
generated
|
@ -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()
|
|
||||||
-- Array of file names indicating root directory. Modify to your liking.
|
|
||||||
local root_names = {".envrc", ".git", "Makefile", ".svn", ".hg"}
|
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
|
|
||||||
|
|
Loading…
Reference in a new issue