# Syntax highlighting

Language support such as tokenizing and highlighting is provided by [Tree-sitter](https://tree-sitter.github.io). Each language requires a Tree-sitter grammar to have tokenization and queries to provide additional features such as syntax highlighting.

## Grammars

To learn more about queries, see [Creating Parsers](https://tree-sitter.github.io/tree-sitter/creating-parsers).

Lapce has select few (Rust, JSON, TOML) grammars built-in for convenience. All other languages need to provide an externally loadable grammar in a form of shared library. Lapce will update all supported grammars by default on start-up.

Grammars are stored in `$LOCAL_DATA_DIR/grammars` directory. Each grammar should be named `${DLL_PREFIX}tree-sitter-${NAME}.${DLL_SUFFIX}` where `${NAME}` refers to language name and `${DLL_PREFIX}`/`${DLL_SUFFIX}` refer to OS defined constants for shared libraries (see [`DLL_PREFIX`](https://doc.rust-lang.org/std/env/consts/constant.DLL_PREFIX.html) and [`DLL_EXTENSION`](https://doc.rust-lang.org/std/env/consts/constant.DLL_EXTENSION.html)).

Grammars are loaded in order:

1. User provided grammar
2. System provider grammar
3. Built-in grammar

## Queries

To learn more about queries, see [Syntax Highlighting#Queries](https://tree-sitter.github.io/tree-sitter/syntax-highlighting#queries).

Queries are stored in `$SETTINGS_DIR/queries/$LANGUAGE`. Currently only `highlights.scm` are supported.

## Language

For Lapce to know how to deal with files, it's required to include a language definition which contains which filenames or extensions to match to language, what indent or comment type it uses and additional configuration (that depends on tree-sitter) such as code lens (unrelated to LSP code lens) or sticky headers tokens.

Languages are defined in [`lapce-core/src/language.rs`](https://github.com/lapce/lapce/blob/master/lapce-core/src/language.rs).

## Language Server Protocol

LSP providers should define `DocumentSelector` via the extension API and/or through `volt.toml` extension manifest. It can be otherwise defined through `language_id_from_path` in [`lapce-proxy/src/buffer.rs`](https://github.com/lapce/lapce/blob/master/lapce-proxy/src/buffer.rs).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lapce.dev/features/syntax-highlighting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
