Skip to content

Feat - Add open and close tree events #1151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Apr 18, 2022

Conversation

HPRIOR
Copy link
Contributor

@HPRIOR HPRIOR commented Apr 10, 2022

Add 'open' and 'close' callbacks or 'hooks' which can be set during setup and which fire when toggling the sidebar.

This can be used to interact with other plugins. For example, the position of a tab bar can be changed according to the width of the file tree when it is opened or closed.

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document in help and readme.

See CONTRIBUTING.md

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be nvim-tree.events e.g. nvim-tree.events.on_nvim_tree_open

@HPRIOR HPRIOR marked this pull request as draft April 11, 2022 07:21
@HPRIOR
Copy link
Contributor Author

HPRIOR commented Apr 11, 2022

Hey @alex-courtis, I'll implement the suggested changes.

With regard to your previous comment an additional section in the config would be a good idea if 'hooks' are to be added elsewhere. Something like:

hooks = {
    tree_open = nil,
    tree_close = nil,
},

Would it be possible to explain how the event system works?

Copy link
Member

@kyazdani42 kyazdani42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to setup the hooks yourself, the user will call the the event registration himself, no need to set it during setup :)

@HPRIOR
Copy link
Contributor Author

HPRIOR commented Apr 12, 2022

you don't need to setup the hooks yourself, the user will call the the event registration himself, no need to set it during setup :)

Ok got it. Can I still add the ability to register and to fire open and close events?

@kyazdani42
Copy link
Member

i'm not even sure you placed the event at the right location to be honest 😄 i think i would place them in view.lua. There are multiple ways the tree can be opened, it won't just work by setting it in toggle :)

@HPRIOR
Copy link
Contributor Author

HPRIOR commented Apr 12, 2022

@kyazdani42 thanks for your feedback. I have moved the event dispatches to view.lua. Let me know if there is anything else I can do. If the code is not to your liking otherwise, I would really like to see this feature at some point!

@HPRIOR HPRIOR marked this pull request as ready for review April 12, 2022 22:00
@HPRIOR HPRIOR requested a review from alex-courtis April 12, 2022 22:00
@HPRIOR HPRIOR changed the title Feat - add hooks to config Feat - Add open and close tree events Apr 12, 2022
@alex-courtis
Copy link
Member

With regard to your previous comment an additional section in the config would be a good idea

That was a mistake on my part... before I realised that we should use events ;)

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to document this in the help: nvim-tree.events

@HPRIOR
Copy link
Contributor Author

HPRIOR commented Apr 14, 2022

We need to document this in the help: nvim-tree.events

I've added the documentation

@HPRIOR HPRIOR requested a review from kyazdani42 April 15, 2022 10:27
@kyazdani42 kyazdani42 merged commit cff5a10 into nvim-tree:master Apr 18, 2022
@HPRIOR HPRIOR deleted the feat-add-hooks-to-config branch April 18, 2022 16:00
@gegoune
Copy link
Collaborator

gegoune commented Apr 18, 2022

Another useful event would be on_tree_leave (or similar) for when window is not closed but no longer active.

@kyazdani42
Copy link
Member

what do you mean no longer active ? when the tree buffer is replaced by a file buffer ?

@gegoune
Copy link
Collaborator

gegoune commented Apr 19, 2022

Apologies for not being clear. I mean event when cursor leaves nvim-tree window. Wither because file was opened or by any window movement, such as <C-w><C-w>.

@kyazdani42
Copy link
Member

well...

au WinLeave NvimTree_* ...

?

@gegoune
Copy link
Collaborator

gegoune commented Apr 20, 2022

Absolutely, that's what I have at the moment, and with on open event I can replace one of autocmds, but only one. Leaving tree remains as autocmd rather than event, which feels bit odd.

@kyazdani42
Copy link
Member

Well i'm not sure how it would be different inside the plugin ^^'

@gegoune
Copy link
Collaborator

gegoune commented Apr 21, 2022

That's absolutely alright, just thought that it could make events more complete. I am fine sticking to auto commands.

@kyazdani42
Copy link
Member

😄 now you can define autocmd in lua with callbacks so i think it doesn't matter too much :)

@gegoune
Copy link
Collaborator

gegoune commented Apr 21, 2022

And this is exactly what I have. Thanks though and apologies for the noise. :)

Almo7aya pushed a commit to Almo7aya/nvim-tree.lua that referenced this pull request Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants