forked from qt-creator/qt-creator
Editor: Fix transformSelection() for block selection
Task-number: QTCREATORBUG-7643 Change-Id: I26f9a3637a39a26e82b72de1143e31b5e55b076e Reviewed-by: Leandro Melo <leandro.melo@nokia.com>
This commit is contained in:
@@ -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();
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user