From 55353859d0aed7d22a4ae239e18abe64fdbbe7c2 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Fri, 7 Jul 2023 22:33:48 +0800 Subject: [PATCH] - enhancement: Auto insert spaces between #include and <> when reformat - enhancement: Auto insert spaces between #include and "" when reformat --- NEWS.md | 2 ++ tools/astyle/ASFormatter.cpp | 23 ++++++++++++++++++++++- tools/astyle/ASResource.cpp | 8 ++++---- tools/astyle/astyle.h | 3 ++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/NEWS.md b/NEWS.md index 09019e24..97fb4b0a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -33,6 +33,8 @@ Red Panda C++ Version 2.23 - enhancement: Folder mode in "File in files" dialog. - enhancement: When open a file, test if it contains binary contains. - enhancement: Correctly reformat C++ three-way comparision operator "<=>" + - enhancement: Auto insert spaces between #include and <> when reformat + Red Panda C++ Version 2.22 diff --git a/tools/astyle/ASFormatter.cpp b/tools/astyle/ASFormatter.cpp index 4739b638..9b82ba0d 100644 --- a/tools/astyle/ASFormatter.cpp +++ b/tools/astyle/ASFormatter.cpp @@ -210,6 +210,7 @@ void ASFormatter::init(ASSourceIterator* si) isInPreprocessor = false; isInPreprocessorDefineDef = false; isInPreprocessorBeautify = false; + isInPreprocessorInclude = false; doesLineStartComment = false; lineEndsInCommentOnly = false; lineIsCommentOnly = false; @@ -592,6 +593,13 @@ string ASFormatter::nextLine() if (currentChar == '"' || (currentChar == '\'' && !isDigitSeparator(currentLine, charNum))) { + + if (isInPreprocessor && isInPreprocessorInclude && + currentChar=='\"') { + if (previousChar!=' ' && previousChar!='\t') + appendSpacePad(); + isInPreprocessorInclude=false; + } formatQuoteOpener(); testForTimeToSplitFormattedLine(); continue; @@ -626,10 +634,18 @@ string ASFormatter::nextLine() if (isInPreprocessor) { + if (isInPreprocessorInclude && + (currentChar=='<' || currentChar=='\"')) { + if (previousChar!=' ' && previousChar!='\t') + appendSpacePad(); + isInPreprocessorInclude=false; + } + appendCurrentChar(); continue; } + if (isInTemplate && shouldCloseTemplates) { if (previousNonWSChar == '>' && isWhiteSpace(currentChar) && peekNextChar() == '>') @@ -2673,6 +2689,7 @@ bool ASFormatter::getNextLine(bool emptyLineWasDeleted /*false*/) && (previousNonWSChar != '\\' || isEmptyLine(currentLine))) { + isInPreprocessorInclude = false; isInPreprocessor = false; isInPreprocessorDefineDef = false; } @@ -5652,7 +5669,11 @@ void ASFormatter::processPreprocessor() for (int i = 0; i < addedPreproc; i++) braceTypeStack->pop_back(); } - } + } + else if (currentLine.compare(preproc, 7, "include") == 0) + { + isInPreprocessorInclude = true; + } else if (currentLine.compare(preproc, 6, "define") == 0) isInPreprocessorDefineDef = true; } diff --git a/tools/astyle/ASResource.cpp b/tools/astyle/ASResource.cpp index 0888b599..710b7562 100644 --- a/tools/astyle/ASResource.cpp +++ b/tools/astyle/ASResource.cpp @@ -240,8 +240,8 @@ void ASResource::buildCastOperators(vector* castOperators) */ void ASResource::buildHeaders(vector* headers, int fileType, bool beautifier) { - const size_t elements = 25; - headers->reserve(elements); + const size_t elements = 25; + headers->reserve(elements); headers->emplace_back(&AS_IF); headers->emplace_back(&AS_ELSE); @@ -262,7 +262,7 @@ void ASResource::buildHeaders(vector* headers, int fileType, bool { headers->emplace_back(&_AS_TRY); // __try headers->emplace_back(&_AS_FINALLY); // __finally - headers->emplace_back(&_AS_EXCEPT); // __except + headers->emplace_back(&_AS_EXCEPT); // __except } if (fileType == JAVA_TYPE) { @@ -295,7 +295,7 @@ void ASResource::buildHeaders(vector* headers, int fileType, bool } } - assert(headers->size() < elements); + assert(headers->size() < elements); sort(headers->begin(), headers->end(), sortOnName); } diff --git a/tools/astyle/astyle.h b/tools/astyle/astyle.h index 91185fe7..b2fc8855 100644 --- a/tools/astyle/astyle.h +++ b/tools/astyle/astyle.h @@ -939,7 +939,8 @@ private: // variables bool noTrimCommentContinuation; bool isInPreprocessor; bool isInPreprocessorDefineDef; - bool isInPreprocessorBeautify; + bool isInPreprocessorBeautify; + bool isInPreprocessorInclude; bool isInTemplate; bool doesLineStartComment; bool lineEndsInCommentOnly;