Skip to content

Commit d85b671

Browse files
feat(picker): allow custom function actions.open_file.window_picker.picker (#1782)
* feat: allow passing a custom function as a window picker WIP * fix: move logic expression to if statement If `M.window_picker.custom_function()` returns `nil` then `pick_win_id()` will run (the or part). We don't want that. More verbose, but better. * feat(open): add window_picker.picker * feat(open): add window_picker.picker * style nit * feat(open): add window_picker.picker * docs: add window_picker.picker documentation * docs: add window_picker.picker documentation Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent 18272f8 commit d85b671

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/nvim-tree-lua.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ Subsequent calls to setup will replace the previous configuration.
348348
resize_window = true,
349349
window_picker = {
350350
enable = true,
351+
picker = "default",
351352
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
352353
exclude = {
353354
filetype = { "notify", "packer", "qf", "diff", "fugitive", "fugitiveblame" },
@@ -999,6 +1000,16 @@ Configuration for various actions.
9991000
from which you last opened the tree.
10001001
Type: `boolean`, Default: `true`
10011002

1003+
*nvim-tree.actions.open_file.window_picker.picker*
1004+
Change the default window picker, can be a string `"default"` or a function.
1005+
The function should return the window id that will open the node,
1006+
or `nil` if an invalid window is picked or user cancelled the action.
1007+
Type: `string` | `function`, Default: `"default"`
1008+
e.g. s1n7ax/nvim-window-picker plugin: >
1009+
window_picker = {
1010+
enable = true,
1011+
picker = require('window-picker').pick_window,
1012+
<
10021013
*nvim-tree.actions.open_file.window_picker.chars*
10031014
A string of chars used as identifiers by the window picker.
10041015
Type: `string`, Default: `"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"`

lua/nvim-tree.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
640640
resize_window = true,
641641
window_picker = {
642642
enable = true,
643+
picker = "default",
643644
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
644645
exclude = {
645646
filetype = { "notify", "packer", "qf", "diff", "fugitive", "fugitiveblame" },
@@ -699,6 +700,7 @@ local FIELD_OVERRIDE_TYPECHECK = {
699700
on_attach = { ["function"] = true, string = true },
700701
sort_by = { ["function"] = true, string = true },
701702
root_folder_label = { ["function"] = true, string = true },
703+
picker = { ["function"] = true, string = true },
702704
}
703705

704706
local function validate_options(conf)

lua/nvim-tree/actions/node/open-file.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ local function get_target_winid(mode, win_ids)
175175
end
176176
else
177177
-- pick a window
178-
target_winid = pick_win_id()
178+
if type(M.window_picker.picker) == "function" then
179+
target_winid = M.window_picker.picker()
180+
else
181+
target_winid = pick_win_id()
182+
end
179183
if target_winid == nil then
180184
-- pick failed/cancelled
181185
return

0 commit comments

Comments
 (0)