From 2c48f265364bf106faa8da2ec8681d30e2eb4178 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Mon, 19 May 2025 07:57:55 +0200 Subject: [PATCH] Formatting fix for inline types with interleaved member access/attribute --- IDEHelper/Compiler/BfPrinter.cpp | 37 ++++++++++++++++++++++++++------ IDEHelper/Compiler/BfPrinter.h | 13 +++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/IDEHelper/Compiler/BfPrinter.cpp b/IDEHelper/Compiler/BfPrinter.cpp index c66c1ede..f2da195a 100644 --- a/IDEHelper/Compiler/BfPrinter.cpp +++ b/IDEHelper/Compiler/BfPrinter.cpp @@ -209,14 +209,39 @@ void BfPrinter::FlushVisitChild() std::stable_sort(nodeQueue.begin(), nodeQueue.end(), CompareNodeStart); - for (auto& node : nodeQueue) - { - mNextStateModify = node; + ChildQueueState childQueueState; + childQueueState.mQueue = &nodeQueue; + mActiveChildQueues.Add(&childQueueState); - VisitChild(node.mQueuedNode); - if (mVirtualNewLineIdx == mNextStateModify.mWantNewLineIdx) - mVirtualNewLineIdx = node.mWantNewLineIdx; + auto _HandleStateNotify = [&](StateModify node) + { + mNextStateModify = node; + + VisitChild(node.mQueuedNode); + if (mVirtualNewLineIdx == mNextStateModify.mWantNewLineIdx) + mVirtualNewLineIdx = node.mWantNewLineIdx; + }; + + while (childQueueState.mIdx < childQueueState.mQueue->mSize) + { + auto node = (*childQueueState.mQueue)[childQueueState.mIdx++]; + if (mActiveChildQueues.mSize > 1) + { + // Check for nodes in the prev queue that are actual inside the new queue (can happen with inline type declarations) + auto prevQueue = mActiveChildQueues[mActiveChildQueues.mSize - 2]; + while (true) + { + auto prevQueueNode = (*prevQueue->mQueue)[prevQueue->mIdx]; + if (prevQueueNode.mQueuedNode->mSrcStart >= node.mQueuedNode->mSrcStart) + break; + prevQueue->mIdx++; + _HandleStateNotify(prevQueueNode); + } + } + _HandleStateNotify(node); } + + mActiveChildQueues.pop_back(); } void BfPrinter::VisitChildWithPrecedingSpace(BfAstNode* bfAstNode) diff --git a/IDEHelper/Compiler/BfPrinter.h b/IDEHelper/Compiler/BfPrinter.h index 0ee568b6..1e1f63c9 100644 --- a/IDEHelper/Compiler/BfPrinter.h +++ b/IDEHelper/Compiler/BfPrinter.h @@ -49,6 +49,18 @@ public: } }; + struct ChildQueueState + { + Array* mQueue; + int mIdx; + + ChildQueueState() + { + mQueue = NULL; + mIdx = 0; + } + }; + BfSourceData* mSource; BfParserData* mParser; @@ -62,6 +74,7 @@ public: int mTriviaIdx; int mCurSrcIdx; + Array mActiveChildQueues; Array mChildNodeQueue; int mFormatStart; int mFormatEnd;