michael@0: (function() { michael@0: namespace = "multi_"; michael@0: michael@0: function hasSelections(cm) { michael@0: var sels = cm.listSelections(); michael@0: var given = (arguments.length - 1) / 4; michael@0: if (sels.length != given) michael@0: throw new Failure("expected " + given + " selections, found " + sels.length); michael@0: for (var i = 0, p = 1; i < given; i++, p += 4) { michael@0: var anchor = Pos(arguments[p], arguments[p + 1]); michael@0: var head = Pos(arguments[p + 2], arguments[p + 3]); michael@0: eqPos(sels[i].anchor, anchor, "anchor of selection " + i); michael@0: eqPos(sels[i].head, head, "head of selection " + i); michael@0: } michael@0: } michael@0: function hasCursors(cm) { michael@0: var sels = cm.listSelections(); michael@0: var given = (arguments.length - 1) / 2; michael@0: if (sels.length != given) michael@0: throw new Failure("expected " + given + " selections, found " + sels.length); michael@0: for (var i = 0, p = 1; i < given; i++, p += 2) { michael@0: eqPos(sels[i].anchor, sels[i].head, "something selected for " + i); michael@0: var head = Pos(arguments[p], arguments[p + 1]); michael@0: eqPos(sels[i].head, head, "selection " + i); michael@0: } michael@0: } michael@0: michael@0: testCM("getSelection", function(cm) { michael@0: select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)}, {anchor: Pos(2, 2), head: Pos(2, 0)}); michael@0: eq(cm.getSelection(), "1234\n56\n90"); michael@0: eq(cm.getSelection(false).join("|"), "1234|56|90"); michael@0: eq(cm.getSelections().join("|"), "1234\n56|90"); michael@0: }, {value: "1234\n5678\n90"}); michael@0: michael@0: testCM("setSelection", function(cm) { michael@0: select(cm, Pos(3, 0), Pos(0, 0), {anchor: Pos(2, 5), head: Pos(1, 0)}); michael@0: hasSelections(cm, 0, 0, 0, 0, michael@0: 2, 5, 1, 0, michael@0: 3, 0, 3, 0); michael@0: cm.setSelection(Pos(1, 2), Pos(1, 1)); michael@0: hasSelections(cm, 1, 2, 1, 1); michael@0: select(cm, {anchor: Pos(1, 1), head: Pos(2, 4)}, michael@0: {anchor: Pos(0, 0), head: Pos(1, 3)}, michael@0: Pos(3, 0), Pos(2, 2)); michael@0: hasSelections(cm, 0, 0, 2, 4, michael@0: 3, 0, 3, 0); michael@0: cm.setSelections([{anchor: Pos(0, 1), head: Pos(0, 2)}, michael@0: {anchor: Pos(1, 1), head: Pos(1, 2)}, michael@0: {anchor: Pos(2, 1), head: Pos(2, 2)}], 1); michael@0: eqPos(cm.getCursor("head"), Pos(1, 2)); michael@0: eqPos(cm.getCursor("anchor"), Pos(1, 1)); michael@0: eqPos(cm.getCursor("from"), Pos(1, 1)); michael@0: eqPos(cm.getCursor("to"), Pos(1, 2)); michael@0: cm.setCursor(Pos(1, 1)); michael@0: hasCursors(cm, 1, 1); michael@0: }, {value: "abcde\nabcde\nabcde\n"}); michael@0: michael@0: testCM("somethingSelected", function(cm) { michael@0: select(cm, Pos(0, 1), {anchor: Pos(0, 3), head: Pos(0, 5)}); michael@0: eq(cm.somethingSelected(), true); michael@0: select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5)); michael@0: eq(cm.somethingSelected(), false); michael@0: }, {value: "123456789"}); michael@0: michael@0: testCM("extendSelection", function(cm) { michael@0: select(cm, Pos(0, 1), Pos(1, 1), Pos(2, 1)); michael@0: cm.setExtending(true); michael@0: cm.extendSelections([Pos(0, 2), Pos(1, 0), Pos(2, 3)]); michael@0: hasSelections(cm, 0, 1, 0, 2, michael@0: 1, 1, 1, 0, michael@0: 2, 1, 2, 3); michael@0: cm.extendSelection(Pos(2, 4), Pos(2, 0)); michael@0: hasSelections(cm, 2, 4, 2, 0); michael@0: }, {value: "1234\n1234\n1234"}); michael@0: michael@0: testCM("addSelection", function(cm) { michael@0: select(cm, Pos(0, 1), Pos(1, 1)); michael@0: cm.addSelection(Pos(0, 0), Pos(0, 4)); michael@0: hasSelections(cm, 0, 0, 0, 4, michael@0: 1, 1, 1, 1); michael@0: cm.addSelection(Pos(2, 2)); michael@0: hasSelections(cm, 0, 0, 0, 4, michael@0: 1, 1, 1, 1, michael@0: 2, 2, 2, 2); michael@0: }, {value: "1234\n1234\n1234"}); michael@0: michael@0: testCM("replaceSelection", function(cm) { michael@0: var selections = [{anchor: Pos(0, 0), head: Pos(0, 1)}, michael@0: {anchor: Pos(0, 2), head: Pos(0, 3)}, michael@0: {anchor: Pos(0, 4), head: Pos(0, 5)}, michael@0: {anchor: Pos(2, 1), head: Pos(2, 4)}, michael@0: {anchor: Pos(2, 5), head: Pos(2, 6)}]; michael@0: var val = "123456\n123456\n123456"; michael@0: cm.setValue(val); michael@0: cm.setSelections(selections); michael@0: cm.replaceSelection("ab", "around"); michael@0: eq(cm.getValue(), "ab2ab4ab6\n123456\n1ab5ab"); michael@0: hasSelections(cm, 0, 0, 0, 2, michael@0: 0, 3, 0, 5, michael@0: 0, 6, 0, 8, michael@0: 2, 1, 2, 3, michael@0: 2, 4, 2, 6); michael@0: cm.setValue(val); michael@0: cm.setSelections(selections); michael@0: cm.replaceSelection("", "around"); michael@0: eq(cm.getValue(), "246\n123456\n15"); michael@0: hasSelections(cm, 0, 0, 0, 0, michael@0: 0, 1, 0, 1, michael@0: 0, 2, 0, 2, michael@0: 2, 1, 2, 1, michael@0: 2, 2, 2, 2); michael@0: cm.setValue(val); michael@0: cm.setSelections(selections); michael@0: cm.replaceSelection("X\nY\nZ", "around"); michael@0: hasSelections(cm, 0, 0, 2, 1, michael@0: 2, 2, 4, 1, michael@0: 4, 2, 6, 1, michael@0: 8, 1, 10, 1, michael@0: 10, 2, 12, 1); michael@0: cm.replaceSelection("a", "around"); michael@0: hasSelections(cm, 0, 0, 0, 1, michael@0: 0, 2, 0, 3, michael@0: 0, 4, 0, 5, michael@0: 2, 1, 2, 2, michael@0: 2, 3, 2, 4); michael@0: cm.replaceSelection("xy", "start"); michael@0: hasSelections(cm, 0, 0, 0, 0, michael@0: 0, 3, 0, 3, michael@0: 0, 6, 0, 6, michael@0: 2, 1, 2, 1, michael@0: 2, 4, 2, 4); michael@0: cm.replaceSelection("z\nf"); michael@0: hasSelections(cm, 1, 1, 1, 1, michael@0: 2, 1, 2, 1, michael@0: 3, 1, 3, 1, michael@0: 6, 1, 6, 1, michael@0: 7, 1, 7, 1); michael@0: eq(cm.getValue(), "z\nfxy2z\nfxy4z\nfxy6\n123456\n1z\nfxy5z\nfxy"); michael@0: }); michael@0: michael@0: function select(cm) { michael@0: var sels = []; michael@0: for (var i = 1; i < arguments.length; i++) { michael@0: var arg = arguments[i]; michael@0: if (arg.head) sels.push(arg); michael@0: else sels.push({head: arg, anchor: arg}); michael@0: } michael@0: cm.setSelections(sels, sels.length - 1); michael@0: } michael@0: michael@0: testCM("indentSelection", function(cm) { michael@0: select(cm, Pos(0, 1), Pos(1, 1)); michael@0: cm.indentSelection(4); michael@0: eq(cm.getValue(), " foo\n bar\nbaz"); michael@0: michael@0: select(cm, Pos(0, 2), Pos(0, 3), Pos(0, 4)); michael@0: cm.indentSelection(-2); michael@0: eq(cm.getValue(), " foo\n bar\nbaz"); michael@0: michael@0: select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)}, michael@0: {anchor: Pos(1, 3), head: Pos(2, 0)}); michael@0: cm.indentSelection(-2); michael@0: eq(cm.getValue(), "foo\n bar\nbaz"); michael@0: }, {value: "foo\nbar\nbaz"}); michael@0: michael@0: testCM("killLine", function(cm) { michael@0: select(cm, Pos(0, 1), Pos(0, 2), Pos(1, 1)); michael@0: cm.execCommand("killLine"); michael@0: eq(cm.getValue(), "f\nb\nbaz"); michael@0: cm.execCommand("killLine"); michael@0: eq(cm.getValue(), "fbbaz"); michael@0: cm.setValue("foo\nbar\nbaz"); michael@0: select(cm, Pos(0, 1), {anchor: Pos(0, 2), head: Pos(2, 1)}); michael@0: cm.execCommand("killLine"); michael@0: eq(cm.getValue(), "faz"); michael@0: }, {value: "foo\nbar\nbaz"}); michael@0: michael@0: testCM("deleteLine", function(cm) { michael@0: select(cm, Pos(0, 0), michael@0: {head: Pos(0, 1), anchor: Pos(2, 0)}, michael@0: Pos(4, 0)); michael@0: cm.execCommand("deleteLine"); michael@0: eq(cm.getValue(), "4\n6\n7"); michael@0: select(cm, Pos(2, 1)); michael@0: cm.execCommand("deleteLine"); michael@0: eq(cm.getValue(), "4\n6\n"); michael@0: }, {value: "1\n2\n3\n4\n5\n6\n7"}); michael@0: michael@0: testCM("deleteH", function(cm) { michael@0: select(cm, Pos(0, 4), {anchor: Pos(1, 4), head: Pos(1, 5)}); michael@0: cm.execCommand("delWordAfter"); michael@0: eq(cm.getValue(), "foo bar baz\nabc ef ghi\n"); michael@0: cm.execCommand("delWordAfter"); michael@0: eq(cm.getValue(), "foo baz\nabc ghi\n"); michael@0: cm.execCommand("delCharBefore"); michael@0: cm.execCommand("delCharBefore"); michael@0: eq(cm.getValue(), "fo baz\nab ghi\n"); michael@0: select(cm, Pos(0, 3), Pos(0, 4), Pos(0, 5)); michael@0: cm.execCommand("delWordAfter"); michael@0: eq(cm.getValue(), "fo \nab ghi\n"); michael@0: }, {value: "foo bar baz\nabc def ghi\n"}); michael@0: michael@0: testCM("goLineStart", function(cm) { michael@0: select(cm, Pos(0, 2), Pos(0, 3), Pos(1, 1)); michael@0: cm.execCommand("goLineStart"); michael@0: hasCursors(cm, 0, 0, 1, 0); michael@0: select(cm, Pos(1, 1), Pos(0, 1)); michael@0: cm.setExtending(true); michael@0: cm.execCommand("goLineStart"); michael@0: hasSelections(cm, 0, 1, 0, 0, michael@0: 1, 1, 1, 0); michael@0: }, {value: "foo\nbar\nbaz"}); michael@0: michael@0: testCM("moveV", function(cm) { michael@0: select(cm, Pos(0, 2), Pos(1, 2)); michael@0: cm.execCommand("goLineDown"); michael@0: hasCursors(cm, 1, 2, 2, 2); michael@0: cm.execCommand("goLineUp"); michael@0: hasCursors(cm, 0, 2, 1, 2); michael@0: cm.execCommand("goLineUp"); michael@0: hasCursors(cm, 0, 0, 0, 2); michael@0: cm.execCommand("goLineUp"); michael@0: hasCursors(cm, 0, 0); michael@0: select(cm, Pos(0, 2), Pos(1, 2)); michael@0: cm.setExtending(true); michael@0: cm.execCommand("goLineDown"); michael@0: hasSelections(cm, 0, 2, 2, 2); michael@0: }, {value: "12345\n12345\n12345"}); michael@0: michael@0: testCM("moveH", function(cm) { michael@0: select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5), Pos(2, 3)); michael@0: cm.execCommand("goCharRight"); michael@0: hasCursors(cm, 0, 2, 0, 4, 1, 0, 2, 4); michael@0: cm.execCommand("goCharLeft"); michael@0: hasCursors(cm, 0, 1, 0, 3, 0, 5, 2, 3); michael@0: for (var i = 0; i < 15; i++) michael@0: cm.execCommand("goCharRight"); michael@0: hasCursors(cm, 2, 4, 2, 5); michael@0: }, {value: "12345\n12345\n12345"}); michael@0: michael@0: testCM("newlineAndIndent", function(cm) { michael@0: select(cm, Pos(0, 5), Pos(1, 5)); michael@0: cm.execCommand("newlineAndIndent"); michael@0: hasCursors(cm, 1, 2, 3, 2); michael@0: eq(cm.getValue(), "x = [\n 1];\ny = [\n 2];"); michael@0: cm.undo(); michael@0: eq(cm.getValue(), "x = [1];\ny = [2];"); michael@0: hasCursors(cm, 0, 5, 1, 5); michael@0: select(cm, Pos(0, 5), Pos(0, 6)); michael@0: cm.execCommand("newlineAndIndent"); michael@0: hasCursors(cm, 1, 2, 2, 0); michael@0: eq(cm.getValue(), "x = [\n 1\n];\ny = [2];"); michael@0: }, {value: "x = [1];\ny = [2];", mode: "javascript"}); michael@0: michael@0: testCM("goDocStartEnd", function(cm) { michael@0: select(cm, Pos(0, 1), Pos(1, 1)); michael@0: cm.execCommand("goDocStart"); michael@0: hasCursors(cm, 0, 0); michael@0: select(cm, Pos(0, 1), Pos(1, 1)); michael@0: cm.execCommand("goDocEnd"); michael@0: hasCursors(cm, 1, 3); michael@0: select(cm, Pos(0, 1), Pos(1, 1)); michael@0: cm.setExtending(true); michael@0: cm.execCommand("goDocEnd"); michael@0: hasSelections(cm, 1, 1, 1, 3); michael@0: }, {value: "abc\ndef"}); michael@0: michael@0: testCM("selectionHistory", function(cm) { michael@0: for (var i = 0; i < 3; ++i) michael@0: cm.addSelection(Pos(0, i * 2), Pos(0, i * 2 + 1)); michael@0: cm.execCommand("undoSelection"); michael@0: eq(cm.getSelection(), "1\n2"); michael@0: cm.execCommand("undoSelection"); michael@0: eq(cm.getSelection(), "1"); michael@0: cm.execCommand("undoSelection"); michael@0: eq(cm.getSelection(), ""); michael@0: eqPos(cm.getCursor(), Pos(0, 0)); michael@0: cm.execCommand("redoSelection"); michael@0: eq(cm.getSelection(), "1"); michael@0: cm.execCommand("redoSelection"); michael@0: eq(cm.getSelection(), "1\n2"); michael@0: cm.execCommand("redoSelection"); michael@0: eq(cm.getSelection(), "1\n2\n3"); michael@0: }, {value: "1 2 3"}); michael@0: })();