From 61f3a438d3a729a6be941bca1ff4c6a97a33f221 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Mon, 15 Oct 2018 17:44:23 -0400 Subject: Cargo Format Run `cargo fmt` and ignore generated files --- crates/ra_syntax/src/algo/mod.rs | 54 +++++++++++++++++++------------- crates/ra_syntax/src/algo/visit.rs | 63 ++++++++++++++++++++++---------------- crates/ra_syntax/src/algo/walk.rs | 6 +--- 3 files changed, 71 insertions(+), 52 deletions(-) (limited to 'crates/ra_syntax/src/algo') diff --git a/crates/ra_syntax/src/algo/mod.rs b/crates/ra_syntax/src/algo/mod.rs index e686a5704..b4896c482 100644 --- a/crates/ra_syntax/src/algo/mod.rs +++ b/crates/ra_syntax/src/algo/mod.rs @@ -1,16 +1,18 @@ -pub mod walk; pub mod visit; +pub mod walk; use crate::{ - SyntaxNodeRef, TextUnit, TextRange, text_utils::{contains_offset_nonstrict, is_subrange}, + SyntaxNodeRef, TextRange, TextUnit, }; pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffset { let range = node.range(); assert!( contains_offset_nonstrict(range, offset), - "Bad offset: range {:?} offset {:?}", range, offset + "Bad offset: range {:?} offset {:?}", + range, + offset ); if range.is_empty() { return LeafAtOffset::None; @@ -20,20 +22,23 @@ pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffse return LeafAtOffset::Single(node); } - let mut children = node.children() - .filter(|child| { - let child_range = child.range(); - !child_range.is_empty() && contains_offset_nonstrict(child_range, offset) - }); + let mut children = node.children().filter(|child| { + let child_range = child.range(); + !child_range.is_empty() && contains_offset_nonstrict(child_range, offset) + }); let left = children.next().unwrap(); let right = children.next(); assert!(children.next().is_none()); return if let Some(right) = right { - match (find_leaf_at_offset(left, offset), find_leaf_at_offset(right, offset)) { - (LeafAtOffset::Single(left), LeafAtOffset::Single(right)) => - LeafAtOffset::Between(left, right), - _ => unreachable!() + match ( + find_leaf_at_offset(left, offset), + find_leaf_at_offset(right, offset), + ) { + (LeafAtOffset::Single(left), LeafAtOffset::Single(right)) => { + LeafAtOffset::Between(left, right) + } + _ => unreachable!(), } } else { find_leaf_at_offset(left, offset) @@ -44,7 +49,7 @@ pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffse pub enum LeafAtOffset<'a> { None, Single(SyntaxNodeRef<'a>), - Between(SyntaxNodeRef<'a>, SyntaxNodeRef<'a>) + Between(SyntaxNodeRef<'a>, SyntaxNodeRef<'a>), } impl<'a> LeafAtOffset<'a> { @@ -52,7 +57,7 @@ impl<'a> LeafAtOffset<'a> { match self { LeafAtOffset::None => None, LeafAtOffset::Single(node) => Some(node), - LeafAtOffset::Between(_, right) => Some(right) + LeafAtOffset::Between(_, right) => Some(right), } } @@ -60,7 +65,7 @@ impl<'a> LeafAtOffset<'a> { match self { LeafAtOffset::None => None, LeafAtOffset::Single(node) => Some(node), - LeafAtOffset::Between(left, _) => Some(left) + LeafAtOffset::Between(left, _) => Some(left), } } } @@ -71,8 +76,14 @@ impl<'f> Iterator for LeafAtOffset<'f> { fn next(&mut self) -> Option> { match *self { LeafAtOffset::None => None, - LeafAtOffset::Single(node) => { *self = LeafAtOffset::None; Some(node) } - LeafAtOffset::Between(left, right) => { *self = LeafAtOffset::Single(right); Some(left) } + LeafAtOffset::Single(node) => { + *self = LeafAtOffset::None; + Some(node) + } + LeafAtOffset::Between(left, right) => { + *self = LeafAtOffset::Single(right); + Some(left) + } } } } @@ -81,14 +92,15 @@ pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRe assert!( is_subrange(root.range(), range), "node range: {:?}, target range: {:?}", - root.range(), range, + root.range(), + range, ); let (left, right) = match ( find_leaf_at_offset(root, range.start()).right_biased(), - find_leaf_at_offset(root, range.end()).left_biased() + find_leaf_at_offset(root, range.end()).left_biased(), ) { (Some(l), Some(r)) => (l, r), - _ => return root + _ => return root, }; common_ancestor(left, right) @@ -103,7 +115,7 @@ fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNo panic!("Can't find common ancestor of {:?} and {:?}", n1, n2) } -pub fn generate(seed: Option, step: impl Fn(&T) -> Option) -> impl Iterator { +pub fn generate(seed: Option, step: impl Fn(&T) -> Option) -> impl Iterator { ::itertools::unfold(seed, move |slot| { slot.take().map(|curr| { *slot = step(&curr); diff --git a/crates/ra_syntax/src/algo/visit.rs b/crates/ra_syntax/src/algo/visit.rs index 1ae988a87..c021f464c 100644 --- a/crates/ra_syntax/src/algo/visit.rs +++ b/crates/ra_syntax/src/algo/visit.rs @@ -1,23 +1,31 @@ -use std::marker::PhantomData; -use crate::{SyntaxNodeRef, AstNode}; +use crate::{AstNode, SyntaxNodeRef}; +use std::marker::PhantomData; -pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> { +pub fn visitor<'a, T>() -> impl Visitor<'a, Output = T> { EmptyVisitor { ph: PhantomData } } -pub fn visitor_ctx<'a, T, C>(ctx: C) -> impl VisitorCtx<'a, Output=T, Ctx=C> { - EmptyVisitorCtx { ph: PhantomData, ctx } +pub fn visitor_ctx<'a, T, C>(ctx: C) -> impl VisitorCtx<'a, Output = T, Ctx = C> { + EmptyVisitorCtx { + ph: PhantomData, + ctx, + } } pub trait Visitor<'a>: Sized { type Output; fn accept(self, node: SyntaxNodeRef<'a>) -> Option; fn visit(self, f: F) -> Vis - where N: AstNode<'a>, - F: FnOnce(N) -> Self::Output, + where + N: AstNode<'a>, + F: FnOnce(N) -> Self::Output, { - Vis { inner: self, f, ph: PhantomData } + Vis { + inner: self, + f, + ph: PhantomData, + } } } @@ -26,16 +34,21 @@ pub trait VisitorCtx<'a>: Sized { type Ctx; fn accept(self, node: SyntaxNodeRef<'a>) -> Result; fn visit(self, f: F) -> VisCtx - where N: AstNode<'a>, - F: FnOnce(N, Self::Ctx) -> Self::Output, + where + N: AstNode<'a>, + F: FnOnce(N, Self::Ctx) -> Self::Output, { - VisCtx { inner: self, f, ph: PhantomData } + VisCtx { + inner: self, + f, + ph: PhantomData, + } } } #[derive(Debug)] struct EmptyVisitor { - ph: PhantomData T> + ph: PhantomData T>, } impl<'a, T> Visitor<'a> for EmptyVisitor { @@ -69,10 +82,10 @@ pub struct Vis { } impl<'a, V, N, F> Visitor<'a> for Vis - where - V: Visitor<'a>, - N: AstNode<'a>, - F: FnOnce(N) -> >::Output, +where + V: Visitor<'a>, + N: AstNode<'a>, + F: FnOnce(N) -> >::Output, { type Output = >::Output; @@ -90,21 +103,19 @@ pub struct VisCtx { } impl<'a, V, N, F> VisitorCtx<'a> for VisCtx - where - V: VisitorCtx<'a>, - N: AstNode<'a>, - F: FnOnce(N, >::Ctx) -> >::Output, +where + V: VisitorCtx<'a>, + N: AstNode<'a>, + F: FnOnce(N, >::Ctx) -> >::Output, { type Output = >::Output; type Ctx = >::Ctx; fn accept(self, node: SyntaxNodeRef<'a>) -> Result { let VisCtx { inner, f, .. } = self; - inner.accept(node).or_else(|ctx| - match N::cast(node) { - None => Err(ctx), - Some(node) => Ok(f(node, ctx)) - } - ) + inner.accept(node).or_else(|ctx| match N::cast(node) { + None => Err(ctx), + Some(node) => Ok(f(node, ctx)), + }) } } diff --git a/crates/ra_syntax/src/algo/walk.rs b/crates/ra_syntax/src/algo/walk.rs index d34415626..9afa86401 100644 --- a/crates/ra_syntax/src/algo/walk.rs +++ b/crates/ra_syntax/src/algo/walk.rs @@ -1,8 +1,4 @@ -use crate::{ - SyntaxNodeRef, - algo::generate, -}; - +use crate::{algo::generate, SyntaxNodeRef}; #[derive(Debug, Copy, Clone)] pub enum WalkEvent<'a> { -- cgit v1.2.3