feat(resize): add ability to grow and shrink the tree when drawing (#1293)

Will adapt the view size based on the longest line.
fixes #865
This commit is contained in:
Kiyan 2022-05-31 09:05:00 +02:00 committed by GitHub
parent 6b26628acf
commit 2002b21be7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 0 deletions

View File

@ -83,6 +83,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
reload_on_bufenter = false, reload_on_bufenter = false,
respect_buf_cwd = false, respect_buf_cwd = false,
view = { view = {
adaptive_size = false,
width = 30, width = 30,
height = 30, height = 30,
hide_root_folder = false, hide_root_folder = false,

View File

@ -101,6 +101,7 @@ Values may be functions. Warning: this may result in unexpected behaviour.
reload_on_bufenter = false, reload_on_bufenter = false,
respect_buf_cwd = false, respect_buf_cwd = false,
view = { view = {
adaptive_size = false,
width = 30, width = 30,
height = 30, height = 30,
hide_root_folder = false, hide_root_folder = false,
@ -418,6 +419,11 @@ Git integration with icons and colors.
*nvim-tree.view* *nvim-tree.view*
Window / buffer setup. Window / buffer setup.
*nvim-tree.view.adaptive_size*
Resize the window on each draw based on the longest line.
Only works when |nvim-tree.view.side| is `left` or `right`.
Type: `boolean`, Default: `false`
*nvim-tree.view.hide_root_folder* *nvim-tree.view.hide_root_folder*
Hide the path of the current working directory on top of the tree. Hide the path of the current working directory on top of the tree.
Type: `boolean`, Default: `false` Type: `boolean`, Default: `false`

View File

@ -360,6 +360,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
reload_on_bufenter = false, reload_on_bufenter = false,
respect_buf_cwd = false, respect_buf_cwd = false,
view = { view = {
adaptive_size = false,
width = 30, width = 30,
height = 30, height = 30,
hide_root_folder = false, hide_root_folder = false,

View File

@ -95,6 +95,8 @@ function M.draw()
diagnostics.update() diagnostics.update()
end end
view.grow_from_content()
log.profile_end(ps, "draw") log.profile_end(ps, "draw")
end end

View File

@ -5,6 +5,7 @@ local M = {}
local events = require "nvim-tree.events" local events = require "nvim-tree.events"
M.View = { M.View = {
adaptive_size = false,
tabpages = {}, tabpages = {},
cursors = {}, cursors = {},
hide_root_folder = false, hide_root_folder = false,
@ -187,6 +188,24 @@ function M.open(options)
events._dispatch_on_tree_open() events._dispatch_on_tree_open()
end 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
for _, l in pairs(lines) do
if max_length < #l then
max_length = #l
end
end
M.resize(max_length)
end
function M.grow_from_content()
if M.View.adaptive_size and M.View.side == "left" or M.View.side == "right" then
grow()
end
end
function M.resize(size) function M.resize(size)
if type(size) == "string" then if type(size) == "string" then
size = vim.trim(size) size = vim.trim(size)
@ -373,8 +392,10 @@ end
function M.setup(opts) function M.setup(opts)
local options = opts.view or {} local options = opts.view or {}
M.View.adaptive_size = options.adaptive_size
M.View.side = options.side M.View.side = options.side
M.View.width = options.width M.View.width = options.width
M.View.initial_width = get_size()
M.View.height = options.height M.View.height = options.height
M.View.hide_root_folder = options.hide_root_folder M.View.hide_root_folder = options.hide_root_folder
M.View.preserve_window_proportions = options.preserve_window_proportions M.View.preserve_window_proportions = options.preserve_window_proportions