fix: always open new window and set window options locally
- use setlocal for options instead of using nvim_win_set_option - create a new window when there is only the tree window - resize the tree when opening new files
This commit is contained in:
@@ -26,19 +26,17 @@ M.Tree = {
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
buf_options = {
|
options = {
|
||||||
'noswapfile',
|
'noswapfile',
|
||||||
},
|
'norelativenumber',
|
||||||
win_options = {
|
'nonumber',
|
||||||
relativenumber = false,
|
'nolist',
|
||||||
number = false,
|
'winfixwidth',
|
||||||
list = false,
|
'winfixheight',
|
||||||
winfixwidth = true,
|
'nofoldenable',
|
||||||
winfixheight = true,
|
'nospell',
|
||||||
foldenable = false,
|
'foldmethod=manual',
|
||||||
spell = false,
|
'foldcolumn=0'
|
||||||
foldmethod = 'manual',
|
|
||||||
foldcolumn = '0'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,14 +177,28 @@ function M.set_index_and_redraw(fname)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function check_and_open_split()
|
||||||
|
if #api.nvim_list_wins() == 1 then
|
||||||
|
api.nvim_command("vnew")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function M.open_file(mode, filename)
|
function M.open_file(mode, filename)
|
||||||
api.nvim_command('noautocmd wincmd '..window_opts.open_command)
|
api.nvim_command('noautocmd wincmd '..window_opts.open_command)
|
||||||
if mode == 'preview' then
|
if mode == 'preview' then
|
||||||
|
check_and_open_split()
|
||||||
api.nvim_command(string.format("edit %s", filename))
|
api.nvim_command(string.format("edit %s", filename))
|
||||||
api.nvim_command('noautocmd wincmd '..window_opts.preview_command)
|
api.nvim_command('noautocmd wincmd '..window_opts.preview_command)
|
||||||
else
|
else
|
||||||
|
if mode == 'edit' then
|
||||||
|
check_and_open_split()
|
||||||
|
end
|
||||||
api.nvim_command(string.format("%s %s", mode, filename))
|
api.nvim_command(string.format("%s %s", mode, filename))
|
||||||
end
|
end
|
||||||
|
local cur_win = api.nvim_get_current_win()
|
||||||
|
M.win_focus()
|
||||||
|
api.nvim_command('vertical resize '..M.Tree.win_width)
|
||||||
|
M.win_focus(cur_win)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.change_dir(foldername)
|
function M.change_dir(foldername)
|
||||||
@@ -252,12 +264,6 @@ local function create_win()
|
|||||||
api.nvim_command("vsplit")
|
api.nvim_command("vsplit")
|
||||||
api.nvim_command("wincmd "..window_opts.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)
|
||||||
|
|
||||||
local winnr = api.nvim_get_current_win()
|
|
||||||
|
|
||||||
for opt, val in pairs(M.Tree.win_options) do
|
|
||||||
api.nvim_win_set_option(winnr, opt, val)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.close()
|
function M.close()
|
||||||
@@ -273,23 +279,24 @@ function M.open()
|
|||||||
create_win()
|
create_win()
|
||||||
api.nvim_win_set_buf(M.Tree.winnr(), M.Tree.bufnr)
|
api.nvim_win_set_buf(M.Tree.winnr(), M.Tree.bufnr)
|
||||||
|
|
||||||
for _, opt in pairs(M.Tree.buf_options) do
|
for _, opt in pairs(M.Tree.options) do
|
||||||
api.nvim_command('setlocal '..opt)
|
api.nvim_command('setlocal '..opt)
|
||||||
end
|
end
|
||||||
api.nvim_command('setlocal '..window_opts.split_command)
|
|
||||||
|
|
||||||
renderer.draw(M.Tree, not M.Tree.loaded)
|
renderer.draw(M.Tree, not M.Tree.loaded)
|
||||||
M.Tree.loaded = true
|
M.Tree.loaded = true
|
||||||
|
|
||||||
api.nvim_buf_set_option(M.Tree.bufnr, 'filetype', M.Tree.buf_name)
|
api.nvim_buf_set_option(M.Tree.bufnr, 'filetype', M.Tree.buf_name)
|
||||||
|
api.nvim_command('setlocal '..window_opts.split_command)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.win_open()
|
function M.win_open()
|
||||||
return M.Tree.winnr() ~= nil
|
return M.Tree.winnr() ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.win_focus()
|
function M.win_focus(winnr)
|
||||||
api.nvim_set_current_win(M.Tree.winnr())
|
local wnr = winnr or M.Tree.winnr()
|
||||||
|
api.nvim_set_current_win(wnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.toggle_ignored()
|
function M.toggle_ignored()
|
||||||
|
|||||||
Reference in New Issue
Block a user