feat(view): add view.width.padding (#1941)

* fix: variable width accounts for sign/number columns

* Add dynamic sizing padding options

with https://github.com/neovim/neovim/pull/20621 merged in it is now
possible to fully customize the status-column in nvim (the column on the
left containing line-numbers, fold info, signs and borders).

A fair few cool implementations have popped up like:
- https://github.com/CKolkey/config/blob/master/nvim/after/plugin/statuscolumn.lua
- https://github.com/luukvbaal/statuscol.nvim
- and my own personal one (based on CKolkey's fantastic work) https://git.hendrikpeter.net/hendrikpeter/pico-vim/-/blob/main/lua/peva/status_column.lua

The problem with nvim-tree however is that dynamic sizing doesn't take
the custom size of a status column into account and the end of file
names get clipped off. This little patch should fix that (and give some
examples to help other status_column modders get started).

Thanks for looking at this and thanks for making this amazing plugin,
I've been using it for a while and I really like it!

* allow padding function, update docs, rollback readme

* typo in example setup

* help formatting

---------

Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
Peter van der Meulen 2023-01-28 04:07:14 +01:00 committed by GitHub
parent 55028e30d7
commit e05ed6a60f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 9 deletions

View File

@ -74,7 +74,7 @@ require("nvim-tree").setup()
require("nvim-tree").setup({ require("nvim-tree").setup({
sort_by = "case_sensitive", sort_by = "case_sensitive",
view = { view = {
adaptive_size = true, width = 30,
mappings = { mappings = {
list = { list = {
{ key = "u", action = "dir_up" }, { key = "u", action = "dir_up" },

View File

@ -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({ require("nvim-tree").setup({
sort_by = "case_sensitive", sort_by = "case_sensitive",
view = { view = {
width = 30,
mappings = { mappings = {
list = { list = {
{ key = "u", action = "dir_up" }, { key = "u", action = "dir_up" },
@ -733,13 +734,17 @@ Window / buffer setup.
Type: `string | number | function | table`, Default: `30` Type: `string | number | function | table`, Default: `30`
*nvim-tree.view.width.min* *nvim-tree.view.width.min*
Minimum dynamic width. Minimum dynamic width.
Type: `string | number | function`, Default: `30` Type: `string | number | function`, Default: `30`
*nvim-tree.view.width.max* *nvim-tree.view.width.max*
Maximum dynamic width, -1 for unbounded. Maximum dynamic width, -1 for unbounded.
Type: `string | number | function`, Default: `-1` 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* *nvim-tree.view.side*
Side of the tree, can be `"left"`, `"right"`. Side of the tree, can be `"left"`, `"right"`.
Type: `string`, Default: `"left"` Type: `string`, Default: `"left"`

View File

@ -6,6 +6,7 @@ local log = require "nvim-tree.log"
local DEFAULT_MIN_WIDTH = 30 local DEFAULT_MIN_WIDTH = 30
local DEFAULT_MAX_WIDTH = -1 local DEFAULT_MAX_WIDTH = -1
local DEFAULT_PADDING = 1
M.View = { M.View = {
adaptive_size = false, adaptive_size = false,
@ -102,7 +103,6 @@ local function create_buffer(bufnr)
end end
local function get_size(size) local function get_size(size)
size = size or M.View.width
if type(size) == "number" then if type(size) == "number" then
return size return size
elseif type(size) == "function" then elseif type(size) == "function" then
@ -113,6 +113,11 @@ local function get_size(size)
return math.floor(vim.o.columns * percent_as_decimal) return math.floor(vim.o.columns * percent_as_decimal)
end end
local function get_width(size)
size = size or M.View.width
return get_size(size)
end
local move_tbl = { local move_tbl = {
left = "H", left = "H",
right = "L", right = "L",
@ -252,8 +257,16 @@ end
local function grow() local function grow()
local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0 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 lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false)
-- 1 column of right-padding to indicate end of path -- number of columns of right-padding to indicate end of path
local padding = 3 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)
local resizing_width = M.View.initial_width - padding local resizing_width = M.View.initial_width - padding
local max_width local max_width
@ -261,7 +274,7 @@ local function grow()
if M.View.max_width == -1 then if M.View.max_width == -1 then
max_width = -1 max_width = -1
else else
max_width = get_size(M.View.max_width) - padding max_width = get_width(M.View.max_width) - padding
end end
for _, l in pairs(lines) do for _, l in pairs(lines) do
@ -313,7 +326,7 @@ function M.resize(size)
return return
end end
local new_size = get_size() local new_size = get_width()
vim.api.nvim_win_set_width(M.get_winnr(), new_size) vim.api.nvim_win_set_width(M.get_winnr(), new_size)
events._dispatch_on_tree_resize(new_size) events._dispatch_on_tree_resize(new_size)
@ -520,12 +533,13 @@ function M.setup(opts)
M.View.adaptive_size = true M.View.adaptive_size = true
M.View.width = options.width.min or DEFAULT_MIN_WIDTH M.View.width = options.width.min or DEFAULT_MIN_WIDTH
M.View.max_width = options.width.max or DEFAULT_MAX_WIDTH M.View.max_width = options.width.max or DEFAULT_MAX_WIDTH
M.View.padding = options.width.padding or DEFAULT_PADDING
else else
M.View.adaptive_size = false M.View.adaptive_size = false
M.View.width = options.width M.View.width = options.width
end end
M.View.initial_width = get_size() M.View.initial_width = get_width()
end end
return M return M