mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
flip comma
This commit is contained in:
parent
56aa6e20e0
commit
66be735aa9
12 changed files with 273 additions and 57 deletions
|
@ -74,7 +74,6 @@ impl<'f> Iterator for LeafAtOffset<'f> {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef {
|
||||
assert!(is_subrange(root.range(), range));
|
||||
let (left, right) = match (
|
||||
|
@ -88,6 +87,26 @@ pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRe
|
|||
common_ancestor(left, right)
|
||||
}
|
||||
|
||||
pub fn ancestors<'a>(node: SyntaxNodeRef<'a>) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
|
||||
generate(Some(node), |&node| node.parent())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Direction {
|
||||
Forward,
|
||||
Backward,
|
||||
}
|
||||
|
||||
pub fn siblings<'a>(
|
||||
node: SyntaxNodeRef<'a>,
|
||||
direction: Direction
|
||||
) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
|
||||
generate(Some(node), move |&node| match direction {
|
||||
Direction::Forward => node.next_sibling(),
|
||||
Direction::Backward => node.prev_sibling(),
|
||||
})
|
||||
}
|
||||
|
||||
fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> {
|
||||
for p in ancestors(n1) {
|
||||
if ancestors(n2).any(|a| a == p) {
|
||||
|
@ -97,24 +116,6 @@ fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNo
|
|||
panic!("Can't find common ancestor of {:?} and {:?}", n1, n2)
|
||||
}
|
||||
|
||||
pub fn ancestors<'a>(node: SyntaxNodeRef<'a>) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
|
||||
Ancestors(Some(node))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Ancestors<'a>(Option<SyntaxNodeRef<'a>>);
|
||||
|
||||
impl<'a> Iterator for Ancestors<'a> {
|
||||
type Item = SyntaxNodeRef<'a>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.0.take().map(|n| {
|
||||
self.0 = n.parent();
|
||||
n
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool {
|
||||
range.start() <= offset && offset <= range.end()
|
||||
}
|
||||
|
@ -122,3 +123,12 @@ fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool {
|
|||
fn is_subrange(range: TextRange, subrange: TextRange) -> bool {
|
||||
range.start() <= subrange.start() && subrange.end() <= range.end()
|
||||
}
|
||||
|
||||
fn generate<T>(seed: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item=T> {
|
||||
::itertools::unfold(seed, move |slot| {
|
||||
slot.take().map(|curr| {
|
||||
*slot = step(&curr);
|
||||
curr
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ pub(crate) struct SyntaxInfo {
|
|||
}
|
||||
|
||||
impl SyntaxKind {
|
||||
pub(crate) fn is_trivia(self) -> bool {
|
||||
pub fn is_trivia(self) -> bool {
|
||||
match self {
|
||||
WHITESPACE | COMMENT | DOC_COMMENT => true,
|
||||
_ => false,
|
||||
|
|
|
@ -101,6 +101,17 @@ impl<R: TreeRoot> SyntaxNode<R> {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn prev_sibling(&self) -> Option<SyntaxNode<R>> {
|
||||
let red = self.red();
|
||||
let parent = self.parent()?;
|
||||
let prev_sibling_idx = red.index_in_parent()?.checked_sub(1)?;
|
||||
let sibling_red = parent.red().get_child(prev_sibling_idx)?;
|
||||
Some(SyntaxNode {
|
||||
root: self.root.clone(),
|
||||
red: sibling_red,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_leaf(&self) -> bool {
|
||||
self.first_child().is_none()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue