Skip to content

feat(marks): add navigation (next, previous, select) #1415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1264,4 +1264,16 @@ default.
To get the list of marked paths, you can call
`require("nvim-tree.marks").get_marks()`. This will return `{node}`.

*nvim-tree.bookmarks.navigation*

Navigation for marks is not bound by default in nvim-tree because we don't
want to focus the tree view each time we wish to switch to another mark.

This requires binding bookmark navigation yourself.

-- in your lua configuration
vim.keymap.set("n", "<leader>mn", require("nvim-tree.marks.navigation").next)
vim.keymap.set("n", "<leader>mp", require("nvim-tree.marks.navigation").prev)
vim.keymap.set("n", "<leader>ms", require("nvim-tree.marks.navigation").select)

vim:tw=78:ts=4:sw=4:et:ft=help:norl:
13 changes: 1 addition & 12 deletions lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,6 @@ local function open_in_new_window(filename, mode, win_ids)
lib.set_target_win()
end

local function is_already_open(filename, win_ids)
for _, id in ipairs(win_ids) do
if filename == api.nvim_buf_get_name(api.nvim_win_get_buf(id)) then
api.nvim_set_current_win(id)
return true
end
end

return false
end

local function is_already_loaded(filename)
for _, buf_id in ipairs(api.nvim_list_bufs()) do
if api.nvim_buf_is_loaded(buf_id) and filename == api.nvim_buf_get_name(buf_id) then
Expand Down Expand Up @@ -258,7 +247,7 @@ function M.fn(mode, filename)
local win_ids = api.nvim_tabpage_list_wins(tabpage)
local buf_loaded = is_already_loaded(filename)

local found = is_already_open(filename, win_ids)
local found = utils.is_in_displayed_buffer(filename)
if found and mode == "preview" then
return
end
Expand Down
93 changes: 93 additions & 0 deletions lua/nvim-tree/marks/navigation.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
local Iterator = require "nvim-tree.iterators.node-iterator"
local core = require "nvim-tree.core"
local Marks = require "nvim-tree.marks"
local open_file = require "nvim-tree.actions.node.open-file"
local utils = require "nvim-tree.utils"
local lib = require "nvim-tree.lib"

local function get_nearest(node, where)
local first, prev, next, last = nil, nil, nil, nil
local found = false

Iterator.builder(core.get_explorer().nodes)
:recursor(function(n)
return n.open and n.nodes
end)
:applier(function(n)
if n.absolute_path == node.absolute_path then
found = true
return
end

if not Marks.get_mark(n) then
return
end

last = n
first = first or n

if found and not next then
next = n
end

if not found then
prev = n
end
end)
:iterate()

if not found then
return
end

if where == "next" then
return next or first
else
return prev or last
end
end

local function get(where, node)
if node then
return get_nearest(node, where)
end
end

local function open_or_focus(node)
if node and not node.nodes and not utils.is_in_displayed_buffer(node.absolute_path) then
open_file.fn("edit", node.absolute_path)
elseif node then
utils.focus_file(node.absolute_path)
end
end

local function navigate_to(where)
return function()
local node = lib.get_node_at_cursor()
local next = get(where, node)
open_or_focus(next)
end
end

local M = {}

M.next = navigate_to "next"
M.prev = navigate_to "prev"

function M.select()
local list = vim.tbl_map(function(n)
return n.absolute_path
end, Marks.get_marks())

vim.ui.select(list, {
prompt = "Select a file to open or a folder to focus",
}, function(choice)
if not choice or choice == "" then
return
end
local node = Marks.get_mark { absolute_path = choice }
open_or_focus(node)
end)
end

return M
10 changes: 10 additions & 0 deletions lua/nvim-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,14 @@ function M.focus_file(path)
require("nvim-tree.view").set_cursor { i + 1, 1 }
end

function M.is_in_displayed_buffer(path)
for _, w in pairs(vim.api.nvim_tabpage_list_wins(0)) do
local b = vim.api.nvim_win_get_buf(w)
if vim.api.nvim_buf_get_name(b) == path then
return true
end
end
return false
end

return M