Skip to content

Commit

Permalink
add custom telescope local diff keymap and util functions 🔍
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Jul 15, 2024
1 parent 37b555e commit 9828d7a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nvim/lua/core/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ map("n", "<leader>ft", ":Telescope<cr>", { desc = "Other pickers..." })
map("n", "<leader>fS", ":Telescope resession<cr>", { desc = "Find Session" })
map("n", "<leader><leader>", ":Telescope smart_open<cr>", { desc = "Smart open" })
map("n", "<leader>fh", ":Telescope help_tags<cr>", { desc = "Find help tags" })
map("n", "<leader>fq", function()
require("core.utils").telescope_diff_file()
end, { desc = "Find file to compare with current buffer" })

-- Clear search with <esc>
map("n", "<esc>", ":noh<cr><esc>", { desc = "Escape and clear hlsearch" })
Expand Down
26 changes: 26 additions & 0 deletions nvim/lua/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9828d7a

Please sign in to comment.