Skip to content

Commit f5f8d9d

Browse files
committed
feat(utils): replace edit with b if buffer exists, closes #144
1 parent c4da4a4 commit f5f8d9d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lua/neo-tree/utils.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ M.debounce = function(id, fn, frequency_in_ms, strategy)
9898
end
9999
end
100100

101+
M.find_buffer_by_name = function(name)
102+
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
103+
local buf_name = vim.api.nvim_buf_get_name(buf)
104+
if buf_name == name then
105+
return buf
106+
end
107+
end
108+
return -1
109+
end
110+
101111
---Gets diagnostic severity counts for all files
102112
---@return table table { file_path = { Error = int, Warning = int, Information = int, Hint = int, Unknown = int } }
103113
M.get_diagnostic_counts = function()
@@ -207,6 +217,14 @@ end
207217
---@param open_cmd string The vimcommand to use to open the file
208218
M.open_file = function(state, path, open_cmd)
209219
open_cmd = open_cmd or "edit"
220+
if open_cmd == "edit" then
221+
-- If the file is already open, switch to it.
222+
local bufnr = M.find_buffer_by_name(path)
223+
if bufnr > 0 then
224+
open_cmd = "b"
225+
end
226+
end
227+
210228
if M.truthy(path) then
211229
local events = require("neo-tree.events")
212230
local event_result = events.fire_event(events.FILE_OPEN_REQUESTED, {

0 commit comments

Comments
 (0)