Simple tree-sitter object selections

A couple months ago I had the urge to write a script to implement tree-sitter object selections in Kakoune, similar to what Helix supports out of the box. It was a good exercise to become familiar with tree-sitter and some intricacies on how adding custom objects to Kakoune works[1].

I didn’t make it a plugin because it is simple and I want people to inspect and adjust it rather than consuming it in a packaged way. It is available in my (UNLICENSEd) dotfiles at dotfiles/kak/autoload/ts-objects at main · caksoylar/dotfiles · GitHub. It only requires pipx for seamlessly running a Python script with inline tree-sitter dependencies, and by default supports c, python, yaml and json[2]. The queries are fetched from Helix repo right now, using the get_queries.sh script.

Since the code is relatively simple, there are a few other reasons you might want to have a look besides wanting to use tree-sitter object selections:

  • You want to see how object mode logic can be implemented (see get_new_selection definition and usage in kts.py)
  • You want to see how simple tree-sitter queries and captures work

As a bonus, also see the tsobj_loose option which if set, makes the object queries find the next valid object rather than failing[3]. I find this less strict mode more useful in practice.


  1. …which IMO could be documented better, specifically how object_flags and select_mode specify exactly what the object selector needs to do. ↩︎

  2. More can be added by adding an inline dependency to kts.py and editing get_parser_and_queries function. ↩︎

  3. These operations failing if the cursor isn’t in a valid object is the “proper” Kakoune object behavior that the built-in ones obey. ↩︎

2 Likes

This is already implemented in KTS. Is there anything there that you think is missing?

I haven’t tried KTS properly (personal reasons that can be skill issue: can’t find a binary release, don’t want to install a Rust toolchain, got confused going between GH/Sourcehut etc.), so not sure.

The main reason is I wanted to learn, and Python performance isn’t an issue because it isn’t a syntax highlighter.

Like I say above, this isn’t a plugin that I am intending to maintain for others, KTS can be that. It’s a proof-of-concept that I will use and maybe someone else can read as a reference.

1 Like