CppEditor: Simplify handling of comment splitting

Change-Id: I9a6e4962b52321313bb80cc3c3a77fac80aa0536
Reviewed-by: Christian Stenger <christian.stenger@digia.com>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com>
This commit is contained in:
hjk
2014-10-15 09:24:09 +02:00
parent 96af4e3c8e
commit 0e53554dfb
3 changed files with 67 additions and 114 deletions

View File

@@ -33,11 +33,11 @@
#include "cppautocompleter.h" #include "cppautocompleter.h"
#include <cpptools/cpptoolssettings.h> #include <cpptools/cpptoolssettings.h>
#include <cpptools/commentssettings.h>
#include <cpptools/doxygengenerator.h> #include <cpptools/doxygengenerator.h>
#include <texteditor/texteditor.h> #include <texteditor/texteditor.h>
#include <QDebug> #include <QDebug>
#include <QKeyEvent>
#include <QTextBlock> #include <QTextBlock>
using namespace CppTools; using namespace CppTools;
@@ -53,13 +53,10 @@ bool isStartOfDoxygenComment(const QTextCursor &cursor)
+ document->characterAt(pos - 2) + document->characterAt(pos - 2)
+ document->characterAt(pos - 1); + document->characterAt(pos - 1);
if ((comment == QLatin1String("/**")) return comment == QLatin1String("/**")
|| (comment == QLatin1String("/*!")) || comment == QLatin1String("/*!")
|| (comment == QLatin1String("///")) || comment == QLatin1String("///")
|| (comment == QLatin1String("//!"))) { || comment == QLatin1String("//!");
return true;
}
return false;
} }
DoxygenGenerator::DocumentationStyle doxygenStyle(const QTextCursor &cursor, DoxygenGenerator::DocumentationStyle doxygenStyle(const QTextCursor &cursor,
@@ -119,7 +116,7 @@ bool isNextLineCppStyleComment(const QTextCursor &cursor)
bool isCppStyleContinuation(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 /// 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; return false;
} }
bool handleDoxygenCppStyleContinuation(QTextCursor &cursor, QKeyEvent *e) bool handleDoxygenCppStyleContinuation(QTextCursor &cursor)
{ {
const int blockPos = cursor.positionInBlock(); const int blockPos = cursor.positionInBlock();
const QString &text = cursor.block().text(); const QString &text = cursor.block().text();
@@ -165,12 +162,10 @@ bool handleDoxygenCppStyleContinuation(QTextCursor &cursor, QKeyEvent *e)
newLine.append(QLatin1Char(' ')); newLine.append(QLatin1Char(' '));
cursor.insertText(newLine); cursor.insertText(newLine);
e->accept();
return true; return true;
} }
bool handleDoxygenContinuation(QTextCursor &cursor, bool handleDoxygenContinuation(QTextCursor &cursor,
QKeyEvent *e,
const QTextDocument *doc, const QTextDocument *doc,
const bool enableDoxygen, const bool enableDoxygen,
const bool leadingAsterisks) 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 // b) current line is in the middle of a multi-line Qt or Java style comment
if (enableDoxygen && !cursor.atEnd() && isCursorAfterCppComment(cursor, doc)) if (enableDoxygen && !cursor.atEnd() && isCursorAfterCppComment(cursor, doc))
return handleDoxygenCppStyleContinuation(cursor, e); return handleDoxygenCppStyleContinuation(cursor);
if (!leadingAsterisks) if (!leadingAsterisks)
return false; return false;
@@ -223,7 +218,6 @@ bool handleDoxygenContinuation(QTextCursor &cursor,
newLine.append(QLatin1Char(' ')); newLine.append(QLatin1Char(' '));
} }
cursor.insertText(newLine); cursor.insertText(newLine);
e->accept();
return true; return true;
} }
} }
@@ -236,25 +230,14 @@ bool handleDoxygenContinuation(QTextCursor &cursor,
namespace CppEditor { namespace CppEditor {
namespace Internal { namespace Internal {
CppDocumentationCommentHelper::CppDocumentationCommentHelper( bool trySplitComment(TextEditor::TextEditorWidget *editorWidget)
TextEditor::TextEditorWidget *editorWidget)
: m_editorWidget(editorWidget)
, m_settings(CppToolsSettings::instance()->commentsSettings())
{ {
connect(CppToolsSettings::instance(), const CommentsSettings &settings = CppToolsSettings::instance()->commentsSettings();
SIGNAL(commentsSettingsChanged(CppTools::CommentsSettings)), if (!settings.m_enableDoxygen && !settings.m_leadingAsterisks)
this,
SLOT(onCommentsSettingsChanged(CppTools::CommentsSettings)));
}
bool CppDocumentationCommentHelper::handleKeyPressEvent(QKeyEvent *e) const
{
if (!m_settings.m_enableDoxygen && !m_settings.m_leadingAsterisks)
return false; return false;
if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) { QTextCursor cursor = editorWidget->textCursor();
QTextCursor cursor = m_editorWidget->textCursor(); if (!editorWidget->autoCompleter()->isInComment(cursor))
if (!m_editorWidget->autoCompleter()->isInComment(cursor))
return false; return false;
// We are interested on two particular cases: // We are interested on two particular cases:
@@ -264,23 +247,23 @@ bool CppDocumentationCommentHelper::handleKeyPressEvent(QKeyEvent *e) const
// enter. If leading asterisk(s) is set we need to write a comment continuation // enter. If leading asterisk(s) is set we need to write a comment continuation
// with those. // with those.
if (m_settings.m_enableDoxygen && cursor.positionInBlock() >= 3) { if (settings.m_enableDoxygen && cursor.positionInBlock() >= 3) {
const int pos = cursor.position(); const int pos = cursor.position();
if (isStartOfDoxygenComment(cursor)) { if (isStartOfDoxygenComment(cursor)) {
QTextDocument *textDocument = m_editorWidget->document(); QTextDocument *textDocument = editorWidget->document();
DoxygenGenerator::DocumentationStyle style = doxygenStyle(cursor, textDocument); DoxygenGenerator::DocumentationStyle style = doxygenStyle(cursor, textDocument);
// Check if we're already in a CppStyle Doxygen comment => continuation // Check if we're already in a CppStyle Doxygen comment => continuation
// Needs special handling since CppStyle does not have start and end markers // Needs special handling since CppStyle does not have start and end markers
if ((style == DoxygenGenerator::CppStyleA || style == DoxygenGenerator::CppStyleB) if ((style == DoxygenGenerator::CppStyleA || style == DoxygenGenerator::CppStyleB)
&& isCppStyleContinuation(cursor)) { && isCppStyleContinuation(cursor)) {
return handleDoxygenCppStyleContinuation(cursor, e); return handleDoxygenCppStyleContinuation(cursor);
} }
DoxygenGenerator doxygen; DoxygenGenerator doxygen;
doxygen.setStyle(style); doxygen.setStyle(style);
doxygen.setAddLeadingAsterisks(m_settings.m_leadingAsterisks); doxygen.setAddLeadingAsterisks(settings.m_leadingAsterisks);
doxygen.setGenerateBrief(m_settings.m_generateBrief); doxygen.setGenerateBrief(settings.m_generateBrief);
doxygen.setStartComment(false); doxygen.setStartComment(false);
// Move until we reach any possibly meaningful content. // Move until we reach any possibly meaningful content.
@@ -295,9 +278,8 @@ bool CppDocumentationCommentHelper::handleKeyPressEvent(QKeyEvent *e) const
cursor.setPosition(pos); cursor.setPosition(pos);
cursor.insertText(comment); cursor.insertText(comment);
cursor.setPosition(pos - 3, QTextCursor::KeepAnchor); cursor.setPosition(pos - 3, QTextCursor::KeepAnchor);
m_editorWidget->textDocument()->autoIndent(cursor); editorWidget->textDocument()->autoIndent(cursor);
cursor.endEditBlock(); cursor.endEditBlock();
e->accept();
return true; return true;
} }
} }
@@ -306,18 +288,9 @@ bool CppDocumentationCommentHelper::handleKeyPressEvent(QKeyEvent *e) const
} // right after first doxygen comment } // right after first doxygen comment
return handleDoxygenContinuation(cursor, return handleDoxygenContinuation(cursor,
e, editorWidget->document(),
m_editorWidget->document(), settings.m_enableDoxygen,
m_settings.m_enableDoxygen, settings.m_leadingAsterisks);
m_settings.m_leadingAsterisks);
}
return false;
}
void CppDocumentationCommentHelper::onCommentsSettingsChanged(const CommentsSettings &settings)
{
m_settings = settings;
} }
} // namespace Internal } // namespace Internal

View File

@@ -31,35 +31,14 @@
#ifndef CPPDOCUMENTATIONCOMMENTHELPER_H #ifndef CPPDOCUMENTATIONCOMMENTHELPER_H
#define CPPDOCUMENTATIONCOMMENTHELPER_H #define CPPDOCUMENTATIONCOMMENTHELPER_H
#include <QObject> #include <cppeditor_global.h>
#include <cpptools/commentssettings.h>
QT_FORWARD_DECLARE_CLASS(QKeyEvent)
namespace TextEditor { class TextEditorWidget; } namespace TextEditor { class TextEditorWidget; }
namespace CppEditor { namespace CppEditor {
namespace Internal { namespace Internal {
class CppDocumentationCommentHelper : public QObject bool trySplitComment(TextEditor::TextEditorWidget *editorWidget);
{
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;
};
} // namespace Internal } // namespace Internal
} // namespace CppEditor } // namespace CppEditor

View File

@@ -110,8 +110,6 @@ public:
CppEditorDocument *m_cppEditorDocument; CppEditorDocument *m_cppEditorDocument;
CppEditorOutline *m_cppEditorOutline; CppEditorOutline *m_cppEditorOutline;
CppDocumentationCommentHelper m_cppDocumentationCommentHelper;
QTimer m_updateFunctionDeclDefLinkTimer; QTimer m_updateFunctionDeclDefLinkTimer;
CppLocalRenaming m_localRenaming; CppLocalRenaming m_localRenaming;
@@ -132,7 +130,6 @@ CppEditorWidgetPrivate::CppEditorWidgetPrivate(CppEditorWidget *q)
: m_modelManager(CppModelManager::instance()) : m_modelManager(CppModelManager::instance())
, m_cppEditorDocument(qobject_cast<CppEditorDocument *>(q->textDocument())) , m_cppEditorDocument(qobject_cast<CppEditorDocument *>(q->textDocument()))
, m_cppEditorOutline(new CppEditorOutline(q)) , m_cppEditorOutline(new CppEditorOutline(q))
, m_cppDocumentationCommentHelper(q)
, m_localRenaming(q) , m_localRenaming(q)
, m_useSelectionsUpdater(q) , m_useSelectionsUpdater(q)
, m_declDefLinkFinder(new FunctionDeclDefLinkFinder(q)) , m_declDefLinkFinder(new FunctionDeclDefLinkFinder(q))
@@ -541,8 +538,12 @@ void CppEditorWidget::keyPressEvent(QKeyEvent *e)
if (handleStringSplitting(e)) if (handleStringSplitting(e))
return; return;
if (d->m_cppDocumentationCommentHelper.handleKeyPressEvent(e)) if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
if (trySplitComment(this)) {
e->accept();
return; return;
}
}
TextEditorWidget::keyPressEvent(e); TextEditorWidget::keyPressEvent(e);
} }