• Stars
    star
    495
  • Rank 88,974 (Top 2 %)
  • Language
    Scheme
  • License
    Apache License 2.0
  • Created over 3 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Location and syntax aware text objects which *do what you mean*

nvim-treesitter-textsubjects

Location and syntax aware text objects which *do what you mean*

Screen.Recording.2022-01-09.at.11.34.26.PM.mov

Quick Start

require('nvim-treesitter.configs').setup {
    textsubjects = {
        enable = true,
        prev_selection = ',', -- (Optional) keymap to select the previous selection
        keymaps = {
            ['.'] = 'textsubjects-smart',
            [';'] = 'textsubjects-container-outer',
            ['i;'] = 'textsubjects-container-inner',
        },
    },
}

This will enable the . (this is the mapping I use but <cr> is quite ergonomic too) and ; text objects which will select a different part of the syntax tree depending on your location in it. See below for what each query matches.

Whether the selection is by character or by line will also depend on your location in the syntax tree (handled by a few simple heuristics). See below for more details on what parts of the syntax tree are matched.

Note: I know these names are a bit confusing, but naming them is quite difficult.

Text Subjects

Note: I'm open to adding more queries or support for another language, just open and issue or a PR and I can work with you to get the query working.

container-inner container-outer smart
c 🟩 🟩 🟩
cpp 🟩 🟩 🟩
elixir ⬜ 🟩 🟩
foam ⬜ 🟩 🟩
go 🟩 🟩 🟩
javascript / jsx ⬜ 🟩 🟩
julia ⬜ 🟩 🟩
lua 🟩 🟩 🟩
php 🟩 🟩 🟩
python 🟩 🟩 🟩
r 🟩 🟩 🟩
ruby 🟩 🟩 🟩
rust 🟩 🟩 🟩
scss ⬜ ⬜ 🟩
typescript / tsx ⬜ 🟩 🟩
fennel 🟩 🟩 🟩
nix ⬜ ⬜ 🟩

textsubjects-smart

Patterns: comments, consecutive line comments, function calls, function definitions, class definitions, loops, if statements, return values, arguments.

See queries/*/textsubjects-smart.scm for full information on the query.

textsubjects-container-outer

Patterns: Classes, structs, functions, methods.

See queries/*/textsubjects-container-outer.scm for full information on the query.

textsubjects-container-inner

Patterns: Inside Classes, structs, functions, methods.

See queries/*/textsubjects-container-inner.scm for full information on the query.

Custom Query

You can create your own text subjects by creating a Tree-sitter query that has ranges named range. This query file name can be provided in the keymaps about.

Ranges can be created as follows:

((comment) @_start @_end
     (#make-range! "range" @_start @_end))

See queries/*/textsubjects-smart.scm for examples or open an issue if you need any help writing a query.

Alternatives

  • nvim-treesitter-textobjects: I still use this and it's a great plugin (especially the lookahead feature πŸ˜‰). If you want to target the textobject exactly then this plugin is the way to go. However, it can quickly lead to having too many text object mappings (I had 8 before making this plugin) and it will always deal with whitespace based on the way you invoked it rather than handling this heuristically.
  • nvim-treesitter-incremental-selection-mod: This is similar but doesn't act as a text objects. When incrementing the selection it will do so strictly by scope or by looking at the parent node which is often way too strict for practical use. For example, it will select all the arguments in (,) then can be expanded a few times to reach the next scope inside {,}, but these can both be selected more directly with ib and iB respectively so it would make more sense to skip them since the user probably doesn't want to select them. Incremental selection sounds nice but often isn't how people edit text (at least not me). On top of all this, it's strict with whitespace whereas this plugin will try to handle it heuristically.

More Repositories

1

vim-illuminate

illuminate.vim - (Neo)Vim plugin for automatically highlighting other uses of the word under the cursor using either LSP, Tree-sitter, or regex matching.
Lua
2,156
star
2

vim-hexokinase

hexokinase.vim - (Neo)Vim plugin for asynchronously displaying the colours in the file (#rrggbb, #rgb, rgb(a)? functions, hsl(a)? functions, web colours, custom patterns)
Vim Script
960
star
3

base16-nvim

Neovim plugin for building a sync base16 colorscheme. Includes support for Treesitter and LSP highlight groups.
Lua
500
star
4

nvim-treesitter-endwise

Wisely add "end" in Ruby, Vimscript, Lua, etc. Tree-sitter aware alternative to tpope's vim-endwise
Ruby
328
star
5

tcount

Count your code by tokens and patterns in the syntax tree. A tokei/scc/cloc alternative.
Rust
65
star
6

nvim-align

Neovim plugin for aligning text
Lua
40
star
7

ToonUp

Android App that lets you Choose from Thousands of Cartoons/Movies/Animes to Watch for Free
Kotlin
19
star
8

ahocorasick

The fastest Golang implementation of the Aho-Corasick algorithm for string-searching.
Go
18
star
9

dotfiles

My dotfiles: neovim, git, zsh, kitty
Lua
16
star
10

hexokinase

Fast text scraper to find and convert colours (hex, rgb, rgb, hsl, web colours, custom patterns, etc.) into hex values
Go
15
star
11

NOTES-CS246

Object-Oriented Software Development - C++
15
star
12

tagrity

Automatically regenerate your tags on file changes https://rubygems.org/gems/tagrity
Ruby
10
star
13

vim-tranquille

tranquille.vim - Vim plugin for searching without moving the cursor
Vim Script
8
star
14

nvim-hotline

Minimal Lua wrappers for 'statusline' and 'tabline'
Lua
8
star
15

VimHelp

Android app for browsing vim/neovim docs offline
Kotlin
6
star
16

MethodHooker

Xposed module for method hooking Android apps
Java
3
star
17

nvim-animator

Neovim plugin that animates the change in a value for use in animations.
Lua
3
star
18

vim-indexor

indexor.vim - Vim plugin for indexing lists
Vim Script
2
star
19

fzy-rs

fzy scoring algorithm as a Rust crate
Rust
2
star
20

rejects

POC Regular expression implementation using Rust macros to compile the automaton at compile time.
Rust
2
star
21

nvim-sourcerer

Automatically source your init.lua when it gets modified anywhere.
Lua
2
star
22

WordSearch

Kotlin
2
star
23

RhymeTime

Android app for looking up rhymes - Advanced usage example of Dagger.android
Kotlin
2
star
24

cpid1

An init (PID 1) process purpose-built for containers
Rust
1
star
25

SmartyFloaty

Android app that displays overlay and automatically disables it when permissions pop up.
Java
1
star
26

vim-spotlight

Searching through and open Apps using fzf inside Vim.
Vim Script
1
star
27

book

JavaScript
1
star
28

Quasar

Android App that lets you choose any image on your phone to act as a semi-transparent overlay
Java
1
star
29

rwc

Rust implementation of wc with a slightly nicer UI
Rust
1
star
30

nvim-carom

WIP: carom.nvim - execute macros asynchronously
Vim Script
1
star
31

tc

Moved to https://github.com/rrethy/tcount
1
star
32

vim-impiared

impiared.vim - a pair to match plugins of square, round, and curly brackets
Vim Script
1
star
33

cs350-os161-utils

Some utilities to ease the pain of working with os161 in CS350 at UWaterloo
Ruby
1
star
34

kubectl-x

`kubectl` plugin with various useful extensions.
Go
1
star