Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aaronik/treewalker.nvim

A neovim plugin for fast navigation around the abstract syntax tree
https://github.com/aaronik/treewalker.nvim

abstract-syntax-tree navigation neovim plugin

Last synced: 2 days ago
JSON representation

A neovim plugin for fast navigation around the abstract syntax tree

Awesome Lists containing this project

README

        

![build status](https://github.com/aaronik/treewalker.nvim/actions/workflows/test.yml/badge.svg)

Static Badge

Neovim

# Treewalker.nvim

![A demo of moving around some code quickly using the plugin](static/fast_demo.gif)

Treewalker is a plugin that gives you the ability to **move around your code in a syntax tree aware manner**.
It uses [Treesitter](https://github.com/tree-sitter/tree-sitter) under the hood for syntax tree awareness.
It offers six subcommands: Up, Down, Right, and Left for movement, and SwapUp and SwapDown for intelligent node swapping.

Each movement command moves you through the syntax tree in an intuitive way.

* **Up/Down** - Moves up or down to the next neighbor node
* **Right** - Finds the next good child node
* **Left** - Finds the next good parent node

The swap commands intelligently swap nodes, including comments and attributes/decorators.

---

Typing out the Move commands manually
A demo of moving around some code slowly typing out each Treewalker move command

Typing out the Swap commands manually
A demo of swapping code slowly using Treewalker swap commands

---

### Installation

##### Lazy:
```lua
{
"aaronik/treewalker.nvim",

-- The following options are the defaults.
-- Treewalker aims for sane defaults, so these are each individually optional,
-- and the whole opts block is optional as well.
opts = {
-- Whether to briefly highlight the node after jumping to it
highlight = true,

-- How long should above highlight last (in ms)
highlight_duration = 250,

-- The color of the above highlight. Must be a valid vim highlight group.
-- (see :h highlight-group for options)
highlight_group = "ColorColumn",
}
}
```

#### Mapping

This is how I have mine mapped; in `init.lua`:

```lua
vim.keymap.set({ 'n', 'v' }, '', 'Treewalker Up', { noremap = true, silent = true })
vim.keymap.set({ 'n', 'v' }, '', 'Treewalker Down', { noremap = true, silent = true })
vim.keymap.set({ 'n', 'v' }, '', 'Treewalker Right', { noremap = true, silent = true })
vim.keymap.set({ 'n', 'v' }, '', 'Treewalker Left', { noremap = true, silent = true })
vim.keymap.set('n', '', 'Treewalker SwapDown', { noremap = true, silent = true })
vim.keymap.set('n', '', 'Treewalker SwapUp', { noremap = true, silent = true })
```

I also utilize some
[nvim-treesitter-textobjects](https://github.com/nvim-treesitter/nvim-treesitter-textobjects?tab=readme-ov-file#text-objects-swap)
commands to get lateral swapping (that way I get all four `` maps in a natural and intuitive feeling way):

```lua
vim.keymap.set('n', "", ":TSTextobjectSwapNext @parameter.inner", { noremap = true, silent = true })
vim.keymap.set('n', "", ":TSTextobjectSwapPrevious @parameter.inner", { noremap = true, silent = true })
```

The above can also be accomplished with
[nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) using
[ts_utils](https://github.com/nvim-treesitter/nvim-treesitter?tab=readme-ov-file#utilities).
See [this PR](https://github.com/aaronik/treewalker.nvim/pull/10/files) for
an example of that!