Skip to content

Commit 497c31c

Browse files
committed
1 parent 9efb30f commit 497c31c

File tree

1 file changed

+129
-130
lines changed

1 file changed

+129
-130
lines changed

lua/telescope/_extensions/git_worktree.lua

Lines changed: 129 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,28 @@ local conf = require("telescope.config").values
1111
local git_worktree = require("git-worktree")
1212

1313
local get_worktree_path = function(prompt_bufnr)
14-
local selection = action_state.get_selected_entry(prompt_bufnr)
15-
return selection.path
14+
local selection = action_state.get_selected_entry(prompt_bufnr)
15+
return selection.path
1616
end
1717

1818
local switch_worktree = function(prompt_bufnr)
19-
local worktree_path = get_worktree_path(prompt_bufnr)
20-
actions.close(prompt_bufnr)
21-
if worktree_path ~= nil then
22-
git_worktree.switch_worktree(worktree_path)
23-
end
19+
local worktree_path = get_worktree_path(prompt_bufnr)
20+
actions.close(prompt_bufnr)
21+
if worktree_path ~= nil then
22+
git_worktree.switch_worktree(worktree_path)
23+
end
2424
end
2525

2626
local delete_worktree = function(prompt_bufnr, force)
27-
local worktree_path = get_worktree_path(prompt_bufnr)
28-
actions.close(prompt_bufnr)
29-
if worktree_path ~= nil then
30-
git_worktree.delete_worktree(worktree_path, force)
31-
end
27+
local worktree_path = get_worktree_path(prompt_bufnr)
28+
actions.close(prompt_bufnr)
29+
if worktree_path ~= nil then
30+
git_worktree.delete_worktree(worktree_path, force)
31+
end
3232
end
3333

