Giter VIP home page Giter VIP logo

nvim-treesitter-textsubjects's Introduction

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',
            ['i;'] = { 'textsubjects-container-inner', desc = "Select inside containers (classes, functions, etc.)" },
        },
    },
}

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.

nvim-treesitter-textsubjects's People

Contributors

rrethy avatar stephenprater avatar timoleistner avatar lf- avatar foamscience avatar muniftanjim avatar rktjmp avatar gelio avatar bbbert avatar aspeddro avatar younger-1 avatar iasoon avatar smoka7 avatar nitper avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.