diff --git a/src/editor/EditorCommandHandlers.js b/src/editor/EditorCommandHandlers.js index e0d7c9edb39..fdad1ea48bb 100644 --- a/src/editor/EditorCommandHandlers.js +++ b/src/editor/EditorCommandHandlers.js @@ -133,20 +133,23 @@ define(function (require, exports, module) { if (!editor) { return; } - - var sel = editor.getSelection(); - - var hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch); - + + var sel = editor.getSelection(), + hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch), + delimiter = ""; + if (!hasSelection) { sel.start.ch = 0; sel.end = {line: sel.start.line + 1, ch: 0}; + if (sel.end.line === editor.lineCount()) { + delimiter = "\n"; + } } - + // Make the edit var doc = editor.document; - - var selectedText = doc.getRange(sel.start, sel.end); + + var selectedText = doc.getRange(sel.start, sel.end) + delimiter; doc.replaceRange(selectedText, sel.start); } diff --git a/test/spec/EditorCommandHandlers-test.js b/test/spec/EditorCommandHandlers-test.js index 36a94901ae6..80dc09e1058 100644 --- a/test/spec/EditorCommandHandlers-test.js +++ b/test/spec/EditorCommandHandlers-test.js @@ -368,6 +368,22 @@ define(function (require, exports, module) { expect(myDocument.getText()).toEqual(expectedText); expectCursorAt({line: 2, ch: 10}); }); + + it("should duplicate line + \n if selected line is at end of file", function () { + var lines = defaultContent.split("\n"), + len = lines.length; + + // place cursor at the beginning of the last line + myEditor.setCursorPos(len - 1, 0); + + CommandManager.execute(Commands.EDIT_DUPLICATE, myEditor); + + lines.push("}"); + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectCursorAt({line: len, ch: 0}); + }); it("should duplicate first line", function () { // place cursor at start of line 0