Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/ScintillaSorter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,33 @@ ScintillaSorter::ScintillaSorter(ScintillaNext *editor)
void ScintillaSorter::sort(const Sorter &sorter)
{
const QByteArray eol = editor->eolString();
const QByteArray text = readEditorText();
QVector<QByteArrayView> lines = ByteArrayUtils::split(text, eol);
QByteArray text = readEditorText();
const QByteArray selectedText = editor->getSelText();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just using the selected text will give odd results if partial lines are selected. It would need to sort whole lines. From the beginning of the line the selection starts, to the end of the line the end of the selection is on.

QVector<QByteArrayView> lines;
qsizetype textStartIndex = 0;

if (!selectedText.isEmpty())
{
textStartIndex = text.indexOf(selectedText);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will give incorrect results if the selectedText is in multiple location in the document.

lines = ByteArrayUtils::split(selectedText, eol);
}
else
{
lines = ByteArrayUtils::split(text, eol);
}

sorter.sort(lines);

QByteArray result = ByteArrayUtils::join(lines, eol);
writeEditorText(result);

text.replace(textStartIndex, result.size(), result.data());

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to update the entire file, once the start and end location are determined then only that section of the editor's contents can be replaced.

writeEditorText(text);
}

const QByteArray ScintillaSorter::readEditorText()
{
sptr_t pointer = editor->characterPointer();

int len = editor->length();

return QByteArray::fromRawData(reinterpret_cast<const char *>(pointer), len);
Expand All @@ -48,4 +63,4 @@ void ScintillaSorter::writeEditorText(const QByteArray &result)
{
editor->targetWholeDocument();
editor->replaceTarget(-1, result.constData());
}
}
Loading