Add findroot function for selecting telescope file search base dir
This commit is contained in:
parent
8725cbfe54
commit
80cbab3afb
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
|
3
lua/plugins/luadev.lua
Normal file
3
lua/plugins/luadev.lua
Normal file
|
@ -0,0 +1,3 @@
|
|||
return {
|
||||
"bfredl/nvim-luadev",
|
||||
}
|
|
@ -12,8 +12,14 @@ return {
|
|||
keys = function()
|
||||
local builtin = require("telescope.builtin")
|
||||
local utils = require("telescope.utils")
|
||||
-- stylua: ignore start
|
||||
return {
|
||||
{ ',,', function() builtin.fd { cwd = utils.buffer_dir() } end, desc = "Find files in current file's dir" },
|
||||
{ ',,',
|
||||
function()
|
||||
builtin.fd { cwd = require("findroot")(utils.buffer_dir()) }
|
||||
end,
|
||||
desc = "Find files in base dir (defined by .telescope-root file)"
|
||||
},
|
||||
{ ',ff', builtin.fd, desc = "Find files" },
|
||||
{ ',fg', builtin.git_files, desc = "Find files (git)" },
|
||||
{ ',gs', builtin.git_status, desc = "Git status" },
|
||||
|
@ -27,6 +33,7 @@ return {
|
|||
{ "<leader>;", builtin.commands, desc = "Commands" },
|
||||
}
|
||||
end,
|
||||
--stylua: ignore end
|
||||
opts = {
|
||||
defaults = {
|
||||
prompt_prefix = " ",
|
||||
|
@ -34,20 +41,19 @@ return {
|
|||
},
|
||||
extensions = {
|
||||
fzf = {
|
||||
fuzzy = true, -- false will only do exact matching
|
||||
override_generic_sorter = true, -- override the generic sorter
|
||||
override_file_sorter = true, -- override the file sorter
|
||||
case_mode = "smart_case", -- or "ignore_case" or "respect_case"
|
||||
-- the default case_mode is "smart_case"
|
||||
fuzzy = true,
|
||||
override_generic_sorter = true,
|
||||
override_file_sorter = true,
|
||||
case_mode = "smart_case",
|
||||
},
|
||||
["ui-select"] = {
|
||||
require("telescope.themes").get_dropdown {}
|
||||
require("telescope.themes").get_dropdown({}),
|
||||
},
|
||||
},
|
||||
},
|
||||
config = function(_, opts)
|
||||
local telescope = require('telescope')
|
||||
local telescope = require("telescope")
|
||||
telescope.setup(opts)
|
||||
telescope.load_extension('fzf')
|
||||
telescope.load_extension("fzf")
|
||||
end,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue