Skip to main content

Tree-sitter support for Vim

Project description

 _    ______  ______    _____ _ __  __
| |  / / __ \/ ____/   / ___/(_) /_/ /____  _____
| | / / /_/ / __/      \__ \/ / __/ __/ _ \/ ___/
| |/ / ____/ /___     ___/ / / /_/ /_/  __/ /     Tree-sitter in Vim
|___/_/   /_____/____/____/_/\__/\__/\___/_/

Welcome to the Tree-sitter support for Vim.

status:

This is version 0.2.1

This is alpha software, but I use it all the time for syntax highlighting and it has been behaving reliably for me.

This library is targeted at ‘vanilla’ Vim. It is doubtful that this could easily be made to work with Neovim and, in any case, Neovim already has fairly mature support for Tree-sitter.

Introduction

This library is a plugin for VPE.

Tree-sitter is an incremental parsing library that is intended to be fast enough to be integrated into text editors in order to provide syntax aware tooling. One obvious use is as a mechanism to implement syntax highlighting, which is the primary reason that this library exist; i.e. to support VPE_Syntax.

This library provides a mechanism to associate a Tree-sitter parse tree with any buffer that displays a supported language. Client code of this library can register to receive callbacks whenever the tree has been updated.

The following example demonstrates the main parts of the API. First tree-sitter must be enabled for a buffer.

import vpe
import vpe_sitter
from vpe import vim
from vpe_sitter.listen import Listener, ConditionCode, AffectedLines

# Try enabling tree-sitter for the current buffer.
if msg := vpe_sitter.treesit_current_buffer():
    # Not possible for this buffer. Most likely because the buffer's
    # filetype is not a supported language.
    vpe.error_msg(msg)
else:
    buf: vpe.Buffer = vim.current.buffer
    listener: Listener = buf.store('tree-sitter').listener

Multiple clients can do this, they will all share the same Tree-sitter Listener instance. The client will need to provide a callback function along the lines of:

def handle_tree_change(
        code: ConditionCode, affected_lines: AffectedLines) -> None:
    """Handle a tree change notification.

    The affected_lines parameter is a list of Python ranges.
    """
    match code:
        case ConditionCode.NEW_CLEAN_TREE:
            # A new tree that matches the buffer is available.

        case ConditionCode.NEW_OUT_OF_DATE_TREE:
            # A new tree is available, but the buffer changed during
            # parsing.

        case ConditionCode.PENDING_CHANGES:
            # Changes to the buffer have been detected and a new parse tree
            # will be generated shortly.

You can then register for callbacks using the Listener instance.

listener.add_parse_complete_callback(handle_tree_change)

Client code will typically have to use the generated Tree-sitter Tree instance, which is available as the Listener.tree property. See the Tree-sitter Tree documentation for the API available to client code. Be warned that this link points to the documentation for the latest Tree-sitter Python bindings, not the ones use by this library. Watch this space for online documentation that will define a version correct API for Tree object.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vpe_sitter-0.2.1.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vpe_sitter-0.2.1-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file vpe_sitter-0.2.1.tar.gz.

File metadata

  • Download URL: vpe_sitter-0.2.1.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for vpe_sitter-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f7cabbda6e43c07c5a70ee6b99e5d173747245117dca01c2bb23c80accaf9bb0
MD5 19b3ca19fcd7edb593a6949224d0ec37
BLAKE2b-256 1da4d4a0040cd1153b3a81d1fda2aa145856449891905aa27cfbe05999ed1224

See more details on using hashes here.

File details

Details for the file vpe_sitter-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: vpe_sitter-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for vpe_sitter-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 263eb9b372b6257cb304140ec0cf0a09dca616cfc6de83ce57aca7f578138bcb
MD5 36f2fb151b177a3135f84bc45c2ad5dd
BLAKE2b-256 d01af6fb4a9953152a69aad3508c35f92c85abd7f6e4e5150bfdc96d86df8e46

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page