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 inkts.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.
…which IMO could be documented better, specifically how
object_flags
andselect_mode
specify exactly what the object selector needs to do. ↩︎More can be added by adding an inline dependency to
kts.py
and editingget_parser_and_queries
function. ↩︎These operations failing if the cursor isn’t in a valid object is the “proper” Kakoune object behavior that the built-in ones obey. ↩︎