Skip to content

Commit

Permalink
Merge pull request ThePrimeagen#15 from polarmutex/feat-telescope-create
Browse files Browse the repository at this point in the history
Telescope Create
  • Loading branch information
ThePrimeagen committed Apr 19, 2021
2 parents 6af6782 + 8b4f270 commit 065dc3c
Showing 1 changed file with 75 additions and 32 deletions.
107 changes: 75 additions & 32 deletions lua/telescope/_extensions/git_worktree.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Path = require("plenary.path")
local Window = require("plenary.window.float")
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local actions = require("telescope.actions")
Expand All @@ -17,49 +18,91 @@ local get_worktree_path = function(prompt_bufnr)
return rel_path:make_relative(gwt.get_root())
end

local switch_worktree = function(prompt_bufnr)
local worktree_path = get_worktree_path(prompt_bufnr)
actions.close(prompt_bufnr)
if worktree_path ~= nil then
gwt.switch_worktree(worktree_path)
end
end

local delete_worktree = function(prompt_bufnr, force)
local worktree_path = get_worktree_path(prompt_bufnr)
actions.close(prompt_bufnr)
if worktree_path ~= nil then
gwt.delete_worktree(worktree_path, force)
end
end

local create_worktree = function()
require("telescope.builtin").git_branches(
{
attach_mappings = function(_)

actions.select_default:replace(
function(prompt_bufnr, _)
local selected_entry = action_state.get_selected_entry()
actions.close(prompt_bufnr)
if selected_entry ~= nil then
local branch = selected_entry.value

local window = Window.percentage_range_window(0.5, 0.2)
vim.api.nvim_buf_set_option(window.bufnr, "buftype",
"prompt")
vim.fn.prompt_setprompt(window.bufnr, "Worktree Location: ")
vim.fn.prompt_setcallback(window.bufnr, function(text)
vim.api.nvim_win_close(window.win_id, true)
vim.api.nvim_buf_delete(window.bufnr, {force = true})
if text ~= "" then
gwt.create_worktree(text, branch)
else
print("No path to create worktree")
end
end)
vim.cmd [[startinsert]]

end
end)

-- do we need to replace other default maps?

return true
end
})
end

local telescope_git_worktree = function(opts)
opts = opts or {}
pickers.new({}, {
prompt_prefix = "Git Worktrees >",
finder = finders.new_oneshot_job(vim.tbl_flatten({
"git",
"worktree",
"list",
}), opts),
finder = finders.new_oneshot_job(vim.tbl_flatten({"git", "worktree", "list"}),
opts),
sorter = conf.generic_sorter({}),
attach_mappings = function(_, map)
-- Switch to chosen worktree
action_set.select:replace(function(prompt_bufnr, _)
local worktree_path = get_worktree_path(prompt_bufnr)
actions.close(prompt_bufnr)
if worktree_path ~= nil then
gwt.switch_worktree(worktree_path)
end
end)
-- Delete chosen worktree
action_set.select:replace(switch_worktree)

map("i", "<c-d>", function(prompt_bufnr)
local worktree_path = get_worktree_path(prompt_bufnr)
actions.close(prompt_bufnr)
if worktree_path ~= nil then
gwt.delete_worktree(worktree_path)
end
delete_worktree(prompt_bufnr)
end)
map("n", "<c-d>", function(prompt_bufnr)
delete_worktree(prompt_bufnr)
end)
-- Force delete chosen worktree
map("i", "<c-D>", function(prompt_bufnr)
local worktree_path = get_worktree_path(prompt_bufnr)
actions.close(prompt_bufnr)
if worktree_path ~= nil then
gwt.delete_worktree(worktree_path, true)
end
delete_worktree(prompt_bufnr, true)
end)
-- TODO Create chosen worktree
map("n", "<c-D>", function(prompt_bufnr)
delete_worktree(prompt_bufnr, true)
end)

return true
end,
end
}):find()
end

return require("telescope").register_extension({
exports = {
git_worktrees = telescope_git_worktree,
},
})
return require("telescope").register_extension(
{
exports = {
git_worktrees = telescope_git_worktree,
create_git_worktree = create_worktree
}
})

0 comments on commit 065dc3c

Please sign in to comment.