Struct syntect::parsing::Scope

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

A hierarchy of atoms with semi-standardized names used to accord semantic information to a specific piece of text.

These are generally written with the atoms separated by dots, and - by convention - atoms are all lowercase alphanumeric.

Example scopes: text.plain, punctuation.definition.string.begin.ruby, meta.function.parameters.rust

syntect uses an optimized format for storing these that allows super fast comparison and determining if one scope is a prefix of another. It also always takes 16 bytes of space. It accomplishes this by using a global repository to store string values and using bit-packed 16 bit numbers to represent and compare atoms. Like “atoms” or “symbols” in other languages. This means that while comparing and prefix are fast, extracting a string is relatively slower but ideally should be very rare.

Implementations§

source§

impl Scope

source

pub fn new(s: &str) -> Result<Scope, ParseScopeError>

Parses a Scope from a series of atoms separated by dot (.) characters

Example: Scope::new("meta.rails.controller")

source

pub fn atom_at(self, index: usize) -> u16

Gets the atom number at a given index.

I can’t think of any reason you’d find this useful. It is used internally for turning a scope back into a string.

source

pub fn len(self) -> u32

Returns the number of atoms in the scope

source

pub fn is_empty(self) -> bool

source

pub fn build_string(self) -> String

Returns a string representation of this scope

This requires locking a global repo and shouldn’t be done frequently.

source

pub fn is_prefix_of(self, s: Scope) -> bool

Tests if this scope is a prefix of another scope. Note that the empty scope is always a prefix.

This operation uses bitwise operations and is very fast

Examples
use syntect::parsing::Scope;
assert!( Scope::new("string").unwrap()
        .is_prefix_of(Scope::new("string.quoted").unwrap()));
assert!( Scope::new("string.quoted").unwrap()
        .is_prefix_of(Scope::new("string.quoted").unwrap()));
assert!( Scope::new("").unwrap()
        .is_prefix_of(Scope::new("meta.rails.controller").unwrap()));
assert!(!Scope::new("source.php").unwrap()
        .is_prefix_of(Scope::new("source").unwrap()));
assert!(!Scope::new("source.php").unwrap()
        .is_prefix_of(Scope::new("source.ruby").unwrap()));
assert!(!Scope::new("meta.php").unwrap()
        .is_prefix_of(Scope::new("source.php").unwrap()));
assert!(!Scope::new("meta.php").unwrap()
        .is_prefix_of(Scope::new("source.php.wow").unwrap()));

Trait Implementations§

source§

impl Clone for Scope

source§

fn clone(&self) -> Scope

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 Scope

source§

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

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

impl Default for Scope

source§

fn default() -> Scope

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

impl<'de> Deserialize<'de> for Scope

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 Display for Scope

source§

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

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

impl FromStr for Scope

§

type Err = ParseScopeError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Scope, ParseScopeError>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Scope

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Scope

source§

fn cmp(&self, other: &Scope) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<Scope> for Scope

source§

fn eq(&self, other: &Scope) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Scope> for Scope

source§

fn partial_cmp(&self, other: &Scope) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Scope

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

impl Copy for Scope

source§

impl Eq for Scope

source§

impl StructuralEq for Scope

source§

impl StructuralPartialEq for Scope

Auto Trait Implementations§

§

impl RefUnwindSafe for Scope

§

impl Send for Scope

§

impl Sync for Scope

§

impl Unpin for Scope

§

impl UnwindSafe for Scope

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere 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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

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