From c52fb15dd3ce11651261fbc34182d8e859c066d8 Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Thu, 31 Jan 2013 00:46:53 +0100 Subject: [PATCH 01/12] Add feature. Open line above/below the current line (Ctrl-Enter / Ctrl-Shift-Enter). --- src/base-config/keyboard.json | 6 +++ src/command/Commands.js | 2 + src/command/Menus.js | 2 + src/editor/EditorCommandHandlers.js | 83 ++++++++++++++++++++++++----- src/nls/it/strings.js | 2 + src/nls/root/strings.js | 2 + 6 files changed, 83 insertions(+), 14 deletions(-) diff --git a/src/base-config/keyboard.json b/src/base-config/keyboard.json index 77280b5d070..21697f422f8 100644 --- a/src/base-config/keyboard.json +++ b/src/base-config/keyboard.json @@ -130,6 +130,12 @@ "platform": "mac" } ], + "edit.openLineAbove": [ + "Ctrl-Enter" + ], + "edit.openLineBelow": [ + "Ctrl-Shift-Enter" + ], "edit.lineComment": [ "Ctrl-/" ], diff --git a/src/command/Commands.js b/src/command/Commands.js index 797d78c26b9..bb15c2dd35a 100644 --- a/src/command/Commands.js +++ b/src/command/Commands.js @@ -71,6 +71,8 @@ define(function (require, exports, module) { exports.EDIT_BLOCK_COMMENT = "edit.blockComment"; exports.EDIT_LINE_UP = "edit.lineUp"; exports.EDIT_LINE_DOWN = "edit.lineDown"; + exports.EDIT_OPEN_LINE_ABOVE = "edit.openLineAbove"; + exports.EDIT_OPEN_LINE_BELOW = "edit.openLineBelow"; // VIEW exports.VIEW_HIDE_SIDEBAR = "view.hideSidebar"; diff --git a/src/command/Menus.js b/src/command/Menus.js index 3f1a5440a5c..da9842fdd40 100644 --- a/src/command/Menus.js +++ b/src/command/Menus.js @@ -1039,6 +1039,8 @@ 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); diff --git a/src/editor/EditorCommandHandlers.js b/src/editor/EditorCommandHandlers.js index 82c58831365..915c9027d53 100644 --- a/src/editor/EditorCommandHandlers.js +++ b/src/editor/EditorCommandHandlers.js @@ -646,6 +646,59 @@ define(function (require, exports, module) { moveLine(editor, DIRECTION_DOWN); } + /** + * Inserts a new and smart indented line above/below the selected text, or current line if no selection. + * The cursor is moved in the new line. + * @param {Editor} editor - target editor + * @param {Number} direction - direction where to place the new line (-1,+1) => (Up,Down) + */ + function openLine(editor, direction) { + editor = editor || EditorManager.getFocusedEditor(); + if (!editor) { + return; + } + + var sel = editor.getSelection(), + hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch); + + // Insert the new line + var doc = editor.document; + + 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); + break; + case DIRECTION_DOWN: + if (hasSelection && sel.end.ch === 0) { + // Compesate cursor position in linewise selection + 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; + } + } + + /** + * Inserts a new and smart indented line above the selected text, or current line if no selection. + * The cursor is moved in the new line. + */ + function openLineAbove(editor) { + openLine(editor, DIRECTION_UP); + } + + /** + * Inserts a new and smart indented line below the selected text, or current line if no selection. + * The cursor is moved in the new line. + */ + function openLineBelow(editor) { + openLine(editor, DIRECTION_DOWN); + } + /** * Indent a line of text if no selection. Otherwise, indent all lines in selection. */ @@ -721,19 +774,21 @@ define(function (require, exports, module) { } // Register commands - CommandManager.register(Strings.CMD_INDENT, Commands.EDIT_INDENT, indentText); - CommandManager.register(Strings.CMD_UNINDENT, Commands.EDIT_UNINDENT, unidentText); - CommandManager.register(Strings.CMD_COMMENT, Commands.EDIT_LINE_COMMENT, lineComment); - CommandManager.register(Strings.CMD_BLOCK_COMMENT, Commands.EDIT_BLOCK_COMMENT, blockComment); - CommandManager.register(Strings.CMD_DUPLICATE, Commands.EDIT_DUPLICATE, duplicateText); - CommandManager.register(Strings.CMD_DELETE_LINES, Commands.EDIT_DELETE_LINES, deleteCurrentLines); - CommandManager.register(Strings.CMD_LINE_UP, Commands.EDIT_LINE_UP, moveLineUp); - CommandManager.register(Strings.CMD_LINE_DOWN, Commands.EDIT_LINE_DOWN, moveLineDown); - CommandManager.register(Strings.CMD_SELECT_LINE, Commands.EDIT_SELECT_LINE, selectLine); + CommandManager.register(Strings.CMD_INDENT, Commands.EDIT_INDENT, indentText); + CommandManager.register(Strings.CMD_UNINDENT, Commands.EDIT_UNINDENT, unidentText); + CommandManager.register(Strings.CMD_COMMENT, Commands.EDIT_LINE_COMMENT, lineComment); + CommandManager.register(Strings.CMD_BLOCK_COMMENT, Commands.EDIT_BLOCK_COMMENT, blockComment); + CommandManager.register(Strings.CMD_DUPLICATE, Commands.EDIT_DUPLICATE, duplicateText); + CommandManager.register(Strings.CMD_DELETE_LINES, Commands.EDIT_DELETE_LINES, deleteCurrentLines); + CommandManager.register(Strings.CMD_LINE_UP, Commands.EDIT_LINE_UP, moveLineUp); + CommandManager.register(Strings.CMD_LINE_DOWN, Commands.EDIT_LINE_DOWN, moveLineDown); + CommandManager.register(Strings.CMD_OPEN_LINE_ABOVE, Commands.EDIT_OPEN_LINE_ABOVE, openLineAbove); + CommandManager.register(Strings.CMD_OPEN_LINE_BELOW, Commands.EDIT_OPEN_LINE_BELOW, openLineBelow); + CommandManager.register(Strings.CMD_SELECT_LINE, Commands.EDIT_SELECT_LINE, selectLine); - CommandManager.register(Strings.CMD_UNDO, Commands.EDIT_UNDO, handleUndo); - CommandManager.register(Strings.CMD_REDO, Commands.EDIT_REDO, handleRedo); - CommandManager.register(Strings.CMD_CUT, Commands.EDIT_CUT, ignoreCommand); - CommandManager.register(Strings.CMD_COPY, Commands.EDIT_COPY, ignoreCommand); - CommandManager.register(Strings.CMD_PASTE, Commands.EDIT_PASTE, ignoreCommand); + CommandManager.register(Strings.CMD_UNDO, Commands.EDIT_UNDO, handleUndo); + CommandManager.register(Strings.CMD_REDO, Commands.EDIT_REDO, handleRedo); + CommandManager.register(Strings.CMD_CUT, Commands.EDIT_CUT, ignoreCommand); + CommandManager.register(Strings.CMD_COPY, Commands.EDIT_COPY, ignoreCommand); + CommandManager.register(Strings.CMD_PASTE, Commands.EDIT_PASTE, ignoreCommand); }); diff --git a/src/nls/it/strings.js b/src/nls/it/strings.js index 49244bee5ef..35ac5935280 100644 --- a/src/nls/it/strings.js +++ b/src/nls/it/strings.js @@ -188,6 +188,8 @@ define({ "CMD_COMMENT" : "Commenta/De-commenta linee", "CMD_LINE_UP" : "Sposta la linea in alto", "CMD_LINE_DOWN" : "Sposta la linea in basso", + "CMD_OPEN_LINE_ABOVE" : "Apri linea in alto", + "CMD_OPEN_LINE_BELOW" : "Apri linea in basso", // View menu commands "VIEW_MENU" : "Vista", diff --git a/src/nls/root/strings.js b/src/nls/root/strings.js index f908c540dc9..7d289affb77 100644 --- a/src/nls/root/strings.js +++ b/src/nls/root/strings.js @@ -200,6 +200,8 @@ define({ "CMD_BLOCK_COMMENT" : "Toggle Block Comment", "CMD_LINE_UP" : "Move Line Up", "CMD_LINE_DOWN" : "Move Line Down", + "CMD_OPEN_LINE_ABOVE" : "Open Line Above", + "CMD_OPEN_LINE_BELOW" : "Open Line Below", // View menu commands "VIEW_MENU" : "View", From c4ae5c50890c591d3097359354ea6a34daa8ea81 Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Thu, 31 Jan 2013 16:43:36 +0100 Subject: [PATCH 02/12] Add unit tests for Open Line feature --- test/spec/EditorCommandHandlers-test.js | 213 +++++++++++++++++++++++- 1 file changed, 212 insertions(+), 1 deletion(-) diff --git a/test/spec/EditorCommandHandlers-test.js b/test/spec/EditorCommandHandlers-test.js index 8707e80dcfb..9436e00f601 100644 --- a/test/spec/EditorCommandHandlers-test.js +++ b/test/spec/EditorCommandHandlers-test.js @@ -1028,6 +1028,217 @@ define(function (require, exports, module) { expectSelection({start: {line: 4, ch: 0}, end: {line: 5, ch: 0}}); }); }); - + + describe("Open Line Above and Below", function () { + beforeEach(setupFullEditor); + + it("should insert new line above if no selection", function () { + // place cursor in line 1 + myEditor.setCursorPos(1, 10); + + 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}); + }); + + it("should insert new line above with no indentation if no selection", function () { + // place cursor in line 0 + myEditor.setCursorPos(0, 10); + + 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 when characters selected", function () { + // select characters 0-10 in line 1 + myEditor.setSelection({line: 1, ch: 0}, {line: 1, ch: 10}); + + 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}); + }); + + it("should insert new line above when linewise selection", function () { + // select all of line 1 and 2, Including trailing \n + myEditor.setSelection({line: 1, ch: 0}, {line: 3, ch: 0}); + + 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}); + }); + + it("should insert new line above when multiple line selection", function () { + // selection from line 2 character 6 to line 5 character 2 + myEditor.setSelection({line: 2, ch: 6}, {line: 5, ch: 2}); + + 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); + + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectCursorAt({line: 2, ch: 8}); + }); + + it("should insert new line below when no selection", function () { + // place cursor in line 0 + myEditor.setCursorPos(0, 10); + + 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}); + }); + + it("should insert new line below with no indentation if no selection", function () { + // place cursor in line 7 + 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: 8, ch: 0}); + }); + + it("should insert new line below when characters selected", function () { + // select characters 0-10 in line 0 + myEditor.setSelection({line: 0, ch: 0}, {line: 0, ch: 10}); + + 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}); + }); + + it("should insert new line below when linewise selection", function () { + // select all of line 1 and 2, Including trailing \n + myEditor.setSelection({line: 1, ch: 0}, {line: 3, ch: 0}); + + 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); + + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectCursorAt({line: 3, ch: 8}); + }); + + it("should insert new line below when multiple line selection", function () { + // selection from line 1 character 4 to line 4 character 2 + myEditor.setSelection({line: 1, ch: 4}, {line: 4, ch: 2}); + + 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(5, 0, indentation); + + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectCursorAt({line: 5, ch: 8}); + }); + }); }); }); From 056f84a26699769517b4aee95b18c0c5a6f17112 Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Fri, 1 Feb 2013 10:20:12 +0100 Subject: [PATCH 03/12] Improve Open Line feature. Add additional unit tests. --- src/command/Menus.js | 2 - src/editor/EditorCommandHandlers.js | 19 +-- test/spec/EditorCommandHandlers-test.js | 205 +++++++++++++++--------- 3 files changed, 137 insertions(+), 89 deletions(-) diff --git a/src/command/Menus.js b/src/command/Menus.js index da9842fdd40..3f1a5440a5c 100644 --- a/src/command/Menus.js +++ b/src/command/Menus.js @@ -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); diff --git a/src/editor/EditorCommandHandlers.js b/src/editor/EditorCommandHandlers.js index 915c9027d53..e9f044b27fe 100644 --- a/src/editor/EditorCommandHandlers.js +++ b/src/editor/EditorCommandHandlers.js @@ -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); } /** diff --git a/test/spec/EditorCommandHandlers-test.js b/test/spec/EditorCommandHandlers-test.js index 9436e00f601..780ad811ce7 100644 --- a/test/spec/EditorCommandHandlers-test.js +++ b/test/spec/EditorCommandHandlers-test.js @@ -1030,6 +1030,16 @@ define(function (require, exports, module) { }); describe("Open Line Above and Below", function () { + var indentUnit = Editor.getIndentUnit(); + var indentation = (function () { + // 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 () { @@ -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); @@ -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 () { @@ -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 () { @@ -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 () { @@ -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); @@ -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 () { @@ -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 () { @@ -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); }); }); }); From 8d54428693bcd629901534cf62a8fb3f6743367d Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Tue, 12 Feb 2013 01:28:21 +0100 Subject: [PATCH 04/12] Invert OpenLine shortcuts --- src/base-config/keyboard.json | 4 ++-- src/editor/EditorCommandHandlers.js | 2 ++ test/spec/EditorCommandHandlers-test.js | 7 +++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/base-config/keyboard.json b/src/base-config/keyboard.json index 21697f422f8..db89d7be2c3 100644 --- a/src/base-config/keyboard.json +++ b/src/base-config/keyboard.json @@ -131,10 +131,10 @@ } ], "edit.openLineAbove": [ - "Ctrl-Enter" + "Ctrl-Shift-Enter" ], "edit.openLineBelow": [ - "Ctrl-Shift-Enter" + "Ctrl-Enter" ], "edit.lineComment": [ "Ctrl-/" diff --git a/src/editor/EditorCommandHandlers.js b/src/editor/EditorCommandHandlers.js index e9f044b27fe..b75a78fdd01 100644 --- a/src/editor/EditorCommandHandlers.js +++ b/src/editor/EditorCommandHandlers.js @@ -687,6 +687,7 @@ define(function (require, exports, module) { /** * Inserts a new and smart indented line above the selected text, or current line if no selection. * The cursor is moved in the new line. + * @param {Editor} editor - target editor */ function openLineAbove(editor) { openLine(editor, DIRECTION_UP); @@ -695,6 +696,7 @@ define(function (require, exports, module) { /** * Inserts a new and smart indented line below the selected text, or current line if no selection. * The cursor is moved in the new line. + * @param {Editor} editor - target editor */ function openLineBelow(editor) { openLine(editor, DIRECTION_DOWN); diff --git a/test/spec/EditorCommandHandlers-test.js b/test/spec/EditorCommandHandlers-test.js index 780ad811ce7..a54ecc8596a 100644 --- a/test/spec/EditorCommandHandlers-test.js +++ b/test/spec/EditorCommandHandlers-test.js @@ -1031,13 +1031,12 @@ define(function (require, exports, module) { describe("Open Line Above and Below", function () { var indentUnit = Editor.getIndentUnit(); + var indentation = (function () { // generate indent string once var spaces = []; - while (spaces.length < indentUnit) { - spaces.push(" "); - } - return spaces.join(""); + spaces.length = indentUnit + 1; + return spaces.join(" "); }()); beforeEach(setupFullEditor); From 56f678ec8387319779c2462bb25f0f38a2b57a50 Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Fri, 15 Feb 2013 16:00:16 +0100 Subject: [PATCH 05/12] Fix OpenLine problem with inline editors. --- src/editor/EditorCommandHandlers.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/editor/EditorCommandHandlers.js b/src/editor/EditorCommandHandlers.js index b75a78fdd01..ce0279c1609 100644 --- a/src/editor/EditorCommandHandlers.js +++ b/src/editor/EditorCommandHandlers.js @@ -663,25 +663,28 @@ define(function (require, exports, module) { // Insert the new line var doc = editor.document; - var line; + var cm = editor._codeMirror; switch (direction) { case DIRECTION_UP: - line = sel.start.line; + if (sel.start.line !== editor.getFirstVisibleLine()) { + --sel.start.line; + editor.setCursorPos(sel.start.line, doc.getLine(sel.start.line).length); + cm.execCommand("newlineAndIndent"); + } else { + doc.replaceRange("\n", {line: sel.start.line, ch: 0}); + editor.setCursorPos(sel.start.line, 0); + } break; case DIRECTION_DOWN: if (hasSelection && sel.end.ch === 0) { // If linewise selection - line = sel.end.line; - } else { - line = ++sel.end.line; + --sel.end.line; } + editor.setCursorPos(sel.end.line, doc.getLine(sel.end.line).length); + cm.execCommand("newlineAndIndent"); break; } - - doc.replaceRange("\n", {line: line, ch: 0}); - editor._codeMirror.indentLine(line); - editor.setCursorPos(line, doc.getLine(line).length); } /** From 3eea8da2b9e1a0a72711f86f2ef21f11842b64f3 Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Sat, 16 Feb 2013 16:27:29 +0100 Subject: [PATCH 06/12] Fix OpenLine undo history --- src/editor/EditorCommandHandlers.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/editor/EditorCommandHandlers.js b/src/editor/EditorCommandHandlers.js index ce0279c1609..46b9cea1478 100644 --- a/src/editor/EditorCommandHandlers.js +++ b/src/editor/EditorCommandHandlers.js @@ -662,29 +662,28 @@ define(function (require, exports, module) { hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch); // Insert the new line - var doc = editor.document; - var cm = editor._codeMirror; + var cm = editor._codeMirror, + doc = editor.document, + line; switch (direction) { case DIRECTION_UP: - if (sel.start.line !== editor.getFirstVisibleLine()) { - --sel.start.line; - editor.setCursorPos(sel.start.line, doc.getLine(sel.start.line).length); - cm.execCommand("newlineAndIndent"); - } else { - doc.replaceRange("\n", {line: sel.start.line, ch: 0}); - editor.setCursorPos(sel.start.line, 0); - } + line = sel.start.line; break; case DIRECTION_DOWN: - if (hasSelection && sel.end.ch === 0) { - // If linewise selection - --sel.end.line; + line = sel.end.line; + if (!(hasSelection && sel.end.ch === 0)) { + // If not linewise selection + line++; } - editor.setCursorPos(sel.end.line, doc.getLine(sel.end.line).length); - cm.execCommand("newlineAndIndent"); break; } + + cm.operation(function () { + doc.replaceRange("\n", {line: line, ch: 0}); + cm.indentLine(line); + editor.setCursorPos(line, doc.getLine(line).length); + }); } /** From a118ef17bdfba99cf5797365872d86713e16e666 Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Mon, 18 Feb 2013 00:52:11 +0100 Subject: [PATCH 07/12] Add few improvements in OpenLine --- src/editor/EditorCommandHandlers.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/editor/EditorCommandHandlers.js b/src/editor/EditorCommandHandlers.js index 46b9cea1478..f70cf6291b7 100644 --- a/src/editor/EditorCommandHandlers.js +++ b/src/editor/EditorCommandHandlers.js @@ -659,13 +659,12 @@ define(function (require, exports, module) { } var sel = editor.getSelection(), - hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch); - - // Insert the new line - var cm = editor._codeMirror, + hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch), + cm = editor._codeMirror, doc = editor.document, line; + // Insert the new line switch (direction) { case DIRECTION_UP: line = sel.start.line; @@ -679,10 +678,10 @@ define(function (require, exports, module) { break; } - cm.operation(function () { + doc.batchOperation(function () { doc.replaceRange("\n", {line: line, ch: 0}); cm.indentLine(line); - editor.setCursorPos(line, doc.getLine(line).length); + editor.setSelection({line: line, ch: null}); }); } From 190d58aa45de0b3ccb43eb1ad6e8a62ca44b9405 Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Thu, 11 Apr 2013 10:32:31 +0200 Subject: [PATCH 08/12] Include TomMalbran's suggestions --- src/editor/EditorCommandHandlers.js | 2 +- test/spec/EditorCommandHandlers-test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/editor/EditorCommandHandlers.js b/src/editor/EditorCommandHandlers.js index 64a3b6add3d..a93ae5773b2 100644 --- a/src/editor/EditorCommandHandlers.js +++ b/src/editor/EditorCommandHandlers.js @@ -732,8 +732,8 @@ define(function (require, exports, module) { doc.batchOperation(function () { doc.replaceRange("\n", {line: line, ch: 0}); cm.indentLine(line); - editor.setSelection({line: line, ch: null}); }); + editor.setSelection({line: line, ch: null}); } /** diff --git a/test/spec/EditorCommandHandlers-test.js b/test/spec/EditorCommandHandlers-test.js index 8a1883ebad7..90a98c707c3 100644 --- a/test/spec/EditorCommandHandlers-test.js +++ b/test/spec/EditorCommandHandlers-test.js @@ -2276,7 +2276,7 @@ define(function (require, exports, module) { }); describe("Open Line Above and Below", function () { - var indentUnit = Editor.getIndentUnit(); + var indentUnit = Editor.getSpaceUnits(); var indentation = (function () { // generate indent string once From 9ebbe0879be3b8d30eda478bdcc13a908cfbc400 Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Thu, 18 Apr 2013 12:08:17 +0200 Subject: [PATCH 09/12] Add special case for last line in inline editor --- src/editor/EditorCommandHandlers.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/editor/EditorCommandHandlers.js b/src/editor/EditorCommandHandlers.js index 88ec7d0c033..00365352325 100644 --- a/src/editor/EditorCommandHandlers.js +++ b/src/editor/EditorCommandHandlers.js @@ -728,10 +728,12 @@ define(function (require, exports, module) { return; } - var sel = editor.getSelection(), - hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch), - cm = editor._codeMirror, - doc = editor.document, + var sel = editor.getSelection(), + hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch), + isInlineWidget = !!EditorManager.getFocusedInlineWidget(), + lastLine = editor.getLastVisibleLine(), + cm = editor._codeMirror, + doc = editor.document, line; // Insert the new line @@ -749,7 +751,11 @@ define(function (require, exports, module) { } doc.batchOperation(function () { - doc.replaceRange("\n", {line: line, ch: 0}); + if (line > lastLine && isInlineWidget) { + doc.replaceRange("\n", {line: line - 1, ch: doc.getLine(line - 1).length}); + } else { + doc.replaceRange("\n", {line: line, ch: 0}); + } cm.indentLine(line); }); editor.setSelection({line: line, ch: null}); From dd7537aa9caa6a9438c79af45e59b282ef5e2ac0 Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Fri, 19 Apr 2013 18:49:57 +0200 Subject: [PATCH 10/12] Include changes to OpenLinecode and new tests --- src/editor/EditorCommandHandlers.js | 14 +- test/spec/EditorCommandHandlers-test.js | 263 +++++++++++++++--------- 2 files changed, 171 insertions(+), 106 deletions(-) diff --git a/src/editor/EditorCommandHandlers.js b/src/editor/EditorCommandHandlers.js index 00365352325..77add7dad92 100644 --- a/src/editor/EditorCommandHandlers.js +++ b/src/editor/EditorCommandHandlers.js @@ -750,14 +750,12 @@ define(function (require, exports, module) { break; } - doc.batchOperation(function () { - if (line > lastLine && isInlineWidget) { - doc.replaceRange("\n", {line: line - 1, ch: doc.getLine(line - 1).length}); - } else { - doc.replaceRange("\n", {line: line, ch: 0}); - } - cm.indentLine(line); - }); + if (line > lastLine && isInlineWidget) { + doc.replaceRange("\n", {line: line - 1, ch: doc.getLine(line - 1).length}, null, "+input"); + } else { + doc.replaceRange("\n", {line: line, ch: 0}, null, "+input"); + } + cm.indentLine(line, "smart", false); editor.setSelection({line: line, ch: null}); } diff --git a/test/spec/EditorCommandHandlers-test.js b/test/spec/EditorCommandHandlers-test.js index 7d5bf3ac1ca..0f363ade32b 100644 --- a/test/spec/EditorCommandHandlers-test.js +++ b/test/spec/EditorCommandHandlers-test.js @@ -90,6 +90,52 @@ define(function (require, exports, module) { expect(myEditor.getSelection()).toEqual(sel); } + + // Helper functions for creating / closing a window with an inline editor + function createWindowWithInlineEditor(spec, testPath) { + var promise; + + if (!spec.testWindow) { + SpecRunnerUtils.createTestWindowAndRun(spec, function (w) { + spec.testWindow = w; + + // Load module instances from brackets.test + CommandManager = spec.testWindow.brackets.test.CommandManager; + Commands = spec.testWindow.brackets.test.Commands; + EditorManager = spec.testWindow.brackets.test.EditorManager; + + SpecRunnerUtils.loadProjectInTestWindow(testPath); + }); + } + + runs(function () { + promise = CommandManager.execute(Commands.FILE_ADD_TO_WORKING_SET, {fullPath: testPath + "/test.html"}); + waitsForDone(promise, "Open into working set"); + }); + + runs(function () { + // Open inline editor onto test.css's ".testClass" rule + promise = SpecRunnerUtils.toggleQuickEditAtOffset(EditorManager.getCurrentFullEditor(), {line: 8, ch: 11}); + waitsForDone(promise, "Open inline editor"); + }); + + runs(function () { + spec.editor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editors[0]; + }); + } + function closeWindowWithInlineEditor(spec) { + runs(function () { + var promise = CommandManager.execute(Commands.FILE_CLOSE_ALL); + waitsForDone(promise, "Close all open files in working set"); + + // Close the save dialog without saving the changes + var $dlg = spec.testWindow.$(".modal.instance"); + if ($dlg.length) { + SpecRunnerUtils.clickDialogButton("dontsave"); + } + }); + } + describe("Line comment/uncomment", function () { beforeEach(setupFullEditor); @@ -2068,12 +2114,13 @@ define(function (require, exports, module) { expectSelection({start: {line: 0, ch: 0}, end: {line: 7, ch: 1}}); }); }); - + describe("Move Lines Up/Down - inline editor", function () { this.category = "integration"; - var testWindow, promise, editor; + var self = this; + var testPath = SpecRunnerUtils.getTestPath("/spec/EditorCommandHandlers-test-files"); var moveContent = ".testClass {\n" + @@ -2081,72 +2128,37 @@ define(function (require, exports, module) { "}"; beforeEach(function () { - if (!testWindow) { - SpecRunnerUtils.createTestWindowAndRun(this, function (w) { - testWindow = w; - - // Load module instances from brackets.test - CommandManager = testWindow.brackets.test.CommandManager; - Commands = testWindow.brackets.test.Commands; - EditorManager = testWindow.brackets.test.EditorManager; - - SpecRunnerUtils.loadProjectInTestWindow(testPath); - }); - } - - runs(function () { - promise = CommandManager.execute(Commands.FILE_ADD_TO_WORKING_SET, {fullPath: testPath + "/test.html"}); - waitsForDone(promise, "Open into working set"); - }); - - runs(function () { - // Open inline editor onto test.css's ".testClass" rule - promise = SpecRunnerUtils.toggleQuickEditAtOffset(EditorManager.getCurrentFullEditor(), {line: 8, ch: 11}); - waitsForDone(promise, "Open inline editor"); - }); - - runs(function () { - editor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editors[0]; - }); + createWindowWithInlineEditor(self, testPath); }); afterEach(function () { - runs(function () { - var promise = CommandManager.execute(Commands.FILE_CLOSE_ALL); - waitsForDone(promise, "Close all open files in working set"); - - // Close the save dialog without saving the changes - var $dlg = testWindow.$(".modal.instance"); - if ($dlg.length) { - SpecRunnerUtils.clickDialogButton("dontsave"); - } - }); + closeWindowWithInlineEditor(self); }); it("should not move the first line of the inline editor up", function () { - editor.setCursorPos({line: 0, ch: 5}); - CommandManager.execute(Commands.EDIT_LINE_UP, editor); + self.editor.setCursorPos({line: 0, ch: 5}); + CommandManager.execute(Commands.EDIT_LINE_UP, self.editor); - expect(editor.document.getText()).toEqual(moveContent); - expect(editor._codeMirror.doc.historySize().undo).toBe(0); - expect(editor.getFirstVisibleLine()).toBe(0); - expect(editor.getLastVisibleLine()).toBe(2); + expect(self.editor.document.getText()).toEqual(moveContent); + expect(self.editor._codeMirror.doc.historySize().undo).toBe(0); + expect(self.editor.getFirstVisibleLine()).toBe(0); + expect(self.editor.getLastVisibleLine()).toBe(2); }); it("should not move the last line of the inline editor down", function () { - editor.setCursorPos({line: 2, ch: 5}); - CommandManager.execute(Commands.EDIT_LINE_DOWN, editor); + self.editor.setCursorPos({line: 2, ch: 5}); + CommandManager.execute(Commands.EDIT_LINE_DOWN, self.editor); - expect(editor.document.getText()).toEqual(moveContent); - expect(editor._codeMirror.doc.historySize().undo).toBe(0); - expect(editor.getFirstVisibleLine()).toBe(0); - expect(editor.getLastVisibleLine()).toBe(2); + expect(self.editor.document.getText()).toEqual(moveContent); + expect(self.editor._codeMirror.doc.historySize().undo).toBe(0); + expect(self.editor.getFirstVisibleLine()).toBe(0); + expect(self.editor.getLastVisibleLine()).toBe(2); }); it("should be able to move the second to last line of the inline editor down", function () { - editor.setCursorPos({line: 1, ch: 5}); - CommandManager.execute(Commands.EDIT_LINE_DOWN, editor); + self.editor.setCursorPos({line: 1, ch: 5}); + CommandManager.execute(Commands.EDIT_LINE_DOWN, self.editor); var lines = moveContent.split("\n"); var temp = lines[1]; @@ -2154,14 +2166,14 @@ define(function (require, exports, module) { lines[2] = temp; var expectedText = lines.join("\n"); - expect(editor.document.getText()).toEqual(expectedText); - expect(editor.getFirstVisibleLine()).toBe(0); - expect(editor.getLastVisibleLine()).toBe(2); + expect(self.editor.document.getText()).toEqual(expectedText); + expect(self.editor.getFirstVisibleLine()).toBe(0); + expect(self.editor.getLastVisibleLine()).toBe(2); }); it("should be able to move the last line of the inline editor up", function () { - editor.setCursorPos({line: 2, ch: 0}); - CommandManager.execute(Commands.EDIT_LINE_UP, editor); + self.editor.setCursorPos({line: 2, ch: 0}); + CommandManager.execute(Commands.EDIT_LINE_UP, self.editor); var lines = moveContent.split("\n"); var temp = lines[1]; @@ -2169,9 +2181,9 @@ define(function (require, exports, module) { lines[2] = temp; var expectedText = lines.join("\n"); - expect(editor.document.getText()).toEqual(expectedText); - expect(editor.getFirstVisibleLine()).toBe(0); - expect(editor.getLastVisibleLine()).toBe(2); + expect(self.editor.document.getText()).toEqual(expectedText); + expect(self.editor.getFirstVisibleLine()).toBe(0); + expect(self.editor.getLastVisibleLine()).toBe(2); // This must be in the last spec in the suite. runs(function () { @@ -2601,55 +2613,110 @@ define(function (require, exports, module) { expectCursorAt({line: 5, ch: 4 + indentUnit}); }); }); + - describe("Open Line Above and Below - editor with visible range", function () { + describe("Open Line Above and Below - inline editor", function () { + this.category = "integration"; - 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 self = this; + + var testPath = SpecRunnerUtils.getTestPath("/spec/EditorCommandHandlers-test-files"); + + var content = ".testClass {\n" + + " color: red;\n" + + "}"; + + beforeEach(function () { + createWindowWithInlineEditor(self, testPath); + }); + + afterEach(function () { + closeWindowWithInlineEditor(self); + }); + + it("should insert new line above the first line of the inline editor", function () { + self.editor.setSelection({line: 0, ch: 4}, {line: 0, ch: 6}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, self.editor); - var lines = defaultContent.split("\n"); + var lines = content.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); + var expectedText = lines.join("\n"); + + expect(self.editor.document.getText()).toEqual(expectedText); + expect(self.editor.getFirstVisibleLine()).toBe(0); + expect(self.editor.getLastVisibleLine()).toBe(3); }); - 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); + it("should insert new line below the first line of the inline editor", function () { + self.editor.setCursorPos({line: 0, ch: 3}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, self.editor); - 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); + var lines = content.split("\n"); + lines.splice(1, 0, " "); + var expectedText = lines.join("\n"); + + expect(self.editor.document.getText()).toEqual(expectedText); + expect(self.editor.getFirstVisibleLine()).toBe(0); + expect(self.editor.getLastVisibleLine()).toBe(3); }); - - 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); + + it("should insert new line above the last line of the inline editor", function () { + self.editor.setSelection({line: 2, ch: 0}, {line: 2, ch: 1}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, self.editor); - 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); + var lines = content.split("\n"); + lines.splice(2, 0, " "); + var expectedText = lines.join("\n"); + + expect(self.editor.document.getText()).toEqual(expectedText); + expect(self.editor.getFirstVisibleLine()).toBe(0); + expect(self.editor.getLastVisibleLine()).toBe(3); }); - 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); + it("should insert new line below the last line of the inline editor", function () { + self.editor.setCursorPos({line: 3, ch: 0}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, self.editor); - 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); + var lines = content.split("\n"); + lines.splice(3, 0, ""); + var expectedText = lines.join("\n"); + + expect(self.editor.document.getText()).toEqual(expectedText); + expect(self.editor.getFirstVisibleLine()).toBe(0); + expect(self.editor.getLastVisibleLine()).toBe(3); + }); + + it("should insert new indented line above the second line of the inline editor", function () { + self.editor.setCursorPos({line: 1, ch: 5}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, self.editor); + + var lines = content.split("\n"); + lines.splice(1, 0, " "); + var expectedText = lines.join("\n"); + + expect(self.editor.document.getText()).toEqual(expectedText); + expect(self.editor.getFirstVisibleLine()).toBe(0); + expect(self.editor.getLastVisibleLine()).toBe(3); + }); + + it("should insert new indented line below the second line of the inline editor", function () { + self.editor.setCursorPos({line: 1, ch: 5}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, self.editor); + + var lines = content.split("\n"); + lines.splice(2, 0, " "); + var expectedText = lines.join("\n"); + + expect(self.editor.document.getText()).toEqual(expectedText); + expect(self.editor.getFirstVisibleLine()).toBe(0); + expect(self.editor.getLastVisibleLine()).toBe(3); + + // This must be in the last spec in the suite. + runs(function () { + this.after(function () { + SpecRunnerUtils.closeTestWindow(); + }); + }); }); }); }); From 27321f84ef49bbc277a30ebb1300bc5983009973 Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Sat, 20 Apr 2013 11:42:23 +0200 Subject: [PATCH 11/12] Remove Italian strings for OpenLine. Add little changes to tests. --- src/nls/it/strings.js | 2 -- test/spec/EditorCommandHandlers-test.js | 16 +++++++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/nls/it/strings.js b/src/nls/it/strings.js index 31b0ad350d8..a3f6e33a61a 100644 --- a/src/nls/it/strings.js +++ b/src/nls/it/strings.js @@ -199,8 +199,6 @@ define({ "CMD_BLOCK_COMMENT" : "Commenta/De-commenta blocco", "CMD_LINE_UP" : "Sposta la riga in alto", "CMD_LINE_DOWN" : "Sposta la riga in basso", - "CMD_OPEN_LINE_ABOVE" : "Apri linea in alto", - "CMD_OPEN_LINE_BELOW" : "Apri linea in basso", // View menu commands "VIEW_MENU" : "Vista", diff --git a/test/spec/EditorCommandHandlers-test.js b/test/spec/EditorCommandHandlers-test.js index 0f363ade32b..44f52ad9b46 100644 --- a/test/spec/EditorCommandHandlers-test.js +++ b/test/spec/EditorCommandHandlers-test.js @@ -48,6 +48,8 @@ define(function (require, exports, module) { var myDocument, myEditor; + var testPath = SpecRunnerUtils.getTestPath("/spec/EditorCommandHandlers-test-files"); + function setupFullEditor(content, languageId) { content = content || defaultContent; languageId = languageId || "javascript"; @@ -92,7 +94,7 @@ define(function (require, exports, module) { // Helper functions for creating / closing a window with an inline editor - function createWindowWithInlineEditor(spec, testPath) { + function createWindowWithInlineEditor(spec) { var promise; if (!spec.testWindow) { @@ -2121,14 +2123,12 @@ define(function (require, exports, module) { var self = this; - var testPath = SpecRunnerUtils.getTestPath("/spec/EditorCommandHandlers-test-files"); - var moveContent = ".testClass {\n" + " color: red;\n" + "}"; beforeEach(function () { - createWindowWithInlineEditor(self, testPath); + createWindowWithInlineEditor(self); }); afterEach(function () { @@ -2620,14 +2620,12 @@ define(function (require, exports, module) { var self = this; - var testPath = SpecRunnerUtils.getTestPath("/spec/EditorCommandHandlers-test-files"); - var content = ".testClass {\n" + - " color: red;\n" + - "}"; + " color: red;\n" + + "}"; beforeEach(function () { - createWindowWithInlineEditor(self, testPath); + createWindowWithInlineEditor(self); }); afterEach(function () { From 5e2d29e17e5cf4a5cda4e44b3b43c5e1100b5c4d Mon Sep 17 00:00:00 2001 From: Alessandro Di Martino Date: Sat, 20 Apr 2013 19:18:49 +0200 Subject: [PATCH 12/12] Few cleanups for OpenLine's tests --- test/spec/EditorCommandHandlers-test.js | 163 ++++++++++++------------ 1 file changed, 81 insertions(+), 82 deletions(-) diff --git a/test/spec/EditorCommandHandlers-test.js b/test/spec/EditorCommandHandlers-test.js index 44f52ad9b46..7e6eda7d5be 100644 --- a/test/spec/EditorCommandHandlers-test.js +++ b/test/spec/EditorCommandHandlers-test.js @@ -48,7 +48,8 @@ define(function (require, exports, module) { var myDocument, myEditor; - var testPath = SpecRunnerUtils.getTestPath("/spec/EditorCommandHandlers-test-files"); + var testPath = SpecRunnerUtils.getTestPath("/spec/EditorCommandHandlers-test-files"), + testWindow; function setupFullEditor(content, languageId) { content = content || defaultContent; @@ -93,18 +94,18 @@ define(function (require, exports, module) { } - // Helper functions for creating / closing a window with an inline editor + // Helper function for creating a window with an inline editor function createWindowWithInlineEditor(spec) { var promise; - if (!spec.testWindow) { + if (!testWindow) { SpecRunnerUtils.createTestWindowAndRun(spec, function (w) { - spec.testWindow = w; + testWindow = w; // Load module instances from brackets.test - CommandManager = spec.testWindow.brackets.test.CommandManager; - Commands = spec.testWindow.brackets.test.Commands; - EditorManager = spec.testWindow.brackets.test.EditorManager; + CommandManager = testWindow.brackets.test.CommandManager; + Commands = testWindow.brackets.test.Commands; + EditorManager = testWindow.brackets.test.EditorManager; SpecRunnerUtils.loadProjectInTestWindow(testPath); }); @@ -122,22 +123,34 @@ define(function (require, exports, module) { }); runs(function () { - spec.editor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editors[0]; + myEditor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editors[0]; }); } - function closeWindowWithInlineEditor(spec) { + + // Helper function for closing open files in the test window + function closeFilesInTestWindow() { runs(function () { var promise = CommandManager.execute(Commands.FILE_CLOSE_ALL); waitsForDone(promise, "Close all open files in working set"); // Close the save dialog without saving the changes - var $dlg = spec.testWindow.$(".modal.instance"); + var $dlg = testWindow.$(".modal.instance"); if ($dlg.length) { SpecRunnerUtils.clickDialogButton("dontsave"); } }); } + // Helper function for closing the test window. This must be used in the last spec in the suite. + function closeTestWindow() { + runs(function () { + this.after(function () { + SpecRunnerUtils.closeTestWindow(); + testWindow = null; + }); + }); + } + describe("Line comment/uncomment", function () { beforeEach(setupFullEditor); @@ -2121,44 +2134,42 @@ define(function (require, exports, module) { describe("Move Lines Up/Down - inline editor", function () { this.category = "integration"; - var self = this; - var moveContent = ".testClass {\n" + " color: red;\n" + "}"; beforeEach(function () { - createWindowWithInlineEditor(self); + createWindowWithInlineEditor(this); }); afterEach(function () { - closeWindowWithInlineEditor(self); + closeFilesInTestWindow(); }); it("should not move the first line of the inline editor up", function () { - self.editor.setCursorPos({line: 0, ch: 5}); - CommandManager.execute(Commands.EDIT_LINE_UP, self.editor); + myEditor.setCursorPos({line: 0, ch: 5}); + CommandManager.execute(Commands.EDIT_LINE_UP, myEditor); - expect(self.editor.document.getText()).toEqual(moveContent); - expect(self.editor._codeMirror.doc.historySize().undo).toBe(0); - expect(self.editor.getFirstVisibleLine()).toBe(0); - expect(self.editor.getLastVisibleLine()).toBe(2); + expect(myEditor.document.getText()).toEqual(moveContent); + expect(myEditor._codeMirror.doc.historySize().undo).toBe(0); + expect(myEditor.getFirstVisibleLine()).toBe(0); + expect(myEditor.getLastVisibleLine()).toBe(2); }); it("should not move the last line of the inline editor down", function () { - self.editor.setCursorPos({line: 2, ch: 5}); - CommandManager.execute(Commands.EDIT_LINE_DOWN, self.editor); + myEditor.setCursorPos({line: 2, ch: 5}); + CommandManager.execute(Commands.EDIT_LINE_DOWN, myEditor); - expect(self.editor.document.getText()).toEqual(moveContent); - expect(self.editor._codeMirror.doc.historySize().undo).toBe(0); - expect(self.editor.getFirstVisibleLine()).toBe(0); - expect(self.editor.getLastVisibleLine()).toBe(2); + expect(myEditor.document.getText()).toEqual(moveContent); + expect(myEditor._codeMirror.doc.historySize().undo).toBe(0); + expect(myEditor.getFirstVisibleLine()).toBe(0); + expect(myEditor.getLastVisibleLine()).toBe(2); }); it("should be able to move the second to last line of the inline editor down", function () { - self.editor.setCursorPos({line: 1, ch: 5}); - CommandManager.execute(Commands.EDIT_LINE_DOWN, self.editor); + myEditor.setCursorPos({line: 1, ch: 5}); + CommandManager.execute(Commands.EDIT_LINE_DOWN, myEditor); var lines = moveContent.split("\n"); var temp = lines[1]; @@ -2166,14 +2177,14 @@ define(function (require, exports, module) { lines[2] = temp; var expectedText = lines.join("\n"); - expect(self.editor.document.getText()).toEqual(expectedText); - expect(self.editor.getFirstVisibleLine()).toBe(0); - expect(self.editor.getLastVisibleLine()).toBe(2); + expect(myEditor.document.getText()).toEqual(expectedText); + expect(myEditor.getFirstVisibleLine()).toBe(0); + expect(myEditor.getLastVisibleLine()).toBe(2); }); it("should be able to move the last line of the inline editor up", function () { - self.editor.setCursorPos({line: 2, ch: 0}); - CommandManager.execute(Commands.EDIT_LINE_UP, self.editor); + myEditor.setCursorPos({line: 2, ch: 0}); + CommandManager.execute(Commands.EDIT_LINE_UP, myEditor); var lines = moveContent.split("\n"); var temp = lines[1]; @@ -2181,16 +2192,11 @@ define(function (require, exports, module) { lines[2] = temp; var expectedText = lines.join("\n"); - expect(self.editor.document.getText()).toEqual(expectedText); - expect(self.editor.getFirstVisibleLine()).toBe(0); - expect(self.editor.getLastVisibleLine()).toBe(2); + expect(myEditor.document.getText()).toEqual(expectedText); + expect(myEditor.getFirstVisibleLine()).toBe(0); + expect(myEditor.getLastVisibleLine()).toBe(2); - // This must be in the last spec in the suite. - runs(function () { - this.after(function () { - SpecRunnerUtils.closeTestWindow(); - }); - }); + closeTestWindow(); }); }); @@ -2618,103 +2624,96 @@ define(function (require, exports, module) { describe("Open Line Above and Below - inline editor", function () { this.category = "integration"; - var self = this; - var content = ".testClass {\n" + " color: red;\n" + "}"; beforeEach(function () { - createWindowWithInlineEditor(self); + createWindowWithInlineEditor(this); }); afterEach(function () { - closeWindowWithInlineEditor(self); + closeFilesInTestWindow(); }); it("should insert new line above the first line of the inline editor", function () { - self.editor.setSelection({line: 0, ch: 4}, {line: 0, ch: 6}); - CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, self.editor); + myEditor.setSelection({line: 0, ch: 4}, {line: 0, ch: 6}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor); var lines = content.split("\n"); lines.splice(0, 0, ""); var expectedText = lines.join("\n"); - expect(self.editor.document.getText()).toEqual(expectedText); - expect(self.editor.getFirstVisibleLine()).toBe(0); - expect(self.editor.getLastVisibleLine()).toBe(3); + expect(myEditor.document.getText()).toEqual(expectedText); + expect(myEditor.getFirstVisibleLine()).toBe(0); + expect(myEditor.getLastVisibleLine()).toBe(3); }); it("should insert new line below the first line of the inline editor", function () { - self.editor.setCursorPos({line: 0, ch: 3}); - CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, self.editor); + myEditor.setCursorPos({line: 0, ch: 3}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor); var lines = content.split("\n"); lines.splice(1, 0, " "); var expectedText = lines.join("\n"); - expect(self.editor.document.getText()).toEqual(expectedText); - expect(self.editor.getFirstVisibleLine()).toBe(0); - expect(self.editor.getLastVisibleLine()).toBe(3); + expect(myEditor.document.getText()).toEqual(expectedText); + expect(myEditor.getFirstVisibleLine()).toBe(0); + expect(myEditor.getLastVisibleLine()).toBe(3); }); it("should insert new line above the last line of the inline editor", function () { - self.editor.setSelection({line: 2, ch: 0}, {line: 2, ch: 1}); - CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, self.editor); + myEditor.setSelection({line: 2, ch: 0}, {line: 2, ch: 1}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor); var lines = content.split("\n"); lines.splice(2, 0, " "); var expectedText = lines.join("\n"); - expect(self.editor.document.getText()).toEqual(expectedText); - expect(self.editor.getFirstVisibleLine()).toBe(0); - expect(self.editor.getLastVisibleLine()).toBe(3); + expect(myEditor.document.getText()).toEqual(expectedText); + expect(myEditor.getFirstVisibleLine()).toBe(0); + expect(myEditor.getLastVisibleLine()).toBe(3); }); it("should insert new line below the last line of the inline editor", function () { - self.editor.setCursorPos({line: 3, ch: 0}); - CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, self.editor); + myEditor.setCursorPos({line: 3, ch: 0}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor); var lines = content.split("\n"); lines.splice(3, 0, ""); var expectedText = lines.join("\n"); - expect(self.editor.document.getText()).toEqual(expectedText); - expect(self.editor.getFirstVisibleLine()).toBe(0); - expect(self.editor.getLastVisibleLine()).toBe(3); + expect(myEditor.document.getText()).toEqual(expectedText); + expect(myEditor.getFirstVisibleLine()).toBe(0); + expect(myEditor.getLastVisibleLine()).toBe(3); }); it("should insert new indented line above the second line of the inline editor", function () { - self.editor.setCursorPos({line: 1, ch: 5}); - CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, self.editor); + myEditor.setCursorPos({line: 1, ch: 5}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_ABOVE, myEditor); var lines = content.split("\n"); lines.splice(1, 0, " "); var expectedText = lines.join("\n"); - expect(self.editor.document.getText()).toEqual(expectedText); - expect(self.editor.getFirstVisibleLine()).toBe(0); - expect(self.editor.getLastVisibleLine()).toBe(3); + expect(myEditor.document.getText()).toEqual(expectedText); + expect(myEditor.getFirstVisibleLine()).toBe(0); + expect(myEditor.getLastVisibleLine()).toBe(3); }); it("should insert new indented line below the second line of the inline editor", function () { - self.editor.setCursorPos({line: 1, ch: 5}); - CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, self.editor); + myEditor.setCursorPos({line: 1, ch: 5}); + CommandManager.execute(Commands.EDIT_OPEN_LINE_BELOW, myEditor); var lines = content.split("\n"); lines.splice(2, 0, " "); var expectedText = lines.join("\n"); - expect(self.editor.document.getText()).toEqual(expectedText); - expect(self.editor.getFirstVisibleLine()).toBe(0); - expect(self.editor.getLastVisibleLine()).toBe(3); + expect(myEditor.document.getText()).toEqual(expectedText); + expect(myEditor.getFirstVisibleLine()).toBe(0); + expect(myEditor.getLastVisibleLine()).toBe(3); - // This must be in the last spec in the suite. - runs(function () { - this.after(function () { - SpecRunnerUtils.closeTestWindow(); - }); - }); + closeTestWindow(); }); }); });