mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-15 14:54:09 +02:00
Formatting improvements to failed parse
This commit is contained in:
parent
10f47cdc8a
commit
ebd57e7d8e
2 changed files with 74 additions and 8 deletions
|
@ -23,6 +23,7 @@ BfPrinter::BfPrinter(BfRootNode *rootNode, BfRootNode *sidechannelRootNode, BfRo
|
||||||
mTriviaIdx = 0;
|
mTriviaIdx = 0;
|
||||||
mCurSrcIdx = 0;
|
mCurSrcIdx = 0;
|
||||||
mCurIndentLevel = 0;
|
mCurIndentLevel = 0;
|
||||||
|
mCurBlockState = NULL;
|
||||||
mQueuedSpaceCount = 0;
|
mQueuedSpaceCount = 0;
|
||||||
mLastSpaceOffset = 0;
|
mLastSpaceOffset = 0;
|
||||||
mReformatting = false;
|
mReformatting = false;
|
||||||
|
@ -375,6 +376,8 @@ void BfPrinter::WriteIgnoredNode(BfAstNode* node)
|
||||||
startIdx = node->mSrcStart;
|
startIdx = node->mSrcStart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int lineEmittedChars = 0;
|
||||||
|
|
||||||
// This handles tab adjustment within multiline comments
|
// This handles tab adjustment within multiline comments
|
||||||
FlushIndent();
|
FlushIndent();
|
||||||
bool isNewLine = false;
|
bool isNewLine = false;
|
||||||
|
@ -517,7 +520,7 @@ void BfPrinter::WriteIgnoredNode(BfAstNode* node)
|
||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mCurCol + len > mMaxCol)
|
if ((mCurCol + len > mMaxCol) && (lineEmittedChars >= 8))
|
||||||
{
|
{
|
||||||
Write("\n");
|
Write("\n");
|
||||||
mQueuedSpaceCount = mCurIndentLevel * 4;
|
mQueuedSpaceCount = mCurIndentLevel * 4;
|
||||||
|
@ -536,6 +539,7 @@ void BfPrinter::WriteIgnoredNode(BfAstNode* node)
|
||||||
break;
|
break;
|
||||||
startIdx++;
|
startIdx++;
|
||||||
}
|
}
|
||||||
|
lineEmittedChars = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -554,9 +558,15 @@ void BfPrinter::WriteIgnoredNode(BfAstNode* node)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == '\n')
|
if (c == '\n')
|
||||||
|
{
|
||||||
mCurCol = 0;
|
mCurCol = 0;
|
||||||
|
lineEmittedChars = 0;
|
||||||
|
}
|
||||||
else if (isutf(c))
|
else if (isutf(c))
|
||||||
|
{
|
||||||
mCurCol++;
|
mCurCol++;
|
||||||
|
lineEmittedChars++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -568,6 +578,60 @@ void BfPrinter::WriteIgnoredNode(BfAstNode* node)
|
||||||
mExpectingNewLine = wasExpectingNewLine;
|
mExpectingNewLine = wasExpectingNewLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BfPrinter::CheckRawNode(BfAstNode* node)
|
||||||
|
{
|
||||||
|
if (node == NULL)
|
||||||
|
return;
|
||||||
|
if ((!BfNodeIsExact<BfTokenNode>(node)) &&
|
||||||
|
(!BfNodeIsExact<BfIdentifierNode>(node)) &&
|
||||||
|
(!BfNodeIsExact<BfLiteralExpression>(node)))
|
||||||
|
return;
|
||||||
|
|
||||||
|
//mForceUseTrivia = true;
|
||||||
|
|
||||||
|
// Usually 'raw' nodes get merged into larger nodes like expressions/statements, but if they don't then this tries to help formatting
|
||||||
|
mExpectingNewLine = false;
|
||||||
|
mVirtualNewLineIdx = mNextStateModify.mWantNewLineIdx;
|
||||||
|
mNextStateModify.mExpectingSpace = false;
|
||||||
|
|
||||||
|
bool inLineStart = false;
|
||||||
|
int spaceCount = 0;
|
||||||
|
|
||||||
|
auto parserData = node->GetParserData();
|
||||||
|
for (int i = node->mTriviaStart; i < node->mSrcStart; i++)
|
||||||
|
{
|
||||||
|
char c = parserData->mSrc[i];
|
||||||
|
if (c == '\n')
|
||||||
|
{
|
||||||
|
ExpectNewLine();
|
||||||
|
inLineStart = true;
|
||||||
|
spaceCount = 0;
|
||||||
|
}
|
||||||
|
else if (c == '\t')
|
||||||
|
{
|
||||||
|
ExpectSpace();
|
||||||
|
if (inLineStart)
|
||||||
|
spaceCount += 4;
|
||||||
|
}
|
||||||
|
else if (c == ' ')
|
||||||
|
{
|
||||||
|
ExpectSpace();
|
||||||
|
if (inLineStart)
|
||||||
|
spaceCount++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
inLineStart = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((spaceCount > 0) && (mCurBlockState != NULL))
|
||||||
|
{
|
||||||
|
int indentCount = spaceCount / 4;
|
||||||
|
mNextStateModify.mWantVirtualIndent = BF_MAX(indentCount, mCurBlockState->mIndentStart + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void BfPrinter::Visit(BfAstNode* bfAstNode)
|
void BfPrinter::Visit(BfAstNode* bfAstNode)
|
||||||
{
|
{
|
||||||
SetAndRestoreValue<bool> prevForceTrivia(mForceUseTrivia);
|
SetAndRestoreValue<bool> prevForceTrivia(mForceUseTrivia);
|
||||||
|
@ -2868,16 +2932,16 @@ void BfPrinter::DoBlockClose(BfAstNode* prevNode, BfTokenNode* blockOpen, BfToke
|
||||||
void BfPrinter::Visit(BfBlock* block)
|
void BfPrinter::Visit(BfBlock* block)
|
||||||
{
|
{
|
||||||
BlockState blockState;
|
BlockState blockState;
|
||||||
|
SetAndRestoreValue<BlockState*> prevBlockState(mCurBlockState, &blockState);
|
||||||
|
|
||||||
DoBlockOpen(NULL, block->mOpenBrace, block->mCloseBrace, false, blockState);
|
DoBlockOpen(NULL, block->mOpenBrace, block->mCloseBrace, false, blockState);
|
||||||
for (auto& childNodeRef : *block)
|
for (auto& childNodeRef : *block)
|
||||||
{
|
{
|
||||||
BfAstNode* child = childNodeRef;
|
BfAstNode* child = childNodeRef;
|
||||||
SetAndRestoreValue<bool> prevForceTrivia(mForceUseTrivia);
|
SetAndRestoreValue<bool> prevForceTrivia(mForceUseTrivia);
|
||||||
bool isSolitary = child->IsA<BfIdentifierNode>();
|
SetAndRestoreValue<int> prevVirtualIndent(mNextStateModify.mWantVirtualIndent);
|
||||||
if (isSolitary)
|
|
||||||
mForceUseTrivia = true;
|
|
||||||
|
|
||||||
SetAndRestoreValue<BfAstNode*> prevBlockMember(mCurBlockMember, child);
|
SetAndRestoreValue<BfAstNode*> prevBlockMember(mCurBlockMember, child);
|
||||||
|
CheckRawNode(child);
|
||||||
child->Accept(this);
|
child->Accept(this);
|
||||||
}
|
}
|
||||||
DoBlockClose(NULL, block->mOpenBrace, block->mCloseBrace, false, blockState);
|
DoBlockClose(NULL, block->mOpenBrace, block->mCloseBrace, false, blockState);
|
||||||
|
|
|
@ -72,6 +72,7 @@ public:
|
||||||
bool mReformatting;
|
bool mReformatting;
|
||||||
bool mIgnoreTrivia;
|
bool mIgnoreTrivia;
|
||||||
bool mDocPrep;
|
bool mDocPrep;
|
||||||
|
BlockState* mCurBlockState;
|
||||||
int mCurIndentLevel;
|
int mCurIndentLevel;
|
||||||
int mQueuedSpaceCount;
|
int mQueuedSpaceCount;
|
||||||
int mLastSpaceOffset; // Indent difference from original to new
|
int mLastSpaceOffset; // Indent difference from original to new
|
||||||
|
@ -114,6 +115,7 @@ public:
|
||||||
void QueueMethodDeclaration(BfMethodDeclaration* methodDeclaration);
|
void QueueMethodDeclaration(BfMethodDeclaration* methodDeclaration);
|
||||||
int CalcOrigLineSpacing(BfAstNode* bfAstNode, int* lineStartIdx);
|
int CalcOrigLineSpacing(BfAstNode* bfAstNode, int* lineStartIdx);
|
||||||
void WriteIgnoredNode(BfAstNode* node);
|
void WriteIgnoredNode(BfAstNode* node);
|
||||||
|
void CheckRawNode(BfAstNode* node);
|
||||||
|
|
||||||
virtual void Visit(BfAstNode* bfAstNode) override;
|
virtual void Visit(BfAstNode* bfAstNode) override;
|
||||||
virtual void Visit(BfErrorNode* bfErrorNode) override;
|
virtual void Visit(BfErrorNode* bfErrorNode) override;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue