forked from qt-creator/qt-creator
TextEditor: Add sortSelectedLines action
Change-Id: Ifdc82766bac3cfe2e9c287b4ef04902a943c8f72 Reviewed-by: André Hartmann <aha_1980@gmx.de>
This commit is contained in:
@@ -154,6 +154,7 @@ namespace Internal {
|
||||
enum { NExtraSelectionKinds = 12 };
|
||||
|
||||
typedef QString (TransformationMethod)(const QString &);
|
||||
typedef void (ListTransformationMethod)(QStringList &);
|
||||
|
||||
static QString QString_toUpper(const QString &str)
|
||||
{
|
||||
@@ -419,6 +420,8 @@ public:
|
||||
void transformSelection(TransformationMethod method);
|
||||
void transformBlockSelection(TransformationMethod method);
|
||||
|
||||
void transformSelectedLines(ListTransformationMethod method);
|
||||
|
||||
void slotUpdateExtraAreaWidth();
|
||||
void slotUpdateRequest(const QRect &r, int dy);
|
||||
void slotUpdateBlockNotify(const QTextBlock &);
|
||||
@@ -1616,6 +1619,11 @@ void TextEditorWidget::lowercaseSelection()
|
||||
d->transformSelection(&QString_toLower);
|
||||
}
|
||||
|
||||
void TextEditorWidget::sortSelectedLines()
|
||||
{
|
||||
d->transformSelectedLines([](QStringList &list) { list.sort(); });
|
||||
}
|
||||
|
||||
void TextEditorWidget::indent()
|
||||
{
|
||||
int offset = 0;
|
||||
@@ -7893,6 +7901,41 @@ void TextEditorWidgetPrivate::transformBlockSelection(TransformationMethod metho
|
||||
enableBlockSelection(positionBlock, anchorColumn, anchorBlock, positionColumn);
|
||||
}
|
||||
|
||||
void TextEditorWidgetPrivate::transformSelectedLines(ListTransformationMethod method)
|
||||
{
|
||||
if (!method || q->hasBlockSelection())
|
||||
return;
|
||||
|
||||
QTextCursor cursor = q->textCursor();
|
||||
if (!cursor.hasSelection())
|
||||
return;
|
||||
|
||||
const bool downwardDirection = cursor.anchor() < cursor.position();
|
||||
int startPosition = cursor.selectionStart();
|
||||
int endPosition = cursor.selectionEnd();
|
||||
|
||||
cursor.setPosition(startPosition);
|
||||
cursor.movePosition(QTextCursor::StartOfBlock);
|
||||
startPosition = cursor.position();
|
||||
|
||||
cursor.setPosition(endPosition, QTextCursor::KeepAnchor);
|
||||
if (cursor.positionInBlock() == 0)
|
||||
cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::KeepAnchor);
|
||||
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
||||
endPosition = qMax(cursor.position(), endPosition);
|
||||
|
||||
const QString text = cursor.selectedText();
|
||||
QStringList lines = text.split(QChar::ParagraphSeparator);
|
||||
method(lines);
|
||||
cursor.insertText(lines.join(QChar::ParagraphSeparator));
|
||||
|
||||
// (re)select the changed lines
|
||||
// Note: this assumes the transformation did not change the length
|
||||
cursor.setPosition(downwardDirection ? startPosition : endPosition);
|
||||
cursor.setPosition(downwardDirection ? endPosition : startPosition, QTextCursor::KeepAnchor);
|
||||
q->setTextCursor(cursor);
|
||||
}
|
||||
|
||||
void TextEditorWidget::inSnippetMode(bool *active)
|
||||
{
|
||||
*active = d->m_snippetOverlay->isVisible();
|
||||
|
||||
Reference in New Issue
Block a user