David-Kunz/treesitter-unit

github github
syntax plugin
star 87
stars
alert-circle 1
open issues
users 4
subscribers
git-branch 1
forks
CREATED

2021-08-21

UPDATED

8 hours ago

packer

require('packer').startup(function()
  use 'David-Kunz/treesitter-unit'
end)

paq

require "paq" { 
  'David-Kunz/treesitter-unit'
}

treesitter-unit

A tiny Neovim plugin to deal with treesitter units. A unit is defined as a treesitter node including all its children. It allows you to quickly select, yank, delete or replace language-specific ranges.

For inner selections, the main node under the cursor is selected. For outer selections, the next node is selected.

demo-with-highlight

Installation

Requirements: nvim-treesitter including a parser for your language

For vim-plug:

Plug 'David-Kunz/treesitter-unit'

For packer:

use 'David-Kunz/treesitter-unit'

Usage

Select treesitter unit

You can select the current treesitter unit

:lua require"treesitter-unit".select(outer?)

This function takes an optional Boolean flag to specify if the outer scope should be selected as well, default false. For operations like delete, change, ... please see section "Useful mappings".

Automatic Highlighting

You can toggle automatic highlighting for the current treesitter unit.

:lua require"treesitter-unit".toggle_highlighting(higroup?)

As an optional parameter you can specify the highlight group, default: "CursorLine".

Alternative: enable_highlighting(higroup?) and disable_highlighting().

Useful mappings

For init.vim:

xnoremap iu :lua require"treesitter-unit".select()<CR>
xnoremap au :lua require"treesitter-unit".select(true)<CR>
onoremap iu :<c-u>lua require"treesitter-unit".select()<CR>
onoremap au :<c-u>lua require"treesitter-unit".select(true)<CR>

For init.lua:

vim.api.nvim_set_keymap('x', 'iu', ':lua require"treesitter-unit".select()<CR>', {noremap=true})
vim.api.nvim_set_keymap('x', 'au', ':lua require"treesitter-unit".select(true)<CR>', {noremap=true})
vim.api.nvim_set_keymap('o', 'iu', ':<c-u>lua require"treesitter-unit".select()<CR>', {noremap=true})
vim.api.nvim_set_keymap('o', 'au', ':<c-u>lua require"treesitter-unit".select(true)<CR>', {noremap=true})

Examples:

  • viu to select the inner unit
  • cau to change the outer unit

Similar plugins

Making-of video