Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 11 additions & 8 deletions src/editor/EditorCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
16 changes: 16 additions & 0 deletions test/spec/EditorCommandHandlers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
});
Copy link
Member

Choose a reason for hiding this comment

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

Could you reorder so this follows the same pattern as the other unit tests in this file? Move cursor first, execute command, then init lines & expectedText, then check result. Also, note that because we use JSLint we don't strictly keep all vars at the top; so it's unnecessary to declare expectedText initialized to "" and then immediately overwrite it with another value -- just move the var statement to where it's initialized for real.

Copy link
Author

Choose a reason for hiding this comment

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

I'm happy to do it that way, though I'll register an opinion that it's always a good idea – especially with lots of developers in the kitchen – to declare your vars at the top of a function/method, both to pre-empt any hoisting issues and to establish expectations.


it("should duplicate first line", function () {
// place cursor at start of line 0
Expand Down