-
-
Notifications
You must be signed in to change notification settings - Fork 904
Added fix for sort lines by selected text #1106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
| QVector<QByteArrayView> lines; | ||
| qsizetype textStartIndex = 0; | ||
|
|
||
| if (!selectedText.isEmpty()) | ||
| { | ||
| textStartIndex = text.indexOf(selectedText); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
@@ -48,4 +63,4 @@ void ScintillaSorter::writeEditorText(const QByteArray &result) | |
| { | ||
| editor->targetWholeDocument(); | ||
| editor->replaceTarget(-1, result.constData()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.