diff --git a/lua/plugins/telescope.lua b/lua/plugins/telescope.lua index a4fcf4f..b9697ee 100644 --- a/lua/plugins/telescope.lua +++ b/lua/plugins/telescope.lua @@ -167,6 +167,47 @@ return { end, desc = "Find Words", }, + -- Find Words in Directory (scoped) + { + "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) + { + "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", + }, { "fc", function() diff --git a/lua/tools/folding.lua b/lua/tools/folding.lua index 19d61bd..970ba62 100644 --- a/lua/tools/folding.lua +++ b/lua/tools/folding.lua @@ -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', 'z', function() - local any_fold_open = false - for lnum = 1, vim.fn.line('$') do - if vim.fn.foldclosed(lnum) ~= -1 then - any_fold_open = true - break - end - end - if any_fold_open then - -- There's at least one closed fold, so open all - vim.cmd('normal! zR') - else - -- All folds are open, so close all - vim.cmd('normal! zM') - end -end, { noremap = true, silent = true }) +-- Toggle all folds with z +vim.keymap.set("n", "z", function() + local any_fold_closed = false + for lnum = 1, vim.fn.line("$") do + if vim.fn.foldclosed(lnum) ~= -1 then + any_fold_closed = true + break + end + end + if any_fold_closed then + vim.cmd("normal! zR") + else + vim.cmd("normal! zM") + end +end, { noremap = true, silent = true, desc = "Toggle all folds" }) diff --git a/lua/util.lua b/lua/util.lua index 5c19a46..e4f510b 100644 --- a/lua/util.lua +++ b/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