refactor selection support into a separate class

Merge branch 'master' into HEAD

Conflicts:
	src/Editor.js
	src/KeyBinding.js
	src/Selection.js
This commit is contained in:
Fabian Jakobs 2010-04-15 13:58:49 +02:00
commit 7529f90080
12 changed files with 887 additions and 680 deletions

20
test/EventEmitterTest.js Normal file
View file

@ -0,0 +1,20 @@
var EventEmitter = function() {
this.$initEvents();
};
ace.mixin(EventEmitter.prototype, ace.MEventEmitter);
var EventEmitterTest = new TestCase("EventEmitterTest", {
"test: dispatch event with no data" : function() {
var emitter = new EventEmitter();
var called = false;
emitter.addEventListener("juhu", function(e) {
called = true;
assertEquals("juhu", e.type);
});
emitter.$dispatchEvent("juhu");
assertTrue(called);
}
});

View file

@ -6,33 +6,17 @@ var NavigationTest = TestCase("NavigationTest",
return new ace.TextDocument(text);
},
"test: navigate to end of file should place the cursor on last row and column" : function() {
var doc = this.createTextDocument(200, 10);
var editor = new ace.Editor(new MockRenderer(), doc);
editor.navigateFileEnd();
assertPosition(199, 10, editor.getCursorPosition());
},
"test: navigate to end of file should scroll the last line into view" : function() {
var doc = this.createTextDocument(200, 10);
var editor = new ace.Editor(new MockRenderer(), doc);
editor.navigateFileEnd();
var cursor = editor.getCursorPosition();
var cursor = editor.getSelection().getCursor();
assertTrue(editor.getFirstVisibleRow() <= cursor.row);
assertTrue(editor.getLastVisibleRow() >= cursor.row);
},
"test: navigate to start of file should place the cursor on the first row and column" : function() {
var doc = this.createTextDocument(200, 10);
var editor = new ace.Editor(new MockRenderer(), doc);
editor.navigateFileStart();
assertPosition(0, 0, editor.getCursorPosition());
},
"test: navigate to start of file should scroll the first row into view" : function() {
var doc = this.createTextDocument(200, 10);
var editor = new ace.Editor(new MockRenderer(), doc);
@ -43,190 +27,37 @@ var NavigationTest = TestCase("NavigationTest",
assertEquals(0, editor.getFirstVisibleRow());
},
"test: move selection lead to end of file" : function() {
var doc = this.createTextDocument(200, 10);
var editor = new ace.Editor(new MockRenderer(), doc);
editor.moveCursorTo(100, 5);
editor.selectFileEnd();
var selection = editor.getSelectionRange();
assertPosition(100, 5, selection.start);
assertPosition(199, 10, selection.end);
},
"test: move selection lead to start of file" : function() {
var doc = this.createTextDocument(200, 10);
var editor = new ace.Editor(new MockRenderer(), doc);
editor.moveCursorTo(100, 5);
editor.selectFileStart();
var selection = editor.getSelectionRange();
assertPosition(0, 0, selection.start);
assertPosition(100, 5, selection.end);
},
"test: navigate word right" : function() {
var doc = new ace.TextDocument( ["ab",
" Juhu Kinners (abc, 12)", " cde"].join("\n"));
var editor = new ace.Editor(new MockRenderer(), doc);
editor.navigateDown();
assertPosition(1, 0, editor.getCursorPosition());
editor.navigateWordRight();
assertPosition(1, 1, editor.getCursorPosition());
editor.navigateWordRight();
assertPosition(1, 5, editor.getCursorPosition());
editor.navigateWordRight();
assertPosition(1, 6, editor.getCursorPosition());
editor.navigateWordRight();
assertPosition(1, 13, editor.getCursorPosition());
editor.navigateWordRight();
assertPosition(1, 15, editor.getCursorPosition());
editor.navigateWordRight();
assertPosition(1, 18, editor.getCursorPosition());
editor.navigateWordRight();
assertPosition(1, 20, editor.getCursorPosition());
editor.navigateWordRight();
assertPosition(1, 22, editor.getCursorPosition());
editor.navigateWordRight();
assertPosition(1, 23, editor.getCursorPosition());
// wrap line
editor.navigateWordRight();
assertPosition(2, 0, editor.getCursorPosition());
},
"test: select word right if cursor in word" : function() {
var doc = new ace.TextDocument("Juhu Kinners");
var editor = new ace.Editor(new MockRenderer(), doc);
editor.moveCursorTo(0, 2);
editor.navigateWordRight();
assertPosition(0, 4, editor.getCursorPosition());
},
"test: navigate word left" : function() {
var doc = new ace.TextDocument( ["ab",
" Juhu Kinners (abc, 12)", " cde"].join("\n"));
var editor = new ace.Editor(new MockRenderer(), doc);
editor.navigateDown();
editor.navigateLineEnd();
assertPosition(1, 23, editor.getCursorPosition());
editor.navigateWordLeft();
assertPosition(1, 22, editor.getCursorPosition());
editor.navigateWordLeft();
assertPosition(1, 20, editor.getCursorPosition());
editor.navigateWordLeft();
assertPosition(1, 18, editor.getCursorPosition());
editor.navigateWordLeft();
assertPosition(1, 15, editor.getCursorPosition());
editor.navigateWordLeft();
assertPosition(1, 13, editor.getCursorPosition());
editor.navigateWordLeft();
assertPosition(1, 6, editor.getCursorPosition());
editor.navigateWordLeft();
assertPosition(1, 5, editor.getCursorPosition());
editor.navigateWordLeft();
assertPosition(1, 1, editor.getCursorPosition());
editor.navigateWordLeft();
assertPosition(1, 0, editor.getCursorPosition());
// wrap line
editor.navigateWordLeft();
assertPosition(0, 2, editor.getCursorPosition());
},
"test: select word left if cursor in word" : function() {
var doc = new ace.TextDocument("Juhu Kinners");
var editor = new ace.Editor(new MockRenderer(), doc);
editor.moveCursorTo(0, 8);
editor.navigateWordLeft();
assertPosition(0, 5, editor.getCursorPosition());
},
"test: select word right and select" : function() {
var doc = new ace.TextDocument("Juhu Kinners");
var editor = new ace.Editor(new MockRenderer(), doc);
editor.moveCursorTo(0, 0);
editor.selectWordRight();
var selection = editor.getSelectionRange();
assertPosition(0, 0, selection.start);
assertPosition(0, 4, selection.end);
},
"test: select word left and select" : function() {
var doc = new ace.TextDocument("Juhu Kinners");
var editor = new ace.Editor(new MockRenderer(), doc);
editor.moveCursorTo(0, 3);
editor.selectWordLeft();
var selection = editor.getSelectionRange();
assertPosition(0, 0, selection.start);
assertPosition(0, 3, selection.end);
},
"test: goto hidden line should scroll the line into the middle of the viewport" : function() {
var editor = new ace.Editor(new MockRenderer(), this.createTextDocument(200, 5));
editor.navigateTo(0, 0);
editor.gotoLine(100);
assertPosition(100, 0, editor.getCursorPosition());
assertPosition(100, 0, editor.getSelection().getCursor());
assertEquals(90, editor.getFirstVisibleRow());
editor.navigateTo(100, 0);
editor.gotoLine(10);
assertPosition(10, 0, editor.getCursorPosition());
assertPosition(10, 0, editor.getSelection().getCursor());
assertEquals(0, editor.getFirstVisibleRow());
editor.navigateTo(100, 0);
editor.gotoLine(5);
assertPosition(5, 0, editor.getCursorPosition());
assertPosition(5, 0, editor.getSelection().getCursor());
assertEquals(0, editor.getFirstVisibleRow());
editor.navigateTo(100, 0);
editor.gotoLine(0);
assertPosition(0, 0, editor.getCursorPosition());
assertPosition(0, 0, editor.getSelection().getCursor());
assertEquals(0, editor.getFirstVisibleRow());
editor.navigateTo(0, 0);
editor.gotoLine(190);
assertPosition(190, 0, editor.getCursorPosition());
assertPosition(190, 0, editor.getSelection().getCursor());
assertEquals(180, editor.getFirstVisibleRow());
editor.navigateTo(0, 0);
editor.gotoLine(195);
assertPosition(195, 0, editor.getCursorPosition());
assertPosition(195, 0, editor.getSelection().getCursor());
assertEquals(180, editor.getFirstVisibleRow());
},
@ -235,12 +66,12 @@ var NavigationTest = TestCase("NavigationTest",
editor.navigateTo(0, 0);
editor.gotoLine(11);
assertPosition(11, 0, editor.getCursorPosition());
assertPosition(11, 0, editor.getSelection().getCursor());
assertEquals(0, editor.getFirstVisibleRow());
editor.navigateTo(30, 0);
editor.gotoLine(32);
assertPosition(32, 0, editor.getCursorPosition());
assertPosition(32, 0, editor.getSelection().getCursor());
assertEquals(30, editor.getFirstVisibleRow());
}
});

