diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-04-08 16:43:30 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-04-08 16:43:30 +0100 |
commit | 94d9fc2a28ea5d97e3a9293b9dac05bdb00304cc (patch) | |
tree | fa143881504d30df6f13d59ab5dd5805129d5690 /crates/ide | |
parent | 72ad5cbe16a170d3c8a4c9d1bc04640b9e8404e0 (diff) | |
parent | 48b946bde16a6f7b89064005f70ea475960e0521 (diff) |
Merge #8421
8421: Reduce allocations in "Expand macro" formatter r=edwin0cheng a=lnicola
Co-authored-by: Laurențiu Nicola <[email protected]>
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/expand_macro.rs | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index d5628e3df..be0ee03bf 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::iter; | ||
2 | |||
1 | use hir::Semantics; | 3 | use hir::Semantics; |
2 | use ide_db::RootDatabase; | 4 | use ide_db::RootDatabase; |
3 | use syntax::{ | 5 | use syntax::{ |
@@ -91,27 +93,42 @@ fn insert_whitespaces(syn: SyntaxNode) -> String { | |||
91 | let is_last = | 93 | let is_last = |
92 | |f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) }; | 94 | |f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) }; |
93 | 95 | ||
94 | res += &match token.kind() { | 96 | match token.kind() { |
95 | k if is_text(k) && is_next(|it| !it.is_punct(), true) => token.text().to_string() + " ", | 97 | k if is_text(k) && is_next(|it| !it.is_punct(), true) => { |
98 | res.push_str(token.text()); | ||
99 | res.push(' '); | ||
100 | } | ||
96 | L_CURLY if is_next(|it| it != R_CURLY, true) => { | 101 | L_CURLY if is_next(|it| it != R_CURLY, true) => { |
97 | indent += 1; | 102 | indent += 1; |
98 | let leading_space = if is_last(is_text, false) { " " } else { "" }; | 103 | if is_last(is_text, false) { |
99 | format!("{}{{\n{}", leading_space, " ".repeat(indent)) | 104 | res.push(' '); |
105 | } | ||
106 | res.push_str("{\n"); | ||
107 | res.extend(iter::repeat(" ").take(2 * indent)); | ||
100 | } | 108 | } |
101 | R_CURLY if is_last(|it| it != L_CURLY, true) => { | 109 | R_CURLY if is_last(|it| it != L_CURLY, true) => { |
102 | indent = indent.saturating_sub(1); | 110 | 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)); | ||
104 | } | 118 | } |
105 | R_CURLY => format!("}}\n{}", " ".repeat(indent)), | ||
106 | LIFETIME_IDENT if is_next(|it| it == IDENT, true) => { | 119 | LIFETIME_IDENT if is_next(|it| it == IDENT, true) => { |
107 | format!("{} ", token.text().to_string()) | 120 | res.push_str(token.text()); |
121 | res.push(' '); | ||
108 | } | 122 | } |
109 | T![;] => format!(";\n{}", " ".repeat(indent)), | 123 | T![;] => { |
110 | T![->] => " -> ".to_string(), | 124 | res.push_str(";\n"); |
111 | T![=] => " = ".to_string(), | 125 | res.extend(iter::repeat(" ").take(2 * indent)); |
112 | T![=>] => " => ".to_string(), | 126 | } |
113 | _ => token.text().to_string(), | 127 | T![->] => res.push_str(" -> "), |
114 | }; | 128 | T![=] => res.push_str(" = "), |
129 | T![=>] => res.push_str(" => "), | ||
130 | _ => res.push_str(token.text()), | ||
131 | } | ||
115 | 132 | ||
116 | last = Some(token.kind()); | 133 | last = Some(token.kind()); |
117 | } | 134 | } |