mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-16 15:24:10 +02:00
pragma format disable/restore, better space handing in formatting
This commit is contained in:
parent
f9396f69c0
commit
dd6e7f0c2a
4 changed files with 128 additions and 46 deletions
|
@ -404,7 +404,7 @@ namespace IDE.Compiler
|
||||||
}
|
}
|
||||||
|
|
||||||
editWidgetContent.ContentChanged();
|
editWidgetContent.ContentChanged();
|
||||||
Debug.WriteLine("Reformat {0} inserts, total of {1} chars. {2} deletes, total of {3} chars.", insertCount, insertChars, deleteCount, deleteChars);
|
//Debug.WriteLine("Reformat {0} inserts, total of {1} chars. {2} deletes, total of {3} chars.", insertCount, insertChars, deleteCount, deleteChars);
|
||||||
|
|
||||||
sourceEditBatchHelper.Finish();
|
sourceEditBatchHelper.Finish();
|
||||||
}
|
}
|
||||||
|
|
|
@ -785,7 +785,7 @@ void BfParser::HandlePragma(const StringImpl& pragma, BfBlock* block)
|
||||||
auto itr = block->begin();
|
auto itr = block->begin();
|
||||||
auto paramNode = *itr;
|
auto paramNode = *itr;
|
||||||
|
|
||||||
if (paramNode->ToString() == "warning")
|
if (paramNode->ToStringView() == "warning")
|
||||||
{
|
{
|
||||||
++itr;
|
++itr;
|
||||||
//auto iterNode = paramNode->mNext;
|
//auto iterNode = paramNode->mNext;
|
||||||
|
@ -797,11 +797,11 @@ void BfParser::HandlePragma(const StringImpl& pragma, BfBlock* block)
|
||||||
++itr;
|
++itr;
|
||||||
|
|
||||||
bool enable;
|
bool enable;
|
||||||
if (iterNode->ToString() == "disable")
|
if (iterNode->ToStringView() == "disable")
|
||||||
{
|
{
|
||||||
enable = false;
|
enable = false;
|
||||||
}
|
}
|
||||||
else if (iterNode->ToString() == "restore")
|
else if (iterNode->ToStringView() == "restore")
|
||||||
{
|
{
|
||||||
enable = true;
|
enable = true;
|
||||||
}
|
}
|
||||||
|
@ -851,6 +851,19 @@ void BfParser::HandlePragma(const StringImpl& pragma, BfBlock* block)
|
||||||
mPassInstance->FailAfterAt("Expected \"disable\" or \"restore\" after \"warning\"", mSourceData, paramNode->GetSrcEnd() - 1);
|
mPassInstance->FailAfterAt("Expected \"disable\" or \"restore\" after \"warning\"", mSourceData, paramNode->GetSrcEnd() - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (paramNode->ToStringView() == "format")
|
||||||
|
{
|
||||||
|
++itr;
|
||||||
|
BfAstNode* iterNode = itr.Get();
|
||||||
|
if (iterNode)
|
||||||
|
{
|
||||||
|
if ((iterNode->ToStringView() != "disable") &&
|
||||||
|
(iterNode->ToStringView() != "restore"))
|
||||||
|
{
|
||||||
|
mPassInstance->FailAfterAt("Expected \"disable\" or \"restore\" after \"format\"", mSourceData, paramNode->GetSrcEnd() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mPassInstance->FailAt("Unknown #pragma directive", mSourceData, paramNode->GetSrcStart(), paramNode->GetSrcLength());
|
mPassInstance->FailAt("Unknown #pragma directive", mSourceData, paramNode->GetSrcStart(), paramNode->GetSrcLength());
|
||||||
|
@ -2306,7 +2319,7 @@ void BfParser::NextToken(int endIdx, bool outerIsInterpolate, bool disablePrepro
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!handled)
|
if ((!handled) && (!disablePreprocessor))
|
||||||
{
|
{
|
||||||
auto bfCommentNode = mAlloc->Alloc<BfCommentNode>();
|
auto bfCommentNode = mAlloc->Alloc<BfCommentNode>();
|
||||||
bfCommentNode->Init(this);
|
bfCommentNode->Init(this);
|
||||||
|
|
|
@ -48,6 +48,7 @@ BfPrinter::BfPrinter(BfRootNode *rootNode, BfRootNode *sidechannelRootNode, BfRo
|
||||||
mTabSize = 4;
|
mTabSize = 4;
|
||||||
mWantsTabsAsSpaces = false;
|
mWantsTabsAsSpaces = false;
|
||||||
mIndentCaseLabels = false;
|
mIndentCaseLabels = false;
|
||||||
|
mFormatDisableCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BfPrinter::Write(const StringView& str)
|
void BfPrinter::Write(const StringView& str)
|
||||||
|
@ -298,6 +299,16 @@ int BfPrinter::CalcOrigLineSpacing(BfAstNode* bfAstNode, int* lineStartIdx)
|
||||||
|
|
||||||
void BfPrinter::WriteIgnoredNode(BfAstNode* node)
|
void BfPrinter::WriteIgnoredNode(BfAstNode* node)
|
||||||
{
|
{
|
||||||
|
Update(node);
|
||||||
|
|
||||||
|
if (!mReformatting)
|
||||||
|
{
|
||||||
|
int startIdx = BF_MAX(node->mTriviaStart, mTriviaIdx);
|
||||||
|
Write(node, startIdx, node->mSrcEnd - startIdx);
|
||||||
|
mTriviaIdx = node->mSrcEnd;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
bool startsWithSpace = false;
|
bool startsWithSpace = false;
|
||||||
|
|
||||||
bool wasExpectingNewLine = mExpectingNewLine;
|
bool wasExpectingNewLine = mExpectingNewLine;
|
||||||
|
@ -600,7 +611,7 @@ void BfPrinter::WriteIgnoredNode(BfAstNode* node)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wantsPrefixSpace)
|
if (wantsPrefixSpace)
|
||||||
{
|
{
|
||||||
mQueuedSpaceCount++;
|
mQueuedSpaceCount++;
|
||||||
wantsPrefixSpace = false;
|
wantsPrefixSpace = false;
|
||||||
}
|
}
|
||||||
|
@ -693,6 +704,54 @@ void BfPrinter::CheckRawNode(BfAstNode* node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BfPrinter::Update(BfAstNode* bfAstNode)
|
||||||
|
{
|
||||||
|
// This won't be true if we move nodes around during refactoring
|
||||||
|
if (bfAstNode->GetSrcStart() >= mCurSrcIdx)
|
||||||
|
{
|
||||||
|
mCurSrcIdx = bfAstNode->GetSrcStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((!mReformatting) && (mFormatDisableCount == 0) && (mFormatStart != -1) && (mCurSrcIdx >= mFormatStart) && ((mCurSrcIdx < mFormatEnd) || (mFormatEnd == -1)))
|
||||||
|
{
|
||||||
|
mReformatting = true;
|
||||||
|
|
||||||
|
// We entered the format region, figure our what our current indent level is
|
||||||
|
int backIdx = mCurSrcIdx - 1;
|
||||||
|
int prevSpaceCount = 0;
|
||||||
|
auto astNodeSrc = bfAstNode->GetSourceData();
|
||||||
|
while (backIdx >= 0)
|
||||||
|
{
|
||||||
|
char c = astNodeSrc->mSrc[backIdx];
|
||||||
|
if (c == ' ')
|
||||||
|
prevSpaceCount++;
|
||||||
|
else if (c == '\t')
|
||||||
|
prevSpaceCount += mTabSize;
|
||||||
|
else if (c == '\n')
|
||||||
|
{
|
||||||
|
// Found previous line
|
||||||
|
mCurIndentLevel = prevSpaceCount / mTabSize;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
prevSpaceCount = 0;
|
||||||
|
}
|
||||||
|
backIdx--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mFormatDisableCount != 0)
|
||||||
|
mReformatting = false;
|
||||||
|
|
||||||
|
if ((mCurSrcIdx >= mFormatEnd) && (mFormatEnd != -1))
|
||||||
|
mReformatting = false;
|
||||||
|
|
||||||
|
bool expectingNewLine = mNextStateModify.mWantNewLineIdx != mVirtualNewLineIdx;
|
||||||
|
if (expectingNewLine)
|
||||||
|
mExpectingNewLine = true;
|
||||||
|
}
|
||||||
|
|
||||||
void BfPrinter::Visit(BfAstNode* bfAstNode)
|
void BfPrinter::Visit(BfAstNode* bfAstNode)
|
||||||
{
|
{
|
||||||
SetAndRestoreValue<bool> prevForceTrivia(mForceUseTrivia);
|
SetAndRestoreValue<bool> prevForceTrivia(mForceUseTrivia);
|
||||||
|
@ -743,43 +802,7 @@ void BfPrinter::Visit(BfAstNode* bfAstNode)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This won't be true if we move nodes around during refactoring
|
Update(bfAstNode);
|
||||||
if (bfAstNode->GetSrcStart() >= mCurSrcIdx)
|
|
||||||
{
|
|
||||||
mCurSrcIdx = bfAstNode->GetSrcStart();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((!mReformatting) && (mFormatStart != -1) && (mCurSrcIdx >= mFormatStart) && ((mCurSrcIdx < mFormatEnd) || (mFormatEnd == -1)))
|
|
||||||
{
|
|
||||||
mReformatting = true;
|
|
||||||
|
|
||||||
// We entered the format region, figure our what our current indent level is
|
|
||||||
int backIdx = mCurSrcIdx - 1;
|
|
||||||
int prevSpaceCount = 0;
|
|
||||||
auto astNodeSrc = bfAstNode->GetSourceData();
|
|
||||||
while (backIdx >= 0)
|
|
||||||
{
|
|
||||||
char c = astNodeSrc->mSrc[backIdx];
|
|
||||||
if (c == ' ')
|
|
||||||
prevSpaceCount++;
|
|
||||||
else if (c == '\t')
|
|
||||||
prevSpaceCount += mTabSize;
|
|
||||||
else if (c == '\n')
|
|
||||||
{
|
|
||||||
// Found previous line
|
|
||||||
mCurIndentLevel = prevSpaceCount / mTabSize;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
prevSpaceCount = 0;
|
|
||||||
}
|
|
||||||
backIdx--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((mCurSrcIdx >= mFormatEnd) && (mFormatEnd != -1))
|
|
||||||
mReformatting = false;
|
|
||||||
|
|
||||||
// When triviaStart == -1, that indicates it's a combined node where the text we want to process is on the inside
|
// When triviaStart == -1, that indicates it's a combined node where the text we want to process is on the inside
|
||||||
if (bfAstNode->GetTriviaStart() != -1)
|
if (bfAstNode->GetTriviaStart() != -1)
|
||||||
|
@ -807,7 +830,11 @@ void BfPrinter::Visit(BfAstNode* bfAstNode)
|
||||||
int prevSpaceCount = -1;
|
int prevSpaceCount = -1;
|
||||||
int spaceCount = 0;
|
int spaceCount = 0;
|
||||||
auto astNodeSrc = bfAstNode->GetSourceData();
|
auto astNodeSrc = bfAstNode->GetSourceData();
|
||||||
for (int i = mTriviaIdx; i < bfAstNode->GetSrcStart(); i++)
|
|
||||||
|
bool canUseTrivia = true;
|
||||||
|
int spaceTriviaStart = -1;
|
||||||
|
int triviaEnd = bfAstNode->GetSrcStart();
|
||||||
|
for (int i = mTriviaIdx; i < triviaEnd; i++)
|
||||||
{
|
{
|
||||||
if (mIgnoreTrivia)
|
if (mIgnoreTrivia)
|
||||||
break;
|
break;
|
||||||
|
@ -815,9 +842,17 @@ void BfPrinter::Visit(BfAstNode* bfAstNode)
|
||||||
char c = astNodeSrc->mSrc[i];
|
char c = astNodeSrc->mSrc[i];
|
||||||
|
|
||||||
if (c == ' ')
|
if (c == ' ')
|
||||||
|
{
|
||||||
|
if (spaceTriviaStart == -1)
|
||||||
|
spaceTriviaStart = i;
|
||||||
spaceCount++;
|
spaceCount++;
|
||||||
|
}
|
||||||
else if (c == '\t')
|
else if (c == '\t')
|
||||||
|
{
|
||||||
|
if (spaceTriviaStart == -1)
|
||||||
|
spaceTriviaStart = i;
|
||||||
spaceCount += mTabSize;
|
spaceCount += mTabSize;
|
||||||
|
}
|
||||||
|
|
||||||
if (((c == '\n') || (i == bfAstNode->GetSrcStart() - 1)) && (hadPrevLineSpacing) && (prevSpaceCount > 0))
|
if (((c == '\n') || (i == bfAstNode->GetSrcStart() - 1)) && (hadPrevLineSpacing) && (prevSpaceCount > 0))
|
||||||
{
|
{
|
||||||
|
@ -829,6 +864,7 @@ void BfPrinter::Visit(BfAstNode* bfAstNode)
|
||||||
|
|
||||||
if (c == '\n')
|
if (c == '\n')
|
||||||
{
|
{
|
||||||
|
canUseTrivia = false;
|
||||||
hadNewline = true;
|
hadNewline = true;
|
||||||
int backIdx = i - 1;
|
int backIdx = i - 1;
|
||||||
prevSpaceCount = 0;
|
prevSpaceCount = 0;
|
||||||
|
@ -861,15 +897,28 @@ void BfPrinter::Visit(BfAstNode* bfAstNode)
|
||||||
backIdx--;
|
backIdx--;
|
||||||
}
|
}
|
||||||
spaceCount = 0;
|
spaceCount = 0;
|
||||||
|
spaceTriviaStart = -1;
|
||||||
}
|
}
|
||||||
else if (!isspace((uint8)c))
|
else if (!isspace((uint8)c))
|
||||||
spaceCount = 0;
|
{
|
||||||
}
|
spaceCount = 0;
|
||||||
|
spaceTriviaStart = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((canUseTrivia) && (spaceCount > 1) && (spaceTriviaStart != -1))
|
||||||
|
{
|
||||||
|
Write(bfAstNode, spaceTriviaStart, triviaEnd - spaceTriviaStart);
|
||||||
|
mNextStateModify.mExpectingSpace = false;
|
||||||
|
usedTrivia = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (usedTrivia)
|
if (usedTrivia)
|
||||||
{
|
{
|
||||||
// Already did whitespace
|
// Already did whitespace
|
||||||
|
mNextStateModify.mExpectingSpace = false;
|
||||||
}
|
}
|
||||||
else if ((mNextStateModify.mDoingBlockOpen) || (mNextStateModify.mDoingBlockClose) || (mIsFirstStatementInBlock))
|
else if ((mNextStateModify.mDoingBlockOpen) || (mNextStateModify.mDoingBlockClose) || (mIsFirstStatementInBlock))
|
||||||
{
|
{
|
||||||
|
@ -1057,11 +1106,27 @@ void BfPrinter::Visit(BfCommentNode* commentNode)
|
||||||
void BfPrinter::Visit(BfPreprocesorIgnoredSectionNode* preprocesorIgnoredSection)
|
void BfPrinter::Visit(BfPreprocesorIgnoredSectionNode* preprocesorIgnoredSection)
|
||||||
{
|
{
|
||||||
WriteIgnoredNode(preprocesorIgnoredSection);
|
WriteIgnoredNode(preprocesorIgnoredSection);
|
||||||
|
ExpectNewLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BfPrinter::Visit(BfPreprocessorNode* preprocessorNode)
|
void BfPrinter::Visit(BfPreprocessorNode* preprocessorNode)
|
||||||
{
|
{
|
||||||
WriteIgnoredNode(preprocessorNode);
|
WriteIgnoredNode(preprocessorNode);
|
||||||
|
|
||||||
|
if ((preprocessorNode->mCommand->ToStringView() == "pragma") &&
|
||||||
|
(preprocessorNode->mArgument != NULL) &&
|
||||||
|
(preprocessorNode->mArgument->mChildArr.mSize == 2) &&
|
||||||
|
(preprocessorNode->mArgument->mChildArr[0] != NULL) &&
|
||||||
|
(preprocessorNode->mArgument->mChildArr[0]->ToStringView() == "format") &&
|
||||||
|
(preprocessorNode->mArgument->mChildArr[1] != NULL))
|
||||||
|
{
|
||||||
|
if (preprocessorNode->mArgument->mChildArr[1]->ToStringView() == "disable")
|
||||||
|
mFormatDisableCount++;
|
||||||
|
else if (preprocessorNode->mArgument->mChildArr[1]->ToStringView() == "restore")
|
||||||
|
mFormatDisableCount = BF_MAX(0, mFormatDisableCount - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ExpectNewLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BfPrinter::Visit(BfAttributeDirective* attributeDirective)
|
void BfPrinter::Visit(BfAttributeDirective* attributeDirective)
|
||||||
|
@ -2976,6 +3041,8 @@ void BfPrinter::Visit(BfTypeDeclaration* typeDeclaration)
|
||||||
VisitChild(typeDeclaration->mDefineNode);
|
VisitChild(typeDeclaration->mDefineNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ExpectNewLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BfPrinter::Visit(BfUsingDirective* usingDirective)
|
void BfPrinter::Visit(BfUsingDirective* usingDirective)
|
||||||
|
|
|
@ -66,6 +66,7 @@ public:
|
||||||
Array<StateModify> mChildNodeQueue;
|
Array<StateModify> mChildNodeQueue;
|
||||||
int mFormatStart;
|
int mFormatStart;
|
||||||
int mFormatEnd;
|
int mFormatEnd;
|
||||||
|
int mFormatDisableCount;
|
||||||
StateModify mNextStateModify;
|
StateModify mNextStateModify;
|
||||||
|
|
||||||
String mOutString;
|
String mOutString;
|
||||||
|
@ -98,6 +99,7 @@ public:
|
||||||
BfPrinter(BfRootNode* rootNode, BfRootNode* sidechannelRootNode, BfRootNode* errorRootNode);
|
BfPrinter(BfRootNode* rootNode, BfRootNode* sidechannelRootNode, BfRootNode* errorRootNode);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
void Update(BfAstNode* bfAstNode);
|
||||||
bool CheckReplace(BfAstNode* astNode);
|
bool CheckReplace(BfAstNode* astNode);
|
||||||
void FlushIndent();
|
void FlushIndent();
|
||||||
void Write(const StringView& str);
|
void Write(const StringView& str);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue