fix(open-file): focus file if already opened
This commit is contained in:
parent
89becc7604
commit
4900d66370
@ -247,13 +247,15 @@ function M.fn(mode, filename)
|
||||
local win_ids = api.nvim_tabpage_list_wins(tabpage)
|
||||
local buf_loaded = is_already_loaded(filename)
|
||||
|
||||
local found = utils.is_in_displayed_buffer(filename)
|
||||
if found and mode == "preview" then
|
||||
local found_win = utils.get_win_buf_from_path(filename)
|
||||
if found_win and mode == "preview" then
|
||||
return
|
||||
end
|
||||
|
||||
if not found then
|
||||
if not found_win then
|
||||
open_in_new_window(filename, mode, win_ids)
|
||||
else
|
||||
api.nvim_set_current_win(found_win)
|
||||
end
|
||||
|
||||
if M.resize_window then
|
||||
|
||||
@ -54,7 +54,7 @@ local function get(where, node)
|
||||
end
|
||||
|
||||
local function open_or_focus(node)
|
||||
if node and not node.nodes and not utils.is_in_displayed_buffer(node.absolute_path) then
|
||||
if node and not node.nodes and not utils.get_win_buf_from_path(node.absolute_path) then
|
||||
open_file.fn("edit", node.absolute_path)
|
||||
elseif node then
|
||||
utils.focus_file(node.absolute_path)
|
||||
|
||||
@ -335,14 +335,14 @@ function M.focus_file(path)
|
||||
require("nvim-tree.view").set_cursor { i + 1, 1 }
|
||||
end
|
||||
|
||||
function M.is_in_displayed_buffer(path)
|
||||
function M.get_win_buf_from_path(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
|
||||
return w, b
|
||||
end
|
||||
end
|
||||
return false
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Loading…
Reference in New Issue
Block a user