Skip to content

Commit f670e51

Browse files
committed
feat: add neo_tree_buffer_enter/leave events, closes #155, #165, #211
1 parent 2fdacf7 commit f670e51

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

doc/neo-tree.txt

+33-14
Original file line numberDiff line numberDiff line change
@@ -717,29 +717,29 @@ of objects in the form:
717717

718718
The following events are available:
719719

720-
"before_render"
720+
"before_render"~
721721
Fired after items have been collected from the source but before drawing the
722722
nodes of the tree. This is the best place to gather additional data to be used
723723
by components. The argument passed is the state of the source, which is also
724724
passed to components and commands down the line.
725725

726-
"after_render"
726+
"after_render"~
727727
Fired after the tree has been rendered. The argument passed is the state of the
728728
source, which is also passed to components and commands down the line.
729729

730-
"file_added"
730+
"file_added"~
731731
Fired after a file (or folder) has been created, either by using the "add"
732732
command or by copy and paste. The arg is the full path to the new file.
733733

734-
"file_deleted"
734+
"file_deleted"~
735735
Fired after a file (or folder) has been deleted. The arg is the full path to the
736736
deleted file.
737737

738-
"file_moved"
738+
"file_moved"~
739739
Fired after a file (or folder) has been moved. The arg is a table containing
740740
`source` and `destination` properties.
741741

742-
"file_open_requested"
742+
"file_open_requested"~
743743
Fired just before a file is opened. The arg is a table containing the `state`
744744
of the source being used, the `path` of the file to be opened, and `open_cmd`,
745745
which is the open command that was requested. `open_cmd` will be either |edit|,
@@ -748,38 +748,57 @@ which is the open command that was requested. `open_cmd` will be either |edit|,
748748
was not. If `{ handled = true }` is not returned, the file will be opened using
749749
the built-in logic.
750750

751-
"file_opened"
751+
"file_opened"~
752752
Fired after a file has been opened. You might use this to auto-close the window
753753
or clear the filter. The arg is the path of the file opened.
754754

755-
"file_renamed"
755+
"file_renamed"~
756756
Fired after a file (or folder) has been renamed. The arg is an table containing
757757
`source` and `destination` properties.
758758

759+
"neo_tree_buffer_enter"~
760+
Fired after entering a neo-tree buffer. It is also right after neo-tree applies
761+
it's own settings, so it's the ideal place to apply any local settings you would
762+
like to have.
763+
764+
"neo_tree_buffer_leave"~
765+
Fired after a neo-tree buffer was exited. Technically it fires when entering a
766+
buffer that is not neo-tree, when the last buffer enter event was neo-tree.
767+
768+
"neo_tree_popup_buffer_enter"~
769+
Fired after entering a neo-tree popup buffer. This includes things such as file
770+
rename prompts and filter inputs. It runs right after neo-tree applies it's own
771+
settings, so it's the ideal place to apply any local settings you would like to
772+
have.
773+
774+
"neo_tree_popup_buffer_leave"~
775+
Fired after leaving a neo-tree popup buffer.
776+
777+
759778
NOTE: The following events are used internally and not intended for end user
760779
usage. You can use them if you want, but beware that they may be debounced, and
761780
the details of how frequently they are fired and what events are dropped will be
762781
changed without warning.
763782

764-
"vim_diagnostic_changed"
783+
"vim_diagnostic_changed"~
765784
Fired on the |DiagnosticChanged| autocmd event. The arg is a table with one
766785
property: `diagnostics_lookup`, which is a table where the keys are file names
767786
and the values are tables with diagnostic counts by severity level.
768787

769-
"vim_buffer_changed"
788+
"vim_buffer_changed"~
770789
Fired on the following autocmd events: |BufDelete|, |BufWritePost|,
771790
|BufFilePost|, |BufNew|
772791

773-
"vim_buffer_enter"
792+
"vim_buffer_enter"~
774793
Fired on the following autocmd events: |BufEnter|, |BufWinEnter|
775794

