diff --git a/lua/lazyvim/plugins/editor.lua b/lua/lazyvim/plugins/editor.lua index 8f4891b7..bb80a65d 100644 --- a/lua/lazyvim/plugins/editor.lua +++ b/lua/lazyvim/plugins/editor.lua @@ -179,17 +179,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", { cwd = vim.uv.cwd() }), desc = "Recent (cwd)" }, -- git { "gc", "Telescope git_commits", desc = "Commits" }, { "gs", "Telescope git_status", desc = "Status" }, @@ -201,8 +201,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" }, @@ -213,11 +213,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() @@ -246,12 +246,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 index 67048fc0..d2af3749 100644 --- a/lua/lazyvim/plugins/extras/editor/fzf.lua +++ b/lua/lazyvim/plugins/extras/editor/fzf.lua @@ -1,29 +1,19 @@ -local M = {} +---@class FzfLuaOpts: lazyvim.util.pick.Opts +---@field cmd string? ----@class FzfLuaOpts: table ----@field cwd boolean|string +LazyVim.pick.commands = { + files = "files", +} ----@param opts? {cwd?:string|boolean} -function M.fzf(builtin, opts) +---@param command string +---@param opts? FzfLuaOpts +LazyVim.pick._open = function(command, opts) opts = opts or {} - return function() - local dir = opts.cwd == true and vim.uv.cwd() or LazyVim.root() - opts.cwd = dir - if - builtin == "files" - and vim.uv.fs_stat(dir .. "/.git") - and not vim.uv.fs_stat(dir .. "/.ignore") - and not vim.uv.fs_stat(dir .. "/.rgignore") - then - if opts.cmd == nil then - opts.cmd = "git ls-files --exclude-standard --cached --others" - end - builtin = "git_files" - end - - return require("fzf-lua")[builtin](opts) + 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 return { @@ -74,17 +64,17 @@ return { "FzfLua buffers sort_mru=true sort_lastused=true", desc = "Switch Buffer", }, - { "/", M.fzf("live_grep"), desc = "Grep (Root Dir)" }, + { "/", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" }, { ":", "FzfLua command_history", desc = "Command History" }, - { "", M.fzf("files"), desc = "Find Files (Root Dir)" }, + { "", LazyVim.pick("auto"), desc = "Find Files (Root Dir)" }, -- find { "fb", "FzfLua buffers sort_mru=true sort_lastused=true", desc = "Buffers" }, - { "fc", M.fzf("files", { cwd = vim.fn.stdpath("config") }), desc = "Find Config File" }, - { "ff", M.fzf("files"), desc = "Find Files (Root Dir)" }, - { "fF", M.fzf("files", { cwd = true }), 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", "FzfLua git_files", desc = "Find Files (git-files)" }, { "fr", "FzfLua oldfiles", desc = "Recent" }, - { "fR", M.fzf("oldfiles", { cwd = vim.uv.cwd() }), desc = "Recent (cwd)" }, + { "fR", LazyVim.pick("oldfiles", { root = false }), desc = "Recent (cwd)" }, -- git { "gc", "FzfLua git_commits", desc = "Commits" }, { "gs", "FzfLua git_status", desc = "Status" }, @@ -96,8 +86,8 @@ return { { "sC", "FzfLua commands", desc = "Commands" }, { "sd", "FzfLua diagnostics_document", desc = "Document Diagnostics" }, { "sD", "FzfLua diagnostics_workspace", desc = "Workspace Diagnostics" }, - { "sg", M.fzf("live_grep"), desc = "Grep (Root Dir)" }, - { "sG", M.fzf("live_grep", { cwd = true }), desc = "Grep (cwd)" }, + { "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" }, @@ -107,11 +97,11 @@ return { { "sm", "FzfLua marks", desc = "Jump to Mark" }, { "sR", "FzfLua resume", desc = "Resume" }, { "sq", "FzfLua quickfix", desc = "Quickfix List" }, - { "sw", M.fzf("grep_cword"), desc = "Word (Root Dir)" }, - { "sW", M.fzf("grep_cword", { cwd = true }), desc = "Word (cwd)" }, - { "sw", M.fzf("grep_visual"), mode = "v", desc = "Selection (Root Dir)" }, - { "sW", M.fzf("grep_visual", { cwd = true }), mode = "v", desc = "Selection (cwd)" }, - { "uC", M.fzf("colorschemes"), desc = "Colorscheme with Preview" }, + { "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() 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..ec64987f --- /dev/null +++ b/lua/lazyvim/util/pick.lua @@ -0,0 +1,71 @@ +---@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 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() + 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/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