From daff837b231d6f2aa20c944d65c2f3081cfd8a5d Mon Sep 17 00:00:00 2001 From: kyazdani42 Date: Fri, 28 Feb 2020 17:24:08 +0100 Subject: [PATCH] add LuaTreeFindFile, doc and option --- README.md | 23 +++++++++++++++-------- doc/nvim-tree-lua.txt | 14 ++++++++++++++ lua/lib/state.lua | 33 +++++++++++++++++++++++++++++++++ lua/tree.lua | 16 ++++++++++++++++ plugin/tree.vim | 5 +++++ 5 files changed, 83 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4af432da..24b24973 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,12 @@ Plug 'kyazdani42/nvim-tree.lua' 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, not working on mac atm +let g:lua_tree_follow = 1 "0 by default, this option will bind BufEnter to the LuaTreeFindFile command +" :help LuaTreeFindFile for more info nnoremap :LuaTreeToggle -nnoremap n :LuaTreeRefresh +nnoremap r :LuaTreeRefresh +nnoremap n :LuaTreeFindFile ``` ## KeyBindings @@ -52,18 +55,22 @@ nnoremap n :LuaTreeRefresh ![alt text](.github/screenshot.png?raw=true "file explorer") -## TODO 1 +## TODO + +### Perf / Fixes - Tree creation should be async -- 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 - refactor all `system` call to `libuv` functions, with better error management -- 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 + +### Features +- sneak like cd command to find a file/directory +- better default colors (use default vim groups) +- create proper highlight groups or add highlight function to give the user ability to setup colors themselves +- add `` to open buffer in new tab + +### Window Feature - 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/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index ee04774d..7bac7cbf 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -26,6 +26,14 @@ open or close the tree |:LuaTreeRefresh| *:LuaTreeRefresh* refresh the tree + +|:LuaTreeFindFile| *:LuaTreeFindFile* + +The command will change the cursor in the tree for the current bufname. + +It will also open the leafs of the tree leading to the file in the buffer +(if you opened a file with something else than the LuaTree, like `fzf`) + ============================================================================== OPTIONS *nvim-tree-options* @@ -45,6 +53,12 @@ Each pattern is passed into the 'ls' function as `--ignore=PATTERN` > example: let g:lua_tree_ignore = [ '.git', 'node_modules' ] < + +|g:lua_tree_follow| *g:lua_tree_follow* + +Can be `0` or `1`. When `1`, will bind |:LuaTreeFindFile| to |BufEnter| + + ============================================================================== INFORMATIONS *nvim-tree-info* diff --git a/lua/lib/state.lua b/lua/lib/state.lua index d7fe0acc..5c2e3dd7 100644 --- a/lua/lib/state.lua +++ b/lua/lib/state.lua @@ -119,6 +119,38 @@ local function refresh_tree() end end +local function clone(obj) + if type(obj) ~= 'table' then return obj end + local res = {} + for k, v in pairs(obj) do res[clone(k)] = clone(v) end + return res +end + +local function find_file(path) + local relpath = string.sub(path, #ROOT_PATH + 1, -1) + + local tree_copy = clone(Tree) + + for i, node in pairs(tree_copy) do + if node.relpath and string.match(relpath, node.relpath) then + if node.relpath == relpath then + Tree = clone(tree_copy) + return i + end + if node.dir and not node.open then + local path = node.path .. node.name + node.open = true + local dirs = list_dirs(path) + for j, n in pairs(create_nodes(path, node.relpath, node.depth + 1, dirs)) do + table.insert(tree_copy, i + j, n) + end + end + end + end + + return nil +end + local function open_dir(tree_index) local node = Tree[tree_index]; node.open = not node.open @@ -152,4 +184,5 @@ return { open_dir = open_dir; set_root_path = set_root_path; get_cwd = get_cwd; + find_file = find_file; } diff --git a/lua/tree.lua b/lua/tree.lua index 85376aab..49664eaf 100644 --- a/lua/tree.lua +++ b/lua/tree.lua @@ -16,6 +16,7 @@ local init_tree = state.init_tree local open_dir = state.open_dir local refresh_tree = state.refresh_tree local set_root_path = state.set_root_path +local find_file = state.find_file local winutils = require 'lib/winutils' local update_view = winutils.update_view @@ -24,6 +25,7 @@ local close = winutils.close local open = winutils.open local set_mappings = winutils.set_mappings local replace_tree = winutils.replace_tree +local get_win = winutils.get_win local git = require 'lib/git' local refresh_git = git.refresh_git @@ -147,6 +149,19 @@ local function check_buffer_and_open() end end +local function find() + local line = find_file(api.nvim_buf_get_name(0)) + if not line then return end + + update_view() + + local win = get_win() + if win then + api.nvim_win_set_cursor(win, { line, 0 }) + end + +end + return { toggle = toggle; open_file = open_file; @@ -155,5 +170,6 @@ return { check_windows_and_close = check_windows_and_close; check_buffer_and_open = check_buffer_and_open; replace_tree = replace_tree; + find = find; } diff --git a/plugin/tree.vim b/plugin/tree.vim index 103a43ca..e1bfb57d 100644 --- a/plugin/tree.vim +++ b/plugin/tree.vim @@ -12,6 +12,10 @@ au BufWritePost * lua require'tree'.refresh() au BufEnter * lua require'tree'.check_windows_and_close() au VimEnter * lua require'tree'.check_buffer_and_open() +if get(g:, 'lua_tree_follow') != 0 + au BufEnter * :LuaTreeFindFile +endif + " 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 @@ -20,6 +24,7 @@ au VimEnter * lua require'tree'.check_buffer_and_open() command! LuaTreeToggle lua require'tree'.toggle() command! LuaTreeRefresh lua require'tree'.refresh() +command! LuaTreeFindFile lua require'tree'.find() let &cpo = s:save_cpo unlet s:save_cpo