mirror of
https://github.com/youwen5/neovim-flake.git
synced 2024-11-24 19:03:49 -08:00
feat: automatically set root directory based on heuristics
This commit is contained in:
parent
6ab1d3adfb
commit
cb70a0c30a
1 changed files with 32 additions and 0 deletions
|
@ -62,5 +62,37 @@
|
||||||
pattern = "LspProgressStatusUpdated",
|
pattern = "LspProgressStatusUpdated",
|
||||||
callback = require("lualine").refresh,
|
callback = require("lualine").refresh,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
-- Array of file names indicating root directory. Modify to your liking.
|
||||||
|
local root_names = { '.git', 'Makefile', '.svn', '.hg' }
|
||||||
|
|
||||||
|
-- Cache to use for speed up (at cost of possibly outdated results)
|
||||||
|
local root_cache = {}
|
||||||
|
|
||||||
|
local set_root = function()
|
||||||
|
-- Get directory path to start search from
|
||||||
|
local path = vim.api.nvim_buf_get_name(0)
|
||||||
|
if path == "" then return 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 end
|
||||||
|
root = vim.fs.dirname(root_file)
|
||||||
|
root_cache[path] = root
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Set current directory
|
||||||
|
vim.fn.chdir(root)
|
||||||
|
|
||||||
|
-- Run direnv
|
||||||
|
vim.cmd "DirenvExport"
|
||||||
|
vim.cmd "LspRestart"
|
||||||
|
end
|
||||||
|
|
||||||
|
local root_augroup = vim.api.nvim_create_augroup('MyAutoRoot', {})
|
||||||
|
vim.api.nvim_create_autocmd('BufEnter', { group = root_augroup, callback = set_root })
|
||||||
'';
|
'';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue