Skip to content

Commit e8af8cf

Browse files
committed
Fix wrapping long strings logic in formatter
1 parent 1fe8e77 commit e8af8cf

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

formatter/generic/genericformatter.cpp

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ struct Item
183183
items.back().AddTokenToLastAtom(token);
184184
}
185185

186+
void AddTokenToLastStringComponent(const InstructionTextToken& token)
187+
{
188+
if (!tokens.empty())
189+
tokens.push_back(token);
190+
else if (items.empty())
191+
items.push_back(Item {StringComponent, {}, {token}, 0});
192+
else
193+
items.back().AddTokenToLastStringComponent(token);
194+
}
195+
186196
void CalculateWidth()
187197
{
188198
width = 0;
@@ -204,7 +214,6 @@ struct ItemLayoutStackEntry
204214
size_t desiredWidth;
205215
size_t desiredContinuationWidth;
206216
size_t desiredStringWidth;
207-
size_t desiredStringContinuationWidth;
208217
bool newLineOnReenteringScope;
209218
};
210219

@@ -355,6 +364,16 @@ static vector<InstructionTextToken> ParseStringToken(
355364

356365
static vector<Item> CreateStringGroups(const vector<Item>& items)
357366
{
367+
// We handle strings mostly the same as other types except the introduction
368+
// of the StringComponent and StringWhitespace types.
369+
//
370+
// The reason we introduce these is for the specific behaviors when formatting multiline
371+
// string annotations. String annotations have a different desired width than tokens
372+
// like arguments, comments, etc.
373+
//
374+
// Additionally, we don't wrap trailing whitespace until the preceding token is within
375+
// the wrapping width, unlike other token types.
376+
358377
vector<Item> result, pending;
359378
bool hasStrings = false;
360379
for (auto& i : items)
@@ -370,7 +389,7 @@ static vector<Item> CreateStringGroups(const vector<Item>& items)
370389
else
371390
{
372391
for (auto& j : i.tokens)
373-
pending.back().AddTokenToLastAtom(j);
392+
pending.back().AddTokenToLastStringComponent(j);
374393
result.push_back(Item {StringComponent, pending, {}, 0});
375394
}
376395
pending.clear();
@@ -385,18 +404,18 @@ static vector<Item> CreateStringGroups(const vector<Item>& items)
385404
result.push_back(Item {StringComponent, pending, {}, 0});
386405
pending.clear();
387406
}
388-
result.push_back(Item {StringWhitespace, i.items, i.tokens, i.width});
407+
result.push_back(Item {StringWhitespace, i.items, i.tokens, 0});
389408
}
390409
else if (i.type == FormatSpecifier || i.type == EscapeSequence)
391410
{
392411
// Flush previous tokens before special sequences like format specifiers or
393412
// escape sequences
394413
if (!pending.empty())
395414
{
396-
result.push_back(Item {StringComponent, pending, {}, 0 });
415+
result.push_back(Item {StringComponent, pending, {}, 0});
397416
pending.clear();
398417
}
399-
result.push_back(Item { StringComponent, i.items, i.tokens, i.width});
418+
result.push_back(Item { StringComponent, i.items, i.tokens, 0});
400419
}
401420
else if (i.type == StartOfContainer && pending.empty())
402421
{
@@ -750,19 +769,10 @@ vector<DisassemblyTextLine> GenericLineFormatter::FormatLines(
750769
if (indentation < settings.desiredLineLength)
751770
{
752771
size_t remainingStringWidth = settings.desiredLineLength - indentation;
753-
if (remainingStringWidth > desiredWidth)
772+
if (remainingStringWidth > desiredStringWidth)
754773
desiredStringWidth = remainingStringWidth;
755774
}
756775

757-
// Compute target width for continuation string wrapping lines
758-
size_t desiredStringContinuationWidth = settings.stringWrappingWidth;
759-
if (continuationIndentation < settings.desiredLineLength)
760-
{
761-
size_t remainingStringWidth = settings.desiredLineLength - continuationIndentation;
762-
if (remainingStringWidth > desiredStringContinuationWidth)
763-
desiredStringContinuationWidth = remainingStringWidth;
764-
}
765-
766776
// Gather the indentation tokens at the beginning of the line
767777
vector<InstructionTextToken> indentationTokens = currentLine.GetAddressAndIndentationTokens();
768778
size_t tokenIndex = indentationTokens.size();
@@ -927,7 +937,7 @@ vector<DisassemblyTextLine> GenericLineFormatter::FormatLines(
927937
bool firstTokenOfLine = true;
928938

929939
stack<ItemLayoutStackEntry> layoutStack;
930-
layoutStack.push({items, additionalContinuationIndentation, desiredWidth, desiredContinuationWidth, desiredStringWidth, desiredStringContinuationWidth, false});
940+
layoutStack.push({items, additionalContinuationIndentation, desiredWidth, desiredContinuationWidth, desiredStringWidth, false});
931941

932942
auto newLine = [&]() {
933943
if (!firstTokenOfLine)
@@ -952,7 +962,6 @@ vector<DisassemblyTextLine> GenericLineFormatter::FormatLines(
952962
outputLine.tokens.emplace_back(TextToken, string(additionalContinuationIndentation, ' '));
953963
currentWidth = 0;
954964
desiredWidth = desiredContinuationWidth;
955-
desiredStringWidth = desiredStringContinuationWidth;
956965
firstTokenOfLine = true;
957966
};
958967

@@ -966,7 +975,6 @@ vector<DisassemblyTextLine> GenericLineFormatter::FormatLines(
966975
desiredWidth = layoutStackEntry.desiredWidth;
967976
desiredContinuationWidth = layoutStackEntry.desiredContinuationWidth;
968977
desiredStringWidth = layoutStackEntry.desiredStringWidth;
969-
desiredStringContinuationWidth = layoutStackEntry.desiredStringContinuationWidth;
970978

971979
// Check to see if the scope we are returning to needs a new line. This is used when an argument
972980
// spans multiple lines. The rest of the arguments are placed on separate lines from the long argument.
@@ -979,10 +987,11 @@ vector<DisassemblyTextLine> GenericLineFormatter::FormatLines(
979987
{
980988
// If a string is too wide to fit on the current line, create a newline
981989
// without additional indentation
990+
LogError("Line length vs. desired %zu vs %zu", currentWidth + item->width, desiredStringWidth);
982991
newLine();
983992
continue;
984993
}
985-
if (currentWidth + item->width > desiredWidth && item->type != StringWhitespace)
994+
if (currentWidth + item->width > desiredWidth && item->type != StringWhitespace && item->type != StringComponent)
986995
{
987996
// Current item is too wide to fit on the current line, will need to start a new line.
988997
// Whitespace is allowed to be too wide; we push it on as the preceding word is wrapped.
@@ -1001,7 +1010,7 @@ vector<DisassemblyTextLine> GenericLineFormatter::FormatLines(
10011010
if (next != items.end())
10021011
{
10031012
layoutStack.push({vector(next, items.end()), additionalContinuationIndentation,
1004-
desiredWidth, desiredContinuationWidth, desiredStringWidth, desiredStringContinuationWidth, true});
1013+
desiredWidth, desiredContinuationWidth, desiredStringWidth, true});
10051014
}
10061015

10071016
newLine();
@@ -1012,13 +1021,8 @@ vector<DisassemblyTextLine> GenericLineFormatter::FormatLines(
10121021
else
10131022
desiredContinuationWidth -= settings.tabWidth;
10141023

1015-
if (desiredStringContinuationWidth < settings.minimumContentLength + settings.tabWidth)
1016-
desiredStringContinuationWidth = settings.minimumContentLength;
1017-
else
1018-
desiredStringContinuationWidth -= settings.tabWidth;
1019-
10201024
layoutStack.push({item->items, additionalContinuationIndentation, desiredWidth,
1021-
desiredContinuationWidth, desiredStringWidth, desiredStringContinuationWidth, false});
1025+
desiredContinuationWidth, desiredStringWidth, false});
10221026
break;
10231027
}
10241028

@@ -1028,10 +1032,10 @@ vector<DisassemblyTextLine> GenericLineFormatter::FormatLines(
10281032
if (next != items.end())
10291033
{
10301034
layoutStack.push({vector(next, items.end()), additionalContinuationIndentation,
1031-
desiredWidth, desiredContinuationWidth, desiredStringWidth, desiredStringContinuationWidth, false});
1035+
desiredWidth, desiredContinuationWidth, desiredStringWidth, false});
10321036
}
10331037
layoutStack.push({item->items, additionalContinuationIndentation, desiredWidth,
1034-
desiredContinuationWidth, desiredStringWidth, desiredStringContinuationWidth, false});
1038+
desiredContinuationWidth, desiredStringWidth, false});
10351039
break;
10361040
}
10371041

0 commit comments

Comments
 (0)