Skip to content

Commit 3e0711a

Browse files
authored
Merge pull request #427 from alexcrichton/no-recurse
Simplify the `reset_indentation` function
2 parents bae324c + 331a86a commit 3e0711a

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

crates/cli-support/src/lib.rs

+14-26
Original file line numberDiff line numberDiff line change
@@ -438,35 +438,23 @@ impl fmt::Display for MyError {
438438
}
439439

440440
fn reset_indentation(s: &str) -> String {
441-
indent_recurse(s.lines(), 0)
442-
}
441+
let mut indent: u32 = 0;
442+
let mut dst = String::new();
443443

444-
fn indent_recurse(mut lines: ::std::str::Lines, current_indent: usize) -> String {
445-
if let Some(line) = lines.next() {
446-
let mut trimmed = line.trim().to_owned();
447-
let mut next_indent = current_indent;
448-
let mut current_indent = current_indent;
449-
if trimmed.ends_with('{') {
450-
next_indent += 1;
444+
for line in s.lines() {
445+
let line = line.trim();
446+
if line.starts_with('}') || line.ends_with('}') {
447+
indent = indent.saturating_sub(1);
451448
}
452-
if trimmed.starts_with('}') || trimmed.ends_with('}') {
453-
if current_indent > 0 {
454-
current_indent -= 1;
455-
}
456-
if next_indent > 0 {
457-
next_indent -= 1;
458-
}
449+
let extra = if line.starts_with(':') || line.starts_with('?') { 1 } else { 0 };
450+
for _ in 0..indent + extra {
451+
dst.push_str(" ");
459452
}
460-
if trimmed.starts_with('?') || trimmed.starts_with(':') {
461-
current_indent += 1;
453+
dst.push_str(line);
454+
dst.push_str("\n");
455+
if line.ends_with('{') {
456+
indent += 1;
462457
}
463-
format!(
464-
"\n{}{}{}",
465-
" ".repeat(current_indent),
466-
&trimmed,
467-
&indent_recurse(lines, next_indent)
468-
)
469-
} else {
470-
String::new()
471458
}
459+
return dst
472460
}

0 commit comments

Comments
 (0)