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

@@ -58,7 +58,7 @@ require'nvim-tree'.setup {
}, },
-- hijack the cursor in the tree to put it at the start of the filename -- hijack the cursor in the tree to put it at the start of the filename
hijack_cursor = false, hijack_cursor = false,
-- updates the root directory of the tree on `DirChanged` (when your run `:cd` usually) -- updates the root directory of the tree on `DirChanged` (when your run `:cd` usually)
update_cwd = false, update_cwd = false,
-- show lsp diagnostics in the signcolumn -- show lsp diagnostics in the signcolumn
lsp_diagnostics = false, lsp_diagnostics = false,
@@ -82,8 +82,10 @@ require'nvim-tree'.setup {
}, },
view = { 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, 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 of the tree, can be one of 'left' | 'right' | 'top' | 'bottom'
side = 'left', side = 'left',
-- if true the tree will resize itself after opening a file -- if true the tree will resize itself after opening a file

View File

@@ -90,6 +90,7 @@ function.
}, },
view = { view = {
width = 30, width = 30,
height = 30,
side = 'left', side = 'left',
auto_resize = false, auto_resize = false,
mappings = { mappings = {
@@ -204,26 +205,31 @@ Here is a list of the options available in the setup call:
- |view|: window / buffer setup - |view|: window / buffer setup
- |view.width|: width of the window, can be either a `%` string or - |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` type: `string | number`
default: `30` 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.side|: side of the tree, can be one of 'left' | 'right' | 'bottom' | 'top' - |view.side|: side of the tree, can be one of 'left' | 'right' | 'bottom' | 'top'
Note that bottom/top are not working correctly yet. Note that bottom/top are not working correctly yet.
type: `string` type: `string`
default: 'left' default: 'left'
- |view.auto_resize|: auto resize the tree after opening a file - |view.auto_resize|: auto resize the tree after opening a file
type: `boolean` type: `boolean`
default: false default: false
- |view.mappings|: configuration options for keymaps - |view.mappings|: configuration options for keymaps
- |view.mappings.custom_only|: will use only the provided user mappings and not the default - |view.mappings.custom_only|: will use only the provided user mappings and not the default
otherwise, extends the default mappings with the provided user mappings otherwise, extends the default mappings with the provided user mappings
type: `boolean` type: `boolean`
default: false default: false
- |view,mappings.list|: a list of keymaps that will extend or override the default keymaps - |view,mappings.list|: a list of keymaps that will extend or override the default keymaps
type: list of `{ key: table of strings or string, mode: string (vim-mode), cb: callback function as a string }` type: list of `{ key: table of strings or string, mode: string (vim-mode), cb: callback function as a string }`
default: {} default: {}

View File

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

View File

@@ -80,14 +80,23 @@ end
function M.window_options() function M.window_options()
local opts = {} 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.open_command = 'h'
opts.preview_command = 'l' opts.preview_command = 'l'
opts.split_command = 'aboveleft' opts.split_command = 'aboveleft'
else elseif side == "left" then
opts.open_command = 'l' opts.open_command = 'l'
opts.preview_command = 'h' opts.preview_command = 'h'
opts.split_command = 'belowright' 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 end
return opts return opts

View File

@@ -13,7 +13,6 @@ local populate = pops.populate
local refresh_entries = pops.refresh_entries local refresh_entries = pops.refresh_entries
local first_init_done = false local first_init_done = false
local window_opts = config.window_options()
local M = {} 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 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 -- Target is invalid, or window does not exist in current tabpage: create
-- new window -- 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() target_winid = api.nvim_get_current_win()
M.Tree.target_winid = target_winid M.Tree.target_winid = target_winid

View File

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