From 9828d7a97e61d79b8c796ae606be0e1eac652c10 Mon Sep 17 00:00:00 2001 From: Scott McKendry <39483124+scottmckendry@users.noreply.github.com> Date: Mon, 15 Jul 2024 15:39:27 +1200 Subject: [PATCH] add custom telescope local diff keymap and util functions :mag: --- nvim/lua/core/keymaps.lua | 3 +++ nvim/lua/core/utils.lua | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/nvim/lua/core/keymaps.lua b/nvim/lua/core/keymaps.lua index 9fbde49..34cec5a 100644 --- a/nvim/lua/core/keymaps.lua +++ b/nvim/lua/core/keymaps.lua @@ -66,6 +66,9 @@ map("n", "ft", ":Telescope", { desc = "Other pickers..." }) map("n", "fS", ":Telescope resession", { desc = "Find Session" }) map("n", "", ":Telescope smart_open", { desc = "Smart open" }) map("n", "fh", ":Telescope help_tags", { desc = "Find help tags" }) +map("n", "fq", function() + require("core.utils").telescope_diff_file() +end, { desc = "Find file to compare with current buffer" }) -- Clear search with map("n", "", ":noh", { desc = "Escape and clear hlsearch" }) diff --git a/nvim/lua/core/utils.lua b/nvim/lua/core/utils.lua index 01d5c74..cfe9b01 100644 --- a/nvim/lua/core/utils.lua +++ b/nvim/lua/core/utils.lua @@ -103,4 +103,30 @@ function M.open_help(buf) end end +--- Display a diff between the current buffer and a given file +--- @param file string The file to diff against the current buffer +function M.diff_file(file) + local current_file = vim.fn.expand("%:p") + vim.cmd("edit " .. file) + vim.cmd("vert diffsplit " .. current_file) +end + +--- Open a telescope picker to select a file to diff against the current buffer +function M.telescope_diff_file() + require("telescope.builtin").find_files({ + prompt_title = "Select File to Compare", + attach_mappings = function(prompt_bufnr) + local actions = require("telescope.actions") + local action_state = require("telescope.actions.state") + + actions.select_default:replace(function() + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + M.diff_file(selection.value) + end) + return true + end, + }) +end + return M