Skip to content

Commit 586189c

Browse files
committed
Avoid wrapping code inlines
1 parent 13001a8 commit 586189c

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

src/formatting/formatter.rs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,41 @@ impl<'i> Formatter<'i> {
328328
sub.add_fragment_reference(Syntax::Neutral, " ");
329329
sub.add_fragment_reference(Syntax::Structure, "}");
330330
sub.flush_current();
331-
sub.fragments
331+
332+
let mut combined = String::new();
333+
for (_syntax, content) in &sub.fragments {
334+
combined.push_str(&content);
335+
}
336+
337+
vec![(Syntax::Structure, Cow::Owned(combined))]
332338
}
333339
}
334340
}
335341

342+
fn render_string_interpolation(&self, expr: &'i Expression) -> Vec<(Syntax, Cow<'i, str>)> {
343+
let mut sub = self.subformatter();
344+
sub.add_fragment_reference(Syntax::Structure, "{");
345+
sub.add_fragment_reference(Syntax::Neutral, " ");
346+
sub.append_expression(expr);
347+
sub.add_fragment_reference(Syntax::Neutral, " ");
348+
sub.add_fragment_reference(Syntax::Structure, "}");
349+
sub.flush_current();
350+
sub.fragments
351+
}
352+
336353
fn render_application(&self, invocation: &'i Invocation) -> Vec<(Syntax, Cow<'i, str>)> {
337354
let mut sub = self.subformatter();
338355
sub.append_application(invocation);
339356
sub.flush_current();
340-
sub.fragments
357+
358+
// Combine all fragments into a single atomic fragment to prevent wrapping
359+
let mut combined = String::new();
360+
for (_syntax, content) in &sub.fragments {
361+
combined.push_str(&content);
362+
}
363+
364+
// Return as a single fragment
365+
vec![(Syntax::Invocation, Cow::Owned(combined))]
341366
}
342367

343368
fn render_binding(
@@ -369,7 +394,14 @@ impl<'i> Formatter<'i> {
369394
sub.add_fragment_reference(Syntax::Neutral, " ");
370395
sub.append_variables(variables);
371396
sub.flush_current();
372-
sub.fragments
397+
398+
// Combine all fragments into a single atomic fragment to prevent wrapping
399+
let mut combined = String::new();
400+
for (_syntax, content) in &sub.fragments {
401+
combined.push_str(&content);
402+
}
403+
404+
vec![(Syntax::Structure, Cow::Owned(combined))]
373405
}
374406

375407
pub fn append_char(&mut self, c: char) {
@@ -909,7 +941,7 @@ impl<'i> Formatter<'i> {
909941
self.add_fragment_reference(Syntax::String, text);
910942
}
911943
Piece::Interpolation(expr) => {
912-
let fragments = self.render_inline_code(expr);
944+
let fragments = self.render_string_interpolation(expr);
913945
for (syntax, content) in fragments {
914946
self.add_fragment(syntax, content);
915947
}
@@ -1238,21 +1270,28 @@ impl<'a, 'i> Line<'a, 'i> {
12381270
let fragments = self
12391271
.output
12401272
.render_inline_code(expr);
1241-
self.add_fragments(fragments);
1273+
for (syntax, content) in fragments {
1274+
self.add_no_wrap(syntax, content);
1275+
}
12421276
}
12431277

12441278
fn add_application(&mut self, invocation: &'i Invocation) {
12451279
let fragments = self
12461280
.output
12471281
.render_application(invocation);
1248-
self.add_fragments(fragments);
1282+
for (syntax, content) in fragments {
1283+
self.add_no_wrap(syntax, content);
1284+
}
12491285
}
12501286

12511287
fn add_binding(&mut self, inner_descriptive: &'i Descriptive, variables: &'i Vec<Identifier>) {
12521288
let fragments = self
12531289
.output
12541290
.render_binding(inner_descriptive, variables);
1255-
self.add_fragments(fragments);
1291+
// Bindings should not wrap - add as a single non-wrapping unit
1292+
for (syntax, content) in fragments {
1293+
self.add_no_wrap(syntax, content);
1294+
}
12561295
}
12571296

12581297
fn add_fragments(&mut self, fragments: Vec<(Syntax, Cow<'i, str>)>) {
@@ -1262,6 +1301,17 @@ impl<'a, 'i> Line<'a, 'i> {
12621301
}
12631302
}
12641303

1304+
fn add_no_wrap(&mut self, syntax: Syntax, content: Cow<'i, str>) {
1305+
// Add content that must never wrap mid-construct (inline code,
1306+
// applications, bindings) Unlike add_atomic_cow(), this bypasses
1307+
// width checking entirely to preserve the integrity of these language
1308+
// constructs on single lines
1309+
let len = content.len() as u8;
1310+
self.current
1311+
.push((syntax, content));
1312+
self.position += len;
1313+
}
1314+
12651315
fn wrap_line(&mut self) {
12661316
// Emit all current fragments to the output
12671317
for (syntax, content) in self

0 commit comments

Comments
 (0)