From c864a7d378da2a11afb09302b3220264e2aa3409 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Tue, 25 Feb 2025 13:53:12 +0100 Subject: [PATCH] feat(picker.input): added some ctrl+r keymaps similar to cmdline. Closes #1420 --- lua/snacks/picker/actions.lua | 23 +++++++++++++++++++++++ lua/snacks/picker/config/defaults.lua | 7 +++++++ 2 files changed, 30 insertions(+) diff --git a/lua/snacks/picker/actions.lua b/lua/snacks/picker/actions.lua index 0e5b1f38..00d6b2ce 100644 --- a/lua/snacks/picker/actions.lua +++ b/lua/snacks/picker/actions.lua @@ -13,6 +13,9 @@ local M = {} ---@field field? string ---@field notify? boolean +---@class snacks.picker.insert.Action: snacks.picker.Action +---@field expr string + ---@enum (key) snacks.picker.EditCmd local edit_cmd = { edit = "buffer", @@ -197,6 +200,26 @@ function M.toggle_maximize(picker) picker.layout:maximize() end +function M.insert(picker, _, action) + ---@cast action snacks.picker.insert.Action + if action.expr then + local value = "" + vim.api.nvim_buf_call(picker.input.filter.current_buf, function() + value = action.expr == "line" and vim.api.nvim_get_current_line() or vim.fn.expand(action.expr) + end) + vim.api.nvim_win_call(picker.input.win.win, function() + vim.api.nvim_put({ value }, "c", true, true) + end) + end +end +M.insert_cword = { action = "insert", expr = "" } +M.insert_cWORD = { action = "insert", expr = "" } +M.insert_filename = { action = "insert", expr = "%" } +M.insert_file = { action = "insert", expr = "" } +M.insert_line = { action = "insert", expr = "line" } +M.insert_file_full = { action = "insert", expr = ":p" } +M.insert_alt = { action = "insert", expr = "#" } + function M.toggle_preview(picker) picker:toggle("preview") end diff --git a/lua/snacks/picker/config/defaults.lua b/lua/snacks/picker/config/defaults.lua index d4c95abc..d5c648a1 100644 --- a/lua/snacks/picker/config/defaults.lua +++ b/lua/snacks/picker/config/defaults.lua @@ -233,6 +233,13 @@ local defaults = { [""] = { "tab", mode = { "n", "i" } }, [""] = { "list_scroll_up", mode = { "i", "n" } }, [""] = { "edit_vsplit", mode = { "i", "n" } }, + ["#"] = { "insert_alt", mode = "i" }, + ["%"] = { "insert_filename", mode = "i" }, + [""] = { "insert_cWORD", mode = "i" }, + [""] = { "insert_file", mode = "i" }, + [""] = { "insert_line", mode = "i" }, + [""] = { "insert_file_full", mode = "i" }, + [""] = { "insert_cword", mode = "i" }, ["H"] = "layout_left", ["J"] = "layout_bottom", ["K"] = "layout_top",