Skip to content

Feat branch complete on attach #1579

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 55 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
9da4462
chore(mappings): migrate legacy mappings under the hood
kyazdani42 Jul 31, 2022
2df2710
chore(mappings): POC for help and :help on_attach keymaps
alex-courtis Aug 1, 2022
de53d64
chore(mappings): POC for help and :help on_attach keymaps
alex-courtis Aug 1, 2022
a847166
Merge branch 'master' into chore/action_cb_help_poc
alex-courtis Aug 1, 2022
2320d17
chore(mappings): add desc to all mappings, show in help, reformat help
alex-courtis Aug 2, 2022
cd6d295
chore(mappings): add desc to all mappings
alex-courtis Aug 2, 2022
5fac9a1
chore(mappings): add desc to all mappings
alex-courtis Aug 6, 2022
57e055e
Merge branch 'master' into chore/action_cb_help_poc
alex-courtis Sep 4, 2022
420c909
chore(mappings): escape help keys
alex-courtis Sep 4, 2022
b4d8ef2
Merge branch 'chore/migrate-legacy-mappings' into feat-branch-complet…
alex-courtis Sep 4, 2022
3eb5e4c
chore(mappings): migrate legacy mappings under the hood: map keymap t…
alex-courtis Sep 4, 2022
f6f439b
chore(mappings): migrate legacy mappings under the hood: remove dispatch
alex-courtis Sep 4, 2022
adb117a
Revert "chore(mappings): migrate legacy mappings under the hood: remo…
alex-courtis Sep 4, 2022
c5883a1
chore(mappings): migrate legacy mappings under the hood: pass node to…
alex-courtis Sep 4, 2022
a456480
chore(mappings): migrate legacy mappings under the hood: remove dispatch
alex-courtis Sep 4, 2022
3ff5408
chore(mappings): migrate legacy mappings under the hood: replace mapp…
alex-courtis Sep 4, 2022
c97c97b
chore(mappings): generate on_attach from user's legacy mappings
alex-courtis Sep 5, 2022
b2cdee5
chore(mappings): generate on_attach from user's legacy mappings
alex-courtis Sep 5, 2022
13b6c8c
Merge remote-tracking branch 'origin/master' into feat-branch-complet…
alex-courtis Nov 6, 2022
b36b7b4
chore(mappings): merge cleanup
alex-courtis Nov 6, 2022
46010a9
Merge branch 'master' into feat-branch-complete-on-attach
alex-courtis Nov 27, 2022
1978dc1
chore(mappings): use default mappings when on_attach not present, log…
alex-courtis Nov 27, 2022
da6fcd2
on_attach is default or user only, legacy and generation includes def…
alex-courtis Dec 11, 2022
64beb33
Merge branch 'master' into feat-branch-complete-on-attach
alex-courtis Dec 11, 2022
8603a2b
Merge branch 'master' into feat-branch-complete-on-attach
alex-courtis Dec 12, 2022
915d28b
Merge branch 'master' into feat-branch-complete-on-attach
alex-courtis Jan 26, 2023
94634c0
on_attach_default hardcoded
alex-courtis Jan 26, 2023
f495bca
format default_on_attach
alex-courtis Jan 26, 2023
ed23287
source default on_attach directly
alex-courtis Jan 26, 2023
c2b9793
remove human mappings help
alex-courtis Jan 26, 2023
43d1900
simplified on_attach generation
alex-courtis Jan 26, 2023
af6de11
Merge branch 'master' into feat-branch-complete-on-attach
alex-courtis Jan 29, 2023
c35027b
simplified on_attach generation
alex-courtis Jan 29, 2023
7627d21
generate default on_attach
alex-courtis Jan 29, 2023
3455829
generate default on_attach
alex-courtis Jan 29, 2023
00e8d24
Merge branch 'master' into feat-branch-complete-on-attach
alex-courtis Feb 12, 2023
df24c8a
Merge branch 'master' into feat-branch-complete-on-attach
alex-courtis Feb 12, 2023
68096fc
split out keymap_legacy
alex-courtis Feb 12, 2023
24ecf35
add recently introduced mappings
alex-courtis Feb 12, 2023
25dc48d
legacy api.config.mappings.active and default
alex-courtis Feb 12, 2023
e01fa1c
legacy api.config.mappings.active and default
alex-courtis Feb 12, 2023
d26b57a
Merge branch 'master' into feat-branch-complete-on-attach
alex-courtis Feb 20, 2023
4f89a04
on_attach help and readme
alex-courtis Feb 20, 2023
90ea114
legacy generate handles action = ""
alex-courtis Feb 20, 2023
f073cba
legacy generate handles action =
alex-courtis Feb 20, 2023
00be1db
legacy generate gives defaults when no user mappings
alex-courtis Feb 20, 2023
3ca305c
legacy generate handles action = ""
alex-courtis Feb 20, 2023
4ec73bb
legacy generate api handles overrides
alex-courtis Feb 20, 2023
b7b1427
legacy generate handles subsequent setup, on_attach retains deep copi…
alex-courtis Feb 21, 2023
8359ffc
add wiki link to generated on_attach
alex-courtis Feb 21, 2023
7c2d7eb
Merge remote-tracking branch 'origin/master' into feat-branch-complet…
alex-courtis Feb 27, 2023
8810d3c
add opts helper function for on_attach, prefixing 'nvim-tree: '
alex-courtis Feb 27, 2023
3054033
add opts helper function for on_attach, prefixing 'nvim-tree: '
alex-courtis Feb 27, 2023
e68f2e8
more comments in generated on_attach
alex-courtis Feb 27, 2023
9791804
update release date
alex-courtis Feb 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
on_attach help and readme
  • Loading branch information
alex-courtis committed Feb 20, 2023
commit 4f89a04e99e4f097f002d996575808bd5c7594e9
34 changes: 9 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Take a look at the [wiki](https://github.com/nvim-tree/nvim-tree.lua/wiki) for S

Existing `*_on_setup*` mechanisms have been removed in favour of [Open At Startup](https://github.com/nvim-tree/nvim-tree.lua/wiki/Open-At-Startup)

## New Mapping Method 2023-03-01

[:help nvim-tree.view.mappings](doc/nvim-tree-lua.txt) have been deprecated in favour of [:help nvim-tree.on_attach](doc/nvim-tree-lua.txt). Please visit [Migrating To on_attach](https://github.com/nvim-tree/nvim-tree.lua/wiki/Migrating-To-on_attach) to transition.

## Requirements

[neovim >=0.8.0](https://github.com/neovim/neovim/wiki/Installing-Neovim)
Expand Down Expand Up @@ -77,14 +81,6 @@ require("nvim-tree").setup()
-- OR setup with some options
require("nvim-tree").setup({
sort_by = "case_sensitive",
view = {
width = 30,
mappings = {
list = {
{ key = "u", action = "dir_up" },
},
},
},
renderer = {
group_empty = true,
},
Expand All @@ -96,7 +92,7 @@ require("nvim-tree").setup({

For complete list of available configuration options see [:help nvim-tree-setup](doc/nvim-tree-lua.txt)

Each option is documented in `:help nvim-tree.OPTION_NAME`. Nested options can be accessed by appending `.`, for example [:help nvim-tree.view.mappings](doc/nvim-tree-lua.txt)
Each option is documented in `:help nvim-tree.OPTION_NAME`. Nested options can be accessed by appending `.`, for example [:help nvim-tree.filters.dotfiles](doc/nvim-tree-lua.txt)

## Commands

Expand All @@ -114,10 +110,10 @@ Basic commands:

## Mappings

nvim-tree comes with number of mappings; for default mappings please see [:help nvim-tree-default-mappings](doc/nvim-tree-lua.txt), for way of configuring mappings see [:help nvim-tree-mappings](doc/nvim-tree-lua.txt)

`g?` toggles help, showing all the mappings and their actions.

To customise your mappings see [:help nvim-tree.on_attach](doc/nvim-tree-lua.txt) and [:help nvim-tree-mappings](doc/nvim-tree-lua.txt)

## Roadmap

nvim-tree is stable and new major features will not be added. The focus is on existing user experience.
Expand All @@ -133,25 +129,13 @@ Development is focused on:

## API

nvim-tree exposes a public API. This is non breaking, with additions made as necessary.
nvim-tree exposes a public API. This is non breaking, with additions made as necessary. See [:help nvim-tree-api](doc/nvim-tree-lua.txt)

See wiki [Recipes](https://github.com/nvim-tree/nvim-tree.lua/wiki/Recipes) and [Tips](https://github.com/nvim-tree/nvim-tree.lua/wiki/Tips) for ideas and insipration.

Please raise a [feature request](https://github.com/nvim-tree/nvim-tree.lua/issues/new?assignees=&labels=feature+request&template=feature_request.md&title=) if the API is insufficent for your needs. [Contributions](#Contributing) are always welcome.

[:help nvim-tree-api](doc/nvim-tree-lua.txt)

### Events

Users may subscribe to events that nvim-tree will dispatch in a variety of situations.

[:help nvim-tree-events](doc/nvim-tree-lua.txt)

### Actions

Custom actions may be mapped which can invoke API or perform your own actions.

[:help nvim-tree-mappings](doc/nvim-tree-lua.txt)
You may also subscribe to events that nvim-tree will dispatch in a variety of situations, see [:help nvim-tree-events](doc/nvim-tree-lua.txt)

## Contributing

Expand Down
173 changes: 110 additions & 63 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ CONTENTS *nvim-tree*
4. Setup/Configuration |nvim-tree-setup|
5. Api |nvim-tree-api|
6. Mappings |nvim-tree-mappings|
6.1 Default Mappings |nvim-tree-mappings-default|
6.2 Legacy Mappings |nvim-tree-mappings-legacy|
7. Highlight Groups |nvim-tree-highlight|
8. Events |nvim-tree-events|
9. Bookmarks |nvim-tree-bookmarks|
Expand Down Expand Up @@ -99,27 +101,27 @@ Setup should be run in a lua file or in a |lua-heredoc| if using in a vim file.
==============================================================================
3. COMMANDS *nvim-tree-commands*

|:NvimTreeOpen|
*:NvimTreeOpen*

opens the tree. Takes an optional path argument.

|:NvimTreeClose|
*:NvimTreeClose*

closes the tree

|:NvimTreeToggle|
*:NvimTreeToggle*

open or close the tree. Takes an optional path argument.

|:NvimTreeFocus|
*:NvimTreeFocus*

open the tree if it is closed, and then focus on the tree

|:NvimTreeRefresh|
*:NvimTreeRefresh*

refresh the tree

|:NvimTreeFindFile|
*:NvimTreeFindFile*

The command will change the cursor in the tree for the current bufname.

Expand All @@ -129,35 +131,41 @@ Setup should be run in a lua file or in a |lua-heredoc| if using in a vim file.

Invoke with a bang `:NvimTreeFindFile!` to update the root.

|:NvimTreeFindFileToggle|
*:NvimTreeFindFileToggle*

close the tree or change the cursor in the tree for the current bufname,
similar to combination of |:NvimTreeToggle| and |:NvimTreeFindFile|. Takes an
optional path argument.

Invoke with a bang `:NvimTreeFindFileToggle!` to update the root.

|:NvimTreeClipboard|
*:NvimTreeClipboard*

Print clipboard content for both cut and copy

|:NvimTreeResize|
*:NvimTreeResize*

Resize the NvimTree window to the given size. Example: `:NvimTreeResize 50`
resizes the window to the width of 50. If the size starts with "+" or "-" it
adds or removes the given value to the current window width.
Example `:NvimTreeResize -20` removes the value 20 from the current width. And
`:NvimTreeResize +20` adds the value 20 to the current width.

|:NvimTreeCollapse|
*:NvimTreeCollapse*

Collapses the nvim-tree recursively.

|:NvimTreeCollapseKeepBuffers|
*:NvimTreeCollapseKeepBuffers*

Collapses the nvim-tree recursively, but keep the directories open, which are
used in an open buffer.

*:NvimTreeGenerateOnAttach*

Creates and opens a new file `/tmp/my_on_attach.lua` containing an
|nvim-tree.on_attach| function based on your |nvim-tree.view.mappings|,
|nvim-tree.remove_keymaps| as well as the defaults.
See https://github.com/nvim-tree/nvim-tree.lua/wiki/Migrating-To-on_attach

==============================================================================
4. SETUP *nvim-tree-setup*
Expand All @@ -184,7 +192,7 @@ Subsequent calls to setup will replace the previous configuration.
sync_root_with_cwd = false,
reload_on_bufenter = false,
respect_buf_cwd = false,
on_attach = "disable",
on_attach = "default",
remove_keymaps = false,
select_prompts = false,
view = {
Expand Down Expand Up @@ -682,23 +690,13 @@ performance.
Type: {string}, Default: `{}`

*nvim-tree.on_attach*
Function ran when creating the nvim-tree buffer.
This can be used to attach keybindings to the tree buffer.
When on_attach is not a function, |nvim-tree.view.mappings| will be used.
Type: `function(bufnr)`, Default: `"disable"`
e.g. >
local api = require('nvim-tree.api')
Runs when creating the nvim-tree buffer. Use this to set your nvim-tree
specific mappings. See |nvim-tree-mappings|.
When on_attach is not a function, |nvim-tree-mappings-default| will be called.
Type: `function(bufnr) | string`, Default: `"default"`

local function on_attach(bufnr)
vim.keymap.set('n', '?', api.tree.toggle_help, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Help' })
vim.keymap.set('n', 'h', api.tree.toggle_help, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Help' })
vim.keymap.set("n", "<C-P>", function()
local node = api.tree.get_node_under_cursor()
print(node.absolute_path)
end, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = "my description" })
end
<
*nvim-tree.remove_keymaps*
Deprecated: please see |nvim-tree-mappings-legacy|
This can be used to remove the default mappings in the tree.
- Remove specific keys by passing a `string` table of keys
eg. {"<C-o>", "<CR>", "o", "<Tab>"}
Expand Down Expand Up @@ -775,7 +773,7 @@ Window / buffer setup.
Type: `string`, Default: `"yes"`

*nvim-tree.view.mappings*
Configuration options for |nvim-tree-mappings|
Deprecated: please see |nvim-tree-mappings-legacy|

*nvim-tree.view.mappings.custom_only*
Will use only the provided user mappings and not the default otherwise,
Expand All @@ -786,7 +784,7 @@ Window / buffer setup.
*nvim-tree.view.mappings.list*
A list of keymaps that will extend or override the default keymaps.
Type: `table`
Default: see |nvim-tree-default-mappings|
Default: see |nvim-tree-mappings-legacy|

*nvim-tree.view.float*
Configuration options for floating window
Expand Down Expand Up @@ -1445,54 +1443,52 @@ api.config.mappings.default() *nvim-tree.api.config.mappings.default()*
==============================================================================
6. MAPPINGS *nvim-tree-mappings*

Setting your own mapping in the configuration will soon be deprecated, see
|nvim-tree.on_attach| for experimental replacement.

`view.mappings.list` is a table of:
{key} (string|table of string) mandatory |{lhs}|.

{action} (string) mandatory name of the action.
`""` to remove an action with {key}. The case of {key} must
exactly match the action you are removing e.g. `'<Tab>'` to
remove the default preview action.
An arbitrary description when using `action_cb`.
Mappings are set via the |nvim-tree.on_attach| function, which is run upon
creating the nvim-tree buffer. Mappings may be directly to |nvim-tree-api|
functions or your own.

{action_cb} (function) optional custom function that will be called.
Receives the node as a parameter.
Non-empty description for `action` is required.
When on_attach is not a function, |nvim-tree-mappings-default| will be used.

{mode} (string) optional mode as per |nvim_set_keymap|.
Default `'n'`.
Active mappings may be viewed via HELP, default `g?`. The mapping's description
is used when displaying HELP.

Examples:
The `on_attach` function is passed the `bufnr` of nvim-tree. Use
|vim.keymap.set()| or |nvim_set_keymap()| to define mappings as usual. e.g.
>
local function print_node_path(node)
print(node.absolute_path)
end
local M = {}

----

view = {
mappings = {
list = {
-- remove a default mapping for cd
{ key = "<2-RightMouse>", action = "" },
local api = require("nvim-tree.api")

-- add multiple normal mode mappings for edit
{ key = { "<CR>", "o" }, action = "edit", mode = "n" },
function M.on_attach(bufnr)
-- put some default mappings here
vim.keymap.set('n', 'h', api.tree.toggle_help, { desc = 'Help', buffer = bufnr, noremap = true, silent = true, nowait = true })
vim.keymap.set('n', '?', api.tree.toggle_help, { desc = 'Help', buffer = bufnr, noremap = true, silent = true, nowait = true })
vim.keymap.set('n', 'p', M.print_node_path, { desc = 'Print', buffer = bufnr, noremap = true, silent = true, nowait = true })
end

-- custom action
{ key = "p", action = "print_the_node_path", action_cb = print_node_path },
function M.print_node_path()
local node = api.tree.get_node_under_cursor()
print(node.absolute_path)
end

----
require("nvim-tree").setup({
on_attach = M.on_attach,
--
})
<
Mouse support defined in |KeyBindings|
Mouse support is defined in |KeyBindings|

Single left mouse mappings can be achieved via `<LeftRelease>`.

Single right / middle mouse mappings will requre changes to |mousemodel| or |mouse|.

DEFAULT MAPPINGS *nvim-tree-default-mappings*
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed these as they are problematic to generate; DEFAULT_ON_ATTACH is quite readable.

==============================================================================
6.1 DEFAULT MAPPINGS *nvim-tree-mappings-default*

In the absence of an |nvim-tree.on_attach| function, the following defaults
will be applied.

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')
Expand Down Expand Up @@ -1552,7 +1548,58 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
vim.keymap.set('n', '<2-RightMouse>', api.tree.change_root_to_node, { desc = 'CD', buffer = bufnr, noremap = true, silent = true, nowait = true })
-- END_DEFAULT_ON_ATTACH
end
<
==============================================================================
6.2 LEGACY MAPPINGS *nvim-tree-mappings-legacy*

nvim-tree mappings were provided via the deprecated |nvim-tree.view.mappings|
and |nvim-tree.remove_keymaps|

These are ignored when |nvim-tree.on_attach| is present.

You are encouraged to migrate you existing legacy mappings to
|nvim-tree.on_attach| using |:NvimTreeGenerateOnAttach|

Please visit https://github.com/nvim-tree/nvim-tree.lua/wiki/Migrating-To-on_attach

`view.mappings.list` is a table of:
{key} (string|table of string) mandatory |{lhs}|.

{action} (string) mandatory name of the action.
`""` to remove an action with {key}. The case of {key} must
exactly match the action you are removing e.g. `'<Tab>'` to
remove the default preview action.
An arbitrary description when using `action_cb`.

{action_cb} (function) optional custom function that will be called.
Receives the node as a parameter.
Non-empty description for `action` is required.

{mode} (string) optional mode as per |nvim_set_keymap|.
Default `'n'`.

Examples:
>
local function print_node_path(node)
print(node.absolute_path)
end

----

view = {
mappings = {
list = {
-- remove a default mapping for cd
{ key = "<2-RightMouse>", action = "" },

-- add multiple normal mode mappings for edit
{ key = { "<CR>", "o" }, action = "edit", mode = "n" },

-- custom action
{ key = "p", action = "print_the_node_path", action_cb = print_node_path },

----
<
==============================================================================
7. HIGHLIGHT GROUPS *nvim-tree-highlight*

Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
sync_root_with_cwd = false,
reload_on_bufenter = false,
respect_buf_cwd = false,
on_attach = "disable",
on_attach = "default",
remove_keymaps = false,
select_prompts = false,
view = {
Expand Down