Skip to main content

Syntax

Struct Syntax 

Source
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

Source

pub fn new() -> Self

A syntax that styles nothing yet.

Source

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.

Source

pub fn ignore_case(self) -> Self

Keywords match in any case (SQL). Call before keywords.

Source

pub fn strings(self, quotes: &str, style: TextStyle) -> Self

String literals: each character of quotes opens one, closed by the same character.

Source

pub fn escape(self, escape: char) -> Self

Inside strings, escape makes the next character literal (\ in most languages).

Source

pub fn doubled_quotes(self) -> Self

Inside strings, a doubled quote is a literal quote ('it''s' in SQL).

Source

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.

Source

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).

Source

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).

Source

pub fn numbers(self, style: TextStyle) -> Self

Numbers: a word starting with a digit (42, 3.14, 0x1F, 1e-9).

Source

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.

Source

pub fn rust() -> Self

Rust: keywords pink and bold, common types purple, strings red, numbers blue, comments gray and italic.

Source

pub fn highlight(&self, text: &str) -> Vec<(Range<usize>, TextStyle)>

The styled runs of text, in order (UTF-8 byte ranges).

Trait Implementations§

Source§

impl Clone for Syntax

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Syntax

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> AutoreleaseSafe for T
where T: ?Sized,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.