Skip to content

Commit e05ed6a

Browse files
feat(view): add view.width.padding (#1941)
* fix: variable width accounts for sign/number columns * Add dynamic sizing padding options with neovim/neovim#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>
1 parent 55028e3 commit e05ed6a

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ require("nvim-tree").setup()
7474
require("nvim-tree").setup({
7575
sort_by = "case_sensitive",
7676
view = {
77-
adaptive_size = true,
77+
width = 30,
7878
mappings = {
7979
list = {
8080
{ key = "u", action = "dir_up" },

doc/nvim-tree-lua.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Setup should be run in a lua file or in a |lua-heredoc| if using in a vim file.
8181
require("nvim-tree").setup({
8282
sort_by = "case_sensitive",
8383
view = {
84+
width = 30,
8485
mappings = {
8586
list = {
8687
{ key = "u", action = "dir_up" },
@@ -733,13 +734,17 @@ Window / buffer setup.
733734
Type: `string | number | function | table`, Default: `30`
734735

735736
*nvim-tree.view.width.min*
736-
Minimum dynamic width.
737+
Minimum dynamic width.
737738
Type: `string | number | function`, Default: `30`
738739

739740
*nvim-tree.view.width.max*
740-
Maximum dynamic width, -1 for unbounded.
741+
Maximum dynamic width, -1 for unbounded.
741742
Type: `string | number | function`, Default: `-1`
742743

744+
*nvim-tree.view.width.padding*
745+
Extra padding to the right.
746+
Type: `string | number | function`, Default: `1`
747+
743748
*nvim-tree.view.side*
744749
Side of the tree, can be `"left"`, `"right"`.
745750
Type: `string`, Default: `"left"`

lua/nvim-tree/view.lua

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local log = require "nvim-tree.log"
66

77
local DEFAULT_MIN_WIDTH = 30
88
local DEFAULT_MAX_WIDTH = -1
9+
local DEFAULT_PADDING = 1
910

1011
M.View = {
1112
adaptive_size = false,
@@ -102,7 +103,6 @@ local function create_buffer(bufnr)
102103
end
103104

104105
local function get_size(size)
105-
size = size or M.View.width
106106
if type(size) == "number" then
107107
return size
108108
elseif type(size) == "function" then
@@ -113,6 +113,11 @@ local function get_size(size)
113113
return math.floor(vim.o.columns * percent_as_decimal)
114114
end
115115

116+
local function get_width(size)
117+
size = size or M.View.width
118+
return get_size(size)
119+
end
120+
116121
local move_tbl = {
117122
left = "H",
118123
right = "L",
@@ -252,16 +257,24 @@ end
252257
local function grow()
253258
local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
254259
local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false)
255-
-- 1 column of right-padding to indicate end of path
256-
local padding = 3
260+
-- number of columns of right-padding to indicate end of path
261+
local padding = get_size(M.View.padding)
262+
263+
-- account for sign/number columns etc.
264+
local wininfo = vim.fn.getwininfo(M.get_winnr())
265+
if type(wininfo) == "table" and type(wininfo[1]) == "table" then
266+
padding = padding + wininfo[1].textoff
267+
end
268+
print(padding)
269+
257270
local resizing_width = M.View.initial_width - padding
258271
local max_width
259272

260273
-- maybe bound max
261274
if M.View.max_width == -1 then
262275
max_width = -1
263276
else
264-
max_width = get_size(M.View.max_width) - padding
277+
max_width = get_width(M.View.max_width) - padding
265278
end
266279

267280
for _, l in pairs(lines) do
@@ -313,7 +326,7 @@ function M.resize(size)
313326
return
314327
end
315328

316-
local new_size = get_size()
329+
local new_size = get_width()
317330
vim.api.nvim_win_set_width(M.get_winnr(), new_size)
318331

319332
events._dispatch_on_tree_resize(new_size)
@@ -520,12 +533,13 @@ function M.setup(opts)
520533
M.View.adaptive_size = true
521534
M.View.width = options.width.min or DEFAULT_MIN_WIDTH
522535
M.View.max_width = options.width.max or DEFAULT_MAX_WIDTH
536+
M.View.padding = options.width.padding or DEFAULT_PADDING
523537
else
524538
M.View.adaptive_size = false
525539
M.View.width = options.width
526540
end
527541

528-
M.View.initial_width = get_size()
542+
M.View.initial_width = get_width()
529543
end
530544

531545
return M

0 commit comments

Comments
 (0)