776-
"vim_dir_changed"
795+
"vim_dir_changed"~
777796
Fired on the |DirChanged| autocmd event
778797

779-
"vim_win_enter"
798+
"vim_win_enter"~
780799
Fired on the |WinEnter| autocmd event
781800

782-
"vim_colorscheme"
801+
"vim_colorscheme"~
783802
Fired on the |ColorScheme| autocmd event
784803

785804

lua/neo-tree/defaults.lua

+12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ local config = {
5050
-- print(args.source, " moved to ", args.destination)
5151
-- end
5252
-- },
53+
-- {
54+
-- event = "neo_tree_buffer_enter",
55+
-- handler = function()
56+
-- vim.cmd 'highlight! Cursor blend=100'
57+
-- end
58+
-- },
59+
-- {
60+
-- event = "neo_tree_buffer_leave",
61+
-- handler = function()
62+
-- vim.cmd 'highlight! Cursor guibg=#5f87af blend=0'
63+
-- end
64+
-- }
5365
--},
5466
default_component_configs = {
5567
indent = {

lua/neo-tree/events/init.lua

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ local M = {
1616
FS_EVENT = "fs_event",
1717
GIT_EVENT = "git_event",
1818
GIT_STATUS_CHANGED = "git_status_changed",
19+
NEO_TREE_BUFFER_ENTER = "neo_tree_buffer_enter",
20+
NEO_TREE_BUFFER_LEAVE = "neo_tree_buffer_leave",
21+
NEO_TREE_POPUP_BUFFER_ENTER = "neo_tree_popup_buffer_enter",
22+
NEO_TREE_POPUP_BUFFER_LEAVE = "neo_tree_popup_buffer_leave",
1923
VIM_BUFFER_ADDED = "vim_buffer_added",
2024
VIM_BUFFER_CHANGED = "vim_buffer_changed",
2125
VIM_BUFFER_DELETED = "vim_buffer_deleted",

lua/neo-tree/setup/init.lua

+14
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ local define_events = function()
6161
events_setup = true
6262
end
6363

64+
local last_buffer_enter_filetype = nil
6465
M.buffer_enter_event = function()
6566
-- if it is a neo-tree window, just set local options
6667
if vim.bo.filetype == "neo-tree" then
@@ -70,16 +71,29 @@ M.buffer_enter_event = function()
7071
setlocal winhighlight=Normal:NeoTreeNormal,NormalNC:NeoTreeNormalNC,CursorLine:NeoTreeCursorLine,FloatBorder:NeoTreeFloatBorder
7172
setlocal nolist nospell nonumber norelativenumber
7273
]])
74+
events.fire_event(events.NEO_TREE_BUFFER_ENTER)
75+
last_buffer_enter_filetype = vim.bo.filetype
7376
return
7477
end
7578
if vim.bo.filetype == "neo-tree-popup" then
7679
vim.cmd([[
7780
setlocal winhighlight=Normal:NeoTreeNormal,FloatBorder:NeoTreeFloatBorder
7881
setlocal nolist nospell nonumber norelativenumber
7982
]])
83+
events.fire_event(events.NEO_TREE_POPUP_BUFFER_ENTER)
84+
last_buffer_enter_filetype = vim.bo.filetype
8085
return
8186
end
8287

88+
if last_buffer_enter_filetype == "neo-tree" then
89+
events.fire_event(events.NEO_TREE_BUFFER_LEAVE)
90+
end
91+
if last_buffer_enter_filetype == "neo-tree-popup" then
92+
events.fire_event(events.NEO_TREE_POPUP_BUFFER_LEAVE)
93+
end
94+
last_buffer_enter_filetype = vim.bo.filetype
95+
96+
8397
-- there is nothing more we want to do with floating windows
8498
if utils.is_floating() then
8599
return

0 commit comments

Comments
 (0)