From 4eec64939e2e3103158c587710eac712f40d30de Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Sun, 6 Nov 2022 09:43:28 +0800 Subject: [PATCH] - fix: Edting / show context menu when code analysis is turned on may crash app. - fix: Show context menu when edting non c/c++ file may crash app. --- NEWS.md | 2 ++ RedPandaIDE/editor.cpp | 38 +++++++++++++++++++++++--------------- RedPandaIDE/mainwindow.cpp | 18 +++++++++++------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/NEWS.md b/NEWS.md index 12aef19c..280564d8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,8 @@ Red Panda C++ Version 2.2 - fix: Pressing '*' at begin of line will crash app - enhancement: switch header/source in editor's context menu - enhancement: base class dropdown list in new class dialog now works + - fix: Edting / show context menu when code analysis is turned on may crash app. + - fix: Show context menu when edting non c/c++ file may crash app. Red Panda C++ Version 2.1 diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index dbfd03e2..e79f03bd 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -915,7 +915,7 @@ void Editor::onPreparePaintHighlightToken(int line, int aChar, const QString &to if (token.isEmpty()) return; - if (mParser && mParser->enabled() && highlighter()) { + if (mParser && highlighter()) { QString lineText = document()->getString(line-1); if (mParser->isIncludeLine(lineText)) { if (cursor() == Qt::PointingHandCursor) { @@ -932,7 +932,7 @@ void Editor::onPreparePaintHighlightToken(int line, int aChar, const QString &to } } } - } else if (attr == highlighter()->identifierAttribute()) { + } else if (mParser->enabled() && attr == highlighter()->identifierAttribute()) { QSynedit::BufferCoord p{aChar,line}; // BufferCoord pBeginPos,pEndPos; // QString s= getWordAtPosition(this,p, pBeginPos,pEndPos, WordPurpose::wpInformation); @@ -1057,18 +1057,19 @@ bool Editor::event(QEvent *event) switch (reason) { case TipType::Preprocessor: // When hovering above a preprocessor line, determine if we want to show an include or a identifier hint - s = document()->getString(p.line - 1); - isIncludeNextLine = mParser->isIncludeNextLine(s); - if (!isIncludeNextLine) - isIncludeLine = mParser->isIncludeLine(s); - if (!isIncludeNextLine &&!isIncludeLine) - s = wordAtRowCol(p); + if (mParser) { + s = document()->getString(p.line - 1); + isIncludeNextLine = mParser->isIncludeNextLine(s); + if (!isIncludeNextLine) + isIncludeLine = mParser->isIncludeLine(s); + if (!isIncludeNextLine &&!isIncludeLine) + s = wordAtRowCol(p); + } break; case TipType::Identifier: if (pMainWindow->debugger()->executing() && !pMainWindow->debugger()->inferiorRunning()) s = getWordAtPosition(this,p, pBeginPos,pEndPos, WordPurpose::wpEvaluation); // debugging - else if (//devEditor.ParserHints and - !mCompletionPopup->isVisible() + else if (!mCompletionPopup->isVisible() && !mHeaderCompletionPopup->isVisible()) { expression = getExpressionAtPosition(p); s = expression.join(""); // information during coding @@ -1090,7 +1091,9 @@ bool Editor::event(QEvent *event) s = s.trimmed(); if ((s == mCurrentWord) && (mCurrentTipType == reason)) { - if (qApp->queryKeyboardModifiers() == Qt::ControlModifier) { + if (mParser + && mParser->enabled() + && qApp->queryKeyboardModifiers() == Qt::ControlModifier) { if (!hasFocus()) activate(); setCursor(Qt::PointingHandCursor); @@ -1148,7 +1151,9 @@ bool Editor::event(QEvent *event) if (!hint.isEmpty()) { // QApplication* app = dynamic_cast(QApplication::instance()); // if (app->keyboardModifiers().testFlag(Qt::ControlModifier)) { - if (qApp->queryKeyboardModifiers() == Qt::ControlModifier) { + if (mParser + && mParser->enabled() + && qApp->queryKeyboardModifiers() == Qt::ControlModifier) { if (!hasFocus()) activate(); setCursor(Qt::PointingHandCursor); @@ -1201,7 +1206,7 @@ void Editor::mouseReleaseEvent(QMouseEvent *event) && (event->button() == Qt::LeftButton)) { QSynedit::BufferCoord p; - if (pointToCharLine(event->pos(),p)) { + if (mParser && pointToCharLine(event->pos(),p)) { QString s = document()->getString(p.line - 1); if (mParser->isIncludeNextLine(s)) { QString filename = mParser->getHeaderFileName(mFilename,s, true); @@ -1211,7 +1216,7 @@ void Editor::mouseReleaseEvent(QMouseEvent *event) QString filename = mParser->getHeaderFileName(mFilename,s); pMainWindow->openFile(filename); return; - } else { + } else if (mParser->enabled()) { gotoDefinition(p); return; } @@ -3457,7 +3462,7 @@ bool Editor::onCompletionInputMethod(QInputMethodEvent *event) Editor::TipType Editor::getTipType(QPoint point, QSynedit::BufferCoord& pos) { // Only allow in the text area... - if (pointToCharLine(point, pos)) { + if (pointToCharLine(point, pos) && highlighter()) { if (!pMainWindow->debugger()->executing() && getSyntaxIssueAtPosition(pos)) { return TipType::Error; @@ -3628,6 +3633,9 @@ void Editor::updateFunctionTip(bool showTip) if (!highlighter()) return; + if (!mParser || !mParser->enabled()) + return; + bool isFunction = false; auto action = finally([&isFunction]{ if (!isFunction) diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index e0cd08bc..efbbfeb4 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -4424,10 +4424,12 @@ void MainWindow::onEditorContextMenu(const QPoint& pos) //mouse on editing area menu.addAction(ui->actionCompile_Run); menu.addAction(ui->actionDebug); - menu.addSeparator(); - menu.addAction(ui->actionGoto_Declaration); - menu.addAction(ui->actionGoto_Definition); - menu.addAction(ui->actionFind_references); + if (editor->parser() && editor->parser()->enabled()) { + menu.addSeparator(); + menu.addAction(ui->actionGoto_Declaration); + menu.addAction(ui->actionGoto_Definition); + menu.addAction(ui->actionFind_references); + } menu.addSeparator(); menu.addAction(ui->actionOpen_Containing_Folder); @@ -4454,9 +4456,11 @@ void MainWindow::onEditorContextMenu(const QPoint& pos) menu.addAction(ui->actionFile_Properties); //these actions needs parser - ui->actionGoto_Declaration->setEnabled(!editor->parser()->parsing()); - ui->actionGoto_Definition->setEnabled(!editor->parser()->parsing()); - ui->actionFind_references->setEnabled(!editor->parser()->parsing()); + if (editor->parser() && editor->parser()->enabled()) { + ui->actionGoto_Declaration->setEnabled(!editor->parser()->parsing()); + ui->actionGoto_Definition->setEnabled(!editor->parser()->parsing()); + ui->actionFind_references->setEnabled(!editor->parser()->parsing()); + } } else { //mouse on gutter