pub trait SyntaxNodeExt {
Show 21 methods
// Provided methods
fn tokens(&self) -> Vec<SyntaxToken> ⓘ { ... }
fn lossy_tokens(&self) -> Vec<SyntaxToken> ⓘ { ... }
fn first_lossy_token(&self) -> Option<SyntaxToken> { ... }
fn is<T: AstNode>(&self) -> bool { ... }
fn to<T: AstNode>(&self) -> T { ... }
fn try_to<T: AstNode>(&self) -> Option<T> { ... }
fn lexical_eq(&self, right: &SyntaxNode) -> bool { ... }
fn color(&self) -> String { ... }
fn trimmed_range(&self) -> TextRange { ... }
fn trimmed_text(&self) -> SyntaxText { ... }
fn in_ts(&self, set: TokenSet) -> bool { ... }
fn readable_stmt_name(&self) -> String { ... }
fn is_loop(&self) -> bool { ... }
fn descendants_with<F>(&self, func: &mut F)
where F: FnMut(&SyntaxNode) -> bool { ... }
fn structural_lossy_token_eq(&self, tokens: &[impl AsRef<str>]) -> bool { ... }
fn contains_comments(&self) -> bool { ... }
fn child_with_kind(&self, kind: SyntaxKind) -> Option<SyntaxNode> { ... }
fn expr_parent(&self) -> Option<SyntaxNode> { ... }
fn child_with_ast<T: AstNode>(&self) -> Option<T> { ... }
fn descendants_with_tokens_with<F>(&self, func: &mut F)
where F: FnMut(&SyntaxElement) -> bool { ... }
fn token_with_kind(&self, kind: SyntaxKind) -> Option<SyntaxToken> { ... }
}
Expand description
Extensions to rowan’s SyntaxNode
Provided Methods§
Sourcefn tokens(&self) -> Vec<SyntaxToken> ⓘ
fn tokens(&self) -> Vec<SyntaxToken> ⓘ
Get all of the tokens of this node, recursively, including whitespace and comments.
Sourcefn lossy_tokens(&self) -> Vec<SyntaxToken> ⓘ
fn lossy_tokens(&self) -> Vec<SyntaxToken> ⓘ
Get all the tokens of this node, recursively, not including whitespace and comments.
Sourcefn first_lossy_token(&self) -> Option<SyntaxToken>
fn first_lossy_token(&self) -> Option<SyntaxToken>
Get the first non-whitespace child token.
Sourcefn is<T: AstNode>(&self) -> bool
fn is<T: AstNode>(&self) -> bool
Check if the node is a certain AST node and that it can be casted to it.
Sourcefn to<T: AstNode>(&self) -> T
fn to<T: AstNode>(&self) -> T
Cast this node to a certain AST node.
§Panics
Panics if the underlying node cannot be cast to the AST node
Sourcefn lexical_eq(&self, right: &SyntaxNode) -> bool
fn lexical_eq(&self, right: &SyntaxNode) -> bool
Compare two syntax nodes by comparing their underlying non-whitespace tokens.
This is a more accurate way of comparing nodes because it does not count whitespace.
Text based equality counts foo. bar
and foo.bar
as different, while this counts them as the same.
§Examples
use rslint_parser::{SyntaxNodeExt, parse_expr};
let left = parse_expr("foo. bar", 0).syntax();
let right = parse_expr("foo.bar", 0).syntax();
assert!(left.lexical_eq(&right));
assert_ne!(left.text(), right.text());
Sourcefn color(&self) -> String
fn color(&self) -> String
Syntax highlight the node’s text into an ANSI string. If stdout and stderr are not terminals, this will return the raw node text.
Sourcefn trimmed_range(&self) -> TextRange
fn trimmed_range(&self) -> TextRange
Get the text range of this node, not including any leading or trailing whitespace.
§Examples
use rslint_parser::{SyntaxNodeExt, parse_expr, TextRange};
let node = parse_expr(" foo. bar ", 0).syntax();
assert_eq!(node.trimmed_range(), TextRange::new(1.into(), 9.into()));
assert_eq!(node.text_range(), TextRange::new(0.into(), 11.into()));
Sourcefn trimmed_text(&self) -> SyntaxText
fn trimmed_text(&self) -> SyntaxText
Get the text of this node, not including leading or trailing whitespace
§Examples
use rslint_parser::{SyntaxNodeExt, parse_expr, TextRange};
let node = parse_expr(" foo. bar ", 0).syntax();
assert_eq!(node.trimmed_text(), "foo. bar");
Sourcefn in_ts(&self, set: TokenSet) -> bool
fn in_ts(&self, set: TokenSet) -> bool
Check whether this node’s kind is contained in a token set.
Sourcefn readable_stmt_name(&self) -> String
fn readable_stmt_name(&self) -> String
A human readable name for this node’s kind. e.g.:
BREAK_STMT
=> Break statement
Returns a capitalized name without an underscore for anything not a statement. e.g.:
FN_DECL
=> Fn decl
Sourcefn descendants_with<F>(&self, func: &mut F)
fn descendants_with<F>(&self, func: &mut F)
Go over the descendants of this node, at every descendant call func
, and keep traversing
the descendants of that node if the function’s return is true
. If the function returns false
then stop traversing the descendants of that node go on to the next child.
For example:
ROOT
CHILD // <-- Call `F` with CHILD, `F` returns `false` so go on to the next child...
SUBCHILD
CHILD // <-- Call `F` with CHILD, `F` return `true` so go on to the children of CHILD
SUBCHILD // <-- Same thing
SUBSUBCHILD
CHILD // Go on to the next child and do the same thing
Sourcefn structural_lossy_token_eq(&self, tokens: &[impl AsRef<str>]) -> bool
fn structural_lossy_token_eq(&self, tokens: &[impl AsRef<str>]) -> bool
Separate all the lossy tokens of this node, then compare each token’s text with the corresponding
text in tokens
.
Sourcefn contains_comments(&self) -> bool
fn contains_comments(&self) -> bool
Whether the node contains any comments.
Sourcefn child_with_kind(&self, kind: SyntaxKind) -> Option<SyntaxNode>
fn child_with_kind(&self, kind: SyntaxKind) -> Option<SyntaxNode>
Get the first child with a specific kind.
Sourcefn expr_parent(&self) -> Option<SyntaxNode>
fn expr_parent(&self) -> Option<SyntaxNode>
Get the parent of this node, recursing through any grouping expressions
Sourcefn child_with_ast<T: AstNode>(&self) -> Option<T>
fn child_with_ast<T: AstNode>(&self) -> Option<T>
Get the first child in this node that can be casted to an AST node
Sourcefn descendants_with_tokens_with<F>(&self, func: &mut F)
fn descendants_with_tokens_with<F>(&self, func: &mut F)
Same as descendants_with
but considers tokens too.
Sourcefn token_with_kind(&self, kind: SyntaxKind) -> Option<SyntaxToken>
fn token_with_kind(&self, kind: SyntaxKind) -> Option<SyntaxToken>
Get a specific token in the node which matches a syntax kind.
This does not consider tokens in descendant nodes
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.