mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Reduce allocations in Expand macro
This commit is contained in:
parent
eb248d85a0
commit
48b946bde1
1 changed files with 30 additions and 13 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
use std::iter;
|
||||||
|
|
||||||
use hir::Semantics;
|
use hir::Semantics;
|
||||||
use ide_db::RootDatabase;
|
use ide_db::RootDatabase;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
|
@ -91,27 +93,42 @@ fn insert_whitespaces(syn: SyntaxNode) -> String {
|
||||||
let is_last =
|
let is_last =
|
||||||
|f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
|
|f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
|
||||||
|
|
||||||
res += &match token.kind() {
|
match token.kind() {
|
||||||
k if is_text(k) && is_next(|it| !it.is_punct(), true) => token.text().to_string() + " ",
|
k if is_text(k) && is_next(|it| !it.is_punct(), true) => {
|
||||||
|
res.push_str(token.text());
|
||||||
|
res.push(' ');
|
||||||
|
}
|
||||||
L_CURLY if is_next(|it| it != R_CURLY, true) => {
|
L_CURLY if is_next(|it| it != R_CURLY, true) => {
|
||||||
indent += 1;
|
indent += 1;
|
||||||
let leading_space = if is_last(is_text, false) { " " } else { "" };
|
if is_last(is_text, false) {
|
||||||
format!("{}{{\n{}", leading_space, " ".repeat(indent))
|
res.push(' ');
|
||||||
|
}
|
||||||
|
res.push_str("{\n");
|
||||||
|
res.extend(iter::repeat(" ").take(2 * indent));
|
||||||
}
|
}
|
||||||
R_CURLY if is_last(|it| it != L_CURLY, true) => {
|
R_CURLY if is_last(|it| it != L_CURLY, true) => {
|
||||||
indent = indent.saturating_sub(1);
|
indent = indent.saturating_sub(1);
|
||||||
format!("\n{}}}", " ".repeat(indent))
|
res.push('\n');
|
||||||
|
res.extend(iter::repeat(" ").take(2 * indent));
|
||||||
|
res.push_str("}");
|
||||||
|
}
|
||||||
|
R_CURLY => {
|
||||||
|
res.push_str("}\n");
|
||||||
|
res.extend(iter::repeat(" ").take(2 * indent));
|
||||||
}
|
}
|
||||||
R_CURLY => format!("}}\n{}", " ".repeat(indent)),
|
|
||||||
LIFETIME_IDENT if is_next(|it| it == IDENT, true) => {
|
LIFETIME_IDENT if is_next(|it| it == IDENT, true) => {
|
||||||
format!("{} ", token.text().to_string())
|
res.push_str(token.text());
|
||||||
|
res.push(' ');
|
||||||
|
}
|
||||||
|
T![;] => {
|
||||||
|
res.push_str(";\n");
|
||||||
|
res.extend(iter::repeat(" ").take(2 * indent));
|
||||||
|
}
|
||||||
|
T![->] => res.push_str(" -> "),
|
||||||
|
T![=] => res.push_str(" = "),
|
||||||
|
T![=>] => res.push_str(" => "),
|
||||||
|
_ => res.push_str(token.text()),
|
||||||
}
|
}
|
||||||
T![;] => format!(";\n{}", " ".repeat(indent)),
|
|
||||||
T![->] => " -> ".to_string(),
|
|
||||||
T![=] => " = ".to_string(),
|
|
||||||
T![=>] => " => ".to_string(),
|
|
||||||
_ => token.text().to_string(),
|
|
||||||
};
|
|
||||||
|
|
||||||
last = Some(token.kind());
|
last = Some(token.kind());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue