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:
parent
6b26628acf
commit
2002b21be7
@ -83,6 +83,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
|
||||
reload_on_bufenter = false,
|
||||
respect_buf_cwd = false,
|
||||
view = {
|
||||
adaptive_size = false,
|
||||
width = 30,
|
||||
height = 30,
|
||||
hide_root_folder = false,
|
||||
|
||||
@ -101,6 +101,7 @@ Values may be functions. Warning: this may result in unexpected behaviour.
|
||||
reload_on_bufenter = false,
|
||||
respect_buf_cwd = false,
|
||||
view = {
|
||||
adaptive_size = false,
|
||||
width = 30,
|
||||
height = 30,
|
||||
hide_root_folder = false,
|
||||
@ -418,6 +419,11 @@ Git integration with icons and colors.
|
||||
*nvim-tree.view*
|
||||
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*
|
||||
Hide the path of the current working directory on top of the tree.
|
||||
Type: `boolean`, Default: `false`
|
||||
|
||||
@ -360,6 +360,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
||||
reload_on_bufenter = false,
|
||||
respect_buf_cwd = false,
|
||||
view = {
|
||||
adaptive_size = false,
|
||||
width = 30,
|
||||
height = 30,
|
||||
hide_root_folder = false,
|
||||
|
||||
@ -95,6 +95,8 @@ function M.draw()
|
||||
diagnostics.update()
|
||||
end
|
||||
|
||||
view.grow_from_content()
|
||||
|
||||
log.profile_end(ps, "draw")
|
||||
end
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ local M = {}
|
||||
local events = require "nvim-tree.events"
|
||||
|
||||
M.View = {
|
||||
adaptive_size = false,
|
||||
tabpages = {},
|
||||
cursors = {},
|
||||
hide_root_folder = false,
|
||||
@ -187,6 +188,24 @@ function M.open(options)
|
||||
events._dispatch_on_tree_open()
|
||||
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)
|
||||
if type(size) == "string" then
|
||||
size = vim.trim(size)
|
||||
@ -373,8 +392,10 @@ end
|
||||
|
||||
function M.setup(opts)
|
||||
local options = opts.view or {}
|
||||
M.View.adaptive_size = options.adaptive_size
|
||||
M.View.side = options.side
|
||||
M.View.width = options.width
|
||||
M.View.initial_width = get_size()
|
||||
M.View.height = options.height
|
||||
M.View.hide_root_folder = options.hide_root_folder
|
||||
M.View.preserve_window_proportions = options.preserve_window_proportions
|
||||
|
||||
Loading…
Reference in New Issue
Block a user