cd to folder when doing nvim FOLDER and layout code

- when doing `nvim FOLDER` it works as expected, opening the tree
on the correct folder (updating the ROOT_PATH)
- add the code for keeping the tree window on the side no matter
what, but since the lack of window events, disable this feature.
This commit is contained in:
kyazdani42
2020-02-28 00:59:57 +01:00
parent a6f4ec6270
commit f6e44c1726
4 changed files with 57 additions and 17 deletions

View File

@@ -40,7 +40,7 @@ local BUF_OPTIONS = {
}
local WIN_WIDTH = 30
local SIDE = 'topleft'
local SIDE = 'H'
if api.nvim_call_function('exists', { 'g:lua_tree_width' }) == 1 then
WIN_WIDTH = api.nvim_get_var('lua_tree_width')
@@ -48,7 +48,7 @@ end
if api.nvim_call_function('exists', { 'g:lua_tree_side' }) == 1 then
if api.nvim_get_var('lua_tree_side') == 'right' then
SIDE = 'rightbelow'
SIDE = 'L'
end
end
@@ -66,13 +66,33 @@ local function open()
api.nvim_buf_set_option(buf, opt, val)
end
api.nvim_command(SIDE.. ' '..WIN_WIDTH..'vnew')
api.nvim_command('vnew')
api.nvim_command('wincmd '..SIDE)
api.nvim_command('vertical resize '..WIN_WIDTH)
api.nvim_win_set_buf(0, buf)
for _, opt in pairs(BUF_OPTIONS) do
api.nvim_command('setlocal '..opt)
end
end
local function replace_tree()
local win = get_win()
if not win then return end
local tree_position = api.nvim_win_get_position(win)
local win_width = api.nvim_win_get_width(win)
-- TODO: change this to check on right side with window width - win_width == tree_position[2]
if win_width == WIN_WIDTH and tree_position[2] == 0 then return end
local current_win = api.nvim_get_current_win()
api.nvim_set_current_win(win)
api.nvim_command('wincmd '..SIDE)
api.nvim_command('vertical resize '..WIN_WIDTH)
api.nvim_set_current_win(current_win)
end
local function close()
local win = get_win()
if not win then return end
@@ -97,11 +117,6 @@ local function update_view(update_cursor)
end
end
local function is_win_open()
return get_buf() ~= nil
end
local function set_mappings()
local buf = get_buf()
if not buf then return end
@@ -125,6 +140,10 @@ local function set_mappings()
end
end
local function is_win_open()
return get_buf() ~= nil
end
return {
open = open;
close = close;
@@ -133,4 +152,5 @@ return {
get_buf = get_buf;
get_win = get_win;
set_mappings = set_mappings;
replace_tree = replace_tree;
}

View File

@@ -23,6 +23,7 @@ local is_win_open = winutils.is_win_open
local close = winutils.close
local open = winutils.open
local set_mappings = winutils.set_mappings
local replace_tree = winutils.replace_tree
local git = require 'lib/git'
local refresh_git = git.refresh_git
@@ -50,11 +51,9 @@ local function open_file(open_type)
if node.name == '..' then
api.nvim_command('cd ..')
local new_path
if get_cwd() == '/' then
new_path = '/'
else
new_path = get_cwd() .. '/'
local new_path = get_cwd()
if new_path ~= '/' then
new_path = new_path .. '/'
end
set_root_path(new_path)
@@ -132,7 +131,20 @@ end
local function check_buffer_and_open()
local bufname = api.nvim_buf_get_name(0)
if bufname == '' or is_dir(bufname) then toggle() end
if bufname == '' then
toggle()
elseif is_dir(bufname) then
api.nvim_command('cd ' .. bufname)
local new_path = get_cwd()
if new_path ~= '/' then
new_path = new_path .. '/'
end
set_root_path(new_path)
init_tree()
toggle()
end
end
return {
@@ -142,5 +154,6 @@ return {
refresh = refresh;
check_windows_and_close = check_windows_and_close;
check_buffer_and_open = check_buffer_and_open;
replace_tree = replace_tree;
}