Skip to content

Commit fb32f35

Browse files
committed
chore: allow configuring height
also fixes window management for top and bottom tree side
1 parent 8fe2b54 commit fb32f35

File tree

6 files changed

+53
-20
lines changed

6 files changed

+53
-20
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ require'nvim-tree'.setup {
5858
},
5959
-- hijack the cursor in the tree to put it at the start of the filename
6060
hijack_cursor = false,
61-
-- updates the root directory of the tree on `DirChanged` (when your run `:cd` usually)
61+
-- updates the root directory of the tree on `DirChanged` (when your run `:cd` usually)
6262
update_cwd = false,
6363
-- show lsp diagnostics in the signcolumn
6464
lsp_diagnostics = false,
@@ -82,8 +82,10 @@ require'nvim-tree'.setup {
8282
},
8383

8484
view = {
85-
-- width of the window, can be either a number (columns) or a string in `%`
85+
-- width of the window, can be either a number (columns) or a string in `%`, for left or right side placement
8686
width = 30,
87+
-- height of the window, can be either a number (columns) or a string in `%`, for top or bottom side placement
88+
height = 30,
8789
-- side of the tree, can be one of 'left' | 'right' | 'top' | 'bottom'
8890
side = 'left',
8991
-- if true the tree will resize itself after opening a file

doc/nvim-tree-lua.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ function.
9090
},
9191
view = {
9292
width = 30,
93+
height = 30,
9394
side = 'left',
9495
auto_resize = false,
9596
mappings = {
@@ -204,26 +205,31 @@ Here is a list of the options available in the setup call:
204205
- |view|: window / buffer setup
205206

206207
- |view.width|: width of the window, can be either a `%` string or
207-
a number representing columns
208+
a number representing columns. Only works with |view.side| `left` or `right`
208209
type: `string | number`
209210
default: `30`
210-
211+
212+
- |view.height|: height of the window, can be either a `%` string or
213+
a number representing rows. Only works with |view.side| `top` or `bottom`
214+
type: `string | number`
215+
default: `30`
216+
211217
- |view.side|: side of the tree, can be one of 'left' | 'right' | 'bottom' | 'top'
212218
Note that bottom/top are not working correctly yet.
213219
type: `string`
214220
default: 'left'
215-
221+
216222
- |view.auto_resize|: auto resize the tree after opening a file
217223
type: `boolean`
218224
default: false
219-
225+
220226
- |view.mappings|: configuration options for keymaps
221-
227+
222228
- |view.mappings.custom_only|: will use only the provided user mappings and not the default
223229
otherwise, extends the default mappings with the provided user mappings
224230
type: `boolean`
225231
default: false
226-
232+
227233
- |view,mappings.list|: a list of keymaps that will extend or override the default keymaps
228234
type: list of `{ key: table of strings or string, mode: string (vim-mode), cb: callback function as a string }`
229235
default: {}

lua/nvim-tree.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ end
270270

271271
function M.resize(size)
272272
view.View.width = size
273+
view.View.height = size
273274
view.resize()
274275
end
275276

lua/nvim-tree/config.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,23 @@ end
8080

8181
function M.window_options()
8282
local opts = {}
83-
if vim.g.nvim_tree_side == 'right' then
83+
local side = require'nvim-tree.view'.View.side
84+
if side == 'right' then
8485
opts.open_command = 'h'
8586
opts.preview_command = 'l'
8687
opts.split_command = 'aboveleft'
87-
else
88+
elseif side == "left" then
8889
opts.open_command = 'l'
8990
opts.preview_command = 'h'
9091
opts.split_command = 'belowright'
92+
elseif side == "top" then
93+
opts.open_command = 'j'
94+
opts.preview_command = 'k'
95+
opts.split_command = 'bot'
96+
else
97+
opts.open_command = 'k'
98+
opts.preview_command = 'j'
99+
opts.split_command = 'top'
91100
end
92101

93102
return opts

lua/nvim-tree/lib.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ local populate = pops.populate
1313
local refresh_entries = pops.refresh_entries
1414

1515
local first_init_done = false
16-
local window_opts = config.window_options()
1716

1817
local M = {}
1918

@@ -368,7 +367,9 @@ function M.open_file(mode, filename)
368367
if not target_winid or not vim.tbl_contains(win_ids, target_winid) then
369368
-- Target is invalid, or window does not exist in current tabpage: create
370369
-- new window
371-
vim.cmd(window_opts.split_command .. " vsp")
370+
local window_opts = config.window_options()
371+
local splitside = view.is_vertical() and "vsp" or "sp"
372+
vim.cmd(window_opts.split_command .. " " .. splitside)
372373
target_winid = api.nvim_get_current_win()
373374
M.Tree.target_winid = target_winid
374375

lua/nvim-tree/view.lua

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ end
106106

107107
local DEFAULT_CONFIG = {
108108
width = 30,
109+
height = 30,
109110
side = 'left',
110111
auto_resize = false,
111112
mappings = {
@@ -141,6 +142,7 @@ function M.setup(opts)
141142
local options = vim.tbl_deep_extend('force', DEFAULT_CONFIG, opts)
142143
M.View.side = options.side
143144
M.View.width = options.width
145+
M.View.height = options.height
144146
M.View.auto_resize = opts.auto_resize
145147
if options.mappings.custom_only then
146148
M.View.mappings = options.mappings.list
@@ -186,7 +188,8 @@ function M._prevent_buffer_override()
186188
end
187189

188190
if #vim.api.nvim_list_wins() < 2 then
189-
vim.cmd("vsplit")
191+
local cmd = M.is_vertical() and "vsplit" or "split"
192+
vim.cmd(cmd)
190193
else
191194
vim.cmd("wincmd "..move_cmd[M.View.side])
192195
end
@@ -228,12 +231,18 @@ function M.focus(winnr, open_if_closed)
228231
a.nvim_set_current_win(wnr)
229232
end
230233

231-
local function get_width()
232-
if type(M.View.width) == "number" then
233-
return M.View.width
234+
function M.is_vertical()
235+
return M.View.side == 'left' or M.View.side == 'right'
236+
end
237+
238+
local function get_size()
239+
local width_or_height = M.is_vertical() and 'width' or 'height'
240+
local size = M.View[width_or_height]
241+
if type(size) == "number" then
242+
return size
234243
end
235-
local width_as_number = tonumber(M.View.width:sub(0, -2))
236-
local percent_as_decimal = width_as_number / 100
244+
local size_as_number = tonumber(size:sub(0, -2))
245+
local percent_as_decimal = size_as_number / 100
237246
return math.floor(vim.o.columns * percent_as_decimal)
238247
end
239248

@@ -242,7 +251,11 @@ function M.resize()
242251
return
243252
end
244253

245-
a.nvim_win_set_width(M.get_winnr(), get_width())
254+
if M.is_vertical() then
255+
a.nvim_win_set_width(M.get_winnr(), get_size())
256+
else
257+
a.nvim_win_set_height(M.get_winnr(), get_size())
258+
end
246259
end
247260

248261
local move_tbl = {
@@ -268,7 +281,8 @@ end
268281
function M.replace_window()
269282
local move_to = move_tbl[M.View.side]
270283
a.nvim_command("wincmd "..move_to)
271-
a.nvim_command("vertical resize "..get_width())
284+
local resize_direction = M.is_vertical() and 'vertical ' or ''
285+
a.nvim_command(resize_direction.."resize "..get_size())
272286
end
273287

274288
local function open_window()

0 commit comments

Comments
 (0)