aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cppeditor/cppquickfixes.cpp
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@qt.io>2023-11-06 13:41:39 +0100
committerChristian Kandeler <christian.kandeler@qt.io>2023-11-09 12:51:10 +0000
commite3c1179abbd81fda7011e79ae20c10de9573b001 (patch)
treebcf3d5f624fd1702c139a1538bde2b42391cb8d8 /src/plugins/cppeditor/cppquickfixes.cpp
parent266f9bc63218fc5ed78161838032ca2dbff629c0 (diff)
CppEditor: Manually indent moved comments
Using the indenter yields unepected results in certain contexts. Fixes: QTCREATORBUG-29786 Change-Id: Id15eff841d2aa54e7fff65c6bf728516e03f9fc6 Reviewed-by: David Schulz <david.schulz@qt.io> Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'src/plugins/cppeditor/cppquickfixes.cpp')
-rw-r--r--src/plugins/cppeditor/cppquickfixes.cpp45
1 files changed, 43 insertions, 2 deletions
diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp
index 7f08b96200..012785df34 100644
--- a/src/plugins/cppeditor/cppquickfixes.cpp
+++ b/src/plugins/cppeditor/cppquickfixes.cpp
@@ -34,10 +34,13 @@
#include <extensionsystem/pluginmanager.h>
+#include <projectexplorer/editorconfiguration.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/projecttree.h>
#include <projectexplorer/projectmanager.h>
+#include <texteditor/tabsettings.h>
+
#include <utils/algorithm.h>
#include <utils/basetreeview.h>
#include <utils/layoutbuilder.h>
@@ -9610,7 +9613,45 @@ private:
comments.first(), sourceFile->document());
const int sourceCommentEndPos = sourceTu->getTokenEndPositionInDocument(
comments.last(), sourceFile->document());
- const QString functionDoc = sourceFile->textOf(sourceCommentStartPos, sourceCommentEndPos);
+
+ // Manually adjust indentation, as both our built-in indenter and ClangFormat
+ // are unreliable with regards to comment continuation lines.
+ auto tabSettings = [](CppRefactoringFilePtr file) {
+ if (auto editor = file->editor())
+ return editor->textDocument()->tabSettings();
+ return ProjectExplorer::actualTabSettings(file->filePath(), nullptr);
+ };
+ const TabSettings &sts = tabSettings(sourceFile);
+ const TabSettings &tts = tabSettings(targetFile);
+ const QTextBlock insertionBlock = targetFile->document()->findBlock(insertionPos);
+ const int insertionColumn = tts.columnAt(insertionBlock.text(),
+ insertionPos - insertionBlock.position());
+ const QTextBlock removalBlock = sourceFile->document()->findBlock(sourceCommentStartPos);
+ const QTextBlock removalBlockEnd = sourceFile->document()->findBlock(sourceCommentEndPos);
+ const int removalColumn = sts.columnAt(removalBlock.text(),
+ sourceCommentStartPos - removalBlock.position());
+ const int columnOffset = insertionColumn - removalColumn;
+ QString functionDoc;
+ if (columnOffset != 0) {
+ for (QTextBlock block = removalBlock;
+ block.isValid() && block != removalBlockEnd.next();
+ block = block.next()) {
+ QString text = block.text() + QChar::ParagraphSeparator;
+ if (block == removalBlockEnd)
+ text = text.left(sourceCommentEndPos - block.position());
+ if (block == removalBlock) {
+ text = text.mid(sourceCommentStartPos - block.position());
+ } else {
+ int lineIndentColumn = sts.indentationColumn(text) + columnOffset;
+ text.replace(0,
+ TabSettings::firstNonSpace(text),
+ tts.indentationString(0, lineIndentColumn, 0, insertionBlock));
+ }
+ functionDoc += text;
+ }
+ } else {
+ functionDoc = sourceFile->textOf(sourceCommentStartPos, sourceCommentEndPos);
+ }
// Remove comment plus leading and trailing whitespace, including trailing newline.
const auto removeAtSource = [&](ChangeSet &changeSet) {
@@ -9642,10 +9683,10 @@ private:
ChangeSet targetChangeSet;
targetChangeSet.insert(insertionPos, functionDoc);
targetChangeSet.insert(insertionPos, "\n");
+ targetChangeSet.insert(insertionPos, QString(insertionColumn, ' '));
if (targetFile == sourceFile)
removeAtSource(targetChangeSet);
targetFile->setChangeSet(targetChangeSet);
- targetFile->appendIndentRange({insertionPos, insertionPos + int(functionDoc.length())});
const bool targetFileSuccess = targetFile->apply();
if (targetFile == sourceFile || !targetFileSuccess)
return;