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

@ -17,7 +17,7 @@ Plug 'kyazdani42/nvim-tree.lua'
```vim ```vim
let g:lua_tree_side = 'right' | 'left' "left by default let g:lua_tree_side = 'right' | 'left' "left by default
let g:lua_tree_size = 40 "30 by default let g:lua_tree_size = 40 "30 by default
let g:lua_tree_ignore = [ '.git', 'node_modules', '.cache' ] "empty by default let g:lua_tree_ignore = [ '.git', 'node_modules', '.cache' ] "empty by default, not working on mac atm
nnoremap <C-n> :LuaTreeToggle<CR> nnoremap <C-n> :LuaTreeToggle<CR>
nnoremap <leader>n :LuaTreeRefresh<CR> nnoremap <leader>n :LuaTreeRefresh<CR>
@ -55,14 +55,15 @@ nnoremap <leader>n :LuaTreeRefresh<CR>
## TODO ## TODO
- Tree creation should be async - Tree creation should be async
- better error checking when fs updates - better error checking when fs updates
- open tree when running vim on a folder
- sneak like cd command to find a directory - sneak like cd command to find a directory
- better default colors (use default vim groups) - better default colors (use default vim groups)
- give user option to choose for file generation command
- command to find current file in the directory structure - command to find current file in the directory structure
- create proper highlight groups or add highlight function to give the user ability to setup colors themselves - create proper highlight groups or add highlight function to give the user ability to setup colors themselves
- bufferize leafs of node being closed so when opening again the node, we open every directory that was previously open - bufferize leafs of node being closed so when opening again the node, we open every directory that was previously open
- use libuv functions instead of `touch` and `mkdir` in `create_file()` and allow file creation with path like `foo/bar/baz` - use libuv functions instead of `touch` and `mkdir` in `create_file()` and allow file creation with path like `foo/bar/baz`
- better window management: - better window management:
- check tree buffer/window for change so we can avoid it being resized or moved around or replaced by another file - check tree buffer/window for change so we can avoid it being resized or moved around or replaced by another file
- monitor window layout in current tab to open files in the right place - monitor window layout in current tab to open files in the right place
- add `<C-t>` to open buffer in new tab - add `<C-t>` to open buffer in new tab
> this might be a little hard to implement since window layout events do not exist yet

View File

@ -40,7 +40,7 @@ local BUF_OPTIONS = {
} }
local WIN_WIDTH = 30 local WIN_WIDTH = 30
local SIDE = 'topleft' local SIDE = 'H'
if api.nvim_call_function('exists', { 'g:lua_tree_width' }) == 1 then if api.nvim_call_function('exists', { 'g:lua_tree_width' }) == 1 then
WIN_WIDTH = api.nvim_get_var('lua_tree_width') 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_call_function('exists', { 'g:lua_tree_side' }) == 1 then
if api.nvim_get_var('lua_tree_side') == 'right' then if api.nvim_get_var('lua_tree_side') == 'right' then
SIDE = 'rightbelow' SIDE = 'L'
end end
end end
@ -66,13 +66,33 @@ local function open()
api.nvim_buf_set_option(buf, opt, val) api.nvim_buf_set_option(buf, opt, val)
end 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) api.nvim_win_set_buf(0, buf)
for _, opt in pairs(BUF_OPTIONS) do for _, opt in pairs(BUF_OPTIONS) do
api.nvim_command('setlocal '..opt) api.nvim_command('setlocal '..opt)
end end
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 function close()
local win = get_win() local win = get_win()
if not win then return end if not win then return end
@ -97,11 +117,6 @@ local function update_view(update_cursor)
end end
end end
local function is_win_open()
return get_buf() ~= nil
end
local function set_mappings() local function set_mappings()
local buf = get_buf() local buf = get_buf()
if not buf then return end if not buf then return end
@ -125,6 +140,10 @@ local function set_mappings()
end end
end end
local function is_win_open()
return get_buf() ~= nil
end
return { return {
open = open; open = open;
close = close; close = close;
@ -133,4 +152,5 @@ return {
get_buf = get_buf; get_buf = get_buf;
get_win = get_win; get_win = get_win;
set_mappings = set_mappings; 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 close = winutils.close
local open = winutils.open local open = winutils.open
local set_mappings = winutils.set_mappings local set_mappings = winutils.set_mappings
local replace_tree = winutils.replace_tree
local git = require 'lib/git' local git = require 'lib/git'
local refresh_git = git.refresh_git local refresh_git = git.refresh_git
@ -50,11 +51,9 @@ local function open_file(open_type)
if node.name == '..' then if node.name == '..' then
api.nvim_command('cd ..') api.nvim_command('cd ..')
local new_path local new_path = get_cwd()
if get_cwd() == '/' then if new_path ~= '/' then
new_path = '/' new_path = new_path .. '/'
else
new_path = get_cwd() .. '/'
end end
set_root_path(new_path) set_root_path(new_path)
@ -132,7 +131,20 @@ end
local function check_buffer_and_open() local function check_buffer_and_open()
local bufname = api.nvim_buf_get_name(0) 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 end
return { return {
@ -142,5 +154,6 @@ return {
refresh = refresh; refresh = refresh;
check_windows_and_close = check_windows_and_close; check_windows_and_close = check_windows_and_close;
check_buffer_and_open = check_buffer_and_open; check_buffer_and_open = check_buffer_and_open;
replace_tree = replace_tree;
} }

View File

@ -12,6 +12,12 @@ au BufWritePost * lua require'tree'.refresh()
au BufEnter * lua require'tree'.check_windows_and_close() au BufEnter * lua require'tree'.check_windows_and_close()
au VimEnter * lua require'tree'.check_buffer_and_open() au VimEnter * lua require'tree'.check_buffer_and_open()
" TODO: WinEnter is not the right autocommand for this task,
" but we do not have LayoutChange or WinMove kind of option atm,
" so this is deactivated by default to avoid messing up users workflows
" au WinEnter * lua require'tree'.replace_tree()
command! LuaTreeToggle lua require'tree'.toggle() command! LuaTreeToggle lua require'tree'.toggle()
command! LuaTreeRefresh lua require'tree'.refresh() command! LuaTreeRefresh lua require'tree'.refresh()