|
| 1 | +use std::iter; |
| 2 | + |
1 | 3 | use hir::Semantics;
|
2 | 4 | use ide_db::RootDatabase;
|
3 | 5 | use syntax::{
|
@@ -91,27 +93,42 @@ fn insert_whitespaces(syn: SyntaxNode) -> String {
|
91 | 93 | let is_last =
|
92 | 94 | |f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
|
93 | 95 |
|
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 | + } |
96 | 101 | L_CURLY if is_next(|it| it != R_CURLY, true) => {
|
97 | 102 | 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)); |
100 | 108 | }
|
101 | 109 | R_CURLY if is_last(|it| it != L_CURLY, true) => {
|
102 | 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 | 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)), |
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 | + } |
115 | 132 |
|
116 | 133 | last = Some(token.kind());
|
117 | 134 | }
|
|
0 commit comments