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 <cpptools/cpptoolssettings.h>
#include <cpptools/commentssettings.h>
#include <cpptools/doxygengenerator.h>
#include <texteditor/texteditor.h>
#include <QDebug>
#include <QKeyEvent>
#include <QTextBlock>
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,25 +230,14 @@ 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))
QTextCursor cursor = editorWidget->textCursor();
if (!editorWidget->autoCompleter()->isInComment(cursor))
return false;
// 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
// with those.
if (m_settings.m_enableDoxygen && cursor.positionInBlock() >= 3) {
if (settings.m_enableDoxygen && cursor.positionInBlock() >= 3) {
const int pos = cursor.position();
if (isStartOfDoxygenComment(cursor)) {
QTextDocument *textDocument = m_editorWidget->document();
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, e);
return handleDoxygenCppStyleContinuation(cursor);
}
DoxygenGenerator doxygen;
doxygen.setStyle(style);
doxygen.setAddLeadingAsterisks(m_settings.m_leadingAsterisks);
doxygen.setGenerateBrief(m_settings.m_generateBrief);
doxygen.setAddLeadingAsterisks(settings.m_leadingAsterisks);
doxygen.setGenerateBrief(settings.m_generateBrief);
doxygen.setStartComment(false);
// Move until we reach any possibly meaningful content.
@@ -295,9 +278,8 @@ bool CppDocumentationCommentHelper::handleKeyPressEvent(QKeyEvent *e) const
cursor.setPosition(pos);
cursor.insertText(comment);
cursor.setPosition(pos - 3, QTextCursor::KeepAnchor);
m_editorWidget->textDocument()->autoIndent(cursor);
editorWidget->textDocument()->autoIndent(cursor);
cursor.endEditBlock();
e->accept();
return true;
}
}
@@ -306,18 +288,9 @@ bool CppDocumentationCommentHelper::handleKeyPressEvent(QKeyEvent *e) const
} // right after first doxygen comment
return handleDoxygenContinuation(cursor,
e,
m_editorWidget->document(),
m_settings.m_enableDoxygen,
m_settings.m_leadingAsterisks);
}
return false;
}
void CppDocumentationCommentHelper::onCommentsSettingsChanged(const CommentsSettings &settings)
{
m_settings = settings;
editorWidget->document(),
settings.m_enableDoxygen,
settings.m_leadingAsterisks);
}
} // namespace Internal

View File

@@ -31,35 +31,14 @@
#ifndef CPPDOCUMENTATIONCOMMENTHELPER_H
#define CPPDOCUMENTATIONCOMMENTHELPER_H
#include <QObject>
#include <cpptools/commentssettings.h>
QT_FORWARD_DECLARE_CLASS(QKeyEvent)
#include <cppeditor_global.h>
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

View File

@@ -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<CppEditorDocument *>(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))
if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
if (trySplitComment(this)) {
e->accept();
return;
}
}
TextEditorWidget::keyPressEvent(e);
}