Skip to content

Commit d393eb7

Browse files
authored
Use cached indentation strings in render queue (#801)
1 parent 9478bdf commit d393eb7

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

librubyfmt/src/render_queue_writer.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ use crate::line_tokens::*;
44
use crate::render_targets::{
55
AbstractTokenTarget, BreakableCallChainEntry, BreakableEntry, ConvertType,
66
};
7+
use crate::util::get_indent;
78
#[cfg(debug_assertions)]
89
use log::debug;
10+
use std::borrow::Cow;
911
use std::io::{self, Write};
1012

1113
pub const MAX_LINE_LENGTH: usize = 120;
@@ -55,9 +57,11 @@ impl<'src> RenderQueueWriter<'src> {
5557
contents,
5658
}) => {
5759
if !contents.is_empty() {
58-
let mut new_contents: String =
59-
(0..(accum.additional_indent * 2)).map(|_| ' ').collect();
60-
new_contents.push_str(contents.as_str());
60+
let new_contents = format!(
61+
"{}{}",
62+
get_indent(accum.additional_indent as usize * 2),
63+
contents
64+
);
6165
next_token = ConcreteLineTokenAndTargets::ConcreteLineToken(
6266
ConcreteLineToken::Comment {
6367
contents: new_contents,
@@ -72,19 +76,16 @@ impl<'src> RenderQueueWriter<'src> {
7276
.map(|k| k.is_squiggly())
7377
.unwrap_or(false)
7478
{
75-
let indent: String =
76-
(0..(accum.additional_indent * 2)).map(|_| ' ').collect();
79+
let indent = get_indent(accum.additional_indent as usize * 2);
7780
let new_contents = part
7881
.split('\n')
7982
.map(|p| {
8083
if p.is_empty() {
81-
return p.to_string();
84+
return p.into();
8285
}
83-
let mut line = indent.clone();
84-
line.push_str(p);
85-
line
86+
format!("{}{}", indent, p).into()
8687
})
87-
.collect::<Vec<String>>()
88+
.collect::<Vec<Cow<'_, str>>>()
8889
.join("\n");
8990
next_token = clats_direct_part(new_contents)
9091
}
@@ -98,9 +99,11 @@ impl<'src> RenderQueueWriter<'src> {
9899
// Bare heredocs (e.g. <<FOO) must have the closing ident completely unindented, so
99100
// ignore them in this case
100101
if current_heredoc_kind.map(|k| !k.is_bare()).unwrap_or(false) {
101-
let mut new_contents: String =
102-
(0..(accum.additional_indent * 2)).map(|_| ' ').collect();
103-
new_contents.push_str(symbol.as_str());
102+
let new_contents: String = format!(
103+
"{}{}",
104+
get_indent(accum.additional_indent as usize * 2),
105+
symbol
106+
);
104107
next_token = clats_heredoc_close(new_contents);
105108
}
106109
current_heredoc_kind = None;

0 commit comments

Comments
 (0)