From 7323c81bd6209c247248244b12682cc345630301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Cie=C5=9Bla?= Date: Sat, 6 Aug 2022 07:40:07 +0200 Subject: [PATCH] feat(view): Floating nvim tree window #1377 (#1462) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: Alexander Courtis --- doc/nvim-tree-lua.txt | 31 +++++++++++++ lua/nvim-tree.lua | 57 ++++++++++++++++-------- lua/nvim-tree/actions/node/open-file.lua | 2 +- lua/nvim-tree/view.lua | 9 +++- 4 files changed, 78 insertions(+), 21 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index ea099490..2b59d5cc 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -187,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, @@ -639,6 +651,25 @@ Window / buffer setup. Type: `table` 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* UI rendering setup diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 44fed7de..8e8c6d04 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -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 @@ -453,6 +457,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, @@ -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 }, @@ -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 invalid then - if msg then - msg = string.format("%s | %s", msg, invalid) - else - msg = string.format("%s", invalid) + 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) + else + msg = string.format("%s", invalid) + end + user[k] = nil + else + validate(v, def[k], prefix .. k .. ".") end - user[k] = nil - else - validate(v, def[k], prefix .. k .. ".") end end end diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 8c736b7a..98d1faf8 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -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() diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 4e5f1c85..dbe880ff 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -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 @@ -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