Skip to content

feat(mapping): deprecate user mappings and add on_attach #1424

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 1 commit into from
Jul 26, 2022

Conversation

kyazdani42
Copy link
Member

@kyazdani42 kyazdani42 commented Jul 14, 2022

See the help doc. I think this is better than providing our custom binding methods
The idea was taken from gitsigns.nvim.
We have a new keymap module that uses the API directly. I've taken the same map from the actions/init.lua file and migrated the action keys to callback keys. We will need to update the script for the default mappings too once this lands.

@kyazdani42 kyazdani42 requested a review from alex-courtis July 14, 2022 08:48
@alex-courtis
Copy link
Member

alex-courtis commented Jul 16, 2022

To confirm: we are completely removing actions e.g. edit_in_place and the user's mappings will call the API directly.

This is the right way forward. Users invoking nvim_buf_set_keymap directly is obvious and clear and will be more future proof.
The mapping / action code is messy and can be difficult / unintuitive for the user. This makes the problem Go Away.

Its missing a way for users to reuse the existing actions.

This shouldn't be a problem if they are mapping directly to the API.

We will need to provide all the default mappings in the help, so that users can copy-paste to create their own e.g.

vim.api.nvim_buf_set_keymap(bufnr, 'n', 'hs', '<cmd>lua require"gitsigns".stage_hunk()<CR>', {})
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'Y', '<cmd>lua require"nvim-tree.actions.fs.copy-paste".copy_path()<CR>', {})

Users could not use regular vimscript keymaps, however that is acceptable.

users can easily migrate to this

We will need to create a page to explain the migration process with examples.

We will unfortunately need to provide a legacy migration path, pointing them to the migration doc. Users will be very upset if we don't do this.

Perhaps we could provide a function to convert their existing mappings to nvim_buf_set_keymaps...

We could start thinking on how to handle a public API for the plugin.

We can do that at any time before this. It's on my list ;)

@kyazdani42

This comment was marked as resolved.

@kyazdani42

This comment was marked as resolved.

@alex-courtis

This comment was marked as resolved.

@kyazdani42 kyazdani42 mentioned this pull request Jul 17, 2022
2 tasks
@kyazdani42 kyazdani42 force-pushed the feat/on-attach branch 2 times, most recently from f68cae2 to f573e97 Compare July 25, 2022 12:59
@kyazdani42 kyazdani42 requested a review from alex-courtis July 25, 2022 12:59
@kyazdani42 kyazdani42 force-pushed the feat/on-attach branch 3 times, most recently from 0ab0eb9 to aab36b6 Compare July 25, 2022 13:12
@kyazdani42 kyazdani42 marked this pull request as ready for review July 25, 2022 13:12
@kyazdani42
Copy link
Member Author

@alex-courtis i've migrated the keymap code to a new module. We will do a 2 step deprecation, 1st switching the default to the on_attach (in the next week or two) with a warning message, and in a month or two we'll remove the whole code and options related to this.
For now we can leave it like this, so users that wish to try this can, and we make sure the UX is all right, then we'll do the migration.
We could also write a script that will generate the new configuration for the user. Might not be too complicated.
We might need to take time and notice maintainers of things like lunarvim because they have internal stuff to change and it might take some time for them to implement.

@alex-courtis
Copy link
Member

alex-courtis commented Jul 26, 2022

on_attach (in the next week or two) with a warning message, and in a month or two we'll remove the whole code and options related to this.

Let's wait until we have completed everything and are ready to cut over to the new keymap. You know how upset users get when we show warnings ;)

There may be an unforseen issue that requires us to make an api or on_attach change, which is OK for early adopters but not for everyone.

We might need to take time and notice maintainers of things like lunarvim because they have internal stuff to change and it might take some time for them to implement.

Yes. We can give them some time; there is no rush to remove the legacy mappings.

@alex-courtis
Copy link
Member

alex-courtis commented Jul 26, 2022

Pushed a draft mechanism to write current mappings -> on_attach

Feel free to revert / change etc.

Run :NvimTreeCreateOnAttach

We could probably just simplify the helper to

  local function map(key, fn)
    vim.keymap.set("n", key, function(...)
      fn(...)
    end, { buffer = bufnr, noremap = true })
  end

however I'm not sure what else you have in mind with the mappings...

@alex-courtis
Copy link
Member

Not sure how we are going to do the DEFAULT_KEYMAPS for update-help.sh

Might have to do something horrible like:

  {
    key = { "<CR>", "o", "<2-LeftMouse>" },
    callback = { "node", "open", "edit", },
    desc = "open a file or folder; root will cd to the above directory",
  },

...

function M.set_keymaps(bufnr)
  local opts = { noremap = true, silent = true, nowait = true, buffer = bufnr }
  for _, km in ipairs(M.keymaps) do
    local keys = type(km.key) == "table" and km.key or { km.key }
    for _, key in ipairs(keys) do
      local fn = Api
      for _, t in ipairs(km.callback) do
        fn = fn[t]
      end
      vim.keymap.set("n", key, fn, opts)
    end
  end
end

...

@kyazdani42
Copy link
Member Author

i'm moving your commit in a separate PR so we can discuss it more. I think we can probably have a better strategy for migrating user configs under the hood too.
I think the default keymap is not too problematic, we could simply stringify the lua code for the callback.
I'm merging this since it's non breaking. let's iterate :)

See the help doc. I think this is better than providing our custom
binding methods, idea taken from gitsigns.nvim.
Once this is complete, we could remove the whole mapping code and
simplify the actions -> dispatch configurations.
The new keymap.lua module is now used whenever on_attach is defined.
@kyazdani42 kyazdani42 merged commit 64cc3c1 into master Jul 26, 2022
@kyazdani42 kyazdani42 deleted the feat/on-attach branch July 26, 2022 09:09
@alex-courtis
Copy link
Member

i'm moving your commit in a separate PR so we can discuss it more. I think we can probably have a better strategy for migrating user configs under the hood too. I think the default keymap is not too problematic, we could simply stringify the lua code for the callback. I'm merging this since it's non breaking. let's iterate :)

This was the right desicion. Early feedback is useful. #1461

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.

2 participants