Skip to content

Commit 1fe8e77

Browse files
committed
Fix string wrapping deleting closing quotes
1 parent e273cdc commit 1fe8e77

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

formatter/generic/genericformatter.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,10 @@ static vector<Item> CreateStringGroups(const vector<Item>& items)
362362
if (i.type == StringSeparator && !i.tokens.empty())
363363
{
364364
// We try to push separators onto a preceding word, otherwise treat as
365-
// a singular atom
365+
// a singular string component
366366
if (pending.empty())
367367
{
368-
result.push_back(Item {Atom, {}, {i.tokens}, 0});
368+
result.push_back(Item {StringComponent, {}, {i.tokens}, 0});
369369
}
370370
else
371371
{
@@ -378,7 +378,8 @@ static vector<Item> CreateStringGroups(const vector<Item>& items)
378378
}
379379
else if (i.type == StringWhitespace)
380380
{
381-
// Special case because we let whitespace trail even if over width
381+
// Whitespace is allowed to exceed width without wrapping, so we use a special
382+
// type for it.
382383
if (!pending.empty())
383384
{
384385
result.push_back(Item {StringComponent, pending, {}, 0});
@@ -395,17 +396,17 @@ static vector<Item> CreateStringGroups(const vector<Item>& items)
395396
result.push_back(Item {StringComponent, pending, {}, 0 });
396397
pending.clear();
397398
}
398-
result.push_back(Item { Atom, i.items, i.tokens, i.width});
399+
result.push_back(Item { StringComponent, i.items, i.tokens, i.width});
399400
}
400-
401401
else if (i.type == StartOfContainer && pending.empty())
402402
{
403403
result.push_back(i);
404404
}
405405
else if (i.type == EndOfContainer && hasStrings && !pending.empty())
406406
{
407-
result.push_back(Item {StringComponent, pending, {}, 0});
407+
result.push_back(Item {Group, pending, {}, 0});
408408
result.push_back(i);
409+
pending.clear();
409410
}
410411
else
411412
{
@@ -748,16 +749,16 @@ vector<DisassemblyTextLine> GenericLineFormatter::FormatLines(
748749
size_t desiredStringWidth = settings.stringWrappingWidth;
749750
if (indentation < settings.desiredLineLength)
750751
{
751-
size_t remainingStringWidth = desiredStringWidth - indentation;
752-
if (remainingStringWidth > desiredStringWidth)
752+
size_t remainingStringWidth = settings.desiredLineLength - indentation;
753+
if (remainingStringWidth > desiredWidth)
753754
desiredStringWidth = remainingStringWidth;
754755
}
755756

756757
// Compute target width for continuation string wrapping lines
757758
size_t desiredStringContinuationWidth = settings.stringWrappingWidth;
758759
if (continuationIndentation < settings.desiredLineLength)
759760
{
760-
size_t remainingStringWidth = desiredStringContinuationWidth - continuationIndentation;
761+
size_t remainingStringWidth = settings.desiredLineLength - continuationIndentation;
761762
if (remainingStringWidth > desiredStringContinuationWidth)
762763
desiredStringContinuationWidth = remainingStringWidth;
763764
}
@@ -779,8 +780,7 @@ vector<DisassemblyTextLine> GenericLineFormatter::FormatLines(
779780
{
780781
case BraceToken:
781782
// Beginning of string
782-
if (tokenIndex + 1 < currentLine.tokens.size()
783-
&& currentLine.tokens[tokenIndex + 1].type == StringToken)
783+
if (trimmedText == "\"" && currentLine.tokens[tokenIndex + 1].type == StringToken)
784784
{
785785
// Create a ContainerContents item and place it onto the item stack. This will hold anything
786786
// inside the container once the end of the container is found.
@@ -792,14 +792,18 @@ vector<DisassemblyTextLine> GenericLineFormatter::FormatLines(
792792
items.push_back(Item {StartOfContainer, {}, {token}, 0});
793793
}
794794
// End of string
795-
else if (currentLine.tokens[tokenIndex].type == StringToken
796-
&& tokenIndex + 1 < currentLine.tokens.size()
797-
&& currentLine.tokens[tokenIndex + 1].type == BraceToken)
795+
else if (trimmedText == "\"" && currentLine.tokens[tokenIndex - 1].type == StringToken)
798796
{
799-
// Create a ContainerContents item and place it onto the item stack. This will hold anything
800-
// inside the container once the end of the container is found.
801-
items.push_back(Item {Container, {}, {}, 0});
802-
itemStack.push(items);
797+
items.push_back(Item {EndOfContainer, {}, {token}, 0});
798+
799+
if (itemStack.empty())
800+
break;
801+
802+
// Go back up the item stack and add the items to the container
803+
vector<Item> parent = itemStack.top();
804+
itemStack.pop();
805+
parent.back().items.insert(parent.back().items.end(), items.begin(), items.end());
806+
items = parent;
803807
}
804808
else if (trimmedText == "(" || trimmedText == "[" || trimmedText == "{")
805809
{

0 commit comments

Comments
 (0)