Struct syntect::easy::HighlightFile [] [src]

pub struct HighlightFile<'a> {
    pub reader: BufReader<File>,
    pub highlight_lines: HighlightLines<'a>,
}

Convenience struct containing everything you need to highlight a file. Use the reader to get the lines of the file and the highlight_lines to highlight them. See the new method docs for more information.

Fields

Methods

impl<'a> HighlightFile<'a>
[src]

Constructs a file reader and a line highlighter to get you reading files as fast as possible. Auto-detects the syntax from the extension and constructs a HighlightLines with the correct syntax and theme.

Examples

This example uses reader.lines() to get lines without a newline character. See the syncat example for an example of reading lines with a newline character, which gets slightly more robust and fast syntax highlighting, at the cost of a couple extra lines of code.

use syntect::parsing::SyntaxSet;
use syntect::highlighting::{ThemeSet, Style};
use syntect::util::as_24_bit_terminal_escaped;
use syntect::easy::HighlightFile;
use std::io::BufRead;

let ss = SyntaxSet::load_defaults_nonewlines();
let ts = ThemeSet::load_defaults();

let mut highlighter = HighlightFile::new("testdata/highlight_test.erb", &ss, &ts.themes["base16-ocean.dark"]).unwrap();
for maybe_line in highlighter.reader.lines() {
    let line = maybe_line.unwrap();
    let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight(&line);
    println!("{}", as_24_bit_terminal_escaped(&regions[..], true));
}