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.1.0

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 demonstrate 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.1.0.tar.gz (15.9 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.1.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: vpe_sitter-0.1.0.tar.gz
  • Upload date:
  • Size: 15.9 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.1.0.tar.gz
Algorithm Hash digest
SHA256 ea77cce419603565a4db015a810717e6757220427e052649309e1f3ceb5bb779
MD5 fb92535ee0645f977d6d5dbfe463c32a
BLAKE2b-256 d29e3e307d73d8bb7031b13b080d6d569cbd8e51cbd6cd1178e2f144222a10ed

See more details on using hashes here.

File details

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

File metadata

  • Download URL: vpe_sitter-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f92aa98a9abb371b136efc27de17170a8df901eadcde3e5e1f346f5af49efde
MD5 9fc77cf00e7776b3067b8bca578f9c81
BLAKE2b-256 25b934898f619ea2f87836dbbd1db3c5c2540f85998a18118c0db604cbd8ac9e

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