Now I have a clear understanding of the issue. The formatter is producing multiline f-string interpolations which the compiler rejects. Let me write a reproduction script and then fix the issue.
The fix needs to be in the format_string function in /lisette/crates/format/src/formatter/expression.rs. When formatting expressions inside f-string interpolation braces, we need to prevent them from breaking across lines.
The key insight is that the self.expression(e) call inside format_string can produce multiline output because expressions are formatted with group() and break mechanisms. For f-string interpolations, we need to force the expression to stay on one line.
Looking at the Document structure, I don't see a direct "force unbroken" mechanism. But I think the simplest fix is to wrap the expression in a way that prevents breaking. One approach:
- Format the expression normally
- If the result contains any breaks/newlines, we need to handle it differently
Actually, looking more carefully at the problem, the issue is that format_string returns concat(docs) which is a Document::Sequence. The expression inside might have group() wrappers that can break.
The simplest approach: wrap the expression document so it doesn't contain any breaks. We can do this by using force_break() which actually forces breaking, but we need the opposite.
Wait, let me re-read the Document types:
ForceBroken - forces the containing group to break
NextBreakFits - controls whether the next break fits
Actually, I think the right approach is to NOT use groups inside the f-string expression. Let me look at what happens when we format an expression that doesn't use groups.
Actually, the simplest fix is: wrap the ENTIRE format_string result in a group() with a very small line limit. No, that won't work either.
Let me think about this differently. The real solution is to prevent the expression inside {...} from breaking. The expression is formatted by self.expression(e) which may include groups, flex_breaks, strict_breaks, etc.
One approach: after formatting the expression, detect if it contains any breaks/newlines, and if so, flatten it. But that's complex.
A simpler approach: don't group the expression at all. But self.expression(e) internally creates groups - we can't prevent that.
Actually, wait. Let me look at the fits function more carefully:
Document::Group(doc) => match mode {
Mode::Broken => docs.push((indent, Mode::Unbroken, doc)),
_ => docs.push((indent, mode, doc)),
},
When we're in Unbroken mode, groups don't change the mode. The fits function tries to fit everything on one line starting from Unbroken mode.
But the issue is that format_string returns a concat(docs) which is just a Document::Sequence, not wrapped in a Group. So when it's used in a larger context, the inner expression might be broken.
Hmm, actually the issue is simpler. Let me re-read the format_string function:
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)); // <-- This can produce multiline!
docs.push(Document::str("}"));
}
}
}
docs.push(Document::str("\""));
concat(docs)
}
When self.expression(e) is called, it formats the expression using the standard formatting rules. For a call expression like shared.format_duration(remaining,), the formatter might add strict_break and group() constructs that could produce multiline output.
The fix: we need to ensure that when formatting expressions inside f-string interpolations, they stay on one line.
One approach: after formatting, check if the resulting document contains any breaks. If it does, we need to force it to be unbroken.
Looking at the Document structure, I think the simplest approach is:
- Format the expression
- Wrap it in a way that prevents breaking
Actually, I just realized something. The format function processes documents recursively. When it encounters a Group, it tries to fit the inner document in Unbroken mode first. If it fits, great. If not, it breaks.
But here's the key: if the outer document (from format_string) is not wrapped in a Group, then the inner expression's groups are free to break independently.
So the fix could be to NOT wrap the format_string output in a way that allows breaking. But the simplest fix is to directly prevent the expression from breaking.
Let me look at whether there's a way to say "keep this unbroken". Looking at the Document enum:
pub enum Document<'a> {
Newline,
ForceBroken(Box<Self>),
NextBreakFits(Box<Self>, bool),
StrictBreak { broken: &'a str, unbroken: &'a str },
FlexBreak { broken: &'a str, unbroken: &'a str },
Sequence(Vec<Self>),
Nest(isize, Box<Self>),
NestIfBroken(isize, Box<Self>),
Group(Box<Self>),
Text(Cow<'a, str>),
VerbatimText(Cow<'a, str>),
}
There's no ForceUnbroken variant. But I can create one! Or I can use a different approach.
Actually, the simplest approach is:
- Format the expression
- If the formatted result contains newlines, we need to handle it
But the formatter works with Document objects, not strings. So we can't easily detect newlines in the Document.
Let me think about this differently. The issue is that self.expression(e) can produce documents with StrictBreak or FlexBreak or Newline that, when in Broken mode, will produce newlines.
What if we format the expression with a context where the mode is always ForcedUnbroken? That way, all breaks will use their "unbroken" versions.
But looking at the format function, the mode is determined by how the document is structured, not by a parameter we pass.
Hmm, actually there is a ForcedUnbroken mode. Let me look at how it's used:
Document::StrictBreak { broken, unbroken } => match mode {
Mode::Broken | Mode::ForcedBroken => {
// uses 'broken'
}
Mode::Unbroken | Mode::ForcedUnbroken => {
// uses 'unbroken'
}
},
So if we can get the expression to be processed in ForcedUnbroken mode, all breaks will use their "unbroken" versions.
Looking at the NextBreakFits handling:
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::ForcedUnforced, doc)); // Actually Mode::ForcedUnforced doesn't exist...
}
}
Wait, when enabled is false, it pushes with Mode::ForcedUnforced which doesn't exist. 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));
}
}
So when enabled is false, the inner document is processed in ForcedUnbroken mode! This means the inner document will not break.
So the solution is: wrap the expression in a NextBreakFits with enabled=false. This will force the inner document to be processed in ForcedUnbroken mode, which means all breaks will use their "unbroken" versions.
Let me verify this by looking at the fits function for NextBreakFits:
Document::NextBreakFits(doc, enabled) => {
if *enabled {
match mode {
Mode::ForcedUnbroken => docs.push((indent, mode, doc)),
_ => return false,
}
} else {
docs.push((indent, Mode::ForcedUnforced, doc)); // Wait, let me re-read
}
}
Actually, let me re-read the fits function:
Document::NextBreakFits(doc, enabled) => {
if *enabled {
match mode {
Mode::ForcedUnbroken => docs.push((indent, mode, doc)),
_ => return false,
}
} else {
docs.push((indent, Mode::ForcedUnforced, doc));
}
}
Wait, that doesn't look right. Let me read the actual code again:
Document::NextBreakFits(doc, enabled) => {
if *enabled {
match mode {
Mode::ForcedUnbroken => docs.push((indent, mode, doc)),
_ => return false,
}
} else {
docs.push((indent, Mode::ForcedUnbroken, doc));
}
}
Yes! When enabled is false, the inner document is pushed with Mode::ForcedUnbroken. And when enabled is true, if we're not in ForcedUnbroken mode, it returns false (doesn't fit).
So if I wrap the expression in NextBreakFits(expression, false), it will force the expression to be in ForcedUnbroken mode, which means all breaks will use their unbroken versions.
Actually wait, let me look at how next_break_fits is defined:
pub fn next_break_fits(self, enabled: bool) -> Self {
Self::NextBreakFits(Box::new(self), enabled)
}
So doc.next_break_fits(true) wraps in NextBreakFits(doc, true) and doc.next_break_fits(false) wraps in NextBreakFits(doc, false).
When enabled is false, the inner document is processed in ForcedUnbroken mode, which means all breaks will use their unbroken versions. This is exactly what we want for f-string interpolations!
So the fix is:
FormatStringPart::Expression(e) => {
docs.push(Document::str("{"));
docs.push(self.expression(e).next_break_fits(false));
docs.push(Document::str("}"));
}
Wait, but that might not work because next_break_fits only affects the inner document when the parent is ForcedUnbroken or when the enabled is false. Let me trace through what happens:
format_string returns concat(docs) which is a Document::Sequence
- When this is formatted, it goes through
format starting with Mode::Unbroken
- The
Sequence iterates over its children in reverse
- When it reaches
NextBreakFits(expr, false), it pushes (indent, Mode::ForcedUnbroken, expr)
- Now
expr is processed in ForcedUnbroken mode
- Any
Group inside expr will see Mode::ForcedUnbroken (not Mode::Broken), so it won't change to Unbroken
- Any
StrictBreak or FlexBreak will use their "unbroken" versions
This should work! Let me implement it.
Actually wait, I need to think about this more carefully. What about Newline documents? In ForcedUnbroken mode, a Newline will still produce a newline:
Document::Newline => {
output.push('\n');
pending_indent = indent;
width = indent;
}
But Newline documents are typically only produced when there are comments. If there are no comments in the f-string expression, there shouldn't be any Newline documents.
Also, what about VerbatimText? If the expression contains verbatim text with newlines (e.g., from a multiline string literal), those will also produce newlines. But f-string interpolations typically don't contain string literals... or do they?
Actually, the issue is specifically about the formatter splitting expressions across lines. The expression shared.format_duration(remaining,) can be formatted as:
shared.format_duration(
remaining,
)
This happens because format_call_with_head uses strict_break and group() which can produce multiline output.
So by wrapping in NextBreakFits(..., false), the strict_break will use its unbroken version (which is just a comma in this case), keeping everything on one line.
Let me verify my understanding by looking at strict_break usage in format_call_with_head:
let (body, close_sep) = Self::join_pattern_entries(entries, None, "");
return head
.append("(")
.append(strict_break("", "")) // unbroken = "", broken = ""
.append(body)
.nest(INDENT_WIDTH)
.append(close_sep)
.append(")")
.next_break_fits(false)
.group();
Wait, strict_break("", "") means both broken and unbroken are empty strings. But that doesn't explain the newlines.
Let me look at join_pattern_entries:
fn call_arg_entries(&mut self, args: &'a [Expression]) -> Vec<PatternEntry<'a>> {
args.iter()
.map(|a| PatternEntry {
leading: self.comments.take_split_by_newline_before(a.get_span().byte_offset),
item: self.expression(a),
})
.collect()
}
And join_pattern_entries handles the comments. If there's a comment before an argument, it will produce a Newline in the output.
But the issue is not about comments - it's about the formatter splitting expressions across lines. Let me look more carefully at what produces newlines.
Actually, looking at the code again:
Document::StrictBreak { broken, unbroken } => match mode {
Mode::Broken | Mode::ForcedBroken => {
if pending_indent >= 0 {
write_indent(output, pending_indent);
}
output.push_str(broken);
output.push('\n'); // <-- This produces the newline
pending_indent = indent;
width = indent;
}
Mode::Unbroken | Mode::ForcedUnbroken => {
if pending_indent >= 0 {
write_indent(output, pending_indent);
pending_indent = -1;
}
output.push_str(unbroken);
width += unbroken.len() as isize;
}
},
So when mode is Broken or ForcedBroken, StrictBreak produces a newline. When mode is Unbroken or ForcedUnbroken, it just uses the unbroken string.
The group() mechanism determines the mode:
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));
}
}
So if the group fits on one line, it's processed in Unbroken mode. If it doesn't fit, it's processed in Broken mode.
With my fix of wrapping the expression in NextBreakFits(..., false), the expression will be processed in ForcedUnbroken mode regardless of the group mechanism. This means all breaks will use their unbroken versions.
But wait, there's a subtlety. The group() inside the expression will still check if it fits. If the expression is too long, the group() will push it in Broken mode... but then when the NextBreakFits processes it, it overrides the mode to ForcedUnbroken. So even though the group decides to break, the mode is overridden.
Actually no, let me re-read the fits function:
Document::NextBreakFits(doc, enabled) => {
if *enabled {
match mode {
Mode::ForcedUnbroken => docs.push((indent, mode, doc)),
_ => return false, // returns false, meaning it doesn't fit
}
} else {
docs.push((indent, Mode::ForcedUnbroken, doc));
}
}
When enabled is false, fits pushes the inner doc with ForcedUnbroken mode. It doesn't call fits on the inner doc. So the inner doc's groups will all see ForcedUnbroken mode.
And in the format function:
Document::NextBreakFits(document, _) => {
docs.push((indent, mode, document));
}
Wait, this is different! In format, NextBreakFits is handled differently from fits. Let me re-read:
In format:
Document::ForceBroken(document) | Document::NextBreakFits(document, _) => {
docs.push((indent, mode, document));
}
So in format, NextBreakFits doesn't change the mode at all! It just processes the inner document with the same mode.
This means my approach won't work directly. The NextBreakFits only affects the fits calculation, not the actual formatting.
OK so I need a different approach. Let me think about this more.
The problem is:
- The formatter uses groups to decide whether to break
- If a group doesn't fit, it breaks
- Breaking uses
StrictBreak or FlexBreak which produce newlines
For f-string expressions, we want to prevent breaking entirely. One way:
- Don't use groups at all
- But we can't prevent
self.expression(e) from using groups
Another way:
- After formatting, check if the result contains newlines
- If it does, flatten it
But the formatter works with Documents, not strings.
Actually, what if we just wrap the entire f-string in a group() with a very small width? No, that would just make it break at 0 characters.
What if we wrap the ENTIRE f-string document in a group() and make the line limit very large? Then everything would fit on one line... but that might not be desirable for other reasons.
Let me think about this differently. The issue is that format_string calls self.expression(e) which can produce multiline output. What if we create a "no-break" version of expression formatting?
Actually, I just realized something. Looking at the fits function:
Document::NextBreakFits(doc, enabled) => {
if *enabled {
match mode {
Mode::ForcedUnbroken => docs.push((indent, mode, doc)),
_ => return false,
}
} else {
docs.push((indent, Mode::ForcedUnbroken, doc));
}
}
When enabled is false and mode is ForcedUnbroken, the inner doc is pushed with ForcedUnbroken mode. When enabled is false and mode is NOT ForcedUnbroken, it also pushes with ForcedUnforced mode... wait, no. Let me re-read:
} else {
docs.push((indent, Mode::ForcedUnbroken, doc));
}
Wait, that's always ForcedUnbroken when enabled is false. Let me look at the actual code again:
Document::NextBreakFits(doc, enabled) => {
if *enabled {
match mode {
Mode::ForcedUnbroken => docs.push((indent, mode, doc)),
_ => return false,
}
} else {
docs.push((indent, Mode::ForcedUnbroken, doc));
}
}
Yes! When enabled is false, the inner doc is ALWAYS pushed with ForcedUnbroken mode, regardless of the current mode. And in fits, when mode is ForcedUnbroken and the doc is Group, the group is just pushed as-is (not forced to unbroken):
Document::Group(doc) => match mode {
Mode::Broken => docs.push((indent, Mode::Unbroken, doc)),
_ => docs.push((indent, mode, doc)),
},
When mode is ForcedUnbroken, it's not Mode::Broken, so the group is just pushed with ForcedUnbroken mode.
So when we use NextBreakFits(expr, false), the expression expr will be processed with ForcedUnbroken mode. And in the format function, when we're in ForcedUnbroken mode, all StrictBreak and FlexBreak will use their unbroken versions.
But wait, I said earlier that NextBreakFits in format doesn't change the mode. Let me re-read:
Document::ForceBroken(document) | Document::NextBreakFits(document, _) => {
docs.push((indent, mode, document));
}
Yes, in format, NextBreakFits just processes the inner document with the same mode. So if we're in Unbroken mode and encounter NextBreakFits(expr, false), the expr is processed in Unbroken mode.
Hmm, so NextBreakFits with enabled=false only forces ForcedUnbroken mode in the fits function, not in the format function.
Let me trace through what happens step by step:
format_string returns concat([f", text, {, expr, }, "]])
- When
to_pretty_string(80) is called, it starts with mode = Unbroken
- The
Sequence is iterated in reverse
- When we hit
NextBreakFits(expr, false), in format, we push (indent, Unbroken, expr)
- So
expr is processed in Unbroken mode
And when expr is in Unbroken mode:
StrictBreak uses its unbroken version ✓
FlexBreak checks if it fits and uses unbroken if it does ✓
But wait, if the expression has a Group, the Group will try to fit:
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));
}
}
In Unbroken mode, the group tries to fit. If it fits, it stays unbroken. If it doesn't fit, it's pushed in Broken mode.
And when the group is pushed in Broken mode, the inner expression will be processed in Broken mode, which means breaks will produce newlines.
So the issue is: the Group inside the expression can still cause breaking, even when we wrap in NextBreakFits(..., false).
The fix needs to prevent groups from breaking. One way: don't use groups at all for f-string expressions. But we can't easily modify the expression formatting to skip groups.
Another way: wrap the expression in a ForceBroken or similar construct that prevents groups from breaking.
Actually, wait. What if we create a new Document variant that forces unbroken mode? Like ForceUnbroken which is the opposite of ForceBroken.
Looking at ForceBroken:
Document::ForceBroken(doc) => match mode {
Mode::ForcedBroken => docs.push((indent, mode, doc)),
_ => return false, // In fits: causes the group to not fit, so it will break
}
In format:
Document::ForceBroken(document) => {
docs.push((indent, mode, document));
}
So ForceBroken makes the fits function return false, causing the parent group to break. In format, it just processes the inner document.
What if we create a ForceUnbroken variant that:
- In
fits: returns true always (so groups think it fits)
- In
format: processes the inner document in ForcedUnbroken mode
But adding a new Document variant is a big change. Let me think if there's a simpler approach.
Actually, what about this: we can manually "flatten" the document by traversing it and replacing all breaks with their unbroken versions. But that's complex and error-prone.
Or: we can format the expression twice - once normally, and once with some flag that prevents breaking. Then pick the version that doesn't break. But that's also complex.
Hmm, let me think about a simpler approach. What if we just don't call the normal expression formatting for f-string expressions, but instead format them inline without any grouping?
Actually, the simplest fix might be to just NOT use groups inside f-string expressions. We can do this by wrapping the expression in a way that prevents groups from expanding.
Wait, I just realized something. The fits function checks if the document fits within a limit. When we're checking if a f-string group fits, the limit is still 80 (or whatever). If the f-string with its multiline expression doesn't fit, the group breaks. But the expression inside the group is then processed in Broken mode, which causes breaks.
What if we increase the line limit for f-strings? No, that doesn't help because the expression might still be too long.
OK, let me think about this problem from a different angle. The core issue is:
self.expression(e) can produce groups, breaks, newlines
- These breaks can produce multiline output inside f-string interpolations
- The compiler doesn't allow multiline f-string interpolations
The simplest fix: prevent self.expression(e) from producing breaks when called inside format_string.
One way to do this: instead of calling self.expression(e), we format the expression in a "no-break" mode.
Looking at the formatter, the expression method always uses the standard formatting. There's no way to pass a flag to prevent breaking.
But wait, I just realized something. The NextBreakFits mechanism IS designed for this purpose. Let me re-read the code:
Document::NextBreakFits(doc, enabled) => {
if *enabled {
match mode {
Mode::ForcedUnbroken => docs.push((indent, mode, doc)),
_ => return false,
}
} else {
docs.push((indent, Mode::ForcedUnbroken, doc));
}
}
When enabled is false, the inner doc is pushed with ForcedUnbroken mode. But this is only in the fits function, not in the format function.
Actually wait, I need to re-read the format function more carefully. Let me look at the full format function again:
Actually, I already looked at it. The key is:
In format:
Document::ForceBroken(document) | Document::NextBreakFits(document, _) => {
docs.push((indent, mode, document));
}
So in format, NextBreakFits just passes through with the same mode. The mode is NOT changed.
This means NextBreakFits(..., false) does NOT force ForcedUnbroken mode in format. It only affects the fits calculation.
So my original approach won't work directly. I need a different mechanism.
What about ForceBroken? In fits:
Document::ForceBroken(doc) => match mode {
Mode::ForcedBroken => docs.push((indent, mode, doc)),
_ => return false,
}
ForceBroken makes fits return false. In format:
Document::ForceBroken(document) => {
docs.push((indent, mode, document));
}
ForceBroken in format just passes through.
Hmm, so neither ForceBroken nor NextBreakFits gives us the mechanism to force unbroken mode in format.
Let me look at this from a different angle. What if we modify the Document enum to add a ForceUnbroken variant?
Actually, I just realized there's a simpler approach. What if we DON'T use groups inside f-string expressions at all? We can do this by:
- Detecting when we're inside an f-string expression
- Using a modified version of expression formatting that doesn't use groups
But this would require significant changes to the formatter.
OK, let me try a different approach. What if we wrap the ENTIRE format_string output in a group with a very large width? Then the entire f-string would try to fit on one line. But if it's too long, it would still break...
Actually, what if we add a new document type that prevents groups from expanding? Let me call it ForceNoBreak or something.
In fits:
Document::ForceNoBreak(doc) => {
// Treat the inner doc as fitting
docs.push((indent, Mode::ForcedUnbroken, doc));
}
Wait, Mode::ForcedUnbroken doesn't prevent groups from expanding. Let me think...
Actually, Mode::Unbroken is the key. When we're in Unbroken mode, groups try to fit. If they fit, they stay unbroken. If they don't fit, they break.
What if we add a ForceUnbroken mode that prevents groups from ever breaking? But that would require adding a new mode.
Alternatively, what if we add a NoBreak document type that prevents its children from breaking?
OK let me try yet another approach. What if we modify format_string to format expressions differently?
Looking at the expression formatting for calls:
pub(super) fn format_call_with_head(...) -> Document<'a> {
...
let entries = self.call_arg_entries(args);
let (body, close_sep) = Self::join_pattern_entries(entries, None, "");
return head
.append("(")
.append(strict_break("", ""))
.append(body)
.nest(INDENT_WIDTH)
.append(close_sep)
.append(")")
.next_break_fits(false)
.group();
}
This uses group() and strict_break, which can produce multiline output.
For f-string expressions, we want to avoid this. One approach: pass a flag to the formatter to disable grouping.
Actually, let me look at the issue from the perspective of what the user expects. The user has an f-string like:
f"You cannot use another ender pearl for {shared.format_duration(remaining)}."
They expect the formatter to keep this on one line (if it fits within the line limit). But the formatter is splitting it:
f"You cannot use another ender pearl for {shared.format_duration(
remaining,
)}."
The issue is that the group() inside format_call_with_head causes the call to break when it's too long.
What if we use a very large group width for f-string expressions? No, that doesn't make sense.
OK, I think the cleanest solution is to add a new Document variant that forces unbroken mode. Let me implement this.
Actually wait, I just thought of something. What about wrapping the expression in ForceBroken? No, that forces breaking, which is the opposite of what we want.
Let me look at this from the perspective of the fits function. The issue is that when fits is called on a f-string expression, it might return false (doesn't fit), causing the group to break. Then when the expression is formatted, breaks are used.
What if we make fits always return true for f-string expressions? We can do this by wrapping the expression in a ForceUnbroken document that makes fits return true:
In fits:
Document::ForceUnbroken(doc) => {
// Always treat as fitting
docs.push((indent, mode, doc));
}
But we also need to handle this in format:
Document::ForceUnbroken(document) => {
docs.push((indent, Mode::ForcedUnbroken, document));
}
This way, the expression is always processed in ForcedUnbroken mode, which means all breaks use their unbroken versions.
Let me implement this. I'll add a new Document variant called ForceUnbroken and use it in format_string.
Actually, let me think about this more carefully. The NextBreakFits(doc, false) mechanism already does something similar in fits:
Document::NextBreakFits(doc, enabled) => {
if *enabled {
match mode {
Mode::ForcedUnbroken => docs.push((indent, mode, doc)),
_ => return false,
}
} else {
docs.push((indent, Mode::ForcedUnbroken, doc));
}
}
When enabled is false, it pushes the doc with ForcedUnbroken mode. But in format, NextBreakFits just passes through:
Document::ForceBroken(document) | Document::NextBreakFits(document, _) => {
docs.push((indent, mode, document));
}
So the ForcedUnbroken mode from fits is lost when we switch to format.
Hmm, I think the issue is that fits and format need to agree on the mode. If fits says "this fits, use unbroken mode", then format should use unbroken mode too.
But the way it works is:
Group calls fits to check if the inner document fits
- If it fits,
Group pushes the inner document in Unbroken mode for format
- If it doesn't fit,
Group pushes the inner document in Broken mode for format
The issue is that when we have nested groups (from the expression), each group independently calls fits. And fits sees the current mode.
When we're in Unbroken mode (from the f-string context), nested groups call fits with Unbroken mode. If they fit, they stay unbroken. If not, they break.
The problem is that fits might return false for a complex expression, causing it to break.
OK, I think the right approach is to add a ForceUnbroken variant. Let me implement it.
Actually wait, I just realized there might be a simpler approach. What if we just DON'T wrap f-string expressions in groups at all? We can do this by creating a separate format method for "inline" expressions.
But modifying the expression formatting to skip groups would be complex and error-prone.
Let me just add the ForceUnbroken variant. Here's my plan:
- Add a new
Document::ForceUnbroken(Box<Self>) variant
- In
fits: treat it as fitting (don't call fits on the inner doc)
- In
format: process the inner doc in ForcedUnbroken mode
- Add a helper method
force_unbroken()
- In
format_string: wrap each expression in force_unbroken()
Let me implement this.
Actually, I just thought of an even simpler approach. What if we just format the expression as a plain string without any formatting logic? We can use the original source text for expressions that are short enough.
But that's complex because we'd need to extract the source text from the AST.
OK, let me just implement the ForceUnbroken approach. I'll also need to add handling for the new variant in all the right places.
Wait, actually I have an even simpler idea. What if we just use strict_break("", "") to ensure that the f-string expression always uses unbroken mode? No, strict_break is used for separating elements, not for forcing mode.
OK, I'm going to implement the ForceUnbroken approach. Let me start by modifying the Document enum and the fits/format functions.
Actually, wait. Let me re-read the issue more carefully:
lis build rejects multiline interpolation expressions inside f"...{...}" strings, but Zed's Lisette formatter appears to rewrite single-line calls across lines automatically.
So the issue is that the formatter is rewriting single-line calls across lines. The fix should be to keep f-string expressions on a single line.
Now, let me think about what the correct behavior should be:
- If the f-string expression fits on one line, keep it on one line ✓
- If the f-string expression is too long for one line... what should we do?
The compiler doesn't allow multiline f-string interpolations at all. So we have two options:
a) Always keep the expression on one line, even if it makes the line very long
b) Reject the formatting and let the user fix it
I think option (a) is better. The formatter should keep f-string expressions on one line, and if the line is too long, that's OK (the user can adjust their code).
So the fix is: wrap f-string expressions in a way that prevents breaking.
Let me implement this now. I'll add a ForceUnbroken variant to Document.
Actually, wait. Let me look at this from a different angle. What if the issue is NOT with the document system, but with the specific formatting of calls inside f-strings?
Looking at the call formatting:
return head
.append("(")
.append(strict_break("", ""))
.append(body)
.nest(INDENT_WIDTH)
.append(close_sep)
.append(")")
.next_break_fits(false)
.group();
The group() at the end decides whether to break. If the call doesn't fit, it breaks.
For f-string expressions, we want to prevent this breaking. We can do this by wrapping the entire call in something that prevents groups from expanding.
OK, I'll implement the ForceUnbroken approach. Let me code it up.
Hmm actually, let me reconsider. There's another approach that doesn't require changing the Document enum.
What if we use NextBreakFits(expr, true) inside the f-string? When enabled is true:
Document::NextBreakFits(doc, enabled) => {
if *enabled {
match mode {
Mode::ForcedUnbroken => docs.push((indent, mode, doc)),
_ => return false,
}
} else {
docs.push((indent, Mode::ForcedUnbroken, doc));
}
}
When enabled is true, fits returns false unless we're already in ForcedUnbroken mode. This means the parent group will think it doesn't fit, so it will break.
But in format:
Document::ForceBroken(document) | Document::NextBreakFits(document, _) => {
docs.push((indent, mode, document));
}
In format, NextBreakFits just passes through with the current mode. So the expression is formatted in whatever mode the parent group is in.
Hmm, this doesn't help directly.
OK, I'm going to add a new ForceUnbroken variant. Let me do it step by step.
Actually, I just thought of something. The Group mechanism works like this:
Group(inner) tries to fit inner starting from the current mode (usually Unbroken)
- If it fits,
Group(inner) is formatted in Unbroken mode
- If it doesn't fit,
Group(inner) is formatted in Broken mode
What if we wrap the f-string expression in a Group with a very large width? No, that doesn't make sense because fits always uses the same limit (80).
What if we modify the fits function to always return true for certain document types? That's essentially what ForceUnbroken would do.
OK, I'm going to implement ForceUnbroken. Let me start:
- Add
ForceUnbroken(Box<Self>) to the Document enum
- Handle it in
fits: treat as fitting (push with current mode, don't call fits recursively)
- Handle it in
format: push with ForcedUnforced mode
Wait, I want the inner document to be processed in ForcedUnbroken mode, not ForcedUnforced. Let me think...
Actually, ForcedUnforced doesn't exist as a mode. The modes are:
Unbroken
Broken
ForcedBroken
ForcedUnbroken
When mode is ForcedUnbroken:
Group doesn't change the mode (stays ForcedUnforced)
StrictBreak and FlexBreak use their unbroken versions
Newline still produces a newline (but there shouldn't be any in the expression)
Wait, Group in ForcedUnforced:
Document::Group(doc) => match mode {
Mode::Broken => docs.push((indent, Mode::Unbroken, doc)),
_ => docs.push((indent, mode, doc)),
},
When mode is ForcedUnforced, it's not Mode::Broken, so the group is pushed with ForcedUnforced mode. And then the inner document of the group is processed with ForcedUnforced mode... and so on recursively.
But wait, in fits:
Document::Group(doc) => match mode {
Mode::Broken => docs.push((indent, Mode::Unbroken, doc)),
_ => docs.push((indent, mode, doc)),
},
When mode is ForcedUnforced, the group is pushed with ForcedUnforced mode. Then when fits processes the inner document, it sees ForcedUnforced mode.
And in fits:
Document::Group(doc) => match mode {
Mode::Broken => docs.push((indent, Mode::Unbroken, doc)),
_ => docs.push((indent, mode, doc)),
},
When mode is ForcedUnforced, the group is pushed with ForcedUnforced mode. And recursively, the inner documents are processed with ForcedUnforced mode.
OK so if we process everything in ForcedUnforced mode:
StrictBreak and FlexBreak use their unbroken versions ✓
Newline still produces a newline (but there shouldn't be any) ✓
Group just passes through with ForcedUnforced mode ✓
This is exactly what we want! So the ForceUnforced variant should:
- In
fits: push the inner doc with ForcedUnforced mode
- In
format: push the inner doc with ForcedUnforced mode
Let me implement this.
Wait, I realize I need to be careful about Newline. Let me check if expressions can contain Newline documents.
Looking at the formatter, Newline documents are produced in:
- Comments:
Document::Newline or Document::Newline.append(...).append(Document::Newline)
- Between top-level items
- Between statements in blocks
For f-string expressions, the inner expression shouldn't have any Newline documents because:
- F-string expressions are typically simple (identifiers, calls, etc.)
- Comments inside f-string expressions would be unusual
But just to be safe, I should handle Newline in ForcedUnforced mode by converting it to a space or empty string. Let me add this handling.
Actually, let me first implement the basic fix and see if it works. I can add Newline handling later if needed.
Here's my plan:
- Add
ForceUnforced(Box<Self>) to the Document enum
- Handle it in
fits: push the inner doc with ForcedUnforced mode
- Handle it in
format: push the inner doc with ForcedUnforced mode
- Add a
force_unforced() method
- In
format_string: wrap each expression in force_unforced()
Wait, I named it ForceUnforced but I meant ForceUnbroken. Let me stick with ForceUnbroken.
Hmm, actually the mode is called ForcedUnforced but I want the document to force Unforced. So the document should be called ForceUnforced and it forces the mode to be ForcedUnforced.
OK let me just call it ForceNoBreak for clarity. The mode would be ForcedUnforced.
Actually, let me just use the naming convention from the existing code:
ForceBroken forces ForcedBroken mode
ForceUnforced forces ForcedUnforced mode
Let me implement this.
Actually, I realize there's a naming issue. The existing mode is ForcedUnforced and there's ForceBroken. I'll add ForceUnforced (or ForceNoBreak for clarity).
Wait, looking at the modes again:
enum Mode {
Unbroken,
Broken,
ForcedBroken,
ForcedUnforced,
}
And there's ForceBroken:
pub fn force_break(self) -> Self {
Self::ForceBroken(Box::new(self))
}
So I'll add force_no_break and ForceNoBreak. Or I can add force_unbroken and ForceUnforced.
Let me just call it force_no_break and ForceNoBreak for clarity.
Here's the implementation:
In lindig.rs:
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Mode {
Unbroken,
Broken,
ForcedBroken,
ForcedUnforced,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Document<'a> {
Newline,
ForceBroken(Box<Self>),
ForceNoBreak(Box<Self>), // NEW
NextBreakFits(Box<Self>, bool),
StrictBreak { broken: &'a str, unbroken: &'a str },
FlexBreak { broken: &'a str, unbroken: &'a str },
Sequence(Vec<Self>),
Nest(isize, Box<Self>),
NestIfBroken(isize, Box<Self>),
Group(Box<Self>),
Text(Cow<'a, str>),
VerbatimText(Cow<'a, str>),
}
In fits:
Document::ForceNoBreak(doc) => {
// Always treat as fitting, process in ForcedUnforced mode
docs.push((indent, Mode::ForcedUnforced, doc));
}
In format:
Document::ForceNoBreak(document) | Document::NextBreakFits(document, _) => {
docs.push((indent, Mode::ForcedUnforced, document));
}
Wait, that changes the existing NextBreakFits handling too. Let me be more careful.
In format, the current handling is:
Document::ForceBroken(document) | Document::NextBreakFits(document, _) => {
docs.push((indent, mode, document));
}
I want to change this to:
Document::ForceBroken(document) => {
docs.push((indent, mode, document));
}
Document::ForceNoBreak(document) => {
docs.push((indent, Mode::ForcedUnforced, document));
}
Document::NextBreakFits(document, _) => {
docs.push((indent, mode, document));
}
OK, I think this is clean. Let me implement it.
Actually, let me think about Newline in ForcedUnforced mode. If there are any Newline documents inside the f-string expression (from comments), they will still produce newlines. To prevent this, I should convert Newline to a space in ForcedUnforced mode.
But actually, looking at the code, Newline documents are typically produced at statement boundaries, not inside expressions. And for f-string expressions, the expression is typically a simple identifier or call, which shouldn't produce Newline documents.
Let me just implement the basic fix first and test it. If there are Newline issues, I'll handle them later.
OK let me implement this. First, let me write a test to reproduce the issue.
Actually, let me first try to understand what happens when we format an f-string with a call expression.
Let me create a simple test file:
fn test() {
let x = f"You cannot use another ender pearl for {shared.format_duration(remaining)}."
}
Let me format this and see what happens.
Actually, I should check if there's a lis CLI tool to format files:
<tool_call>
<function=bash_terminal>
<parameter=command>
ls -la /lisette/crates/cli/
</tool_call>