From f1b04965de85712a0e120d0d64b237b6c9f50a5e Mon Sep 17 00:00:00 2001 From: Kristijan Husak Date: Wed, 22 Jul 2020 11:50:18 +0200 Subject: [PATCH 1/2] Add mappings for jumping to previous or next git item. --- README.md | 10 ++++++++++ doc/nvim-tree-lua.txt | 28 ++++++++++++++++------------ lua/lib/config.lua | 2 ++ lua/lib/lib.lua | 2 ++ lua/tree.lua | 20 ++++++++++++++++++++ 5 files changed, 50 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1a19f1357d8..989541d1e69 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,14 @@ let g:lua_tree_bindings = { \ 'toggle_ignored': 'I', \ 'preview': '', \ 'cd': '', + \ 'create': 'a', + \ 'remove': 'd', + \ 'rename': 'r', + \ 'cut': 'x', + \ 'copy': 'c', + \ 'paste': 'p', + \ 'prev_git_item': '[c', + \ 'next_git_item': ']c', } " Disable default mappings by plugin @@ -97,6 +105,8 @@ highlight LuaTreeFolderIcon guibg=blue - type `c` to add/remove file/directory to copy clipboard - type `p` to paste from clipboard. Cut clipboard has precedence over copy (will prompt for confirmation) - type `d` to delete a file (will prompt for confirmation) +- type `]c` to go to next git item +- type `[c` to go to prev git item - if the file is a directory, `` will open the directory otherwise it will open the file in the buffer near the tree - if the file is a symlink, `` will follow the symlink (if the target is a file) - `` will open the file in a vertical split diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 3f5f05a6fd0..744e4ab99b7 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -145,6 +145,8 @@ INFORMATIONS *nvim-tree-info* - type 'p' to paste from clipboard. Cut clipboard has precedence over copy (will prompt for confirmation) - type 'd' to delete a file (will prompt for confirmation) +- type ']c' to go to next git item +- type '[c' to go to prev git item - if the file is a directory, '' will open the directory - otherwise it will open the file in the buffer near the tree @@ -166,18 +168,20 @@ you can change default keybindings by defining this variable. default keybindings will be applied to undefined keys. > let g:lua_tree_bindings = { - \ edit: '', - \ edit_vsplit: '', - \ edit_split: '', - \ edit_tab: '', - \ cd: '', - \ preview: '', - \ create: 'a', - \ remove: 'd', - \ rename: 'r', - \ cut: 'x', - \ copy: 'c', - \ paste: 'p', + \ edit: '', + \ edit_vsplit: '', + \ edit_split: '', + \ edit_tab: '', + \ cd: '', + \ preview: '', + \ create: 'a', + \ remove: 'd', + \ rename: 'r', + \ cut: 'x', + \ copy: 'c', + \ paste: 'p', + \ prev_git_item: '[c', + \ next_git_item: ']c', \ } |Features| *nvim-tree-features* diff --git a/lua/lib/config.lua b/lua/lib/config.lua index 54d83e5dcde..0e8e262c5e4 100644 --- a/lua/lib/config.lua +++ b/lua/lib/config.lua @@ -58,6 +58,8 @@ function M.get_bindings() cut = keybindings.cut or 'x', copy = keybindings.copy or 'c', paste = keybindings.paste or 'p', + prev_git_item = keybindings.prev_git_item or '[c', + next_git_item = keybindings.next_git_item or ']c', } end diff --git a/lua/lib/lib.lua b/lua/lib/lib.lua index 7c981054e29..eebf02c4b5b 100644 --- a/lua/lib/lib.lua +++ b/lua/lib/lib.lua @@ -213,6 +213,8 @@ local function set_mappings() [bindings.cut] = 'on_keypress("cut")'; [bindings.copy] = 'on_keypress("copy")'; [bindings.paste] = 'on_keypress("paste")'; + [bindings.prev_git_item] = 'on_keypress("prev_git_item")'; + [bindings.next_git_item] = 'on_keypress("next_git_item")'; gx = "xdg_open()"; } diff --git a/lua/tree.lua b/lua/tree.lua index 5ca4e8aaf71..8d827f05626 100644 --- a/lua/tree.lua +++ b/lua/tree.lua @@ -1,5 +1,6 @@ local luv = vim.loop local lib = require'lib.lib' +local config = require'lib.config' local colors = require'lib.colors' local renderer = require'lib.renderer' local fs = require'lib.fs' @@ -28,6 +29,21 @@ function M.open() end end + +local function go_to_git_item(mode) + local icon_state = config.get_icon_state() + if not icon_state.show_git_icon then return end + + local icons = {} + for _, icon in pairs(icon_state.icons.git_icons) do + table.insert(icons, icon) + end + + icons = table.concat(icons, '\\|') + local flags = mode == 'prev_git_item' and 'b' or '' + return vim.fn.search(icons, flags) +end + function M.on_keypress(mode) local node = lib.get_node_at_cursor() if not node then return end @@ -55,6 +71,10 @@ function M.on_keypress(mode) return lib.toggle_ignored() end + if mode == 'next_git_item' or mode == 'prev_git_item' then + return go_to_git_item(mode) + end + if node.name == ".." then return lib.change_dir("..") elseif mode == "cd" and node.entries ~= nil then From 28c300acb488a633408039669d3d55e72abc7afc Mon Sep 17 00:00:00 2001 From: Kristijan Husak Date: Wed, 22 Jul 2020 12:29:52 +0200 Subject: [PATCH 2/2] Cache functions for jumping to prev/next git item. --- lua/tree.lua | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lua/tree.lua b/lua/tree.lua index 8d827f05626..fdd86d5dd1b 100644 --- a/lua/tree.lua +++ b/lua/tree.lua @@ -29,21 +29,18 @@ function M.open() end end - -local function go_to_git_item(mode) +local function gen_go_to(mode) local icon_state = config.get_icon_state() - if not icon_state.show_git_icon then return end - - local icons = {} - for _, icon in pairs(icon_state.icons.git_icons) do - table.insert(icons, icon) - end - - icons = table.concat(icons, '\\|') local flags = mode == 'prev_git_item' and 'b' or '' - return vim.fn.search(icons, flags) + local icons = table.concat(vim.tbl_values(icon_state.icons.git_icons), '\\|') + return function() + return icon_state.show_git_icon and vim.fn.search(icons, flags) + end end +local go_to_prev_git_item = gen_go_to('prev_git_item') +local go_to_next_git_item = gen_go_to('next_git_item') + function M.on_keypress(mode) local node = lib.get_node_at_cursor() if not node then return end @@ -71,8 +68,12 @@ function M.on_keypress(mode) return lib.toggle_ignored() end - if mode == 'next_git_item' or mode == 'prev_git_item' then - return go_to_git_item(mode) + if mode == 'prev_git_item' then + return go_to_prev_git_item() + end + + if mode == 'next_git_item' then + return go_to_next_git_item() end if node.name == ".." then