- enhancement: don't show cpp defines when editing c files

This commit is contained in:
Roy Qu 2022-07-28 13:51:38 +08:00
parent 7d2a8b7dde
commit c7e8ee6072
9 changed files with 82 additions and 11 deletions

View File

@ -2,6 +2,7 @@ Red Panda C++ Version 1.1.6
- fix: block indent doesn't work
- fix: selection is not correctly set after input in column mode
- enhancement: don't show cpp defines when editing c files
Red Panda C++ Version 1.1.5

View File

@ -344,8 +344,9 @@ bool Editor::saveAs(const QString &name, bool fromProject){
clearSyntaxIssues();
pMainWindow->fileSystemWatcher()->removePath(mFilename);
if (pSettings->codeCompletion().enabled() && mParser)
if (pSettings->codeCompletion().enabled() && mParser) {
mParser->invalidateFile(mFilename);
}
try {
mFilename = newName;
saveFile(mFilename);
@ -2527,6 +2528,11 @@ bool Editor::handleCodeCompletion(QChar key)
void Editor::initParser()
{
mParser = std::make_shared<CppParser>();
if (mUseCppSyntax) {
mParser->setLanguage(ParserLanguage::CPlusPlus);
} else {
mParser->setLanguage(ParserLanguage::C);
}
mParser->setOnGetFileStream(
std::bind(
&EditorList::getContentFromOpenedEditor,pMainWindow->editorList(),
@ -2667,6 +2673,11 @@ void Editor::reparse()
return;
if (mParser)
mParser->setEnabled(pSettings->codeCompletion().enabled());
ParserLanguage language = mUseCppSyntax?ParserLanguage::CPlusPlus:ParserLanguage::C;
if (language!=mParser->language() && !mInProject) {
mParser->setLanguage(language);
resetCppParser(mParser);
}
parseFile(mParser,mFilename,mInProject);
}

View File

@ -31,6 +31,7 @@ CppParser::CppParser(QObject *parent) : QObject(parent),
mMutex(QMutex::Recursive)
{
mParserId = cppParserCount.fetchAndAddRelaxed(1);
mLanguage = ParserLanguage::CPlusPlus;
mSerialCount = 0;
updateSerialId();
mUniqId = 0;
@ -4644,6 +4645,16 @@ void CppParser::updateSerialId()
mSerialId = QString("%1 %2").arg(mParserId).arg(mSerialCount);
}
ParserLanguage CppParser::language() const
{
return mLanguage;
}
void CppParser::setLanguage(ParserLanguage newLanguage)
{
mLanguage = newLanguage;
}
const StatementModel &CppParser::statementList() const

View File

@ -137,6 +137,9 @@ public:
const StatementModel &statementList() const;
ParserLanguage language() const;
void setLanguage(ParserLanguage newLanguage);
signals:
void onProgress(const QString& fileName, int total, int current);
void onBusy();
@ -507,6 +510,7 @@ private:
private:
int mParserId;
ParserLanguage mLanguage;
int mSerialCount;
QString mSerialId;
int mUniqId;

View File

@ -1295,7 +1295,7 @@ QString CppPreprocessor::expandDefines(QString line)
} else {
//none braced argument (next word)
defineStart = tail;
if ((tail>=line.length()) || !isMacroIdentChar(line[searchPos])) {
if ((tail>=line.length()) || !isMacroIdentChar(line[defineStart])) {
line = ""; // broken line
break;
}

View File

@ -22,6 +22,11 @@
#include <QVector>
#include <memory>
enum class ParserLanguage {
C,
CPlusPlus
};
struct CodeSnippet {
QString caption; //Name
QString prefix; //Prefix used in code suggestion

View File

@ -1518,7 +1518,8 @@ Settings::CompilerSet::CompilerSet(const Settings::CompilerSet &set):
mVersion(set.mVersion),
mType(set.mType),
mName(set.mName),
mDefines(set.mDefines),
mCppDefines(set.mCppDefines),
mCDefines(set.mCDefines),
mTarget(set.mTarget),
mCompilerType(set.mCompilerType),
mCompilerSetType(set.mCompilerSetType),
@ -1854,14 +1855,25 @@ void Settings::CompilerSet::setName(const QString &value)
mName = value;
}
const QStringList& Settings::CompilerSet::defines()
const QStringList& Settings::CompilerSet::CDefines()
{
if (!mFullLoaded && !binDirs().isEmpty()) {
mFullLoaded=true;
setDirectories(binDirs()[0],mCompilerType);
setDefines();
}
return mDefines;
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
@ -2079,14 +2091,33 @@ void Settings::CompilerSet::setDefines() {
QByteArray output = getCompilerOutput(ccompiler.absolutePath(),ccompiler.fileName(),arguments);
// 'cpp.exe -dM -E -x c++ -std=c++17 NUL'
mDefines.clear();
mCppDefines.clear();
QList<QByteArray> lines = output.split('\n');
for (QByteArray& line:lines) {
QByteArray trimmedLine = line.trimmed();
if (!trimmedLine.isEmpty()) {
mDefines.append(trimmedLine);
mCppDefines.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);
}
}
}
void Settings::CompilerSet::setExecutables()

View File

@ -1223,7 +1223,8 @@ public:
void setType(const QString& value);
const QString& name() const;
void setName(const QString& value);
const QStringList& defines();
const QStringList& CppDefines();
const QStringList& CDefines();
const QString& target() const;
void setTarget(const QString& value);
@ -1295,7 +1296,8 @@ public:
QString mVersion; // "4.7.1"
QString mType; // "TDM-GCC", "MinGW"
QString mName; // "TDM-GCC 4.7.1 Release"
QStringList mDefines; // list of predefined constants
QStringList mCppDefines; // list of predefined constants
QStringList mCDefines; // list of predefined constants
QString mTarget; // 'X86_64' / 'i686'
QString mCompilerType; // 'Clang' / 'GCC'
int mCompilerSetType; // RELEASE/ DEBUG/ Profile

View File

@ -653,8 +653,14 @@ void resetCppParser(std::shared_ptr<CppParser> parser, int compilerSetIndex)
}
//TODO: Add default include dirs last, just like gcc does
// Set defines
for (QString define:compilerSet->defines()) {
parser->addHardDefineByLine(define); // predefined constants from -dM -E
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
}
}
// add a Red Pand C++ 's own macro
parser->addHardDefineByLine("#define EGE_FOR_AUTO_CODE_COMPLETETION_ONLY");