From f3e92100cba3d4537d07b2086aebce5d328c5579 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Fri, 17 Aug 2012 16:35:48 +0200 Subject: [PATCH] Editor: Fix transformSelection() for block selection Task-number: QTCREATORBUG-7643 Change-Id: I26f9a3637a39a26e82b72de1143e31b5e55b076e Reviewed-by: Leandro Melo --- src/plugins/texteditor/basetexteditor.cpp | 57 ++++++++++++++++++++++- src/plugins/texteditor/basetexteditor.h | 1 + 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 5c17d071609..6d95e71107b 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -6455,8 +6455,12 @@ int BaseTextEditorWidget::rowCount() const */ void BaseTextEditorWidget::transformSelection(Internal::TransformationMethod method) { - QTextCursor cursor = textCursor(); + if (hasBlockSelection()) { + transformBlockSelection(method); + return; + } + QTextCursor cursor = textCursor(); int pos = cursor.position(); int anchor = cursor.anchor(); @@ -6482,6 +6486,57 @@ void BaseTextEditorWidget::transformSelection(Internal::TransformationMethod met setTextCursor(cursor); } +void BaseTextEditorWidget::transformBlockSelection(Internal::TransformationMethod method) +{ + QTextCursor cursor = textCursor(); + int minPos = cursor.anchor(); + int maxPos = cursor.position(); + if (minPos > maxPos) + qSwap(minPos, maxPos); + int leftBound = verticalBlockSelectionFirstColumn(); + int rightBound = verticalBlockSelectionLastColumn(); + BaseTextBlockSelection::Anchor anchorPosition = d->m_blockSelection.anchor; + QString text = cursor.selectedText(); + QString transformedText = text; + QTextBlock currentLine = document()->findBlock(minPos); + int lineStart = currentLine.position(); + do { + if (currentLine.contains(lineStart + leftBound)) { + int currentBlockWidth = qBound(0, currentLine.text().length() - leftBound, + rightBound - leftBound); + cursor.setPosition(lineStart + leftBound); + cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, currentBlockWidth); + transformedText.replace(lineStart + leftBound - minPos, currentBlockWidth, + (cursor.selectedText().*method)()); + } + currentLine = currentLine.next(); + if (!currentLine.isValid()) + break; + lineStart = currentLine.position(); + } while (lineStart < maxPos); + + if (transformedText == text) { + // if the transformation does not do anything to the selection, do no create an undo step + return; + } + + cursor.setPosition(minPos); + cursor.setPosition(maxPos, QTextCursor::KeepAnchor); + cursor.insertText(transformedText); + // restore former block selection + if (anchorPosition <= BaseTextBlockSelection::TopRight) + qSwap(minPos, maxPos); + cursor.setPosition(minPos); + cursor.setPosition(maxPos, QTextCursor::KeepAnchor); + d->m_blockSelection.fromSelection(tabSettings(), cursor); + d->m_blockSelection.anchor = anchorPosition; + d->m_inBlockSelectionMode = true; + d->m_blockSelection.firstVisualColumn = leftBound; + d->m_blockSelection.lastVisualColumn = rightBound; + setTextCursor(d->m_blockSelection.selection(tabSettings())); + viewport()->update(); +} + void BaseTextEditorWidget::inSnippetMode(bool *active) { *active = d->m_snippetOverlay->isVisible(); diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index 891d2669d95..44ed266a233 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -576,6 +576,7 @@ private: void processTooltipRequest(const QTextCursor &c); void transformSelection(Internal::TransformationMethod method); + void transformBlockSelection(Internal::TransformationMethod method); private slots: void handleBlockSelection(int diff_row, int diff_col);