Struct syntect::easy::HighlightFile

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

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§

§reader: BufReader<File>§highlight_lines: HighlightLines<'a>

Implementations§

source§

impl<'a> HighlightFile<'a>

source

pub fn new<P: AsRef<Path>>( path_obj: P, ss: &SyntaxSet, theme: &'a Theme ) -> Result<HighlightFile<'a>>

Constructs a file reader and a line highlighter to get you reading files as fast as possible.

This auto-detects the syntax from the extension and constructs a HighlightLines with the correct syntax and theme.

§Examples

Using the newlines mode is a bit involved but yields more robust and glitch-free highlighting, as well as being slightly faster since it can re-use a line buffer.

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_newlines();
let ts = ThemeSet::load_defaults();

let mut highlighter = HighlightFile::new("testdata/highlight_test.erb", &ss, &ts.themes["base16-ocean.dark"]).unwrap();
let mut line = String::new();
while highlighter.reader.read_line(&mut line)? > 0 {
    {
        let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight_line(&line, &ss).unwrap();
        print!("{}", as_24_bit_terminal_escaped(&regions[..], true));
    } // until NLL this scope is needed so we can clear the buffer after
    line.clear(); // read_line appends so we need to clear between lines
}

This example uses reader.lines() to get lines without a newline character, it’s simpler but may break on rare tricky cases.

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(&line, &ss).unwrap();
    println!("{}", as_24_bit_terminal_escaped(&regions[..], true));
}

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for HighlightFile<'a>

§

impl<'a> !Send for HighlightFile<'a>

§

impl<'a> !Sync for HighlightFile<'a>

§

impl<'a> Unpin for HighlightFile<'a>

§

impl<'a> UnwindSafe for HighlightFile<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.