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

Commit c5947a9

Browse files
committed
Merge pull request #2729 from zeis/openline
Add feature. Open line above/below the current line.
2 parents c687da9 + 5e2d29e commit c5947a9

5 files changed

Lines changed: 481 additions & 85 deletions

File tree

src/base-config/keyboard.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@
130130
"platform": "mac"
131131
}
132132
],
133+
"edit.openLineAbove": [
134+
"Ctrl-Shift-Enter"
135+
],
136+
"edit.openLineBelow": [
137+
"Ctrl-Enter"
138+
],
133139
"edit.lineComment": [
134140
"Ctrl-/"
135141
],

src/command/Commands.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ define(function (require, exports, module) {
7373
exports.EDIT_BLOCK_COMMENT = "edit.blockComment";
7474
exports.EDIT_LINE_UP = "edit.lineUp";
7575
exports.EDIT_LINE_DOWN = "edit.lineDown";
76+
exports.EDIT_OPEN_LINE_ABOVE = "edit.openLineAbove";
77+
exports.EDIT_OPEN_LINE_BELOW = "edit.openLineBelow";
7678
exports.TOGGLE_CLOSE_BRACKETS = "edit.autoCloseBrackets";
7779

7880
// VIEW

src/editor/EditorCommandHandlers.js

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,67 @@ define(function (require, exports, module) {
716716
moveLine(editor, DIRECTION_DOWN);
717717
}
718718

719+
/**
720+
* Inserts a new and smart indented line above/below the selected text, or current line if no selection.
721+
* The cursor is moved in the new line.
722+
* @param {Editor} editor - target editor
723+
* @param {Number} direction - direction where to place the new line (-1,+1) => (Up,Down)
724+
*/
725+
function openLine(editor, direction) {
726+
editor = editor || EditorManager.getFocusedEditor();
727+
if (!editor) {
728+
return;
729+
}
730+
731+
var sel = editor.getSelection(),
732+
hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch),
733+
isInlineWidget = !!EditorManager.getFocusedInlineWidget(),
734+
lastLine = editor.getLastVisibleLine(),
735+
cm = editor._codeMirror,
736+
doc = editor.document,
737+
line;
738+
739+
// Insert the new line
740+
switch (direction) {
741+
case DIRECTION_UP:
742+
line = sel.start.line;
743+
break;
744+
case DIRECTION_DOWN:
745+
line = sel.end.line;
746+
if (!(hasSelection && sel.end.ch === 0)) {
747+
// If not linewise selection
748+
line++;
749+
}
750+
break;
751+
}
752+
753+
if (line > lastLine && isInlineWidget) {
754+
doc.replaceRange("\n", {line: line - 1, ch: doc.getLine(line - 1).length}, null, "+input");
755+
} else {
756+
doc.replaceRange("\n", {line: line, ch: 0}, null, "+input");
757+
}
758+
cm.indentLine(line, "smart", false);
759+
editor.setSelection({line: line, ch: null});
760+
}
761+
762+
/**
763+
* Inserts a new and smart indented line above the selected text, or current line if no selection.
764+
* The cursor is moved in the new line.
765+
* @param {Editor} editor - target editor
766+
*/
767+
function openLineAbove(editor) {
768+
openLine(editor, DIRECTION_UP);
769+
}
770+
771+
/**
772+
* Inserts a new and smart indented line below the selected text, or current line if no selection.
773+
* The cursor is moved in the new line.
774+
* @param {Editor} editor - target editor
775+
*/
776+
function openLineBelow(editor) {
777+
openLine(editor, DIRECTION_DOWN);
778+
}
779+
719780
/**
720781
* Indent a line of text if no selection. Otherwise, indent all lines in selection.
721782
*/
@@ -805,20 +866,22 @@ define(function (require, exports, module) {
805866
}
806867

807868
// Register commands
808-
CommandManager.register(Strings.CMD_INDENT, Commands.EDIT_INDENT, indentText);
809-
CommandManager.register(Strings.CMD_UNINDENT, Commands.EDIT_UNINDENT, unidentText);
810-
CommandManager.register(Strings.CMD_COMMENT, Commands.EDIT_LINE_COMMENT, lineComment);
811-
CommandManager.register(Strings.CMD_BLOCK_COMMENT, Commands.EDIT_BLOCK_COMMENT, blockComment);
812-
CommandManager.register(Strings.CMD_DUPLICATE, Commands.EDIT_DUPLICATE, duplicateText);
813-
CommandManager.register(Strings.CMD_DELETE_LINES, Commands.EDIT_DELETE_LINES, deleteCurrentLines);
814-
CommandManager.register(Strings.CMD_LINE_UP, Commands.EDIT_LINE_UP, moveLineUp);
815-
CommandManager.register(Strings.CMD_LINE_DOWN, Commands.EDIT_LINE_DOWN, moveLineDown);
816-
CommandManager.register(Strings.CMD_SELECT_LINE, Commands.EDIT_SELECT_LINE, selectLine);
817-
818-
CommandManager.register(Strings.CMD_UNDO, Commands.EDIT_UNDO, handleUndo);
819-
CommandManager.register(Strings.CMD_REDO, Commands.EDIT_REDO, handleRedo);
820-
CommandManager.register(Strings.CMD_CUT, Commands.EDIT_CUT, ignoreCommand);
821-
CommandManager.register(Strings.CMD_COPY, Commands.EDIT_COPY, ignoreCommand);
822-
CommandManager.register(Strings.CMD_PASTE, Commands.EDIT_PASTE, ignoreCommand);
823-
CommandManager.register(Strings.CMD_SELECT_ALL, Commands.EDIT_SELECT_ALL, _handleSelectAll);
869+
CommandManager.register(Strings.CMD_INDENT, Commands.EDIT_INDENT, indentText);
870+
CommandManager.register(Strings.CMD_UNINDENT, Commands.EDIT_UNINDENT, unidentText);
871+
CommandManager.register(Strings.CMD_COMMENT, Commands.EDIT_LINE_COMMENT, lineComment);
872+
CommandManager.register(Strings.CMD_BLOCK_COMMENT, Commands.EDIT_BLOCK_COMMENT, blockComment);
873+
CommandManager.register(Strings.CMD_DUPLICATE, Commands.EDIT_DUPLICATE, duplicateText);
874+
CommandManager.register(Strings.CMD_DELETE_LINES, Commands.EDIT_DELETE_LINES, deleteCurrentLines);
875+
CommandManager.register(Strings.CMD_LINE_UP, Commands.EDIT_LINE_UP, moveLineUp);
876+
CommandManager.register(Strings.CMD_LINE_DOWN, Commands.EDIT_LINE_DOWN, moveLineDown);
877+
CommandManager.register(Strings.CMD_OPEN_LINE_ABOVE, Commands.EDIT_OPEN_LINE_ABOVE, openLineAbove);
878+
CommandManager.register(Strings.CMD_OPEN_LINE_BELOW, Commands.EDIT_OPEN_LINE_BELOW, openLineBelow);
879+
CommandManager.register(Strings.CMD_SELECT_LINE, Commands.EDIT_SELECT_LINE, selectLine);
880+
881+
CommandManager.register(Strings.CMD_UNDO, Commands.EDIT_UNDO, handleUndo);
882+
CommandManager.register(Strings.CMD_REDO, Commands.EDIT_REDO, handleRedo);
883+
CommandManager.register(Strings.CMD_CUT, Commands.EDIT_CUT, ignoreCommand);
884+
CommandManager.register(Strings.CMD_COPY, Commands.EDIT_COPY, ignoreCommand);
885+
CommandManager.register(Strings.CMD_PASTE, Commands.EDIT_PASTE, ignoreCommand);
886+
CommandManager.register(Strings.CMD_SELECT_ALL, Commands.EDIT_SELECT_ALL, _handleSelectAll);
824887
});

src/nls/root/strings.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ define({
207207
"CMD_BLOCK_COMMENT" : "Toggle Block Comment",
208208
"CMD_LINE_UP" : "Move Line Up",
209209
"CMD_LINE_DOWN" : "Move Line Down",
210+
"CMD_OPEN_LINE_ABOVE" : "Open Line Above",
211+
"CMD_OPEN_LINE_BELOW" : "Open Line Below",
210212
"CMD_TOGGLE_CLOSE_BRACKETS" : "Auto Close Braces",
211213

212214
// View menu commands

0 commit comments

Comments
 (0)