Coder Social home page Coder Social logo

kiyoon / treesitter-indent-object.nvim Goto Github PK

View Code? Open in Web Editor NEW
46.0 4.0 1.0 21 KB

Context-aware indent textobject powered by Treesitter. `vai` to select current context.

License: MIT License

Lua 98.12% Vim Script 1.88%
neovim neovim-plugin neovim-plugins nvim nvim-plugin nvim-treesitter treesitter vim vim-indent vim-plugin

treesitter-indent-object.nvim's Introduction

Treesitter-indent-object.nvim

vai to select current context!

Context-aware smart indent object to select block, powered by treesitter.

This plugin is intended to be used with indent-blankline.nvim, so you see the scope before you select.

Install

Use your favourite plugin manager to install.

Example with lazy.nvim

This includes lazy-loading on keymaps. If you install like this, you can ignore every instruction below.

  {
    "kiyoon/treesitter-indent-object.nvim",
    keys = {
      {
        "ai",
        function() require'treesitter_indent_object.textobj'.select_indent_outer() end,
        mode = {"x", "o"},
        desc = "Select context-aware indent (outer)",
      },
      {
        "aI",
        function() require'treesitter_indent_object.textobj'.select_indent_outer(true) end,
        mode = {"x", "o"},
        desc = "Select context-aware indent (outer, line-wise)",
      },
      {
        "ii",
        function() require'treesitter_indent_object.textobj'.select_indent_inner() end,
        mode = {"x", "o"},
        desc = "Select context-aware indent (inner, partial range)",
      },
      {
        "iI",
        function() require'treesitter_indent_object.textobj'.select_indent_inner(true, 'V') end,
        mode = {"x", "o"},
        desc = "Select context-aware indent (inner, entire range) in line-wise visual mode",
      },
    },
  },
Click to see instructions for packer and vim-plug

Example with Packer

wbthomason/packer.nvim

-- init.lua
require("packer").startup(function()
  use "nvim-treesitter/nvim-treesitter"
  use "lukas-reineke/indent-blankline.nvim"  -- optional
  use "kiyoon/treesitter-indent-object.nvim"
end)

Example with Plug

junegunn/vim-plug

" init.vim
call plug#begin('~/.vim/plugged')
Plug 'nvim-treesitter/nvim-treesitter'
Plug 'lukas-reineke/indent-blankline.nvim'  " optional
Plug 'kiyoon/treesitter-indent-object.nvim'
call plug#end()

Setup

setup() is not required, but here is the recommendation.

require("indent_blankline").setup {
    show_current_context = true,
    show_current_context_start = true,
}

-- Actually, no setup is required, but
-- if setup comes after the indent_blankline,
-- it will try to follow the pattern matching options
-- used in indent_blankline
require("treesitter_indent_object").setup()

Key bindings are not configured by default. Here are some examples.

" vai to select context-aware indent
xmap ai <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_outer()<CR>
omap ai <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_outer()<CR>
" vaI to ensure selecting entire line (or just use Vai)
xmap aI <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_outer(true)<CR>
omap aI <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_outer(true)<CR>
" vii to select inner block (only if block, only else block, etc.)
xmap ii <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_inner()<CR>
omap ii <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_inner()<CR>
" viI to select entire inner range (including if, else, etc.) in line-wise visual mode
xmap iI <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_inner(true, 'V')<CR>
omap iI <Cmd>lua require'treesitter_indent_object.textobj'.select_indent_inner(true, 'V')<CR>

Lua equivalent:

-- select context-aware indent
vim.keymap.set({"x", "o"}, "ai", function() require'treesitter_indent_object.textobj'.select_indent_outer() end)
-- ensure selecting entire line (or just use Vai)
vim.keymap.set({"x", "o"}, "aI", function() require'treesitter_indent_object.textobj'.select_indent_outer(true) end)
-- select inner block (only if block, only else block, etc.)
vim.keymap.set({"x", "o"}, "ii", function() require'treesitter_indent_object.textobj'.select_indent_inner() end)
-- select entire inner range (including if, else, etc.) in line-wise visual mode
vim.keymap.set({"x", "o"}, "iI", function() require'treesitter_indent_object.textobj'.select_indent_inner(true, 'V') end)

treesitter-indent-object.nvim's People

Contributors

kiyoon avatar ve5li avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

treesitter-indent-object.nvim's Issues

Feature Request: New Text object

This is an awesome plugin!

It would be useful to have a new text object that is similar to the existing text objects, but a new version that is restricted or bounded by newlines at the same indentation level:

Example:

    def sample_function():
        print("hello world")

        print("some text")
        print("more *text")      <--- cursor here
        print("even more text")

        print("some more text")

This would select:

        print("some text")
        print("more text")
        print("even more text")

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.