syntastica
Modern and easy syntax highlighting using tree-sitter
Note: If viewing this file on GitHub or crates.io, some links might not be working. Go to the custom docs page or the docs.rs page instead, which additionally include the Features section.
Overview
To use syntastica, you probably want to depend on three crates:
- The main
syntasticacrate for all the logic. - A parser collection to provide language support (see parser collections)
- The theme collection for some default themes (see theme collection)
So for example:
= "<version>"
= { = "<version>", = ["some"] }
= "<version>"
Use cases
syntastica has three main ways of highlighting code, for three different use
cases:
- Highlight one input exactly once: see [
highlight] and this example - Highlight one input multiple times (e.g. with different themes or
renderers): see [
Processor::process_once], [render], and this example - Highlight multiple different inputs any number of times: see
[
Processor], [render], and this example
Subprojects
Besides the main syntastica crate, many other crates for different purposes
were developed and are included in the repository. This section aims to provide
a good overview.
Parser collections
The main syntastica crate provides no tree-sitter parsers and queries by
itself. However, the project does provide three different parser collections
with different advantages and drawbacks each. All three collections depend on
syntastica-queries for the tree-sitter queries. Choose
one, and add it as a dependency next to syntastica itself.
All three parser collections also provide the same public API and provide
features for all supported languages, as well as the three feature groups
some, most, and all. Take a look at the respective crate documentation for
more information.
If you want to additionally use languages that are not in any of these parser collections, one approach is shown in the custom languages example.
syntastica-parsersis probably the easiest to start with. It uses parsers from crates.io. This has the main benefit of being well integrated in the cargo ecosystem. However, many tree-sitter parsers do not get published to crates.io, and those that are, are usually very outdated. Thus, this collection is relatively limited.- syntastica-parsers-git
is probably the best choice overall. It contains all supported languages, and
when WebAssembly compilation will be supported, this will be the
collection to use. It pulls pinned revisions of parser git repositories in the
build script and links to the C and C++ parser sources. As such, it does not
depend on the upstream parsers to have up-to-date Rust bindings. However, this
way of fetching the parsers requires the
gitcommand to be accessible and internet access during compilation, which may not be desirable. Additionally, compilation can take very long, because there is no clean way to cache the fetched repositories between builds. syntastica-parsers-gitdepis a mix of both of the above. It uses cargo git dependencies to fetch the parser repositories and depends on a remote Rust binding (which is why not all parsers are included). The main disadvantages are that this collection cannot be published to crates.io, because it depends on crates that are not on crates.io (namely the parsers). This means, to use it you must also depend on it using a git dependency, which in turn forbids your crate to be published on crates.io. Unlikesyntastica-parsers-githowever, the parsers only need to be fetched once by cargo, and following builds will be much faster.
Theme collection
To render highlighted code to end users, a
theme is needed, which specifies the colors to use for
which theme key. The syntastica project comes with a
separate crate containing a few default themes:
syntastica-themes.
If you wish to create your own theme, have a look at the
custom theme example and the documentation for the
[theme!] macro.
Crates for internal use
The syntastica repository/workspace also includes some crates which are not
meant for outside use, but are instead used internally. These are listed below.
Note: There are no guarantees about the public API of these crates! If, for any reason, you have to depend on one of them, then pin the exact version using
<crate> = "=<version>".
syntastica-coredefines types, traits, constants, etc. which are used in multiple of the other crates. The mainsyntasticacrate re-exports all those items transparently, so that external projects only need a dependency on that. The items are defined insyntastica-corehowever, to avoid cyclic (dev-)dependencies inside this workspace.syntastica-macrosdefines procedural macros for use exclusively inside this workspace. This crate allows the list of languages/parsers to be in one combinedlanguages.tomlfile, and the different macros are used in the different places where this list needs to be referenced.syntastica-highlightis a fork oftree-sitter-highlight, which is adjusted and trimmed down for the use insyntastica. It contains the main highlighting logic.- syntastica-queries
is a collection of tree-sitter queries for all supported languages. It is
marked as "for internal use", because all three
parser collections depend on this crate and expose the
queries through their implementation of
LanguageSet. Unlike the previous crates in this list however, you may actually want to depend on this crate yourself, if you only need the queries.
General side-products
This list includes crates which were developed for syntastica but have no
direct association with the main project and can be used completely separately.
rsexpris a generic S-expression parser with added support for square-brackets, strings, and comments. Additionally, the parsed S-expressions can be pretty-printed to provide a uniform formatting. Seedprint-plugin-sexprfor more information on using this as a formatter. Insyntasticathis crate is used for parsing (and formatting) the tree-sitter queries in thequeriesdirectory. These are processed bycargo xtask codegen queriesand result in the queries inside thegenerated_queriesdirectory, which are the ones that are bundled withsyntastica-queries.lua-patternis a parser for Lua patterns. These are similar to regular expressions, but generally more limited. The crate also provides a best-effort conversion to regular expression strings. Insyntasticathis is used, as many of the source queries are forked from nvim-treesitter which makes heavy use of#lua-match?predicates for matching with Lua patterns. The official tree-sitter Rust bindings do not support Lua pattern matching however (obviously), which is why during the processing of the queries (withcargo xtask codegen queries), all Lua patterns are replaced with regular expressions using this crate.syntastica-query-preprocessoris a pre-processor for tree-sitter queries which allows usage of; inherits <lang>comments, conditional skipping of nodes with comments, usage of additional predicates likelua-match?,contains?andany-of?, Neovim's old injections syntax, and order reversing for priority flipping. The crate can be used to use queries designed for Neovim with the official tree-sitter Rust bindings with minimal manual changes. Despite havingsyntasticain the name, the crate can be used externally and does not depend on any of the othersyntastica-crates. Insyntasticait is used in thecodegen queriesxtask, because many of the queries are forked from nvim-treesitter, and to adjust the queries for older parser versions from crates.io.
WebAssembly support
TODO: WebAssembly support
Examples
This section contains some basic usage examples. More specific examples can be
found in the documentation of some items such as the [Processor] type or the
[render] function. Additionally, the
examples
directory contains a few complete examples.
This is the list of examples found here:
- Highlight once
- Highlight the same input multiple times
- Highlight multiple different inputs
- Detect the language based on a file type
- Specify a custom theme
Example: highlight once
This example shows the easiest and quickest way to use syntastica. See the
section about use cases for when it is appropriate to use
syntastica this way.
use TerminalRenderer;
use ;
let output = highlight
.unwrap_or_else;
println!;
Example: highlight the same input multiple times
This example shows how to render the same input with two different themes using two different renderers.
use ;
use ;
// process the input once, but store the raw highlight information
let highlights = process_once
.unwrap_or_else;
// render the highlights to the terminal using the
// gruvbox dark theme on a dark gray background
println!;
// render the same input to HTML using the onelight theme
let html = render;
// you could for example write that to a file called `index.html`:
// std::fs::write("index.html", html).unwrap();
Example: highlight multiple different inputs
This example shows how a [Processor] can be reused if multiple different
inputs should be highlighted.
use ;
use ;
// create a language set and a `Processor`
let language_set = new;
let mut processor = new;
// Note: `language_set` has to be stored in a variable, because the processor
// is bound to the lifetime of the reference passed to `new`
// process some input
let highlights_rust = processor.process
.unwrap_or_else;
// process some other input in another language
let highlights_js = processor.process
.unwrap_or_else;
// render the rust code to the terminal using the
// gruvbox dark theme on a dark gray background
println!;
// render the same rust code to HTML using the onelight theme
let html = render;
// you could for example write that to a file called `index.html`:
// std::fs::write("index.html", html).unwrap();
// now render the javascript code to the terminal using the
// onedark theme and no background color
println!;
Example: detect language from file type
This is an alteration of the first example showing how to detect the language to use based on a file type. See that first example for explanations of the rest of the code.
syntastica uses tft for file types which
provides automatic detection.
use ;
use ;
// detect the file type given a file's path and content.
// this requires a dependency on `tft`
let ft = detect;
let language_set = new;
let output = highlight
.unwrap_or_else;
println!;
Example: custom theme
This is an alteration of the first example showing
how to create a simple custom theme. See that first example for explanations of
the rest of the code, and see the documentation of the [theme!] macro for more
information.
use ;
use ;
let theme = theme! ;
let output = highlight
.unwrap_or_else;
println!;
Versioning
All crates in this workspace whose names start with syntastica share the same
version. The typical semantic versioning rules are used across the public APIs
of all of these, except for
the ones listed as internal. The
other crates in this workspace have their own separate
versions.
Versions are specified as MAJOR.MINOR.PATCH. As long as the MAJOR version
specifier is still at 0, changes to the MINOR version may also be breaking
changes. The PATCH part is only incremented if the public API stays exactly
the same.
Inspiration
TODO: shortly explain origins (lirstings)
TODO
- easy compilation to WebAssembly using
tree-sitter-c2rust