Skip to content

Commit

Permalink
Merge pull request #213 from trishume/release-v3.0
Browse files Browse the repository at this point in the history
Release v3.0
  • Loading branch information
trishume committed Oct 9, 2018
2 parents 51208d3 + cdd0555 commit 043a679
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 47 deletions.
7 changes: 0 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ cache:

script:
- cargo build
- |
if [ "$TRAVIS_OS_NAME" == "linux" ]
then
p=$(cd ./target/debug/build/onig_sys-*/out/lib/ && pwd)
echo "adding $p to linker path"
export LD_LIBRARY_PATH="${p}:${LD_LIBRARY_PATH}"
fi
- cargo test
- make assets
- make syntest
Expand Down
54 changes: 54 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Version 3.0

This is a major release with multiple breaking API changes, although upgrading shouldn't be too difficult. It fixes bugs and comes with some nice new features.

## Breaking changes and upgrading

- The `SyntaxSet` API has been revamped to use a builder and an arena of contexts. See [example usage](https://github.com/trishume/syntect/blob/51208d35a6d98c07468fbe044d5c6f37eb129205/examples/gendata.rs#L25-L28).
- Many functions now need to be passed the `SyntaxSet` that goes with the rest of their arguments because of this new arena.
- Filename added to `LoadingError::ParseSyntax`
- Many functions in the `html` module now take the `newlines` version of syntaxes.
- These methods have also been renamed, partially so that code that needs updating doesn't break without a compile error.
- The HTML they output also treats newlines slightly differently and I think more correctly but uglier when you look at the HTML.

#### Breaking rename upgrade guide

- `SyntaxSet::add_syntax -> SyntaxSetBuilder::add`
- `SyntaxSet::load_syntaxes -> SyntaxSetBuilder::add_from_folder`
- `SyntaxSet::load_plain_text_syntax -> SyntaxSetBuilder::add_plain_text_syntax`
- `html::highlighted_snippet_for_string -> html::highlighted_html_for_string`: also change to `newlines` `SyntaxSet`
- `html::highlighted_snippet_for_file -> html::highlighted_html_for_file`: also change to `newlines` `SyntaxSet`
- `html::styles_to_coloured_html -> html::styled_line_to_highlighted_html`: also change to `newlines` `SyntaxSet`
- `html::start_coloured_html_snippet -> html::start_highlighted_html_snippet`: return type also changed

## Major changes and new features

- Use arena for contexts (#182 #186 #187 #190 #195): This makes the code cleaner, enables use of syntaxes from multiple threads, and prevents accidental misuse.
- This involves a new `SyntaxSetBuilder` API for constructing new `SyntaxSet`s
- See the revamped [parsyncat example](https://github.com/trishume/syntect/blob/51208d35a6d98c07468fbe044d5c6f37eb129205/examples/parsyncat.rs).
- Encourage use of newlines (#197 #207 #196): The `nonewlines` mode is often buggy so we made it easier to use the `newlines` mode.
- Added a `LinesWithEndings` utility for iterating over the lines of a string with `\n` characters.
- Reengineer the `html` module to use `newlines` syntaxes.
- Add helpers for modifying highlighted lines (#198): For use cases like highlighting a piece of text in a blog code snippet or debugger. This allows you to reach into the highlighted spans and add styles.
- Check out `split_at` and `modify_range` in the `util` module.
- New `ThemeSet::add_from_folder` function (#200): For modifying existing theme sets.

## Bug Fixes

- Improve nonewlines regex rewriting: #212 #211
- Reengineer theme application to match Sublime: #209
- Also mark contexts referenced by name as "no prototype" (same as ST): #180
- keep with_prototype when switching contexts with `set`: #177 #166
- Fix unused import warning: #174
- Ignore trailing dots in selectors: #173
- Fix `embed` to not include prototypes: #172 #160

## Upgraded dependencies

- plist: `0.2 -> 0.3`
- regex: `0.2 -> 1.0`
- onig: `3.2.1 -> 4.1`

# Prior versions

See the Github release notes: <https://github.com/trishume/syntect/releases>
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ keywords = ["syntax", "highlighting", "highlighter", "colouring", "parsing"]
categories = ["parser-implementations", "parsing", "text-processing"]
readme = "Readme.md"
license = "MIT"
version = "2.1.0" # remember to update html_root_url
version = "3.0.0" # remember to update html_root_url
authors = ["Tristan Hume <tristan@thume.ca>"]
exclude = [
"testdata/*",
Expand Down
73 changes: 38 additions & 35 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,23 @@

`syntect` is a syntax highlighting library for Rust that uses [Sublime Text syntax definitions](http://www.sublimetext.com/docs/3/syntax.html#include-syntax). It aims to be a good solution for any Rust project that needs syntax highlighting, including deep integration with text editors written in Rust. It's used in production by at least two companies, and by [many open source projects](#projects-using-syntect).

If you are writing a text editor (or something else needing highlighting) in Rust and this library doesn't fit your needs, I consider that a bug and you should file an issue or email me.
If you are writing a text editor (or something else needing highlighting) in Rust and this library doesn't fit your needs, I consider that a bug and you should file an issue or email me. I consider this project mostly complete, I still maintain it and review PRs, but it's not under heavy development.

**Note:** I consider this project "done" in the sense that it works quite well for its intended purpose, accomplishes the major goals I had, and I'm unlikely to make any sweeping changes.
I won't be committing much anymore because the marginal return on additional work isn't very high. Rest assured if you submit PRs I will review them and likely merge promptly.
I'll also quite possibly still fix issues and definitely offer advice and knowledge on how the library works. Basically I'll be maintaining the library but not developing it further.
I've spent months working on, tweaking, optimizing, documenting and testing this library. If you still have any reasons you don't think it fits your needs, file an issue or email me.
## Important Links

### Rendered docs: <https://docs.rs/syntect>
- API docs with examples: <https://docs.rs/syntect>
- [Changelogs and upgrade notes for past releases](https://github.com/trishume/syntect/releases)

## Getting Started

`syntect` is [available on crates.io](https://crates.io/crates/syntect). You can install it by adding this line to your `Cargo.toml`:

```toml
syntect = "2.1"
syntect = "3.0"
```

After that take a look at the [documentation](https://docs.rs/syntect) and the [examples](https://github.com/trishume/syntect/tree/master/examples).

**Note:** with stable Rust on Linux there is a possibility you might have to add `./target/debug/build/onig_sys-*/out/lib/` to your `LD_LIBRARY_PATH` environment variable. I dunno why or even if this happens on other places than Travis, but see `travis.yml` for what it does to make it work. Do this if you see `libonig.so: cannot open shared object file`.

If you've cloned this repository, be sure to run

```
Expand All @@ -36,17 +32,10 @@ git submodule update --init

to fetch all the required dependencies for running the tests.

### Feature Flags

Syntect makes heavy use of [cargo features](http://doc.crates.io/manifest.html#the-features-section), to support users who require only a subset of functionality. In particular, it is possible to use the highlighting component of syntect without the parser (for instance when hand-rolling a higher performance parser for a particular language), by adding `default-features = false` to the syntect entry in your `Cargo.toml`.

For more information on available features, see the features section in `Cargo.toml`.

## Features/Goals

- [x] Work with many languages (accomplished through using existing grammar formats)
- [x] Highlight super quickly, faster than every editor except Sublime Text 3
- [x] Load up quickly, currently in around 23ms but could potentially be even faster.
- [x] Highlight super quickly, faster than nearly all text editors
- [x] Include easy to use API for basic cases
- [x] API allows use in fancy text editors with piece tables and incremental re-highlighting and the like.
- [x] Expose internals of the parsing process so text editors can do things like cache parse states and use semantic info for code intelligence
Expand All @@ -55,6 +44,7 @@ For more information on available features, see the features section in `Cargo.t
- [x] Well documented, I've tried to add a useful documentation comment to everything that isn't utterly self explanatory.
- [x] Built-in output to coloured HTML `<pre>` tags or 24-bit colour ANSI terminal escape sequences.
- [x] Nearly complete compatibility with Sublime Text 3, including lots of edge cases. Passes nearly all of Sublime's syntax tests, see [issue 59](https://github.com/trishume/syntect/issues/59).
- [x] Load up quickly, currently in around 23ms but could potentially be even faster.

## Screenshots

Expand All @@ -65,22 +55,29 @@ There's currently an example program called `syncat` that prints one of the sour
![Solarized Light](http://i.imgur.com/l3zcO4J.png)
![InspiredGithub](http://i.imgur.com/a7U1r2j.png)

## Roadmap

- [x] Sketch out representation of a Sublime Text syntax
- [x] Parse `.sublime-syntax` files into the representation.
- [x] Write an interpreter for the `.sublime-syntax` state machine that highlights an incoming iterator of file lines into an iterator of scope-annotated text.
- [x] Parse TextMate/Sublime Text theme files
- [x] Highlight a scope-annotated iterator into a colour-annotated iterator for display.
- [x] Ability to dump loaded packages as binary file and load them with lazy regex compilation for fast start up times.
- [x] Bundle dumped default syntaxes into the library binary so library users don't need an assets folder with Sublime Text packages.
- [x] Add nice API wrappers for simple use cases. The base APIs are designed for deep high performance integration with arbitrary text editor data structures.
- [x] Document the API better and make things private that don't need to be public
- [x] Detect file syntax based on first line
- [x] Make it really fast (mosty two hot-paths need caching, same places Textmate 2 caches)
- [ ] Make syncat a better demo, and maybe more demo programs
- [ ] Add sRGB colour correction (not sure if this is necessary, could be the job of the text editor)
- [ ] Add C bindings so it can be used as a C library from other languages.
## Example Code

Prints highlighted lines of a string to the terminal. See the [easy](https://docs.rs/syntect/latest/syntect/easy/index.html) and [html](https://docs.rs/syntect/latest/syntect/html/index.html) module docs for more basic use case examples.

```rust
use syntect::easy::HighlightLines;
use syntect::parsing::SyntaxSet;
use syntect::highlighting::{ThemeSet, Style};
use syntect::util::{as_24_bit_terminal_escaped, LinesWithEndings};

// Load these once at the start of your program
let ps = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();

let syntax = ps.find_syntax_by_extension("rs").unwrap();
let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}";
for line in LinesWithEndings::from(s) {
let ranges: Vec<(Style, &str)> = h.highlight(line, &ps);
let escaped = as_24_bit_terminal_escaped(&ranges[..], true);
println!("{}", escaped);
}
```

## Performance

Expand Down Expand Up @@ -110,7 +107,13 @@ All measurements were taken on a mid 2012 15" retina Macbook Pro.
- ~1.9ms to parse and highlight the 30 line 791 character `testdata/highlight_test.erb` file. This works out to around 16,000 lines/second or 422 kilobytes/second.
- ~250ms end to end for `syncat` to start, load the definitions, highlight the test file and shut down. This is mostly spent loading.

### Caching
## Feature Flags

Syntect makes heavy use of [cargo features](http://doc.crates.io/manifest.html#the-features-section), to support users who require only a subset of functionality. In particular, it is possible to use the highlighting component of syntect without the parser (for instance when hand-rolling a higher performance parser for a particular language), by adding `default-features = false` to the syntect entry in your `Cargo.toml`.

For more information on available features, see the features section in `Cargo.toml`.

## Caching

Because `syntect`'s API exposes internal cacheable data structures, there is a caching strategy that text editors can use that allows the text on screen to be re-rendered instantaneously regardless of the file size when a change is made after the initial highlight.

Expand All @@ -120,7 +123,7 @@ This way from the time the edit happens to the time the new colouring gets rende

Any time the file is changed the latest cached state is found, the cache is cleared after that point, and a background job is started. Any already running jobs are stopped because they would be working on old state. This way you can just have one thread dedicated to highlighting that is always doing the most up-to-date work, or sleeping.

### Parallelizing
## Parallelizing

Since 3.0, `syntect` can be used to do parsing/highlighting in parallel. `SyntaxSet` is both `Send` and `Sync` and so can easily be used from multiple threads. It is also `Clone`, which means you can construct a syntax set and then clone it to use for other threads if you prefer.

Expand Down
1 change: 0 additions & 1 deletion examples/syncat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ fn main() {
};

if let Some(folder) = matches.opt_str("extra-syntaxes") {
// TODO: no way to go back to builder anymore :/
let mut builder = ss.into_builder();
builder.add_from_folder(folder, !no_newlines).unwrap();
ss = builder.build();
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! Welcome to the syntect docs.
//! These are still a work in progress but a lot of the important things have
//! been documented already.
//!
//! Much more info about syntect is available on the [Github Page](https://github.com/trishume/syntect).
//!
Expand All @@ -15,7 +13,7 @@
//! Some docs have example code but a good place to look is the `syncat` example as well as the source code
//! for the `easy` module in `easy.rs` as that shows how to plug the various parts together for common use cases.

#![doc(html_root_url = "https://docs.rs/syntect/2.1.0")]
#![doc(html_root_url = "https://docs.rs/syntect/3.0.0")]

#[cfg(feature = "yaml-load")]
extern crate yaml_rust;
Expand Down

0 comments on commit 043a679

Please sign in to comment.