177
test/SelectionTest.js Normal file
View file

@ -0,0 +1,177 @@
var SelectionTest = TestCase("SelectionTest",
{
createTextDocument : function(rows, cols) {
var line = new Array(cols + 1).join("a");
var text = new Array(rows).join(line + "\n") + line;
return new ace.TextDocument(text);
},
"test: move cursor to end of file should place the cursor on last row and column" : function() {
var doc = this.createTextDocument(200, 10);
var selection = doc.getSelection();
selection.moveCursorFileEnd();
assertPosition(199, 10, selection.getCursor());
},
"test: moveCursor to start of file should place the cursor on the first row and column" : function() {
var doc = this.createTextDocument(200, 10);
var selection = doc.getSelection();
selection.moveCursorFileStart();
assertPosition(0, 0, selection.getCursor());
},
"test: move selection lead to end of file" : function() {
var doc = this.createTextDocument(200, 10);
var selection = doc.getSelection();
selection.moveCursorTo(100, 5);
selection.selectFileEnd();
var range = selection.getRange();
assertPosition(100, 5, range.start);
assertPosition(199, 10, range.end);
},
"test: move selection lead to start of file" : function() {
var doc = this.createTextDocument(200, 10);
var selection = doc.getSelection();
selection.moveCursorTo(100, 5);
selection.selectFileStart();
var range = selection.getRange();
assertPosition(0, 0, range.start);
assertPosition(100, 5, range.end);
},
"test: move cursor word right" : function() {
var doc = new ace.TextDocument( ["ab",
" Juhu Kinners (abc, 12)", " cde"].join("\n"));
var selection = doc.getSelection();
selection.moveCursorDown();
assertPosition(1, 0, selection.getCursor());
selection.moveCursorWordRight();
assertPosition(1, 1, selection.getCursor());
selection.moveCursorWordRight();
assertPosition(1, 5, selection.getCursor());
selection.moveCursorWordRight();
assertPosition(1, 6, selection.getCursor());
selection.moveCursorWordRight();
assertPosition(1, 13, selection.getCursor());
selection.moveCursorWordRight();
assertPosition(1, 15, selection.getCursor());
selection.moveCursorWordRight();
assertPosition(1, 18, selection.getCursor());
selection.moveCursorWordRight();
assertPosition(1, 20, selection.getCursor());
selection.moveCursorWordRight();
assertPosition(1, 22, selection.getCursor());
selection.moveCursorWordRight();
assertPosition(1, 23, selection.getCursor());
// wrap line
selection.moveCursorWordRight();
assertPosition(2, 0, selection.getCursor());
},
"test: select word right if cursor in word" : function() {
var doc = new ace.TextDocument("Juhu Kinners");
var selection = doc.getSelection();
selection.moveCursorTo(0, 2);
selection.moveCursorWordRight();
assertPosition(0, 4, selection.getCursor());
},
"test: moveCursor word left" : function() {
var doc = new ace.TextDocument( ["ab",
" Juhu Kinners (abc, 12)", " cde"].join("\n"));
var selection = doc.getSelection();
selection.moveCursorDown();
selection.moveCursorLineEnd();
assertPosition(1, 23, selection.getCursor());
selection.moveCursorWordLeft();
assertPosition(1, 22, selection.getCursor());
selection.moveCursorWordLeft();
assertPosition(1, 20, selection.getCursor());
selection.moveCursorWordLeft();
assertPosition(1, 18, selection.getCursor());
selection.moveCursorWordLeft();
assertPosition(1, 15, selection.getCursor());
selection.moveCursorWordLeft();
assertPosition(1, 13, selection.getCursor());
selection.moveCursorWordLeft();
assertPosition(1, 6, selection.getCursor());
selection.moveCursorWordLeft();
assertPosition(1, 5, selection.getCursor());
selection.moveCursorWordLeft();
assertPosition(1, 1, selection.getCursor());
selection.moveCursorWordLeft();
assertPosition(1, 0, selection.getCursor());
// wrap line
selection.moveCursorWordLeft();
assertPosition(0, 2, selection.getCursor());
},
"test: select word left if cursor in word" : function() {
var doc = new ace.TextDocument("Juhu Kinners");
var selection = doc.getSelection();
selection.moveCursorTo(0, 8);
selection.moveCursorWordLeft();
assertPosition(0, 5, selection.getCursor());
},
"test: select word right and select" : function() {
var doc = new ace.TextDocument("Juhu Kinners");
var selection = doc.getSelection();
selection.moveCursorTo(0, 0);
selection.selectWordRight();
var range = selection.getRange();
assertPosition(0, 0, range.start);
assertPosition(0, 4, range.end);
},
"test: select word left and select" : function() {
var doc = new ace.TextDocument("Juhu Kinners");
var selection = doc.getSelection();
selection.moveCursorTo(0, 3);
selection.selectWordLeft();
var range = selection.getRange();
assertPosition(0, 0, range.start);
assertPosition(0, 3, range.end);
}
});

