3
3
<img alt =" Static Badge " src =" https://img.shields.io/badge/100%25_lua-purple " style =" height : 20px ;" >
4
4
</span >
5
5
![ build status] ( https://github.com/aaronik/treewalker.nvim/actions/workflows/test.yml/badge.svg )
6
+ [ ![ Github All Releases] ( https://img.shields.io/github/downloads/aaronik/treewalker.nvim/total.svg )] ( )
6
7
7
8
# Treewalker.nvim
8
9
@@ -22,6 +23,8 @@ The swap commands intelligently swap nodes, including comments and attributes/de
22
23
23
24
---
24
25
26
+ ### More Examples
27
+
25
28
<details >
26
29
<summary >Typing out the Move commands manually</summary >
27
30
<img src =" static/slow_move_demo.gif " alt =" A demo of moving around some code slowly typing out each Treewalker move command " >
@@ -36,14 +39,14 @@ The swap commands intelligently swap nodes, including comments and attributes/de
36
39
37
40
### Installation
38
41
39
- #### [ Lazy] ( https://github.com/folke/lazy.nvim ) :
42
+ #### [ Lazy] ( https://github.com/folke/lazy.nvim )
40
43
``` lua
41
44
{
42
- " aaronik/treewalker.nvim" ,
45
+ ' aaronik/treewalker.nvim' ,
43
46
44
47
-- The following options are the defaults.
45
48
-- Treewalker aims for sane defaults, so these are each individually optional,
46
- -- and the whole opts block is optional as well.
49
+ -- and setup() does not need to be called, so the whole opts block is optional as well.
47
50
opts = {
48
51
-- Whether to briefly highlight the node after jumping to it
49
52
highlight = true ,
@@ -53,17 +56,18 @@ The swap commands intelligently swap nodes, including comments and attributes/de
53
56
54
57
-- The color of the above highlight. Must be a valid vim highlight group.
55
58
-- (see :h highlight-group for options)
56
- highlight_group = " ColorColumn" ,
59
+ highlight_group = ' ColorColumn' ,
57
60
}
58
61
}
59
62
```
60
63
61
- #### [ Packer] ( https://github.com/wbthomason/packer.nvim ) :
64
+ #### [ Packer] ( https://github.com/wbthomason/packer.nvim )
62
65
``` lua
63
66
use {
64
- " aaronik/treewalker.nvim" ,
67
+ ' aaronik/treewalker.nvim' ,
65
68
66
69
-- The setup function is optional, defaults are meant to be sane
70
+ -- and setup does not need to be called
67
71
setup = function ()
68
72
require (' treewalker' ).setup ({
69
73
-- Whether to briefly highlight the node after jumping to it
@@ -74,46 +78,53 @@ use {
74
78
75
79
-- The color of the above highlight. Must be a valid vim highlight group.
76
80
-- (see :h highlight-group for options)
77
- highlight_group = " ColorColumn" ,
81
+ highlight_group = ' ColorColumn' ,
78
82
})
79
83
end
80
84
}
81
85
```
82
86
83
- #### [ Vim-plug] ( https://github.com/junegunn/vim-plug ) :
87
+ #### [ Vim-plug] ( https://github.com/junegunn/vim-plug )
84
88
``` vimscript
85
- Plug " aaronik/treewalker.nvim"
89
+ Plug ' aaronik/treewalker.nvim'
86
90
87
- " The following is optional
88
- :lua require(" treewalker" ).setup({ highlight = true, highlight_duration = 250, highlight_group = " ColorColumn" })
91
+ " This line is optional
92
+ :lua require(' treewalker' ).setup({ highlight = true, highlight_duration = 250, highlight_group = ' ColorColumn' })
89
93
```
90
94
91
95
---
92
96
93
- #### Mapping
97
+ ### Mapping
98
+
99
+ I've found Ctrl - h / j / k / l to be a really natural flow for this plugin, and adding
100
+ Shift to that for swapping just felt so clean. So here are the mappings I use:
94
101
95
- This is how I have mine mapped; in ` init.lua ` :
102
+ In ` init.lua ` :
96
103
97
104
``` lua
98
- vim .keymap .set ({ ' n' , ' v' }, ' <C-k>' , ' <cmd>Treewalker Up<cr>' , { noremap = true , silent = true })
99
- vim .keymap .set ({ ' n' , ' v' }, ' <C-j>' , ' <cmd>Treewalker Down<cr>' , { noremap = true , silent = true })
100
- vim .keymap .set ({ ' n' , ' v' }, ' <C-l>' , ' <cmd>Treewalker Right<cr>' , { noremap = true , silent = true })
101
- vim .keymap .set ({ ' n' , ' v' }, ' <C-h>' , ' <cmd>Treewalker Left<cr>' , { noremap = true , silent = true })
102
- vim .keymap .set (' n' , ' <C-S-j>' , ' <cmd>Treewalker SwapDown<cr>' , { noremap = true , silent = true })
103
- vim .keymap .set (' n' , ' <C-S-k>' , ' <cmd>Treewalker SwapUp<cr>' , { noremap = true , silent = true })
105
+ -- movement
106
+ vim .keymap .set ({ ' n' , ' v' }, ' <C-k>' , ' <cmd>Treewalker Up<cr>' , { silent = true })
107
+ vim .keymap .set ({ ' n' , ' v' }, ' <C-j>' , ' <cmd>Treewalker Down<cr>' , { silent = true })
108
+ vim .keymap .set ({ ' n' , ' v' }, ' <C-l>' , ' <cmd>Treewalker Right<cr>' , { silent = true })
109
+ vim .keymap .set ({ ' n' , ' v' }, ' <C-h>' , ' <cmd>Treewalker Left<cr>' , { silent = true })
110
+
111
+ -- swapping up/down
112
+ vim .keymap .set (' n' , ' <C-S-j>' , ' <cmd>Treewalker SwapDown<cr>' , { silent = true })
113
+ vim .keymap .set (' n' , ' <C-S-k>' , ' <cmd>Treewalker SwapUp<cr>' , { silent = true })
104
114
```
105
115
106
- I also utilize some
107
- [ nvim-treesitter-textobjects] ( https://github.com/nvim-treesitter/nvim-treesitter-textobjects?tab=readme-ov-file#text-objects-swap )
108
- commands to get lateral swapping (that way I get all four ` <C-S-{h,j,k,l}> ` maps in a natural and intuitive feeling way) :
116
+ Many other plugins do left/right swapping perfectly, so there's no need for Treewalker to implement those.
117
+ I use [ nvim-treesitter-textobjects] ( https://github.com/nvim-treesitter/nvim-treesitter-textobjects?tab=readme-ov-file#text-objects-swap )
118
+ to complete all four ` Ctrl-Shift-* ` maps:
109
119
110
120
``` lua
111
- vim .keymap .set (' n' , ' <C-S-l>' , ' <cmd>TSTextobjectSwapNext @parameter.inner<CR>' , { noremap = true , silent = true })
112
- vim .keymap .set (' n' , ' <C-S-h>' , ' <cmd>TSTextobjectSwapPrevious @parameter.inner<CR>' , { noremap = true , silent = true })
121
+ -- swapping left/right
122
+ vim .keymap .set (' n' , ' <C-S-l>' , ' <cmd>TSTextobjectSwapNext @parameter.inner<CR>' , { silent = true })
123
+ vim .keymap .set (' n' , ' <C-S-h>' , ' <cmd>TSTextobjectSwapPrevious @parameter.inner<CR>' , { silent = true })
113
124
```
114
125
115
126
The above can also be accomplished with
116
127
[ nvim-treesitter] ( https://github.com/nvim-treesitter/nvim-treesitter ) using
117
128
[ ts_utils] ( https://github.com/nvim-treesitter/nvim-treesitter?tab=readme-ov-file#utilities ) .
118
- See [ this PR] ( https://github.com/aaronik/treewalker.nvim/pull/10/files ) for
119
- an example of that!
129
+ See [ this PR] ( https://github.com/aaronik/treewalker.nvim/pull/10/files#diff-1cedf17af3ebbcfeee2dfb1071445dcabfdb30f8e9f83ff0da95ea7e6e7ccde5R107-R130 )
130
+ by @ duqcyxwd for an example of how to implement that.
0 commit comments