forked from qt-creator/qt-creator
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:
@@ -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,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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
return;
|
||||
if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
|
||||
if (trySplitComment(this)) {
|
||||
e->accept();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
TextEditorWidget::keyPressEvent(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user