1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 04:22:20 +02:00

Improved formatting of string interpolation expressions

This commit is contained in:
Brian Fiete 2025-05-28 11:02:12 +02:00
parent 07a2e88960
commit 116d9c6f01
2 changed files with 50 additions and 5 deletions

View file

@ -1426,9 +1426,45 @@ void BfPrinter::Visit(BfLiteralExpression* literalExpr)
void BfPrinter::Visit(BfStringInterpolationExpression* stringInterpolationExpression)
{
Visit(stringInterpolationExpression->ToBase());
String str;
stringInterpolationExpression->ToString(str);
Write(str);
int startIdx = 0;
int exprIdx = 0;
auto _Flush = [&](int endIdx)
{
Write(StringView(str, startIdx, endIdx - startIdx));
startIdx = endIdx;
};
for (int strIdx = 0; strIdx < str.mLength; strIdx++)
{
char c = str[strIdx];
int curSrcIdx = stringInterpolationExpression->mSrcStart + strIdx;
if (exprIdx < stringInterpolationExpression->mExpressions.mSize)
{
auto expr = stringInterpolationExpression->mExpressions[exprIdx];
if (expr->mSrcStart == curSrcIdx)
{
_Flush(strIdx);
if (auto block = BfNodeDynCast<BfBlock>(expr))
HandleBlock(block, true);
else
VisitChild(expr);
exprIdx++;
strIdx = expr->mSrcEnd - stringInterpolationExpression->mSrcStart;
startIdx = strIdx;
continue;
}
}
}
_Flush(str.mLength);
}
void BfPrinter::Visit(BfIdentifierNode* identifierNode)
@ -3251,7 +3287,7 @@ void BfPrinter::DoBlockOpen(BfAstNode* prevNode, BfTokenNode* blockOpen, BfToken
if (prevNode != NULL)
ExpectIndent();
}
else
else if (!blockState.mIsCompact)
ExpectSpace();
if (queue)
QueueVisitChild(blockOpen);
@ -3259,7 +3295,7 @@ void BfPrinter::DoBlockOpen(BfAstNode* prevNode, BfTokenNode* blockOpen, BfToken
VisitChild(blockOpen);
if (!doInlineBlock)
ExpectIndent();
else
else if (!blockState.mIsCompact)
ExpectSpace();
blockState.mDoInlineBlock = doInlineBlock;
}
@ -3271,7 +3307,7 @@ void BfPrinter::DoBlockClose(BfAstNode* prevNode, BfTokenNode* blockOpen, BfToke
ExpectUnindent();
mNextStateModify.mDoingBlockClose = true;
}
else
else if (!blockState.mIsCompact)
ExpectSpace();
if (queue)
QueueVisitChild(blockClose);
@ -3285,10 +3321,11 @@ void BfPrinter::DoBlockClose(BfAstNode* prevNode, BfTokenNode* blockOpen, BfToke
}
}
void BfPrinter::Visit(BfBlock* block)
void BfPrinter::HandleBlock(BfBlock* block, bool isCompact)
{
BlockState blockState;
SetAndRestoreValue<BlockState*> prevBlockState(mCurBlockState, &blockState);
blockState.mIsCompact = isCompact;
DoBlockOpen(NULL, block->mOpenBrace, block->mCloseBrace, false, blockState);
for (auto& childNodeRef : *block)
@ -3305,6 +3342,11 @@ void BfPrinter::Visit(BfBlock* block)
ExpectNewLine();
}
void BfPrinter::Visit(BfBlock* block)
{
HandleBlock(block);
}
void BfPrinter::Visit(BfRootNode* rootNode)
{
for (auto child : rootNode->mChildArr)