Commit f3e92100 authored by Christian Stenger's avatar Christian Stenger
Browse files

Editor: Fix transformSelection() for block selection



Task-number: QTCREATORBUG-7643

Change-Id: I26f9a3637a39a26e82b72de1143e31b5e55b076e
Reviewed-by: default avatarLeandro Melo <leandro.melo@nokia.com>
parent a46f0520
...@@ -6455,8 +6455,12 @@ int BaseTextEditorWidget::rowCount() const ...@@ -6455,8 +6455,12 @@ int BaseTextEditorWidget::rowCount() const
*/ */
void BaseTextEditorWidget::transformSelection(Internal::TransformationMethod method) void BaseTextEditorWidget::transformSelection(Internal::TransformationMethod method)
{ {
QTextCursor cursor = textCursor(); if (hasBlockSelection()) {
transformBlockSelection(method);
return;
}
QTextCursor cursor = textCursor();
int pos = cursor.position(); int pos = cursor.position();
int anchor = cursor.anchor(); int anchor = cursor.anchor();
...@@ -6482,6 +6486,57 @@ void BaseTextEditorWidget::transformSelection(Internal::TransformationMethod met ...@@ -6482,6 +6486,57 @@ void BaseTextEditorWidget::transformSelection(Internal::TransformationMethod met
setTextCursor(cursor); 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) void BaseTextEditorWidget::inSnippetMode(bool *active)
{ {
*active = d->m_snippetOverlay->isVisible(); *active = d->m_snippetOverlay->isVisible();
......
...@@ -576,6 +576,7 @@ private: ...@@ -576,6 +576,7 @@ private:
void processTooltipRequest(const QTextCursor &c); void processTooltipRequest(const QTextCursor &c);
void transformSelection(Internal::TransformationMethod method); void transformSelection(Internal::TransformationMethod method);
void transformBlockSelection(Internal::TransformationMethod method);
private slots: private slots:
void handleBlockSelection(int diff_row, int diff_col); void handleBlockSelection(int diff_row, int diff_col);
......
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