Skip to content

Commit 21c255f

Browse files
authored
Use &str refs for heredoc start tokens (#776)
1 parent dd57974 commit 21c255f

4 files changed

Lines changed: 64 additions & 34 deletions

File tree

librubyfmt/src/format_prism.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -667,9 +667,7 @@ fn format_string_node<'src>(ps: &mut ParserState<'src>, string_node: prism::Stri
667667
format_heredoc(
668668
ps,
669669
HeredocNodeType::Plain(string_node),
670-
opener
671-
.expect("Heredocs must have an opening loc for the opening tag (<<FOO etc.)")
672-
.to_string(),
670+
opener.expect("Heredocs must have an opening loc for the opening tag (<<FOO etc.)"),
673671
);
674672
return;
675673
}
@@ -758,9 +756,7 @@ fn format_interpolated_string_node<'src>(
758756
format_heredoc(
759757
ps,
760758
HeredocNodeType::Interpolated(interpolated_string_node),
761-
opener
762-
.expect("Heredocs must have an opening loc for the opening tag (<<FOO etc.)")
763-
.to_string(),
759+
opener.expect("Heredocs must have an opening loc for the opening tag (<<FOO etc.)"),
764760
);
765761
// The rest of this machinery is handled in format_inner_string
766762
// From here on out, assume we're not in a heredoc
@@ -862,14 +858,14 @@ impl<'src> HeredocNodeType<'src> {
862858
fn format_heredoc<'src>(
863859
ps: &mut ParserState<'src>,
864860
heredoc: HeredocNodeType<'src>,
865-
heredoc_symbol: String,
861+
heredoc_symbol: &'src str,
866862
) {
867-
let heredoc_kind = HeredocKind::from_string(&heredoc_symbol);
863+
let heredoc_kind = HeredocKind::from_string(heredoc_symbol);
868864
ps.emit_heredoc_start(heredoc_symbol, heredoc_kind);
869865

870866
let parts = heredoc.parts();
871867
ps.push_heredoc_content(
872-
loc_to_str(heredoc.closing_loc()).trim().to_string(),
868+
loc_to_str(heredoc.closing_loc()).trim(),
873869
heredoc_kind,
874870
ps.get_line_number_for_offset(heredoc.closing_loc().start_offset()),
875871
|n: &mut ParserState<'src>| {

librubyfmt/src/heredoc_string.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
use crate::types::ColNumber;
24
use crate::util::get_indent;
35

@@ -29,15 +31,15 @@ impl HeredocKind {
2931
}
3032

3133
#[derive(Debug, Clone)]
32-
pub struct HeredocString {
33-
symbol: String,
34+
pub struct HeredocString<'src> {
35+
symbol: Cow<'src, str>,
3436
pub kind: HeredocKind,
3537
pub buf: Vec<u8>,
3638
pub indent: ColNumber,
3739
}
3840

39-
impl HeredocString {
40-
pub fn new(symbol: String, kind: HeredocKind, buf: Vec<u8>, indent: ColNumber) -> Self {
41+
impl<'src> HeredocString<'src> {
42+
pub fn new(symbol: Cow<'src, str>, kind: HeredocKind, buf: Vec<u8>, indent: ColNumber) -> Self {
4143
HeredocString {
4244
symbol,
4345
kind,

librubyfmt/src/line_tokens.rs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,28 @@ pub fn clats_indent<'src>(depth: ColNumber) -> ConcreteLineTokenAndTargets<'src>
2929
#[derive(Debug, Clone, PartialEq, Eq)]
3030
pub enum ConcreteLineToken<'src> {
3131
HardNewLine,
32-
Indent { depth: u32 },
33-
Keyword { keyword: &'static str },
32+
Indent {
33+
depth: u32,
34+
},
35+
Keyword {
36+
keyword: &'static str,
37+
},
3438
DefKeyword,
3539
ClassKeyword,
3640
ModuleKeyword,
3741
DoKeyword,
38-
ModKeyword { contents: &'static str },
39-
ConditionalKeyword { contents: &'static str },
40-
DirectPart { part: Cow<'src, str> },
41-
MethodName { name: Cow<'src, str> },
42+
ModKeyword {
43+
contents: &'static str,
44+
},
45+
ConditionalKeyword {
46+
contents: &'static str,
47+
},
48+
DirectPart {
49+
part: Cow<'src, str>,
50+
},
51+
MethodName {
52+
name: Cow<'src, str>,
53+
},
4254
CommaSpace,
4355
Comma,
4456
Space,
@@ -52,21 +64,34 @@ pub enum ConcreteLineToken<'src> {
5264
CloseCurlyBracket,
5365
OpenParen,
5466
CloseParen,
55-
Op { op: Cow<'src, str> },
67+
Op {
68+
op: Cow<'src, str>,
69+
},
5670
DoubleQuote,
57-
LTStringContent { content: Cow<'src, str> },
71+
LTStringContent {
72+
content: Cow<'src, str>,
73+
},
5874
SingleSlash,
59-
Comment { contents: String },
60-
Delim { contents: &'static str },
75+
Comment {
76+
contents: String,
77+
},
78+
Delim {
79+
contents: &'static str,
80+
},
6181
End,
62-
HeredocClose { symbol: String },
82+
HeredocClose {
83+
symbol: String,
84+
},
6385
DataEnd,
6486
// These are "magic" tokens. They have no concrete representation,
6587
// but they're meaningful inside of the render queue
6688
AfterCallChain,
6789
BeginCallChainIndent,
6890
EndCallChainIndent,
69-
HeredocStart { kind: HeredocKind, symbol: String },
91+
HeredocStart {
92+
kind: HeredocKind,
93+
symbol: Cow<'src, str>,
94+
},
7095
}
7196

7297
impl<'src> ConcreteLineToken<'src> {
@@ -105,7 +130,7 @@ impl<'src> ConcreteLineToken<'src> {
105130
Self::End => Cow::Borrowed("end"),
106131
Self::HeredocClose { symbol } => Cow::Owned(symbol),
107132
Self::DataEnd => Cow::Borrowed("__END__"),
108-
Self::HeredocStart { symbol, .. } => Cow::Owned(symbol),
133+
Self::HeredocStart { symbol, .. } => symbol,
109134
// no-op, this is purely semantic information
110135
// for the render queue
111136
Self::AfterCallChain | Self::BeginCallChainIndent | Self::EndCallChainIndent => {
@@ -246,8 +271,8 @@ impl<'src> ConcreteLineTokenAndTargets<'src> {
246271
pub enum AbstractLineToken<'src> {
247272
// this is all bodil's fault
248273
ConcreteLineToken(ConcreteLineToken<'src>),
249-
CollapsingNewLine(Option<Vec<HeredocString>>),
250-
SoftNewline(Option<Vec<HeredocString>>),
274+
CollapsingNewLine(Option<Vec<HeredocString<'src>>>),
275+
SoftNewline(Option<Vec<HeredocString<'src>>>),
251276
SoftIndent { depth: u32 },
252277
BreakableEntry(BreakableEntry<'src>),
253278
BreakableCallChainEntry(BreakableCallChainEntry<'src>),

librubyfmt/src/parser_state.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub struct ParserState<'src> {
6363
render_queue: BaseQueue<'src>,
6464
current_orig_line_number: LineNumber,
6565
comments_hash: FileComments,
66-
heredoc_strings: Vec<HeredocString>,
66+
heredoc_strings: Vec<HeredocString<'src>>,
6767
comments_to_insert: Option<CommentBlock>,
6868
breakable_entry_stack: Vec<Breakable<'src>>,
6969
formatting_context: Vec<FormattingContext>,
@@ -97,7 +97,7 @@ impl<'src> ParserState<'src> {
9797
}
9898
pub(crate) fn push_heredoc_content<F>(
9999
&mut self,
100-
symbol: String,
100+
symbol: impl Into<Cow<'src, str>>,
101101
kind: HeredocKind,
102102
end_line: LineNumber,
103103
formatting_func: F,
@@ -119,15 +119,22 @@ impl<'src> ParserState<'src> {
119119

120120
let data = next_ps.render_to_buffer();
121121
self.heredoc_strings.push(HeredocString::new(
122-
symbol,
122+
symbol.into(),
123123
kind,
124124
data,
125125
self.current_spaces(),
126126
));
127127
}
128128

129-
pub(crate) fn emit_heredoc_start(&mut self, symbol: String, kind: HeredocKind) {
130-
self.push_concrete_token(ConcreteLineToken::HeredocStart { kind, symbol });
129+
pub(crate) fn emit_heredoc_start(
130+
&mut self,
131+
symbol: impl Into<Cow<'src, str>>,
132+
kind: HeredocKind,
133+
) {
134+
self.push_concrete_token(ConcreteLineToken::HeredocStart {
135+
kind,
136+
symbol: symbol.into(),
137+
});
131138
}
132139

133140
pub(crate) fn emit_heredoc_close(&mut self, symbol: String) {
@@ -792,7 +799,7 @@ impl<'src> ParserState<'src> {
792799
self.render_queue.into_tokens()
793800
}
794801

795-
pub(crate) fn gather_heredocs(&mut self) -> Option<Vec<HeredocString>> {
802+
pub(crate) fn gather_heredocs(&mut self) -> Option<Vec<HeredocString<'src>>> {
796803
if self.heredoc_strings.is_empty() {
797804
None
798805
} else {

0 commit comments

Comments
 (0)