Struct synoptic::highlighter::Highlighter
source · [−]pub struct Highlighter {
pub regex: HashMap<String, Vec<Regex>>,
pub multiline_regex: HashMap<String, Vec<Regex>>,
pub bounded: Vec<Bounded>,
}
Expand description
For performing highlighting operations
You can create a new Highlighter instance using the new
method
let mut h = Highlighter::new();
Fields
regex: HashMap<String, Vec<Regex>>
multiline_regex: HashMap<String, Vec<Regex>>
bounded: Vec<Bounded>
Implementations
sourceimpl Highlighter
impl Highlighter
sourcepub fn join(&mut self, regex: &[&str], token: &str) -> Result<(), ReError>
pub fn join(&mut self, regex: &[&str], token: &str) -> Result<(), ReError>
This method allows you to add multiple definitions to the highlighter The first argument is for your list of definitions and the second is for the name This is useful for adding lists of keywords, for example:
let mut python = Highlighter::new();
python.join(&["def", "return", "import"], "keyword");
For multiline tokens, you can add (?ms) or (?sm) to the beginning
Errors
This will return an error if one or more of your regex expressions are invalid
sourcepub fn add(&mut self, regex: &str, token: &str) -> Result<(), ReError>
pub fn add(&mut self, regex: &str, token: &str) -> Result<(), ReError>
This method allows you to add a single definition to the highlighter The first argument is for your definition and the second is for the name This is useful for adding things like regular expressions, for example:
let mut python = Highlighter::new();
python.add("[0-9]+", "number");
For multiline tokens, you can add (?ms) or (?sm) to the beginning.
(See the add_bounded
method for a better way of doing multiline tokens
if you plan on doing file buffering.)
Errors
This will return an error if your regex is invalid
sourcepub fn add_bounded(
&mut self,
start: &str,
end: &str,
escaping: bool,
token: &str
)
pub fn add_bounded(
&mut self,
start: &str,
end: &str,
escaping: bool,
token: &str
)
This method allows you to add a special, non-regex definition to the highlighter This not only makes it clearer to use for multiline tokens, but it will also allow you to buffer files from memory, and still be able to highlight multiline tokens, without having to have the end part visible in order to create a token. The first argument is for the text that starts the token The second argument is for the text that ends the token The third argument is true if you want to allow for escaping of the end token, false if not (for example, you might want to allow string escaping in strings). The forth argument is for the token name.
let mut rust = Highlighter::new();
rust.add_bounded("/*", "*/", false, "comment");
You can still use regex to create a multiline token, but doing that won’t guarantee that your highlighting will survive file buffering.
sourcepub fn run_bounded(
&self,
context: &str,
result: &mut HashMap<usize, Vec<FullToken>>
)
pub fn run_bounded(
&self,
context: &str,
result: &mut HashMap<usize, Vec<FullToken>>
)
A utility function to scan for just bounded tokens
sourcepub fn run_line(&self, context: &str, line: usize) -> Option<Vec<Token>>
pub fn run_line(&self, context: &str, line: usize) -> Option<Vec<Token>>
This is the method that you call to get the stream of tokens for a specific line.
The first argument is the string with the code that you wish to highlight.
the second argument is the line number that you wish to highlight.
It returns a vector of tokens which can be used to highlight the individual line
let mut lua = Highlighter::new();
lua.add("(?ms)[[.*?]]", "string");
lua.add("print", "keyword");
lua.run_line(r#"
print ([[ Hello World!
]])
"#, 2);
This example will return the second line, with the ]]
marked as a string
The advantage of using this over the run
method is that it is a lot faster
This is because it only has to render one line rather than all of them, saving time
This won’t work with bounded tokens due to problems with determining what is a start token and what isn’t. Bounded tokens require all lines above to be loaded, which run line doesn’t assume.
sourcepub fn run(&self, code: &str) -> Vec<Vec<Token>>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn run(&self, code: &str) -> Vec<Vec<Token>>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
This is the method that you call to get the stream of tokens The argument is the string with the code that you wish to highlight Return a vector of a vector of tokens, representing the lines and the tokens in them
let mut python = Highlighter::new();
python.add("[0-9]+", "number");
python.run("some numbers: 123");
This example will highlight the numbers 123
in the string
Trait Implementations
sourceimpl Clone for Highlighter
impl Clone for Highlighter
sourcefn clone(&self) -> Highlighter
fn clone(&self) -> Highlighter
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for Highlighter
impl Debug for Highlighter
Auto Trait Implementations
impl RefUnwindSafe for Highlighter
impl Send for Highlighter
impl Sync for Highlighter
impl Unpin for Highlighter
impl UnwindSafe for Highlighter
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
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more