Skip to content
This repository has been 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
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Improve Open Line feature. Add additional unit tests.
  • Loading branch information
zeis committed Feb 12, 2013
commit 056f84a26699769517b4aee95b18c0c5a6f17112
2 changes: 0 additions & 2 deletions src/command/Menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,8 +1039,6 @@ define(function (require, exports, module) {
menu.addMenuItem(Commands.EDIT_DELETE_LINES);
menu.addMenuItem(Commands.EDIT_LINE_UP);
menu.addMenuItem(Commands.EDIT_LINE_DOWN);
menu.addMenuItem(Commands.EDIT_OPEN_LINE_ABOVE);
menu.addMenuItem(Commands.EDIT_OPEN_LINE_BELOW);
menu.addMenuDivider();
menu.addMenuItem(Commands.EDIT_LINE_COMMENT);
menu.addMenuItem(Commands.EDIT_BLOCK_COMMENT);
Expand Down
19 changes: 10 additions & 9 deletions src/editor/EditorCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,24 +663,25 @@ define(function (require, exports, module) {

// Insert the new line
var doc = editor.document;
var line;

switch (direction) {
case DIRECTION_UP:
doc.replaceRange("\n", {line: sel.start.line - 1});
editor._codeMirror.indentLine(sel.start.line);
editor.setCursorPos(sel.start.line, doc.getLine(sel.start.line).length);
line = sel.start.line;
break;
case DIRECTION_DOWN:
if (hasSelection && sel.end.ch === 0) {
// Compesate cursor position in linewise selection
sel.end.line--;
// If linewise selection
line = sel.end.line;
} else {
line = ++sel.end.line;
}

doc.replaceRange("\n", {line: sel.end.line});
editor._codeMirror.indentLine(sel.end.line + 1);
editor.setCursorPos(sel.end.line + 1, doc.getLine(sel.end.line + 1).length);
break;
}

doc.replaceRange("\n", {line: line, ch: 0});
editor._codeMirror.indentLine(line);
editor.setCursorPos(line, doc.getLine(line).length);
}

/**
Expand Down
205 changes: 127 additions & 78 deletions test/spec/EditorCommandHandlers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,16 @@ define(function (require, exports, module) {
});

describe("Open Line Above and Below", function () {
var indentUnit = Editor.getIndentUnit();
Copy link
Contributor

Choose a reason for hiding this comment

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

This API was changed in the last sprint to Editor.getSpaceUnits().

var indentation = (function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could actually just do var indentation = (new Array(indentUnit + 1)).join(" ") to get the indentUnit spaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It raises the JSLint literal notation error.

Copy link
Contributor

Choose a reason for hiding this comment

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

Woops, I just noticed that @dangoor made the same suggestion. Lets leave it like that then.

// generate indent string once
var spaces = [];
while (spaces.length < indentUnit) {
spaces.push(" ");
}
return spaces.join("");
}());

beforeEach(setupFullEditor);

it("should insert new line above if no selection", function () {
Expand All @@ -1039,23 +1049,43 @@ define(function (require, exports, module) {
CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor);

var lines = defaultContent.split("\n");

var indentUnit = Editor.getIndentUnit();
var spaces = [];
while (spaces.length < indentUnit) {
spaces.push(" ");
}
var indentation = spaces.join("");
lines.splice(1, 0, indentation);
var expectedText = lines.join("\n");

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 1, ch: indentUnit});
});

it("should insert new line above the first line if no selection", function () {
// place cursor in the first line
myEditor.setCursorPos(0, 0);

CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor);

var lines = defaultContent.split("\n");
lines.splice(0, 0, "");
var expectedText = lines.join("\n");

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 0, ch: 0});
});

it("should insert new line above the last line if no selection", function () {
// place cursor in the last line
myEditor.setCursorPos(7, 0);

CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor);

var lines = defaultContent.split("\n");
lines.splice(7, 0, indentation);
var expectedText = lines.join("\n");

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 1, ch: 4});
expectCursorAt({line: 7, ch: indentUnit});
});

it("should insert new line above with no indentation if no selection", function () {
// place cursor in line 0
// place cursor in the middle of line 0
myEditor.setCursorPos(0, 10);

CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor);
Expand All @@ -1075,19 +1105,11 @@ define(function (require, exports, module) {
CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor);

var lines = defaultContent.split("\n");

var indentUnit = Editor.getIndentUnit();
var spaces = [];
while (spaces.length < indentUnit) {
spaces.push(" ");
}
var indentation = spaces.join("");
lines.splice(1, 0, indentation);

var expectedText = lines.join("\n");

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 1, ch: 4});
expectCursorAt({line: 1, ch: indentUnit});
});

it("should insert new line above when linewise selection", function () {
Expand All @@ -1097,19 +1119,11 @@ define(function (require, exports, module) {
CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor);

var lines = defaultContent.split("\n");

var indentUnit = Editor.getIndentUnit();
var spaces = [];
while (spaces.length < indentUnit) {
spaces.push(" ");
}
var indentation = spaces.join("");
lines.splice(1, 0, indentation);

var expectedText = lines.join("\n");

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 1, ch: 4});
expectCursorAt({line: 1, ch: indentUnit});
});

it("should insert new line above when multiple line selection", function () {
Expand All @@ -1119,20 +1133,11 @@ define(function (require, exports, module) {
CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor);

var lines = defaultContent.split("\n");

var indentUnit = Editor.getIndentUnit();
indentUnit *= 2;
var spaces = [];
while (spaces.length < indentUnit) {
spaces.push(" ");
}
var indentation = spaces.join("");
lines.splice(2, 0, indentation);

lines.splice(2, 0, " " + indentation);
var expectedText = lines.join("\n");

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 2, ch: 8});
expectCursorAt({line: 2, ch: 4 + indentUnit});
});

it("should insert new line below when no selection", function () {
Expand All @@ -1142,24 +1147,44 @@ define(function (require, exports, module) {
CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor);

var lines = defaultContent.split("\n");
lines.splice(1, 0, indentation);
var expectedText = lines.join("\n");

var indentUnit = Editor.getIndentUnit();
var spaces = [];
while (spaces.length < indentUnit) {
spaces.push(" ");
}
var indentation = spaces.join("");
expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 1, ch: indentUnit});
});

it("should insert new line below the first line if no selection", function () {
// place cursor in the first line
myEditor.setCursorPos(0, 0);

CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor);

var lines = defaultContent.split("\n");
lines.splice(1, 0, indentation);
var expectedText = lines.join("\n");

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 1, ch: indentUnit});
});

it("should insert new line below the last line if no selection", function () {
// place cursor in the last line
myEditor.setCursorPos(7, 0);

CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor);

var lines = defaultContent.split("\n");
lines.splice(8, 0, "");
var expectedText = lines.join("\n");

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 1, ch: 4});
expectCursorAt({line: 8, ch: 0});
});

it("should insert new line below with no indentation if no selection", function () {
// place cursor in line 7
myEditor.setCursorPos(7, 0);
// place cursor in line 7 character 1
myEditor.setCursorPos(7, 1);

CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor);

Expand All @@ -1178,19 +1203,11 @@ define(function (require, exports, module) {
CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor);

var lines = defaultContent.split("\n");

var indentUnit = Editor.getIndentUnit();
var spaces = [];
while (spaces.length < indentUnit) {
spaces.push(" ");
}
var indentation = spaces.join("");
lines.splice(1, 0, indentation);

var expectedText = lines.join("\n");

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 1, ch: 4});
expectCursorAt({line: 1, ch: indentUnit});
});

it("should insert new line below when linewise selection", function () {
Expand All @@ -1200,20 +1217,11 @@ define(function (require, exports, module) {
CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor);

var lines = defaultContent.split("\n");

var indentUnit = Editor.getIndentUnit();
indentUnit *= 2;
var spaces = [];
while (spaces.length < indentUnit) {
spaces.push(" ");
}
var indentation = spaces.join("");
lines.splice(3, 0, indentation);

lines.splice(3, 0, " " + indentation);
var expectedText = lines.join("\n");

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 3, ch: 8});
expectCursorAt({line: 3, ch: 4 + indentUnit});
});

it("should insert new line below when multiple line selection", function () {
Expand All @@ -1222,22 +1230,63 @@ define(function (require, exports, module) {

CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor);

var lines = defaultContent.split("\n");
lines.splice(5, 0, " " + indentation);
var expectedText = lines.join("\n");

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 5, ch: 4 + indentUnit});
});
});

describe("Open Line Above and Below - editor with visible range", function () {

it("should insert new line above the top line of the visible range", function () {
makeEditorWithRange({startLine: 0, endLine: 4});
myEditor.setSelection({line: 0, ch: 4}, {line: 0, ch: 6});
CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor);

var lines = defaultContent.split("\n");
lines.splice(0, 0, "");
expect(myDocument.getText()).toEqual(lines.join("\n"));
expect(myEditor._visibleRange.startLine).toBe(0);
expect(myEditor._visibleRange.endLine).toBe(5);
});

it("should insert new line above the last line of the visible range", function () {
makeEditorWithRange({startLine: 0, endLine: 0});
myEditor.setSelection({line: 0, ch: 4}, {line: 0, ch: 6});
CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor);

var indentUnit = Editor.getIndentUnit();
indentUnit *= 2;
var spaces = [];
while (spaces.length < indentUnit) {
spaces.push(" ");
}
var indentation = spaces.join("");
lines.splice(5, 0, indentation);
var lines = defaultContent.split("\n");
lines.splice(0, 0, "");
expect(myDocument.getText()).toEqual(lines.join("\n"));
expect(myEditor._visibleRange.startLine).toBe(0);
expect(myEditor._visibleRange.endLine).toBe(1);
});

it("should insert new line below the first line of the visible range", function () {
makeEditorWithRange({startLine: 7, endLine: 7});
myEditor.setSelection({line: 7, ch: 0}, {line: 7, ch: 1});
CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor);

var expectedText = lines.join("\n");
var lines = defaultContent.split("\n");
lines.splice(8, 0, "");
expect(myDocument.getText()).toEqual(lines.join("\n"));
expect(myEditor._visibleRange.startLine).toBe(7);
expect(myEditor._visibleRange.endLine).toBe(8);
});

it("should insert new line below the last line of the visible range", function () {
makeEditorWithRange({startLine: 6, endLine: 7});
myEditor.setSelection({line: 7, ch: 0}, {line: 7, ch: 1});
CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor);

expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 5, ch: 8});
var lines = defaultContent.split("\n");
lines.splice(8, 0, "");
expect(myDocument.getText()).toEqual(lines.join("\n"));
expect(myEditor._visibleRange.startLine).toBe(6);
expect(myEditor._visibleRange.endLine).toBe(8);
});
});
});
Expand Down