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

Add feature. Open line above/below the current line. #2729

Merged
merged 19 commits into from
Apr 22, 2013
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/base-config/keyboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@
"platform": "mac"
}
],
"edit.openLineAbove": [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These keyboard shortcuts are actually the reverse of what I'd expect. ctrl-enter for open line below and ctrl-shift-enter for open line above. I just fired up Sublime 3 and it agrees with me. From my own usage, I've probably opened lines below 10 times for every time I've opened above...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you on this. I also open lines below more frequently than above. I just kept the same shortcuts as in Visual Studio. I never used Sublime Text.
In the last commit they have been inverted.

"Ctrl-Shift-Enter"
],
"edit.openLineBelow": [
"Ctrl-Enter"
],
"edit.lineComment": [
"Ctrl-/"
],
Expand Down
2 changes: 2 additions & 0 deletions src/command/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ define(function (require, exports, module) {
exports.EDIT_BLOCK_COMMENT = "edit.blockComment";
exports.EDIT_LINE_UP = "edit.lineUp";
exports.EDIT_LINE_DOWN = "edit.lineDown";
exports.EDIT_OPEN_LINE_ABOVE = "edit.openLineAbove";
exports.EDIT_OPEN_LINE_BELOW = "edit.openLineBelow";
exports.TOGGLE_CLOSE_BRACKETS = "edit.autoCloseBrackets";

// VIEW
Expand Down
91 changes: 75 additions & 16 deletions src/editor/EditorCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,63 @@ define(function (require, exports, module) {
moveLine(editor, DIRECTION_DOWN);
}

/**
* Inserts a new and smart indented line above/below the selected text, or current line if no selection.
* The cursor is moved in the new line.
* @param {Editor} editor - target editor
* @param {Number} direction - direction where to place the new line (-1,+1) => (Up,Down)
*/
function openLine(editor, direction) {
editor = editor || EditorManager.getFocusedEditor();
if (!editor) {
return;
}

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

// Insert the new line
switch (direction) {
case DIRECTION_UP:
line = sel.start.line;
break;
case DIRECTION_DOWN:
line = sel.end.line;
if (!(hasSelection && sel.end.ch === 0)) {
// If not linewise selection
line++;
}
break;
}

doc.batchOperation(function () {
doc.replaceRange("\n", {line: line, ch: 0});
cm.indentLine(line);
});
editor.setSelection({line: line, ch: null});
}

/**
* Inserts a new and smart indented line above the selected text, or current line if no selection.
* The cursor is moved in the new line.
* @param {Editor} editor - target editor
*/
function openLineAbove(editor) {
openLine(editor, DIRECTION_UP);
}

/**
* Inserts a new and smart indented line below the selected text, or current line if no selection.
* The cursor is moved in the new line.
* @param {Editor} editor - target editor
*/
function openLineBelow(editor) {
openLine(editor, DIRECTION_DOWN);
}

/**
* Indent a line of text if no selection. Otherwise, indent all lines in selection.
*/
Expand Down Expand Up @@ -786,20 +843,22 @@ define(function (require, exports, module) {
}

// Register commands
CommandManager.register(Strings.CMD_INDENT, Commands.EDIT_INDENT, indentText);
CommandManager.register(Strings.CMD_UNINDENT, Commands.EDIT_UNINDENT, unidentText);
CommandManager.register(Strings.CMD_COMMENT, Commands.EDIT_LINE_COMMENT, lineComment);
CommandManager.register(Strings.CMD_BLOCK_COMMENT, Commands.EDIT_BLOCK_COMMENT, blockComment);
CommandManager.register(Strings.CMD_DUPLICATE, Commands.EDIT_DUPLICATE, duplicateText);
CommandManager.register(Strings.CMD_DELETE_LINES, Commands.EDIT_DELETE_LINES, deleteCurrentLines);
CommandManager.register(Strings.CMD_LINE_UP, Commands.EDIT_LINE_UP, moveLineUp);
CommandManager.register(Strings.CMD_LINE_DOWN, Commands.EDIT_LINE_DOWN, moveLineDown);
CommandManager.register(Strings.CMD_SELECT_LINE, Commands.EDIT_SELECT_LINE, selectLine);

CommandManager.register(Strings.CMD_UNDO, Commands.EDIT_UNDO, handleUndo);
CommandManager.register(Strings.CMD_REDO, Commands.EDIT_REDO, handleRedo);
CommandManager.register(Strings.CMD_CUT, Commands.EDIT_CUT, ignoreCommand);
CommandManager.register(Strings.CMD_COPY, Commands.EDIT_COPY, ignoreCommand);
CommandManager.register(Strings.CMD_PASTE, Commands.EDIT_PASTE, ignoreCommand);
CommandManager.register(Strings.CMD_SELECT_ALL, Commands.EDIT_SELECT_ALL, _handleSelectAll);
CommandManager.register(Strings.CMD_INDENT, Commands.EDIT_INDENT, indentText);
CommandManager.register(Strings.CMD_UNINDENT, Commands.EDIT_UNINDENT, unidentText);
CommandManager.register(Strings.CMD_COMMENT, Commands.EDIT_LINE_COMMENT, lineComment);
CommandManager.register(Strings.CMD_BLOCK_COMMENT, Commands.EDIT_BLOCK_COMMENT, blockComment);
CommandManager.register(Strings.CMD_DUPLICATE, Commands.EDIT_DUPLICATE, duplicateText);
CommandManager.register(Strings.CMD_DELETE_LINES, Commands.EDIT_DELETE_LINES, deleteCurrentLines);
CommandManager.register(Strings.CMD_LINE_UP, Commands.EDIT_LINE_UP, moveLineUp);
CommandManager.register(Strings.CMD_LINE_DOWN, Commands.EDIT_LINE_DOWN, moveLineDown);
CommandManager.register(Strings.CMD_OPEN_LINE_ABOVE, Commands.EDIT_OPEN_LINE_ABOVE, openLineAbove);
CommandManager.register(Strings.CMD_OPEN_LINE_BELOW, Commands.EDIT_OPEN_LINE_BELOW, openLineBelow);
CommandManager.register(Strings.CMD_SELECT_LINE, Commands.EDIT_SELECT_LINE, selectLine);

CommandManager.register(Strings.CMD_UNDO, Commands.EDIT_UNDO, handleUndo);
CommandManager.register(Strings.CMD_REDO, Commands.EDIT_REDO, handleRedo);
CommandManager.register(Strings.CMD_CUT, Commands.EDIT_CUT, ignoreCommand);
CommandManager.register(Strings.CMD_COPY, Commands.EDIT_COPY, ignoreCommand);
CommandManager.register(Strings.CMD_PASTE, Commands.EDIT_PASTE, ignoreCommand);
CommandManager.register(Strings.CMD_SELECT_ALL, Commands.EDIT_SELECT_ALL, _handleSelectAll);
});
2 changes: 2 additions & 0 deletions src/nls/it/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ define({
"CMD_BLOCK_COMMENT" : "Commenta/De-commenta blocco",
"CMD_LINE_UP" : "Sposta la riga in alto",
"CMD_LINE_DOWN" : "Sposta la riga in basso",
"CMD_OPEN_LINE_ABOVE" : "Apri linea in alto",
"CMD_OPEN_LINE_BELOW" : "Apri linea in basso",

// View menu commands
"VIEW_MENU" : "Vista",
Expand Down
2 changes: 2 additions & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ define({
"CMD_BLOCK_COMMENT" : "Toggle Block Comment",
"CMD_LINE_UP" : "Move Line Up",
"CMD_LINE_DOWN" : "Move Line Down",
"CMD_OPEN_LINE_ABOVE" : "Open Line Above",
"CMD_OPEN_LINE_BELOW" : "Open Line Below",
"CMD_TOGGLE_CLOSE_BRACKETS" : "Auto Close Braces",

// View menu commands
Expand Down
Loading