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 | |
parent | b764c38436fcb9426eb7da3be4f5fbcd63b316f5 (diff) |
Nice string formatting
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir_def/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres.rs | 5 | ||||
-rw-r--r-- | crates/ra_hir_ty/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/test_db.rs | 3 | ||||
-rw-r--r-- | crates/ra_syntax/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 15 | ||||
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 7 | ||||
-rw-r--r-- | crates/rust-analyzer/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/rust-analyzer/src/world.rs | 15 | ||||
-rw-r--r-- | crates/stdx/src/lib.rs | 2 |
10 files changed, 34 insertions, 21 deletions
diff --git a/crates/ra_hir_def/Cargo.toml b/crates/ra_hir_def/Cargo.toml index 30a12337e..56e791e3e 100644 --- a/crates/ra_hir_def/Cargo.toml +++ b/crates/ra_hir_def/Cargo.toml | |||
@@ -15,6 +15,8 @@ either = "1.5.3" | |||
15 | anymap = "0.12.1" | 15 | anymap = "0.12.1" |
16 | drop_bomb = "0.1.4" | 16 | drop_bomb = "0.1.4" |
17 | 17 | ||
18 | stdx = { path = "../stdx" } | ||
19 | |||
18 | ra_arena = { path = "../ra_arena" } | 20 | ra_arena = { path = "../ra_arena" } |
19 | ra_db = { path = "../ra_db" } | 21 | ra_db = { path = "../ra_db" } |
20 | ra_syntax = { path = "../ra_syntax" } | 22 | ra_syntax = { path = "../ra_syntax" } |
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 40bdc34f5..f279c2ad4 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs | |||
@@ -63,6 +63,7 @@ use ra_db::{CrateId, Edition, FileId}; | |||
63 | use ra_prof::profile; | 63 | use ra_prof::profile; |
64 | use ra_syntax::ast; | 64 | use ra_syntax::ast; |
65 | use rustc_hash::FxHashMap; | 65 | use rustc_hash::FxHashMap; |
66 | use stdx::format_to; | ||
66 | 67 | ||
67 | use crate::{ | 68 | use crate::{ |
68 | db::DefDatabase, | 69 | db::DefDatabase, |
@@ -246,7 +247,7 @@ impl CrateDefMap { | |||
246 | entries.sort_by_key(|(name, _)| name.clone()); | 247 | entries.sort_by_key(|(name, _)| name.clone()); |
247 | 248 | ||
248 | for (name, def) in entries { | 249 | for (name, def) in entries { |
249 | *buf += &format!("{}:", name); | 250 | format_to!(buf, "{}:", name); |
250 | 251 | ||
251 | if def.types.is_some() { | 252 | if def.types.is_some() { |
252 | *buf += " t"; | 253 | *buf += " t"; |
@@ -265,7 +266,7 @@ impl CrateDefMap { | |||
265 | } | 266 | } |
266 | 267 | ||
267 | for (name, child) in map.modules[module].children.iter() { | 268 | for (name, child) in map.modules[module].children.iter() { |
268 | let path = path.to_string() + &format!("::{}", name); | 269 | let path = &format!("{}::{}", path, name); |
269 | go(buf, map, &path, *child); | 270 | go(buf, map, &path, *child); |
270 | } | 271 | } |
271 | } | 272 | } |
diff --git a/crates/ra_hir_ty/Cargo.toml b/crates/ra_hir_ty/Cargo.toml index 9962112db..5a58d70cf 100644 --- a/crates/ra_hir_ty/Cargo.toml +++ b/crates/ra_hir_ty/Cargo.toml | |||
@@ -13,6 +13,8 @@ ena = "0.13.1" | |||
13 | log = "0.4.8" | 13 | log = "0.4.8" |
14 | rustc-hash = "1.1.0" | 14 | rustc-hash = "1.1.0" |
15 | 15 | ||
16 | stdx = { path = "../stdx" } | ||
17 | |||
16 | hir_def = { path = "../ra_hir_def", package = "ra_hir_def" } | 18 | hir_def = { path = "../ra_hir_def", package = "ra_hir_def" } |
17 | hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" } | 19 | hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" } |
18 | ra_arena = { path = "../ra_arena" } | 20 | ra_arena = { path = "../ra_arena" } |
diff --git a/crates/ra_hir_ty/src/test_db.rs b/crates/ra_hir_ty/src/test_db.rs index 5bbeabf51..208096aab 100644 --- a/crates/ra_hir_ty/src/test_db.rs +++ b/crates/ra_hir_ty/src/test_db.rs | |||
@@ -10,6 +10,7 @@ use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink}; | |||
10 | use ra_db::{ | 10 | use ra_db::{ |
11 | salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, Upcast, | 11 | salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, Upcast, |
12 | }; | 12 | }; |
13 | use stdx::format_to; | ||
13 | 14 | ||
14 | use crate::{db::HirDatabase, expr::ExprValidator}; | 15 | use crate::{db::HirDatabase, expr::ExprValidator}; |
15 | 16 | ||
@@ -131,7 +132,7 @@ impl TestDB { | |||
131 | for f in fns { | 132 | for f in fns { |
132 | let infer = self.infer(f.into()); | 133 | let infer = self.infer(f.into()); |
133 | let mut sink = DiagnosticSink::new(|d| { | 134 | let mut sink = DiagnosticSink::new(|d| { |
134 | buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); | 135 | format_to!(buf, "{:?}: {}\n", d.syntax_node(self).text(), d.message()); |
135 | }); | 136 | }); |
136 | infer.add_diagnostics(self, f, &mut sink); | 137 | infer.add_diagnostics(self, f, &mut sink); |
137 | let mut validator = ExprValidator::new(f, infer, &mut sink); | 138 | let mut validator = ExprValidator::new(f, infer, &mut sink); |
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" | |||
18 | arrayvec = "0.5.1" | 18 | arrayvec = "0.5.1" |
19 | once_cell = "1.3.1" | 19 | once_cell = "1.3.1" |
20 | 20 | ||
21 | stdx = { path = "../stdx" } | ||
22 | |||
21 | ra_text_edit = { path = "../ra_text_edit" } | 23 | ra_text_edit = { path = "../ra_text_edit" } |
22 | ra_parser = { path = "../ra_parser" } | 24 | ra_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. |
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, |
diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index e071e9b8d..8fe6799d2 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml | |||
@@ -30,6 +30,8 @@ serde = { version = "1.0.104", features = ["derive"] } | |||
30 | serde_json = "1.0.48" | 30 | serde_json = "1.0.48" |
31 | threadpool = "1.7.1" | 31 | threadpool = "1.7.1" |
32 | 32 | ||
33 | stdx = { path = "../stdx" } | ||
34 | |||
33 | lsp-server = "0.3.1" | 35 | lsp-server = "0.3.1" |
34 | ra_cargo_watch = { path = "../ra_cargo_watch" } | 36 | ra_cargo_watch = { path = "../ra_cargo_watch" } |
35 | ra_ide = { path = "../ra_ide" } | 37 | ra_ide = { path = "../ra_ide" } |
diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs index de85bb017..64a7b907e 100644 --- a/crates/rust-analyzer/src/world.rs +++ b/crates/rust-analyzer/src/world.rs | |||
@@ -19,6 +19,7 @@ use ra_ide::{ | |||
19 | use ra_project_model::{get_rustc_cfg_options, ProcMacroClient, ProjectWorkspace}; | 19 | use ra_project_model::{get_rustc_cfg_options, ProcMacroClient, ProjectWorkspace}; |
20 | use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; | 20 | use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; |
21 | use relative_path::RelativePathBuf; | 21 | use relative_path::RelativePathBuf; |
22 | use stdx::format_to; | ||
22 | 23 | ||
23 | use crate::{ | 24 | use crate::{ |
24 | diagnostics::{CheckFixes, DiagnosticCollection}, | 25 | diagnostics::{CheckFixes, DiagnosticCollection}, |
@@ -319,23 +320,23 @@ impl WorldSnapshot { | |||
319 | } | 320 | } |
320 | 321 | ||
321 | pub fn status(&self) -> String { | 322 | pub fn status(&self) -> String { |
322 | let mut res = String::new(); | 323 | let mut buf = String::new(); |
323 | if self.workspaces.is_empty() { | 324 | if self.workspaces.is_empty() { |
324 | res.push_str("no workspaces\n") | 325 | buf.push_str("no workspaces\n") |
325 | } else { | 326 | } else { |
326 | res.push_str("workspaces:\n"); | 327 | buf.push_str("workspaces:\n"); |
327 | for w in self.workspaces.iter() { | 328 | for w in self.workspaces.iter() { |
328 | res += &format!("{} packages loaded\n", w.n_packages()); | 329 | format_to!(buf, "{} packages loaded\n", w.n_packages()); |
329 | } | 330 | } |
330 | } | 331 | } |
331 | res.push_str("\nanalysis:\n"); | 332 | buf.push_str("\nanalysis:\n"); |
332 | res.push_str( | 333 | buf.push_str( |
333 | &self | 334 | &self |
334 | .analysis | 335 | .analysis |
335 | .status() | 336 | .status() |
336 | .unwrap_or_else(|_| "Analysis retrieval was cancelled".to_owned()), | 337 | .unwrap_or_else(|_| "Analysis retrieval was cancelled".to_owned()), |
337 | ); | 338 | ); |
338 | res | 339 | buf |
339 | } | 340 | } |
340 | 341 | ||
341 | pub fn workspace_root_for(&self, file_id: FileId) -> Option<&Path> { | 342 | pub fn workspace_root_for(&self, file_id: FileId) -> Option<&Path> { |
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index a01ca6d4d..8492c17af 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -21,7 +21,7 @@ where | |||
21 | I: Iterator, | 21 | I: Iterator, |
22 | I::Item: fmt::Display, | 22 | I::Item: fmt::Display, |
23 | { | 23 | { |
24 | fn sep_by<'a>(self, sep: &'a std::primitive::str) -> SepByBuilder<'a, Self> { | 24 | fn sep_by<'a>(self, sep: &'a str) -> SepByBuilder<'a, Self> { |
25 | SepByBuilder::new(sep, self) | 25 | SepByBuilder::new(sep, self) |
26 | } | 26 | } |
27 | } | 27 | } |