From 39aadfc5fa0e0f4477502a3e6f6425a233841fc9 Mon Sep 17 00:00:00 2001 From: Ramez Gerges Date: Thu, 12 Jan 2023 16:05:51 +0200 Subject: [PATCH 01/12] feat: max_width for adaptive_size --- doc/nvim-tree-lua.txt | 7 +++++++ lua/nvim-tree.lua | 2 ++ lua/nvim-tree/view.lua | 21 ++++++++++++++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 2c5d6a3b438..20ff2db96cd 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -194,6 +194,7 @@ Subsequent calls to setup will replace the previous configuration. cursorline = true, debounce_delay = 15, width = 30, + max_width = -1, hide_root_folder = false, side = "left", preserve_window_proportions = false, @@ -730,6 +731,12 @@ Window / buffer setup. a function. Type: `string | number | function`, Default: `30` + *nvim-tree.view.max_width* + Maximum width of the window when `nvim-tree.view.adaptive_size` is `true`, can be + a `%` string, a number representing columns or a function. If set to `-1`, the + width is unbounded. + Type: `string | number | function`, Default: `-1` + *nvim-tree.view.side* Side of the tree, can be `"left"`, `"right"`. Type: `string`, Default: `"left"` diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 883bc5cc511..0ed5e17899a 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -518,6 +518,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS cursorline = true, debounce_delay = 15, width = 30, + max_width = -1, hide_root_folder = false, side = "left", preserve_window_proportions = false, @@ -745,6 +746,7 @@ local FIELD_SKIP_VALIDATE = { local FIELD_OVERRIDE_TYPECHECK = { width = { string = true, ["function"] = true, number = true }, + max_width = { string = true, ["function"] = true, number = true }, remove_keymaps = { boolean = true, table = true }, on_attach = { ["function"] = true, string = true }, sort_by = { ["function"] = true, string = true }, diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 4da5ad65ca5..d33bb08581f 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -98,8 +98,8 @@ local function create_buffer(bufnr) events._dispatch_tree_attached_post(M.get_bufnr()) end -local function get_size() - local size = M.View.width +local function get_size(size) + size = size or M.View.width if type(size) == "number" then return size elseif type(size) == "function" then @@ -246,14 +246,20 @@ end local function grow() local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) - local max_length = M.View.initial_width + local padding = 3 + local resizing_width = M.View.initial_width - padding + local max_width = get_size(M.View.max_width) - padding for _, l in pairs(lines) do - local count = vim.fn.strchars(l) + 3 -- plus some padding - if max_length < count then - max_length = count + local count = vim.fn.strchars(l) + if resizing_width < count then + resizing_width = count + end + if M.View.adaptive_size and max_width >= 0 and resizing_width >= max_width then + resizing_width = max_width + break end end - M.resize(max_length) + M.resize(resizing_width + padding) end function M.grow_from_content() @@ -486,6 +492,7 @@ function M.setup(opts) M.View.centralize_selection = options.centralize_selection M.View.side = (options.side == "right") and "right" or "left" M.View.width = options.width + M.View.max_width = options.max_width M.View.height = options.height M.View.initial_width = get_size() M.View.hide_root_folder = options.hide_root_folder From cdfcf308675b34ba505f412d976c674670de3c4e Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 15 Jan 2023 12:21:56 +1100 Subject: [PATCH 02/12] view grow calculates size correctly based on sign column visibility --- lua/nvim-tree/view.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index d33bb08581f..12f6ab9dfcd 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -246,7 +246,7 @@ end local function grow() local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) - local padding = 3 + local padding = M.View.winopts.signcolumn and 2 or 0 local resizing_width = M.View.initial_width - padding local max_width = get_size(M.View.max_width) - padding for _, l in pairs(lines) do From f2181e6f95fa1b8f908e61fad995a9f8fd9b6b39 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 15 Jan 2023 13:47:23 +1100 Subject: [PATCH 03/12] limit width to a minimum of 20 --- lua/nvim-tree.lua | 7 ++++--- lua/nvim-tree/events.lua | 4 ++-- lua/nvim-tree/view.lua | 36 +++++++++++++++++++++++------------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 0ed5e17899a..6b376ca8743 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -518,7 +518,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS cursorline = true, debounce_delay = 15, width = 30, - max_width = -1, + max_width = 30, hide_root_folder = false, side = "left", preserve_window_proportions = false, @@ -745,8 +745,9 @@ local FIELD_SKIP_VALIDATE = { } local FIELD_OVERRIDE_TYPECHECK = { - width = { string = true, ["function"] = true, number = true }, - max_width = { string = true, ["function"] = true, number = true }, + width = { string = true, ["function"] = true, number = true, ["table"] = true, }, + max = { string = true, ["function"] = true, number = true }, + min = { string = true, ["function"] = true, number = true }, remove_keymaps = { boolean = true, table = true }, on_attach = { ["function"] = true, string = true }, sort_by = { ["function"] = true, string = true }, diff --git a/lua/nvim-tree/events.lua b/lua/nvim-tree/events.lua index 63a9f80b654..8d8e2d6b67f 100644 --- a/lua/nvim-tree/events.lua +++ b/lua/nvim-tree/events.lua @@ -83,8 +83,8 @@ function M._dispatch_on_tree_close() end --@private -function M._dispatch_on_tree_resize(size) - dispatch(M.Event.Resize, size) +function M._dispatch_on_tree_resize(width) + dispatch(M.Event.Resize, width) end --@private diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 12f6ab9dfcd..33f95e8ab9a 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -4,6 +4,9 @@ local events = require "nvim-tree.events" local utils = require "nvim-tree.utils" local log = require "nvim-tree.log" +-- there are sizing oddities when dropping below the default winwidth of 20 +local MIN_WIDTH = 20 + M.View = { adaptive_size = false, centralize_selection = false, @@ -98,16 +101,23 @@ local function create_buffer(bufnr) events._dispatch_tree_attached_post(M.get_bufnr()) end -local function get_size(size) - size = size or M.View.width +---Calculate a window width, minimum MIN_WIDTH +---@param size number|function|string +---@return number +local function calculate_width(size) + local width + if type(size) == "number" then - return size + width = size elseif type(size) == "function" then - return size() + width = size() + else + local size_as_number = tonumber(size:sub(0, -2)) + local percent_as_decimal = size_as_number / 100 + width = math.floor(vim.o.columns * percent_as_decimal) end - local size_as_number = tonumber(size:sub(0, -2)) - local percent_as_decimal = size_as_number / 100 - return math.floor(vim.o.columns * percent_as_decimal) + + return math.max(MIN_WIDTH, width) end local move_tbl = { @@ -246,9 +256,9 @@ end local function grow() local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) - local padding = M.View.winopts.signcolumn and 2 or 0 + local padding = M.View.winopts.signcolumn and 2 or 0 local resizing_width = M.View.initial_width - padding - local max_width = get_size(M.View.max_width) - padding + local max_width = calculate_width(M.View.max_width) - padding for _, l in pairs(lines) do local count = vim.fn.strchars(l) if resizing_width < count then @@ -298,10 +308,10 @@ function M.resize(size) return end - local new_size = get_size() - vim.api.nvim_win_set_width(M.get_winnr(), new_size) + local new_width = calculate_width(M.View.width) + vim.api.nvim_win_set_width(M.get_winnr(), new_width) - events._dispatch_on_tree_resize(new_size) + events._dispatch_on_tree_resize(new_width) if not M.View.preserve_window_proportions then vim.cmd ":wincmd =" @@ -494,7 +504,7 @@ function M.setup(opts) M.View.width = options.width M.View.max_width = options.max_width M.View.height = options.height - M.View.initial_width = get_size() + M.View.initial_width = calculate_width(M.View.width) M.View.hide_root_folder = options.hide_root_folder M.View.tab = opts.tab M.View.preserve_window_proportions = options.preserve_window_proportions From dac0920f87c5a8acd3fffd9b0e4a2474f2e08dcb Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 15 Jan 2023 14:42:35 +1100 Subject: [PATCH 04/12] adaptive_size -> min/max table --- doc/nvim-tree-lua.txt | 20 +++++++++----- lua/nvim-tree.lua | 2 -- lua/nvim-tree/legacy.lua | 11 ++++++++ lua/nvim-tree/view.lua | 58 ++++++++++++++++++++++++++-------------- 4 files changed, 63 insertions(+), 28 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 20ff2db96cd..67d7873b397 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -82,7 +82,6 @@ Setup should be run in a lua file or in a |lua-heredoc| if using in a vim file. require("nvim-tree").setup({ sort_by = "case_sensitive", view = { - adaptive_size = true, mappings = { list = { { key = "u", action = "dir_up" }, @@ -189,12 +188,10 @@ Subsequent calls to setup will replace the previous configuration. remove_keymaps = false, select_prompts = false, view = { - adaptive_size = false, centralize_selection = false, cursorline = true, debounce_delay = 15, width = 30, - max_width = -1, hide_root_folder = false, side = "left", preserve_window_proportions = false, @@ -727,9 +724,20 @@ Window / buffer setup. Type: `boolean`, Default: `false` *nvim-tree.view.width* - Width of the window, can be a `%` string, a number representing columns or - a function. - Type: `string | number | function`, Default: `30` + Width of the window: can be a `%` string, a number representing columns, a + function or a table. + A minimum of 20 as per the |winwidth| default will always apply. + A table indicates that the view should be dynamically sized based on the + longest line (previously `view.adaptive_size`). + Type: `string | number | function | table`, Default: `30` + + *nvim-tree.view.width.min* + Minimum dynamic width. + Type: `string | number | function`, Default: `20` + + *nvim-tree.view.width.max* + Maximum dynamic width, -1 for unbounded. + Type: `string | number | function`, Default: `-1` *nvim-tree.view.max_width* Maximum width of the window when `nvim-tree.view.adaptive_size` is `true`, can be diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 6b376ca8743..59aa6eaf5ab 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -513,12 +513,10 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS remove_keymaps = false, select_prompts = false, view = { - adaptive_size = false, centralize_selection = false, cursorline = true, debounce_delay = 15, width = 30, - max_width = 30, hide_root_folder = false, side = "left", preserve_window_proportions = false, diff --git a/lua/nvim-tree/legacy.lua b/lua/nvim-tree/legacy.lua index 2b0eee06178..1c815d0305b 100644 --- a/lua/nvim-tree/legacy.lua +++ b/lua/nvim-tree/legacy.lua @@ -30,6 +30,17 @@ local function refactored(opts) -- 2023/01/08 utils.move_missing_val(opts, "trash", "require_confirm", opts, "ui.confirm", "trash", true) + + -- 2023/01/15 + if opts.view and opts.view.adaptive_size ~= nil then + if opts.view.adaptive_size and type(opts.view.width) ~= "table" then + local width = opts.view.width + opts.view.width = { + min = width + } + end + opts.view.adaptive_size = nil + end end local function removed(opts) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 33f95e8ab9a..3f0c77b0c99 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -92,9 +92,9 @@ local function create_buffer(bufnr) vim.bo[M.get_bufnr()][option] = value end - if type(M.on_attach) == "function" then + if type(M.config.on_attach) == "function" then require("nvim-tree.keymap").set_keymaps(M.get_bufnr()) - M.on_attach(M.get_bufnr()) + M.config.on_attach(M.get_bufnr()) else require("nvim-tree.actions").apply_mappings(M.get_bufnr()) end @@ -111,10 +111,12 @@ local function calculate_width(size) width = size elseif type(size) == "function" then width = size() - else + elseif type(size) == "string" then local size_as_number = tonumber(size:sub(0, -2)) local percent_as_decimal = size_as_number / 100 width = math.floor(vim.o.columns * percent_as_decimal) + else + width = MIN_WIDTH end return math.max(MIN_WIDTH, width) @@ -258,7 +260,15 @@ local function grow() local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) local padding = M.View.winopts.signcolumn and 2 or 0 local resizing_width = M.View.initial_width - padding - local max_width = calculate_width(M.View.max_width) - padding + local max_width + + -- maybe bound max + if M.config.width.max == -1 then + max_width = -1 + else + max_width = calculate_width(M.config.width.max) - padding + end + for _, l in pairs(lines) do local count = vim.fn.strchars(l) if resizing_width < count then @@ -497,23 +507,31 @@ function M.reset_winhl() end function M.setup(opts) - local options = opts.view or {} - M.View.adaptive_size = options.adaptive_size - M.View.centralize_selection = options.centralize_selection - M.View.side = (options.side == "right") and "right" or "left" - M.View.width = options.width - M.View.max_width = options.max_width - M.View.height = options.height + M.config = opts.view or {} + + M.View.centralize_selection = M.config.centralize_selection + M.View.side = (M.config.side == "right") and "right" or "left" + M.View.height = M.config.height + M.View.hide_root_folder = M.config.hide_root_folder + M.View.tab = M.config.tab + M.View.preserve_window_proportions = M.config.preserve_window_proportions + M.View.winopts.cursorline = M.config.cursorline + M.View.winopts.number = M.config.number + M.View.winopts.relativenumber = M.config.relativenumber + M.View.winopts.signcolumn = M.config.signcolumn + M.View.float = M.config.float + + if type(M.config.width) == "table" then + M.config.width.min = M.config.width.min or MIN_WIDTH + M.config.width.max = M.config.width.max or -1 + M.View.adaptive_size = true + M.View.width = M.config.width.min + else + M.View.adaptive_size = false + M.View.width = M.config.width + end + M.View.initial_width = calculate_width(M.View.width) - M.View.hide_root_folder = options.hide_root_folder - M.View.tab = opts.tab - M.View.preserve_window_proportions = options.preserve_window_proportions - M.View.winopts.cursorline = options.cursorline - 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 return M From 4b06efe030d368b9dc553c89dfeb76668f5ef9a6 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 15 Jan 2023 14:46:51 +1100 Subject: [PATCH 05/12] harden view size calculations against bad user input --- lua/nvim-tree/view.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 3f0c77b0c99..c33628dc21a 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -111,10 +111,17 @@ local function calculate_width(size) width = size elseif type(size) == "function" then width = size() + if type(width) ~= "number" then + width = MIN_WIDTH + end elseif type(size) == "string" then local size_as_number = tonumber(size:sub(0, -2)) - local percent_as_decimal = size_as_number / 100 - width = math.floor(vim.o.columns * percent_as_decimal) + if type(size_as_number) == "number" then + local percent_as_decimal = size_as_number / 100 + width = math.floor(vim.o.columns * percent_as_decimal) + else + width = MIN_WIDTH + end else width = MIN_WIDTH end From 5442e1087dca14f4d3373b12731404c815a7be15 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 15 Jan 2023 14:49:04 +1100 Subject: [PATCH 06/12] style --- lua/nvim-tree.lua | 2 +- lua/nvim-tree/legacy.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 59aa6eaf5ab..f7c6b10f4f0 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -743,7 +743,7 @@ local FIELD_SKIP_VALIDATE = { } local FIELD_OVERRIDE_TYPECHECK = { - width = { string = true, ["function"] = true, number = true, ["table"] = true, }, + width = { string = true, ["function"] = true, number = true, ["table"] = true }, max = { string = true, ["function"] = true, number = true }, min = { string = true, ["function"] = true, number = true }, remove_keymaps = { boolean = true, table = true }, diff --git a/lua/nvim-tree/legacy.lua b/lua/nvim-tree/legacy.lua index 1c815d0305b..dffe6b5510e 100644 --- a/lua/nvim-tree/legacy.lua +++ b/lua/nvim-tree/legacy.lua @@ -36,7 +36,7 @@ local function refactored(opts) if opts.view.adaptive_size and type(opts.view.width) ~= "table" then local width = opts.view.width opts.view.width = { - min = width + min = width, } end opts.view.adaptive_size = nil From b70abdb3ec17fb44c2468b221d8e11e75f2bb8d5 Mon Sep 17 00:00:00 2001 From: Ramez Gerges Date: Tue, 17 Jan 2023 13:51:13 +0200 Subject: [PATCH 07/12] add back an extra column of padding to adaptive resizing --- lua/nvim-tree/view.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index c33628dc21a..725a368bd5b 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -265,7 +265,8 @@ end local function grow() local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) - local padding = M.View.winopts.signcolumn and 2 or 0 + -- 1 column of right-padding to indicate end of path + local padding = (M.View.winopts.signcolumn and 2 or 0) + 1 local resizing_width = M.View.initial_width - padding local max_width From ae7d6eceb4b11fe13ed7c5ec49b1ccf1cdbf6d73 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 21 Jan 2023 14:15:55 +1100 Subject: [PATCH 08/12] back out: limit width to a minimum of 20 --- doc/nvim-tree-lua.txt | 9 +-------- lua/nvim-tree/view.lua | 45 +++++++++++++----------------------------- 2 files changed, 15 insertions(+), 39 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 67d7873b397..a5d846aa781 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -726,25 +726,18 @@ Window / buffer setup. *nvim-tree.view.width* Width of the window: can be a `%` string, a number representing columns, a function or a table. - A minimum of 20 as per the |winwidth| default will always apply. A table indicates that the view should be dynamically sized based on the longest line (previously `view.adaptive_size`). Type: `string | number | function | table`, Default: `30` *nvim-tree.view.width.min* Minimum dynamic width. - Type: `string | number | function`, Default: `20` + Type: `string | number | function`, Default: `30` *nvim-tree.view.width.max* Maximum dynamic width, -1 for unbounded. Type: `string | number | function`, Default: `-1` - *nvim-tree.view.max_width* - Maximum width of the window when `nvim-tree.view.adaptive_size` is `true`, can be - a `%` string, a number representing columns or a function. If set to `-1`, the - width is unbounded. - Type: `string | number | function`, Default: `-1` - *nvim-tree.view.side* Side of the tree, can be `"left"`, `"right"`. Type: `string`, Default: `"left"` diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 725a368bd5b..becb56bebfe 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -4,8 +4,7 @@ local events = require "nvim-tree.events" local utils = require "nvim-tree.utils" local log = require "nvim-tree.log" --- there are sizing oddities when dropping below the default winwidth of 20 -local MIN_WIDTH = 20 +local DEFAULT_MIN_WIDTH = 30 M.View = { adaptive_size = false, @@ -101,32 +100,16 @@ local function create_buffer(bufnr) events._dispatch_tree_attached_post(M.get_bufnr()) end ----Calculate a window width, minimum MIN_WIDTH ----@param size number|function|string ----@return number -local function calculate_width(size) - local width - +local function get_size(size) + size = size or M.View.width if type(size) == "number" then - width = size + return size elseif type(size) == "function" then - width = size() - if type(width) ~= "number" then - width = MIN_WIDTH - end - elseif type(size) == "string" then - local size_as_number = tonumber(size:sub(0, -2)) - if type(size_as_number) == "number" then - local percent_as_decimal = size_as_number / 100 - width = math.floor(vim.o.columns * percent_as_decimal) - else - width = MIN_WIDTH - end - else - width = MIN_WIDTH + return size() end - - return math.max(MIN_WIDTH, width) + local size_as_number = tonumber(size:sub(0, -2)) + local percent_as_decimal = size_as_number / 100 + return math.floor(vim.o.columns * percent_as_decimal) end local move_tbl = { @@ -274,7 +257,7 @@ local function grow() if M.config.width.max == -1 then max_width = -1 else - max_width = calculate_width(M.config.width.max) - padding + max_width = get_size(M.config.width.max) - padding end for _, l in pairs(lines) do @@ -326,10 +309,10 @@ function M.resize(size) return end - local new_width = calculate_width(M.View.width) - vim.api.nvim_win_set_width(M.get_winnr(), new_width) + local new_size = get_size() + vim.api.nvim_win_set_width(M.get_winnr(), new_size) - events._dispatch_on_tree_resize(new_width) + events._dispatch_on_tree_resize(new_size) if not M.View.preserve_window_proportions then vim.cmd ":wincmd =" @@ -530,7 +513,7 @@ function M.setup(opts) M.View.float = M.config.float if type(M.config.width) == "table" then - M.config.width.min = M.config.width.min or MIN_WIDTH + M.config.width.min = M.config.width.min or DEFAULT_MIN_WIDTH M.config.width.max = M.config.width.max or -1 M.View.adaptive_size = true M.View.width = M.config.width.min @@ -539,7 +522,7 @@ function M.setup(opts) M.View.width = M.config.width end - M.View.initial_width = calculate_width(M.View.width) + M.View.initial_width = get_size(M.View.width) end return M From 8e5e01935a7499336084f18a455ec279b6318fab Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 21 Jan 2023 14:18:47 +1100 Subject: [PATCH 09/12] revert unnecessary change --- lua/nvim-tree/events.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/events.lua b/lua/nvim-tree/events.lua index 8d8e2d6b67f..63a9f80b654 100644 --- a/lua/nvim-tree/events.lua +++ b/lua/nvim-tree/events.lua @@ -83,8 +83,8 @@ function M._dispatch_on_tree_close() end --@private -function M._dispatch_on_tree_resize(width) - dispatch(M.Event.Resize, width) +function M._dispatch_on_tree_resize(size) + dispatch(M.Event.Resize, size) end --@private From f9a033d40decbb14542e69769a35609a3bbb00be Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 21 Jan 2023 14:56:26 +1100 Subject: [PATCH 10/12] backout: view grow calculates size correctly based on sign column visibility --- lua/nvim-tree/view.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index becb56bebfe..d1b10979778 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -249,7 +249,7 @@ local function grow() local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false) -- 1 column of right-padding to indicate end of path - local padding = (M.View.winopts.signcolumn and 2 or 0) + 1 + local padding = 3 local resizing_width = M.View.initial_width - padding local max_width From c1d1160c0748dd93638a57a9dbd097252dafebc3 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 21 Jan 2023 15:02:11 +1100 Subject: [PATCH 11/12] remove adaptive_size from help --- doc/nvim-tree-lua.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index a5d846aa781..0d36690845e 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -701,10 +701,6 @@ Type: `boolean`, Default: `false` *nvim-tree.view* Window / buffer setup. - *nvim-tree.view.adaptive_size* - Resize the window on each draw based on the longest line. - Type: `boolean`, Default: `false` - *nvim-tree.view.centralize_selection* When entering nvim-tree, reposition the view so that the current node is initially centralized, see |zz|. From 33e426e8519b906dde07793f8faeea9a2d7a85dc Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 21 Jan 2023 15:15:56 +1100 Subject: [PATCH 12/12] backout unnecessary change M.View.config --- lua/nvim-tree/view.lua | 48 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index d1b10979778..2a7e48638ef 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -5,6 +5,7 @@ local utils = require "nvim-tree.utils" local log = require "nvim-tree.log" local DEFAULT_MIN_WIDTH = 30 +local DEFAULT_MAX_WIDTH = -1 M.View = { adaptive_size = false, @@ -91,9 +92,9 @@ local function create_buffer(bufnr) vim.bo[M.get_bufnr()][option] = value end - if type(M.config.on_attach) == "function" then + if type(M.on_attach) == "function" then require("nvim-tree.keymap").set_keymaps(M.get_bufnr()) - M.config.on_attach(M.get_bufnr()) + M.on_attach(M.get_bufnr()) else require("nvim-tree.actions").apply_mappings(M.get_bufnr()) end @@ -254,10 +255,10 @@ local function grow() local max_width -- maybe bound max - if M.config.width.max == -1 then + if M.View.max_width == -1 then max_width = -1 else - max_width = get_size(M.config.width.max) - padding + max_width = get_size(M.View.max_width) - padding end for _, l in pairs(lines) do @@ -498,31 +499,30 @@ function M.reset_winhl() end function M.setup(opts) - M.config = opts.view or {} - - M.View.centralize_selection = M.config.centralize_selection - M.View.side = (M.config.side == "right") and "right" or "left" - M.View.height = M.config.height - M.View.hide_root_folder = M.config.hide_root_folder - M.View.tab = M.config.tab - M.View.preserve_window_proportions = M.config.preserve_window_proportions - M.View.winopts.cursorline = M.config.cursorline - M.View.winopts.number = M.config.number - M.View.winopts.relativenumber = M.config.relativenumber - M.View.winopts.signcolumn = M.config.signcolumn - M.View.float = M.config.float - - if type(M.config.width) == "table" then - M.config.width.min = M.config.width.min or DEFAULT_MIN_WIDTH - M.config.width.max = M.config.width.max or -1 + local options = opts.view or {} + M.View.centralize_selection = options.centralize_selection + M.View.side = (options.side == "right") and "right" or "left" + M.View.height = options.height + M.View.hide_root_folder = options.hide_root_folder + M.View.tab = opts.tab + M.View.preserve_window_proportions = options.preserve_window_proportions + M.View.winopts.cursorline = options.cursorline + 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 + + if type(options.width) == "table" then M.View.adaptive_size = true - M.View.width = M.config.width.min + M.View.width = options.width.min or DEFAULT_MIN_WIDTH + M.View.max_width = options.width.max or DEFAULT_MAX_WIDTH else M.View.adaptive_size = false - M.View.width = M.config.width + M.View.width = options.width end - M.View.initial_width = get_size(M.View.width) + M.View.initial_width = get_size() end return M