Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1282 from ShadowCloud/master
Browse files Browse the repository at this point in the history
Command to shift line(s) up/down
  • Loading branch information
peterflynn committed Jul 31, 2012
2 parents 4c4d40c + 45034aa commit f8349ae
Show file tree
Hide file tree
Showing 5 changed files with 409 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/command/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ define(function (require, exports, module) {
exports.EDIT_UNINDENT = "edit.unindent";
exports.EDIT_DUPLICATE = "edit.duplicate";
exports.EDIT_LINE_COMMENT = "edit.lineComment";
exports.EDIT_LINE_UP = "edit.lineUp";
exports.EDIT_LINE_DOWN = "edit.lineDown";
exports.TOGGLE_USE_TAB_CHARS = "debug.useTabChars";

// VIEW
Expand Down
9 changes: 9 additions & 0 deletions src/command/Menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,15 @@ define(function (require, exports, module) {
menu.addMenuItem(Commands.EDIT_INDENT, [{key: "Indent", displayKey: "Tab"}]);
menu.addMenuItem(Commands.EDIT_UNINDENT, [{key: "Unindent", displayKey: "Shift-Tab"}]);
menu.addMenuItem(Commands.EDIT_DUPLICATE, "Ctrl-D");
menu.addMenuItem(Commands.EDIT_LINE_UP, [{key: "Ctrl-Shift-Up", displayKey: "Ctrl-Shift-\u2191",
platform: "win"},
{key: "Cmd-Ctrl-Up", displayKey: "Cmd-Ctrl-\u2191",
platform: "mac"}]);
menu.addMenuItem(Commands.EDIT_LINE_DOWN, [{key: "Ctrl-Shift-Down", displayKey: "Ctrl-Shift-\u2191",
platform: "win"},
{key: "Cmd-Ctrl-Down", displayKey: "Cmd-Ctrl-\u2191",
platform: "mac"}]);
menu.addMenuDivider();
menu.addMenuItem(Commands.EDIT_LINE_COMMENT, "Ctrl-/");
menu.addMenuDivider();
menu.addMenuItem(Commands.TOGGLE_USE_TAB_CHARS);
Expand Down
88 changes: 88 additions & 0 deletions src/editor/EditorCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ define(function (require, exports, module) {
EditorManager = require("editor/EditorManager");


/**
* List of constants
*/
var DIRECTION_UP = -1;
var DIRECTION_DOWN = +1;

/**
* Add or remove line-comment tokens to all the lines in the selected range, preserving selection
* and cursor position. Applies to currently focused Editor.
Expand Down Expand Up @@ -152,6 +158,86 @@ define(function (require, exports, module) {
var selectedText = doc.getRange(sel.start, sel.end) + delimiter;
doc.replaceRange(selectedText, sel.start);
}

/**
* Moves the selected text, or current line if no selection. The cursor/selection
* moves with the line/lines.
* @param {Editor} editor - target editor
* @param {Number} direction - direction of the move (-1,+1) => (Up,Down)
*/
function moveLine(editor, direction) {
editor = editor || EditorManager.getFocusedEditor();
if (!editor) {
return;
}

var doc = editor.document,
sel = editor.getSelection(),
originalSel = editor.getSelection(),
hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch);

sel.start.ch = 0;
// The end of the selection becomes the start of the next line, if it isn't already
if (!hasSelection || sel.end.ch !== 0) {
sel.end = {line: sel.end.line + 1, ch: 0};
}

// Make the move
switch (direction) {
case DIRECTION_UP:
if (sel.start.line !== 0) {
doc.batchOperation(function () {
var prevText = doc.getRange({ line: sel.start.line - 1, ch: 0 }, sel.start);

if (sel.end.line === editor.lineCount()) {
prevText = "\n" + prevText.substring(0, prevText.length - 1);
}

doc.replaceRange("", { line: sel.start.line - 1, ch: 0 }, sel.start);
doc.replaceRange(prevText, { line: sel.end.line - 1, ch: 0 });

// Make sure CodeMirror hasn't expanded the selection to include
// the line we inserted below.
originalSel.start.line--;
originalSel.end.line--;
editor.setSelection(originalSel.start, originalSel.end);
});
}
break;
case DIRECTION_DOWN:
if (sel.end.line < editor.lineCount()) {
doc.batchOperation(function () {
var nextText = doc.getRange(sel.end, { line: sel.end.line + 1, ch: 0 });

var deletionStart = sel.end;
if (sel.end.line === editor.lineCount() - 1) {
nextText += "\n";
deletionStart = { line: sel.end.line - 1, ch: doc.getLine(sel.end.line - 1).length };
}

doc.replaceRange("", deletionStart, { line: sel.end.line + 1, ch: 0 });
doc.replaceRange(nextText, { line: sel.start.line, ch: 0 });
});
}
break;
}
}

/**
* Moves the selected text, or current line if no selection, one line up. The cursor/selection
* moves with the line/lines.
*/
function moveLineUp(editor) {
moveLine(editor, DIRECTION_UP);
}

/**
* Moves the selected text, or current line if no selection, one line down. The cursor/selection
* moves with the line/lines.
*/
function moveLineDown(editor) {
moveLine(editor, DIRECTION_DOWN);
}

/**
* Indent a line of text if no selection. Otherwise, indent all lines in selection.
Expand Down Expand Up @@ -182,4 +268,6 @@ define(function (require, exports, module) {
CommandManager.register(Strings.CMD_UNINDENT, Commands.EDIT_UNINDENT, unidentText);
CommandManager.register(Strings.CMD_COMMENT, Commands.EDIT_LINE_COMMENT, lineComment);
CommandManager.register(Strings.CMD_DUPLICATE, Commands.EDIT_DUPLICATE, duplicateText);
CommandManager.register(Strings.CMD_LINE_UP, Commands.EDIT_LINE_UP, moveLineUp);
CommandManager.register(Strings.CMD_LINE_DOWN, Commands.EDIT_LINE_DOWN, moveLineDown);
});
2 changes: 2 additions & 0 deletions src/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ define(function (require, exports, module) {
exports.CMD_UNINDENT = "Unindent";
exports.CMD_DUPLICATE = "Duplicate";
exports.CMD_COMMENT = "Comment/Uncomment Lines";
exports.CMD_LINE_UP = "Move Line(s) Up";
exports.CMD_LINE_DOWN = "Move Line(s) Down";

// View menu commands
exports.VIEW_MENU = "View";
Expand Down
Loading

0 comments on commit f8349ae

Please sign in to comment.