From f6e44c1726ffe7aa170edcda2b7965c2c1253aa1 Mon Sep 17 00:00:00 2001 From: kyazdani42 Date: Fri, 28 Feb 2020 00:59:57 +0100 Subject: [PATCH] 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. --- README.md | 7 ++++--- lua/lib/winutils.lua | 36 ++++++++++++++++++++++++++++-------- lua/tree.lua | 25 +++++++++++++++++++------ plugin/tree.vim | 6 ++++++ 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d5a7364f..aae8d8f6 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Plug 'kyazdani42/nvim-tree.lua' ```vim let g:lua_tree_side = 'right' | 'left' "left 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 :LuaTreeToggle nnoremap n :LuaTreeRefresh @@ -55,14 +55,15 @@ nnoremap n :LuaTreeRefresh ## TODO - Tree creation should be async - better error checking when fs updates -- open tree when running vim on a folder - sneak like cd command to find a directory - 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 - 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 - 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 - monitor window layout in current tab to open files in the right place - add `` to open buffer in new tab +> this might be a little hard to implement since window layout events do not exist yet diff --git a/lua/lib/winutils.lua b/lua/lib/winutils.lua index 7ab9a7e9..62faf1b0 100644 --- a/lua/lib/winutils.lua +++ b/lua/lib/winutils.lua @@ -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; } diff --git a/lua/tree.lua b/lua/tree.lua index db632a56..85376aab 100644 --- a/lua/tree.lua +++ b/lua/tree.lua @@ -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; } diff --git a/plugin/tree.vim b/plugin/tree.vim index a6e176f9..103a43ca 100644 --- a/plugin/tree.vim +++ b/plugin/tree.vim @@ -12,6 +12,12 @@ au BufWritePost * lua require'tree'.refresh() au BufEnter * lua require'tree'.check_windows_and_close() 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! LuaTreeRefresh lua require'tree'.refresh()