diff options
author | Aleksey Kladov <[email protected]> | 2020-03-28 10:08:19 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-03-28 11:27:54 +0000 |
commit | 6596e7cddfc00281362c3640781f6cd6bc0b5614 (patch) | |
tree | 9a2b4f264e37dba87fa80d10ce5872090bcdbfef /crates/ra_syntax/src | |
parent | b764c38436fcb9426eb7da3be4f5fbcd63b316f5 (diff) |
Nice string formatting
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 15 | ||||
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 7 |
2 files changed, 12 insertions, 10 deletions
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 @@ | |||
1 | //! This module contains free-standing functions for creating AST fragments out | 1 | //! This module contains free-standing functions for creating AST fragments out |
2 | //! of smaller pieces. | 2 | //! of smaller pieces. |
3 | use itertools::Itertools; | 3 | use itertools::Itertools; |
4 | use stdx::format_to; | ||
4 | 5 | ||
5 | use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; | 6 | use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; |
6 | 7 | ||
@@ -34,14 +35,14 @@ pub fn use_tree( | |||
34 | let mut buf = "use ".to_string(); | 35 | let mut buf = "use ".to_string(); |
35 | buf += &path.syntax().to_string(); | 36 | buf += &path.syntax().to_string(); |
36 | if let Some(use_tree_list) = use_tree_list { | 37 | if let Some(use_tree_list) = use_tree_list { |
37 | buf += &format!("::{}", use_tree_list); | 38 | format_to!(buf, "::{}", use_tree_list); |
38 | } | 39 | } |
39 | if add_star { | 40 | if add_star { |
40 | buf += "::*"; | 41 | buf += "::*"; |
41 | } | 42 | } |
42 | 43 | ||
43 | if let Some(alias) = alias { | 44 | if let Some(alias) = alias { |
44 | buf += &format!(" {}", alias); | 45 | format_to!(buf, " {}", alias); |
45 | } | 46 | } |
46 | ast_from_text(&buf) | 47 | ast_from_text(&buf) |
47 | } | 48 | } |
@@ -70,15 +71,15 @@ pub fn block_expr( | |||
70 | stmts: impl IntoIterator<Item = ast::Stmt>, | 71 | stmts: impl IntoIterator<Item = ast::Stmt>, |
71 | tail_expr: Option<ast::Expr>, | 72 | tail_expr: Option<ast::Expr>, |
72 | ) -> ast::BlockExpr { | 73 | ) -> ast::BlockExpr { |
73 | let mut text = "{\n".to_string(); | 74 | let mut buf = "{\n".to_string(); |
74 | for stmt in stmts.into_iter() { | 75 | for stmt in stmts.into_iter() { |
75 | text += &format!(" {}\n", stmt); | 76 | format_to!(buf, " {}\n", stmt); |
76 | } | 77 | } |
77 | if let Some(tail_expr) = tail_expr { | 78 | if let Some(tail_expr) = tail_expr { |
78 | text += &format!(" {}\n", tail_expr) | 79 | format_to!(buf, " {}\n", tail_expr) |
79 | } | 80 | } |
80 | text += "}"; | 81 | buf += "}"; |
81 | ast_from_text(&format!("fn f() {}", text)) | 82 | ast_from_text(&format!("fn f() {}", buf)) |
82 | } | 83 | } |
83 | 84 | ||
84 | pub fn block_from_expr(e: ast::Expr) -> ast::Block { | 85 | 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; | |||
32 | #[doc(hidden)] | 32 | #[doc(hidden)] |
33 | pub mod fuzz; | 33 | pub mod fuzz; |
34 | 34 | ||
35 | use std::{fmt::Write, marker::PhantomData, sync::Arc}; | 35 | use std::{marker::PhantomData, sync::Arc}; |
36 | 36 | ||
37 | use ra_text_edit::AtomTextEdit; | 37 | use ra_text_edit::AtomTextEdit; |
38 | use stdx::format_to; | ||
38 | 39 | ||
39 | use crate::syntax_node::GreenNode; | 40 | use crate::syntax_node::GreenNode; |
40 | 41 | ||
@@ -115,7 +116,7 @@ impl Parse<SourceFile> { | |||
115 | pub fn debug_dump(&self) -> String { | 116 | pub fn debug_dump(&self) -> String { |
116 | let mut buf = format!("{:#?}", self.tree().syntax()); | 117 | let mut buf = format!("{:#?}", self.tree().syntax()); |
117 | for err in self.errors.iter() { | 118 | for err in self.errors.iter() { |
118 | writeln!(buf, "error {:?}: {}", err.range(), err).unwrap(); | 119 | format_to!(buf, "error {:?}: {}\n", err.range(), err); |
119 | } | 120 | } |
120 | buf | 121 | buf |
121 | } | 122 | } |
@@ -296,7 +297,7 @@ fn api_walkthrough() { | |||
296 | NodeOrToken::Node(it) => it.text().to_string(), | 297 | NodeOrToken::Node(it) => it.text().to_string(), |
297 | NodeOrToken::Token(it) => it.text().to_string(), | 298 | NodeOrToken::Token(it) => it.text().to_string(), |
298 | }; | 299 | }; |
299 | buf += &format!("{:indent$}{:?} {:?}\n", " ", text, node.kind(), indent = indent); | 300 | format_to!(buf, "{:indent$}{:?} {:?}\n", " ", text, node.kind(), indent = indent); |
300 | indent += 2; | 301 | indent += 2; |
301 | } | 302 | } |
302 | WalkEvent::Leave(_) => indent -= 2, | 303 | WalkEvent::Leave(_) => indent -= 2, |