@@ -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} ) ;
0 commit comments