-
-
Notifications
You must be signed in to change notification settings - Fork 616
Expose DEFAULT_KEYMAPS And Current Mappings #1858
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
Comments
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)
|
@mrjones2014 No need for a copy. Replied in the original issue/thread. |
Yeah I meant if nvim-tree would be interested in having the plugin in this repo |
Left some notes about
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. Is that enough, considering the future in which |
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 So a |
That would work nicely: we have a solution for now and a solution for the future. Let's let the 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. |
I think this is all that would be needed. These would be set after the user's |
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 One other thing is actually hooking into |
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 |
I was originally thinking that we’d literally wrap the 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 so
require('legendary').keymaps(keymaps)
end
}) However I think the 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 API
require('legendary').keymaps(keymaps)
end
}) |
api.events.subscribe(Event.OnAttachPost, function(data)
-- not sure what data we could provide
require('legendary').keymaps(keymaps)
end) We would not need to concern ourselves with once etc. as this would always be run once after the user's
Alternative: user provides legendary with their |
This change breaks down into three parts:
I'll split the last one into a new issue if you're OK with the proposal. |
Yeah, that sounds good. That’s exactly what we’d need for a proper integration |
@hinell @mrjones2014 I would be grateful if you tested the new API: cd /path/to/nvim-tree.lua
git pull
git checkout 1858-add-current-default-mapping-api
|
Working on testing this today. |
This works. I left several comments about the events API, but this keymapping API works great. |
@alex-courtis Why not to export callbacks? This fails for me on The actions on nvim-tree stop working both from keyboard and from Legendary. In the latter case this might be because callbacs for keys aren't exported. I request to export them explicitly. local keys = require("nvim-tree.api").config.mappings.default()
print(vim.inspect(keys)) =>
-- Prints :
...
}
action = "edit",
desc = "open a file or folder; root will cd to the above directory",
key = { "<CR>", "o", "<2-LeftMouse>" }
}
...
|
In this case (description-only items added to |
That sounds reasonable: use case: user retrieves a default action, puts that in their mapping list with a different key and calls setup again. A non-public-API wrapper would suit rather than a full migration to public API. I will have to time-box this to 1 hour as my time is limited. #1549 is the end-state solution that needs work. Edit: this is more complex RE node injections and possiblilty of failure. To cover the use case of ammending default actions the user can simply leave |
A lightweight wrapper around action callbacks (ACBs) sounds good. Feel free to close this issue. If something is broken or there is a specific use case for direct ACBs - I will let you know.
I'm NOT using |
That is the Right Thing To Do. It's actually done here properly via API, just not yet released. Closing this issue... we will all get to our planned end state eventually. |
@alex-courtis I see that the methods to fetch the default mappings that were only added recently (bac962c) are now already deprectated again (7495975). I've also seen the new migration guide for mappings. As a new user I actually only want to override 1 single mapping so there's nothing to migrate for me. But still it seems like the new plan is that in that case you still have to copy all the default mappings into your config. This would extremely bloat my config file and feels a bit weird. Is there really no way to apply the defaults (e.g. by calling some api method in |
@alex-courtis From looking at the current code here's the solution that almost works for me: on_attach = function(bufnr)
local api = require('nvim-tree.api')
local keymap = require('nvim-tree.keymap')
keymap.default_on_attach()
vim.keymap.set('n', '+', api.tree.change_root_to_node, { desc='nvim-tree: CD', buffer=bufnr, noremap=true, silent=true, nowait=true})
end Only drawback: Help for default mappings is lost (no idea why that is). I'm also aware that So what is your suggestion for my use case? Shouldn't something like this be added to the API? |
That is not present in the API. I'll get back to you.
You would need to pass bufnr to |
Reading through earlier discussions. All the information from The actions are present in the form of the callback, which can be nvim-tree API or user defined. The callback can be compared with the API to determine what it does. That is not an answer, however I am confident we can come to a solution. |
Eager fetching of mappings, before nvim-tree is setup or opened, could be done:
I believe that telescope does something similar. |
Once again, the above is not an answer, just a possibility. What are you doing with the mappings from the deprecated API that you can't do via If it's a matter of defaults vs active we can work something out. |
@mikehaertl see |
@alex-courtis Works fine, thanks. |
@mrjones2014 @hinell following up: What is still needed to achieve the desired legendary functionality? |
Everything described here: #1869 As well as a function to get the currently applied keymaps considering user config. |
@alex-courtis Just give an example, like in this reply over here |
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... |
... 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 |
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. |
That could be possible. What would you expect, in addition to |
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
This uses the scratch buffer approach and returns all the buffer locals applied by nvim-tree. |
I left one comment on the PR with a small implementation suggestion. But this works just fine. Combined with this PR on 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 |
Sorry, been busy in a while. @alex-courtis Good job as always. Thanks. Please, make deprecation notice upon calls to 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 @mikehaertl Please document the way we implement it. |
I requet to export default keymapping to ensure stable api .e.g.:
Can this functionality be implemented utilising API?
The text was updated successfully, but these errors were encountered: