- enhancement: Code Completion now respect compiler set's language standard settings.

This commit is contained in:
Roy Qu 2023-01-19 11:08:32 +08:00
parent 8603fa10ee
commit 4456772fa7
7 changed files with 63 additions and 74 deletions

View File

@ -5,6 +5,7 @@ Red Panda C++ Version 2.10
- enhancement: Don't create temp backup for readonly files. - enhancement: Don't create temp backup for readonly files.
- enhancement: Add "Help"/"Submit Iusses". - enhancement: Add "Help"/"Submit Iusses".
- enhancement: Add "Help"/"Document" for Simplified Chinese users. - 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 Red Panda C++ Version 2.9

View File

@ -136,7 +136,7 @@ bool FileCompiler::prepareForCompile()
bool FileCompiler::prepareForRebuild() bool FileCompiler::prepareForRebuild()
{ {
QString exeName=compilerSet()->getCompileOptionValue(mFilename); QString exeName=compilerSet()->getOutputFilename(mFilename);
QFile file(exeName); QFile file(exeName);

View File

@ -220,6 +220,9 @@ public:
const PCppParser &parser() const; const PCppParser &parser() const;
void tab() override; void tab() override;
static PCppParser sharedParser(ParserLanguage language);
signals: signals:
void renamed(const QString& oldName, const QString& newName, bool firstSave); void renamed(const QString& oldName, const QString& newName, bool firstSave);
void fileSaved(const QString& filename, bool inProject); void fileSaved(const QString& filename, bool inProject);
@ -286,7 +289,6 @@ private:
void onExportedFormatToken(QSynedit::PSyntaxer syntaxer, int Line, int column, const QString& token, void onExportedFormatToken(QSynedit::PSyntaxer syntaxer, int Line, int column, const QString& token,
QSynedit::PTokenAttribute &attr); QSynedit::PTokenAttribute &attr);
void onScrollBarValueChanged(); void onScrollBarValueChanged();
static PCppParser sharedParser(ParserLanguage language);
private: private:
QDateTime mBackupTime; QDateTime mBackupTime;
QFile* mBackupFile; QFile* mBackupFile;

View File

@ -1571,14 +1571,19 @@ void MainWindow::changeOptions(const QString &widgetName, const QString &groupNa
return; return;
} }
Editor *e = mEditorList->getEditor(); if (mProject) {
if (mProject && !e) {
scanActiveProject(true); 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<ProjectUnit> unit) {
void MainWindow::reparseNonProjectEditors() void MainWindow::reparseNonProjectEditors()
{ {
if (pSettings->codeCompletion().shareParser()) {
bool hasC=false;
bool hasCpp=false;
for(int i=0;i<mEditorList->pageCount();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;i<mEditorList->pageCount();i++) { for (int i=0;i<mEditorList->pageCount();i++) {
Editor* e=(*mEditorList)[i]; Editor* e=(*mEditorList)[i];
if (!e->inProject()) { if (!e->inProject()) {

View File

@ -1677,8 +1677,6 @@ Settings::CompilerSet::CompilerSet(const QString& compilerFolder, const QString&
setUserInput(); setUserInput();
setDefines();
mFullLoaded = true; mFullLoaded = true;
} else { } else {
mFullLoaded = false; mFullLoaded = false;
@ -1707,8 +1705,6 @@ Settings::CompilerSet::CompilerSet(const Settings::CompilerSet &set):
mVersion(set.mVersion), mVersion(set.mVersion),
mType(set.mType), mType(set.mType),
mName(set.mName), mName(set.mName),
mCppDefines(set.mCppDefines),
mCDefines(set.mCDefines),
mTarget(set.mTarget), mTarget(set.mTarget),
mCompilerType(set.mCompilerType), mCompilerType(set.mCompilerType),
mCompilerSetType(set.mCompilerSetType), mCompilerSetType(set.mCompilerSetType),
@ -1994,7 +1990,6 @@ QStringList &Settings::CompilerSet::defaultCIncludeDirs()
if (!mFullLoaded && !binDirs().isEmpty()) { if (!mFullLoaded && !binDirs().isEmpty()) {
mFullLoaded=true; mFullLoaded=true;
setDirectories(binDirs()[0],mCompilerType); setDirectories(binDirs()[0],mCompilerType);
setDefines();
} }
return mDefaultCIncludeDirs; return mDefaultCIncludeDirs;
} }
@ -2004,7 +1999,6 @@ QStringList &Settings::CompilerSet::defaultCppIncludeDirs()
if (!mFullLoaded && !binDirs().isEmpty()) { if (!mFullLoaded && !binDirs().isEmpty()) {
mFullLoaded=true; mFullLoaded=true;
setDirectories(binDirs()[0],mCompilerType); setDirectories(binDirs()[0],mCompilerType);
setDefines();
} }
return mDefaultCppIncludeDirs; return mDefaultCppIncludeDirs;
} }
@ -2014,7 +2008,6 @@ QStringList &Settings::CompilerSet::defaultLibDirs()
if (!mFullLoaded && !binDirs().isEmpty()) { if (!mFullLoaded && !binDirs().isEmpty()) {
mFullLoaded=true; mFullLoaded=true;
setDirectories(binDirs()[0],mCompilerType); setDirectories(binDirs()[0],mCompilerType);
setDefines();
} }
return mLibDirs; return mLibDirs;
} }
@ -2059,27 +2052,6 @@ void Settings::CompilerSet::setName(const QString &value)
mName = 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 const QString &Settings::CompilerSet::target() const
{ {
return mTarget; 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 // get default defines
QStringList arguments; QStringList arguments;
arguments.append("-dM"); arguments.append("-dM");
arguments.append("-E"); arguments.append("-E");
arguments.append("-x"); arguments.append("-x");
QString key;
if (isCpp) {
arguments.append("c++"); arguments.append("c++");
arguments.append("-std=c++17"); 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); arguments.append(NULL_FILE);
qDebug()<<arguments;
QFileInfo ccompiler(mCCompiler); QFileInfo ccompiler(mCCompiler);
QByteArray output = getCompilerOutput(ccompiler.absolutePath(),ccompiler.fileName(),arguments); QByteArray output = getCompilerOutput(ccompiler.absolutePath(),ccompiler.fileName(),arguments);
// 'cpp.exe -dM -E -x c++ -std=c++17 NUL' // 'cpp.exe -dM -E -x c++ -std=c++17 NUL'
mCppDefines.clear(); QStringList result;
QList<QByteArray> lines = output.split('\n'); QList<QByteArray> lines = output.split('\n');
for (QByteArray& line:lines) { for (QByteArray& line:lines) {
QByteArray trimmedLine = line.trimmed(); QByteArray trimmedLine = line.trimmed();
if (!trimmedLine.isEmpty()) { if (!trimmedLine.isEmpty()) {
mCppDefines.append(trimmedLine); result.append(trimmedLine);
} }
} }
return result;
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);
}
}
} }
void Settings::CompilerSet::setExecutables() void Settings::CompilerSet::setExecutables()

View File

@ -1334,8 +1334,7 @@ public:
void setType(const QString& value); void setType(const QString& value);
const QString& name() const; const QString& name() const;
void setName(const QString& value); void setName(const QString& value);
const QStringList& CppDefines(); QStringList defines(bool isCpp);
const QStringList& CDefines();
const QString& target() const; const QString& target() const;
void setTarget(const QString& value); void setTarget(const QString& value);
@ -1393,7 +1392,6 @@ public:
private: private:
void setDirectories(const QString& binDir, CompilerType mCompilerType); void setDirectories(const QString& binDir, CompilerType mCompilerType);
//load hard defines //load hard defines
void setDefines();
void setExecutables(); void setExecutables();
void setUserInput(); void setUserInput();
@ -1426,8 +1424,6 @@ public:
QString mVersion; // "4.7.1" QString mVersion; // "4.7.1"
QString mType; // "TDM-GCC", "MinGW" QString mType; // "TDM-GCC", "MinGW"
QString mName; // "TDM-GCC 4.7.1 Release" 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' QString mTarget; // 'X86_64' / 'i686'
CompilerType mCompilerType; // 'Clang' / 'GCC' CompilerType mCompilerType; // 'Clang' / 'GCC'
CompilerSetType mCompilerSetType; // RELEASE/ DEBUG/ Profile CompilerSetType mCompilerSetType; // RELEASE/ DEBUG/ Profile

View File

@ -310,16 +310,9 @@ void resetCppParser(std::shared_ptr<CppParser> parser, int compilerSetIndex)
foreach (const QString& file,compilerSet->defaultCIncludeDirs()) { foreach (const QString& file,compilerSet->defaultCIncludeDirs()) {
parser->addIncludePath(file); parser->addIncludePath(file);
} }
//TODO: Add default include dirs last, just like gcc does
// Set defines // Set defines
if (parser->language()==ParserLanguage::C) { for (QString define:compilerSet->defines(parser->language()==ParserLanguage::CPlusPlus)) {
for (QString define:compilerSet->CDefines()) { parser->addHardDefineByLine(define);
parser->addHardDefineByLine(define); // predefined constants from -dM -E
}
} else {
for (QString define:compilerSet->CppDefines()) {
parser->addHardDefineByLine(define); // predefined constants from -dM -E
}
} }
// add a Red Pand C++ 's own macro // add a Red Pand C++ 's own macro
parser->addHardDefineByLine("#define EGE_FOR_AUTO_CODE_COMPLETETION_ONLY"); parser->addHardDefineByLine("#define EGE_FOR_AUTO_CODE_COMPLETETION_ONLY");