Struct syntect::parsing::ScopeStack

source ·
pub struct ScopeStack {
    pub scopes: Vec<Scope>,
    /* private fields */
}
Expand description

A stack/sequence of scopes for representing hierarchies for a given token of text

This is also used within ScopeSelectors.

In Sublime Text, the scope stack at a given point can be seen by pressing ctrl+shift+p. Also see the TextMate docs.

Example for a JS string inside a script tag in a Rails ERB file: text.html.ruby text.html.basic source.js.embedded.html string.quoted.double.js

Fields§

§scopes: Vec<Scope>

Implementations§

source§

impl ScopeStack

source

pub fn new() -> ScopeStack

source

pub fn from_vec(v: Vec<Scope>) -> ScopeStack

Note: creating a ScopeStack with this doesn’t contain information on what to do when clear_scopes contexts end.

source

pub fn push(&mut self, s: Scope)

source

pub fn pop(&mut self)

source

pub fn apply(&mut self, op: &ScopeStackOp) -> Result<(), ScopeError>

Modifies this stack according to the operation given

Use this to create a stack from a Vec of changes given by the parser.

source

pub fn apply_with_hook<F>( &mut self, op: &ScopeStackOp, hook: F ) -> Result<(), ScopeError>where F: FnMut(BasicScopeStackOp, &[Scope]),

Modifies this stack according to the operation given and calls the hook for each basic operation.

Like apply but calls hook for every basic modification (as defined by BasicScopeStackOp). Use this to do things only when the scope stack changes.

source

pub fn debug_print(&self, repo: &ScopeRepository)

Prints out each scope in the stack separated by spaces and then a newline. Top of the stack at the end.

source

pub fn bottom_n(&self, n: usize) -> &[Scope]

Returns the bottom n elements of the stack.

Equivalent to &scopes[0..n] on a Vec

source

pub fn as_slice(&self) -> &[Scope]

Return a slice of the scopes in this stack

source

pub fn len(&self) -> usize

Return the height/length of this stack

source

pub fn is_empty(&self) -> bool

source

pub fn does_match(&self, stack: &[Scope]) -> Option<MatchPower>

Checks if this stack as a selector matches the given stack, returning the match score if so

Higher match scores indicate stronger matches. Scores are ordered according to the rules found at https://manual.macromates.com/en/scope_selectors

It accomplishes this ordering through some floating point math ensuring deeper and longer matches matter. Unfortunately it is only guaranteed to return perfectly accurate results up to stack depths of 17, but it should be reasonably good even afterwards. TextMate has the exact same limitation, dunno about Sublime Text.

Examples
use syntect::parsing::{ScopeStack, MatchPower};
use std::str::FromStr;
assert_eq!(ScopeStack::from_str("a.b c e.f").unwrap()
    .does_match(ScopeStack::from_str("a.b c.d e.f.g").unwrap().as_slice()),
    Some(MatchPower(0o212u64 as f64)));
assert_eq!(ScopeStack::from_str("a c.d.e").unwrap()
    .does_match(ScopeStack::from_str("a.b c.d e.f.g").unwrap().as_slice()),
    None);

Trait Implementations§

source§

impl Clone for ScopeStack

source§

fn clone(&self) -> ScopeStack

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 ScopeStack

source§

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

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

impl Default for ScopeStack

source§

fn default() -> ScopeStack

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

impl<'de> Deserialize<'de> for ScopeStack

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 ScopeStack

source§

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

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

impl FromStr for ScopeStack

source§

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

Parses a scope stack from a whitespace separated list of scopes.

§

type Err = ParseScopeError

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

impl PartialEq<ScopeStack> for ScopeStack

source§

fn eq(&self, other: &ScopeStack) -> 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 Serialize for ScopeStack

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 Eq for ScopeStack

source§

impl StructuralEq for ScopeStack

source§

impl StructuralPartialEq for ScopeStack

Auto Trait Implementations§

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