forked from qt-creator/qt-creator
Editor: fix upper and lower case on blockselection.
Task-number: QTCREATORBUG-11546 Change-Id: I2e8b5b05ff0044e5ab159119f24dff873be949d7 Reviewed-by: Christian Stenger <christian.stenger@digia.com>
This commit is contained in:
@@ -6166,6 +6166,13 @@ void BaseTextBlockSelection::moveAnchor(int blockNumber, int visualColumn)
|
|||||||
lastBlock.movePosition(QTextCursor::EndOfBlock);
|
lastBlock.movePosition(QTextCursor::EndOfBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int BaseTextBlockSelection::position(const TabSettings &ts) const
|
||||||
|
{
|
||||||
|
const QTextBlock &block = anchor <= TopRight ? lastBlock.block() : firstBlock.block();
|
||||||
|
const int column = anchor % 2 ? firstVisualColumn : lastVisualColumn;
|
||||||
|
return block.position() + ts.positionAtColumn(block.text(), column);
|
||||||
|
}
|
||||||
|
|
||||||
QTextCursor BaseTextBlockSelection::selection(const TabSettings &ts) const
|
QTextCursor BaseTextBlockSelection::selection(const TabSettings &ts) const
|
||||||
{
|
{
|
||||||
QTextCursor cursor = firstBlock;
|
QTextCursor cursor = firstBlock;
|
||||||
@@ -6334,50 +6341,46 @@ void BaseTextEditorWidget::transformSelection(TransformationMethod method)
|
|||||||
void BaseTextEditorWidget::transformBlockSelection(TransformationMethod method)
|
void BaseTextEditorWidget::transformBlockSelection(TransformationMethod method)
|
||||||
{
|
{
|
||||||
QTextCursor cursor = textCursor();
|
QTextCursor cursor = textCursor();
|
||||||
int minPos = cursor.anchor();
|
const TabSettings &ts = d->m_document->tabSettings();
|
||||||
int maxPos = cursor.position();
|
|
||||||
if (minPos > maxPos)
|
// saved to restore the blockselection
|
||||||
qSwap(minPos, maxPos);
|
const int selectionPosition = d->m_blockSelection.position(ts);
|
||||||
int leftBound = verticalBlockSelectionFirstColumn();
|
const int anchorColumn = d->m_blockSelection.anchorColumnNumber();
|
||||||
int rightBound = verticalBlockSelectionLastColumn();
|
const int anchorBlock = d->m_blockSelection.anchorBlockNumber();
|
||||||
BaseTextBlockSelection::Anchor anchorPosition = d->m_blockSelection.anchor;
|
const BaseTextBlockSelection::Anchor anchor = d->m_blockSelection.anchor;
|
||||||
QString text = cursor.selectedText();
|
|
||||||
QString transformedText = text;
|
QTextBlock block = d->m_blockSelection.firstBlock.block();
|
||||||
QTextBlock currentLine = document()->findBlock(minPos);
|
const QTextBlock &lastBlock = d->m_blockSelection.lastBlock.block();
|
||||||
int lineStart = currentLine.position();
|
|
||||||
do {
|
cursor.beginEditBlock();
|
||||||
if (currentLine.contains(lineStart + leftBound)) {
|
for (;;) {
|
||||||
int currentBlockWidth = qBound(0, currentLine.text().length() - leftBound,
|
// get position of the selection
|
||||||
rightBound - leftBound);
|
const QString &blockText = block.text();
|
||||||
cursor.setPosition(lineStart + leftBound);
|
const int startPos = block.position()
|
||||||
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, currentBlockWidth);
|
+ ts.positionAtColumn(blockText, d->m_blockSelection.firstVisualColumn);
|
||||||
transformedText.replace(lineStart + leftBound - minPos, currentBlockWidth,
|
const int endPos = block.position()
|
||||||
(cursor.selectedText().*method)());
|
+ ts.positionAtColumn(blockText, d->m_blockSelection.lastVisualColumn);
|
||||||
|
|
||||||
|
// check if the selection is inside the text block
|
||||||
|
if (startPos < endPos) {
|
||||||
|
cursor.setPosition(startPos);
|
||||||
|
cursor.setPosition(endPos, QTextCursor::KeepAnchor);
|
||||||
|
const QString &transformedText = (d->m_document->textAt(startPos, endPos - startPos).*method)();
|
||||||
|
if (transformedText != cursor.selectedText())
|
||||||
|
cursor.insertText(transformedText);
|
||||||
}
|
}
|
||||||
currentLine = currentLine.next();
|
if (block == lastBlock)
|
||||||
if (!currentLine.isValid())
|
|
||||||
break;
|
break;
|
||||||
lineStart = currentLine.position();
|
block = block.next();
|
||||||
} while (lineStart < maxPos);
|
|
||||||
|
|
||||||
if (transformedText == text) {
|
|
||||||
// if the transformation does not do anything to the selection, do no create an undo step
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
cursor.endEditBlock();
|
||||||
|
|
||||||
cursor.setPosition(minPos);
|
|
||||||
cursor.setPosition(maxPos, QTextCursor::KeepAnchor);
|
|
||||||
cursor.insertText(transformedText);
|
|
||||||
// restore former block selection
|
// restore former block selection
|
||||||
if (anchorPosition <= BaseTextBlockSelection::TopRight)
|
cursor.setPosition(selectionPosition, QTextCursor::MoveAnchor);
|
||||||
qSwap(minPos, maxPos);
|
|
||||||
cursor.setPosition(minPos);
|
|
||||||
cursor.setPosition(maxPos, QTextCursor::KeepAnchor);
|
|
||||||
d->m_blockSelection.fromSelection(d->m_document->tabSettings(), cursor);
|
|
||||||
d->m_blockSelection.anchor = anchorPosition;
|
|
||||||
d->m_inBlockSelectionMode = true;
|
d->m_inBlockSelectionMode = true;
|
||||||
d->m_blockSelection.firstVisualColumn = leftBound;
|
d->m_blockSelection.fromSelection(ts, cursor);
|
||||||
d->m_blockSelection.lastVisualColumn = rightBound;
|
d->m_blockSelection.anchor = anchor;
|
||||||
|
d->m_blockSelection.moveAnchor(anchorBlock, anchorColumn);
|
||||||
setTextCursor(d->m_blockSelection.selection(d->m_document->tabSettings()));
|
setTextCursor(d->m_blockSelection.selection(d->m_document->tabSettings()));
|
||||||
viewport()->update();
|
viewport()->update();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ public:
|
|||||||
enum Anchor {TopLeft = 0, TopRight, BottomLeft, BottomRight} anchor;
|
enum Anchor {TopLeft = 0, TopRight, BottomLeft, BottomRight} anchor;
|
||||||
BaseTextBlockSelection():firstVisualColumn(0), lastVisualColumn(0), anchor(BottomRight){}
|
BaseTextBlockSelection():firstVisualColumn(0), lastVisualColumn(0), anchor(BottomRight){}
|
||||||
void moveAnchor(int blockNumber, int visualColumn);
|
void moveAnchor(int blockNumber, int visualColumn);
|
||||||
|
int position(const TabSettings &ts) const;
|
||||||
inline int anchorColumnNumber() const { return (anchor % 2) ? lastVisualColumn : firstVisualColumn; }
|
inline int anchorColumnNumber() const { return (anchor % 2) ? lastVisualColumn : firstVisualColumn; }
|
||||||
inline int anchorBlockNumber() const {
|
inline int anchorBlockNumber() const {
|
||||||
return (anchor <= TopRight ? firstBlock.blockNumber() : lastBlock.blockNumber()); }
|
return (anchor <= TopRight ? firstBlock.blockNumber() : lastBlock.blockNumber()); }
|
||||||
|
|||||||
136
src/plugins/texteditor/basetexteditor_test.cpp
Normal file
136
src/plugins/texteditor/basetexteditor_test.cpp
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
||||||
|
** Contact: http://www.qt-project.org/legal
|
||||||
|
**
|
||||||
|
** This file is part of Qt Creator.
|
||||||
|
**
|
||||||
|
** Commercial License Usage
|
||||||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||||||
|
** accordance with the commercial license agreement provided with the
|
||||||
|
** Software or, alternatively, in accordance with the terms contained in
|
||||||
|
** a written agreement between you and Digia. For licensing terms and
|
||||||
|
** conditions see http://qt.digia.com/licensing. For further information
|
||||||
|
** use the contact form at http://qt.digia.com/contact-us.
|
||||||
|
**
|
||||||
|
** GNU Lesser General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||||
|
** General Public License version 2.1 as published by the Free Software
|
||||||
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
||||||
|
** packaging of this file. Please review the following information to
|
||||||
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
||||||
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||||||
|
**
|
||||||
|
** In addition, as a special exception, Digia gives you certain additional
|
||||||
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
||||||
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifdef WITH_TESTS
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <QtTest/QtTest>
|
||||||
|
|
||||||
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
|
#include <coreplugin/coreconstants.h>
|
||||||
|
|
||||||
|
#include "basetexteditor.h"
|
||||||
|
#include "texteditorplugin.h"
|
||||||
|
|
||||||
|
using namespace TextEditor;
|
||||||
|
|
||||||
|
enum TransFormationType { Uppercase, Lowercase };
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(TransFormationType)
|
||||||
|
|
||||||
|
void Internal::TextEditorPlugin::testBlockSelectionTransformation_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<QByteArray>("input");
|
||||||
|
QTest::addColumn<QByteArray>("transformedText");
|
||||||
|
QTest::addColumn<int>("position");
|
||||||
|
QTest::addColumn<int>("anchor");
|
||||||
|
QTest::addColumn<TransFormationType>("type");
|
||||||
|
|
||||||
|
QTest::newRow("uppercase")
|
||||||
|
<< QByteArray("aaa\nbbb\nccc\n")
|
||||||
|
<< QByteArray("aAa\nbBb\ncCc\n")
|
||||||
|
<< 10 << 1 << Uppercase;
|
||||||
|
QTest::newRow("lowercase")
|
||||||
|
<< QByteArray("AAA\nBBB\nCCC\n")
|
||||||
|
<< QByteArray("AaA\nBbB\nCcC\n")
|
||||||
|
<< 10 << 1 << Lowercase;
|
||||||
|
QTest::newRow("uppercase_leading_tabs")
|
||||||
|
<< QByteArray("\taaa\n\tbbb\n\tccc\n")
|
||||||
|
<< QByteArray("\taAa\n\tbBb\n\tcCc\n")
|
||||||
|
<< 13 << 2 << Uppercase;
|
||||||
|
QTest::newRow("lowercase_leading_tabs")
|
||||||
|
<< QByteArray("\tAAA\n\tBBB\n\tCCC\n")
|
||||||
|
<< QByteArray("\tAaA\n\tBbB\n\tCcC\n")
|
||||||
|
<< 13 << 2 << Lowercase;
|
||||||
|
QTest::newRow("uppercase_mixed_leading_whitespace")
|
||||||
|
<< QByteArray("\taaa\n bbbbb\n ccccc\n")
|
||||||
|
<< QByteArray("\tAaa\n bbbbB\n ccccC\n")
|
||||||
|
<< 24 << 1 << Uppercase;
|
||||||
|
QTest::newRow("lowercase_mixed_leading_whitespace")
|
||||||
|
<< QByteArray("\tAAA\n BBBBB\n CCCCC\n")
|
||||||
|
<< QByteArray("\taAA\n BBBBb\n CCCCc\n")
|
||||||
|
<< 24 << 1 << Lowercase;
|
||||||
|
QTest::newRow("uppercase_mid_tabs1")
|
||||||
|
<< QByteArray("a\ta\nbbbbbbbbb\nccccccccc\n")
|
||||||
|
<< QByteArray("a\ta\nbBBBBBbbb\ncCCCCCccc\n")
|
||||||
|
<< 20 << 1 << Uppercase;
|
||||||
|
QTest::newRow("lowercase_mid_tabs2")
|
||||||
|
<< QByteArray("AA\taa\n\t\t\nccccCCCCCC\n")
|
||||||
|
<< QByteArray("Aa\taa\n\t\t\ncccccccccC\n")
|
||||||
|
<< 18 << 1 << Lowercase;
|
||||||
|
QTest::newRow("uppercase_over_line_ending")
|
||||||
|
<< QByteArray("aaaaa\nbbb\nccccc\n")
|
||||||
|
<< QByteArray("aAAAa\nbBB\ncCCCc\n")
|
||||||
|
<< 14 << 1 << Uppercase;
|
||||||
|
QTest::newRow("lowercase_over_line_ending")
|
||||||
|
<< QByteArray("AAAAA\nBBB\nCCCCC\n")
|
||||||
|
<< QByteArray("AaaaA\nBbb\nCcccC\n")
|
||||||
|
<< 14 << 1 << Lowercase;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Internal::TextEditorPlugin::testBlockSelectionTransformation()
|
||||||
|
{
|
||||||
|
// fetch test data
|
||||||
|
QFETCH(QByteArray, input);
|
||||||
|
QFETCH(QByteArray, transformedText);
|
||||||
|
QFETCH(int, position);
|
||||||
|
QFETCH(int, anchor);
|
||||||
|
QFETCH(TransFormationType, type);
|
||||||
|
|
||||||
|
// open editor
|
||||||
|
Core::IEditor *editor = Core::EditorManager::openEditorWithContents(
|
||||||
|
Core::Constants::K_DEFAULT_TEXT_EDITOR_ID, 0, input);
|
||||||
|
QVERIFY(editor);
|
||||||
|
if (BaseTextEditor *textEditor = qobject_cast<BaseTextEditor*>(editor)) {
|
||||||
|
|
||||||
|
// place cursor and enable block selection
|
||||||
|
BaseTextEditorWidget *editorWidget = textEditor->editorWidget();
|
||||||
|
QTextCursor cursor = editorWidget->textCursor();
|
||||||
|
cursor.setPosition(anchor);
|
||||||
|
cursor.setPosition(position, QTextCursor::KeepAnchor);
|
||||||
|
editorWidget->setTextCursor(cursor);
|
||||||
|
editorWidget->setBlockSelection(true);
|
||||||
|
|
||||||
|
// transform blockselection
|
||||||
|
switch (type) {
|
||||||
|
case Uppercase:
|
||||||
|
editorWidget->uppercaseSelection();
|
||||||
|
break;
|
||||||
|
case Lowercase:
|
||||||
|
editorWidget->lowercaseSelection();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
QCOMPARE(textEditor->baseTextDocument()->plainText().toLatin1(), transformedText);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
Core::EditorManager::closeEditor(editor, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // ifdef WITH_TESTS
|
||||||
@@ -240,3 +240,8 @@ FORMS += \
|
|||||||
tabsettingswidget.ui \
|
tabsettingswidget.ui \
|
||||||
codestyleselectorwidget.ui
|
codestyleselectorwidget.ui
|
||||||
RESOURCES += texteditor.qrc
|
RESOURCES += texteditor.qrc
|
||||||
|
|
||||||
|
equals(TEST, 1) {
|
||||||
|
SOURCES += basetexteditor_test.cpp
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -268,4 +268,12 @@ QtcPlugin {
|
|||||||
"snippetssettingspage.ui",
|
"snippetssettingspage.ui",
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Group {
|
||||||
|
name: "Tests"
|
||||||
|
condition: project.testsEnabled
|
||||||
|
files: [
|
||||||
|
"basetexteditor_test.cpp",
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,6 +76,9 @@ private slots:
|
|||||||
#ifdef WITH_TESTS
|
#ifdef WITH_TESTS
|
||||||
void testSnippetParsing_data();
|
void testSnippetParsing_data();
|
||||||
void testSnippetParsing();
|
void testSnippetParsing();
|
||||||
|
|
||||||
|
void testBlockSelectionTransformation_data();
|
||||||
|
void testBlockSelectionTransformation();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
Reference in New Issue
Block a user