- enhancement: Auto insert spaces between #include and <> when reformat

- enhancement: Auto insert spaces between #include and "" when reformat
This commit is contained in:
Roy Qu 2023-07-07 22:33:48 +08:00
parent 5e71973152
commit 55353859d0
4 changed files with 30 additions and 6 deletions

View File

@ -33,6 +33,8 @@ Red Panda C++ Version 2.23
- enhancement: Folder mode in "File in files" dialog. - enhancement: Folder mode in "File in files" dialog.
- enhancement: When open a file, test if it contains binary contains. - enhancement: When open a file, test if it contains binary contains.
- enhancement: Correctly reformat C++ three-way comparision operator "<=>" - enhancement: Correctly reformat C++ three-way comparision operator "<=>"
- enhancement: Auto insert spaces between #include and <> when reformat
Red Panda C++ Version 2.22 Red Panda C++ Version 2.22

View File

@ -210,6 +210,7 @@ void ASFormatter::init(ASSourceIterator* si)
isInPreprocessor = false; isInPreprocessor = false;
isInPreprocessorDefineDef = false; isInPreprocessorDefineDef = false;
isInPreprocessorBeautify = false; isInPreprocessorBeautify = false;
isInPreprocessorInclude = false;
doesLineStartComment = false; doesLineStartComment = false;
lineEndsInCommentOnly = false; lineEndsInCommentOnly = false;
lineIsCommentOnly = false; lineIsCommentOnly = false;
@ -592,6 +593,13 @@ string ASFormatter::nextLine()
if (currentChar == '"' if (currentChar == '"'
|| (currentChar == '\'' && !isDigitSeparator(currentLine, charNum))) || (currentChar == '\'' && !isDigitSeparator(currentLine, charNum)))
{ {
if (isInPreprocessor && isInPreprocessorInclude &&
currentChar=='\"') {
if (previousChar!=' ' && previousChar!='\t')
appendSpacePad();
isInPreprocessorInclude=false;
}
formatQuoteOpener(); formatQuoteOpener();
testForTimeToSplitFormattedLine(); testForTimeToSplitFormattedLine();
continue; continue;
@ -626,10 +634,18 @@ string ASFormatter::nextLine()
if (isInPreprocessor) if (isInPreprocessor)
{ {
if (isInPreprocessorInclude &&
(currentChar=='<' || currentChar=='\"')) {
if (previousChar!=' ' && previousChar!='\t')
appendSpacePad();
isInPreprocessorInclude=false;
}
appendCurrentChar(); appendCurrentChar();
continue; continue;
} }
if (isInTemplate && shouldCloseTemplates) if (isInTemplate && shouldCloseTemplates)
{ {
if (previousNonWSChar == '>' && isWhiteSpace(currentChar) && peekNextChar() == '>') if (previousNonWSChar == '>' && isWhiteSpace(currentChar) && peekNextChar() == '>')
@ -2673,6 +2689,7 @@ bool ASFormatter::getNextLine(bool emptyLineWasDeleted /*false*/)
&& (previousNonWSChar != '\\' && (previousNonWSChar != '\\'
|| isEmptyLine(currentLine))) || isEmptyLine(currentLine)))
{ {
isInPreprocessorInclude = false;
isInPreprocessor = false; isInPreprocessor = false;
isInPreprocessorDefineDef = false; isInPreprocessorDefineDef = false;
} }
@ -5652,7 +5669,11 @@ void ASFormatter::processPreprocessor()
for (int i = 0; i < addedPreproc; i++) for (int i = 0; i < addedPreproc; i++)
braceTypeStack->pop_back(); braceTypeStack->pop_back();
} }
} }
else if (currentLine.compare(preproc, 7, "include") == 0)
{
isInPreprocessorInclude = true;
}
else if (currentLine.compare(preproc, 6, "define") == 0) else if (currentLine.compare(preproc, 6, "define") == 0)
isInPreprocessorDefineDef = true; isInPreprocessorDefineDef = true;
} }

View File

@ -240,8 +240,8 @@ void ASResource::buildCastOperators(vector<const string*>* castOperators)
*/ */
void ASResource::buildHeaders(vector<const string*>* headers, int fileType, bool beautifier) void ASResource::buildHeaders(vector<const string*>* headers, int fileType, bool beautifier)
{ {
const size_t elements = 25; const size_t elements = 25;
headers->reserve(elements); headers->reserve(elements);
headers->emplace_back(&AS_IF); headers->emplace_back(&AS_IF);
headers->emplace_back(&AS_ELSE); headers->emplace_back(&AS_ELSE);
@ -262,7 +262,7 @@ void ASResource::buildHeaders(vector<const string*>* headers, int fileType, bool
{ {
headers->emplace_back(&_AS_TRY); // __try headers->emplace_back(&_AS_TRY); // __try
headers->emplace_back(&_AS_FINALLY); // __finally headers->emplace_back(&_AS_FINALLY); // __finally
headers->emplace_back(&_AS_EXCEPT); // __except headers->emplace_back(&_AS_EXCEPT); // __except
} }
if (fileType == JAVA_TYPE) if (fileType == JAVA_TYPE)
{ {
@ -295,7 +295,7 @@ void ASResource::buildHeaders(vector<const string*>* headers, int fileType, bool
} }
} }
assert(headers->size() < elements); assert(headers->size() < elements);
sort(headers->begin(), headers->end(), sortOnName); sort(headers->begin(), headers->end(), sortOnName);
} }

View File

@ -939,7 +939,8 @@ private: // variables
bool noTrimCommentContinuation; bool noTrimCommentContinuation;
bool isInPreprocessor; bool isInPreprocessor;
bool isInPreprocessorDefineDef; bool isInPreprocessorDefineDef;
bool isInPreprocessorBeautify; bool isInPreprocessorBeautify;
bool isInPreprocessorInclude;
bool isInTemplate; bool isInTemplate;
bool doesLineStartComment; bool doesLineStartComment;
bool lineEndsInCommentOnly; bool lineEndsInCommentOnly;