View file

@ -37,7 +37,7 @@ var TextEditTest = TestCase("TextEditTest",
var editor = new ace.Editor(new MockRenderer(), doc);
editor.moveCursorTo(1, 3);
editor.selectDown();
editor.getSelection().selectDown();
editor.blockIndent(" ");
@ -46,9 +46,9 @@ var TextEditTest = TestCase("TextEditTest",
assertPosition(2, 7, editor.getCursorPosition());
var selection = editor.getSelectionRange();
assertPosition(1, 7, selection.start);
assertPosition(2, 7, selection.end);
var range = editor.getSelectionRange();
assertPosition(1, 7, range.start);
assertPosition(2, 7, range.end);
},
"test: outdent block" : function() {
@ -56,8 +56,8 @@ var TextEditTest = TestCase("TextEditTest",
var editor = new ace.Editor(new MockRenderer(), doc);
editor.moveCursorTo(0, 3);
editor.selectDown();
editor.selectDown();
editor.getSelection().selectDown();
editor.getSelection().selectDown();
editor.blockOutdent(" ");
assertEquals([" a12345", "b12345", " c12345"].join("\n"),
@ -65,18 +65,18 @@ var TextEditTest = TestCase("TextEditTest",
assertPosition(2, 1, editor.getCursorPosition());
var selection = editor.getSelectionRange();
assertPosition(0, 1, selection.start);
assertPosition(2, 1, selection.end);
var range = editor.getSelectionRange();
assertPosition(0, 1, range.start);
assertPosition(2, 1, range.end);
editor.blockOutdent(" ");
assertEquals([" a12345", "b12345", " c12345"].join("\n"),
doc.toString());
var selection = editor.getSelectionRange();
assertPosition(0, 1, selection.start);
assertPosition(2, 1, selection.end);
var range = editor.getSelectionRange();
assertPosition(0, 1, range.start);
assertPosition(2, 1, range.end);
},
"test: outent without a selection should update cursor" : function() {
@ -95,7 +95,7 @@ var TextEditTest = TestCase("TextEditTest",
var editor = new ace.Editor(new MockRenderer(), doc, new ace.mode.JavaScript());
editor.moveCursorTo(0, 2);
editor.selectDown();
editor.getSelection().selectDown();
editor.toggleCommentLines();
@ -111,9 +111,9 @@ var TextEditTest = TestCase("TextEditTest",
var editor = new ace.Editor(new MockRenderer(), doc, new ace.mode.JavaScript());
editor.moveCursorTo(0, 1);
editor.selectDown();
editor.selectRight();
editor.selectRight();
editor.getSelection().selectDown();
editor.getSelection().selectRight();
editor.getSelection().selectRight();
editor.toggleCommentLines();
@ -129,26 +129,26 @@ var TextEditTest = TestCase("TextEditTest",
var editor = new ace.Editor(new MockRenderer(), doc);
editor.moveCursorTo(0, 1);
editor.selectDown();
editor.getSelection().selectDown();
editor.moveLinesDown();
assertEquals(["33", "11", "22", "44"].join("\n"), doc.toString());
assertPosition(1, 0, editor.getCursorPosition());
assertPosition(3, 0, editor.getSelectionAnchor());
assertPosition(1, 0, editor.getSelectionLead());
assertPosition(3, 0, editor.getSelection().getSelectionAnchor());
assertPosition(1, 0, editor.getSelection().getSelectionLead());
editor.moveLinesDown();
assertEquals(["33", "44", "11", "22"].join("\n"), doc.toString());
assertPosition(2, 0, editor.getCursorPosition());
assertPosition(3, 2, editor.getSelectionAnchor());
assertPosition(2, 0, editor.getSelectionLead());
assertPosition(3, 2, editor.getSelection().getSelectionAnchor());
assertPosition(2, 0, editor.getSelection().getSelectionLead());
// moving again should have no effect
editor.moveLinesDown();
assertEquals(["33", "44", "11", "22"].join("\n"), doc.toString());
assertPosition(2, 0, editor.getCursorPosition());
assertPosition(3, 2, editor.getSelectionAnchor());
assertPosition(2, 0, editor.getSelectionLead());
assertPosition(3, 2, editor.getSelection().getSelectionAnchor());
assertPosition(2, 0, editor.getSelection().getSelectionLead());
},
"test: move lines up should select moved lines" : function() {
@ -156,19 +156,19 @@ var TextEditTest = TestCase("TextEditTest",
var editor = new ace.Editor(new MockRenderer(), doc);
editor.moveCursorTo(2, 1);
editor.selectDown();
editor.getSelection().selectDown();
editor.moveLinesUp();
assertEquals(["11", "33", "44", "22"].join("\n"), doc.toString());
assertPosition(1, 0, editor.getCursorPosition());
assertPosition(3, 0, editor.getSelectionAnchor());
assertPosition(1, 0, editor.getSelectionLead());
assertPosition(3, 0, editor.getSelection().getSelectionAnchor());
assertPosition(1, 0, editor.getSelection().getSelectionLead());
editor.moveLinesUp();
assertEquals(["33", "44", "11", "22"].join("\n"), doc.toString());
assertPosition(0, 0, editor.getCursorPosition());
assertPosition(2, 0, editor.getSelectionAnchor());
assertPosition(0, 0, editor.getSelectionLead());
assertPosition(2, 0, editor.getSelection().getSelectionAnchor());
assertPosition(0, 0, editor.getSelection().getSelectionLead());
},
"test: move line without active selection should move cursor to start of the moved line" : function()
@ -188,5 +188,30 @@ var TextEditTest = TestCase("TextEditTest",
editor.moveLinesUp();
assertEquals(["11", "22", "33", "44"].join("\n"), doc.toString());
assertPosition(1, 0, editor.getCursorPosition());
},
"test: input a tab with soft tab should convert it to spaces" : function() {
var doc = new ace.TextDocument("");
var editor = new ace.Editor(new MockRenderer(), doc);
editor.setTabSize(2);
editor.setUseSoftTabs(true);
editor.onTextInput("\t");
assertEquals(" ", doc.toString());
editor.setTabSize(5);
editor.onTextInput("\t");
assertEquals(" ", doc.toString());
},
"test: input tab without soft tabs should keep the tab character" : function() {
var doc = new ace.TextDocument("");
var editor = new ace.Editor(new MockRenderer(), doc);
editor.setUseSoftTabs(false);
editor.onTextInput("\t");
assertEquals("\t", doc.toString());
}
});