add LuaTreeFindFile, doc and option

This commit is contained in:
kyazdani42 2020-02-28 17:24:08 +01:00
parent b9398b285d
commit daff837b23
5 changed files with 83 additions and 8 deletions

View File

@ -18,9 +18,12 @@ Plug 'kyazdani42/nvim-tree.lua'
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, not working on mac atm 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 <C-n> :LuaTreeToggle<CR> nnoremap <C-n> :LuaTreeToggle<CR>
nnoremap <leader>n :LuaTreeRefresh<CR> nnoremap <leader>r :LuaTreeRefresh<CR>
nnoremap <leader>n :LuaTreeFindFile<CR>
``` ```
## KeyBindings ## KeyBindings
@ -52,18 +55,22 @@ nnoremap <leader>n :LuaTreeRefresh<CR>
![alt text](.github/screenshot.png?raw=true "file explorer") ![alt text](.github/screenshot.png?raw=true "file explorer")
## TODO 1 ## TODO
### Perf / Fixes
- Tree creation should be async - 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 - 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 - 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 `<C-t>` to open buffer in new tab
### Window Feature
- 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
> this might be a little hard to implement since window layout events do not exist yet > this might be a little hard to implement since window layout events do not exist yet

View File

@ -26,6 +26,14 @@ open or close the tree
|:LuaTreeRefresh| *:LuaTreeRefresh* |:LuaTreeRefresh| *:LuaTreeRefresh*
refresh the tree 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* 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' ] 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* INFORMATIONS *nvim-tree-info*

View File

@ -119,6 +119,38 @@ local function refresh_tree()
end end
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 function open_dir(tree_index)
local node = Tree[tree_index]; local node = Tree[tree_index];
node.open = not node.open node.open = not node.open
@ -152,4 +184,5 @@ return {
open_dir = open_dir; open_dir = open_dir;
set_root_path = set_root_path; set_root_path = set_root_path;
get_cwd = get_cwd; get_cwd = get_cwd;
find_file = find_file;
} }

View File

@ -16,6 +16,7 @@ local init_tree = state.init_tree
local open_dir = state.open_dir local open_dir = state.open_dir
local refresh_tree = state.refresh_tree local refresh_tree = state.refresh_tree
local set_root_path = state.set_root_path local set_root_path = state.set_root_path
local find_file = state.find_file
local winutils = require 'lib/winutils' local winutils = require 'lib/winutils'
local update_view = winutils.update_view local update_view = winutils.update_view
@ -24,6 +25,7 @@ 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 replace_tree = winutils.replace_tree
local get_win = winutils.get_win
local git = require 'lib/git' local git = require 'lib/git'
local refresh_git = git.refresh_git local refresh_git = git.refresh_git
@ -147,6 +149,19 @@ local function check_buffer_and_open()
end end
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 { return {
toggle = toggle; toggle = toggle;
open_file = open_file; open_file = open_file;
@ -155,5 +170,6 @@ return {
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; replace_tree = replace_tree;
find = find;
} }

View File

@ -12,6 +12,10 @@ 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()
if get(g:, 'lua_tree_follow') != 0
au BufEnter * :LuaTreeFindFile
endif
" TODO: WinEnter is not the right autocommand for this task, " TODO: WinEnter is not the right autocommand for this task,
" but we do not have LayoutChange or WinMove kind of option atm, " but we do not have LayoutChange or WinMove kind of option atm,
" so this is deactivated by default to avoid messing up users workflows " 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! LuaTreeToggle lua require'tree'.toggle()
command! LuaTreeRefresh lua require'tree'.refresh() command! LuaTreeRefresh lua require'tree'.refresh()
command! LuaTreeFindFile lua require'tree'.find()
let &cpo = s:save_cpo let &cpo = s:save_cpo
unlet s:save_cpo unlet s:save_cpo