From 4456772fa7a3561970401d037ea8ed286cfd425e Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Thu, 19 Jan 2023 11:08:32 +0800 Subject: [PATCH] - enhancement: Code Completion now respect compiler set's language standard settings. --- NEWS.md | 1 + RedPandaIDE/compiler/filecompiler.cpp | 2 +- RedPandaIDE/editor.h | 4 +- RedPandaIDE/mainwindow.cpp | 41 ++++++++++++--- RedPandaIDE/settings.cpp | 72 ++++++++------------------- RedPandaIDE/settings.h | 6 +-- RedPandaIDE/utils.cpp | 11 +--- 7 files changed, 63 insertions(+), 74 deletions(-) diff --git a/NEWS.md b/NEWS.md index a1980265..94c20fb5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,7 @@ Red Panda C++ Version 2.10 - enhancement: Don't create temp backup for readonly files. - enhancement: Add "Help"/"Submit Iusses". - enhancement: Add "Help"/"Document" for Simplified Chinese users. + - enhancement: Code Completion now respect compiler set's language standard settings. Red Panda C++ Version 2.9 diff --git a/RedPandaIDE/compiler/filecompiler.cpp b/RedPandaIDE/compiler/filecompiler.cpp index 846b33d5..f4a3b49c 100644 --- a/RedPandaIDE/compiler/filecompiler.cpp +++ b/RedPandaIDE/compiler/filecompiler.cpp @@ -136,7 +136,7 @@ bool FileCompiler::prepareForCompile() bool FileCompiler::prepareForRebuild() { - QString exeName=compilerSet()->getCompileOptionValue(mFilename); + QString exeName=compilerSet()->getOutputFilename(mFilename); QFile file(exeName); diff --git a/RedPandaIDE/editor.h b/RedPandaIDE/editor.h index 5ba2a989..5615657e 100644 --- a/RedPandaIDE/editor.h +++ b/RedPandaIDE/editor.h @@ -220,6 +220,9 @@ public: const PCppParser &parser() const; void tab() override; + + static PCppParser sharedParser(ParserLanguage language); + signals: void renamed(const QString& oldName, const QString& newName, bool firstSave); void fileSaved(const QString& filename, bool inProject); @@ -286,7 +289,6 @@ private: void onExportedFormatToken(QSynedit::PSyntaxer syntaxer, int Line, int column, const QString& token, QSynedit::PTokenAttribute &attr); void onScrollBarValueChanged(); - static PCppParser sharedParser(ParserLanguage language); private: QDateTime mBackupTime; QFile* mBackupFile; diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index cfb0f61b..962668c1 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -1571,14 +1571,19 @@ void MainWindow::changeOptions(const QString &widgetName, const QString &groupNa return; } - Editor *e = mEditorList->getEditor(); - if (mProject && !e) { + if (mProject) { scanActiveProject(true); - } else if (mProject && e && e->inProject()) { - scanActiveProject(true); - } else if (e) { - reparseNonProjectEditors(); } + reparseNonProjectEditors(); + +// Editor *e = mEditorList->getEditor(); +// if (mProject && !e) { +// scanActiveProject(true); +// } else if (mProject && e && e->inProject()) { +// scanActiveProject(true); +// } else if (e) { +// reparseNonProjectEditors(); +// } } @@ -7188,6 +7193,30 @@ void MainWindow::setProjectViewCurrentUnit(std::shared_ptr unit) { void MainWindow::reparseNonProjectEditors() { + if (pSettings->codeCompletion().shareParser()) { + bool hasC=false; + bool hasCpp=false; + for(int i=0;ipageCount();i++) { + Editor* e=(*mEditorList)[i]; + if (!e->inProject() && e->parser()) { + if (e->parser()->language()==ParserLanguage::C) { + hasC=true; + } else if (e->parser()->language()==ParserLanguage::CPlusPlus) { + hasCpp=true; + } + } + } + if (hasC) { + PCppParser parser{Editor::sharedParser(ParserLanguage::C)}; + if (parser) + resetCppParser(parser); + } + if (hasCpp) { + PCppParser parser{Editor::sharedParser(ParserLanguage::CPlusPlus)}; + if (parser) + resetCppParser(parser); + } + } for (int i=0;ipageCount();i++) { Editor* e=(*mEditorList)[i]; if (!e->inProject()) { diff --git a/RedPandaIDE/settings.cpp b/RedPandaIDE/settings.cpp index 1b5e22e1..fe04b214 100644 --- a/RedPandaIDE/settings.cpp +++ b/RedPandaIDE/settings.cpp @@ -1677,8 +1677,6 @@ Settings::CompilerSet::CompilerSet(const QString& compilerFolder, const QString& setUserInput(); - setDefines(); - mFullLoaded = true; } else { mFullLoaded = false; @@ -1707,8 +1705,6 @@ Settings::CompilerSet::CompilerSet(const Settings::CompilerSet &set): mVersion(set.mVersion), mType(set.mType), mName(set.mName), - mCppDefines(set.mCppDefines), - mCDefines(set.mCDefines), mTarget(set.mTarget), mCompilerType(set.mCompilerType), mCompilerSetType(set.mCompilerSetType), @@ -1994,7 +1990,6 @@ QStringList &Settings::CompilerSet::defaultCIncludeDirs() if (!mFullLoaded && !binDirs().isEmpty()) { mFullLoaded=true; setDirectories(binDirs()[0],mCompilerType); - setDefines(); } return mDefaultCIncludeDirs; } @@ -2004,7 +1999,6 @@ QStringList &Settings::CompilerSet::defaultCppIncludeDirs() if (!mFullLoaded && !binDirs().isEmpty()) { mFullLoaded=true; setDirectories(binDirs()[0],mCompilerType); - setDefines(); } return mDefaultCppIncludeDirs; } @@ -2014,7 +2008,6 @@ QStringList &Settings::CompilerSet::defaultLibDirs() if (!mFullLoaded && !binDirs().isEmpty()) { mFullLoaded=true; setDirectories(binDirs()[0],mCompilerType); - setDefines(); } return mLibDirs; } @@ -2059,27 +2052,6 @@ void Settings::CompilerSet::setName(const QString &value) mName = value; } -const QStringList& Settings::CompilerSet::CDefines() -{ - if (!mFullLoaded && !binDirs().isEmpty()) { - mFullLoaded=true; - setDirectories(binDirs()[0],mCompilerType); - setDefines(); - } - return mCDefines; -} - -const QStringList &Settings::CompilerSet::CppDefines() -{ - if (!mFullLoaded && !binDirs().isEmpty()) { - mFullLoaded=true; - setDirectories(binDirs()[0],mCompilerType); - setDefines(); - } - return mCppDefines; - -} - const QString &Settings::CompilerSet::target() const { return mTarget; @@ -2290,46 +2262,42 @@ void Settings::CompilerSet::setProperties(const QString& binDir, const QString& } } -void Settings::CompilerSet::setDefines() { +QStringList Settings::CompilerSet::defines(bool isCpp) { // get default defines QStringList arguments; arguments.append("-dM"); arguments.append("-E"); arguments.append("-x"); - arguments.append("c++"); - arguments.append("-std=c++17"); + QString key; + if (isCpp) { + arguments.append("c++"); + key=CC_CMD_OPT_STD; + } else { + arguments.append("c"); + key=C_CMD_OPT_STD; + } + //language standard + PCompilerOption pOption = CompilerInfoManager::getCompilerOption(compilerType(), key); + if (pOption) { + if (!mCompileOptions[key].isEmpty()) + arguments.append(pOption->setting + mCompileOptions[key]); + } + arguments.append(NULL_FILE); + qDebug()< lines = output.split('\n'); for (QByteArray& line:lines) { QByteArray trimmedLine = line.trimmed(); if (!trimmedLine.isEmpty()) { - mCppDefines.append(trimmedLine); + result.append(trimmedLine); } } - - arguments.clear(); - arguments.append("-dM"); - arguments.append("-E"); - arguments.append("-x"); - arguments.append("c"); - arguments.append(NULL_FILE); - output = getCompilerOutput(ccompiler.absolutePath(),ccompiler.fileName(),arguments); - // 'cpp.exe -dM -E -x c NUL' - - mCDefines.clear(); - lines = output.split('\n'); - for (QByteArray& line:lines) { - QByteArray trimmedLine = line.trimmed(); - if (!trimmedLine.isEmpty()) { - mCDefines.append(trimmedLine); - } - } - + return result; } void Settings::CompilerSet::setExecutables() diff --git a/RedPandaIDE/settings.h b/RedPandaIDE/settings.h index a29f7d47..b4c7a987 100644 --- a/RedPandaIDE/settings.h +++ b/RedPandaIDE/settings.h @@ -1334,8 +1334,7 @@ public: void setType(const QString& value); const QString& name() const; void setName(const QString& value); - const QStringList& CppDefines(); - const QStringList& CDefines(); + QStringList defines(bool isCpp); const QString& target() const; void setTarget(const QString& value); @@ -1393,7 +1392,6 @@ public: private: void setDirectories(const QString& binDir, CompilerType mCompilerType); //load hard defines - void setDefines(); void setExecutables(); void setUserInput(); @@ -1426,8 +1424,6 @@ public: QString mVersion; // "4.7.1" QString mType; // "TDM-GCC", "MinGW" QString mName; // "TDM-GCC 4.7.1 Release" - QStringList mCppDefines; // list of predefined constants - QStringList mCDefines; // list of predefined constants QString mTarget; // 'X86_64' / 'i686' CompilerType mCompilerType; // 'Clang' / 'GCC' CompilerSetType mCompilerSetType; // RELEASE/ DEBUG/ Profile diff --git a/RedPandaIDE/utils.cpp b/RedPandaIDE/utils.cpp index 0dc15dd3..6ea19f98 100644 --- a/RedPandaIDE/utils.cpp +++ b/RedPandaIDE/utils.cpp @@ -310,16 +310,9 @@ void resetCppParser(std::shared_ptr parser, int compilerSetIndex) foreach (const QString& file,compilerSet->defaultCIncludeDirs()) { parser->addIncludePath(file); } - //TODO: Add default include dirs last, just like gcc does // Set defines - if (parser->language()==ParserLanguage::C) { - for (QString define:compilerSet->CDefines()) { - parser->addHardDefineByLine(define); // predefined constants from -dM -E - } - } else { - for (QString define:compilerSet->CppDefines()) { - parser->addHardDefineByLine(define); // predefined constants from -dM -E - } + for (QString define:compilerSet->defines(parser->language()==ParserLanguage::CPlusPlus)) { + parser->addHardDefineByLine(define); } // add a Red Pand C++ 's own macro parser->addHardDefineByLine("#define EGE_FOR_AUTO_CODE_COMPLETETION_ONLY");