Skip to content

Commit

Permalink
Merge pull request #11 from ingorichter/ingo/shuffle-selection
Browse files Browse the repository at this point in the history
Ingo/shuffle selection
  • Loading branch information
ingorichter authored Jun 17, 2020
2 parents ffec7fd + 1005ce6 commit 762a93d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 66 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: node_js
node_js:
- 0.10
- 8.15.1
- 9.11.1
- 12.3.1
before_script:
- npm install -g grunt-cli
script:
Expand Down
144 changes: 91 additions & 53 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ define(function (require, exports) {
return allLines;
}

function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

function swap(array, indexA, indexB) {
var tmp = array[indexA];
array[indexA] = array[indexB];
array[indexB] = tmp;
}

function _shuffleArray(arrayOfStrings) {
// probably not the most efficient way of doing it...
var i,
entries = arrayOfStrings.length,
maxArrayIndex = entries - 1;

for (i = 0; i < entries; i++) {
var newIndex = getRandomInt(0, maxArrayIndex);
swap(arrayOfStrings, i, newIndex);
}

return arrayOfStrings;
}

// Function to run when the menu item is clicked
function handleSortLines() {
var editor = exports._getEditor(),
Expand Down Expand Up @@ -95,41 +119,40 @@ define(function (require, exports) {
}
}

function handleReverseLinesSelection() {
var editor = exports._getEditor(),
function handleReverseLinesSelection() {
var editor = exports._getEditor(),
codemirror = editor._codeMirror;

if (editor) {
if (editor.lineCount() > 0) {
if (codemirror.somethingSelected()) {
var selection = codemirror.getSelection();
var removedLastLineBreak = false;
if (editor) {
if (editor.lineCount() > 0) {
if (codemirror.somethingSelected()) {
var selection = codemirror.getSelection();
var removedLastLineBreak = false;

// preserve the last line break, because the last fully selected line has
// a line break at the end. We add this after the sort
if (selection.lastIndexOf("\n") === (selection.length - 1)) {
selection = selection.substr(0, selection.length - 1);
removedLastLineBreak = true;
}
// preserve the last line break, because the last fully selected line has
// a line break at the end. We add this after the sort
if (selection.lastIndexOf("\n") === (selection.length - 1)) {
selection = selection.substr(0, selection.length - 1);
removedLastLineBreak = true;
}

var allLines = selection.split("\n");
var i;
for (i = 0; i < allLines.length / 2; i++) {
var index = allLines.length - 1 - i;
var allLines = selection.split("\n");
var i;
for (i = 0; i < allLines.length / 2; i++) {
var index = allLines.length - 1 - i;

var tmp = allLines[i];
allLines[i] = allLines[index];
allLines[index] = tmp;
}
var tmp = allLines[i];
allLines[i] = allLines[index];
allLines[index] = tmp;
}

codemirror.replaceSelection(allLines.join("\n") + (removedLastLineBreak ? "\n" : ""));
codemirror.replaceSelection(allLines.join("\n") + (removedLastLineBreak ? "\n" : ""));
}
}
}
}
}


function handleReverseLines() {
function handleReverseLines() {
var codemirror = _getCodeMirror(),
allLines = getLines(codemirror);

Expand All @@ -156,28 +179,31 @@ define(function (require, exports) {
codemirror.setValue(allLines.join("\n"));
}

function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

function handleShuffleLines() {
var codemirror = _getCodeMirror(),
allLines = getLines(codemirror);
var editor = exports._getEditor(),
codemirror = editor._codeMirror;

// probably not the most efficient way of doing it...
var i,
lineCount = allLines.length,
maxArrayIndex = lineCount - 1;
if (editor.lineCount() > 0) {
if (codemirror.somethingSelected()) {
var selection = codemirror.getSelection();
var removedLastLineBreak = false;

for (i = 0; i < lineCount; i++) {
var newIndex = getRandomInt(0, maxArrayIndex);
// preserve the last line break, because the last fully selected line has
// a line break at the end. We add this after the sort
if (selection.lastIndexOf("\n") === (selection.length - 1)) {
selection = selection.substr(0, selection.length - 1);
removedLastLineBreak = true;
}

var tmp = allLines[i];
allLines[i] = allLines[newIndex];
allLines[newIndex] = tmp;
}
var allLines = _shuffleArray(selection.split("\n"));

codemirror.setValue(allLines.join("\n"));
codemirror.replaceSelection(allLines.join("\n") + (removedLastLineBreak ? "\n" : ""));
} else {
var allLines = getLines(codemirror),
result = _shuffleArray(allLines);
codemirror.setValue(result.join("\n"));
}
}
}

/**
Expand All @@ -204,11 +230,11 @@ define(function (require, exports) {
}

// First, register a command - a UI-less object associating an id to a handler
var COMMAND_SORTLINES = "de.richter.brackets.extension.brackets-sort-text.sortLines"; // package-style naming to avoid collisions
var COMMAND_REVERSELINES = "de.richter.brackets.extension.brackets-sort-text.reverseLines"; // package-style naming to avoid collisions
var COMMAND_SORTLINESBYLENGTH = "de.richter.brackets.extension.brackets-sort-text.sortLinesByLength"; // package-style naming to avoid collisions
var COMMAND_SHUFFLELINES = "de.richter.brackets.extension.brackets-sort-text.shuffleLines"; // package-style naming to avoid collisions
var COMMAND_UNIQUELINES = "de.richter.brackets.extension.brackets-sort-text.uniqueLines"; // package-style naming to avoid collisions
var COMMAND_SORTLINES = "de.richter.brackets.extension.brackets-sort-text.sortLines"; // package-style naming to avoid collisions
var COMMAND_REVERSELINES = "de.richter.brackets.extension.brackets-sort-text.reverseLines"; // package-style naming to avoid collisions
var COMMAND_SORTLINESBYLENGTH = "de.richter.brackets.extension.brackets-sort-text.sortLinesByLength"; // package-style naming to avoid collisions
var COMMAND_SHUFFLELINES = "de.richter.brackets.extension.brackets-sort-text.shuffleLines"; // package-style naming to avoid collisions
var COMMAND_UNIQUELINES = "de.richter.brackets.extension.brackets-sort-text.uniqueLines"; // package-style naming to avoid collisions
var COMMAND_REVERSELINESSELECTION = "de.richter.brackets.extension.brackets-sort-text.reverseLinesSelection";

CommandManager.register(Strings.SORT_LINES, COMMAND_SORTLINES, handleSortLines);
Expand All @@ -221,12 +247,24 @@ define(function (require, exports) {
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
// this check is there to prevent the testrunnner from failing to load the test
if (menu) {
menu.addMenuItem(COMMAND_SORTLINES, [{key: "F7"}]);
menu.addMenuItem(COMMAND_REVERSELINES, [{key: "Shift-F7"}]);
menu.addMenuItem(COMMAND_REVERSELINESSELECTION, [{key: "Shift-Ctrl-F7"}]);
menu.addMenuItem(COMMAND_SORTLINESBYLENGTH, [{key: "Ctrl-F7"}]);
menu.addMenuItem(COMMAND_SHUFFLELINES, [{key: "Alt-F7"}]);
menu.addMenuItem(COMMAND_UNIQUELINES, [{key: "Ctrl-Alt-F7"}]);
menu.addMenuItem(COMMAND_SORTLINES, [{
key: "F7"
}]);
menu.addMenuItem(COMMAND_REVERSELINES, [{
key: "Shift-F7"
}]);
menu.addMenuItem(COMMAND_REVERSELINESSELECTION, [{
key: "Shift-Ctrl-F7"
}]);
menu.addMenuItem(COMMAND_SORTLINESBYLENGTH, [{
key: "Ctrl-F7"
}]);
menu.addMenuItem(COMMAND_SHUFFLELINES, [{
key: "Alt-F7"
}]);
menu.addMenuItem(COMMAND_UNIQUELINES, [{
key: "Ctrl-Alt-F7"
}]);
}

// Public API
Expand Down
27 changes: 15 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Sort Text extension for Brackets. Provide operations to sort, reverse, randomize and remove duplicate lines.",
"author": "Ingo Richter <ingo.richter+github@gmail.com>",
"license": "MIT",
"version": "0.1.4",
"version": "0.1.5",
"homepage": "https://github.com/ingorichter/brackets-sort-text/",
"bugs": "https://github.com/ingorichter/brackets-sort-text/issues",
"repository": {
Expand All @@ -17,16 +17,16 @@
"utils"
],
"devDependencies": {
"grunt": "~0.4.5",
"grunt-cli": "~0.1.13",
"grunt-contrib-clean": "~0.6.0",
"grunt-contrib-compress": "~0.13.0",
"grunt-contrib-jshint": "~0.11.2",
"grunt-eslint": "^15.0.0",
"grunt-mocha": "~0.4.13",
"grunt-zip": "^0.16.2",
"load-grunt-tasks": "~3.2.0",
"time-grunt": "~1.2.1"
"grunt": "~1.1.0",
"grunt-cli": "~1.3.2",
"grunt-contrib-clean": "~2.0.0",
"grunt-contrib-compress": "~1.6.0",
"grunt-contrib-jshint": "^2.1.0",
"grunt-eslint": "^23.0.0",
"grunt-mocha": "^1.2.0",
"grunt-zip": "^0.18.2",
"load-grunt-tasks": "~5.1.0",
"time-grunt": "^2.0.0"
},
"engines": {
"brackets": ">=0.30.0"
Expand All @@ -40,5 +40,8 @@
"i18n": [
"en",
"de"
]
],
"dependencies": {
"eslint": "^7.2.0"
}
}

0 comments on commit 762a93d

Please # to comment.