Skip to content

Commit 5b6205c

Browse files
committed
feat: closes #85, add file_open_requested event to override built-in logic
1 parent 3421936 commit 5b6205c

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

doc/neo-tree.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,18 @@ Fired after a file (or folder) has been deleted. The arg is the full path to the
431431
deleted file.
432432

433433
"file_moved"
434-
Fired after a file (or folder) has been moved. The arg is an table containing
434+
Fired after a file (or folder) has been moved. The arg is a table containing
435435
`source` and `destination` properties.
436436

437+
"file_open_requested"
438+
Fired just before a file is opened. The arg is a table containing the `state`
439+
of the source being used, the `path` of the file to be opened, and `open_cmd`,
440+
which is the open command that was requested. `open_cmd` will be either |edit|,
441+
|split|, or |vsplit|. This function should return a table with a property called
442+
`handled` which is true if the file open operation was handled, or false if it
443+
was not. If `{ handled = true }` is not returned, the file will be opened using
444+
the built-in logic.
445+
437446
"file_opened"
438447
Fired after a file has been opened. You might use this to auto-close the window
439448
or clear the filter. The arg is the path of the file opened.

lua/neo-tree/events/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local M = {
99
FILE_ADDED = "file_added",
1010
FILE_DELETED = "file_deleted",
1111
FILE_MOVED = "file_moved",
12+
FILE_OPEN_REQUESTED = "file_open_requested",
1213
FILE_OPENED = "file_opened",
1314
FILE_RENAMED = "file_renamed",
1415
FS_EVENT = "fs_event",

lua/neo-tree/events/queue.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ local fire_event_internal = function(event, args)
128128
local id = event_handler.id or event_handler
129129
if success then
130130
log.trace("Handler ", id, " for " .. event .. " called successfully.")
131+
if
132+
type(result) == "table"
133+
and type(result.handled) == "boolean"
134+
and result.handled == true
135+
then
136+
log.trace(
137+
"Handler ",
138+
id,
139+
" for " .. event .. " returned handled = true, skipping the rest of the queue."
140+
)
141+
return result
142+
end
131143
else
132144
log.error(string.format("Error in event handler for event %s[%s]: %s", event, id, result))
133145
end
@@ -145,7 +157,7 @@ M.fire_event = function(event, args)
145157
fire_event_internal(event, args or {})
146158
end, freq)
147159
else
148-
fire_event_internal(event, args or {})
160+
return fire_event_internal(event, args or {})
149161
end
150162
end
151163

lua/neo-tree/utils.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ end
302302
M.open_file = function(state, path, open_cmd)
303303
open_cmd = open_cmd or "edit"
304304
if M.truthy(path) then
305+
local events = require("neo-tree.events")
306+
local event_result = events.fire_event(events.FILE_OPEN_REQUESTED, {
307+
state = state,
308+
path = path,
309+
open_cmd = open_cmd,
310+
}) or {}
311+
if event_result.handled then
312+
events.fire_event(events.FILE_OPENED, path)
313+
return
314+
end
305315
-- use last window if possible
306316
local suitable_window_found = false
307317
local nt = require("neo-tree")
@@ -337,7 +347,6 @@ M.open_file = function(state, path, open_cmd)
337347
else
338348
vim.cmd(open_cmd .. " " .. path)
339349
end
340-
local events = require("neo-tree.events")
341350
events.fire_event(events.FILE_OPENED, path)
342351
end
343352
end

0 commit comments

Comments
 (0)