Skip to content

Commit 48b946b

Browse files
committed
Reduce allocations in Expand macro
1 parent eb248d8 commit 48b946b

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

crates/ide/src/expand_macro.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::iter;
2+
13
use hir::Semantics;
24
use ide_db::RootDatabase;
35
use syntax::{
@@ -91,27 +93,42 @@ fn insert_whitespaces(syn: SyntaxNode) -> String {
9193
let is_last =
9294
|f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
9395

94-
res += &match token.kind() {
95-
k if is_text(k) && is_next(|it| !it.is_punct(), true) => token.text().to_string() + " ",
96+
match token.kind() {
97+
k if is_text(k) && is_next(|it| !it.is_punct(), true) => {
98+
res.push_str(token.text());
99+
res.push(' ');
100+
}
96101
L_CURLY if is_next(|it| it != R_CURLY, true) => {
97102
indent += 1;
98-
let leading_space = if is_last(is_text, false) { " " } else { "" };
99-
format!("{}{{\n{}", leading_space, " ".repeat(indent))
103+
if is_last(is_text, false) {
104+
res.push(' ');
105+
}
106+
res.push_str("{\n");
107+
res.extend(iter::repeat(" ").take(2 * indent));
100108
}
101109
R_CURLY if is_last(|it| it != L_CURLY, true) => {
102110
indent = indent.saturating_sub(1);
103-
format!("\n{}}}", " ".repeat(indent))
111+
res.push('\n');
112+
res.extend(iter::repeat(" ").take(2 * indent));
113+
res.push_str("}");
114+
}
115+
R_CURLY => {
116+
res.push_str("}\n");
117+
res.extend(iter::repeat(" ").take(2 * indent));
104118
}
105-
R_CURLY => format!("}}\n{}", " ".repeat(indent)),
106119
LIFETIME_IDENT if is_next(|it| it == IDENT, true) => {
107-
format!("{} ", token.text().to_string())
120+
res.push_str(token.text());
121+
res.push(' ');
108122
}
109-
T![;] => format!(";\n{}", " ".repeat(indent)),
110-
T![->] => " -> ".to_string(),
111-
T![=] => " = ".to_string(),
112-
T![=>] => " => ".to_string(),
113-
_ => token.text().to_string(),
114-
};
123+
T![;] => {
124+
res.push_str(";\n");
125+
res.extend(iter::repeat(" ").take(2 * indent));
126+
}
127+
T![->] => res.push_str(" -> "),
128+
T![=] => res.push_str(" = "),
129+
T![=>] => res.push_str(" => "),
130+
_ => res.push_str(token.text()),
131+
}
115132

116133
last = Some(token.kind());
117134
}

0 commit comments

Comments
 (0)