Add ability to sort lines randomly

Close #8682, close #8683
pull/8685/head
Scott Sumner 2020-08-07 16:46:30 -04:00 committed by Don HO
parent 1efac7dacb
commit ca3d514722
6 changed files with 28 additions and 1 deletions

View File

@ -120,6 +120,7 @@
<Item id="42064" name="Sort Lines As Decimals (Comma) Descending"/>
<Item id="42065" name="Sort Lines As Decimals (Dot) Ascending"/>
<Item id="42066" name="Sort Lines As Decimals (Dot) Descending"/>
<Item id="42078" name="Sort Lines Randomly"/>
<Item id="42016" name="&amp;UPPERCASE"/>
<Item id="42017" name="&amp;lowercase"/>
<Item id="42067" name="&amp;Proper Case"/>

View File

@ -31,6 +31,7 @@
#include <algorithm>
#include <utility>
#include <random>
// Base interface for line sorting.
class ISorter
@ -385,4 +386,19 @@ protected:
}
};
class RandomSorter : public ISorter
{
public:
unsigned seed;
RandomSorter(bool isDescending, size_t fromColumn, size_t toColumn) : ISorter(isDescending, fromColumn, toColumn)
{
seed = static_cast<unsigned>(time(NULL));
}
std::vector<generic_string> sort(std::vector<generic_string> lines) override
{
std::shuffle(lines.begin(), lines.end(), std::default_random_engine(seed));
return lines;
}
};
#endif //NPP_SORTERS_H

View File

@ -322,6 +322,8 @@ BEGIN
MENUITEM "Sort Lines As Integers Descending", IDM_EDIT_SORTLINES_INTEGER_DESCENDING
MENUITEM "Sort Lines As Decimals (Comma) Descending", IDM_EDIT_SORTLINES_DECIMALCOMMA_DESCENDING
MENUITEM "Sort Lines As Decimals (Dot) Descending", IDM_EDIT_SORTLINES_DECIMALDOT_DESCENDING
MENUITEM SEPARATOR
MENUITEM "Sort Lines Randomly", IDM_EDIT_SORTLINES_RANDOMLY
END
POPUP "Comment/Uncomment"
BEGIN

View File

@ -550,6 +550,7 @@ void Notepad_plus::command(int id)
case IDM_EDIT_SORTLINES_DECIMALCOMMA_DESCENDING:
case IDM_EDIT_SORTLINES_DECIMALDOT_ASCENDING:
case IDM_EDIT_SORTLINES_DECIMALDOT_DESCENDING:
case IDM_EDIT_SORTLINES_RANDOMLY:
{
std::lock_guard<std::mutex> lock(command_mutex);
@ -619,10 +620,14 @@ void Notepad_plus::command(int id)
{
pSorter = std::unique_ptr<ISorter>(new DecimalCommaSorter(isDescending, fromColumn, toColumn));
}
else
else if (id == IDM_EDIT_SORTLINES_DECIMALDOT_DESCENDING || id == IDM_EDIT_SORTLINES_DECIMALDOT_ASCENDING)
{
pSorter = std::unique_ptr<ISorter>(new DecimalDotSorter(isDescending, fromColumn, toColumn));
}
else
{
pSorter = std::unique_ptr<ISorter>(new RandomSorter(isDescending, fromColumn, toColumn));
}
try
{
_pEditView->sortLines(fromLine, toLine, pSorter.get());
@ -3449,6 +3454,7 @@ void Notepad_plus::command(int id)
case IDM_EDIT_SORTLINES_DECIMALCOMMA_DESCENDING:
case IDM_EDIT_SORTLINES_DECIMALDOT_ASCENDING:
case IDM_EDIT_SORTLINES_DECIMALDOT_DESCENDING:
case IDM_EDIT_SORTLINES_RANDOMLY:
case IDM_EDIT_BLANKLINEABOVECURRENT:
case IDM_EDIT_BLANKLINEBELOWCURRENT:
case IDM_VIEW_FULLSCREENTOGGLE :

View File

@ -144,6 +144,7 @@ static const WinMenuKeyDefinition winKeyDefs[] =
{ VK_NULL, IDM_EDIT_SORTLINES_DECIMALCOMMA_DESCENDING, false, false, false, nullptr },
{ VK_NULL, IDM_EDIT_SORTLINES_DECIMALDOT_ASCENDING, false, false, false, nullptr },
{ VK_NULL, IDM_EDIT_SORTLINES_DECIMALDOT_DESCENDING, false, false, false, nullptr },
{ VK_NULL, IDM_EDIT_SORTLINES_RANDOMLY, false, false, false, nullptr },
{ VK_Q, IDM_EDIT_BLOCK_COMMENT, true, false, false, nullptr },
{ VK_K, IDM_EDIT_BLOCK_COMMENT_SET, true, false, false, nullptr },
{ VK_K, IDM_EDIT_BLOCK_UNCOMMENT, true, false, true, nullptr },

View File

@ -130,6 +130,7 @@
#define IDM_EDIT_SORTLINES_DECIMALCOMMA_DESCENDING (IDM_EDIT + 64)
#define IDM_EDIT_SORTLINES_DECIMALDOT_ASCENDING (IDM_EDIT + 65)
#define IDM_EDIT_SORTLINES_DECIMALDOT_DESCENDING (IDM_EDIT + 66)
#define IDM_EDIT_SORTLINES_RANDOMLY (IDM_EDIT + 78)
#define IDM_EDIT_OPENASFILE (IDM_EDIT + 73)
#define IDM_EDIT_OPENINFOLDER (IDM_EDIT + 74)