Repository navigation
Add a highlight sub-crate and expose its functionality through the CLI - #283
Conversation
| source: &'a [u8], | ||
| language: Language, | ||
| property_sheet: &'a PropertySheet<Properties>, | ||
| injection_callback: &'a F, |
There was a problem hiding this comment.
The highlighting functionality is mostly exposed through this highlight function and the highlight_html wrapper.
They both take an injection_callback parameter that specifies how to retrieve other 'embedded' languages. This callback is necessary because that language retrieval works differently in different contexts. In the Tree-sitter CLI, the languages are loaded lazily and dynamically using dlopen, whereas in other apps, the languages may be statically linked into the app.
| language: Language, | ||
| property_sheet: &'a PropertySheet<Properties>, | ||
| injection_callback: &'a F1, | ||
| attribute_callback: &'a F2, |
There was a problem hiding this comment.
The highlight_html function also takes a second callback - attribute_callback that specifies how to render a scope as HTML. In the case of the CLI, it uses an inline style attribute, whereas other apps may want to map scopes to their own classes.
| fs::create_dir_all(&config_dir).unwrap(); | ||
| let mut loader = Loader::new(config_dir); | ||
| // TODO - make configurable | ||
| let parser_repo_paths = vec![home_dir.join("github")]; |
There was a problem hiding this comment.
Here I've hard coded the CLI to look for parsers in the ~/github folder. I could probably allow this list of paths to be specified in a config file in the ~/.tree-sitter folder instead. I just haven't decided how I want that config file to look yet.
| "string": 28, | ||
| "tag": {"color": 18}, | ||
| "variable.builtin": {"bold": true} | ||
| } |
There was a problem hiding this comment.
The CLI will load an optional theme file at ~/.tree-sitter/theme.json that looks like this. The keys are the scopes, and the values are ansi color numbers.
|
I published this to crates.io as |
|
Neato! If I'm looking at this right we should be able to drop all Tree-Sitter specific highlight code from Treelights and depend directly on this crate now. Is that correct? |
|
@vmg Yeah, exactly. I've just opened a PR to do that. |
When there are embedded documents, multiple scopes can start or end at the same position. Previously, there was no guarantee that the ScopeEnd events would always occur in the reverse order of the ScopeStart events. The easiest way to avoid exposing inconsistency is to not surface the scopes being ended.
This PR adds a new top-level directory containing a rust crate called
tree_sitter_highlight. This crate provides functionality to perform syntax highlighting using a Tree-sitter parser. I've also added ahighlightsubcommand to the Tree-sitter CLI, which uses this new crate to perform syntax highlighting at the command line.Library
The
tree_sitter_highlightcrate exposes two public functions:highlightreturns an iterator that yieldsScopeStartandScopeEndvalues, which can be used to render any kind of syntax highlighting.highlight_htmlreturns a vector of HTML strings - one for each line in the document.CLI
The
tree-sitter highlightcommand renders ANSI colors to the terminal.Screenshot (note the HTML within JavaScript within HTML):
A few steps are required in order for this command to find parsers on your file system:
The parsers must be in a well-known location, with a directory name that matches a well-known pattern. Currently, they must match the glob pattern:
~/github/tree-sitter-*. It'll be pretty easy to make this configurable.The parsers must have some new fields in their
package.jsonfile. Here's an example. The new fields are partly based on how Atom grammars works, documented here.The important new fields are:
file-types- An array of filename suffixes. The grammar will be used for files whose names end with one of these suffixes. Note that the suffix may be an entire filename.highlights- The relative path to a compiled Property Sheet that should be used for syntax highlighting.injection-regex- An optional regex that is used to determine when this language should be used to parse a portion of a larger document. The regex will be tested against the text of a syntax node which is identified by theinjection-languageproperty of a node in the parent document. More docs on this to come.