# Simple tree-sitter object selections

**URL:** <https://discuss.kakoune.com/t/simple-tree-sitter-object-selections/2619>\
**Category:** Plugins\
**Tags:** language\
**Created:** [January 12, 2025, 6:24am UTC](https://discuss.kakoune.com/t/simple-tree-sitter-object-selections/2619 "2025-01-12T06:24:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bravekarma](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@bravekarma](https://discuss.kakoune.com/u/bravekarma)\
**Post date:** [January 12, 2025, 6:24am UTC](https://discuss.kakoune.com/t/simple-tree-sitter-object-selections/2619/1 "2025-01-12T06:24:46Z")

</div>

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](https://github.com/caksoylar/dotfiles/tree/main/kak/autoload/ts-objects). 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.

---

<div class="post-metadata">

**Author:** ![phaazon](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/phaazon/32/1189_2.png) [@phaazon](https://discuss.kakoune.com/u/phaazon)\
**Post date:** [January 23, 2025, 7:06pm UTC](https://discuss.kakoune.com/t/simple-tree-sitter-object-selections/2619/2 "2025-01-23T19:06:22Z")

</div>

This is already implemented in [KTS](https://sr.ht/~hadronized/kak-tree-sitter). Is there anything there that you think is missing?

---

<div class="post-metadata">

**Author:** ![bravekarma](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@bravekarma](https://discuss.kakoune.com/u/bravekarma)\
**Post date:** [January 28, 2025, 10:53pm UTC](https://discuss.kakoune.com/t/simple-tree-sitter-object-selections/2619/3 "2025-01-28T22:53:38Z")

</div>

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.
