C/C++ use differnet parser

This commit is contained in:
Roy Qu 2022-10-28 09:47:34 +08:00
parent 833be397b0
commit 2ce732cfa5
8 changed files with 49 additions and 35 deletions

View File

@ -1,7 +1,7 @@
Red Panda C++ Version 2.1
- fix: editors that not in the editing panel shouldn't trigger switch breakpoint
- fix:editors that not in the editing panel shouldn't show context menu
- fix: editors that not in the editing panel shouldn't show context menu
- enhancement: add "editors share one code parser" in "options" / "editor" / "code completion", to reduce memory usage.
Turned off by default on PCs with memory > 4G; Force turned on PCs with memory < 1G.
- enhancement: add "goto block start"/"goto block end" in "Code" menu

View File

@ -69,7 +69,7 @@ const char* SaveException::what() const noexcept {
return mReasonBuffer;
}
std::weak_ptr<CppParser> Editor::mSharedParser;
QHash<ParserLanguage,std::weak_ptr<CppParser>> Editor::mSharedParsers;
Editor::Editor(QWidget *parent):
Editor(parent,QObject::tr("untitled"),ENCODING_AUTO_DETECT,nullptr,true,nullptr)
@ -2584,7 +2584,7 @@ void Editor::initParser()
if (pSettings->codeCompletion().enabled()
&& (highlighter() && highlighter()->getClass() == QSynedit::HighlighterClass::CppHighlighter)
) {
mParser = sharedParser();
mParser = sharedParser(mUseCppSyntax?ParserLanguage::CPlusPlus:ParserLanguage::C);
}
return;
}
@ -2743,12 +2743,19 @@ void Editor::reparse(bool resetParser)
return;
//mParser->setEnabled(pSettings->codeCompletion().enabled());
ParserLanguage language = mUseCppSyntax?ParserLanguage::CPlusPlus:ParserLanguage::C;
if (!inProject() && !pSettings->codeCompletion().shareParser()) {
if (language!=mParser->language()) {
mParser->setLanguage(language);
resetCppParser(mParser);
} else if (resetParser) {
resetCppParser(mParser);
if (!inProject()) {
if (pSettings->codeCompletion().shareParser()) {
if (language!=mParser->language()) {
mParser->invalidateFile(mFilename);
mParser=sharedParser(language);
}
} else {
if (language!=mParser->language()) {
mParser->setLanguage(language);
resetCppParser(mParser);
} else if (resetParser) {
resetCppParser(mParser);
}
}
}
parseFile(mParser,mFilename, inProject());
@ -3942,19 +3949,22 @@ void Editor::onScrollBarValueChanged()
pMainWindow->functionTip()->hide();
}
PCppParser Editor::sharedParser()
PCppParser Editor::sharedParser(ParserLanguage language)
{
PCppParser parser=mSharedParser.lock();
PCppParser parser;
if (mSharedParsers.contains(language)) {
std::weak_ptr<CppParser> weakParser=mSharedParsers[language].lock();
}
if (!parser) {
parser=std::make_shared<CppParser>();
parser->setLanguage(ParserLanguage::CPlusPlus);
parser->setLanguage(language);
parser->setOnGetFileStream(
std::bind(
&EditorList::getContentFromOpenedEditor,pMainWindow->editorList(),
std::placeholders::_1, std::placeholders::_2));
resetCppParser(parser);
parser->setEnabled(true);
mSharedParser=parser;
mSharedParsers.insert(language,parser);
}
return parser;
}

View File

@ -292,7 +292,7 @@ private:
void onExportedFormatToken(QSynedit::PHighlighter syntaxHighlighter, int Line, int column, const QString& token,
QSynedit::PHighlighterAttribute &attr);
void onScrollBarValueChanged();
static PCppParser sharedParser();
static PCppParser sharedParser(ParserLanguage language);
private:
QByteArray mEncodingOption; // the encoding type set by the user
QByteArray mFileEncoding; // the real encoding of the file (auto detected)
@ -343,7 +343,7 @@ private:
QTimer mFunctionTipTimer;
int mHoverModifiedLine;
static std::weak_ptr<CppParser> mSharedParser;
static QHash<ParserLanguage,std::weak_ptr<CppParser>> mSharedParsers;
// QWidget interface
protected:

View File

@ -201,6 +201,11 @@ bool EditorList::closeEditor(Editor* editor, bool transferFocus, bool force) {
} else {
pMainWindow->updateClassBrowserForEditor(nullptr);
}
} else {
editor = getEditor();
if (!editor) {
pMainWindow->updateClassBrowserForEditor(nullptr);
}
}
emit editorClosed();
endUpdate();

