diff --git a/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp b/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp index 094a94c7732fbcef35b3059c7420c0fc8e849222..6403aa399c8e110015718846d2c7a02abe7875ef 100644 --- a/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp +++ b/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp @@ -122,8 +122,8 @@ bool isCppStyleContinuation(const QTextCursor& cursor) return (isPreviousLineCppStyleComment(cursor) || isNextLineCppStyleComment(cursor)); } -/// Check if line is a CppStyle Doxygen comment and the cursor is after the comment -bool isCursorAfterCppComment(const QTextCursor &cursor, const QTextDocument *doc) +bool lineStartsWithCppDoxygenCommentAndCursorIsAfter(const QTextCursor &cursor, + const QTextDocument *doc) { QTextCursor cursorFirstNonBlank(cursor); cursorFirstNonBlank.movePosition(QTextCursor::StartOfLine); @@ -139,6 +139,27 @@ bool isCursorAfterCppComment(const QTextCursor &cursor, const QTextDocument *doc return false; } +bool isCursorAfterNonNestedCppStyleComment(const QTextCursor &cursor, + TextEditor::TextEditorWidget *editorWidget) +{ + QTextDocument *document = editorWidget->document(); + QTextCursor cursorBeforeCppComment(cursor); + while (document->characterAt(cursorBeforeCppComment.position()) != QLatin1Char('/') + && cursorBeforeCppComment.movePosition(QTextCursor::PreviousCharacter)) { + } + + if (!cursorBeforeCppComment.movePosition(QTextCursor::PreviousCharacter)) + return false; + + if (document->characterAt(cursorBeforeCppComment.position()) != QLatin1Char('/')) + return false; + + if (!cursorBeforeCppComment.movePosition(QTextCursor::PreviousCharacter)) + return false; + + return !editorWidget->autoCompleter()->isInComment(cursorBeforeCppComment); +} + bool handleDoxygenCppStyleContinuation(QTextCursor &cursor, QKeyEvent *e) { const int blockPos = cursor.positionInBlock(); @@ -171,16 +192,23 @@ bool handleDoxygenCppStyleContinuation(QTextCursor &cursor, QKeyEvent *e) bool handleDoxygenContinuation(QTextCursor &cursor, QKeyEvent *e, - const QTextDocument *doc, + TextEditor::TextEditorWidget *editorWidget, const bool enableDoxygen, const bool leadingAsterisks) { + const QTextDocument *doc = editorWidget->document(); + // It might be a continuation if: // a) current line starts with /// or //! and cursor is positioned after the comment // b) current line is in the middle of a multi-line Qt or Java style comment - if (enableDoxygen && !cursor.atEnd() && isCursorAfterCppComment(cursor, doc)) - return handleDoxygenCppStyleContinuation(cursor, e); + if (!cursor.atEnd()) { + if (enableDoxygen && lineStartsWithCppDoxygenCommentAndCursorIsAfter(cursor, doc)) + return handleDoxygenCppStyleContinuation(cursor, e); + + if (isCursorAfterNonNestedCppStyleComment(cursor, editorWidget)) + return false; + } // We continue the comment if the cursor is after a comment's line asterisk and if // there's no asterisk immediately after the cursor (that would already be considered @@ -224,6 +252,14 @@ bool handleDoxygenContinuation(QTextCursor &cursor, else newLine.append(QLatin1String(" ")); } else { + // If '*' is not within a comment, skip. + QTextCursor cursorOnFirstNonWhiteSpace(cursor); + const int positionOnFirstNonWhiteSpace = cursor.position() - blockPos + offset; + cursorOnFirstNonWhiteSpace.setPosition(positionOnFirstNonWhiteSpace); + if (!editorWidget->autoCompleter()->isInComment(cursorOnFirstNonWhiteSpace)) + return false; + + // ...otherwise do the continuation int start = offset; while (offset < blockPos && currentLine.at(offset) == QLatin1Char('*')) ++offset; @@ -316,7 +352,7 @@ bool CppDocumentationCommentHelper::handleKeyPressEvent(QKeyEvent *e) const return handleDoxygenContinuation(cursor, e, - m_editorWidget->document(), + m_editorWidget, m_settings.m_enableDoxygen, m_settings.m_leadingAsterisks); } diff --git a/src/plugins/cppeditor/cppdoxygen_test.cpp b/src/plugins/cppeditor/cppdoxygen_test.cpp index 3cd9a13f3b9c6296448de4300ff54b82489f20c9..c412ab8ee9943070222b95cdf7275da7684e5ac3 100644 --- a/src/plugins/cppeditor/cppdoxygen_test.cpp +++ b/src/plugins/cppeditor/cppdoxygen_test.cpp @@ -268,6 +268,24 @@ void CppEditorPlugin::test_doxygen_comments_data() "void d(); ///\n" "\n" ); + + QTest::newRow("noContinuationForExpressionAndComment1") << _( + "bool preventFolding;\n" + "*foo //|\n" + ) << _( + "bool preventFolding;\n" + "*foo //\n" + "\n" + ); + + QTest::newRow("noContinuationForExpressionAndComment2") << _( + "bool preventFolding;\n" + "*foo /*|\n" + ) << _( + "bool preventFolding;\n" + "*foo /*\n" + " \n" + ); } void CppEditorPlugin::test_doxygen_comments()