Skip to content
This repository was 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
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
@@ -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-/"
],
2 changes: 2 additions & 0 deletions src/command/Commands.js
Original file line number Diff line number Diff line change
@@ -73,6 +73,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
95 changes: 79 additions & 16 deletions src/editor/EditorCommandHandlers.js
Original file line number Diff line number Diff line change
@@ -716,6 +716,67 @@ 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),
isInlineWidget = !!EditorManager.getFocusedInlineWidget(),
lastLine = editor.getLastVisibleLine(),
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;
}

if (line > lastLine && isInlineWidget) {
doc.replaceRange("\n", {line: line - 1, ch: doc.getLine(line - 1).length}, null, "+input");
} else {
doc.replaceRange("\n", {line: line, ch: 0}, null, "+input");
}
cm.indentLine(line, "smart", false);
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.
*/
@@ -805,20 +866,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/root/strings.js
Original file line number Diff line number Diff line change
@@ -207,6 +207,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
461 changes: 392 additions & 69 deletions test/spec/EditorCommandHandlers-test.js

Large diffs are not rendered by default.