diff --git a/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp b/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp index 2cf4bd0f580..8cf228caf73 100644 --- a/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp +++ b/src/plugins/cppeditor/cppdocumentationcommenthelper.cpp @@ -33,11 +33,11 @@ #include "cppautocompleter.h" #include +#include #include #include #include -#include #include using namespace CppTools; @@ -53,13 +53,10 @@ bool isStartOfDoxygenComment(const QTextCursor &cursor) + document->characterAt(pos - 2) + document->characterAt(pos - 1); - if ((comment == QLatin1String("/**")) - || (comment == QLatin1String("/*!")) - || (comment == QLatin1String("///")) - || (comment == QLatin1String("//!"))) { - return true; - } - return false; + return comment == QLatin1String("/**") + || comment == QLatin1String("/*!") + || comment == QLatin1String("///") + || comment == QLatin1String("//!"); } DoxygenGenerator::DocumentationStyle doxygenStyle(const QTextCursor &cursor, @@ -119,7 +116,7 @@ bool isNextLineCppStyleComment(const QTextCursor &cursor) bool isCppStyleContinuation(const QTextCursor& cursor) { - return (isPreviousLineCppStyleComment(cursor) || isNextLineCppStyleComment(cursor)); + return isPreviousLineCppStyleComment(cursor) || isNextLineCppStyleComment(cursor); } /// Check if line is a CppStyle Doxygen comment and the cursor is after the comment @@ -139,7 +136,7 @@ bool isCursorAfterCppComment(const QTextCursor &cursor, const QTextDocument *doc return false; } -bool handleDoxygenCppStyleContinuation(QTextCursor &cursor, QKeyEvent *e) +bool handleDoxygenCppStyleContinuation(QTextCursor &cursor) { const int blockPos = cursor.positionInBlock(); const QString &text = cursor.block().text(); @@ -165,12 +162,10 @@ bool handleDoxygenCppStyleContinuation(QTextCursor &cursor, QKeyEvent *e) newLine.append(QLatin1Char(' ')); cursor.insertText(newLine); - e->accept(); return true; } bool handleDoxygenContinuation(QTextCursor &cursor, - QKeyEvent *e, const QTextDocument *doc, const bool enableDoxygen, const bool leadingAsterisks) @@ -180,7 +175,7 @@ bool handleDoxygenContinuation(QTextCursor &cursor, // 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); + return handleDoxygenCppStyleContinuation(cursor); if (!leadingAsterisks) return false; @@ -223,7 +218,6 @@ bool handleDoxygenContinuation(QTextCursor &cursor, newLine.append(QLatin1Char(' ')); } cursor.insertText(newLine); - e->accept(); return true; } } @@ -236,88 +230,67 @@ bool handleDoxygenContinuation(QTextCursor &cursor, namespace CppEditor { namespace Internal { -CppDocumentationCommentHelper::CppDocumentationCommentHelper( - TextEditor::TextEditorWidget *editorWidget) - : m_editorWidget(editorWidget) - , m_settings(CppToolsSettings::instance()->commentsSettings()) +bool trySplitComment(TextEditor::TextEditorWidget *editorWidget) { - connect(CppToolsSettings::instance(), - SIGNAL(commentsSettingsChanged(CppTools::CommentsSettings)), - this, - SLOT(onCommentsSettingsChanged(CppTools::CommentsSettings))); -} - -bool CppDocumentationCommentHelper::handleKeyPressEvent(QKeyEvent *e) const -{ - if (!m_settings.m_enableDoxygen && !m_settings.m_leadingAsterisks) + const CommentsSettings &settings = CppToolsSettings::instance()->commentsSettings(); + if (!settings.m_enableDoxygen && !settings.m_leadingAsterisks) return false; - if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) { - QTextCursor cursor = m_editorWidget->textCursor(); - if (!m_editorWidget->autoCompleter()->isInComment(cursor)) - return false; + QTextCursor cursor = editorWidget->textCursor(); + if (!editorWidget->autoCompleter()->isInComment(cursor)) + return false; - // We are interested on two particular cases: - // 1) The cursor is right after a /**, /*!, /// or ///! and the user pressed enter. - // If Doxygen is enabled we need to generate an entire comment block. - // 2) The cursor is already in the middle of a multi-line comment and the user pressed - // enter. If leading asterisk(s) is set we need to write a comment continuation - // with those. + // We are interested on two particular cases: + // 1) The cursor is right after a /**, /*!, /// or ///! and the user pressed enter. + // If Doxygen is enabled we need to generate an entire comment block. + // 2) The cursor is already in the middle of a multi-line comment and the user pressed + // enter. If leading asterisk(s) is set we need to write a comment continuation + // with those. - if (m_settings.m_enableDoxygen && cursor.positionInBlock() >= 3) { - const int pos = cursor.position(); - if (isStartOfDoxygenComment(cursor)) { - QTextDocument *textDocument = m_editorWidget->document(); - DoxygenGenerator::DocumentationStyle style = doxygenStyle(cursor, textDocument); - - // Check if we're already in a CppStyle Doxygen comment => continuation - // Needs special handling since CppStyle does not have start and end markers - if ((style == DoxygenGenerator::CppStyleA || style == DoxygenGenerator::CppStyleB) - && isCppStyleContinuation(cursor)) { - return handleDoxygenCppStyleContinuation(cursor, e); - } - - DoxygenGenerator doxygen; - doxygen.setStyle(style); - doxygen.setAddLeadingAsterisks(m_settings.m_leadingAsterisks); - doxygen.setGenerateBrief(m_settings.m_generateBrief); - doxygen.setStartComment(false); - - // Move until we reach any possibly meaningful content. - while (textDocument->characterAt(cursor.position()).isSpace() - && cursor.movePosition(QTextCursor::NextCharacter)) { - } - - if (!cursor.atEnd()) { - const QString &comment = doxygen.generate(cursor); - if (!comment.isEmpty()) { - cursor.beginEditBlock(); - cursor.setPosition(pos); - cursor.insertText(comment); - cursor.setPosition(pos - 3, QTextCursor::KeepAnchor); - m_editorWidget->textDocument()->autoIndent(cursor); - cursor.endEditBlock(); - e->accept(); - return true; - } - } + if (settings.m_enableDoxygen && cursor.positionInBlock() >= 3) { + const int pos = cursor.position(); + if (isStartOfDoxygenComment(cursor)) { + QTextDocument *textDocument = editorWidget->document(); + DoxygenGenerator::DocumentationStyle style = doxygenStyle(cursor, textDocument); + // Check if we're already in a CppStyle Doxygen comment => continuation + // Needs special handling since CppStyle does not have start and end markers + if ((style == DoxygenGenerator::CppStyleA || style == DoxygenGenerator::CppStyleB) + && isCppStyleContinuation(cursor)) { + return handleDoxygenCppStyleContinuation(cursor); } - } // right after first doxygen comment - return handleDoxygenContinuation(cursor, - e, - m_editorWidget->document(), - m_settings.m_enableDoxygen, - m_settings.m_leadingAsterisks); - } + DoxygenGenerator doxygen; + doxygen.setStyle(style); + doxygen.setAddLeadingAsterisks(settings.m_leadingAsterisks); + doxygen.setGenerateBrief(settings.m_generateBrief); + doxygen.setStartComment(false); - return false; -} + // Move until we reach any possibly meaningful content. + while (textDocument->characterAt(cursor.position()).isSpace() + && cursor.movePosition(QTextCursor::NextCharacter)) { + } -void CppDocumentationCommentHelper::onCommentsSettingsChanged(const CommentsSettings &settings) -{ - m_settings = settings; + if (!cursor.atEnd()) { + const QString &comment = doxygen.generate(cursor); + if (!comment.isEmpty()) { + cursor.beginEditBlock(); + cursor.setPosition(pos); + cursor.insertText(comment); + cursor.setPosition(pos - 3, QTextCursor::KeepAnchor); + editorWidget->textDocument()->autoIndent(cursor); + cursor.endEditBlock(); + return true; + } + } + + } + } // right after first doxygen comment + + return handleDoxygenContinuation(cursor, + editorWidget->document(), + settings.m_enableDoxygen, + settings.m_leadingAsterisks); } } // namespace Internal diff --git a/src/plugins/cppeditor/cppdocumentationcommenthelper.h b/src/plugins/cppeditor/cppdocumentationcommenthelper.h index faaf9ec55fe..99ff1c63883 100644 --- a/src/plugins/cppeditor/cppdocumentationcommenthelper.h +++ b/src/plugins/cppeditor/cppdocumentationcommenthelper.h @@ -31,35 +31,14 @@ #ifndef CPPDOCUMENTATIONCOMMENTHELPER_H #define CPPDOCUMENTATIONCOMMENTHELPER_H -#include - -#include - -QT_FORWARD_DECLARE_CLASS(QKeyEvent) +#include namespace TextEditor { class TextEditorWidget; } namespace CppEditor { namespace Internal { -class CppDocumentationCommentHelper : public QObject -{ - Q_OBJECT - Q_DISABLE_COPY(CppDocumentationCommentHelper) - -public: - explicit CppDocumentationCommentHelper(TextEditor::TextEditorWidget *editorWidget); - bool handleKeyPressEvent(QKeyEvent *e) const; - -private slots: - void onCommentsSettingsChanged(const CppTools::CommentsSettings &settings); - -private: - CppDocumentationCommentHelper(); - - TextEditor::TextEditorWidget *m_editorWidget; - CppTools::CommentsSettings m_settings; -}; +bool trySplitComment(TextEditor::TextEditorWidget *editorWidget); } // namespace Internal } // namespace CppEditor diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index 9c42200c7f4..54053240c5e 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -110,8 +110,6 @@ public: CppEditorDocument *m_cppEditorDocument; CppEditorOutline *m_cppEditorOutline; - CppDocumentationCommentHelper m_cppDocumentationCommentHelper; - QTimer m_updateFunctionDeclDefLinkTimer; CppLocalRenaming m_localRenaming; @@ -132,7 +130,6 @@ CppEditorWidgetPrivate::CppEditorWidgetPrivate(CppEditorWidget *q) : m_modelManager(CppModelManager::instance()) , m_cppEditorDocument(qobject_cast(q->textDocument())) , m_cppEditorOutline(new CppEditorOutline(q)) - , m_cppDocumentationCommentHelper(q) , m_localRenaming(q) , m_useSelectionsUpdater(q) , m_declDefLinkFinder(new FunctionDeclDefLinkFinder(q)) @@ -541,8 +538,12 @@ void CppEditorWidget::keyPressEvent(QKeyEvent *e) if (handleStringSplitting(e)) return; - if (d->m_cppDocumentationCommentHelper.handleKeyPressEvent(e)) - return; + if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) { + if (trySplitComment(this)) { + e->accept(); + return; + } + } TextEditorWidget::keyPressEvent(e); }