Skip to content

A neovim plugin for fast navigation around the abstract syntax tree

License

Notifications You must be signed in to change notification settings

aaronik/treewalker.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

build status Static Badge Neovim

Treewalker.nvim

A demo of moving around some code quickly using the plugin

Treewalker is a plugin that gives you the ability to move around your code in a syntax tree aware manner. It uses Treesitter 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:
{
  "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:

vim.keymap.set({ 'n', 'v' }, '<C-k>', '<cmd>Treewalker Up<cr>', { noremap = true, silent = true })
vim.keymap.set({ 'n', 'v' }, '<C-j>', '<cmd>Treewalker Down<cr>', { noremap = true, silent = true })
vim.keymap.set({ 'n', 'v' }, '<C-l>', '<cmd>Treewalker Right<cr>', { noremap = true, silent = true })
vim.keymap.set({ 'n', 'v' }, '<C-h>', '<cmd>Treewalker Left<cr>', { noremap = true, silent = true })
vim.keymap.set('n', '<C-S-j>', '<cmd>Treewalker SwapDown<cr>', { noremap = true, silent = true })
vim.keymap.set('n', '<C-S-k>', '<cmd>Treewalker SwapUp<cr>', { noremap = true, silent = true })

I also utilize some nvim-treesitter-textobjects 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):

vim.keymap.set('n', "<C-S-l>", ":TSTextobjectSwapNext @parameter.inner<CR>", { noremap = true, silent = true })
vim.keymap.set('n', "<C-S-h>", ":TSTextobjectSwapPrevious @parameter.inner<CR>", { noremap = true, silent = true })

The above can also be accomplished with nvim-treesitter using ts_utils. See this PR for an example of that!

About

A neovim plugin for fast navigation around the abstract syntax tree

Topics

Resources

License

Stars

Watchers

Forks