From 6596e7cddfc00281362c3640781f6cd6bc0b5614 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 28 Mar 2020 11:08:19 +0100 Subject: Nice string formatting --- crates/ra_syntax/Cargo.toml | 2 ++ crates/ra_syntax/src/ast/make.rs | 15 ++++++++------- crates/ra_syntax/src/lib.rs | 7 ++++--- 3 files changed, 14 insertions(+), 10 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml index 6fccc2303..3c6ae77e4 100644 --- a/crates/ra_syntax/Cargo.toml +++ b/crates/ra_syntax/Cargo.toml @@ -18,6 +18,8 @@ rustc-hash = "1.1.0" arrayvec = "0.5.1" once_cell = "1.3.1" +stdx = { path = "../stdx" } + ra_text_edit = { path = "../ra_text_edit" } ra_parser = { path = "../ra_parser" } diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index dbf8e6370..0c908573d 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -1,6 +1,7 @@ //! This module contains free-standing functions for creating AST fragments out //! of smaller pieces. use itertools::Itertools; +use stdx::format_to; use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; @@ -34,14 +35,14 @@ pub fn use_tree( let mut buf = "use ".to_string(); buf += &path.syntax().to_string(); if let Some(use_tree_list) = use_tree_list { - buf += &format!("::{}", use_tree_list); + format_to!(buf, "::{}", use_tree_list); } if add_star { buf += "::*"; } if let Some(alias) = alias { - buf += &format!(" {}", alias); + format_to!(buf, " {}", alias); } ast_from_text(&buf) } @@ -70,15 +71,15 @@ pub fn block_expr( stmts: impl IntoIterator, tail_expr: Option, ) -> ast::BlockExpr { - let mut text = "{\n".to_string(); + let mut buf = "{\n".to_string(); for stmt in stmts.into_iter() { - text += &format!(" {}\n", stmt); + format_to!(buf, " {}\n", stmt); } if let Some(tail_expr) = tail_expr { - text += &format!(" {}\n", tail_expr) + format_to!(buf, " {}\n", tail_expr) } - text += "}"; - ast_from_text(&format!("fn f() {}", text)) + buf += "}"; + ast_from_text(&format!("fn f() {}", buf)) } pub fn block_from_expr(e: ast::Expr) -> ast::Block { diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index cef926ed3..f0e16dc2b 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -32,9 +32,10 @@ pub mod ast; #[doc(hidden)] pub mod fuzz; -use std::{fmt::Write, marker::PhantomData, sync::Arc}; +use std::{marker::PhantomData, sync::Arc}; use ra_text_edit::AtomTextEdit; +use stdx::format_to; use crate::syntax_node::GreenNode; @@ -115,7 +116,7 @@ impl Parse { pub fn debug_dump(&self) -> String { let mut buf = format!("{:#?}", self.tree().syntax()); for err in self.errors.iter() { - writeln!(buf, "error {:?}: {}", err.range(), err).unwrap(); + format_to!(buf, "error {:?}: {}\n", err.range(), err); } buf } @@ -296,7 +297,7 @@ fn api_walkthrough() { NodeOrToken::Node(it) => it.text().to_string(), NodeOrToken::Token(it) => it.text().to_string(), }; - buf += &format!("{:indent$}{:?} {:?}\n", " ", text, node.kind(), indent = indent); + format_to!(buf, "{:indent$}{:?} {:?}\n", " ", text, node.kind(), indent = indent); indent += 2; } WalkEvent::Leave(_) => indent -= 2, -- cgit v1.2.3