|
| 1 | +# Writing tree-sitter queries for ad |
| 2 | + |
| 3 | +### Overview |
| 4 | + |
| 5 | +`ad` currently supports a subset of the tree-sitter based syntax highlighting you may be familiar |
| 6 | +with from other text editors. The configuration options in your `config.toml` under the `tree_sitter` |
| 7 | +key will direct `ad` to search for scheme highlights files under a given directory for the languages |
| 8 | +that you configure. These query files can, for the most part, be copied from other editors but there |
| 9 | +are a couple of caveats that you should keep in mind. |
| 10 | + |
| 11 | + 1. Support for custom predicates (such as neovim's `#lua-match?` and `#contains?` is not provided. |
| 12 | + Queries using unsupported predicates will be rejected, resulting in tree-sitter not being enabled |
| 13 | + for the language in question. |
| 14 | + 2. ad follows the same precedence approach as neovim for overlapping queries, with the _last_ match |
| 15 | + being used in place of previous matches. When writing your `highlights.scm` file you should place |
| 16 | + more specific rules towards the end of the file and more general rules towards the start. |
| 17 | + 3. ad does not currently provide any support for indentation, injections, locals or folds. So the |
| 18 | + resulting highlights you obtain may not exactly match what you are used to from another editor if |
| 19 | + you are copying over existing queries. |
| 20 | + |
| 21 | + |
| 22 | +### Getting started |
| 23 | + |
| 24 | +ad provides some support for tree-sitter parsers that I have been able to test and verify as part of my |
| 25 | +local setup. For the most part they are adapted from the ones found in [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter/tree/master/queries) |
| 26 | +with the the modigications outlined below. If you are testing your own queries it is often better to start |
| 27 | +with a subset of the `highlights.scm` file from another editor and incrementally add in more queries |
| 28 | +while verifying that the results in ad look the way that you expect. |
| 29 | + |
| 30 | +Beyond that simple advice, the tree-sitter documentation on writing queries is the best place to start |
| 31 | +if you are looking to configure your own custom queries: |
| 32 | + https://tree-sitter.github.io/tree-sitter/using-parsers/queries/index.html |
| 33 | + |
| 34 | + |
| 35 | +### Troubleshooting |
| 36 | + |
| 37 | +- **Where are my highlights that I configured?** |
| 38 | + - First make sure that you don't have any typos in your config (we all do it) and that the highlighting |
| 39 | + provided in the ad repo is working correctly. |
| 40 | + - If you are sure that you have things set up correctly then try running `:view-logs` to see if there |
| 41 | + are any warnings about the queries that you are using. You should see a log line asserting that the |
| 42 | + tree-sitter parser has been initialised, and if not there should be an error message explaining why |
| 43 | + setup failed. |
| 44 | + |
| 45 | +- **What is this warning about unsupported predicates?** |
| 46 | + - A common feature of tree-sitter queries is the use of [predicates](https://tree-sitter.github.io/tree-sitter/using-parsers/queries/3-predicates-and-directives.html) |
| 47 | + to conditionally select nodes in the tree based on their content as well as their type. Tree-sitter |
| 48 | + supports providing and running custom predicates but it is up to the program running the query to |
| 49 | + provide the implementation of how custom predicates are applied. `neovim` and other editors tend to |
| 50 | + make extensive use of custom predicates that are not supported inside of ad. If a query you are |
| 51 | + using contains any unsupported predicates it will be rejected with warning logs listing the predicates |
| 52 | + that were found. |
| 53 | + - Typically, you can replace `#lua-match?` with the tree-sitter built-in `#match?` predicate and have |
| 54 | + it produce the expected result. |
| 55 | + - You can also replace `#contains?` with `#match? .. ".*$contained_string.*"` |
| 56 | + |
| 57 | +- **Why are comments not rendering with the correct styling?** |
| 58 | + - Comment queries from neovim frequently have a secondary `@spell` tag associated with them that ad |
| 59 | + will end up using as the tag for the match (in place of, for example, `@comment`). If you remove |
| 60 | + the `@spell` tag from your query you should see your comments receive the correct highlighting. |
0 commit comments