View File

@ -1188,6 +1188,9 @@ PStatement CppParser::addStatement(const PStatement& parent,
newType += newCommand.front();
newCommand.remove(0,1); // remove first
}
// if (newCommand.startsWith("::") && parent && kind!=StatementKind::skBlock ) {
// qDebug()<<command<<fileName<<line<<kind<<parent->fullName;
// }
QString noNameArgs = "";
if (kind == StatementKind::skConstructor
@ -3334,13 +3337,14 @@ void CppParser::internalParse(const QString &fileName)
if (!handleStatement())
break;
}
//reduce memory usage
internalClear();
#ifdef QT_DEBUG
// mTokenizer.dumpTokens("r:\\tokens.txt");
// mTokenizer.dumpTokens(QString("r:\\tokens-%1.txt").arg(extractFileName(fileName)));
//
// mStatementList.dumpAll("r:\\all-stats.txt");
#endif
//reduce memory usage
internalClear();
}
}

View File

@ -24,7 +24,7 @@
using GetFileStreamCallBack = std::function<bool (const QString&, QStringList&)>;
enum class ParserLanguage {
enum ParserLanguage {
C,
CPlusPlus
};

View File

@ -3926,7 +3926,7 @@ void Settings::CodeCompletion::doLoad()
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
if (statex.ullAvailPhys > (long long int)4*1024*1024*1024) {
if (statex.ullAvailPhys > (long long int)8*1024*1024*1024) {
shouldShare = false;
}
#endif

View File

@ -181,14 +181,9 @@ void CodeCompletionPopup::addChildren(PStatement scopeStatement, const QString &
if (childStatement->fileName.isEmpty()) {
// hard defines
addStatement(childStatement,fileName,-1);
} else if (!( childStatement->kind == StatementKind::skConstructor
|| childStatement->kind == StatementKind::skDestructor
|| childStatement->kind == StatementKind::skBlock)
&& (!mAddedStatements.contains(childStatement->command))
&& (
isIncluded(childStatement->fileName)
|| isIncluded(childStatement->definitionFileName)
)
} else if (
isIncluded(childStatement->fileName)
|| isIncluded(childStatement->definitionFileName)
) {
//we must check if the statement is included by the file
addStatement(childStatement,fileName,line);
@ -196,11 +191,7 @@ void CodeCompletionPopup::addChildren(PStatement scopeStatement, const QString &
}
} else {
for (const PStatement& childStatement: children) {
if (!( childStatement->kind == StatementKind::skConstructor
|| childStatement->kind == StatementKind::skDestructor
|| childStatement->kind == StatementKind::skBlock)
&& (!mAddedStatements.contains(childStatement->command)))
addStatement(childStatement,fileName,line);
addStatement(childStatement,fileName,line);
}
}
}
@ -209,6 +200,10 @@ void CodeCompletionPopup::addStatement(PStatement statement, const QString &file
{
if (mAddedStatements.contains(statement->command))
return;
if (statement->kind == StatementKind::skConstructor
|| statement->kind == StatementKind::skDestructor
|| statement->kind == StatementKind::skBlock)
return;
if ((line!=-1)
&& (line < statement->line)
&& (fileName == statement->fileName))
@ -409,9 +404,9 @@ void CodeCompletionPopup::filterList(const QString &member)
int totalPos = 0;
statement->matchPositions.clear();
if (hideSymbolsTwoUnderline && statement->command.startsWith("__")) {
continue;
} else if (hideSymbolsUnderline && statement->command.startsWith("_")) {
continue;
} else {
foreach (const QChar& ch, member) {
if (mIgnoreCase)