summaryrefslogtreecommitdiff
path: root/book
diff options
context:
space:
mode:
authorGokul Soumya2021-10-23 02:41:19 +0000
committerGitHub2021-10-23 02:41:19 +0000
commit4ee92cad19cc94f0751f91fa9391d1899353d740 (patch)
tree794be048905f5d5026ba1968dc0152d12473c024 /book
parentc5298caa752dee136ab1a21dae27a702a00d8eea (diff)
Add treesitter textobjects (#728)
* Add treesitter textobject queries Only for Go, Python and Rust for now. * Add tree-sitter textobjects Only has functions and class objects as of now. * Fix tests * Add docs for tree-sitter textobjects * Add guide for creating new textobject queries * Add parameter textobject Only parameter.inside is implemented now, parameter.around will probably require custom predicates akin to nvim' `make-range` since we want to select a trailing comma too (a comma will be an anonymous node and matching against them doesn't work similar to named nodes) * Simplify TextObject cell init
Diffstat (limited to 'book')
-rw-r--r--book/src/SUMMARY.md2
-rw-r--r--book/src/guides/README.md4
-rw-r--r--book/src/guides/textobject.md30
-rw-r--r--book/src/usage.md13
4 files changed, 46 insertions, 3 deletions
diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md
index 3fa8e067..56f50e21 100644
--- a/book/src/SUMMARY.md
+++ b/book/src/SUMMARY.md
@@ -8,3 +8,5 @@
- [Keymap](./keymap.md)
- [Key Remapping](./remapping.md)
- [Hooks](./hooks.md)
+- [Guides](./guides/README.md)
+ - [Adding Textobject Queries](./guides/textobject.md)
diff --git a/book/src/guides/README.md b/book/src/guides/README.md
new file mode 100644
index 00000000..96e62978
--- /dev/null
+++ b/book/src/guides/README.md
@@ -0,0 +1,4 @@
+# Guides
+
+This section contains guides for adding new language server configurations,
+tree-sitter grammers, textobject queries, etc.
diff --git a/book/src/guides/textobject.md b/book/src/guides/textobject.md
new file mode 100644
index 00000000..50b3b574
--- /dev/null
+++ b/book/src/guides/textobject.md
@@ -0,0 +1,30 @@
+# Adding Textobject Queries
+
+Textobjects that are language specific ([like functions, classes, etc][textobjects])
+require an accompanying tree-sitter grammar and a `textobjects.scm` query file
+to work properly. Tree-sitter allows us to query the source code syntax tree
+and capture specific parts of it. The queries are written in a lisp dialect.
+More information on how to write queries can be found in the [official tree-sitter
+documentation](tree-sitter-queries).
+
+Query files should be placed in `runtime/queries/{language}/textobjects.scm`
+when contributing. Note that to test the query files locally you should put
+them under your local runtime directory (`~/.config/helix/runtime` on Linux
+for example).
+
+The following [captures][tree-sitter-captures] are recognized:
+
+| Capture Name |
+| --- |
+| `function.inside` |
+| `function.around` |
+| `class.inside` |
+| `class.around` |
+| `parameter.inside` |
+
+[Example query files][textobject-examples] can be found in the helix GitHub repository.
+
+[textobjects]: ../usage.md#textobjects
+[tree-sitter-queries]: https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax
+[tree-sitter-captures]: https://tree-sitter.github.io/tree-sitter/using-parsers#capturing-nodes
+[textobject-examples]: https://github.com/search?q=repo%3Ahelix-editor%2Fhelix+filename%3Atextobjects.scm&type=Code&ref=advsearch&l=&l=
diff --git a/book/src/usage.md b/book/src/usage.md
index 2de8d01a..d31e03a1 100644
--- a/book/src/usage.md
+++ b/book/src/usage.md
@@ -51,9 +51,10 @@ Multiple characters are currently not supported, but planned.
## Textobjects
-Currently supported: `word`, `surround`.
+Currently supported: `word`, `surround`, `function`, `class`, `parameter`.
![textobject-demo](https://user-images.githubusercontent.com/23398472/124231131-81a4bb00-db2d-11eb-9d10-8e577ca7b177.gif)
+![textobject-treesitter-demo](https://user-images.githubusercontent.com/23398472/132537398-2a2e0a54-582b-44ab-a77f-eb818942203d.gif)
- `ma` - Select around the object (`va` in vim, `<alt-a>` in kakoune)
- `mi` - Select inside the object (`vi` in vim, `<alt-i>` in kakoune)
@@ -62,5 +63,11 @@ Currently supported: `word`, `surround`.
| --- | --- |
| `w` | Word |
| `(`, `[`, `'`, etc | Specified surround pairs |
-
-Textobjects based on treesitter, like `function`, `class`, etc are planned.
+| `f` | Function |
+| `c` | Class |
+| `p` | Parameter |
+
+Note: `f`, `c`, etc need a tree-sitter grammar active for the current
+document and a special tree-sitter query file to work properly. [Only
+some grammars](https://github.com/search?q=repo%3Ahelix-editor%2Fhelix+filename%3Atextobjects.scm&type=Code&ref=advsearch&l=&l=)
+currently have the query file implemented. Contributions are welcome !