fix(open-file): focus file if already opened

This commit is contained in:
kiyan 2022-07-16 15:38:50 +02:00
parent 89becc7604
commit 4900d66370
3 changed files with 9 additions and 7 deletions

View File

@ -247,13 +247,15 @@ function M.fn(mode, filename)
local win_ids = api.nvim_tabpage_list_wins(tabpage) local win_ids = api.nvim_tabpage_list_wins(tabpage)
local buf_loaded = is_already_loaded(filename) local buf_loaded = is_already_loaded(filename)
local found = utils.is_in_displayed_buffer(filename) local found_win = utils.get_win_buf_from_path(filename)
if found and mode == "preview" then if found_win and mode == "preview" then
return return
end end
if not found then if not found_win then
open_in_new_window(filename, mode, win_ids) open_in_new_window(filename, mode, win_ids)
else
api.nvim_set_current_win(found_win)
end end
if M.resize_window then if M.resize_window then

View File

@ -54,7 +54,7 @@ local function get(where, node)
end end
local function open_or_focus(node) 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) open_file.fn("edit", node.absolute_path)
elseif node then elseif node then
utils.focus_file(node.absolute_path) utils.focus_file(node.absolute_path)

View File

@ -335,14 +335,14 @@ function M.focus_file(path)
require("nvim-tree.view").set_cursor { i + 1, 1 } require("nvim-tree.view").set_cursor { i + 1, 1 }
end 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 for _, w in pairs(vim.api.nvim_tabpage_list_wins(0)) do
local b = vim.api.nvim_win_get_buf(w) local b = vim.api.nvim_win_get_buf(w)
if vim.api.nvim_buf_get_name(b) == path then if vim.api.nvim_buf_get_name(b) == path then
return true return w, b
end end
end end
return false return nil, nil
end end
return M return M