diff --git a/PowerEditor/src/NppCommands.cpp b/PowerEditor/src/NppCommands.cpp index 2d4683f4..91369fa2 100644 --- a/PowerEditor/src/NppCommands.cpp +++ b/PowerEditor/src/NppCommands.cpp @@ -341,18 +341,46 @@ void Notepad_plus::command(int id) break; case IDM_EDIT_SORTLINES: - { - _pEditView->execute(SCI_BEGINUNDOACTION); - _pEditView->quickSortLines(0, _pEditView->execute(SCI_GETLINECOUNT) - 1); - _pEditView->execute(SCI_ENDUNDOACTION); - } - break; - case IDM_EDIT_SORTLINESREVERSE: { + // default: no selection + size_t fromLine = 0; + size_t toLine = _pEditView->execute(SCI_GETLINECOUNT) - 1; + + // multi-selection is not allowed + if (_pEditView->execute(SCI_GETSELECTIONS) > 1) + return; + + // retangle-selection is not allowed + if (_pEditView->execute(SCI_SELECTIONISRECTANGLE)) + return; + + int selStart = _pEditView->execute(SCI_GETSELECTIONSTART); + int selEnd = _pEditView->execute(SCI_GETSELECTIONEND); + bool hasSelection = selStart != selEnd; + + pair lineRange = _pEditView->getSelectionLinesRange(); + + if (hasSelection) + { + // one single line selection is not allowed + if ((lineRange.second - lineRange.first) == 0) + return; + fromLine = lineRange.first; + toLine = lineRange.second; + } + _pEditView->execute(SCI_BEGINUNDOACTION); - _pEditView->quickSortLines(0, _pEditView->execute(SCI_GETLINECOUNT) - 1, true); + _pEditView->quickSortLines(fromLine, toLine, id == IDM_EDIT_SORTLINESREVERSE); _pEditView->execute(SCI_ENDUNDOACTION); + + if (hasSelection) // there was 1 selection, so we restore it + { + int posStart = _pEditView->execute(SCI_POSITIONFROMLINE, lineRange.first); + int posEnd = _pEditView->execute(SCI_GETLINEENDPOSITION, lineRange.second); + _pEditView->execute(SCI_SETSELECTIONSTART, posStart); + _pEditView->execute(SCI_SETSELECTIONEND, posEnd); + } } break; diff --git a/PowerEditor/src/ScitillaComponent/ScintillaEditView.cpp b/PowerEditor/src/ScitillaComponent/ScintillaEditView.cpp index 460f259d..c180d1f6 100644 --- a/PowerEditor/src/ScitillaComponent/ScintillaEditView.cpp +++ b/PowerEditor/src/ScitillaComponent/ScintillaEditView.cpp @@ -2216,11 +2216,12 @@ void ScintillaEditView::currentLineDown() const } } - +// Get selection range : (fromLine, toLine) +// return (-1, -1) if multi-selection pair ScintillaEditView::getSelectionLinesRange() const { pair range(-1, -1); - if (execute(SCI_GETSELECTIONS) > 1) + if (execute(SCI_GETSELECTIONS) > 1) // multi-selection return range; int start = execute(SCI_GETSELECTIONSTART); int end = execute(SCI_GETSELECTIONEND);