diff --git a/lua/lazyvim/plugins/editor.lua b/lua/lazyvim/plugins/editor.lua index 0e01da81..d4a1c7fb 100644 --- a/lua/lazyvim/plugins/editor.lua +++ b/lua/lazyvim/plugins/editor.lua @@ -180,17 +180,17 @@ return { "Telescope buffers sort_mru=true sort_lastused=true", desc = "Switch Buffer", }, - { "/", LazyVim.telescope("live_grep"), desc = "Grep (Root Dir)" }, + { "/", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" }, { ":", "Telescope command_history", desc = "Command History" }, - { "", LazyVim.telescope("files"), desc = "Find Files (Root Dir)" }, + { "", LazyVim.pick("auto"), desc = "Find Files (Root Dir)" }, -- find { "fb", "Telescope buffers sort_mru=true sort_lastused=true", desc = "Buffers" }, - { "fc", LazyVim.telescope.config_files(), desc = "Find Config File" }, - { "ff", LazyVim.telescope("files"), desc = "Find Files (Root Dir)" }, - { "fF", LazyVim.telescope("files", { cwd = false }), desc = "Find Files (cwd)" }, + { "fc", LazyVim.pick.config_files(), desc = "Find Config File" }, + { "ff", LazyVim.pick("auto"), desc = "Find Files (Root Dir)" }, + { "fF", LazyVim.pick("auto", { root = false }), desc = "Find Files (cwd)" }, { "fg", "Telescope git_files", desc = "Find Files (git-files)" }, { "fr", "Telescope oldfiles", desc = "Recent" }, - { "fR", LazyVim.telescope("oldfiles", { cwd = vim.uv.cwd() }), desc = "Recent (cwd)" }, + { "fR", LazyVim.pick("oldfiles"), desc = "Recent (cwd)" }, -- git { "gc", "Telescope git_commits", desc = "Commits" }, { "gs", "Telescope git_status", desc = "Status" }, @@ -202,8 +202,8 @@ return { { "sC", "Telescope commands", desc = "Commands" }, { "sd", "Telescope diagnostics bufnr=0", desc = "Document Diagnostics" }, { "sD", "Telescope diagnostics", desc = "Workspace Diagnostics" }, - { "sg", LazyVim.telescope("live_grep"), desc = "Grep (Root Dir)" }, - { "sG", LazyVim.telescope("live_grep", { cwd = false }), desc = "Grep (cwd)" }, + { "sg", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" }, + { "sG", LazyVim.pick("live_grep", { root = false }), desc = "Grep (cwd)" }, { "sh", "Telescope help_tags", desc = "Help Pages" }, { "sH", "Telescope highlights", desc = "Search Highlight Groups" }, { "sj", "Telescope jumplist", desc = "Jumplist" }, @@ -214,11 +214,11 @@ return { { "so", "Telescope vim_options", desc = "Options" }, { "sR", "Telescope resume", desc = "Resume" }, { "sq", "Telescope quickfix", desc = "Quickfix List" }, - { "sw", LazyVim.telescope("grep_string", { word_match = "-w" }), desc = "Word (Root Dir)" }, - { "sW", LazyVim.telescope("grep_string", { cwd = false, word_match = "-w" }), desc = "Word (cwd)" }, - { "sw", LazyVim.telescope("grep_string"), mode = "v", desc = "Selection (Root Dir)" }, - { "sW", LazyVim.telescope("grep_string", { cwd = false }), mode = "v", desc = "Selection (cwd)" }, - { "uC", LazyVim.telescope("colorscheme", { enable_preview = true }), desc = "Colorscheme with Preview" }, + { "sw", LazyVim.pick("grep_string", { word_match = "-w" }), desc = "Word (Root Dir)" }, + { "sW", LazyVim.pick("grep_string", { root = false, word_match = "-w" }), desc = "Word (cwd)" }, + { "sw", LazyVim.pick("grep_string"), mode = "v", desc = "Selection (Root Dir)" }, + { "sW", LazyVim.pick("grep_string", { root = false }), mode = "v", desc = "Selection (cwd)" }, + { "uC", LazyVim.pick("colorscheme", { enable_preview = true }), desc = "Colorscheme with Preview" }, { "ss", function() @@ -247,12 +247,12 @@ return { local find_files_no_ignore = function() local action_state = require("telescope.actions.state") local line = action_state.get_current_line() - LazyVim.telescope("find_files", { no_ignore = true, default_text = line })() + LazyVim.pick("find_files", { no_ignore = true, default_text = line })() end local find_files_with_hidden = function() local action_state = require("telescope.actions.state") local line = action_state.get_current_line() - LazyVim.telescope("find_files", { hidden = true, default_text = line })() + LazyVim.pick("find_files", { hidden = true, default_text = line })() end return { diff --git a/lua/lazyvim/plugins/extras/editor/fzf.lua b/lua/lazyvim/plugins/extras/editor/fzf.lua new file mode 100644 index 00000000..89cefac1 --- /dev/null +++ b/lua/lazyvim/plugins/extras/editor/fzf.lua @@ -0,0 +1,195 @@ +---@class FzfLuaOpts: lazyvim.util.pick.Opts +---@field cmd string? + +LazyVim.pick.commands = { + files = "files", +} + +---@param command string +---@param opts? FzfLuaOpts +LazyVim.pick._open = function(command, opts) + opts = opts or {} + if opts.cmd == nil and command == "git_files" and opts.show_untracked then + opts.cmd = "git ls-files --exclude-standard --cached --others" + end + return require("fzf-lua")[command](opts) +end + +local function symbols_filter(entry, ctx) + ctx.symbols_filter = ctx.symbols_filter or require("lazyvim.config").get_kind_filter(ctx.bufnr) + return vim.tbl_contains(ctx.symbols_filter, entry.kind) +end + +return { + { + "nvim-telescope/telescope.nvim", + enabled = false, + }, + { + "ibhagwan/fzf-lua", + event = "VeryLazy", + opts = function(_, opts) + local config = require("fzf-lua.config") + local actions = require("fzf-lua.actions") + + -- Quickfix + config.defaults.keymap.fzf["ctrl-q"] = "select-all+accept" + config.defaults.keymap.builtin[""] = "preview-page-down" + config.defaults.keymap.builtin[""] = "preview-page-up" + + -- Trouble + config.defaults.actions.files["ctrl-t"] = require("trouble.sources.fzf").actions.open + + -- Toggle root dir / cwd + config.defaults.actions.files["ctrl-r"] = function(_, ctx) + local o = vim.deepcopy(ctx.__call_opts) + o.root = o.root == false + o.cwd = nil + o.buf = ctx.__CTX.bufnr + LazyVim.pick.open(ctx.__INFO.cmd, o) + end + config.defaults.actions.files["alt-c"] = config.defaults.actions.files["ctrl-r"] + + -- use the same prompt for all + local defaults = require("fzf-lua.profiles.default-title") + local function fix(t) + t.prompt = t.prompt ~= nil and " " or nil + for _, v in pairs(t) do + if type(v) == "table" then + fix(v) + end + end + end + fix(defaults) + + vim.api.nvim_set_hl(0, "FzfLuaPath", { link = "Directory", default = true }) + + return vim.tbl_deep_extend("force", opts, defaults, { + fzf_colors = true, + files = { + cwd_prompt = false, + actions = { + ["alt-i"] = { actions.toggle_ignore }, + ["alt-h"] = { actions.toggle_hidden }, + }, + }, + grep = { + formatter = "path.hl", + actions = { + ["alt-i"] = { actions.toggle_ignore }, + ["alt-h"] = { actions.toggle_hidden }, + }, + }, + formatters = { + path = { + hl = { + _to = function() + local _, escseq = require("fzf-lua.utils").ansi_from_hl("FzfLuaPath", "foo") + return [[ + return function(s, _, m) + return "]] .. (escseq or "") .. [[" + .. s .. m.utils.ansi_escseq.clear + end + ]] + end, + }, + }, + }, + }) + end, + keys = { + { "", "close", ft = "fzf", mode = "t", nowait = true }, + { "", "", ft = "fzf", mode = "t", nowait = true }, + { "", "", ft = "fzf", mode = "t", nowait = true }, + { + ",", + "FzfLua buffers sort_mru=true sort_lastused=true", + desc = "Switch Buffer", + }, + { "/", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" }, + { ":", "FzfLua command_history", desc = "Command History" }, + { "", LazyVim.pick("auto"), desc = "Find Files (Root Dir)" }, + -- find + { "fb", "FzfLua buffers sort_mru=true sort_lastused=true", desc = "Buffers" }, + { "fc", LazyVim.pick.config_files(), desc = "Find Config File" }, + { "ff", LazyVim.pick("auto"), desc = "Find Files (Root Dir)" }, + { "fF", LazyVim.pick("auto", { root = false }), desc = "Find Files (cwd)" }, + { "fg", "FzfLua git_files", desc = "Find Files (git-files)" }, + { "fr", "FzfLua oldfiles", desc = "Recent" }, + { "fR", LazyVim.pick("oldfiles", { root = false }), desc = "Recent (cwd)" }, + -- git + { "gc", "FzfLua git_commits", desc = "Commits" }, + { "gs", "FzfLua git_status", desc = "Status" }, + -- search + { 's"', "FzfLua registers", desc = "Registers" }, + { "sa", "FzfLua autocmds", desc = "Auto Commands" }, + { "sb", "FzfLua grep_curbuf", desc = "Buffer" }, + { "sc", "FzfLua command_history", desc = "Command History" }, + { "sC", "FzfLua commands", desc = "Commands" }, + { "sd", "FzfLua diagnostics_document", desc = "Document Diagnostics" }, + { "sD", "FzfLua diagnostics_workspace", desc = "Workspace Diagnostics" }, + { "sg", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" }, + { "sG", LazyVim.pick("live_grep", { root = false }), desc = "Grep (cwd)" }, + { "sh", "FzfLua help_tags", desc = "Help Pages" }, + { "sH", "FzfLua highlights", desc = "Search Highlight Groups" }, + { "sj", "FzfLua jumps", desc = "Jumplist" }, + { "sk", "FzfLua keymaps", desc = "Key Maps" }, + { "sl", "FzfLua loclist", desc = "Location List" }, + { "sM", "FzfLua man_pages", desc = "Man Pages" }, + { "sm", "FzfLua marks", desc = "Jump to Mark" }, + { "sR", "FzfLua resume", desc = "Resume" }, + { "sq", "FzfLua quickfix", desc = "Quickfix List" }, + { "sw", LazyVim.pick("grep_cword"), desc = "Word (Root Dir)" }, + { "sW", LazyVim.pick("grep_cword", { root = false }), desc = "Word (cwd)" }, + { "sw", LazyVim.pick("grep_visual"), mode = "v", desc = "Selection (Root Dir)" }, + { "sW", LazyVim.pick("grep_visual", { root = false }), mode = "v", desc = "Selection (cwd)" }, + { "uC", LazyVim.pick("colorschemes"), desc = "Colorscheme with Preview" }, + { + "ss", + function() + require("fzf-lua").lsp_document_symbols({ + regex_filter = symbols_filter, + }) + end, + desc = "Goto Symbol", + }, + { + "sS", + function() + require("fzf-lua").lsp_dynamic_workspace_symbols({ + regex_filter = symbols_filter, + }) + end, + desc = "Goto Symbol (Workspace)", + }, + }, + }, + + { + "folke/todo-comments.nvim", + optional = true, + -- stylua: ignore + keys = { + { "st", function() require("todo-comments.fzf").todo() end, desc = "Todo" }, + { "sT", function () require("todo-comments.fzf").todo({ keywords = { "TODO", "FIX", "FIXME" } }) end, desc = "Todo/Fix/Fixme" }, + }, + }, + + { + "neovim/nvim-lspconfig", + opts = function() + local Keys = require("lazyvim.plugins.lsp.keymaps").get() + vim.list_extend(Keys, { + { + "gd", + "FzfLua lsp_definitions jump_to_single_result=true", + desc = "Goto Definition", + has = "definition", + }, + { "gr", "FzfLua lsp_references jump_to_single_result=true", desc = "References", nowait = true }, + { "gI", "FzfLua lsp_implementations jump_to_single_result=true", desc = "Goto Implementation" }, + { "gy", "FzfLua lsp_typedefs jump_to_single_result=true", desc = "Goto T[y]pe Definition" }, + }) + end, + }, +} diff --git a/lua/lazyvim/plugins/ui.lua b/lua/lazyvim/plugins/ui.lua index e3c7aa87..c9bc8b6a 100644 --- a/lua/lazyvim/plugins/ui.lua +++ b/lua/lazyvim/plugins/ui.lua @@ -343,11 +343,11 @@ return { header = vim.split(logo, "\n"), -- stylua: ignore center = { - { action = LazyVim.telescope("files"), desc = " Find File", icon = " ", key = "f" }, + { action = LazyVim.pick(), desc = " Find File", icon = " ", key = "f" }, { action = "ene | startinsert", desc = " New File", icon = " ", key = "n" }, - { action = "Telescope oldfiles", desc = " Recent Files", icon = " ", key = "r" }, - { action = "Telescope live_grep", desc = " Find Text", icon = " ", key = "g" }, - { action = [[lua LazyVim.telescope.config_files()()]], desc = " Config", icon = " ", key = "c" }, + { action = LazyVim.pick("oldfiles"), desc = " Recent Files", icon = " ", key = "r" }, + { action = LazyVim.pick("live_grep"), desc = " Find Text", icon = " ", key = "g" }, + { action = LazyVim.pick.config_files(), desc = " Config", icon = " ", key = "c" }, { action = 'lua require("persistence").load()', desc = " Restore Session", icon = " ", key = "s" }, { action = "LazyExtras", desc = " Lazy Extras", icon = " ", key = "x" }, { action = "Lazy", desc = " Lazy", icon = "󰒲 ", key = "l" }, diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index d8f485ed..94704228 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -17,6 +17,7 @@ local LazyUtil = require("lazy.core.util") ---@field json lazyvim.util.json ---@field lualine lazyvim.util.lualine ---@field mini lazyvim.util.mini +---@field pick lazyvim.util.pick ---@field cmp lazyvim.util.cmp local M = {} @@ -281,4 +282,5 @@ function M.memoize(fn) return cache[key] end end + return M diff --git a/lua/lazyvim/util/pick.lua b/lua/lazyvim/util/pick.lua new file mode 100644 index 00000000..30160215 --- /dev/null +++ b/lua/lazyvim/util/pick.lua @@ -0,0 +1,72 @@ +---@class lazyvim.util.pick +---@overload fun(command:string, opts?:lazyvim.util.pick.Opts): fun() +local M = setmetatable({}, { + __call = function(m, ...) + return m.wrap(...) + end, +}) + +---@class lazyvim.util.pick.Opts: table +---@field root? boolean +---@field cwd? string +---@field buf? number +---@field show_untracked? boolean + +---@type table +M.commands = { + files = "find_files", +} + +---@param command? string +---@param opts? lazyvim.util.pick.Opts +function M.open(command, opts) + command = command or "auto" + opts = opts or {} + + opts = vim.deepcopy(opts) + + if type(opts.cwd) == "boolean" then + LazyVim.warn("LazyVim.pick: opts.cwd should be a string or nil") + opts.cwd = nil + end + + if not opts.cwd and opts.root ~= false then + opts.cwd = LazyVim.root({ buf = opts.buf }) + end + + local cwd = opts.cwd or vim.uv.cwd() + if command == "auto" then + command = "files" + if + vim.uv.fs_stat(cwd .. "/.git") + and not vim.uv.fs_stat(cwd .. "/.ignore") + and not vim.uv.fs_stat(cwd .. "/.rgignore") + then + command = "git_files" + opts.show_untracked = opts.show_untracked ~= false + end + end + command = M.commands[command] or command + M._open(command, opts) +end + +---@param command? string +---@param opts? lazyvim.util.pick.Opts +function M.wrap(command, opts) + opts = opts or {} + return function() + M.open(command, vim.deepcopy(opts)) + end +end + +---@param command string +---@param opts? lazyvim.util.pick.Opts +function M._open(command, opts) + return LazyVim.telescope.open(command, opts) +end + +function M.config_files() + return M.wrap("files", { cwd = vim.fn.stdpath("config") }) +end + +return M diff --git a/lua/lazyvim/util/root.lua b/lua/lazyvim/util/root.lua index 4523f3cf..a401ccf4 100644 --- a/lua/lazyvim/util/root.lua +++ b/lua/lazyvim/util/root.lua @@ -168,13 +168,14 @@ end -- * lsp root_dir -- * root pattern of filename of the current buffer -- * root pattern of cwd ----@param opts? {normalize?:boolean} +---@param opts? {normalize?:boolean, buf?:number} ---@return string function M.get(opts) - local buf = vim.api.nvim_get_current_buf() + opts = opts or {} + local buf = opts.buf or vim.api.nvim_get_current_buf() local ret = M.cache[buf] if not ret then - local roots = M.detect({ all = false }) + local roots = M.detect({ all = false, buf = buf }) ret = roots[1] and roots[1].paths[1] or vim.uv.cwd() M.cache[buf] = ret end diff --git a/lua/lazyvim/util/telescope.lua b/lua/lazyvim/util/telescope.lua index bb030627..b6414d2c 100644 --- a/lua/lazyvim/util/telescope.lua +++ b/lua/lazyvim/util/telescope.lua @@ -1,9 +1,5 @@ ----@class lazyvim.util.telescope.opts ----@field cwd? string|boolean ----@field show_untracked? boolean - ---@class lazyvim.util.telescope ----@overload fun(builtin:string, opts?:lazyvim.util.telescope.opts) +---@overload fun(builtin:string, opts?:lazyvim.util.pick.Opts) local M = setmetatable({}, { __call = function(m, ...) return m.telescope(...) @@ -14,50 +10,39 @@ local M = setmetatable({}, { -- cwd will default to lazyvim.util.get_root -- for `files`, git_files or find_files will be chosen depending on .git ---@param builtin string ----@param opts? lazyvim.util.telescope.opts -function M.telescope(builtin, opts) - local params = { builtin = builtin, opts = opts } - return function() - builtin = params.builtin - opts = params.opts - opts = vim.tbl_deep_extend("force", { cwd = LazyVim.root() }, opts or {}) --[[@as lazyvim.util.telescope.opts]] - if builtin == "files" then - if - vim.uv.fs_stat((opts.cwd or vim.uv.cwd()) .. "/.git") - and not vim.uv.fs_stat((opts.cwd or vim.uv.cwd()) .. "/.ignore") - and not vim.uv.fs_stat((opts.cwd or vim.uv.cwd()) .. "/.rgignore") - then - if opts.show_untracked == nil then - opts.show_untracked = true - end - builtin = "git_files" - else - builtin = "find_files" - end +---@param opts? lazyvim.util.pick.Opts +function M.open(builtin, opts) + opts = opts or {} + if opts.cwd and opts.cwd ~= vim.uv.cwd() then + local function open_cwd_dir() + local action_state = require("telescope.actions.state") + local line = action_state.get_current_line() + LazyVim.pick.open( + builtin, + vim.tbl_deep_extend("force", {}, opts or {}, { + root = false, + default_text = line, + }) + ) end - if opts.cwd and opts.cwd ~= vim.uv.cwd() then - local function open_cwd_dir() - local action_state = require("telescope.actions.state") - local line = action_state.get_current_line() - M.telescope( - params.builtin, - vim.tbl_deep_extend("force", {}, params.opts or {}, { cwd = false, default_text = line }) - )() - end - ---@diagnostic disable-next-line: inject-field - opts.attach_mappings = function(_, map) - -- opts.desc is overridden by telescope, until it's changed there is this fix - map("i", "", open_cwd_dir, { desc = "Open cwd Directory" }) - return true - end + ---@diagnostic disable-next-line: inject-field + opts.attach_mappings = function(_, map) + -- opts.desc is overridden by telescope, until it's changed there is this fix + map("i", "", open_cwd_dir, { desc = "Open cwd Directory" }) + return true end - - require("telescope.builtin")[builtin](opts) end + + require("telescope.builtin")[builtin](opts) +end + +M.telescope = function(...) + LazyVim.deprecate("LazyVim.telescope", "LazyVim.pick") + return LazyVim.pick.wrap(...) end function M.config_files() - return LazyVim.telescope("find_files", { cwd = vim.fn.stdpath("config") }) + return LazyVim.pick.config_files() end return M