Add findroot function for selecting telescope file search base dir
This commit is contained in:
parent
8725cbfe54
commit
80cbab3afb
3 changed files with 79 additions and 20 deletions
50
lua/findroot.lua
Normal file
50
lua/findroot.lua
Normal file
|
@ -0,0 +1,50 @@
|
|||
local Path = require("plenary.path")
|
||||
local source_basedir_markers = {
|
||||
".telescope-root",
|
||||
".editorconfig",
|
||||
"build.sbt",
|
||||
"package.json",
|
||||
"setup.py",
|
||||
".git",
|
||||
}
|
||||
function find_upwards_any(base, filenames)
|
||||
local folder = Path:new(base)
|
||||
local root = Path.path.root(folder)
|
||||
|
||||
while folder:absolute() ~= root do
|
||||
for _, filename in ipairs(filenames) do
|
||||
local p = folder:joinpath(filename)
|
||||
if p:exists() then
|
||||
return p
|
||||
end
|
||||
end
|
||||
folder = folder:parent()
|
||||
end
|
||||
return ""
|
||||
end
|
||||
|
||||
local function find_dir(base)
|
||||
print("base: ", base)
|
||||
if base == nil then
|
||||
base = vim.loop.cwd()
|
||||
end
|
||||
--@type Path
|
||||
local search_root
|
||||
search_root = find_upwards_any(base, source_basedir_markers)
|
||||
print("search_root: ", search_root)
|
||||
if search_root ~= "" then
|
||||
local file = search_root:_split()
|
||||
if file[#file] == ".telescope-root" then
|
||||
local target = search_root:parent():joinpath(search_root:head(1))
|
||||
if target:exists() then
|
||||
return tostring(target)
|
||||
end
|
||||
else
|
||||
return tostring(search_root:parent())
|
||||
end
|
||||
else
|
||||
return base
|
||||
end
|
||||
end
|
||||
|
||||
return find_dir
|
Loading…
Add table
Add a link
Reference in a new issue