Commit cbc64077 authored by Nikolai Kosjar's avatar Nikolai Kosjar
Browse files

CppEditor: Fix comment continuation



Do not continue a comment with '*' for

	*foo //<Enter>

and

	*foo /*<Enter>

Task-number: QTCREATORBUG-13415
Change-Id: I0bf062fb96942930f8c9a268bff832de219c7787
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@theqtcompany.com>
Reviewed-by: default avatarEike Ziller <eike.ziller@theqtcompany.com>
parent 65bb5856
......@@ -122,8 +122,8 @@ bool isCppStyleContinuation(const QTextCursor& cursor)
return (isPreviousLineCppStyleComment(cursor) || isNextLineCppStyleComment(cursor));
}
/// Check if line is a CppStyle Doxygen comment and the cursor is after the comment
bool isCursorAfterCppComment(const QTextCursor &cursor, const QTextDocument *doc)
bool lineStartsWithCppDoxygenCommentAndCursorIsAfter(const QTextCursor &cursor,
const QTextDocument *doc)
{
QTextCursor cursorFirstNonBlank(cursor);
cursorFirstNonBlank.movePosition(QTextCursor::StartOfLine);
......@@ -139,6 +139,27 @@ bool isCursorAfterCppComment(const QTextCursor &cursor, const QTextDocument *doc
return false;
}
bool isCursorAfterNonNestedCppStyleComment(const QTextCursor &cursor,
TextEditor::TextEditorWidget *editorWidget)
{
QTextDocument *document = editorWidget->document();
QTextCursor cursorBeforeCppComment(cursor);
while (document->characterAt(cursorBeforeCppComment.position()) != QLatin1Char('/')
&& cursorBeforeCppComment.movePosition(QTextCursor::PreviousCharacter)) {
}
if (!cursorBeforeCppComment.movePosition(QTextCursor::PreviousCharacter))
return false;
if (document->characterAt(cursorBeforeCppComment.position()) != QLatin1Char('/'))
return false;
if (!cursorBeforeCppComment.movePosition(QTextCursor::PreviousCharacter))
return false;
return !editorWidget->autoCompleter()->isInComment(cursorBeforeCppComment);
}
bool handleDoxygenCppStyleContinuation(QTextCursor &cursor, QKeyEvent *e)
{
const int blockPos = cursor.positionInBlock();
......@@ -171,16 +192,23 @@ bool handleDoxygenCppStyleContinuation(QTextCursor &cursor, QKeyEvent *e)
bool handleDoxygenContinuation(QTextCursor &cursor,
QKeyEvent *e,
const QTextDocument *doc,
TextEditor::TextEditorWidget *editorWidget,
const bool enableDoxygen,
const bool leadingAsterisks)
{
const QTextDocument *doc = editorWidget->document();
// It might be a continuation if:
// a) current line starts with /// or //! and cursor is positioned after the comment
// 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);
if (!cursor.atEnd()) {
if (enableDoxygen && lineStartsWithCppDoxygenCommentAndCursorIsAfter(cursor, doc))
return handleDoxygenCppStyleContinuation(cursor, e);
if (isCursorAfterNonNestedCppStyleComment(cursor, editorWidget))
return false;
}
// We continue the comment if the cursor is after a comment's line asterisk and if
// there's no asterisk immediately after the cursor (that would already be considered
......@@ -224,6 +252,14 @@ bool handleDoxygenContinuation(QTextCursor &cursor,
else
newLine.append(QLatin1String(" "));
} else {
// If '*' is not within a comment, skip.
QTextCursor cursorOnFirstNonWhiteSpace(cursor);
const int positionOnFirstNonWhiteSpace = cursor.position() - blockPos + offset;
cursorOnFirstNonWhiteSpace.setPosition(positionOnFirstNonWhiteSpace);
if (!editorWidget->autoCompleter()->isInComment(cursorOnFirstNonWhiteSpace))
return false;
// ...otherwise do the continuation
int start = offset;
while (offset < blockPos && currentLine.at(offset) == QLatin1Char('*'))
++offset;
......@@ -316,7 +352,7 @@ bool CppDocumentationCommentHelper::handleKeyPressEvent(QKeyEvent *e) const
return handleDoxygenContinuation(cursor,
e,
m_editorWidget->document(),
m_editorWidget,
m_settings.m_enableDoxygen,
m_settings.m_leadingAsterisks);
}
......
......@@ -268,6 +268,24 @@ void CppEditorPlugin::test_doxygen_comments_data()
"void d(); ///\n"
"\n"
);
QTest::newRow("noContinuationForExpressionAndComment1") << _(
"bool preventFolding;\n"
"*foo //|\n"
) << _(
"bool preventFolding;\n"
"*foo //\n"
"\n"
);
QTest::newRow("noContinuationForExpressionAndComment2") << _(
"bool preventFolding;\n"
"*foo /*|\n"
) << _(
"bool preventFolding;\n"
"*foo /*\n"
" \n"
);
}
void CppEditorPlugin::test_doxygen_comments()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment