Skip to content
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
8 changes: 3 additions & 5 deletions lua/neo-tree/sources/buffers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ local follow_internal = function()
if vim.bo.filetype == "neo-tree" or vim.bo.filetype == "neo-tree-popup" then
return
end
local path_to_reveal = manager.get_path_to_reveal(true)
if not utils.truthy(path_to_reveal) then
return false
end
local bufnr = vim.api.nvim_get_current_buf()
local path_to_reveal = manager.get_path_to_reveal(true) or tostring(bufnr)

local state = get_state()
if state.current_position == "float" then
Expand Down Expand Up @@ -145,7 +143,7 @@ M.setup = function(config, global_config)
manager.subscribe(M.name, {
event = e,
handler = function(args)
if utils.is_real_file(args.afile) then
if args.afile == "" or utils.is_real_file(args.afile) then
M.buffers_changed()
end
end,
Expand Down
47 changes: 30 additions & 17 deletions lua/neo-tree/sources/buffers/lib/items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ M.get_open_buffers = function(state)
context.folders[root.path] = root
local terminals = {}

local function add_buffer(bufnr, path)
local is_loaded = vim.api.nvim_buf_is_loaded(bufnr)
if is_loaded or state.show_unloaded then
local is_listed = vim.fn.buflisted(bufnr)
if is_listed == 1 then
if path == "" then
path = "[No Name]"
end
local success, item = pcall(file_items.create_item, context, path, "file", bufnr)
if success then
item.extra = {
bufnr = bufnr,
is_listed = is_listed,
}
else
log.error("Error creating item for " .. path .. ": " .. item)
end
end
end
end

local bufs = vim.api.nvim_list_bufs()
for _, b in ipairs(bufs) do
local path = vim.api.nvim_buf_get_name(b)
Expand All @@ -45,25 +66,17 @@ M.get_open_buffers = function(state)
if utils.is_subpath(state.path, abs_path) then
table.insert(terminals, item)
end
elseif path == "" then
add_buffer(b, path)
else
local rootsub = path:sub(1, #state.path)
-- make sure this is within the root path
if rootsub == state.path then
local is_loaded = vim.api.nvim_buf_is_loaded(b)
if is_loaded or state.show_unloaded then
local is_listed = vim.fn.buflisted(b)
if is_listed == 1 then
local success, item = pcall(file_items.create_item, context, path, "file")
if success then
item.extra = {
bufnr = b,
is_listed = is_listed,
}
else
log.error("Error creating item for " .. path .. ": " .. item)
end
end
if #state.path > 1 then
local rootsub = path:sub(1, #state.path)
-- make sure this is within the root path
if rootsub == state.path then
add_buffer(b, path)
end
else
add_buffer(b, path)
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lua/neo-tree/sources/common/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,14 @@ local open_with_cmd = function(state, open_cmd, toggle_directory, open_file)
local function open()
M.revert_preview()
local path = node.path or node:get_id()
local bufnr = node.extra and node.extra.bufnr
if node.type == "terminal" then
path = node:get_id()
end
if type(open_file) == "function" then
open_file(state, path, open_cmd)
open_file(state, path, open_cmd, bufnr)
else
utils.open_file(state, path, open_cmd)
utils.open_file(state, path, open_cmd, bufnr)
end
local extra = node.extra or {}
local pos = extra.position or extra.end_position
Expand Down
20 changes: 13 additions & 7 deletions lua/neo-tree/sources/common/file-items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,26 @@ end

local create_item, set_parents

function create_item(context, path, _type)
-- avoid creating duplicate items
if context.folders[path] or context.nesting[path] then
return context.folders[path] or context.nesting[path]
end

function create_item(context, path, _type, bufnr)
local parent_path, name = utils.split_path(path)
local id = path
if path == "[No Name]" and bufnr then
parent_path = context.state.path
name = "[No Name]"
id = tostring(bufnr)
else
-- avoid creating duplicate items
if context.folders[path] or context.nesting[path] then
return context.folders[path] or context.nesting[path]
end
end

if _type == nil then
local stat = vim.loop.fs_stat(path)
_type = stat and stat.type or "unknown"
end
local item = {
id = path,
id = id,
name = name,
parent_path = parent_path,
path = path,
Expand Down
12 changes: 8 additions & 4 deletions lua/neo-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -501,25 +501,29 @@ end
---@param state table The state of the source
---@param path string The file to open
---@param open_cmd string The vimcommand to use to open the file
M.open_file = function(state, path, open_cmd)
---@param bufnr number|nil The buffer number to open
M.open_file = function(state, path, open_cmd, bufnr)
open_cmd = open_cmd or "edit"
if open_cmd == "edit" or open_cmd == "e" then
-- If the file is already open, switch to it.
local bufnr = M.find_buffer_by_name(path)
if bufnr > 0 then
bufnr = bufnr or M.find_buffer_by_name(path)
if bufnr <= 0 then
bufnr = nil
else
open_cmd = "b"
end
end

if M.truthy(path) then
local escaped_path = vim.fn.fnameescape(path)
local escaped_path = bufnr or vim.fn.fnameescape(path)
local events = require("neo-tree.events")
local result = true
local err = nil
local event_result = events.fire_event(events.FILE_OPEN_REQUESTED, {
state = state,
path = path,
open_cmd = open_cmd,
bufnr = bufnr,
}) or {}
if event_result.handled then
events.fire_event(events.FILE_OPENED, path)
Expand Down