[ENHANCEMENT] Enhance sort lines feature: sort selected lines if several lines are selected.

git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@1163 f5eea248-9336-0410-98b8-ebc06183d4e3
remotes/trunk
Don Ho 11 years ago
parent 3fc26bbcf8
commit adc711fb09

@ -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<int, int> 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;

@ -2216,11 +2216,12 @@ void ScintillaEditView::currentLineDown() const
}
}
// Get selection range : (fromLine, toLine)
// return (-1, -1) if multi-selection
pair<int, int> ScintillaEditView::getSelectionLinesRange() const
{
pair<int, int> 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);

Loading…
Cancel
Save