From 6d5d82e412dea19ea48eecc6f7d5a4aa223a9599 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 20 Jul 2019 12:48:24 +0300 Subject: move debug_dump to fmt::Debug --- crates/ra_cli/src/main.rs | 2 +- crates/ra_hir/src/expr.rs | 2 +- crates/ra_ide_api/src/display/navigation_target.rs | 2 +- crates/ra_ide_api/src/syntax_tree.rs | 6 +-- crates/ra_mbe/src/tests.rs | 8 ++-- crates/ra_syntax/src/fuzz.rs | 6 +-- crates/ra_syntax/src/lib.rs | 2 +- crates/ra_syntax/src/parsing/reparsing.rs | 4 +- crates/ra_syntax/src/syntax_node.rs | 56 ++++++++++------------ crates/ra_syntax/src/validation.rs | 4 +- 10 files changed, 42 insertions(+), 50 deletions(-) (limited to 'crates') diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 375e2f508..de8191ca3 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -55,7 +55,7 @@ fn main() -> Result<()> { let _p = profile("parsing"); let file = file()?; if !matches.is_present("no-dump") { - println!("{}", file.syntax().debug_dump()); + println!("{:#?}", file.syntax()); } std::mem::forget(file); } diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 66cb9633b..6eed67f50 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -843,7 +843,7 @@ where let file_id = call_id.as_file(MacroFileKind::Expr); if let Some(node) = self.db.parse_or_expand(file_id) { if let Some(expr) = ast::Expr::cast(node) { - log::debug!("macro expansion {}", expr.syntax().debug_dump()); + log::debug!("macro expansion {:#?}", expr.syntax()); let old_file_id = std::mem::replace(&mut self.current_file_id, file_id); let id = self.collect_expr(expr); diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 8aff5f2cd..c212ebf28 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -233,7 +233,7 @@ impl NavigationTarget { pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget { let src = macro_call.source(db); - log::debug!("nav target {}", src.ast.syntax().debug_dump()); + log::debug!("nav target {:#?}", src.ast.syntax()); NavigationTarget::from_named( src.file_id.original_file(db), &src.ast, diff --git a/crates/ra_ide_api/src/syntax_tree.rs b/crates/ra_ide_api/src/syntax_tree.rs index 3d7373d02..ae1849aaa 100644 --- a/crates/ra_ide_api/src/syntax_tree.rs +++ b/crates/ra_ide_api/src/syntax_tree.rs @@ -25,9 +25,9 @@ pub(crate) fn syntax_tree( } }; - node.debug_dump() + format!("{:#?}", node) } else { - parse.tree().syntax().debug_dump() + format!("{:#?}", parse.tree().syntax()) } } @@ -85,7 +85,7 @@ fn syntax_tree_for_token(node: &SyntaxToken, text_range: TextRange) -> Option { impl Parse { pub fn debug_dump(&self) -> String { - let mut buf = self.tree().syntax().debug_dump(); + let mut buf = format!("{:#?}", self.tree().syntax()); for err in self.errors.iter() { writeln!(buf, "error {:?}: {}", err.location(), err.kind()).unwrap(); } diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs index b4ad9e019..c7183299b 100644 --- a/crates/ra_syntax/src/parsing/reparsing.rs +++ b/crates/ra_syntax/src/parsing/reparsing.rs @@ -188,8 +188,8 @@ mod tests { }; assert_eq_text!( - &fully_reparsed.tree().syntax().debug_dump(), - &incrementally_reparsed.tree().syntax().debug_dump(), + &format!("{:#?}", fully_reparsed.tree().syntax()), + &format!("{:#?}", incrementally_reparsed.tree().syntax()), ); } diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index 51bae04de..3b20e96e2 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -6,11 +6,7 @@ //! The *real* implementation is in the (language-agnostic) `rowan` crate, this //! modules just wraps its API. -use std::{ - fmt::{self, Write}, - iter::successors, - ops::RangeInclusive, -}; +use std::{fmt, iter::successors, ops::RangeInclusive}; use ra_parser::ParseError; use rowan::GreenNodeBuilder; @@ -36,8 +32,29 @@ pub enum InsertPosition { pub struct SyntaxNode(pub(crate) rowan::cursor::SyntaxNode); impl fmt::Debug for SyntaxNode { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{:?}@{:?}", self.kind(), self.range()) + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if f.alternate() { + let mut level = 0; + for event in self.preorder_with_tokens() { + match event { + WalkEvent::Enter(element) => { + for _ in 0..level { + write!(f, " ")?; + } + match element { + SyntaxElement::Node(node) => writeln!(f, "{:?}", node)?, + SyntaxElement::Token(token) => writeln!(f, "{:?}", token)?, + } + level += 1; + } + WalkEvent::Leave(_) => level -= 1, + } + } + assert_eq!(level, 0); + Ok(()) + } else { + write!(f, "{:?}@{:?}", self.kind(), self.range()) + } } } @@ -173,31 +190,6 @@ impl SyntaxNode { }) } - pub fn debug_dump(&self) -> String { - let mut level = 0; - let mut buf = String::new(); - - for event in self.preorder_with_tokens() { - match event { - WalkEvent::Enter(element) => { - for _ in 0..level { - buf.push_str(" "); - } - match element { - SyntaxElement::Node(node) => writeln!(buf, "{:?}", node).unwrap(), - SyntaxElement::Token(token) => writeln!(buf, "{:?}", token).unwrap(), - } - level += 1; - } - WalkEvent::Leave(_) => level -= 1, - } - } - - assert_eq!(level, 0); - - buf - } - pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { self.0.replace_with(replacement) } diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 7140d10c3..943f88de9 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs @@ -89,9 +89,9 @@ pub(crate) fn validate_block_structure(root: &SyntaxNode) { assert_eq!( node.parent(), pair.parent(), - "\nunpaired curleys:\n{}\n{}\n", + "\nunpaired curleys:\n{}\n{:#?}\n", root.text(), - root.debug_dump(), + root, ); assert!( node.next_sibling().is_none() && pair.prev_sibling().is_none(), -- cgit v1.2.3