Struct syntect::easy::HighlightLines
source · [−]pub struct HighlightLines<'a> { /* private fields */ }
Expand description
Simple way to go directly from lines of text to colored tokens.
Depending on how you load the syntaxes (see the SyntaxSet
docs), this can either take
strings with trailing \n
s or without.
Examples
Prints colored lines of a string to the terminal
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) { // LinesWithEndings enables use of newlines mode
let ranges: Vec<(Style, &str)> = h.highlight_line(line, &ps).unwrap();
let escaped = as_24_bit_terminal_escaped(&ranges[..], true);
print!("{}", escaped);
}
Implementations
sourceimpl<'a> HighlightLines<'a>
impl<'a> HighlightLines<'a>
pub fn new(syntax: &SyntaxReference, theme: &'a Theme) -> HighlightLines<'a>
pub fn highlight<'b>(
&mut self,
line: &'b str,
syntax_set: &SyntaxSet
) -> Vec<(Style, &'b str)>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
👎 Deprecated since 5.0.0:
Renamed to highlight_line
to make it clear it should be passed a single line at a time
Auto Trait Implementations
impl<'a> RefUnwindSafe for HighlightLines<'a>
impl<'a> !Send for HighlightLines<'a>
impl<'a> !Sync for HighlightLines<'a>
impl<'a> Unpin for HighlightLines<'a>
impl<'a> UnwindSafe for HighlightLines<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more