diff --git a/lua/lazyvim/plugins/editor.lua b/lua/lazyvim/plugins/editor.lua index c41a5db7..f3f9da7b 100644 --- a/lua/lazyvim/plugins/editor.lua +++ b/lua/lazyvim/plugins/editor.lua @@ -142,156 +142,6 @@ return { }, }, - -- Fuzzy finder. - -- The default key bindings to find files will use Telescope's - -- `find_files` or `git_files` depending on whether the - -- directory is a git repo. - { - "nvim-telescope/telescope.nvim", - cmd = "Telescope", - version = false, -- telescope did only one release, so use HEAD for now - dependencies = { - { - "nvim-telescope/telescope-fzf-native.nvim", - build = have_make and "make" - or "cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build", - enabled = have_make or have_cmake, - config = function(plugin) - LazyVim.on_load("telescope.nvim", function() - local ok, err = pcall(require("telescope").load_extension, "fzf") - if not ok then - local lib = plugin.dir .. "/build/libfzf." .. (LazyVim.is_win() and "dll" or "so") - if not vim.uv.fs_stat(lib) then - LazyVim.warn("`telescope-fzf-native.nvim` not built. Rebuilding...") - require("lazy").build({ plugins = { plugin }, show = false }):wait(function() - LazyVim.info("Rebuilding `telescope-fzf-native.nvim` done.\nPlease restart Neovim.") - end) - else - LazyVim.error("Failed to load `telescope-fzf-native.nvim`:\n" .. err) - end - end - end) - end, - }, - }, - keys = { - { - ",", - "Telescope buffers sort_mru=true sort_lastused=true", - desc = "Switch Buffer", - }, - { "/", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" }, - { ":", "Telescope command_history", desc = "Command History" }, - { "", LazyVim.pick("auto"), desc = "Find Files (Root Dir)" }, - -- find - { "fb", "Telescope 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", "Telescope git_files", desc = "Find Files (git-files)" }, - { "fr", "Telescope oldfiles", desc = "Recent" }, - { "fR", LazyVim.pick("oldfiles", { cwd = vim.uv.cwd() }), desc = "Recent (cwd)" }, - -- git - { "gc", "Telescope git_commits", desc = "Commits" }, - { "gs", "Telescope git_status", desc = "Status" }, - -- search - { 's"', "Telescope registers", desc = "Registers" }, - { "sa", "Telescope autocommands", desc = "Auto Commands" }, - { "sb", "Telescope current_buffer_fuzzy_find", desc = "Buffer" }, - { "sc", "Telescope command_history", desc = "Command History" }, - { "sC", "Telescope commands", desc = "Commands" }, - { "sd", "Telescope diagnostics bufnr=0", desc = "Document Diagnostics" }, - { "sD", "Telescope diagnostics", desc = "Workspace Diagnostics" }, - { "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" }, - { "sk", "Telescope keymaps", desc = "Key Maps" }, - { "sl", "Telescope loclist", desc = "Location List" }, - { "sM", "Telescope man_pages", desc = "Man Pages" }, - { "sm", "Telescope marks", desc = "Jump to Mark" }, - { "so", "Telescope vim_options", desc = "Options" }, - { "sR", "Telescope resume", desc = "Resume" }, - { "sq", "Telescope quickfix", desc = "Quickfix List" }, - { "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() - require("telescope.builtin").lsp_document_symbols({ - symbols = require("lazyvim.config").get_kind_filter(), - }) - end, - desc = "Goto Symbol", - }, - { - "sS", - function() - require("telescope.builtin").lsp_dynamic_workspace_symbols({ - symbols = require("lazyvim.config").get_kind_filter(), - }) - end, - desc = "Goto Symbol (Workspace)", - }, - }, - opts = function() - local actions = require("telescope.actions") - - local open_with_trouble = function(...) - return require("trouble.sources.telescope").open(...) - end - local find_files_no_ignore = function() - local action_state = require("telescope.actions.state") - local line = action_state.get_current_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.pick("find_files", { hidden = true, default_text = line })() - end - - return { - defaults = { - prompt_prefix = " ", - selection_caret = " ", - -- open files in the first window that is an actual file. - -- use the current window if no other window is available. - get_selection_window = function() - local wins = vim.api.nvim_list_wins() - table.insert(wins, 1, vim.api.nvim_get_current_win()) - for _, win in ipairs(wins) do - local buf = vim.api.nvim_win_get_buf(win) - if vim.bo[buf].buftype == "" then - return win - end - end - return 0 - end, - mappings = { - i = { - [""] = open_with_trouble, - [""] = open_with_trouble, - [""] = find_files_no_ignore, - [""] = find_files_with_hidden, - [""] = actions.cycle_history_next, - [""] = actions.cycle_history_prev, - [""] = actions.preview_scrolling_down, - [""] = actions.preview_scrolling_up, - }, - n = { - ["q"] = actions.close, - }, - }, - }, - } - end, - }, - -- Flash enhances the built-in search functionality by showing labels -- at the end of each match, letting you quickly jump to a specific -- location. @@ -311,38 +161,6 @@ return { }, }, - -- Flash Telescope config - { - "nvim-telescope/telescope.nvim", - optional = true, - opts = function(_, opts) - if not LazyVim.has("flash.nvim") then - return - end - local function flash(prompt_bufnr) - require("flash").jump({ - pattern = "^", - label = { after = { 0, 0 } }, - search = { - mode = "search", - exclude = { - function(win) - return vim.bo[vim.api.nvim_win_get_buf(win)].filetype ~= "TelescopeResults" - end, - }, - }, - action = function(match) - local picker = require("telescope.actions.state").get_current_picker(prompt_bufnr) - picker:set_selection(match.pos[1] - 1) - end, - }) - end - opts.defaults = vim.tbl_deep_extend("force", opts.defaults or {}, { - mappings = { n = { s = flash }, i = { [""] = flash } }, - }) - end, - }, - -- which-key helps you remember key bindings by showing a popup -- with the active keybindings of the command you started typing. { @@ -482,4 +300,17 @@ return { { "sT", "TodoTelescope keywords=TODO,FIX,FIXME", desc = "Todo/Fix/Fixme" }, }, }, + + { + import = "lazyvim.plugins.extras.editor.fzf", + enabled = function() + return LazyVim.has_extra("editor.fzf") + end, + }, + { + import = "lazyvim.plugins.extras.editor.telescope", + enabled = function() + return not LazyVim.has_extra("editor.fzf") + end, + }, } diff --git a/lua/lazyvim/plugins/extras/editor/fzf.lua b/lua/lazyvim/plugins/extras/editor/fzf.lua index 58ec7695..c37e6788 100644 --- a/lua/lazyvim/plugins/extras/editor/fzf.lua +++ b/lua/lazyvim/plugins/extras/editor/fzf.lua @@ -1,18 +1,25 @@ ---@class FzfLuaOpts: lazyvim.util.pick.Opts ---@field cmd string? -LazyVim.pick.commands = { - files = "files", -} +---@type LazyPicker +local picker = { + name = "fzf", + 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) + ---@param command string + ---@param opts? FzfLuaOpts + 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, +} +if not LazyVim.pick.register(picker) then + return {} end local function symbols_filter(entry, ctx) @@ -26,11 +33,8 @@ local function symbols_filter(entry, ctx) end return { - { "stevearc/dressing.nvim", enabled = false }, - { - "nvim-telescope/telescope.nvim", - enabled = false, - }, + desc = "Awesome picker for FZF (alternative to Telescope)", + recommended = true, { "ibhagwan/fzf-lua", event = "VeryLazy", diff --git a/lua/lazyvim/plugins/extras/editor/telescope.lua b/lua/lazyvim/plugins/extras/editor/telescope.lua new file mode 100644 index 00000000..9f5bd71c --- /dev/null +++ b/lua/lazyvim/plugins/extras/editor/telescope.lua @@ -0,0 +1,242 @@ +-- TODO: +-- - [ ] remove telescope util +-- - [ ] better lsp bindings + +---@type LazyPicker +local picker = { + name = "telescope", + commands = { + files = "find_files", + }, + -- this will return a function that calls telescope. + -- 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.pick.Opts + open = function(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 + ---@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 + end + + require("telescope.builtin")[builtin](opts) + end, +} +if not LazyVim.pick.register(picker) then + return {} +end + +return { + -- Fuzzy finder. + -- The default key bindings to find files will use Telescope's + -- `find_files` or `git_files` depending on whether the + -- directory is a git repo. + { + "nvim-telescope/telescope.nvim", + cmd = "Telescope", + version = false, -- telescope did only one release, so use HEAD for now + dependencies = { + { + "nvim-telescope/telescope-fzf-native.nvim", + build = have_make and "make" + or "cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build", + enabled = have_make or have_cmake, + config = function(plugin) + LazyVim.on_load("telescope.nvim", function() + local ok, err = pcall(require("telescope").load_extension, "fzf") + if not ok then + local lib = plugin.dir .. "/build/libfzf." .. (LazyVim.is_win() and "dll" or "so") + if not vim.uv.fs_stat(lib) then + LazyVim.warn("`telescope-fzf-native.nvim` not built. Rebuilding...") + require("lazy").build({ plugins = { plugin }, show = false }):wait(function() + LazyVim.info("Rebuilding `telescope-fzf-native.nvim` done.\nPlease restart Neovim.") + end) + else + LazyVim.error("Failed to load `telescope-fzf-native.nvim`:\n" .. err) + end + end + end) + end, + }, + }, + keys = { + { + ",", + "Telescope buffers sort_mru=true sort_lastused=true", + desc = "Switch Buffer", + }, + { "/", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" }, + { ":", "Telescope command_history", desc = "Command History" }, + { "", LazyVim.pick("auto"), desc = "Find Files (Root Dir)" }, + -- find + { "fb", "Telescope 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", "Telescope git_files", desc = "Find Files (git-files)" }, + { "fr", "Telescope oldfiles", desc = "Recent" }, + { "fR", LazyVim.pick("oldfiles", { cwd = vim.uv.cwd() }), desc = "Recent (cwd)" }, + -- git + { "gc", "Telescope git_commits", desc = "Commits" }, + { "gs", "Telescope git_status", desc = "Status" }, + -- search + { 's"', "Telescope registers", desc = "Registers" }, + { "sa", "Telescope autocommands", desc = "Auto Commands" }, + { "sb", "Telescope current_buffer_fuzzy_find", desc = "Buffer" }, + { "sc", "Telescope command_history", desc = "Command History" }, + { "sC", "Telescope commands", desc = "Commands" }, + { "sd", "Telescope diagnostics bufnr=0", desc = "Document Diagnostics" }, + { "sD", "Telescope diagnostics", desc = "Workspace Diagnostics" }, + { "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" }, + { "sk", "Telescope keymaps", desc = "Key Maps" }, + { "sl", "Telescope loclist", desc = "Location List" }, + { "sM", "Telescope man_pages", desc = "Man Pages" }, + { "sm", "Telescope marks", desc = "Jump to Mark" }, + { "so", "Telescope vim_options", desc = "Options" }, + { "sR", "Telescope resume", desc = "Resume" }, + { "sq", "Telescope quickfix", desc = "Quickfix List" }, + { "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() + require("telescope.builtin").lsp_document_symbols({ + symbols = require("lazyvim.config").get_kind_filter(), + }) + end, + desc = "Goto Symbol", + }, + { + "sS", + function() + require("telescope.builtin").lsp_dynamic_workspace_symbols({ + symbols = require("lazyvim.config").get_kind_filter(), + }) + end, + desc = "Goto Symbol (Workspace)", + }, + }, + opts = function() + local actions = require("telescope.actions") + + local open_with_trouble = function(...) + return require("trouble.sources.telescope").open(...) + end + local find_files_no_ignore = function() + local action_state = require("telescope.actions.state") + local line = action_state.get_current_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.pick("find_files", { hidden = true, default_text = line })() + end + + return { + defaults = { + prompt_prefix = " ", + selection_caret = " ", + -- open files in the first window that is an actual file. + -- use the current window if no other window is available. + get_selection_window = function() + local wins = vim.api.nvim_list_wins() + table.insert(wins, 1, vim.api.nvim_get_current_win()) + for _, win in ipairs(wins) do + local buf = vim.api.nvim_win_get_buf(win) + if vim.bo[buf].buftype == "" then + return win + end + end + return 0 + end, + mappings = { + i = { + [""] = open_with_trouble, + [""] = open_with_trouble, + [""] = find_files_no_ignore, + [""] = find_files_with_hidden, + [""] = actions.cycle_history_next, + [""] = actions.cycle_history_prev, + [""] = actions.preview_scrolling_down, + [""] = actions.preview_scrolling_up, + }, + n = { + ["q"] = actions.close, + }, + }, + }, + } + end, + }, + + -- Flash Telescope config + { + "folke/flash.nvim", + optional = true, + opts = function(_, opts) + local function flash(prompt_bufnr) + require("flash").jump({ + pattern = "^", + label = { after = { 0, 0 } }, + search = { + mode = "search", + exclude = { + function(win) + return vim.bo[vim.api.nvim_win_get_buf(win)].filetype ~= "TelescopeResults" + end, + }, + }, + action = function(match) + local picker = require("telescope.actions.state").get_current_picker(prompt_bufnr) + picker:set_selection(match.pos[1] - 1) + end, + }) + end + opts.defaults = vim.tbl_deep_extend("force", opts.defaults or {}, { + mappings = { n = { s = flash }, i = { [""] = flash } }, + }) + end, + }, + + -- better vim.ui with telescope + { + "stevearc/dressing.nvim", + lazy = true, + init = function() + ---@diagnostic disable-next-line: duplicate-set-field + vim.ui.select = function(...) + require("lazy").load({ plugins = { "dressing.nvim" } }) + return vim.ui.select(...) + end + ---@diagnostic disable-next-line: duplicate-set-field + vim.ui.input = function(...) + require("lazy").load({ plugins = { "dressing.nvim" } }) + return vim.ui.input(...) + end + end, + }, +} diff --git a/lua/lazyvim/plugins/ui.lua b/lua/lazyvim/plugins/ui.lua index 9d4fdda7..744585c0 100644 --- a/lua/lazyvim/plugins/ui.lua +++ b/lua/lazyvim/plugins/ui.lua @@ -34,24 +34,6 @@ return { end, }, - -- better vim.ui - { - "stevearc/dressing.nvim", - lazy = true, - init = function() - ---@diagnostic disable-next-line: duplicate-set-field - vim.ui.select = function(...) - require("lazy").load({ plugins = { "dressing.nvim" } }) - return vim.ui.select(...) - end - ---@diagnostic disable-next-line: duplicate-set-field - vim.ui.input = function(...) - require("lazy").load({ plugins = { "dressing.nvim" } }) - return vim.ui.input(...) - end - end, - }, - -- This is what powers LazyVim's fancy-looking -- tabs, which include filetype icons and close buttons. { diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index 4a1478e6..212d6ae6 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -32,6 +32,7 @@ local deprecated = { toggle_diagnostics = { "toggle", "diagnostics" }, toggle_number = { "toggle", "number" }, fg = "ui", + telescope = "pick", } setmetatable(M, { @@ -78,8 +79,10 @@ end ---@param extra string function M.has_extra(extra) + local Config = require("lazyvim.config") local modname = "lazyvim.plugins.extras." .. extra return vim.tbl_contains(require("lazy.core.config").spec.modules, modname) + or vim.tbl_contains(Config.json.data.extras, modname) end ---@param fn fun() diff --git a/lua/lazyvim/util/pick.lua b/lua/lazyvim/util/pick.lua index 30160215..b8e4a94b 100644 --- a/lua/lazyvim/util/pick.lua +++ b/lua/lazyvim/util/pick.lua @@ -12,14 +12,43 @@ local M = setmetatable({}, { ---@field buf? number ---@field show_untracked? boolean +---@class LazyPicker +---@field name string +---@field open fun(command:string, opts?:lazyvim.util.pick.Opts) +---@field commands table + +---@type LazyPicker? +M.picker = nil + ---@type table M.commands = { files = "find_files", } +---@param picker LazyPicker +function M.register(picker) + -- this only happens when using :LazyExtras + -- so allow to get the full spec + if vim.v.vim_did_enter == 1 then + return true + end + if M.picker then + LazyVim.warn( + "`LazyVim.pick`: picker already set to `" .. M.picker.name .. "`,\nignoring new picker `" .. picker.name .. "`" + ) + return false + end + M.picker = picker + return true +end + ---@param command? string ---@param opts? lazyvim.util.pick.Opts function M.open(command, opts) + if not M.picker then + return LazyVim.error("LazyVim.pick: picker not set") + end + command = command or "auto" opts = opts or {} @@ -46,8 +75,8 @@ function M.open(command, opts) opts.show_untracked = opts.show_untracked ~= false end end - command = M.commands[command] or command - M._open(command, opts) + command = M.picker.commands[command] or command + M.picker.open(command, opts) end ---@param command? string diff --git a/lua/lazyvim/util/telescope.lua b/lua/lazyvim/util/telescope.lua deleted file mode 100644 index b6414d2c..00000000 --- a/lua/lazyvim/util/telescope.lua +++ /dev/null @@ -1,48 +0,0 @@ ----@class lazyvim.util.telescope ----@overload fun(builtin:string, opts?:lazyvim.util.pick.Opts) -local M = setmetatable({}, { - __call = function(m, ...) - return m.telescope(...) - end, -}) - --- this will return a function that calls telescope. --- 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.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 - ---@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 - 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.pick.config_files() -end - -return M