Skip to content

Commit 7323c81

Browse files
Khreesalex-courtis
andauthored
feat(view): Floating nvim tree window #1377 (#1462)
* Simple mock-up of floating nvim-tree window * Passing whole table to nvim_open_win() * Run update-help.sh * Use vim.api alias * Add comment to float options * Added `anchor` to float options * Enabling float window enforces `actions.open_file.quit_on_open` * Added documentation * add view.float.open_win_config, skipping validation * Made nvim-tree window closes when float is enabled * Close nvim-tree window when out of focus * Update help Co-authored-by: Krzysztof Cieśla <[email protected]> Co-authored-by: Alexander Courtis <[email protected]>
1 parent 1685484 commit 7323c81

File tree

4 files changed

+76
-19
lines changed

4 files changed

+76
-19
lines changed

doc/nvim-tree-lua.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,24 @@ Subsequent calls to setup will replace the previous configuration.
187187
number = false,
188188
relativenumber = false,
189189
signcolumn = "yes",
190+
-- @deprecated
190191
mappings = {
191192
custom_only = false,
192193
list = {
193194
-- user mappings go here
194195
},
195196
},
197+
float = {
198+
enable = false,
199+
open_win_config = {
200+
relative = "editor",
201+
border = "rounded",
202+
width = 30,
203+
height = 30,
204+
row = 1,
205+
col = 1,
206+
},
207+
},
196208
},
197209
renderer = {
198210
add_trailing = false,
@@ -639,6 +651,25 @@ Window / buffer setup.
639651
Type: `table`
640652
Default: see |nvim-tree-default-mappings|
641653

654+
*nvim-tree.view.float*
655+
Configuration options for floating window
656+
657+
*nvim-tree.view.float.enable*
658+
Display nvim-tree window as float (enforces |nvim-tree.actions.open_file.quit_on_open| if set).
659+
Type: `boolean`, Default: `false`
660+
661+
*nvim-tree.view.float.open_win_config*
662+
Floating window config. See |nvim_open_win| for more details.
663+
Type: `table`, Default:
664+
`{`
665+
`relative = "editor",`
666+
`border = "rounded",`
667+
`width = 30,`
668+
`height = 30,`
669+
`row = 1,`
670+
`col = 1,`
671+
`}`
672+
642673
*nvim-tree.renderer*
643674
UI rendering setup
644675

lua/nvim-tree.lua

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ local function setup_autocommands(opts)
414414
end,
415415
})
416416
end
417+
418+
if opts.view.float.enable then
419+
create_nvim_tree_autocmd("WinLeave", { pattern = "NvimTree_*", callback = view.close })
420+
end
417421
end
418422

419423
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
@@ -453,6 +457,17 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
453457
-- user mappings go here
454458
},
455459
},
460+
float = {
461+
enable = false,
462+
open_win_config = {
463+
relative = "editor",
464+
border = "rounded",
465+
width = 30,
466+
height = 30,
467+
row = 1,
468+
col = 1,
469+
},
470+
},
456471
},
457472
renderer = {
458473
add_trailing = false,
@@ -605,6 +620,10 @@ local function merge_options(conf)
605620
return vim.tbl_deep_extend("force", DEFAULT_OPTS, conf or {})
606621
end
607622

623+
local FIELD_SKIP_VALIDATE = {
624+
open_win_config = true,
625+
}
626+
608627
local FIELD_OVERRIDE_TYPECHECK = {
609628
width = { string = true, ["function"] = true, number = true },
610629
height = { string = true, ["function"] = true, number = true },
@@ -622,25 +641,27 @@ local function validate_options(conf)
622641
end
623642

624643
for k, v in pairs(user) do
625-
local invalid
626-
local override_typecheck = FIELD_OVERRIDE_TYPECHECK[k] or {}
627-
if def[k] == nil then
628-
-- option does not exist
629-
invalid = string.format("unknown option: %s%s", prefix, k)
630-
elseif type(v) ~= type(def[k]) and not override_typecheck[type(v)] then
631-
-- option is of the wrong type and is not a function
632-
invalid = string.format("invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v))
633-
end
644+
if not FIELD_SKIP_VALIDATE[k] then
645+
local invalid
646+
local override_typecheck = FIELD_OVERRIDE_TYPECHECK[k] or {}
647+
if def[k] == nil then
648+
-- option does not exist
649+
invalid = string.format("unknown option: %s%s", prefix, k)
650+
elseif type(v) ~= type(def[k]) and not override_typecheck[type(v)] then
651+
-- option is of the wrong type and is not a function
652+
invalid = string.format("invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v))
653+
end
634654

635-
if invalid then
636-
if msg then
637-
msg = string.format("%s | %s", msg, invalid)
655+
if invalid then
656+
if msg then
657+
msg = string.format("%s | %s", msg, invalid)
658+
else
659+
msg = string.format("%s", invalid)
660+
end
661+
user[k] = nil
638662
else
639-
msg = string.format("%s", invalid)
663+
validate(v, def[k], prefix .. k .. ".")
640664
end
641-
user[k] = nil
642-
else
643-
validate(v, def[k], prefix .. k .. ".")
644665
end
645666
end
646667
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function M.fn(mode, filename)
280280
end
281281

282282
function M.setup(opts)
283-
M.quit_on_open = opts.actions.open_file.quit_on_open
283+
M.quit_on_open = opts.actions.open_file.quit_on_open or opts.view.float.enable
284284
M.resize_window = opts.actions.open_file.resize_window
285285
if opts.actions.open_file.window_picker.chars then
286286
opts.actions.open_file.window_picker.chars = tostring(opts.actions.open_file.window_picker.chars):upper()

lua/nvim-tree/view.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ local function set_window_options_and_buffer()
134134
end
135135

136136
local function open_window()
137-
a.nvim_command "vsp"
138-
M.reposition_window()
137+
if M.View.float.enable then
138+
a.nvim_open_win(0, true, M.View.float.open_win_config)
139+
else
140+
a.nvim_command "vsp"
141+
M.reposition_window()
142+
end
139143
setup_tabpage(a.nvim_get_current_tabpage())
140144
set_window_options_and_buffer()
141145
end
@@ -431,6 +435,7 @@ function M.setup(opts)
431435
M.View.winopts.number = options.number
432436
M.View.winopts.relativenumber = options.relativenumber
433437
M.View.winopts.signcolumn = options.signcolumn
438+
M.View.float = options.float
434439
M.on_attach = opts.on_attach
435440
end
436441

0 commit comments

Comments
 (0)