Struct syntect::parsing::SyntaxSet

source ·
pub struct SyntaxSet { /* private fields */ }
Expand description

A syntax set holds multiple syntaxes that have been linked together.

Use a SyntaxSetBuilder to load syntax definitions and build a syntax set.

After building, the syntax set is immutable and can no longer be modified, but you can convert it back into a builder by using the into_builder method.

Implementations§

source§

impl SyntaxSet

source

pub fn load_defaults_nonewlines() -> SyntaxSet

Instantiates a new syntax set from a binary dump of Sublime Text’s default open source syntax definitions.

These dumps are included in this library’s binary for convenience.

This method loads the version for parsing line strings with no \n characters at the end. If you’re able to efficiently include newlines at the end of strings, use load_defaults_newlines since it works better. See SyntaxSetBuilder::add_from_folder for more info on this issue.

This is the recommended way of creating a syntax set for non-advanced use cases. It is also significantly faster than loading the YAML files.

Note that you can load additional syntaxes after doing this. If you want you can even use the fact that SyntaxDefinitions are serializable with the bincode crate to cache dumps of additional syntaxes yourself.

source

pub fn load_defaults_newlines() -> SyntaxSet

Same as load_defaults_nonewlines but for parsing line strings with newlines at the end.

These are separate methods because thanks to linker garbage collection, only the serialized dumps for the method(s) you call will be included in the binary (each is ~200kb for now).

source§

impl SyntaxSet

source

pub fn new() -> SyntaxSet

source

pub fn load_from_folder<P: AsRef<Path>>( folder: P ) -> Result<SyntaxSet, LoadingError>

Convenience constructor for creating a builder, then loading syntax definitions from a folder and then building the syntax set.

Note that this uses lines_include_newline set to false, see the add_from_folder method docs on SyntaxSetBuilder for an explanation as to why this might not be the best.

source

pub fn syntaxes(&self) -> &[SyntaxReference]

The list of syntaxes in the set

source

pub fn find_syntax_by_scope(&self, scope: Scope) -> Option<&SyntaxReference>

Finds a syntax by its default scope, for example source.regexp finds the regex syntax.

This and all similar methods below do a linear search of syntaxes, this should be fast because there aren’t many syntaxes, but don’t think you can call it a bajillion times per second.

source

pub fn find_syntax_by_name<'a>( &'a self, name: &str ) -> Option<&'a SyntaxReference>

source

pub fn find_syntax_by_extension<'a>( &'a self, extension: &str ) -> Option<&'a SyntaxReference>

source

pub fn find_syntax_by_token<'a>( &'a self, s: &str ) -> Option<&'a SyntaxReference>

Searches for a syntax first by extension and then by case-insensitive name

This is useful for things like Github-flavoured-markdown code block highlighting where all you have to go on is a short token given by the user

source

pub fn find_syntax_by_first_line<'a>( &'a self, s: &str ) -> Option<&'a SyntaxReference>

Try to find the syntax for a file based on its first line

This uses regexes that come with some sublime syntax grammars for matching things like shebangs and mode lines like -*- Mode: C -*-

source

pub fn find_syntax_by_path<'a>( &'a self, path: &str ) -> Option<&'a SyntaxReference>

Searches for a syntax by it’s original file path when it was first loaded from disk

This is primarily useful for syntax tests. Some may specify a Packages/PackageName/SyntaxName.sublime-syntax path, and others may just have SyntaxName.sublime-syntax. This caters for these by matching the end of the path of the loaded syntax definition files

source

pub fn find_syntax_for_file<P: AsRef<Path>>( &self, path_obj: P ) -> Result<Option<&SyntaxReference>>

Convenience method that tries to find the syntax for a file path, first by extension/name and then by first line of the file if that doesn’t work.

May IO Error because it sometimes tries to read the first line of the file.

§Examples

When determining how to highlight a file, use this in combination with a fallback to plain text:

use syntect::parsing::SyntaxSet;
let ss = SyntaxSet::load_defaults_newlines();
let syntax = ss.find_syntax_for_file("testdata/highlight_test.erb")
    .unwrap() // for IO errors, you may want to use try!() or another plain text fallback
    .unwrap_or_else(|| ss.find_syntax_plain_text());
assert_eq!(syntax.name, "HTML (Rails)");
source

pub fn find_syntax_plain_text(&self) -> &SyntaxReference

Finds a syntax for plain text, which usually has no highlighting rules.

This is good as a fallback when you can’t find another syntax but you still want to use the same highlighting pipeline code.

This syntax should always be present, if not this method will panic. If the way you load syntaxes doesn’t create one, use add_plain_text_syntax.

§Examples
use syntect::parsing::SyntaxSetBuilder;
let mut builder = SyntaxSetBuilder::new();
builder.add_plain_text_syntax();
let ss = builder.build();
let syntax = ss.find_syntax_by_token("rs").unwrap_or_else(|| ss.find_syntax_plain_text());
assert_eq!(syntax.name, "Plain Text");
source

pub fn into_builder(self) -> SyntaxSetBuilder

Converts this syntax set into a builder so that more syntaxes can be added to it.

Note that newly added syntaxes can have references to existing syntaxes in the set, but not the other way around.

source

pub fn find_unlinked_contexts(&self) -> BTreeSet<String>

Trait Implementations§

source§

impl Clone for SyntaxSet

source§

fn clone(&self) -> SyntaxSet

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for SyntaxSet

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for SyntaxSet

source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for SyntaxSet

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for SyntaxSet

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> 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> ToOwned for T
where T: Clone,

§

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>,

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,