diff options
Diffstat (limited to 'devtools/client/shared/sourceeditor/test')
43 files changed, 12974 insertions, 0 deletions
diff --git a/devtools/client/shared/sourceeditor/test/CodeMirrorTestActors.sys.mjs b/devtools/client/shared/sourceeditor/test/CodeMirrorTestActors.sys.mjs new file mode 100644 index 0000000000..bb0457599b --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/CodeMirrorTestActors.sys.mjs @@ -0,0 +1,49 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +let gCallback; + +export class CodeMirrorTestParent extends JSWindowActorParent { + static setCallback(callback) { + gCallback = callback; + } + + receiveMessage(message) { + if (gCallback) { + gCallback(message.name, message.data); + } + } +} + +export class CodeMirrorTestChild extends JSWindowActorChild { + handleEvent(event) { + if (event.type == "DOMWindowCreated") { + this.contentWindow.wrappedJSObject.mozilla_setStatus = ( + statusMsg, + type, + customMsg + ) => { + this.sendAsyncMessage("setStatus", { + statusMsg, + type, + customMsg, + }); + }; + + this.check(); + } + } + + check() { + const doc = this.contentWindow.document; + const out = doc.getElementById("status"); + if (!out || !out.classList.contains("done")) { + this.contentWindow.setTimeout(() => this.check(), 100); + return; + } + + this.sendAsyncMessage("done", { + failed: this.contentWindow.wrappedJSObject.failed, + }); + } +} diff --git a/devtools/client/shared/sourceeditor/test/browser.ini b/devtools/client/shared/sourceeditor/test/browser.ini new file mode 100644 index 0000000000..055f478b7e --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser.ini @@ -0,0 +1,51 @@ +[DEFAULT] +tags = devtools +subsuite = devtools +support-files = + codemirror/comment_test.js + codemirror/doc_test.js + codemirror/driver.js + codemirror/emacs_test.js + codemirror/mode_test.css + codemirror/mode_test.js + codemirror/multi_test.js + codemirror/search_test.js + codemirror/sublime_test.js + codemirror/test.js + codemirror/vim_test.js + codemirror/codemirror.html + codemirror/vimemacs.html + codemirror/mode/javascript/test.js + css_statemachine_testcases.css + css_statemachine_tests.json + css_autocompletion_tests.json + head.js + head.xhtml + cm_mode_ruby.js + cm_script_injection_test.js + CodeMirrorTestActors.sys.mjs + !/devtools/client/shared/test/shared-head.js + !/devtools/client/shared/test/telemetry-test-helpers.js + +[browser_editor_autocomplete_basic.js] +[browser_editor_autocomplete_events.js] +[browser_editor_alt_b_f.js] +[browser_editor_basic.js] +[browser_editor_cursor_blink.js] +[browser_editor_cursor.js] +[browser_editor_disableSearchAddon.js] +[browser_editor_find_again.js] +[browser_editor_goto_line.js] +[browser_editor_history.js] +[browser_editor_markers.js] +[browser_editor_movelines.js] +[browser_editor_prefs.js] +[browser_editor_addons.js] +[browser_codemirror.js] +[browser_css_autocompletion.js] +[browser_css_getInfo.js] +[browser_css_statemachine.js] +[browser_detectindent.js] +[browser_vimemacs.js] +skip-if = os == 'linux'&&debug # bug 981707 + diff --git a/devtools/client/shared/sourceeditor/test/browser_codemirror.js b/devtools/client/shared/sourceeditor/test/browser_codemirror.js new file mode 100644 index 0000000000..ca19c20d9f --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_codemirror.js @@ -0,0 +1,33 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const URI = + "chrome://mochitests/content/browser/devtools/client/shared/sourceeditor/" + + "test/codemirror/codemirror.html"; + +add_task(async function test() { + requestLongerTimeout(3); + + /* + * In devtools/client/shared/sourceeditor/test/codemirror/search_test.js there is a test + * multilineInsensitiveSlow which assumes an operation takes less than 100ms. + * With a precision of 100ms, if we get unlikely and begin execution towards the + * end of one spot (e.g. at 95 ms) we will clamp down, take (e.g.) 10ms to execute + * and it will appear to take 100ms. + * + * To avoid this, we hardcode to 2ms of precision. + * + * In theory we don't need to set the pref for all of CodeMirror, in practice + * it seems very difficult to set a pref for just one of the tests. + */ + await SpecialPowers.pushPrefEnv({ + set: [ + ["privacy.reduceTimerPrecision", true], + ["privacy.resistFingerprinting.reduceTimerPrecision.microseconds", 2000], + ], + }); + + await runCodeMirrorTest(URI); +}); diff --git a/devtools/client/shared/sourceeditor/test/browser_css_autocompletion.js b/devtools/client/shared/sourceeditor/test/browser_css_autocompletion.js new file mode 100644 index 0000000000..558e9d9587 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_css_autocompletion.js @@ -0,0 +1,171 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const CSSCompleter = require("resource://devtools/client/shared/sourceeditor/css-autocompleter.js"); + +const CSS_URI = + "http://mochi.test:8888/browser/devtools/client/shared/sourceeditor" + + "/test/css_statemachine_testcases.css"; +const TESTS_URI = + "http://mochi.test:8888/browser/devtools/client" + + "/shared/sourceeditor/test/css_autocompletion_tests.json"; + +const source = read(CSS_URI); +const { tests } = JSON.parse(read(TESTS_URI)); + +const TEST_URI = + "data:text/html;charset=UTF-8," + + encodeURIComponent( + [ + "<!DOCTYPE html>", + "<html>", + " <head>", + " <title>CSS State machine tests.</title>", + " <style type='text/css'>", + "#progress {", + " width: 500px; height: 30px;", + " border: 1px solid black;", + " position: relative", + "}", + "#progress div {", + " width: 0%; height: 100%;", + " background: green;", + " position: absolute;", + " z-index: -1; top: 0", + "}", + "#progress.failed div {", + " background: red !important;", + "}", + "#progress.failed:after {", + " content: 'Some tests failed';", + " color: white", + "}", + "#progress:before {", + " content: 'Running test ' attr(data-progress) ' of " + + tests.length + + "';", + " color: white;", + " text-shadow: 0 0 2px darkgreen;", + "}", + " </style>", + " </head>", + " <body>", + " <h2>State machine tests for CSS autocompleter.</h2><br>", + " <div id='progress' data-progress='0'>", + " <div></div>", + " </div>", + " <div id='devtools-menu' class='devtools-toolbarbutton'></div>", + " <div id='devtools-toolbarbutton' class='devtools-menulist'></div>", + " <div id='devtools-anotherone'></div>", + " <div id='devtools-yetagain'></div>", + " <div id='devtools-itjustgoeson'></div>", + " <div id='devtools-okstopitnow'></div>", + " <div class='hidden-labels-box devtools-toolbarbutton devtools-menulist'></div>", + " <div class='devtools-menulist'></div>", + " <div class='devtools-menulist'></div>", + /* eslint-disable max-len */ + " <tabs class='devtools-toolbarbutton'><tab></tab><tab></tab><tab></tab></tabs><tabs></tabs>", + /* eslint-enable max-len */ + " <button class='category-name visible'></button>", + " <div class='devtools-toolbarbutton' label='true'>", + " <hbox class='toolbarbutton-menubutton-button'></hbox></div>", + " </body>", + " </html>", + ].join("\n") + ); + +let browser; +let index = 0; +let completer = null; +let inspector; + +add_task(async function test() { + const tab = await addTab(TEST_URI); + browser = tab.linkedBrowser; + await runTests(); + browser = null; + gBrowser.removeCurrentTab(); +}); + +async function runTests() { + const target = await createAndAttachTargetForTab(gBrowser.selectedTab); + inspector = await target.getFront("inspector"); + const walker = inspector.walker; + completer = new CSSCompleter({ + walker, + cssProperties: getClientCssProperties(), + }); + await checkStateAndMoveOn(); + await completer.walker.release(); + await target.destroy(); + inspector = null; + completer = null; +} + +async function checkStateAndMoveOn() { + if (index == tests.length) { + return; + } + + const [lineCh, expectedSuggestions] = tests[index]; + const [line, ch] = lineCh; + + ++index; + await SpecialPowers.spawn( + browser, + [[index, tests.length]], + function ([idx, len]) { + const progress = content.document.getElementById("progress"); + const progressDiv = content.document.querySelector("#progress > div"); + progress.dataset.progress = idx; + progressDiv.style.width = (100 * idx) / len + "%"; + } + ); + + const actualSuggestions = await completer.complete(limit(source, lineCh), { + line, + ch, + }); + await checkState(expectedSuggestions, actualSuggestions); + await checkStateAndMoveOn(); +} + +async function checkState(expected, actual) { + if (expected.length != actual.length) { + ok( + false, + "Number of suggestions did not match up for state " + + index + + ". Expected: " + + expected.length + + ", Actual: " + + actual.length + ); + await SpecialPowers.spawn(browser, [], function () { + const progress = content.document.getElementById("progress"); + progress.classList.add("failed"); + }); + return; + } + + for (let i = 0; i < actual.length; i++) { + if (expected[i] != actual[i].label) { + ok( + false, + "Suggestion " + + i + + " of state " + + index + + " did not match up" + + ". Expected: " + + expected[i] + + ". Actual: " + + actual[i].label + ); + return; + } + } + ok(true, "Test " + index + " passed. "); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_css_getInfo.js b/devtools/client/shared/sourceeditor/test/browser_css_getInfo.js new file mode 100644 index 0000000000..c4d9385edf --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_css_getInfo.js @@ -0,0 +1,250 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const CSSCompleter = require("resource://devtools/client/shared/sourceeditor/css-autocompleter.js"); + +const source = [ + ".devtools-toolbar {", + " -moz-appearance: none;", + " padding:4px 3px;border-bottom-width: 1px;", + " border-bottom-style: solid;", + "}", + "", + "#devtools-menu.devtools-menulist,", + ".devtools-toolbarbutton#devtools-menu {", + " -moz-appearance: none;", + " align-items: center;", + " min-width: 78px;", + " min-height: 22px;", + " text-shadow: 0 -1px 0 hsla(210,8%,5%,.45);", + " border: 1px solid hsla(210,8%,5%,.45);", + " border-radius: 3px;", + " background: linear-gradient(hsla(212,7%,57%,.35),", + " hsla(212,7%,57%,.1)) padding-box;", + " margin: 0 3px;", + " color: inherit;", + "}", + "", + ".devtools-toolbarbutton > hbox.toolbarbutton-menubutton-button {", + " flex-direction: row;", + "}", + "", + ".devtools-menulist:active,", + "#devtools-toolbarbutton:focus {", + " outline: 1px dotted hsla(210,30%,85%,0.7);", + " outline-offset : -4px;", + "}", + "", + ".devtools-toolbarbutton:not([label]) {", + " min-width: 32px;", + "}", + "", + ".devtools-toolbarbutton:not([label]) > .toolbarbutton-text, .devtools-toolbar {", + " display: none;", + "}", +].join("\n"); + +// Format of test cases : +// [ +// {line, ch}, - The caret position at which the getInfo call should be made +// expectedState, - The expected state at the caret +// expectedSelector, - The expected selector for the state +// expectedProperty, - The expected property name for states value and property +// expectedValue, - If state is value, then the expected value +// ] + +/* eslint-disable max-len */ +const tests = [ + [{ line: 0, ch: 13 }, "selector", ".devtools-toolbar"], + [ + { line: 8, ch: 13 }, + "property", + [ + "#devtools-menu.devtools-menulist", + ".devtools-toolbarbutton#devtools-menu ", + ], + "-moz-appearance", + ], + [ + { line: 28, ch: 25 }, + "value", + [".devtools-menulist:active", "#devtools-toolbarbutton:focus "], + "outline-offset", + "-4px", + ], + [{ line: 4, ch: 1 }, "null"], + [{ line: 5, ch: 0 }, "null"], + [{ line: 31, ch: 13 }, "selector", ".devtools-toolbarbutton:not([label])"], + [ + { line: 35, ch: 23 }, + "selector", + ".devtools-toolbarbutton:not([label]) > .toolbarbutton-text", + ], + [{ line: 35, ch: 70 }, "selector", ".devtools-toolbar"], + [ + { line: 27, ch: 14 }, + "value", + [".devtools-menulist:active", "#devtools-toolbarbutton:focus "], + "outline", + "1px dotted hsla(210,30%,85%,0.7)", + ], + [ + { line: 16, ch: 16 }, + "value", + [ + "#devtools-menu.devtools-menulist", + ".devtools-toolbarbutton#devtools-menu ", + ], + "background", + "linear-gradient(hsla(212,7%,57%,.35),\n hsla(212,7%,57%,.1)) padding-box", + ], + [ + { line: 16, ch: 3 }, + "value", + [ + "#devtools-menu.devtools-menulist", + ".devtools-toolbarbutton#devtools-menu ", + ], + "background", + "linear-gradient(hsla(212,7%,57%,.35),\n hsla(212,7%,57%,.1)) padding-box", + ], + [ + { line: 15, ch: 25 }, + "value", + [ + "#devtools-menu.devtools-menulist", + ".devtools-toolbarbutton#devtools-menu ", + ], + "background", + "linear-gradient(hsla(212,7%,57%,.35),\n hsla(212,7%,57%,.1)) padding-box", + ], +]; +/* eslint-enable max-len */ + +const TEST_URI = + "data:text/html;charset=UTF-8," + + encodeURIComponent( + [ + "<!DOCTYPE html>", + "<html>", + " <head>", + " <title>CSS contextual information tests.</title>", + " <style type='text/css'>", + "#progress {", + " width: 500px; height: 30px;", + " border: 1px solid black;", + " position: relative", + "}", + "#progress div {", + " width: 0%; height: 100%;", + " background: green;", + " position: absolute;", + " z-index: -1; top: 0", + "}", + "#progress.failed div {", + " background: red !important;", + "}", + "#progress.failed:after {", + " content: 'Some tests failed';", + " color: white", + "}", + "#progress:before {", + " content: 'Running test ' attr(data-progress) ' of " + + tests.length + + "';", + " color: white;", + " text-shadow: 0 0 2px darkgreen;", + "}", + " </style>", + " </head>", + " <body>", + " <h2>State machine tests for CSS autocompleter.</h2><br>", + " <div id='progress' data-progress='0'>", + " <div></div>", + " </div>", + " </body>", + " </html>", + ].join("\n") + ); + +add_task(async function test() { + const tab = await addTab(TEST_URI); + const browser = tab.linkedBrowser; + + const completer = new CSSCompleter({ + cssProperties: getClientCssProperties(), + }); + const matches = (arr, toCheck) => !arr.some((x, i) => x != toCheck[i]); + const checkState = (expected, actual) => { + if (expected[0] == "null" && actual == null) { + return true; + } else if ( + expected[0] == actual.state && + expected[0] == "selector" && + expected[1] == actual.selector + ) { + return true; + } else if ( + expected[0] == actual.state && + expected[0] == "property" && + matches(expected[1], actual.selectors) && + expected[2] == actual.propertyName + ) { + return true; + } else if ( + expected[0] == actual.state && + expected[0] == "value" && + matches(expected[1], actual.selectors) && + expected[2] == actual.propertyName && + expected[3] == actual.value + ) { + return true; + } + return false; + }; + + let i = 0; + for (const expected of tests) { + ++i; + const caret = expected.splice(0, 1)[0]; + await SpecialPowers.spawn( + browser, + [[i, tests.length]], + function ([idx, len]) { + const progress = content.document.getElementById("progress"); + const progressDiv = content.document.querySelector("#progress > div"); + progress.dataset.progress = idx; + progressDiv.style.width = (100 * idx) / len + "%"; + } + ); + const actual = completer.getInfoAt(source, caret); + if (checkState(expected, actual)) { + ok(true, "Test " + i + " passed. "); + } else { + ok( + false, + "Test " + + i + + " failed. Expected state : [" + + expected + + "] " + + "but found [" + + actual.state + + ", " + + (actual.selector || actual.selectors) + + ", " + + actual.propertyName + + ", " + + actual.value + + "]." + ); + await SpecialPowers.spawn(browser, [], function () { + const progress = content.document.getElementById("progress"); + progress.classList.add("failed"); + }); + } + } + gBrowser.removeCurrentTab(); +}); diff --git a/devtools/client/shared/sourceeditor/test/browser_css_statemachine.js b/devtools/client/shared/sourceeditor/test/browser_css_statemachine.js new file mode 100644 index 0000000000..103322904f --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_css_statemachine.js @@ -0,0 +1,144 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const CSSCompleter = require("resource://devtools/client/shared/sourceeditor/css-autocompleter.js"); + +const CSS_URI = + "http://mochi.test:8888/browser/devtools/client/shared/sourceeditor" + + "/test/css_statemachine_testcases.css"; +const TESTS_URI = + "http://mochi.test:8888/browser/devtools/client" + + "/shared/sourceeditor/test/css_statemachine_tests.json"; + +const source = read(CSS_URI); +const { tests } = JSON.parse(read(TESTS_URI)); + +const TEST_URI = + "data:text/html;charset=UTF-8," + + encodeURIComponent( + [ + "<!DOCTYPE html>", + "<html>", + " <head>", + " <title>CSS State machine tests.</title>", + " <style type='text/css'>", + "#progress {", + " width: 500px; height: 30px;", + " border: 1px solid black;", + " position: relative", + "}", + "#progress div {", + " width: 0%; height: 100%;", + " background: green;", + " position: absolute;", + " z-index: -1; top: 0", + "}", + "#progress.failed div {", + " background: red !important;", + "}", + "#progress.failed:after {", + " content: 'Some tests failed';", + " color: white", + "}", + "#progress:before {", + " content: 'Running test ' attr(data-progress) ' of " + + tests.length + + "';", + " color: white;", + " text-shadow: 0 0 2px darkgreen;", + "}", + " </style>", + " </head>", + " <body>", + " <h2>State machine tests for CSS autocompleter.</h2><br>", + " <div id='progress' data-progress='0'>", + " <div></div>", + " </div>", + " </body>", + " </html>", + ].join("\n") + ); + +add_task(async function test() { + const tab = await addTab(TEST_URI); + const browser = tab.linkedBrowser; + + const completer = new CSSCompleter({ + cssProperties: getClientCssProperties(), + }); + const checkState = state => { + if (state[0] == "null" && (!completer.state || completer.state == "null")) { + return true; + } else if ( + state[0] == completer.state && + state[0] == "selector" && + state[1] == completer.selectorState && + state[2] == completer.completing && + state[3] == completer.selector + ) { + return true; + } else if ( + state[0] == completer.state && + state[0] == "value" && + state[2] == completer.completing && + state[3] == completer.propertyName + ) { + return true; + } else if ( + state[0] == completer.state && + state[2] == completer.completing && + state[0] != "selector" && + state[0] != "value" + ) { + return true; + } + return false; + }; + + let i = 0; + for (const testcase of tests) { + ++i; + await SpecialPowers.spawn( + browser, + [[i, tests.length]], + function ([idx, len]) { + const progress = content.document.getElementById("progress"); + const progressDiv = content.document.querySelector("#progress > div"); + progress.dataset.progress = idx; + progressDiv.style.width = (100 * idx) / len + "%"; + } + ); + completer.resolveState(limit(source, testcase[0]), { + line: testcase[0][0], + ch: testcase[0][1], + }); + if (checkState(testcase[1])) { + ok(true, "Test " + i + " passed. "); + } else { + ok( + false, + "Test " + + i + + " failed. Expected state : [" + + testcase[1] + + "] " + + "but found [" + + completer.state + + ", " + + completer.selectorState + + ", " + + completer.completing + + ", " + + (completer.propertyName || completer.selector) + + "]." + ); + await SpecialPowers.spawn(browser, [], function () { + const progress = content.document.getElementById("progress"); + progress.classList.add("failed"); + }); + } + } + gBrowser.removeCurrentTab(); +}); diff --git a/devtools/client/shared/sourceeditor/test/browser_detectindent.js b/devtools/client/shared/sourceeditor/test/browser_detectindent.js new file mode 100644 index 0000000000..80f2487417 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_detectindent.js @@ -0,0 +1,99 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const TWO_SPACES_CODE = [ + "/*", + " * tricky comment block", + " */", + "div {", + " color: red;", + " background: blue;", + "}", + " ", + "span {", + " padding-left: 10px;", + "}", +].join("\n"); + +const FOUR_SPACES_CODE = [ + "var obj = {", + " addNumbers: function() {", + " var x = 5;", + " var y = 18;", + " return x + y;", + " },", + " ", + " /*", + " * Do some stuff to two numbers", + " * ", + " * @param x", + " * @param y", + " * ", + " * @return the result of doing stuff", + " */", + " subtractNumbers: function(x, y) {", + " var x += 7;", + " var y += 18;", + " var result = x - y;", + " result %= 2;", + " }", + "}", +].join("\n"); + +const TABS_CODE = [ + "/*", + " * tricky comment block", + " */", + "div {", + "\tcolor: red;", + "\tbackground: blue;", + "}", + "", + "span {", + "\tpadding-left: 10px;", + "}", +].join("\n"); + +const NONE_CODE = [ + "var x = 0;", + " // stray thing", + "var y = 9;", + " ", + "", +].join("\n"); + +async function test() { + waitForExplicitFinish(); + + const { ed, win } = await setup(); + is(ed.getOption("indentUnit"), 2, "2 spaces before code added"); + is(ed.getOption("indentWithTabs"), false, "spaces is default"); + + ed.setText(NONE_CODE); + is(ed.getOption("indentUnit"), 2, "2 spaces after un-detectable code"); + is( + ed.getOption("indentWithTabs"), + false, + "spaces still set after un-detectable code" + ); + + ed.setText(FOUR_SPACES_CODE); + is(ed.getOption("indentUnit"), 4, "4 spaces detected in 4 space code"); + is(ed.getOption("indentWithTabs"), false, "spaces detected in 4 space code"); + + ed.setText(TWO_SPACES_CODE); + is(ed.getOption("indentUnit"), 2, "2 spaces detected in 2 space code"); + is(ed.getOption("indentWithTabs"), false, "spaces detected in 2 space code"); + + ed.setText(TABS_CODE); + is(ed.getOption("indentUnit"), 2, "2 space indentation unit"); + is( + ed.getOption("indentWithTabs"), + true, + "tabs detected in majority tabs code" + ); + + teardown(ed, win); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_addons.js b/devtools/client/shared/sourceeditor/test/browser_editor_addons.js new file mode 100644 index 0000000000..85aec38ec3 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_addons.js @@ -0,0 +1,33 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +async function test() { + waitForExplicitFinish(); + + const { ed, win } = await setup(); + const doc = win.document.querySelector("iframe").contentWindow.document; + + // trailingspace.js + ed.setText("Hello "); + ed.setOption("showTrailingSpace", false); + ok(!doc.querySelector(".cm-trailingspace")); + ed.setOption("showTrailingSpace", true); + ok(doc.querySelector(".cm-trailingspace")); + + // foldcode.js and foldgutter.js + ed.setMode(Editor.modes.js); + ed.setText("function main() {\nreturn 'Hello, World!';\n}"); + executeSoon(() => testFold(doc, ed, win)); +} + +function testFold(doc, ed, win) { + // Wait until folding arrow is there. + if (!doc.querySelector(".CodeMirror-foldgutter-open")) { + executeSoon(() => testFold(doc, ed, win)); + return; + } + + teardown(ed, win); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_alt_b_f.js b/devtools/client/shared/sourceeditor/test/browser_editor_alt_b_f.js new file mode 100644 index 0000000000..cacefd8dac --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_alt_b_f.js @@ -0,0 +1,46 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// Ensure Alt-B and Alt-F keyboard shortcuts work as expected in the source editor. +// See Bug 1481443. + +add_task(async function () { + const { ed, win } = await setup(); + const editorDoc = ed.container.contentDocument; + await promiseWaitForFocus(); + const isMacOS = Services.appinfo.OS === "Darwin"; + + ed.focus(); + + const initialText = "a b c d e"; + ed.setText(initialText); + + ed.setCursor({ line: 1, ch: initialText.length }); + + EventUtils.synthesizeKey("b", { altKey: true }, editorDoc.defaultView); + + // A character is added only on OSX. + let expectedText = isMacOS ? initialText + "b" : initialText; + is( + ed.getCursor().ch, + expectedText.length, + "Cursor is at expected position after Alt-B" + ); + is(ed.getText(), expectedText, "Editor has expected content after Alt-B"); + + EventUtils.synthesizeKey("f", { altKey: true }, editorDoc.defaultView); + + // A character is added only on OSX. + expectedText = isMacOS ? expectedText + "f" : initialText; + is( + ed.getCursor().ch, + expectedText.length, + "Cursor is at expected position after Alt-F" + ); + is(ed.getText(), expectedText, "Editor has expected content after Alt-F"); + + ed.destroy(); + win.close(); +}); diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_autocomplete_basic.js b/devtools/client/shared/sourceeditor/test/browser_editor_autocomplete_basic.js new file mode 100644 index 0000000000..c7dc9c8a97 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_autocomplete_basic.js @@ -0,0 +1,51 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const AUTOCOMPLETION_PREF = "devtools.editor.autocomplete"; + +// Test to make sure that different autocompletion modes can be created, +// switched, and destroyed. This doesn't test the actual autocompletion +// popups, only their integration with the editor. +async function test() { + waitForExplicitFinish(); + const { ed, win } = await setup(); + const edWin = ed.container.contentWindow.wrappedJSObject; + testJS(ed, edWin); + testCSS(ed, edWin); + testPref(ed, edWin); + teardown(ed, win); +} + +function testJS(ed, win) { + ok(!ed.getOption("autocomplete"), "Autocompletion is not set"); + + ed.setMode(Editor.modes.js); + ed.setOption("autocomplete", true); + + ok(ed.getOption("autocomplete"), "Autocompletion is set"); +} + +function testCSS(ed, win) { + ok(ed.getOption("autocomplete"), "Autocompletion is set"); + + ed.setMode(Editor.modes.css); + ed.setOption("autocomplete", true); + + ok(ed.getOption("autocomplete"), "Autocompletion is still set"); +} + +function testPref(ed, win) { + ed.setMode(Editor.modes.js); + ed.setOption("autocomplete", true); + + ok(ed.getOption("autocomplete"), "Autocompletion is set"); + + info("Preffing autocompletion off"); + Services.prefs.setBoolPref(AUTOCOMPLETION_PREF, false); + + ok(ed.getOption("autocomplete"), "Autocompletion is still set"); + + Services.prefs.clearUserPref(AUTOCOMPLETION_PREF); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_autocomplete_events.js b/devtools/client/shared/sourceeditor/test/browser_editor_autocomplete_events.js new file mode 100644 index 0000000000..6022c2c203 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_autocomplete_events.js @@ -0,0 +1,157 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const TEST_URI = + "data:text/html;charset=UTF-8,<html><body><bar></bar>" + + "<div id='baz'></div><body></html>"; + +add_task(async function () { + await addTab(TEST_URI); + await runTests(); +}); + +async function runTests() { + const target = await createAndAttachTargetForTab(gBrowser.selectedTab); + const inspector = await target.getFront("inspector"); + const walker = inspector.walker; + const { ed, win, edWin } = await setup({ + autocomplete: true, + mode: Editor.modes.css, + autocompleteOpts: { + walker, + cssProperties: getClientCssProperties(), + }, + }); + await testMouse(ed, edWin); + await testKeyboard(ed, edWin); + await testKeyboardCycle(ed, edWin); + await testKeyboardCycleForPrefixedString(ed, edWin); + await testKeyboardCSSComma(ed, edWin); + await testCloseOnEscape(ed, edWin); + teardown(ed, win); +} + +async function testKeyboard(ed, win) { + ed.focus(); + ed.setText("b"); + ed.setCursor({ line: 1, ch: 1 }); + + const popupOpened = ed.getAutocompletionPopup().once("popup-opened"); + + const autocompleteKey = Editor.keyFor("autocompletion", { + noaccel: true, + }).toUpperCase(); + EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win); + + info("Waiting for popup to be opened"); + await popupOpened; + + EventUtils.synthesizeKey("VK_RETURN", {}, win); + is(ed.getText(), "bar", "Editor text has been updated"); +} + +async function testKeyboardCycle(ed, win) { + ed.focus(); + ed.setText("b"); + ed.setCursor({ line: 1, ch: 1 }); + + const popupOpened = ed.getAutocompletionPopup().once("popup-opened"); + + const autocompleteKey = Editor.keyFor("autocompletion", { + noaccel: true, + }).toUpperCase(); + EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win); + + info("Waiting for popup to be opened"); + await popupOpened; + + EventUtils.synthesizeKey("VK_DOWN", {}, win); + is(ed.getText(), "bar", "Editor text has been updated"); + + EventUtils.synthesizeKey("VK_DOWN", {}, win); + is(ed.getText(), "body", "Editor text has been updated"); + + EventUtils.synthesizeKey("VK_DOWN", {}, win); + is(ed.getText(), "#baz", "Editor text has been updated"); +} + +async function testKeyboardCycleForPrefixedString(ed, win) { + ed.focus(); + ed.setText("#b"); + ed.setCursor({ line: 1, ch: 2 }); + + const popupOpened = ed.getAutocompletionPopup().once("popup-opened"); + + const autocompleteKey = Editor.keyFor("autocompletion", { + noaccel: true, + }).toUpperCase(); + EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win); + + info("Waiting for popup to be opened"); + await popupOpened; + + EventUtils.synthesizeKey("VK_DOWN", {}, win); + is(ed.getText(), "#baz", "Editor text has been updated"); +} + +async function testKeyboardCSSComma(ed, win) { + ed.focus(); + ed.setText("b"); + ed.setCursor({ line: 1, ch: 1 }); + + let isPopupOpened = false; + const popupOpened = ed.getAutocompletionPopup().once("popup-opened"); + popupOpened.then(() => { + isPopupOpened = true; + }); + + EventUtils.synthesizeKey(",", {}, win); + + await wait(500); + + ok(!isPopupOpened, "Autocompletion shouldn't be opened"); +} + +async function testMouse(ed, win) { + ed.focus(); + ed.setText("b"); + ed.setCursor({ line: 1, ch: 1 }); + + const popupOpened = ed.getAutocompletionPopup().once("popup-opened"); + + const autocompleteKey = Editor.keyFor("autocompletion", { + noaccel: true, + }).toUpperCase(); + EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win); + + info("Waiting for popup to be opened"); + await popupOpened; + ed.getAutocompletionPopup()._list.children[2].click(); + is(ed.getText(), "#baz", "Editor text has been updated"); +} + +async function testCloseOnEscape(ed, win) { + ed.focus(); + ed.setText("b"); + ed.setCursor({ line: 1, ch: 1 }); + + const popupOpened = ed.getAutocompletionPopup().once("popup-opened"); + + const autocompleteKey = Editor.keyFor("autocompletion", { + noaccel: true, + }).toUpperCase(); + EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win); + + info("Waiting for popup to be opened"); + await popupOpened; + + is(ed.getAutocompletionPopup().isOpen, true, "The popup is open"); + + const popupClosed = ed.getAutocompletionPopup().once("popup-closed"); + EventUtils.synthesizeKey("VK_ESCAPE", {}, win); + + await popupClosed; + is(ed.getAutocompletionPopup().isOpen, false, "Escape key closed popup"); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_basic.js b/devtools/client/shared/sourceeditor/test/browser_editor_basic.js new file mode 100644 index 0000000000..9373990bf7 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_basic.js @@ -0,0 +1,75 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +async function test() { + waitForExplicitFinish(); + const { ed, win } = await setup(); + // appendTo + const cmFrame = win.document.querySelector("iframe"); + const cmStyle = cmFrame.contentDocument.getElementById("cmBaseStyle"); + ok(~cmStyle.innerHTML.indexOf(".CodeMirror"), "correct iframe is there"); + + // getOption/setOption + ok(ed.getOption("styleActiveLine"), "getOption works"); + ed.setOption("styleActiveLine", false); + ok(!ed.getOption("styleActiveLine"), "setOption works"); + + // Language modes + is(ed.getMode(), Editor.modes.text, "getMode"); + ed.setMode(Editor.modes.js); + is(ed.getMode(), Editor.modes.js, "setMode"); + + // Content + is(ed.getText(), "Hello.", "getText"); + ed.setText("Hi.\nHow are you?"); + is(ed.getText(), "Hi.\nHow are you?", "setText"); + is(ed.getText(1), "How are you?", "getText(num)"); + is(ed.getText(5), "", "getText(num) when num is out of scope"); + + ed.replaceText("YOU", { line: 1, ch: 8 }, { line: 1, ch: 11 }); + is(ed.getText(1), "How are YOU?", "replaceText(str, from, to)"); + ed.replaceText("you?", { line: 1, ch: 8 }); + is(ed.getText(1), "How are you?", "replaceText(str, from)"); + ed.replaceText("Hello."); + is(ed.getText(), "Hello.", "replaceText(str)"); + + ed.insertText(", sir/madam", { line: 0, ch: 5 }); + is(ed.getText(), "Hello, sir/madam.", "insertText"); + + // Add-ons + ed.extend({ whoami: () => "Anton", whereami: () => "Mozilla" }); + is(ed.whoami(), "Anton", "extend/1"); + is(ed.whereami(), "Mozilla", "extend/2"); + + // Line classes + ed.setText("Hello!\nHow are you?"); + ok(!ed.hasLineClass(0, "test"), "no test line class"); + ed.addLineClass(0, "test"); + ok(ed.hasLineClass(0, "test"), "test line class is there"); + ed.removeLineClass(0, "test"); + ok(!ed.hasLineClass(0, "test"), "test line class is gone"); + + // Font size + const size = ed.getFontSize(); + is("number", typeof size, "we have the default font size"); + ed.setFontSize(ed.getFontSize() + 1); + is(ed.getFontSize(), size + 1, "new font size was set"); + + info("Check that we display unicode values for non-printable characters"); + ed.setText("> \u202e \u2066 - \u2069 \u2066 <"); + + const doc = win.document.querySelector("iframe").contentWindow.document; + const nonPrintableCharElements = Array.from( + doc.querySelectorAll(".cm-non-printable-char") + ); + + Assert.deepEqual( + nonPrintableCharElements.map(el => el.textContent), + ["\\u202e", "\\u2066", "\\u2069", "\\u2066"], + "non printable chars are displayed as expected" + ); + + teardown(ed, win); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_cursor.js b/devtools/client/shared/sourceeditor/test/browser_editor_cursor.js new file mode 100644 index 0000000000..c4e71424d5 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_cursor.js @@ -0,0 +1,52 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +async function test() { + waitForExplicitFinish(); + const { ed, win } = await setup(); + ch(ed.getCursor(), { line: 0, ch: 0 }, "default cursor position is ok"); + ed.setText("Hello.\nHow are you?"); + + ed.setCursor({ line: 1, ch: 5 }); + ch(ed.getCursor(), { line: 1, ch: 5 }, "setCursor({ line, ch })"); + + ch(ed.getPosition(7), { line: 1, ch: 0 }, "getPosition(num)"); + ch(ed.getPosition(7, 1)[0], { line: 1, ch: 0 }, "getPosition(num, num)[0]"); + ch(ed.getPosition(7, 1)[1], { line: 0, ch: 1 }, "getPosition(num, num)[1]"); + + ch(ed.getOffset({ line: 1, ch: 0 }), 7, "getOffset(num)"); + ch( + ed.getOffset({ line: 1, ch: 0 }, { line: 0, ch: 1 })[0], + 7, + "getOffset(num, num)[0]" + ); + ch( + ed.getOffset({ line: 1, ch: 0 }, { line: 0, ch: 1 })[0], + 2, + "getOffset(num, num)[1]" + ); + + is(ed.getSelection(), "", "nothing is selected"); + ed.setSelection({ line: 0, ch: 0 }, { line: 0, ch: 5 }); + is(ed.getSelection(), "Hello", "setSelection"); + + ed.dropSelection(); + is(ed.getSelection(), "", "dropSelection"); + + // Check that shift-click on a gutter selects the whole line (bug 919707) + const iframe = win.document.querySelector("iframe"); + const gutter = iframe.contentWindow.document.querySelector( + ".CodeMirror-gutters" + ); + + EventUtils.sendMouseEvent( + { type: "mousedown", shiftKey: true }, + gutter, + iframe.contentWindow + ); + is(ed.getSelection(), "", "shift-click"); + + teardown(ed, win); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_cursor_blink.js b/devtools/client/shared/sourceeditor/test/browser_editor_cursor_blink.js new file mode 100644 index 0000000000..5005afc0fd --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_cursor_blink.js @@ -0,0 +1,73 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// Test to make sure that the editor reacts to preference changes + +const CARET_BLINK_TIME = "ui.caretBlinkTime"; + +add_task(async function () { + Services.prefs.clearUserPref(CARET_BLINK_TIME); + + info(`Test when "${CARET_BLINK_TIME}" isn't set`); + let { ed, win } = await setup(); + checkCssCustomPropertyValue( + ed, + 530, + "When preference isn't set, blink time is set to codeMirror default" + ); + ed.destroy(); + win.close(); + + info(`Test with a positive value for "${CARET_BLINK_TIME}"`); + let blinkTime = 200; + Services.prefs.setIntPref(CARET_BLINK_TIME, blinkTime); + ({ ed, win } = await setup()); + + checkCssCustomPropertyValue( + ed, + blinkTime, + "When preference is set, blink time reflects the pref value" + ); + ed.destroy(); + win.close(); + + info(`Test when "${CARET_BLINK_TIME}" is 0`); + blinkTime = 0; + Services.prefs.setIntPref(CARET_BLINK_TIME, blinkTime); + ({ ed, win } = await setup()); + + checkCssCustomPropertyValue( + ed, + blinkTime, + "When preference value is 0, blink time is also 0" + ); + ed.destroy(); + win.close(); + + info(`Test when "${CARET_BLINK_TIME}" is -1`); + blinkTime = -1; + Services.prefs.setIntPref(CARET_BLINK_TIME, blinkTime); + ({ ed, win } = await setup()); + + checkCssCustomPropertyValue( + ed, + 0, + "When preference value is negative, blink time is 0" + ); + ed.destroy(); + win.close(); + + Services.prefs.clearUserPref(CARET_BLINK_TIME); +}); + +function checkCssCustomPropertyValue(editor, expectedMsValue, assertionText) { + is( + editor.codeMirror + .getWrapperElement() + .style.getPropertyValue("--caret-blink-time"), + `${expectedMsValue}ms`, + assertionText + ); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_disableSearchAddon.js b/devtools/client/shared/sourceeditor/test/browser_editor_disableSearchAddon.js new file mode 100644 index 0000000000..87cf7563d9 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_disableSearchAddon.js @@ -0,0 +1,39 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// Ensure disableSearchAddon config works as expected in the source editor. + +const isMacOS = Services.appinfo.OS === "Darwin"; +const { LocalizationHelper } = require("resource://devtools/shared/l10n.js"); +const L10N = new LocalizationHelper( + "devtools/client/locales/sourceeditor.properties" +); + +const FIND_KEY = L10N.getStr("find.key"); +const REPLACE_KEY = L10N.getStr( + isMacOS ? "replaceAllMac.key" : "replaceAll.key" +); + +add_task(async function () { + const { ed, win } = await setup({ + disableSearchAddon: true, + }); + + const edDoc = ed.container.contentDocument; + const edWin = edDoc.defaultView; + + await promiseWaitForFocus(); + ed.focus(); + + synthesizeKeyShortcut(FIND_KEY, edWin); + const searchInput = edDoc.querySelector("input[type=search]"); + ok(!searchInput, "the search input is not displayed"); + + synthesizeKeyShortcut(REPLACE_KEY, edWin); + const replaceInput = edDoc.querySelector(".CodeMirror-dialog > input"); + ok(!replaceInput, "the replace input is not displayed"); + + teardown(ed, win); +}); diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_find_again.js b/devtools/client/shared/sourceeditor/test/browser_editor_find_again.js new file mode 100644 index 0000000000..7eadbd74e8 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_find_again.js @@ -0,0 +1,218 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { LocalizationHelper } = require("resource://devtools/shared/l10n.js"); +const L10N = new LocalizationHelper( + "devtools/client/locales/sourceeditor.properties" +); + +const { OS } = Services.appinfo; + +// On linux, getting immediately the selection's range here fails, returning +const FIND_KEY = L10N.getStr("find.key"); +const FINDNEXT_KEY = L10N.getStr("findNext.key"); +const FINDPREV_KEY = L10N.getStr("findPrev.key"); +// the replace's key with the appropriate modifiers based on OS +const REPLACE_KEY = + OS == "Darwin" + ? L10N.getStr("replaceAllMac.key") + : L10N.getStr("replaceAll.key"); + +// values like it's not selected – even if the selection is visible. +// For the record, setting the selection's range immediately doesn't have +// any effect. +// It's like the <input> is not ready yet. +// Therefore, we trigger the UI focus event to the <input>, waiting for the +// response. +// Using a timeout could also work, but that is more precise, ensuring also +// the execution of the listeners added to the <input>'s focus. +const dispatchAndWaitForFocus = target => + new Promise(resolve => { + target.addEventListener( + "focus", + function () { + resolve(target); + }, + { once: true } + ); + + target.dispatchEvent(new UIEvent("focus")); + }); + +function openSearchBox(ed) { + const edDoc = ed.container.contentDocument; + const edWin = edDoc.defaultView; + + let input = edDoc.querySelector("input[type=search]"); + ok(!input, "search box closed"); + + // The editor needs the focus to properly receive the `synthesizeKey` + ed.focus(); + + synthesizeKeyShortcut(FINDNEXT_KEY, edWin); + input = edDoc.querySelector("input[type=search]"); + ok(input, "find again command key opens the search box"); +} + +function testFindAgain(ed, inputLine, expectCursor, isFindPrev = false) { + const edDoc = ed.container.contentDocument; + const edWin = edDoc.defaultView; + + const input = edDoc.querySelector("input[type=search]"); + input.value = inputLine; + + // Ensure the input has the focus before send the key – necessary on Linux, + // it seems that during the tests can be lost + input.focus(); + + if (isFindPrev) { + synthesizeKeyShortcut(FINDPREV_KEY, edWin); + } else { + synthesizeKeyShortcut(FINDNEXT_KEY, edWin); + } + + ch( + ed.getCursor(), + expectCursor, + "find: " + inputLine + " expects cursor: " + expectCursor.toSource() + ); +} + +const testSearchBoxTextIsSelected = async function (ed) { + const edDoc = ed.container.contentDocument; + const edWin = edDoc.defaultView; + + let input = edDoc.querySelector("input[type=search]"); + ok(input, "search box is opened"); + + // Ensure the input has the focus before send the key – necessary on Linux, + // it seems that during the tests can be lost + input.focus(); + + // Close search box + EventUtils.synthesizeKey("VK_ESCAPE", {}, edWin); + + input = edDoc.querySelector("input[type=search]"); + ok(!input, "search box is closed"); + + // Re-open the search box + synthesizeKeyShortcut(FIND_KEY, edWin); + + input = edDoc.querySelector("input[type=search]"); + ok(input, "find command key opens the search box"); + + await dispatchAndWaitForFocus(input); + + let { selectionStart, selectionEnd, value } = input; + + ok( + selectionStart === 0 && selectionEnd === value.length, + "search box's text is selected when re-opened" + ); + + // Removing selection + input.setSelectionRange(0, 0); + + synthesizeKeyShortcut(FIND_KEY, edWin); + + ({ selectionStart, selectionEnd } = input); + + ok( + selectionStart === 0 && selectionEnd === value.length, + "search box's text is selected when find key is pressed" + ); + + // Close search box + EventUtils.synthesizeKey("VK_ESCAPE", {}, edWin); +}; + +const testReplaceBoxTextIsSelected = async function (ed) { + const edDoc = ed.container.contentDocument; + const edWin = edDoc.defaultView; + + let input = edDoc.querySelector(".CodeMirror-dialog > input"); + ok(!input, "dialog box with replace is closed"); + + // The editor needs the focus to properly receive the `synthesizeKey` + ed.focus(); + + synthesizeKeyShortcut(REPLACE_KEY, edWin); + + input = edDoc.querySelector(".CodeMirror-dialog > input"); + ok(input, "dialog box with replace is opened"); + + input.value = "line 5"; + + // Ensure the input has the focus before send the key – necessary on Linux, + // it seems that during the tests can be lost + input.focus(); + + await dispatchAndWaitForFocus(input); + + let { selectionStart, selectionEnd, value } = input; + + ok( + !(selectionStart === 0 && selectionEnd === value.length), + "Text in dialog box is not selected" + ); + + synthesizeKeyShortcut(REPLACE_KEY, edWin); + + ({ selectionStart, selectionEnd } = input); + + ok( + selectionStart === 0 && selectionEnd === value.length, + "dialog box's text is selected when replace key is pressed" + ); + + // Close dialog box + EventUtils.synthesizeKey("VK_ESCAPE", {}, edWin); +}; + +add_task(async function () { + const { ed, win } = await setup(); + + ed.setText( + [ + "// line 1", + "// line 2", + "// line 3", + "// line 4", + "// line 5", + ].join("\n") + ); + + await promiseWaitForFocus(); + + openSearchBox(ed); + + const testVectors = [ + // Starting here expect data needs to get updated for length changes to + // "textLines" above. + ["line", { line: 0, ch: 7 }], + ["line", { line: 1, ch: 8 }], + ["line", { line: 2, ch: 9 }], + ["line", { line: 3, ch: 10 }], + ["line", { line: 4, ch: 11 }], + ["ne 3", { line: 2, ch: 11 }], + ["line 1", { line: 0, ch: 9 }], + // Testing find prev + ["line", { line: 4, ch: 11 }, true], + ["line", { line: 3, ch: 10 }, true], + ["line", { line: 2, ch: 9 }, true], + ["line", { line: 1, ch: 8 }, true], + ["line", { line: 0, ch: 7 }, true], + ]; + + for (const v of testVectors) { + await testFindAgain(ed, ...v); + } + + await testSearchBoxTextIsSelected(ed); + + await testReplaceBoxTextIsSelected(ed); + + teardown(ed, win); +}); diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_goto_line.js b/devtools/client/shared/sourceeditor/test/browser_editor_goto_line.js new file mode 100644 index 0000000000..325c8c6dd5 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_goto_line.js @@ -0,0 +1,91 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +function testJumpToLine(ed, inputLine, expectCursor) { + ed.jumpToLine(); + const editorDoc = ed.container.contentDocument; + const lineInput = editorDoc.querySelector("input"); + lineInput.value = inputLine; + EventUtils.synthesizeKey("VK_RETURN", {}, editorDoc.defaultView); + // CodeMirror lines and columns are 0-based. + ch( + ed.getCursor(), + expectCursor, + "jumpToLine " + inputLine + " expects cursor " + expectCursor.toSource() + ); +} + +async function test() { + waitForExplicitFinish(); + const { ed, win } = await setup(); + const textLines = [ + "// line 1", + "// line 2", + "// line 3", + "// line 4", + "// line 5", + "", + ]; + ed.setText(textLines.join("\n")); + await promiseWaitForFocus(); + + const testVectors = [ + // Various useless inputs go to line 0, column 0 or do nothing. + ["", { line: 0, ch: 0 }], + [":", { line: 0, ch: 0 }], + [" ", { line: 0, ch: 0 }], + [" : ", { line: 0, ch: 0 }], + ["a:b", { line: 0, ch: 0 }], + ["LINE: COLUMN ", { line: 0, ch: 0 }], + ["-1", { line: 0, ch: 0 }], + [":-1", { line: 0, ch: 0 }], + ["-1:-1", { line: 0, ch: 0 }], + ["0", { line: 0, ch: 0 }], + [":0", { line: 0, ch: 0 }], + ["0:0", { line: 0, ch: 0 }], + // Starting here expect data needs to get updated for length changes to + // "textLines" above. + // Just jump to line + ["1", { line: 0, ch: 0 }], + // Jump to second character in line + ["1:2", { line: 0, ch: 1 }], + // Jump to last character on line + ["1:9", { line: 0, ch: 8 }], + // Jump just after last character on line (end of line) + ["1:10", { line: 0, ch: 9 }], + // Jump one character past end of line (gets clamped to end of line) + ["1:11", { line: 0, ch: 9 }], + ["2", { line: 1, ch: 0 }], + ["2:2", { line: 1, ch: 1 }], + ["2:10", { line: 1, ch: 9 }], + ["2:11", { line: 1, ch: 10 }], + ["2:12", { line: 1, ch: 10 }], + ["3", { line: 2, ch: 0 }], + ["3:2", { line: 2, ch: 1 }], + ["3:11", { line: 2, ch: 10 }], + ["3:12", { line: 2, ch: 11 }], + ["3:13", { line: 2, ch: 11 }], + ["4", { line: 3, ch: 0 }], + ["4:2", { line: 3, ch: 1 }], + ["4:12", { line: 3, ch: 11 }], + ["4:13", { line: 3, ch: 12 }], + ["4:14", { line: 3, ch: 12 }], + ["5", { line: 4, ch: 0 }], + ["5:2", { line: 4, ch: 1 }], + ["5:13", { line: 4, ch: 12 }], + ["5:14", { line: 4, ch: 13 }], + ["5:15", { line: 4, ch: 13 }], + // One line beyond last newline in editor text: + ["6", { line: 5, ch: 0 }], + ["6:2", { line: 5, ch: 0 }], + // Two line beyond last newline in editor text (gets clamped): + ["7", { line: 5, ch: 0 }], + ["7:2", { line: 5, ch: 0 }], + ]; + testVectors.forEach(vector => { + testJumpToLine(ed, vector[0], vector[1]); + }); + teardown(ed, win); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_history.js b/devtools/client/shared/sourceeditor/test/browser_editor_history.js new file mode 100644 index 0000000000..2602c28236 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_history.js @@ -0,0 +1,30 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +async function test() { + waitForExplicitFinish(); + const { ed, win } = await setup(); + ok(ed.isClean(), "default isClean"); + ok(!ed.canUndo(), "default canUndo"); + ok(!ed.canRedo(), "default canRedo"); + + ed.setText("Hello, World!"); + ok(!ed.isClean(), "isClean"); + ok(ed.canUndo(), "canUndo"); + ok(!ed.canRedo(), "canRedo"); + + ed.undo(); + ok(ed.isClean(), "isClean after undo"); + ok(!ed.canUndo(), "canUndo after undo"); + ok(ed.canRedo(), "canRedo after undo"); + + ed.setText("What's up?"); + ed.setClean(); + ok(ed.isClean(), "isClean after setClean"); + ok(ed.canUndo(), "canUndo after setClean"); + ok(!ed.canRedo(), "canRedo after setClean"); + + teardown(ed, win); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_markers.js b/devtools/client/shared/sourceeditor/test/browser_editor_markers.js new file mode 100644 index 0000000000..e56ea3a425 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_markers.js @@ -0,0 +1,43 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +async function test() { + waitForExplicitFinish(); + const { ed, win } = await setup(); + ok(!ed.hasMarker(0, "breakpoints", "test"), "default is ok"); + ed.addMarker(0, "breakpoints", "test"); + ed.addMarker(0, "breakpoints", "test2"); + ok(ed.hasMarker(0, "breakpoints", "test"), "addMarker/1"); + ok(ed.hasMarker(0, "breakpoints", "test2"), "addMarker/2"); + + ed.removeMarker(0, "breakpoints", "test"); + ok(!ed.hasMarker(0, "breakpoints", "test"), "removeMarker/1"); + ok(ed.hasMarker(0, "breakpoints", "test2"), "removeMarker/2"); + + ed.removeAllMarkers("breakpoints"); + ok(!ed.hasMarker(0, "breakpoints", "test"), "removeAllMarkers/1"); + ok(!ed.hasMarker(0, "breakpoints", "test2"), "removeAllMarkers/2"); + + ed.addMarker(0, "breakpoints", "breakpoint"); + ed.setMarkerListeners( + 0, + "breakpoints", + "breakpoint", + { + click: (line, marker, param) => { + is(line, 0, "line is ok"); + is(marker.className, "breakpoint", "marker is ok"); + ok(param, "click is ok"); + + teardown(ed, win); + }, + }, + [true] + ); + + const env = win.document.querySelector("iframe").contentWindow; + const div = env.document.querySelector("div.breakpoint"); + div.click(); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_movelines.js b/devtools/client/shared/sourceeditor/test/browser_editor_movelines.js new file mode 100644 index 0000000000..b95139b5a9 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_movelines.js @@ -0,0 +1,61 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +async function test() { + waitForExplicitFinish(); + const { ed, win } = await setup(); + const simpleProg = + "function foo() {\n let i = 1;\n let j = 2;\n " + "return bar;\n}"; + ed.setText(simpleProg); + + // Move first line up + ed.setCursor({ line: 0, ch: 0 }); + ed.moveLineUp(); + is(ed.getText(0), "function foo() {", "getText(num)"); + ch(ed.getCursor(), { line: 0, ch: 0 }, "getCursor"); + + // Move last line down + ed.setCursor({ line: 4, ch: 0 }); + ed.moveLineDown(); + is(ed.getText(4), "}", "getText(num)"); + ch(ed.getCursor(), { line: 4, ch: 0 }, "getCursor"); + + // Move line 2 up + ed.setCursor({ line: 1, ch: 5 }); + ed.moveLineUp(); + is(ed.getText(0), " let i = 1;", "getText(num)"); + is(ed.getText(1), "function foo() {", "getText(num)"); + ch(ed.getCursor(), { line: 0, ch: 5 }, "getCursor"); + + // Undo previous move by moving line 1 down + ed.moveLineDown(); + is(ed.getText(0), "function foo() {", "getText(num)"); + is(ed.getText(1), " let i = 1;", "getText(num)"); + ch(ed.getCursor(), { line: 1, ch: 5 }, "getCursor"); + + // Move line 2 and 3 up + ed.setSelection({ line: 1, ch: 0 }, { line: 2, ch: 0 }); + ed.moveLineUp(); + is(ed.getText(0), " let i = 1;", "getText(num)"); + is(ed.getText(1), " let j = 2;", "getText(num)"); + is(ed.getText(2), "function foo() {", "getText(num)"); + ch(ed.getCursor("start"), { line: 0, ch: 0 }, "getCursor(string)"); + ch(ed.getCursor("end"), { line: 1, ch: 0 }, "getCursor(string)"); + + // Move line 1 to 3 down twice + ed.dropSelection(); + ed.setSelection({ line: 0, ch: 7 }, { line: 2, ch: 5 }); + ed.moveLineDown(); + ed.moveLineDown(); + is(ed.getText(0), " return bar;", "getText(num)"); + is(ed.getText(1), "}", "getText(num)"); + is(ed.getText(2), " let i = 1;", "getText(num)"); + is(ed.getText(3), " let j = 2;", "getText(num)"); + is(ed.getText(4), "function foo() {", "getText(num)"); + ch(ed.getCursor("start"), { line: 2, ch: 7 }, "getCursor(string)"); + ch(ed.getCursor("end"), { line: 4, ch: 5 }, "getCursor(string)"); + + teardown(ed, win); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_editor_prefs.js b/devtools/client/shared/sourceeditor/test/browser_editor_prefs.js new file mode 100644 index 0000000000..2063796f76 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_editor_prefs.js @@ -0,0 +1,139 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// Test to make sure that the editor reacts to preference changes + +const TAB_SIZE = "devtools.editor.tabsize"; +const ENABLE_CODE_FOLDING = "devtools.editor.enableCodeFolding"; +const EXPAND_TAB = "devtools.editor.expandtab"; +const KEYMAP = "devtools.editor.keymap"; +const AUTO_CLOSE = "devtools.editor.autoclosebrackets"; +const AUTOCOMPLETE = "devtools.editor.autocomplete"; +const DETECT_INDENT = "devtools.editor.detectindentation"; + +async function test() { + waitForExplicitFinish(); + const { ed, win } = await setup(); + Assert.deepEqual( + ed.getOption("gutters"), + ["CodeMirror-linenumbers", "breakpoints", "CodeMirror-foldgutter"], + "gutters is correct" + ); + + ed.setText("Checking preferences."); + + info("Turning prefs off"); + + ed.setOption("autocomplete", true); + + Services.prefs.setIntPref(TAB_SIZE, 2); + Services.prefs.setBoolPref(ENABLE_CODE_FOLDING, false); + Services.prefs.setBoolPref(EXPAND_TAB, false); + Services.prefs.setCharPref(KEYMAP, "default"); + Services.prefs.setBoolPref(AUTO_CLOSE, false); + Services.prefs.setBoolPref(AUTOCOMPLETE, false); + Services.prefs.setBoolPref(DETECT_INDENT, false); + + Assert.deepEqual( + ed.getOption("gutters"), + ["CodeMirror-linenumbers", "breakpoints"], + "gutters is correct" + ); + + is(ed.getOption("tabSize"), 2, "tabSize is correct"); + is(ed.getOption("indentUnit"), 2, "indentUnit is correct"); + is(ed.getOption("foldGutter"), false, "foldGutter is correct"); + is( + ed.getOption("enableCodeFolding"), + undefined, + "enableCodeFolding is correct" + ); + is(ed.getOption("indentWithTabs"), true, "indentWithTabs is correct"); + is(ed.getOption("keyMap"), "default", "keyMap is correct"); + is(ed.getOption("autoCloseBrackets"), false, "autoCloseBrackets is correct"); + is(ed.getOption("autocomplete"), true, "autocomplete is correct"); + ok(!ed.isAutocompletionEnabled(), "Autocompletion is not enabled"); + + info("Turning prefs on"); + + Services.prefs.setIntPref(TAB_SIZE, 4); + Services.prefs.setBoolPref(ENABLE_CODE_FOLDING, true); + Services.prefs.setBoolPref(EXPAND_TAB, true); + Services.prefs.setCharPref(KEYMAP, "sublime"); + Services.prefs.setBoolPref(AUTO_CLOSE, true); + Services.prefs.setBoolPref(AUTOCOMPLETE, true); + + Assert.deepEqual( + ed.getOption("gutters"), + ["CodeMirror-linenumbers", "breakpoints", "CodeMirror-foldgutter"], + "gutters is correct" + ); + + is(ed.getOption("tabSize"), 4, "tabSize is correct"); + is(ed.getOption("indentUnit"), 4, "indentUnit is correct"); + is(ed.getOption("foldGutter"), true, "foldGutter is correct"); + is( + ed.getOption("enableCodeFolding"), + undefined, + "enableCodeFolding is correct" + ); + is(ed.getOption("indentWithTabs"), false, "indentWithTabs is correct"); + is( + ed.getOption("autoCloseBrackets"), + "()[]{}''\"\"``", + "autoCloseBrackets is correct" + ); + is(ed.getOption("autocomplete"), true, "autocomplete is correct"); + ok(ed.isAutocompletionEnabled(), "Autocompletion is enabled"); + + // Since the keyMap files are lazily loaded, this can take some time. We need to wait + // until the option has the expected value. + info("Wait for the keyMap option to be updated"); + await waitUntil(() => ed.getOption("keyMap") === "sublime"); + is(ed.getOption("keyMap"), "sublime", "keyMap is correct"); + + info("Forcing foldGutter off using enableCodeFolding"); + ed.setOption("enableCodeFolding", false); + + is(ed.getOption("foldGutter"), false, "foldGutter is correct"); + is(ed.getOption("enableCodeFolding"), false, "enableCodeFolding is correct"); + Assert.deepEqual( + ed.getOption("gutters"), + ["CodeMirror-linenumbers", "breakpoints"], + "gutters is correct" + ); + + info("Forcing foldGutter on using enableCodeFolding"); + ed.setOption("enableCodeFolding", true); + + is(ed.getOption("foldGutter"), true, "foldGutter is correct"); + is(ed.getOption("enableCodeFolding"), true, "enableCodeFolding is correct"); + Assert.deepEqual( + ed.getOption("gutters"), + ["CodeMirror-linenumbers", "breakpoints", "CodeMirror-foldgutter"], + "gutters is correct" + ); + + info("Checking indentation detection"); + + Services.prefs.setBoolPref(DETECT_INDENT, true); + + ed.setText("Detecting\n\tTabs"); + is(ed.getOption("indentWithTabs"), true, "indentWithTabs is correct"); + is(ed.getOption("indentUnit"), 4, "indentUnit is correct"); + + ed.setText("body {\n color:red;\n a:b;\n}"); + is(ed.getOption("indentWithTabs"), false, "indentWithTabs is correct"); + is(ed.getOption("indentUnit"), 2, "indentUnit is correct"); + + Services.prefs.clearUserPref(TAB_SIZE); + Services.prefs.clearUserPref(EXPAND_TAB); + Services.prefs.clearUserPref(KEYMAP); + Services.prefs.clearUserPref(AUTO_CLOSE); + Services.prefs.clearUserPref(AUTOCOMPLETE); + Services.prefs.clearUserPref(DETECT_INDENT); + + teardown(ed, win); +} diff --git a/devtools/client/shared/sourceeditor/test/browser_vimemacs.js b/devtools/client/shared/sourceeditor/test/browser_vimemacs.js new file mode 100644 index 0000000000..cda5899b70 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/browser_vimemacs.js @@ -0,0 +1,13 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const URI = + "chrome://mochitests/content/browser/devtools/client" + + "/shared/sourceeditor/test/codemirror/vimemacs.html"; + +add_task(async function test() { + requestLongerTimeout(4); + await runCodeMirrorTest(URI); +}); diff --git a/devtools/client/shared/sourceeditor/test/cm_mode_ruby.js b/devtools/client/shared/sourceeditor/test/cm_mode_ruby.js new file mode 100644 index 0000000000..991dede14a --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/cm_mode_ruby.js @@ -0,0 +1,285 @@ +// CodeMirror, copyright (c) by Marijn Haverbeke and others +// Distributed under an MIT license: http://codemirror.net/LICENSE + +(function (mod) { + if (typeof exports == "object" && typeof module == "object") // CommonJS + mod(require("resource://devtools/client/shared/lib/codemirror.js")); + else if (typeof define == "function" && define.amd) // AMD + define(["../../lib/codemirror"], mod); + else // Plain browser env + mod(CodeMirror); +})(function (CodeMirror) { + "use strict"; + + CodeMirror.defineMode("ruby", function (config) { + function wordObj(words) { + var o = {}; + for (var i = 0, e = words.length; i < e; ++i) o[words[i]] = true; + return o; + } + var keywords = wordObj([ + "alias", "and", "BEGIN", "begin", "break", "case", "class", "def", "defined?", "do", "else", + "elsif", "END", "end", "ensure", "false", "for", "if", "in", "module", "next", "not", "or", + "redo", "rescue", "retry", "return", "self", "super", "then", "true", "undef", "unless", + "until", "when", "while", "yield", "nil", "raise", "throw", "catch", "fail", "loop", "callcc", + "caller", "lambda", "proc", "public", "protected", "private", "require", "load", + "require_relative", "extend", "autoload", "__END__", "__FILE__", "__LINE__", "__dir__" + ]); + var indentWords = wordObj(["def", "class", "case", "for", "while", "module", "then", + "catch", "loop", "proc", "begin"]); + var dedentWords = wordObj(["end", "until"]); + var matching = {"[": "]", "{": "}", "(": ")"}; + var curPunc; + + function chain(newtok, stream, state) { + state.tokenize.push(newtok); + return newtok(stream, state); + } + + function tokenBase(stream, state) { + curPunc = null; + if (stream.sol() && stream.match("=begin") && stream.eol()) { + state.tokenize.push(readBlockComment); + return "comment"; + } + if (stream.eatSpace()) return null; + var ch = stream.next(), m; + if (ch == "`" || ch == "'" || ch == '"') { + return chain(readQuoted(ch, "string", ch == '"' || ch == "`"), stream, state); + } else if (ch == "/") { + var currentIndex = stream.current().length; + if (stream.skipTo("/")) { + var search_till = stream.current().length; + stream.backUp(stream.current().length - currentIndex); + var balance = 0; // balance brackets + while (stream.current().length < search_till) { + var chchr = stream.next(); + if (chchr == "(") balance += 1; + else if (chchr == ")") balance -= 1; + if (balance < 0) break; + } + stream.backUp(stream.current().length - currentIndex); + if (balance == 0) + return chain(readQuoted(ch, "string-2", true), stream, state); + } + return "operator"; + } else if (ch == "%") { + var style = "string", embed = true; + if (stream.eat("s")) style = "atom"; + else if (stream.eat(/[WQ]/)) style = "string"; + else if (stream.eat(/[r]/)) style = "string-2"; + else if (stream.eat(/[wxq]/)) { style = "string"; embed = false; } + var delim = stream.eat(/[^\w\s=]/); + if (!delim) return "operator"; + if (matching.propertyIsEnumerable(delim)) delim = matching[delim]; + return chain(readQuoted(delim, style, embed, true), stream, state); + } else if (ch == "#") { + stream.skipToEnd(); + return "comment"; + } else if (ch == "<" && (m = stream.match(/^<-?[\`\"\']?([a-zA-Z_?]\w*)[\`\"\']?(?:;|$)/))) { + return chain(readHereDoc(m[1]), stream, state); + } else if (ch == "0") { + if (stream.eat("x")) stream.eatWhile(/[\da-fA-F]/); + else if (stream.eat("b")) stream.eatWhile(/[01]/); + else stream.eatWhile(/[0-7]/); + return "number"; + } else if (/\d/.test(ch)) { + stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?/); + return "number"; + } else if (ch == "?") { + while (stream.match(/^\\[CM]-/)) {} + if (stream.eat("\\")) stream.eatWhile(/\w/); + else stream.next(); + return "string"; + } else if (ch == ":") { + if (stream.eat("'")) return chain(readQuoted("'", "atom", false), stream, state); + if (stream.eat('"')) return chain(readQuoted('"', "atom", true), stream, state); + + // :> :>> :< :<< are valid symbols + if (stream.eat(/[\<\>]/)) { + stream.eat(/[\<\>]/); + return "atom"; + } + + // :+ :- :/ :* :| :& :! are valid symbols + if (stream.eat(/[\+\-\*\/\&\|\:\!]/)) { + return "atom"; + } + + // Symbols can't start by a digit + if (stream.eat(/[a-zA-Z$@_\xa1-\uffff]/)) { + stream.eatWhile(/[\w$\xa1-\uffff]/); + // Only one ? ! = is allowed and only as the last character + stream.eat(/[\?\!\=]/); + return "atom"; + } + return "operator"; + } else if (ch == "@" && stream.match(/^@?[a-zA-Z_\xa1-\uffff]/)) { + stream.eat("@"); + stream.eatWhile(/[\w\xa1-\uffff]/); + return "variable-2"; + } else if (ch == "$") { + if (stream.eat(/[a-zA-Z_]/)) { + stream.eatWhile(/[\w]/); + } else if (stream.eat(/\d/)) { + stream.eat(/\d/); + } else { + stream.next(); // Must be a special global like $: or $! + } + return "variable-3"; + } else if (/[a-zA-Z_\xa1-\uffff]/.test(ch)) { + stream.eatWhile(/[\w\xa1-\uffff]/); + stream.eat(/[\?\!]/); + if (stream.eat(":")) return "atom"; + return "ident"; + } else if (ch == "|" && (state.varList || state.lastTok == "{" || state.lastTok == "do")) { + curPunc = "|"; + return null; + } else if (/[\(\)\[\]{}\\;]/.test(ch)) { + curPunc = ch; + return null; + } else if (ch == "-" && stream.eat(">")) { + return "arrow"; + } else if (/[=+\-\/*:\.^%<>~|]/.test(ch)) { + var more = stream.eatWhile(/[=+\-\/*:\.^%<>~|]/); + if (ch == "." && !more) curPunc = "."; + return "operator"; + } else { + return null; + } + } + + function tokenBaseUntilBrace(depth) { + if (!depth) depth = 1; + return function (stream, state) { + if (stream.peek() == "}") { + if (depth == 1) { + state.tokenize.pop(); + return state.tokenize[state.tokenize.length - 1](stream, state); + } else { + state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth - 1); + } + } else if (stream.peek() == "{") { + state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth + 1); + } + return tokenBase(stream, state); + }; + } + function tokenBaseOnce() { + var alreadyCalled = false; + return function (stream, state) { + if (alreadyCalled) { + state.tokenize.pop(); + return state.tokenize[state.tokenize.length - 1](stream, state); + } + alreadyCalled = true; + return tokenBase(stream, state); + }; + } + function readQuoted(quote, style, embed, unescaped) { + return function (stream, state) { + var escaped = false, ch; + + if (state.context.type === "read-quoted-paused") { + state.context = state.context.prev; + stream.eat("}"); + } + + while ((ch = stream.next()) != null) { + if (ch == quote && (unescaped || !escaped)) { + state.tokenize.pop(); + break; + } + if (embed && ch == "#" && !escaped) { + if (stream.eat("{")) { + if (quote == "}") { + state.context = {prev: state.context, type: "read-quoted-paused"}; + } + state.tokenize.push(tokenBaseUntilBrace()); + break; + } else if (/[@\$]/.test(stream.peek())) { + state.tokenize.push(tokenBaseOnce()); + break; + } + } + escaped = !escaped && ch == "\\"; + } + return style; + }; + } + function readHereDoc(phrase) { + return function (stream, state) { + if (stream.match(phrase)) state.tokenize.pop(); + else stream.skipToEnd(); + return "string"; + }; + } + function readBlockComment(stream, state) { + if (stream.sol() && stream.match("=end") && stream.eol()) + state.tokenize.pop(); + stream.skipToEnd(); + return "comment"; + } + + return { + startState: function () { + return {tokenize: [tokenBase], + indented: 0, + context: {type: "top", indented: -config.indentUnit}, + continuedLine: false, + lastTok: null, + varList: false}; + }, + + token: function (stream, state) { + if (stream.sol()) state.indented = stream.indentation(); + var style = state.tokenize[state.tokenize.length - 1](stream, state), kwtype; + var thisTok = curPunc; + if (style == "ident") { + var word = stream.current(); + style = state.lastTok == "." ? "property" + : keywords.propertyIsEnumerable(stream.current()) ? "keyword" + : /^[A-Z]/.test(word) ? "tag" + : (state.lastTok == "def" || state.lastTok == "class" || state.varList) ? "def" + : "variable"; + if (style == "keyword") { + thisTok = word; + if (indentWords.propertyIsEnumerable(word)) kwtype = "indent"; + else if (dedentWords.propertyIsEnumerable(word)) kwtype = "dedent"; + else if ((word == "if" || word == "unless") && stream.column() == stream.indentation()) + kwtype = "indent"; + else if (word == "do" && state.context.indented < state.indented) + kwtype = "indent"; + } + } + if (curPunc || (style && style != "comment")) state.lastTok = thisTok; + if (curPunc == "|") state.varList = !state.varList; + + if (kwtype == "indent" || /[\(\[\{]/.test(curPunc)) + state.context = {prev: state.context, type: curPunc || style, indented: state.indented}; + else if ((kwtype == "dedent" || /[\)\]\}]/.test(curPunc)) && state.context.prev) + state.context = state.context.prev; + + if (stream.eol()) + state.continuedLine = (curPunc == "\\" || style == "operator"); + return style; + }, + + indent: function (state, textAfter) { + if (state.tokenize[state.tokenize.length - 1] != tokenBase) return 0; + var firstChar = textAfter && textAfter.charAt(0); + var ct = state.context; + var closing = ct.type == matching[firstChar] || + ct.type == "keyword" && /^(?:end|until|else|elsif|when|rescue)\b/.test(textAfter); + return ct.indented + (closing ? 0 : config.indentUnit) + + (state.continuedLine ? config.indentUnit : 0); + }, + + electricChars: "}de", // enD and rescuE + lineComment: "#" + }; + }); + + CodeMirror.defineMIME("text/x-ruby", "ruby"); + +}); diff --git a/devtools/client/shared/sourceeditor/test/cm_script_injection_test.js b/devtools/client/shared/sourceeditor/test/cm_script_injection_test.js new file mode 100644 index 0000000000..85d0794682 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/cm_script_injection_test.js @@ -0,0 +1,10 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* global editor */ + +"use strict"; + +window.addEventListener("editorReady", function () { + editor.setText("Script successfully injected!"); +}); diff --git a/devtools/client/shared/sourceeditor/test/codemirror/codemirror.html b/devtools/client/shared/sourceeditor/test/codemirror/codemirror.html new file mode 100644 index 0000000000..950747c490 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/codemirror.html @@ -0,0 +1,213 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>CodeMirror: Basic Tests</title> + <link rel="stylesheet" href="chrome://devtools/content/shared/sourceeditor/codemirror/lib/codemirror.css"> + <link rel="stylesheet" href="cm_mode_test.css"> + <!--<link rel="stylesheet" href="../doc/docs.css">--> + + <script src="chrome://devtools/content/shared/sourceeditor/codemirror/codemirror.bundle.js"></script> + <script src="chrome://devtools/content/shared/sourceeditor/codemirror/keymap/emacs.js"></script> + <script src="chrome://devtools/content/shared/sourceeditor/codemirror/keymap/sublime.js"></script> + <script src="chrome://devtools/content/shared/sourceeditor/codemirror/keymap/vim.js"></script> + + <style type="text/css"> + .ok {color: #090;} + .fail {color: #e00;} + .error {color: #c90;} + .done {font-weight: bold;} + #progress { + background: #45d; + color: white; + text-shadow: 0 0 1px #45d, 0 0 2px #45d, 0 0 3px #45d; + font-weight: bold; + white-space: pre; + } + #testground { + visibility: hidden; + } + #testground.offscreen { + visibility: visible; + position: absolute; + left: -10000px; + top: -10000px; + } + .CodeMirror { border: 1px solid black; } + </style> + </head> + <body> + <h1>CodeMirror: Basic Tests</h1> + + <p>A limited set of programmatic sanity tests for CodeMirror.</p> + + <div style="border: 1px solid black; padding: 1px; max-width: 700px;"> + <div style="width: 0px;" id=progress><div style="padding: 3px;">Ran <span id="progress_ran">0</span><span id="progress_total"> of 0</span> tests</div></div> + </div> + <p id=status>Please enable JavaScript...</p> + <div id=output></div> + + <div id=testground></div> + + <script src="driver.js"></script> + <script src="test.js"></script> + <script src="comment_test.js"></script> + <script src="doc_test.js"></script> + <script src="driver.js"></script> + <script src="emacs_test.js"></script> + <script src="mode_test.js"></script> + <script src="mode/javascript/test.js"></script> + <script src="multi_test.js"></script> + <script src="search_test.js"></script> + + <!-- VIM and Emacs mode tests are in vimemacs.html + <script src="cm_sublime_test.js"></script> + <script src="cm_vim_test.js"></script> + <script src="cm_emacs_test.js"></script> + --> + + <!-- These modes/addons are not used by Editor + <script src="doc_test.js"></script> + <script src="../mode/css/css.js"></script> + <script src="../mode/css/test.js"></script> + <script src="../mode/css/scss_test.js"></script> + <script src="../mode/xml/xml.js"></script> + <script src="../mode/htmlmixed/htmlmixed.js"></script> + <script src="../mode/ruby/ruby.js"></script> + <script src="../mode/haml/haml.js"></script> + <script src="../mode/haml/test.js"></script> + <script src="../mode/markdown/markdown.js"></script> + <script src="../mode/markdown/test.js"></script> + <script src="../mode/gfm/gfm.js"></script> + <script src="../mode/gfm/test.js"></script> + <script src="../mode/stex/stex.js"></script> + <script src="../mode/stex/test.js"></script> + <script src="../mode/xquery/xquery.js"></script> + <script src="../mode/xquery/test.js"></script> + <script src="../addon/mode/multiplex_test.js"></script>--> + + <script> + window.onload = runHarness; + CodeMirror.on(window, 'hashchange', runHarness); + + function esc(str) { + return str.replace(/[<&]/, function(ch) { return ch == "<" ? "<" : "&"; }); + } + + var output = document.getElementById("output"), + progress = document.getElementById("progress"), + progressRan = document.getElementById("progress_ran").childNodes[0], + progressTotal = document.getElementById("progress_total").childNodes[0]; + + var count = 0, + failed = 0, + skipped = 0, + bad = "", + running = false, // Flag that states tests are running + quit = false, // Flag to quit tests ASAP + verbose = false, // Adds message for *every* test to output + phantom = false; + + function runHarness(){ + if (running) { + quit = true; + setStatus("Restarting tests...", '', true); + setTimeout(function(){runHarness();}, 500); + return; + } + filters = []; + verbose = false; + if (window.location.hash.substr(1)){ + var strings = window.location.hash.substr(1).split(","); + while (strings.length) { + var s = strings.shift(); + if (s === "verbose") + verbose = true; + else + filters.push(parseTestFilter(decodeURIComponent(s))); + } + } + quit = false; + running = true; + setStatus("Loading tests..."); + count = 0; + failed = 0; + skipped = 0; + bad = ""; + totalTests = countTests(); + progressTotal.nodeValue = " of " + totalTests; + progressRan.nodeValue = count; + output.innerHTML = ''; + document.getElementById("testground").innerHTML = "<form>" + + "<textarea id=\"code\" name=\"code\"></textarea>" + + "<input type=submit value=ok name=submit>" + + "</form>"; + runTests(displayTest); + } + + function setStatus(message, className, force){ + if (quit && !force) return; + if (!message) throw("must provide message"); + var status = document.getElementById("status").childNodes[0]; + status.nodeValue = message; + status.parentNode.className = className; + } + function addOutput(name, className, code){ + var newOutput = document.createElement("dl"); + var newTitle = document.createElement("dt"); + newTitle.className = className; + newTitle.appendChild(document.createTextNode(name)); + newOutput.appendChild(newTitle); + var newMessage = document.createElement("dd"); + newMessage.innerHTML = code; + newOutput.appendChild(newTitle); + newOutput.appendChild(newMessage); + output.appendChild(newOutput); + } + function displayTest(type, name, customMessage) { + var message = "???"; + if (type != "done" && type != "skipped") ++count; + progress.style.width = (count * (progress.parentNode.clientWidth - 2) / totalTests) + "px"; + progressRan.nodeValue = count; + if (type == "ok") { + message = "Test '" + name + "' succeeded"; + if (!verbose) customMessage = false; + } else if (type == "skipped") { + message = "Test '" + name + "' skipped"; + ++skipped; + if (!verbose) customMessage = false; + } else if (type == "expected") { + message = "Test '" + name + "' failed as expected"; + if (!verbose) customMessage = false; + } else if (type == "error" || type == "fail") { + ++failed; + message = "Test '" + name + "' failed"; + } else if (type == "done") { + if (failed) { + type += " fail"; + message = failed + " failure" + (failed > 1 ? "s" : ""); + } else if (count < totalTests) { + failed = totalTests - count; + type += " fail"; + message = failed + " failure" + (failed > 1 ? "s" : ""); + } else { + type += " ok"; + message = "All passed"; + if (skipped) { + message += " (" + skipped + " skipped)"; + } + } + progressTotal.nodeValue = ''; + customMessage = true; // Hack to avoid adding to output + } + if (window.mozilla_setStatus) + mozilla_setStatus(message, type, customMessage); + if (verbose && !customMessage) customMessage = message; + setStatus(message, type); + if (customMessage && customMessage.length > 0) { + addOutput(name, type, customMessage); + } + } + </script> + </body> +</html> diff --git a/devtools/client/shared/sourceeditor/test/codemirror/comment_test.js b/devtools/client/shared/sourceeditor/test/codemirror/comment_test.js new file mode 100644 index 0000000000..c6b9fe8109 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/comment_test.js @@ -0,0 +1,114 @@ +namespace = "comment_"; + +(function() { + function test(name, mode, run, before, after) { + return testCM(name, function(cm) { + run(cm); + eq(cm.getValue(), after); + }, {value: before, mode: mode}); + } + + var simpleProg = "function foo() {\n return bar;\n}"; + var inlineBlock = "foo(/* bar */ true);"; + var inlineBlocks = "foo(/* bar */ true, /* baz */ false);"; + var multiLineInlineBlock = ["above();", "foo(/* bar */ true);", "below();"]; + + test("block", "javascript", function(cm) { + cm.blockComment(Pos(0, 3), Pos(3, 0), {blockCommentLead: " *"}); + }, simpleProg + "\n", "/* function foo() {\n * return bar;\n * }\n */"); + + test("blockToggle", "javascript", function(cm) { + cm.blockComment(Pos(0, 3), Pos(2, 0), {blockCommentLead: " *"}); + cm.uncomment(Pos(0, 3), Pos(2, 0), {blockCommentLead: " *"}); + }, simpleProg, simpleProg); + + test("blockToggle2", "javascript", function(cm) { + cm.setCursor({line: 0, ch: 7 /* inside the block comment */}); + cm.execCommand("toggleComment"); + }, inlineBlock, "foo(bar true);"); + + // This test should work but currently fails. + // test("blockToggle3", "javascript", function(cm) { + // cm.setCursor({line: 0, ch: 7 /* inside the first block comment */}); + // cm.execCommand("toggleComment"); + // }, inlineBlocks, "foo(bar true, /* baz */ false);"); + + test("line", "javascript", function(cm) { + cm.lineComment(Pos(1, 1), Pos(1, 1)); + }, simpleProg, "function foo() {\n// return bar;\n}"); + + test("lineToggle", "javascript", function(cm) { + cm.lineComment(Pos(0, 0), Pos(2, 1)); + cm.uncomment(Pos(0, 0), Pos(2, 1)); + }, simpleProg, simpleProg); + + test("fallbackToBlock", "css", function(cm) { + cm.lineComment(Pos(0, 0), Pos(2, 1)); + }, "html {\n border: none;\n}", "/* html {\n border: none;\n} */"); + + test("fallbackToLine", "ruby", function(cm) { + cm.blockComment(Pos(0, 0), Pos(1)); + }, "def blah()\n return hah\n", "# def blah()\n# return hah\n"); + + test("ignoreExternalBlockComments", "javascript", function(cm) { + cm.execCommand("toggleComment"); + }, inlineBlocks, "// " + inlineBlocks); + + test("ignoreExternalBlockComments2", "javascript", function(cm) { + cm.setCursor({line: 0, ch: null /* eol */}); + cm.execCommand("toggleComment"); + }, inlineBlocks, "// " + inlineBlocks); + + test("ignoreExternalBlockCommentsMultiLineAbove", "javascript", function(cm) { + cm.setSelection({line: 0, ch: 0}, {line: 1, ch: 1}); + cm.execCommand("toggleComment"); + }, multiLineInlineBlock.join("\n"), ["// " + multiLineInlineBlock[0], + "// " + multiLineInlineBlock[1], + multiLineInlineBlock[2]].join("\n")); + + test("ignoreExternalBlockCommentsMultiLineBelow", "javascript", function(cm) { + cm.setSelection({line: 1, ch: 13 /* after end of block comment */}, {line: 2, ch: 1}); + cm.execCommand("toggleComment"); + }, multiLineInlineBlock.join("\n"), [multiLineInlineBlock[0], + "// " + multiLineInlineBlock[1], + "// " + multiLineInlineBlock[2]].join("\n")); + + test("commentRange", "javascript", function(cm) { + cm.blockComment(Pos(1, 2), Pos(1, 13), {fullLines: false}); + }, simpleProg, "function foo() {\n /*return bar;*/\n}"); + + test("indented", "javascript", function(cm) { + cm.lineComment(Pos(1, 0), Pos(2), {indent: true}); + }, simpleProg, "function foo() {\n// return bar;\n// }"); + + test("singleEmptyLine", "javascript", function(cm) { + cm.setCursor(1); + cm.execCommand("toggleComment"); + }, "a;\n\nb;", "a;\n// \nb;"); + + test("dontMessWithStrings", "javascript", function(cm) { + cm.execCommand("toggleComment"); + }, "console.log(\"/*string*/\");", "// console.log(\"/*string*/\");"); + + test("dontMessWithStrings2", "javascript", function(cm) { + cm.execCommand("toggleComment"); + }, "console.log(\"// string\");", "// console.log(\"// string\");"); + + test("dontMessWithStrings3", "javascript", function(cm) { + cm.execCommand("toggleComment"); + }, "// console.log(\"// string\");", "console.log(\"// string\");"); + + test("includeLastLine", "javascript", function(cm) { + cm.execCommand("selectAll") + cm.execCommand("toggleComment") + }, "// foo\n// bar\nbaz", "// // foo\n// // bar\n// baz") + + test("uncommentWithTrailingBlockEnd", "xml", function(cm) { + cm.execCommand("toggleComment") + }, "<!-- foo --> -->", "foo -->") + + test("dontCommentInComment", "xml", function(cm) { + cm.setCursor(1, 0) + cm.execCommand("toggleComment") + }, "<!-- foo\nbar -->", "<!-- foo\nbar -->") +})(); diff --git a/devtools/client/shared/sourceeditor/test/codemirror/doc_test.js b/devtools/client/shared/sourceeditor/test/codemirror/doc_test.js new file mode 100644 index 0000000000..3af20ff98d --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/doc_test.js @@ -0,0 +1,371 @@ +(function() { + // A minilanguage for instantiating linked CodeMirror instances and Docs + function instantiateSpec(spec, place, opts) { + var names = {}, pos = 0, l = spec.length, editors = []; + while (spec) { + var m = spec.match(/^(\w+)(\*?)(?:='([^\']*)'|<(~?)(\w+)(?:\/(\d+)-(\d+))?)\s*/); + var name = m[1], isDoc = m[2], cur; + if (m[3]) { + cur = isDoc ? CodeMirror.Doc(m[3]) : CodeMirror(place, clone(opts, {value: m[3]})); + } else { + var other = m[5]; + if (!names.hasOwnProperty(other)) { + names[other] = editors.length; + editors.push(CodeMirror(place, opts)); + } + var doc = editors[names[other]].linkedDoc({ + sharedHist: !m[4], + from: m[6] ? Number(m[6]) : null, + to: m[7] ? Number(m[7]) : null + }); + cur = isDoc ? doc : CodeMirror(place, clone(opts, {value: doc})); + } + names[name] = editors.length; + editors.push(cur); + spec = spec.slice(m[0].length); + } + return editors; + } + + function clone(obj, props) { + if (!obj) return; + clone.prototype = obj; + var inst = new clone(); + if (props) for (var n in props) if (props.hasOwnProperty(n)) + inst[n] = props[n]; + return inst; + } + + function eqAll(val) { + var end = arguments.length, msg = null; + if (typeof arguments[end-1] == "string") + msg = arguments[--end]; + if (i == end) throw new Error("No editors provided to eqAll"); + for (var i = 1; i < end; ++i) + eq(arguments[i].getValue(), val, msg) + } + + function testDoc(name, spec, run, opts, expectFail) { + if (!opts) opts = {}; + + return test("doc_" + name, function() { + var place = document.getElementById("testground"); + var editors = instantiateSpec(spec, place, opts); + var successful = false; + + try { + run.apply(null, editors); + successful = true; + } finally { + if (!successful || verbose) { + place.style.visibility = "visible"; + } else { + for (var i = 0; i < editors.length; ++i) + if (editors[i] instanceof CodeMirror) + place.removeChild(editors[i].getWrapperElement()); + } + } + }, expectFail); + } + + var ie_lt8 = /MSIE [1-7]\b/.test(navigator.userAgent); + + function testBasic(a, b) { + eqAll("x", a, b); + a.setValue("hey"); + eqAll("hey", a, b); + b.setValue("wow"); + eqAll("wow", a, b); + a.replaceRange("u\nv\nw", Pos(0, 3)); + b.replaceRange("i", Pos(0, 4)); + b.replaceRange("j", Pos(2, 1)); + eqAll("wowui\nv\nwj", a, b); + } + + testDoc("basic", "A='x' B<A", testBasic); + testDoc("basicSeparate", "A='x' B<~A", testBasic); + + testDoc("sharedHist", "A='ab\ncd\nef' B<A", function(a, b) { + a.replaceRange("x", Pos(0)); + b.replaceRange("y", Pos(1)); + a.replaceRange("z", Pos(2)); + eqAll("abx\ncdy\nefz", a, b); + a.undo(); + a.undo(); + eqAll("abx\ncd\nef", a, b); + a.redo(); + eqAll("abx\ncdy\nef", a, b); + b.redo(); + eqAll("abx\ncdy\nefz", a, b); + a.undo(); b.undo(); a.undo(); a.undo(); + eqAll("ab\ncd\nef", a, b); + }, null, ie_lt8); + + testDoc("undoIntact", "A='ab\ncd\nef' B<~A", function(a, b) { + a.replaceRange("x", Pos(0)); + b.replaceRange("y", Pos(1)); + a.replaceRange("z", Pos(2)); + a.replaceRange("q", Pos(0)); + eqAll("abxq\ncdy\nefz", a, b); + a.undo(); + a.undo(); + eqAll("abx\ncdy\nef", a, b); + b.undo(); + eqAll("abx\ncd\nef", a, b); + a.redo(); + eqAll("abx\ncd\nefz", a, b); + a.redo(); + eqAll("abxq\ncd\nefz", a, b); + a.undo(); a.undo(); a.undo(); a.undo(); + eqAll("ab\ncd\nef", a, b); + b.redo(); + eqAll("ab\ncdy\nef", a, b); + }); + + testDoc("undoConflict", "A='ab\ncd\nef' B<~A", function(a, b) { + a.replaceRange("x", Pos(0)); + a.replaceRange("z", Pos(2)); + // This should clear the first undo event in a, but not the second + b.replaceRange("y", Pos(0)); + a.undo(); a.undo(); + eqAll("abxy\ncd\nef", a, b); + a.replaceRange("u", Pos(2)); + a.replaceRange("v", Pos(0)); + // This should clear both events in a + b.replaceRange("w", Pos(0)); + a.undo(); a.undo(); + eqAll("abxyvw\ncd\nefu", a, b); + }); + + testDoc("doubleRebase", "A='ab\ncd\nef\ng' B<~A C<B", function(a, b, c) { + c.replaceRange("u", Pos(3)); + a.replaceRange("", Pos(0, 0), Pos(1, 0)); + c.undo(); + eqAll("cd\nef\ng", a, b, c); + }); + + testDoc("undoUpdate", "A='ab\ncd\nef' B<~A", function(a, b) { + a.replaceRange("x", Pos(2)); + b.replaceRange("u\nv\nw\n", Pos(0, 0)); + a.undo(); + eqAll("u\nv\nw\nab\ncd\nef", a, b); + a.redo(); + eqAll("u\nv\nw\nab\ncd\nefx", a, b); + a.undo(); + eqAll("u\nv\nw\nab\ncd\nef", a, b); + b.undo(); + a.redo(); + eqAll("ab\ncd\nefx", a, b); + a.undo(); + eqAll("ab\ncd\nef", a, b); + }); + + testDoc("undoKeepRanges", "A='abcdefg' B<A", function(a, b) { + var m = a.markText(Pos(0, 1), Pos(0, 3), {className: "foo"}); + b.replaceRange("x", Pos(0, 0)); + eqCharPos(m.find().from, Pos(0, 2)); + b.replaceRange("yzzy", Pos(0, 1), Pos(0)); + eq(m.find(), null); + b.undo(); + eqCharPos(m.find().from, Pos(0, 2)); + b.undo(); + eqCharPos(m.find().from, Pos(0, 1)); + }); + + testDoc("longChain", "A='uv' B<A C<B D<C", function(a, b, c, d) { + a.replaceSelection("X"); + eqAll("Xuv", a, b, c, d); + d.replaceRange("Y", Pos(0)); + eqAll("XuvY", a, b, c, d); + }); + + testDoc("broadCast", "B<A C<A D<A E<A", function(a, b, c, d, e) { + b.setValue("uu"); + eqAll("uu", a, b, c, d, e); + a.replaceRange("v", Pos(0, 1)); + eqAll("uvu", a, b, c, d, e); + }); + + // A and B share a history, C and D share a separate one + testDoc("islands", "A='x\ny\nz' B<A C<~A D<C", function(a, b, c, d) { + a.replaceRange("u", Pos(0)); + d.replaceRange("v", Pos(2)); + b.undo(); + eqAll("x\ny\nzv", a, b, c, d); + c.undo(); + eqAll("x\ny\nz", a, b, c, d); + a.redo(); + eqAll("xu\ny\nz", a, b, c, d); + d.redo(); + eqAll("xu\ny\nzv", a, b, c, d); + }); + + testDoc("unlink", "B<A C<A D<B", function(a, b, c, d) { + a.setValue("hi"); + b.unlinkDoc(a); + d.setValue("aye"); + eqAll("hi", a, c); + eqAll("aye", b, d); + a.setValue("oo"); + eqAll("oo", a, c); + eqAll("aye", b, d); + }); + + testDoc("bareDoc", "A*='foo' B*<A C<B", function(a, b, c) { + is(a instanceof CodeMirror.Doc); + is(b instanceof CodeMirror.Doc); + is(c instanceof CodeMirror); + eqAll("foo", a, b, c); + a.replaceRange("hey", Pos(0, 0), Pos(0)); + c.replaceRange("!", Pos(0)); + eqAll("hey!", a, b, c); + b.unlinkDoc(a); + b.setValue("x"); + eqAll("x", b, c); + eqAll("hey!", a); + }); + + testDoc("swapDoc", "A='a' B*='b' C<A", function(a, b, c) { + var d = a.swapDoc(b); + d.setValue("x"); + eqAll("x", c, d); + eqAll("b", a, b); + }); + + testDoc("docKeepsScroll", "A='x' B*='y'", function(a, b) { + addDoc(a, 200, 200); + a.scrollIntoView(Pos(199, 200)); + var c = a.swapDoc(b); + a.swapDoc(c); + var pos = a.getScrollInfo(); + is(pos.left > 0, "not at left"); + is(pos.top > 0, "not at top"); + }); + + testDoc("copyDoc", "A='u'", function(a) { + var copy = a.getDoc().copy(true); + a.setValue("foo"); + copy.setValue("bar"); + var old = a.swapDoc(copy); + eq(a.getValue(), "bar"); + a.undo(); + eq(a.getValue(), "u"); + a.swapDoc(old); + eq(a.getValue(), "foo"); + eq(old.historySize().undo, 1); + eq(old.copy(false).historySize().undo, 0); + }); + + testDoc("docKeepsMode", "A='1+1'", function(a) { + var other = CodeMirror.Doc("hi", "text/x-markdown"); + a.setOption("mode", "text/javascript"); + var old = a.swapDoc(other); + eq(a.getOption("mode"), "text/x-markdown"); + eq(a.getMode().name, "markdown"); + a.swapDoc(old); + eq(a.getOption("mode"), "text/javascript"); + eq(a.getMode().name, "javascript"); + }); + + testDoc("subview", "A='1\n2\n3\n4\n5' B<~A/1-3", function(a, b) { + eq(b.getValue(), "2\n3"); + eq(b.firstLine(), 1); + b.setCursor(Pos(4)); + eqCharPos(b.getCursor(), Pos(2, 1)); + a.replaceRange("-1\n0\n", Pos(0, 0)); + eq(b.firstLine(), 3); + eqCharPos(b.getCursor(), Pos(4, 1)); + a.undo(); + eqCharPos(b.getCursor(), Pos(2, 1)); + b.replaceRange("oyoy\n", Pos(2, 0)); + eq(a.getValue(), "1\n2\noyoy\n3\n4\n5"); + b.undo(); + eq(a.getValue(), "1\n2\n3\n4\n5"); + }); + + testDoc("subviewEditOnBoundary", "A='11\n22\n33\n44\n55' B<~A/1-4", function(a, b) { + a.replaceRange("x\nyy\nz", Pos(0, 1), Pos(2, 1)); + eq(b.firstLine(), 2); + eq(b.lineCount(), 2); + eq(b.getValue(), "z3\n44"); + a.replaceRange("q\nrr\ns", Pos(3, 1), Pos(4, 1)); + eq(b.firstLine(), 2); + eq(b.getValue(), "z3\n4q"); + eq(a.getValue(), "1x\nyy\nz3\n4q\nrr\ns5"); + a.execCommand("selectAll"); + a.replaceSelection("!"); + eqAll("!", a, b); + }); + + + testDoc("sharedMarker", "A='ab\ncd\nef\ngh' B<A C<~A/1-2", function(a, b, c) { + var mark = b.markText(Pos(0, 1), Pos(3, 1), + {className: "cm-searching", shared: true}); + var found = a.findMarksAt(Pos(0, 2)); + eq(found.length, 1); + eq(found[0], mark); + eq(c.findMarksAt(Pos(1, 1)).length, 1); + eqCharPos(mark.find().from, Pos(0, 1)); + eqCharPos(mark.find().to, Pos(3, 1)); + b.replaceRange("x\ny\n", Pos(0, 0)); + eqCharPos(mark.find().from, Pos(2, 1)); + eqCharPos(mark.find().to, Pos(5, 1)); + var cleared = 0; + CodeMirror.on(mark, "clear", function() {++cleared;}); + b.operation(function(){mark.clear();}); + eq(a.findMarksAt(Pos(3, 1)).length, 0); + eq(b.findMarksAt(Pos(3, 1)).length, 0); + eq(c.findMarksAt(Pos(3, 1)).length, 0); + eq(mark.find(), null); + eq(cleared, 1); + }); + + testDoc("sharedMarkerCopy", "A='abcde'", function(a) { + var shared = a.markText(Pos(0, 1), Pos(0, 3), {shared: true}); + var b = a.linkedDoc(); + var found = b.findMarksAt(Pos(0, 2)); + eq(found.length, 1); + eq(found[0], shared); + shared.clear(); + eq(b.findMarksAt(Pos(0, 2)), 0); + }); + + testDoc("sharedMarkerDetach", "A='abcde' B<A C<B", function(a, b, c) { + var shared = a.markText(Pos(0, 1), Pos(0, 3), {shared: true}); + a.unlinkDoc(b); + var inB = b.findMarksAt(Pos(0, 2)); + eq(inB.length, 1); + is(inB[0] != shared); + var inC = c.findMarksAt(Pos(0, 2)); + eq(inC.length, 1); + is(inC[0] != shared); + inC[0].clear(); + is(shared.find()); + }); + + testDoc("sharedBookmark", "A='ab\ncd\nef\ngh' B<A C<~A/1-2", function(a, b, c) { + var mark = b.setBookmark(Pos(1, 1), {shared: true}); + var found = a.findMarksAt(Pos(1, 1)); + eq(found.length, 1); + eq(found[0], mark); + eq(c.findMarksAt(Pos(1, 1)).length, 1); + eqCharPos(mark.find(), Pos(1, 1)); + b.replaceRange("x\ny\n", Pos(0, 0)); + eqCharPos(mark.find(), Pos(3, 1)); + var cleared = 0; + CodeMirror.on(mark, "clear", function() {++cleared;}); + b.operation(function() {mark.clear();}); + eq(a.findMarks(Pos(0, 0), Pos(5)).length, 0); + eq(b.findMarks(Pos(0, 0), Pos(5)).length, 0); + eq(c.findMarks(Pos(0, 0), Pos(5)).length, 0); + eq(mark.find(), null); + eq(cleared, 1); + }); + + testDoc("undoInSubview", "A='line 0\nline 1\nline 2\nline 3\nline 4' B<A/1-4", function(a, b) { + b.replaceRange("x", Pos(2, 0)); + a.undo(); + eq(a.getValue(), "line 0\nline 1\nline 2\nline 3\nline 4"); + eq(b.getValue(), "line 1\nline 2\nline 3"); + }); +})(); diff --git a/devtools/client/shared/sourceeditor/test/codemirror/driver.js b/devtools/client/shared/sourceeditor/test/codemirror/driver.js new file mode 100644 index 0000000000..8789f73be9 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/driver.js @@ -0,0 +1,142 @@ +var tests = [], filters = [], allNames = []; + +function Failure(why) {this.message = why;} +Failure.prototype.toString = function() { return this.message; }; + +function indexOf(collection, elt) { + if (collection.indexOf) return collection.indexOf(elt); + for (var i = 0, e = collection.length; i < e; ++i) + if (collection[i] == elt) return i; + return -1; +} + +function test(name, run, expectedFail) { + // Force unique names + var originalName = name; + var i = 2; // Second function would be NAME_2 + while (indexOf(allNames, name) !== -1){ + name = originalName + "_" + i; + i++; + } + allNames.push(name); + // Add test + tests.push({name: name, func: run, expectedFail: expectedFail}); + return name; +} +var namespace = ""; +function testCM(name, run, opts, expectedFail) { + return test(namespace + name, function() { + var place = document.getElementById("testground"), cm = window.cm = CodeMirror(place, opts); + var successful = false; + try { + run(cm); + successful = true; + } finally { + if (!successful || verbose) { + place.style.visibility = "visible"; + } else { + place.removeChild(cm.getWrapperElement()); + } + } + }, expectedFail); +} + +function runTests(callback) { + var totalTime = 0; + function step(i) { + for (;;) { + if (i === tests.length) { + running = false; + return callback("done"); + } + var test = tests[i], skip = false; + if (filters.length) { + skip = true; + for (var j = 0; j < filters.length; j++) + if (test.name.match(filters[j])) skip = false; + } + if (skip) { + callback("skipped", test.name, message); + i++; + } else { + break; + } + } + var expFail = test.expectedFail, startTime = +new Date, threw = false; + try { + var message = test.func(); + } catch(e) { + threw = true; + if (expFail) callback("expected", test.name); + else if (e instanceof Failure) callback("fail", test.name, e.message); + else { + var pos = /(?:\bat |@).*?([^\/:]+):(\d+)/.exec(e.stack); + if (pos) console["log"](e.stack); + callback("error", test.name, e.toString() + (pos ? " (" + pos[1] + ":" + pos[2] + ")" : "")); + } + } + if (!threw) { + if (expFail) callback("fail", test.name, message || "expected failure, but passed"); + else callback("ok", test.name, message); + } + if (!quit) { // Run next test + var delay = 0; + totalTime += (+new Date) - startTime; + if (totalTime > 500){ + totalTime = 0; + delay = 50; + } + setTimeout(function(){step(i + 1);}, delay); + } else { // Quit tests + running = false; + return null; + } + } + step(0); +} + +function label(str, msg) { + if (msg) return str + " (" + msg + ")"; + return str; +} +function eq(a, b, msg) { + if (a != b) throw new Failure(label(a + " != " + b, msg)); +} +function near(a, b, margin, msg) { + if (Math.abs(a - b) > margin) + throw new Failure(label(a + " is not close to " + b + " (" + margin + ")", msg)); +} +function eqCharPos(a, b, msg) { + function str(p) { return "{line:" + p.line + ",ch:" + p.ch + ",sticky:" + p.sticky + "}"; } + if (a == b) return; + if (a == null) throw new Failure(label("comparing null to " + str(b), msg)); + if (b == null) throw new Failure(label("comparing " + str(a) + " to null", msg)); + if (a.line != b.line || a.ch != b.ch) throw new Failure(label(str(a) + " != " + str(b), msg)); +} +function eqCursorPos(a, b, msg) { + eqCharPos(a, b, msg); + if (a) eq(a.sticky, b.sticky, msg ? msg + ' (sticky)' : 'sticky'); +} +function is(a, msg) { + if (!a) throw new Failure(label("assertion failed", msg)); +} + +function countTests() { + if (!filters.length) return tests.length; + var sum = 0; + for (var i = 0; i < tests.length; ++i) { + var name = tests[i].name; + for (var j = 0; j < filters.length; j++) { + if (name.match(filters[j])) { + ++sum; + break; + } + } + } + return sum; +} + +function parseTestFilter(s) { + if (/_\*$/.test(s)) return new RegExp("^" + s.slice(0, s.length - 2), "i"); + else return new RegExp(s, "i"); +} diff --git a/devtools/client/shared/sourceeditor/test/codemirror/emacs_test.js b/devtools/client/shared/sourceeditor/test/codemirror/emacs_test.js new file mode 100644 index 0000000000..412dba4b42 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/emacs_test.js @@ -0,0 +1,149 @@ +(function() { + "use strict"; + + var Pos = CodeMirror.Pos; + namespace = "emacs_"; + + var eventCache = {}; + function fakeEvent(keyName) { + var event = eventCache[key]; + if (event) return event; + + var ctrl, shift, alt; + var key = keyName.replace(/\w+-/g, function(type) { + if (type == "Ctrl-") ctrl = true; + else if (type == "Alt-") alt = true; + else if (type == "Shift-") shift = true; + return ""; + }); + var code; + for (var c in CodeMirror.keyNames) + if (CodeMirror.keyNames[c] == key) { code = c; break; } + if (code == null) throw new Error("Unknown key: " + key); + + return eventCache[keyName] = { + type: "keydown", keyCode: code, ctrlKey: ctrl, shiftKey: shift, altKey: alt, + preventDefault: function(){}, stopPropagation: function(){} + }; + } + + function sim(name, start /*, actions... */) { + var keys = Array.prototype.slice.call(arguments, 2); + testCM(name, function(cm) { + for (var i = 0; i < keys.length; ++i) { + var cur = keys[i]; + if (cur instanceof Pos) cm.setCursor(cur); + else if (cur.call) cur(cm); + else cm.triggerOnKeyDown(fakeEvent(cur)); + } + }, {keyMap: "emacs", value: start, mode: "javascript"}); + } + + function at(line, ch, sticky) { return function(cm) { eqCursorPos(cm.getCursor(), Pos(line, ch, sticky)); }; } + function txt(str) { return function(cm) { eq(cm.getValue(), str); }; } + + sim("motionHSimple", "abc", "Ctrl-F", "Ctrl-F", "Ctrl-B", at(0, 1, "after")); + sim("motionHMulti", "abcde", + "Ctrl-4", "Ctrl-F", at(0, 4, "before"), "Ctrl--", "Ctrl-2", "Ctrl-F", at(0, 2, "after"), + "Ctrl-5", "Ctrl-B", at(0, 0, "after")); + + sim("motionHWord", "abc. def ghi", + "Alt-F", at(0, 3, "before"), "Alt-F", at(0, 8, "before"), + "Ctrl-B", "Alt-B", at(0, 5, "after"), "Alt-B", at(0, 0, "after")); + sim("motionHWordMulti", "abc. def ghi ", + "Ctrl-3", "Alt-F", at(0, 12, "before"), "Ctrl-2", "Alt-B", at(0, 5, "after"), + "Ctrl--", "Alt-B", at(0, 8, "before")); + + sim("motionVSimple", "a\nb\nc\n", "Ctrl-N", "Ctrl-N", "Ctrl-P", at(1, 0, "after")); + sim("motionVMulti", "a\nb\nc\nd\ne\n", + "Ctrl-2", "Ctrl-N", at(2, 0, "after"), "Ctrl-F", "Ctrl--", "Ctrl-N", at(1, 1, "before"), + "Ctrl--", "Ctrl-3", "Ctrl-P", at(4, 1, "before")); + + sim("killYank", "abc\ndef\nghi", + "Ctrl-F", "Ctrl-Space", "Ctrl-N", "Ctrl-N", "Ctrl-W", "Ctrl-E", "Ctrl-Y", + txt("ahibc\ndef\ng")); + sim("killRing", "abcdef", + "Ctrl-Space", "Ctrl-F", "Ctrl-W", "Ctrl-Space", "Ctrl-F", "Ctrl-W", + "Ctrl-Y", "Alt-Y", + txt("acdef")); + sim("copyYank", "abcd", + "Ctrl-Space", "Ctrl-E", "Alt-W", "Ctrl-Y", + txt("abcdabcd")); + + sim("killLineSimple", "foo\nbar", "Ctrl-F", "Ctrl-K", txt("f\nbar")); + sim("killLineEmptyLine", "foo\n \nbar", "Ctrl-N", "Ctrl-K", txt("foo\nbar")); + sim("killLineMulti", "foo\nbar\nbaz", + "Ctrl-F", "Ctrl-F", "Ctrl-K", "Ctrl-K", "Ctrl-K", "Ctrl-A", "Ctrl-Y", + txt("o\nbarfo\nbaz")); + + sim("moveByParagraph", "abc\ndef\n\n\nhij\nklm\n\n", + "Ctrl-F", "Ctrl-Down", at(2, 0), "Ctrl-Down", at(6, 0), + "Ctrl-N", "Ctrl-Up", at(3, 0), "Ctrl-Up", at(0, 0), + Pos(1, 2), "Ctrl-Down", at(2, 0), Pos(4, 2), "Ctrl-Up", at(3, 0)); + sim("moveByParagraphMulti", "abc\n\ndef\n\nhij\n\nklm", + "Ctrl-U", "2", "Ctrl-Down", at(3, 0), + "Shift-Alt-.", "Ctrl-3", "Ctrl-Up", at(1, 0)); + + sim("moveBySentence", "sentence one! sentence\ntwo\n\nparagraph two", + "Alt-E", at(0, 13), "Alt-E", at(1, 3), "Ctrl-F", "Alt-A", at(0, 13)); + + sim("moveByExpr", "function foo(a, b) {}", + "Ctrl-Alt-F", at(0, 8), "Ctrl-Alt-F", at(0, 12), "Ctrl-Alt-F", at(0, 18), + "Ctrl-Alt-B", at(0, 12), "Ctrl-Alt-B", at(0, 9)); + sim("moveByExprMulti", "foo bar baz bug", + "Ctrl-2", "Ctrl-Alt-F", at(0, 7), + "Ctrl--", "Ctrl-Alt-F", at(0, 4), + "Ctrl--", "Ctrl-2", "Ctrl-Alt-B", at(0, 11)); + sim("delExpr", "var x = [\n a,\n b\n c\n];", + Pos(0, 8), "Ctrl-Alt-K", txt("var x = ;"), "Ctrl-/", + Pos(4, 1), "Ctrl-Alt-Backspace", txt("var x = ;")); + sim("delExprMulti", "foo bar baz", + "Ctrl-2", "Ctrl-Alt-K", txt(" baz"), + "Ctrl-/", "Ctrl-E", "Ctrl-2", "Ctrl-Alt-Backspace", txt("foo ")); + + sim("justOneSpace", "hi bye ", + Pos(0, 4), "Alt-Space", txt("hi bye "), + Pos(0, 4), "Alt-Space", txt("hi b ye "), + "Ctrl-A", "Alt-Space", "Ctrl-E", "Alt-Space", txt(" hi b ye ")); + + sim("openLine", "foo bar", "Alt-F", "Ctrl-O", txt("foo\n bar")) + + sim("transposeChar", "abcd\ne", + "Ctrl-F", "Ctrl-T", "Ctrl-T", txt("bcad\ne"), at(0, 3), + "Ctrl-F", "Ctrl-T", "Ctrl-T", "Ctrl-T", txt("bcda\ne"), at(0, 4), + "Ctrl-F", "Ctrl-T", txt("bcde\na"), at(1, 1)); + + sim("manipWordCase", "foo BAR bAZ", + "Alt-C", "Alt-L", "Alt-U", txt("Foo bar BAZ"), + "Ctrl-A", "Alt-U", "Alt-L", "Alt-C", txt("FOO bar Baz")); + sim("manipWordCaseMulti", "foo Bar bAz", + "Ctrl-2", "Alt-U", txt("FOO BAR bAz"), + "Ctrl-A", "Ctrl-3", "Alt-C", txt("Foo Bar Baz")); + + sim("upExpr", "foo {\n bar[];\n baz(blah);\n}", + Pos(2, 7), "Ctrl-Alt-U", at(2, 5), "Ctrl-Alt-U", at(0, 4)); + sim("transposeExpr", "do foo[bar] dah", + Pos(0, 6), "Ctrl-Alt-T", txt("do [bar]foo dah")); + + sim("clearMark", "abcde", Pos(0, 2), "Ctrl-Space", "Ctrl-F", "Ctrl-F", + "Ctrl-G", "Ctrl-W", txt("abcde")); + + sim("delRegion", "abcde", "Ctrl-Space", "Ctrl-F", "Ctrl-F", "Delete", txt("cde")); + sim("backspaceRegion", "abcde", "Ctrl-Space", "Ctrl-F", "Ctrl-F", "Backspace", txt("cde")); + + sim("backspaceDoesntAddToRing", "foobar", "Ctrl-F", "Ctrl-F", "Ctrl-F", "Ctrl-K", "Backspace", "Backspace", "Ctrl-Y", txt("fbar")); + + testCM("save", function(cm) { + var saved = false; + CodeMirror.commands.save = function(cm) { saved = cm.getValue(); }; + cm.triggerOnKeyDown(fakeEvent("Ctrl-X")); + cm.triggerOnKeyDown(fakeEvent("Ctrl-S")); + is(saved, "hi"); + }, {value: "hi", keyMap: "emacs"}); + + testCM("gotoInvalidLineFloat", function(cm) { + cm.openDialog = function(_, cb) { cb("2.2"); }; + cm.triggerOnKeyDown(fakeEvent("Alt-G")); + cm.triggerOnKeyDown(fakeEvent("G")); + }, {value: "1\n2\n3\n4", keyMap: "emacs"}); +})(); diff --git a/devtools/client/shared/sourceeditor/test/codemirror/mode/javascript/test.js b/devtools/client/shared/sourceeditor/test/codemirror/mode/javascript/test.js new file mode 100644 index 0000000000..327eac76b9 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/mode/javascript/test.js @@ -0,0 +1,513 @@ +// CodeMirror, copyright (c) by Marijn Haverbeke and others +// Distributed under an MIT license: https://codemirror.net/LICENSE + +(function() { + var mode = CodeMirror.getMode({indentUnit: 2}, "javascript"); + function MT(name) { test.mode(name, mode, Array.prototype.slice.call(arguments, 1)); } + + MT("locals", + "[keyword function] [def foo]([def a], [def b]) { [keyword var] [def c] [operator =] [number 10]; [keyword return] [variable-2 a] [operator +] [variable-2 c] [operator +] [variable d]; }"); + + MT("comma-and-binop", + "[keyword function](){ [keyword var] [def x] [operator =] [number 1] [operator +] [number 2], [def y]; }"); + + MT("destructuring", + "([keyword function]([def a], [[[def b], [def c] ]]) {", + " [keyword let] {[def d], [property foo]: [def c][operator =][number 10], [def x]} [operator =] [variable foo]([variable-2 a]);", + " [[[variable-2 c], [variable y] ]] [operator =] [variable-2 c];", + "})();"); + + MT("destructure_trailing_comma", + "[keyword let] {[def a], [def b],} [operator =] [variable foo];", + "[keyword let] [def c];"); // Parser still in good state? + + MT("class_body", + "[keyword class] [def Foo] {", + " [property constructor]() {}", + " [property sayName]() {", + " [keyword return] [string-2 `foo${][variable foo][string-2 }oo`];", + " }", + "}"); + + MT("class", + "[keyword class] [def Point] [keyword extends] [variable SuperThing] {", + " [keyword get] [property prop]() { [keyword return] [number 24]; }", + " [property constructor]([def x], [def y]) {", + " [keyword super]([string 'something']);", + " [keyword this].[property x] [operator =] [variable-2 x];", + " }", + "}"); + + MT("anonymous_class_expression", + "[keyword const] [def Adder] [operator =] [keyword class] [keyword extends] [variable Arithmetic] {", + " [property add]([def a], [def b]) {}", + "};"); + + MT("named_class_expression", + "[keyword const] [def Subber] [operator =] [keyword class] [def Subtract] {", + " [property sub]([def a], [def b]) {}", + "};"); + + MT("class_async_method", + "[keyword class] [def Foo] {", + " [property sayName1]() {}", + " [keyword async] [property sayName2]() {}", + "}"); + + MT("import", + "[keyword function] [def foo]() {", + " [keyword import] [def $] [keyword from] [string 'jquery'];", + " [keyword import] { [def encrypt], [def decrypt] } [keyword from] [string 'crypto'];", + "}"); + + MT("import_trailing_comma", + "[keyword import] {[def foo], [def bar],} [keyword from] [string 'baz']") + + MT("import_dynamic", + "[keyword import]([string 'baz']).[property then]") + + MT("import_dynamic", + "[keyword const] [def t] [operator =] [keyword import]([string 'baz']).[property then]") + + MT("const", + "[keyword function] [def f]() {", + " [keyword const] [[ [def a], [def b] ]] [operator =] [[ [number 1], [number 2] ]];", + "}"); + + MT("for/of", + "[keyword for]([keyword let] [def of] [keyword of] [variable something]) {}"); + + MT("for await", + "[keyword for] [keyword await]([keyword let] [def of] [keyword of] [variable something]) {}"); + + MT("generator", + "[keyword function*] [def repeat]([def n]) {", + " [keyword for]([keyword var] [def i] [operator =] [number 0]; [variable-2 i] [operator <] [variable-2 n]; [operator ++][variable-2 i])", + " [keyword yield] [variable-2 i];", + "}"); + + MT("let_scoping", + "[keyword function] [def scoped]([def n]) {", + " { [keyword var] [def i]; } [variable-2 i];", + " { [keyword let] [def j]; [variable-2 j]; } [variable j];", + " [keyword if] ([atom true]) { [keyword const] [def k]; [variable-2 k]; } [variable k];", + "}"); + + MT("switch_scoping", + "[keyword switch] ([variable x]) {", + " [keyword default]:", + " [keyword let] [def j];", + " [keyword return] [variable-2 j]", + "}", + "[variable j];") + + MT("leaving_scope", + "[keyword function] [def a]() {", + " {", + " [keyword const] [def x] [operator =] [number 1]", + " [keyword if] ([atom true]) {", + " [keyword let] [def y] [operator =] [number 2]", + " [keyword var] [def z] [operator =] [number 3]", + " [variable console].[property log]([variable-2 x], [variable-2 y], [variable-2 z])", + " }", + " [variable console].[property log]([variable-2 x], [variable y], [variable-2 z])", + " }", + " [variable console].[property log]([variable x], [variable y], [variable-2 z])", + "}") + + MT("quotedStringAddition", + "[keyword let] [def f] [operator =] [variable a] [operator +] [string 'fatarrow'] [operator +] [variable c];"); + + MT("quotedFatArrow", + "[keyword let] [def f] [operator =] [variable a] [operator +] [string '=>'] [operator +] [variable c];"); + + MT("fatArrow", + "[variable array].[property filter]([def a] [operator =>] [variable-2 a] [operator +] [number 1]);", + "[variable a];", // No longer in scope + "[keyword let] [def f] [operator =] ([[ [def a], [def b] ]], [def c]) [operator =>] [variable-2 a] [operator +] [variable-2 c];", + "[variable c];"); + + MT("fatArrow_stringDefault", + "([def a], [def b] [operator =] [string 'x\\'y']) [operator =>] [variable-2 a] [operator +] [variable-2 b]") + + MT("spread", + "[keyword function] [def f]([def a], [meta ...][def b]) {", + " [variable something]([variable-2 a], [meta ...][variable-2 b]);", + "}"); + + MT("quasi", + "[variable re][string-2 `fofdlakj${][variable x] [operator +] ([variable re][string-2 `foo`]) [operator +] [number 1][string-2 }fdsa`] [operator +] [number 2]"); + + MT("quasi_no_function", + "[variable x] [operator =] [string-2 `fofdlakj${][variable x] [operator +] [string-2 `foo`] [operator +] [number 1][string-2 }fdsa`] [operator +] [number 2]"); + + MT("indent_statement", + "[keyword var] [def x] [operator =] [number 10]", + "[variable x] [operator +=] [variable y] [operator +]", + " [atom Infinity]", + "[keyword debugger];"); + + MT("indent_if", + "[keyword if] ([number 1])", + " [keyword break];", + "[keyword else] [keyword if] ([number 2])", + " [keyword continue];", + "[keyword else]", + " [number 10];", + "[keyword if] ([number 1]) {", + " [keyword break];", + "} [keyword else] [keyword if] ([number 2]) {", + " [keyword continue];", + "} [keyword else] {", + " [number 10];", + "}"); + + MT("indent_for", + "[keyword for] ([keyword var] [def i] [operator =] [number 0];", + " [variable i] [operator <] [number 100];", + " [variable i][operator ++])", + " [variable doSomething]([variable i]);", + "[keyword debugger];"); + + MT("indent_c_style", + "[keyword function] [def foo]()", + "{", + " [keyword debugger];", + "}"); + + MT("indent_else", + "[keyword for] (;;)", + " [keyword if] ([variable foo])", + " [keyword if] ([variable bar])", + " [number 1];", + " [keyword else]", + " [number 2];", + " [keyword else]", + " [number 3];"); + + MT("indent_funarg", + "[variable foo]([number 10000],", + " [keyword function]([def a]) {", + " [keyword debugger];", + "};"); + + MT("indent_below_if", + "[keyword for] (;;)", + " [keyword if] ([variable foo])", + " [number 1];", + "[number 2];"); + + MT("indent_semicolonless_if", + "[keyword function] [def foo]() {", + " [keyword if] ([variable x])", + " [variable foo]()", + "}") + + MT("indent_semicolonless_if_with_statement", + "[keyword function] [def foo]() {", + " [keyword if] ([variable x])", + " [variable foo]()", + " [variable bar]()", + "}") + + MT("multilinestring", + "[keyword var] [def x] [operator =] [string 'foo\\]", + "[string bar'];"); + + MT("scary_regexp", + "[string-2 /foo[[/]]bar/];"); + + MT("indent_strange_array", + "[keyword var] [def x] [operator =] [[", + " [number 1],,", + " [number 2],", + "]];", + "[number 10];"); + + MT("param_default", + "[keyword function] [def foo]([def x] [operator =] [string-2 `foo${][number 10][string-2 }bar`]) {", + " [keyword return] [variable-2 x];", + "}"); + + MT( + "param_destructuring", + "[keyword function] [def foo]([def x] [operator =] [string-2 `foo${][number 10][string-2 }bar`]) {", + " [keyword return] [variable-2 x];", + "}"); + + MT("new_target", + "[keyword function] [def F]([def target]) {", + " [keyword if] ([variable-2 target] [operator &&] [keyword new].[keyword target].[property name]) {", + " [keyword return] [keyword new]", + " .[keyword target];", + " }", + "}"); + + MT("async", + "[keyword async] [keyword function] [def foo]([def args]) { [keyword return] [atom true]; }"); + + MT("async_assignment", + "[keyword const] [def foo] [operator =] [keyword async] [keyword function] ([def args]) { [keyword return] [atom true]; };"); + + MT("async_object", + "[keyword let] [def obj] [operator =] { [property async]: [atom false] };"); + + // async be highlighet as keyword and foo as def, but it requires potentially expensive look-ahead. See #4173 + MT("async_object_function", + "[keyword let] [def obj] [operator =] { [property async] [property foo]([def args]) { [keyword return] [atom true]; } };"); + + MT("async_object_properties", + "[keyword let] [def obj] [operator =] {", + " [property prop1]: [keyword async] [keyword function] ([def args]) { [keyword return] [atom true]; },", + " [property prop2]: [keyword async] [keyword function] ([def args]) { [keyword return] [atom true]; },", + " [property prop3]: [keyword async] [keyword function] [def prop3]([def args]) { [keyword return] [atom true]; },", + "};"); + + MT("async_arrow", + "[keyword const] [def foo] [operator =] [keyword async] ([def args]) [operator =>] { [keyword return] [atom true]; };"); + + MT("async_jquery", + "[variable $].[property ajax]({", + " [property url]: [variable url],", + " [property async]: [atom true],", + " [property method]: [string 'GET']", + "});"); + + MT("async_variable", + "[keyword const] [def async] [operator =] {[property a]: [number 1]};", + "[keyword const] [def foo] [operator =] [string-2 `bar ${][variable async].[property a][string-2 }`];") + + MT("bigint", "[number 1n] [operator +] [number 0x1afn] [operator +] [number 0o064n] [operator +] [number 0b100n];") + + MT("async_comment", + "[keyword async] [comment /**/] [keyword function] [def foo]([def args]) { [keyword return] [atom true]; }"); + + MT("indent_switch", + "[keyword switch] ([variable x]) {", + " [keyword default]:", + " [keyword return] [number 2]", + "}") + + MT("regexp_corner_case", + "[operator +]{} [operator /] [atom undefined];", + "[[[meta ...][string-2 /\\//] ]];", + "[keyword void] [string-2 /\\//];", + "[keyword do] [string-2 /\\//]; [keyword while] ([number 0]);", + "[keyword if] ([number 0]) {} [keyword else] [string-2 /\\//];", + "[string-2 `${][variable async][operator ++][string-2 }//`];", + "[string-2 `${]{} [operator /] [string-2 /\\//}`];") + + MT("return_eol", + "[keyword return]", + "{} [string-2 /5/]") + + MT("numeric separator", + "[number 123_456];", + "[number 0xdead_c0de];", + "[number 0o123_456];", + "[number 0b1101_1101];", + "[number .123_456e0_1];", + "[number 1E+123_456];", + "[number 12_34_56n];") + + MT("underscore property", + "[variable something].[property _property];", + "[variable something].[property _123];", + "[variable something].[property _for];", + "[variable _for];", + "[variable _123];") + + var ts_mode = CodeMirror.getMode({indentUnit: 2}, "application/typescript") + function TS(name) { + test.mode(name, ts_mode, Array.prototype.slice.call(arguments, 1)) + } + + TS("typescript_extend_type", + "[keyword class] [def Foo] [keyword extends] [type Some][operator <][type Type][operator >] {}") + + TS("typescript_arrow_type", + "[keyword let] [def x]: ([variable arg]: [type Type]) [operator =>] [type ReturnType]") + + TS("typescript_class", + "[keyword class] [def Foo] {", + " [keyword public] [keyword static] [property main]() {}", + " [keyword private] [property _foo]: [type string];", + "}") + + TS("typescript_literal_types", + "[keyword import] [keyword *] [keyword as] [def Sequelize] [keyword from] [string 'sequelize'];", + "[keyword interface] [def MyAttributes] {", + " [property truthy]: [string 'true'] [operator |] [number 1] [operator |] [atom true];", + " [property falsy]: [string 'false'] [operator |] [number 0] [operator |] [atom false];", + "}", + "[keyword interface] [def MyInstance] [keyword extends] [type Sequelize].[type Instance] [operator <] [type MyAttributes] [operator >] {", + " [property rawAttributes]: [type MyAttributes];", + " [property truthy]: [string 'true'] [operator |] [number 1] [operator |] [atom true];", + " [property falsy]: [string 'false'] [operator |] [number 0] [operator |] [atom false];", + "}") + + TS("typescript_extend_operators", + "[keyword export] [keyword interface] [def UserModel] [keyword extends]", + " [type Sequelize].[type Model] [operator <] [type UserInstance], [type UserAttributes] [operator >] {", + " [property findById]: (", + " [variable userId]: [type number]", + " ) [operator =>] [type Promise] [operator <] [type Array] [operator <] { [property id], [property name] } [operator >>];", + " [property updateById]: (", + " [variable userId]: [type number],", + " [variable isActive]: [type boolean]", + " ) [operator =>] [type Promise] [operator <] [type AccountHolderNotificationPreferenceInstance] [operator >];", + " }") + + TS("typescript_interface_with_const", + "[keyword const] [def hello]: {", + " [property prop1][operator ?]: [type string];", + " [property prop2][operator ?]: [type string];", + "} [operator =] {};") + + TS("typescript_double_extend", + "[keyword export] [keyword interface] [def UserAttributes] {", + " [property id][operator ?]: [type number];", + " [property createdAt][operator ?]: [type Date];", + "}", + "[keyword export] [keyword interface] [def UserInstance] [keyword extends] [type Sequelize].[type Instance][operator <][type UserAttributes][operator >], [type UserAttributes] {", + " [property id]: [type number];", + " [property createdAt]: [type Date];", + "}"); + + TS("typescript_index_signature", + "[keyword interface] [def A] {", + " [[ [variable prop]: [type string] ]]: [type any];", + " [property prop1]: [type any];", + "}"); + + TS("typescript_generic_class", + "[keyword class] [def Foo][operator <][type T][operator >] {", + " [property bar]() {}", + " [property foo](): [type Foo] {}", + "}") + + TS("typescript_type_when_keyword", + "[keyword export] [keyword type] [type AB] [operator =] [type A] [operator |] [type B];", + "[keyword type] [type Flags] [operator =] {", + " [property p1]: [type string];", + " [property p2]: [type boolean];", + "};") + + TS("typescript_type_when_not_keyword", + "[keyword class] [def HasType] {", + " [property type]: [type string];", + " [property constructor]([def type]: [type string]) {", + " [keyword this].[property type] [operator =] [variable-2 type];", + " }", + " [property setType]({ [def type] }: { [property type]: [type string]; }) {", + " [keyword this].[property type] [operator =] [variable-2 type];", + " }", + "}") + + TS("typescript_function_generics", + "[keyword function] [def a]() {}", + "[keyword function] [def b][operator <][type IA] [keyword extends] [type object], [type IB] [keyword extends] [type object][operator >]() {}", + "[keyword function] [def c]() {}") + + TS("typescript_complex_return_type", + "[keyword function] [def A]() {", + " [keyword return] [keyword this].[property property];", + "}", + "[keyword function] [def B](): [type Promise][operator <]{ [[ [variable key]: [type string] ]]: [type any] } [operator |] [atom null][operator >] {", + " [keyword return] [keyword this].[property property];", + "}") + + TS("typescript_complex_type_casting", + "[keyword const] [def giftpay] [operator =] [variable config].[property get]([string 'giftpay']) [keyword as] { [[ [variable platformUuid]: [type string] ]]: { [property version]: [type number]; [property apiCode]: [type string]; } };") + + TS("typescript_keyof", + "[keyword function] [def x][operator <][type T] [keyword extends] [keyword keyof] [type X][operator >]([def a]: [type T]) {", + " [keyword return]") + + TS("typescript_new_typeargs", + "[keyword let] [def x] [operator =] [keyword new] [variable Map][operator <][type string], [type Date][operator >]([string-2 `foo${][variable bar][string-2 }`])") + + TS("modifiers", + "[keyword class] [def Foo] {", + " [keyword public] [keyword abstract] [property bar]() {}", + " [property constructor]([keyword readonly] [keyword private] [def x]) {}", + "}") + + TS("arrow prop", + "({[property a]: [def p] [operator =>] [variable-2 p]})") + + TS("generic in function call", + "[keyword this].[property a][operator <][type Type][operator >]([variable foo]);", + "[keyword this].[property a][operator <][variable Type][operator >][variable foo];") + + TS("type guard", + "[keyword class] [def Appler] {", + " [keyword static] [property assertApple]([def fruit]: [type Fruit]): [variable-2 fruit] [keyword is] [type Apple] {", + " [keyword if] ([operator !]([variable-2 fruit] [keyword instanceof] [variable Apple]))", + " [keyword throw] [keyword new] [variable Error]();", + " }", + "}") + + TS("type as variable", + "[variable type] [operator =] [variable x] [keyword as] [type Bar];"); + + TS("enum body", + "[keyword export] [keyword const] [keyword enum] [def CodeInspectionResultType] {", + " [def ERROR] [operator =] [string 'problem_type_error'],", + " [def WARNING] [operator =] [string 'problem_type_warning'],", + " [def META],", + "}") + + TS("parenthesized type", + "[keyword class] [def Foo] {", + " [property x] [operator =] [keyword new] [variable A][operator <][type B], [type string][operator |](() [operator =>] [type void])[operator >]();", + " [keyword private] [property bar]();", + "}") + + TS("abstract class", + "[keyword export] [keyword abstract] [keyword class] [def Foo] {}") + + TS("interface without semicolons", + "[keyword interface] [def Foo] {", + " [property greet]([def x]: [type int]): [type blah]", + " [property bar]: [type void]", + "}") + + var jsonld_mode = CodeMirror.getMode( + {indentUnit: 2}, + {name: "javascript", jsonld: true} + ); + function LD(name) { + test.mode(name, jsonld_mode, Array.prototype.slice.call(arguments, 1)); + } + + LD("json_ld_keywords", + '{', + ' [meta "@context"]: {', + ' [meta "@base"]: [string "http://example.com"],', + ' [meta "@vocab"]: [string "http://xmlns.com/foaf/0.1/"],', + ' [property "likesFlavor"]: {', + ' [meta "@container"]: [meta "@list"]', + ' [meta "@reverse"]: [string "@beFavoriteOf"]', + ' },', + ' [property "nick"]: { [meta "@container"]: [meta "@set"] },', + ' [property "nick"]: { [meta "@container"]: [meta "@index"] }', + ' },', + ' [meta "@graph"]: [[ {', + ' [meta "@id"]: [string "http://dbpedia.org/resource/John_Lennon"],', + ' [property "name"]: [string "John Lennon"],', + ' [property "modified"]: {', + ' [meta "@value"]: [string "2010-05-29T14:17:39+02:00"],', + ' [meta "@type"]: [string "http://www.w3.org/2001/XMLSchema#dateTime"]', + ' }', + ' } ]]', + '}'); + + LD("json_ld_fake", + '{', + ' [property "@fake"]: [string "@fake"],', + ' [property "@contextual"]: [string "@identifier"],', + ' [property "user@domain.com"]: [string "@graphical"],', + ' [property "@ID"]: [string "@@ID"]', + '}'); +})(); diff --git a/devtools/client/shared/sourceeditor/test/codemirror/mode_test.css b/devtools/client/shared/sourceeditor/test/codemirror/mode_test.css new file mode 100644 index 0000000000..f83271b4e2 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/mode_test.css @@ -0,0 +1,23 @@ +.mt-output .mt-token { + border: 1px solid #ddd; + white-space: pre; + font-family: "Consolas", monospace; + text-align: center; +} + +.mt-output .mt-style { + font-size: x-small; +} + +.mt-output .mt-state { + font-size: x-small; + vertical-align: top; +} + +.mt-output .mt-state-row { + display: none; +} + +.mt-state-unhide .mt-output .mt-state-row { + display: table-row; +} diff --git a/devtools/client/shared/sourceeditor/test/codemirror/mode_test.js b/devtools/client/shared/sourceeditor/test/codemirror/mode_test.js new file mode 100644 index 0000000000..e7c0cf92ae --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/mode_test.js @@ -0,0 +1,193 @@ +/** + * Helper to test CodeMirror highlighting modes. It pretty prints output of the + * highlighter and can check against expected styles. + * + * Mode tests are registered by calling test.mode(testName, mode, + * tokens), where mode is a mode object as returned by + * CodeMirror.getMode, and tokens is an array of lines that make up + * the test. + * + * These lines are strings, in which styled stretches of code are + * enclosed in brackets `[]`, and prefixed by their style. For + * example, `[keyword if]`. Brackets in the code itself must be + * duplicated to prevent them from being interpreted as token + * boundaries. For example `a[[i]]` for `a[i]`. If a token has + * multiple styles, the styles must be separated by ampersands, for + * example `[tag&error </hmtl>]`. + * + * See the test.js files in the css, markdown, gfm, and stex mode + * directories for examples. + */ +(function() { + function findSingle(str, pos, ch) { + for (;;) { + var found = str.indexOf(ch, pos); + if (found == -1) return null; + if (str.charAt(found + 1) != ch) return found; + pos = found + 2; + } + } + + var styleName = /[\w&-_]+/g; + function parseTokens(strs) { + var tokens = [], plain = ""; + for (var i = 0; i < strs.length; ++i) { + if (i) plain += "\n"; + var str = strs[i], pos = 0; + while (pos < str.length) { + var style = null, text; + if (str.charAt(pos) == "[" && str.charAt(pos+1) != "[") { + styleName.lastIndex = pos + 1; + var m = styleName.exec(str); + style = m[0].replace(/&/g, " "); + var textStart = pos + style.length + 2; + var end = findSingle(str, textStart, "]"); + if (end == null) throw new Error("Unterminated token at " + pos + " in '" + str + "'" + style); + text = str.slice(textStart, end); + pos = end + 1; + } else { + var end = findSingle(str, pos, "["); + if (end == null) end = str.length; + text = str.slice(pos, end); + pos = end; + } + text = text.replace(/\[\[|\]\]/g, function(s) {return s.charAt(0);}); + tokens.push({style: style, text: text}); + plain += text; + } + } + return {tokens: tokens, plain: plain}; + } + + test.mode = function(name, mode, tokens, modeName) { + var data = parseTokens(tokens); + return test((modeName || mode.name) + "_" + name, function() { + return compare(data.plain, data.tokens, mode); + }); + }; + + function esc(str) { + return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'"); + } + + function compare(text, expected, mode) { + + var expectedOutput = []; + for (var i = 0; i < expected.length; ++i) { + var sty = expected[i].style; + if (sty && sty.indexOf(" ")) sty = sty.split(' ').sort().join(' '); + expectedOutput.push({style: sty, text: expected[i].text}); + } + + var observedOutput = highlight(text, mode); + + var s = ""; + var diff = highlightOutputsDifferent(expectedOutput, observedOutput); + if (diff != null) { + s += '<div class="mt-test mt-fail">'; + s += '<pre>' + esc(text) + '</pre>'; + s += '<div class="cm-s-default">'; + s += 'expected:'; + s += prettyPrintOutputTable(expectedOutput, diff); + s += 'observed: [<a onclick="this.parentElement.className+=\' mt-state-unhide\'">display states</a>]'; + s += prettyPrintOutputTable(observedOutput, diff); + s += '</div>'; + s += '</div>'; + } + if (observedOutput.indentFailures) { + for (var i = 0; i < observedOutput.indentFailures.length; i++) + s += "<div class='mt-test mt-fail'>" + esc(observedOutput.indentFailures[i]) + "</div>"; + } + if (s) throw new Failure(s); + } + + function stringify(obj) { + function replacer(key, obj) { + if (typeof obj == "function") { + var m = obj.toString().match(/function\s*[^\s(]*/); + return m ? m[0] : "function"; + } + return obj; + } + if (window.JSON && JSON.stringify) + return JSON.stringify(obj, replacer, 2); + return "[unsupported]"; // Fail safely if no native JSON. + } + + function highlight(string, mode) { + var state = mode.startState(); + + var lines = string.replace(/\r\n/g,'\n').split('\n'); + var st = [], pos = 0; + for (var i = 0; i < lines.length; ++i) { + var line = lines[i], newLine = true; + if (mode.indent) { + var ws = line.match(/^\s*/)[0]; + var indent = mode.indent(state, line.slice(ws.length), line); + if (indent != CodeMirror.Pass && indent != ws.length) + (st.indentFailures || (st.indentFailures = [])).push( + "Indentation of line " + (i + 1) + " is " + indent + " (expected " + ws.length + ")"); + } + var stream = new CodeMirror.StringStream(line, 4, { + lookAhead: function(n) { return lines[i + n] } + }); + if (line == "" && mode.blankLine) mode.blankLine(state); + /* Start copied code from CodeMirror.highlight */ + while (!stream.eol()) { + for (var j = 0; j < 10 && stream.start >= stream.pos; j++) + var compare = mode.token(stream, state); + if (j == 10) + throw new Failure("Failed to advance the stream." + stream.string + " " + stream.pos); + var substr = stream.current(); + if (compare && compare.indexOf(" ") > -1) compare = compare.split(' ').sort().join(' '); + stream.start = stream.pos; + if (pos && st[pos-1].style == compare && !newLine) { + st[pos-1].text += substr; + } else if (substr) { + st[pos++] = {style: compare, text: substr, state: stringify(state)}; + } + // Give up when line is ridiculously long + if (stream.pos > 5000) { + st[pos++] = {style: null, text: this.text.slice(stream.pos)}; + break; + } + newLine = false; + } + } + + return st; + } + + function highlightOutputsDifferent(o1, o2) { + var minLen = Math.min(o1.length, o2.length); + for (var i = 0; i < minLen; ++i) + if (o1[i].style != o2[i].style || o1[i].text != o2[i].text) return i; + if (o1.length > minLen || o2.length > minLen) return minLen; + } + + function prettyPrintOutputTable(output, diffAt) { + var s = '<table class="mt-output">'; + s += '<tr>'; + for (var i = 0; i < output.length; ++i) { + var style = output[i].style, val = output[i].text; + s += + '<td class="mt-token"' + (i == diffAt ? " style='background: pink'" : "") + '>' + + '<span class="cm-' + esc(String(style)) + '">' + + esc(val.replace(/ /g,'\xb7')) + // · MIDDLE DOT + '</span>' + + '</td>'; + } + s += '</tr><tr>'; + for (var i = 0; i < output.length; ++i) { + s += '<td class="mt-style"><span>' + (output[i].style || null) + '</span></td>'; + } + if(output[0].state) { + s += '</tr><tr class="mt-state-row" title="State AFTER each token">'; + for (var i = 0; i < output.length; ++i) { + s += '<td class="mt-state"><pre>' + esc(output[i].state) + '</pre></td>'; + } + } + s += '</tr></table>'; + return s; + } +})(); diff --git a/devtools/client/shared/sourceeditor/test/codemirror/multi_test.js b/devtools/client/shared/sourceeditor/test/codemirror/multi_test.js new file mode 100644 index 0000000000..cc042f7399 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/multi_test.js @@ -0,0 +1,295 @@ +(function() { + namespace = "multi_"; + + function hasSelections(cm) { + var sels = cm.listSelections(); + var given = (arguments.length - 1) / 4; + if (sels.length != given) + throw new Failure("expected " + given + " selections, found " + sels.length); + for (var i = 0, p = 1; i < given; i++, p += 4) { + var anchor = Pos(arguments[p], arguments[p + 1]); + var head = Pos(arguments[p + 2], arguments[p + 3]); + eqCharPos(sels[i].anchor, anchor, "anchor of selection " + i); + eqCharPos(sels[i].head, head, "head of selection " + i); + } + } + function hasCursors(cm) { + var sels = cm.listSelections(); + var given = (arguments.length - 1) / 2; + if (sels.length != given) + throw new Failure("expected " + given + " selections, found " + sels.length); + for (var i = 0, p = 1; i < given; i++, p += 2) { + eqCursorPos(sels[i].anchor, sels[i].head, "something selected for " + i); + var head = Pos(arguments[p], arguments[p + 1]); + eqCharPos(sels[i].head, head, "selection " + i); + } + } + + testCM("getSelection", function(cm) { + select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)}, {anchor: Pos(2, 2), head: Pos(2, 0)}); + eq(cm.getSelection(), "1234\n56\n90"); + eq(cm.getSelection(false).join("|"), "1234|56|90"); + eq(cm.getSelections().join("|"), "1234\n56|90"); + }, {value: "1234\n5678\n90"}); + + testCM("setSelection", function(cm) { + select(cm, Pos(3, 0), Pos(0, 0), {anchor: Pos(2, 5), head: Pos(1, 0)}); + hasSelections(cm, 0, 0, 0, 0, + 2, 5, 1, 0, + 3, 0, 3, 0); + cm.setSelection(Pos(1, 2), Pos(1, 1)); + hasSelections(cm, 1, 2, 1, 1); + select(cm, {anchor: Pos(1, 1), head: Pos(2, 4)}, + {anchor: Pos(0, 0), head: Pos(1, 3)}, + Pos(3, 0), Pos(2, 2)); + hasSelections(cm, 0, 0, 2, 4, + 3, 0, 3, 0); + cm.setSelections([{anchor: Pos(0, 1), head: Pos(0, 2)}, + {anchor: Pos(1, 1), head: Pos(1, 2)}, + {anchor: Pos(2, 1), head: Pos(2, 2)}], 1); + eqCharPos(cm.getCursor("head"), Pos(1, 2)); + eqCharPos(cm.getCursor("anchor"), Pos(1, 1)); + eqCharPos(cm.getCursor("from"), Pos(1, 1)); + eqCharPos(cm.getCursor("to"), Pos(1, 2)); + cm.setCursor(Pos(1, 1)); + hasCursors(cm, 1, 1); + }, {value: "abcde\nabcde\nabcde\n"}); + + testCM("somethingSelected", function(cm) { + select(cm, Pos(0, 1), {anchor: Pos(0, 3), head: Pos(0, 5)}); + eq(cm.somethingSelected(), true); + select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5)); + eq(cm.somethingSelected(), false); + }, {value: "123456789"}); + + testCM("extendSelection", function(cm) { + select(cm, Pos(0, 1), Pos(1, 1), Pos(2, 1)); + cm.setExtending(true); + cm.extendSelections([Pos(0, 2), Pos(1, 0), Pos(2, 3)]); + hasSelections(cm, 0, 1, 0, 2, + 1, 1, 1, 0, + 2, 1, 2, 3); + cm.extendSelection(Pos(2, 4), Pos(2, 0)); + hasSelections(cm, 2, 4, 2, 0); + }, {value: "1234\n1234\n1234"}); + + testCM("addSelection", function(cm) { + select(cm, Pos(0, 1), Pos(1, 1)); + cm.addSelection(Pos(0, 0), Pos(0, 4)); + hasSelections(cm, 0, 0, 0, 4, + 1, 1, 1, 1); + cm.addSelection(Pos(2, 2)); + hasSelections(cm, 0, 0, 0, 4, + 1, 1, 1, 1, + 2, 2, 2, 2); + }, {value: "1234\n1234\n1234"}); + + testCM("replaceSelection", function(cm) { + var selections = [{anchor: Pos(0, 0), head: Pos(0, 1)}, + {anchor: Pos(0, 2), head: Pos(0, 3)}, + {anchor: Pos(0, 4), head: Pos(0, 5)}, + {anchor: Pos(2, 1), head: Pos(2, 4)}, + {anchor: Pos(2, 5), head: Pos(2, 6)}]; + var val = "123456\n123456\n123456"; + cm.setValue(val); + cm.setSelections(selections); + cm.replaceSelection("ab", "around"); + eq(cm.getValue(), "ab2ab4ab6\n123456\n1ab5ab"); + hasSelections(cm, 0, 0, 0, 2, + 0, 3, 0, 5, + 0, 6, 0, 8, + 2, 1, 2, 3, + 2, 4, 2, 6); + cm.setValue(val); + cm.setSelections(selections); + cm.replaceSelection("", "around"); + eq(cm.getValue(), "246\n123456\n15"); + hasSelections(cm, 0, 0, 0, 0, + 0, 1, 0, 1, + 0, 2, 0, 2, + 2, 1, 2, 1, + 2, 2, 2, 2); + cm.setValue(val); + cm.setSelections(selections); + cm.replaceSelection("X\nY\nZ", "around"); + hasSelections(cm, 0, 0, 2, 1, + 2, 2, 4, 1, + 4, 2, 6, 1, + 8, 1, 10, 1, + 10, 2, 12, 1); + cm.replaceSelection("a", "around"); + hasSelections(cm, 0, 0, 0, 1, + 0, 2, 0, 3, + 0, 4, 0, 5, + 2, 1, 2, 2, + 2, 3, 2, 4); + cm.replaceSelection("xy", "start"); + hasSelections(cm, 0, 0, 0, 0, + 0, 3, 0, 3, + 0, 6, 0, 6, + 2, 1, 2, 1, + 2, 4, 2, 4); + cm.replaceSelection("z\nf"); + hasSelections(cm, 1, 1, 1, 1, + 2, 1, 2, 1, + 3, 1, 3, 1, + 6, 1, 6, 1, + 7, 1, 7, 1); + eq(cm.getValue(), "z\nfxy2z\nfxy4z\nfxy6\n123456\n1z\nfxy5z\nfxy"); + }); + + function select(cm) { + var sels = []; + for (var i = 1; i < arguments.length; i++) { + var arg = arguments[i]; + if (arg.head) sels.push(arg); + else sels.push({head: arg, anchor: arg}); + } + cm.setSelections(sels, sels.length - 1); + } + + testCM("indentSelection", function(cm) { + select(cm, Pos(0, 1), Pos(1, 1)); + cm.indentSelection(4); + eq(cm.getValue(), " foo\n bar\nbaz"); + + select(cm, Pos(0, 2), Pos(0, 3), Pos(0, 4)); + cm.indentSelection(-2); + eq(cm.getValue(), " foo\n bar\nbaz"); + + select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)}, + {anchor: Pos(1, 3), head: Pos(2, 0)}); + cm.indentSelection(-2); + eq(cm.getValue(), "foo\n bar\nbaz"); + }, {value: "foo\nbar\nbaz"}); + + testCM("killLine", function(cm) { + select(cm, Pos(0, 1), Pos(0, 2), Pos(1, 1)); + cm.execCommand("killLine"); + eq(cm.getValue(), "f\nb\nbaz"); + cm.execCommand("killLine"); + eq(cm.getValue(), "fbbaz"); + cm.setValue("foo\nbar\nbaz"); + select(cm, Pos(0, 1), {anchor: Pos(0, 2), head: Pos(2, 1)}); + cm.execCommand("killLine"); + eq(cm.getValue(), "faz"); + }, {value: "foo\nbar\nbaz"}); + + testCM("deleteLine", function(cm) { + select(cm, Pos(0, 0), + {head: Pos(0, 1), anchor: Pos(2, 0)}, + Pos(4, 0)); + cm.execCommand("deleteLine"); + eq(cm.getValue(), "4\n6\n7"); + select(cm, Pos(2, 1)); + cm.execCommand("deleteLine"); + eq(cm.getValue(), "4\n6\n"); + }, {value: "1\n2\n3\n4\n5\n6\n7"}); + + testCM("deleteH", function(cm) { + select(cm, Pos(0, 4), {anchor: Pos(1, 4), head: Pos(1, 5)}); + cm.execCommand("delWordAfter"); + eq(cm.getValue(), "foo bar baz\nabc ef ghi\n"); + cm.execCommand("delWordAfter"); + eq(cm.getValue(), "foo baz\nabc ghi\n"); + cm.execCommand("delCharBefore"); + cm.execCommand("delCharBefore"); + eq(cm.getValue(), "fo baz\nab ghi\n"); + select(cm, Pos(0, 3), Pos(0, 4), Pos(0, 5)); + cm.execCommand("delWordAfter"); + eq(cm.getValue(), "fo \nab ghi\n"); + }, {value: "foo bar baz\nabc def ghi\n"}); + + testCM("goLineStart", function(cm) { + select(cm, Pos(0, 2), Pos(0, 3), Pos(1, 1)); + cm.execCommand("goLineStart"); + hasCursors(cm, 0, 0, 1, 0); + select(cm, Pos(1, 1), Pos(0, 1)); + cm.setExtending(true); + cm.execCommand("goLineStart"); + hasSelections(cm, 0, 1, 0, 0, + 1, 1, 1, 0); + }, {value: "foo\nbar\nbaz"}); + + testCM("moveV", function(cm) { + select(cm, Pos(0, 2), Pos(1, 2)); + cm.execCommand("goLineDown"); + hasCursors(cm, 1, 2, 2, 2); + cm.execCommand("goLineUp"); + hasCursors(cm, 0, 2, 1, 2); + cm.execCommand("goLineUp"); + hasCursors(cm, 0, 0, 0, 2); + cm.execCommand("goLineUp"); + hasCursors(cm, 0, 0); + select(cm, Pos(0, 2), Pos(1, 2)); + cm.setExtending(true); + cm.execCommand("goLineDown"); + hasSelections(cm, 0, 2, 2, 2); + }, {value: "12345\n12345\n12345"}); + + testCM("moveH", function(cm) { + select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5), Pos(2, 3)); + cm.execCommand("goCharRight"); + hasCursors(cm, 0, 2, 0, 4, 1, 0, 2, 4); + cm.execCommand("goCharLeft"); + hasCursors(cm, 0, 1, 0, 3, 0, 5, 2, 3); + for (var i = 0; i < 15; i++) + cm.execCommand("goCharRight"); + hasCursors(cm, 2, 4, 2, 5); + }, {value: "12345\n12345\n12345"}); + + testCM("newlineAndIndent", function(cm) { + select(cm, Pos(0, 5), Pos(1, 5)); + cm.execCommand("newlineAndIndent"); + hasCursors(cm, 1, 2, 3, 2); + eq(cm.getValue(), "x = [\n 1];\ny = [\n 2];"); + cm.undo(); + eq(cm.getValue(), "x = [1];\ny = [2];"); + hasCursors(cm, 0, 5, 1, 5); + select(cm, Pos(0, 5), Pos(0, 6)); + cm.execCommand("newlineAndIndent"); + hasCursors(cm, 1, 2, 2, 0); + eq(cm.getValue(), "x = [\n 1\n];\ny = [2];"); + }, {value: "x = [1];\ny = [2];", mode: "javascript"}); + + testCM("goDocStartEnd", function(cm) { + select(cm, Pos(0, 1), Pos(1, 1)); + cm.execCommand("goDocStart"); + hasCursors(cm, 0, 0); + select(cm, Pos(0, 1), Pos(1, 1)); + cm.execCommand("goDocEnd"); + hasCursors(cm, 1, 3); + select(cm, Pos(0, 1), Pos(1, 1)); + cm.setExtending(true); + cm.execCommand("goDocEnd"); + hasSelections(cm, 1, 1, 1, 3); + }, {value: "abc\ndef"}); + + testCM("selectionHistory", function(cm) { + for (var i = 0; i < 3; ++i) + cm.addSelection(Pos(0, i * 2), Pos(0, i * 2 + 1)); + cm.execCommand("undoSelection"); + eq(cm.getSelection(), "1\n2"); + cm.execCommand("undoSelection"); + eq(cm.getSelection(), "1"); + cm.execCommand("undoSelection"); + eq(cm.getSelection(), ""); + eqCharPos(cm.getCursor(), Pos(0, 0)); + cm.execCommand("redoSelection"); + eq(cm.getSelection(), "1"); + cm.execCommand("redoSelection"); + eq(cm.getSelection(), "1\n2"); + cm.execCommand("redoSelection"); + eq(cm.getSelection(), "1\n2\n3"); + }, {value: "1 2 3"}); + + testCM("selectionsMayTouch", function(cm) { + select(cm, Pos(0, 0), Pos(0, 2)) + cm.setExtending(true); + cm.extendSelections([Pos(0, 2), Pos(0, 4)]) + hasSelections(cm, 0, 0, 0, 2, + 0, 2, 0, 4) + cm.extendSelections([Pos(0, 3), Pos(0, 4)]) + hasSelections(cm, 0, 0, 0, 4) + }, {selectionsMayTouch: true, value: "1234"}) +})(); diff --git a/devtools/client/shared/sourceeditor/test/codemirror/search_test.js b/devtools/client/shared/sourceeditor/test/codemirror/search_test.js new file mode 100644 index 0000000000..e3188de529 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/search_test.js @@ -0,0 +1,85 @@ +(function() { + "use strict"; + + function run(doc, query, options) { + var cursor = doc.getSearchCursor(query, null, options); + for (var i = 3; i < arguments.length; i += 4) { + var found = cursor.findNext(); + is(found, "not enough results (forward)"); + eqCharPos(Pos(arguments[i], arguments[i + 1]), cursor.from(), "from, forward, " + (i - 3) / 4); + eqCharPos(Pos(arguments[i + 2], arguments[i + 3]), cursor.to(), "to, forward, " + (i - 3) / 4); + } + is(!cursor.findNext(), "too many matches (forward)"); + for (var i = arguments.length - 4; i >= 3; i -= 4) { + var found = cursor.findPrevious(); + is(found, "not enough results (backwards)"); + eqCharPos(Pos(arguments[i], arguments[i + 1]), cursor.from(), "from, backwards, " + (i - 3) / 4); + eqCharPos(Pos(arguments[i + 2], arguments[i + 3]), cursor.to(), "to, backwards, " + (i - 3) / 4); + } + is(!cursor.findPrevious(), "too many matches (backwards)"); + } + + function test(name, f) { window.test("search_" + name, f) } + + test("simple", function() { + var doc = new CodeMirror.Doc("abcdefg\nabcdefg") + run(doc, "cde", false, 0, 2, 0, 5, 1, 2, 1, 5); + }); + + test("multiline", function() { + var doc = new CodeMirror.Doc("hallo\na\nb\ngoodbye") + run(doc, "llo\na\nb\ngoo", false, 0, 2, 3, 3); + run(doc, "blah\na\nb\nhall", false); + run(doc, "bye\nx\neye", false); + }); + + test("regexp", function() { + var doc = new CodeMirror.Doc("abcde\nabcde") + run(doc, /bcd/, false, 0, 1, 0, 4, 1, 1, 1, 4); + run(doc, /BCD/, false); + run(doc, /BCD/i, false, 0, 1, 0, 4, 1, 1, 1, 4); + }); + + test("regexpMultiline", function() { + var doc = new CodeMirror.Doc("foo foo\nbar\nbaz") + run(doc, /fo[^]*az/, {multiline: true}, 0, 0, 2, 3) + run(doc, /[oa][^u]/, {multiline: true}, 0, 1, 0, 3, 0, 5, 0, 7, 1, 1, 1, 3, 2, 1, 2, 3) + run(doc, /[a][^u]{2}/, {multiline: true}, 1, 1, 2, 0) + }) + + test("insensitive", function() { + var doc = new CodeMirror.Doc("hallo\nHALLO\noink\nhAllO") + run(doc, "All", false, 3, 1, 3, 4); + run(doc, "All", true, 0, 1, 0, 4, 1, 1, 1, 4, 3, 1, 3, 4); + }); + + test("multilineInsensitive", function() { + var doc = new CodeMirror.Doc("zie ginds komT\nDe Stoomboot\nuit Spanje weer aan") + run(doc, "komt\nde stoomboot\nuit", false); + run(doc, "komt\nde stoomboot\nuit", {caseFold: true}, 0, 10, 2, 3); + run(doc, "kOMt\ndE stOOmboot\nuiT", {caseFold: true}, 0, 10, 2, 3); + }); + + test("multilineInsensitiveSlow", function() { + var text = "" + for (var i = 0; i < 1000; i++) text += "foo\nbar\n" + var doc = new CodeMirror.Doc("find\nme\n" + text + "find\nme\n") + var t0 = +new Date + run(doc, /find\nme/, {multiline: true}, 0, 0, 1, 2, 2002, 0, 2003, 2) + is(+new Date - t0 < 100) + }) + + test("expandingCaseFold", function() { + var doc = new CodeMirror.Doc("<b>İİ İİ</b>\n<b>uu uu</b>") + run(doc, "</b>", true, 0, 8, 0, 12, 1, 8, 1, 12); + run(doc, "İİ", true, 0, 3, 0, 5, 0, 6, 0, 8); + }); + + test("normalize", function() { + if (!String.prototype.normalize) return + var doc = new CodeMirror.Doc("yılbaşı\n수 있을까\nLe taux d'humidité à London") + run(doc, "s", false, 0, 5, 0, 6) + run(doc, "이", false, 1, 2, 1, 3) + run(doc, "a", false, 0, 4, 0, 5, 2, 4, 2, 5, 2, 19, 2, 20) + }) +})(); diff --git a/devtools/client/shared/sourceeditor/test/codemirror/sublime_test.js b/devtools/client/shared/sourceeditor/test/codemirror/sublime_test.js new file mode 100644 index 0000000000..09bb951247 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/sublime_test.js @@ -0,0 +1,284 @@ +(function() { + "use strict"; + + var Pos = CodeMirror.Pos; + namespace = "sublime_"; + + function stTest(name) { + var actions = Array.prototype.slice.call(arguments, 1); + testCM(name, function(cm) { + for (var i = 0; i < actions.length; i++) { + var action = actions[i]; + if (typeof action == "string" && i == 0) + cm.setValue(action); + else if (typeof action == "string") + cm.execCommand(action); + else if (action instanceof Pos) + cm.setCursor(action); + else + action(cm); + } + }); + } + + function at(line, ch, msg) { + return function(cm) { + eq(cm.listSelections().length, 1); + eqCursorPos(cm.getCursor("head"), Pos(line, ch), msg); + eqCursorPos(cm.getCursor("anchor"), Pos(line, ch), msg); + }; + } + + function val(content, msg) { + return function(cm) { eq(cm.getValue(), content, msg); }; + } + + function argsToRanges(args) { + if (args.length % 4) throw new Error("Wrong number of arguments for ranges."); + var ranges = []; + for (var i = 0; i < args.length; i += 4) + ranges.push({anchor: Pos(args[i], args[i + 1]), + head: Pos(args[i + 2], args[i + 3])}); + return ranges; + } + + function setSel() { + var ranges = argsToRanges(arguments); + return function(cm) { cm.setSelections(ranges, 0); }; + } + + function hasSel() { + var ranges = argsToRanges(arguments); + return function(cm) { + var sels = cm.listSelections(); + if (sels.length != ranges.length) + throw new Failure("Expected " + ranges.length + " selections, but found " + sels.length); + for (var i = 0; i < sels.length; i++) { + eqCharPos(sels[i].anchor, ranges[i].anchor, "anchor " + i); + eqCharPos(sels[i].head, ranges[i].head, "head " + i); + } + }; + } + + stTest("bySubword", "the foo_bar DooDahBah \n a", + "goSubwordLeft", at(0, 0), + "goSubwordRight", at(0, 3), + "goSubwordRight", at(0, 7), + "goSubwordRight", at(0, 11), + "goSubwordRight", at(0, 15), + "goSubwordRight", at(0, 18), + "goSubwordRight", at(0, 21), + "goSubwordRight", at(0, 22), + "goSubwordRight", at(1, 0), + "goSubwordRight", at(1, 2), + "goSubwordRight", at(1, 2), + "goSubwordLeft", at(1, 1), + "goSubwordLeft", at(1, 0), + "goSubwordLeft", at(0, 22), + "goSubwordLeft", at(0, 18), + "goSubwordLeft", at(0, 15), + "goSubwordLeft", at(0, 12), + "goSubwordLeft", at(0, 8), + "goSubwordLeft", at(0, 4), + "goSubwordLeft", at(0, 0)); + + stTest("splitSelectionByLine", "abc\ndef\nghi", + setSel(0, 1, 2, 2), + "splitSelectionByLine", + hasSel(0, 1, 0, 3, + 1, 0, 1, 3, + 2, 0, 2, 2)); + + stTest("splitSelectionByLineMulti", "abc\ndef\nghi\njkl", + setSel(0, 1, 1, 1, + 1, 2, 3, 2, + 3, 3, 3, 3), + "splitSelectionByLine", + hasSel(0, 1, 0, 3, + 1, 0, 1, 1, + 1, 2, 1, 3, + 2, 0, 2, 3, + 3, 0, 3, 2, + 3, 3, 3, 3)); + + stTest("selectLine", "abc\ndef\nghi", + setSel(0, 1, 0, 1, + 2, 0, 2, 1), + "selectLine", + hasSel(0, 0, 1, 0, + 2, 0, 2, 3), + setSel(0, 1, 1, 0), + "selectLine", + hasSel(0, 0, 2, 0)); + + stTest("insertLineAfter", "abcde\nfghijkl\nmn", + setSel(0, 1, 0, 1, + 0, 3, 0, 3, + 1, 2, 1, 2, + 1, 3, 1, 5), "insertLineAfter", + hasSel(1, 0, 1, 0, + 3, 0, 3, 0), val("abcde\n\nfghijkl\n\nmn")); + + stTest("insertLineBefore", "abcde\nfghijkl\nmn", + setSel(0, 1, 0, 1, + 0, 3, 0, 3, + 1, 2, 1, 2, + 1, 3, 1, 5), "insertLineBefore", + hasSel(0, 0, 0, 0, + 2, 0, 2, 0), val("\nabcde\n\nfghijkl\nmn")); + + stTest("selectNextOccurrence", "a foo bar\nfoobar foo", + setSel(0, 2, 0, 5), + "selectNextOccurrence", hasSel(0, 2, 0, 5, + 1, 0, 1, 3), + "selectNextOccurrence", hasSel(0, 2, 0, 5, + 1, 0, 1, 3, + 1, 7, 1, 10), + "selectNextOccurrence", hasSel(0, 2, 0, 5, + 1, 0, 1, 3, + 1, 7, 1, 10), + Pos(0, 3), "selectNextOccurrence", hasSel(0, 2, 0, 5), + "selectNextOccurrence", hasSel(0, 2, 0, 5, + 1, 7, 1, 10), + setSel(0, 6, 0, 9), + "selectNextOccurrence", hasSel(0, 6, 0, 9, + 1, 3, 1, 6)); + + stTest("selectScope", "foo(a) {\n bar[1, 2];\n}", + "selectScope", hasSel(0, 0, 2, 1), + Pos(0, 4), "selectScope", hasSel(0, 4, 0, 5), + Pos(0, 5), "selectScope", hasSel(0, 4, 0, 5), + Pos(0, 6), "selectScope", hasSel(0, 0, 2, 1), + Pos(0, 8), "selectScope", hasSel(0, 8, 2, 0), + Pos(1, 2), "selectScope", hasSel(0, 8, 2, 0), + Pos(1, 6), "selectScope", hasSel(1, 6, 1, 10), + Pos(1, 9), "selectScope", hasSel(1, 6, 1, 10), + "selectScope", hasSel(0, 8, 2, 0), + "selectScope", hasSel(0, 0, 2, 1)); + + stTest("goToBracket", "foo(a) {\n bar[1, 2];\n}", + Pos(0, 0), "goToBracket", at(0, 0), + Pos(0, 4), "goToBracket", at(0, 5), "goToBracket", at(0, 4), + Pos(0, 8), "goToBracket", at(2, 0), "goToBracket", at(0, 8), + Pos(1, 2), "goToBracket", at(2, 0), + Pos(1, 7), "goToBracket", at(1, 10), "goToBracket", at(1, 6)); + + stTest("swapLine", "1\n2\n3---\n4\n5", + "swapLineDown", val("2\n1\n3---\n4\n5"), + "swapLineUp", val("1\n2\n3---\n4\n5"), + "swapLineUp", val("1\n2\n3---\n4\n5"), + Pos(4, 1), "swapLineDown", val("1\n2\n3---\n4\n5"), + setSel(0, 1, 0, 1, + 1, 0, 2, 0, + 2, 2, 2, 2), + "swapLineDown", val("4\n1\n2\n3---\n5"), + hasSel(1, 1, 1, 1, + 2, 0, 3, 0, + 3, 2, 3, 2), + "swapLineUp", val("1\n2\n3---\n4\n5"), + hasSel(0, 1, 0, 1, + 1, 0, 2, 0, + 2, 2, 2, 2)); + + stTest("swapLineEmptyBottomSel", "1\n2\n3", + setSel(0, 1, 1, 0), + "swapLineDown", val("2\n1\n3"), hasSel(1, 1, 2, 0), + "swapLineUp", val("1\n2\n3"), hasSel(0, 1, 1, 0), + "swapLineUp", val("1\n2\n3"), hasSel(0, 0, 0, 0)); + + stTest("swapLineUpFromEnd", "a\nb\nc", + Pos(2, 1), "swapLineUp", + hasSel(1, 1, 1, 1), val("a\nc\nb")); + + stTest("joinLines", "abc\ndef\nghi\njkl", + "joinLines", val("abc def\nghi\njkl"), at(0, 4), + "undo", + setSel(0, 2, 1, 1), "joinLines", + val("abc def ghi\njkl"), hasSel(0, 2, 0, 8), + "undo", + setSel(0, 1, 0, 1, + 1, 1, 1, 1, + 3, 1, 3, 1), "joinLines", + val("abc def ghi\njkl"), hasSel(0, 4, 0, 4, + 0, 8, 0, 8, + 1, 3, 1, 3)); + + stTest("duplicateLine", "abc\ndef\nghi", + Pos(1, 0), "duplicateLine", val("abc\ndef\ndef\nghi"), at(2, 0), + "undo", + setSel(0, 1, 0, 1, + 1, 1, 1, 1, + 2, 1, 2, 1), "duplicateLine", + val("abc\nabc\ndef\ndef\nghi\nghi"), hasSel(1, 1, 1, 1, + 3, 1, 3, 1, + 5, 1, 5, 1)); + stTest("duplicateLineSelection", "abcdef", + setSel(0, 1, 0, 1, + 0, 2, 0, 4, + 0, 5, 0, 5), + "duplicateLine", + val("abcdef\nabcdcdef\nabcdcdef"), hasSel(2, 1, 2, 1, + 2, 4, 2, 6, + 2, 7, 2, 7)); + + stTest("sortLines", "c\nb\na\nC\nB\nA", + "sortLines", val("A\nB\nC\na\nb\nc"), + "undo", + setSel(0, 0, 2, 0, + 3, 0, 5, 0), + "sortLines", val("b\nc\na\nB\nC\nA"), + hasSel(0, 0, 2, 0, + 3, 0, 5, 0), + "undo", + setSel(1, 0, 5, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA")); + + stTest("bookmarks", "abc\ndef\nghi\njkl", + Pos(0, 1), "toggleBookmark", + setSel(1, 1, 1, 2), "toggleBookmark", + setSel(2, 1, 2, 2), "toggleBookmark", + "nextBookmark", hasSel(0, 1, 0, 1), + "nextBookmark", hasSel(1, 1, 1, 2), + "nextBookmark", hasSel(2, 1, 2, 2), + "prevBookmark", hasSel(1, 1, 1, 2), + "prevBookmark", hasSel(0, 1, 0, 1), + "prevBookmark", hasSel(2, 1, 2, 2), + "prevBookmark", hasSel(1, 1, 1, 2), + "toggleBookmark", + "prevBookmark", hasSel(2, 1, 2, 2), + "prevBookmark", hasSel(0, 1, 0, 1), + "selectBookmarks", hasSel(0, 1, 0, 1, + 2, 1, 2, 2), + "clearBookmarks", + Pos(0, 0), "selectBookmarks", at(0, 0)); + + stTest("smartBackspace", " foo\n bar", + setSel(0, 2, 0, 2, 1, 4, 1, 4, 1, 6, 1, 6), "smartBackspace", + val("foo\n br")) + + stTest("upAndDowncaseAtCursor", "abc\ndef x\nghI", + setSel(0, 1, 0, 3, + 1, 1, 1, 1, + 1, 4, 1, 4), "upcaseAtCursor", + val("aBC\nDEF x\nghI"), hasSel(0, 1, 0, 3, + 1, 3, 1, 3, + 1, 4, 1, 4), + "downcaseAtCursor", + val("abc\ndef x\nghI"), hasSel(0, 1, 0, 3, + 1, 3, 1, 3, + 1, 4, 1, 4)); + + stTest("mark", "abc\ndef\nghi", + Pos(1, 1), "setSublimeMark", + Pos(2, 1), "selectToSublimeMark", hasSel(2, 1, 1, 1), + Pos(0, 1), "swapWithSublimeMark", at(1, 1), "swapWithSublimeMark", at(0, 1), + "deleteToSublimeMark", val("aef\nghi"), + "sublimeYank", val("abc\ndef\nghi"), at(1, 1)); + + stTest("findUnder", "foo foobar a", + "findUnder", hasSel(0, 4, 0, 7), + "findUnder", hasSel(0, 0, 0, 3), + "findUnderPrevious", hasSel(0, 4, 0, 7), + "findUnderPrevious", hasSel(0, 0, 0, 3), + Pos(0, 4), "findUnder", hasSel(0, 4, 0, 10), + Pos(0, 11), "findUnder", hasSel(0, 11, 0, 11)); +})(); diff --git a/devtools/client/shared/sourceeditor/test/codemirror/test.js b/devtools/client/shared/sourceeditor/test/codemirror/test.js new file mode 100644 index 0000000000..846d94e8a9 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/test.js @@ -0,0 +1,2686 @@ +var Pos = CodeMirror.Pos; + +CodeMirror.defaults.rtlMoveVisually = true; + +function forEach(arr, f) { + for (var i = 0, e = arr.length; i < e; ++i) f(arr[i], i); +} + +function addDoc(cm, width, height) { + var content = [], line = ""; + for (var i = 0; i < width; ++i) line += "x"; + for (var i = 0; i < height; ++i) content.push(line); + cm.setValue(content.join("\n")); +} + +function byClassName(elt, cls) { + if (elt.getElementsByClassName) return elt.getElementsByClassName(cls); + var found = [], re = new RegExp("\\b" + cls + "\\b"); + function search(elt) { + if (elt.nodeType == 3) return; + if (re.test(elt.className)) found.push(elt); + for (var i = 0, e = elt.childNodes.length; i < e; ++i) + search(elt.childNodes[i]); + } + search(elt); + return found; +} + +var ie_lt8 = /MSIE [1-7]\b/.test(navigator.userAgent); +var ie_lt9 = /MSIE [1-8]\b/.test(navigator.userAgent); +var mac = /Mac/.test(navigator.platform); +var phantom = /PhantomJS/.test(navigator.userAgent); +var opera = /Opera\/\./.test(navigator.userAgent); +var opera_version = opera && navigator.userAgent.match(/Version\/(\d+\.\d+)/); +if (opera_version) opera_version = Number(opera_version); +var opera_lt10 = opera && (!opera_version || opera_version < 10); + +namespace = "core_"; + +test("core_fromTextArea", function() { + var te = document.getElementById("code"); + te.value = "CONTENT"; + var cm = CodeMirror.fromTextArea(te); + is(!te.offsetHeight); + eq(cm.getValue(), "CONTENT"); + cm.setValue("foo\nbar"); + eq(cm.getValue(), "foo\nbar"); + cm.save(); + is(/^foo\r?\nbar$/.test(te.value)); + cm.setValue("xxx"); + cm.toTextArea(); + is(te.offsetHeight); + eq(te.value, "xxx"); +}); + +testCM("getRange", function(cm) { + eq(cm.getLine(0), "1234"); + eq(cm.getLine(1), "5678"); + eq(cm.getLine(2), null); + eq(cm.getLine(-1), null); + eq(cm.getRange(Pos(0, 0), Pos(0, 3)), "123"); + eq(cm.getRange(Pos(0, -1), Pos(0, 200)), "1234"); + eq(cm.getRange(Pos(0, 2), Pos(1, 2)), "34\n56"); + eq(cm.getRange(Pos(1, 2), Pos(100, 0)), "78"); +}, {value: "1234\n5678"}); + +testCM("replaceRange", function(cm) { + eq(cm.getValue(), ""); + cm.replaceRange("foo\n", Pos(0, 0)); + eq(cm.getValue(), "foo\n"); + cm.replaceRange("a\nb", Pos(0, 1)); + eq(cm.getValue(), "fa\nboo\n"); + eq(cm.lineCount(), 3); + cm.replaceRange("xyzzy", Pos(0, 0), Pos(1, 1)); + eq(cm.getValue(), "xyzzyoo\n"); + cm.replaceRange("abc", Pos(0, 0), Pos(10, 0)); + eq(cm.getValue(), "abc"); + eq(cm.lineCount(), 1); +}); + +testCM("selection", function(cm) { + cm.setSelection(Pos(0, 4), Pos(2, 2)); + is(cm.somethingSelected()); + eq(cm.getSelection(), "11\n222222\n33"); + eqCursorPos(cm.getCursor(false), Pos(2, 2)); + eqCursorPos(cm.getCursor(true), Pos(0, 4)); + cm.setSelection(Pos(1, 0)); + is(!cm.somethingSelected()); + eq(cm.getSelection(), ""); + eqCursorPos(cm.getCursor(true), Pos(1, 0)); + cm.replaceSelection("abc", "around"); + eq(cm.getSelection(), "abc"); + eq(cm.getValue(), "111111\nabc222222\n333333"); + cm.replaceSelection("def", "end"); + eq(cm.getSelection(), ""); + eqCursorPos(cm.getCursor(true), Pos(1, 3)); + cm.setCursor(Pos(2, 1)); + eqCursorPos(cm.getCursor(true), Pos(2, 1)); + cm.setCursor(1, 2); + eqCursorPos(cm.getCursor(true), Pos(1, 2)); +}, {value: "111111\n222222\n333333"}); + +testCM("extendSelection", function(cm) { + cm.setExtending(true); + addDoc(cm, 10, 10); + cm.setSelection(Pos(3, 5)); + eqCursorPos(cm.getCursor("head"), Pos(3, 5)); + eqCursorPos(cm.getCursor("anchor"), Pos(3, 5)); + cm.setSelection(Pos(2, 5), Pos(5, 5)); + eqCursorPos(cm.getCursor("head"), Pos(5, 5)); + eqCursorPos(cm.getCursor("anchor"), Pos(2, 5)); + eqCursorPos(cm.getCursor("start"), Pos(2, 5)); + eqCursorPos(cm.getCursor("end"), Pos(5, 5)); + cm.setSelection(Pos(5, 5), Pos(2, 5)); + eqCursorPos(cm.getCursor("head"), Pos(2, 5)); + eqCursorPos(cm.getCursor("anchor"), Pos(5, 5)); + eqCursorPos(cm.getCursor("start"), Pos(2, 5)); + eqCursorPos(cm.getCursor("end"), Pos(5, 5)); + cm.extendSelection(Pos(3, 2)); + eqCursorPos(cm.getCursor("head"), Pos(3, 2)); + eqCursorPos(cm.getCursor("anchor"), Pos(5, 5)); + cm.extendSelection(Pos(6, 2)); + eqCursorPos(cm.getCursor("head"), Pos(6, 2)); + eqCursorPos(cm.getCursor("anchor"), Pos(5, 5)); + cm.extendSelection(Pos(6, 3), Pos(6, 4)); + eqCursorPos(cm.getCursor("head"), Pos(6, 4)); + eqCursorPos(cm.getCursor("anchor"), Pos(5, 5)); + cm.extendSelection(Pos(0, 3), Pos(0, 4)); + eqCursorPos(cm.getCursor("head"), Pos(0, 3)); + eqCursorPos(cm.getCursor("anchor"), Pos(5, 5)); + cm.extendSelection(Pos(4, 5), Pos(6, 5)); + eqCursorPos(cm.getCursor("head"), Pos(6, 5)); + eqCursorPos(cm.getCursor("anchor"), Pos(4, 5)); + cm.setExtending(false); + cm.extendSelection(Pos(0, 3), Pos(0, 4)); + eqCursorPos(cm.getCursor("head"), Pos(0, 3)); + eqCursorPos(cm.getCursor("anchor"), Pos(0, 4)); +}); + +testCM("lines", function(cm) { + eq(cm.getLine(0), "111111"); + eq(cm.getLine(1), "222222"); + eq(cm.getLine(-1), null); + cm.replaceRange("", Pos(1, 0), Pos(2, 0)) + cm.replaceRange("abc", Pos(1, 0), Pos(1)); + eq(cm.getValue(), "111111\nabc"); +}, {value: "111111\n222222\n333333"}); + +testCM("indent", function(cm) { + cm.indentLine(1); + eq(cm.getLine(1), " blah();"); + cm.setOption("indentUnit", 8); + cm.indentLine(1); + eq(cm.getLine(1), "\tblah();"); + cm.setOption("indentUnit", 10); + cm.setOption("tabSize", 4); + cm.indentLine(1); + eq(cm.getLine(1), "\t\t blah();"); +}, {value: "if (x) {\nblah();\n}", indentUnit: 3, indentWithTabs: true, tabSize: 8}); + +testCM("indentByNumber", function(cm) { + cm.indentLine(0, 2); + eq(cm.getLine(0), " foo"); + cm.indentLine(0, -200); + eq(cm.getLine(0), "foo"); + cm.setSelection(Pos(0, 0), Pos(1, 2)); + cm.indentSelection(3); + eq(cm.getValue(), " foo\n bar\nbaz"); +}, {value: "foo\nbar\nbaz"}); + +test("core_defaults", function() { + var defsCopy = {}, defs = CodeMirror.defaults; + for (var opt in defs) defsCopy[opt] = defs[opt]; + defs.indentUnit = 5; + defs.value = "uu"; + defs.indentWithTabs = true; + defs.tabindex = 55; + var place = document.getElementById("testground"), cm = CodeMirror(place); + try { + eq(cm.getOption("indentUnit"), 5); + cm.setOption("indentUnit", 10); + eq(defs.indentUnit, 5); + eq(cm.getValue(), "uu"); + eq(cm.getOption("indentWithTabs"), true); + eq(cm.getInputField().tabIndex, 55); + } + finally { + for (var opt in defsCopy) defs[opt] = defsCopy[opt]; + place.removeChild(cm.getWrapperElement()); + } +}); + +testCM("lineInfo", function(cm) { + eq(cm.lineInfo(-1), null); + var mark = document.createElement("span"); + var lh = cm.setGutterMarker(1, "FOO", mark); + var info = cm.lineInfo(1); + eq(info.text, "222222"); + eq(info.gutterMarkers.FOO, mark); + eq(info.line, 1); + eq(cm.lineInfo(2).gutterMarkers, null); + cm.setGutterMarker(lh, "FOO", null); + eq(cm.lineInfo(1).gutterMarkers, null); + cm.setGutterMarker(1, "FOO", mark); + cm.setGutterMarker(0, "FOO", mark); + cm.clearGutter("FOO"); + eq(cm.lineInfo(0).gutterMarkers, null); + eq(cm.lineInfo(1).gutterMarkers, null); +}, {value: "111111\n222222\n333333"}); + +testCM("coords", function(cm) { + cm.setSize(null, 100); + addDoc(cm, 32, 200); + var top = cm.charCoords(Pos(0, 0)); + var bot = cm.charCoords(Pos(200, 30)); + is(top.left < bot.left); + is(top.top < bot.top); + is(top.top < top.bottom); + cm.scrollTo(null, 100); + var top2 = cm.charCoords(Pos(0, 0)); + is(top.top > top2.top); + eq(top.left, top2.left); +}); + +testCM("coordsChar", function(cm) { + addDoc(cm, 35, 70); + for (var i = 0; i < 2; ++i) { + var sys = i ? "local" : "page"; + for (var ch = 0; ch <= 35; ch += 5) { + for (var line = 0; line < 70; line += 5) { + cm.setCursor(line, ch); + var coords = cm.charCoords(Pos(line, ch), sys); + var pos = cm.coordsChar({left: coords.left + 1, top: coords.top + 1}, sys); + eqCharPos(pos, Pos(line, ch)); + } + } + } +}, {lineNumbers: true}); + +testCM("coordsCharBidi", function(cm) { + addDoc(cm, 35, 70); + // Put an rtl character into each line to trigger the bidi code path in coordsChar + cm.setValue(cm.getValue().replace(/\bx/g, 'و')) + for (var i = 0; i < 2; ++i) { + var sys = i ? "local" : "page"; + for (var ch = 2; ch <= 35; ch += 5) { + for (var line = 0; line < 70; line += 5) { + cm.setCursor(line, ch); + var coords = cm.charCoords(Pos(line, ch), sys); + var pos = cm.coordsChar({left: coords.left + 1, top: coords.top + 1}, sys); + eqCharPos(pos, Pos(line, ch)); + } + } + } +}, {lineNumbers: true}); + +testCM("badBidiOptimization", function(cm) { + var coords = cm.charCoords(Pos(0, 34)) + eqCharPos(cm.coordsChar({left: coords.right, top: coords.top + 2}), Pos(0, 34)) +}, {value: "----------<p class=\"title\">هل يمكنك اختيار مستوى قسط التأمين الذي ترغب بدفعه؟</p>"}) + +testCM("posFromIndex", function(cm) { + cm.setValue( + "This function should\n" + + "convert a zero based index\n" + + "to line and ch." + ); + + var examples = [ + { index: -1, line: 0, ch: 0 }, // <- Tests clipping + { index: 0, line: 0, ch: 0 }, + { index: 10, line: 0, ch: 10 }, + { index: 39, line: 1, ch: 18 }, + { index: 55, line: 2, ch: 7 }, + { index: 63, line: 2, ch: 15 }, + { index: 64, line: 2, ch: 15 } // <- Tests clipping + ]; + + for (var i = 0; i < examples.length; i++) { + var example = examples[i]; + var pos = cm.posFromIndex(example.index); + eq(pos.line, example.line); + eq(pos.ch, example.ch); + if (example.index >= 0 && example.index < 64) + eq(cm.indexFromPos(pos), example.index); + } +}); + +testCM("undo", function(cm) { + cm.replaceRange("def", Pos(0, 0), Pos(0)); + eq(cm.historySize().undo, 1); + cm.undo(); + eq(cm.getValue(), "abc"); + eq(cm.historySize().undo, 0); + eq(cm.historySize().redo, 1); + cm.redo(); + eq(cm.getValue(), "def"); + eq(cm.historySize().undo, 1); + eq(cm.historySize().redo, 0); + cm.setValue("1\n\n\n2"); + cm.clearHistory(); + eq(cm.historySize().undo, 0); + for (var i = 0; i < 20; ++i) { + cm.replaceRange("a", Pos(0, 0)); + cm.replaceRange("b", Pos(3, 0)); + } + eq(cm.historySize().undo, 40); + for (var i = 0; i < 40; ++i) + cm.undo(); + eq(cm.historySize().redo, 40); + eq(cm.getValue(), "1\n\n\n2"); +}, {value: "abc"}); + +testCM("undoDepth", function(cm) { + cm.replaceRange("d", Pos(0)); + cm.replaceRange("e", Pos(0)); + cm.replaceRange("f", Pos(0)); + cm.undo(); cm.undo(); cm.undo(); + eq(cm.getValue(), "abcd"); +}, {value: "abc", undoDepth: 4}); + +testCM("undoDoesntClearValue", function(cm) { + cm.undo(); + eq(cm.getValue(), "x"); +}, {value: "x"}); + +testCM("undoMultiLine", function(cm) { + cm.operation(function() { + cm.replaceRange("x", Pos(0, 0)); + cm.replaceRange("y", Pos(1, 0)); + }); + cm.undo(); + eq(cm.getValue(), "abc\ndef\nghi"); + cm.operation(function() { + cm.replaceRange("y", Pos(1, 0)); + cm.replaceRange("x", Pos(0, 0)); + }); + cm.undo(); + eq(cm.getValue(), "abc\ndef\nghi"); + cm.operation(function() { + cm.replaceRange("y", Pos(2, 0)); + cm.replaceRange("x", Pos(1, 0)); + cm.replaceRange("z", Pos(2, 0)); + }); + cm.undo(); + eq(cm.getValue(), "abc\ndef\nghi", 3); +}, {value: "abc\ndef\nghi"}); + +testCM("undoComposite", function(cm) { + cm.replaceRange("y", Pos(1)); + cm.operation(function() { + cm.replaceRange("x", Pos(0)); + cm.replaceRange("z", Pos(2)); + }); + eq(cm.getValue(), "ax\nby\ncz\n"); + cm.undo(); + eq(cm.getValue(), "a\nby\nc\n"); + cm.undo(); + eq(cm.getValue(), "a\nb\nc\n"); + cm.redo(); cm.redo(); + eq(cm.getValue(), "ax\nby\ncz\n"); +}, {value: "a\nb\nc\n"}); + +testCM("undoSelection", function(cm) { + cm.setSelection(Pos(0, 2), Pos(0, 4)); + cm.replaceSelection(""); + cm.setCursor(Pos(1, 0)); + cm.undo(); + eqCursorPos(cm.getCursor(true), Pos(0, 2)); + eqCursorPos(cm.getCursor(false), Pos(0, 4)); + cm.setCursor(Pos(1, 0)); + cm.redo(); + eqCursorPos(cm.getCursor(true), Pos(0, 2)); + eqCursorPos(cm.getCursor(false), Pos(0, 2)); +}, {value: "abcdefgh\n"}); + +testCM("undoSelectionAsBefore", function(cm) { + cm.replaceSelection("abc", "around"); + cm.undo(); + cm.redo(); + eq(cm.getSelection(), "abc"); +}); + +testCM("selectionChangeConfusesHistory", function(cm) { + cm.replaceSelection("abc", null, "dontmerge"); + cm.operation(function() { + cm.setCursor(Pos(0, 0)); + cm.replaceSelection("abc", null, "dontmerge"); + }); + eq(cm.historySize().undo, 2); +}); + +testCM("markTextSingleLine", function(cm) { + forEach([{a: 0, b: 1, c: "", f: 2, t: 5}, + {a: 0, b: 4, c: "", f: 0, t: 2}, + {a: 1, b: 2, c: "x", f: 3, t: 6}, + {a: 4, b: 5, c: "", f: 3, t: 5}, + {a: 4, b: 5, c: "xx", f: 3, t: 7}, + {a: 2, b: 5, c: "", f: 2, t: 3}, + {a: 2, b: 5, c: "abcd", f: 6, t: 7}, + {a: 2, b: 6, c: "x", f: null, t: null}, + {a: 3, b: 6, c: "", f: null, t: null}, + {a: 0, b: 9, c: "hallo", f: null, t: null}, + {a: 4, b: 6, c: "x", f: 3, t: 4}, + {a: 4, b: 8, c: "", f: 3, t: 4}, + {a: 6, b: 6, c: "a", f: 3, t: 6}, + {a: 8, b: 9, c: "", f: 3, t: 6}], function(test) { + cm.setValue("1234567890"); + var r = cm.markText(Pos(0, 3), Pos(0, 6), {className: "foo"}); + cm.replaceRange(test.c, Pos(0, test.a), Pos(0, test.b)); + var f = r.find(); + eq(f && f.from.ch, test.f); eq(f && f.to.ch, test.t); + }); +}); + +testCM("markTextMultiLine", function(cm) { + function p(v) { return v && Pos(v[0], v[1]); } + forEach([{a: [0, 0], b: [0, 5], c: "", f: [0, 0], t: [2, 5]}, + {a: [0, 0], b: [0, 5], c: "foo\n", f: [1, 0], t: [3, 5]}, + {a: [0, 1], b: [0, 10], c: "", f: [0, 1], t: [2, 5]}, + {a: [0, 5], b: [0, 6], c: "x", f: [0, 6], t: [2, 5]}, + {a: [0, 0], b: [1, 0], c: "", f: [0, 0], t: [1, 5]}, + {a: [0, 6], b: [2, 4], c: "", f: [0, 5], t: [0, 7]}, + {a: [0, 6], b: [2, 4], c: "aa", f: [0, 5], t: [0, 9]}, + {a: [1, 2], b: [1, 8], c: "", f: [0, 5], t: [2, 5]}, + {a: [0, 5], b: [2, 5], c: "xx", f: null, t: null}, + {a: [0, 0], b: [2, 10], c: "x", f: null, t: null}, + {a: [1, 5], b: [2, 5], c: "", f: [0, 5], t: [1, 5]}, + {a: [2, 0], b: [2, 3], c: "", f: [0, 5], t: [2, 2]}, + {a: [2, 5], b: [3, 0], c: "a\nb", f: [0, 5], t: [2, 5]}, + {a: [2, 3], b: [3, 0], c: "x", f: [0, 5], t: [2, 3]}, + {a: [1, 1], b: [1, 9], c: "1\n2\n3", f: [0, 5], t: [4, 5]}], function(test) { + cm.setValue("aaaaaaaaaa\nbbbbbbbbbb\ncccccccccc\ndddddddd\n"); + var r = cm.markText(Pos(0, 5), Pos(2, 5), + {className: "CodeMirror-matchingbracket"}); + cm.replaceRange(test.c, p(test.a), p(test.b)); + var f = r.find(); + eqCursorPos(f && f.from, p(test.f)); eqCursorPos(f && f.to, p(test.t)); + }); +}); + +testCM("markTextUndo", function(cm) { + var marker1, marker2, bookmark; + marker1 = cm.markText(Pos(0, 1), Pos(0, 3), + {className: "CodeMirror-matchingbracket"}); + marker2 = cm.markText(Pos(0, 0), Pos(2, 1), + {className: "CodeMirror-matchingbracket"}); + bookmark = cm.setBookmark(Pos(1, 5)); + cm.operation(function(){ + cm.replaceRange("foo", Pos(0, 2)); + cm.replaceRange("bar\nbaz\nbug\n", Pos(2, 0), Pos(3, 0)); + }); + var v1 = cm.getValue(); + cm.setValue(""); + eq(marker1.find(), null); eq(marker2.find(), null); eq(bookmark.find(), null); + cm.undo(); + eqCursorPos(bookmark.find(), Pos(1, 5), "still there"); + cm.undo(); + var m1Pos = marker1.find(), m2Pos = marker2.find(); + eqCursorPos(m1Pos.from, Pos(0, 1)); eqCursorPos(m1Pos.to, Pos(0, 3)); + eqCursorPos(m2Pos.from, Pos(0, 0)); eqCursorPos(m2Pos.to, Pos(2, 1)); + eqCursorPos(bookmark.find(), Pos(1, 5)); + cm.redo(); cm.redo(); + eq(bookmark.find(), null); + cm.undo(); + eqCursorPos(bookmark.find(), Pos(1, 5)); + eq(cm.getValue(), v1); +}, {value: "1234\n56789\n00\n"}); + +testCM("markTextStayGone", function(cm) { + var m1 = cm.markText(Pos(0, 0), Pos(0, 1)); + cm.replaceRange("hi", Pos(0, 2)); + m1.clear(); + cm.undo(); + eq(m1.find(), null); +}, {value: "hello"}); + +testCM("markTextAllowEmpty", function(cm) { + var m1 = cm.markText(Pos(0, 1), Pos(0, 2), {clearWhenEmpty: false}); + is(m1.find()); + cm.replaceRange("x", Pos(0, 0)); + is(m1.find()); + cm.replaceRange("y", Pos(0, 2)); + is(m1.find()); + cm.replaceRange("z", Pos(0, 3), Pos(0, 4)); + is(!m1.find()); + var m2 = cm.markText(Pos(0, 1), Pos(0, 2), {clearWhenEmpty: false, + inclusiveLeft: true, + inclusiveRight: true}); + cm.replaceRange("q", Pos(0, 1), Pos(0, 2)); + is(m2.find()); + cm.replaceRange("", Pos(0, 0), Pos(0, 3)); + is(!m2.find()); + var m3 = cm.markText(Pos(0, 1), Pos(0, 1), {clearWhenEmpty: false}); + cm.replaceRange("a", Pos(0, 3)); + is(m3.find()); + cm.replaceRange("b", Pos(0, 1)); + is(!m3.find()); +}, {value: "abcde"}); + +testCM("markTextStacked", function(cm) { + var m1 = cm.markText(Pos(0, 0), Pos(0, 0), {clearWhenEmpty: false}); + var m2 = cm.markText(Pos(0, 0), Pos(0, 0), {clearWhenEmpty: false}); + cm.replaceRange("B", Pos(0, 1)); + is(m1.find() && m2.find()); +}, {value: "A"}); + +testCM("undoPreservesNewMarks", function(cm) { + cm.markText(Pos(0, 3), Pos(0, 4)); + cm.markText(Pos(1, 1), Pos(1, 3)); + cm.replaceRange("", Pos(0, 3), Pos(3, 1)); + var mBefore = cm.markText(Pos(0, 0), Pos(0, 1)); + var mAfter = cm.markText(Pos(0, 5), Pos(0, 6)); + var mAround = cm.markText(Pos(0, 2), Pos(0, 4)); + cm.undo(); + eqCursorPos(mBefore.find().from, Pos(0, 0)); + eqCursorPos(mBefore.find().to, Pos(0, 1)); + eqCursorPos(mAfter.find().from, Pos(3, 3)); + eqCursorPos(mAfter.find().to, Pos(3, 4)); + eqCursorPos(mAround.find().from, Pos(0, 2)); + eqCursorPos(mAround.find().to, Pos(3, 2)); + var found = cm.findMarksAt(Pos(2, 2)); + eq(found.length, 1); + eq(found[0], mAround); +}, {value: "aaaa\nbbbb\ncccc\ndddd"}); + +testCM("markClearBetween", function(cm) { + cm.setValue("aaa\nbbb\nccc\nddd\n"); + cm.markText(Pos(0, 0), Pos(2)); + cm.replaceRange("aaa\nbbb\nccc", Pos(0, 0), Pos(2)); + eq(cm.findMarksAt(Pos(1, 1)).length, 0); +}); + +testCM("findMarksMiddle", function(cm) { + var mark = cm.markText(Pos(1, 1), Pos(3, 1)); + var found = cm.findMarks(Pos(2, 1), Pos(2, 2)); + eq(found.length, 1); + eq(found[0], mark); +}, {value: "line 0\nline 1\nline 2\nline 3"}); + +testCM("deleteSpanCollapsedInclusiveLeft", function(cm) { + var from = Pos(1, 0), to = Pos(1, 1); + var m = cm.markText(from, to, {collapsed: true, inclusiveLeft: true}); + // Delete collapsed span. + cm.replaceRange("", from, to); +}, {value: "abc\nX\ndef"}); + +testCM("markTextCSS", function(cm) { + function present() { + var spans = cm.display.lineDiv.getElementsByTagName("span"); + for (var i = 0; i < spans.length; i++) + if (spans[i].style.color && spans[i].textContent == "cdef") return true; + } + var m = cm.markText(Pos(0, 2), Pos(0, 6), {css: "color: cyan"}); + is(present()); + m.clear(); + is(!present()); +}, {value: "abcdefgh"}); + +testCM("markTextWithAttributes", function(cm) { + function present() { + var spans = cm.display.lineDiv.getElementsByTagName("span"); + for (var i = 0; i < spans.length; i++) + if (spans[i].getAttribute("label") == "label" && spans[i].textContent == "cdef") return true; + } + var m = cm.markText(Pos(0, 2), Pos(0, 6), {attributes: {label: "label"}}); + is(present()); + m.clear(); + is(!present()); +}, {value: "abcdefgh"}); + +testCM("bookmark", function(cm) { + function p(v) { return v && Pos(v[0], v[1]); } + forEach([{a: [1, 0], b: [1, 1], c: "", d: [1, 4]}, + {a: [1, 1], b: [1, 1], c: "xx", d: [1, 7]}, + {a: [1, 4], b: [1, 5], c: "ab", d: [1, 6]}, + {a: [1, 4], b: [1, 6], c: "", d: null}, + {a: [1, 5], b: [1, 6], c: "abc", d: [1, 5]}, + {a: [1, 6], b: [1, 8], c: "", d: [1, 5]}, + {a: [1, 4], b: [1, 4], c: "\n\n", d: [3, 1]}, + {bm: [1, 9], a: [1, 1], b: [1, 1], c: "\n", d: [2, 8]}], function(test) { + cm.setValue("1234567890\n1234567890\n1234567890"); + var b = cm.setBookmark(p(test.bm) || Pos(1, 5)); + cm.replaceRange(test.c, p(test.a), p(test.b)); + eqCursorPos(b.find(), p(test.d)); + }); +}); + +testCM("bookmarkInsertLeft", function(cm) { + var br = cm.setBookmark(Pos(0, 2), {insertLeft: false}); + var bl = cm.setBookmark(Pos(0, 2), {insertLeft: true}); + cm.setCursor(Pos(0, 2)); + cm.replaceSelection("hi"); + eqCursorPos(br.find(), Pos(0, 2)); + eqCursorPos(bl.find(), Pos(0, 4)); + cm.replaceRange("", Pos(0, 4), Pos(0, 5)); + cm.replaceRange("", Pos(0, 2), Pos(0, 4)); + cm.replaceRange("", Pos(0, 1), Pos(0, 2)); + // Verify that deleting next to bookmarks doesn't kill them + eqCursorPos(br.find(), Pos(0, 1)); + eqCursorPos(bl.find(), Pos(0, 1)); +}, {value: "abcdef"}); + +testCM("bookmarkCursor", function(cm) { + var pos01 = cm.cursorCoords(Pos(0, 1)), pos11 = cm.cursorCoords(Pos(1, 1)), + pos20 = cm.cursorCoords(Pos(2, 0)), pos30 = cm.cursorCoords(Pos(3, 0)), + pos41 = cm.cursorCoords(Pos(4, 1)); + cm.setBookmark(Pos(0, 1), {widget: document.createTextNode("←"), insertLeft: true}); + cm.setBookmark(Pos(2, 0), {widget: document.createTextNode("←"), insertLeft: true}); + cm.setBookmark(Pos(1, 1), {widget: document.createTextNode("→")}); + cm.setBookmark(Pos(3, 0), {widget: document.createTextNode("→")}); + var new01 = cm.cursorCoords(Pos(0, 1)), new11 = cm.cursorCoords(Pos(1, 1)), + new20 = cm.cursorCoords(Pos(2, 0)), new30 = cm.cursorCoords(Pos(3, 0)); + near(new01.left, pos01.left, 1); + near(new01.top, pos01.top, 1); + is(new11.left > pos11.left, "at right, middle of line"); + near(new11.top == pos11.top, 1); + near(new20.left, pos20.left, 1); + near(new20.top, pos20.top, 1); + is(new30.left > pos30.left, "at right, empty line"); + near(new30.top, pos30, 1); + cm.setBookmark(Pos(4, 0), {widget: document.createTextNode("→")}); + is(cm.cursorCoords(Pos(4, 1)).left > pos41.left, "single-char bug"); +}, {value: "foo\nbar\n\n\nx\ny"}); + +testCM("multiBookmarkCursor", function(cm) { + if (phantom) return; + var ms = [], m; + function add(insertLeft) { + for (var i = 0; i < 3; ++i) { + var node = document.createElement("span"); + node.innerHTML = "X"; + ms.push(cm.setBookmark(Pos(0, 1), {widget: node, insertLeft: insertLeft})); + } + } + var base1 = cm.cursorCoords(Pos(0, 1)).left, base4 = cm.cursorCoords(Pos(0, 4)).left; + add(true); + near(base1, cm.cursorCoords(Pos(0, 1)).left, 1); + while (m = ms.pop()) m.clear(); + add(false); + near(base4, cm.cursorCoords(Pos(0, 1)).left, 1); +}, {value: "abcdefg"}); + +testCM("getAllMarks", function(cm) { + addDoc(cm, 10, 10); + var m1 = cm.setBookmark(Pos(0, 2)); + var m2 = cm.markText(Pos(0, 2), Pos(3, 2)); + var m3 = cm.markText(Pos(1, 2), Pos(1, 8)); + var m4 = cm.markText(Pos(8, 0), Pos(9, 0)); + eq(cm.getAllMarks().length, 4); + m1.clear(); + m3.clear(); + eq(cm.getAllMarks().length, 2); +}); + +testCM("setValueClears", function(cm) { + cm.addLineClass(0, "wrap", "foo"); + var mark = cm.markText(Pos(0, 0), Pos(1, 1), {inclusiveLeft: true, inclusiveRight: true}); + cm.setValue("foo"); + is(!cm.lineInfo(0).wrapClass); + is(!mark.find()); +}, {value: "a\nb"}); + +testCM("bug577", function(cm) { + cm.setValue("a\nb"); + cm.clearHistory(); + cm.setValue("fooooo"); + cm.undo(); +}); + +testCM("scrollSnap", function(cm) { + cm.setSize(100, 100); + addDoc(cm, 200, 200); + cm.setCursor(Pos(100, 180)); + var info = cm.getScrollInfo(); + is(info.left > 0 && info.top > 0); + cm.setCursor(Pos(0, 0)); + info = cm.getScrollInfo(); + is(info.left == 0 && info.top == 0, "scrolled clean to top"); + cm.setCursor(Pos(100, 180)); + cm.setCursor(Pos(199, 0)); + info = cm.getScrollInfo(); + is(info.left == 0 && info.top + 2 > info.height - cm.getScrollerElement().clientHeight, "scrolled clean to bottom"); +}); + +testCM("scrollIntoView", function(cm) { + if (phantom) return; + function test(line, ch, msg) { + var pos = Pos(line, ch); + cm.scrollIntoView(pos); + var outer = cm.getWrapperElement().getBoundingClientRect(); + var box = cm.charCoords(pos, "window"); + is(box.left >= outer.left, msg + " (left)"); + is(box.right <= outer.right, msg + " (right)"); + is(box.top >= outer.top, msg + " (top)"); + is(box.bottom <= outer.bottom, msg + " (bottom)"); + } + addDoc(cm, 200, 200); + test(199, 199, "bottom right"); + test(0, 0, "top left"); + test(100, 100, "center"); + test(199, 0, "bottom left"); + test(0, 199, "top right"); + test(100, 100, "center again"); +}); + +testCM("scrollBackAndForth", function(cm) { + addDoc(cm, 1, 200); + cm.operation(function() { + cm.scrollIntoView(Pos(199, 0)); + cm.scrollIntoView(Pos(4, 0)); + }); + is(cm.getScrollInfo().top > 0); +}); + +testCM("selectAllNoScroll", function(cm) { + addDoc(cm, 1, 200); + cm.execCommand("selectAll"); + eq(cm.getScrollInfo().top, 0); + cm.setCursor(199); + cm.execCommand("selectAll"); + is(cm.getScrollInfo().top > 0); +}); + +testCM("selectionPos", function(cm) { + if (phantom || cm.getOption("inputStyle") != "textarea") return; + cm.setSize(100, 100); + addDoc(cm, 200, 100); + cm.setSelection(Pos(1, 100), Pos(98, 100)); + var lineWidth = cm.charCoords(Pos(0, 200), "local").left; + var lineHeight = (cm.charCoords(Pos(99)).top - cm.charCoords(Pos(0)).top) / 100; + cm.scrollTo(0, 0); + var selElt = byClassName(cm.getWrapperElement(), "CodeMirror-selected"); + var outer = cm.getWrapperElement().getBoundingClientRect(); + var sawMiddle, sawTop, sawBottom; + for (var i = 0, e = selElt.length; i < e; ++i) { + var box = selElt[i].getBoundingClientRect(); + var atLeft = box.left - outer.left < 30; + var width = box.right - box.left; + var atRight = box.right - outer.left > .8 * lineWidth; + if (atLeft && atRight) { + sawMiddle = true; + is(box.bottom - box.top > 90 * lineHeight, "middle high"); + is(width > .9 * lineWidth, "middle wide"); + } else { + is(width > .4 * lineWidth, "top/bot wide enough"); + is(width < .6 * lineWidth, "top/bot slim enough"); + if (atLeft) { + sawBottom = true; + is(box.top - outer.top > 96 * lineHeight, "bot below"); + } else if (atRight) { + sawTop = true; + is(box.top - outer.top < 2.1 * lineHeight, "top above"); + } + } + } + is(sawTop && sawBottom && sawMiddle, "all parts"); +}, null); + +testCM("restoreHistory", function(cm) { + cm.setValue("abc\ndef"); + cm.replaceRange("hello", Pos(1, 0), Pos(1)); + cm.replaceRange("goop", Pos(0, 0), Pos(0)); + cm.undo(); + var storedVal = cm.getValue(), storedHist = cm.getHistory(); + if (window.JSON) storedHist = JSON.parse(JSON.stringify(storedHist)); + eq(storedVal, "abc\nhello"); + cm.setValue(""); + cm.clearHistory(); + eq(cm.historySize().undo, 0); + cm.setValue(storedVal); + cm.setHistory(storedHist); + cm.redo(); + eq(cm.getValue(), "goop\nhello"); + cm.undo(); cm.undo(); + eq(cm.getValue(), "abc\ndef"); +}); + +testCM("doubleScrollbar", function(cm) { + var dummy = document.body.appendChild(document.createElement("p")); + dummy.style.cssText = "height: 50px; overflow: scroll; width: 50px"; + var scrollbarWidth = dummy.offsetWidth + 1 - dummy.clientWidth; + document.body.removeChild(dummy); + if (scrollbarWidth < 2) return; + cm.setSize(null, 100); + addDoc(cm, 1, 300); + var wrap = cm.getWrapperElement(); + is(wrap.offsetWidth - byClassName(wrap, "CodeMirror-lines")[0].offsetWidth <= scrollbarWidth * 1.5); +}); + +testCM("weirdLinebreaks", function(cm) { + cm.setValue("foo\nbar\rbaz\r\nquux\n\rplop"); + is(cm.getValue(), "foo\nbar\nbaz\nquux\n\nplop"); + is(cm.lineCount(), 6); + cm.setValue("\n\n"); + is(cm.lineCount(), 3); +}); + +testCM("setSize", function(cm) { + cm.setSize(100, 100); + var wrap = cm.getWrapperElement(); + is(wrap.offsetWidth, 100); + is(wrap.offsetHeight, 100); + cm.setSize("100%", "3em"); + is(wrap.style.width, "100%"); + is(wrap.style.height, "3em"); + cm.setSize(null, 40); + is(wrap.style.width, "100%"); + is(wrap.style.height, "40px"); +}); + +function foldLines(cm, start, end, autoClear) { + return cm.markText(Pos(start, 0), Pos(end - 1), { + inclusiveLeft: true, + inclusiveRight: true, + collapsed: true, + clearOnEnter: autoClear + }); +} + +testCM("collapsedLines", function(cm) { + addDoc(cm, 4, 10); + var range = foldLines(cm, 4, 5), cleared = 0; + CodeMirror.on(range, "clear", function() {cleared++;}); + cm.setCursor(Pos(3, 0)); + CodeMirror.commands.goLineDown(cm); + eqCharPos(cm.getCursor(), Pos(5, 0)); + cm.replaceRange("abcdefg", Pos(3, 0), Pos(3)); + cm.setCursor(Pos(3, 6)); + CodeMirror.commands.goLineDown(cm); + eqCharPos(cm.getCursor(), Pos(5, 4)); + cm.replaceRange("ab", Pos(3, 0), Pos(3)); + cm.setCursor(Pos(3, 2)); + CodeMirror.commands.goLineDown(cm); + eqCharPos(cm.getCursor(), Pos(5, 2)); + cm.operation(function() {range.clear(); range.clear();}); + eq(cleared, 1); +}); + +testCM("collapsedRangeCoordsChar", function(cm) { + var pos_1_3 = cm.charCoords(Pos(1, 3)); + pos_1_3.left += 2; pos_1_3.top += 2; + var opts = {collapsed: true, inclusiveLeft: true, inclusiveRight: true}; + var m1 = cm.markText(Pos(0, 0), Pos(2, 0), opts); + eqCharPos(cm.coordsChar(pos_1_3), Pos(3, 3)); + m1.clear(); + var m1 = cm.markText(Pos(0, 0), Pos(1, 1), {collapsed: true, inclusiveLeft: true}); + var m2 = cm.markText(Pos(1, 1), Pos(2, 0), {collapsed: true, inclusiveRight: true}); + eqCharPos(cm.coordsChar(pos_1_3), Pos(3, 3)); + m1.clear(); m2.clear(); + var m1 = cm.markText(Pos(0, 0), Pos(1, 6), opts); + eqCharPos(cm.coordsChar(pos_1_3), Pos(3, 3)); +}, {value: "123456\nabcdef\nghijkl\nmnopqr\n"}); + +testCM("collapsedRangeBetweenLinesSelected", function(cm) { + if (cm.getOption("inputStyle") != "textarea") return; + var widget = document.createElement("span"); + widget.textContent = "\u2194"; + cm.markText(Pos(0, 3), Pos(1, 0), {replacedWith: widget}); + cm.setSelection(Pos(0, 3), Pos(1, 0)); + var selElts = byClassName(cm.getWrapperElement(), "CodeMirror-selected"); + for (var i = 0, w = 0; i < selElts.length; i++) + w += selElts[i].offsetWidth; + is(w > 0); +}, {value: "one\ntwo"}); + +testCM("randomCollapsedRanges", function(cm) { + addDoc(cm, 20, 500); + cm.operation(function() { + for (var i = 0; i < 200; i++) { + var start = Pos(Math.floor(Math.random() * 500), Math.floor(Math.random() * 20)); + if (i % 4) + try { cm.markText(start, Pos(start.line + 2, 1), {collapsed: true}); } + catch(e) { if (!/overlapping/.test(String(e))) throw e; } + else + cm.markText(start, Pos(start.line, start.ch + 4), {"className": "foo"}); + } + }); +}); + +testCM("hiddenLinesAutoUnfold", function(cm) { + var range = foldLines(cm, 1, 3, true), cleared = 0; + CodeMirror.on(range, "clear", function() {cleared++;}); + cm.setCursor(Pos(3, 0)); + eq(cleared, 0); + cm.execCommand("goCharLeft"); + eq(cleared, 1); + range = foldLines(cm, 1, 3, true); + CodeMirror.on(range, "clear", function() {cleared++;}); + eqCursorPos(cm.getCursor(), Pos(3, 0)); + cm.setCursor(Pos(0, 3)); + cm.execCommand("goCharRight"); + eq(cleared, 2); +}, {value: "abc\ndef\nghi\njkl"}); + +testCM("hiddenLinesSelectAll", function(cm) { // Issue #484 + addDoc(cm, 4, 20); + foldLines(cm, 0, 10); + foldLines(cm, 11, 20); + CodeMirror.commands.selectAll(cm); + eqCursorPos(cm.getCursor(true), Pos(10, 0)); + eqCursorPos(cm.getCursor(false), Pos(10, 4)); +}); + +testCM("clickFold", function(cm) { // Issue #5392 + cm.setValue("foo { bar }") + var widget = document.createElement("span") + widget.textContent = "<>" + cm.markText(Pos(0, 5), Pos(0, 10), {replacedWith: widget}) + var after = cm.charCoords(Pos(0, 10)) + var foundOn = cm.coordsChar({left: after.left - 1, top: after.top + 4}) + is(foundOn.ch <= 5 || foundOn.ch >= 10, "Position is not inside the folded range") +}) + +testCM("everythingFolded", function(cm) { + addDoc(cm, 2, 2); + function enterPress() { + cm.triggerOnKeyDown({type: "keydown", keyCode: 13, preventDefault: function(){}, stopPropagation: function(){}}); + } + var fold = foldLines(cm, 0, 2); + enterPress(); + eq(cm.getValue(), "xx\nxx"); + fold.clear(); + fold = foldLines(cm, 0, 2, true); + eq(fold.find(), null); + enterPress(); + eq(cm.getValue(), "\nxx\nxx"); +}); + +testCM("structuredFold", function(cm) { + if (phantom) return; + addDoc(cm, 4, 8); + var range = cm.markText(Pos(1, 2), Pos(6, 2), { + replacedWith: document.createTextNode("Q") + }); + cm.setCursor(0, 3); + CodeMirror.commands.goLineDown(cm); + eqCharPos(cm.getCursor(), Pos(6, 2)); + CodeMirror.commands.goCharLeft(cm); + eqCharPos(cm.getCursor(), Pos(1, 2)); + CodeMirror.commands.delCharAfter(cm); + eq(cm.getValue(), "xxxx\nxxxx\nxxxx"); + addDoc(cm, 4, 8); + range = cm.markText(Pos(1, 2), Pos(6, 2), { + replacedWith: document.createTextNode("M"), + clearOnEnter: true + }); + var cleared = 0; + CodeMirror.on(range, "clear", function(){++cleared;}); + cm.setCursor(0, 3); + CodeMirror.commands.goLineDown(cm); + eqCharPos(cm.getCursor(), Pos(6, 2)); + CodeMirror.commands.goCharLeft(cm); + eqCharPos(cm.getCursor(), Pos(6, 1)); + eq(cleared, 1); + range.clear(); + eq(cleared, 1); + range = cm.markText(Pos(1, 2), Pos(6, 2), { + replacedWith: document.createTextNode("Q"), + clearOnEnter: true + }); + range.clear(); + cm.setCursor(1, 2); + CodeMirror.commands.goCharRight(cm); + eqCharPos(cm.getCursor(), Pos(1, 3)); + range = cm.markText(Pos(2, 0), Pos(4, 4), { + replacedWith: document.createTextNode("M") + }); + cm.setCursor(1, 0); + CodeMirror.commands.goLineDown(cm); + eqCharPos(cm.getCursor(), Pos(2, 0)); +}, null); + +testCM("nestedFold", function(cm) { + addDoc(cm, 10, 3); + function fold(ll, cl, lr, cr) { + return cm.markText(Pos(ll, cl), Pos(lr, cr), {collapsed: true}); + } + var inner1 = fold(0, 6, 1, 3), inner2 = fold(0, 2, 1, 8), outer = fold(0, 1, 2, 3), inner0 = fold(0, 5, 0, 6); + cm.setCursor(0, 1); + CodeMirror.commands.goCharRight(cm); + eqCursorPos(cm.getCursor(), Pos(2, 3)); + inner0.clear(); + CodeMirror.commands.goCharLeft(cm); + eqCursorPos(cm.getCursor(), Pos(0, 1)); + outer.clear(); + CodeMirror.commands.goCharRight(cm); + eqCursorPos(cm.getCursor(), Pos(0, 2, "before")); + CodeMirror.commands.goCharRight(cm); + eqCursorPos(cm.getCursor(), Pos(1, 8)); + inner2.clear(); + CodeMirror.commands.goCharLeft(cm); + eqCursorPos(cm.getCursor(), Pos(1, 7, "after")); + cm.setCursor(0, 5); + CodeMirror.commands.goCharRight(cm); + eqCursorPos(cm.getCursor(), Pos(0, 6, "before")); + CodeMirror.commands.goCharRight(cm); + eqCursorPos(cm.getCursor(), Pos(1, 3)); +}); + +testCM("badNestedFold", function(cm) { + addDoc(cm, 4, 4); + cm.markText(Pos(0, 2), Pos(3, 2), {collapsed: true}); + var caught; + try {cm.markText(Pos(0, 1), Pos(0, 3), {collapsed: true});} + catch(e) {caught = e;} + is(caught instanceof Error, "no error"); + is(/overlap/i.test(caught.message), "wrong error"); +}); + +testCM("nestedFoldOnSide", function(cm) { + var m1 = cm.markText(Pos(0, 1), Pos(2, 1), {collapsed: true, inclusiveRight: true}); + var m2 = cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true}); + cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true}).clear(); + try { cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true, inclusiveLeft: true}); } + catch(e) { var caught = e; } + is(caught && /overlap/i.test(caught.message)); + var m3 = cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true}); + var m4 = cm.markText(Pos(2, 0), Pos(2, 1), {collapse: true, inclusiveRight: true}); + m1.clear(); m4.clear(); + m1 = cm.markText(Pos(0, 1), Pos(2, 1), {collapsed: true}); + cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true}).clear(); + try { cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true, inclusiveRight: true}); } + catch(e) { var caught = e; } + is(caught && /overlap/i.test(caught.message)); +}, {value: "ab\ncd\ef"}); + +testCM("editInFold", function(cm) { + addDoc(cm, 4, 6); + var m = cm.markText(Pos(1, 2), Pos(3, 2), {collapsed: true}); + cm.replaceRange("", Pos(0, 0), Pos(1, 3)); + cm.replaceRange("", Pos(2, 1), Pos(3, 3)); + cm.replaceRange("a\nb\nc\nd", Pos(0, 1), Pos(1, 0)); + cm.cursorCoords(Pos(0, 0)); +}); + +testCM("wrappingInlineWidget", function(cm) { + cm.setSize("11em"); + var w = document.createElement("span"); + w.style.color = "red"; + w.innerHTML = "one two three four"; + cm.markText(Pos(0, 6), Pos(0, 9), {replacedWith: w}); + var cur0 = cm.cursorCoords(Pos(0, 0)), cur1 = cm.cursorCoords(Pos(0, 10)); + is(cur0.top < cur1.top); + is(cur0.bottom < cur1.bottom); + var curL = cm.cursorCoords(Pos(0, 6)), curR = cm.cursorCoords(Pos(0, 9)); + eq(curL.top, cur0.top); + eq(curL.bottom, cur0.bottom); + eq(curR.top, cur1.top); + eq(curR.bottom, cur1.bottom); + cm.replaceRange("", Pos(0, 9), Pos(0)); + curR = cm.cursorCoords(Pos(0, 9)); + if (phantom) return; + eq(curR.top, cur1.top); + eq(curR.bottom, cur1.bottom); +}, {value: "1 2 3 xxx 4", lineWrapping: true}); + +testCM("showEmptyWidgetSpan", function(cm) { + var marker = cm.markText(Pos(0, 2), Pos(0, 2), { + clearWhenEmpty: false, + replacedWith: document.createTextNode("X") + }); + var text = cm.display.view[0].text; + eq(text.textContent || text.innerText, "abXc"); +}, {value: "abc"}); + +testCM("changedInlineWidget", function(cm) { + cm.setSize("10em"); + var w = document.createElement("span"); + w.innerHTML = "x"; + var m = cm.markText(Pos(0, 4), Pos(0, 5), {replacedWith: w}); + w.innerHTML = "and now the widget is really really long all of a sudden and a scrollbar is needed"; + m.changed(); + var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0]; + is(hScroll.scrollWidth > hScroll.clientWidth); +}, {value: "hello there"}); + +testCM("changedBookmark", function(cm) { + cm.setSize("10em"); + var w = document.createElement("span"); + w.innerHTML = "x"; + var m = cm.setBookmark(Pos(0, 4), {widget: w}); + w.innerHTML = "and now the widget is really really long all of a sudden and a scrollbar is needed"; + m.changed(); + var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0]; + is(hScroll.scrollWidth > hScroll.clientWidth); +}, {value: "abcdefg"}); + +testCM("inlineWidget", function(cm) { + var w = cm.setBookmark(Pos(0, 2), {widget: document.createTextNode("uu")}); + cm.setCursor(0, 2); + CodeMirror.commands.goLineDown(cm); + eqCharPos(cm.getCursor(), Pos(1, 4)); + cm.setCursor(0, 2); + cm.replaceSelection("hi"); + eqCharPos(w.find(), Pos(0, 2)); + cm.setCursor(0, 1); + cm.replaceSelection("ay"); + eqCharPos(w.find(), Pos(0, 4)); + eq(cm.getLine(0), "uayuhiuu"); +}, {value: "uuuu\nuuuuuu"}); + +testCM("wrappingAndResizing", function(cm) { + cm.setSize(null, "auto"); + cm.setOption("lineWrapping", true); + var wrap = cm.getWrapperElement(), h0 = wrap.offsetHeight; + var doc = "xxx xxx xxx xxx xxx"; + cm.setValue(doc); + for (var step = 10, w = cm.charCoords(Pos(0, 18), "div").right;; w += step) { + cm.setSize(w); + if (wrap.offsetHeight <= h0 * (opera_lt10 ? 1.2 : 1.5)) { + if (step == 10) { w -= 10; step = 1; } + else break; + } + } + // Ensure that putting the cursor at the end of the maximally long + // line doesn't cause wrapping to happen. + cm.setCursor(Pos(0, doc.length)); + eq(wrap.offsetHeight, h0); + cm.replaceSelection("x"); + is(wrap.offsetHeight > h0, "wrapping happens"); + // Now add a max-height and, in a document consisting of + // almost-wrapped lines, go over it so that a scrollbar appears. + cm.setValue(doc + "\n" + doc + "\n"); + cm.getScrollerElement().style.maxHeight = "100px"; + cm.replaceRange("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n!\n", Pos(2, 0)); + forEach([Pos(0, doc.length), Pos(0, doc.length - 1), + Pos(0, 0), Pos(1, doc.length), Pos(1, doc.length - 1)], + function(pos) { + var coords = cm.charCoords(pos); + eqCharPos(pos, cm.coordsChar({left: coords.left + 2, top: coords.top + 5})); + }); +}, null, ie_lt8); + +testCM("measureEndOfLine", function(cm) { + if (phantom) return; + cm.setSize(null, "auto"); + var inner = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild; + var lh = inner.offsetHeight; + for (var step = 10, w = cm.charCoords(Pos(0, 7), "div").right;; w += step) { + cm.setSize(w); + if (inner.offsetHeight < 2.5 * lh) { + if (step == 10) { w -= 10; step = 1; } + else break; + } + } + cm.setValue(cm.getValue() + "\n\n"); + var endPos = cm.charCoords(Pos(0, 18), "local"); + is(endPos.top > lh * .8, "not at top"); + is(endPos.left > w - 20, "at right"); + endPos = cm.charCoords(Pos(0, 18)); + eqCursorPos(cm.coordsChar({left: endPos.left, top: endPos.top + 5}), Pos(0, 18, "before")); + + var wrapPos = cm.cursorCoords(Pos(0, 9, "before")); + is(wrapPos.top < endPos.top, "wrapPos is actually in first line"); + eqCursorPos(cm.coordsChar({left: wrapPos.left + 10, top: wrapPos.top}), Pos(0, 9, "before")); +}, {mode: "text/html", value: "<!-- foo barrr -->", lineWrapping: true}, ie_lt8 || opera_lt10); + +testCM("measureWrappedEndOfLine", function(cm) { + if (phantom) return; + cm.setSize(null, "auto"); + var inner = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild; + var lh = inner.offsetHeight; + for (var step = 10, w = cm.charCoords(Pos(0, 7), "div").right;; w += step) { + cm.setSize(w); + if (inner.offsetHeight < 2.5 * lh) { + if (step == 10) { w -= 10; step = 1; } + else break; + } + } + for (var i = 0; i < 3; ++i) { + var endPos = cm.charCoords(Pos(0, 12)); // Next-to-last since last would wrap (#1862) + endPos.left += w; // Add width of editor just to be sure that we are behind last character + eqCursorPos(cm.coordsChar(endPos), Pos(0, 13, "before")); + endPos.left += w * 100; + eqCursorPos(cm.coordsChar(endPos), Pos(0, 13, "before")); + cm.setValue("0123456789abcابجابجابجابج"); + if (i == 1) { + var node = document.createElement("div"); + node.innerHTML = "hi"; node.style.height = "30px"; + cm.addLineWidget(0, node, {above: true}); + } + } +}, {mode: "text/html", value: "0123456789abcde0123456789", lineWrapping: true}, ie_lt8 || opera_lt10); + +testCM("measureEndOfLineBidi", function(cm) { + eqCursorPos(cm.coordsChar({left: 5000, top: cm.charCoords(Pos(0, 0)).top}), Pos(0, 8, "after")) +}, {value: "إإإإuuuuإإإإ"}) + +testCM("measureWrappedBidiLevel2", function(cm) { + cm.setSize(cm.charCoords(Pos(0, 6), "editor").right + 60) + var c9 = cm.charCoords(Pos(0, 9)) + eqCharPos(cm.coordsChar({left: c9.right - 1, top: c9.top + 1}), Pos(0, 9)) +}, {value: "foobar إإ إإ إإ إإ 555 بببببب", lineWrapping: true}) + +testCM("measureWrappedBeginOfLine", function(cm) { + if (phantom) return; + cm.setSize(null, "auto"); + var inner = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild; + var lh = inner.offsetHeight; + for (var step = 10, w = cm.charCoords(Pos(0, 7), "div").right;; w += step) { + cm.setSize(w); + if (inner.offsetHeight < 2.5 * lh) { + if (step == 10) { w -= 10; step = 1; } + else break; + } + } + var beginOfSecondLine = Pos(0, 13, "after"); + for (var i = 0; i < 2; ++i) { + var beginPos = cm.charCoords(Pos(0, 0)); + beginPos.left -= w; + eqCursorPos(cm.coordsChar(beginPos), Pos(0, 0, "after")); + beginPos = cm.cursorCoords(beginOfSecondLine); + beginPos.left = 0; + eqCursorPos(cm.coordsChar(beginPos), beginOfSecondLine); + cm.setValue("0123456789abcابجابجابجابج"); + beginOfSecondLine = Pos(0, 25, "before"); + } +}, {mode: "text/html", value: "0123456789abcde0123456789", lineWrapping: true}); + +testCM("scrollVerticallyAndHorizontally", function(cm) { + if (cm.getOption("inputStyle") != "textarea") return; + cm.setSize(100, 100); + addDoc(cm, 40, 40); + cm.setCursor(39); + var wrap = cm.getWrapperElement(), bar = byClassName(wrap, "CodeMirror-vscrollbar")[0]; + is(bar.offsetHeight < wrap.offsetHeight, "vertical scrollbar limited by horizontal one"); + var cursorBox = byClassName(wrap, "CodeMirror-cursor")[0].getBoundingClientRect(); + var editorBox = wrap.getBoundingClientRect(); + is(cursorBox.bottom < editorBox.top + cm.getScrollerElement().clientHeight, + "bottom line visible"); +}, {lineNumbers: true}); + +testCM("moveVstuck", function(cm) { + var lines = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild, h0 = lines.offsetHeight; + var val = "fooooooooooooooooooooooooo baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar\n"; + cm.setValue(val); + for (var w = cm.charCoords(Pos(0, 26), "div").right * 2.8;; w += 5) { + cm.setSize(w); + if (lines.offsetHeight <= 3.5 * h0) break; + } + cm.setCursor(Pos(0, val.length - 1)); + cm.moveV(-1, "line"); + eqCursorPos(cm.getCursor(), Pos(0, 27, "before")); + is(cm.cursorCoords(null, "local").top < h0, "cursor is in first visual line"); +}, {lineWrapping: true}, ie_lt8 || opera_lt10); + +testCM("collapseOnMove", function(cm) { + cm.setSelection(Pos(0, 1), Pos(2, 4)); + cm.execCommand("goLineUp"); + is(!cm.somethingSelected()); + eqCharPos(cm.getCursor(), Pos(0, 1)); + cm.setSelection(Pos(0, 1), Pos(2, 4)); + cm.execCommand("goPageDown"); + is(!cm.somethingSelected()); + eqCharPos(cm.getCursor(), Pos(2, 4)); + cm.execCommand("goLineUp"); + cm.execCommand("goLineUp"); + eqCharPos(cm.getCursor(), Pos(0, 4)); + cm.setSelection(Pos(0, 1), Pos(2, 4)); + cm.execCommand("goCharLeft"); + is(!cm.somethingSelected()); + eqCharPos(cm.getCursor(), Pos(0, 1)); +}, {value: "aaaaa\nb\nccccc"}); + +testCM("clickTab", function(cm) { + var p0 = cm.charCoords(Pos(0, 0)); + eqCharPos(cm.coordsChar({left: p0.left + 5, top: p0.top + 5}), Pos(0, 0)); + eqCharPos(cm.coordsChar({left: p0.right - 5, top: p0.top + 5}), Pos(0, 1)); +}, {value: "\t\n\n", lineWrapping: true, tabSize: 8}); + +testCM("verticalScroll", function(cm) { + cm.setSize(100, 200); + cm.setValue("foo\nbar\nbaz\n"); + var sc = cm.getScrollerElement(), baseWidth = sc.scrollWidth; + cm.replaceRange("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah", Pos(0, 0), Pos(0)); + is(sc.scrollWidth > baseWidth, "scrollbar present"); + cm.replaceRange("foo", Pos(0, 0), Pos(0)); + if (!phantom) eq(sc.scrollWidth, baseWidth, "scrollbar gone"); + cm.replaceRange("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah", Pos(0, 0), Pos(0)); + cm.replaceRange("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbh", Pos(1, 0), Pos(1)); + is(sc.scrollWidth > baseWidth, "present again"); + var curWidth = sc.scrollWidth; + cm.replaceRange("foo", Pos(0, 0), Pos(0)); + is(sc.scrollWidth < curWidth, "scrollbar smaller"); + is(sc.scrollWidth > baseWidth, "but still present"); +}); + +testCM("extraKeys", function(cm) { + var outcome; + function fakeKey(expected, code, props) { + if (typeof code == "string") code = code.charCodeAt(0); + var e = {type: "keydown", keyCode: code, preventDefault: function(){}, stopPropagation: function(){}}; + if (props) for (var n in props) e[n] = props[n]; + outcome = null; + cm.triggerOnKeyDown(e); + eq(outcome, expected); + } + CodeMirror.commands.testCommand = function() {outcome = "tc";}; + CodeMirror.commands.goTestCommand = function() {outcome = "gtc";}; + cm.setOption("extraKeys", {"Shift-X": function() {outcome = "sx";}, + "X": function() {outcome = "x";}, + "Ctrl-Alt-U": function() {outcome = "cau";}, + "End": "testCommand", + "Home": "goTestCommand", + "Tab": false}); + fakeKey(null, "U"); + fakeKey("cau", "U", {ctrlKey: true, altKey: true}); + fakeKey(null, "U", {shiftKey: true, ctrlKey: true, altKey: true}); + fakeKey("x", "X"); + fakeKey("sx", "X", {shiftKey: true}); + fakeKey("tc", 35); + fakeKey(null, 35, {shiftKey: true}); + fakeKey("gtc", 36); + fakeKey("gtc", 36, {shiftKey: true}); + fakeKey(null, 9); +}, null, window.opera && mac); + +testCM("wordMovementCommands", function(cm) { + cm.execCommand("goWordLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); + eqCursorPos(cm.getCursor(), Pos(0, 7, "before")); + cm.execCommand("goWordLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 5, "after")); + cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); + eqCursorPos(cm.getCursor(), Pos(0, 12, "before")); + cm.execCommand("goWordLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 9, "after")); + cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); + eqCursorPos(cm.getCursor(), Pos(0, 24, "before")); + cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); + eqCursorPos(cm.getCursor(), Pos(1, 9, "before")); + cm.execCommand("goWordRight"); + eqCursorPos(cm.getCursor(), Pos(1, 13, "before")); + cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); + eqCharPos(cm.getCursor(), Pos(2, 0)); +}, {value: "this is (the) firstline.\na foo12\u00e9\u00f8\u00d7bar\n"}); + +testCM("groupMovementCommands", function(cm) { + cm.execCommand("goGroupLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + cm.execCommand("goGroupRight"); + eqCursorPos(cm.getCursor(), Pos(0, 4, "before")); + cm.execCommand("goGroupRight"); + eqCursorPos(cm.getCursor(), Pos(0, 7, "before")); + cm.execCommand("goGroupRight"); + eqCursorPos(cm.getCursor(), Pos(0, 10, "before")); + cm.execCommand("goGroupLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 7, "after")); + cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight"); + eqCursorPos(cm.getCursor(), Pos(0, 15, "before")); + cm.setCursor(Pos(0, 17)); + cm.execCommand("goGroupLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 16, "after")); + cm.execCommand("goGroupLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 14, "after")); + cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight"); + eqCursorPos(cm.getCursor(), Pos(0, 20, "before")); + cm.execCommand("goGroupRight"); + eqCursorPos(cm.getCursor(), Pos(1, 0, "after")); + cm.execCommand("goGroupRight"); + eqCursorPos(cm.getCursor(), Pos(1, 2, "before")); + cm.execCommand("goGroupRight"); + eqCursorPos(cm.getCursor(), Pos(1, 5, "before")); + cm.execCommand("goGroupLeft"); cm.execCommand("goGroupLeft"); + eqCursorPos(cm.getCursor(), Pos(1, 0, "after")); + cm.execCommand("goGroupLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 20, "after")); + cm.execCommand("goGroupLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 16, "after")); +}, {value: "booo ba---quux. ffff\n abc d"}); + +testCM("groupsAndWhitespace", function(cm) { + var positions = [Pos(0, 0), Pos(0, 2), Pos(0, 5), Pos(0, 9), Pos(0, 11), + Pos(1, 0), Pos(1, 2), Pos(1, 5)]; + for (var i = 1; i < positions.length; i++) { + cm.execCommand("goGroupRight"); + eqCharPos(cm.getCursor(), positions[i]); + } + for (var i = positions.length - 2; i >= 0; i--) { + cm.execCommand("goGroupLeft"); + eqCharPos(cm.getCursor(), i == 2 ? Pos(0, 6, "before") : positions[i]); + } +}, {value: " foo +++ \n bar"}); + +testCM("charMovementCommands", function(cm) { + cm.execCommand("goCharLeft"); cm.execCommand("goColumnLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + cm.execCommand("goCharRight"); cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(0, 2, "before")); + cm.setCursor(Pos(1, 0)); + cm.execCommand("goColumnLeft"); + eqCursorPos(cm.getCursor(), Pos(1, 0)); + cm.execCommand("goCharLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 5, "before")); + cm.execCommand("goColumnRight"); + eqCursorPos(cm.getCursor(), Pos(0, 5, "before")); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(1, 0, "after")); + cm.execCommand("goLineEnd"); + eqCursorPos(cm.getCursor(), Pos(1, 5, "before")); + cm.execCommand("goLineStartSmart"); + eqCursorPos(cm.getCursor(), Pos(1, 1, "after")); + cm.execCommand("goLineStartSmart"); + eqCursorPos(cm.getCursor(), Pos(1, 0, "after")); + cm.setCursor(Pos(2, 0)); + cm.execCommand("goCharRight"); cm.execCommand("goColumnRight"); + eqCursorPos(cm.getCursor(), Pos(2, 0)); +}, {value: "line1\n ine2\n"}); + +testCM("verticalMovementCommands", function(cm) { + cm.execCommand("goLineUp"); + eqCharPos(cm.getCursor(), Pos(0, 0)); + cm.execCommand("goLineDown"); + if (!phantom) // This fails in PhantomJS, though not in a real Webkit + eqCharPos(cm.getCursor(), Pos(1, 0)); + cm.setCursor(Pos(1, 12)); + cm.execCommand("goLineDown"); + eqCharPos(cm.getCursor(), Pos(2, 5)); + cm.execCommand("goLineDown"); + eqCharPos(cm.getCursor(), Pos(3, 0)); + cm.execCommand("goLineUp"); + eqCharPos(cm.getCursor(), Pos(2, 5)); + cm.execCommand("goLineUp"); + eqCharPos(cm.getCursor(), Pos(1, 12)); + cm.execCommand("goPageDown"); + eqCharPos(cm.getCursor(), Pos(5, 0)); + cm.execCommand("goPageDown"); cm.execCommand("goLineDown"); + eqCharPos(cm.getCursor(), Pos(5, 0)); + cm.execCommand("goPageUp"); + eqCharPos(cm.getCursor(), Pos(0, 0)); +}, {value: "line1\nlong long line2\nline3\n\nline5\n"}); + +testCM("verticalMovementCommandsWrapping", function(cm) { + cm.setSize(120); + cm.setCursor(Pos(0, 5)); + cm.execCommand("goLineDown"); + eq(cm.getCursor().line, 0); + is(cm.getCursor().ch > 5, "moved beyond wrap"); + for (var i = 0; ; ++i) { + is(i < 20, "no endless loop"); + cm.execCommand("goLineDown"); + var cur = cm.getCursor(); + if (cur.line == 1) eq(cur.ch, 5); + if (cur.line == 2) { eq(cur.ch, 1); break; } + } +}, {value: "a very long line that wraps around somehow so that we can test cursor movement\nshortone\nk", + lineWrapping: true}); + +testCM("verticalMovementCommandsSingleLine", function(cm) { + cm.display.wrapper.style.height = "auto"; + cm.refresh(); + cm.execCommand("goLineUp"); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + cm.execCommand("goLineDown"); + eqCursorPos(cm.getCursor(), Pos(0, 11)); + cm.setCursor(Pos(0, 5)); + cm.execCommand("goLineDown"); + eqCursorPos(cm.getCursor(), Pos(0, 11)); + cm.execCommand("goLineDown"); + eqCursorPos(cm.getCursor(), Pos(0, 11)); + cm.execCommand("goLineUp"); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + cm.execCommand("goLineUp"); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + cm.execCommand("goPageDown"); + eqCursorPos(cm.getCursor(), Pos(0, 11)); + cm.execCommand("goPageDown"); cm.execCommand("goLineDown"); + eqCursorPos(cm.getCursor(), Pos(0, 11)); + cm.execCommand("goPageUp"); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + cm.setCursor(Pos(0, 5)); + cm.execCommand("goPageUp"); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + cm.setCursor(Pos(0, 5)); + cm.execCommand("goPageDown"); + eqCursorPos(cm.getCursor(), Pos(0, 11)); +}, {value: "single line"}); + + +testCM("rtlMovement", function(cm) { + if (cm.getOption("inputStyle") != "textarea") return; + forEach(["خحج", "خحabcخحج", "abخحخحجcd", "abخde", "abخح2342خ1حج", "خ1ح2خح3حxج", + "خحcd", "1خحcd", "abcdeح1ج", "خمرحبها مها!", "foobarر", "خ ة ق", + "<img src=\"/בדיקה3.jpg\">", "يتم السحب في 05 فبراير 2014"], function(line) { + cm.setValue(line + "\n"); cm.execCommand("goLineStart"); + var cursors = byClassName(cm.getWrapperElement(), "CodeMirror-cursors")[0]; + var cursor = cursors.firstChild; + var prevX = cursor.offsetLeft, prevY = cursor.offsetTop; + for (var i = 0; i <= line.length; ++i) { + cm.execCommand("goCharRight"); + cursor = cursors.firstChild; + if (i == line.length) is(cursor.offsetTop > prevY, "next line"); + else is(cursor.offsetLeft > prevX, "moved right"); + prevX = cursor.offsetLeft; prevY = cursor.offsetTop; + } + cm.setCursor(0, 0); cm.execCommand("goLineEnd"); + prevX = cursors.firstChild.offsetLeft; + for (var i = 0; i < line.length; ++i) { + cm.execCommand("goCharLeft"); + cursor = cursors.firstChild; + is(cursor.offsetLeft < prevX, "moved left"); + prevX = cursor.offsetLeft; + } + }); +}, null, ie_lt9); + +// Verify that updating a line clears its bidi ordering +testCM("bidiUpdate", function(cm) { + cm.setCursor(Pos(0, 2, "before")); + cm.replaceSelection("خحج", "start"); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(0, 6, "before")); +}, {value: "abcd\n"}); + +testCM("movebyTextUnit", function(cm) { + cm.setValue("בְּרֵאשִ\nééé́\n"); + cm.execCommand("goLineStart"); + for (var i = 0; i < 4; ++i) cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(0, 0, "after")); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(1, 0, "after")); + cm.execCommand("goCharRight"); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(1, 4, "before")); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(1, 7, "before")); +}); + +testCM("lineChangeEvents", function(cm) { + addDoc(cm, 3, 5); + var log = [], want = ["ch 0", "ch 1", "del 2", "ch 0", "ch 0", "del 1", "del 3", "del 4"]; + for (var i = 0; i < 5; ++i) { + CodeMirror.on(cm.getLineHandle(i), "delete", function(i) { + return function() {log.push("del " + i);}; + }(i)); + CodeMirror.on(cm.getLineHandle(i), "change", function(i) { + return function() {log.push("ch " + i);}; + }(i)); + } + cm.replaceRange("x", Pos(0, 1)); + cm.replaceRange("xy", Pos(1, 1), Pos(2)); + cm.replaceRange("foo\nbar", Pos(0, 1)); + cm.replaceRange("", Pos(0, 0), Pos(cm.lineCount())); + eq(log.length, want.length, "same length"); + for (var i = 0; i < log.length; ++i) + eq(log[i], want[i]); +}); + +testCM("scrollEntirelyToRight", function(cm) { + if (phantom || cm.getOption("inputStyle") != "textarea") return; + addDoc(cm, 500, 2); + cm.setCursor(Pos(0, 500)); + var wrap = cm.getWrapperElement(), cur = byClassName(wrap, "CodeMirror-cursor")[0]; + is(wrap.getBoundingClientRect().right > cur.getBoundingClientRect().left); +}); + +testCM("lineWidgets", function(cm) { + addDoc(cm, 500, 3); + var last = cm.charCoords(Pos(2, 0)); + var node = document.createElement("div"); + node.innerHTML = "hi"; + var widget = cm.addLineWidget(1, node); + is(last.top < cm.charCoords(Pos(2, 0)).top, "took up space"); + cm.setCursor(Pos(1, 1)); + cm.execCommand("goLineDown"); + eqCharPos(cm.getCursor(), Pos(2, 1)); + cm.execCommand("goLineUp"); + eqCharPos(cm.getCursor(), Pos(1, 1)); +}); + +testCM("lineWidgetFocus", function(cm) { + var place = document.getElementById("testground"); + place.className = "offscreen"; + try { + addDoc(cm, 500, 10); + var node = document.createElement("input"); + var widget = cm.addLineWidget(1, node); + node.focus(); + eq(document.activeElement, node); + cm.replaceRange("new stuff", Pos(1, 0)); + eq(document.activeElement, node); + } finally { + place.className = ""; + } +}); + +testCM("lineWidgetCautiousRedraw", function(cm) { + var node = document.createElement("div"); + node.innerHTML = "hahah"; + var w = cm.addLineWidget(0, node); + var redrawn = false; + w.on("redraw", function() { redrawn = true; }); + cm.replaceSelection("0"); + is(!redrawn); +}, {value: "123\n456"}); + + +var knownScrollbarWidth; +function scrollbarWidth(measure) { + if (knownScrollbarWidth != null) return knownScrollbarWidth; + var div = document.createElement('div'); + div.style.cssText = "width: 50px; height: 50px; overflow-x: scroll"; + document.body.appendChild(div); + knownScrollbarWidth = div.offsetHeight - div.clientHeight; + document.body.removeChild(div); + return knownScrollbarWidth || 0; +} + +testCM("lineWidgetChanged", function(cm) { + addDoc(cm, 2, 300); + var halfScrollbarWidth = scrollbarWidth(cm.display.measure)/2; + cm.setOption('lineNumbers', true); + cm.setSize(600, cm.defaultTextHeight() * 50); + cm.scrollTo(null, cm.heightAtLine(125, "local")); + + var expectedWidgetHeight = 60; + var expectedLinesInWidget = 3; + function w() { + var node = document.createElement("div"); + // we use these children with just under half width of the line to check measurements are made with correct width + // when placed in the measure div. + // If the widget is measured at a width much narrower than it is displayed at, the underHalf children will span two lines and break the test. + // If the widget is measured at a width much wider than it is displayed at, the overHalf children will combine and break the test. + // Note that this test only checks widgets where coverGutter is true, because these require extra styling to get the width right. + // It may also be worthwhile to check this for non-coverGutter widgets. + // Visually: + // Good: + // | ------------- display width ------------- | + // | ------- widget-width when measured ------ | + // | | -- under-half -- | | -- under-half -- | | + // | | --- over-half --- | | + // | | --- over-half --- | | + // Height: measured as 3 lines, same as it will be when actually displayed + + // Bad (too narrow): + // | ------------- display width ------------- | + // | ------ widget-width when measured ----- | < -- uh oh + // | | -- under-half -- | | + // | | -- under-half -- | | < -- when measured, shoved to next line + // | | --- over-half --- | | + // | | --- over-half --- | | + // Height: measured as 4 lines, more than expected . Will be displayed as 3 lines! + + // Bad (too wide): + // | ------------- display width ------------- | + // | -------- widget-width when measured ------- | < -- uh oh + // | | -- under-half -- | | -- under-half -- | | + // | | --- over-half --- | | --- over-half --- | | < -- when measured, combined on one line + // Height: measured as 2 lines, less than expected. Will be displayed as 3 lines! + + var barelyUnderHalfWidthHtml = '<div style="display: inline-block; height: 1px; width: '+(285 - halfScrollbarWidth)+'px;"></div>'; + var barelyOverHalfWidthHtml = '<div style="display: inline-block; height: 1px; width: '+(305 - halfScrollbarWidth)+'px;"></div>'; + node.innerHTML = new Array(3).join(barelyUnderHalfWidthHtml) + new Array(3).join(barelyOverHalfWidthHtml); + node.style.cssText = "background: yellow;font-size:0;line-height: " + (expectedWidgetHeight/expectedLinesInWidget) + "px;"; + return node; + } + var info0 = cm.getScrollInfo(); + var w0 = cm.addLineWidget(0, w(), { coverGutter: true }); + var w150 = cm.addLineWidget(150, w(), { coverGutter: true }); + var w300 = cm.addLineWidget(300, w(), { coverGutter: true }); + var info1 = cm.getScrollInfo(); + eq(info0.height + (3 * expectedWidgetHeight), info1.height); + eq(info0.top + expectedWidgetHeight, info1.top); + expectedWidgetHeight = 12; + w0.node.style.lineHeight = w150.node.style.lineHeight = w300.node.style.lineHeight = (expectedWidgetHeight/expectedLinesInWidget) + "px"; + w0.changed(); w150.changed(); w300.changed(); + var info2 = cm.getScrollInfo(); + eq(info0.height + (3 * expectedWidgetHeight), info2.height); + eq(info0.top + expectedWidgetHeight, info2.top); +}); + +testCM("lineWidgetIssue5486", function(cm) { + // [prepare] + // 2nd line is combined to 1st line due to markText + // 2nd line has a lineWidget below + + cm.setValue("Lorem\nIpsue\nDollar") + + var el = document.createElement('div') + el.style.height='50px' + el.textContent = '[[LINE WIDGET]]' + + var lineWidget = cm.addLineWidget(1, el, { + above: false, + coverGutter: false, + noHScroll: false, + showIfHidden: false, + }) + + var marker = document.createElement('span') + marker.textContent = '[--]' + + cm.markText({line:0, ch: 1}, {line:1, ch: 4}, { + replacedWith: marker + }) + + // before resizing the lineWidget, measure 3rd line position + + var measure_1 = Math.round(cm.charCoords({line:2, ch:0}).top) + + // resize lineWidget, height + 50 px + + el.style.height='100px' + el.textContent += "\nlineWidget size changed.\nTry moving cursor to line 3?" + + lineWidget.changed() + + // re-measure 3rd line position + var measure_2 = Math.round(cm.charCoords({line:2, ch:0}).top) + eq(measure_2, measure_1 + 50) + + // (extra test) + // + // add char to the right of the folded marker + // and re-measure 3rd line position + + cm.replaceRange('-', {line:1, ch: 5}) + var measure_3 = Math.round(cm.charCoords({line:2, ch:0}).top) + eq(measure_3, measure_2) +}); + +testCM("getLineNumber", function(cm) { + addDoc(cm, 2, 20); + var h1 = cm.getLineHandle(1); + eq(cm.getLineNumber(h1), 1); + cm.replaceRange("hi\nbye\n", Pos(0, 0)); + eq(cm.getLineNumber(h1), 3); + cm.setValue(""); + eq(cm.getLineNumber(h1), null); +}); + +testCM("jumpTheGap", function(cm) { + if (phantom) return; + var longLine = "abcdef ghiklmnop qrstuvw xyz "; + longLine += longLine; longLine += longLine; longLine += longLine; + cm.replaceRange(longLine, Pos(2, 0), Pos(2)); + cm.setSize("200px", null); + cm.getWrapperElement().style.lineHeight = 2; + cm.refresh(); + cm.setCursor(Pos(0, 1)); + cm.execCommand("goLineDown"); + eqCharPos(cm.getCursor(), Pos(1, 1)); + cm.execCommand("goLineDown"); + eqCharPos(cm.getCursor(), Pos(2, 1)); + cm.execCommand("goLineDown"); + eq(cm.getCursor().line, 2); + is(cm.getCursor().ch > 1); + cm.execCommand("goLineUp"); + eqCharPos(cm.getCursor(), Pos(2, 1)); + cm.execCommand("goLineUp"); + eqCharPos(cm.getCursor(), Pos(1, 1)); + var node = document.createElement("div"); + node.innerHTML = "hi"; node.style.height = "30px"; + cm.addLineWidget(0, node); + cm.addLineWidget(1, node.cloneNode(true), {above: true}); + cm.setCursor(Pos(0, 2)); + cm.execCommand("goLineDown"); + eqCharPos(cm.getCursor(), Pos(1, 2)); + cm.execCommand("goLineUp"); + eqCharPos(cm.getCursor(), Pos(0, 2)); +}, {lineWrapping: true, value: "abc\ndef\nghi\njkl\n"}); + +testCM("addLineClass", function(cm) { + function cls(line, text, bg, wrap, gutter) { + var i = cm.lineInfo(line); + eq(i.textClass, text); + eq(i.bgClass, bg); + eq(i.wrapClass, wrap); + if (typeof i.handle.gutterClass !== 'undefined') { + eq(i.handle.gutterClass, gutter); + } + } + cm.addLineClass(0, "text", "foo"); + cm.addLineClass(0, "text", "bar"); + cm.addLineClass(1, "background", "baz"); + cm.addLineClass(1, "wrap", "foo"); + cm.addLineClass(1, "gutter", "gutter-class"); + cls(0, "foo bar", null, null, null); + cls(1, null, "baz", "foo", "gutter-class"); + var lines = cm.display.lineDiv; + eq(byClassName(lines, "foo").length, 2); + eq(byClassName(lines, "bar").length, 1); + eq(byClassName(lines, "baz").length, 1); + eq(byClassName(lines, "gutter-class").length, 2); // Gutter classes are reflected in 2 nodes + cm.removeLineClass(0, "text", "foo"); + cls(0, "bar", null, null, null); + cm.removeLineClass(0, "text", "foo"); + cls(0, "bar", null, null, null); + cm.removeLineClass(0, "text", "bar"); + cls(0, null, null, null); + + cm.addLineClass(1, "wrap", "quux"); + cls(1, null, "baz", "foo quux", "gutter-class"); + cm.removeLineClass(1, "wrap"); + cls(1, null, "baz", null, "gutter-class"); + cm.removeLineClass(1, "gutter", "gutter-class"); + eq(byClassName(lines, "gutter-class").length, 0); + cls(1, null, "baz", null, null); + + cm.addLineClass(1, "gutter", "gutter-class"); + cls(1, null, "baz", null, "gutter-class"); + cm.removeLineClass(1, "gutter", "gutter-class"); + cls(1, null, "baz", null, null); + +}, {value: "hohoho\n", lineNumbers: true}); + +testCM("atomicMarker", function(cm) { + addDoc(cm, 10, 10); + + function atom(ll, cl, lr, cr, li, ri, ls, rs) { + var options = { + atomic: true, + inclusiveLeft: li, + inclusiveRight: ri + }; + + if (ls === true || ls === false) options["selectLeft"] = ls; + if (rs === true || rs === false) options["selectRight"] = rs; + + return cm.markText(Pos(ll, cl), Pos(lr, cr), options); + } + + // Can cursor to the left and right of a normal marker by jumping across it + var m = atom(0, 1, 0, 5); + cm.setCursor(Pos(0, 1)); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(0, 5)); + cm.execCommand("goCharLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 1)); + m.clear(); + + // Can't cursor to the left of a marker when inclusiveLeft=true + m = atom(0, 0, 0, 5, true); + eqCursorPos(cm.getCursor(), Pos(0, 5), "pushed out"); + cm.execCommand("goCharLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 5)); + m.clear(); + + // Can't cursor to the left of a marker when inclusiveLeft=false and selectLeft=false + m = atom(0, 0, 0, 5, false, false, false); + cm.setCursor(Pos(0, 5)); + eqCursorPos(cm.getCursor(), Pos(0, 5), "pushed out"); + cm.execCommand("goCharLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 5)); + m.clear(); + + // Can cursor to the left of a marker when inclusiveLeft=false and selectLeft=True + m = atom(0, 0, 0, 5, false, false, true); + cm.setCursor(Pos(0, 5)); + eqCursorPos(cm.getCursor(), Pos(0, 5), "pushed out"); + cm.execCommand("goCharLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + m.clear(); + + // Can't cursor to the right of a marker when inclusiveRight=true + m = atom(0, 0, 0, 5, false, true); + cm.setCursor(Pos(0, 0)); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(0, 6)); + m.clear(); + + // Can't cursor to the right of a marker when inclusiveRight=false and selectRight=false + m = atom(0, 0, 0, 5, false, false, true, false); + cm.setCursor(Pos(0, 0)); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(0, 6)); + m.clear(); + + // Can cursor to the right of a marker when inclusiveRight=false and selectRight=True + m = atom(0, 0, 0, 5, false, false, true, true); + cm.setCursor(Pos(0, 0)); + eqCursorPos(cm.getCursor(), Pos(0, 0)); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(0, 5)); + m.clear(); + + // Can't cursor to the right of a multiline marker when inclusiveRight=true + m = atom(8, 4, 9, 10, false, true); + cm.setCursor(Pos(9, 8)); + eqCursorPos(cm.getCursor(), Pos(8, 4), "set"); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(8, 4), "char right"); + cm.execCommand("goLineDown"); + eqCursorPos(cm.getCursor(), Pos(8, 4), "line down"); + cm.execCommand("goCharLeft"); + eqCursorPos(cm.getCursor(), Pos(8, 3, "after")); + m.clear(); + + // Cursor jumps across a multiline atomic marker, + // and backspace deletes the entire marker + m = atom(1, 1, 3, 8); + cm.setCursor(Pos(0, 0)); + cm.setCursor(Pos(2, 0)); + eqCursorPos(cm.getCursor(), Pos(3, 8)); + cm.execCommand("goCharLeft"); + eqCursorPos(cm.getCursor(), Pos(1, 1)); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(3, 8)); + cm.execCommand("goLineUp"); + eqCursorPos(cm.getCursor(), Pos(1, 1)); + cm.execCommand("goLineDown"); + eqCursorPos(cm.getCursor(), Pos(3, 8)); + cm.execCommand("delCharBefore"); + eq(cm.getValue().length, 80, "del chunk"); + m.clear(); + addDoc(cm, 10, 10); + + // Delete before an atomic marker deletes the entire marker + m = atom(3, 0, 5, 5); + cm.setCursor(Pos(3, 0)); + cm.execCommand("delWordAfter"); + eq(cm.getValue().length, 82, "del chunk"); + m.clear(); + addDoc(cm, 10, 10); +}); + +testCM("selectionBias", function(cm) { + cm.markText(Pos(0, 1), Pos(0, 3), {atomic: true}); + cm.setCursor(Pos(0, 2)); + eqCursorPos(cm.getCursor(), Pos(0, 1)); + cm.setCursor(Pos(0, 2)); + eqCursorPos(cm.getCursor(), Pos(0, 3)); + cm.setCursor(Pos(0, 2)); + eqCursorPos(cm.getCursor(), Pos(0, 1)); + cm.setCursor(Pos(0, 2), null, {bias: -1}); + eqCursorPos(cm.getCursor(), Pos(0, 1)); + cm.setCursor(Pos(0, 4)); + cm.setCursor(Pos(0, 2), null, {bias: 1}); + eqCursorPos(cm.getCursor(), Pos(0, 3)); +}, {value: "12345"}); + +testCM("selectionHomeEnd", function(cm) { + cm.markText(Pos(1, 0), Pos(1, 1), {atomic: true, inclusiveLeft: true}); + cm.markText(Pos(1, 3), Pos(1, 4), {atomic: true, inclusiveRight: true}); + cm.setCursor(Pos(1, 2)); + cm.execCommand("goLineStart"); + eqCursorPos(cm.getCursor(), Pos(1, 1)); + cm.execCommand("goLineEnd"); + eqCursorPos(cm.getCursor(), Pos(1, 3)); +}, {value: "ab\ncdef\ngh"}); + +testCM("readOnlyMarker", function(cm) { + function mark(ll, cl, lr, cr, at) { + return cm.markText(Pos(ll, cl), Pos(lr, cr), + {readOnly: true, atomic: at}); + } + var m = mark(0, 1, 0, 4); + cm.setCursor(Pos(0, 2)); + cm.replaceSelection("hi", "end"); + eqCursorPos(cm.getCursor(), Pos(0, 2)); + eq(cm.getLine(0), "abcde"); + cm.execCommand("selectAll"); + cm.replaceSelection("oops", "around"); + eq(cm.getValue(), "oopsbcd"); + cm.undo(); + eqCursorPos(m.find().from, Pos(0, 1)); + eqCursorPos(m.find().to, Pos(0, 4)); + m.clear(); + cm.setCursor(Pos(0, 2)); + cm.replaceSelection("hi", "around"); + eq(cm.getLine(0), "abhicde"); + eqCursorPos(cm.getCursor(), Pos(0, 4)); + m = mark(0, 2, 2, 2, true); + cm.setSelection(Pos(1, 1), Pos(2, 4)); + cm.replaceSelection("t", "end"); + eqCursorPos(cm.getCursor(), Pos(2, 3)); + eq(cm.getLine(2), "klto"); + cm.execCommand("goCharLeft"); + cm.execCommand("goCharLeft"); + eqCursorPos(cm.getCursor(), Pos(0, 2)); + cm.setSelection(Pos(0, 1), Pos(0, 3)); + cm.replaceSelection("xx", "around"); + eqCursorPos(cm.getCursor(), Pos(0, 3)); + eq(cm.getLine(0), "axxhicde"); +}, {value: "abcde\nfghij\nklmno\n"}); + +testCM("dirtyBit", function(cm) { + eq(cm.isClean(), true); + cm.replaceSelection("boo", null, "test"); + eq(cm.isClean(), false); + cm.undo(); + eq(cm.isClean(), true); + cm.replaceSelection("boo", null, "test"); + cm.replaceSelection("baz", null, "test"); + cm.undo(); + eq(cm.isClean(), false); + cm.markClean(); + eq(cm.isClean(), true); + cm.undo(); + eq(cm.isClean(), false); + cm.redo(); + eq(cm.isClean(), true); +}); + +testCM("changeGeneration", function(cm) { + cm.replaceSelection("x"); + var softGen = cm.changeGeneration(); + cm.replaceSelection("x"); + cm.undo(); + eq(cm.getValue(), ""); + is(!cm.isClean(softGen)); + cm.replaceSelection("x"); + var hardGen = cm.changeGeneration(true); + cm.replaceSelection("x"); + cm.undo(); + eq(cm.getValue(), "x"); + is(cm.isClean(hardGen)); +}); + +testCM("addKeyMap", function(cm) { + function sendKey(code) { + cm.triggerOnKeyDown({type: "keydown", keyCode: code, + preventDefault: function(){}, stopPropagation: function(){}}); + } + + sendKey(39); + eqCursorPos(cm.getCursor(), Pos(0, 1, "before")); + var test = 0; + var map1 = {Right: function() { ++test; }}, map2 = {Right: function() { test += 10; }} + cm.addKeyMap(map1); + sendKey(39); + eqCursorPos(cm.getCursor(), Pos(0, 1, "before")); + eq(test, 1); + cm.addKeyMap(map2, true); + sendKey(39); + eq(test, 2); + cm.removeKeyMap(map1); + sendKey(39); + eq(test, 12); + cm.removeKeyMap(map2); + sendKey(39); + eq(test, 12); + eqCursorPos(cm.getCursor(), Pos(0, 2, "before")); + cm.addKeyMap({Right: function() { test = 55; }, name: "mymap"}); + sendKey(39); + eq(test, 55); + cm.removeKeyMap("mymap"); + sendKey(39); + eqCursorPos(cm.getCursor(), Pos(0, 3, "before")); +}, {value: "abc"}); + +function mouseDown(cm, button, pos, mods) { + var coords = cm.charCoords(pos, "window") + var event = {type: "mousedown", + preventDefault: Math.min, + which: button, + target: cm.display.lineDiv, + clientX: coords.left, clientY: coords.top} + if (mods) for (var prop in mods) event[prop] = mods[prop] + cm.triggerOnMouseDown(event) +} + +testCM("mouseBinding", function(cm) { + var fired = [] + cm.addKeyMap({ + "Shift-LeftClick": function(_cm, pos) { + eqCharPos(pos, Pos(1, 2)) + fired.push("a") + }, + "Shift-LeftDoubleClick": function() { fired.push("b") }, + "Shift-LeftTripleClick": function() { fired.push("c") } + }) + + function send(button, mods) { mouseDown(cm, button, Pos(1, 2), mods) } + send(1, {shiftKey: true}) + send(1, {shiftKey: true}) + send(1, {shiftKey: true}) + send(1, {}) + send(2, {ctrlKey: true}) + send(2, {ctrlKey: true}) + eq(fired.join(" "), "a b c") +}, {value: "foo\nbar\nbaz"}) + +testCM("configureMouse", function(cm) { + cm.setOption("configureMouse", function() { return {unit: "word"} }) + mouseDown(cm, 1, Pos(0, 5)) + eqCharPos(cm.getCursor("from"), Pos(0, 4)) + eqCharPos(cm.getCursor("to"), Pos(0, 7)) + cm.setOption("configureMouse", function() { return {extend: true} }) + mouseDown(cm, 1, Pos(0, 0)) + eqCharPos(cm.getCursor("from"), Pos(0, 0)) + eqCharPos(cm.getCursor("to"), Pos(0, 4)) +}, {value: "foo bar baz"}) + +testCM("findPosH", function(cm) { + forEach([{from: Pos(0, 0), to: Pos(0, 1, "before"), by: 1}, + {from: Pos(0, 0), to: Pos(0, 0), by: -1, hitSide: true}, + {from: Pos(0, 0), to: Pos(0, 4, "before"), by: 1, unit: "word"}, + {from: Pos(0, 0), to: Pos(0, 8, "before"), by: 2, unit: "word"}, + {from: Pos(0, 0), to: Pos(2, 0, "after"), by: 20, unit: "word", hitSide: true}, + {from: Pos(0, 7), to: Pos(0, 5, "after"), by: -1, unit: "word"}, + {from: Pos(0, 4), to: Pos(0, 8, "before"), by: 1, unit: "word"}, + {from: Pos(1, 0), to: Pos(1, 18, "before"), by: 3, unit: "word"}, + {from: Pos(1, 22), to: Pos(1, 5, "after"), by: -3, unit: "word"}, + {from: Pos(1, 15), to: Pos(1, 10, "after"), by: -5}, + {from: Pos(1, 15), to: Pos(1, 10, "after"), by: -5, unit: "column"}, + {from: Pos(1, 15), to: Pos(1, 0, "after"), by: -50, unit: "column", hitSide: true}, + {from: Pos(1, 15), to: Pos(1, 24, "before"), by: 50, unit: "column", hitSide: true}, + {from: Pos(1, 15), to: Pos(2, 0, "after"), by: 50, hitSide: true}], function(t) { + var r = cm.findPosH(t.from, t.by, t.unit || "char"); + eqCursorPos(r, t.to); + eq(!!r.hitSide, !!t.hitSide); + }); +}, {value: "line one\nline two.something.other\n"}); + +testCM("beforeChange", function(cm) { + cm.on("beforeChange", function(cm, change) { + var text = []; + for (var i = 0; i < change.text.length; ++i) + text.push(change.text[i].replace(/\s/g, "_")); + change.update(null, null, text); + }); + cm.setValue("hello, i am a\nnew document\n"); + eq(cm.getValue(), "hello,_i_am_a\nnew_document\n"); + CodeMirror.on(cm.getDoc(), "beforeChange", function(doc, change) { + if (change.from.line == 0) change.cancel(); + }); + cm.setValue("oops"); // Canceled + eq(cm.getValue(), "hello,_i_am_a\nnew_document\n"); + cm.replaceRange("hey hey hey", Pos(1, 0), Pos(2, 0)); + eq(cm.getValue(), "hello,_i_am_a\nhey_hey_hey"); +}, {value: "abcdefghijk"}); + +testCM("beforeChangeUndo", function(cm) { + cm.replaceRange("hi", Pos(0, 0), Pos(0)); + cm.replaceRange("bye", Pos(0, 0), Pos(0)); + eq(cm.historySize().undo, 2); + cm.on("beforeChange", function(cm, change) { + is(!change.update); + change.cancel(); + }); + cm.undo(); + eq(cm.historySize().undo, 0); + eq(cm.getValue(), "bye\ntwo"); +}, {value: "one\ntwo"}); + +testCM("beforeSelectionChange", function(cm) { + function notAtEnd(cm, pos) { + var len = cm.getLine(pos.line).length; + if (!len || pos.ch == len) return Pos(pos.line, pos.ch - 1); + return pos; + } + cm.on("beforeSelectionChange", function(cm, obj) { + obj.update([{anchor: notAtEnd(cm, obj.ranges[0].anchor), + head: notAtEnd(cm, obj.ranges[0].head)}]); + }); + + addDoc(cm, 10, 10); + cm.execCommand("goLineEnd"); + eqCursorPos(cm.getCursor(), Pos(0, 9)); + cm.execCommand("selectAll"); + eqCursorPos(cm.getCursor("start"), Pos(0, 0)); + eqCursorPos(cm.getCursor("end"), Pos(9, 9)); +}); + +testCM("change_removedText", function(cm) { + cm.setValue("abc\ndef"); + + var removedText = []; + cm.on("change", function(cm, change) { + removedText.push(change.removed); + }); + + cm.operation(function() { + cm.replaceRange("xyz", Pos(0, 0), Pos(1,1)); + cm.replaceRange("123", Pos(0,0)); + }); + + eq(removedText.length, 2); + eq(removedText[0].join("\n"), "abc\nd"); + eq(removedText[1].join("\n"), ""); + + var removedText = []; + cm.undo(); + eq(removedText.length, 2); + eq(removedText[0].join("\n"), "123"); + eq(removedText[1].join("\n"), "xyz"); + + var removedText = []; + cm.redo(); + eq(removedText.length, 2); + eq(removedText[0].join("\n"), "abc\nd"); + eq(removedText[1].join("\n"), ""); +}); + +testCM("lineStyleFromMode", function(cm) { + CodeMirror.defineMode("test_mode", function() { + return {token: function(stream) { + if (stream.match(/^\[[^\]]*\]/)) return " line-brackets "; + if (stream.match(/^\([^\)]*\)/)) return " line-background-parens "; + if (stream.match(/^<[^>]*>/)) return " span line-line line-background-bg "; + stream.match(/^\s+|^\S+/); + }}; + }); + cm.setOption("mode", "test_mode"); + var bracketElts = byClassName(cm.getWrapperElement(), "brackets"); + eq(bracketElts.length, 1, "brackets count"); + eq(bracketElts[0].nodeName, "PRE"); + is(!/brackets.*brackets/.test(bracketElts[0].className)); + var parenElts = byClassName(cm.getWrapperElement(), "parens"); + eq(parenElts.length, 1, "parens count"); + eq(parenElts[0].nodeName, "DIV"); + is(!/parens.*parens/.test(parenElts[0].className)); + eq(parenElts[0].parentElement.nodeName, "DIV"); + + is(byClassName(cm.getWrapperElement(), "bg").length > 0); + is(byClassName(cm.getWrapperElement(), "line").length > 0); + var spanElts = byClassName(cm.getWrapperElement(), "cm-span"); + eq(spanElts.length, 2); + is(/^\s*cm-span\s*$/.test(spanElts[0].className)); +}, {value: "line1: [br] [br]\nline2: (par) (par)\nline3: <tag> <tag>"}); + +testCM("lineStyleFromBlankLine", function(cm) { + CodeMirror.defineMode("lineStyleFromBlankLine_mode", function() { + return {token: function(stream) { stream.skipToEnd(); return "comment"; }, + blankLine: function() { return "line-blank"; }}; + }); + cm.setOption("mode", "lineStyleFromBlankLine_mode"); + var blankElts = byClassName(cm.getWrapperElement(), "blank"); + eq(blankElts.length, 1); + eq(blankElts[0].nodeName, "PRE"); + cm.replaceRange("x", Pos(1, 0)); + blankElts = byClassName(cm.getWrapperElement(), "blank"); + eq(blankElts.length, 0); +}, {value: "foo\n\nbar"}); + +CodeMirror.registerHelper("xxx", "a", "A"); +CodeMirror.registerHelper("xxx", "b", "B"); +CodeMirror.defineMode("yyy", function() { + return { + token: function(stream) { stream.skipToEnd(); }, + xxx: ["a", "b", "q"] + }; +}); +CodeMirror.registerGlobalHelper("xxx", "c", function(m) { return m.enableC; }, "C"); + +testCM("helpers", function(cm) { + cm.setOption("mode", "yyy"); + eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "A/B"); + cm.setOption("mode", {name: "yyy", modeProps: {xxx: "b", enableC: true}}); + eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "B/C"); + cm.setOption("mode", "javascript"); + eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), ""); +}); + +testCM("selectionHistory", function(cm) { + for (var i = 0; i < 3; i++) { + cm.setExtending(true); + cm.execCommand("goCharRight"); + cm.setExtending(false); + cm.execCommand("goCharRight"); + cm.execCommand("goCharRight"); + } + cm.execCommand("undoSelection"); + eq(cm.getSelection(), "c"); + cm.execCommand("undoSelection"); + eq(cm.getSelection(), ""); + eqCursorPos(cm.getCursor(), Pos(0, 4, "before")); + cm.execCommand("undoSelection"); + eq(cm.getSelection(), "b"); + cm.execCommand("redoSelection"); + eq(cm.getSelection(), ""); + eqCursorPos(cm.getCursor(), Pos(0, 4, "before")); + cm.execCommand("redoSelection"); + eq(cm.getSelection(), "c"); + cm.execCommand("redoSelection"); + eq(cm.getSelection(), ""); + eqCursorPos(cm.getCursor(), Pos(0, 6, "before")); +}, {value: "a b c d"}); + +testCM("selectionChangeReducesRedo", function(cm) { + cm.replaceSelection("X"); + cm.execCommand("goCharRight"); + cm.undoSelection(); + cm.execCommand("selectAll"); + cm.undoSelection(); + eq(cm.getValue(), "Xabc"); + eqCursorPos(cm.getCursor(), Pos(0, 1)); + cm.undoSelection(); + eq(cm.getValue(), "abc"); +}, {value: "abc"}); + +testCM("selectionHistoryNonOverlapping", function(cm) { + cm.setSelection(Pos(0, 0), Pos(0, 1)); + cm.setSelection(Pos(0, 2), Pos(0, 3)); + cm.execCommand("undoSelection"); + eqCursorPos(cm.getCursor("anchor"), Pos(0, 0)); + eqCursorPos(cm.getCursor("head"), Pos(0, 1)); +}, {value: "1234"}); + +testCM("cursorMotionSplitsHistory", function(cm) { + cm.replaceSelection("a"); + cm.execCommand("goCharRight"); + cm.replaceSelection("b"); + cm.replaceSelection("c"); + cm.undo(); + eq(cm.getValue(), "a1234"); + eqCursorPos(cm.getCursor(), Pos(0, 2, "before")); + cm.undo(); + eq(cm.getValue(), "1234"); + eqCursorPos(cm.getCursor(), Pos(0, 0)); +}, {value: "1234"}); + +testCM("selChangeInOperationDoesNotSplit", function(cm) { + for (var i = 0; i < 4; i++) { + cm.operation(function() { + cm.replaceSelection("x"); + cm.setCursor(Pos(0, cm.getCursor().ch - 1)); + }); + } + eqCursorPos(cm.getCursor(), Pos(0, 0)); + eq(cm.getValue(), "xxxxa"); + cm.undo(); + eq(cm.getValue(), "a"); +}, {value: "a"}); + +testCM("alwaysMergeSelEventWithChangeOrigin", function(cm) { + cm.replaceSelection("U", null, "foo"); + cm.setSelection(Pos(0, 0), Pos(0, 1), {origin: "foo"}); + cm.undoSelection(); + eq(cm.getValue(), "a"); + cm.replaceSelection("V", null, "foo"); + cm.setSelection(Pos(0, 0), Pos(0, 1), {origin: "bar"}); + cm.undoSelection(); + eq(cm.getValue(), "Va"); +}, {value: "a"}); + +testCM("getTokenAt", function(cm) { + var tokPlus = cm.getTokenAt(Pos(0, 2)); + eq(tokPlus.type, "operator"); + eq(tokPlus.string, "+"); + var toks = cm.getLineTokens(0); + eq(toks.length, 3); + forEach([["number", "1"], ["operator", "+"], ["number", "2"]], function(expect, i) { + eq(toks[i].type, expect[0]); + eq(toks[i].string, expect[1]); + }); +}, {value: "1+2", mode: "javascript"}); + +testCM("getTokenTypeAt", function(cm) { + eq(cm.getTokenTypeAt(Pos(0, 0)), "number"); + eq(cm.getTokenTypeAt(Pos(0, 6)), "string"); + cm.addOverlay({ + token: function(stream) { + if (stream.match("foo")) return "foo"; + else stream.next(); + } + }); + eq(byClassName(cm.getWrapperElement(), "cm-foo").length, 1); + eq(cm.getTokenTypeAt(Pos(0, 6)), "string"); +}, {value: "1 + 'foo'", mode: "javascript"}); + +testCM("addOverlay", function(cm) { + cm.addOverlay({ + token: function(stream) { + var base = stream.baseToken() + if (!/comment/.test(base.type) && stream.match(/\d+/)) return "x" + stream.next() + } + }) + var x = byClassName(cm.getWrapperElement(), "cm-x") + is(x.length, 1) + is(x[0].textContent, "233") + cm.replaceRange("", Pos(0, 4), Pos(0, 6)) + is(byClassName(cm.getWrapperElement(), "cm-x").length, 2) +}, {value: "foo /* 100 */\nbar + 233;\nbaz", mode: "javascript"}) + +testCM("resizeLineWidget", function(cm) { + addDoc(cm, 200, 3); + var widget = document.createElement("pre"); + widget.innerHTML = "imwidget"; + widget.style.background = "yellow"; + cm.addLineWidget(1, widget, {noHScroll: true}); + cm.setSize(40); + is(widget.parentNode.offsetWidth < 42); +}); + +testCM("combinedOperations", function(cm) { + var place = document.getElementById("testground"); + var other = CodeMirror(place, {value: "123"}); + try { + cm.operation(function() { + cm.addLineClass(0, "wrap", "foo"); + other.addLineClass(0, "wrap", "foo"); + }); + eq(byClassName(cm.getWrapperElement(), "foo").length, 1); + eq(byClassName(other.getWrapperElement(), "foo").length, 1); + cm.operation(function() { + cm.removeLineClass(0, "wrap", "foo"); + other.removeLineClass(0, "wrap", "foo"); + }); + eq(byClassName(cm.getWrapperElement(), "foo").length, 0); + eq(byClassName(other.getWrapperElement(), "foo").length, 0); + } finally { + place.removeChild(other.getWrapperElement()); + } +}, {value: "abc"}); + +testCM("eventOrder", function(cm) { + var seen = []; + cm.on("change", function() { + if (!seen.length) cm.replaceSelection("."); + seen.push("change"); + }); + cm.on("cursorActivity", function() { + cm.replaceSelection("!"); + seen.push("activity"); + }); + cm.replaceSelection("/"); + eq(seen.join(","), "change,change,activity,change"); +}); + +testCM("splitSpaces_nonspecial", function(cm) { + eq(byClassName(cm.getWrapperElement(), "cm-invalidchar").length, 0); +}, { + specialChars: /[\u00a0]/, + value: "spaces -> <- between" +}); + +test("core_rmClass", function() { + var node = document.createElement("div"); + node.className = "foo-bar baz-quux yadda"; + CodeMirror.rmClass(node, "quux"); + eq(node.className, "foo-bar baz-quux yadda"); + CodeMirror.rmClass(node, "baz-quux"); + eq(node.className, "foo-bar yadda"); + CodeMirror.rmClass(node, "yadda"); + eq(node.className, "foo-bar"); + CodeMirror.rmClass(node, "foo-bar"); + eq(node.className, ""); + node.className = " foo "; + CodeMirror.rmClass(node, "foo"); + eq(node.className, ""); +}); + +test("core_addClass", function() { + var node = document.createElement("div"); + CodeMirror.addClass(node, "a"); + eq(node.className, "a"); + CodeMirror.addClass(node, "a"); + eq(node.className, "a"); + CodeMirror.addClass(node, "b"); + eq(node.className, "a b"); + CodeMirror.addClass(node, "a"); + CodeMirror.addClass(node, "b"); + eq(node.className, "a b"); +}); + +testCM("lineSeparator", function(cm) { + eq(cm.lineCount(), 3); + eq(cm.getLine(1), "bar\r"); + eq(cm.getLine(2), "baz\rquux"); + cm.setOption("lineSeparator", "\r"); + eq(cm.lineCount(), 5); + eq(cm.getLine(4), "quux"); + eq(cm.getValue(), "foo\rbar\r\rbaz\rquux"); + eq(cm.getValue("\n"), "foo\nbar\n\nbaz\nquux"); + cm.setOption("lineSeparator", null); + cm.setValue("foo\nbar\r\nbaz\rquux"); + eq(cm.lineCount(), 4); +}, {value: "foo\nbar\r\nbaz\rquux", + lineSeparator: "\n"}); + +var extendingChars = /[\u0300-\u036f\u0483-\u0489\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u065e\u0670\u06d6-\u06dc\u06de-\u06e4\u06e7\u06e8\u06ea-\u06ed\u0711\u0730-\u074a\u07a6-\u07b0\u07eb-\u07f3\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0900-\u0902\u093c\u0941-\u0948\u094d\u0951-\u0955\u0962\u0963\u0981\u09bc\u09be\u09c1-\u09c4\u09cd\u09d7\u09e2\u09e3\u0a01\u0a02\u0a3c\u0a41\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a70\u0a71\u0a75\u0a81\u0a82\u0abc\u0ac1-\u0ac5\u0ac7\u0ac8\u0acd\u0ae2\u0ae3\u0b01\u0b3c\u0b3e\u0b3f\u0b41-\u0b44\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b82\u0bbe\u0bc0\u0bcd\u0bd7\u0c3e-\u0c40\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0cbc\u0cbf\u0cc2\u0cc6\u0ccc\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0d3e\u0d41-\u0d44\u0d4d\u0d57\u0d62\u0d63\u0dca\u0dcf\u0dd2-\u0dd4\u0dd6\u0ddf\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0eb1\u0eb4-\u0eb9\u0ebb\u0ebc\u0ec8-\u0ecd\u0f18\u0f19\u0f35\u0f37\u0f39\u0f71-\u0f7e\u0f80-\u0f84\u0f86\u0f87\u0f90-\u0f97\u0f99-\u0fbc\u0fc6\u102d-\u1030\u1032-\u1037\u1039\u103a\u103d\u103e\u1058\u1059\u105e-\u1060\u1071-\u1074\u1082\u1085\u1086\u108d\u109d\u135f\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b7-\u17bd\u17c6\u17c9-\u17d3\u17dd\u180b-\u180d\u18a9\u1920-\u1922\u1927\u1928\u1932\u1939-\u193b\u1a17\u1a18\u1a56\u1a58-\u1a5e\u1a60\u1a62\u1a65-\u1a6c\u1a73-\u1a7c\u1a7f\u1b00-\u1b03\u1b34\u1b36-\u1b3a\u1b3c\u1b42\u1b6b-\u1b73\u1b80\u1b81\u1ba2-\u1ba5\u1ba8\u1ba9\u1c2c-\u1c33\u1c36\u1c37\u1cd0-\u1cd2\u1cd4-\u1ce0\u1ce2-\u1ce8\u1ced\u1dc0-\u1de6\u1dfd-\u1dff\u200c\u200d\u20d0-\u20f0\u2cef-\u2cf1\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua66f-\ua672\ua67c\ua67d\ua6f0\ua6f1\ua802\ua806\ua80b\ua825\ua826\ua8c4\ua8e0-\ua8f1\ua926-\ua92d\ua947-\ua951\ua980-\ua982\ua9b3\ua9b6-\ua9b9\ua9bc\uaa29-\uaa2e\uaa31\uaa32\uaa35\uaa36\uaa43\uaa4c\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uabe5\uabe8\uabed\udc00-\udfff\ufb1e\ufe00-\ufe0f\ufe20-\ufe26\uff9e\uff9f]/ +var getChar = function (noExtending) { var res; do {res = String.fromCharCode(Math.floor(Math.random()*0x8ac)); } while ([0x90].indexOf(res.charCodeAt(0)) != -1 || (noExtending && extendingChars.test(res))); return res } +var getString = function (n) { var res = getChar(true); while (--n > 0) res += getChar(); return res } + +function makeItWrapAfter(cm, pos) { + var firstLineTop = cm.cursorCoords(Pos(0, 0)).top; + for(var w = 0, posTop; posTop != firstLineTop; ++w) { + cm.setSize(w); + posTop = cm.charCoords(pos).top; + } +} + +function countIf(arr, f) { + var result = 0 + for (var i = 0; i < arr.length; i++) if (f[arr[i]]) result++ + return result +} + +function testMoveBidi(str) { + testCM("move_bidi_" + str, function(cm) { + if (cm.getOption("inputStyle") != "textarea" || !cm.getOption("rtlMoveVisually")) return; + cm.getScrollerElement().style.fontFamily = "monospace"; + makeItWrapAfter(cm, Pos(0, 5)); + + var steps = str.length - countIf(str.split(""), function(ch) { return extendingChars.test(ch) }); + var lineBreaks = {} + lineBreaks[6 - countIf(str.substr(0, 5).split(""), function(ch) { return extendingChars.test(ch) })] = 'w'; + if (str.indexOf("\n") != -1) { + lineBreaks[steps - 2] = 'n'; + } + + // Make sure we are at the visual beginning of the first line + cm.execCommand("goLineStart"); + + var prevCoords = cm.cursorCoords(), coords; + for(var i = 0; i < steps; ++i) { + cm.execCommand("goCharRight"); + coords = cm.cursorCoords(); + if ((i >= 10 && i <= 12) && !lineBreaks[i] && coords.left < prevCoords.left && coords.top > prevCoords.top) { + // The first line wraps twice + lineBreaks[i] = 'w'; + } + if (!lineBreaks[i]) { + is(coords.left > prevCoords.left, "In step " + i + ", cursor didn't move right"); + eq(coords.top, prevCoords.top, "In step " + i + ", cursor moved out of line"); + } else { + is(coords.left < prevCoords.left, i); + is(coords.top > prevCoords.top, i); + } + prevCoords = coords; + } + + cm.execCommand("goCharRight"); + coords = cm.cursorCoords(); + eq(coords.left, prevCoords.left, "Moving " + steps + " steps right didn't reach the end"); + eq(coords.top, prevCoords.top, "Moving " + steps + " steps right didn't reach the end"); + + for(i = steps - 1; i >= 0; --i) { + cm.execCommand("goCharLeft"); + coords = cm.cursorCoords(); + if (!(lineBreaks[i] == 'n' || lineBreaks[i + 1] == 'w')) { + is(coords.left < prevCoords.left, "In step " + i + ", cursor didn't move left"); + eq(coords.top, prevCoords.top, "In step " + i + ", cursor is not at the same line anymore"); + } else { + is(coords.left > prevCoords.left, i); + is(coords.top < prevCoords.top, i); + } + prevCoords = coords; + } + + cm.execCommand("goCharLeft"); + coords = cm.cursorCoords(); + eq(coords.left, prevCoords.left, "Moving " + steps + " steps left didn't reach the beginning"); + eq(coords.top, prevCoords.top, "Moving " + steps + " steps left didn't reach the beginning"); + }, {value: str, lineWrapping: true}) +}; + +function testMoveEndBidi(str) { + testCM("move_end_bidi_" + str, function(cm) { + cm.getScrollerElement().style.fontFamily = "monospace"; + makeItWrapAfter(cm, Pos(0, 5)); + + cm.execCommand("goLineStart"); + var pos = cm.doc.getCursor(); + cm.execCommand("goCharLeft"); + eqCursorPos(pos, cm.doc.getCursor()); + + cm.execCommand("goLineEnd"); + pos = cm.doc.getCursor(); + cm.execCommand("goColumnRight"); + eqCursorPos(pos, cm.doc.getCursor()); + }, {value: str, lineWrapping: true}) +}; + +var bidiTests = []; + +// We don't correctly implement L1 UBA +// See https://bugzilla.mozilla.org/show_bug.cgi?id=1331501 +// and https://bugs.chromium.org/p/chromium/issues/detail?id=673405 +/* +bidiTests.push("Say ا ب جabj\nS"); +bidiTests.push("Sayyy ا ا ب ج"); +*/ + +if (!phantom) { + bidiTests.push("Όȝǝڪȉۥ״ۺ׆ɀҩۏ\nҳ"); + bidiTests.push("ŌӰтقȤƥ٣ĎȺ١\nϚ"); + bidiTests.push("ٻоҤѕѽΩ־؉ïίքdz\nٵ"); + bidiTests.push("ĆՕƿɁǞϮؠȩóć\nď"); + bidiTests.push("RŨďңŪzϢŎƏԖڇڦ\nӈ"); + bidiTests.push("ό۷٢ԜһОצЉيčǟ\nѩ"); + bidiTests.push("ۑÚҳҕڬġڹհяųKV\nr"); + bidiTests.push("źڻғúہ4ם1Ƞc1a\nԁ"); + bidiTests.push("ҒȨҟփƞ٦ԓȦڰғâƥ\nڤ"); + bidiTests.push("ϖسՉȏŧΔԛdžĎӟیڡ\nέ"); + bidiTests.push("۹ؼL۵ĺȧКԙػא7״\nم"); + bidiTests.push("ن (ي)\u2009أقواس"); // thin space to throw off Firefox 51's broken white-space compressing behavior +} + +bidiTests.push("քմѧǮßپüŢҍҞўڳ\nӧ"); + +//bidiTests.push("Count ١ ٢ ٣ ٤"); +//bidiTests.push("ӣאƦϰ؊ȓېÛوը٬ز\nϪ"); +//bidiTests.push("ҾճٳџIՖӻ٥ڏ\nێ"); +//bidiTests.push("ҬÓФڂį٦Ͽɓڐͳٵ\nՈ"); +//bidiTests.push("aѴNijȻهˇ҃ڱӧǻֵ\na"); +//bidiTests.push(" a٧ا٢ ب جa\nS"); + +for (var i = 0; i < bidiTests.length; ++i) { + testMoveBidi(bidiTests[i]); + testMoveEndBidi(bidiTests[i]); +} + +/* +for (var i = 0; i < 5; ++i) { + testMoveBidi(getString(12) + "\n" + getString(1)); +} +*/ + +function testCoordsWrappedBidi(str) { + testCM("coords_wrapped_bidi_" + str, function(cm) { + cm.getScrollerElement().style.fontFamily = "monospace"; + makeItWrapAfter(cm, Pos(0, 5)); + + // Make sure we are at the visual beginning of the first line + var pos = Pos(0, 0), lastPos; + cm.doc.setCursor(pos); + do { + lastPos = pos; + cm.execCommand("goCharLeft"); + pos = cm.doc.getCursor(); + } while (pos != lastPos) + + var top = cm.charCoords(Pos(0, 0)).top, lastTop; + for (var i = 1; i < str.length; ++i) { + lastTop = top; + top = cm.charCoords(Pos(0, i)).top; + is(top >= lastTop); + } + }, {value: str, lineWrapping: true}) +}; + +testCoordsWrappedBidi("Count ١ ٢ ٣ ٤"); +/* +for (var i = 0; i < 5; ++i) { + testCoordsWrappedBidi(getString(50)); +} +*/ + +testCM("rtl_wrapped_selection", function(cm) { + cm.setSelection(Pos(0, 10), Pos(0, 190)) + is(byClassName(cm.getWrapperElement(), "CodeMirror-selected").length >= 3) +}, {value: new Array(10).join(" فتي تم تضمينها فتي تم"), lineWrapping: true}) + +testCM("bidi_wrapped_selection", function(cm) { + if (phantom) return + cm.setSize(cm.charCoords(Pos(0, 10), "editor").left) + cm.setSelection(Pos(0, 37), Pos(0, 80)) + var blocks = byClassName(cm.getWrapperElement(), "CodeMirror-selected") + is(blocks.length >= 2) + is(blocks.length <= 3) + var boxTop = blocks[0].getBoundingClientRect(), boxBot = blocks[blocks.length - 1].getBoundingClientRect() + is(boxTop.left > cm.charCoords(Pos(0, 1)).right) + is(boxBot.right < cm.charCoords(Pos(0, cm.getLine(0).length - 2)).left) +}, {value: "<p>مفتي11 تم تضمينهفتي تم تضمينها فتي تفتي تم تضمينها فتي تفتي تم تضمينها فتي تفتي تم تضمينها فتي تا فت10ي ت</p>", lineWrapping: true}) + +testCM("delete_wrapped", function(cm) { + makeItWrapAfter(cm, Pos(0, 2)); + cm.doc.setCursor(Pos(0, 3, "after")); + cm.deleteH(-1, "char"); + eq(cm.getLine(0), "1245"); +}, {value: "12345", lineWrapping: true}) + +testCM("issue_4878", function(cm) { + if (phantom) return + cm.setCursor(Pos(1, 12, "after")); + cm.moveH(-1, "char"); + eqCursorPos(cm.getCursor(), Pos(0, 113, "before")); +}, {value: " في تطبيق السمات مرة واحدة https://github.com/codemirror/CodeMirror/issues/4878#issuecomment-330550964على سبيل المثال <code>\"foo bar\"</code>\n" + +" سيتم تعيين", direction: "rtl", lineWrapping: true}); + +CodeMirror.defineMode("lookahead_mode", function() { + // Colors text as atom if the line two lines down has an x in it + return { + token: function(stream) { + stream.skipToEnd() + return /x/.test(stream.lookAhead(2)) ? "atom" : null + } + } +}) + +testCM("mode_lookahead", function(cm) { + eq(cm.getTokenAt(Pos(0, 1)).type, "atom") + eq(cm.getTokenAt(Pos(1, 1)).type, "atom") + eq(cm.getTokenAt(Pos(2, 1)).type, null) + cm.replaceRange("\n", Pos(2, 0)) + eq(cm.getTokenAt(Pos(0, 1)).type, null) + eq(cm.getTokenAt(Pos(1, 1)).type, "atom") +}, {value: "foo\na\nx\nx\n", mode: "lookahead_mode"}) diff --git a/devtools/client/shared/sourceeditor/test/codemirror/vim_test.js b/devtools/client/shared/sourceeditor/test/codemirror/vim_test.js new file mode 100644 index 0000000000..72a9896b59 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/vim_test.js @@ -0,0 +1,4729 @@ +var Pos = CodeMirror.Pos; +CodeMirror.Vim.suppressErrorLogging = true; + +var code = '' + +' wOrd1 (#%\n' + +' word3] \n' + +'aopop pop 0 1 2 3 4\n' + +' (a) [b] {c} \n' + +'int getchar(void) {\n' + +' static char buf[BUFSIZ];\n' + +' static char *bufp = buf;\n' + +' if (n == 0) { /* buffer is empty */\n' + +' n = read(0, buf, sizeof buf);\n' + +' bufp = buf;\n' + +' }\n' + +'\n' + +' return (--n >= 0) ? (unsigned char) *bufp++ : EOF;\n' + +' \n' + +'}\n'; + +var lines = (function() { + lineText = code.split('\n'); + var ret = []; + for (var i = 0; i < lineText.length; i++) { + ret[i] = { + line: i, + length: lineText[i].length, + lineText: lineText[i], + textStart: /^\s*/.exec(lineText[i])[0].length + }; + } + return ret; +})(); +var endOfDocument = makeCursor(lines.length - 1, + lines[lines.length - 1].length); +var wordLine = lines[0]; +var bigWordLine = lines[1]; +var charLine = lines[2]; +var bracesLine = lines[3]; +var seekBraceLine = lines[4]; + +var word1 = { + start: new Pos(wordLine.line, 1), + end: new Pos(wordLine.line, 5) +}; +var word2 = { + start: new Pos(wordLine.line, word1.end.ch + 2), + end: new Pos(wordLine.line, word1.end.ch + 4) +}; +var word3 = { + start: new Pos(bigWordLine.line, 1), + end: new Pos(bigWordLine.line, 5) +}; +var bigWord1 = word1; +var bigWord2 = word2; +var bigWord3 = { + start: new Pos(bigWordLine.line, 1), + end: new Pos(bigWordLine.line, 7) +}; +var bigWord4 = { + start: new Pos(bigWordLine.line, bigWord1.end.ch + 3), + end: new Pos(bigWordLine.line, bigWord1.end.ch + 7) +}; + +var oChars = [ new Pos(charLine.line, 1), + new Pos(charLine.line, 3), + new Pos(charLine.line, 7) ]; +var pChars = [ new Pos(charLine.line, 2), + new Pos(charLine.line, 4), + new Pos(charLine.line, 6), + new Pos(charLine.line, 8) ]; +var numChars = [ new Pos(charLine.line, 10), + new Pos(charLine.line, 12), + new Pos(charLine.line, 14), + new Pos(charLine.line, 16), + new Pos(charLine.line, 18)]; +var parens1 = { + start: new Pos(bracesLine.line, 1), + end: new Pos(bracesLine.line, 3) +}; +var squares1 = { + start: new Pos(bracesLine.line, 5), + end: new Pos(bracesLine.line, 7) +}; +var curlys1 = { + start: new Pos(bracesLine.line, 9), + end: new Pos(bracesLine.line, 11) +}; +var seekOutside = { + start: new Pos(seekBraceLine.line, 1), + end: new Pos(seekBraceLine.line, 16) +}; +var seekInside = { + start: new Pos(seekBraceLine.line, 14), + end: new Pos(seekBraceLine.line, 11) +}; + +function copyCursor(cur) { + return new Pos(cur.line, cur.ch); +} + +function forEach(arr, func) { + for (var i = 0; i < arr.length; i++) { + func(arr[i], i, arr); + } +} + +function expectFail(fn) { + try { + fn(); + } catch(expected) { + return; + }; + throw new Error("Expected to throw an error"); +} + +function testVim(name, run, opts, expectedFail) { + var vimOpts = { + lineNumbers: true, + vimMode: true, + showCursorWhenSelecting: true, + value: code + }; + for (var prop in opts) { + if (opts.hasOwnProperty(prop)) { + vimOpts[prop] = opts[prop]; + } + } + return test('vim_' + name, function() { + var place = document.getElementById("testground"); + var cm = CodeMirror(place, vimOpts); + var vim = CodeMirror.Vim.maybeInitVimState_(cm); + + function doKeysFn(cm) { + return function(args) { + if (args instanceof Array) { + arguments = args; + } + for (var i = 0; i < arguments.length; i++) { + var result = CodeMirror.Vim.handleKey(cm, arguments[i]); + if (!result && cm.state.vim.insertMode) { + cm.replaceSelections(fillArray(arguments[i], cm.listSelections().length)); + } + } + } + } + function doInsertModeKeysFn(cm) { + return function(args) { + if (args instanceof Array) { arguments = args; } + function executeHandler(handler) { + if (typeof handler == 'string') { + CodeMirror.commands[handler](cm); + } else { + handler(cm); + } + return true; + } + for (var i = 0; i < arguments.length; i++) { + var key = arguments[i]; + // Find key in keymap and handle. + var handled = CodeMirror.lookupKey(key, cm.getOption('keyMap'), executeHandler, cm); + // Record for insert mode. + if (handled == "handled" && cm.state.vim.insertMode && arguments[i] != 'Esc') { + var lastChange = CodeMirror.Vim.getVimGlobalState_().macroModeState.lastInsertModeChanges; + if (lastChange && (key.indexOf('Delete') != -1 || key.indexOf('Backspace') != -1)) { + lastChange.changes.push(new CodeMirror.Vim.InsertModeKey(key)); + } + } + } + } + } + function doExFn(cm) { + return function(command) { + cm.openDialog = helpers.fakeOpenDialog(command); + helpers.doKeys(':'); + } + } + function assertCursorAtFn(cm) { + return function(line, ch) { + var pos; + if (ch == null && typeof line.line == 'number') { + pos = line; + } else { + pos = makeCursor(line, ch); + } + eqCursorPos(cm.getCursor(), pos); + } + } + function fakeOpenDialog(result) { + return function(text, callback) { + return callback(result); + } + } + function fakeOpenNotification(matcher) { + return function(text) { + matcher(text); + } + } + var helpers = { + doKeys: doKeysFn(cm), + // Warning: Only emulates keymap events, not character insertions. Use + // replaceRange to simulate character insertions. + // Keys are in CodeMirror format, NOT vim format. + doInsertModeKeys: doInsertModeKeysFn(cm), + doEx: doExFn(cm), + assertCursorAt: assertCursorAtFn(cm), + fakeOpenDialog: fakeOpenDialog, + fakeOpenNotification: fakeOpenNotification, + getRegisterController: function() { + return CodeMirror.Vim.getRegisterController(); + } + } + CodeMirror.Vim.resetVimGlobalState_(); + var successful = false; + var savedOpenNotification = cm.openNotification; + var savedOpenDialog = cm.openDialog; + try { + run(cm, vim, helpers); + successful = true; + } finally { + cm.openNotification = savedOpenNotification; + cm.openDialog = savedOpenDialog; + if (!successful || verbose) { + place.style.visibility = "visible"; + } else { + place.removeChild(cm.getWrapperElement()); + } + } + }, expectedFail); +}; +testVim('qq@q', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'q', 'l', 'l', 'q'); + helpers.assertCursorAt(0,2); + helpers.doKeys('@', 'q'); + helpers.assertCursorAt(0,4); +}, { value: ' '}); +testVim('@@', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'q', 'l', 'l', 'q'); + helpers.assertCursorAt(0,2); + helpers.doKeys('@', 'q'); + helpers.assertCursorAt(0,4); + helpers.doKeys('@', '@'); + helpers.assertCursorAt(0,6); +}, { value: ' '}); +var jumplistScene = ''+ + 'word\n'+ + '(word)\n'+ + '{word\n'+ + 'word.\n'+ + '\n'+ + 'word search\n'+ + '}word\n'+ + 'word\n'+ + 'word\n'; +function testJumplist(name, keys, endPos, startPos, dialog) { + endPos = makeCursor(endPos[0], endPos[1]); + startPos = makeCursor(startPos[0], startPos[1]); + testVim(name, function(cm, vim, helpers) { + CodeMirror.Vim.resetVimGlobalState_(); + if(dialog)cm.openDialog = helpers.fakeOpenDialog('word'); + cm.setCursor(startPos); + helpers.doKeys.apply(null, keys); + helpers.assertCursorAt(endPos); + }, {value: jumplistScene}); +} +testJumplist('jumplist_H', ['H', '<C-o>'], [5,2], [5,2]); +testJumplist('jumplist_M', ['M', '<C-o>'], [2,2], [2,2]); +testJumplist('jumplist_L', ['L', '<C-o>'], [2,2], [2,2]); +testJumplist('jumplist_[[', ['[', '[', '<C-o>'], [5,2], [5,2]); +testJumplist('jumplist_]]', [']', ']', '<C-o>'], [2,2], [2,2]); +testJumplist('jumplist_G', ['G', '<C-o>'], [5,2], [5,2]); +testJumplist('jumplist_gg', ['g', 'g', '<C-o>'], [5,2], [5,2]); +testJumplist('jumplist_%', ['%', '<C-o>'], [1,5], [1,5]); +testJumplist('jumplist_{', ['{', '<C-o>'], [1,5], [1,5]); +testJumplist('jumplist_}', ['}', '<C-o>'], [1,5], [1,5]); +testJumplist('jumplist_\'', ['m', 'a', 'h', '\'', 'a', 'h', '<C-i>'], [1,0], [1,5]); +testJumplist('jumplist_`', ['m', 'a', 'h', '`', 'a', 'h', '<C-i>'], [1,5], [1,5]); +testJumplist('jumplist_*_cachedCursor', ['*', '<C-o>'], [1,3], [1,3]); +testJumplist('jumplist_#_cachedCursor', ['#', '<C-o>'], [1,3], [1,3]); +testJumplist('jumplist_n', ['#', 'n', '<C-o>'], [1,1], [2,3]); +testJumplist('jumplist_N', ['#', 'N', '<C-o>'], [1,1], [2,3]); +testJumplist('jumplist_repeat_<c-o>', ['*', '*', '*', '3', '<C-o>'], [2,3], [2,3]); +testJumplist('jumplist_repeat_<c-i>', ['*', '*', '*', '3', '<C-o>', '2', '<C-i>'], [5,0], [2,3]); +testJumplist('jumplist_repeated_motion', ['3', '*', '<C-o>'], [2,3], [2,3]); +testJumplist('jumplist_/', ['/', '<C-o>'], [2,3], [2,3], 'dialog'); +testJumplist('jumplist_?', ['?', '<C-o>'], [2,3], [2,3], 'dialog'); +testJumplist('jumplist_skip_deleted_mark<c-o>', + ['*', 'n', 'n', 'k', 'd', 'k', '<C-o>', '<C-o>', '<C-o>'], + [0,2], [0,2]); +testJumplist('jumplist_skip_deleted_mark<c-i>', + ['*', 'n', 'n', 'k', 'd', 'k', '<C-o>', '<C-i>', '<C-i>'], + [1,0], [0,2]); + +/** + * @param name Name of the test + * @param keys An array of keys or a string with a single key to simulate. + * @param endPos The expected end position of the cursor. + * @param startPos The position the cursor should start at, defaults to 0, 0. + */ +function testMotion(name, keys, endPos, startPos) { + testVim(name, function(cm, vim, helpers) { + if (!startPos) { + startPos = new Pos(0, 0); + } + cm.setCursor(startPos); + helpers.doKeys(keys); + helpers.assertCursorAt(endPos); + }); +} + +function makeCursor(line, ch) { + return new Pos(line, ch); +} + +function offsetCursor(cur, offsetLine, offsetCh) { + return new Pos(cur.line + offsetLine, cur.ch + offsetCh); +} + +// Motion tests +testMotion('|', '|', makeCursor(0, 0), makeCursor(0,4)); +testMotion('|_repeat', ['3', '|'], makeCursor(0, 2), makeCursor(0,4)); +testMotion('h', 'h', makeCursor(0, 0), word1.start); +testMotion('h_repeat', ['3', 'h'], offsetCursor(word1.end, 0, -3), word1.end); +testMotion('l', 'l', makeCursor(0, 1)); +testMotion('l_repeat', ['2', 'l'], makeCursor(0, 2)); +testMotion('j', 'j', offsetCursor(word1.end, 1, 0), word1.end); +testMotion('j_repeat', ['2', 'j'], offsetCursor(word1.end, 2, 0), word1.end); +testMotion('j_repeat_clip', ['1000', 'j'], endOfDocument); +testMotion('k', 'k', offsetCursor(word3.end, -1, 0), word3.end); +testMotion('k_repeat', ['2', 'k'], makeCursor(0, 4), makeCursor(2, 4)); +testMotion('k_repeat_clip', ['1000', 'k'], makeCursor(0, 4), makeCursor(2, 4)); +testMotion('w', 'w', word1.start); +testMotion('keepHPos', ['5', 'j', 'j', '7', 'k'], makeCursor(8, 12), makeCursor(12, 12)); +testMotion('keepHPosEol', ['$', '2', 'j'], makeCursor(2, 18)); +testMotion('w_multiple_newlines_no_space', 'w', makeCursor(12, 2), makeCursor(11, 2)); +testMotion('w_multiple_newlines_with_space', 'w', makeCursor(14, 0), makeCursor(12, 51)); +testMotion('w_repeat', ['2', 'w'], word2.start); +testMotion('w_wrap', ['w'], word3.start, word2.start); +testMotion('w_endOfDocument', 'w', endOfDocument, endOfDocument); +testMotion('w_start_to_end', ['1000', 'w'], endOfDocument, makeCursor(0, 0)); +testMotion('W', 'W', bigWord1.start); +testMotion('W_repeat', ['2', 'W'], bigWord3.start, bigWord1.start); +testMotion('e', 'e', word1.end); +testMotion('e_repeat', ['2', 'e'], word2.end); +testMotion('e_wrap', 'e', word3.end, word2.end); +testMotion('e_endOfDocument', 'e', endOfDocument, endOfDocument); +testMotion('e_start_to_end', ['1000', 'e'], endOfDocument, makeCursor(0, 0)); +testMotion('b', 'b', word3.start, word3.end); +testMotion('b_repeat', ['2', 'b'], word2.start, word3.end); +testMotion('b_wrap', 'b', word2.start, word3.start); +testMotion('b_startOfDocument', 'b', makeCursor(0, 0), makeCursor(0, 0)); +testMotion('b_end_to_start', ['1000', 'b'], makeCursor(0, 0), endOfDocument); +testMotion('ge', ['g', 'e'], word2.end, word3.end); +testMotion('ge_repeat', ['2', 'g', 'e'], word1.end, word3.start); +testMotion('ge_wrap', ['g', 'e'], word2.end, word3.start); +testMotion('ge_startOfDocument', ['g', 'e'], makeCursor(0, 0), + makeCursor(0, 0)); +testMotion('ge_end_to_start', ['1000', 'g', 'e'], makeCursor(0, 0), endOfDocument); +testMotion('gg', ['g', 'g'], makeCursor(lines[0].line, lines[0].textStart), + makeCursor(3, 1)); +testMotion('gg_repeat', ['3', 'g', 'g'], + makeCursor(lines[2].line, lines[2].textStart)); +testMotion('G', 'G', + makeCursor(lines[lines.length - 1].line, lines[lines.length - 1].textStart), + makeCursor(3, 1)); +testMotion('G_repeat', ['3', 'G'], makeCursor(lines[2].line, + lines[2].textStart)); +// TODO: Make the test code long enough to test Ctrl-F and Ctrl-B. +testMotion('0', '0', makeCursor(0, 0), makeCursor(0, 8)); +testMotion('^', '^', makeCursor(0, lines[0].textStart), makeCursor(0, 8)); +testMotion('+', '+', makeCursor(1, lines[1].textStart), makeCursor(0, 8)); +testMotion('-', '-', makeCursor(0, lines[0].textStart), makeCursor(1, 4)); +testMotion('_', ['6','_'], makeCursor(5, lines[5].textStart), makeCursor(0, 8)); +testMotion('$', '$', makeCursor(0, lines[0].length - 1), makeCursor(0, 1)); +testMotion('$_repeat', ['2', '$'], makeCursor(1, lines[1].length - 1), + makeCursor(0, 3)); +testMotion('f', ['f', 'p'], pChars[0], makeCursor(charLine.line, 0)); +testMotion('f_repeat', ['2', 'f', 'p'], pChars[2], pChars[0]); +testMotion('f_num', ['f', '2'], numChars[2], makeCursor(charLine.line, 0)); +testMotion('t', ['t','p'], offsetCursor(pChars[0], 0, -1), + makeCursor(charLine.line, 0)); +testMotion('t_repeat', ['2', 't', 'p'], offsetCursor(pChars[2], 0, -1), + pChars[0]); +testMotion('F', ['F', 'p'], pChars[0], pChars[1]); +testMotion('F_repeat', ['2', 'F', 'p'], pChars[0], pChars[2]); +testMotion('T', ['T', 'p'], offsetCursor(pChars[0], 0, 1), pChars[1]); +testMotion('T_repeat', ['2', 'T', 'p'], offsetCursor(pChars[0], 0, 1), pChars[2]); +testMotion('%_parens', ['%'], parens1.end, parens1.start); +testMotion('%_squares', ['%'], squares1.end, squares1.start); +testMotion('%_braces', ['%'], curlys1.end, curlys1.start); +testMotion('%_seek_outside', ['%'], seekOutside.end, seekOutside.start); +testMotion('%_seek_inside', ['%'], seekInside.end, seekInside.start); +testVim('%_seek_skip', function(cm, vim, helpers) { + cm.setCursor(0,0); + helpers.doKeys(['%']); + helpers.assertCursorAt(0,9); +}, {value:'01234"("()'}); +testVim('%_skip_string', function(cm, vim, helpers) { + cm.setCursor(0,0); + helpers.doKeys(['%']); + helpers.assertCursorAt(0,4); + cm.setCursor(0,2); + helpers.doKeys(['%']); + helpers.assertCursorAt(0,0); +}, {value:'(")")'}); +testVim('%_skip_comment', function(cm, vim, helpers) { + cm.setCursor(0,0); + helpers.doKeys(['%']); + helpers.assertCursorAt(0,6); + cm.setCursor(0,3); + helpers.doKeys(['%']); + helpers.assertCursorAt(0,0); +}, {value:'(/*)*/)'}); +// Make sure that moving down after going to the end of a line always leaves you +// at the end of a line, but preserves the offset in other cases +testVim('Changing lines after Eol operation', function(cm, vim, helpers) { + cm.setCursor(0,0); + helpers.doKeys(['$']); + helpers.doKeys(['j']); + // After moving to Eol and then down, we should be at Eol of line 2 + helpers.assertCursorAt(new Pos(1, lines[1].length - 1)); + helpers.doKeys(['j']); + // After moving down, we should be at Eol of line 3 + helpers.assertCursorAt(new Pos(2, lines[2].length - 1)); + helpers.doKeys(['h']); + helpers.doKeys(['j']); + // After moving back one space and then down, since line 4 is shorter than line 2, we should + // be at Eol of line 2 - 1 + helpers.assertCursorAt(new Pos(3, lines[3].length - 1)); + helpers.doKeys(['j']); + helpers.doKeys(['j']); + // After moving down again, since line 3 has enough characters, we should be back to the + // same place we were at on line 1 + helpers.assertCursorAt(new Pos(5, lines[2].length - 2)); +}); +//making sure gj and gk recover from clipping +testVim('gj_gk_clipping', function(cm,vim,helpers){ + cm.setCursor(0, 1); + helpers.doKeys('g','j','g','j'); + helpers.assertCursorAt(2, 1); + helpers.doKeys('g','k','g','k'); + helpers.assertCursorAt(0, 1); +},{value: 'line 1\n\nline 2'}); +//testing a mix of j/k and gj/gk +testVim('j_k_and_gj_gk', function(cm,vim,helpers){ + cm.setSize(120); + cm.setCursor(0, 0); + //go to the last character on the first line + helpers.doKeys('$'); + //move up/down on the column within the wrapped line + //side-effect: cursor is not locked to eol anymore + helpers.doKeys('g','k'); + var cur=cm.getCursor(); + eq(cur.line,0); + is((cur.ch<176),'gk didn\'t move cursor back (1)'); + helpers.doKeys('g','j'); + helpers.assertCursorAt(0, 176); + //should move to character 177 on line 2 (j/k preserve character index within line) + helpers.doKeys('j'); + //due to different line wrapping, the cursor can be on a different screen-x now + //gj and gk preserve screen-x on movement, much like moveV + helpers.doKeys('3','g','k'); + cur=cm.getCursor(); + eq(cur.line,1); + is((cur.ch<176),'gk didn\'t move cursor back (2)'); + helpers.doKeys('g','j','2','g','j'); + //should return to the same character-index + helpers.doKeys('k'); + helpers.assertCursorAt(0, 176); +},{ lineWrapping:true, value: 'This line is intentially long to test movement of gj and gk over wrapped lines. I will start on the end of this line, then make a step up and back to set the origin for j and k.\nThis line is supposed to be even longer than the previous. I will jump here and make another wiggle with gj and gk, before I jump back to the line above. Both wiggles should not change my cursor\'s target character but both j/k and gj/gk change each other\'s reference position.'}); +testVim('gj_gk', function(cm, vim, helpers) { + if (phantom) return; + cm.setSize(120); + // Test top of document edge case. + cm.setCursor(0, 4); + helpers.doKeys('g', 'j'); + helpers.doKeys('10', 'g', 'k'); + helpers.assertCursorAt(0, 4); + + // Test moving down preserves column position. + helpers.doKeys('g', 'j'); + var pos1 = cm.getCursor(); + var expectedPos2 = new Pos(0, (pos1.ch - 4) * 2 + 4); + helpers.doKeys('g', 'j'); + helpers.assertCursorAt(expectedPos2); + + // Move to the last character + cm.setCursor(0, 0); + // Move left to reset HSPos + helpers.doKeys('h'); + // Test bottom of document edge case. + helpers.doKeys('100', 'g', 'j'); + var endingPos = cm.getCursor(); + is(endingPos != 0, 'gj should not be on wrapped line 0'); + var topLeftCharCoords = cm.charCoords(makeCursor(0, 0)); + var endingCharCoords = cm.charCoords(endingPos); + is(topLeftCharCoords.left == endingCharCoords.left, 'gj should end up on column 0'); +},{ lineNumbers: false, lineWrapping:true, value: 'Thislineisintentionallylongtotestmovementofgjandgkoverwrappedlines.' }); +testVim('}', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('}'); + helpers.assertCursorAt(1, 0); + cm.setCursor(0, 0); + helpers.doKeys('2', '}'); + helpers.assertCursorAt(4, 0); + cm.setCursor(0, 0); + helpers.doKeys('6', '}'); + helpers.assertCursorAt(5, 0); +}, { value: 'a\n\nb\nc\n\nd' }); +testVim('{', function(cm, vim, helpers) { + cm.setCursor(5, 0); + helpers.doKeys('{'); + helpers.assertCursorAt(4, 0); + cm.setCursor(5, 0); + helpers.doKeys('2', '{'); + helpers.assertCursorAt(1, 0); + cm.setCursor(5, 0); + helpers.doKeys('6', '{'); + helpers.assertCursorAt(0, 0); +}, { value: 'a\n\nb\nc\n\nd' }); +testVim('(', function(cm, vim, helpers) { + cm.setCursor(6, 23); + helpers.doKeys('('); + helpers.assertCursorAt(6, 14); + helpers.doKeys('2', '('); + helpers.assertCursorAt(5, 0); + helpers.doKeys('('); + helpers.assertCursorAt(4, 0); + helpers.doKeys('('); + helpers.assertCursorAt(3, 0); + helpers.doKeys('('); + helpers.assertCursorAt(2, 0); + helpers.doKeys('('); + helpers.assertCursorAt(0, 0); + helpers.doKeys('('); + helpers.assertCursorAt(0, 0); +}, { value: 'sentence1.\n\n\nsentence2\n\nsentence3. sentence4\n sentence5? sentence6!' }); +testVim(')', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('2', ')'); + helpers.assertCursorAt(3, 0); + helpers.doKeys(')'); + helpers.assertCursorAt(4, 0); + helpers.doKeys(')'); + helpers.assertCursorAt(5, 0); + helpers.doKeys(')'); + helpers.assertCursorAt(5, 11); + helpers.doKeys(')'); + helpers.assertCursorAt(6, 14); + helpers.doKeys(')'); + helpers.assertCursorAt(6, 23); + helpers.doKeys(')'); + helpers.assertCursorAt(6, 23); +}, { value: 'sentence1.\n\n\nsentence2\n\nsentence3. sentence4\n sentence5? sentence6!' }); +testVim('paragraph_motions', function(cm, vim, helpers) { + cm.setCursor(10, 0); + helpers.doKeys('{'); + helpers.assertCursorAt(4, 0); + helpers.doKeys('{'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('2', '}'); + helpers.assertCursorAt(7, 0); + helpers.doKeys('2', '}'); + helpers.assertCursorAt(16, 0); + + cm.setCursor(9, 0); + helpers.doKeys('}'); + helpers.assertCursorAt(14, 0); + + cm.setCursor(6, 0); + helpers.doKeys('}'); + helpers.assertCursorAt(7, 0); + + // ip inside empty space + cm.setCursor(10, 0); + helpers.doKeys('v', 'i', 'p'); + eqCursorPos(Pos(7, 0), cm.getCursor('anchor')); + eqCursorPos(Pos(12, 0), cm.getCursor('head')); + helpers.doKeys('i', 'p'); + eqCursorPos(Pos(7, 0), cm.getCursor('anchor')); + eqCursorPos(Pos(13, 1), cm.getCursor('head')); + helpers.doKeys('2', 'i', 'p'); + eqCursorPos(Pos(7, 0), cm.getCursor('anchor')); + eqCursorPos(Pos(16, 1), cm.getCursor('head')); + + // should switch to visualLine mode + cm.setCursor(14, 0); + helpers.doKeys('<Esc>', 'v', 'i', 'p'); + helpers.assertCursorAt(14, 0); + + cm.setCursor(14, 0); + helpers.doKeys('<Esc>', 'V', 'i', 'p'); + eqCursorPos(Pos(16, 1), cm.getCursor('head')); + + // ap inside empty space + cm.setCursor(10, 0); + helpers.doKeys('<Esc>', 'v', 'a', 'p'); + eqCursorPos(Pos(7, 0), cm.getCursor('anchor')); + eqCursorPos(Pos(13, 1), cm.getCursor('head')); + helpers.doKeys('a', 'p'); + eqCursorPos(Pos(7, 0), cm.getCursor('anchor')); + eqCursorPos(Pos(16, 1), cm.getCursor('head')); + + cm.setCursor(13, 0); + helpers.doKeys('v', 'a', 'p'); + eqCursorPos(Pos(13, 0), cm.getCursor('anchor')); + eqCursorPos(Pos(14, 0), cm.getCursor('head')); + + cm.setCursor(16, 0); + helpers.doKeys('v', 'a', 'p'); + eqCursorPos(Pos(14, 0), cm.getCursor('anchor')); + eqCursorPos(Pos(16, 1), cm.getCursor('head')); + + cm.setCursor(0, 0); + helpers.doKeys('v', 'a', 'p'); + eqCursorPos(Pos(0, 0), cm.getCursor('anchor')); + eqCursorPos(Pos(4, 0), cm.getCursor('head')); + + cm.setCursor(0, 0); + helpers.doKeys('d', 'i', 'p'); + var register = helpers.getRegisterController().getRegister(); + eq('a\na\n', register.toString()); + is(register.linewise); + helpers.doKeys('3', 'j', 'p'); + helpers.doKeys('y', 'i', 'p'); + is(register.linewise); + eq('b\na\na\nc\n', register.toString()); +}, { value: 'a\na\n\n\n\nb\nc\n\n\n\n\n\n\nd\n\ne\nf' }); + +// Operator tests +testVim('dl', function(cm, vim, helpers) { + var curStart = makeCursor(0, 0); + cm.setCursor(curStart); + helpers.doKeys('d', 'l'); + eq('word1 ', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq(' ', register.toString()); + is(!register.linewise); + eqCursorPos(curStart, cm.getCursor()); +}, { value: ' word1 ' }); +testVim('dl_eol', function(cm, vim, helpers) { + cm.setCursor(0, 6); + helpers.doKeys('d', 'l'); + eq(' word1', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq(' ', register.toString()); + is(!register.linewise); + helpers.assertCursorAt(0, 5); +}, { value: ' word1 ' }); +testVim('dl_repeat', function(cm, vim, helpers) { + var curStart = makeCursor(0, 0); + cm.setCursor(curStart); + helpers.doKeys('2', 'd', 'l'); + eq('ord1 ', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq(' w', register.toString()); + is(!register.linewise); + eqCursorPos(curStart, cm.getCursor()); +}, { value: ' word1 ' }); +testVim('dh', function(cm, vim, helpers) { + var curStart = makeCursor(0, 3); + cm.setCursor(curStart); + helpers.doKeys('d', 'h'); + eq(' wrd1 ', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('o', register.toString()); + is(!register.linewise); + eqCursorPos(offsetCursor(curStart, 0 , -1), cm.getCursor()); +}, { value: ' word1 ' }); +testVim('dj', function(cm, vim, helpers) { + var curStart = makeCursor(0, 3); + cm.setCursor(curStart); + helpers.doKeys('d', 'j'); + eq(' word3', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq(' word1\nword2\n', register.toString()); + is(register.linewise); + helpers.assertCursorAt(0, 1); +}, { value: ' word1\nword2\n word3' }); +testVim('dj_end_of_document', function(cm, vim, helpers) { + var curStart = makeCursor(0, 3); + cm.setCursor(curStart); + helpers.doKeys('d', 'j'); + eq('', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq(' word1 \n', register.toString()); + is(register.linewise); + helpers.assertCursorAt(0, 0); +}, { value: ' word1 ' }); +testVim('dk', function(cm, vim, helpers) { + var curStart = makeCursor(1, 3); + cm.setCursor(curStart); + helpers.doKeys('d', 'k'); + eq(' word3', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq(' word1\nword2\n', register.toString()); + is(register.linewise); + helpers.assertCursorAt(0, 1); +}, { value: ' word1\nword2\n word3' }); +testVim('dk_start_of_document', function(cm, vim, helpers) { + var curStart = makeCursor(0, 3); + cm.setCursor(curStart); + helpers.doKeys('d', 'k'); + eq('', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq(' word1 \n', register.toString()); + is(register.linewise); + helpers.assertCursorAt(0, 0); +}, { value: ' word1 ' }); +testVim('dw_space', function(cm, vim, helpers) { + var curStart = makeCursor(0, 0); + cm.setCursor(curStart); + helpers.doKeys('d', 'w'); + eq('word1 ', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq(' ', register.toString()); + is(!register.linewise); + eqCursorPos(curStart, cm.getCursor()); +}, { value: ' word1 ' }); +testVim('dw_word', function(cm, vim, helpers) { + var curStart = makeCursor(0, 1); + cm.setCursor(curStart); + helpers.doKeys('d', 'w'); + eq(' word2', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('word1 ', register.toString()); + is(!register.linewise); + eqCursorPos(curStart, cm.getCursor()); +}, { value: ' word1 word2' }); +testVim('dw_unicode_word', function(cm, vim, helpers) { + helpers.doKeys('d', 'w'); + eq(cm.getValue().length, 10); + helpers.doKeys('d', 'w'); + eq(cm.getValue().length, 6); + helpers.doKeys('d', 'w'); + eq(cm.getValue().length, 5); + helpers.doKeys('d', 'e'); + eq(cm.getValue().length, 2); +}, { value: ' \u0562\u0561\u0580\u0587\xbbe\xb5g ' }); +testVim('dw_only_word', function(cm, vim, helpers) { + // Test that if there is only 1 word left, dw deletes till the end of the + // line. + cm.setCursor(0, 1); + helpers.doKeys('d', 'w'); + eq(' ', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('word1 ', register.toString()); + is(!register.linewise); + helpers.assertCursorAt(0, 0); +}, { value: ' word1 ' }); +testVim('dw_eol', function(cm, vim, helpers) { + // Assert that dw does not delete the newline if last word to delete is at end + // of line. + cm.setCursor(0, 1); + helpers.doKeys('d', 'w'); + eq(' \nword2', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('word1', register.toString()); + is(!register.linewise); + helpers.assertCursorAt(0, 0); +}, { value: ' word1\nword2' }); +testVim('dw_eol_with_multiple_newlines', function(cm, vim, helpers) { + // Assert that dw does not delete the newline if last word to delete is at end + // of line and it is followed by multiple newlines. + cm.setCursor(0, 1); + helpers.doKeys('d', 'w'); + eq(' \n\nword2', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('word1', register.toString()); + is(!register.linewise); + helpers.assertCursorAt(0, 0); +}, { value: ' word1\n\nword2' }); +testVim('dw_empty_line_followed_by_whitespace', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('d', 'w'); + eq(' \nword', cm.getValue()); +}, { value: '\n \nword' }); +testVim('dw_empty_line_followed_by_word', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('d', 'w'); + eq('word', cm.getValue()); +}, { value: '\nword' }); +testVim('dw_empty_line_followed_by_empty_line', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('d', 'w'); + eq('\n', cm.getValue()); +}, { value: '\n\n' }); +testVim('dw_whitespace_followed_by_whitespace', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('d', 'w'); + eq('\n \n', cm.getValue()); +}, { value: ' \n \n' }); +testVim('dw_whitespace_followed_by_empty_line', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('d', 'w'); + eq('\n\n', cm.getValue()); +}, { value: ' \n\n' }); +testVim('dw_word_whitespace_word', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('d', 'w'); + eq('\n \nword2', cm.getValue()); +}, { value: 'word1\n \nword2'}) +testVim('dw_end_of_document', function(cm, vim, helpers) { + cm.setCursor(1, 2); + helpers.doKeys('d', 'w'); + eq('\nab', cm.getValue()); +}, { value: '\nabc' }); +testVim('dw_repeat', function(cm, vim, helpers) { + // Assert that dw does delete newline if it should go to the next line, and + // that repeat works properly. + cm.setCursor(0, 1); + helpers.doKeys('d', '2', 'w'); + eq(' ', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('word1\nword2', register.toString()); + is(!register.linewise); + helpers.assertCursorAt(0, 0); +}, { value: ' word1\nword2' }); +testVim('de_word_start_and_empty_lines', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('d', 'e'); + eq('\n\n', cm.getValue()); +}, { value: 'word\n\n' }); +testVim('de_word_end_and_empty_lines', function(cm, vim, helpers) { + cm.setCursor(0, 3); + helpers.doKeys('d', 'e'); + eq('wor', cm.getValue()); +}, { value: 'word\n\n\n' }); +testVim('de_whitespace_and_empty_lines', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('d', 'e'); + eq('', cm.getValue()); +}, { value: ' \n\n\n' }); +testVim('de_end_of_document', function(cm, vim, helpers) { + cm.setCursor(1, 2); + helpers.doKeys('d', 'e'); + eq('\nab', cm.getValue()); +}, { value: '\nabc' }); +testVim('db_empty_lines', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doKeys('d', 'b'); + eq('\n\n', cm.getValue()); +}, { value: '\n\n\n' }); +testVim('db_word_start_and_empty_lines', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doKeys('d', 'b'); + eq('\nword', cm.getValue()); +}, { value: '\n\nword' }); +testVim('db_word_end_and_empty_lines', function(cm, vim, helpers) { + cm.setCursor(2, 3); + helpers.doKeys('d', 'b'); + eq('\n\nd', cm.getValue()); +}, { value: '\n\nword' }); +testVim('db_whitespace_and_empty_lines', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doKeys('d', 'b'); + eq('', cm.getValue()); +}, { value: '\n \n' }); +testVim('db_start_of_document', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('d', 'b'); + eq('abc\n', cm.getValue()); +}, { value: 'abc\n' }); +testVim('dge_empty_lines', function(cm, vim, helpers) { + cm.setCursor(1, 0); + helpers.doKeys('d', 'g', 'e'); + // Note: In real VIM the result should be '', but it's not quite consistent, + // since 2 newlines are deleted. But in the similar case of word\n\n, only + // 1 newline is deleted. We'll diverge from VIM's behavior since it's much + // easier this way. + eq('\n', cm.getValue()); +}, { value: '\n\n' }); +testVim('dge_word_and_empty_lines', function(cm, vim, helpers) { + cm.setCursor(1, 0); + helpers.doKeys('d', 'g', 'e'); + eq('wor\n', cm.getValue()); +}, { value: 'word\n\n'}); +testVim('dge_whitespace_and_empty_lines', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doKeys('d', 'g', 'e'); + eq('', cm.getValue()); +}, { value: '\n \n' }); +testVim('dge_start_of_document', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('d', 'g', 'e'); + eq('bc\n', cm.getValue()); +}, { value: 'abc\n' }); +testVim('d_inclusive', function(cm, vim, helpers) { + // Assert that when inclusive is set, the character the cursor is on gets + // deleted too. + var curStart = makeCursor(0, 1); + cm.setCursor(curStart); + helpers.doKeys('d', 'e'); + eq(' ', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('word1', register.toString()); + is(!register.linewise); + eqCursorPos(curStart, cm.getCursor()); +}, { value: ' word1 ' }); +testVim('d_reverse', function(cm, vim, helpers) { + // Test that deleting in reverse works. + cm.setCursor(1, 0); + helpers.doKeys('d', 'b'); + eq(' word2 ', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('word1\n', register.toString()); + is(!register.linewise); + helpers.assertCursorAt(0, 1); +}, { value: ' word1\nword2 ' }); +testVim('dd', function(cm, vim, helpers) { + cm.setCursor(0, 3); + var expectedBuffer = cm.getRange(new Pos(0, 0), + new Pos(1, 0)); + var expectedLineCount = cm.lineCount() - 1; + helpers.doKeys('d', 'd'); + eq(expectedLineCount, cm.lineCount()); + var register = helpers.getRegisterController().getRegister(); + eq(expectedBuffer, register.toString()); + is(register.linewise); + helpers.assertCursorAt(0, lines[1].textStart); +}); +testVim('dd_prefix_repeat', function(cm, vim, helpers) { + cm.setCursor(0, 3); + var expectedBuffer = cm.getRange(new Pos(0, 0), + new Pos(2, 0)); + var expectedLineCount = cm.lineCount() - 2; + helpers.doKeys('2', 'd', 'd'); + eq(expectedLineCount, cm.lineCount()); + var register = helpers.getRegisterController().getRegister(); + eq(expectedBuffer, register.toString()); + is(register.linewise); + helpers.assertCursorAt(0, lines[2].textStart); +}); +testVim('dd_motion_repeat', function(cm, vim, helpers) { + cm.setCursor(0, 3); + var expectedBuffer = cm.getRange(new Pos(0, 0), + new Pos(2, 0)); + var expectedLineCount = cm.lineCount() - 2; + helpers.doKeys('d', '2', 'd'); + eq(expectedLineCount, cm.lineCount()); + var register = helpers.getRegisterController().getRegister(); + eq(expectedBuffer, register.toString()); + is(register.linewise); + helpers.assertCursorAt(0, lines[2].textStart); +}); +testVim('dd_multiply_repeat', function(cm, vim, helpers) { + cm.setCursor(0, 3); + var expectedBuffer = cm.getRange(new Pos(0, 0), + new Pos(6, 0)); + var expectedLineCount = cm.lineCount() - 6; + helpers.doKeys('2', 'd', '3', 'd'); + eq(expectedLineCount, cm.lineCount()); + var register = helpers.getRegisterController().getRegister(); + eq(expectedBuffer, register.toString()); + is(register.linewise); + helpers.assertCursorAt(0, lines[6].textStart); +}); +testVim('dd_lastline', function(cm, vim, helpers) { + cm.setCursor(cm.lineCount(), 0); + var expectedLineCount = cm.lineCount() - 1; + helpers.doKeys('d', 'd'); + eq(expectedLineCount, cm.lineCount()); + helpers.assertCursorAt(cm.lineCount() - 1, 0); +}); +testVim('dd_only_line', function(cm, vim, helpers) { + cm.setCursor(0, 0); + var expectedRegister = cm.getValue() + "\n"; + helpers.doKeys('d','d'); + eq(1, cm.lineCount()); + eq('', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq(expectedRegister, register.toString()); +}, { value: "thisistheonlyline" }); +// Yank commands should behave the exact same as d commands, expect that nothing +// gets deleted. +testVim('yw_repeat', function(cm, vim, helpers) { + // Assert that yw does yank newline if it should go to the next line, and + // that repeat works properly. + var curStart = makeCursor(0, 1); + cm.setCursor(curStart); + helpers.doKeys('y', '2', 'w'); + eq(' word1\nword2', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('word1\nword2', register.toString()); + is(!register.linewise); + eqCursorPos(curStart, cm.getCursor()); +}, { value: ' word1\nword2' }); +testVim('yy_multiply_repeat', function(cm, vim, helpers) { + var curStart = makeCursor(0, 3); + cm.setCursor(curStart); + var expectedBuffer = cm.getRange(new Pos(0, 0), + new Pos(6, 0)); + var expectedLineCount = cm.lineCount(); + helpers.doKeys('2', 'y', '3', 'y'); + eq(expectedLineCount, cm.lineCount()); + var register = helpers.getRegisterController().getRegister(); + eq(expectedBuffer, register.toString()); + is(register.linewise); + eqCursorPos(curStart, cm.getCursor()); +}); +testVim('2dd_blank_P', function(cm, vim, helpers) { + helpers.doKeys('2', 'd', 'd', 'P'); + eq('\na\n\n', cm.getValue()); +}, { value: '\na\n\n' }); +// Change commands behave like d commands except that it also enters insert +// mode. In addition, when the change is linewise, an additional newline is +// inserted so that insert mode starts on that line. +testVim('cw', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('c', '2', 'w'); + eq(' word3', cm.getValue()); + helpers.assertCursorAt(0, 0); +}, { value: 'word1 word2 word3'}); +testVim('cw_repeat', function(cm, vim, helpers) { + // Assert that cw does delete newline if it should go to the next line, and + // that repeat works properly. + var curStart = makeCursor(0, 1); + cm.setCursor(curStart); + helpers.doKeys('c', '2', 'w'); + eq(' ', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('word1\nword2', register.toString()); + is(!register.linewise); + eqCursorPos(curStart, cm.getCursor()); + eq('vim-insert', cm.getOption('keyMap')); +}, { value: ' word1\nword2' }); +testVim('cc_multiply_repeat', function(cm, vim, helpers) { + cm.setCursor(0, 3); + var expectedBuffer = cm.getRange(new Pos(0, 0), + new Pos(6, 0)); + var expectedLineCount = cm.lineCount() - 5; + helpers.doKeys('2', 'c', '3', 'c'); + eq(expectedLineCount, cm.lineCount()); + var register = helpers.getRegisterController().getRegister(); + eq(expectedBuffer, register.toString()); + is(register.linewise); + eq('vim-insert', cm.getOption('keyMap')); +}); +testVim('ct', function(cm, vim, helpers) { + cm.setCursor(0, 9); + helpers.doKeys('c', 't', 'w'); + eq(' word1 word3', cm.getValue()); + helpers.doKeys('<Esc>', 'c', '|'); + eq(' word3', cm.getValue()); + helpers.assertCursorAt(0, 0); + helpers.doKeys('<Esc>', '2', 'u', 'w', 'h'); + helpers.doKeys('c', '2', 'g', 'e'); + eq(' wordword3', cm.getValue()); +}, { value: ' word1 word2 word3'}); +testVim('cc_should_not_append_to_document', function(cm, vim, helpers) { + var expectedLineCount = cm.lineCount(); + cm.setCursor(cm.lastLine(), 0); + helpers.doKeys('c', 'c'); + eq(expectedLineCount, cm.lineCount()); +}); +function fillArray(val, times) { + var arr = []; + for (var i = 0; i < times; i++) { + arr.push(val); + } + return arr; +} +testVim('c_visual_block', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('<C-v>', '2', 'j', 'l', 'l', 'l', 'c'); + helpers.doKeys('hello'); + eq('1hello\n5hello\nahellofg', cm.getValue()); + helpers.doKeys('<Esc>'); + cm.setCursor(2, 3); + helpers.doKeys('<C-v>', '2', 'k', 'h', 'C'); + helpers.doKeys('world'); + eq('1hworld\n5hworld\nahworld', cm.getValue()); +}, {value: '1234\n5678\nabcdefg'}); +testVim('c_visual_block_replay', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('<C-v>', '2', 'j', 'l', 'c'); + helpers.doKeys('fo'); + eq('1fo4\n5fo8\nafodefg', cm.getValue()); + helpers.doKeys('<Esc>'); + cm.setCursor(0, 0); + helpers.doKeys('.'); + eq('foo4\nfoo8\nfoodefg', cm.getValue()); +}, {value: '1234\n5678\nabcdefg'}); +testVim('I_visual_block_replay', function(cm, vim, helpers) { + cm.setCursor(0, 2); + helpers.doKeys('<C-v>', '2', 'j', 'l', 'I'); + helpers.doKeys('+-') + eq('12+-34\n56+-78\nab+-cdefg\nxyz', cm.getValue()); + helpers.doKeys('<Esc>'); + // ensure that repeat location doesn't depend on last selection + cm.setCursor(3, 2); + helpers.doKeys('g', 'v') + eq("+-34\n+-78\n+-cd", cm.getSelection()) + cm.setCursor(0, 3); + helpers.doKeys('<C-v>', '1', 'j', '2', 'l'); + eq("-34\n-78", cm.getSelection()); + cm.setCursor(0, 0); + eq("", cm.getSelection()); + helpers.doKeys('g', 'v'); + eq("-34\n-78", cm.getSelection()); + cm.setCursor(1, 1); + helpers.doKeys('.'); + eq('12+-34\n5+-6+-78\na+-b+-cdefg\nx+-yz', cm.getValue()); +}, {value: '1234\n5678\nabcdefg\nxyz'}); + +testVim('d_visual_block', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('<C-v>', '2', 'j', 'l', 'l', 'l', 'd'); + eq('1\n5\nafg', cm.getValue()); +}, {value: '1234\n5678\nabcdefg'}); +testVim('D_visual_block', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('<C-v>', '2', 'j', 'l', 'D'); + eq('1\n5\na', cm.getValue()); +}, {value: '1234\n5678\nabcdefg'}); + +testVim('s_visual_block', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('<C-v>', '2', 'j', 'l', 'l', 'l', 's'); + helpers.doKeys('hello{'); + eq('1hello{\n5hello{\nahello{fg\n', cm.getValue()); + helpers.doKeys('<Esc>'); + cm.setCursor(2, 3); + helpers.doKeys('<C-v>', '1', 'k', 'h', 'S'); + helpers.doKeys('world'); + eq('1hello{\n world\n', cm.getValue()); +}, {value: '1234\n5678\nabcdefg\n'}); + +// Swapcase commands edit in place and do not modify registers. +testVim('g~w_repeat', function(cm, vim, helpers) { + // Assert that dw does delete newline if it should go to the next line, and + // that repeat works properly. + var curStart = makeCursor(0, 1); + cm.setCursor(curStart); + helpers.doKeys('g', '~', '2', 'w'); + eq(' WORD1\nWORD2', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('', register.toString()); + is(!register.linewise); + eqCursorPos(curStart, cm.getCursor()); +}, { value: ' word1\nword2' }); +testVim('g~g~', function(cm, vim, helpers) { + var curStart = makeCursor(0, 3); + cm.setCursor(curStart); + var expectedLineCount = cm.lineCount(); + var expectedValue = cm.getValue().toUpperCase(); + helpers.doKeys('2', 'g', '~', '3', 'g', '~'); + eq(expectedValue, cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('', register.toString()); + is(!register.linewise); + eqCursorPos(curStart, cm.getCursor()); +}, { value: ' word1\nword2\nword3\nword4\nword5\nword6' }); +testVim('gu_and_gU', function(cm, vim, helpers) { + var curStart = makeCursor(0, 7); + var value = cm.getValue(); + cm.setCursor(curStart); + helpers.doKeys('2', 'g', 'U', 'w'); + eq(cm.getValue(), 'wa wb xX WC wd'); + eqCursorPos(curStart, cm.getCursor()); + helpers.doKeys('2', 'g', 'u', 'w'); + eq(cm.getValue(), value); + + helpers.doKeys('2', 'g', 'U', 'B'); + eq(cm.getValue(), 'wa WB Xx wc wd'); + eqCursorPos(makeCursor(0, 3), cm.getCursor()); + + cm.setCursor(makeCursor(0, 4)); + helpers.doKeys('g', 'u', 'i', 'w'); + eq(cm.getValue(), 'wa wb Xx wc wd'); + eqCursorPos(makeCursor(0, 3), cm.getCursor()); + + // TODO: support gUgU guu + // eqCursorPos(makeCursor(0, 0), cm.getCursor()); + + var register = helpers.getRegisterController().getRegister(); + eq('', register.toString()); + is(!register.linewise); +}, { value: 'wa wb xx wc wd' }); +testVim('visual_block_~', function(cm, vim, helpers) { + cm.setCursor(1, 1); + helpers.doKeys('<C-v>', 'l', 'l', 'j', '~'); + helpers.assertCursorAt(1, 1); + eq('hello\nwoRLd\naBCDe', cm.getValue()); + cm.setCursor(2, 0); + helpers.doKeys('v', 'l', 'l', '~'); + helpers.assertCursorAt(2, 0); + eq('hello\nwoRLd\nAbcDe', cm.getValue()); +},{value: 'hello\nwOrld\nabcde' }); +testVim('._swapCase_visualBlock', function(cm, vim, helpers) { + helpers.doKeys('<C-v>', 'j', 'j', 'l', '~'); + cm.setCursor(0, 3); + helpers.doKeys('.'); + eq('HelLO\nWorLd\nAbcdE', cm.getValue()); +},{value: 'hEllo\nwOrlD\naBcDe' }); +testVim('._delete_visualBlock', function(cm, vim, helpers) { + helpers.doKeys('<C-v>', 'j', 'x'); + eq('ive\ne\nsome\nsugar', cm.getValue()); + helpers.doKeys('.'); + eq('ve\n\nsome\nsugar', cm.getValue()); + helpers.doKeys('j', 'j', '.'); + eq('ve\n\nome\nugar', cm.getValue()); + helpers.doKeys('u', '<C-r>', '.'); + eq('ve\n\nme\ngar', cm.getValue()); +},{value: 'give\nme\nsome\nsugar' }); +testVim('>{motion}', function(cm, vim, helpers) { + cm.setCursor(1, 3); + var expectedLineCount = cm.lineCount(); + var expectedValue = ' word1\n word2\nword3 '; + helpers.doKeys('>', 'k'); + eq(expectedValue, cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('', register.toString()); + is(!register.linewise); + helpers.assertCursorAt(0, 3); +}, { value: ' word1\nword2\nword3 ', indentUnit: 2 }); +testVim('>>', function(cm, vim, helpers) { + cm.setCursor(0, 3); + var expectedLineCount = cm.lineCount(); + var expectedValue = ' word1\n word2\nword3 '; + helpers.doKeys('2', '>', '>'); + eq(expectedValue, cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('', register.toString()); + is(!register.linewise); + helpers.assertCursorAt(0, 3); +}, { value: ' word1\nword2\nword3 ', indentUnit: 2 }); +testVim('<{motion}', function(cm, vim, helpers) { + cm.setCursor(1, 3); + var expectedLineCount = cm.lineCount(); + var expectedValue = ' word1\nword2\nword3 '; + helpers.doKeys('<', 'k'); + eq(expectedValue, cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('', register.toString()); + is(!register.linewise); + helpers.assertCursorAt(0, 1); +}, { value: ' word1\n word2\nword3 ', indentUnit: 2 }); +testVim('<<', function(cm, vim, helpers) { + cm.setCursor(0, 3); + var expectedLineCount = cm.lineCount(); + var expectedValue = ' word1\nword2\nword3 '; + helpers.doKeys('2', '<', '<'); + eq(expectedValue, cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('', register.toString()); + is(!register.linewise); + helpers.assertCursorAt(0, 1); +}, { value: ' word1\n word2\nword3 ', indentUnit: 2 }); +testVim('=', function(cm, vim, helpers) { + cm.setCursor(0, 3); + helpers.doKeys('<C-v>', 'j', 'j'); + var expectedValue = 'word1\nword2\nword3'; + helpers.doKeys('='); + eq(expectedValue, cm.getValue()); +}, { value: ' word1\n word2\n word3', indentUnit: 2 }); + +// Edit tests +function testEdit(name, before, pos, edit, after) { + return testVim(name, function(cm, vim, helpers) { + var ch = before.search(pos) + var line = before.substring(0, ch).split('\n').length - 1; + if (line) { + ch = before.substring(0, ch).split('\n').pop().length; + } + cm.setCursor(line, ch); + helpers.doKeys.apply(this, edit.split('')); + eq(after, cm.getValue()); + }, {value: before}); +} + +// These Delete tests effectively cover word-wise Change, Visual & Yank. +// Tabs are used as differentiated whitespace to catch edge cases. +// Normal word: +testEdit('diw_mid_spc', 'foo \tbAr\t baz', /A/, 'diw', 'foo \t\t baz'); +testEdit('daw_mid_spc', 'foo \tbAr\t baz', /A/, 'daw', 'foo \tbaz'); +testEdit('diw_mid_punct', 'foo \tbAr.\t baz', /A/, 'diw', 'foo \t.\t baz'); +testEdit('daw_mid_punct', 'foo \tbAr.\t baz', /A/, 'daw', 'foo.\t baz'); +testEdit('diw_mid_punct2', 'foo \t,bAr.\t baz', /A/, 'diw', 'foo \t,.\t baz'); +testEdit('daw_mid_punct2', 'foo \t,bAr.\t baz', /A/, 'daw', 'foo \t,.\t baz'); +testEdit('diw_start_spc', 'bAr \tbaz', /A/, 'diw', ' \tbaz'); +testEdit('daw_start_spc', 'bAr \tbaz', /A/, 'daw', 'baz'); +testEdit('diw_start_punct', 'bAr. \tbaz', /A/, 'diw', '. \tbaz'); +testEdit('daw_start_punct', 'bAr. \tbaz', /A/, 'daw', '. \tbaz'); +testEdit('diw_end_spc', 'foo \tbAr', /A/, 'diw', 'foo \t'); +testEdit('daw_end_spc', 'foo \tbAr', /A/, 'daw', 'foo'); +testEdit('diw_end_punct', 'foo \tbAr.', /A/, 'diw', 'foo \t.'); +testEdit('daw_end_punct', 'foo \tbAr.', /A/, 'daw', 'foo.'); +// Big word: +testEdit('diW_mid_spc', 'foo \tbAr\t baz', /A/, 'diW', 'foo \t\t baz'); +testEdit('daW_mid_spc', 'foo \tbAr\t baz', /A/, 'daW', 'foo \tbaz'); +testEdit('diW_mid_punct', 'foo \tbAr.\t baz', /A/, 'diW', 'foo \t\t baz'); +testEdit('daW_mid_punct', 'foo \tbAr.\t baz', /A/, 'daW', 'foo \tbaz'); +testEdit('diW_mid_punct2', 'foo \t,bAr.\t baz', /A/, 'diW', 'foo \t\t baz'); +testEdit('daW_mid_punct2', 'foo \t,bAr.\t baz', /A/, 'daW', 'foo \tbaz'); +testEdit('diW_start_spc', 'bAr\t baz', /A/, 'diW', '\t baz'); +testEdit('daW_start_spc', 'bAr\t baz', /A/, 'daW', 'baz'); +testEdit('diW_start_punct', 'bAr.\t baz', /A/, 'diW', '\t baz'); +testEdit('daW_start_punct', 'bAr.\t baz', /A/, 'daW', 'baz'); +testEdit('diW_end_spc', 'foo \tbAr', /A/, 'diW', 'foo \t'); +testEdit('daW_end_spc', 'foo \tbAr', /A/, 'daW', 'foo'); +testEdit('diW_end_punct', 'foo \tbAr.', /A/, 'diW', 'foo \t'); +testEdit('daW_end_punct', 'foo \tbAr.', /A/, 'daW', 'foo'); +// Deleting text objects +// Open and close on same line +testEdit('di(_open_spc', 'foo (bAr) baz', /\(/, 'di(', 'foo () baz'); +testEdit('di)_open_spc', 'foo (bAr) baz', /\(/, 'di)', 'foo () baz'); +testEdit('dib_open_spc', 'foo (bAr) baz', /\(/, 'dib', 'foo () baz'); +testEdit('da(_open_spc', 'foo (bAr) baz', /\(/, 'da(', 'foo baz'); +testEdit('da)_open_spc', 'foo (bAr) baz', /\(/, 'da)', 'foo baz'); + +testEdit('di(_middle_spc', 'foo (bAr) baz', /A/, 'di(', 'foo () baz'); +testEdit('di)_middle_spc', 'foo (bAr) baz', /A/, 'di)', 'foo () baz'); +testEdit('da(_middle_spc', 'foo (bAr) baz', /A/, 'da(', 'foo baz'); +testEdit('da)_middle_spc', 'foo (bAr) baz', /A/, 'da)', 'foo baz'); + +testEdit('di(_close_spc', 'foo (bAr) baz', /\)/, 'di(', 'foo () baz'); +testEdit('di)_close_spc', 'foo (bAr) baz', /\)/, 'di)', 'foo () baz'); +testEdit('da(_close_spc', 'foo (bAr) baz', /\)/, 'da(', 'foo baz'); +testEdit('da)_close_spc', 'foo (bAr) baz', /\)/, 'da)', 'foo baz'); + +testEdit('di`', 'foo `bAr` baz', /`/, 'di`', 'foo `` baz'); +testEdit('di>', 'foo <bAr> baz', /</, 'di>', 'foo <> baz'); +testEdit('da<', 'foo <bAr> baz', /</, 'da<', 'foo baz'); + +// delete around and inner b. +testEdit('dab_on_(_should_delete_around_()block', 'o( in(abc) )', /\(a/, 'dab', 'o( in )'); + +// delete around and inner B. +testEdit('daB_on_{_should_delete_around_{}block', 'o{ in{abc} }', /{a/, 'daB', 'o{ in }'); +testEdit('diB_on_{_should_delete_inner_{}block', 'o{ in{abc} }', /{a/, 'diB', 'o{ in{} }'); + +testEdit('da{_on_{_should_delete_inner_block', 'o{ in{abc} }', /{a/, 'da{', 'o{ in }'); +testEdit('di[_on_(_should_not_delete', 'foo (bAr) baz', /\(/, 'di[', 'foo (bAr) baz'); +testEdit('di[_on_)_should_not_delete', 'foo (bAr) baz', /\)/, 'di[', 'foo (bAr) baz'); +testEdit('da[_on_(_should_not_delete', 'foo (bAr) baz', /\(/, 'da[', 'foo (bAr) baz'); +testEdit('da[_on_)_should_not_delete', 'foo (bAr) baz', /\)/, 'da[', 'foo (bAr) baz'); +testMotion('di(_outside_should_stay', ['d', 'i', '('], new Pos(0, 0), new Pos(0, 0)); + +// Open and close on different lines, equally indented +testEdit('di{_middle_spc', 'a{\n\tbar\n}b', /r/, 'di{', 'a{}b'); +testEdit('di}_middle_spc', 'a{\n\tbar\n}b', /r/, 'di}', 'a{}b'); +testEdit('da{_middle_spc', 'a{\n\tbar\n}b', /r/, 'da{', 'ab'); +testEdit('da}_middle_spc', 'a{\n\tbar\n}b', /r/, 'da}', 'ab'); +testEdit('daB_middle_spc', 'a{\n\tbar\n}b', /r/, 'daB', 'ab'); + +// open and close on diff lines, open indented less than close +testEdit('di{_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'di{', 'a{}b'); +testEdit('di}_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'di}', 'a{}b'); +testEdit('da{_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'da{', 'ab'); +testEdit('da}_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'da}', 'ab'); + +// open and close on diff lines, open indented more than close +testEdit('di[_middle_spc', 'a\t[\n\tbar\n]b', /r/, 'di[', 'a\t[]b'); +testEdit('di]_middle_spc', 'a\t[\n\tbar\n]b', /r/, 'di]', 'a\t[]b'); +testEdit('da[_middle_spc', 'a\t[\n\tbar\n]b', /r/, 'da[', 'a\tb'); +testEdit('da]_middle_spc', 'a\t[\n\tbar\n]b', /r/, 'da]', 'a\tb'); + +// open and close on diff lines, open indented more than close +testEdit('di<_middle_spc', 'a\t<\n\tbar\n>b', /r/, 'di<', 'a\t<>b'); +testEdit('di>_middle_spc', 'a\t<\n\tbar\n>b', /r/, 'di>', 'a\t<>b'); +testEdit('da<_middle_spc', 'a\t<\n\tbar\n>b', /r/, 'da<', 'a\tb'); +testEdit('da>_middle_spc', 'a\t<\n\tbar\n>b', /r/, 'da>', 'a\tb'); + +function testSelection(name, before, pos, keys, sel) { + return testVim(name, function(cm, vim, helpers) { + var ch = before.search(pos) + var line = before.substring(0, ch).split('\n').length - 1; + if (line) { + ch = before.substring(0, ch).split('\n').pop().length; + } + cm.setCursor(line, ch); + helpers.doKeys.apply(this, keys.split('')); + eq(sel, cm.getSelection()); + }, {value: before}); +} +testSelection('viw_middle_spc', 'foo \tbAr\t baz', /A/, 'viw', 'bAr'); +testSelection('vaw_middle_spc', 'foo \tbAr\t baz', /A/, 'vaw', 'bAr\t '); +testSelection('viw_middle_punct', 'foo \tbAr,\t baz', /A/, 'viw', 'bAr'); +testSelection('vaW_middle_punct', 'foo \tbAr,\t baz', /A/, 'vaW', 'bAr,\t '); +testSelection('viw_start_spc', 'foo \tbAr\t baz', /b/, 'viw', 'bAr'); +testSelection('viw_end_spc', 'foo \tbAr\t baz', /r/, 'viw', 'bAr'); +testSelection('viw_eol', 'foo \tbAr', /r/, 'viw', 'bAr'); +testSelection('vi{_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'vi{', '\n\tbar\n\t'); +testSelection('va{_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'va{', '{\n\tbar\n\t}'); + +testVim('mouse_select', function(cm, vim, helpers) { + cm.setSelection(Pos(0, 2), Pos(0, 4), {origin: '*mouse'}); + is(cm.state.vim.visualMode); + is(!cm.state.vim.visualLine); + is(!cm.state.vim.visualBlock); + helpers.doKeys('<Esc>'); + is(!cm.somethingSelected()); + helpers.doKeys('g', 'v'); + eq('cd', cm.getSelection()); +}, {value: 'abcdef'}); + +// Operator-motion tests +testVim('D', function(cm, vim, helpers) { + cm.setCursor(0, 3); + helpers.doKeys('D'); + eq(' wo\nword2\n word3', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('rd1', register.toString()); + is(!register.linewise); + helpers.assertCursorAt(0, 2); +}, { value: ' word1\nword2\n word3' }); +testVim('C', function(cm, vim, helpers) { + var curStart = makeCursor(0, 3); + cm.setCursor(curStart); + helpers.doKeys('C'); + eq(' wo\nword2\n word3', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('rd1', register.toString()); + is(!register.linewise); + eqCursorPos(curStart, cm.getCursor()); + eq('vim-insert', cm.getOption('keyMap')); +}, { value: ' word1\nword2\n word3' }); +testVim('Y', function(cm, vim, helpers) { + var curStart = makeCursor(0, 3); + cm.setCursor(curStart); + helpers.doKeys('Y'); + eq(' word1\nword2\n word3', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq(' word1\n', register.toString()); + is(register.linewise); + helpers.assertCursorAt(0, 3); +}, { value: ' word1\nword2\n word3' }); +testVim('Yy_blockwise', function(cm, vim, helpers) { + helpers.doKeys('<C-v>', 'j', '2', 'l', 'Y'); + helpers.doKeys('G', 'p', 'g', 'g'); + helpers.doKeys('<C-v>', 'j', '2', 'l', 'y'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('$', 'p'); + eq('123456123\n123456123\n123456\n123456', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('123\n123', register.toString()); + is(register.blockwise); + helpers.assertCursorAt(0, 6); + helpers.doKeys('$', 'j', 'p'); + helpers.doKeys('$', 'j', 'P'); + eq("123456123\n123456123123\n123456 121233\n123456 123", cm.getValue()); +}, { value: '123456\n123456\n' }); +testVim('~', function(cm, vim, helpers) { + helpers.doKeys('3', '~'); + eq('ABCdefg', cm.getValue()); + helpers.assertCursorAt(0, 3); +}, { value: 'abcdefg' }); + +// Action tests +testVim('ctrl-a', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('<C-a>'); + eq('-9', cm.getValue()); + helpers.assertCursorAt(0, 1); + helpers.doKeys('2','<C-a>'); + eq('-7', cm.getValue()); +}, {value: '-10'}); +testVim('ctrl-x', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('<C-x>'); + eq('-1', cm.getValue()); + helpers.assertCursorAt(0, 1); + helpers.doKeys('2','<C-x>'); + eq('-3', cm.getValue()); +}, {value: '0'}); +testVim('<C-x>/<C-a> search forward', function(cm, vim, helpers) { + forEach(['<C-x>', '<C-a>'], function(key) { + cm.setCursor(0, 0); + helpers.doKeys(key); + helpers.assertCursorAt(0, 5); + helpers.doKeys('l'); + helpers.doKeys(key); + helpers.assertCursorAt(0, 10); + cm.setCursor(0, 11); + helpers.doKeys(key); + helpers.assertCursorAt(0, 11); + }); +}, {value: '__jmp1 jmp2 jmp'}); +testVim('insert_ctrl_w', function(cm, vim, helpers) { + var curStart = makeCursor(0, 10); + cm.setCursor(curStart); + helpers.doKeys('a'); + helpers.doKeys('<C-w>'); + eq('word1/', cm.getValue()); + var register = helpers.getRegisterController().getRegister(); + eq('word2', register.toString()); + is(!register.linewise); + var curEnd = makeCursor(0, 6); + eqCursorPos(curEnd, cm.getCursor()); + eq('vim-insert', cm.getOption('keyMap')); +}, { value: 'word1/word2' }); +testVim('normal_ctrl_w', function(cm, vim, helpers) { + var curStart = makeCursor(0, 3); + cm.setCursor(curStart); + helpers.doKeys('<C-w>'); + eq('word', cm.getValue()); + var curEnd = makeCursor(0, 3); + helpers.assertCursorAt(0,3); + eqCursorPos(curEnd, cm.getCursor()); + eq('vim', cm.getOption('keyMap')); +}, {value: 'word'}); +testVim('a', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('a'); + helpers.assertCursorAt(0, 2); + eq('vim-insert', cm.getOption('keyMap')); +}); +testVim('a_eol', function(cm, vim, helpers) { + cm.setCursor(0, lines[0].length - 1); + helpers.doKeys('a'); + helpers.assertCursorAt(0, lines[0].length); + eq('vim-insert', cm.getOption('keyMap')); +}); +testVim('A_endOfSelectedArea', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('v', 'j', 'l'); + helpers.doKeys('A'); + helpers.assertCursorAt(1, 2); + eq('vim-insert', cm.getOption('keyMap')); +}, {value: 'foo\nbar'}); +testVim('i', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('i'); + helpers.assertCursorAt(0, 1); + eq('vim-insert', cm.getOption('keyMap')); +}); +testVim('i_repeat', function(cm, vim, helpers) { + helpers.doKeys('3', 'i'); + helpers.doKeys('test') + helpers.doKeys('<Esc>'); + eq('testtesttest', cm.getValue()); + helpers.assertCursorAt(0, 11); +}, { value: '' }); +testVim('i_repeat_delete', function(cm, vim, helpers) { + cm.setCursor(0, 4); + helpers.doKeys('2', 'i'); + helpers.doKeys('z') + helpers.doInsertModeKeys('Backspace', 'Backspace'); + helpers.doKeys('<Esc>'); + eq('abe', cm.getValue()); + helpers.assertCursorAt(0, 1); +}, { value: 'abcde' }); +testVim('insert', function(cm, vim, helpers) { + helpers.doKeys('i'); + eq('vim-insert', cm.getOption('keyMap')); + eq(false, cm.state.overwrite); + helpers.doKeys('<Ins>'); + eq('vim-replace', cm.getOption('keyMap')); + eq(true, cm.state.overwrite); + helpers.doKeys('<Ins>'); + eq('vim-insert', cm.getOption('keyMap')); + eq(false, cm.state.overwrite); +}); +testVim('i_backspace', function(cm, vim, helpers) { + cm.setCursor(0, 10); + helpers.doKeys('i'); + helpers.doInsertModeKeys('Backspace'); + helpers.assertCursorAt(0, 9); + eq('012345678', cm.getValue()); +}, { value: '0123456789'}); +testVim('i_overwrite_backspace', function(cm, vim, helpers) { + cm.setCursor(0, 10); + helpers.doKeys('i'); + helpers.doKeys('<Ins>'); + helpers.doInsertModeKeys('Backspace'); + helpers.assertCursorAt(Pos(0, 9, "after")); + eq('0123456789', cm.getValue()); +}, { value: '0123456789'}); +testVim('i_forward_delete', function(cm, vim, helpers) { + cm.setCursor(0, 3); + helpers.doKeys('i'); + helpers.doInsertModeKeys('Delete'); + helpers.assertCursorAt(0, 3); + eq('A124\nBCD', cm.getValue()); + helpers.doInsertModeKeys('Delete'); + helpers.assertCursorAt(0, 3); + eq('A12\nBCD', cm.getValue()); + helpers.doInsertModeKeys('Delete'); + helpers.assertCursorAt(0, 3); + eq('A12BCD', cm.getValue()); +}, { value: 'A1234\nBCD'}); +testVim('forward_delete', function(cm, vim, helpers) { + cm.setCursor(0, 3); + helpers.doInsertModeKeys('Delete'); + helpers.assertCursorAt(0, 3); + eq('A124\nBCD', cm.getValue()); + helpers.doInsertModeKeys('Delete'); + helpers.assertCursorAt(0, 2); + eq('A12\nBCD', cm.getValue()); + helpers.doInsertModeKeys('Delete'); + helpers.assertCursorAt(0, 1); + eq('A1\nBCD', cm.getValue()); +}, { value: 'A1234\nBCD'}); +testVim('A', function(cm, vim, helpers) { + helpers.doKeys('A'); + helpers.assertCursorAt(0, lines[0].length); + eq('vim-insert', cm.getOption('keyMap')); +}); +testVim('A_visual_block', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('<C-v>', '2', 'j', 'l', 'l', 'A'); + helpers.doKeys('hello'); + eq('testhello\nmehello\npleahellose', cm.getValue()); + helpers.doKeys('<Esc>'); + cm.setCursor(0, 0); + helpers.doKeys('.'); + // TODO this doesn't work yet + // eq('teshellothello\nme hello hello\nplehelloahellose', cm.getValue()); +}, {value: 'test\nme\nplease'}); +testVim('I', function(cm, vim, helpers) { + cm.setCursor(0, 4); + helpers.doKeys('I'); + helpers.assertCursorAt(0, lines[0].textStart); + eq('vim-insert', cm.getOption('keyMap')); +}); +testVim('I_repeat', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('3', 'I'); + helpers.doKeys('test') + helpers.doKeys('<Esc>'); + eq('testtesttestblah', cm.getValue()); + helpers.assertCursorAt(0, 11); +}, { value: 'blah' }); +testVim('I_visual_block', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('<C-v>', '2', 'j', 'l', 'l', 'I'); + helpers.doKeys('hello'); + eq('hellotest\nhellome\nhelloplease', cm.getValue()); +}, {value: 'test\nme\nplease'}); +testVim('o', function(cm, vim, helpers) { + cm.setCursor(0, 4); + helpers.doKeys('o'); + eq('word1\n\nword2', cm.getValue()); + helpers.assertCursorAt(1, 0); + eq('vim-insert', cm.getOption('keyMap')); +}, { value: 'word1\nword2' }); +testVim('o_repeat', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('3', 'o'); + helpers.doKeys('test') + helpers.doKeys('<Esc>'); + eq('\ntest\ntest\ntest', cm.getValue()); + helpers.assertCursorAt(3, 3); +}, { value: '' }); +testVim('O', function(cm, vim, helpers) { + cm.setCursor(0, 4); + helpers.doKeys('O'); + eq('\nword1\nword2', cm.getValue()); + helpers.assertCursorAt(0, 0); + eq('vim-insert', cm.getOption('keyMap')); +}, { value: 'word1\nword2' }); +testVim('J', function(cm, vim, helpers) { + cm.setCursor(0, 4); + helpers.doKeys('J'); + var expectedValue = 'word1 word2\nword3\n word4'; + eq(expectedValue, cm.getValue()); + helpers.assertCursorAt(0, expectedValue.indexOf('word2') - 1); +}, { value: 'word1 \n word2\nword3\n word4' }); +testVim('J_repeat', function(cm, vim, helpers) { + cm.setCursor(0, 4); + helpers.doKeys('3', 'J'); + var expectedValue = 'word1 word2 word3\n word4'; + eq(expectedValue, cm.getValue()); + helpers.assertCursorAt(0, expectedValue.indexOf('word3') - 1); +}, { value: 'word1 \n word2\nword3\n word4' }); +testVim('p', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.getRegisterController().pushText('"', 'yank', 'abc\ndef', false); + helpers.doKeys('p'); + eq('__abc\ndef_', cm.getValue()); + helpers.assertCursorAt(1, 2); +}, { value: '___' }); +testVim('p_register', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.getRegisterController().getRegister('a').setText('abc\ndef', false); + helpers.doKeys('"', 'a', 'p'); + eq('__abc\ndef_', cm.getValue()); + helpers.assertCursorAt(1, 2); +}, { value: '___' }); +testVim('p_wrong_register', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.getRegisterController().getRegister('a').setText('abc\ndef', false); + helpers.doKeys('p'); + eq('___', cm.getValue()); + helpers.assertCursorAt(0, 1); +}, { value: '___' }); +testVim('p_line', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.getRegisterController().pushText('"', 'yank', ' a\nd\n', true); + helpers.doKeys('2', 'p'); + eq('___\n a\nd\n a\nd', cm.getValue()); + helpers.assertCursorAt(1, 2); +}, { value: '___' }); +testVim('p_lastline', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.getRegisterController().pushText('"', 'yank', ' a\nd', true); + helpers.doKeys('2', 'p'); + eq('___\n a\nd\n a\nd', cm.getValue()); + helpers.assertCursorAt(1, 2); +}, { value: '___' }); +testVim(']p_first_indent_is_smaller', function(cm, vim, helpers) { + helpers.getRegisterController().pushText('"', 'yank', ' abc\n def\n', true); + helpers.doKeys(']', 'p'); + eq(' ___\n abc\n def', cm.getValue()); +}, { value: ' ___' }); +testVim(']p_first_indent_is_larger', function(cm, vim, helpers) { + helpers.getRegisterController().pushText('"', 'yank', ' abc\n def\n', true); + helpers.doKeys(']', 'p'); + eq(' ___\n abc\ndef', cm.getValue()); +}, { value: ' ___' }); +testVim(']p_with_tab_indents', function(cm, vim, helpers) { + helpers.getRegisterController().pushText('"', 'yank', '\t\tabc\n\t\t\tdef\n', true); + helpers.doKeys(']', 'p'); + eq('\t___\n\tabc\n\t\tdef', cm.getValue()); +}, { value: '\t___', indentWithTabs: true}); +testVim(']p_with_spaces_translated_to_tabs', function(cm, vim, helpers) { + helpers.getRegisterController().pushText('"', 'yank', ' abc\n def\n', true); + helpers.doKeys(']', 'p'); + eq('\t___\n\tabc\n\t\tdef', cm.getValue()); +}, { value: '\t___', indentWithTabs: true, tabSize: 2 }); +testVim('[p', function(cm, vim, helpers) { + helpers.getRegisterController().pushText('"', 'yank', ' abc\n def\n', true); + helpers.doKeys('[', 'p'); + eq(' abc\n def\n ___', cm.getValue()); +}, { value: ' ___' }); +testVim('P', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.getRegisterController().pushText('"', 'yank', 'abc\ndef', false); + helpers.doKeys('P'); + eq('_abc\ndef__', cm.getValue()); + helpers.assertCursorAt(1, 3); +}, { value: '___' }); +testVim('P_line', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.getRegisterController().pushText('"', 'yank', ' a\nd\n', true); + helpers.doKeys('2', 'P'); + eq(' a\nd\n a\nd\n___', cm.getValue()); + helpers.assertCursorAt(0, 2); +}, { value: '___' }); +testVim('r', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('3', 'r', 'u'); + eq('wuuuet\nanother', cm.getValue(),'3r failed'); + helpers.assertCursorAt(0, 3); + cm.setCursor(0, 4); + helpers.doKeys('v', 'j', 'h', 'r', '<Space>'); + eq('wuuu \n her', cm.getValue(),'Replacing selection by space-characters failed'); + cm.setValue("ox"); + helpers.doKeys('r', '<C-c>'); + eq('ox', cm.getValue()); + helpers.doKeys('r', '<Del>'); + eq('ox', cm.getValue()); + helpers.doKeys('r', '<CR>'); + eq('\nx', cm.getValue()); +}, { value: 'wordet\nanother' }); +testVim('r_visual_block', function(cm, vim, helpers) { + cm.setCursor(2, 3); + helpers.doKeys('<C-v>', 'k', 'k', 'h', 'h', 'r', 'l'); + eq('1lll\n5lll\nalllefg', cm.getValue()); + helpers.doKeys('<C-v>', 'l', 'j', 'r', '<Space>'); + eq('1 l\n5 l\nalllefg', cm.getValue()); + cm.setCursor(2, 0); + helpers.doKeys('o'); + helpers.doKeys('\t\t') + helpers.doKeys('<Esc>'); + helpers.doKeys('<C-v>', 'h', 'h', 'r', 'r'); + eq('1 l\n5 l\nalllefg\nrrrrrrrr', cm.getValue()); +}, {value: '1234\n5678\nabcdefg'}); +testVim('R', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('R'); + helpers.assertCursorAt(0, 1); + eq('vim-replace', cm.getOption('keyMap')); + is(cm.state.overwrite, 'Setting overwrite state failed'); +}); +testVim('mark', function(cm, vim, helpers) { + cm.setCursor(2, 2); + helpers.doKeys('m', 't'); + cm.setCursor(0, 0); + helpers.doKeys('`', 't'); + helpers.assertCursorAt(2, 2); + cm.setCursor(2, 0); + cm.replaceRange(' h', cm.getCursor()); + cm.setCursor(0, 0); + helpers.doKeys('\'', 't'); + helpers.assertCursorAt(2, 3); +}); +testVim('mark\'', function(cm, vim, helpers) { + cm.setCursor(2, 2); + cm.setCursor(0, 0); + helpers.doKeys('`', '\''); + helpers.assertCursorAt(2, 2); + cm.setCursor(2, 0); + cm.replaceRange(' h', cm.getCursor()); + cm.setCursor(0, 0); + helpers.doKeys('\'', '\''); + helpers.assertCursorAt(2, 3); +}); +testVim('mark.', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('O', 'testing', '<Esc>'); + cm.setCursor(3, 3); + helpers.doKeys('\'', '.'); + helpers.assertCursorAt(0, 0); + cm.setCursor(4, 4); + helpers.doKeys('`', '.'); + helpers.assertCursorAt(0, 6); +}); +testVim('jumpToMark_next', function(cm, vim, helpers) { + cm.setCursor(2, 2); + helpers.doKeys('m', 't'); + cm.setCursor(0, 0); + helpers.doKeys(']', '`'); + helpers.assertCursorAt(2, 2); + cm.setCursor(0, 0); + helpers.doKeys(']', '\''); + helpers.assertCursorAt(2, 0); +}); +testVim('jumpToMark_next_repeat', function(cm, vim, helpers) { + cm.setCursor(2, 2); + helpers.doKeys('m', 'a'); + cm.setCursor(3, 2); + helpers.doKeys('m', 'b'); + cm.setCursor(4, 2); + helpers.doKeys('m', 'c'); + cm.setCursor(0, 0); + helpers.doKeys('2', ']', '`'); + helpers.assertCursorAt(3, 2); + cm.setCursor(0, 0); + helpers.doKeys('2', ']', '\''); + helpers.assertCursorAt(3, 1); +}); +testVim('jumpToMark_next_sameline', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doKeys('m', 'a'); + cm.setCursor(2, 4); + helpers.doKeys('m', 'b'); + cm.setCursor(2, 2); + helpers.doKeys(']', '`'); + helpers.assertCursorAt(2, 4); +}); +testVim('jumpToMark_next_onlyprev', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doKeys('m', 'a'); + cm.setCursor(4, 0); + helpers.doKeys(']', '`'); + helpers.assertCursorAt(4, 0); +}); +testVim('jumpToMark_next_nomark', function(cm, vim, helpers) { + cm.setCursor(2, 2); + helpers.doKeys(']', '`'); + helpers.assertCursorAt(2, 2); + helpers.doKeys(']', '\''); + helpers.assertCursorAt(2, 0); +}); +testVim('jumpToMark_next_linewise_over', function(cm, vim, helpers) { + cm.setCursor(2, 2); + helpers.doKeys('m', 'a'); + cm.setCursor(3, 4); + helpers.doKeys('m', 'b'); + cm.setCursor(2, 1); + helpers.doKeys(']', '\''); + helpers.assertCursorAt(3, 1); +}); +testVim('jumpToMark_next_action', function(cm, vim, helpers) { + cm.setCursor(2, 2); + helpers.doKeys('m', 't'); + cm.setCursor(0, 0); + helpers.doKeys('d', ']', '`'); + helpers.assertCursorAt(0, 0); + var actual = cm.getLine(0); + var expected = 'pop pop 0 1 2 3 4'; + eq(actual, expected, "Deleting while jumping to the next mark failed."); +}); +testVim('jumpToMark_next_line_action', function(cm, vim, helpers) { + cm.setCursor(2, 2); + helpers.doKeys('m', 't'); + cm.setCursor(0, 0); + helpers.doKeys('d', ']', '\''); + helpers.assertCursorAt(0, 1); + var actual = cm.getLine(0); + var expected = ' (a) [b] {c} ' + eq(actual, expected, "Deleting while jumping to the next mark line failed."); +}); +testVim('jumpToMark_prev', function(cm, vim, helpers) { + cm.setCursor(2, 2); + helpers.doKeys('m', 't'); + cm.setCursor(4, 0); + helpers.doKeys('[', '`'); + helpers.assertCursorAt(2, 2); + cm.setCursor(4, 0); + helpers.doKeys('[', '\''); + helpers.assertCursorAt(2, 0); +}); +testVim('jumpToMark_prev_repeat', function(cm, vim, helpers) { + cm.setCursor(2, 2); + helpers.doKeys('m', 'a'); + cm.setCursor(3, 2); + helpers.doKeys('m', 'b'); + cm.setCursor(4, 2); + helpers.doKeys('m', 'c'); + cm.setCursor(5, 0); + helpers.doKeys('2', '[', '`'); + helpers.assertCursorAt(3, 2); + cm.setCursor(5, 0); + helpers.doKeys('2', '[', '\''); + helpers.assertCursorAt(3, 1); +}); +testVim('jumpToMark_prev_sameline', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doKeys('m', 'a'); + cm.setCursor(2, 4); + helpers.doKeys('m', 'b'); + cm.setCursor(2, 2); + helpers.doKeys('[', '`'); + helpers.assertCursorAt(2, 0); +}); +testVim('jumpToMark_prev_onlynext', function(cm, vim, helpers) { + cm.setCursor(4, 4); + helpers.doKeys('m', 'a'); + cm.setCursor(2, 0); + helpers.doKeys('[', '`'); + helpers.assertCursorAt(2, 0); +}); +testVim('jumpToMark_prev_nomark', function(cm, vim, helpers) { + cm.setCursor(2, 2); + helpers.doKeys('[', '`'); + helpers.assertCursorAt(2, 2); + helpers.doKeys('[', '\''); + helpers.assertCursorAt(2, 0); +}); +testVim('jumpToMark_prev_linewise_over', function(cm, vim, helpers) { + cm.setCursor(2, 2); + helpers.doKeys('m', 'a'); + cm.setCursor(3, 4); + helpers.doKeys('m', 'b'); + cm.setCursor(3, 6); + helpers.doKeys('[', '\''); + helpers.assertCursorAt(2, 0); +}); +testVim('delmark_single', function(cm, vim, helpers) { + cm.setCursor(1, 2); + helpers.doKeys('m', 't'); + helpers.doEx('delmarks t'); + cm.setCursor(0, 0); + helpers.doKeys('`', 't'); + helpers.assertCursorAt(0, 0); +}); +testVim('delmark_range', function(cm, vim, helpers) { + cm.setCursor(1, 2); + helpers.doKeys('m', 'a'); + cm.setCursor(2, 2); + helpers.doKeys('m', 'b'); + cm.setCursor(3, 2); + helpers.doKeys('m', 'c'); + cm.setCursor(4, 2); + helpers.doKeys('m', 'd'); + cm.setCursor(5, 2); + helpers.doKeys('m', 'e'); + helpers.doEx('delmarks b-d'); + cm.setCursor(0, 0); + helpers.doKeys('`', 'a'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'b'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'c'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'd'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'e'); + helpers.assertCursorAt(5, 2); +}); +testVim('delmark_multi', function(cm, vim, helpers) { + cm.setCursor(1, 2); + helpers.doKeys('m', 'a'); + cm.setCursor(2, 2); + helpers.doKeys('m', 'b'); + cm.setCursor(3, 2); + helpers.doKeys('m', 'c'); + cm.setCursor(4, 2); + helpers.doKeys('m', 'd'); + cm.setCursor(5, 2); + helpers.doKeys('m', 'e'); + helpers.doEx('delmarks bcd'); + cm.setCursor(0, 0); + helpers.doKeys('`', 'a'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'b'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'c'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'd'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'e'); + helpers.assertCursorAt(5, 2); +}); +testVim('delmark_multi_space', function(cm, vim, helpers) { + cm.setCursor(1, 2); + helpers.doKeys('m', 'a'); + cm.setCursor(2, 2); + helpers.doKeys('m', 'b'); + cm.setCursor(3, 2); + helpers.doKeys('m', 'c'); + cm.setCursor(4, 2); + helpers.doKeys('m', 'd'); + cm.setCursor(5, 2); + helpers.doKeys('m', 'e'); + helpers.doEx('delmarks b c d'); + cm.setCursor(0, 0); + helpers.doKeys('`', 'a'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'b'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'c'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'd'); + helpers.assertCursorAt(1, 2); + helpers.doKeys('`', 'e'); + helpers.assertCursorAt(5, 2); +}); +testVim('delmark_all', function(cm, vim, helpers) { + cm.setCursor(1, 2); + helpers.doKeys('m', 'a'); + cm.setCursor(2, 2); + helpers.doKeys('m', 'b'); + cm.setCursor(3, 2); + helpers.doKeys('m', 'c'); + cm.setCursor(4, 2); + helpers.doKeys('m', 'd'); + cm.setCursor(5, 2); + helpers.doKeys('m', 'e'); + helpers.doEx('delmarks a b-de'); + cm.setCursor(0, 0); + helpers.doKeys('`', 'a'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('`', 'b'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('`', 'c'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('`', 'd'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('`', 'e'); + helpers.assertCursorAt(0, 0); +}); +testVim('visual', function(cm, vim, helpers) { + helpers.doKeys('l', 'v', 'l', 'l'); + helpers.assertCursorAt(0, 4); + eqCursorPos(makeCursor(0, 1), cm.getCursor('anchor')); + helpers.doKeys('d'); + eq('15', cm.getValue()); +}, { value: '12345' }); +testVim('visual_yank', function(cm, vim, helpers) { + helpers.doKeys('v', '3', 'l', 'y'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('p'); + eq('aa te test for yank', cm.getValue()); +}, { value: 'a test for yank' }) +testVim('visual_w', function(cm, vim, helpers) { + helpers.doKeys('v', 'w'); + eq(cm.getSelection(), 'motion t'); +}, { value: 'motion test'}); +testVim('visual_initial_selection', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('v'); + cm.getSelection('n'); +}, { value: 'init'}); +testVim('visual_crossover_left', function(cm, vim, helpers) { + cm.setCursor(0, 2); + helpers.doKeys('v', 'l', 'h', 'h'); + cm.getSelection('ro'); +}, { value: 'cross'}); +testVim('visual_crossover_left', function(cm, vim, helpers) { + cm.setCursor(0, 2); + helpers.doKeys('v', 'h', 'l', 'l'); + cm.getSelection('os'); +}, { value: 'cross'}); +testVim('visual_crossover_up', function(cm, vim, helpers) { + cm.setCursor(3, 2); + helpers.doKeys('v', 'j', 'k', 'k'); + eqCursorPos(Pos(2, 2), cm.getCursor('head')); + eqCursorPos(Pos(3, 3), cm.getCursor('anchor')); + helpers.doKeys('k'); + eqCursorPos(Pos(1, 2), cm.getCursor('head')); + eqCursorPos(Pos(3, 3), cm.getCursor('anchor')); +}, { value: 'cross\ncross\ncross\ncross\ncross\n'}); +testVim('visual_crossover_down', function(cm, vim, helpers) { + cm.setCursor(1, 2); + helpers.doKeys('v', 'k', 'j', 'j'); + eqCursorPos(Pos(2, 3), cm.getCursor('head')); + eqCursorPos(Pos(1, 2), cm.getCursor('anchor')); + helpers.doKeys('j'); + eqCursorPos(Pos(3, 3), cm.getCursor('head')); + eqCursorPos(Pos(1, 2), cm.getCursor('anchor')); +}, { value: 'cross\ncross\ncross\ncross\ncross\n'}); +testVim('visual_exit', function(cm, vim, helpers) { + helpers.doKeys('<C-v>', 'l', 'j', 'j', '<Esc>'); + eqCursorPos(cm.getCursor('anchor'), cm.getCursor('head')); + eq(vim.visualMode, false); +}, { value: 'hello\nworld\nfoo' }); +testVim('visual_line', function(cm, vim, helpers) { + helpers.doKeys('l', 'V', 'l', 'j', 'j', 'd'); + eq(' 4\n 5', cm.getValue()); +}, { value: ' 1\n 2\n 3\n 4\n 5' }); +testVim('visual_block_move_to_eol', function(cm, vim, helpers) { + // moveToEol should move all block cursors to end of line + cm.setCursor(0, 0); + helpers.doKeys('<C-v>', 'G', '$'); + var selections = cm.getSelections().join(); + eq('123,45,6', selections); + // Checks that with cursor at Infinity, finding words backwards still works. + helpers.doKeys('2', 'k', 'b'); + selections = cm.getSelections().join(); + eq('1', selections); +}, {value: '123\n45\n6'}); +testVim('visual_block_different_line_lengths', function(cm, vim, helpers) { + // test the block selection with lines of different length + // i.e. extending the selection + // till the end of the longest line. + helpers.doKeys('<C-v>', 'l', 'j', 'j', '6', 'l', 'd'); + helpers.doKeys('d', 'd', 'd', 'd'); + eq('', cm.getValue()); +}, {value: '1234\n5678\nabcdefg'}); +testVim('visual_block_truncate_on_short_line', function(cm, vim, helpers) { + // check for left side selection in case + // of moving up to a shorter line. + cm.replaceRange('', cm.getCursor()); + cm.setCursor(3, 4); + helpers.doKeys('<C-v>', 'l', 'k', 'k', 'd'); + eq('hello world\n{\ntis\nsa!', cm.getValue()); +}, {value: 'hello world\n{\nthis is\nsparta!'}); +testVim('visual_block_corners', function(cm, vim, helpers) { + cm.setCursor(1, 2); + helpers.doKeys('<C-v>', '2', 'l', 'k'); + // circle around the anchor + // and check the selections + var selections = cm.getSelections(); + eq('345891', selections.join('')); + helpers.doKeys('4', 'h'); + selections = cm.getSelections(); + eq('123678', selections.join('')); + helpers.doKeys('j', 'j'); + selections = cm.getSelections(); + eq('678abc', selections.join('')); + helpers.doKeys('4', 'l'); + selections = cm.getSelections(); + eq('891cde', selections.join('')); +}, {value: '12345\n67891\nabcde'}); +testVim('visual_block_mode_switch', function(cm, vim, helpers) { + // switch between visual modes + cm.setCursor(1, 1); + // blockwise to characterwise visual + helpers.doKeys('<C-v>', 'j', 'l', 'v'); + var selections = cm.getSelections(); + eq('7891\nabc', selections.join('')); + // characterwise to blockwise + helpers.doKeys('<C-v>'); + selections = cm.getSelections(); + eq('78bc', selections.join('')); + // blockwise to linewise visual + helpers.doKeys('V'); + selections = cm.getSelections(); + eq('67891\nabcde', selections.join('')); +}, {value: '12345\n67891\nabcde'}); +testVim('visual_block_crossing_short_line', function(cm, vim, helpers) { + // visual block with long and short lines + cm.setCursor(0, 3); + helpers.doKeys('<C-v>', 'j', 'j', 'j'); + var selections = cm.getSelections().join(); + eq('4,,d,b', selections); + helpers.doKeys('3', 'k'); + selections = cm.getSelections().join(); + eq('4', selections); + helpers.doKeys('5', 'j', 'k'); + selections = cm.getSelections().join(""); + eq(10, selections.length); +}, {value: '123456\n78\nabcdefg\nfoobar\n}\n'}); +testVim('visual_block_curPos_on_exit', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('<C-v>', '3' , 'l', '<Esc>'); + eqCursorPos(makeCursor(0, 3), cm.getCursor()); + helpers.doKeys('h', '<C-v>', '2' , 'j' ,'3' , 'l'); + eq(cm.getSelections().join(), "3456,,cdef"); + helpers.doKeys('4' , 'h'); + eq(cm.getSelections().join(), "23,8,bc"); + helpers.doKeys('2' , 'l'); + eq(cm.getSelections().join(), "34,,cd"); +}, {value: '123456\n78\nabcdefg\nfoobar'}); + +testVim('visual_marks', function(cm, vim, helpers) { + helpers.doKeys('l', 'v', 'l', 'l', 'j', 'j', 'v'); + // Test visual mode marks + cm.setCursor(2, 1); + helpers.doKeys('\'', '<'); + helpers.assertCursorAt(0, 1); + helpers.doKeys('\'', '>'); + helpers.assertCursorAt(2, 0); +}); +testVim('visual_join', function(cm, vim, helpers) { + helpers.doKeys('l', 'V', 'l', 'j', 'j', 'J'); + eq(' 1 2 3\n 4\n 5', cm.getValue()); + is(!vim.visualMode); +}, { value: ' 1\n 2\n 3\n 4\n 5' }); +testVim('visual_join_2', function(cm, vim, helpers) { + helpers.doKeys('G', 'V', 'g', 'g', 'J'); + eq('1 2 3 4 5 6 ', cm.getValue()); + is(!vim.visualMode); +}, { value: '1\n2\n3\n4\n5\n6\n'}); +testVim('visual_blank', function(cm, vim, helpers) { + helpers.doKeys('v', 'k'); + eq(vim.visualMode, true); +}, { value: '\n' }); +testVim('reselect_visual', function(cm, vim, helpers) { + helpers.doKeys('l', 'v', 'l', 'l', 'l', 'y', 'g', 'v'); + helpers.assertCursorAt(0, 5); + eqCursorPos(makeCursor(0, 1), cm.getCursor('anchor')); + helpers.doKeys('v'); + cm.setCursor(1, 0); + helpers.doKeys('v', 'l', 'l', 'p'); + eq('123456\n2345\nbar', cm.getValue()); + cm.setCursor(0, 0); + helpers.doKeys('g', 'v'); + // here the fake cursor is at (1, 3) + helpers.assertCursorAt(1, 4); + eqCursorPos(makeCursor(1, 0), cm.getCursor('anchor')); + helpers.doKeys('v'); + cm.setCursor(2, 0); + helpers.doKeys('v', 'l', 'l', 'g', 'v'); + helpers.assertCursorAt(1, 4); + eqCursorPos(makeCursor(1, 0), cm.getCursor('anchor')); + helpers.doKeys('g', 'v'); + helpers.assertCursorAt(2, 3); + eqCursorPos(makeCursor(2, 0), cm.getCursor('anchor')); + eq('123456\n2345\nbar', cm.getValue()); +}, { value: '123456\nfoo\nbar' }); +testVim('reselect_visual_line', function(cm, vim, helpers) { + helpers.doKeys('l', 'V', 'j', 'j', 'V', 'g', 'v', 'd'); + eq('foo\nand\nbar', cm.getValue()); + cm.setCursor(1, 0); + helpers.doKeys('V', 'y', 'j'); + helpers.doKeys('V', 'p' , 'g', 'v', 'd'); + eq('foo\nand', cm.getValue()); +}, { value: 'hello\nthis\nis\nfoo\nand\nbar' }); +testVim('reselect_visual_block', function(cm, vim, helpers) { + cm.setCursor(1, 2); + helpers.doKeys('<C-v>', 'k', 'h', '<C-v>'); + cm.setCursor(2, 1); + helpers.doKeys('v', 'l', 'g', 'v'); + eqCursorPos(Pos(1, 2), vim.sel.anchor); + eqCursorPos(Pos(0, 1), vim.sel.head); + // Ensure selection is done with visual block mode rather than one + // continuous range. + eq(cm.getSelections().join(''), '23oo') + helpers.doKeys('g', 'v'); + eqCursorPos(Pos(2, 1), vim.sel.anchor); + eqCursorPos(Pos(2, 2), vim.sel.head); + helpers.doKeys('<Esc>'); + // Ensure selection of deleted range + cm.setCursor(1, 1); + helpers.doKeys('v', '<C-v>', 'j', 'd', 'g', 'v'); + eq(cm.getSelections().join(''), 'or'); +}, { value: '123456\nfoo\nbar' }); +testVim('s_normal', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('s'); + helpers.doKeys('<Esc>'); + eq('ac', cm.getValue()); +}, { value: 'abc'}); +testVim('s_visual', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('v', 's'); + helpers.doKeys('<Esc>'); + helpers.assertCursorAt(0, 0); + eq('ac', cm.getValue()); +}, { value: 'abc'}); +testVim('o_visual', function(cm, vim, helpers) { + cm.setCursor(0,0); + helpers.doKeys('v','l','l','l','o'); + helpers.assertCursorAt(0,0); + helpers.doKeys('v','v','j','j','j','o'); + helpers.assertCursorAt(0,0); + helpers.doKeys('O'); + helpers.doKeys('l','l') + helpers.assertCursorAt(3, 3); + helpers.doKeys('d'); + eq('p',cm.getValue()); +}, { value: 'abcd\nefgh\nijkl\nmnop'}); +testVim('o_visual_block', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('<C-v>','3','j','l','l', 'o'); + eqCursorPos(Pos(3, 3), vim.sel.anchor); + eqCursorPos(Pos(0, 1), vim.sel.head); + helpers.doKeys('O'); + eqCursorPos(Pos(3, 1), vim.sel.anchor); + eqCursorPos(Pos(0, 3), vim.sel.head); + helpers.doKeys('o'); + eqCursorPos(Pos(0, 3), vim.sel.anchor); + eqCursorPos(Pos(3, 1), vim.sel.head); +}, { value: 'abcd\nefgh\nijkl\nmnop'}); +testVim('changeCase_visual', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('v', 'l', 'l'); + helpers.doKeys('U'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('v', 'l', 'l'); + helpers.doKeys('u'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('l', 'l', 'l', '.'); + helpers.assertCursorAt(0, 3); + cm.setCursor(0, 0); + helpers.doKeys('q', 'a', 'v', 'j', 'U', 'q'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('j', '@', 'a'); + helpers.assertCursorAt(1, 0); + cm.setCursor(3, 0); + helpers.doKeys('V', 'U', 'j', '.'); + eq('ABCDEF\nGHIJKL\nMnopq\nSHORT LINE\nLONG LINE OF TEXT', cm.getValue()); +}, { value: 'abcdef\nghijkl\nmnopq\nshort line\nlong line of text'}); +testVim('changeCase_visual_block', function(cm, vim, helpers) { + cm.setCursor(2, 1); + helpers.doKeys('<C-v>', 'k', 'k', 'h', 'U'); + eq('ABcdef\nGHijkl\nMNopq\nfoo', cm.getValue()); + cm.setCursor(0, 2); + helpers.doKeys('.'); + eq('ABCDef\nGHIJkl\nMNOPq\nfoo', cm.getValue()); + // check when last line is shorter. + cm.setCursor(2, 2); + helpers.doKeys('.'); + eq('ABCDef\nGHIJkl\nMNOPq\nfoO', cm.getValue()); +}, { value: 'abcdef\nghijkl\nmnopq\nfoo'}); +testVim('visual_paste', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('v', 'l', 'l', 'y'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('3', 'l', 'j', 'v', 'l', 'p'); + helpers.assertCursorAt(1, 5); + eq('this is a\nunithitest for visual paste', cm.getValue()); + cm.setCursor(0, 0); + // in case of pasting whole line + helpers.doKeys('y', 'y'); + cm.setCursor(1, 6); + helpers.doKeys('v', 'l', 'l', 'l', 'p'); + helpers.assertCursorAt(2, 0); + eq('this is a\nunithi\nthis is a\n for visual paste', cm.getValue()); +}, { value: 'this is a\nunit test for visual paste'}); + +// This checks the contents of the register used to paste the text +testVim('v_paste_from_register', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('"', 'a', 'y', 'w'); + cm.setCursor(1, 0); + helpers.doKeys('v', 'p'); + cm.openDialog = helpers.fakeOpenDialog('registers'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/a\s+register/.test(text)); + }); +}, { value: 'register contents\nare not erased'}); +testVim('S_normal', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('j', 'S'); + helpers.doKeys('<Esc>'); + helpers.assertCursorAt(1, 1); + eq('aa{\n \ncc', cm.getValue()); + helpers.doKeys('j', 'S'); + eq('aa{\n \n ', cm.getValue()); + helpers.assertCursorAt(2, 2); + helpers.doKeys('<Esc>'); + helpers.doKeys('d', 'd', 'd', 'd'); + helpers.assertCursorAt(0, 0); + helpers.doKeys('S'); + is(vim.insertMode); + eq('', cm.getValue()); +}, { value: 'aa{\nbb\ncc'}); +testVim('blockwise_paste', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('<C-v>', '3', 'j', 'l', 'y'); + cm.setCursor(0, 2); + // paste one char after the current cursor position + helpers.doKeys('p'); + eq('helhelo\nworwold\nfoofo\nbarba', cm.getValue()); + cm.setCursor(0, 0); + helpers.doKeys('v', '4', 'l', 'y'); + cm.setCursor(0, 0); + helpers.doKeys('<C-v>', '3', 'j', 'p'); + eq('helheelhelo\norwold\noofo\narba', cm.getValue()); +}, { value: 'hello\nworld\nfoo\nbar'}); +testVim('blockwise_paste_long/short_line', function(cm, vim, helpers) { + // extend short lines in case of different line lengths. + cm.setCursor(0, 0); + helpers.doKeys('<C-v>', 'j', 'j', 'y'); + cm.setCursor(0, 3); + helpers.doKeys('p'); + eq('hellho\nfoo f\nbar b', cm.getValue()); +}, { value: 'hello\nfoo\nbar'}); +testVim('blockwise_paste_cut_paste', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('<C-v>', '2', 'j', 'x'); + cm.setCursor(0, 0); + helpers.doKeys('P'); + eq('cut\nand\npaste\nme', cm.getValue()); +}, { value: 'cut\nand\npaste\nme'}); +testVim('blockwise_paste_from_register', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('<C-v>', '2', 'j', '"', 'a', 'y'); + cm.setCursor(0, 3); + helpers.doKeys('"', 'a', 'p'); + eq('foobfar\nhellho\nworlwd', cm.getValue()); +}, { value: 'foobar\nhello\nworld'}); +testVim('blockwise_paste_last_line', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('<C-v>', '2', 'j', 'l', 'y'); + cm.setCursor(3, 0); + helpers.doKeys('p'); + eq('cut\nand\npaste\nmcue\n an\n pa', cm.getValue()); +}, { value: 'cut\nand\npaste\nme'}); + +testVim('S_visual', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('v', 'j', 'S'); + helpers.doKeys('<Esc>'); + helpers.assertCursorAt(0, 0); + eq('\ncc', cm.getValue()); +}, { value: 'aa\nbb\ncc'}); + +testVim('d_/', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('match'); + helpers.doKeys('2', 'd', '/'); + helpers.assertCursorAt(0, 0); + eq('match \n next', cm.getValue()); + cm.openDialog = helpers.fakeOpenDialog('2'); + helpers.doKeys('d', ':'); + // TODO eq(' next', cm.getValue()); +}, { value: 'text match match \n next' }); +testVim('/ and n/N', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('match'); + helpers.doKeys('/'); + helpers.assertCursorAt(0, 11); + helpers.doKeys('n'); + helpers.assertCursorAt(1, 6); + helpers.doKeys('N'); + helpers.assertCursorAt(0, 11); + + cm.setCursor(0, 0); + helpers.doKeys('2', '/'); + helpers.assertCursorAt(1, 6); +}, { value: 'match nope match \n nope Match' }); +testVim('/_case', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('Match'); + helpers.doKeys('/'); + helpers.assertCursorAt(1, 6); +}, { value: 'match nope match \n nope Match' }); +testVim('/_2_pcre', function(cm, vim, helpers) { + CodeMirror.Vim.setOption('pcre', true); + cm.openDialog = helpers.fakeOpenDialog('(word){2}'); + helpers.doKeys('/'); + helpers.assertCursorAt(1, 9); + helpers.doKeys('n'); + helpers.assertCursorAt(2, 1); +}, { value: 'word\n another wordword\n wordwordword\n' }); +testVim('/_2_nopcre', function(cm, vim, helpers) { + CodeMirror.Vim.setOption('pcre', false); + cm.openDialog = helpers.fakeOpenDialog('\\(word\\)\\{2}'); + helpers.doKeys('/'); + helpers.assertCursorAt(1, 9); + helpers.doKeys('n'); + helpers.assertCursorAt(2, 1); +}, { value: 'word\n another wordword\n wordwordword\n' }); +testVim('/_nongreedy', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('aa'); + helpers.doKeys('/'); + helpers.assertCursorAt(0, 4); + helpers.doKeys('n'); + helpers.assertCursorAt(1, 3); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 0); +}, { value: 'aaa aa \n a aa'}); +testVim('?_nongreedy', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('aa'); + helpers.doKeys('?'); + helpers.assertCursorAt(1, 3); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 4); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 0); +}, { value: 'aaa aa \n a aa'}); +testVim('/_greedy', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('a+'); + helpers.doKeys('/'); + helpers.assertCursorAt(0, 4); + helpers.doKeys('n'); + helpers.assertCursorAt(1, 1); + helpers.doKeys('n'); + helpers.assertCursorAt(1, 3); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 0); +}, { value: 'aaa aa \n a aa'}); +testVim('?_greedy', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('a+'); + helpers.doKeys('?'); + helpers.assertCursorAt(1, 3); + helpers.doKeys('n'); + helpers.assertCursorAt(1, 1); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 4); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 0); +}, { value: 'aaa aa \n a aa'}); +testVim('/_greedy_0_or_more', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('a*'); + helpers.doKeys('/'); + helpers.assertCursorAt(0, 3); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 4); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 5); + helpers.doKeys('n'); + helpers.assertCursorAt(1, 0); + helpers.doKeys('n'); + helpers.assertCursorAt(1, 1); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 0); +}, { value: 'aaa aa\n aa'}); +testVim('?_greedy_0_or_more', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('a*'); + helpers.doKeys('?'); + helpers.assertCursorAt(1, 1); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 5); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 3); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 0); +}, { value: 'aaa aa\n aa'}); +testVim('? and n/N', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('match'); + helpers.doKeys('?'); + helpers.assertCursorAt(1, 6); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 11); + helpers.doKeys('N'); + helpers.assertCursorAt(1, 6); + + cm.setCursor(0, 0); + helpers.doKeys('2', '?'); + helpers.assertCursorAt(0, 11); +}, { value: 'match nope match \n nope Match' }); +testVim('*', function(cm, vim, helpers) { + cm.setCursor(0, 9); + helpers.doKeys('*'); + helpers.assertCursorAt(0, 22); + + cm.setCursor(0, 9); + helpers.doKeys('2', '*'); + helpers.assertCursorAt(1, 8); +}, { value: 'nomatch match nomatch match \nnomatch Match' }); +testVim('*_no_word', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('*'); + helpers.assertCursorAt(0, 0); +}, { value: ' \n match \n' }); +testVim('*_symbol', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('*'); + helpers.assertCursorAt(1, 0); +}, { value: ' /}\n/} match \n' }); +testVim('#', function(cm, vim, helpers) { + cm.setCursor(0, 9); + helpers.doKeys('#'); + helpers.assertCursorAt(1, 8); + + cm.setCursor(0, 9); + helpers.doKeys('2', '#'); + helpers.assertCursorAt(0, 22); +}, { value: 'nomatch match nomatch match \nnomatch Match' }); +testVim('*_seek', function(cm, vim, helpers) { + // Should skip over space and symbols. + cm.setCursor(0, 3); + helpers.doKeys('*'); + helpers.assertCursorAt(0, 22); +}, { value: ' := match nomatch match \nnomatch Match' }); +testVim('#', function(cm, vim, helpers) { + // Should skip over space and symbols. + cm.setCursor(0, 3); + helpers.doKeys('#'); + helpers.assertCursorAt(1, 8); +}, { value: ' := match nomatch match \nnomatch Match' }); +testVim('g*', function(cm, vim, helpers) { + cm.setCursor(0, 8); + helpers.doKeys('g', '*'); + helpers.assertCursorAt(0, 18); + cm.setCursor(0, 8); + helpers.doKeys('3', 'g', '*'); + helpers.assertCursorAt(1, 8); +}, { value: 'matches match alsoMatch\nmatchme matching' }); +testVim('g#', function(cm, vim, helpers) { + cm.setCursor(0, 8); + helpers.doKeys('g', '#'); + helpers.assertCursorAt(0, 0); + cm.setCursor(0, 8); + helpers.doKeys('3', 'g', '#'); + helpers.assertCursorAt(1, 0); +}, { value: 'matches match alsoMatch\nmatchme matching' }); +testVim('macro_insert', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'a', '0', 'i'); + helpers.doKeys('foo') + helpers.doKeys('<Esc>'); + helpers.doKeys('q', '@', 'a'); + eq('foofoo', cm.getValue()); +}, { value: ''}); +testVim('macro_insert_repeat', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'a', '$', 'a'); + helpers.doKeys('larry.') + helpers.doKeys('<Esc>'); + helpers.doKeys('a'); + helpers.doKeys('curly.') + helpers.doKeys('<Esc>'); + helpers.doKeys('q'); + helpers.doKeys('a'); + helpers.doKeys('moe.') + helpers.doKeys('<Esc>'); + helpers.doKeys('@', 'a'); + // At this point, the most recent edit should be the 2nd insert change + // inside the macro, i.e. "curly.". + helpers.doKeys('.'); + eq('larry.curly.moe.larry.curly.curly.', cm.getValue()); +}, { value: ''}); +testVim('macro_space', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('<Space>', '<Space>'); + helpers.assertCursorAt(0, 2); + helpers.doKeys('q', 'a', '<Space>', '<Space>', 'q'); + helpers.assertCursorAt(0, 4); + helpers.doKeys('@', 'a'); + helpers.assertCursorAt(0, 6); + helpers.doKeys('@', 'a'); + helpers.assertCursorAt(0, 8); +}, { value: 'one line of text.'}); +testVim('macro_t_search', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'a', 't', 'e', 'q'); + helpers.assertCursorAt(0, 1); + helpers.doKeys('l', '@', 'a'); + helpers.assertCursorAt(0, 6); + helpers.doKeys('l', ';'); + helpers.assertCursorAt(0, 12); +}, { value: 'one line of text.'}); +testVim('macro_f_search', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'b', 'f', 'e', 'q'); + helpers.assertCursorAt(0, 2); + helpers.doKeys('@', 'b'); + helpers.assertCursorAt(0, 7); + helpers.doKeys(';'); + helpers.assertCursorAt(0, 13); +}, { value: 'one line of text.'}); +testVim('macro_slash_search', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'c'); + cm.openDialog = helpers.fakeOpenDialog('e'); + helpers.doKeys('/', 'q'); + helpers.assertCursorAt(0, 2); + helpers.doKeys('@', 'c'); + helpers.assertCursorAt(0, 7); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 13); +}, { value: 'one line of text.'}); +testVim('macro_multislash_search', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'd'); + cm.openDialog = helpers.fakeOpenDialog('e'); + helpers.doKeys('/'); + cm.openDialog = helpers.fakeOpenDialog('t'); + helpers.doKeys('/', 'q'); + helpers.assertCursorAt(0, 12); + helpers.doKeys('@', 'd'); + helpers.assertCursorAt(0, 15); +}, { value: 'one line of text to rule them all.'}); +testVim('macro_last_ex_command_register', function (cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doEx('s/a/b'); + helpers.doKeys('2', '@', ':'); + eq('bbbaa', cm.getValue()); + helpers.assertCursorAt(0, 2); +}, { value: 'aaaaa'}); +testVim('macro_last_run_macro', function (cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'a', 'C', 'a', '<Esc>', 'q'); + helpers.doKeys('q', 'b', 'C', 'b', '<Esc>', 'q'); + helpers.doKeys('@', 'a'); + helpers.doKeys('d', 'd'); + helpers.doKeys('@', '@'); + eq('a', cm.getValue()); +}, { value: ''}); +testVim('macro_parens', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'z', 'i'); + helpers.doKeys('(') + helpers.doKeys('<Esc>'); + helpers.doKeys('e', 'a'); + helpers.doKeys(')') + helpers.doKeys('<Esc>'); + helpers.doKeys('q'); + helpers.doKeys('w', '@', 'z'); + helpers.doKeys('w', '@', 'z'); + eq('(see) (spot) (run)', cm.getValue()); +}, { value: 'see spot run'}); +testVim('macro_overwrite', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'z', '0', 'i'); + helpers.doKeys('I ') + helpers.doKeys('<Esc>'); + helpers.doKeys('q'); + helpers.doKeys('e'); + // Now replace the macro with something else. + helpers.doKeys('q', 'z', 'a'); + helpers.doKeys('.') + helpers.doKeys('<Esc>'); + helpers.doKeys('q'); + helpers.doKeys('e', '@', 'z'); + helpers.doKeys('e', '@', 'z'); + eq('I see. spot. run.', cm.getValue()); +}, { value: 'see spot run'}); +testVim('macro_search_f', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'a', 'f', ' '); + helpers.assertCursorAt(0,3); + helpers.doKeys('q', '0'); + helpers.assertCursorAt(0,0); + helpers.doKeys('@', 'a'); + helpers.assertCursorAt(0,3); +}, { value: 'The quick brown fox jumped over the lazy dog.'}); +testVim('macro_search_2f', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'a', '2', 'f', ' '); + helpers.assertCursorAt(0,9); + helpers.doKeys('q', '0'); + helpers.assertCursorAt(0,0); + helpers.doKeys('@', 'a'); + helpers.assertCursorAt(0,9); +}, { value: 'The quick brown fox jumped over the lazy dog.'}); +testVim('macro_yank_tick', function(cm, vim, helpers) { + cm.setCursor(0, 0); + // Start recording a macro into the \' register. + helpers.doKeys('q', '\''); + helpers.doKeys('y', '<Right>', '<Right>', '<Right>', '<Right>', 'p'); + helpers.assertCursorAt(0,4); + eq('the tex parrot', cm.getValue()); +}, { value: 'the ex parrot'}); +testVim('yank_register', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('"', 'a', 'y', 'y'); + helpers.doKeys('j', '"', 'b', 'y', 'y'); + cm.openDialog = helpers.fakeOpenDialog('registers'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/a\s+foo/.test(text)); + is(/b\s+bar/.test(text)); + }); + helpers.doKeys(':'); +}, { value: 'foo\nbar'}); +testVim('yank_visual_block', function(cm, vim, helpers) { + cm.setCursor(0, 1); + helpers.doKeys('<C-v>', 'l', 'j', '"', 'a', 'y'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/a\s+oo\nar/.test(text)); + }); + helpers.doKeys(':'); +}, { value: 'foo\nbar'}); +testVim('yank_append_line_to_line_register', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('"', 'a', 'y', 'y'); + helpers.doKeys('j', '"', 'A', 'y', 'y'); + cm.openDialog = helpers.fakeOpenDialog('registers'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/a\s+foo\nbar/.test(text)); + is(/"\s+foo\nbar/.test(text)); + }); + helpers.doKeys(':'); +}, { value: 'foo\nbar'}); +testVim('yank_append_word_to_word_register', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('"', 'a', 'y', 'w'); + helpers.doKeys('j', '"', 'A', 'y', 'w'); + cm.openDialog = helpers.fakeOpenDialog('registers'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/a\s+foobar/.test(text)); + is(/"\s+foobar/.test(text)); + }); + helpers.doKeys(':'); +}, { value: 'foo\nbar'}); +testVim('yank_append_line_to_word_register', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('"', 'a', 'y', 'w'); + helpers.doKeys('j', '"', 'A', 'y', 'y'); + cm.openDialog = helpers.fakeOpenDialog('registers'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/a\s+foo\nbar/.test(text)); + is(/"\s+foo\nbar/.test(text)); + }); + helpers.doKeys(':'); +}, { value: 'foo\nbar'}); +testVim('yank_append_word_to_line_register', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('"', 'a', 'y', 'y'); + helpers.doKeys('j', '"', 'A', 'y', 'w'); + cm.openDialog = helpers.fakeOpenDialog('registers'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/a\s+foo\nbar/.test(text)); + is(/"\s+foo\nbar/.test(text)); + }); + helpers.doKeys(':'); +}, { value: 'foo\nbar'}); +testVim('macro_register', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('q', 'a', 'i'); + helpers.doKeys('gangnam') + helpers.doKeys('<Esc>'); + helpers.doKeys('q'); + helpers.doKeys('q', 'b', 'o'); + helpers.doKeys('style') + helpers.doKeys('<Esc>'); + helpers.doKeys('q'); + cm.openDialog = helpers.fakeOpenDialog('registers'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/a\s+i/.test(text)); + is(/b\s+o/.test(text)); + }); + helpers.doKeys(':'); +}, { value: ''}); +testVim('._register', function(cm,vim,helpers) { + cm.setCursor(0,0); + helpers.doKeys('i'); + helpers.doKeys('foo') + helpers.doKeys('<Esc>'); + cm.openDialog = helpers.fakeOpenDialog('registers'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/\.\s+foo/.test(text)); + }); + helpers.doKeys(':'); +}, {value: ''}); +testVim(':_register', function(cm,vim,helpers) { + helpers.doEx('bar'); + cm.openDialog = helpers.fakeOpenDialog('registers'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/:\s+bar/.test(text)); + }); + helpers.doKeys(':'); +}, {value: ''}); +testVim('search_register_escape', function(cm, vim, helpers) { + // Check that the register is restored if the user escapes rather than confirms. + cm.openDialog = helpers.fakeOpenDialog('waldo'); + helpers.doKeys('/'); + var onKeyDown; + var onKeyUp; + var KEYCODES = { + f: 70, + o: 79, + Esc: 27 + }; + cm.openDialog = function(template, callback, options) { + onKeyDown = options.onKeyDown; + onKeyUp = options.onKeyUp; + }; + var close = function() {}; + helpers.doKeys('/'); + // Fake some keyboard events coming in. + onKeyDown({keyCode: KEYCODES.f}, '', close); + onKeyUp({keyCode: KEYCODES.f}, '', close); + onKeyDown({keyCode: KEYCODES.o}, 'f', close); + onKeyUp({keyCode: KEYCODES.o}, 'f', close); + onKeyDown({keyCode: KEYCODES.o}, 'fo', close); + onKeyUp({keyCode: KEYCODES.o}, 'fo', close); + onKeyDown({keyCode: KEYCODES.Esc}, 'foo', close); + cm.openDialog = helpers.fakeOpenDialog('registers'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/waldo/.test(text)); + is(!/foo/.test(text)); + }); + helpers.doKeys(':'); +}, {value: ''}); +testVim('search_register', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('foo'); + helpers.doKeys('/'); + cm.openDialog = helpers.fakeOpenDialog('registers'); + cm.openNotification = helpers.fakeOpenNotification(function(text) { + is(/\/\s+foo/.test(text)); + }); + helpers.doKeys(':'); +}, {value: ''}); +testVim('search_history', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('this'); + helpers.doKeys('/'); + cm.openDialog = helpers.fakeOpenDialog('checks'); + helpers.doKeys('/'); + cm.openDialog = helpers.fakeOpenDialog('search'); + helpers.doKeys('/'); + cm.openDialog = helpers.fakeOpenDialog('history'); + helpers.doKeys('/'); + cm.openDialog = helpers.fakeOpenDialog('checks'); + helpers.doKeys('/'); + var onKeyDown; + var onKeyUp; + var query = ''; + var keyCodes = { + Up: 38, + Down: 40 + }; + cm.openDialog = function(template, callback, options) { + onKeyUp = options.onKeyUp; + onKeyDown = options.onKeyDown; + }; + var close = function(newVal) { + if (typeof newVal == 'string') query = newVal; + } + helpers.doKeys('/'); + onKeyDown({keyCode: keyCodes.Up}, query, close); + onKeyUp({keyCode: keyCodes.Up}, query, close); + eq(query, 'checks'); + onKeyDown({keyCode: keyCodes.Up}, query, close); + onKeyUp({keyCode: keyCodes.Up}, query, close); + eq(query, 'history'); + onKeyDown({keyCode: keyCodes.Up}, query, close); + onKeyUp({keyCode: keyCodes.Up}, query, close); + eq(query, 'search'); + onKeyDown({keyCode: keyCodes.Up}, query, close); + onKeyUp({keyCode: keyCodes.Up}, query, close); + eq(query, 'this'); + onKeyDown({keyCode: keyCodes.Down}, query, close); + onKeyUp({keyCode: keyCodes.Down}, query, close); + eq(query, 'search'); +}, {value: ''}); +testVim('exCommand_history', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('registers'); + helpers.doKeys(':'); + cm.openDialog = helpers.fakeOpenDialog('sort'); + helpers.doKeys(':'); + cm.openDialog = helpers.fakeOpenDialog('map'); + helpers.doKeys(':'); + cm.openDialog = helpers.fakeOpenDialog('invalid'); + helpers.doKeys(':'); + var onKeyDown; + var onKeyUp; + var input = ''; + var keyCodes = { + Up: 38, + Down: 40, + s: 115 + }; + cm.openDialog = function(template, callback, options) { + onKeyUp = options.onKeyUp; + onKeyDown = options.onKeyDown; + }; + var close = function(newVal) { + if (typeof newVal == 'string') input = newVal; + } + helpers.doKeys(':'); + onKeyDown({keyCode: keyCodes.Up}, input, close); + eq(input, 'invalid'); + onKeyDown({keyCode: keyCodes.Up}, input, close); + eq(input, 'map'); + onKeyDown({keyCode: keyCodes.Up}, input, close); + eq(input, 'sort'); + onKeyDown({keyCode: keyCodes.Up}, input, close); + eq(input, 'registers'); + onKeyDown({keyCode: keyCodes.s}, '', close); + input = 's'; + onKeyDown({keyCode: keyCodes.Up}, input, close); + eq(input, 'sort'); +}, {value: ''}); +testVim('search_clear', function(cm, vim, helpers) { + var onKeyDown; + var input = ''; + var keyCodes = { + Ctrl: 17, + u: 85 + }; + cm.openDialog = function(template, callback, options) { + onKeyDown = options.onKeyDown; + }; + var close = function(newVal) { + if (typeof newVal == 'string') input = newVal; + } + helpers.doKeys('/'); + input = 'foo'; + onKeyDown({keyCode: keyCodes.Ctrl}, input, close); + onKeyDown({keyCode: keyCodes.u, ctrlKey: true}, input, close); + eq(input, ''); +}); +testVim('exCommand_clear', function(cm, vim, helpers) { + var onKeyDown; + var input = ''; + var keyCodes = { + Ctrl: 17, + u: 85 + }; + cm.openDialog = function(template, callback, options) { + onKeyDown = options.onKeyDown; + }; + var close = function(newVal) { + if (typeof newVal == 'string') input = newVal; + } + helpers.doKeys(':'); + input = 'foo'; + onKeyDown({keyCode: keyCodes.Ctrl}, input, close); + onKeyDown({keyCode: keyCodes.u, ctrlKey: true}, input, close); + eq(input, ''); +}); +testVim('.', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('2', 'd', 'w'); + helpers.doKeys('.'); + eq('5 6', cm.getValue()); +}, { value: '1 2 3 4 5 6'}); +testVim('._repeat', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('2', 'd', 'w'); + helpers.doKeys('3', '.'); + eq('6', cm.getValue()); +}, { value: '1 2 3 4 5 6'}); +testVim('._insert', function(cm, vim, helpers) { + helpers.doKeys('i'); + helpers.doKeys('test') + helpers.doKeys('<Esc>'); + helpers.doKeys('.'); + eq('testestt', cm.getValue()); + helpers.assertCursorAt(0, 6); + helpers.doKeys('O'); + helpers.doKeys('xyz') + helpers.doInsertModeKeys('Backspace'); + helpers.doInsertModeKeys('Down'); + helpers.doKeys('<Esc>'); + helpers.doKeys('.'); + eq('xy\nxy\ntestestt', cm.getValue()); + helpers.assertCursorAt(1, 1); +}, { value: ''}); +testVim('._insert_repeat', function(cm, vim, helpers) { + helpers.doKeys('i'); + helpers.doKeys('test') + cm.setCursor(0, 4); + helpers.doKeys('<Esc>'); + helpers.doKeys('2', '.'); + eq('testesttestt', cm.getValue()); + helpers.assertCursorAt(0, 10); +}, { value: ''}); +testVim('._repeat_insert', function(cm, vim, helpers) { + helpers.doKeys('3', 'i'); + helpers.doKeys('te') + cm.setCursor(0, 2); + helpers.doKeys('<Esc>'); + helpers.doKeys('.'); + eq('tetettetetee', cm.getValue()); + helpers.assertCursorAt(0, 10); +}, { value: ''}); +testVim('._insert_o', function(cm, vim, helpers) { + helpers.doKeys('o'); + helpers.doKeys('z') + cm.setCursor(1, 1); + helpers.doKeys('<Esc>'); + helpers.doKeys('.'); + eq('\nz\nz', cm.getValue()); + helpers.assertCursorAt(2, 0); +}, { value: ''}); +testVim('._insert_o_repeat', function(cm, vim, helpers) { + helpers.doKeys('o'); + helpers.doKeys('z') + helpers.doKeys('<Esc>'); + cm.setCursor(1, 0); + helpers.doKeys('2', '.'); + eq('\nz\nz\nz', cm.getValue()); + helpers.assertCursorAt(3, 0); +}, { value: ''}); +testVim('._insert_o_indent', function(cm, vim, helpers) { + helpers.doKeys('o'); + helpers.doKeys('z') + helpers.doKeys('<Esc>'); + cm.setCursor(1, 2); + helpers.doKeys('.'); + eq('{\n z\n z', cm.getValue()); + helpers.assertCursorAt(2, 2); +}, { value: '{'}); +testVim('._insert_cw', function(cm, vim, helpers) { + helpers.doKeys('c', 'w'); + helpers.doKeys('test') + helpers.doKeys('<Esc>'); + cm.setCursor(0, 3); + helpers.doKeys('2', 'l'); + helpers.doKeys('.'); + eq('test test word3', cm.getValue()); + helpers.assertCursorAt(0, 8); +}, { value: 'word1 word2 word3' }); +testVim('._insert_cw_repeat', function(cm, vim, helpers) { + // For some reason, repeat cw in desktop VIM will does not repeat insert mode + // changes. Will conform to that behavior. + helpers.doKeys('c', 'w'); + helpers.doKeys('test'); + helpers.doKeys('<Esc>'); + cm.setCursor(0, 4); + helpers.doKeys('l'); + helpers.doKeys('2', '.'); + eq('test test', cm.getValue()); + helpers.assertCursorAt(0, 8); +}, { value: 'word1 word2 word3' }); +testVim('._delete', function(cm, vim, helpers) { + cm.setCursor(0, 5); + helpers.doKeys('i'); + helpers.doInsertModeKeys('Backspace'); + helpers.doKeys('<Esc>'); + helpers.doKeys('.'); + eq('zace', cm.getValue()); + helpers.assertCursorAt(0, 1); +}, { value: 'zabcde'}); +testVim('._delete_repeat', function(cm, vim, helpers) { + cm.setCursor(0, 6); + helpers.doKeys('i'); + helpers.doInsertModeKeys('Backspace'); + helpers.doKeys('<Esc>'); + helpers.doKeys('2', '.'); + eq('zzce', cm.getValue()); + helpers.assertCursorAt(0, 1); +}, { value: 'zzabcde'}); +testVim('._visual_>', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('V', 'j', '>'); + cm.setCursor(2, 0) + helpers.doKeys('.'); + eq(' 1\n 2\n 3\n 4', cm.getValue()); + helpers.assertCursorAt(2, 2); +}, { value: '1\n2\n3\n4'}); +testVim('._replace_repeat', function(cm, vim, helpers) { + helpers.doKeys('R'); + cm.replaceRange('123', cm.getCursor(), offsetCursor(cm.getCursor(), 0, 3)); + cm.setCursor(0, 3); + helpers.doKeys('<Esc>'); + helpers.doKeys('2', '.'); + eq('12123123\nabcdefg', cm.getValue()); + helpers.assertCursorAt(0, 7); + cm.setCursor(1, 0); + helpers.doKeys('.'); + eq('12123123\n123123g', cm.getValue()); + helpers.doKeys('l', '"', '.', 'p'); + eq('12123123\n123123g123', cm.getValue()); +}, { value: 'abcdef\nabcdefg'}); +testVim('f;', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('f', 'x'); + helpers.doKeys(';'); + helpers.doKeys('2', ';'); + eq(9, cm.getCursor().ch); +}, { value: '01x3xx678x'}); +testVim('F;', function(cm, vim, helpers) { + cm.setCursor(0, 8); + helpers.doKeys('F', 'x'); + helpers.doKeys(';'); + helpers.doKeys('2', ';'); + eq(2, cm.getCursor().ch); +}, { value: '01x3xx6x8x'}); +testVim('t;', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('t', 'x'); + helpers.doKeys(';'); + helpers.doKeys('2', ';'); + eq(8, cm.getCursor().ch); +}, { value: '01x3xx678x'}); +testVim('T;', function(cm, vim, helpers) { + cm.setCursor(0, 9); + helpers.doKeys('T', 'x'); + helpers.doKeys(';'); + helpers.doKeys('2', ';'); + eq(2, cm.getCursor().ch); +}, { value: '0xx3xx678x'}); +testVim('f,', function(cm, vim, helpers) { + cm.setCursor(0, 6); + helpers.doKeys('f', 'x'); + helpers.doKeys(','); + helpers.doKeys('2', ','); + eq(2, cm.getCursor().ch); +}, { value: '01x3xx678x'}); +testVim('F,', function(cm, vim, helpers) { + cm.setCursor(0, 3); + helpers.doKeys('F', 'x'); + helpers.doKeys(','); + helpers.doKeys('2', ','); + eq(9, cm.getCursor().ch); +}, { value: '01x3xx678x'}); +testVim('t,', function(cm, vim, helpers) { + cm.setCursor(0, 6); + helpers.doKeys('t', 'x'); + helpers.doKeys(','); + helpers.doKeys('2', ','); + eq(3, cm.getCursor().ch); +}, { value: '01x3xx678x'}); +testVim('T,', function(cm, vim, helpers) { + cm.setCursor(0, 4); + helpers.doKeys('T', 'x'); + helpers.doKeys(','); + helpers.doKeys('2', ','); + eq(8, cm.getCursor().ch); +}, { value: '01x3xx67xx'}); +testVim('fd,;', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('f', '4'); + cm.setCursor(0, 0); + helpers.doKeys('d', ';'); + eq('56789', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 9); + helpers.doKeys('d', ','); + eq('01239', cm.getValue()); +}, { value: '0123456789'}); +testVim('Fd,;', function(cm, vim, helpers) { + cm.setCursor(0, 9); + helpers.doKeys('F', '4'); + cm.setCursor(0, 9); + helpers.doKeys('d', ';'); + eq('01239', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 0); + helpers.doKeys('d', ','); + eq('56789', cm.getValue()); +}, { value: '0123456789'}); +testVim('td,;', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('t', '4'); + cm.setCursor(0, 0); + helpers.doKeys('d', ';'); + eq('456789', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 9); + helpers.doKeys('d', ','); + eq('012349', cm.getValue()); +}, { value: '0123456789'}); +testVim('Td,;', function(cm, vim, helpers) { + cm.setCursor(0, 9); + helpers.doKeys('T', '4'); + cm.setCursor(0, 9); + helpers.doKeys('d', ';'); + eq('012349', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 0); + helpers.doKeys('d', ','); + eq('456789', cm.getValue()); +}, { value: '0123456789'}); +testVim('fc,;', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('f', '4'); + cm.setCursor(0, 0); + helpers.doKeys('c', ';', '<Esc>'); + eq('56789', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 9); + helpers.doKeys('c', ','); + eq('01239', cm.getValue()); +}, { value: '0123456789'}); +testVim('Fc,;', function(cm, vim, helpers) { + cm.setCursor(0, 9); + helpers.doKeys('F', '4'); + cm.setCursor(0, 9); + helpers.doKeys('c', ';', '<Esc>'); + eq('01239', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 0); + helpers.doKeys('c', ','); + eq('56789', cm.getValue()); +}, { value: '0123456789'}); +testVim('tc,;', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('t', '4'); + cm.setCursor(0, 0); + helpers.doKeys('c', ';', '<Esc>'); + eq('456789', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 9); + helpers.doKeys('c', ','); + eq('012349', cm.getValue()); +}, { value: '0123456789'}); +testVim('Tc,;', function(cm, vim, helpers) { + cm.setCursor(0, 9); + helpers.doKeys('T', '4'); + cm.setCursor(0, 9); + helpers.doKeys('c', ';', '<Esc>'); + eq('012349', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 0); + helpers.doKeys('c', ','); + eq('456789', cm.getValue()); +}, { value: '0123456789'}); +testVim('fy,;', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('f', '4'); + cm.setCursor(0, 0); + helpers.doKeys('y', ';', 'P'); + eq('012340123456789', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 9); + helpers.doKeys('y', ',', 'P'); + eq('012345678456789', cm.getValue()); +}, { value: '0123456789'}); +testVim('Fy,;', function(cm, vim, helpers) { + cm.setCursor(0, 9); + helpers.doKeys('F', '4'); + cm.setCursor(0, 9); + helpers.doKeys('y', ';', 'p'); + eq('012345678945678', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 0); + helpers.doKeys('y', ',', 'P'); + eq('012340123456789', cm.getValue()); +}, { value: '0123456789'}); +testVim('ty,;', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys('t', '4'); + cm.setCursor(0, 0); + helpers.doKeys('y', ';', 'P'); + eq('01230123456789', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 9); + helpers.doKeys('y', ',', 'p'); + eq('01234567895678', cm.getValue()); +}, { value: '0123456789'}); +testVim('Ty,;', function(cm, vim, helpers) { + cm.setCursor(0, 9); + helpers.doKeys('T', '4'); + cm.setCursor(0, 9); + helpers.doKeys('y', ';', 'p'); + eq('01234567895678', cm.getValue()); + helpers.doKeys('u'); + cm.setCursor(0, 0); + helpers.doKeys('y', ',', 'P'); + eq('01230123456789', cm.getValue()); +}, { value: '0123456789'}); +testVim('HML', function(cm, vim, helpers) { + var lines = 35; + var textHeight = cm.defaultTextHeight(); + cm.setSize(600, lines*textHeight); + cm.setCursor(120, 0); + helpers.doKeys('H'); + helpers.assertCursorAt(86, 2); + helpers.doKeys('L'); + helpers.assertCursorAt(120, 4); + helpers.doKeys('M'); + helpers.assertCursorAt(103,4); +}, { value: (function(){ + var lines = new Array(100); + var upper = ' xx\n'; + var lower = ' xx\n'; + upper = lines.join(upper); + lower = lines.join(lower); + return upper + lower; +})()}); + +var zVals = []; +forEach(['zb','zz','zt','z-','z.','z<CR>'], function(e, idx){ + var lineNum = 250; + var lines = 35; + testVim(e, function(cm, vim, helpers) { + var k1 = e[0]; + var k2 = e.substring(1); + var textHeight = cm.defaultTextHeight(); + cm.setSize(600, lines*textHeight); + cm.setCursor(lineNum, 0); + helpers.doKeys(k1, k2); + zVals[idx] = cm.getScrollInfo().top; + }, { value: (function(){ + return new Array(500).join('\n'); + })()}); +}); +testVim('zb_to_bottom', function(cm, vim, helpers){ + var lineNum = 250; + cm.setSize(600, 35*cm.defaultTextHeight()); + cm.setCursor(lineNum, 0); + helpers.doKeys('z', 'b'); + var scrollInfo = cm.getScrollInfo(); + eq(scrollInfo.top + scrollInfo.clientHeight, cm.charCoords(Pos(lineNum, 0), 'local').bottom); +}, { value: (function(){ + return new Array(500).join('\n'); +})()}); +testVim('zt_to_top', function(cm, vim, helpers){ + var lineNum = 250; + cm.setSize(600, 35*cm.defaultTextHeight()); + cm.setCursor(lineNum, 0); + helpers.doKeys('z', 't'); + eq(cm.getScrollInfo().top, cm.charCoords(Pos(lineNum, 0), 'local').top); +}, { value: (function(){ + return new Array(500).join('\n'); +})()}); +testVim('zb<zz', function(cm, vim, helpers){ + eq(zVals[0]<zVals[1], true); +}); +testVim('zz<zt', function(cm, vim, helpers){ + eq(zVals[1]<zVals[2], true); +}); +testVim('zb==z-', function(cm, vim, helpers){ + eq(zVals[0], zVals[3]); +}); +testVim('zz==z.', function(cm, vim, helpers){ + eq(zVals[1], zVals[4]); +}); +testVim('zt==z<CR>', function(cm, vim, helpers){ + eq(zVals[2], zVals[5]); +}); + +var moveTillCharacterSandbox = + 'The quick brown fox \n'; +testVim('moveTillCharacter', function(cm, vim, helpers){ + cm.setCursor(0, 0); + // Search for the 'q'. + cm.openDialog = helpers.fakeOpenDialog('q'); + helpers.doKeys('/'); + eq(4, cm.getCursor().ch); + // Jump to just before the first o in the list. + helpers.doKeys('t'); + helpers.doKeys('o'); + eq('The quick brown fox \n', cm.getValue()); + // Delete that one character. + helpers.doKeys('d'); + helpers.doKeys('t'); + helpers.doKeys('o'); + eq('The quick bown fox \n', cm.getValue()); + // Delete everything until the next 'o'. + helpers.doKeys('.'); + eq('The quick box \n', cm.getValue()); + // An unmatched character should have no effect. + helpers.doKeys('d'); + helpers.doKeys('t'); + helpers.doKeys('q'); + eq('The quick box \n', cm.getValue()); + // Matches should only be possible on single lines. + helpers.doKeys('d'); + helpers.doKeys('t'); + helpers.doKeys('z'); + eq('The quick box \n', cm.getValue()); + // After all that, the search for 'q' should still be active, so the 'N' command + // can run it again in reverse. Use that to delete everything back to the 'q'. + helpers.doKeys('d'); + helpers.doKeys('N'); + eq('The ox \n', cm.getValue()); + eq(4, cm.getCursor().ch); +}, { value: moveTillCharacterSandbox}); +testVim('searchForPipe', function(cm, vim, helpers){ + CodeMirror.Vim.setOption('pcre', false); + cm.setCursor(0, 0); + // Search for the '|'. + cm.openDialog = helpers.fakeOpenDialog('|'); + helpers.doKeys('/'); + eq(4, cm.getCursor().ch); +}, { value: 'this|that'}); + + +var scrollMotionSandbox = + '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n'; +testVim('scrollMotion', function(cm, vim, helpers){ + var prevCursor, prevScrollInfo; + cm.setCursor(0, 0); + // ctrl-y at the top of the file should have no effect. + helpers.doKeys('<C-y>'); + eq(0, cm.getCursor().line); + prevScrollInfo = cm.getScrollInfo(); + helpers.doKeys('<C-e>'); + eq(1, cm.getCursor().line); + is(prevScrollInfo.top < cm.getScrollInfo().top); + // Jump to the end of the sandbox. + cm.setCursor(1000, 0); + prevCursor = cm.getCursor(); + // ctrl-e at the bottom of the file should have no effect. + helpers.doKeys('<C-e>'); + eq(prevCursor.line, cm.getCursor().line); + prevScrollInfo = cm.getScrollInfo(); + helpers.doKeys('<C-y>'); + eq(prevCursor.line - 1, cm.getCursor().line, "Y"); + is(prevScrollInfo.top > cm.getScrollInfo().top); +}, { value: scrollMotionSandbox}); + +var squareBracketMotionSandbox = ''+ + '({\n'+//0 + ' ({\n'+//11 + ' /*comment {\n'+//2 + ' */(\n'+//3 + '#else \n'+//4 + ' /* )\n'+//5 + '#if }\n'+//6 + ' )}*/\n'+//7 + ')}\n'+//8 + '{}\n'+//9 + '#else {{\n'+//10 + '{}\n'+//11 + '}\n'+//12 + '{\n'+//13 + '#endif\n'+//14 + '}\n'+//15 + '}\n'+//16 + '#else';//17 +testVim('[[, ]]', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys(']', ']'); + helpers.assertCursorAt(9,0); + helpers.doKeys('2', ']', ']'); + helpers.assertCursorAt(13,0); + helpers.doKeys(']', ']'); + helpers.assertCursorAt(17,0); + helpers.doKeys('[', '['); + helpers.assertCursorAt(13,0); + helpers.doKeys('2', '[', '['); + helpers.assertCursorAt(9,0); + helpers.doKeys('[', '['); + helpers.assertCursorAt(0,0); +}, { value: squareBracketMotionSandbox}); +testVim('[], ][', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doKeys(']', '['); + helpers.assertCursorAt(12,0); + helpers.doKeys('2', ']', '['); + helpers.assertCursorAt(16,0); + helpers.doKeys(']', '['); + helpers.assertCursorAt(17,0); + helpers.doKeys('[', ']'); + helpers.assertCursorAt(16,0); + helpers.doKeys('2', '[', ']'); + helpers.assertCursorAt(12,0); + helpers.doKeys('[', ']'); + helpers.assertCursorAt(0,0); +}, { value: squareBracketMotionSandbox}); +testVim('[{, ]}', function(cm, vim, helpers) { + cm.setCursor(4, 10); + helpers.doKeys('[', '{'); + helpers.assertCursorAt(2,12); + helpers.doKeys('2', '[', '{'); + helpers.assertCursorAt(0,1); + cm.setCursor(4, 10); + helpers.doKeys(']', '}'); + helpers.assertCursorAt(6,11); + helpers.doKeys('2', ']', '}'); + helpers.assertCursorAt(8,1); + cm.setCursor(0,1); + helpers.doKeys(']', '}'); + helpers.assertCursorAt(8,1); + helpers.doKeys('[', '{'); + helpers.assertCursorAt(0,1); +}, { value: squareBracketMotionSandbox}); +testVim('[(, ])', function(cm, vim, helpers) { + cm.setCursor(4, 10); + helpers.doKeys('[', '('); + helpers.assertCursorAt(3,14); + helpers.doKeys('2', '[', '('); + helpers.assertCursorAt(0,0); + cm.setCursor(4, 10); + helpers.doKeys(']', ')'); + helpers.assertCursorAt(5,11); + helpers.doKeys('2', ']', ')'); + helpers.assertCursorAt(8,0); + helpers.doKeys('[', '('); + helpers.assertCursorAt(0,0); + helpers.doKeys(']', ')'); + helpers.assertCursorAt(8,0); +}, { value: squareBracketMotionSandbox}); +testVim('[*, ]*, [/, ]/', function(cm, vim, helpers) { + forEach(['*', '/'], function(key){ + cm.setCursor(7, 0); + helpers.doKeys('2', '[', key); + helpers.assertCursorAt(2,2); + helpers.doKeys('2', ']', key); + helpers.assertCursorAt(7,5); + }); +}, { value: squareBracketMotionSandbox}); +testVim('[#, ]#', function(cm, vim, helpers) { + cm.setCursor(10, 3); + helpers.doKeys('2', '[', '#'); + helpers.assertCursorAt(4,0); + helpers.doKeys('5', ']', '#'); + helpers.assertCursorAt(17,0); + cm.setCursor(10, 3); + helpers.doKeys(']', '#'); + helpers.assertCursorAt(14,0); +}, { value: squareBracketMotionSandbox}); +testVim('[m, ]m, [M, ]M', function(cm, vim, helpers) { + cm.setCursor(11, 0); + helpers.doKeys('[', 'm'); + helpers.assertCursorAt(10,7); + helpers.doKeys('4', '[', 'm'); + helpers.assertCursorAt(1,3); + helpers.doKeys('5', ']', 'm'); + helpers.assertCursorAt(11,0); + helpers.doKeys('[', 'M'); + helpers.assertCursorAt(9,1); + helpers.doKeys('3', ']', 'M'); + helpers.assertCursorAt(15,0); + helpers.doKeys('5', '[', 'M'); + helpers.assertCursorAt(7,3); +}, { value: squareBracketMotionSandbox}); + +testVim('i_indent_right', function(cm, vim, helpers) { + cm.setCursor(0, 3); + var expectedValue = ' word1\nword2\nword3 '; + helpers.doKeys('i', '<C-t>'); + eq(expectedValue, cm.getValue()); + helpers.assertCursorAt(0, 5); +}, { value: ' word1\nword2\nword3 ', indentUnit: 2 }); +testVim('i_indent_left', function(cm, vim, helpers) { + cm.setCursor(0, 3); + var expectedValue = ' word1\nword2\nword3 '; + helpers.doKeys('i', '<C-d>'); + eq(expectedValue, cm.getValue()); + helpers.assertCursorAt(0, 1); +}, { value: ' word1\nword2\nword3 ', indentUnit: 2 }); + +// Ex mode tests +testVim('ex_go_to_line', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doEx('4'); + helpers.assertCursorAt(3, 0); +}, { value: 'a\nb\nc\nd\ne\n'}); +testVim('ex_go_to_mark', function(cm, vim, helpers) { + cm.setCursor(3, 0); + helpers.doKeys('m', 'a'); + cm.setCursor(0, 0); + helpers.doEx('\'a'); + helpers.assertCursorAt(3, 0); +}, { value: 'a\nb\nc\nd\ne\n'}); +testVim('ex_go_to_line_offset', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doEx('+3'); + helpers.assertCursorAt(3, 0); + helpers.doEx('-1'); + helpers.assertCursorAt(2, 0); + helpers.doEx('.2'); + helpers.assertCursorAt(4, 0); + helpers.doEx('.-3'); + helpers.assertCursorAt(1, 0); +}, { value: 'a\nb\nc\nd\ne\n'}); +testVim('ex_go_to_mark_offset', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doKeys('m', 'a'); + cm.setCursor(0, 0); + helpers.doEx('\'a1'); + helpers.assertCursorAt(3, 0); + helpers.doEx('\'a-1'); + helpers.assertCursorAt(1, 0); + helpers.doEx('\'a+2'); + helpers.assertCursorAt(4, 0); +}, { value: 'a\nb\nc\nd\ne\n'}); +testVim('ex_write', function(cm, vim, helpers) { + var tmp = CodeMirror.commands.save; + var written; + var actualCm; + CodeMirror.commands.save = function(cm) { + written = true; + actualCm = cm; + }; + // Test that w, wr, wri ... write all trigger :write. + var command = 'write'; + for (var i = 1; i < command.length; i++) { + written = false; + actualCm = null; + helpers.doEx(command.substring(0, i)); + eq(written, true); + eq(actualCm, cm); + } + CodeMirror.commands.save = tmp; +}); +testVim('ex_sort', function(cm, vim, helpers) { + helpers.doEx('sort'); + eq('Z\na\nb\nc\nd', cm.getValue()); +}, { value: 'b\nZ\nd\nc\na'}); +testVim('ex_sort_reverse', function(cm, vim, helpers) { + helpers.doEx('sort!'); + eq('d\nc\nb\na', cm.getValue()); +}, { value: 'b\nd\nc\na'}); +testVim('ex_sort_range', function(cm, vim, helpers) { + helpers.doEx('2,3sort'); + eq('b\nc\nd\na', cm.getValue()); +}, { value: 'b\nd\nc\na'}); +testVim('ex_sort_oneline', function(cm, vim, helpers) { + helpers.doEx('2sort'); + // Expect no change. + eq('b\nd\nc\na', cm.getValue()); +}, { value: 'b\nd\nc\na'}); +testVim('ex_sort_ignoreCase', function(cm, vim, helpers) { + helpers.doEx('sort i'); + eq('a\nb\nc\nd\nZ', cm.getValue()); +}, { value: 'b\nZ\nd\nc\na'}); +testVim('ex_sort_unique', function(cm, vim, helpers) { + helpers.doEx('sort u'); + eq('Z\na\nb\nc\nd', cm.getValue()); +}, { value: 'b\nZ\na\na\nd\na\nc\na'}); +testVim('ex_sort_decimal', function(cm, vim, helpers) { + helpers.doEx('sort d'); + eq('d3\n s5\n6\n.9', cm.getValue()); +}, { value: '6\nd3\n s5\n.9'}); +testVim('ex_sort_decimal_negative', function(cm, vim, helpers) { + helpers.doEx('sort d'); + eq('z-9\nd3\n s5\n6\n.9', cm.getValue()); +}, { value: '6\nd3\n s5\n.9\nz-9'}); +testVim('ex_sort_decimal_reverse', function(cm, vim, helpers) { + helpers.doEx('sort! d'); + eq('.9\n6\n s5\nd3', cm.getValue()); +}, { value: '6\nd3\n s5\n.9'}); +testVim('ex_sort_hex', function(cm, vim, helpers) { + helpers.doEx('sort x'); + eq(' s5\n6\n.9\n&0xB\nd3', cm.getValue()); +}, { value: '6\nd3\n s5\n&0xB\n.9'}); +testVim('ex_sort_octal', function(cm, vim, helpers) { + helpers.doEx('sort o'); + eq('.9\n.8\nd3\n s5\n6', cm.getValue()); +}, { value: '6\nd3\n s5\n.9\n.8'}); +testVim('ex_sort_decimal_mixed', function(cm, vim, helpers) { + helpers.doEx('sort d'); + eq('z\ny\nc1\nb2\na3', cm.getValue()); +}, { value: 'a3\nz\nc1\ny\nb2'}); +testVim('ex_sort_decimal_mixed_reverse', function(cm, vim, helpers) { + helpers.doEx('sort! d'); + eq('a3\nb2\nc1\nz\ny', cm.getValue()); +}, { value: 'a3\nz\nc1\ny\nb2'}); +testVim('ex_sort_pattern_alpha', function(cm, vim, helpers) { + helpers.doEx('sort /[a-z]/'); + eq('a3\nb2\nc1\ny\nz', cm.getValue()); +}, { value: 'z\ny\nc1\nb2\na3'}); +testVim('ex_sort_pattern_alpha_reverse', function(cm, vim, helpers) { + helpers.doEx('sort! /[a-z]/'); + eq('z\ny\nc1\nb2\na3', cm.getValue()); +}, { value: 'z\ny\nc1\nb2\na3'}); +testVim('ex_sort_pattern_alpha_ignoreCase', function(cm, vim, helpers) { + helpers.doEx('sort i/[a-z]/'); + eq('a3\nb2\nC1\nY\nz', cm.getValue()); +}, { value: 'z\nY\nC1\nb2\na3'}); +testVim('ex_sort_pattern_alpha_longer', function(cm, vim, helpers) { + helpers.doEx('sort /[a-z]+/'); + eq('a\naa\nab\nade\nadele\nadelle\nadriana\nalex\nalexandra\nb\nc\ny\nz', cm.getValue()); +}, { value: 'z\nab\naa\nade\nadelle\nalexandra\nalex\nadriana\nadele\ny\nc\nb\na'}); +testVim('ex_sort_pattern_alpha_only', function(cm, vim, helpers) { + helpers.doEx('sort /^[a-z]$/'); + eq('z1\ny2\na3\nb\nc', cm.getValue()); +}, { value: 'z1\ny2\na3\nc\nb'}); +testVim('ex_sort_pattern_alpha_only_reverse', function(cm, vim, helpers) { + helpers.doEx('sort! /^[a-z]$/'); + eq('c\nb\nz1\ny2\na3', cm.getValue()); +}, { value: 'z1\ny2\na3\nc\nb'}); +testVim('ex_sort_pattern_alpha_num', function(cm, vim, helpers) { + helpers.doEx('sort /[a-z][0-9]/'); + eq('c\nb\na3\ny2\nz1', cm.getValue()); +}, { value: 'z1\ny2\na3\nc\nb'}); +// test for :global command +testVim('ex_global', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doEx('g/one/s//two'); + eq('two two\n two two\n two two', cm.getValue()); + helpers.doEx('1,2g/two/s//one'); + eq('one one\n one one\n two two', cm.getValue()); +}, {value: 'one one\n one one\n one one'}); +testVim('ex_global_confirm', function(cm, vim, helpers) { + cm.setCursor(0, 0); + var onKeyDown; + var openDialogSave = cm.openDialog; + var KEYCODES = { + a: 65, + n: 78, + q: 81, + y: 89 + }; + // Intercept the ex command, 'global' + cm.openDialog = function(template, callback, options) { + // Intercept the prompt for the embedded ex command, 'substitute' + cm.openDialog = function(template, callback, options) { + onKeyDown = options.onKeyDown; + }; + callback('g/one/s//two/gc'); + }; + helpers.doKeys(':'); + var close = function() {}; + onKeyDown({keyCode: KEYCODES.n}, '', close); + onKeyDown({keyCode: KEYCODES.y}, '', close); + onKeyDown({keyCode: KEYCODES.a}, '', close); + onKeyDown({keyCode: KEYCODES.q}, '', close); + onKeyDown({keyCode: KEYCODES.y}, '', close); + eq('one two\n two two\n one one\n two one\n one one', cm.getValue()); +}, {value: 'one one\n one one\n one one\n one one\n one one'}); +// Basic substitute tests. +testVim('ex_substitute_same_line', function(cm, vim, helpers) { + cm.setCursor(1, 0); + helpers.doEx('s/one/two/g'); + eq('one one\n two two', cm.getValue()); +}, { value: 'one one\n one one'}); +testVim('ex_substitute_alternate_separator', function(cm, vim, helpers) { + cm.setCursor(1, 0); + helpers.doEx('s#o/e#two#g'); + eq('o/e o/e\n two two', cm.getValue()); +}, { value: 'o/e o/e\n o/e o/e'}); +testVim('ex_substitute_full_file', function(cm, vim, helpers) { + cm.setCursor(1, 0); + helpers.doEx('%s/one/two/g'); + eq('two two\n two two', cm.getValue()); +}, { value: 'one one\n one one'}); +testVim('ex_substitute_input_range', function(cm, vim, helpers) { + cm.setCursor(1, 0); + helpers.doEx('1,3s/\\d/0/g'); + eq('0\n0\n0\n4', cm.getValue()); +}, { value: '1\n2\n3\n4' }); +testVim('ex_substitute_range_current_to_input', function(cm, vim, helpers) { + cm.setCursor(1, 0); + helpers.doEx('.,3s/\\d/0/g'); + eq('1\n0\n0\n4', cm.getValue()); +}, { value: '1\n2\n3\n4' }); +testVim('ex_substitute_range_input_to_current', function(cm, vim, helpers) { + cm.setCursor(3, 0); + helpers.doEx('2,.s/\\d/0/g'); + eq('1\n0\n0\n0\n5', cm.getValue()); +}, { value: '1\n2\n3\n4\n5' }); +testVim('ex_substitute_range_offset', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doEx('-1,+1s/\\d/0/g'); + eq('1\n0\n0\n0\n5', cm.getValue()); +}, { value: '1\n2\n3\n4\n5' }); +testVim('ex_substitute_range_implicit_offset', function(cm, vim, helpers) { + cm.setCursor(0, 0); + helpers.doEx('.1,.3s/\\d/0/g'); + eq('1\n0\n0\n0\n5', cm.getValue()); +}, { value: '1\n2\n3\n4\n5' }); +testVim('ex_substitute_to_eof', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doEx('.,$s/\\d/0/g'); + eq('1\n2\n0\n0\n0', cm.getValue()); +}, { value: '1\n2\n3\n4\n5' }); +testVim('ex_substitute_to_relative_eof', function(cm, vim, helpers) { + cm.setCursor(4, 0); + helpers.doEx('2,$-2s/\\d/0/g'); + eq('1\n0\n0\n4\n5', cm.getValue()); +}, { value: '1\n2\n3\n4\n5' }); +testVim('ex_substitute_range_mark', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doKeys('ma'); + cm.setCursor(0, 0); + helpers.doEx('.,\'as/\\d/0/g'); + eq('0\n0\n0\n4\n5', cm.getValue()); +}, { value: '1\n2\n3\n4\n5' }); +testVim('ex_substitute_range_mark_offset', function(cm, vim, helpers) { + cm.setCursor(2, 0); + helpers.doKeys('ma'); + cm.setCursor(0, 0); + helpers.doEx('\'a-1,\'a+1s/\\d/0/g'); + eq('1\n0\n0\n0\n5', cm.getValue()); +}, { value: '1\n2\n3\n4\n5' }); +testVim('ex_substitute_visual_range', function(cm, vim, helpers) { + cm.setCursor(1, 0); + // Set last visual mode selection marks '< and '> at lines 2 and 4 + helpers.doKeys('V', '2', 'j', 'v'); + helpers.doEx('\'<,\'>s/\\d/0/g'); + eq('1\n0\n0\n0\n5', cm.getValue()); +}, { value: '1\n2\n3\n4\n5' }); +testVim('ex_substitute_empty_query', function(cm, vim, helpers) { + // If the query is empty, use last query. + cm.setCursor(1, 0); + cm.openDialog = helpers.fakeOpenDialog('1'); + helpers.doKeys('/'); + helpers.doEx('s//b/g'); + eq('abb ab2 ab3', cm.getValue()); +}, { value: 'a11 a12 a13' }); +testVim('ex_substitute_javascript', function(cm, vim, helpers) { + CodeMirror.Vim.setOption('pcre', false); + cm.setCursor(1, 0); + // Throw all the things that javascript likes to treat as special values + // into the replace part. All should be literal (this is VIM). + helpers.doEx('s/\\(\\d+\\)/$$ $\' $` $& \\1/g') + eq('a $$ $\' $` $& 0 b', cm.getValue()); +}, { value: 'a 0 b' }); +testVim('ex_substitute_empty_arguments', function(cm,vim,helpers) { + cm.setCursor(0, 0); + helpers.doEx('s/a/b/g'); + cm.setCursor(1, 0); + helpers.doEx('s'); + eq('b b\nb a', cm.getValue()); +}, {value: 'a a\na a'}); + +// More complex substitute tests that test both pcre and nopcre options. +function testSubstitute(name, options) { + testVim(name + '_pcre', function(cm, vim, helpers) { + cm.setCursor(1, 0); + CodeMirror.Vim.setOption('pcre', true); + helpers.doEx(options.expr); + eq(options.expectedValue, cm.getValue()); + }, options); + // If no noPcreExpr is defined, assume that it's the same as the expr. + var noPcreExpr = options.noPcreExpr ? options.noPcreExpr : options.expr; + testVim(name + '_nopcre', function(cm, vim, helpers) { + cm.setCursor(1, 0); + CodeMirror.Vim.setOption('pcre', false); + helpers.doEx(noPcreExpr); + eq(options.expectedValue, cm.getValue()); + }, options); +} +testSubstitute('ex_substitute_capture', { + value: 'a11 a12 a13', + expectedValue: 'a1111 a1212 a1313', + // $n is a backreference + expr: 's/(\\d+)/$1$1/g', + // \n is a backreference. + noPcreExpr: 's/\\(\\d+\\)/\\1\\1/g'}); +testSubstitute('ex_substitute_capture2', { + value: 'a 0 b', + expectedValue: 'a $00 b', + expr: 's/(\\d+)/$$$1$1/g', + noPcreExpr: 's/\\(\\d+\\)/$\\1\\1/g'}); +testSubstitute('ex_substitute_nocapture', { + value: 'a11 a12 a13', + expectedValue: 'a$1$1 a$1$1 a$1$1', + expr: 's/(\\d+)/$$1$$1/g', + noPcreExpr: 's/\\(\\d+\\)/$1$1/g'}); +testSubstitute('ex_substitute_nocapture2', { + value: 'a 0 b', + expectedValue: 'a $10 b', + expr: 's/(\\d+)/$$1$1/g', + noPcreExpr: 's/\\(\\d+\\)/\\$1\\1/g'}); +testSubstitute('ex_substitute_nocapture', { + value: 'a b c', + expectedValue: 'a $ c', + expr: 's/b/$$/', + noPcreExpr: 's/b/$/'}); +testSubstitute('ex_substitute_slash_regex', { + value: 'one/two \n three/four', + expectedValue: 'one|two \n three|four', + expr: '%s/\\//|'}); +testSubstitute('ex_substitute_pipe_regex', { + value: 'one|two \n three|four', + expectedValue: 'one,two \n three,four', + expr: '%s/\\|/,/', + noPcreExpr: '%s/|/,/'}); +testSubstitute('ex_substitute_or_regex', { + value: 'one|two \n three|four', + expectedValue: 'ana|twa \n thraa|faar', + expr: '%s/o|e|u/a/g', + noPcreExpr: '%s/o\\|e\\|u/a/g'}); +testSubstitute('ex_substitute_or_word_regex', { + value: 'one|two \n three|four', + expectedValue: 'five|five \n three|four', + expr: '%s/(one|two)/five/g', + noPcreExpr: '%s/\\(one\\|two\\)/five/g'}); +testSubstitute('ex_substitute_forward_slash_regex', { + value: 'forward slash \/ was here', + expectedValue: 'forward slash was here', + expr: '%s#\\/##g', + noPcreExpr: '%s#/##g'}); +testVim("ex_substitute_ampersand_pcre", function(cm, vim, helpers) { + cm.setCursor(0, 0); + CodeMirror.Vim.setOption('pcre', true); + helpers.doEx('%s/foo/namespace.&/'); + eq("namespace.foo", cm.getValue()); + }, { value: 'foo' }); +testVim("ex_substitute_ampersand_multiple_pcre", function(cm, vim, helpers) { + cm.setCursor(0, 0); + CodeMirror.Vim.setOption('pcre', true); + helpers.doEx('%s/f.o/namespace.&/'); + eq("namespace.foo\nnamespace.fzo", cm.getValue()); + }, { value: 'foo\nfzo' }); +testVim("ex_escaped_ampersand_should_not_substitute_pcre", function(cm, vim, helpers) { + cm.setCursor(0, 0); + CodeMirror.Vim.setOption('pcre', true); + helpers.doEx('%s/foo/namespace.\\&/'); + eq("namespace.&", cm.getValue()); + }, { value: 'foo' }); +testSubstitute('ex_substitute_backslashslash_regex', { + value: 'one\\two \n three\\four', + expectedValue: 'one,two \n three,four', + expr: '%s/\\\\/,'}); +testSubstitute('ex_substitute_slash_replacement', { + value: 'one,two \n three,four', + expectedValue: 'one/two \n three/four', + expr: '%s/,/\\/'}); +testSubstitute('ex_substitute_backslash_replacement', { + value: 'one,two \n three,four', + expectedValue: 'one\\two \n three\\four', + expr: '%s/,/\\\\/g'}); +testSubstitute('ex_substitute_multibackslash_replacement', { + value: 'one,two \n three,four', + expectedValue: 'one\\\\\\\\two \n three\\\\\\\\four', // 2*8 backslashes. + expr: '%s/,/\\\\\\\\\\\\\\\\/g'}); // 16 backslashes. +testSubstitute('ex_substitute_dollar_match', { + value: 'one,two \n three,four', + expectedValue: 'one,two ,\n three,four', + expr: '%s/$/,/g'}); +testSubstitute('ex_substitute_newline_match', { + value: 'one,two \n three,four', + expectedValue: 'one,two , three,four', + expr: '%s/\\n/,/g'}); +testSubstitute('ex_substitute_newline_replacement', { + value: 'one,two \n three,four', + expectedValue: 'one\ntwo \n three\nfour', + expr: '%s/,/\\n/g'}); +testSubstitute('ex_substitute_braces_word', { + value: 'ababab abb ab{2}', + expectedValue: 'ab abb ab{2}', + expr: '%s/(ab){2}//g', + noPcreExpr: '%s/\\(ab\\)\\{2\\}//g'}); +testSubstitute('ex_substitute_braces_range', { + value: 'a aa aaa aaaa', + expectedValue: 'a a', + expr: '%s/a{2,3}//g', + noPcreExpr: '%s/a\\{2,3\\}//g'}); +testSubstitute('ex_substitute_braces_literal', { + value: 'ababab abb ab{2}', + expectedValue: 'ababab abb ', + expr: '%s/ab\\{2\\}//g', + noPcreExpr: '%s/ab{2}//g'}); +testSubstitute('ex_substitute_braces_char', { + value: 'ababab abb ab{2}', + expectedValue: 'ababab ab{2}', + expr: '%s/ab{2}//g', + noPcreExpr: '%s/ab\\{2\\}//g'}); +testSubstitute('ex_substitute_braces_no_escape', { + value: 'ababab abb ab{2}', + expectedValue: 'ababab ab{2}', + expr: '%s/ab{2}//g', + noPcreExpr: '%s/ab\\{2}//g'}); +testSubstitute('ex_substitute_count', { + value: '1\n2\n3\n4', + expectedValue: '1\n0\n0\n4', + expr: 's/\\d/0/i 2'}); +testSubstitute('ex_substitute_count_with_range', { + value: '1\n2\n3\n4', + expectedValue: '1\n2\n0\n0', + expr: '1,3s/\\d/0/ 3'}); +testSubstitute('ex_substitute_not_global', { + value: 'aaa\nbaa\ncaa', + expectedValue: 'xaa\nbxa\ncxa', + expr: '%s/a/x/'}); +function testSubstituteConfirm(name, command, initialValue, expectedValue, keys, finalPos) { + testVim(name, function(cm, vim, helpers) { + var savedOpenDialog = cm.openDialog; + var savedKeyName = CodeMirror.keyName; + var onKeyDown; + var recordedCallback; + var closed = true; // Start out closed, set false on second openDialog. + function close() { + closed = true; + } + // First openDialog should save callback. + cm.openDialog = function(template, callback, options) { + recordedCallback = callback; + } + // Do first openDialog. + helpers.doKeys(':'); + // Second openDialog should save keyDown handler. + cm.openDialog = function(template, callback, options) { + onKeyDown = options.onKeyDown; + closed = false; + }; + // Return the command to Vim and trigger second openDialog. + recordedCallback(command); + // The event should really use keyCode, but here just mock it out and use + // key and replace keyName to just return key. + CodeMirror.keyName = function (e) { return e.key; } + keys = keys.toUpperCase(); + for (var i = 0; i < keys.length; i++) { + is(!closed); + onKeyDown({ key: keys.charAt(i) }, '', close); + } + try { + eq(expectedValue, cm.getValue()); + helpers.assertCursorAt(finalPos); + is(closed); + } catch(e) { + throw e + } finally { + // Restore overridden functions. + CodeMirror.keyName = savedKeyName; + cm.openDialog = savedOpenDialog; + } + }, { value: initialValue }); +} +testSubstituteConfirm('ex_substitute_confirm_emptydoc', + '%s/x/b/c', '', '', '', makeCursor(0, 0)); +testSubstituteConfirm('ex_substitute_confirm_nomatch', + '%s/x/b/c', 'ba a\nbab', 'ba a\nbab', '', makeCursor(0, 0)); +testSubstituteConfirm('ex_substitute_confirm_accept', + '%s/a/b/cg', 'ba a\nbab', 'bb b\nbbb', 'yyy', makeCursor(1, 1)); +testSubstituteConfirm('ex_substitute_confirm_random_keys', + '%s/a/b/cg', 'ba a\nbab', 'bb b\nbbb', 'ysdkywerty', makeCursor(1, 1)); +testSubstituteConfirm('ex_substitute_confirm_some', + '%s/a/b/cg', 'ba a\nbab', 'bb a\nbbb', 'yny', makeCursor(1, 1)); +testSubstituteConfirm('ex_substitute_confirm_all', + '%s/a/b/cg', 'ba a\nbab', 'bb b\nbbb', 'a', makeCursor(1, 1)); +testSubstituteConfirm('ex_substitute_confirm_accept_then_all', + '%s/a/b/cg', 'ba a\nbab', 'bb b\nbbb', 'ya', makeCursor(1, 1)); +testSubstituteConfirm('ex_substitute_confirm_quit', + '%s/a/b/cg', 'ba a\nbab', 'bb a\nbab', 'yq', makeCursor(0, 3)); +testSubstituteConfirm('ex_substitute_confirm_last', + '%s/a/b/cg', 'ba a\nbab', 'bb b\nbab', 'yl', makeCursor(0, 3)); +testSubstituteConfirm('ex_substitute_confirm_oneline', + '1s/a/b/cg', 'ba a\nbab', 'bb b\nbab', 'yl', makeCursor(0, 3)); +testSubstituteConfirm('ex_substitute_confirm_range_accept', + '1,2s/a/b/cg', 'aa\na \na\na', 'bb\nb \na\na', 'yyy', makeCursor(1, 0)); +testSubstituteConfirm('ex_substitute_confirm_range_some', + '1,3s/a/b/cg', 'aa\na \na\na', 'ba\nb \nb\na', 'ynyy', makeCursor(2, 0)); +testSubstituteConfirm('ex_substitute_confirm_range_all', + '1,3s/a/b/cg', 'aa\na \na\na', 'bb\nb \nb\na', 'a', makeCursor(2, 0)); +testSubstituteConfirm('ex_substitute_confirm_range_last', + '1,3s/a/b/cg', 'aa\na \na\na', 'bb\nb \na\na', 'yyl', makeCursor(1, 0)); +//:noh should clear highlighting of search-results but allow to resume search through n +testVim('ex_noh_clearSearchHighlight', function(cm, vim, helpers) { + cm.openDialog = helpers.fakeOpenDialog('match'); + helpers.doKeys('?'); + helpers.doEx('noh'); + eq(vim.searchState_.getOverlay(),null,'match-highlighting wasn\'t cleared'); + helpers.doKeys('n'); + helpers.assertCursorAt(0, 11,'can\'t resume search after clearing highlighting'); +}, { value: 'match nope match \n nope Match' }); +testVim('ex_yank', function (cm, vim, helpers) { + var curStart = makeCursor(3, 0); + cm.setCursor(curStart); + helpers.doEx('y'); + var register = helpers.getRegisterController().getRegister(); + var line = cm.getLine(3); + eq(line + '\n', register.toString()); +}); +testVim('set_boolean', function(cm, vim, helpers) { + CodeMirror.Vim.defineOption('testoption', true, 'boolean'); + // Test default value is set. + is(CodeMirror.Vim.getOption('testoption')); + // Test fail to set to non-boolean + var result = CodeMirror.Vim.setOption('testoption', '5'); + is(result instanceof Error); + // Test setOption + CodeMirror.Vim.setOption('testoption', false); + is(!CodeMirror.Vim.getOption('testoption')); +}); +testVim('ex_set_boolean', function(cm, vim, helpers) { + CodeMirror.Vim.defineOption('testoption', true, 'boolean'); + // Test default value is set. + is(CodeMirror.Vim.getOption('testoption')); + is(!cm.state.currentNotificationClose); + // Test fail to set to non-boolean + helpers.doEx('set testoption=22'); + is(cm.state.currentNotificationClose); + // Test setOption + helpers.doEx('set notestoption'); + is(!CodeMirror.Vim.getOption('testoption')); +}); +testVim('set_string', function(cm, vim, helpers) { + CodeMirror.Vim.defineOption('testoption', 'a', 'string'); + // Test default value is set. + eq('a', CodeMirror.Vim.getOption('testoption')); + // Test no fail to set non-string. + var result = CodeMirror.Vim.setOption('testoption', true); + is(!result); + // Test fail to set 'notestoption' + result = CodeMirror.Vim.setOption('notestoption', 'b'); + is(result instanceof Error); + // Test setOption + CodeMirror.Vim.setOption('testoption', 'c'); + eq('c', CodeMirror.Vim.getOption('testoption')); +}); +testVim('ex_set_string', function(cm, vim, helpers) { + CodeMirror.Vim.defineOption('testopt', 'a', 'string'); + // Test default value is set. + eq('a', CodeMirror.Vim.getOption('testopt')); + // Test fail to set 'notestopt' + is(!cm.state.currentNotificationClose); + helpers.doEx('set notestopt=b'); + is(cm.state.currentNotificationClose); + // Test setOption + helpers.doEx('set testopt=c') + eq('c', CodeMirror.Vim.getOption('testopt')); + helpers.doEx('set testopt=c') + eq('c', CodeMirror.Vim.getOption('testopt', cm)); //local || global + eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'})); // local + eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'})); // global + eq('c', CodeMirror.Vim.getOption('testopt')); // global + // Test setOption global + helpers.doEx('setg testopt=d') + eq('c', CodeMirror.Vim.getOption('testopt', cm)); + eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'})); + eq('d', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'})); + eq('d', CodeMirror.Vim.getOption('testopt')); + // Test setOption local + helpers.doEx('setl testopt=e') + eq('e', CodeMirror.Vim.getOption('testopt', cm)); + eq('e', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'})); + eq('d', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'})); + eq('d', CodeMirror.Vim.getOption('testopt')); +}); +testVim('ex_set_callback', function(cm, vim, helpers) { + var global; + + function cb(val, cm, cfg) { + if (val === undefined) { + // Getter + if (cm) { + return cm._local; + } else { + return global; + } + } else { + // Setter + if (cm) { + cm._local = val; + } else { + global = val; + } + } + } + + CodeMirror.Vim.defineOption('testopt', 'a', 'string', cb); + // Test default value is set. + eq('a', CodeMirror.Vim.getOption('testopt')); + // Test fail to set 'notestopt' + is(!cm.state.currentNotificationClose); + helpers.doEx('set notestopt=b'); + is(cm.state.currentNotificationClose); + // Test setOption (Identical to the string tests, but via callback instead) + helpers.doEx('set testopt=c') + eq('c', CodeMirror.Vim.getOption('testopt', cm)); //local || global + eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'})); // local + eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'})); // global + eq('c', CodeMirror.Vim.getOption('testopt')); // global + // Test setOption global + helpers.doEx('setg testopt=d') + eq('c', CodeMirror.Vim.getOption('testopt', cm)); + eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'})); + eq('d', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'})); + eq('d', CodeMirror.Vim.getOption('testopt')); + // Test setOption local + helpers.doEx('setl testopt=e') + eq('e', CodeMirror.Vim.getOption('testopt', cm)); + eq('e', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'})); + eq('d', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'})); + eq('d', CodeMirror.Vim.getOption('testopt')); +}) +testVim('ex_set_filetype', function(cm, vim, helpers) { + CodeMirror.defineMode('test_mode', function() { + return {token: function(stream) { + stream.match(/^\s+|^\S+/); + }}; + }); + CodeMirror.defineMode('test_mode_2', function() { + return {token: function(stream) { + stream.match(/^\s+|^\S+/); + }}; + }); + // Test mode is set. + helpers.doEx('set filetype=test_mode'); + eq('test_mode', cm.getMode().name); + // Test 'ft' alias also sets mode. + helpers.doEx('set ft=test_mode_2'); + eq('test_mode_2', cm.getMode().name); +}); +testVim('ex_set_filetype_null', function(cm, vim, helpers) { + CodeMirror.defineMode('test_mode', function() { + return {token: function(stream) { + stream.match(/^\s+|^\S+/); + }}; + }); + cm.setOption('mode', 'test_mode'); + // Test mode is set to null. + helpers.doEx('set filetype='); + eq('null', cm.getMode().name); +}); + +testVim('mapclear', function(cm, vim, helpers) { + CodeMirror.Vim.map('w', 'l'); + cm.setCursor(0, 0); + helpers.assertCursorAt(0, 0); + helpers.doKeys('w'); + helpers.assertCursorAt(0, 1); + CodeMirror.Vim.mapclear('visual'); + helpers.doKeys('v', 'w', 'v'); + helpers.assertCursorAt(0, 4); + helpers.doKeys('w'); + helpers.assertCursorAt(0, 5); + CodeMirror.Vim.mapclear(); +}, { value: 'abc abc' }); +testVim('mapclear_context', function(cm, vim, helpers) { + CodeMirror.Vim.map('w', 'l', 'normal'); + cm.setCursor(0, 0); + helpers.assertCursorAt(0, 0); + helpers.doKeys('w'); + helpers.assertCursorAt(0, 1); + CodeMirror.Vim.mapclear('normal'); + helpers.doKeys('w'); + helpers.assertCursorAt(0, 4); + CodeMirror.Vim.mapclear(); +}, { value: 'abc abc' }); + +testVim('ex_map_key2key', function(cm, vim, helpers) { + helpers.doEx('map a x'); + helpers.doKeys('a'); + helpers.assertCursorAt(0, 0); + eq('bc', cm.getValue()); + CodeMirror.Vim.mapclear(); +}, { value: 'abc' }); +testVim('ex_unmap_key2key', function(cm, vim, helpers) { + helpers.doEx('map a x'); + helpers.doEx('unmap a'); + helpers.doKeys('a'); + eq('vim-insert', cm.getOption('keyMap')); + CodeMirror.Vim.mapclear(); +}, { value: 'abc' }); +testVim('ex_unmap_key2key_does_not_remove_default', function(cm, vim, helpers) { + expectFail(function() { + helpers.doEx('unmap a'); + }); + helpers.doKeys('a'); + eq('vim-insert', cm.getOption('keyMap')); + CodeMirror.Vim.mapclear(); +}, { value: 'abc' }); +testVim('ex_map_key2key_to_colon', function(cm, vim, helpers) { + helpers.doEx('map ; :'); + var dialogOpened = false; + cm.openDialog = function() { + dialogOpened = true; + } + helpers.doKeys(';'); + eq(dialogOpened, true); + CodeMirror.Vim.mapclear(); +}); +testVim('ex_map_ex2key:', function(cm, vim, helpers) { + helpers.doEx('map :del x'); + helpers.doEx('del'); + helpers.assertCursorAt(0, 0); + eq('bc', cm.getValue()); + CodeMirror.Vim.mapclear(); +}, { value: 'abc' }); +testVim('ex_map_ex2ex', function(cm, vim, helpers) { + helpers.doEx('map :del :w'); + var tmp = CodeMirror.commands.save; + var written = false; + var actualCm; + CodeMirror.commands.save = function(cm) { + written = true; + actualCm = cm; + }; + helpers.doEx('del'); + CodeMirror.commands.save = tmp; + eq(written, true); + eq(actualCm, cm); + CodeMirror.Vim.mapclear(); +}); +testVim('ex_map_key2ex', function(cm, vim, helpers) { + helpers.doEx('map a :w'); + var tmp = CodeMirror.commands.save; + var written = false; + var actualCm; + CodeMirror.commands.save = function(cm) { + written = true; + actualCm = cm; + }; + helpers.doKeys('a'); + CodeMirror.commands.save = tmp; + eq(written, true); + eq(actualCm, cm); + CodeMirror.Vim.mapclear(); +}); +testVim('ex_map_key2key_visual_api', function(cm, vim, helpers) { + CodeMirror.Vim.map('b', ':w', 'visual'); + var tmp = CodeMirror.commands.save; + var written = false; + var actualCm; + CodeMirror.commands.save = function(cm) { + written = true; + actualCm = cm; + }; + // Mapping should not work in normal mode. + helpers.doKeys('b'); + eq(written, false); + // Mapping should work in visual mode. + helpers.doKeys('v', 'b'); + eq(written, true); + eq(actualCm, cm); + + CodeMirror.commands.save = tmp; + CodeMirror.Vim.mapclear(); +}); +testVim('ex_imap', function(cm, vim, helpers) { + CodeMirror.Vim.map('jk', '<Esc>', 'insert'); + helpers.doKeys('i'); + is(vim.insertMode); + helpers.doKeys('j', 'k'); + is(!vim.insertMode); + cm.setCursor(0, 1); + CodeMirror.Vim.map('jj', '<Esc>', 'insert'); + helpers.doKeys('<C-v>', '2', 'j', 'l', 'c'); + helpers.doKeys('f', 'o'); + eq('1fo4\n5fo8\nafodefg', cm.getValue()); + helpers.doKeys('j', 'j'); + cm.setCursor(0, 0); + helpers.doKeys('.'); + eq('foo4\nfoo8\nfoodefg', cm.getValue()); + CodeMirror.Vim.mapclear(); +}, { value: '1234\n5678\nabcdefg' }); +testVim('ex_unmap_api', function(cm, vim, helpers) { + CodeMirror.Vim.map('<Alt-X>', 'gg', 'normal'); + is(CodeMirror.Vim.handleKey(cm, "<Alt-X>", "normal"), "Alt-X key is mapped"); + CodeMirror.Vim.unmap("<Alt-X>", "normal"); + is(!CodeMirror.Vim.handleKey(cm, "<Alt-X>", "normal"), "Alt-X key is unmapped"); + CodeMirror.Vim.mapclear(); +}); +// Testing registration of functions as ex-commands and mapping to <Key>-keys +testVim('ex_api_test', function(cm, vim, helpers) { + var res=false; + var val='from'; + CodeMirror.Vim.defineEx('extest','ext',function(cm,params){ + if(params.args)val=params.args[0]; + else res=true; + }); + helpers.doEx(':ext to'); + eq(val,'to','Defining ex-command failed'); + CodeMirror.Vim.map('<C-CR><Space>',':ext'); + helpers.doKeys('<C-CR>','<Space>'); + is(res,'Mapping to key failed'); + CodeMirror.Vim.mapclear(); +}); +// For now, this test needs to be last because it messes up : for future tests. +testVim('ex_map_key2key_from_colon', function(cm, vim, helpers) { + helpers.doEx('map : x'); + helpers.doKeys(':'); + helpers.assertCursorAt(0, 0); + eq('bc', cm.getValue()); + CodeMirror.Vim.mapclear(); +}, { value: 'abc' }); + +testVim('noremap', function(cm, vim, helpers) { + CodeMirror.Vim.noremap(';', 'l'); + cm.setCursor(0, 0); + eq('wOrd1', cm.getValue()); + // Mapping should work in normal mode. + helpers.doKeys(';', 'r', '1'); + eq('w1rd1', cm.getValue()); + // Mapping will not work in insert mode because of no current fallback + // keyToKey mapping support. + helpers.doKeys('i', ';', '<Esc>'); + eq('w;1rd1', cm.getValue()); + // unmap all mappings + CodeMirror.Vim.mapclear(); +}, { value: 'wOrd1' }); +testVim('noremap_swap', function(cm, vim, helpers) { + CodeMirror.Vim.noremap('i', 'a', 'normal'); + CodeMirror.Vim.noremap('a', 'i', 'normal'); + cm.setCursor(0, 0); + // 'a' should act like 'i'. + helpers.doKeys('a'); + eqCursorPos(Pos(0, 0), cm.getCursor()); + // ...and 'i' should act like 'a'. + helpers.doKeys('<Esc>', 'i'); + eqCursorPos(Pos(0, 1), cm.getCursor()); + // unmap all mappings + CodeMirror.Vim.mapclear(); +}, { value: 'foo' }); +testVim('noremap_map_interaction', function(cm, vim, helpers) { + // noremap should clobber map + CodeMirror.Vim.map(';', 'l'); + CodeMirror.Vim.noremap(';', 'l'); + CodeMirror.Vim.map('l', 'j'); + cm.setCursor(0, 0); + helpers.doKeys(';'); + eqCursorPos(Pos(0, 1), cm.getCursor()); + helpers.doKeys('l'); + eqCursorPos(Pos(1, 1), cm.getCursor()); + // map should be able to point to a noremap + CodeMirror.Vim.map('m', ';'); + helpers.doKeys('m'); + eqCursorPos(Pos(1, 2), cm.getCursor()); + // unmap all mappings + CodeMirror.Vim.mapclear(); +}, { value: 'wOrd1\nwOrd2' }); +testVim('noremap_map_interaction2', function(cm, vim, helpers) { + // map should point to the most recent noremap + CodeMirror.Vim.noremap(';', 'l'); + CodeMirror.Vim.map('m', ';'); + CodeMirror.Vim.noremap(';', 'h'); + cm.setCursor(0, 0); + helpers.doKeys('l'); + eqCursorPos(Pos(0, 1), cm.getCursor()); + helpers.doKeys('m'); + eqCursorPos(Pos(0, 0), cm.getCursor()); + // unmap all mappings + CodeMirror.Vim.mapclear(); +}, { value: 'wOrd1\nwOrd2' }); + +// Test event handlers +testVim('beforeSelectionChange', function(cm, vim, helpers) { + cm.setCursor(0, 100); + eqCursorPos(cm.getCursor('head'), cm.getCursor('anchor')); +}, { value: 'abc' }); + +testVim('increment_binary', function(cm, vim, helpers) { + cm.setCursor(0, 4); + helpers.doKeys('<C-a>'); + eq('0b001', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0b010', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0b001', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0b000', cm.getValue()); + cm.setCursor(0, 0); + helpers.doKeys('<C-a>'); + eq('0b001', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0b010', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0b001', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0b000', cm.getValue()); +}, { value: '0b000' }); + +testVim('increment_octal', function(cm, vim, helpers) { + cm.setCursor(0, 2); + helpers.doKeys('<C-a>'); + eq('001', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('002', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('003', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('004', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('005', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('006', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('007', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('010', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('007', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('006', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('005', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('004', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('003', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('002', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('001', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('000', cm.getValue()); + cm.setCursor(0, 0); + helpers.doKeys('<C-a>'); + eq('001', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('002', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('001', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('000', cm.getValue()); +}, { value: '000' }); + +testVim('increment_decimal', function(cm, vim, helpers) { + cm.setCursor(0, 2); + helpers.doKeys('<C-a>'); + eq('101', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('102', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('103', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('104', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('105', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('106', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('107', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('108', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('109', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('110', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('109', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('108', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('107', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('106', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('105', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('104', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('103', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('102', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('101', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('100', cm.getValue()); + cm.setCursor(0, 0); + helpers.doKeys('<C-a>'); + eq('101', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('102', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('101', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('100', cm.getValue()); +}, { value: '100' }); + +testVim('increment_decimal_single_zero', function(cm, vim, helpers) { + helpers.doKeys('<C-a>'); + eq('1', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('2', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('3', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('4', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('5', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('6', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('7', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('8', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('9', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('10', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('9', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('8', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('7', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('6', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('5', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('4', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('3', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('2', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('1', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0', cm.getValue()); + cm.setCursor(0, 0); + helpers.doKeys('<C-a>'); + eq('1', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('2', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('1', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0', cm.getValue()); +}, { value: '0' }); + +testVim('increment_hexadecimal', function(cm, vim, helpers) { + cm.setCursor(0, 2); + helpers.doKeys('<C-a>'); + eq('0x1', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0x2', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0x3', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0x4', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0x5', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0x6', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0x7', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0x8', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0x9', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0xa', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0xb', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0xc', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0xd', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0xe', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0xf', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0x10', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x0f', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x0e', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x0d', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x0c', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x0b', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x0a', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x09', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x08', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x07', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x06', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x05', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x04', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x03', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x02', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x01', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x00', cm.getValue()); + cm.setCursor(0, 0); + helpers.doKeys('<C-a>'); + eq('0x01', cm.getValue()); + helpers.doKeys('<C-a>'); + eq('0x02', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x01', cm.getValue()); + helpers.doKeys('<C-x>'); + eq('0x00', cm.getValue()); +}, { value: '0x0' }); diff --git a/devtools/client/shared/sourceeditor/test/codemirror/vimemacs.html b/devtools/client/shared/sourceeditor/test/codemirror/vimemacs.html new file mode 100644 index 0000000000..5d56b57b11 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/codemirror/vimemacs.html @@ -0,0 +1,215 @@ +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <title>CodeMirror: VIM/Emacs tests</title> + <link rel="stylesheet" href="chrome://devtools/content/shared/sourceeditor/codemirror/lib/codemirror.css"> + <link rel="stylesheet" href="cm_mode_test.css"> + <!--<link rel="stylesheet" href="../doc/docs.css">--> + + <script src="chrome://devtools/content/shared/sourceeditor/codemirror/codemirror.bundle.js"></script> + <script src="chrome://devtools/content/shared/sourceeditor/codemirror/keymap/emacs.js"></script> + <script src="chrome://devtools/content/shared/sourceeditor/codemirror/keymap/sublime.js"></script> + <script src="chrome://devtools/content/shared/sourceeditor/codemirror/keymap/vim.js"></script> + + <style type="text/css"> + .ok {color: #090;} + .fail {color: #e00;} + .error {color: #c90;} + .done {font-weight: bold;} + #progress { + background: #45d; + color: white; + text-shadow: 0 0 1px #45d, 0 0 2px #45d, 0 0 3px #45d; + font-weight: bold; + white-space: pre; + } + #testground { + visibility: hidden; + } + #testground.offscreen { + visibility: visible; + position: absolute; + left: -10000px; + top: -10000px; + } + .CodeMirror { border: 1px solid black; } + </style> + </head> + <body> + <h1>CodeMirror: VIM/Emacs tests</h1> + + <p>A limited set of programmatic sanity tests for CodeMirror.</p> + + <div style="border: 1px solid black; padding: 1px; max-width: 700px;"> + <div style="width: 0px;" id=progress><div style="padding: 3px;">Ran <span id="progress_ran">0</span><span id="progress_total"> of 0</span> tests</div></div> + </div> + <p id=status>Please enable JavaScript...</p> + <div id=output></div> + + <div id=testground></div> + + <script src="driver.js"></script> + <script src="sublime_test.js"></script> + <script src="vim_test.js"></script> + <script src="emacs_test.js"></script> + + <!-- Basic tests are in codemirror.html + <script src="cm_driver.js"></script> + <script src="cm_test.js"></script> + <script src="cm_comment_test.js"></script> + <script src="cm_doc_test.js"></script> + <script src="cm_driver.js"></script> + <script src="cm_emacs_test.js"></script> + <script src="cm_mode_test.js"></script> + <script src="cm_mode_javascript_test.js"></script> + <script src="cm_multi_test.js"></script> + <script src="cm_search_test.js"></script> + --> + + <!-- These modes/addons are not used by Editor + <script src="doc_test.js"></script> + <script src="../mode/css/css.js"></script> + <script src="../mode/css/test.js"></script> + <script src="../mode/css/scss_test.js"></script> + <script src="../mode/xml/xml.js"></script> + <script src="../mode/htmlmixed/htmlmixed.js"></script> + <script src="../mode/ruby/ruby.js"></script> + <script src="../mode/haml/haml.js"></script> + <script src="../mode/haml/test.js"></script> + <script src="../mode/markdown/markdown.js"></script> + <script src="../mode/markdown/test.js"></script> + <script src="../mode/gfm/gfm.js"></script> + <script src="../mode/gfm/test.js"></script> + <script src="../mode/stex/stex.js"></script> + <script src="../mode/stex/test.js"></script> + <script src="../mode/xquery/xquery.js"></script> + <script src="../mode/xquery/test.js"></script> + <script src="../addon/mode/multiplex_test.js"></script>--> + + <script> + window.onload = runHarness; + CodeMirror.on(window, 'hashchange', runHarness); + + function esc(str) { + return str.replace(/[<&]/, function(ch) { return ch == "<" ? "<" : "&"; }); + } + + var output = document.getElementById("output"), + progress = document.getElementById("progress"), + progressRan = document.getElementById("progress_ran").childNodes[0], + progressTotal = document.getElementById("progress_total").childNodes[0]; + + var count = 0, + failed = 0, + skipped = 0, + bad = "", + running = false, // Flag that states tests are running + quit = false, // Flag to quit tests ASAP + verbose = false, // Adds message for *every* test to output + phantom = false, + Pos = CodeMirror.Pos; // Required for VIM tests + + function runHarness(){ + if (running) { + quit = true; + setStatus("Restarting tests...", '', true); + setTimeout(function(){runHarness();}, 500); + return; + } + filters = []; + verbose = false; + if (window.location.hash.substr(1)){ + var strings = window.location.hash.substr(1).split(","); + while (strings.length) { + var s = strings.shift(); + if (s === "verbose") + verbose = true; + else + filters.push(parseTestFilter(decodeURIComponent(s))); + } + } + quit = false; + running = true; + setStatus("Loading tests..."); + count = 0; + failed = 0; + skipped = 0; + bad = ""; + totalTests = countTests(); + progressTotal.nodeValue = " of " + totalTests; + progressRan.nodeValue = count; + output.innerHTML = ''; + document.getElementById("testground").innerHTML = "<form>" + + "<textarea id=\"code\" name=\"code\"></textarea>" + + "<input type=submit value=ok name=submit>" + + "</form>"; + runTests(displayTest); + } + + function setStatus(message, className, force){ + if (quit && !force) return; + if (!message) throw("must provide message"); + var status = document.getElementById("status").childNodes[0]; + status.nodeValue = message; + status.parentNode.className = className; + } + function addOutput(name, className, code){ + var newOutput = document.createElement("dl"); + var newTitle = document.createElement("dt"); + newTitle.className = className; + newTitle.appendChild(document.createTextNode(name)); + newOutput.appendChild(newTitle); + var newMessage = document.createElement("dd"); + newMessage.innerHTML = code; + newOutput.appendChild(newTitle); + newOutput.appendChild(newMessage); + output.appendChild(newOutput); + } + function displayTest(type, name, customMessage) { + var message = "???"; + if (type != "done" && type != "skipped") ++count; + progress.style.width = (count * (progress.parentNode.clientWidth - 2) / totalTests) + "px"; + progressRan.nodeValue = count; + if (type == "ok") { + message = "Test '" + name + "' succeeded"; + if (!verbose) customMessage = false; + } else if (type == "skipped") { + message = "Test '" + name + "' skipped"; + ++skipped; + if (!verbose) customMessage = false; + } else if (type == "expected") { + message = "Test '" + name + "' failed as expected"; + if (!verbose) customMessage = false; + } else if (type == "error" || type == "fail") { + ++failed; + message = "Test '" + name + "' failed"; + } else if (type == "done") { + if (failed) { + type += " fail"; + message = failed + " failure" + (failed > 1 ? "s" : ""); + } else if (count < totalTests) { + failed = totalTests - count; + type += " fail"; + message = failed + " failure" + (failed > 1 ? "s" : ""); + } else { + type += " ok"; + message = "All passed"; + if (skipped) { + message += " (" + skipped + " skipped)"; + } + } + progressTotal.nodeValue = ''; + customMessage = true; // Hack to avoid adding to output + } + if (window.mozilla_setStatus) + mozilla_setStatus(message, type, customMessage); + if (verbose && !customMessage) customMessage = message; + setStatus(message, type); + if (customMessage && customMessage.length > 0) { + addOutput(name, type, customMessage); + } + } + </script> + </body> +</html> diff --git a/devtools/client/shared/sourceeditor/test/css_autocompletion_tests.json b/devtools/client/shared/sourceeditor/test/css_autocompletion_tests.json new file mode 100644 index 0000000000..a8f09e0741 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/css_autocompletion_tests.json @@ -0,0 +1,106 @@ +{ + "description": [ + "Test states to be tested for css state machine in css-autocompleter.js file.", + "Test cases are of the following format:", + "[", + " [", + " line, # The line location of the cursor", + " ch # The column location of the cursor", + " ],", + " suggestions # Array of expected results", + "]" + ], + "tests": [ + [[0, 10], []], + [ + [4, 7], + [".devtools-menulist", ".devtools-toolbarbutton"] + ], + [ + [5, 8], + [ + "-moz-animation", + "-moz-animation-delay", + "-moz-animation-direction", + "-moz-animation-duration", + "-moz-animation-fill-mode", + "-moz-animation-iteration-count", + "-moz-animation-name", + "-moz-animation-play-state", + "-moz-animation-timing-function", + "-moz-appearance" + ] + ], + [ + [12, 20], + ["none", "number-input"] + ], + [[12, 22], ["none"]], + [ + [17, 22], + ["hsl", "hsla"] + ], + [ + [19, 10], + [ + "background", + "background-attachment", + "background-blend-mode", + "background-clip", + "background-color", + "background-image", + "background-origin", + "background-position", + "background-position-x", + "background-position-y", + "background-repeat", + "background-size" + ] + ], + [ + [21, 9], + ["auto", "inherit", "initial", "revert", "revert-layer", "unset"] + ], + [ + [25, 26], + [ + ".devtools-toolbarbutton > tab", + ".devtools-toolbarbutton > hbox", + ".devtools-toolbarbutton > .toolbarbutton-menubutton-button" + ] + ], + [ + [25, 31], + [".devtools-toolbarbutton > hbox.toolbarbutton-menubutton-button"] + ], + [ + [29, 20], + [".devtools-menulist:after", ".devtools-menulist:active"] + ], + [ + [30, 10], + [ + "#devtools-anotherone", + "#devtools-itjustgoeson", + "#devtools-menu", + "#devtools-okstopitnow", + "#devtools-toolbarbutton", + "#devtools-yetagain" + ] + ], + [[39, 39], [".devtools-toolbarbutton:not([label]) > tab"]], + [ + [43, 51], + [ + ".devtools-toolbarbutton:not([checked=true]):hover:after", + ".devtools-toolbarbutton:not([checked=true]):hover:active" + ] + ], + [[58, 36], ["!important;"]], + [ + [73, 42], + [":lang(", ":last-of-type", ":link", ":last-child"] + ], + [[77, 25], [".visible"]] + ] +} diff --git a/devtools/client/shared/sourceeditor/test/css_statemachine_testcases.css b/devtools/client/shared/sourceeditor/test/css_statemachine_testcases.css new file mode 100644 index 0000000000..d2c51a8413 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/css_statemachine_testcases.css @@ -0,0 +1,121 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +.devtools-toolbar { + -moz-appearance: none; + padding:4px 3px;border-bottom-width: 1px; + border-bottom-style: solid; +} + +#devtools-menu.devtools-menulist, +.devtools-toolbarbutton#devtools-menu { + -moz-appearance: none; + align-items: center; + min-width: 78px; + min-height: 22px; + text-shadow: 0 -1px 0 hsla(210,8%,5%,.45); + border: 1px solid hsla(210,8%,5%,.45); + border-radius: 3px; + background: linear-gradient(hsla(212,7%,57%,.35), hsla(212,7%,57%,.1)) padding-box; + box-shadow: 0 1px 0 hsla(210,16%,76%,.15) inset, 0 0 0 1px hsla(210,16%,76%,.15) inset, 0 1px 0 hsla(210,16%,76%,.15); + margin: 0 3px; + color: inherit; +} + +.devtools-toolbarbutton > hbox.toolbarbutton-menubutton-button { + flex-direction: row; +} + +.devtools-menulist:active, +#devtools-toolbarbutton:focus { + outline: 1px dotted hsla(210,30%,85%,0.7); + outline-offset : -4px; +} + +.devtools-toolbarbutton:not([label]) { + min-width: 32px; +} + +.devtools-toolbarbutton:not([label]) > .toolbarbutton-text { + display: none; +} + +.devtools-toolbarbutton:not([checked=true]):hover:active { + border-color: hsla(210,8%,5%,.6); +} + +.devtools-menulist["open" ="true"], +.devtools-toolbarbutton["open" = true], +.devtools-toolbarbutton[checked= "true"] { + border-color: hsla(210,8%,5%,.6) !important; +} + +.devtools-toolbarbutton["checked"="true"] { + color: hsl(208,100%,60%); +} + +.devtools-toolbarbutton[checked=true]:hover { + background-color: transparent !important; +} + +.devtools-toolbarbutton[checked=true]:hover:active { + background-color: hsla(210,8%,5%,.2) !important; +} + +.devtools-toolbarbutton[type=menu-button] > .toolbarbutton-menubutton-button { + -moz-appearance: none; +} + +.devtools-sidebar-tabs > tabs > tab:first-of-type { + margin-inline-start: -3px; +} + +.devtools-sidebar-tabs > tabs > tab:not(:last-of-type) { + background-size: calc(100% - 2px) 100%, 1px 100%; +} + +.hidden-labels-box:not(.visible) > label, +.hidden-labels-box.visible ~ .hidden-labels-box > label:last-child { + display: none; +} + +/* Maximize the size of the viewport when the window is small */ +@media (max-width: 800px) { + .category-name { + display: none; + } +} + +@media all and (min-width: 300px) { + #error-box { + max-width: 50%; + margin: 0 auto; + background-image: url('chrome://global/skin/icons/information-32.png'); + min-height: 36px; + padding-inline-start: 38px; + } + + button { + width: auto !important; + min-width: 150px; + } + + @keyframes downloadsIndicatorNotificationFinish { + from { opacity: 0; transform: scale(1); } + 20% { + opacity: .65; + animation-timing-function: ease-in; + } to { opacity: 0; + transform: scale(8); } + } +} + +@keyframes smooth { + from { opacity: 0; transform: scale(1); } + 20% { opacity: .65; animation-timing-function: ease-in; } + to { + opacity : 0; + transform: scale(8); + } +} diff --git a/devtools/client/shared/sourceeditor/test/css_statemachine_tests.json b/devtools/client/shared/sourceeditor/test/css_statemachine_tests.json new file mode 100644 index 0000000000..642c1107ae --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/css_statemachine_tests.json @@ -0,0 +1,319 @@ +{ + "description": [ + "Test states to be tested for css state machine in css-autocompleter.js file.", + "Test cases are of the following format:", + "[", + " [", + " line, // The line location of the cursor", + " ch // The column locaiton of the cursor", + " ],", + " [", + " state, // one of CSS_STATES", + " selectorState, // one of SELECTOR_STATES", + " completing, // what is being completed", + " propertyName, // what property is being completed in case of value state", + " // or the current selector that is being completed", + " ]", + "]" + ], + "tests": [ + [ + [0, 10], + ["null", "", "", ""] + ], + [ + [4, 3], + ["selector", "class", "de", ".de"] + ], + [ + [5, 8], + ["property", "null", "-moz-a"] + ], + [ + [5, 21], + ["value", "null", "no", "-moz-appearance"] + ], + [ + [6, 18], + ["property", "null", "padding"] + ], + [ + [6, 24], + ["value", "null", "3", "padding"] + ], + [ + [6, 29], + ["property", "null", "bo"] + ], + [ + [6, 50], + ["value", "null", "1p", "border-bottom-width"] + ], + [ + [7, 24], + ["value", "null", "s", "border-bottom-style"] + ], + [ + [9, 0], + ["null", "null", "", ""] + ], + [ + [10, 6], + ["selector", "id", "devto", "#devto"] + ], + [ + [10, 17], + ["selector", "class", "de", "#devtools-menu.de"] + ], + [ + [11, 5], + ["selector", "class", "devt", ".devt"] + ], + [ + [11, 30], + ["selector", "id", "devtoo", ".devtools-toolbarbutton#devtoo"] + ], + [ + [12, 10], + ["property", "null", "-moz-app"] + ], + [ + [16, 27], + ["value", "null", "hsl", "text-shadow"] + ], + [ + [19, 24], + ["value", "null", "linear-gra", "background"] + ], + [ + [19, 55], + ["value", "null", "hsl", "background"] + ], + [ + [19, 79], + ["value", "null", "paddin", "background"] + ], + [ + [20, 47], + ["value", "null", "ins", "box-shadow"] + ], + [ + [22, 15], + ["value", "null", "inheri", "color"] + ], + [ + [25, 26], + ["selector", "null", "", ".devtools-toolbarbutton > "] + ], + [ + [25, 28], + ["selector", "tag", "hb", ".devtools-toolbarbutton > hb"] + ], + [ + [25, 41], + [ + "selector", + "class", + "toolbarbut", + ".devtools-toolbarbutton > hbox.toolbarbut" + ] + ], + [ + [29, 21], + ["selector", "pseudo", "ac", ".devtools-menulist:ac"] + ], + [ + [30, 27], + ["selector", "pseudo", "foc", "#devtools-toolbarbutton:foc"] + ], + [ + [31, 18], + ["value", "null", "dot", "outline"] + ], + [ + [32, 25], + ["value", "null", "-4p", "outline-offset"] + ], + [ + [35, 26], + ["selector", "pseudo", "no", ".devtools-toolbarbutton:no"] + ], + [ + [35, 28], + ["selector", "null", "not", ""] + ], + [ + [35, 30], + ["selector", "attribute", "l", "[l"] + ], + [ + [39, 46], + [ + "selector", + "class", + "toolba", + ".devtools-toolbarbutton:not([label]) > .toolba" + ] + ], + [ + [43, 39], + ["selector", "value", "tr", "[checked=tr"] + ], + [ + [43, 47], + [ + "selector", + "pseudo", + "hov", + ".devtools-toolbarbutton:not([checked=true]):hov" + ] + ], + [ + [43, 53], + [ + "selector", + "pseudo", + "act", + ".devtools-toolbarbutton:not([checked=true]):hover:act" + ] + ], + [ + [47, 22], + ["selector", "attribute", "op", ".devtools-menulist[op"] + ], + [ + [47, 33], + ["selector", "value", "tr", ".devtools-menulist[open =tr"] + ], + [ + [48, 38], + ["selector", "value", "tr", ".devtools-toolbarbutton[open = tr"] + ], + [ + [49, 40], + ["selector", "value", "true", ".devtools-toolbarbutton[checked= true"] + ], + [ + [53, 34], + ["selector", "value", "=", ".devtools-toolbarbutton[checked="] + ], + [ + [58, 38], + ["value", "null", "!impor", "background-color"] + ], + [ + [61, 41], + ["selector", "pseudo", "hov", ".devtools-toolbarbutton[checked=true]:hov"] + ], + [ + [65, 47], + [ + "selector", + "class", + "to", + ".devtools-toolbarbutton[type=menu-button] > .to" + ] + ], + [ + [69, 44], + [ + "selector", + "pseudo", + "first-of", + ".devtools-sidebar-tabs > tabs > tab:first-of" + ] + ], + [ + [73, 45], + ["selector", "pseudo", "last", ":last"] + ], + [ + [77, 27], + ["selector", "class", "vis", ".vis"] + ], + [ + [78, 34], + ["selector", "class", "hidd", ".hidden-labels-box.visible ~ .hidd"] + ], + [ + [83, 5], + ["media", "null", "medi"] + ], + [ + [83, 22], + ["media", "null", "800"] + ], + [ + [84, 9], + ["selector", "class", "catego", ".catego"] + ], + [ + [89, 9], + ["media", "null", "al"] + ], + [ + [90, 6], + ["selector", "id", "err", "#err"] + ], + [ + [93, 11], + ["property", "null", "backgro"] + ], + [ + [98, 6], + ["selector", "tag", "butt", "butt"] + ], + [ + [99, 22], + ["value", "null", "!impor", "width"] + ], + [ + [103, 5], + ["keyframes", "null", "ke"] + ], + [ + [104, 7], + ["frame", "null", "fro"] + ], + [ + [104, 15], + ["property", "null", "opac"] + ], + [ + [104, 29], + ["property", "null", "transf"] + ], + [ + [104, 38], + ["value", "null", "scal", "transform"] + ], + [ + [105, 8], + ["frame", "null", ""] + ], + [ + [113, 6], + ["keyframes", "null", "keyfr"] + ], + [ + [114, 4], + ["frame", "null", "fr"] + ], + [ + [115, 3], + ["frame", "null", "2"] + ], + [ + [117, 8], + ["property", "null", "opac"] + ], + [ + [117, 16], + ["value", "null", "0", "opacity"] + ], + [ + [121, 0], + ["null", "", ""] + ] + ] +} diff --git a/devtools/client/shared/sourceeditor/test/head.js b/devtools/client/shared/sourceeditor/test/head.js new file mode 100644 index 0000000000..ec9b05b245 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/head.js @@ -0,0 +1,198 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* exported promiseWaitForFocus, setup, ch, teardown, loadHelperScript, + limit, ch, read, codemirrorSetStatus */ + +"use strict"; + +// shared-head.js handles imports, constants, and utility functions +Services.scriptloader.loadSubScript( + "chrome://mochitests/content/browser/devtools/client/shared/test/shared-head.js", + this +); + +const Editor = require("resource://devtools/client/shared/sourceeditor/editor.js"); +const { + getClientCssProperties, +} = require("resource://devtools/client/fronts/css-properties.js"); + +function promiseWaitForFocus(el) { + return new Promise(resolve => waitForFocus(resolve, el)); +} + +async function setup(additionalOpts = {}) { + try { + const opt = "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no"; + const win = Services.ww.openWindow( + null, + CHROME_URL_ROOT + "head.xhtml", + "_blank", + opt, + null + ); + const opts = { + value: "Hello.", + lineNumbers: true, + foldGutter: true, + gutters: [ + "CodeMirror-linenumbers", + "breakpoints", + "CodeMirror-foldgutter", + ], + cssProperties: getClientCssProperties(), + ...additionalOpts, + }; + + await once(win, "load"); + await promiseWaitForFocus(win); + + const box = win.document.querySelector("box"); + const editor = new Editor(opts); + await editor.appendTo(box); + + return { + ed: editor, + win, + edWin: editor.container.contentWindow.wrappedJSObject, + }; + } catch (o_O) { + ok(false, o_O.message); + return null; + } +} + +function ch(exp, act, label) { + is(exp.line, act.line, label + " (line)"); + is(exp.ch, act.ch, label + " (ch)"); +} + +function teardown(ed, win) { + ed.destroy(); + win.close(); + + while (gBrowser.tabs.length > 1) { + gBrowser.removeCurrentTab(); + } + finish(); +} + +/** + * Some tests may need to import one or more of the test helper scripts. + * A test helper script is simply a js file that contains common test code that + * is either not common-enough to be in head.js, or that is located in a + * separate directory. + * The script will be loaded synchronously and in the test's scope. + * @param {String} filePath The file path, relative to the current directory. + * Examples: + * - "helper_attributes_test_runner.js" + */ +function loadHelperScript(filePath) { + const testDir = gTestPath.substr(0, gTestPath.lastIndexOf("/")); + Services.scriptloader.loadSubScript(testDir + "/" + filePath, this); +} + +/** + * This method returns the portion of the input string `source` up to the + * [line, ch] location. + */ +function limit(source, [line, char]) { + line++; + const list = source.split("\n"); + if (list.length < line) { + return source; + } + if (line == 1) { + return list[0].slice(0, char); + } + return [...list.slice(0, line - 1), list[line - 1].slice(0, char)].join("\n"); +} + +function read(url) { + const scriptableStream = Cc[ + "@mozilla.org/scriptableinputstream;1" + ].getService(Ci.nsIScriptableInputStream); + + const channel = NetUtil.newChannel({ + uri: url, + loadUsingSystemPrincipal: true, + }); + const input = channel.open(); + scriptableStream.init(input); + + let data = ""; + while (input.available()) { + data = data.concat(scriptableStream.read(input.available())); + } + scriptableStream.close(); + input.close(); + + return data; +} + +/** + * This function is called by the CodeMirror test runner to report status + * messages from the CM tests. + * @see codemirror.html + */ +function codemirrorSetStatus(statusMsg, type, customMsg) { + switch (type) { + case "expected": + case "ok": + ok(1, statusMsg); + break; + case "error": + case "fail": + ok(0, statusMsg); + break; + default: + info(statusMsg); + break; + } + + if (customMsg && typeof customMsg == "string" && customMsg != statusMsg) { + info(customMsg); + } +} + +async function runCodeMirrorTest(uri) { + const actorURI = + "chrome://mochitests/content/browser/devtools/client/shared/sourceeditor/test/CodeMirrorTestActors.jsm"; + + const { CodeMirrorTestParent } = ChromeUtils.import(actorURI); + + ChromeUtils.registerWindowActor("CodeMirrorTest", { + parent: { + moduleURI: actorURI, + }, + child: { + moduleURI: actorURI, + events: { + DOMWindowCreated: {}, + }, + }, + }); + + const donePromise = new Promise(resolve => { + CodeMirrorTestParent.setCallback((name, data) => { + switch (name) { + case "setStatus": + const { statusMsg, type, customMsg } = data; + codemirrorSetStatus(statusMsg, type, customMsg); + break; + case "done": + resolve(!data.failed); + break; + } + }); + }); + + await addTab(uri); + const result = await donePromise; + ok(result, "CodeMirror tests all passed"); + while (gBrowser.tabs.length > 1) { + gBrowser.removeCurrentTab(); + } + + ChromeUtils.unregisterWindowActor("CodeMirrorTest"); +} diff --git a/devtools/client/shared/sourceeditor/test/head.xhtml b/devtools/client/shared/sourceeditor/test/head.xhtml new file mode 100644 index 0000000000..4e0fe90812 --- /dev/null +++ b/devtools/client/shared/sourceeditor/test/head.xhtml @@ -0,0 +1,5 @@ +<?xml version='1.0'?> +<?xml-stylesheet href='chrome://global/skin/global.css'?> +<window xmlns='http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul' title='Editor' width='600' height='500'> +<box flex='1'/> +</window> |