refacto: export window options in config

This commit is contained in:
kyazdani42 2020-06-08 11:09:53 +02:00
parent 829538dafc
commit 1e67a7c6f4
2 changed files with 23 additions and 22 deletions

View File

@ -57,4 +57,21 @@ function M.get_bindings()
} }
end end
function M.window_options()
local opts = {}
if vim.g.lua_tree_side == 'right' then
opts.side = 'L'
opts.open_command = 'h'
opts.preview_command = 'l'
opts.split_command = 'nosplitright'
else
opts.side = 'H'
opts.open_command = 'l'
opts.preview_command = 'h'
opts.split_command = 'splitright'
end
return opts
end
return M return M

View File

@ -8,6 +8,8 @@ local pops = require'lib.populate'
local populate = pops.populate local populate = pops.populate
local refresh_entries = pops.refresh_entries local refresh_entries = pops.refresh_entries
local window_opts = config.window_options()
local M = {} local M = {}
M.Tree = { M.Tree = {
@ -16,7 +18,6 @@ M.Tree = {
cwd = nil, cwd = nil,
win_width = vim.g.lua_tree_width or 30, win_width = vim.g.lua_tree_width or 30,
loaded = false, loaded = false,
side = 'H',
bufnr = nil, bufnr = nil,
winnr = nil, winnr = nil,
buf_options = { buf_options = {
@ -36,10 +37,6 @@ M.Tree = {
} }
} }
if vim.g.lua_tree_side == 'right' then
M.Tree.side = 'L'
end
function M.init(with_open, with_render) function M.init(with_open, with_render)
M.Tree.cwd = luv.cwd() M.Tree.cwd = luv.cwd()
populate(M.Tree.entries, M.Tree.cwd, M.Tree) populate(M.Tree.entries, M.Tree.cwd, M.Tree)
@ -178,18 +175,10 @@ function M.set_index_and_redraw(fname)
end end
function M.open_file(mode, filename) function M.open_file(mode, filename)
if vim.g.lua_tree_side == 'right' then api.nvim_command('noautocmd wincmd '..window_opts.open_command)
api.nvim_command('noautocmd wincmd h')
else
api.nvim_command('noautocmd wincmd l')
end
if mode == 'preview' then if mode == 'preview' then
api.nvim_command(string.format("edit %s", filename)) api.nvim_command(string.format("edit %s", filename))
if vim.g.lua_tree_side == 'right' then api.nvim_command('noautocmd wincmd '..window_opts.preview_command)
api.nvim_command('noautocmd wincmd l')
else
api.nvim_command('noautocmd wincmd h')
end
else else
api.nvim_command(string.format("%s %s", mode, filename)) api.nvim_command(string.format("%s %s", mode, filename))
end end
@ -246,18 +235,13 @@ local function create_buf()
api.nvim_command('setlocal '..opt) api.nvim_command('setlocal '..opt)
end end
if M.Tree.side == 'L' then api.nvim_command('setlocal '..window_opts.split_command)
api.nvim_command('setlocal nosplitright')
else
api.nvim_command('setlocal splitright')
end
set_mappings() set_mappings()
end end
local function create_win() local function create_win()
api.nvim_command("vsplit") api.nvim_command("vsplit")
api.nvim_command("wincmd "..M.Tree.side) api.nvim_command("wincmd "..window_opts.side)
api.nvim_command("vertical resize "..M.Tree.win_width) api.nvim_command("vertical resize "..M.Tree.win_width)
M.Tree.winnr = api.nvim_get_current_win() M.Tree.winnr = api.nvim_get_current_win()