pub struct Syntax { /* private fields */ }Expand description
How to colour a language. Build one with Syntax::new and its
methods, or start from Syntax::sql or Syntax::rust.
use ferrokit::*;
let ini = Syntax::new()
.keywords(&["true", "false"], TextStyle::new().color(Color::Purple))
.strings("\"", TextStyle::new().color(Color::Red))
.line_comment(";", TextStyle::new().color(Color::Gray))
.numbers(TextStyle::new().color(Color::Blue));
let runs = ini.highlight("debug = true ; for now");
assert_eq!(runs[0], (8..12, TextStyle::new().color(Color::Purple)));Implementations§
Source§impl Syntax
impl Syntax
Sourcepub fn keywords(self, words: &[&str], style: TextStyle) -> Self
pub fn keywords(self, words: &[&str], style: TextStyle) -> Self
Words shown in style. Call again for other groups (types,
functions) in other styles; a word’s first group wins.
Sourcepub fn ignore_case(self) -> Self
pub fn ignore_case(self) -> Self
Keywords match in any case (SQL). Call before keywords.
Sourcepub fn strings(self, quotes: &str, style: TextStyle) -> Self
pub fn strings(self, quotes: &str, style: TextStyle) -> Self
String literals: each character of quotes opens one, closed by the
same character.
Sourcepub fn escape(self, escape: char) -> Self
pub fn escape(self, escape: char) -> Self
Inside strings, escape makes the next character literal (\ in
most languages).
Sourcepub fn doubled_quotes(self) -> Self
pub fn doubled_quotes(self) -> Self
Inside strings, a doubled quote is a literal quote ('it''s' in SQL).
Sourcepub fn quoted_identifiers(self, pairs: &[(char, char)]) -> Self
pub fn quoted_identifiers(self, pairs: &[(char, char)]) -> Self
Quoted identifiers ("name", `name`, [name] in SQL), left
unstyled so a quoted keyword isn’t coloured as one.
Sourcepub fn line_comment(self, prefix: &str, style: TextStyle) -> Self
pub fn line_comment(self, prefix: &str, style: TextStyle) -> Self
Comments from prefix to the end of the line. Call again for more
prefixes; the style is the last given (shared with block comments).
Sourcepub fn block_comment(self, open: &str, close: &str, style: TextStyle) -> Self
pub fn block_comment(self, open: &str, close: &str, style: TextStyle) -> Self
Comments from open to close (to the end of the text if unclosed).
Sourcepub fn numbers(self, style: TextStyle) -> Self
pub fn numbers(self, style: TextStyle) -> Self
Numbers: a word starting with a digit (42, 3.14, 0x1F, 1e-9).
Sourcepub fn sql() -> Self
pub fn sql() -> Self
SQL (SQLite’s dialect, which covers the common ones): keywords pink and bold, types purple, functions indigo, strings red, numbers blue, comments gray and italic.