aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-03-28 11:28:31 +0000
committerGitHub <[email protected]>2020-03-28 11:28:31 +0000
commitc30425dc96895117b644f29b758cee9dac36839b (patch)
treed3ccef4aa8f681cc9de29f0435ad20e87911a6ba /crates/ra_syntax
parenta1fea0d34ee8f3436aefd87d4c133a7ff50ffbb0 (diff)
parent311cbbdad599d51c6f08f7dd72c299f7c0128bb2 (diff)
Merge #3753
3753: Introduce stdx crate r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/Cargo.toml2
-rw-r--r--crates/ra_syntax/src/ast/make.rs15
-rw-r--r--crates/ra_syntax/src/lib.rs7
3 files changed, 14 insertions, 10 deletions
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"
18arrayvec = "0.5.1" 18arrayvec = "0.5.1"
19once_cell = "1.3.1" 19once_cell = "1.3.1"
20 20
21stdx = { path = "../stdx" }
22
21ra_text_edit = { path = "../ra_text_edit" } 23ra_text_edit = { path = "../ra_text_edit" }
22ra_parser = { path = "../ra_parser" } 24ra_parser = { path = "../ra_parser" }
23 25
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.
3use itertools::Itertools; 3use itertools::Itertools;
4use stdx::format_to;
4 5
5use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; 6use 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
84pub fn block_from_expr(e: ast::Expr) -> ast::Block { 85pub 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)]
33pub mod fuzz; 33pub mod fuzz;
34 34
35use std::{fmt::Write, marker::PhantomData, sync::Arc}; 35use std::{marker::PhantomData, sync::Arc};
36 36
37use ra_text_edit::AtomTextEdit; 37use ra_text_edit::AtomTextEdit;
38use stdx::format_to;
38 39
39use crate::syntax_node::GreenNode; 40use 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,