• This repository has been archived on 13/May/2023
  • Stars
    star
    143
  • Rank 257,007 (Top 6 %)
  • Language
    Lua
  • License
    BSD 3-Clause "New...
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

🚀 Structural editing powered by treesitter

This project has moved to sourcehut

architext.nvim

A structural editing plugin for neovim, powered by treesitter.

Installation

Use the only package manager :

use {
  "vigoux/architext.nvim"
  requires = {
    -- Not required, only used to refine the language resolution
    "nvim-treesitter/nvim-treesitter"
  }
}

Usage

Using the REPL

  1. Open a buffer, and be sure to have the correct parsers installed for the buffers filetype.
  2. :ArchitextREPL
  3. Type your query, with captures for the things we want to replace / refer to
  4. Type the replacements for them (empty to not replace), and refer to a capture using @{capture name}
  5. Hit <CR> your edits happen !

Using :Architext

First, open a buffer, be sure to have a parser for it installed.

Then, you can use the :Architext (or :A in short) command. This is a substitue like command, with this signature :

:{range}Architext/{query}(/{capture name}:{replacement})*

Which instanciates into this, to replace every occurence of the identifier foo by bar :

:Architext/((identifier) @i (#eq? @i "foo"))/i:bar/

You can refer to captured nodes like this, lets say you want to wrap an identifier into a function:

:A/((identifier) @wrapit (#eq? @wrapit "foo"))/wrapit:func(@wrapit)/

Additional notes :

  • The delimiter can be anything, and is specified by the first character after the command
  • You can insert a literal @ like so @@

Query templates

You can recall query templates by starting the query argument with a $. Query templates are called like so :

$TEMPLATE:arg1:arg2:...

The : can be replaced by any non uppercase character (though, : is recommended).

If you want to omit an argument :

$TEMPLATE::arg2:...

While this might seem useful, and robust, it is actually not, and no check is done on the number of arguments you pass to a query, and wether they are expected.

Writing templates

Writing templates is easy, and follows an over-simplified snippet-like syntax, for example the builtin IDENT template looks like this :

((identifier) @id (#eq? @id "$1"))

Arguments of the template are called with ${number}, 1-based (why not ?).

Using the API

The main function une architext is architext.edit.edit, which contains all the machinery for handling replacements on (non-template) queries.

The signature is as follows:

edit(buf, parser, query, changes, start_row, end_row)

The arguments are thus:

  1. The buffer number where the edits take place
  2. The parser to use for that given buffer
  3. The query used as reference for matching the tree (think the first part of the :Architext command)
  4. The actual changes to perform, in the form of a map between capture names and their replacements (think, the rest of the :Architext command): note the there must not be the leading @ in the capture names.
  5. The start row (inclusive)
  6. The end row (inclusive)

As an example, here is the API to swap the first two arguments of function calls in lua:

local curbuf = vim.api.nvim_get_current_buf()
local parser = vim.treesitter.get_parser(curbuf)

local query = vim.treesitter.query.parse("lua", [[
  (arguments 
    . (_) @first
    . (_) @second)
]])

require'architext.edit'.edit(curbuf, parser, query, { first =
"@second", second = "@first" }, 0, 10)

Credits

Thanks @tjdevries for the name.

More Repositories

1

notifier.nvim

Non-intrusive notification system for neovim
Lua
190
star
2

LanguageTool.nvim

An asynchronous grammar checker for Neovim using LanguageTool
Vim Script
77
star
3

tree-sitter-viml

Tree-sitter parser for VimL
JavaScript
74
star
4

ltex-ls.nvim

Enhanced integration of ltex-ls for neovim
Lua
43
star
5

complementree.nvim

Tree-sitter powered syntax-aware completion framework.
Lua
43
star
6

oak

A calming vim/neovim colorscheme
Vim Script
34
star
7

azy.nvim

Simple and fast fuzzy finder for neovim
Lua
29
star
8

templar.nvim

A dead simple template manager for neovim written in lua
Lua
25
star
9

treesitter-context.nvim

A context.vim clone powered by treesitter.
Lua
18
star
10

zest

Rust
13
star
11

ratatoskr.nvim

Lua
12
star
12

tree-sitter-fuzz-action

Shell
10
star
13

tree-sitter-diff

A tree-sitter parser for diff files
C
7
star
14

infer.nvim

Read an analyse infer stack traces within neovim
Lua
6
star
15

mem.nvim

A simple TODO management tool for NeoVim
Lua
5
star
16

vigoux

A readme about me !
3
star
17

redict

A TUI and library to work with DICT servers.
Rust
2
star
18

tabbed

My personnal build of suckless tabbed.
C
2
star
19

tree-sitter-mem

A Tree-sitter parser for my TODO markup language
C
2
star
20

dwm

Personnal fork of dwm
C
2
star
21

proofreader.nvim

[WIP] Proof reading plugin for Neovim powered by LanguageTool and Tree-Sitter
Lua
2
star
22

mandelbrust

Multithreaded mandelbrot image generator written in Rust
Rust
1
star
23

slstatus

Personnal build of slstatus
C
1
star
24

slock

My personnal build of suckless slock
C
1
star
25

st

Personal build of the suckless terminal
C
1
star
26

marvim-bot-test

Test repo for the marvim bot
1
star
27

dmenu

C
1
star
28

CALOA

CALOA project
Python
1
star