Skip to content

Commit fe09c05

Browse files
committed
feat(tele): increase telescope config options
- enable option passing during telescope `setup` - add option for changing git worktree display items (ordering, omittion, set widths) - expose worktree specific telescope actions
1 parent d7f4e25 commit fe09c05

File tree

2 files changed

+146
-44
lines changed

2 files changed

+146
-44
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,49 @@ Add the following to your vimrc to load the telescope extension
125125
require("telescope").load_extension("git_worktree")
126126
```
127127

128+
### Configuration
129+
Enjoy the customizability of `telescope.nvim` using all your favorite configurations as well as additional options outlined in the example below.
130+
131+
```lua
132+
require('telescope').setup{
133+
defaults = {
134+
...
135+
},
136+
pickers = {
137+
...
138+
},
139+
extensions = {
140+
git_worktree = {
141+
prompt_title = "Super cool prompt title",
142+
theme = "dropdown",
143+
path_display = { "shorten" },
144+
layout_config = {
145+
width = 70,
146+
height = 20,
147+
},
148+
149+
-- determine what worktree items to show, in order and their corresponding width
150+
-- possible items to show are `branch`, `path`, `sha`
151+
items = {
152+
{ "branch", 50 },
153+
{ "sha", 20 },
154+
},
155+
-- set custom bindings for worktree related actions
156+
mappings = {
157+
["i"] = {
158+
["<C-d>"] = require("telescope").extensions.git_worktree.actions.delete_worktree,
159+
["<C-f>"] = require("telescope").extensions.git_worktree.actions.toggle_forced_deletion,
160+
},
161+
["n"] = {
162+
["<C-d>"] = require("telescope").extensions.git_worktree.actions.delete_worktree,
163+
["<C-f>"] = require("telescope").extensions.git_worktree.actions.toggle_forced_deletion,
164+
},
165+
}
166+
}
167+
}
168+
}
169+
```
170+
128171
### Switch and Delete a worktrees
129172
To bring up the telescope window listing your workspaces run the following
130173

lua/telescope/_extensions/git_worktree.lua

Lines changed: 103 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ local git_worktree = require("git-worktree")
1212

1313
local force_next_deletion = false
1414

15+
local wt_actions = {}
16+
1517
local get_worktree_path = function(prompt_bufnr)
1618
local selection = action_state.get_selected_entry(prompt_bufnr)
1719
return selection.path
@@ -25,7 +27,7 @@ local switch_worktree = function(prompt_bufnr)
2527
end
2628
end
2729

28-
local toggle_forced_deletion = function()
30+
wt_actions.toggle_forced_deletion = function()
2931
-- redraw otherwise the message is not displayed when in insert mode
3032
if force_next_deletion then
3133
print('The next deletion will not be forced')
@@ -68,7 +70,7 @@ local confirm_deletion = function(forcing)
6870
return false
6971
end
7072

71-
local delete_worktree = function(prompt_bufnr)
73+
wt_actions.delete_worktree = function(prompt_bufnr)
7274
if not confirm_deletion() then
7375
return
7476
end
@@ -109,8 +111,58 @@ local create_input_prompt = function(cb)
109111
cb(subtree)
110112
end
111113

114+
local pconf = {
115+
mappings = {
116+
["i"] = {
117+
["<C-d>"] = wt_actions.delete_worktree,
118+
["<C-f>"] = wt_actions.toggle_forced_deletion,
119+
},
120+
["n"] = {
121+
["<C-d>"] = wt_actions.delete_worktree,
122+
["<C-f>"] = wt_actions.toggle_forced_deletion,
123+
},
124+
},
125+
attach_mappings = function(_, _)
126+
action_set.select:replace(switch_worktree)
127+
return true
128+
end
129+
}
130+
131+
local get_default_opts = function(opts)
132+
opts = opts or {}
133+
local defaults = (function()
134+
if pconf.theme then
135+
return require("telescope.themes")["get_" .. pconf.theme](pconf)
136+
end
137+
return vim.deepcopy(pconf)
138+
end)()
139+
140+
if pconf.mappings then
141+
defaults.attach_mappings = function(prompt_bufnr, map)
142+
if pconf.attach_mappings then
143+
pconf.attach_mappings(prompt_bufnr, map)
144+
end
145+
for mode, tbl in pairs(pconf.mappings) do
146+
for key, action in pairs(tbl) do
147+
map(mode, key, action)
148+
end
149+
end
150+
return true
151+
end
152+
end
153+
154+
if opts.attach_mappings then
155+
local opts_attach = opts.attach_mappings
156+
opts.attach_mappings = function(prompt_bufnr, map)
157+
defaults.attach_mappings(prompt_bufnr, map)
158+
return opts_attach(prompt_bufnr, map)
159+
end
160+
end
161+
return vim.tbl_deep_extend("force", defaults, opts)
162+
end
163+
112164
local create_worktree = function(opts)
113-
opts = opts or {}
165+
opts = get_default_opts(opts or {})
114166
opts.attach_mappings = function()
115167
actions.select_default:replace(
116168
function(prompt_bufnr, _)
@@ -142,14 +194,16 @@ local create_worktree = function(opts)
142194
end
143195

144196
local telescope_git_worktree = function(opts)
145-
opts = opts or {}
197+
opts = get_default_opts(opts or {})
146198
local output = utils.get_os_command_output({"git", "worktree", "list"})
147199
local results = {}
148-
local widths = {
149-
path = 0,
150-
sha = 0,
151-
branch = 0
152-
}
200+
201+
local items = vim.F.if_nil(opts.items, {
202+
{ "branch", 0 },
203+
{ "path", 0 },
204+
{ "sha", 0 },
205+
})
206+
local displayer_items = {}
153207

154208
local parse_line = function(line)
155209
local fields = vim.split(string.gsub(line, "%s+", " "), " ")
@@ -161,14 +215,17 @@ local telescope_git_worktree = function(opts)
161215

162216
if entry.sha ~= "(bare)" then
163217
local index = #results + 1
164-
for key, val in pairs(widths) do
165-
if key == 'path' then
166-
local new_path = utils.transform_path(opts, entry[key])
167-
local path_len = strings.strdisplaywidth(new_path or "")
168-
widths[key] = math.max(val, path_len)
169-
else
170-
widths[key] = math.max(val, strings.strdisplaywidth(entry[key] or ""))
218+
for key, item in ipairs(items) do
219+
if not opts.items then
220+
if item[1] == 'path' then
221+
local new_path = utils.transform_path(opts, entry[item[1]])
222+
local path_len = strings.strdisplaywidth(new_path or "")
223+
item[2] = math.max(item[2], path_len)
224+
else
225+
item[2] = math.max(item[2], strings.strdisplaywidth(entry[item[1]] or ""))
226+
end
171227
end
228+
displayer_items[key] = { width = item[2] }
172229
end
173230

174231
table.insert(results, index, entry)
@@ -180,28 +237,33 @@ local telescope_git_worktree = function(opts)
180237
end
181238

182239
if #results == 0 then
240+
error("No git branches found")
183241
return
184242
end
185243

186244
local displayer = require("telescope.pickers.entry_display").create {
187245
separator = " ",
188-
items = {
189-
{ width = widths.branch },
190-
{ width = widths.path },
191-
{ width = widths.sha },
192-
},
246+
items = displayer_items,
193247
}
194248

195249
local make_display = function(entry)
196-
return displayer {
197-
{ entry.branch, "TelescopeResultsIdentifier" },
198-
{ utils.transform_path(opts, entry.path) },
199-
{ entry.sha },
200-
}
250+
local foo = {}
251+
for _, item in ipairs(items) do
252+
if item[1] == "branch" then
253+
table.insert(foo, { entry[item[1]], "TelescopeResultsIdentifier" })
254+
elseif item[1] == "path" then
255+
table.insert(foo, { utils.transform_path(opts, entry[item[1]]) })
256+
elseif item[1] == "sha" then
257+
table.insert(foo, { entry[item[1]] })
258+
else
259+
error("Invalid git-worktree entry item: " .. tostring(item[1]))
260+
end
261+
end
262+
return displayer(foo)
201263
end
202264

203265
pickers.new(opts or {}, {
204-
prompt_title = "Git Worktrees",
266+
prompt_title = opts.prompt_title or "Git Worktrees",
205267
finder = finders.new_table {
206268
results = results,
207269
entry_maker = function(entry)
@@ -212,23 +274,20 @@ local telescope_git_worktree = function(opts)
212274
end,
213275
},
214276
sorter = conf.generic_sorter(opts),
215-
attach_mappings = function(_, map)
216-
action_set.select:replace(switch_worktree)
217-
218-
map("i", "<c-d>", delete_worktree)
219-
map("n", "<c-d>", delete_worktree)
220-
map("i", "<c-f>", toggle_forced_deletion)
221-
map("n", "<c-f>", toggle_forced_deletion)
222-
223-
return true
224-
end
225277
}):find()
226278
end
227279

228-
return require("telescope").register_extension(
229-
{
230-
exports = {
231-
git_worktrees = telescope_git_worktree,
232-
create_git_worktree = create_worktree
233-
}
234-
})
280+
local git_worktree_setup = function(opts)
281+
pconf.mappings = vim.tbl_deep_extend("force", pconf.mappings, require("telescope.config").values.mappings)
282+
pconf = vim.tbl_deep_extend("force", pconf, opts)
283+
end
284+
285+
286+
return require("telescope").register_extension({
287+
setup = git_worktree_setup,
288+
exports = {
289+
git_worktrees = telescope_git_worktree,
290+
create_git_worktree = create_worktree,
291+
actions = wt_actions,
292+
},
293+
})

0 commit comments

Comments
 (0)