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 "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,88 +230,67 @@ 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:
|
||||||
// 1) The cursor is right after a /**, /*!, /// or ///! and the user pressed enter.
|
// 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.
|
// 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
|
// 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
|
// 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
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 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,
|
DoxygenGenerator doxygen;
|
||||||
e,
|
doxygen.setStyle(style);
|
||||||
m_editorWidget->document(),
|
doxygen.setAddLeadingAsterisks(settings.m_leadingAsterisks);
|
||||||
m_settings.m_enableDoxygen,
|
doxygen.setGenerateBrief(settings.m_generateBrief);
|
||||||
m_settings.m_leadingAsterisks);
|
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)
|
if (!cursor.atEnd()) {
|
||||||
{
|
const QString &comment = doxygen.generate(cursor);
|
||||||
m_settings = settings;
|
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
|
} // namespace Internal
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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) {
|
||||||
return;
|
if (trySplitComment(this)) {
|
||||||
|
e->accept();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TextEditorWidget::keyPressEvent(e);
|
TextEditorWidget::keyPressEvent(e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user