Skip to content

Add dynamic sizing padding options #1941

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
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ require("nvim-tree").setup()
require("nvim-tree").setup({
sort_by = "case_sensitive",
view = {
adaptive_size = true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that this is now deprecated, so I updated the example code in the README with the new way of working with dynamic sizes and then tacked my padding options on

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. It seems I forgot to update the README... please update to match help

sort_by = "case_sensitive",

it's only there to provide an example of a simple option to pass to setup.

width = 30,
mappings = {
list = {
{ key = "u", action = "dir_up" },
Expand Down
9 changes: 7 additions & 2 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ 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 = {
width = 30,
mappings = {
list = {
{ key = "u", action = "dir_up" },
Expand Down Expand Up @@ -733,13 +734,17 @@ Window / buffer setup.
Type: `string | number | function | table`, Default: `30`

*nvim-tree.view.width.min*
Minimum dynamic width.
Minimum dynamic width.
Type: `string | number | function`, Default: `30`

*nvim-tree.view.width.max*
Maximum dynamic width, -1 for unbounded.
Maximum dynamic width, -1 for unbounded.
Type: `string | number | function`, Default: `-1`

*nvim-tree.view.width.padding*
Extra padding to the right.
Type: `string | number | function`, Default: `1`

*nvim-tree.view.side*
Side of the tree, can be `"left"`, `"right"`.
Type: `string`, Default: `"left"`
Expand Down
26 changes: 20 additions & 6 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local log = require "nvim-tree.log"

local DEFAULT_MIN_WIDTH = 30
local DEFAULT_MAX_WIDTH = -1
local DEFAULT_PADDING = 1

M.View = {
adaptive_size = false,
Expand Down Expand Up @@ -102,7 +103,6 @@ local function create_buffer(bufnr)
end

local function get_size(size)
size = size or M.View.width
if type(size) == "number" then
return size
elseif type(size) == "function" then
Expand All @@ -113,6 +113,11 @@ local function get_size(size)
return math.floor(vim.o.columns * percent_as_decimal)
end

local function get_width(size)
size = size or M.View.width
return get_size(size)
end

local move_tbl = {
left = "H",
right = "L",
Expand Down Expand Up @@ -249,16 +254,24 @@ 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)
-- 1 column of right-padding to indicate end of path
local padding = 3
-- number of columns of right-padding to indicate end of path
local padding = get_size(M.View.padding)

-- account for sign/number columns etc.
local wininfo = vim.fn.getwininfo(M.get_winnr())
if type(wininfo) == "table" and type(wininfo[1]) == "table" then
padding = padding + wininfo[1].textoff
end
print(padding)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this stray debug print statement.
This gives me an extra "Press ENTER or type command to continue" prompt with the padding printed, whenever I toggle Nvim tree window.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry that was my mistake, the problem was fixed a few days ago in master ^_^


local resizing_width = M.View.initial_width - padding
local max_width

-- maybe bound max
if M.View.max_width == -1 then
max_width = -1
else
max_width = get_size(M.View.max_width) - padding
max_width = get_width(M.View.max_width) - padding
end

for _, l in pairs(lines) do
Expand Down Expand Up @@ -310,7 +323,7 @@ function M.resize(size)
return
end

local new_size = get_size()
local new_size = get_width()
vim.api.nvim_win_set_width(M.get_winnr(), new_size)

events._dispatch_on_tree_resize(new_size)
Expand Down Expand Up @@ -517,12 +530,13 @@ function M.setup(opts)
M.View.adaptive_size = true
M.View.width = options.width.min or DEFAULT_MIN_WIDTH
M.View.max_width = options.width.max or DEFAULT_MAX_WIDTH
M.View.padding = options.width.padding or DEFAULT_PADDING
else
M.View.adaptive_size = false
M.View.width = options.width
end

M.View.initial_width = get_size()
M.View.initial_width = get_width()
end

return M