3434
local create_input_prompt = function(cb)
35-
36-
--[[
35+
--[[
3736
local window = Window.centered({
3837
width = 30,
3938
height = 1
@@ -51,140 +50,140 @@ local create_input_prompt = function(cb)
5150
vim.nvim_command("startinsert")
5251
end)
5352
--]]
54-
--
53+
--
5554

56-
local subtree = vim.fn.input("Path to subtree > ")
57-
cb(subtree)
55+
local subtree = vim.fn.input("Path to subtree > ")
56+
cb(subtree)
5857
end
5958

6059
local create_worktree = function(opts)
61-
opts = opts or {}
62-
opts.attach_mappings = function()
63-
actions.select_default:replace(
64-
function(prompt_bufnr, _)
65-
local selected_entry = action_state.get_selected_entry()
66-
local current_line = action_state.get_current_line()
60+
opts = opts or {}
61+
opts.attach_mappings = function()
62+
actions.select_default:replace(function(prompt_bufnr, _)
63+
local selected_entry = action_state.get_selected_entry()
64+
local current_line = action_state.get_current_line()
6765

68-
actions.close(prompt_bufnr)
66+
actions.close(prompt_bufnr)
6967

70-
local branch = selected_entry ~= nil and
71-
selected_entry.value or current_line
68+
local branch = selected_entry ~= nil and selected_entry.value or current_line
7269

73-
if branch == nil then
74-
return
75-
end
70+
if branch == nil then
71+
return
72+
end
7673

77-
create_input_prompt(function(name)
78-
if name == "" then
79-
name = branch
80-
end
81-
git_worktree.create_worktree(name, branch)
82-
end)
83-
end)
74+
create_input_prompt(function(name)
75+
if name == "" then
76+
name = branch
77+
end
78+
git_worktree.create_worktree(name, branch)
79+
end)
80+
end)
8481

85-
-- do we need to replace other default maps?
82+
-- do we need to replace other default maps?
8683

87-
return true
88-
end
89-
require("telescope.builtin").git_branches(opts)
84+
return true
85+
end
86+
require("telescope.builtin").git_branches(opts)
9087
end
9188

9289
local telescope_git_worktree = function(opts)
93-
opts = opts or {}
94-
local output = utils.get_os_command_output({"git", "worktree", "list"})
95-
local results = {}
96-
local widths = {
97-
path = 0,
98-
sha = 0,
99-
branch = 0
90+
opts = opts or {}
91+
local output = utils.get_os_command_output({ "git", "worktree", "list" })
92+
local results = {}
93+
local widths = {
94+
path = 0,
95+
sha = 0,
96+
branch = 0,
97+
}
98+
99+
local parse_line = function(line)
100+
local fields = vim.split(string.gsub(line, "%s+", " "), " ")
101+
local entry = {
102+
path = fields[1],
103+
sha = fields[2],
104+
branch = fields[3],
100105
}
101106

102-
local parse_line = function(line)
103-
local fields = vim.split(string.gsub(line, "%s+", " "), " ")
104-
local entry = {
105-
path = fields[1],
106-
sha = fields[2],
107-
branch = fields[3],
108-
}
109-
110-
if entry.sha ~= "(bare)" then
111-
local index = #results + 1
112-
for key, val in pairs(widths) do
113-
if key == 'path' then
114-
new_path = utils.transform_path(opts, entry[key])
115-
path_len = strings.strdisplaywidth(new_path or "")
116-
widths[key] = math.max(val, path_len)
117-
else
118-
widths[key] = math.max(val, strings.strdisplaywidth(entry[key] or ""))
119-
end
120-
end
121-
122-
table.insert(results, index, entry)
107+
if entry.sha ~= "(bare)" then
108+
local index = #results + 1
109+
for key, val in pairs(widths) do
110+
if key == "path" then
111+
new_path = utils.transform_path(opts, entry[key])
112+
path_len = strings.strdisplaywidth(new_path or "")
113+
widths[key] = math.max(val, path_len)
114+
else
115+
widths[key] = math.max(val, strings.strdisplaywidth(entry[key] or ""))
123116
end
124-
end
117+
end
125118

126-
for _, line in ipairs(output) do
127-
parse_line(line)
128-
end
129-
130-
if #results == 0 then
131-
return
132-
end
133-
134-
local displayer = require("telescope.pickers.entry_display").create {
135-
separator = " ",
136-
items = {
137-
{ width = widths.branch },
138-
{ width = widths.path },
139-
{ width = widths.sha },
140-
},
141-
}
142-
143-
local make_display = function(entry)
144-
return displayer {
145-
{ entry.branch, "TelescopeResultsIdentifier" },
146-
{ utils.transform_path(opts, entry.path) },
147-
{ entry.sha },
148-
}
119+
table.insert(results, index, entry)
149120
end
121+
end
122+
123+
for _, line in ipairs(output) do
124+
parse_line(line)
125+
end
126+
127+
if #results == 0 then
128+
return
129+
end
130+
131+
local displayer = require("telescope.pickers.entry_display").create({
132+
separator = " ",
133+
items = {
134+
{ width = widths.branch },
135+
{ width = widths.path },
136+
{ width = widths.sha },
137+
},
138+
})
139+
140+
local make_display = function(entry)
141+
local path, _ = utils.transform_path(opts, entry.path)
142+
return displayer({
143+
{ entry.branch, "TelescopeResultsIdentifier" },
144+
{ path },
145+
{ entry.sha },
146+
})
147+
end
148+
149+
pickers
150+
.new(opts or {}, {
151+
prompt_title = "Git Worktrees",
152+
finder = finders.new_table({
153+
results = results,
154+
entry_maker = function(entry)
155+
entry.value = entry.branch
156+
entry.ordinal = entry.branch
157+
entry.display = make_display
158+
return entry
159+
end,
160+
}),
161+
sorter = conf.generic_sorter(opts),
162+
attach_mappings = function(_, map)
163+
action_set.select:replace(switch_worktree)
164+
165+
map("i", "<c-d>", function(prompt_bufnr)
166+
delete_worktree(prompt_bufnr)
167+
end)
168+
map("n", "<c-d>", function(prompt_bufnr)
169+
delete_worktree(prompt_bufnr)
170+
end)
171+
map("i", "<c-D>", function(prompt_bufnr)
172+
delete_worktree(prompt_bufnr, true)
173+
end)
174+
map("n", "<c-D>", function(prompt_bufnr)
175+
delete_worktree(prompt_bufnr, true)
176+
end)
150177

151-
pickers.new(opts or {}, {
152-
prompt_title = "Git Worktrees",
153-
finder = finders.new_table {
154-
results = results,
155-
entry_maker = function(entry)
156-
entry.value = entry.branch
157-
entry.ordinal = entry.branch
158-
entry.display = make_display
159-
return entry
160-
end,
161-
},
162-
sorter = conf.generic_sorter(opts),
163-
attach_mappings = function(_, map)
164-
action_set.select:replace(switch_worktree)
165-
166-
map("i", "<c-d>", function(prompt_bufnr)
167-
delete_worktree(prompt_bufnr)
168-
end)
169-
map("n", "<c-d>", function(prompt_bufnr)
170-
delete_worktree(prompt_bufnr)
171-
end)
172-
map("i", "<c-D>", function(prompt_bufnr)
173-
delete_worktree(prompt_bufnr, true)
174-
end)
175-
map("n", "<c-D>", function(prompt_bufnr)
176-
delete_worktree(prompt_bufnr, true)
177-
end)
178-
179-
return true
180-
end
181-
}):find()
178+
return true
179+
end,
180+
})
181+
:find()
182182
end
183183

184-
return require("telescope").register_extension(
185-
{
186-
exports = {
187-
git_worktrees = telescope_git_worktree,
188-
create_git_worktree = create_worktree
189-
}
190-
})
184+
return require("telescope").register_extension({
185+
exports = {
186+
git_worktrees = telescope_git_worktree,
187+
create_git_worktree = create_worktree,
188+
},
189+
})

0 commit comments

Comments
 (0)