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 <krzysztof.marcin.ciesla@cern.ch>
Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
Krzysztof Cieśla 2022-08-06 07:40:07 +02:00 committed by GitHub
parent 1685484738
commit 7323c81bd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 21 deletions

View File

@ -187,12 +187,24 @@ Subsequent calls to setup will replace the previous configuration.
number = false, number = false,
relativenumber = false, relativenumber = false,
signcolumn = "yes", signcolumn = "yes",
-- @deprecated
mappings = { mappings = {
custom_only = false, custom_only = false,
list = { list = {
-- user mappings go here -- user mappings go here
}, },
}, },
float = {
enable = false,
open_win_config = {
relative = "editor",
border = "rounded",
width = 30,
height = 30,
row = 1,
col = 1,
},
},
}, },
renderer = { renderer = {
add_trailing = false, add_trailing = false,
@ -639,6 +651,25 @@ Window / buffer setup.
Type: `table` Type: `table`
Default: see |nvim-tree-default-mappings| Default: see |nvim-tree-default-mappings|
*nvim-tree.view.float*
Configuration options for floating window
*nvim-tree.view.float.enable*
Display nvim-tree window as float (enforces |nvim-tree.actions.open_file.quit_on_open| if set).
Type: `boolean`, Default: `false`
*nvim-tree.view.float.open_win_config*
Floating window config. See |nvim_open_win| for more details.
Type: `table`, Default:
`{`
`relative = "editor",`
`border = "rounded",`
`width = 30,`
`height = 30,`
`row = 1,`
`col = 1,`
`}`
*nvim-tree.renderer* *nvim-tree.renderer*
UI rendering setup UI rendering setup

View File

@ -414,6 +414,10 @@ local function setup_autocommands(opts)
end, end,
}) })
end end
if opts.view.float.enable then
create_nvim_tree_autocmd("WinLeave", { pattern = "NvimTree_*", callback = view.close })
end
end end
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
@ -453,6 +457,17 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
-- user mappings go here -- user mappings go here
}, },
}, },
float = {
enable = false,
open_win_config = {
relative = "editor",
border = "rounded",
width = 30,
height = 30,
row = 1,
col = 1,
},
},
}, },
renderer = { renderer = {
add_trailing = false, add_trailing = false,
@ -605,6 +620,10 @@ local function merge_options(conf)
return vim.tbl_deep_extend("force", DEFAULT_OPTS, conf or {}) return vim.tbl_deep_extend("force", DEFAULT_OPTS, conf or {})
end end
local FIELD_SKIP_VALIDATE = {
open_win_config = true,
}
local FIELD_OVERRIDE_TYPECHECK = { local FIELD_OVERRIDE_TYPECHECK = {
width = { string = true, ["function"] = true, number = true }, width = { string = true, ["function"] = true, number = true },
height = { string = true, ["function"] = true, number = true }, height = { string = true, ["function"] = true, number = true },
@ -622,25 +641,27 @@ local function validate_options(conf)
end end
for k, v in pairs(user) do for k, v in pairs(user) do
local invalid if not FIELD_SKIP_VALIDATE[k] then
local override_typecheck = FIELD_OVERRIDE_TYPECHECK[k] or {} local invalid
if def[k] == nil then local override_typecheck = FIELD_OVERRIDE_TYPECHECK[k] or {}
-- option does not exist if def[k] == nil then
invalid = string.format("unknown option: %s%s", prefix, k) -- option does not exist
elseif type(v) ~= type(def[k]) and not override_typecheck[type(v)] then invalid = string.format("unknown option: %s%s", prefix, k)
-- option is of the wrong type and is not a function elseif type(v) ~= type(def[k]) and not override_typecheck[type(v)] then
invalid = string.format("invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v)) -- option is of the wrong type and is not a function
end invalid = string.format("invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v))
end
if invalid then
if msg then if invalid then
msg = string.format("%s | %s", msg, invalid) if msg then
else msg = string.format("%s | %s", msg, invalid)
msg = string.format("%s", invalid) else
msg = string.format("%s", invalid)
end
user[k] = nil
else
validate(v, def[k], prefix .. k .. ".")
end end
user[k] = nil
else
validate(v, def[k], prefix .. k .. ".")
end end
end end
end end

View File

@ -280,7 +280,7 @@ function M.fn(mode, filename)
end end
function M.setup(opts) function M.setup(opts)
M.quit_on_open = opts.actions.open_file.quit_on_open M.quit_on_open = opts.actions.open_file.quit_on_open or opts.view.float.enable
M.resize_window = opts.actions.open_file.resize_window M.resize_window = opts.actions.open_file.resize_window
if opts.actions.open_file.window_picker.chars then if opts.actions.open_file.window_picker.chars then
opts.actions.open_file.window_picker.chars = tostring(opts.actions.open_file.window_picker.chars):upper() opts.actions.open_file.window_picker.chars = tostring(opts.actions.open_file.window_picker.chars):upper()

View File

@ -134,8 +134,12 @@ local function set_window_options_and_buffer()
end end
local function open_window() local function open_window()
a.nvim_command "vsp" if M.View.float.enable then
M.reposition_window() a.nvim_open_win(0, true, M.View.float.open_win_config)
else
a.nvim_command "vsp"
M.reposition_window()
end
setup_tabpage(a.nvim_get_current_tabpage()) setup_tabpage(a.nvim_get_current_tabpage())
set_window_options_and_buffer() set_window_options_and_buffer()
end end
@ -431,6 +435,7 @@ function M.setup(opts)
M.View.winopts.number = options.number M.View.winopts.number = options.number
M.View.winopts.relativenumber = options.relativenumber M.View.winopts.relativenumber = options.relativenumber
M.View.winopts.signcolumn = options.signcolumn M.View.winopts.signcolumn = options.signcolumn
M.View.float = options.float
M.on_attach = opts.on_attach M.on_attach = opts.on_attach
end end