Skip to content

Commit c30425d

Browse files
bors[bot]matklad
andauthored
Merge #3753
3753: Introduce stdx crate r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents a1fea0d + 311cbbd commit c30425d

27 files changed

+229
-153
lines changed

Cargo.lock

+10-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_assists/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ authors = ["rust-analyzer developers"]
88
doctest = false
99

1010
[dependencies]
11-
format-buf = "1.0.0"
12-
join_to_string = "0.1.3"
1311
rustc-hash = "1.1.0"
1412
itertools = "0.9.0"
1513
either = "1.5.3"
1614

15+
stdx = { path = "../stdx" }
16+
1717
ra_syntax = { path = "../ra_syntax" }
1818
ra_text_edit = { path = "../ra_text_edit" }
1919
ra_fmt = { path = "../ra_fmt" }

crates/ra_assists/src/handlers/add_custom_impl.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
//! FIXME: write short doc here
2-
3-
use join_to_string::join;
41
use ra_syntax::{
52
ast::{self, AstNode},
63
Direction, SmolStr,
74
SyntaxKind::{IDENT, WHITESPACE},
85
TextRange, TextUnit,
96
};
7+
use stdx::SepBy;
108

119
use crate::{Assist, AssistCtx, AssistId};
1210

13-
const DERIVE_TRAIT: &str = "derive";
14-
1511
// Assist: add_custom_impl
1612
//
1713
// Adds impl block for derived trait.
@@ -38,7 +34,7 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> {
3834
.descendants_with_tokens()
3935
.filter(|t| t.kind() == IDENT)
4036
.find_map(|i| i.into_token())
41-
.filter(|t| *t.text() == DERIVE_TRAIT)?
37+
.filter(|t| *t.text() == "derive")?
4238
.text()
4339
.clone();
4440

@@ -63,8 +59,7 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> {
6359
.filter(|t| t != trait_token.text())
6460
.collect::<Vec<SmolStr>>();
6561
let has_more_derives = !new_attr_input.is_empty();
66-
let new_attr_input =
67-
join(new_attr_input.iter()).separator(", ").surround_with("(", ")").to_string();
62+
let new_attr_input = new_attr_input.iter().sep_by(", ").surround_with("(", ")").to_string();
6863
let new_attr_input_len = new_attr_input.len();
6964

7065
let mut buf = String::new();
@@ -100,9 +95,10 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> {
10095

10196
#[cfg(test)]
10297
mod tests {
103-
use super::*;
10498
use crate::helpers::{check_assist, check_assist_not_applicable};
10599

100+
use super::*;
101+
106102
#[test]
107103
fn add_custom_impl_for_unique_input() {
108104
check_assist(

crates/ra_assists/src/handlers/add_impl.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use format_buf::format;
2-
use join_to_string::join;
31
use ra_syntax::{
42
ast::{self, AstNode, NameOwner, TypeParamsOwner},
53
TextUnit,
64
};
5+
use stdx::{format_to, SepBy};
76

87
use crate::{Assist, AssistCtx, AssistId};
98

@@ -36,7 +35,7 @@ pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> {
3635
let mut buf = String::new();
3736
buf.push_str("\n\nimpl");
3837
if let Some(type_params) = &type_params {
39-
format!(buf, "{}", type_params.syntax());
38+
format_to!(buf, "{}", type_params.syntax());
4039
}
4140
buf.push_str(" ");
4241
buf.push_str(name.text().as_str());
@@ -47,7 +46,9 @@ pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> {
4746
.map(|it| it.text().clone());
4847
let type_params =
4948
type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone());
50-
join(lifetime_params.chain(type_params)).surround_with("<", ">").to_buf(&mut buf);
49+
50+
let generic_params = lifetime_params.chain(type_params).sep_by(", ");
51+
format_to!(buf, "<{}>", generic_params)
5152
}
5253
buf.push_str(" {\n");
5354
edit.set_cursor(start_offset + TextUnit::of_str(&buf));

crates/ra_assists/src/handlers/add_new.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use std::fmt::Write;
2-
3-
use format_buf::format;
41
use hir::Adt;
5-
use join_to_string::join;
62
use ra_syntax::{
73
ast::{
84
self, AstNode, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner, VisibilityOwner,
95
},
106
TextUnit, T,
117
};
8+
use stdx::{format_to, SepBy};
129

1310
use crate::{Assist, AssistCtx, AssistId};
1411

@@ -53,24 +50,22 @@ pub(crate) fn add_new(ctx: AssistCtx) -> Option<Assist> {
5350
buf.push('\n');
5451
}
5552

56-
let vis = strukt.visibility().map(|v| format!("{} ", v.syntax()));
53+
let vis = strukt.visibility().map(|v| format!("{} ", v));
5754
let vis = vis.as_deref().unwrap_or("");
58-
write!(&mut buf, " {}fn new(", vis).unwrap();
59-
60-
join(field_list.fields().filter_map(|f| {
61-
Some(format!("{}: {}", f.name()?.syntax().text(), f.ascribed_type()?.syntax().text()))
62-
}))
63-
.separator(", ")
64-
.to_buf(&mut buf);
6555

66-
buf.push_str(") -> Self { Self {");
67-
68-
join(field_list.fields().filter_map(|f| Some(f.name()?.syntax().text())))
69-
.separator(", ")
70-
.surround_with(" ", " ")
71-
.to_buf(&mut buf);
56+
let params = field_list
57+
.fields()
58+
.filter_map(|f| {
59+
Some(format!(
60+
"{}: {}",
61+
f.name()?.syntax().text(),
62+
f.ascribed_type()?.syntax().text()
63+
))
64+
})
65+
.sep_by(", ");
66+
let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", ");
7267

73-
buf.push_str("} }");
68+
format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields);
7469

7570
let (start_offset, end_offset) = impl_def
7671
.and_then(|impl_def| {
@@ -103,7 +98,7 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String {
10398
let mut buf = String::with_capacity(code.len());
10499
buf.push_str("\n\nimpl");
105100
if let Some(type_params) = &type_params {
106-
format!(buf, "{}", type_params.syntax());
101+
format_to!(buf, "{}", type_params.syntax());
107102
}
108103
buf.push_str(" ");
109104
buf.push_str(strukt.name().unwrap().text().as_str());
@@ -114,10 +109,10 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String {
114109
.map(|it| it.text().clone());
115110
let type_params =
116111
type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone());
117-
join(lifetime_params.chain(type_params)).surround_with("<", ">").to_buf(&mut buf);
112+
format_to!(buf, "<{}>", lifetime_params.chain(type_params).sep_by(", "))
118113
}
119114

120-
format!(&mut buf, " {{\n{}\n}}\n", code);
115+
format_to!(buf, " {{\n{}\n}}\n", code);
121116

122117
buf
123118
}

crates/ra_assists/src/handlers/introduce_variable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use format_buf::format;
21
use ra_syntax::{
32
ast::{self, AstNode},
43
SyntaxKind::{
@@ -7,6 +6,7 @@ use ra_syntax::{
76
},
87
SyntaxNode, TextUnit,
98
};
9+
use stdx::format_to;
1010
use test_utils::tested_by;
1111

1212
use crate::{Assist, AssistCtx, AssistId};
@@ -52,7 +52,7 @@ pub(crate) fn introduce_variable(ctx: AssistCtx) -> Option<Assist> {
5252
buf.push_str("let var_name = ");
5353
TextUnit::of_str("let ")
5454
};
55-
format!(buf, "{}", expr.syntax());
55+
format_to!(buf, "{}", expr.syntax());
5656
let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone());
5757
let is_full_stmt = if let Some(expr_stmt) = &full_stmt {
5858
Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone())

crates/ra_hir_def/Cargo.toml

+2
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

+3-2
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

+2
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/diagnostics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::any::Any;
44

55
use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile};
66
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr};
7+
use stdx::format_to;
78

89
pub use hir_def::diagnostics::UnresolvedModule;
910
pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink};
@@ -37,12 +38,11 @@ pub struct MissingFields {
3738

3839
impl Diagnostic for MissingFields {
3940
fn message(&self) -> String {
40-
use std::fmt::Write;
41-
let mut message = String::from("Missing structure fields:\n");
41+
let mut buf = String::from("Missing structure fields:\n");
4242
for field in &self.missed_fields {
43-
writeln!(message, "- {}", field).unwrap();
43+
format_to!(buf, "- {}", field);
4444
}
45-
message
45+
buf
4646
}
4747
fn source(&self) -> InFile<SyntaxNodePtr> {
4848
InFile { file_id: self.file, value: self.field_list.into() }

crates/ra_hir_ty/src/test_db.rs

+2-1
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);

0 commit comments

Comments
 (0)