feat(api): add api.config.mappings.default_on_attach (#2037)

This commit is contained in:
Alexander Courtis 2023-03-06 10:45:58 +11:00 committed by GitHub
parent 1b453441f4
commit bbb6d48910
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 72 deletions

View File

@ -1425,6 +1425,13 @@ api.tree.toggle_help() *nvim-tree.api.tree.toggle_help()*
- navigate.prev - navigate.prev
- navigate.select - navigate.select
*nvim-tree.api.config.mappings.default_on_attach()*
api.config.mappings.default_on_attach({bufnr})
Set all |nvim-tree-mappings-default|. Call from your |nvim-tree.on_attach|
Parameters: ~
• {bufnr} (number) nvim-tree buffer number passed to |nvim-tree.on_attach|
api.config.mappings.active() *nvim-tree.api.config.mappings.active()* api.config.mappings.active() *nvim-tree.api.config.mappings.active()*
Deprecated: only functions when using legacy |nvim-tree.view.mappings| Deprecated: only functions when using legacy |nvim-tree.view.mappings|
Retrieve a clone of the currently active mappings: defaults + user. Retrieve a clone of the currently active mappings: defaults + user.
@ -1490,14 +1497,13 @@ will be applied.
You are encouraged to copy these to your own |nvim-tree.on_attach| function. You are encouraged to copy these to your own |nvim-tree.on_attach| function.
> >
local on_attach = function(bufnr)
local api = require('nvim-tree.api') local api = require('nvim-tree.api')
-- BEGIN_DEFAULT_ON_ATTACH local function opts(desc)
local opts = function(desc)
return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true } return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
end end
-- BEGIN_DEFAULT_ON_ATTACH
vim.keymap.set('n', '<C-]>', api.tree.change_root_to_node, opts('CD')) vim.keymap.set('n', '<C-]>', api.tree.change_root_to_node, opts('CD'))
vim.keymap.set('n', '<C-e>', api.node.open.replace_tree_buffer, opts('Open: In Place')) vim.keymap.set('n', '<C-e>', api.node.open.replace_tree_buffer, opts('Open: In Place'))
vim.keymap.set('n', '<C-k>', api.node.show_info_popup, opts('Info')) vim.keymap.set('n', '<C-k>', api.node.show_info_popup, opts('Info'))
@ -1551,6 +1557,20 @@ You are encouraged to copy these to your own |nvim-tree.on_attach| function.
vim.keymap.set('n', '<2-LeftMouse>', api.node.open.edit, opts('Open')) vim.keymap.set('n', '<2-LeftMouse>', api.node.open.edit, opts('Open'))
vim.keymap.set('n', '<2-RightMouse>', api.tree.change_root_to_node, opts('CD')) vim.keymap.set('n', '<2-RightMouse>', api.tree.change_root_to_node, opts('CD'))
-- END_DEFAULT_ON_ATTACH -- END_DEFAULT_ON_ATTACH
<
Alternatively, you may apply these default mappings from your |nvim-tree.on_attach| via
|nvim-tree.api.config.mappings.default_on_attach()| e.g.
>
local function my_on_attach(bufnr)
local api = require('nvim-tree.api')
local function opts(desc)
return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
end
api.config.mappings.default_on_attach(bufnr)
-- your removals and mappings go here
end end
< <
============================================================================== ==============================================================================

View File

@ -170,6 +170,8 @@ Api.marks.navigate.next = require("nvim-tree.marks.navigation").next
Api.marks.navigate.prev = require("nvim-tree.marks.navigation").prev Api.marks.navigate.prev = require("nvim-tree.marks.navigation").prev
Api.marks.navigate.select = require("nvim-tree.marks.navigation").select Api.marks.navigate.select = require("nvim-tree.marks.navigation").select
Api.config.mappings.default_on_attach = require("nvim-tree.keymap").default_on_attach
Api.config.mappings.active = function() Api.config.mappings.active = function()
return require("nvim-tree.keymap-legacy").active_mappings_clone() return require("nvim-tree.keymap-legacy").active_mappings_clone()
end end

View File

@ -37,11 +37,10 @@ local BEGIN_ON_ATTACH = [[
-- Please see https://github.com/nvim-tree/nvim-tree.lua/wiki/Migrating-To-on_attach for assistance in migrating. -- Please see https://github.com/nvim-tree/nvim-tree.lua/wiki/Migrating-To-on_attach for assistance in migrating.
-- --
local api = require('nvim-tree.api') local function on_attach(bufnr)
local api = require('nvim-tree.api')
local on_attach = function(bufnr) local function opts(desc)
local opts = function(desc)
return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true } return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
end end
]] ]]

View File

@ -1,14 +1,14 @@
local api = require "nvim-tree.api"
local M = {} local M = {}
-- stylua: ignore start -- stylua: ignore start
function M.default_on_attach(bufnr) function M.default_on_attach(bufnr)
-- BEGIN_DEFAULT_ON_ATTACH local api = require('nvim-tree.api')
local opts = function(desc)
local function opts(desc)
return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true } return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
end end
-- BEGIN_DEFAULT_ON_ATTACH
vim.keymap.set('n', '<C-]>', api.tree.change_root_to_node, opts('CD')) vim.keymap.set('n', '<C-]>', api.tree.change_root_to_node, opts('CD'))
vim.keymap.set('n', '<C-e>', api.node.open.replace_tree_buffer, opts('Open: In Place')) vim.keymap.set('n', '<C-e>', api.node.open.replace_tree_buffer, opts('Open: In Place'))
vim.keymap.set('n', '<C-k>', api.node.show_info_popup, opts('Info')) vim.keymap.set('n', '<C-k>', api.node.show_info_popup, opts('Info'))

View File

@ -29,16 +29,13 @@ begin="BEGIN_DEFAULT_ON_ATTACH"
end="END_DEFAULT_ON_ATTACH" end="END_DEFAULT_ON_ATTACH"
# scrape DEFAULT_ON_ATTACH, indented at 2 # scrape DEFAULT_ON_ATTACH, indented at 2
sed -n -e "/${begin}/,/${end}/{ /${begin}/d; /${end}/d; p; }" lua/nvim-tree/keymap.lua > /tmp/DEFAULT_ON_ATTACH.2.lua sed -n -e "/${begin}/,/${end}/{ /${begin}/d; /${end}/d; p; }" lua/nvim-tree/keymap.lua > /tmp/DEFAULT_ON_ATTACH.lua
# indent some more # help
sed -e "s/^ / /" /tmp/DEFAULT_ON_ATTACH.2.lua > /tmp/DEFAULT_ON_ATTACH.4.lua sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_ON_ATTACH.lua
# help, indented at 4
sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_ON_ATTACH.4.lua
}; /${end}/p; d; }" doc/nvim-tree-lua.txt }; /${end}/p; d; }" doc/nvim-tree-lua.txt
# legacy keymap, indented at 2 # legacy keymap
sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_ON_ATTACH.2.lua sed -i -e "/${begin}/,/${end}/{ /${begin}/{p; r /tmp/DEFAULT_ON_ATTACH.lua
}; /${end}/p; d; }" lua/nvim-tree/keymap-legacy.lua }; /${end}/p; d; }" lua/nvim-tree/keymap-legacy.lua