You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copying my comment here in case you are interested in helping me figure out how an implementation would work with an upstream plugin: mrjones2014/legendary.nvim#289 (comment)
Hey there, this is awesome, thanks for filing the issue!
This may be better suited actually for #184, nvim-tree is probably a good candidate for a first one for a proof of concept.
I'm not sure exactly what they will look like, but I have a few ideas. The best idea I have currently is along the lines of:
Legendary.nvim must be loaded before other plugins. We have a set of plugins, or plugins can be provided by the other plugin itself, at legendary.plugins.[plugin name]. This way, plugins can be looked up dynamically; that is, if they wanted to, nvim-tree could provide its own legendary.nvim integration by adding lua/legendary/plugins/nvim_tree.lua in their repo, just for an example.
The plugin is just a plugin-specific script that must handle registering its keymaps, commands, autocmds, and functions. It may set up autocmds to do so on BufEnter for example, or it may call some custom logic to determine the correct buffer to attach to.
So for this example, and nvim-tree / legendary.nvim plugin would look like:
Copying my comment here in case you are interested in helping me figure out how an implementation would work with an upstream plugin: mrjones2014/legendary.nvim#289 (comment)
Yeah I meant if nvim-tree would be interested in having the plugin in this repo
I'm not quite sure exactly what you mean but we are always open to new ideas.
Well, it can, but the following is not guaranteed to be stable:
Mappings are considered to be API and will never change, only be added to. :help nvim-tree-default-mappings is a source of truth: it is always updated with DEFAULT_MAPPINGS.
Is that enough, considering the future in which view.mapping will be deprecated in favour of an on_attach callback where you have full control, with all mappings invoking public API?
Basically what we would need on the legendary.nvim side is a way to access an up-to-date list of the keymaps (including/considering user config of nvim-tree) with descriptions.
That sounds like what require("nvim-tree.keymap").keymaps is, is that correct?
So a legendary.nvim / nvim-tree plugin integration would look like legendary.nvim wrapping the on_attach function, calling the users' first, then calling our own to peek at the keymaps the user may have set.
We can also expose the current keymaps in the same manner.
I think this is all that would be needed. These would be set after the user's on_attach, correct? So we could wrap that function to get their configured keymaps.
I think this is all that would be needed. These would be set after the user's on_attach, correct? So we could wrap that function to get their configured keymaps.
That would work nicely. You can enumerate all their mappings for the buffer.
One difficulty might be identifying which mappings are nvim-tree specific. A convention approach may be necessary via, say, a prefix in the description. That will be available for the default mappings.
Couldn’t I use require("nvim-tree.keymap").keymaps?
One other thing is actually hooking into on_attach because if the user config loads after our plugin, they’d overwrite our on_attach. Could you possibly do a doautocmd User NvimTreeAttached that we could hook into?
Couldn’t I use require("nvim-tree.keymap").keymaps?
Yes, however that is not API and may be subject to change e.g. refactor.
We will add API for future reliability.
One other thing is actually hooking into on_attach because if the user config loads after our plugin, they’d overwrite our on_attach. Could you possibly do a doautocmd User NvimTreeAttached that we could hook into?
That's really interesting... please elaborate on how that will work.
Adding an event is a possibility :help nvim-tree-event
That's really interesting... please elaborate on how that will work.
I was originally thinking that we’d literally wrap the on_attach function, which would mean if the user then called setup with their own on_attach, ours would be overwritten.
But actually I think we could just use the autocmd I described in my original comment (unless something more specific to nvim-tree is added):
vim.api.nvim_create_autocmd('FileType', {
pattern='NvimTree',
once=true,
callback=function()
-- assuming this autocmd runs after the user’s on_attach-- we can grab the keymaps here, but we’d need a stable API to do sorequire('legendary').keymaps(keymaps)
end
})
However I think the FileType autocmd would run before the user’s on_attach so I think we’d need a custom event. So, whatever code internally calls the user’s on_attach function should then do vim.cmd.doautocmd(‘User NvimTreeAttachPost’) and then on the legendary.nvim side, we’d do:
vim.api.nvim_create_autocmd('User', {
pattern='NvimTreeAttachPost',
once=true,
callback=function()
-- this event should fire immediately after the user’s on_attach function-- grab the user’s configured keymaps from a stable nvim-tree APIrequire('legendary').keymaps(keymaps)
end
})
It should allow us to make a really nice integration with very little work on the user config side. We will be able to just grab the keymaps from nvim-tree and set them in legendary.nvim to be filtered by filetype = 'NvimTree'
It should allow us to make a really nice integration with very little work on the user config side. We will be able to just grab the keymaps from nvim-tree and set them in legendary.nvim to be filtered by filetype = 'NvimTree'
Great news! I look forward to using legendary nvim-tree on release.
@alex-courtis Good job as always. Thanks. Please, make deprecation notice upon calls to default() and active() so we can migrate in the next tag:
print(vim.inspect(require("nvim-tree.api").config.mappings.default()))
-- => WARNING: this api is deprecated in favor of get_keymap_default()
I also think the way you rename the API is a worrysome. Renaming key to lhs in keymaps tables was rather a BREAKING change. Please, don't do this. Otherwise you break plugin too easily.
@mikehaertl Please document the way we implement it.
Activity
mrjones2014 commentedon Dec 25, 2022
Copying my comment here in case you are interested in helping me figure out how an implementation would work with an upstream plugin: mrjones2014/legendary.nvim#289 (comment)
hinell commentedon Dec 25, 2022
@mrjones2014 No need for a copy. Replied in the original issue/thread.
mrjones2014 commentedon Dec 25, 2022
Yeah I meant if nvim-tree would be interested in having the plugin in this repo
alex-courtis commentedon Dec 30, 2022
Left some notes about
on_attachcallback and future direction of mappings: mrjones2014/legendary.nvim#289 (comment)I'm not quite sure exactly what you mean but we are always open to new ideas.
Mappings are considered to be API and will never change, only be added to.
:help nvim-tree-default-mappingsis a source of truth: it is always updated withDEFAULT_MAPPINGS.Is that enough, considering the future in which
view.mappingwill be deprecated in favour of anon_attachcallback where you have full control, with all mappings invoking public API?mrjones2014 commentedon Dec 30, 2022
Basically what we would need on the legendary.nvim side is a way to access an up-to-date list of the keymaps (including/considering user config of nvim-tree) with descriptions.
That sounds like what
require("nvim-tree.keymap").keymapsis, is that correct?So a
legendary.nvim/nvim-treeplugin integration would look likelegendary.nvimwrapping theon_attachfunction, calling the users' first, then calling our own to peek at the keymaps the user may have set.alex-courtis commentedon Dec 30, 2022
That would work nicely: we have a solution for now and a solution for the future.
Let's let the
DEFAULT_KEYMAPSkeymaps out via API. Create a deep copy ofDEFAULT_KEYMAPSinstead of returning the actual defaults.We can also expose the current keymaps in the same manner. Not sure of a use case, but costs little.
We could expose the current mappings for modification however setting up the callbacks would be tricky. Would require a valid use case.
[-]Export default keymappings[/-][+]Expose DEFAULT_KEYMAPS And Current Mappings[/+]mrjones2014 commentedon Dec 30, 2022
I think this is all that would be needed. These would be set after the user's
on_attach, correct? So we could wrap that function to get their configured keymaps.alex-courtis commentedon Dec 30, 2022
That would work nicely. You can enumerate all their mappings for the buffer.
One difficulty might be identifying which mappings are nvim-tree specific. A convention approach may be necessary via, say, a prefix in the description. That will be available for the default mappings.
mrjones2014 commentedon Dec 30, 2022
Couldn’t I use
require("nvim-tree.keymap").keymaps?One other thing is actually hooking into
on_attachbecause if the user config loads after our plugin, they’d overwrite ouron_attach. Could you possibly do adoautocmd User NvimTreeAttachedthat we could hook into?alex-courtis commentedon Dec 30, 2022
Yes, however that is not API and may be subject to change e.g. refactor.
We will add API for future reliability.
That's really interesting... please elaborate on how that will work.
Adding an event is a possibility
:help nvim-tree-eventmrjones2014 commentedon Dec 31, 2022
I was originally thinking that we’d literally wrap the
on_attachfunction, which would mean if the user then calledsetupwith their ownon_attach, ours would be overwritten.But actually I think we could just use the autocmd I described in my original comment (unless something more specific to nvim-tree is added):
However I think the
FileTypeautocmd would run before the user’son_attachso I think we’d need a custom event. So, whatever code internally calls the user’son_attachfunction should then dovim.cmd.doautocmd(‘User NvimTreeAttachPost’)and then on the legendary.nvim side, we’d do:39 remaining items
mrjones2014 commentedon Mar 7, 2023
Everything described here: #1869
As well as a function to get the currently applied keymaps considering user config.
hinell commentedon Mar 7, 2023
@alex-courtis Just give an example, like in this reply over here
fix(#1858): fire TreeAttachedPost event following on_attach call
alex-courtis commentedon Mar 13, 2023
We can reactively enumerate the mappings via event: https://gist.github.com/alex-courtis/984e5b97cc2148778f5353305b911308
The interesting bit is the callback: it's an API function defined in the defaults / by the user OR a custom function mapped by the user.
I did just commit a fix f0a1c6a to ensure that the event is fired. You'll need to pull master.
However...
alex-courtis commentedon Mar 13, 2023
... a better solution might be to proactively retrieve the mappings (default or user) via a scratch buffer: https://gist.github.com/alex-courtis/de9f5cdda08129e3da9127817d7ffe28
The example is mapped for ease of testing.
You'll obviously need to wait until nvim-tree setup has been called, however you have
:help nvim_tree_events_startupto deal with that.hinell commentedon Mar 13, 2023
The best solution is just to export it as an ordinary piece of data. The most cheap and quick. Complexity isn't be justified over here.
alex-courtis commentedon Mar 13, 2023
That could be possible. What would you expect, in addition to
nvim_buf_get_keymap?alex-courtis commentedon Mar 13, 2023
@hinell @mrjones2014
I would be most grateful if you tested an API addition:
cd /path/to/nvim-tree.lua git pull git checkout 1858-api-on-attach-mappings:help nvim-tree.api.config.mappings.get_keymap():help nvim-tree.api.config.mappings.get_keymap_default()This uses the scratch buffer approach and returns all the buffer locals applied by nvim-tree.
mrjones2014 commentedon Mar 14, 2023
I left one comment on the PR with a small implementation suggestion. But this works just fine.
Combined with this PR on
legendary.nvim: mrjones2014/legendary.nvim#317It should allow us to make a really nice integration with very little work on the user config side. We will be able to just grab the keymaps from
nvim-treeand set them inlegendary.nvimto be filtered byfiletype = 'NvimTree'alex-courtis commentedon Mar 20, 2023
Great news! I look forward to using legendary nvim-tree on release.
hinell commentedon Mar 24, 2023
Sorry, been busy in a while.
@alex-courtis Good job as always. Thanks. Please, make deprecation notice upon calls to
default()andactive()so we can migrate in the next tag:I also think the way you rename the API is a worrysome. Renaming
keytolhsin keymaps tables was rather a BREAKING change. Please, don't do this. Otherwise you break plugin too easily.@mikehaertl Please document the way we implement it.
fix(#1858): deprecation warning for config.mappings.active and default
fix(#1858): deprecation warning for config.mappings.active and default (