Skip to content

Commit 6596e7c

Browse files
committed
Nice string formatting
1 parent b764c38 commit 6596e7c

File tree

11 files changed

+38
-21
lines changed

11 files changed

+38
-21
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_hir_def/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ either = "1.5.3"
1515
anymap = "0.12.1"
1616
drop_bomb = "0.1.4"
1717

18+
stdx = { path = "../stdx" }
19+
1820
ra_arena = { path = "../ra_arena" }
1921
ra_db = { path = "../ra_db" }
2022
ra_syntax = { path = "../ra_syntax" }

crates/ra_hir_def/src/nameres.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ use ra_db::{CrateId, Edition, FileId};
6363
use ra_prof::profile;
6464
use ra_syntax::ast;
6565
use rustc_hash::FxHashMap;
66+
use stdx::format_to;
6667

6768
use crate::{
6869
db::DefDatabase,
@@ -246,7 +247,7 @@ impl CrateDefMap {
246247
entries.sort_by_key(|(name, _)| name.clone());
247248

248249
for (name, def) in entries {
249-
*buf += &format!("{}:", name);
250+
format_to!(buf, "{}:", name);
250251

251252
if def.types.is_some() {
252253
*buf += " t";
@@ -265,7 +266,7 @@ impl CrateDefMap {
265266
}
266267

267268
for (name, child) in map.modules[module].children.iter() {
268-
let path = path.to_string() + &format!("::{}", name);
269+
let path = &format!("{}::{}", path, name);
269270
go(buf, map, &path, *child);
270271
}
271272
}

crates/ra_hir_ty/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ ena = "0.13.1"
1313
log = "0.4.8"
1414
rustc-hash = "1.1.0"
1515

16+
stdx = { path = "../stdx" }
17+
1618
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
1719
hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
1820
ra_arena = { path = "../ra_arena" }

crates/ra_hir_ty/src/test_db.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink};
1010
use ra_db::{
1111
salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, Upcast,
1212
};
13+
use stdx::format_to;
1314

1415
use crate::{db::HirDatabase, expr::ExprValidator};
1516

@@ -131,7 +132,7 @@ impl TestDB {
131132
for f in fns {
132133
let infer = self.infer(f.into());
133134
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());
135136
});
136137
infer.add_diagnostics(self, f, &mut sink);
137138
let mut validator = ExprValidator::new(f, infer, &mut sink);

crates/ra_syntax/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ rustc-hash = "1.1.0"
1818
arrayvec = "0.5.1"
1919
once_cell = "1.3.1"
2020

21+
stdx = { path = "../stdx" }
22+
2123
ra_text_edit = { path = "../ra_text_edit" }
2224
ra_parser = { path = "../ra_parser" }
2325

crates/ra_syntax/src/ast/make.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! This module contains free-standing functions for creating AST fragments out
22
//! of smaller pieces.
33
use itertools::Itertools;
4+
use stdx::format_to;
45

56
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken};
67

@@ -34,14 +35,14 @@ pub fn use_tree(
3435
let mut buf = "use ".to_string();
3536
buf += &path.syntax().to_string();
3637
if let Some(use_tree_list) = use_tree_list {
37-
buf += &format!("::{}", use_tree_list);
38+
format_to!(buf, "::{}", use_tree_list);
3839
}
3940
if add_star {
4041
buf += "::*";
4142
}
4243

4344
if let Some(alias) = alias {
44-
buf += &format!(" {}", alias);
45+
format_to!(buf, " {}", alias);
4546
}
4647
ast_from_text(&buf)
4748
}
@@ -70,15 +71,15 @@ pub fn block_expr(
7071
stmts: impl IntoIterator<Item = ast::Stmt>,
7172
tail_expr: Option<ast::Expr>,
7273
) -> ast::BlockExpr {
73-
let mut text = "{\n".to_string();
74+
let mut buf = "{\n".to_string();
7475
for stmt in stmts.into_iter() {
75-
text += &format!(" {}\n", stmt);
76+
format_to!(buf, " {}\n", stmt);
7677
}
7778
if let Some(tail_expr) = tail_expr {
78-
text += &format!(" {}\n", tail_expr)
79+
format_to!(buf, " {}\n", tail_expr)
7980
}
80-
text += "}";
81-
ast_from_text(&format!("fn f() {}", text))
81+
buf += "}";
82+
ast_from_text(&format!("fn f() {}", buf))
8283
}
8384

