CppEditor: Fix comment conversion for indented comments

Due to accidentally overlapping removal/replace operations, it could
happen that we lost actual content when trying to remove whitespace.
Amends f93836b25d.

Change-Id: I7a624e78cf0986ee9818d47ea0812c6632426a24
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Christian Kandeler
2023-11-24 14:22:59 +01:00
parent c3907bf781
commit 61e9a7fe16
2 changed files with 20 additions and 1 deletions

View File

@@ -9115,6 +9115,23 @@ int var2;)";
// A third affected comment
/* An unaffected comment */)";
// FIXME: Remove adjacent newline along with last block
// FIXME: Use CppRefactoringFile to auto-indent continuation lines?
QTest::newRow("C -> C++, indented") << R"(
struct S {
/*
* @This is an
* indented comment.
*/
void func();
)" << R"(
struct S {
// This is an
// indented comment.
void func();
)";
QTest::newRow("C++ -> C / no selection / single line") << R"(
// Other comment, unaffected
// Our @comment

View File

@@ -9438,6 +9438,7 @@ private:
changeSet.remove(block.position() + firstColumn, block.position() + endColumn);
};
const int contentIndex = indexOfActualContent();
int removed = 0;
if (contentIndex == -1) {
if (blockIsRemovable) {
removeBlock();
@@ -9455,6 +9456,7 @@ private:
} else {
changeSet.remove(block.position() + firstColumn,
block.position() + firstColumn + contentIndex);
removed = contentIndex;
}
if (block == firstBlock) {
@@ -9464,7 +9466,7 @@ private:
// If the line starts with enough whitespace, replace it with the
// comment start characters, so we don't move the content to the right
// unnecessarily. Otherwise, insert the comment start characters.
if (blockText.startsWith(QString(newCommentStart.size() + 1, ' '))) {
if (blockText.startsWith(QString(newCommentStart.size() + removed + 1, ' '))) {
changeSet.replace(block.position(),
block.position() + newCommentStart.length(),
newCommentStart);