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,
|
||||
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",
|
||||
function()
|
||||
|
|
|
|||
|
|
@ -1,27 +1,43 @@
|
|||
vim.opt.foldmethod = "expr"
|
||||
vim.opt.foldexpr = "nvim_treesitter#foldexpr()"
|
||||
-- Smart folding: native treesitter for small files, indent for large files
|
||||
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.foldtext = ""
|
||||
|
||||
vim.opt.foldlevel = 99
|
||||
vim.opt.foldlevelstart = 99
|
||||
|
||||
vim.opt.foldnestmax = 3
|
||||
|
||||
vim.keymap.set('n', '<leader>z', function()
|
||||
local any_fold_open = false
|
||||
for lnum = 1, vim.fn.line('$') do
|
||||
-- Toggle all folds with <leader>z
|
||||
vim.keymap.set("n", "<leader>z", function()
|
||||
local any_fold_closed = false
|
||||
for lnum = 1, vim.fn.line("$") do
|
||||
if vim.fn.foldclosed(lnum) ~= -1 then
|
||||
any_fold_open = true
|
||||
any_fold_closed = true
|
||||
break
|
||||
end
|
||||
end
|
||||
if any_fold_open then
|
||||
-- There's at least one closed fold, so open all
|
||||
vim.cmd('normal! zR')
|
||||
if any_fold_closed then
|
||||
vim.cmd("normal! zR")
|
||||
else
|
||||
-- All folds are open, so close all
|
||||
vim.cmd('normal! zM')
|
||||
vim.cmd("normal! zM")
|
||||
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
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue