Skip to content

feat(view): Floating nvim tree window #1377 #1462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ Subsequent calls to setup will replace the previous configuration.
sync_root_with_cwd = false,
reload_on_bufenter = false,
respect_buf_cwd = false,
on_attach = "disable", -- function(bufnr). If nil, will use the deprecated mapping strategy
remove_keymaps = false, -- boolean (disable totally or not) or list of key (lhs)
view = {
adaptive_size = false,
centralize_selection = false,
Expand All @@ -185,12 +187,24 @@ Subsequent calls to setup will replace the previous configuration.
number = false,
relativenumber = false,
signcolumn = "yes",
-- @deprecated
mappings = {
custom_only = false,
list = {
-- user mappings go here
},
},
float = {
enable = false,
open_win_config = {
relative = "editor",
border = "rounded",
width = 30,
height = 30,
row = 1,
col = 1,
},
},
},
renderer = {
add_trailing = false,
Expand Down Expand Up @@ -633,6 +647,25 @@ Window / buffer setup.
*nvim-tree.view.mappings.list*
DEPRECATED: see |nvim-tree.on_attach|

*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*
UI rendering setup

Expand Down
53 changes: 37 additions & 16 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ local function setup_autocommands(opts)
end,
})
end

if opts.view.float.enable then
create_nvim_tree_autocmd("WinLeave", { pattern = "NvimTree_*", callback = view.close })
end
end

local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
Expand Down Expand Up @@ -454,6 +458,17 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
-- user mappings go here
},
},
float = {
enable = false,
open_win_config = {
relative = "editor",
border = "rounded",
width = 30,
height = 30,
row = 1,
col = 1,
},
},
},
renderer = {
add_trailing = false,
Expand Down Expand Up @@ -605,6 +620,10 @@ local function merge_options(conf)
return vim.tbl_deep_extend("force", DEFAULT_OPTS, conf or {})
end

local FIELD_SKIP_VALIDATE = {
open_win_config = true,
}

local FIELD_OVERRIDE_TYPECHECK = {
width = { string = true, ["function"] = true, number = true },
height = { string = true, ["function"] = true, number = true },
Expand All @@ -622,25 +641,27 @@ local function validate_options(conf)
end

for k, v in pairs(user) do
local invalid
local override_typecheck = FIELD_OVERRIDE_TYPECHECK[k] or {}
if def[k] == nil then
-- option does not exist
invalid = string.format("unknown option: %s%s", prefix, k)
elseif type(v) ~= type(def[k]) and not override_typecheck[type(v)] then
-- option is of the wrong type and is not a function
invalid = string.format("invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v))
end
if not FIELD_SKIP_VALIDATE[k] then
local invalid
local override_typecheck = FIELD_OVERRIDE_TYPECHECK[k] or {}
if def[k] == nil then
-- option does not exist
invalid = string.format("unknown option: %s%s", prefix, k)
elseif type(v) ~= type(def[k]) and not override_typecheck[type(v)] then
-- option is of the wrong type and is not a function
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
msg = string.format("%s | %s", msg, invalid)
if invalid then
if msg then
msg = string.format("%s | %s", msg, invalid)
else
msg = string.format("%s", invalid)
end
user[k] = nil
else
msg = string.format("%s", invalid)
validate(v, def[k], prefix .. k .. ".")
end
user[k] = nil
else
validate(v, def[k], prefix .. k .. ".")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function M.fn(mode, filename)
end

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
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()
Expand Down
9 changes: 7 additions & 2 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ local function set_window_options_and_buffer()
end

local function open_window()
a.nvim_command "vsp"
M.reposition_window()
if M.View.float.enable then
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())
set_window_options_and_buffer()
end
Expand Down Expand Up @@ -431,6 +435,7 @@ function M.setup(opts)
M.View.winopts.number = options.number
M.View.winopts.relativenumber = options.relativenumber
M.View.winopts.signcolumn = options.signcolumn
M.View.float = options.float
M.on_attach = opts.on_attach
end

Expand Down