Skip to content

Expose DEFAULT_KEYMAPS And Current Mappings #1858

Description

@hinell

I requet to export default keymapping to ensure stable api .e.g.:

local   keymaps = require("nvim-tree").DEFAULT_KEYMAPS
...

Can this functionality be implemented utilising API?

  • Well, it can, but the following is not guaranteed to be stable:
require("nvim-tree.keymap").keymaps

Note The callbacks should be exported explicitly!

Activity

  1. mrjones2014 commented on Dec 25, 2022

    @mrjones2014

    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:

    User's config:

    require('legendary').setup({
      plugins = {
        nvim_tree = true,
      }
    })

    File: lua/legendary/plugins/nvim_tree.lua

    vim.api.nvim_create_autocmd('FileType', {
      pattern = 'NvimTree',
      once = true,
      callback = function()
            local   nvimtreemappings = require("nvim-tree.keymap").keymaps
            local   keystodisable    = vim.tbl_map(function(e) return e.key end, nvimtreemappings)
            -- Set up Legendary plugin keybindigns
            local keymaps = vim.tbl_map(function(e)
                -- just set up a description-only keymap
                local entry = {}
                entry.mode  = "n"
                entry[1]    = e.key        -- Key
                if type(e.key) == "table" then entry[1] = e.key[1] end
                entry.description = "Nvim-tree: " .. e.desc
                entry.opts = { buffer = bufnr, noremap = true }
                return entry
            end, nvimtreemappings)
            require('legendary').keymaps(keymaps)
      end
    })
  2. hinell commented on Dec 25, 2022

    @hinell
    ContributorAuthor

    @mrjones2014 No need for a copy. Replied in the original issue/thread.

  3. mrjones2014 commented on Dec 25, 2022

    @mrjones2014

    Yeah I meant if nvim-tree would be interested in having the plugin in this repo

  4. alex-courtis commented on Dec 30, 2022

    @alex-courtis
    Member

    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)

    Left some notes about on_attach callback and future direction of mappings: 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?

  5. mrjones2014 commented on Dec 30, 2022

    @mrjones2014

    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.

  6. alex-courtis commented on Dec 30, 2022

    @alex-courtis
    Member

    That would work nicely: we have a solution for now and a solution for the future.

    Let's let the DEFAULT_KEYMAPS keymaps out via API. Create a deep copy of DEFAULT_KEYMAPS instead 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.

  7. added
    API
    and removed on Dec 30, 2022
  8. changed the title [-]Export default keymappings[/-] [+]Expose DEFAULT_KEYMAPS And Current Mappings[/+] on Dec 30, 2022
  9. mrjones2014 commented on Dec 30, 2022

    @mrjones2014

    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.

  10. alex-courtis commented on Dec 30, 2022

    @alex-courtis
    Member

    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.

  11. mrjones2014 commented on Dec 30, 2022

    @mrjones2014

    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?

  12. alex-courtis commented on Dec 30, 2022

    @alex-courtis
    Member

    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

  13. mrjones2014 commented on Dec 31, 2022

    @mrjones2014

    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 so
            require('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 API
            require('legendary').keymaps(keymaps)
      end
    })
  14. 39 remaining items

  15. mrjones2014 commented on Mar 7, 2023

    @mrjones2014

    Everything described here: #1869

    As well as a function to get the currently applied keymaps considering user config.

  16. hinell commented on Mar 7, 2023

    @hinell
    ContributorAuthor

    @alex-courtis Just give an example, like in this reply over here

  17. added a commit that references this issue on Mar 13, 2023
  18. alex-courtis commented on Mar 13, 2023

    @alex-courtis
    Member

    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...

  19. alex-courtis commented on Mar 13, 2023

    @alex-courtis
    Member

    ... 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_startup to deal with that.

  20. hinell commented on Mar 13, 2023

    @hinell
    ContributorAuthor

    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.

  21. alex-courtis commented on Mar 13, 2023

    @alex-courtis
    Member

    The best solution is just to export it as an ordinary data piece. The most cheap and quick. Complexity wouldn't be justified over here.

    That could be possible. What would you expect, in addition to nvim_buf_get_keymap ?

  22. alex-courtis commented on Mar 13, 2023

    @alex-courtis
    Member

    @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.

  23. mrjones2014 commented on Mar 14, 2023

    @mrjones2014

    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#317

    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'

  24. alex-courtis commented on Mar 20, 2023

    @alex-courtis
    Member

    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.

  25. hinell commented on Mar 24, 2023

    @hinell
    ContributorAuthor

    Sorry, been busy in a while.

    @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.

  26. added 2 commits that reference this issue on Mar 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    API

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions