chore: allow configuring height

also fixes window management for top and bottom tree side
This commit is contained in:
kiyan 2021-10-03 14:12:21 +02:00
parent 8fe2b547ac
commit fb32f35d7f
6 changed files with 53 additions and 20 deletions

View File

@ -82,8 +82,10 @@ require'nvim-tree'.setup {
},
view = {
-- width of the window, can be either a number (columns) or a string in `%`
-- width of the window, can be either a number (columns) or a string in `%`, for left or right side placement
width = 30,
-- height of the window, can be either a number (columns) or a string in `%`, for top or bottom side placement
height = 30,
-- side of the tree, can be one of 'left' | 'right' | 'top' | 'bottom'
side = 'left',
-- if true the tree will resize itself after opening a file

View File

@ -90,6 +90,7 @@ function.
},
view = {
width = 30,
height = 30,
side = 'left',
auto_resize = false,
mappings = {
@ -204,7 +205,12 @@ Here is a list of the options available in the setup call:
- |view|: window / buffer setup
- |view.width|: width of the window, can be either a `%` string or
a number representing columns
a number representing columns. Only works with |view.side| `left` or `right`
type: `string | number`
default: `30`
- |view.height|: height of the window, can be either a `%` string or
a number representing rows. Only works with |view.side| `top` or `bottom`
type: `string | number`
default: `30`

View File

@ -270,6 +270,7 @@ end
function M.resize(size)
view.View.width = size
view.View.height = size
view.resize()
end

View File

@ -80,14 +80,23 @@ end
function M.window_options()
local opts = {}
if vim.g.nvim_tree_side == 'right' then
local side = require'nvim-tree.view'.View.side
if side == 'right' then
opts.open_command = 'h'
opts.preview_command = 'l'
opts.split_command = 'aboveleft'
else
elseif side == "left" then
opts.open_command = 'l'
opts.preview_command = 'h'
opts.split_command = 'belowright'
elseif side == "top" then
opts.open_command = 'j'
opts.preview_command = 'k'
opts.split_command = 'bot'
else
opts.open_command = 'k'
opts.preview_command = 'j'
opts.split_command = 'top'
end
return opts

View File

@ -13,7 +13,6 @@ local populate = pops.populate
local refresh_entries = pops.refresh_entries
local first_init_done = false
local window_opts = config.window_options()
local M = {}
@ -368,7 +367,9 @@ function M.open_file(mode, filename)
if not target_winid or not vim.tbl_contains(win_ids, target_winid) then
-- Target is invalid, or window does not exist in current tabpage: create
-- new window
vim.cmd(window_opts.split_command .. " vsp")
local window_opts = config.window_options()
local splitside = view.is_vertical() and "vsp" or "sp"
vim.cmd(window_opts.split_command .. " " .. splitside)
target_winid = api.nvim_get_current_win()
M.Tree.target_winid = target_winid

View File

@ -106,6 +106,7 @@ end
local DEFAULT_CONFIG = {
width = 30,
height = 30,
side = 'left',
auto_resize = false,
mappings = {
@ -141,6 +142,7 @@ function M.setup(opts)
local options = vim.tbl_deep_extend('force', DEFAULT_CONFIG, opts)
M.View.side = options.side
M.View.width = options.width
M.View.height = options.height
M.View.auto_resize = opts.auto_resize
if options.mappings.custom_only then
M.View.mappings = options.mappings.list
@ -186,7 +188,8 @@ function M._prevent_buffer_override()
end
if #vim.api.nvim_list_wins() < 2 then
vim.cmd("vsplit")
local cmd = M.is_vertical() and "vsplit" or "split"
vim.cmd(cmd)
else
vim.cmd("wincmd "..move_cmd[M.View.side])
end
@ -228,12 +231,18 @@ function M.focus(winnr, open_if_closed)
a.nvim_set_current_win(wnr)
end
local function get_width()
if type(M.View.width) == "number" then
return M.View.width
function M.is_vertical()
return M.View.side == 'left' or M.View.side == 'right'
end
local width_as_number = tonumber(M.View.width:sub(0, -2))
local percent_as_decimal = width_as_number / 100
local function get_size()
local width_or_height = M.is_vertical() and 'width' or 'height'
local size = M.View[width_or_height]
if type(size) == "number" then
return size
end
local size_as_number = tonumber(size:sub(0, -2))
local percent_as_decimal = size_as_number / 100
return math.floor(vim.o.columns * percent_as_decimal)
end
@ -242,7 +251,11 @@ function M.resize()
return
end
a.nvim_win_set_width(M.get_winnr(), get_width())
if M.is_vertical() then
a.nvim_win_set_width(M.get_winnr(), get_size())
else
a.nvim_win_set_height(M.get_winnr(), get_size())
end
end
local move_tbl = {
@ -268,7 +281,8 @@ end
function M.replace_window()
local move_to = move_tbl[M.View.side]
a.nvim_command("wincmd "..move_to)
a.nvim_command("vertical resize "..get_width())
local resize_direction = M.is_vertical() and 'vertical ' or ''
a.nvim_command(resize_direction.."resize "..get_size())
end
local function open_window()