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

View File

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

View File

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

View File

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

View File

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

View File

@ -3926,7 +3926,7 @@ void Settings::CodeCompletion::doLoad()
statex.dwLength = sizeof (statex); statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&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; shouldShare = false;
} }
#endif #endif

View File

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