8485
pub fn block_from_expr(e: ast::Expr) -> ast::Block {

crates/ra_syntax/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ pub mod ast;
3232
#[doc(hidden)]
3333
pub mod fuzz;
3434

35-
use std::{fmt::Write, marker::PhantomData, sync::Arc};
35+
use std::{marker::PhantomData, sync::Arc};
3636

3737
use ra_text_edit::AtomTextEdit;
38+
use stdx::format_to;
3839

3940
use crate::syntax_node::GreenNode;
4041

@@ -115,7 +116,7 @@ impl Parse<SourceFile> {
115116
pub fn debug_dump(&self) -> String {
116117
let mut buf = format!("{:#?}", self.tree().syntax());
117118
for err in self.errors.iter() {
118-
writeln!(buf, "error {:?}: {}", err.range(), err).unwrap();
119+
format_to!(buf, "error {:?}: {}\n", err.range(), err);
119120
}
120121
buf
121122
}
@@ -296,7 +297,7 @@ fn api_walkthrough() {
296297
NodeOrToken::Node(it) => it.text().to_string(),
297298
NodeOrToken::Token(it) => it.text().to_string(),
298299
};
299-
buf += &format!("{:indent$}{:?} {:?}\n", " ", text, node.kind(), indent = indent);
300+
format_to!(buf, "{:indent$}{:?} {:?}\n", " ", text, node.kind(), indent = indent);
300301
indent += 2;
301302
}
302303
WalkEvent::Leave(_) => indent -= 2,

crates/rust-analyzer/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ serde = { version = "1.0.104", features = ["derive"] }
3030
serde_json = "1.0.48"
3131
threadpool = "1.7.1"
3232

33+
stdx = { path = "../stdx" }
34+
3335
lsp-server = "0.3.1"
3436
ra_cargo_watch = { path = "../ra_cargo_watch" }
3537
ra_ide = { path = "../ra_ide" }

crates/rust-analyzer/src/world.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use ra_ide::{
1919
use ra_project_model::{get_rustc_cfg_options, ProcMacroClient, ProjectWorkspace};
2020
use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch};
2121
use relative_path::RelativePathBuf;
22+
use stdx::format_to;
2223

2324
use crate::{
2425
diagnostics::{CheckFixes, DiagnosticCollection},
@@ -319,23 +320,23 @@ impl WorldSnapshot {
319320
}
320321

321322
pub fn status(&self) -> String {
322-
let mut res = String::new();
323+
let mut buf = String::new();
323324
if self.workspaces.is_empty() {
324-
res.push_str("no workspaces\n")
325+
buf.push_str("no workspaces\n")
325326
} else {
326-
res.push_str("workspaces:\n");
327+
buf.push_str("workspaces:\n");
327328
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());
329330
}
330331
}
331-
res.push_str("\nanalysis:\n");
332-
res.push_str(
332+
buf.push_str("\nanalysis:\n");
333+
buf.push_str(
333334
&self
334335
.analysis
335336
.status()
336337
.unwrap_or_else(|_| "Analysis retrieval was cancelled".to_owned()),
337338
);
338-
res
339+
buf
339340
}
340341

341342
pub fn workspace_root_for(&self, file_id: FileId) -> Option<&Path> {

crates/stdx/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ where
2121
I: Iterator,
2222
I::Item: fmt::Display,
2323
{
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> {
2525
SepByBuilder::new(sep, self)
2626
}
2727
}

0 commit comments

Comments
 (0)