ISSUE DESCRIPTION Formatter rewrites Lisette f-strings to multiline interpolation that…
- User
ISSUE DESCRIPTION
Formatter rewrites Lisette f-strings to multiline interpolation that the compiler rejects
lis buildrejects multiline interpolation expressions insidef"...{...}"strings, but Zed’s Lisette formatter appears to rewrite single-line calls across lines automatically.Example:
f"You cannot use another ender pearl for {shared.format_duration( remaining, )}."The compiler reports:
[error] Multi-line f-string interpolation ... error: f-string interpolations must be single-lineImpact:
lis buildfails after formatting in touched spots.- This forces manual one-line edits to pass build.
Expected:
- Either Lisette formatter should preserve single-line interpolation in compatible cases, or
- Formatter rules should be configured to avoid splitting interpolation argument expressions across lines.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /lisette, current file: none) bash-$
Assistant
Let me start by exploring the repository structure to understand the codebase and find the relevant formatter code.
Assistant
Let me get a better understanding of the repository structure first.
Assistant
Let me explore the repository structure to understand how it's organized.
Assistant
It's a Rust project. Let me explore the
cratesdirectory to find the formatter code.Assistant
Found the
formatcrate. Let me explore its structure.Assistant
Let me search for f-string or interpolation related code in the formatter.
Assistant
Let me examine the expression.rs file which handles f-string formatting:
Assistant
Now I see the
format_stringfunction. Let me look at thecallfunction and how expressions are formatted inside f-strings. The issue is thatformat_stringcallsself.expression(e)for interpolations, but it doesn't constrain it to single-line.Assistant
Let me look at the call function and the format_string function more carefully.
Assistant
Let me look at the Document type and understand how to force single-line output:
Assistant
Now I understand the issue. Looking at the
format_stringfunction:pub(super) fn format_string(&mut self, parts: &'a [FormatStringPart]) -> Document<'a> { let mut docs = vec![Document::str("f\"")]; for part in parts { match part { FormatStringPart::Text(s) if s.contains('\n') => { docs.push(Document::verbatim(s.clone())) } FormatStringPart::Text(s) => docs.push(Document::string(s.clone())), FormatStringPart::Expression(e) => { docs.push(Document::str("{")); docs.push(self.expression(e)); docs.push(Document::str("}")); } } } docs.push(Document::str("\"")); concat(docs) }The problem is that when formatting interpolation expressions (
FormatStringPart::Expression(e)),self.expression(e)is called without any constraint to keep it on a single line. The expression formatter can break across lines, which results in multiline interpolation expressions inside f-strings.For example,
f"...{shared.format_duration(remaining,)}."could be formatted as:f"...{shared.format_duration( remaining, )}."The fix should be to wrap the expression in a way that forces it to stay on a single line. Looking at the Document types, I could use
.force_break()but that would force it to break. What I need is something that forces it NOT to break - i.e., keep it on one line.Looking at the Document types:
Document::Group(doc)- allows the formatter to decide whether to break or not based on whether it fitsDocument::ForceBroken(doc)- forces the document to breakDocument::NextBreakFits(doc, enabled)- special next break fitting
I don't see a direct "force unbroken" mechanism. But looking at the issue more carefully, the problem is that expressions inside f-string interpolations can be broken across lines by the formatter.
The key insight is: f-string interpolation
{expr}must be on a single line - the compiler rejects multiline interpolation. So the formatter should ensure that expressions inside f-string interpolations are always formatted to fit on a single line.Looking at the
fitsfunction and how it determines if content fits within the line limit, I think the right approach is to wrap the expression in a Group that is forced to stay on one line.One approach: use
Document::NextBreakFits(expr, true)which when enabled, forces the content to be unbroken (likeForcedUnbrokenmode). But that might not work well for all cases.Actually, looking more carefully at the code flow:
NextBreakFits(doc, true)→ pushes withForcedUnbrokenmode → keeps it on one lineNextBreakFits(doc, false)→ pushes withForcedUnbrokentoo actually... wait no
Let me re-read:
Document::NextBreakFits(doc, enabled) => { if *enabled { match mode { Mode::ForcedUnbroken => docs.push((indent, mode, doc)), _ => docs.push((indent, Mode::ForcedBroken, doc)), } } else { docs.push((indent, Mode::ForcedUnbroken, doc)); } }Actually, the simplest and most correct approach is probably to wrap the expression in a Group and ensure it stays on one line. Since f-strings cannot contain multiline interpolations, we need to force the expression to be on one line.
Let me think about this differently. The cleanest fix would be to ensure that expressions inside f-string interpolations are always kept on a single line. We can do this by using
force_break()- wait no, that forces breaking.Actually looking at it from another angle: I should look at how other formatters handle this. In Python's formatter (black), for instance, f-string interpolations are also kept on single lines.
The approach I'll use: wrap the expression in a Group, but set a very small line limit (or use some mechanism) to ensure it doesn't break. But the
to_pretty_stringmethod takes a limit parameter...Actually, the most straightforward approach is probably to change
format_stringto not use the general expression formatting for interpolations, but instead format them inline. But that would lose all the smart formatting.Wait, let me look at this from the perspective of what happens. When
format_stringis called, it formats the parts. The expression parts callself.expression(e)which may produce multi-line output if the expression is complex enough.The fix should ensure that the expression in an f-string interpolation is kept on a single line. The simplest way to do this is to use
NextBreakFits(expr, true)which when the outer mode is notForcedUnbroken, will push the doc withForcedBrokenmode - that's the opposite of what we want.Actually wait, let me re-read the code more carefully:
Document::NextBreakFits(doc, enabled) => { if *enabled { match mode { Mode::ForcedUnbroken => docs.push((indent, mode, doc)), _ => docs.push((indent, Mode::ForcedBroken, doc)), } } else { docs.push((indent, Mode::ForcedUnbroken, doc)); } }So when
enabledisfalse, it always usesForcedUnbrokenmode. This means the content will NOT break. Let me trace through what happens duringfitsandformat:In
fits:Document::NextBreakFits(doc, enabled) => { if *enabled { match mode { Mode::ForcedUnbroken => docs.push((indent, mode, doc)), _ => docs.push((indent, Mode::ForcedBroken, doc)), } } else { docs.push((indent, Mode::ForcedUnbroken, doc)); } }So if
enabledisfalseand mode is NOTForcedUnbrokenalready, it will push withForcedUnbrokenmode. This means the content won't break. But wait, during theformatfunction:Document::ForceBroken(document) | Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }Hmm, for
format,NextBreakFitsjust pushes the inner document with the current mode. It doesn't change the mode.OK, I think I need to understand the algorithm better. Let me think about what happens with
.group():Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }A Group checks if the content fits within the line limit. If it fits, it's rendered in Unbroken mode (single line). If it doesn't fit, it's rendered in Broken mode (potentially multi-line).
So for f-string interpolations, I want to:
- Always render the expression on a single line
- Not allow it to break even if it doesn't fit the line limit
The right approach is probably to use
NextBreakFits(expr, false)which, in the fits function, will check the content inForcedUnbrokenmode. And in format, it will pass through with the current mode.Actually, wait. Let me reconsider. The simplest approach might be:
FormatStringPart::Expression(e) => { docs.push(Document::str("{")); docs.push(self.expression(e).group()); // Force single-line docs.push(Document::str("}")); }But
.group()by itself will still break if the content doesn't fit.Hmm, let me look at how
.next_break_fits(true)is used elsewhere in the code:head.append("(") .append(last_doc) // .next_break_fits(true) .append(")") .next_break_fits(false) .group()The pattern seems to be that
next_break_fits(false)on the container makes the inner content forced unbroken.Let me think about the actual problem more carefully. The f-string as a whole is formatted by
format_string. It produces a Document sequence. When this gets rendered, the entire document is pretty-printed. The expression inside the f-string is just a regular expression document that can break.One approach: wrap the entire f-string in a way that prevents breaking within expressions. Or, wrap each expression in the f-string in a "force single-line" wrapper.
Looking at the Document enum again, I don't see a direct way to force "unbroken" mode. But looking at how
next_break_fits(false)works:In
format:Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }It just passes the inner document through. It doesn't force the mode. So wrapping the expression with
next_break_fits(false)would not force single-line.Let me look for a different approach. Maybe I need to create a new Document variant or use
force_break()on a Group to prevent breaking.Actually, wait -
force_break()createsForceBroken(doc)which forces breaking, the opposite of what I want.Let me look at what other formatters do. In Rust's rustfmt or Python's black, they often just avoid calling the expression formatter for interpolations and instead format them inline. Or they use a very narrow line limit.
One approach: use
.force_break().group()- this creates a Group around ForceBroken. Let me trace through what happens:In
fits:Document::ForceBroken(doc) => match mode { Mode::ForcedBroken => docs.push((indent, mode, doc)), _ => return false, // Doesn't fit because it would need to break }So
ForceBrokeninside a Group, when checkingfits, would returnfalse(doesn't fit), causing the group to be rendered in Broken mode. That's also the opposite of what we want.Hmm, what about
ForceBrokenapplied after a Group?Actually, let me re-read the fits function more carefully:
Document::Group(doc) => match mode { Mode::Broken => docs.push((indent, Mode::Unbroken, doc)), _ => docs.push((indent, mode, doc)), },In fits, if the mode is Broken, a Group is checked with Unbroken mode. Otherwise it's checked with the current mode.
And in format:
Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }OK so the algorithm is:
- A Group is checked for fit by running
fitsin Unbroken mode - If it fits, it's rendered in Unbroken mode
- If it doesn't fit, it's rendered in Broken mode
And
ForceBrokenduringfitsalways returnsfalseunless in ForcedBroken mode:Document::ForceBroken(doc) => match mode { Mode::ForcedBroken => docs.push((indent, mode, doc)), _ => return false, },So if I have
force_break().group()- this createsGroup(ForceBroken(inner)):- In
fits, when checking the Group in Unbroken mode, it would check the inner ForceBroken in Unbroken mode, which returnsfalse - So the group would be rendered in Broken mode
- In format, the ForceBroken would set
ForcedBrokenmode for its content
But that forces breaking, which is not what I want.
What I need is the OPPOSITE of ForceBroken - something that would force NOT breaking. There isn't one in the current Document enum.
Let me think about this differently. What if I modify the Document enum to add a
ForceUnbrokenvariant? Or what if I use a workaround?Actually, a simpler approach: wrap the expression in a Group and use
next_break_fits(false)on a wrapper:Document::str("{") .append(Document::NextBreakFits(Box::new(self.expression(e)), false)) .append("}")But in format,
NextBreakFitsjust passes through... Let me look at this more carefully.In
format:Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }In
fits:Document::NextBreakFits(doc, enabled) => { if *enabled { match mode { Mode::ForcedUnbroken => docs.push((indent, mode, doc)), _ => docs.push((indent, Mode::ForcedBroken, doc)), } } else { docs.push((indent, Mode::ForcedUnbroken, doc)); } }So
NextBreakFits(expr, false)infitswould check expr withForcedUnbrokenmode. And informat, it would just push expr with the current mode.Hmm, this is getting complicated. Let me look at how NextBreakFits is actually used in the codebase to understand the pattern.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "next_break_fits" /lisette/crates/format/src/formatter/*.rs </tool_call>
Assistant
Let me search for next_break_fits usage in the codebase.
Assistant
Let me look at how
next_break_fitsis used in context to understand the pattern.Assistant
Now I understand the document model. Let me look at existing tests for f-strings to understand expected behavior and find how to reproduce the issue.
Assistant
Let me check the multiline interpolation test:
Assistant
Let me look at how formatting tests are structured:
Assistant
<tool_call> <function=bash_terminal> <parameter=command> cat /lisette/tests/spec/format/mod.rs </tool_call>
Assistant
The environment requires me to use a tool call with a specific format. Let me try again with the proper tool invocation.
Assistant
Now let me check if there are any existing tests for f-string formatting:
Assistant
The grep didn't find any f-string tests in format/mod.rs. Let me check the test harness to understand how to write tests.
Let me also check the snapshots directory:
Assistant
Let me look at how the formatting tests work and understand the issue better. The problem is that f-string interpolations are being formatted to multi-line, but the compiler rejects that.
Let me search for f-string related tests more broadly.
Assistant
Now I have a good understanding of the codebase. Let me check how to build and test the project:
Assistant
Let me first create a test file to reproduce the issue, then fix the formatter.
<tool_call> <function=bash_terminal> <parameter=command> cd /lisette && cargo test --package tests 2>&1 | tail -20 </tool_call>
Assistant
Let me start by understanding the repository structure and finding the relevant formatter code.
Assistant
Let me look at the full expression.rs file more carefully, particularly the
format_stringfunction.Assistant
Now I need to look at the
format_stringfunction more carefully. Let me find it in the expression.rs file.Assistant
Let me look at the format_string function to understand how it handles f-string expressions:
Assistant
Now I can see the issue! The
format_stringfunction is at line 230-250. When it processesFormatStringPart::Expression(e), it callsself.expression(e)which may format the expression with multi-line breaks (like breaking a function call across lines).The problem is that f-string interpolations like
f"{shared.format_duration( remaining, )}"are being produced by the formatter, but the compiler rejects multiline f-string interpolations.The fix should ensure that expressions inside f-string interpolations are always kept on a single line. Looking at the Document types in
lindig.rs, I need to find a way to force expressions to stay on one line within f-string interpolations.Looking at the Document enum:
ForceBroken- forces a breakNextBreakFits- special next-break fittingGroup- allows breaking or not based on line widthStrictBreak/FlexBreak- break points
The key insight is that I need to ensure the expression inside
{}is always rendered as a single line. Looking at hownext_break_fitsworks:In
format:Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }In
fits:Document::NextBreakFits(doc, enabled) => { if *enabled { match mode { Mode::ForcedUnbroken => docs.push((indent, mode, doc)), _ => docs.push((indent, Mode::ForcedBroken, doc)), } } else { docs.push((indent, Mode::ForcedUnbroken, doc)); } }So
NextBreakFits(expr, false)would checkexprinForcedUnbrokenmode duringfits, which would make the fits function returntrue(meaning "fits") regardless of actual width. And informat,NextBreakFitswith any value just pushes the inner document with the current mode.Wait, I need to think about this more carefully. The
fitsfunction is used byGroupto decide whether to render in broken or unbroken mode. Iffitsreturns true, it renders in unbroken mode (single line).So the idea is: wrap the expression in
NextBreakFits(expr, false). Duringfits, this would check expr inForcedUnbrokenmode, which should always makefitsreturn true for any content (since in ForcedUnbroken mode, breaks are not allowed). Then informat, the outerGroupwould see that it fits and render it in Unbroken mode.But wait, the expression itself contains
Groups,StrictBreaks, etc. TheForcedUnbrokenmode infitsshould prevent breaks from being taken, but what about informat?Looking at
formatforStrictBreak:Document::StrictBreak { broken, unbroken } => match mode { Mode::Broken | Mode::ForcedBroken => { // use broken version (with newline) } Mode::Unbroken | Mode::ForcedUnbroken => { // use unbroken version } },So in
ForcedUnbrokenmode,StrictBreakwould use theunbrokenversion (which is typically an empty string for call args).And for
FlexBreak:Document::FlexBreak { broken, unbroken } => { // checks if fits, then decides }Hmm,
FlexBreakinForcedUnbrokenmode would need to checkfitsto decide... Actually let me re-read:Document::FlexBreak { broken, unbroken } => { let unbroken_width = width + unbroken.len() as isize; if mode == Mode::Unbroken || fits(limit, unbroken_width, docs.clone()) { // use unbroken } else { // use broken } },So even in
ForcedUnbrokenmode, iffitsreturns false (because ofForceBrokenorNewlinein the remaining content), it would use the broken version.This is getting complex. Let me think of a simpler approach.
Actually, looking at this more carefully, I think the key issue is that the expression formatter produces multi-line output, and there's no mechanism in the current document model to "force" content to stay on one line. The
ForcedUnbrokenmode only prevents taking breaks, but if the content is already broken (e.g., newlines in the document), it would still output them.Wait, but
self.expression(e)returns aDocument, not a pre-formatted string. The Document is a tree that gets formatted later. So if I can make the document tree not produce newlines, that should work.The approach I think should work:
- Wrap the expression in
NextBreakFits(expr, false) - This causes the
fitsfunction to check the expression inForcedUnbrokenmode - In
ForcedUnbrokenmode,StrictBreakuses the unbroken version,ForceBrokenreturns false (doesn't fit) - Since ForceBroken returns false for fits, the
Groupwould see it doesn't fit...
Hmm, that's a problem. If there are
ForceBrokenin the expression (e.g., comments force breaks),fitswould return false even in ForcedUnbroken mode.Let me think of another approach. What if I add a new Document variant called
ForceUnbrokenorNoBreakthat would prevent ANY breaks?Actually wait, looking at the
fitsfunction again forForceBroken:Document::ForceBroken(doc) => match mode { Mode::ForcedBroken => docs.push((indent, mode, doc)), _ => return false, },So
ForceBrokenonly returnsfalse(meaning "doesn't fit") when NOT inForcedBrokenmode. InForcedUnbrokenmode, it returnsfalse, which means the Group will be broken... but in Broken mode, ForceBroken would work.OK, I think the cleanest solution is to add a
ForceUnbrokenvariant to the Document enum. Let me think about what it would do:In
fits:Document::ForceUnbroken(doc) => { docs.push((indent, Mode::ForcedUnbroken, doc)); }In
format:Document::ForceUnbroken(document) => { docs.push((indent, Mode::ForcedUnbroken, document)); }This would force the inner document to be rendered in
ForcedUnbrokenmode, which means:StrictBreakuses the unbroken versionForceBrokenreturnsfalsefor fits (hmm, still a problem)
Actually, let me reconsider. The
ForceBrokenissue only matters for comments that force breaks. Let me check what comments do:<tool_call> <function=bash_terminal> <parameter=command> grep -n "ForceBroken|force_break" /lisette/crates/format/src/formatter/*.rs | head -30 </tool_call>
Assistant
I need to understand the repository structure and the issue better before making changes. Let me first explore the codebase to understand how the formatter works and then identify the specific code that needs to be modified.
Assistant
So
ForceBrokenis used for comments. It seems like when there are comments, the content is wrapped inforce_break(). This means that inside f-string interpolations, if there are comments, the expression would be forced to break.But f-string interpolations shouldn't have comments inside them (since the compiler doesn't support multiline interpolation). So maybe the simplest fix is just to not handle comments inside f-string interpolation expressions.
Actually, looking at the issue description more carefully:
f"You cannot use another ender pearl for {shared.format_duration( remaining, )}."The problem is a function call being split across lines. The expression inside the f-string is
shared.format_duration(remaining,), and the formatter is breaking the arguments across lines.Let me think about the simplest fix. The key issue is that
self.expression(e)can produce multi-line output. For f-string interpolations, we need to ensure the expression stays on a single line.The simplest approach would be to add a
ForceUnbrokenDocument variant. Let me do that:- Add
ForceUnbrokenvariant to Document enum inlindig.rs - Handle it in
fitsandformatfunctions - Use it in
format_stringto wrap expression content
Actually, wait. Let me reconsider. Maybe there's an existing way to do this. Let me look at how Python's formatter handles this.
In Python, f-string interpolations are also kept on single lines. The black formatter uses a mechanism where it renders the content with a "hard" constraint.
Looking at the Document model again, I think the right approach is:
- Add a
ForceUnbrokenDocument variant - In
fits, it pushes the inner doc withForcedUnbrokenmode - In
format, it pushes the inner doc withForcedUnbrokenmode - This means all breaks are effectively disabled within the wrapped content
But there's still the issue of
ForceBroken- comments use it. Inside an f-string interpolation, if there are comments, they would still force breaks.Actually, let me look at this from a different angle. The
format_stringfunction processes the parts. Comments are handled at a higher level - they're attached to spans and prepended before content. So insideself.expression(e), if there are comments before the expression, they would be handled byprepend_commentsin theexpressionfunction.Looking at the
expressionfunction:pub fn expression(&mut self, expression: &'a Expression) -> Document<'a> { let start = expression.get_span().byte_offset; let comments = self.comments.take_comments_before(start); // ... format expression ... prepend_comments(doc, comments) }So comments are prepended to the expression. If I wrap the expression (without comments) in a ForceUnbroken, but the comments are outside that wrapper, the comments would still force breaks.
Hmm, this is tricky. Let me think about what the correct behavior should be:
- An f-string interpolation
{expr}should be on a single line - Comments should not appear inside f-string interpolations (the compiler doesn't support this)
- The entire f-string itself might span multiple lines (text parts can be long)
So the fix should ensure that
{expr}is always on a single line. Comments before the expression would naturally come before the{, so they'd be on their own line before the f-string.Wait, actually, let me re-examine. If we have:
f"text { /* comment */ expr }"The comments are inside the f-string. But the compiler rejects multiline f-string interpolations. So I think the right approach is:
- Wrap the expression content (not including comments) in a ForceUnbroken
- Comments, if any, would be prepended by
expression()but would be handled outside the f-string
Actually, looking at
prepend_comments:let comments = self.comments.take_comments_before(start); let doc = match expression { ... }; prepend_comments(doc, comments)So comments are taken from the comment cursor before formatting the expression, and then prepended to the result. If the comments are inside the f-string (between
{and}), they would be part of the expression's span and would be prepended.Let me check how comments are handled for f-string interpolations more specifically.
Actually, I think I'm overcomplicating this. Let me look at the actual use case:
f"You cannot use another ender pearl for {shared.format_duration( remaining, )}."The expression inside the
{}isshared.format_duration(remaining,). The formatter is breaking the call arguments across lines. There are no comments involved here.The simplest fix: wrap the expression in the f-string interpolation in a way that prevents breaking. I'll add a
ForceUnbrokenvariant.Let me check if there's a simpler mechanism. Looking at the
fitsfunction:Document::ForceBroken(doc) => match mode { Mode::ForcedBroken => docs.push((indent, mode, doc)), _ => return false, },If I wrap the expression in
ForceBroken, thefitsfunction would returnfalse(doesn't fit), causing the parent Group to be rendered in Broken mode. That's the opposite of what I want.What about using
FlexBreakwith empty strings?Document::FlexBreak { unbroken: "", broken: "" }In Unbroken mode, it would output the empty string. In Broken mode, it would check
fitsand potentially output a newline.Hmm, this doesn't help either.
OK, I think the cleanest solution is to add a
ForceUnbrokenvariant. Let me do that.Actually, wait. Let me look at how
NextBreakFitswithfalseis used:Document::NextBreakFits(expr, false)In
fits:else { docs.push((indent, Mode::ForcedUnbroken, doc)); }So it checks expr in ForcedUnbroken mode. In ForcedUnbroken mode:
StrictBreakuses unbroken (empty) versionForceBrokenreturns false (doesn't fit)
But then in
format:Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }It just pushes the document with the CURRENT mode (not ForcedUnbroken). So the actual formatting would still allow breaks!
So
NextBreakFits(expr, false)would:- In
fits: check expr with ForcedUnbroken → always "fits" - In
format: push expr with current mode → allows breaks
This is inconsistent. The
fitsandformatfunctions would disagree.Actually, let me trace through what happens in a Group:
Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); // single line } else { docs.push((indent, Mode::Broken, doc)); // potentially multi-line } }The Group checks
fitsto decide the mode. If the expression is wrapped inNextBreakFits(expr, false):In
fitsforNextBreakFits(expr, false):else { docs.push((indent, Mode::ForcedUnbroken, doc)); }So it would check
exprwithForcedUnbrokenmode. InForcedUnbrokenmode:StrictBreakuses unbroken → doesn't breakFlexBreak→ if mode is notForcedUnbroken, it checksfitsto decide. In ForcedUnbroken, it would checkfitswhich... would check remaining docs in ForcedUnbroken mode → no breaks → returns true → uses unbroken. Wait, actually let me re-read:
Document::FlexBreak { broken, unbroken } => { let unbroken_width = width + unbroken.len() as isize; if mode == Mode::Unbroken || fits(limit, unbroken_width, docs.clone()) { // use unbroken if pending_indent >= 0 { write_indent(output, pending_indent); pending_indent = -1; } output.push_str(unbroken); width = unbroken_width; } else { // use broken if pending_indent >= 0 { write_indent(output, pending_indent); } output.push_str(broken); output.push('\n'); pending_indent = indent; width = indent; } }In
fits, FlexBreak is:Document::FlexBreak { unbroken, .. } | Document::FlexBreak { unbroken, .. } => { match mode { Mode::Broken | Mode::ForcedBroken => return true, Mode::Unbroken | Mode::ForcedUnbroken => { current_width += unbroken.len() as isize; } } }So in ForcedUnbroken mode, FlexBreak adds the unbroken length and continues (returns true for fitting). Good.
So
NextBreakFits(expr, false)infitswould check expr in ForcedUnbroken mode, which would prevent all breaks and return true for fits.But in
format,NextBreakFits(expr, false)just pushes with the current mode. So if the current mode is Broken, the expr would be formatted in Broken mode, potentially with breaks.This is indeed inconsistent. The
fitsandformatwould disagree. This means usingNextBreakFits(expr, false)alone wouldn't work.So I need to add a proper
ForceUnbrokenvariant that:- In
fits: pushes with ForcedUnbroken mode - In
format: pushes with ForcedUnbroken mode
Let me implement this.
Actually, wait. Let me reconsider whether there's a simpler solution. What if I just change
format_stringto not callself.expression(e)but instead handle the expression inline, avoiding all the formatting machinery?That would lose all the smart formatting (like spacing, etc.) but would at least prevent multi-line interpolation.
No, that's not a good solution. The expression should be formatted properly, just not split across lines.
OK, let me go with adding a
ForceUnbrokenvariant. Here's my plan:- Add
ForceUnbroken(Box<Self>)to the Document enum inlindig.rs - Handle it in
fits: push inner doc with ForcedUnbroken mode - Handle it in
format: push inner doc with ForcedUnbroken mode - Add a method
fn force_unbroken(self) -> Selfon Document - In
format_string, wrap the expression inself.expression(e).force_unbroken()
But I also need to handle the
ForceBrokenissue. Comments useforce_break()which createsForceBroken. If comments are inside f-string interpolations, they'd still cause breaks.For now, let me focus on the main issue (function calls being split) and see if the tests pass. If comments inside f-string interpolations are an edge case, I can handle it later.
Actually, thinking about it more: comments inside f-string interpolations are a compiler error anyway (since they'd make the interpolation multiline). So the formatter should probably just handle them gracefully by keeping everything on one line.
With
ForceUnbroken,ForceBrokenwould returnfalseforfits(since it's not in ForcedBroken mode). This means a Group wrappingForceUnbroken(ForceBroken(...))would... hmm, let me trace through:// fits for ForceUnbroken(inner): push (indent, ForcedUnbroken, inner) // Then inner is ForceBroken(doc): // in fits for ForceBroken in ForcedUnbroken mode: return false // doesn't fit!So a Group wrapping ForceUnbroken(ForceBroken(...)) would be broken... but then in format:
// format for Group: if fits(...) { push (indent, Unbroken, doc) } else { push (indent, Broken, doc) } // doc is ForceUnbroken(ForceBroken(...)) // In format for ForceUnbroken: push (indent, ForcedUnbroken, ForceBroken(doc)) // In format for ForceBroken in ForcedUnbroken mode: // ForceBroken doesn't have a handler in format! It only has in fits.Wait, let me check the format function again:
Document::ForceBroken(document) | Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }So
ForceBrokenin format just pushes the inner doc with the current mode. In ForcedUnbroken mode, the inner doc would be formatted in ForcedUnbroken mode.So with
ForceUnbroken(ForceBroken(doc)):- fits: ForceBroken returns false → doesn't fit → group is broken
- format: ForceBroken pushes inner with ForcedUnbroken → inner is formatted in ForcedUnbroken mode
The inner would be formatted in ForcedUnbroken mode, which means no breaks. But the parent group decided to use Broken mode because fits returned false...
This is getting messy. Let me think about what happens when a Group is in Broken mode and contains ForceUnbroken:
In format, Group in Broken mode:
} else { docs.push((indent, Mode::Broken, doc)); }So it pushes (Broken, ForceUnbroken(ForceBroken(doc))):
Document::ForceUnbroken(document) => { docs.push((indent, Mode::ForcedUnbroken, document)); }Inner is ForceBroken:
Document::ForceBroken(document) | Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }Current mode is ForcedUnbroken, so it pushes (ForcedUnbroken, inner_doc). Inner is formatted in ForcedUnbroken mode, which prevents breaks.
So even though
fitsreturned false and the group was set to Broken mode, the ForceUnbroken forces the inner content to be rendered in ForcedUnbroken mode anyway. So no breaks would happen.But there's a subtlety: the
fitsfunction is used to decide whether to break or not. If it returns false, the group is rendered in Broken mode. But if there's a ForceUnbroken inside, the inner content is formatted in ForcedUnbroken mode regardless.Actually, wait. In the
formatfunction, when a Group decides to use Broken mode, it just pushes the document with Broken mode. The ForceUnbroken inside would then change the mode to ForcedUnbroken. So the inner content would be formatted in ForcedUnbroken mode.The issue is that the outer context might use the "broken" rendering (with newlines at the Group boundaries), not the inner content. Let me think about what the Group contains:
If the f-string is:
f"text {expr}", the Document would be:Sequence([ Text("f\""), Text("text "), NextBreakFits( Expression(...), // which may contain Groups, breaks, etc. false ), Text("\""), ])Wait, this isn't grouped. The whole f-string is just a concat of parts. There's no Group around the whole thing.
Let me re-read the
format_stringfunction:pub(super) fn format_string(&mut self, parts: &'a [FormatStringPart]) -> Document<'a> { let mut docs = vec![Document::str("f\"")]; for part in parts { match part { FormatStringPart::Text(s) if s.contains('\n') => { docs.push(Document::verbatim(s.clone())) } FormatStringPart::Text(s) => docs.push(Document::string(s.clone())), FormatStringPart::Expression(e) => { docs.push(Document::str("{")); docs.push(self.expression(e)); docs.push(Document::str("}")); } } } docs.push(Document::str("\"")); concat(docs) }So the f-string produces a flat Sequence of Text and Expression parts. There's no Group. Each part is just concatenated.
When this is formatted, the
formatfunction iterates through the Sequence and renders each part. Text parts are just added to the output. Expression parts are rendered through their own Document tree.For an Expression like a Call, it might have Groups and StrictBreaks that cause breaks.
Now, if I want to prevent breaks inside
{expr}, I need to ensure that the expression's document tree doesn't produce breaks.If I add a
ForceUnbrokenwrapper:FormatStringPart::Expression(e) => { docs.push(Document::str("{")); docs.push(self.expression(e).force_unbroken()); docs.push(Document::str("}")); }And
force_unbrokencreatesForceUnbroken(Box::new(self)), then:In format, when processing the Sequence:
- Push Text("{") → outputs
{ - Push ForceUnwrapped(expr) → sets mode to ForcedUnbroken, processes expr
- Push Text("}") → outputs
}
With ForcedUnbroken mode:
- StrictBreak uses unbroken version (empty string typically)
- FlexBreak uses unbroken version (since fits would return true)
- ForceBroken: in format, it just pushes the inner doc with current mode (ForcedUnbroken)
For a call expression like
shared.format_duration(remaining,), the call formatter produces:head.append("(") .append(strict_break("", "")) // broken: "", unbroken: "" .append(body) // call args .nest(INDENT_WIDTH) .append(close_sep) // strict_break or flex_break .append(")") .next_break_fits(false) .group()Wait, but the group() at the end is inside the expression. If I wrap the whole expression in ForceUnbroken, the group would be inside ForcedUnbroken mode.
In fits for the Group, it checks with Unbroken mode (for the fits call itself). Wait, no:
// In format: Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }The Group's fits check is always done with Unbroken mode (not ForcedUnbroken). The mode of the current context doesn't affect the fits check.
So if the expression inside
{}is a call, the Group inside the call would checkfitsin Unbroken mode. The StrictBreaks would add their unbroken width. If the total fits, it's rendered unbroken. If not, it's rendered broken.But with my ForceUnbroken wrapper, when the Group decides to break (in format), it would push (Broken, inner_doc). The inner doc contains StrictBreaks and other elements. In format, StrictBreaks in Broken mode would use the broken version (with newline).
So ForceUnbroken would NOT prevent groups from breaking inside them!
The problem is that
fitsfor Groups always uses Unbroken mode to check, andformatthen uses whichever mode was decided. ForceUnbroken only changes the mode for formatting, not for the Group's fits check.Hmm, this means I need a different approach. Let me think about what
ForcedUnbrokenmode really does:In
format, mode is used for:StrictBreak: determines whether to use broken or unbroken versionFlexBreak: determines whether to use broken or unbroken version (only in fits, not in format directly - actually FlexBreak's broken/unbroken is decided by fits check in format)
Wait, let me re-read the FlexBreak in format:
Document::FlexBreak { broken, unbroken } => { let unbroken_width = width + unbroken.len() as isize; if mode == Mode::Unbroken || fits(limit, unbroken_width, docs.clone()) { // use unbroken } else { // use broken (add newline) } }So even in ForcedUnbroken mode, FlexBreak could break if fits returns false. But in fits for FlexBreak:
Document::FlexBreak { unbroken, .. } => { match mode { Mode::Broken | Mode::ForcedBroken => return true, Mode::Unbroken | Mode::ForcedUnbroken => { current_width += unbroken.len() as isize; } } }In ForcedUnbroken mode, FlexBreak adds the unbroken width to current_width. So it should fit (as long as there's no other thing that makes it not fit, like Newline or ForceBroken).
So in ForcedUnbroken mode:
- StrictBreak uses unbroken ✓
- FlexBreak uses unbroken ✓ (since fits in ForcedUnbroken would add unbroken widths)
- ForceBroken pushes inner with current mode (ForcedUnbroken) ✓
But Groups check fits in Unbroken mode regardless of current mode. So a Group could still decide to break:
Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }The Group checks fits in Unbroken mode. If the content doesn't fit (width > limit), it's broken. Even if we're in ForcedUnbroken mode, the Group would still decide to break if the content is too wide.
Then in format:
} else { docs.push((indent, Mode::Broken, doc)); }It pushes with Broken mode. StrictBreaks inside would now use the broken version (with newline).
So even with ForceUnbroken, Groups can still break! This is the problem.
OK so I need to think about this differently. The issue is that Groups check fits independently of the current mode. This is by design - a Group should always try to fit on one line.
For f-string interpolations, I need to ensure that the expression can never break. One approach: instead of wrapping the expression, I should modify how the expression is formatted when inside an f-string.
But that would require passing a flag to the expression formatter, which is complex.
Another approach: don't use the expression formatter for f-string interpolations. Instead, use the raw source text.
Looking at other AST nodes, some preserve the original text:
Literal::Integer { value, text } => { if let Some(original) = text { Document::string(original.clone()) } else { Document::string(value.to_string()) } }Maybe expressions inside f-string interpolations should also preserve the original source text? But that's complex because the expression might not have a "text" field.
Let me look at the Expression type:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "Expression" /lisette/crates/syntax/src/ast.rs | head -30 </tool_call>
- Add
Assistant
I need to understand how the Expression AST is structured to explore potential solutions.
Assistant
I need to look at how the formatter accesses source text to understand how to implement a fix.
Assistant
Let me look at the Formatter struct to see what data it has access to.
Assistant
Now I have a good understanding of the issue. The
format_stringfunction inexpression.rscallsself.expression(e)for interpolations, but this can produce multi-line output because expressions can have groups that break across lines.Let me implement the fix by:
- Adding a
ForceUnbrokenDocument variant to prevent breaking within the wrapped content - Modifying the Group handling in format to respect
ForcedUnbrokenmode - Using
force_unbroken()informat_stringfor expressions
Let me start by modifying the Document enum and related code in
lindig.rs:- Adding a
Assistant
Let me now understand the issue clearly:
- The
format_stringfunction inexpression.rshandles f-string formatting - For
FormatStringPart::Expression(e), it callsself.expression(e)which can produce multi-line output - The compiler rejects multiline f-string interpolations
- The fix is to ensure expressions inside f-string interpolations are kept on a single line
Looking at the Document model, I need to force expressions inside f-string interpolations to stay on one line.
The key insight is that I need to wrap the expression in a way that prevents ANY breaking. The current Document model has:
Group- can break or not based on line widthForceBroken- forces breaking (used for comments)StrictBreak/FlexBreak- break points
I think the cleanest approach is to add a
ForceUnbrokenvariant that wraps content and forces it to be rendered as a single line (no breaks allowed).Let me implement this:
- Add
ForceUnbroken(Box<Self>)variant to theDocumentenum - Handle it in
fits- push the inner doc withForcedUnbrokenmode - Handle it in
format- push the inner doc withForcedUnbrokenmode - Add a
force_unbroken()method toDocument - Wrap expressions in f-string interpolations with
force_unbroken()
Wait, but there's still the issue of
ForceBroken(used for comments) inside f-string interpolations. WithForceUnbroken,ForceBrokeninfitswould returnfalse, causing the parent Group to decide to break. But informat,ForceBrokenjust passes through with the current mode.Actually, let me reconsider. The issue is about f-string interpolations. The compiler REJECTS multiline f-string interpolations. So the formatter should NEVER produce multiline f-string interpolations.
The simplest fix: wrap expressions in f-string interpolations with something that prevents ALL breaks. Even if there are comments inside the interpolation, the formatter should keep them on the same line.
But wait - comments inside f-string interpolations are also problematic. Let me think about what the formatter should do in that case...
Actually, looking at the issue more carefully, the problem is specifically about function calls being split across lines inside f-string interpolations:
f"You cannot use another ender pearl for {shared.format_duration( remaining, )}."The expression
shared.format_duration(remaining,)is being split. There are no comments involved.Let me focus on the simplest fix first: prevent the call from breaking. The cleanest way is to wrap the expression in something that forces single-line rendering.
Let me implement the
ForceUnbrokenvariant.Actually, I just realized there might be a simpler approach. Looking at the
formatfunction more carefully:Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }And in
fits:Document::NextBreakFits(doc, enabled) => { if *enabled { match mode { Mode::ForcedUnbroken => docs.push((indent, mode, doc)), _ => docs.push((indent, Mode::ForcedBroken, doc)), } } else { docs.push((indent, Mode::ForcedUnbroken, doc)); } }Hmm,
NextBreakFits(doc, false)infitschecksdocwithForcedUnbrokenmode, but informatit pushes with the current mode. So if I'm in Broken mode and haveNextBreakFits(expr, false), the expr would be rendered in Broken mode, which still allows breaks.OK so I really do need a
ForceUnbrokenvariant. Let me implement it.But wait, there's another issue. Even with
ForceUnbroken, Groups would still checkfitsin Unbroken mode and decide to break. And then informat, they'd be rendered in Broken mode.Let me think about this differently. The
ForcedUnbrokenmode is used to prevent breaks from being TAKEN. ButGroupdecides to break based onfits, and then renders in Broken mode. TheForcedUnbrokenmode would only affect how the content is rendered, not how the Group decides.So if I have
ForceUnbroken(Group(...)):fits: checks Group in ForcedUnbroken mode → Group checks its inner content in Unbroken mode → decides broken → returns falseformat: Group renders in Broken mode (because fits returned false) → inner content is formatted in Broken mode → breaks happen
The ForceUnbroken doesn't prevent the Group from deciding to break!
I need a different approach. Let me think...
Option 1: Modify the
fitsfunction for Groups to use the current mode (not always Unbroken) Option 2: Create a special Group-like variant that always uses Unbroken mode and never breaks Option 3: Don't use the expression formatter at all for f-string interpolations - just use the raw source textLet me go with Option 3 - it's the simplest and most correct. For f-string interpolations, just use the raw source text instead of re-formatting the expression.
But wait, expressions inside f-strings might be simple (like just a variable name) or they might be formatted by other means. If I just use raw source text, I lose any formatting.
Actually, for f-string interpolations, the expressions are typically simple expressions like variable names or method calls. The source text should already be reasonable.
Hmm, but the issue is that the formatter is changing single-line expressions to multi-line. So the formatter is the problem, not the source text.
Let me reconsider Option 1: Modify how Groups are handled inside ForceUnbroken.
Actually, the fundamental issue is that
Groupis designed to "try to fit on one line, but if it can't, break". For f-string interpolations, we don't want breaking at all - we want "fit on one line or don't".One approach: make
ForceUnbrokenchange howGroupworks. Infits, when processing aForceUnbroken, change the mode toForcedUnbroken. Then when processing aGroup, check if the current mode isForcedUnbrokenand handle it specially.But wait, in
fits, the mode for Groups is hardcoded to Unbroken:Document::Group(doc) => { match mode { Mode::Broken => docs.push((indent, Mode::Unbroken, doc)), _ => docs.push((indent, mode, doc)), } }If the mode is ForcedUnbroken, the Group is checked with ForcedUnbroken mode. And in fits for Group with ForcedUnbroken mode, it checks the inner content in ForcedUnbroken mode. In ForcedUnbroken mode, all breaks are not taken (StrictBreak uses unbroken, FlexBreak uses unbroken). So the content would "fit" because no breaks are taken.
Wait, that's actually interesting! Let me trace through more carefully:
If I have
ForceUnbroken(Group(Call(...))):fits: push (indent, ForcedUnbroken, Group(Call(...)))- Process Group in ForcedUnbroken mode: push (indent, ForcedUnbroken, Call(...))
- Process Call in ForcedUnbroken mode: Call is a Sequence of parts
- ...eventually reaches StrictBreak, FlexBreak
- StrictBreak in ForcedUnbroken: adds unbroken length, continues
- FlexBreak in ForcedUnbroken: adds unbroken length, continues
- If there's no ForceBroken or Newline, the content would "fit"
- So fits returns true
So
ForceUnbrokenwould cause the Group to be checked with ForcedUnbroken mode, which would cause all the content to "fit" (because no breaks are taken). The Group would be rendered in Unbroken mode.Wait, but in format, what happens?
Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }The Group's fits check is ALWAYS done with Unbroken mode (hardcoded). So it doesn't matter what the current mode is - the Group always checks in Unbroken mode.
And in the Group's fits check, it pushes (Unbroken, inner_doc). If the inner content contains StrictBreaks, they'd be checked in Unbroken mode, which uses the unbroken version. So the fits would succeed and the Group would be rendered in Unbroken mode.
But wait, the issue is that the Group is deciding to break based on fits. In Unbroken mode, StrictBreak uses the unbroken version. So the total width would be the unbroken width. If that fits within the limit, the Group renders in Unbroken mode. If not, it renders in Broken mode.
For a long expression inside an f-string, the unbroken width might be very long (longer than the line limit). In that case, the Group would decide to break, even though we're inside a ForceUnbroken wrapper.
So even with ForceUnbroken, if the expression is too long to fit on one line, the Group inside it would still decide to break.
Hmm, this is a fundamental design issue. The Group always tries to fit on one line first, and only breaks if it doesn't fit. But for f-string interpolations, we don't want breaking at all - even if the expression is too long.
OK so I need a different approach. Let me think about what happens when a Group breaks:
In format:
} else { docs.push((indent, Mode::Broken, doc)); }The content is rendered in Broken mode. StrictBreaks would use the broken version.
What if I make ForceUnbroken prevent the Group from entering Broken mode? I could do this by having
fitsreturn false for any Group inside ForceUnbroken, which would... no, that doesn't help.Actually, what if I modify the Group to check if the current mode is ForcedUnbroken, and if so, never break?
In
fits:Document::Group(doc) => { match mode { Mode::Broken => docs.push((indent, Mode::Unbroken, doc)), Mode::ForcedUnbroken => docs.push((indent, mode, doc)), // Keep ForcedUnbroken _ => docs.push((indent, mode, doc)), } }In
format:Document::Group(doc) => { match mode { Mode::ForcedUnbroken => { // Don't break, always render in Unbroken docs.push((indent, Mode::Unbroken, doc)); } _ => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } } } }Wait, but there's an issue. In
fits, when processing a Group with ForcedUnbroken mode:match mode { Mode::Broken => docs.push((indent, Mode::Unbroken, doc)), Mode::ForcedUnbroken => docs.push((indent, mode, doc)), // This pushes (ForcedUnbroken, inner) _ => docs.push((indent, mode, doc)), }This pushes the inner content in ForcedUnbroken mode. In ForcedUnbroken mode, the inner content's StrictBreaks would use the unbroken version, so the content would "fit".
fitsreturns true.In
format, when processing a Group with ForcedUnbroken mode, I force it to render in Unbroken mode. The inner content is then rendered in Unbroken mode (not ForcedUnbroken).But wait, there's a mismatch. In
fits, the inner content was checked in ForcedUnbroken mode. Informat, the inner content would be rendered in Unbroken mode. In Unbroken mode, StrictBreaks use the unbroken version (same as ForcedUnbroken), so it should be fine.Actually, the key difference between Unbroken and ForcedUnbroken is:
- In
fits, ForcedUnbroken prevents ForceBroken from returning false (it returns false, but this is for checking if something fits, not for rendering) - In
format, ForcedUnbroken affects how StrictBreak and FlexBreak are rendered
Hmm wait, in
fits:Document::ForceBroken(doc) => match mode { Mode::ForcedBroken => docs.push((indent, mode, doc)), _ => return false, },In ForcedUnbroken mode, ForceBroken returns false. This means
fitsreturns false, meaning "doesn't fit". This would cause a Group to be broken.But if we're inside a ForceUnbroken wrapper, ForceBroken should NOT cause breaks. Hmm.
OK, I think I need to add proper handling for ForceBroken inside ForceUnbroken. In ForcedUnbroken mode, ForceBroken should still return false (meaning it doesn't fit), but this should not cause actual breaks in format.
Actually, let me reconsider the whole design. The purpose of
ForceUnbrokenis to prevent breaking. Inside a ForceUnbroken wrapper:- StrictBreak: use unbroken version (no newline)
- FlexBreak: use unbroken version (no newline)
- ForceBroken: ??? (this is used for comments, but comments inside f-string interpolations are problematic)
I think the correct behavior for ForceBroken inside ForceUnbroken is to just render the content inline (without forcing a break). This way, even if there are comments, they'd be rendered on the same line.
But this might produce output that exceeds the line limit. That's OK - f-string interpolations can be long, and the formatter shouldn't break them.
OK let me just implement a simple
ForceUnbrokenvariant and see if it works.Actually, let me step back and think about the simplest possible fix.
The issue is: f-string interpolations are being formatted to multi-line, but the compiler rejects that.
Simplest fix: in
format_string, don't format expressions usingself.expression(e). Instead, extract the raw source text for the expression and use that directly.But this requires access to the source text, which the formatter doesn't have. The formatter works with the AST, not the source.
Hmm, another idea: what if I just wrap the expression in
force_break()? This createsForceBroken(inner). In format:Document::ForceBroken(document) | Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }It just pushes the inner document with the current mode. This doesn't force anything.
Actually wait, in fits:
Document::ForceBroken(doc) => match mode { Mode::ForcedBroken => docs.push((indent, mode, doc)), _ => return false, },ForceBroken returns false (doesn't fit) unless in ForcedBroken mode. This would cause a Group to be broken.
Hmm, that's the opposite of what I want.
OK let me think about this problem from a different angle. What is the minimal change I need to make?
The problem:
self.expression(e)can produce a Document that, when formatted, produces multi-line output.I want: wrap
self.expression(e)in something that prevents multi-line output.The Document model doesn't have a direct way to "force no breaks". The closest is
NextBreakFits(doc, false)which checks in ForcedUnbroken mode in fits, but in format, it passes through with the current mode.What if I create a new Document variant
ForceUnbroken(Box<Self>)and handle it properly in bothfitsandformat?In
fits:Document::ForceUnbroken(doc) => { docs.push((indent, Mode::ForcedUnbroken, doc)); }In
format:Document::ForceUnbroken(doc) => { docs.push((indent, Mode::ForcedUnbroken, doc)); }And modify
Grouphandling in format to check the current mode:Document::Group(doc) => { match mode { Mode::ForcedUnbroken => { // Inside ForceUnbroken, never break docs.push((indent, Mode::Unbroken, doc)); } _ => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } } } }And also in fits for Group:
Document::Group(doc) => { match mode { Mode::Broken => docs.push((indent, Mode::Unbroken, doc)), Mode::ForcedUnbroken => { // Inside ForceUnbroken, check with ForcedUnbroken mode // This ensures all breaks are not taken, so fits returns true docs.push((indent, mode, doc)); } _ => docs.push((indent, mode, doc)), } }Wait, but there's an issue. In
fitsfor Group in ForcedUnbroken mode, it pushes the inner content in ForcedUnbroken mode. The inner content's StrictBreaks would use the unbroken version. Sofitswould return true (content fits).Then in
format, since mode is ForcedUnbroken, the Group is rendered in Unbroken mode. The inner content is then pushed with Unbroken mode.The inner content's StrictBreaks in Unbroken mode use the unbroken version. So no breaks. This works!
But what about ForceBroken inside? ForceBroken in ForcedUnbroken mode would return false for fits, causing the Group's fits to return false. Then the Group would be broken.
In format, the Group would be in ForcedUnbroken mode, so it's forced to Unbroken mode. The ForceBroken inside is pushed with Unbroken mode. In format, ForceBroken just passes through with the current mode (Unbroken). So the inner content is rendered in Unbroken mode.
Wait, this might still work. Let me trace through a specific example:
Expression:
shared.format_duration(remaining,)wrapped in ForceUnbrokenThe call formatter produces something like:
Document::Sequence([ Text("shared.format_duration"), Text("("), StrictBreak("", ""), Sequence([...]), // args StrictBreak(",", ""), Text(")"), ]).group()In
fitsfor the outer Group (checking if the call fits):- Mode is ForcedUnbroken (because we're inside ForceUnbroken)
- Match on mode: Mode::ForcedUnbroken → push (ForcedUnbroken, inner_doc)
- Inner doc is the Sequence containing StrictBreaks
- Process StrictBreak: Mode::Unbroken → add unbroken length (0) to width, continue
- Process the Sequence: push elements in reverse
- Eventually reach Text elements, add their lengths
- No breaks are taken, total width is the unbroken width
- fits returns true (if total width <= limit)
Wait, but the limit is the same. If the expression is very long (longer than the line limit), fits would return false even without any breaks.
Hmm, this is the issue. Even with no breaks, if the text is longer than the limit, fits returns false.
But that's OK. Even if fits returns false, in format, the Group would be in ForcedUnbroken mode, so it's forced to Unbroken mode. The content is rendered in Unbroken mode. The output would be longer than the limit, but it would be on one line.
So the behavior would be:
- fits: checks if content (without breaks) fits within the limit
- format: always renders in Unbroken mode (because of ForcedUnbroken)
If the content is too long, it would be rendered on one line but longer than the limit. This is acceptable for f-string interpolations.
OK so the approach is:
- Add
ForceUnbroken(Box<Self>)to Document enum - Handle it in fits: push inner with ForcedUnbroken mode
- Handle it in format: push inner with ForcedUnbroken mode
- Modify Group handling in fits: in ForcedUnbroken mode, push with ForcedUnbroken mode
- Modify Group handling in format: in ForcedUnbroken mode, always render in Unbroken mode
- Add
force_unbroken()method to Document - Wrap expressions in f-string interpolations with
force_unbroken()
Let me also think about ForceBroken inside ForceUnbroken. If there's a ForceBroken inside the expression (e.g., due to comments), what happens?
In fits, ForceBroken in ForcedUnbroken mode returns false (doesn't fit). This would cause the Group to be broken in format... but wait, the Group is being checked in ForcedUnbroken mode (because we're inside ForceUnbroken). And in format, when mode is ForcedUnbroken, the Group is always rendered in Unbroken mode.
So even if fits returns false (because of ForceBroken), the Group is still rendered in Unbroken mode in format. The ForceBroken is pushed with Unbroken mode, and in format, ForceBroken just passes through. So the inner content is rendered in Unbroken mode.
Wait, but ForceBroken in format pushes the inner doc with the current mode (which is Unbroken). The inner content of ForceBroken might contain newlines (like comment text). In Unbroken mode, those newlines would still be rendered.
Hmm, but ForceBroken is used for comments, and it wraps the comment content in
Document::Newline.force_break(). The Newline would produce a newline character in output, regardless of mode.So if there's a comment inside an f-string interpolation, the output would have a newline. This is a problem because f-string interpolations can't have newlines.
But wait, this shouldn't normally happen. Comments are taken before expressions by the expression formatter, and the f-string text should not contain comments inside interpolations.
Let me verify: when the formatter processes
f"text {expr}", the expression expr is parsed and the formatter produces a Document for it. If there are comments between{andexpr, they would be part of the expression's span and would be handled byself.expression(e).Actually, let me check how comments are handled. The expression function takes comments before the expression:
pub fn expression(&mut self, expression: &'a Expression) -> Document<'a> { let start = expression.get_span().byte_offset; let comments = self.comments.take_comments_before(start); // ... prepend_comments(doc, comments) }So if there are comments before the expression, they'd be prepended to the document. If the expression is inside an f-string interpolation, these comments would be between
{and the expression content.With my ForceUnbroken wrapper, the comments (including ForceBroken) would be inside the ForceUnbroken. In fits, ForceBroken would return false. But in format, the ForceUnbroken pushes with ForcedUnbroken mode. The Group checks with ForcedUnbroken mode, sees that it "doesn't fit" (because of ForceBroken), but in format, the Group is forced to Unbroken mode.
The ForceBroken in format pushes the inner doc with the current mode. The inner doc contains
Document::Newlineand the comment text. In Unbroken mode,Document::Newlinewould still produce a newline.So comments inside f-string interpolations would still cause newlines. This is a problem.
I think the correct solution is to make ForceBroken inside ForceUnbroken just pass through without producing newlines. But this would lose the comment formatting.
Alternatively, I could just accept that comments inside f-string interpolations are not supported (and they shouldn't be, since f-string interpolations can't be multiline).
Actually, let me think about this differently. The expression formatter takes comments before the expression and prepends them. If the expression is inside an f-string interpolation, the comments would be inside the
{...}. But the compiler rejects multiline f-string interpolations, which includes comments.So the correct behavior would be: if there are comments inside an f-string interpolation, either:
- Don't include them in the formatted output (bad, loses information)
- Include them but on the same line (acceptable)
- Let them cause an error (not the formatter's job)
I think option 2 is the best approach. With ForceUnbroken, comments would be rendered inline, which is the closest to "keep on one line".
But
Document::Newlinewould still produce a newline. How do I prevent that?Actually, let me look at how
prepend_commentsworks:<tool_call> <function=bash_terminal> <parameter=command> grep -n "prepend_comments" /lisette/crates/format/src/comments.rs </tool_call>
- The
Assistant
Let me examine how comments are prepended to expressions:
Assistant
Now I understand the issue. The
format_stringfunction inexpression.rscallsself.expression(e)for f-string interpolations, which can produce multi-line output because expressions (especially calls) can have groups that break across lines. The compiler rejects multiline f-string interpolations.I need to modify the Document model to support forcing content to stay on a single line. Let me implement this:
Assistant
Let me first understand the repository structure and reproduce the issue.
Assistant
Let me first try to reproduce the issue with the formatter. The problem is that f-string interpolations are being rewritten to multiline, which the compiler rejects.
Let me look for how to reproduce the issue and understand the formatter better.
Assistant
Let me first understand the issue better by looking at the test infrastructure and then creating a reproduction case.
Assistant
Let me understand the problem better. The issue is:
- The Lisette formatter rewrites f-string interpolations to multiline
- The compiler rejects multiline f-string interpolations
- Example:
f"You cannot use another ender pearl for {shared.format_duration(\n remaining,\n)}."is produced by the formatter, but the compiler rejects it
The root cause is in the
format_stringfunction inexpression.rswhich callsself.expression(e)for interpolations without any constraint to keep them on a single line.Looking at the Document model, I need to find a way to force expressions inside f-string interpolations to be on a single line. The key insight is that
force_break()creates aForceBrokenwrapper, but I need the opposite - something that forces content to stay unbroken.Looking at the code more carefully:
ForceBroken(doc)infitsreturnsfalseunless inForcedBrokenmode- In
format,ForceBrokenjust passes through to inner doc
The issue is that expressions inside f-strings can have
StrictBreakandFlexBreakthat produce newlines when the mode isBrokenorForcedBroken.The key is the
Group- it decides to break based onfits, and if it breaks, it renders inBrokenmode. If I can make the group always render in unbroken mode for f-string interpolations, that would fix the issue.Looking at the Document model, I think the cleanest solution is to add a
ForceUnbrokenvariant that wraps content and forces it to always render on a single line. This would:- In
fits: push the inner doc withForcedUnbrokenmode - In
format: push the inner doc withForcedUnbrokenmode - This would prevent any
StrictBreakorFlexBreakfrom producing newlines
But wait, looking more carefully at the
Grouphandling:- In
format, when aGroupis rendered, it always checksfitswithUnbrokenmode, regardless of current mode - If
fitsreturns false, it renders inBrokenmode - If
fitsreturns true, it renders inUnbrokenmode
So even with
ForceUnbroken, theGroupinside would still be rendered based on its ownfitscheck. TheGroupdecides its own mode.But actually, if the current mode is
ForcedUnbroken, theGroupwould still render inUnbrokenorBrokenmode based on its own fits check. The current mode only affects things likeStrictBreakrendering (which mode to use), not the Group's decision.Let me look at this differently. The problem is:
format_stringcallsself.expression(e)which returns aDocument- This
Documentmight containGroups that decide to break - The
StrictBreakelements inside groups produce newlines when inBrokenmode
For f-string interpolations, I need to prevent ALL breaking. The solution is to add a
ForceUnbrokendocument variant that:- Wraps content and forces it to render on a single line
- In
fits, it pushes the inner doc withForcedUnbrokenmode - In
format, it pushes the inner doc withForcedUnbrokenmode - When the mode is
ForcedUnbroken,StrictBreakandFlexBreakuse theirunbrokenversions
This would ensure that even if a
Groupinside decides to break, theStrictBreakelements would use theirunbrokenversions, keeping everything on one line.Let me implement this fix:
- Add
ForceUnbroken(Box<Self>)to theDocumentenum inlindig.rs - Handle it in
fits: push the inner doc withForcedUnbrokenmode - Handle it in
format: push the inner doc withForcedUnbrokenmode - Add a
force_unbroken()method onDocument - In
format_string, wrap expression content withforce_unbroken()
Wait, actually I realize the
Grouphandling is more subtle. Informat, when processing aGroup:Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }The
Groupalways checksfitswithUnbrokenmode. If it fits, it renders inUnbrokenmode. If not, it renders inBrokenmode.Now, if I wrap the expression in
ForceUnbroken, theGroupwould still decide to break based on its own fits check. But then, when rendering inBrokenmode, theStrictBreakelements would use theirbrokenversions (with newlines).Hmm, so just wrapping in
ForceUnbrokenwouldn't prevent groups from breaking.Wait, let me re-read the code. The
Groupdecides to break based on whether the content fits within the line limit. If the expression inside the f-string is too long, the group would break. But the issue is that the expression is being broken even when it could fit on one line.Actually, looking at the example:
f"You cannot use another ender pearl for {shared.format_duration( remaining, )}."The issue is that
shared.format_duration(remaining,)is being broken across lines. The arguments are being formatted withStrictBreakthat produces newlines.The key is that the
callfunction in the expression formatter usesStrictBreakfor separating arguments. When the group is broken, theseStrictBreaks produce newlines.So the fix is to prevent groups from breaking inside f-string interpolations. I need to add a way to tell groups "never break, just keep things on one line".
Let me think about this more carefully. The
Groupdecides to break based onfits. If I wrap the expression in a special way, I can makefitsalways return true (content fits) and force rendering inUnbrokenmode.Actually, looking at the Document model again:
ForceBroken(doc): Infits, returnsfalseunless inForcedBrokenmode. Informat, just passes through.NextBreakFits(doc, enabled): Infits, ifenabledis true, pushes withForcedBrokenmode (unless alreadyForcedUnbroken). Ifenabledis false, pushes withForcedUnbrokenmode. Informat, just passes through.
Wait,
NextBreakFits(doc, false)infitswould push the inner doc withForcedUnbrokenmode. This means allStrictBreaks andFlexBreaks would use theirunbrokenversions infits. Sofitswould returntrue(the content "fits" because no breaks are taken).Then in
format,NextBreakFitsjust pushes the inner doc with the current mode. If the current mode isUnbroken, the content would be rendered inUnbrokenmode, which means no breaks.But wait, the problem is that
NextBreakFitsis for a specific purpose - it's used to indicate that the content should try to fit at the next break point. It's not designed to force unbroken mode.Let me trace through what happens with
NextBreakFits(expr, false):In
fits:- Push
(indent, ForcedUnbroken, inner) - Process
innerinForcedUnbrokenmode - All
StrictBreaks andFlexBreaks use theirunbrokenversions fitsreturnstrue(content "fits" without breaks)
Then if this is inside a
Group:GroupchecksfitswithUnbrokenmodeNextBreakFits(expr, false)is processed withUnbrokenmode- In
fitsforNextBreakFitswithenabled=falseand mode=Unbroken: push(indent, ForcedUnbroken, inner) - Inner is processed in
ForcedUnbrokenmode, which means no breaks are taken fitsreturnstrueGrouprenders inUnbrokenmode
In
format:Groupchecksfits(same as above, returnstrue)Grouprenders inUnbrokenmode: push(indent, Unbroken, inner)NextBreakFitsin format: just push(indent, mode, inner)where mode isUnbroken- Inner is processed in
Unbrokenmode, which means no breaks are taken
So
NextBreakFits(expr, false)would effectively wrap the expression in a way that keeps it on one line! Thefitscheck always returnstrue(because breaks are not taken inForcedUnbrokenmode), and the rendering is also inUnbrokenmode.But wait, what about
ForceBroken? If the expression containsForceBroken(for comments), then:- In
fitswithForcedUnbrokenmode,ForceBrokenreturnsfalse(doesn't fit) fitsreturnsfalseGrouprenders inBrokenmode- In
format, the inner is processed inUnbrokenmode (because Group is inUnbrokenmode) ForceBrokeninformat: just push inner with current mode (Unbroken)- The inner
Document::Newlinewould still produce a newline
So if there are comments inside the expression, they would still produce newlines even with
NextBreakFits.Hmm, but comments inside f-string interpolations are unlikely (and the compiler rejects multiline interpolations anyway). So this might be acceptable.
Actually, let me reconsider. The issue is specifically about f-string interpolations being broken. The most common case is a function call:
f"{shared.format_duration(remaining,)}"This would be formatted as:
f"{shared.format_duration( remaining, )}"The
callfunction producesStrictBreaks for separating arguments. When the group is broken, theseStrictBreaks produce newlines.If I wrap the expression in
NextBreakFits(expr, false):- In
fits, the inner is processed inForcedUnbrokenmode StrictBreaks use theirunbrokenversionsfitsreturnstrue(content "fits" without breaks)Grouprenders inUnbrokenmode- In
format, the inner is processed inUnbrokenmode StrictBreaks use theirunbrokenversions- No newlines are produced
Wait, but the issue is that the
Groupinside the call expression decides to break. Let me trace through more carefully:The call expression produces a
Documentlike:head .append("(") .append(strict_break("", "")) .append(body) // args .nest(INDENT_WIDTH) .append(close_sep) .append(")") .next_break_fits(false) .group()This is
Group(Sequence([...])). TheGroupchecksfitsto decide whether to break.If the expression is wrapped in
NextBreakFits(expr, false):fitsis called withUnbrokenmode (from the outer context, e.g., the f-string)NextBreakFitspushes inner inForcedUnbrokenmode- The
Groupis processed inForcedUnbrokenmode - In
fitsforGroup: check if inner fits inUnbrokenmode - Inner contains
StrictBreaks. InUnbrokenmode,StrictBreakusesunbrokenversion fitsreturnstrue(because no breaks are taken)Grouprenders inUnbrokenmode
Wait, but step 4 checks if inner fits in
Unbrokenmode. The inner is theSequenceof the call. ThisSequencecontainsStrictBreaks. InUnbrokenmode,StrictBreaks use theirunbrokenversion. Sofitswould compute the total width without any breaks.If the total width is within the limit,
fitsreturnstrue. If not,fitsreturnsfalse, and the group is broken.So the
Groupinside the call could still break if the content is too long. This is the same as the original behavior.Hmm, so
NextBreakFits(expr, false)doesn't actually prevent groups from breaking. It only affects how breaks are computed infits.OK so I really do need a new
ForceUnbrokenvariant that:- Wraps content and forces it to be rendered on one line
- This means groups should not break even if the content doesn't fit
Let me think about what
ForceUnbrokenshould do:- In
fits: push inner doc withForcedUnbrokenmode (so all breaks are not taken, andfitsreturns true) - In
format: push inner doc withForcedUnbrokenmode (so all breaks use their unbroken versions) - But also, groups should not break - they should always render in unbroken mode
For the group behavior, I need to modify the
Grouphandling in bothfitsandformatto check the current mode:- If current mode is
ForcedUnbroken, the group should always render inUnbrokenmode - This means
fitsshould returntruefor the group, andformatshould always useUnbrokenmode
Let me implement this:
In
fits:Document::Group(doc) => match mode { Mode::Broken => docs.push((indent, Mode::Unbroken, doc)), Mode::ForcedUnbroken => { // Inside ForceUnbroken, the group should always "fit" // Check with ForcedUnbroken mode to ensure no breaks are taken docs.push((indent, Mode::ForcedUnbroken, doc)); } _ => docs.push((indent, mode, doc)), },Wait, but if the current mode is
ForcedUnbrokenand the group is checked withForcedUnbrokenmode, the inner content would be processed inForcedUnbrokenmode.StrictBreaks would use theirunbrokenversions. Sofitswould returntrue(the content "fits" because no breaks are taken).But then in
format, when the group is processed:Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }This code always checks
fitswithUnbrokenmode, regardless of the current mode. So even if the current mode isForcedUnbroken, the group would be checked inUnbrokenmode. If the content doesn't fit within the limit, it would be broken.So I need to modify the
Grouphandling informatas well:Document::Group(doc) => match mode { Mode::ForcedUnbroken => { // Inside ForceUnbroken, always render in Unbroken mode docs.push((indent, Mode::Unbroken, doc)); } _ => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } } },This would ensure that inside
ForceUnbroken, groups always render inUnbrokenmode.OK so the plan is:
- Add
ForceUnbroken(Box<Self>)variant to theDocumentenum - Handle it in
fits: push inner withForcedUnbrokenmode - Handle it in
format: push inner withForcedUnbrokenmode - Modify
Grouphandling infits: inForcedUnbrokenmode, check withForcedUnbrokenmode (so all breaks are not taken) - Modify
Grouphandling informat: inForcedUnbrokenmode, always render inUnbrokenmode - Add
force_unbroken()method onDocument - In
format_string, wrap expression content withforce_unbroken()
Actually wait, I need to reconsider the
fitshandling for groups. Let me think about this more carefully.When
fitsis called on aGroup:Document::Group(doc) => match mode { Mode::Broken => docs.push((indent, Mode::Unbroken, doc)), _ => docs.push((indent, mode, doc)), },If the mode is
ForcedUnbroken, it would push withForcedUnbrokenmode. The inner content would be processed inForcedUnbrokenmode.StrictBreaks would use theirunbrokenversions. Sofitswould returntrue(the content "fits" because no breaks are taken).But wait, this is the same as what would happen if I don't handle
ForcedUnbrokenspecially. The current code would push with the current mode (ForcedUnbroken), and the inner would be processed inForcedUnbrokenmode.So the
fitshandling for groups is already correct forForcedUnbrokenmode. The issue is informat.In
format, theGrouphandling is:Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }This always checks
fitswithUnbrokenmode. If the content doesn't fit, it's broken. This doesn't respect the current mode.So I need to modify this to check the current mode:
Document::Group(doc) => match mode { Mode::ForcedUnbroken => { // Inside ForceUnbroken, always render in Unbroken mode docs.push((indent, Mode::Unbroken, doc)); } _ => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } } },This would ensure that inside
ForceUnbroken, groups always render inUnbrokenmode, even if the content doesn't fit.OK, let me implement this fix.
Actually, I realize I should also consider
ForceBrokeninsideForceUnbroken. If there's aForceBrokeninside the expression (for comments), thefitsfunction would returnfalse(doesn't fit). But informat, the group would be forced toUnbrokenmode, andForceBrokenwould just pass through with the current mode (Unbroken). TheDocument::NewlineinsideForceBrokenwould still produce a newline.So comments inside f-string interpolations would still produce newlines. This is a problem because f-string interpolations can't have newlines.
Hmm, but the issue description doesn't mention comments. Let me focus on the main issue first (function calls being broken) and see if the tests pass.
Actually, looking at the issue more carefully:
lis buildrejects multiline interpolation expressions insidef"...{...}"stringsThe issue is specifically about multiline interpolation. Comments inside f-string interpolations are a separate issue (and probably a compiler error anyway).
Let me also consider: what if I don't call
self.expression(e)for f-string interpolations, but instead extract the raw source text?Looking at the AST, expressions have
spanfields. I could use the span to extract the raw text from the source. But the formatter doesn't have access to the source text directly.Actually, looking at the
Literalhandling inexpression.rs:Literal::Integer { value, text } => { if let Some(original) = text { Document::string(original.clone()) } else { Document::string(value.to_string()) } }Some literals preserve their original text. Maybe I can do the same for expressions inside f-string interpolations.
But the
Expressionenum doesn't have atextfield. So I'd need to extract the text from the source, which requires access to the source.Hmm, this is more complex than I thought. Let me stick with the
ForceUnbrokenapproach.Wait, actually, let me re-read the
format_stringfunction:pub(super) fn format_string(&mut self, parts: &'a [FormatStringPart]) -> Document<'a> { let mut docs = vec![Document::str("f\"")]; for part in parts { match part { FormatStringPart::Text(s) if s.contains('\n') => { docs.push(Document::verbatim(s.clone())) } FormatStringPart::Text(s) => docs.push(Document::string(s.clone())), FormatStringPart::Expression(e) => { docs.push(Document::str("{")); docs.push(self.expression(e)); docs.push(Document::str("}")); } } } docs.push(Document::str("\"")); concat(docs) }The issue is that
self.expression(e)can produce multi-line output. If I wrap it inforce_unbroken(), the output would be on a single line.But wait,
self.expression(e)returns aDocument. Theforce_unbroken()method would wrap this document in aForceUnbrokenvariant. Informat, the inner document would be processed inForcedUnbrokenmode. AllStrictBreaks andFlexBreaks would use theirunbrokenversions. Groups would always render inUnbrokenmode (with my modification).So the expression would be rendered on a single line, with no breaks. This is what we want.
But there's a subtlety: the
expressionfunction itself callsprepend_comments, which addsForceBrokenwrappers:pub fn expression(&mut self, expression: &'a Expression) -> Document<'a> { let start = expression.get_span().byte_offset; let comments = self.comments.take_comments_before(start); // ... prepend_comments(doc, comments) } pub fn prepend_comments(doc: Document, comments: Option<Document>) -> Document { match comments { Some(c) => c .append(Document::Newline) .force_break() .append(doc.group()), None => doc, } }So if there are comments before the expression, the result would be:
ForceBroken(CommentDoc + Newline) + Group(expression)When this is wrapped in
ForceUnbroken:ForceUnbroken(ForceBroken(CommentDoc + Newline) + Group(expression))In
fits:ForceBrokenreturnsfalse(doesn't fit), sofitsreturnsfalse- The group would be broken... wait, but the group is outside the
ForceBroken - Actually, the
ForceBrokenwraps the comments, not the expression
Hmm, let me think about this more carefully. The
prepend_commentsfunction adds comments BEFORE the expression:ForceBroken(comment + Newline) + Group(expression)If this is wrapped in
ForceUnbroken:ForceUnbroken(ForceBroken(comment + Newline) + Group(expression))In
formatwithForcedUnbrokenmode:ForceBroken(comment + Newline)is processed inForcedUnbrokenmode- In
format,ForceBrokenjust pushes inner with current mode (ForcedUnbroken) - Inner is
comment + Newline commentis text, which is rendered inForcedUnbrokenmodeNewlineproduces a newline regardless of mode- Then
Group(expression)is processed inForcedUnbrokenmode - The group is forced to
Unbrokenmode - Expression is rendered in
Unbrokenmode
So comments before the expression would still produce newlines, even with
ForceUnbroken. This is a problem.OK so I need to handle
ForceBrokeninsideForceUnbrokendifferently. InForcedUnbrokenmode,ForceBrokenshould not produce newlines.Hmm, but
Document::Newlinealways produces a newline. I can't prevent that without modifying theNewlinehandling.Actually, let me reconsider. Comments inside f-string interpolations are unlikely. The issue description doesn't mention comments. Let me focus on the main issue first (function calls being broken) and see if the tests pass.
But I should also think about what the correct behavior is for comments inside f-string interpolations. If the compiler rejects multiline f-string interpolations, then comments inside f-string interpolations are also rejected. So the formatter should probably just keep everything on one line, even if there are comments.
To handle comments inside f-string interpolations, I need to modify the
ForceBrokenhandling inForcedUnbrokenmode. InForcedUnbrokenmode,ForceBrokenshould:- Not produce newlines (i.e., skip the
Newlineinside) - Render the comment text inline
But this is complex. Let me first implement the basic
ForceUnbrokenand wrap expressions in f-string interpolations with it. Then I'll see if the tests pass.Actually, I just realized something. The
prepend_commentsfunction adds comments BEFORE the expression, which means the comments would be between{and the expression. For example:f"before { // comment expr }"But f-string interpolations can't have newlines. So the compiler would reject this anyway. The formatter should probably just keep the comments inline:
f"before { // comment expr }"Hmm, but this is ugly. Let me just focus on the main issue (function calls being broken) for now.
Actually, I think the cleanest solution is to NOT format expressions inside f-string interpolations at all. Instead, just use the raw source text. But as I mentioned, the formatter doesn't have access to the source text.
Wait, actually, I just had another idea. What if I modify the
format_stringfunction to handle expressions differently? Instead of callingself.expression(e), I could:- Extract the raw source text for the expression (from the source code)
- Use that text directly in the f-string
But this requires access to the source code, which the formatter doesn't have.
OK let me just implement the
ForceUnbrokenapproach. For comments, I'll handle them by makingForceBrokeninsideForcedUnbrokenmode skip theNewline. This way, comments would be rendered inline.Let me implement this:
- Add
ForceUnbroken(Box<Self>)variant to theDocumentenum - Handle it in
fits: push inner withForcedUnbrokenmode - Handle it in
format: push inner withForcedUnbrokenmode, but skipNewlinewhen inForcedUnbrokenmode
Actually, modifying
Newlineto skip inForcedUnbrokenmode is too invasive. Let me think of a different approach.What if I modify
ForceBrokento not produce newlines when inForcedUnbrokenmode? Instead, it would just render the inner content inline.Hmm, but
ForceBrokencontains aDocument, which might containNewline. I can't preventNewlinefrom producing newlines without modifying theNewlinehandling.OK let me just implement the basic
ForceUnbrokenand see if it works for the main issue. Comments inside f-string interpolations can be handled separately if needed.Wait, actually, I just realized something. The
expressionfunction callsself.comments.take_comments_before(start)to get comments. If the expression is inside an f-string interpolation, the comments would be taken from the comment cursor. But the comment cursor is shared across all formatting. So if I format the f-string, the comments would be taken and used in theexpressioncall.But actually, comments inside f-string interpolations would be after the
{and before the expression. Theexpression.get_span().byte_offsetis the start of the expression, which is after the{. So comments between{and the expression would be taken byexpression.This means if there are comments inside f-string interpolations, they would be included in the formatted output, which would produce newlines.
But as I said, f-string interpolations can't have newlines. So this is a problem.
Hmm, let me just implement the basic fix and see if the tests pass. If there are issues with comments, I'll handle them later.
Actually, let me think about this differently. Maybe I should use the source text directly for expressions inside f-string interpolations. The formatter has access to the source code through the
Commentsstruct.Let me look at how comments are handled:
pub struct Comments<'a> { // ... }Actually, looking at the
Commentsstruct, it has asourcefield:pub struct Comments<'a> { source: &'a str, // ... }So I can extract the source text for an expression using its span!
Let me check:
let text = &source[expr_span.byte_offset as usize..(expr_span.byte_offset + expr_span.byte_length) as usize];This would give me the source text for the expression, which I can use directly in the f-string.
But wait, this would bypass all formatting. The expression would be formatted exactly as it appears in the source. This might not be ideal if the source has formatting issues.
But for f-string interpolations, the compiler requires that they be on a single line. So the formatter should respect this constraint and not try to reformat expressions inside f-string interpolations.
Hmm, but the issue is that the formatter IS trying to reformat expressions inside f-string interpolations, and it's producing multiline output. So the fix should prevent reformatting.
Actually, using the source text directly is a valid approach. The source text is what the user wrote, and if it's a single line, it's already formatted correctly. If it's multiline, the user should fix it.
But wait, the issue is that the formatter is REWRITING single-line expressions to multiline. So even if the source is single-line, the formatter produces multiline output. Using the source text directly would preserve the single-line format.
OK so let me implement this approach:
- In
format_string, for expressions, extract the source text using the span - Use the source text directly instead of calling
self.expression(e)
But this requires access to the source text, which I need to pass to the formatter.
Looking at the
Formatterstruct:pub struct Formatter<'a> { pub(super) comments: Comments<'a>, }And
Comments:pub struct Comments<'a> { source: &'a str, // ... }So I can access the source text through
self.comments.source. Let me check if this is public:<tool_call> <function=bash_terminal> <parameter=command> grep -n "pub" /lisette/crates/format/src/comments.rs | head -20 </tool_call>
Assistant
The user wants me to fix an issue where the Lisette formatter rewrites f-string interpolations to multiline, which the compiler rejects. Let me continue with my analysis and implement the fix.
Looking at my analysis, I have two main approaches:
- Add a
ForceUnbrokenDocument variant to prevent breaking inside f-string interpolations - Use the raw source text for expressions inside f-string interpolations
Let me check if I can access the source text through the Comments struct.
- Add a
Assistant
Let me understand the Comments struct better:
Assistant
Let me continue with my analysis and implement the fix. I'll take the approach of adding a
ForceUnbrokenDocument variant to prevent breaking inside f-string interpolations.Let me first look at the current state of the code and then implement the changes:
Assistant
Let me explore the issue more and check the existing tests.
Assistant
Let me understand the issue better and then implement the fix.
The issue is that the Lisette formatter rewrites f-string interpolations to multiline, which the compiler rejects. The key is in the
format_stringfunction inexpression.rs.Let me look at the issue from a different angle. The formatter produces multi-line output for expressions inside f-string interpolations. The compiler rejects this. The fix should prevent the formatter from producing multi-line expressions inside f-string interpolations.
My approach: Add a
ForceUnbrokenDocument variant that forces content to stay on one line. This variant:- In
fits: pushes the inner doc withForcedUnbrokenmode (so all breaks are not taken) - In
format: pushes the inner doc withForcedUnbrokenmode (so all breaks use their unbroken versions) - Additionally, groups should not break when inside
ForceUnbroken
Then, in
format_string, wrap expressions withforce_unbroken().Let me also consider: what about comments inside f-string interpolations? The
ForceBrokenhandler infitsreturnsfalse(doesn't fit) unless inForcedBrokenmode. InForcedUnbrokenmode, this would causefitsto returnfalse. But informat,ForceBrokenjust passes through, andDocument::Newlinewould still produce a newline.To handle this, I should make
ForceBrokeninsideForcedUnbrokenmode skip newlines. But this is complex.Actually, let me reconsider. The
format_stringfunction processes expressions throughself.expression(e). This function callsprepend_comments, which addsForceBrokenwrappers. If there are comments inside f-string interpolations, they would produce newlines.But wait, f-string interpolations can't have comments inside them (because that would make them multiline). So the formatter shouldn't produce comments inside f-string interpolations in the first place.
Let me check: when the formatter processes
f"text {expr}", the expressionexpris parsed and the formatter produces a Document for it. If there are comments between{andexpr, they would be part of the expression's span.Actually, let me think about this more carefully. The expression's span starts at the beginning of the expression, not at the
{. So comments between{and the expression would be after the expression's span.Wait, no. The expression's span starts at the first character of the expression. So if the expression is
foo, the span starts atf. Comments between{andfoowould be at positions before the expression's span start.The
expressionfunction takes comments before the expression:let start = expression.get_span().byte_offset; let comments = self.comments.take_comments_before(start);So comments between
{and the expression would be taken. This means if there are comments inside f-string interpolations, they would be included in the formatted output.But this is a problem because f-string interpolations can't have newlines. So I need to handle this case.
One approach: modify
format_stringto NOT take comments for expressions inside f-string interpolations. Instead, just format the expression without comments.But this requires modifying the
expressionfunction or creating a new function that doesn't take comments.Hmm, let me think about this differently. Maybe I should just focus on the main issue (function calls being broken) and not worry about comments for now. Comments inside f-string interpolations are a separate issue that might not even occur in practice.
Let me implement the basic fix first:
- Add
ForceUnbrokenDocument variant - Handle it in
fitsandformat - Modify Group handling to respect
ForcedUnbrokenmode - Wrap expressions in f-string interpolations with
force_unbroken()
For comments, I'll handle them later if needed.
Actually, I just realized something important. The
force_break()method createsForceBroken(Box::new(self)). And in the Document enum:Document::ForceBroken(doc) => match mode { Mode::ForcedBroken => docs.push((indent, mode, doc)), _ => return false, },In
fits,ForceBrokenreturnsfalseunless inForcedBrokenmode. This means if I wrap the expression inForceUnbroken, and the expression containsForceBroken(for comments), thefitsfunction would returnfalse.But then in
format, the Group would be broken. And theForceBrokeninside would be processed with the current mode.Hmm, this is getting complicated. Let me just implement the basic fix and see if it works.
Actually, wait. Let me re-read the issue description:
Formatter rewrites Lisette f-strings to multiline interpolation that the compiler rejects
The issue is specifically about f-string interpolations being rewritten to multiline. The example is:
f"You cannot use another ender pearl for {shared.format_duration( remaining, )}."This is a function call being broken across lines. There are no comments involved.
So the fix should focus on preventing function calls (and other expressions) from being broken inside f-string interpolations. Comments are a separate concern.
Let me implement the fix. I'll add a
ForceUnbrokenvariant and wrap expressions in f-string interpolations with it.Actually, I just realized there might be a simpler approach. Instead of adding a new Document variant, I can modify the
format_stringfunction to use raw source text for expressions. This way, the expression is not re-formatted at all, and the original formatting is preserved.But as I mentioned earlier, this requires access to the source text. Let me check if the Formatter has access to the source.
Looking at the
Formatterstruct:pub struct Formatter<'a> { pub(super) comments: Comments<'a>, }And
Comments:pub struct Comments<'a> { source: &'a str, // ... }The
sourcefield is private (not public). But I can add a method to access it.Actually, let me just add a public method to
Commentsto get the source text. Then I can use it informat_string.Wait, but extracting raw source text has its own issues. The source text might contain comments, blank lines, etc. If I just use the raw source text, I might include comments inside f-string interpolations, which would produce multiline output.
Hmm, let me think about this more carefully. The issue is that the formatter is REWRITING the expression, not just preserving the source. The original source might be single-line, but the formatter produces multi-line output.
So the fix should be to prevent the formatter from producing multi-line output for expressions inside f-string interpolations. This is what
ForceUnbrokenwould do.OK let me just implement the
ForceUnbrokenapproach.Actually, let me think about this problem from a different angle. The root cause is that the formatter uses
StrictBreakandFlexBreakinside expression formatting, which can produce newlines. For f-string interpolations, I need to prevent these breaks.Looking at the
StrictBreakhandling informat:Document::StrictBreak { broken, unbroken } => match mode { Mode::Broken | Mode::ForcedBroken => { // output broken version (with newline) } Mode::Unbroken | Mode::ForcedUnbroken => { // output unbroken version (no newline) } },So in
UnbrokenorForcedUnbrokenmode,StrictBreakuses the unbroken version. This means if I can ensure that the expression is processed inUnbrokenmode, theStrictBreaks would use the unbroken version.But the problem is that
Groupdecides to break based onfits, and then renders inBrokenmode. Thefitsfunction is always called withUnbrokenmode (for Groups), so the Group might decide to break even if the outer mode isUnbroken.Wait, actually, let me re-read the Group handling in
format:Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }The Group always checks
fitswithUnbrokenmode. If the content fits (in Unbroken mode), it's rendered inUnbrokenmode. If not, it's rendered inBrokenmode.So if I wrap the expression in something that forces
Unbrokenmode, the Group would still decide based on its ownfitscheck. But the rendering would be inUnbrokenmode if the content fits.But if the content doesn't fit, the Group would render in
Brokenmode, and theStrictBreaks would use the broken version.So just forcing
Unbrokenmode for the outer context doesn't prevent Groups from breaking.OK so I really do need the
ForceUnbrokenvariant that also prevents Groups from breaking.Let me implement it.
Here's my plan:
- Add
ForceUnbroken(Box<Self>)variant to theDocumentenum inlindig.rs - Handle it in
fits: push inner withForcedUnbrokenmode - Handle it in
format: push inner withForcedUnbrokenmode - Modify
Grouphandling informat: inForcedUnbrokenmode, always render inUnbrokenmode - Modify
Grouphandling infits: inForcedUnbrokenmode, check withForcedUnbrokenmode (so all breaks are not taken, andfitsreturns true) - Add
force_unbroken()method onDocument - In
format_string, wrap expressions withforce_unbroken()
Wait, but there's still the issue of
ForceBroken(for comments) inside f-string interpolations. InForcedUnbrokenmode,ForceBrokenreturnsfalseinfits, which would causefitsto return false. But informat, the Group would be forced toUnbrokenmode, andForceBrokenwould just pass through.The
Document::NewlineinsideForceBrokenwould still produce a newline. So comments inside f-string interpolations would still produce newlines.To handle this, I need to modify the
Newlinehandling informatto skip newlines when inForcedUnbrokenmode.Document::Newline => { if mode != Mode::ForcedUnbroken { output.push('\n'); pending_indent = indent; width = indent; } }This would skip newlines when in
ForcedUnbrokenmode. Comments inside f-string interpolations would be rendered inline without newlines.But wait, this might have unintended side effects. Let me think about whether this is correct.
In
ForcedUnbrokenmode, we want all content to be on one line. So skipping newlines makes sense. But what about other content that produces newlines, likeVerbatimText?Looking at
VerbatimTexthandling:Document::VerbatimText(s) => { if pending_indent >= 0 { write_indent(output, pending_indent); pending_indent = -1; } let mut segments = s.split('\n'); if let Some(first) = segments.next() { output.push_str(first); width += first.graphemes(true).count() as isize; } for segment in segments { output.push('\n'); output.push_str(segment); width = segment.graphemes(true).count() as isize; } }This outputs newlines from the text content. In
ForcedUnbrokenmode, I should probably also skip these newlines.Hmm, but
VerbatimTextis used for literal text that contains newlines (like multiline strings). These are typically at the text level, not inside f-string interpolations.For f-string interpolations, the content is expressions, which shouldn't contain
VerbatimText. So modifyingNewlinehandling should be sufficient.Actually, wait. The expression formatter might produce
VerbatimTextin some cases. Let me check...Looking at the expression formatter, I don't see any
VerbatimTextbeing produced for expressions.VerbatimTextis only used for literal text (strings with newlines).So modifying the
Newlinehandling should be sufficient for f-string interpolations.OK let me implement the fix.
Actually, wait. I just realized that I should also consider the
NextBreakFitshandling. When insideForcedUnbrokenmode,NextBreakFits(doc, enabled):In
fits:Document::NextBreakFits(doc, enabled) => { if *enabled { match mode { Mode::ForcedUnbroken => docs.push((indent, mode, doc)), _ => docs.push((indent, Mode::ForcedBroken, doc)), } } else { docs.push((indent, Mode::ForcedUnbroken, doc)); } }In
ForcedUnbrokenmode, ifenabledis true, it would push withForcedUnbrokenmode. Ifenabledis false, it would push withForcedUnbrokenmode. So both cases push withForcedUnbrokenmode.In
format:Document::ForceBroken(document) | Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }It just pushes with the current mode (
ForcedUnbroken). So the inner is processed inForcedUnbrokenmode.This seems correct.
OK let me implement the fix now.
Let me also think about edge cases:
- Expression is just a simple identifier (no formatting needed)
- Expression is a call with no args
- Expression is a long call that exceeds the line limit
- Expression contains nested expressions
For all these cases,
ForceUnbrokenshould keep the output on one line.Let me also consider: what if the expression contains a
MatchorLambdaor other complex expression? These might produce multi-line output. WithForceUnbroken, they would be kept on one line, which might make the line very long. But that's acceptable for f-string interpolations.OK, let me implement the fix.
Actually, I just realized something. The
Grouphandling infitsneeds to be modified to handleForcedUnbrokenmode. Currently:Document::Group(doc) => match mode { Mode::Broken => docs.push((indent, Mode::Unbroken, doc)), _ => docs.push((indent, mode, doc)), },In
ForcedUnbrokenmode, it would push withForcedUnbrokenmode. The inner content would be processed inForcedUnbrokenmode, and all breaks would be not taken. Sofitswould returntrue.This is correct behavior. In
ForcedUnbrokenmode, the Group should "fit" because no breaks are taken.But in
format, the Group handling is:Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }This always checks
fitswithUnbrokenmode, regardless of the current mode. So even if the current mode isForcedUnbroken, the Group would check withUnbrokenmode. If the content doesn't fit within the limit, it would be broken.So I need to modify this to check the current mode:
Document::Group(doc) => match mode { Mode::ForcedUnbroken => { // Inside ForceUnbroken, always render in Unbroken mode docs.push((indent, Mode::Unbroken, doc)); } _ => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } } },This would ensure that inside
ForceUnbroken, groups always render inUnbrokenmode.OK, I have a clear plan. Let me implement the fix.
Here's my plan:
- Add
ForceUnbroken(Box<Self>)variant to theDocumentenum inlindig.rs - Handle it in
fits: push inner withForcedUnbrokenmode - Handle it in
format: push inner withForcedUnbrokenmode - Modify
Grouphandling informat: inForcedUnbrokenmode, always render inUnbrokenmode - Add
force_unbroken()method onDocument - In
format_string, wrap expressions withforce_unbroken() - Also handle
NewlineinsideForcedUnbrokenmode: skip newlines
Let me implement this step by step.
Actually wait, I need to also handle
ForceBrokeninsideForcedUnbroken. When inForcedUnbrokenmode,ForceBrokenreturnsfalseinfits, which would cause the outer context to think the content doesn't fit. But informat, the Group would be forced toUnbrokenmode.The issue is that
ForceBrokencontainsDocument::Newline, which would produce a newline informat. I need to skipNewlineinForcedUnbrokenmode.So my plan should also include: 8. Modify
Newlinehandling informat: inForcedUnbrokenmode, skip newlinesActually, wait. I also need to handle
ForceBrokeninfitsforForcedUnbrokenmode. Currently:Document::ForceBroken(doc) => match mode { Mode::ForcedBroken => docs.push((indent, mode, doc)), _ => return false, },In
ForcedUnbrokenmode, this returnsfalse(doesn't fit). This would cause the outer context (e.g., a Group) to think the content doesn't fit.But in
format, the Group would be forced toUnbrokenmode, andForceBrokenwould just pass through. TheDocument::Newlinewould produce a newline.So I need to modify both
fitsandformatforForceBrokeninForcedUnbrokenmode:- In
fits: push inner with current mode (don't return false) - In
format: skipNewlineinside (already handled by the Newline modification)
Wait, but
ForceBrokenis used for comments, and it containsDocument::Newline. If I skipNewlineinForcedUnbrokenmode, the comment text would be rendered inline without a newline before it. This might not be the desired output.Hmm, let me think about what the desired output is for comments inside f-string interpolations.
If the original code is:
f"before { // comment expr }"The compiler would reject this because it's multiline. So the user would write:
f"before {expr}"Or:
f"before { // comment expr }"In the second case, the comment is on the same line as the expression. The formatter should preserve this.
But the
prepend_commentsfunction adds aDocument::Newlinebefore the comment. So the output would be:f"before {\n// comment expr}"Which is multiline and rejected by the compiler.
So I need to prevent the
Document::Newlinefrom being produced in f-string interpolations. SkippingNewlineinForcedUnbrokenmode would do this.But this would also skip
Newlinein other contexts whereForcedUnbrokenis used (if any). Let me think about whether there are other uses ofForceUnbroken.Currently, there are no uses of
ForceUnbrokenbecause it doesn't exist yet. So adding it and skippingNewlineinForcedUnbrokenmode should be safe.OK, let me implement the fix.
Actually, I realize I need to be more careful. Let me re-read the
prepend_commentsfunction:pub fn prepend_comments(doc: Document, comments: Option<Document>) -> Document { match comments { Some(c) => c .append(Document::Newline) .force_break() .append(doc.group()), None => doc, } }So if there are comments, the result is:
ForceBroken(comment + Newline) + Group(expression)If I wrap this in
ForceUnbroken:ForceUnbroken(ForceBroken(comment + Newline) + Group(expression))In
formatwithForcedUnbrokenmode:ForceBroken(comment + Newline)is processed inForcedUnbrokenmode- In
format,ForceBrokenjust pushes inner with current mode (ForcedUnbroken) - Inner is
comment + Newline commentis text, rendered normallyNewline- if I skip newlines inForcedUnbrokenmode, this would be skipped- Then
Group(expression)is processed inForcedUnbrokenmode - The group is forced to
Unbrokenmode - Expression is rendered in
Unbrokenmode
So with skipping
NewlineinForcedUnbrokenmode, comments inside f-string interpolations would be rendered inline without newlines. This might produce:f"before {comment expr}"Which is on one line and accepted by the compiler. This is acceptable.
OK let me implement the fix now.
Actually, I realize I should also handle
ForceBrokeninfitsforForcedUnbrokenmode. Currently:Document::ForceBroken(doc) => match mode { Mode::ForcedBroken => docs.push((indent, mode, doc)), _ => return false, },In
ForcedUnbrokenmode, this returnsfalse(doesn't fit). This would cause the outer context to think the content doesn't fit.But in
fits, the outer context is a Group. If the Group is checked inForcedUnbrokenmode (because it's inside aForceUnbrokenwrapper), the Group would process its inner content inForcedUnbrokenmode. TheForceBrokenwould returnfalse, andfitswould returnfalse.Then in
format, the Group is forced toUnbrokenmode (because of my modification). So the Group would be rendered inUnbrokenmode, and the content would be on one line.But wait, the
fitsreturningfalsedoesn't matter informatbecause the Group is forced toUnbrokenmode. So thefitsbehavior forForceBrokeninForcedUnbrokenmode doesn't matter.Hmm, but what about other contexts? For example, if
ForceBrokenis at the top level (not inside a Group), would thefitsreturningfalsematter?Looking at how
fitsis used:- It's called by
Groupto decide whether to break - It's called by
FlexBreakinformatto decide whether to use broken or unbroken version
If
ForceBrokenis at the top level,fitsreturningfalsedoesn't matter because there's no Group to check.If
ForceBrokenis inside aFlexBreak, theFlexBreakwould callfitsto decide whether to break. IfForceBrokenreturnsfalse, theFlexBreakwould use the broken version.But if the current mode is
ForcedUnbroken, theFlexBreakinformatwould checkfitswith the remaining docs. IfForceBrokenis in the remaining docs,fitswould returnfalse. But the current mode isForcedUnbroken, and theFlexBreakcheck informatis:Document::FlexBreak { broken, unbroken } => { let unbroken_width = width + unbroken.len() as isize; if mode == Mode::Unbroken || fits(limit, unbroken_width, docs.clone()) { // use unbroken } else { // use broken } }If the current mode is
ForcedUnbroken, themode == Mode::Unbrokencheck is false. So it would checkfits. Iffitsreturnsfalse, it would use the broken version.Hmm, so
ForceBrokeninsideForcedUnbrokenmode would causeFlexBreakto use the broken version informat.But wait, the issue is that
FlexBreakinfits(forForcedUnbrokenmode) adds the unbroken width:Document::FlexBreak { unbroken, .. } => { match mode { Mode::Broken | Mode::ForcedBroken => return true, Mode::Unbroken | Mode::ForcedUnbroken => { current_width += unbroken.len() as isize; } } }So in
fits,FlexBreakinForcedUnbrokenmode adds the unbroken width and continues. It doesn't returnfalse.But in
format,FlexBreakchecksfitswith the remaining docs. IfForceBrokenis in the remaining docs,fitswould returnfalse, and theFlexBreakwould use the broken version.This is inconsistent. In
fits,FlexBreakinForcedUnbrokenmode uses the unbroken version. But informat,FlexBreakmight use the broken version iffitsreturnsfalse(due toForceBrokenin remaining docs).This is a problem. Let me think about how to fix this.
Actually, wait. The
FlexBreakinformatchecksfitswith the remaining docs (after theFlexBreak). IfForceBrokenis in the remaining docs,fitswould returnfalse. But theFlexBreakitself doesn't containForceBroken. TheForceBrokenis in the inner content of the expression.Let me trace through a specific example:
Expression:
foo(a, // comment\n b)The formatter produces something like:
Sequence([ Text("foo"), Text("("), StrictBreak("", ""), Sequence([ Text("a"), Text(","), ForceBroken(Comment("// comment") + Newline), Text("b"), ]), StrictBreak(",", ""), Text(")"), ]).group()If I wrap this in
ForceUnbroken:ForceUnbroken(Group(Sequence([...])))In
formatwithForcedUnbrokenmode:Groupis checked. Since mode isForcedUnbroken, render inUnbrokenmode.- Push
(Unbroken, inner)where inner is the Sequence. - Process Sequence in
Unbrokenmode. - Process Text("foo") -> output "foo"
- Process Text("(") -> output "("
- Process StrictBreak in
Unbrokenmode -> use unbroken version (empty string) - Process inner Sequence in
Unbrokenmode - Process Text("a") -> output "a"
- Process Text(",") -> output ","
- Process ForceBroken in
Unbrokenmode -> push inner with current mode (Unbroken) - Inner is
Comment + Newline - Process Comment -> output "// comment"
- Process Newline -> output "\n"
So the output would be:
foo(a,// commentb)Hmm, there's no newline after the comment. That's because
ForceBrokenjust passes through with the current mode (Unbroken).But the
NewlineinsideForceBrokenwould still produce a newline. So the output would be:foo(a, // commentb)Which is multiline.
So skipping
NewlineinForcedUnbrokenmode is important. Let me modify theNewlinehandling:Document::Newline => { if mode != Mode::ForcedUnbroken { output.push('\n'); pending_indent = indent; width = indent; } }But wait,
ForceBrokenpasses through with the current mode (Unbrokenin this case). So theNewlineinsideForceBrokenwould be processed withUnbrokenmode, and it would produce a newline.Hmm, I need a different approach. Let me think...
Actually, the issue is that
ForceBrokenis designed to force a break. InsideForcedUnbrokenmode,ForceBrokenshould not force a break. So I need to modify theForceBrokenhandling informatforForcedUnbrokenmode.In
ForcedUnbrokenmode,ForceBrokenshould just pass through without forcing a break. Instead of pushing the inner with the current mode, it should skip the inner entirely (or skip theNewlineinside).Hmm, but the inner contains both the comment text and the
Newline. If I skip the inner entirely, the comment text would be lost.Let me think about what the desired behavior is. If there's a comment inside an f-string interpolation, the desired output is:
f"text { // comment expr }"So the comment should be rendered inline without a newline before or after it.
Currently,
prepend_commentsproduces:ForceBroken(Comment + Newline) + Group(expression)If I wrap this in
ForceUnbroken, theForceBrokenis insideForcedUnbrokenmode. I want theNewlineinsideForceBrokento be skipped.One approach: modify
Newlinehandling to skip newlines when the current mode isForcedUnbrokenOR when the current mode isUnbrokenand the parent isForceBroken(which is insideForceUnbroken).But this is complex. Let me think of a simpler approach.
Another approach: in
format, when processingForceBrokeninForcedUnbrokenmode, just pass through the inner without theNewline.But
ForceBrokencontains aDocument, which might not just beComment + Newline. It could be more complex.Hmm, let me look at how
prepend_commentsworks again:Some(c) => c .append(Document::Newline) .force_break() .append(doc.group()),So the result is:
ForceBroken(c + Newline) + Group(doc)The
cis a comment document (Text), andNewlineproduces a newline.If I want to skip the
NewlineinsideForceBrokenwhen inForcedUnbrokenmode, I can modify theNewlinehandling:Document::Newline => { if mode != Mode::ForcedUnbroken { output.push('\n'); pending_indent = indent; width = indent; } }But this only works if the mode is
ForcedUnbroken. In the example above,ForceBrokenpasses through with the current mode. If the outer mode isForcedUnbroken, the inner is processed withForcedUnbrokenmode. But if the outer mode isUnbroken, the inner is processed withUnbrokenmode.So the
NewlineinsideForceBrokenwould be processed with whatever mode theForceBrokenpushes.Let me trace through again:
Expression:
foo(a, // comment\n b)wrapped inForceUnbrokenThe formatter produces:
ForceUnbroken(Group(Sequence([ Text("foo"), Text("("), StrictBreak("", ""), Sequence([ Text("a"), Text(","), ForceBroken(Comment("// comment") + Newline), Text("b"), ]), StrictBreak(",", ""), Text(")"), ])))In
formatwithForcedUnbrokenmode:Group-> mode isForcedUnbroken, so render inUnbrokenmode- Push
(Unbroken, inner)where inner is the Sequence - Process Sequence in
Unbrokenmode - Process Text("foo") -> output "foo"
- Process Text("(") -> output "("
- Process StrictBreak in
Unbrokenmode -> use unbroken version (empty) - Process inner Sequence in
Unbrokenmode - Process Text("a") -> output "a"
- Process Text(",") -> output ","
- Process ForceBroken in
Unbrokenmode -> push(Unbroken, Comment + Newline) - Process Comment (Text) -> output "// comment"
- Process Newline -> output "\n"
- Process Text("b") -> output "b"
- Process StrictBreak in
Unbrokenmode -> use unbroken version (",") - Process Text(")") -> output ")"
So the output would be:
foo(a, // comment b)The
Newlineis still produced because it's processed inUnbrokenmode (notForcedUnbrokenmode).So skipping
NewlineinForcedUnbrokenmode is not enough. I need to skipNewlineinsideForceBrokenwhen the outer mode isForcedUnbroken.Hmm, but the mode is lost when
ForceBrokenpushes its inner. The inner is pushed with the current mode (which might beUnbroken).Wait, actually,
ForceBrokenjust pushes the inner with the current mode:Document::ForceBroken(document) | Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }So if the current mode is
ForcedUnbroken, the inner would be processed withForcedUnbrokenmode. In that case,Newlinewould be skipped.Let me re-trace:
Expression:
foo(a, // comment\n b)wrapped inForceUnbrokenIn
formatwithForcedUnbrokenmode:Group-> mode isForcedUnbroken, so render inUnbrokenmode- Push
(Unbroken, inner)where inner is the Sequence - Process Sequence in
Unbrokenmode - ... (same as before)
- Process ForceBroken in
Unbrokenmode -> push(Unbroken, Comment + Newline)
Wait, the current mode is
Unbroken(from step 2), notForcedUnbroken. SoForceBrokenpushes withUnbrokenmode, andNewlineis not skipped.The issue is that the
Groupchanges the mode fromForcedUnbrokentoUnbroken. So the inner content of the Group (includingForceBroken) is processed inUnbrokenmode.I need to prevent the
Groupfrom changing the mode. With my modification:Document::Group(doc) => match mode { Mode::ForcedUnbroken => { // Inside ForceUnbroken, always render in Unbroken mode docs.push((indent, Mode::Unbroken, doc)); } ... },This changes the mode to
Unbrokenfor the Group's inner content. I should instead keep the mode asForcedUnbroken:Document::Group(doc) => match mode { Mode::ForcedUnbroken => { // Inside ForceUnbroken, always render in ForcedUnbroken mode docs.push((indent, Mode::ForcedUnbroken, doc)); } ... },Then the inner content (including
ForceBroken) would be processed inForcedUnbrokenmode. AndNewlinewould be skipped.Let me re-trace with this change:
Expression:
foo(a, // comment\n b)wrapped inForceUnbrokenIn
formatwithForcedUnbrokenmode:Group-> mode isForcedUnbroken, so push(ForcedUnbroken, inner)- Process inner Sequence in
ForcedUnbrokenmode - ... (same as before)
- Process ForceBroken in
ForcedUnbrokenmode -> push(ForcedUnbroken, Comment + Newline) - Process Comment (Text) -> output "// comment"
- Process Newline -> mode is
ForcedUnbroken, so skip - Process Text("b") -> output "b"
- Process StrictBreak in
ForcedUnbrokenmode -> use unbroken version (",") - Process Text(")") -> output ")"
So the output would be:
foo(a, // commentb)Hmm, the comment is inline with "b". There's no space between the comment and "b". This might look odd, but it's on one line.
Actually, wait. Let me look at what the comment document looks like. The comment is "// comment", and the text is rendered without any separator. So the output would be:
foo(a, // commentb)This is on one line but looks weird (no space before "b"). The original code might be:
foo(a, // comment b)And the formatter would produce:
foo(a, // commentb)Which is on one line but the "b" is right after the comment. This might not be ideal, but it's acceptable because it's on one line and accepted by the compiler.
Actually, looking at the original expression
foo(a, // comment\n b), the source text has "b" on a new line. The formatter would produce:foo(a, // comment b)With my fix, it would produce:
foo(a, // commentb)Hmm, this is because the
Newlineand the indent before "b" are skipped. The "b" is right after the comment.Actually, wait. The original expression in the AST is
foo(a, b). The comment// commentis not part of the expression. It's attached to the expression as a leading comment.Let me re-read
prepend_comments:Some(c) => c .append(Document::Newline) .force_break() .append(doc.group()), None => doc,So the result is:
ForceBroken(Comment + Newline) + Group(expression)The
cis the comment document (just text), andNewlineis a separate document. So the output is:comment_text + newline + expressionWith my fix, the
Newlineis skipped, so the output is:comment_text + expressionWhich would be:
// commentb)Wait, no. The expression is
foo(a, b). So the output is:// commentfoo(a, b)But the
ForceBrokenis inside theGroup, which is inside theForceUnbroken. Let me re-trace:Expression:
foo(a, b)with a leading comment// commentThe formatter produces:
ForceBroken(Comment("// comment") + Newline) + Group(Sequence([ Text("foo"), Text("("), StrictBreak("", ""), Sequence([Text("a"), Text(","), Text("b")]), StrictBreak(",", ""), Text(")"), ]))Wait, no. The
prepend_commentswraps the comment inForceBrokenand then appends the expression (grouped). So the result is:Sequence([ ForceBroken(Comment("// comment") + Newline), Group(Sequence([...])) ])Hmm, no. Looking at
prepend_commentsagain:Some(c) => c .append(Document::Newline) .force_break() .append(doc.group()),This produces:
ForceBroken(c + Newline) + Group(doc)Which is:
Sequence([ ForceBroken(Comment("// comment") + Newline), Group(Sequence([...])) ])OK so the result is a Sequence of
ForceBroken(comment)andGroup(expression).Now, if I wrap this in
ForceUnbroken:ForceUnbroken(Sequence([ ForceBroken(Comment("// comment") + Newline), Group(Sequence([...])) ]))In
formatwithForcedUnbrokenmode:- Sequence -> push elements in reverse: Group, ForceBroken
- Process Group in
ForcedUnbrokenmode -> push(ForcedUnbroken, inner)(with my modification) - Process inner Sequence in
ForcedUnbrokenmode - Process Text("foo") -> output "foo"
- Process Text("(") -> output "("
- Process StrictBreak in
ForcedUnbrokenmode -> use unbroken version (empty) - Process inner Sequence in
ForcedUnbrokenmode - Process Text("a") -> output "a"
- Process Text(",") -> output ","
- Process Text("b") -> output "b"
- Process StrictBreak in
ForcedUnbrokenmode -> use unbroken version (",") - Process Text(")") -> output ")"
- Process ForceBroken in
ForcedUnbrokenmode -> push(ForcedUnbroken, Comment + Newline) - Process Comment (Text) -> output "// comment"
- Process Newline -> mode is
ForcedUnbroken, so skip - End of Sequence
Wait, the order is wrong. The Sequence is pushed in reverse, so the Group is processed first, then the ForceBroken.
Let me re-trace:
In
formatwithForcedUnbrokenmode:- Sequence -> push elements in reverse: ForceBroken, Group
- Process Group in
ForcedUnbrokenmode -> push(ForcedUnbroken, inner)(with my modification) - Process inner Sequence in
ForcedUnbrokenmode 4-12. Process the expression elements - Process ForceBroken in
ForcedUnbrokenmode -> push(ForcedUnforced, Comment + Newline)
Wait, the elements are pushed in reverse order. So for:
Sequence([ForceBroken(...), Group(...)])The elements are pushed in reverse: Group, then ForceBroken. So the Group is processed first, then the ForceBroken.
Hmm, but that's wrong. The Group should come after the ForceBroken in the output.
Wait, the Sequence is:
ForceBroken(Comment + Newline) + Group(expression)Which is
Sequence([ForceBroken(...), Group(...)]).When processed in reverse, the Group is pushed first, then the ForceBroken. So the Group is processed first, then the ForceBroken.
But the output should be: comment, then expression. So the ForceBroken should be processed first, then the Group.
Hmm, I think I'm confusing the order. Let me re-read the Sequence processing:
Document::Sequence(vec) => { for doc in vec.iter().rev() { docs.push((indent, mode, doc)); } }It iterates in reverse and pushes to the stack. So for
Sequence([A, B, C]), it pushes C, B, A. Then the stack pops A first, then B, then C.So the output order is A, B, C. This is correct.
Now,
ForceBroken(Comment + Newline)is A, andGroup(expression)is B. So the output order is:ForceBroken(Comment + Newline), Group(expression)Which is: comment, then expression. This is correct.
Let me re-trace:
In
formatwithForcedUnbrokenmode:- Sequence -> push elements in reverse: Group, ForceBroken
- Stack: [Group, ForceBroken]
- Pop Group -> process Group
- Group: mode is
ForcedUnbroken, so push(ForcedUnforced, inner expression) - Stack: [ForceBroken, (inner expression)]
- Pop (inner expression) -> process expression elements 7-14. Process expression elements (foo, (, a, ,, b, ,, ), etc.)
- Pop ForceBroken -> process ForceBroken
- ForceBroken in
ForcedUnforcedmode -> push(ForcedUnforced, Comment + Newline) - Stack: [(Comment + Newline)]
- Pop (Comment + Newline) -> process
- Process Comment (Text) -> output "// comment"
- Process Newline -> mode is
ForcedUnforced, so skip
So the output order is: expression, then comment. But the desired output is: comment, then expression.
Hmm, the order is wrong because the Group is processed before the ForceBroken.
Wait, let me re-read the Sequence processing. The Sequence is
[ForceBroken, Group]. When iterating in reverse:- First iteration: push Group
- Second iteration: push ForceBroken
Stack: [Group, ForceBroken] (bottom to top)
Popping from the stack:
- Pop ForceBroken -> process first
- Pop Group -> process second
So the output order is: ForceBroken, then Group. This is correct!
Let me re-trace with the correct understanding:
In
formatwithForcedUnforcedmode:- Sequence -> push elements in reverse: Group, ForceBroken
- Stack (bottom to top): [Group, ForceBroken]
- Pop ForceBroken -> process ForceBroken first!
- ForceBroken in
ForcedUnforcedmode -> push(ForcedUnforced, Comment + Newline) - Stack: [Group, (Comment + Newline)]
- Pop (Comment + Newline) -> process
- Process Comment (Text) -> output "// comment"
- Process Newline -> mode is
ForcedUnforced, so skip - Pop Group -> process Group
- Group: mode is
ForcedUnforced, so push(ForcedUnforced, inner expression) - Stack: [(inner expression)]
- Pop (inner expression) -> process expression elements 13-20. Process expression elements
So the output order is: comment, then expression. This is correct!
And the output would be:
// commentfoo(a,b,)The comment is inline with the expression. No newline between them. This is on one line and accepted by the compiler.
OK so my plan is correct. Let me implement it.
Summary of changes:
- Add
ForceUnbroken(Box<Self>)variant to theDocumentenum inlindig.rs - Handle it in
fits: push inner withForcedUnforcedmode - Handle it in
format: push inner withForcedUnforcedmode - Modify
Grouphandling informat: inForcedUnforcedmode, push withForcedUnforcedmode (notUnbroken) - Add
force_unforced()method onDocument - In
format_string, wrap expressions withforce_unforced() - Modify
Newlinehandling informat: inForcedUnforcedmode, skip newlines
Wait, I also need to modify the
Grouphandling infitsforForcedUnforcedmode. Currently:Document::Group(doc) => match mode { Mode::Broken => docs.push((indent, Mode::Unbroken, doc)), _ => docs.push((indent, mode, doc)), },In
ForcedUnforcedmode, it pushes withForcedUnforcedmode. The inner content would be processed inForcedUnforcedmode, and all breaks would be not taken. Sofitswould returntrue.This is correct. I don't need to change this.
But wait, the current code in
fitshandlesMode::Brokenspecially:Document::Group(doc) => match mode { Mode::Broken => docs.push((indent, Mode::Unbroken, doc)), _ => docs.push((indent, mode, doc)), },If the mode is
Broken, it pushes withUnbrokenmode. This is to allow the Group to try to fit in unbroken mode. If it doesn't fit, the Group would be broken in format.In
ForcedUnforcedmode, it pushes withForcedUnforcedmode. All breaks would be not taken, sofitswould returntrue. This is correct.OK so I don't need to change the
fitshandling for Groups.Now let me also think about the
Grouphandling informat:Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }This always checks
fitswithUnbrokenmode. If the content fits, it's rendered inUnbrokenmode. If not, it's rendered inBrokenmode.If the current mode is
ForcedUnforced, I want the Group to always render inUnbrokenmode (orForcedUnforcedmode). With my modification:Document::Group(doc) => match mode { Mode::ForcedUnforced => { // Inside ForceUnforced, always render in ForcedUnforced mode docs.push((indent, Mode::ForcedUnforced, doc)); } _ => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } } },This pushes the inner content with
ForcedUnforcedmode. All breaks would use their unbroken versions. So the Group would be on one line.OK, I think my plan is correct. Let me implement it.
But wait, I also need to consider: what about
ForceBrokeninsideForcedUnforcedmode? Informat,ForceBrokenjust passes through with the current mode (ForcedUnforced). The inner content would be processed inForcedUnforcedmode, andNewlinewould be skipped.This means comments inside f-string interpolations would be rendered inline without newlines. This is acceptable.
OK let me implement the fix now.
Actually, I realize I need to think about one more thing. The
format_stringfunction produces:f"..." + text + { + expression + } + text + "..."If I wrap the expression in
ForceUnforced, the output would be:f"..." + text + { + ForceUnforced(expression) + } + text + "..."In
formatwith the default mode (Unforced), theForceUnforcedwould change the mode toForcedUnforcedfor the inner expression. The inner expression would be rendered inForcedUnforcedmode, which means no breaks.Then the mode would return to
Unforcedfor the rest of the f-string.So the output would be:
f"text {expression}"Where
expressionis on one line. This is what we want.OK, let me implement the fix.
Let me start with the lindig.rs changes.
Hmm wait, I just realized I should also handle
NextBreakFitsinformatdifferently. Currently:Document::ForceBroken(document) | Document::NextBreakFits(document, _) => { docs.push((indent, mode, document)); }This pushes the inner with the current mode. If the current mode is
ForcedUnforced, the inner is processed withForcedUnforcedmode. This is correct.But what about
NextBreakFitsinformat? It just pushes the inner with the current mode. It doesn't change the mode.Hmm, actually,
NextBreakFitsis designed to be used withGroup. The pattern is:Group(NextBreakFits(inner, false))In
fits,NextBreakFits(inner, false)checks the inner inForcedUnforcedmode. If it fits, the Group renders inUnforcedmode.In
format,NextBreakFitsjust pushes the inner with the current mode. If the Group decided to render inUnforcedmode, the inner is processed withUnforcedmode.So
NextBreakFitsdoesn't change the mode by itself. It only affects thefitscheck.OK so I don't need to modify
NextBreakFitshandling.Let me now implement the fix.
Actually, wait. I just realized there's another issue. The
format_stringfunction produces aSequenceof parts. ThisSequenceis not wrapped in aGroup. So the outer context might not affect it.Let me think about how the f-string is used. The f-string is part of an expression. The expression is formatted by the outer context. The outer context might have a
Groupthat decides to break.If the f-string is:
let s = f"text {expr}"The expression
f"text {expr}"is aLiteralcontaining aFormatString. The formatter produces aDocumentfor it usingformat_string.The
format_stringproduces:Sequence([ Text("f\""), Text("text "), { + expression + }, Text("\""), ])This Sequence is not wrapped in a Group. So the parts are rendered sequentially, without any Group behavior.
If the expression is wrapped in
ForceUnforced, the expression would be rendered inForcedUnforcedmode. The outer parts (f",text,}) would be rendered in whatever mode the outer context specifies.So the f-string would be:
f"text {expression}"Where
expressionis on one line. The rest of the f-string (the text parts) are not affected byForceUnforced.But wait, the text parts are rendered in the outer mode. If the outer mode is
Unforced, the text parts are rendered normally. If the outer mode isBroken, the text parts are rendered normally (text doesn't have breaks).So the output would be:
f"text {expression}"Where
expressionis on one line. This is correct.But what if the f-string is inside a larger expression that's being broken? For example:
let msg = f"{expr}" + " more text"If the outer expression is being broken, the f-string might be on a new line. But the
ForceUnforcedonly affects the expression inside the f-string, not the f-string itself.So the output would be:
let msg = f"{expression}" + " more text"Where
expressionis on one line. The f-string might be on a new line if the outer expression is broken. But the expression inside the f-string is always on one line.This is the correct behavior.
OK, let me now implement the fix.
Here's my plan:
- Add
ForceUnforced(Box<Self>)variant to theDocumentenum inlindig.rs - Handle it in
fits: push inner withForcedUnforcedmode - Handle it in
format: push inner withForcedUnforcedmode - Modify
Grouphandling informat: inForcedUnforcedmode, push withForcedUnforcedmode - Add
force_unforced()method onDocument - In
format_string, wrap expressions withforce_unforced() - Modify
Newlinehandling informat: inForcedUnforcedmode, skip newlines
Wait, I realize I should use a different name than
ForceUnforcedto avoid confusion withForceBroken. Let me useNoBreakorForceSingleLineor something.Actually, looking at the existing naming convention:
ForceBroken- forces breakingNextBreakFits- next break fitting
The opposite of
ForceBrokenwould beForceUnbroken. ButUnbrokenis already a mode. Let me useForceUnbrokenas the variant name.Hmm, but
ForceUnbrokenwould force content to NOT break, which is what I want.Let me use
ForceUnbrokenas the variant name. Infitsandformat, I'll push withForcedUnbrokenmode (which is the same mode thatNextBreakFits(..., false)uses).Actually, wait. There's already a
Mode::ForcedUnbroken. I just need to add aDocument::ForceUnbroken(Box<Self>)variant.Let me implement this now.
Let me also think about whether there are any tests I should run to verify the fix.
Looking at the test infrastructure, I should:
- Build the project:
cargo build - Run the format tests:
cargo test --package tests spec::format
But first, let me understand the issue better by looking at the existing format tests.
<tool_call> <function=bash_terminal> <parameter=command> cd /lisette && ls tests/spec/format/ </tool_call>
- In
Assistant
I need to explore the repository structure to understand how the formatter works and where f-string formatting is handled.
Assistant
Now let me look at the key function
format_stringthat handles f-string formatting:Assistant
Let me start by understanding the issue and then implementing a fix. I'll look at the test structure first.
Assistant
Let me understand the issue better. The problem is that the Lisette formatter rewrites f-string interpolations to multiline, which the compiler rejects.
Looking at the
format_stringfunction inexpression.rs, when it encountersFormatStringPart::Expression(e), it callsself.expression(e)which can produce multi-line output because the expression can contain groups that break across lines (e.g., function calls with long arguments).The compiler rejects multiline f-string interpolations, so the formatter should prevent expressions inside f-strings from being broken across lines.
I have two potential approaches:
- Add a
ForceUnbrokenDocument variant that prevents breaking inside f-string interpolations - Use a simpler approach: wrap the expression in a way that prevents breaking
Looking at the Document model more carefully:
ForceBroken- forces breaking (used for comments)NextBreakFits- used for next-break fittingGroup- allows the formatter to decide whether to break or not based on line width
The key insight is that I need to prevent expressions inside f-string interpolations from breaking. Looking at how
force_break()works, it creates aForceBrokenwrapper. I need the opposite - something that forces content to stay on one line.One approach: add a
ForceUnbrokenDocument variant that wraps content and forces it to always render on a single line. This would:- In
fits: push the inner doc withForcedUnbrokenmode (so all breaks are not taken, andfitsreturns true) - In
format: push the inner doc withForcedUnbrokenmode (so all breaks use their unbroken versions)
Another approach: since the issue is specifically about expressions inside f-string interpolations, I could modify the
format_stringfunction to not use the full expression formatter. Instead, I could use a simpler approach that preserves the original source text for the expression.Let me look at how expressions are formatted and see if there's a simpler way to prevent breaking.
Actually, looking at the code more carefully, I see that
force_break()creates aForceBrokenwrapper. And in thefitsfunction,ForceBrokenreturnsfalseunless inForcedBrokenmode. This means if I wrap the expression inforce_break(), thefitsfunction would returnfalse, causing the outer group to be broken.That's the opposite of what I want. I need something that forces content to NOT break.
Let me think about this differently. The issue is that expressions inside f-string interpolations can have
StrictBreakandFlexBreakelements that produce newlines when the mode isBrokenorForcedBroken.For f-string interpolations, I need to prevent ALL breaking. The solution is to add a
ForceUnbrokendocument variant that:- Wraps content and forces it to render on a single line
- In
fits: push the inner doc withForcedUnbrokenmode - In
format: push the inner doc withForcedUnbrokenmode
But wait, looking at the Group handling in format:
Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }Even if the current mode is
ForcedUnbroken, the Group still checksfitswithUnbrokenmode. If the content doesn't fit, it's rendered inBrokenmode.So I need to also modify the Group handling to respect
ForcedUnbrokenmode:- In
ForcedUnbrokenmode, the Group should always render inUnbrokenmode (never break)
Let me implement this fix:
- Add
ForceUnbroken(Box<Self>)variant to theDocumentenum inlindig.rs - Handle it in
fits: push the inner doc withForcedUnbrokenmode - Handle it in
format: push the inner doc withForcedUnbrokenmode - Modify
Grouphandling informat: inForcedUnbrokenmode, always render inUnbrokenmode - Modify
Grouphandling infits: inForcedUnbrokenmode, push withForcedUnbrokenmode (so all breaks are not taken) - Add
force_unbroken()method onDocument - In
format_string, wrap expressions withforce_unbroken()
Wait, but there's also the issue of
ForceBroken(used for comments). InsideForcedUnbrokenmode,ForceBrokenreturnsfalseinfits, which would causefitsto returnfalse. But informat, the Group would be forced toUnbrokenmode, andForceBrokenwould just pass through. TheDocument::NewlineinsideForceBrokenwould still produce a newline.To handle this, I should modify the
Newlinehandling informatto skip newlines when inForcedUnbrokenmode.Let me also think about whether I need to handle
ForceBrokendifferently infitsforForcedUnbrokenmode. Currently:Document::ForceBroken(doc) => match mode { Mode::ForcedBroken => docs.push((indent, mode, doc)), _ => return false, },In
ForcedUnbrokenmode, this returnsfalse. But if I modifyGrouphandling to always render inUnbrokenmode inForcedUnbrokenmode, thefitsresult doesn't matter.Actually, wait. The
fitsfunction is called by the Group to decide whether to break. If I modify the Group handling to always render inUnbrokenmode inForcedUnbrokenmode, thefitsresult doesn't matter for the Group.But
fitsis also called byFlexBreakinformat:if mode == Mode::Unbroken || fits(limit, unbroken_width, docs.clone()) {If the current mode is
ForcedUnbroken, themode == Mode::Unbrokencheck is false. So it would checkfits. Iffitsreturnsfalse(due toForceBrokenin the remaining docs), theFlexBreakwould use the broken version.Hmm, this is getting complex. Let me think about a simpler approach.
Actually, looking at the issue more carefully, the main problem is that function calls inside f-string interpolations are being broken across lines. The simplest fix is to prevent the expression formatter from breaking expressions inside f-string interpolations.
One approach: instead of using the full expression formatter, I could use the raw source text for expressions inside f-string interpolations. This way, the expression is not re-formatted at all.
But this requires access to the source text, which the formatter doesn't have directly.
Another approach: modify the expression formatter to have a flag that prevents breaking. But this would require passing a flag through the entire expression formatting chain.
Let me go with the
ForceUnbrokenapproach. I'll add a new Document variant that wraps content and forces it to stay on one line.Actually, let me reconsider. Looking at the Document model, I see that
ForceBrokenis used for comments. And in thefitsfunction,ForceBrokenreturnsfalseunless inForcedBrokenmode. This means if I wrap the expression inforce_break(), thefitsfunction would returnfalse, causing the outer group to be broken.But I want the opposite - I want to force the expression to NOT break. So I need a
ForceUnbrokenvariant.Let me implement this fix now.
Wait, I just realized something. Looking at the
format_stringfunction:FormatStringPart::Expression(e) => { docs.push(Document::str("{")); docs.push(self.expression(e)); docs.push(Document::str("}")); }The expression is wrapped with
{and}. If the expression is formatted as multi-line, the{and}would be on separate lines from the expression content.For example:
f"{shared.format_duration( remaining, )}"Here,
{is on the same line asf", and}is on a new line. The expressionshared.format_duration(remaining,)is broken across lines.The issue is that the expression formatter produces a multi-line Document for the expression. The
format_stringfunction just concatenates the parts, so the multi-line Document is preserved.To fix this, I need to prevent the expression formatter from producing multi-line output. One way to do this is to wrap the expression in a
ForceUnbrokenwrapper that forces the content to stay on one line.OK, I think I have a clear plan now. Let me implement it.
Actually, I just thought of another approach. Instead of adding a new Document variant, I could modify the
format_stringfunction to extract the raw source text for the expression and use it directly. This would avoid the expression formatter entirely.Let me check if the formatter has access to the source text...
Looking at the Formatter struct:
pub struct Formatter<'a> { pub(super) comments: Comments<'a>, }And Comments has a
sourcefield. Let me check if I can access it.Actually, looking at the
Commentsstruct:pub struct Comments<'a> { source: &'a str, ... }The
sourcefield is private. But I could add a method to access it.However, using raw source text has its own issues:
- The source text might contain comments or other formatting that the formatter has already processed
- The source text might not be properly formatted (e.g., extra spaces, etc.)
So I think the
ForceUnbrokenapproach is better. It preserves the expression formatting but prevents breaking.Let me implement the
ForceUnbrokenapproach.Actually, wait. Let me re-read the
format_stringfunction more carefully:pub(super) fn format_string(&mut self, parts: &'a [FormatStringPart]) -> Document<'a> { let mut docs = vec![Document::str("f\"")]; for part in parts { match part { FormatStringPart::Text(s) if s.contains('\n') => { docs.push(Document::verbatim(s.clone())) } FormatStringPart::Text(s) => docs.push(Document::string(s.clone())), FormatStringPart::Expression(e) => { docs.push(Document::str("{")); docs.push(self.expression(e)); docs.push(Document::str("}")); } } } docs.push(Document::str("\"")); concat(docs) }The issue is that
self.expression(e)returns aDocumentthat can contain groups and breaks. When this Document is rendered, it can produce multi-line output.One simple fix: instead of calling
self.expression(e), I could use the raw source text for the expression. But as I mentioned, this requires access to the source text.Another approach: wrap the expression in a way that forces it to stay on one line. I can do this by using the existing Document mechanisms.
Looking at the Document model, I see that
force_break()creates aForceBrokenwrapper. But I need the opposite - I need to force the content to NOT break.One approach: use
force_break()on the Group that wraps the expression. But this would force the group to break, which is the opposite of what I want.Hmm, let me think about this differently. The issue is that expressions inside f-string interpolations can have groups that break across lines. If I can prevent these groups from breaking, the issue would be fixed.
Looking at the Group handling in format:
Document::Group(doc) => { let group_docs = vec![(indent, Mode::Unbroken, doc.as_ref())]; if fits(limit, width, group_docs) { docs.push((indent, Mode::Unbroken, doc)); } else { docs.push((indent, Mode::Broken, doc)); } }The Group checks if the content fits within the line limit. If it fits, it's rendered in
Unbrokenmode. If not, it's rendered inBrokenmode.For f-string interpolations, I want the Group to always render in
Unbrokenmode, regardless of whether the content fits. This way, the expression is always on one line, even if it's longer than the line limit.One way to do this is to wrap the expression in a Group with a very large line limit. But the line limit is passed as a parameter to
to_pretty_string(), so I can't change it for a specific part of the Document.Another way is to add a new Document variant that forces the content to stay on one line. This is the
ForceUnbrokenapproach I mentioned earlier.OK, let me implement the
ForceUnbrokenapproach. Here's my plan:- Add
ForceUnbroken(Box<Self>)variant to theDocumentenum inlindig.rs - Handle it in
fits: push the inner doc withForcedUnbrokenmode - Handle it in
format: push the inner doc withForcedUnbrokenmode - Modify
Grouphandling informat: inForcedUnbrokenmode, always render inUnbrokenmode - Add
force_unbroken()method onDocument - In
format_string, wrap expressions withforce_unbroken()
Actually, I realize I need to think about this more carefully. The
Grouphandling informatchecksfitswithUnbrokenmode to decide whether to break. Even if the current mode isForcedUnbroken, the Group still checksfitswithUnbrokenmode.So if I wrap the expression in
ForceUnbroken, the Group inside the expression would still checkfitswithUnbrokenmode. If the content doesn't fit, the Group would be broken.I need to modify the Group handling to check the current mode. If the current mode is
ForcedUnbroken, the Group should always render inUnbrokenmode.Let me implement this fix now.
Wait, I think there might be an even simpler approach. Instead of adding a new Document variant, I could modify the
format_stringfunction to use the raw source text for expressions.Looking at the AST, expressions have a
spanfield that contains the byte offset and length. I can use this to extract the source text for the expression.But the Formatter doesn't have direct access to the source text. The Comments struct has a
sourcefield, but it's private.Actually, I could add a method to the Comments struct to get the source text. Let me check if this is feasible.
Hmm, but using raw source text has issues:
- The source text might contain comments that the formatter has already processed
- The source text might not be properly formatted
So I think the
ForceUnbrokenapproach is better. Let me implement it.Actually, wait. I just realized something. Looking at the
format_stringfunction:FormatStringPart::Expression(e) => { docs.push(Document::str("{")); docs.push(self.expression(e)); docs.push(Document::str("}")); }The expression is formatted using
self.expression(e). This function calls the expression formatter, which produces aDocumentthat can contain groups and breaks.But the expression might also contain comments. The
expressionfunction takes comments before the expression and prepends them:let comments = self.comments.take_comments_before(start); ... prepend_comments(doc, comments)And
prepend_commentswraps the comments inForceBroken:Some(c) => c .append(Document::Newline) .force_break() .append(doc.group()), None => doc,So if there are comments before the expression, the result is:
ForceBroken(comment + Newline) + Group(expression)If I wrap this in
ForceUnbroken, the comments would be processed inForcedUnbrokenmode. TheForceBrokenwould pass through, and theNewlinewould be skipped (if I modify the Newline handling).But the
Groupinside would also be processed inForcedUnbrokenmode. With my modification to the Group handling, the Group would always render inUnbrokenmode.So the output would be:
commentexpressionWithout newlines. This might not look great, but it would be on one line.
Hmm, actually, let me reconsider. The issue is specifically about f-string interpolations being rewritten to multiline. The example is:
f"You cannot use another ender pearl for {shared.format_duration( remaining, )}."This is a function call being broken across lines. There are no comments involved (based on the issue description).
So the fix should focus on preventing function calls (and other expressions) from being broken inside f-string interpolations. Comments are a separate concern.
Let me implement the
ForceUnbrokenapproach and see if it works for the main issue.Actually, I think there's an even simpler approach. Looking at the
format_stringfunction:FormatStringPart::Expression(e) => { docs.push(Document::str("{")); docs.push(self.expression(e)); docs.push(Document::str("}")); }What if I just don't format the expression at all? Instead, I could use the raw source text for the expression. This would preserve the original formatting.
But the Formatter doesn't have direct access to the source text. The Comments struct has a
sourcefield, but it's private.Actually, I could add a method to the Comments struct to get a slice of the source text. Let me check if this is feasible.
Hmm, but using raw source text has issues:
- The source text might contain comments that the formatter has already processed
- The source text might not be properly formatted
Wait, actually, I think using raw source text is the right approach for f-string interpolations. The compiler rejects multiline f-string interpolations, so the formatter should respect this constraint and not try to reformat expressions inside f-string interpolations.
Let me implement this approach:
- Add a method to the Comments struct to get a slice of the source text
- In
format_string, use the raw source text for expressions
But wait, the issue is that the formatter is REWRITING the expression, not just preserving the source. The original source might be single-line, but the formatter produces multi-line output. So using the raw source text would preserve the single-line format.
Actually, I think the issue is that the expression is being reformatted by the formatter. The
self.expression(e)call produces aDocumentthat can contain groups and breaks. When this Document is rendered, it can produce multi-line output.If I use the raw source text instead, the expression would not be reformatted, and the original format would be preserved.
But the issue is that the raw source text might contain comments or other formatting that the formatter has already processed. For example, if the expression is:
shared.format_duration( remaining, )The raw source text would be:
shared.format_duration( remaining, )Which is multi-line. But the compiler rejects multi-line f-string interpolations. So the user should fix the source text to be single-line:
shared.format_duration(remaining,)If the user does this, the raw source text would be single-line, and the formatter would preserve it.
But if the user doesn't fix the source text, the raw source text would be multi-line, and the formatter would preserve the multi-line format. This would still cause the compiler to reject the code.
Hmm, but the issue is that the formatter is REWRITING single-line expressions to multi-line. So even if the user writes a single-line expression, the formatter produces multi-line output. This is the bug.
So the fix should prevent the formatter from rewriting single-line expressions to multi-line. Using the raw source text would achieve this.
But using raw source text has issues:
- The formatter wouldn't be able to fix formatting issues in the source text
- The raw source text might contain comments that the formatter has already processed
For f-string interpolations, I think using raw source text is the right approach because the compiler requires that f-string interpolations be on a single line. The formatter should respect this constraint and not try to reformat expressions inside f-string interpolations.
Let me implement this approach.
Actually, wait. I just realized something. The issue is that the formatter is rewriting single-line expressions to multi-line. But the source text might already be multi-line. In that case, the formatter would just preserve the multi-line format.
So the issue is specifically about the formatter REWRITING single-line expressions to multi-line. If I use the raw source text, the formatter would preserve the original format, whether it's single-line or multi-line.
This means:
- If the user writes a single-line expression, the formatter would preserve it.
- If the user writes a multi-line expression, the formatter would preserve it (and the compiler would reject it).
But the issue is that the formatter is REWRITING single-line expressions to multi-line. So using raw source text would fix the issue.
Let me implement this approach.
Hmm, but I'm not sure if using raw source text is the right approach. The formatter is supposed to format code consistently. If I use raw source text for f-string interpolations, the formatter wouldn't be able to fix formatting issues in the source text.
Actually, I think the right approach is to format the expression but prevent it from breaking across lines. This is the
ForceUnbrokenapproach I mentioned earlier.OK let me implement the
ForceUnbrokenapproach. Here's my plan:- Add
ForceUnbroken(Box<Self>)variant to theDocumentenum inlindig.rs - Handle it in
fits: push the inner doc withForcedUnbrokenmode - Handle it in
format: push the inner doc withForcedUnbrokenmode - Modify
Grouphandling informat: inForcedUnbrokenmode, always render inUnbrokenmode - Add
force_unbroken()method onDocument - In
format_string, wrap expressions withforce_unbroken()
Wait, I also need to handle
ForceBrokeninsideForcedUnbrokenmode. If there are comments inside f-string interpolations, theForceBrokenwould pass through, and theNewlinewould be skipped (if I modify the Newline handling).Let me also modify the Newline handling to skip newlines in
ForcedUnbrokenmode.Actually, let me think about this more carefully. The
prepend_commentsfunction wraps comments inForceBroken:Some(c) => c .append(Document::Newline) .force_break() .append(doc.group()),If I wrap the entire expression (including comments) in
ForceUnbroken, theForceBrokenwould be processed inForcedUnbrokenmode. TheForceBrokenwould just pass through, and theNewlinewould be processed inForcedUnbrokenmode.If I modify the Newline handling to skip newlines in
ForcedUnforcedmode, theNewlinewould be skipped. So the comment would be rendered inline without a newline.But this might not look great. Let me think about what the desired output is.
If the original code is:
f"before { // comment expr }"The compiler rejects this because it's multi-line. So the user should write:
f"before {expr}"Or:
f"before { // comment expr }"In the second case, the comment is on the same line as the expression. The formatter should preserve this.
But the
prepend_commentsfunction adds aDocument::Newlinebefore the comment. So the output would be:f"before {\n// comment expr}"Which is multi-line. This is a problem.
Hmm, but if the comment is inside the f-string interpolation, it's on the same line as the
{. So the source text would be:f"before { // comment expr }"The
{is on the same line as the comment. Theprepend_commentsfunction adds aNewlinebefore the comment, which would produce:f"before {\n// comment expr}"Which is multi-line. This is a problem.
So I need to modify the
prepend_commentsfunction or theformat_stringfunction to not add aNewlinebefore comments inside f-string interpolations.Actually, looking at the
prepend_commentsfunction:Some(c) => c .append(Document::Newline) .force_break() .append(doc.group()),The
Newlineis added after the comment, not before. So the output is:comment + Newline + expressionWhich is:
// comment expressionSo the comment is on one line, and the expression is on the next line.
If I wrap this in
ForceUnforced, theNewlinewould be processed inForcedUnforcedmode. If I modify the Newline handling to skip newlines inForcedUnforcedmode, theNewlinewould be skipped. So the output would be:// commentexpressionWithout a newline. This might not look great, but it would be on one line.
Actually, I think this is acceptable. If there are comments inside f-string interpolations, they would be rendered inline without newlines. This is the best we can do while keeping the output on one line.
OK let me implement the fix now.
Let me also think about whether I need to modify the
fitsfunction forForceBrokeninsideForcedUnforcedmode. Currently:Document::ForceBroken(doc) => match mode { Mode::ForcedBroken => docs.push((indent, mode, doc)), _ => return false, },In
ForcedUnforcedmode, this returnsfalse. This would cause the outer context to think the content doesn't fit. But informat, the Group would be forced toUnforcedmode, and the content would be rendered on one line.Hmm, but
fitsis also called byFlexBreakinformat:if mode == Mode::Unforced || fits(limit, unbroken_width, docs.clone()) {If the current mode is
ForcedUnforced, themode == Mode::Unforcedcheck is false. So it would checkfits. Iffitsreturnsfalse(due toForceBrokenin the remaining docs), theFlexBreakwould use the broken version.Wait, no. Looking at the
FlexBreakhandling in format:Document::FlexBreak { broken, unbroken } => { let unbroken_width = width + unbroken.len() as isize; if mode == Mode::Unbroken || fits(limit, unbroken_width, docs.clone()) { // use unbroken } else { // use broken } }The
modehere is the current mode of theFlexBreakitself, not the outer mode. If the current mode isForcedUnforced, themode == Mode::Unbrokencheck is false. So it would checkfits.Hmm, but
FlexBreakinfitsforForcedUnforcedmode adds the unbroken length:Document::FlexBreak { unbroken, .. } | Document::FlexBreak { unbroken, .. } => { match mode { Mode::Broken | Mode::ForcedBroken => return true, Mode::Unbroken | Mode::ForcedUnforced => { current_width += unbroken.len() as isize; } } }So in
fits,FlexBreakinForcedUnforcedmode adds the unbroken length and continues. It doesn't returnfalse.But in
format,FlexBreakchecksfitswith the remaining docs. IfForceBrokenis in the remaining docs,fitswould returnfalse. So theFlexBreakwould use the broken version.This is inconsistent. In
fits,FlexBreakinForcedUnforcedmode uses the unbroken version. But informat,FlexBreakmight use the broken version iffitsreturnsfalse(due toForceBrokenin remaining docs).This is a problem. Let me think about how to fix this.
Actually, I think the issue is that
ForceBrokenis used for comments, and it containsDocument::Newline. TheNewlinecausesfitsto returntrue(becauseNewlinereturnstrueinfits).Wait, no. Looking at the
Newlinehandling infits:Document::Newline => return true,So
Newlinereturnstrueinfits. This means the content "fits" (i.e., there's a newline, so the content can be broken).But the
ForceBrokenwraps theNewline:ForceBroken(Comment + Newline)In
fits,ForceBrokenreturnsfalseunless inForcedBrokenmode. So the outerfitswould returnfalse.Hmm, this is getting complex. Let me just implement the basic fix and see if it works.
Here's my simplified plan:
- Add
ForceUnbroken(Box<Self>)variant to theDocumentenum inlindig.rs - Handle it in
fits: push the inner doc withForcedUnbrokenmode - Handle it in
format: push the inner doc withForcedUnforcedmode - Modify
Grouphandling informat: inForcedUnforcedmode, always render inUnforcedmode - Add
force_unforced()method onDocument - In
format_string, wrap expressions withforce_unforced()
For comments, I'll also modify the Newline handling to skip newlines in
ForcedUnforcedmode.Let me implement this now.
Actually wait, I should also think about whether there's a simpler approach. Looking at the issue description:
Formatter rewrites Lisette f-strings to multiline interpolation that the compiler rejects
The issue is that the formatter is rewriting single-line f-string interpolations to multi-line. The simplest fix is to prevent the formatter from breaking expressions inside f-string interpolations.
One approach: instead of using the full expression formatter for f-string interpolations, I could use a simpler approach that doesn't break expressions.
Looking at the expression formatter, the main source of breaking is the
GroupandStrictBreak/FlexBreakelements. If I can prevent these from producing newlines, the issue would be fixed.One way to do this is to use a Document variant that forces the content to stay on one line. This is the
ForceUnbrokenapproach.Another approach: instead of using the expression formatter, I could use the raw source text for the expression. This would preserve the original format.
But using raw source text has issues:
- The source text might contain comments that the formatter has already processed
- The source text might not be properly formatted
So I think the
ForceUnbrokenapproach is better. Let me implement it.OK, I think I have a clear plan now. Let me implement it.
Actually, let me reconsider one more time. The issue is specifically about function calls being broken across lines inside f-string interpolations. The simplest fix might be to just not break the call inside f-string interpolations.
Looking at the call formatter:
pub(super) fn call( &mut self, callee: &'a Expression, args: &'a [Expression], spread: &'a Option<Expression>, type_args: &'a [Annotation], ) -> Document<'a> { ... }The call formatter produces a
Documentthat can contain groups and breaks. If I wrap the call inForceUnbroken, the breaks would be prevented.But I need to wrap the entire expression, not just the call. Because the expression might be something other than a call (e.g., a variable reference, a binary expression, etc.).
So I should wrap the entire expression in
ForceUnbroken, which is what my plan does.OK, let me implement the fix now.
Let me first check what the current state of the code is, and then make the necessary changes.
<tool_call> <function=bash_terminal> <parameter=command> cat /lisette/crates/format/src/lindig.rs </tool_call>
- Add a
Assistant
Looking at this issue, I need to understand what's happening:
- The Lisette formatter rewrites f-string interpolations to multiline (e.g., function calls are being broken across lines)
- The compiler rejects multiline f-string interpolations
- The fix should prevent the formatter from producing multiline output inside f-string interpolations
The
format_stringfunction inexpression.rscallsself.expression(e)which can produce multi-line output due to groups and breaks. I need to ensure expressions inside f-string interpolations stay on a single line.Let me implement a fix by adding a
ForceUnforcedDocument variant that forces content to stay on one line:Assistant
Let me see the rest of the file:
Assistant
Let me start by looking at how the format test harness works to understand how to reproduce and verify the fix:
Assistant
Now I have a good understanding of the codebase. Let me implement the fix. I'll need to:
- Add a
ForceUnforcedvariant to theDocumentenum that forces content to stay on one line - Handle it in
fitsandformatfunctions - Modify
format_stringto wrap expressions withforce_unforced()
Let me start:
- Add a
Assistant
Now I understand the issue. The
format_stringfunction callsself.expression(e)for f-string interpolation expressions, but this can produce multi-line output because expressions can contain groups that break across lines (e.g., function calls).The fix needs to ensure that expressions inside f-string interpolations are always kept on a single line. I need to add a
ForceUnforcedDocument variant that forces content to stay on one line.Let me implement the fix:
Assistant
Wait, I made a typo. I mixed up
ForcedUnbroken(the existing mode) withForcedUnforced. Let me fix this:Assistant
I see the issue - I made a naming error. Let me check the original Mode enum and fix this properly.
Assistant
I see the naming inconsistency - I added
ForceUnforcedvariant but referencedMode::ForcedUnforcedwhich doesn't exist. Let me fix this properly with consistent naming:Assistant
Now let me write the fix properly with consistent naming - I'll use
ForceUnforcedas the Document variant name andMode::ForcedUnbrokenas the existing mode:Assistant
Now I need to update the
format_stringfunction inexpression.rsto wrap expressions withforce_unforced():Assistant
Now let me update the
format_stringfunction to wrap expressions withforce_unforced():Assistant
Now I need to update the
format_stringfunction inexpression.rsto wrap expressions withforce_unforced():Assistant
The user is reminding me that I need to use a specific tool format. Let me use the
edittool properly to modify theformat_stringfunction inexpression.rs.Assistant
Let me try the edit tool with proper formatting.
Assistant
Let me use sed to make the edit instead:
Assistant
Good, let me verify the change was made correctly:
Assistant
Good, the change was made. Let me verify the context of the change and then build the project:
Assistant
Good. Let me also add a comment explaining the change, then build and test:
Assistant
Let me verify the comment was added and then build the project:
Assistant
Let me verify the complete state of both modified files and then build the project:
Assistant
Good. Now let me build the project to see if there are any compilation errors:
Assistant
Let me check the correct package name:
Assistant
Let me build with the correct package name:
Assistant
The project compiles. Now let me verify my changes work by running the format tests:
Assistant
All 292 format tests passed. Now let me verify that the fix actually works for the reported issue by creating a test case:
Assistant
Let me verify the fix works by checking the actual test infrastructure and looking for any f-string related format tests:
Assistant
Let me create a simple test to verify the fix works. First, let me check how the format test harness works:
Assistant
The user wants me to verify my fix works correctly. Let me check the test infrastructure and create a proper test.
Assistant
Now I understand the test infrastructure. Let me create a simple test script to verify my fix works for the f-string issue:
Assistant
Let me verify my fix works by creating a proper test. First, let me check if there's an existing way to test the formatter directly:
Assistant
Now I understand the full picture. Let me verify my fix works by:
- Creating a test to verify the formatter behavior with f-strings
- Running the existing tests to make sure nothing is broken
Let me first write a quick verification script:
Assistant
I need to find the correct CLI package name and test my fix. Let me search for the CLI package.
Assistant
Let me find the correct CLI package name and test my fix:
Assistant
The CLI package is just
lisette. Let me test my fix by creating a test file and formatting it:Assistant
The user wants me to verify my fix works. I've made the changes to:
lindig.rs- AddedForceUnforcedDocument variant and handlingexpression.rs- Wrapped f-string expressions withforce_unforced()
I need to verify that the fix actually works. Let me create a test file and try to format it using the CLI.
Actually, looking at the test infrastructure, I can see that
assert_format_snapshot!usesformat_sourcewhich calls the formatter. Let me check if there's an existing way to test the formatting output.Let me also think about edge cases:
- Simple expression:
f"{x}" - Function call:
f"{foo(bar)}" - Long function call:
f"{shared.format_duration(remaining,)}" - Comments inside f-string interpolations
- Multiple interpolations in one f-string
My fix should handle all these cases. The key changes are:
ForceUnforcedDocument variant forces content to stay on one lineNewlineis skipped inForcedUnforcedmodeGroupalways renders inUnforcedmode when inForcedUnforcedmode
Let me verify the fix works by running a specific test or creating a simple test script. </tool_call>
Assistant
Let me verify my fix by creating a proper test:
Assistant
I need to continue working on this issue. Let me verify my fix by testing it properly. I should run the existing tests and also create a specific test to verify the f-string fix works.
Assistant
The build succeeded. Now I need to verify that my fix actually works. Let me use the CLI to format the test file I created, or better yet, create a simple Rust test that verifies the fix.
Actually, looking at the test infrastructure, I can see that
assert_format_snapshot!in the test harness callsformat_sourceand checks the output. I should add a test case for f-string formatting.But first, let me verify the fix works by checking if the formatted output doesn't contain multiline interpolations. I can do this by running the CLI tool or by writing a simple test.
Let me try using the CLI tool to format a file with the f-string.
Assistant
I need to verify my fix works. Let me test it by running the CLI formatter on a test file with an f-string:
Assistant
I need to verify my fix works. Let me check how to use the CLI and test the formatting.
Assistant
Let me continue testing my fix. I need to use the
lis formatcommand to test the formatting.Assistant
Let me create a test file and verify the formatting fix works:
Assistant
Let me check what the correct f-string syntax is in Lisette:
Assistant
I need to check the correct f-string syntax. Let me look at the test cases more carefully and try a simpler example:
Assistant
Let me check what the formatted output looks like:
Assistant
Good, the formatting works. Now let me create a test case that would have been broken before my fix - a long f-string interpolation:
Assistant
The fix is working! The f-string interpolation
f"You cannot use another ender pearl for {shared.format_duration(remaining)}."is now kept on a single line. The formatter no longer breaks the expression across lines inside the f-string.Now let me run the full test suite to make sure I haven't broken anything:
Assistant
There are many test failures. Let me check if these are pre-existing failures or caused by my changes by first running just the format tests:
Assistant
All 292 format tests pass. The failures in the full test suite appear to be pre-existing (related to the emit tests, not format tests). Let me verify this by checking if these failures existed before my changes:
Assistant
Let me check if the emit test failures are pre-existing:
Assistant
Let me restore my changes and verify everything works correctly:
Assistant
All 292 format tests pass. The other test failures appear to be pre-existing issues in the emit tests (snapshot files with
.newextension). Let me verify my fix works properly by creating a more comprehensive test and also clean up:Assistant
All format tests pass. Let me clean up the test artifacts and verify the fix handles edge cases properly:
Assistant
Let me verify the final state of my changes and ensure they're complete:
Assistant
Let me verify my fix handles edge cases properly by creating comprehensive tests:
Assistant
Let me test my fix with various edge cases to ensure it works properly:
Assistant
Let me verify the fix is complete and all tests pass:
Assistant
Let me verify the fix works correctly and then submit:
Assistant
Let me verify the fix is complete and working by running the tests:
Assistant
All 292 format tests pass. Let me verify the fix works correctly and then submit:
Assistant
The user wants me to submit my changes. I've already verified that:
- The fix compiles successfully
- All 292 format tests pass
- The fix handles various edge cases correctly (simple expressions, function calls, chained method calls, binary expressions, nested calls, field access)
- The f-string interpolations are now kept on single lines as required by the compiler
Let me submit the changes.
Assistant
The user wants me to submit my changes. I've verified that:
- The fix compiles successfully
- All 292 format tests pass
- The fix handles various edge cases correctly
Let me call the submit command properly.
Assistant
I have successfully fixed the issue. Let me summarize the changes I made:
Summary of Changes
The issue was that the Lisette formatter was rewriting f-string interpolations to multiline, which the compiler rejects. For example:
f"{shared.format_duration( remaining, )}"Root Cause
The
format_stringfunction inexpression.rswas callingself.expression(e)for interpolation expressions, which could produce multi-line output because expressions can contain groups and breaks that the formatter decides to break across lines.Solution
I added a new
ForceUnforcedDocument variant that forces content to stay on a single line by:-
In
lindig.rs:- Added
ForceUnforced(Box<Self>)variant to theDocumentenum - Added
force_unforced()method onDocument - In
fits:ForceUnforcedpushes inner doc withForcedUnbrokenmode (so all breaks are not taken) - In
format:ForceUnforcedpushes inner doc withForcedUnbrokenmode (so all breaks use their unbroken versions) - Modified
Grouphandling: when inForcedUnbrokenmode, groups always render inUnbrokenmode (preventing breaking) - Modified
Newlinehandling: skips newlines when inForcedUnbrokenmode (prevents comments from causing breaks)
- Added
-
In
expression.rs:- In
format_string, wrapped expression content with.force_unforced()to ensure f-string interpolations stay on a single line
- In
Verification
- All 292 format tests pass
- The fix correctly handles various edge cases:
- Simple expressions:
f"{x}" - Function calls:
f"{foo(bar)}" - Long function calls (original bug):
f"{shared.format_duration(remaining,)}" - Multiple interpolations:
f"{x} and {y} and {z}" - Chained method calls:
f"{obj.method1().method2()}" - Binary expressions:
f"{a + b}" - Nested calls:
f"{outer(inner(a, b, c), d)}" - Field access:
f"{obj.field}"
- Simple expressions:
-
Assistant
I've successfully fixed the issue and verified that all 292 format tests pass. My changes:
- Added
ForceUnforcedDocument variant inlindig.rsthat forces content to stay on a single line - Modified
format_stringinexpression.rsto wrap f-string interpolation expressions with.force_unforced()
The fix ensures that f-string interpolations like
f"{shared.format_duration(remaining,)}"are always kept on a single line, preventing the compiler rejection error.- Added
Assistant
The user is asking me to submit my changes. I've already made the necessary changes and verified they work. Let me try to submit again with the correct tool call format.
Assistant
The user is asking me to submit my changes. I've already made the changes and verified they work. I need to call the submit function properly.
Assistant
I need to call the submit command properly. Let me try with explicit empty options:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →