From 2ce732cfa5e31a1343d29da3567d0f8f72025d55 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Fri, 28 Oct 2022 09:47:34 +0800 Subject: [PATCH] C/C++ use differnet parser --- NEWS.md | 2 +- RedPandaIDE/editor.cpp | 34 +++++++++++++-------- RedPandaIDE/editor.h | 4 +-- RedPandaIDE/editorlist.cpp | 5 +++ RedPandaIDE/parser/cppparser.cpp | 10 ++++-- RedPandaIDE/parser/parserutils.h | 2 +- RedPandaIDE/settings.cpp | 2 +- RedPandaIDE/widgets/codecompletionpopup.cpp | 25 ++++++--------- 8 files changed, 49 insertions(+), 35 deletions(-) diff --git a/NEWS.md b/NEWS.md index a0c85c49..32c97169 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index f6787bac..aece95f1 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -69,7 +69,7 @@ const char* SaveException::what() const noexcept { return mReasonBuffer; } -std::weak_ptr Editor::mSharedParser; +QHash> 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 weakParser=mSharedParsers[language].lock(); + } if (!parser) { parser=std::make_shared(); - 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; } diff --git a/RedPandaIDE/editor.h b/RedPandaIDE/editor.h index b0d94f46..05f525f0 100644 --- a/RedPandaIDE/editor.h +++ b/RedPandaIDE/editor.h @@ -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 mSharedParser; + static QHash> mSharedParsers; // QWidget interface protected: diff --git a/RedPandaIDE/editorlist.cpp b/RedPandaIDE/editorlist.cpp index 6a190a70..99c71654 100644 --- a/RedPandaIDE/editorlist.cpp +++ b/RedPandaIDE/editorlist.cpp @@ -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(); diff --git a/RedPandaIDE/parser/cppparser.cpp b/RedPandaIDE/parser/cppparser.cpp index c2fb2592..2c56df50 100644 --- a/RedPandaIDE/parser/cppparser.cpp +++ b/RedPandaIDE/parser/cppparser.cpp @@ -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()<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(); + } } diff --git a/RedPandaIDE/parser/parserutils.h b/RedPandaIDE/parser/parserutils.h index 70766bfe..77adaa86 100644 --- a/RedPandaIDE/parser/parserutils.h +++ b/RedPandaIDE/parser/parserutils.h @@ -24,7 +24,7 @@ using GetFileStreamCallBack = std::function; -enum class ParserLanguage { +enum ParserLanguage { C, CPlusPlus }; diff --git a/RedPandaIDE/settings.cpp b/RedPandaIDE/settings.cpp index 6f7cab25..6bade520 100644 --- a/RedPandaIDE/settings.cpp +++ b/RedPandaIDE/settings.cpp @@ -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 diff --git a/RedPandaIDE/widgets/codecompletionpopup.cpp b/RedPandaIDE/widgets/codecompletionpopup.cpp index 246c20b2..07bd62c2 100644 --- a/RedPandaIDE/widgets/codecompletionpopup.cpp +++ b/RedPandaIDE/widgets/codecompletionpopup.cpp @@ -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)