Add directory-scoped Telescope pickers and smart folding
This commit is contained in:
parent
fedaece719
commit
7affddc1b8
3 changed files with 101 additions and 20 deletions
|
|
@ -167,6 +167,47 @@ return {
|
||||||
end,
|
end,
|
||||||
desc = "Find Words",
|
desc = "Find Words",
|
||||||
},
|
},
|
||||||
|
-- Find Words in Directory (scoped)
|
||||||
|
{
|
||||||
|
"<leader>fW",
|
||||||
|
function()
|
||||||
|
U.pick_directory_then(function(dir)
|
||||||
|
U.cmd_executable("rg", {
|
||||||
|
function()
|
||||||
|
require("telescope.builtin").live_grep({
|
||||||
|
cwd = dir,
|
||||||
|
hidden = true,
|
||||||
|
follow = true,
|
||||||
|
no_ignore = true,
|
||||||
|
no_ignore_parent = true,
|
||||||
|
prompt_title = "Find Words in " .. vim.fn.fnamemodify(dir, ":~:."),
|
||||||
|
})
|
||||||
|
end,
|
||||||
|
function()
|
||||||
|
vim.notify("rg not installed, find words will not function.", 3)
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end)
|
||||||
|
end,
|
||||||
|
desc = "Find Words in Directory",
|
||||||
|
},
|
||||||
|
-- Find Files in Directory (scoped)
|
||||||
|
{
|
||||||
|
"<leader>fF",
|
||||||
|
function()
|
||||||
|
U.pick_directory_then(function(dir)
|
||||||
|
require("telescope.builtin").find_files({
|
||||||
|
cwd = dir,
|
||||||
|
hidden = true,
|
||||||
|
follow = true,
|
||||||
|
no_ignore = true,
|
||||||
|
no_ignore_parent = true,
|
||||||
|
prompt_title = "Find Files in " .. vim.fn.fnamemodify(dir, ":~:."),
|
||||||
|
})
|
||||||
|
end)
|
||||||
|
end,
|
||||||
|
desc = "Find Files in Directory",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"<leader>fc",
|
"<leader>fc",
|
||||||
function()
|
function()
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,43 @@
|
||||||
vim.opt.foldmethod = "expr"
|
-- Smart folding: native treesitter for small files, indent for large files
|
||||||
vim.opt.foldexpr = "nvim_treesitter#foldexpr()"
|
local LARGE_FILE_THRESHOLD = 1000
|
||||||
|
|
||||||
|
local function setup_folding()
|
||||||
|
local line_count = vim.api.nvim_buf_line_count(0)
|
||||||
|
|
||||||
|
if line_count > LARGE_FILE_THRESHOLD then
|
||||||
|
-- Large files: indent-based folding (fast)
|
||||||
|
vim.opt_local.foldmethod = "indent"
|
||||||
|
else
|
||||||
|
-- Normal files: native treesitter folding
|
||||||
|
vim.opt_local.foldmethod = "expr"
|
||||||
|
vim.opt_local.foldexpr = "v:lua.vim.treesitter.foldexpr()"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_create_autocmd({ "BufReadPost", "FileType" }, {
|
||||||
|
group = vim.api.nvim_create_augroup("myconfig-smart-folding", { clear = true }),
|
||||||
|
callback = setup_folding,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Global fold settings
|
||||||
vim.opt.foldcolumn = "0"
|
vim.opt.foldcolumn = "0"
|
||||||
vim.opt.foldtext = ""
|
vim.opt.foldtext = ""
|
||||||
|
|
||||||
vim.opt.foldlevel = 99
|
vim.opt.foldlevel = 99
|
||||||
vim.opt.foldlevelstart = 99
|
vim.opt.foldlevelstart = 99
|
||||||
|
|
||||||
vim.opt.foldnestmax = 3
|
vim.opt.foldnestmax = 3
|
||||||
|
|
||||||
vim.keymap.set('n', '<leader>z', function()
|
-- Toggle all folds with <leader>z
|
||||||
local any_fold_open = false
|
vim.keymap.set("n", "<leader>z", function()
|
||||||
for lnum = 1, vim.fn.line('$') do
|
local any_fold_closed = false
|
||||||
|
for lnum = 1, vim.fn.line("$") do
|
||||||
if vim.fn.foldclosed(lnum) ~= -1 then
|
if vim.fn.foldclosed(lnum) ~= -1 then
|
||||||
any_fold_open = true
|
any_fold_closed = true
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if any_fold_open then
|
if any_fold_closed then
|
||||||
-- There's at least one closed fold, so open all
|
vim.cmd("normal! zR")
|
||||||
vim.cmd('normal! zR')
|
|
||||||
else
|
else
|
||||||
-- All folds are open, so close all
|
vim.cmd("normal! zM")
|
||||||
vim.cmd('normal! zM')
|
|
||||||
end
|
end
|
||||||
end, { noremap = true, silent = true })
|
end, { noremap = true, silent = true, desc = "Toggle all folds" })
|
||||||
|
|
|
||||||
24
lua/util.lua
24
lua/util.lua
|
|
@ -176,4 +176,28 @@ function M.fnZero()
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Opens telescope file browser to pick a directory, then calls callback with the selected path.
|
||||||
|
--- Starts at the current buffer's parent directory for quick access.
|
||||||
|
---@param callback fun(dir: string): nil Function to call with selected directory path
|
||||||
|
function M.pick_directory_then(callback)
|
||||||
|
local actions = require("telescope.actions")
|
||||||
|
local action_state = require("telescope.actions.state")
|
||||||
|
|
||||||
|
require("telescope").extensions.file_browser.file_browser({
|
||||||
|
path = vim.fn.expand("%:p:h"),
|
||||||
|
select_buffer = true,
|
||||||
|
files = false, -- Only show directories
|
||||||
|
depth = false,
|
||||||
|
attach_mappings = function(prompt_bufnr, _)
|
||||||
|
actions.select_default:replace(function()
|
||||||
|
local entry = action_state.get_selected_entry()
|
||||||
|
local dir = entry and entry.path or vim.fn.expand("%:p:h")
|
||||||
|
actions.close(prompt_bufnr)
|
||||||
|
callback(dir)
|
||||||
|
end)
|
||||||
|
return true
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue