vim_test.js 155 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523
  1. var Pos = CodeMirror.Pos;
  2. CodeMirror.Vim.suppressErrorLogging = true;
  3. var code = '' +
  4. ' wOrd1 (#%\n' +
  5. ' word3] \n' +
  6. 'aopop pop 0 1 2 3 4\n' +
  7. ' (a) [b] {c} \n' +
  8. 'int getchar(void) {\n' +
  9. ' static char buf[BUFSIZ];\n' +
  10. ' static char *bufp = buf;\n' +
  11. ' if (n == 0) { /* buffer is empty */\n' +
  12. ' n = read(0, buf, sizeof buf);\n' +
  13. ' bufp = buf;\n' +
  14. ' }\n' +
  15. '\n' +
  16. ' return (--n >= 0) ? (unsigned char) *bufp++ : EOF;\n' +
  17. ' \n' +
  18. '}\n';
  19. var lines = (function() {
  20. lineText = code.split('\n');
  21. var ret = [];
  22. for (var i = 0; i < lineText.length; i++) {
  23. ret[i] = {
  24. line: i,
  25. length: lineText[i].length,
  26. lineText: lineText[i],
  27. textStart: /^\s*/.exec(lineText[i])[0].length
  28. };
  29. }
  30. return ret;
  31. })();
  32. var endOfDocument = makeCursor(lines.length - 1,
  33. lines[lines.length - 1].length);
  34. var wordLine = lines[0];
  35. var bigWordLine = lines[1];
  36. var charLine = lines[2];
  37. var bracesLine = lines[3];
  38. var seekBraceLine = lines[4];
  39. var word1 = {
  40. start: new Pos(wordLine.line, 1),
  41. end: new Pos(wordLine.line, 5)
  42. };
  43. var word2 = {
  44. start: new Pos(wordLine.line, word1.end.ch + 2),
  45. end: new Pos(wordLine.line, word1.end.ch + 4)
  46. };
  47. var word3 = {
  48. start: new Pos(bigWordLine.line, 1),
  49. end: new Pos(bigWordLine.line, 5)
  50. };
  51. var bigWord1 = word1;
  52. var bigWord2 = word2;
  53. var bigWord3 = {
  54. start: new Pos(bigWordLine.line, 1),
  55. end: new Pos(bigWordLine.line, 7)
  56. };
  57. var bigWord4 = {
  58. start: new Pos(bigWordLine.line, bigWord1.end.ch + 3),
  59. end: new Pos(bigWordLine.line, bigWord1.end.ch + 7)
  60. };
  61. var oChars = [ new Pos(charLine.line, 1),
  62. new Pos(charLine.line, 3),
  63. new Pos(charLine.line, 7) ];
  64. var pChars = [ new Pos(charLine.line, 2),
  65. new Pos(charLine.line, 4),
  66. new Pos(charLine.line, 6),
  67. new Pos(charLine.line, 8) ];
  68. var numChars = [ new Pos(charLine.line, 10),
  69. new Pos(charLine.line, 12),
  70. new Pos(charLine.line, 14),
  71. new Pos(charLine.line, 16),
  72. new Pos(charLine.line, 18)];
  73. var parens1 = {
  74. start: new Pos(bracesLine.line, 1),
  75. end: new Pos(bracesLine.line, 3)
  76. };
  77. var squares1 = {
  78. start: new Pos(bracesLine.line, 5),
  79. end: new Pos(bracesLine.line, 7)
  80. };
  81. var curlys1 = {
  82. start: new Pos(bracesLine.line, 9),
  83. end: new Pos(bracesLine.line, 11)
  84. };
  85. var seekOutside = {
  86. start: new Pos(seekBraceLine.line, 1),
  87. end: new Pos(seekBraceLine.line, 16)
  88. };
  89. var seekInside = {
  90. start: new Pos(seekBraceLine.line, 14),
  91. end: new Pos(seekBraceLine.line, 11)
  92. };
  93. function copyCursor(cur) {
  94. return new Pos(cur.line, cur.ch);
  95. }
  96. function forEach(arr, func) {
  97. for (var i = 0; i < arr.length; i++) {
  98. func(arr[i], i, arr);
  99. }
  100. }
  101. function testVim(name, run, opts, expectedFail) {
  102. var vimOpts = {
  103. lineNumbers: true,
  104. vimMode: true,
  105. showCursorWhenSelecting: true,
  106. value: code
  107. };
  108. for (var prop in opts) {
  109. if (opts.hasOwnProperty(prop)) {
  110. vimOpts[prop] = opts[prop];
  111. }
  112. }
  113. return test('vim_' + name, function() {
  114. var place = document.getElementById("testground");
  115. var cm = CodeMirror(place, vimOpts);
  116. var vim = CodeMirror.Vim.maybeInitVimState_(cm);
  117. function doKeysFn(cm) {
  118. return function(args) {
  119. if (args instanceof Array) {
  120. arguments = args;
  121. }
  122. for (var i = 0; i < arguments.length; i++) {
  123. var result = CodeMirror.Vim.handleKey(cm, arguments[i]);
  124. if (!result && cm.state.vim.insertMode) {
  125. cm.replaceSelections(fillArray(arguments[i], cm.listSelections().length));
  126. }
  127. }
  128. }
  129. }
  130. function doInsertModeKeysFn(cm) {
  131. return function(args) {
  132. if (args instanceof Array) { arguments = args; }
  133. function executeHandler(handler) {
  134. if (typeof handler == 'string') {
  135. CodeMirror.commands[handler](cm);
  136. } else {
  137. handler(cm);
  138. }
  139. return true;
  140. }
  141. for (var i = 0; i < arguments.length; i++) {
  142. var key = arguments[i];
  143. // Find key in keymap and handle.
  144. var handled = CodeMirror.lookupKey(key, cm.getOption('keyMap'), executeHandler, cm);
  145. // Record for insert mode.
  146. if (handled == "handled" && cm.state.vim.insertMode && arguments[i] != 'Esc') {
  147. var lastChange = CodeMirror.Vim.getVimGlobalState_().macroModeState.lastInsertModeChanges;
  148. if (lastChange && (key.indexOf('Delete') != -1 || key.indexOf('Backspace') != -1)) {
  149. lastChange.changes.push(new CodeMirror.Vim.InsertModeKey(key));
  150. }
  151. }
  152. }
  153. }
  154. }
  155. function doExFn(cm) {
  156. return function(command) {
  157. cm.openDialog = helpers.fakeOpenDialog(command);
  158. helpers.doKeys(':');
  159. }
  160. }
  161. function assertCursorAtFn(cm) {
  162. return function(line, ch) {
  163. var pos;
  164. if (ch == null && typeof line.line == 'number') {
  165. pos = line;
  166. } else {
  167. pos = makeCursor(line, ch);
  168. }
  169. eqCursorPos(cm.getCursor(), pos);
  170. }
  171. }
  172. function fakeOpenDialog(result) {
  173. return function(text, callback) {
  174. return callback(result);
  175. }
  176. }
  177. function fakeOpenNotification(matcher) {
  178. return function(text) {
  179. matcher(text);
  180. }
  181. }
  182. var helpers = {
  183. doKeys: doKeysFn(cm),
  184. // Warning: Only emulates keymap events, not character insertions. Use
  185. // replaceRange to simulate character insertions.
  186. // Keys are in CodeMirror format, NOT vim format.
  187. doInsertModeKeys: doInsertModeKeysFn(cm),
  188. doEx: doExFn(cm),
  189. assertCursorAt: assertCursorAtFn(cm),
  190. fakeOpenDialog: fakeOpenDialog,
  191. fakeOpenNotification: fakeOpenNotification,
  192. getRegisterController: function() {
  193. return CodeMirror.Vim.getRegisterController();
  194. }
  195. }
  196. CodeMirror.Vim.resetVimGlobalState_();
  197. var successful = false;
  198. var savedOpenNotification = cm.openNotification;
  199. var savedOpenDialog = cm.openDialog;
  200. try {
  201. run(cm, vim, helpers);
  202. successful = true;
  203. } finally {
  204. cm.openNotification = savedOpenNotification;
  205. cm.openDialog = savedOpenDialog;
  206. if (!successful || verbose) {
  207. place.style.visibility = "visible";
  208. } else {
  209. place.removeChild(cm.getWrapperElement());
  210. }
  211. }
  212. }, expectedFail);
  213. };
  214. testVim('qq@q', function(cm, vim, helpers) {
  215. cm.setCursor(0, 0);
  216. helpers.doKeys('q', 'q', 'l', 'l', 'q');
  217. helpers.assertCursorAt(0,2);
  218. helpers.doKeys('@', 'q');
  219. helpers.assertCursorAt(0,4);
  220. }, { value: ' '});
  221. testVim('@@', function(cm, vim, helpers) {
  222. cm.setCursor(0, 0);
  223. helpers.doKeys('q', 'q', 'l', 'l', 'q');
  224. helpers.assertCursorAt(0,2);
  225. helpers.doKeys('@', 'q');
  226. helpers.assertCursorAt(0,4);
  227. helpers.doKeys('@', '@');
  228. helpers.assertCursorAt(0,6);
  229. }, { value: ' '});
  230. var jumplistScene = ''+
  231. 'word\n'+
  232. '(word)\n'+
  233. '{word\n'+
  234. 'word.\n'+
  235. '\n'+
  236. 'word search\n'+
  237. '}word\n'+
  238. 'word\n'+
  239. 'word\n';
  240. function testJumplist(name, keys, endPos, startPos, dialog) {
  241. endPos = makeCursor(endPos[0], endPos[1]);
  242. startPos = makeCursor(startPos[0], startPos[1]);
  243. testVim(name, function(cm, vim, helpers) {
  244. CodeMirror.Vim.resetVimGlobalState_();
  245. if(dialog)cm.openDialog = helpers.fakeOpenDialog('word');
  246. cm.setCursor(startPos);
  247. helpers.doKeys.apply(null, keys);
  248. helpers.assertCursorAt(endPos);
  249. }, {value: jumplistScene});
  250. }
  251. testJumplist('jumplist_H', ['H', '<C-o>'], [5,2], [5,2]);
  252. testJumplist('jumplist_M', ['M', '<C-o>'], [2,2], [2,2]);
  253. testJumplist('jumplist_L', ['L', '<C-o>'], [2,2], [2,2]);
  254. testJumplist('jumplist_[[', ['[', '[', '<C-o>'], [5,2], [5,2]);
  255. testJumplist('jumplist_]]', [']', ']', '<C-o>'], [2,2], [2,2]);
  256. testJumplist('jumplist_G', ['G', '<C-o>'], [5,2], [5,2]);
  257. testJumplist('jumplist_gg', ['g', 'g', '<C-o>'], [5,2], [5,2]);
  258. testJumplist('jumplist_%', ['%', '<C-o>'], [1,5], [1,5]);
  259. testJumplist('jumplist_{', ['{', '<C-o>'], [1,5], [1,5]);
  260. testJumplist('jumplist_}', ['}', '<C-o>'], [1,5], [1,5]);
  261. testJumplist('jumplist_\'', ['m', 'a', 'h', '\'', 'a', 'h', '<C-i>'], [1,0], [1,5]);
  262. testJumplist('jumplist_`', ['m', 'a', 'h', '`', 'a', 'h', '<C-i>'], [1,5], [1,5]);
  263. testJumplist('jumplist_*_cachedCursor', ['*', '<C-o>'], [1,3], [1,3]);
  264. testJumplist('jumplist_#_cachedCursor', ['#', '<C-o>'], [1,3], [1,3]);
  265. testJumplist('jumplist_n', ['#', 'n', '<C-o>'], [1,1], [2,3]);
  266. testJumplist('jumplist_N', ['#', 'N', '<C-o>'], [1,1], [2,3]);
  267. testJumplist('jumplist_repeat_<c-o>', ['*', '*', '*', '3', '<C-o>'], [2,3], [2,3]);
  268. testJumplist('jumplist_repeat_<c-i>', ['*', '*', '*', '3', '<C-o>', '2', '<C-i>'], [5,0], [2,3]);
  269. testJumplist('jumplist_repeated_motion', ['3', '*', '<C-o>'], [2,3], [2,3]);
  270. testJumplist('jumplist_/', ['/', '<C-o>'], [2,3], [2,3], 'dialog');
  271. testJumplist('jumplist_?', ['?', '<C-o>'], [2,3], [2,3], 'dialog');
  272. testJumplist('jumplist_skip_deleted_mark<c-o>',
  273. ['*', 'n', 'n', 'k', 'd', 'k', '<C-o>', '<C-o>', '<C-o>'],
  274. [0,2], [0,2]);
  275. testJumplist('jumplist_skip_deleted_mark<c-i>',
  276. ['*', 'n', 'n', 'k', 'd', 'k', '<C-o>', '<C-i>', '<C-i>'],
  277. [1,0], [0,2]);
  278. /**
  279. * @param name Name of the test
  280. * @param keys An array of keys or a string with a single key to simulate.
  281. * @param endPos The expected end position of the cursor.
  282. * @param startPos The position the cursor should start at, defaults to 0, 0.
  283. */
  284. function testMotion(name, keys, endPos, startPos) {
  285. testVim(name, function(cm, vim, helpers) {
  286. if (!startPos) {
  287. startPos = new Pos(0, 0);
  288. }
  289. cm.setCursor(startPos);
  290. helpers.doKeys(keys);
  291. helpers.assertCursorAt(endPos);
  292. });
  293. }
  294. function makeCursor(line, ch) {
  295. return new Pos(line, ch);
  296. }
  297. function offsetCursor(cur, offsetLine, offsetCh) {
  298. return new Pos(cur.line + offsetLine, cur.ch + offsetCh);
  299. }
  300. // Motion tests
  301. testMotion('|', '|', makeCursor(0, 0), makeCursor(0,4));
  302. testMotion('|_repeat', ['3', '|'], makeCursor(0, 2), makeCursor(0,4));
  303. testMotion('h', 'h', makeCursor(0, 0), word1.start);
  304. testMotion('h_repeat', ['3', 'h'], offsetCursor(word1.end, 0, -3), word1.end);
  305. testMotion('l', 'l', makeCursor(0, 1));
  306. testMotion('l_repeat', ['2', 'l'], makeCursor(0, 2));
  307. testMotion('j', 'j', offsetCursor(word1.end, 1, 0), word1.end);
  308. testMotion('j_repeat', ['2', 'j'], offsetCursor(word1.end, 2, 0), word1.end);
  309. testMotion('j_repeat_clip', ['1000', 'j'], endOfDocument);
  310. testMotion('k', 'k', offsetCursor(word3.end, -1, 0), word3.end);
  311. testMotion('k_repeat', ['2', 'k'], makeCursor(0, 4), makeCursor(2, 4));
  312. testMotion('k_repeat_clip', ['1000', 'k'], makeCursor(0, 4), makeCursor(2, 4));
  313. testMotion('w', 'w', word1.start);
  314. testMotion('w_multiple_newlines_no_space', 'w', makeCursor(12, 2), makeCursor(11, 2));
  315. testMotion('w_multiple_newlines_with_space', 'w', makeCursor(14, 0), makeCursor(12, 51));
  316. testMotion('w_repeat', ['2', 'w'], word2.start);
  317. testMotion('w_wrap', ['w'], word3.start, word2.start);
  318. testMotion('w_endOfDocument', 'w', endOfDocument, endOfDocument);
  319. testMotion('w_start_to_end', ['1000', 'w'], endOfDocument, makeCursor(0, 0));
  320. testMotion('W', 'W', bigWord1.start);
  321. testMotion('W_repeat', ['2', 'W'], bigWord3.start, bigWord1.start);
  322. testMotion('e', 'e', word1.end);
  323. testMotion('e_repeat', ['2', 'e'], word2.end);
  324. testMotion('e_wrap', 'e', word3.end, word2.end);
  325. testMotion('e_endOfDocument', 'e', endOfDocument, endOfDocument);
  326. testMotion('e_start_to_end', ['1000', 'e'], endOfDocument, makeCursor(0, 0));
  327. testMotion('b', 'b', word3.start, word3.end);
  328. testMotion('b_repeat', ['2', 'b'], word2.start, word3.end);
  329. testMotion('b_wrap', 'b', word2.start, word3.start);
  330. testMotion('b_startOfDocument', 'b', makeCursor(0, 0), makeCursor(0, 0));
  331. testMotion('b_end_to_start', ['1000', 'b'], makeCursor(0, 0), endOfDocument);
  332. testMotion('ge', ['g', 'e'], word2.end, word3.end);
  333. testMotion('ge_repeat', ['2', 'g', 'e'], word1.end, word3.start);
  334. testMotion('ge_wrap', ['g', 'e'], word2.end, word3.start);
  335. testMotion('ge_startOfDocument', ['g', 'e'], makeCursor(0, 0),
  336. makeCursor(0, 0));
  337. testMotion('ge_end_to_start', ['1000', 'g', 'e'], makeCursor(0, 0), endOfDocument);
  338. testMotion('gg', ['g', 'g'], makeCursor(lines[0].line, lines[0].textStart),
  339. makeCursor(3, 1));
  340. testMotion('gg_repeat', ['3', 'g', 'g'],
  341. makeCursor(lines[2].line, lines[2].textStart));
  342. testMotion('G', 'G',
  343. makeCursor(lines[lines.length - 1].line, lines[lines.length - 1].textStart),
  344. makeCursor(3, 1));
  345. testMotion('G_repeat', ['3', 'G'], makeCursor(lines[2].line,
  346. lines[2].textStart));
  347. // TODO: Make the test code long enough to test Ctrl-F and Ctrl-B.
  348. testMotion('0', '0', makeCursor(0, 0), makeCursor(0, 8));
  349. testMotion('^', '^', makeCursor(0, lines[0].textStart), makeCursor(0, 8));
  350. testMotion('+', '+', makeCursor(1, lines[1].textStart), makeCursor(0, 8));
  351. testMotion('-', '-', makeCursor(0, lines[0].textStart), makeCursor(1, 4));
  352. testMotion('_', ['6','_'], makeCursor(5, lines[5].textStart), makeCursor(0, 8));
  353. testMotion('$', '$', makeCursor(0, lines[0].length - 1), makeCursor(0, 1));
  354. testMotion('$_repeat', ['2', '$'], makeCursor(1, lines[1].length - 1),
  355. makeCursor(0, 3));
  356. testMotion('f', ['f', 'p'], pChars[0], makeCursor(charLine.line, 0));
  357. testMotion('f_repeat', ['2', 'f', 'p'], pChars[2], pChars[0]);
  358. testMotion('f_num', ['f', '2'], numChars[2], makeCursor(charLine.line, 0));
  359. testMotion('t', ['t','p'], offsetCursor(pChars[0], 0, -1),
  360. makeCursor(charLine.line, 0));
  361. testMotion('t_repeat', ['2', 't', 'p'], offsetCursor(pChars[2], 0, -1),
  362. pChars[0]);
  363. testMotion('F', ['F', 'p'], pChars[0], pChars[1]);
  364. testMotion('F_repeat', ['2', 'F', 'p'], pChars[0], pChars[2]);
  365. testMotion('T', ['T', 'p'], offsetCursor(pChars[0], 0, 1), pChars[1]);
  366. testMotion('T_repeat', ['2', 'T', 'p'], offsetCursor(pChars[0], 0, 1), pChars[2]);
  367. testMotion('%_parens', ['%'], parens1.end, parens1.start);
  368. testMotion('%_squares', ['%'], squares1.end, squares1.start);
  369. testMotion('%_braces', ['%'], curlys1.end, curlys1.start);
  370. testMotion('%_seek_outside', ['%'], seekOutside.end, seekOutside.start);
  371. testMotion('%_seek_inside', ['%'], seekInside.end, seekInside.start);
  372. testVim('%_seek_skip', function(cm, vim, helpers) {
  373. cm.setCursor(0,0);
  374. helpers.doKeys(['%']);
  375. helpers.assertCursorAt(0,9);
  376. }, {value:'01234"("()'});
  377. testVim('%_skip_string', function(cm, vim, helpers) {
  378. cm.setCursor(0,0);
  379. helpers.doKeys(['%']);
  380. helpers.assertCursorAt(0,4);
  381. cm.setCursor(0,2);
  382. helpers.doKeys(['%']);
  383. helpers.assertCursorAt(0,0);
  384. }, {value:'(")")'});
  385. testVim('%_skip_comment', function(cm, vim, helpers) {
  386. cm.setCursor(0,0);
  387. helpers.doKeys(['%']);
  388. helpers.assertCursorAt(0,6);
  389. cm.setCursor(0,3);
  390. helpers.doKeys(['%']);
  391. helpers.assertCursorAt(0,0);
  392. }, {value:'(/*)*/)'});
  393. // Make sure that moving down after going to the end of a line always leaves you
  394. // at the end of a line, but preserves the offset in other cases
  395. testVim('Changing lines after Eol operation', function(cm, vim, helpers) {
  396. cm.setCursor(0,0);
  397. helpers.doKeys(['$']);
  398. helpers.doKeys(['j']);
  399. // After moving to Eol and then down, we should be at Eol of line 2
  400. helpers.assertCursorAt(new Pos(1, lines[1].length - 1));
  401. helpers.doKeys(['j']);
  402. // After moving down, we should be at Eol of line 3
  403. helpers.assertCursorAt(new Pos(2, lines[2].length - 1));
  404. helpers.doKeys(['h']);
  405. helpers.doKeys(['j']);
  406. // After moving back one space and then down, since line 4 is shorter than line 2, we should
  407. // be at Eol of line 2 - 1
  408. helpers.assertCursorAt(new Pos(3, lines[3].length - 1));
  409. helpers.doKeys(['j']);
  410. helpers.doKeys(['j']);
  411. // After moving down again, since line 3 has enough characters, we should be back to the
  412. // same place we were at on line 1
  413. helpers.assertCursorAt(new Pos(5, lines[2].length - 2));
  414. });
  415. //making sure gj and gk recover from clipping
  416. testVim('gj_gk_clipping', function(cm,vim,helpers){
  417. cm.setCursor(0, 1);
  418. helpers.doKeys('g','j','g','j');
  419. helpers.assertCursorAt(2, 1);
  420. helpers.doKeys('g','k','g','k');
  421. helpers.assertCursorAt(0, 1);
  422. },{value: 'line 1\n\nline 2'});
  423. //testing a mix of j/k and gj/gk
  424. testVim('j_k_and_gj_gk', function(cm,vim,helpers){
  425. cm.setSize(120);
  426. cm.setCursor(0, 0);
  427. //go to the last character on the first line
  428. helpers.doKeys('$');
  429. //move up/down on the column within the wrapped line
  430. //side-effect: cursor is not locked to eol anymore
  431. helpers.doKeys('g','k');
  432. var cur=cm.getCursor();
  433. eq(cur.line,0);
  434. is((cur.ch<176),'gk didn\'t move cursor back (1)');
  435. helpers.doKeys('g','j');
  436. helpers.assertCursorAt(0, 176);
  437. //should move to character 177 on line 2 (j/k preserve character index within line)
  438. helpers.doKeys('j');
  439. //due to different line wrapping, the cursor can be on a different screen-x now
  440. //gj and gk preserve screen-x on movement, much like moveV
  441. helpers.doKeys('3','g','k');
  442. cur=cm.getCursor();
  443. eq(cur.line,1);
  444. is((cur.ch<176),'gk didn\'t move cursor back (2)');
  445. helpers.doKeys('g','j','2','g','j');
  446. //should return to the same character-index
  447. helpers.doKeys('k');
  448. helpers.assertCursorAt(0, 176);
  449. },{ 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.'});
  450. testVim('gj_gk', function(cm, vim, helpers) {
  451. if (phantom) return;
  452. cm.setSize(120);
  453. // Test top of document edge case.
  454. cm.setCursor(0, 4);
  455. helpers.doKeys('g', 'j');
  456. helpers.doKeys('10', 'g', 'k');
  457. helpers.assertCursorAt(0, 4);
  458. // Test moving down preserves column position.
  459. helpers.doKeys('g', 'j');
  460. var pos1 = cm.getCursor();
  461. var expectedPos2 = new Pos(0, (pos1.ch - 4) * 2 + 4);
  462. helpers.doKeys('g', 'j');
  463. helpers.assertCursorAt(expectedPos2);
  464. // Move to the last character
  465. cm.setCursor(0, 0);
  466. // Move left to reset HSPos
  467. helpers.doKeys('h');
  468. // Test bottom of document edge case.
  469. helpers.doKeys('100', 'g', 'j');
  470. var endingPos = cm.getCursor();
  471. is(endingPos != 0, 'gj should not be on wrapped line 0');
  472. var topLeftCharCoords = cm.charCoords(makeCursor(0, 0));
  473. var endingCharCoords = cm.charCoords(endingPos);
  474. is(topLeftCharCoords.left == endingCharCoords.left, 'gj should end up on column 0');
  475. },{ lineNumbers: false, lineWrapping:true, value: 'Thislineisintentionallylongtotestmovementofgjandgkoverwrappedlines.' });
  476. testVim('}', function(cm, vim, helpers) {
  477. cm.setCursor(0, 0);
  478. helpers.doKeys('}');
  479. helpers.assertCursorAt(1, 0);
  480. cm.setCursor(0, 0);
  481. helpers.doKeys('2', '}');
  482. helpers.assertCursorAt(4, 0);
  483. cm.setCursor(0, 0);
  484. helpers.doKeys('6', '}');
  485. helpers.assertCursorAt(5, 0);
  486. }, { value: 'a\n\nb\nc\n\nd' });
  487. testVim('{', function(cm, vim, helpers) {
  488. cm.setCursor(5, 0);
  489. helpers.doKeys('{');
  490. helpers.assertCursorAt(4, 0);
  491. cm.setCursor(5, 0);
  492. helpers.doKeys('2', '{');
  493. helpers.assertCursorAt(1, 0);
  494. cm.setCursor(5, 0);
  495. helpers.doKeys('6', '{');
  496. helpers.assertCursorAt(0, 0);
  497. }, { value: 'a\n\nb\nc\n\nd' });
  498. testVim('(', function(cm, vim, helpers) {
  499. cm.setCursor(6, 23);
  500. helpers.doKeys('(');
  501. helpers.assertCursorAt(6, 14);
  502. helpers.doKeys('2', '(');
  503. helpers.assertCursorAt(5, 0);
  504. helpers.doKeys('(');
  505. helpers.assertCursorAt(4, 0);
  506. helpers.doKeys('(');
  507. helpers.assertCursorAt(3, 0);
  508. helpers.doKeys('(');
  509. helpers.assertCursorAt(2, 0);
  510. helpers.doKeys('(');
  511. helpers.assertCursorAt(0, 0);
  512. helpers.doKeys('(');
  513. helpers.assertCursorAt(0, 0);
  514. }, { value: 'sentence1.\n\n\nsentence2\n\nsentence3. sentence4\n sentence5? sentence6!' });
  515. testVim(')', function(cm, vim, helpers) {
  516. cm.setCursor(0, 0);
  517. helpers.doKeys('2', ')');
  518. helpers.assertCursorAt(3, 0);
  519. helpers.doKeys(')');
  520. helpers.assertCursorAt(4, 0);
  521. helpers.doKeys(')');
  522. helpers.assertCursorAt(5, 0);
  523. helpers.doKeys(')');
  524. helpers.assertCursorAt(5, 11);
  525. helpers.doKeys(')');
  526. helpers.assertCursorAt(6, 14);
  527. helpers.doKeys(')');
  528. helpers.assertCursorAt(6, 23);
  529. helpers.doKeys(')');
  530. helpers.assertCursorAt(6, 23);
  531. }, { value: 'sentence1.\n\n\nsentence2\n\nsentence3. sentence4\n sentence5? sentence6!' });
  532. testVim('paragraph_motions', function(cm, vim, helpers) {
  533. cm.setCursor(10, 0);
  534. helpers.doKeys('{');
  535. helpers.assertCursorAt(4, 0);
  536. helpers.doKeys('{');
  537. helpers.assertCursorAt(0, 0);
  538. helpers.doKeys('2', '}');
  539. helpers.assertCursorAt(7, 0);
  540. helpers.doKeys('2', '}');
  541. helpers.assertCursorAt(16, 0);
  542. cm.setCursor(9, 0);
  543. helpers.doKeys('}');
  544. helpers.assertCursorAt(14, 0);
  545. cm.setCursor(6, 0);
  546. helpers.doKeys('}');
  547. helpers.assertCursorAt(7, 0);
  548. // ip inside empty space
  549. cm.setCursor(10, 0);
  550. helpers.doKeys('v', 'i', 'p');
  551. eqCursorPos(Pos(7, 0), cm.getCursor('anchor'));
  552. eqCursorPos(Pos(12, 0), cm.getCursor('head'));
  553. helpers.doKeys('i', 'p');
  554. eqCursorPos(Pos(7, 0), cm.getCursor('anchor'));
  555. eqCursorPos(Pos(13, 1), cm.getCursor('head'));
  556. helpers.doKeys('2', 'i', 'p');
  557. eqCursorPos(Pos(7, 0), cm.getCursor('anchor'));
  558. eqCursorPos(Pos(16, 1), cm.getCursor('head'));
  559. // should switch to visualLine mode
  560. cm.setCursor(14, 0);
  561. helpers.doKeys('<Esc>', 'v', 'i', 'p');
  562. helpers.assertCursorAt(14, 0);
  563. cm.setCursor(14, 0);
  564. helpers.doKeys('<Esc>', 'V', 'i', 'p');
  565. eqCursorPos(Pos(16, 1), cm.getCursor('head'));
  566. // ap inside empty space
  567. cm.setCursor(10, 0);
  568. helpers.doKeys('<Esc>', 'v', 'a', 'p');
  569. eqCursorPos(Pos(7, 0), cm.getCursor('anchor'));
  570. eqCursorPos(Pos(13, 1), cm.getCursor('head'));
  571. helpers.doKeys('a', 'p');
  572. eqCursorPos(Pos(7, 0), cm.getCursor('anchor'));
  573. eqCursorPos(Pos(16, 1), cm.getCursor('head'));
  574. cm.setCursor(13, 0);
  575. helpers.doKeys('v', 'a', 'p');
  576. eqCursorPos(Pos(13, 0), cm.getCursor('anchor'));
  577. eqCursorPos(Pos(14, 0), cm.getCursor('head'));
  578. cm.setCursor(16, 0);
  579. helpers.doKeys('v', 'a', 'p');
  580. eqCursorPos(Pos(14, 0), cm.getCursor('anchor'));
  581. eqCursorPos(Pos(16, 1), cm.getCursor('head'));
  582. cm.setCursor(0, 0);
  583. helpers.doKeys('v', 'a', 'p');
  584. eqCursorPos(Pos(0, 0), cm.getCursor('anchor'));
  585. eqCursorPos(Pos(4, 0), cm.getCursor('head'));
  586. cm.setCursor(0, 0);
  587. helpers.doKeys('d', 'i', 'p');
  588. var register = helpers.getRegisterController().getRegister();
  589. eq('a\na\n', register.toString());
  590. is(register.linewise);
  591. helpers.doKeys('3', 'j', 'p');
  592. helpers.doKeys('y', 'i', 'p');
  593. is(register.linewise);
  594. eq('b\na\na\nc\n', register.toString());
  595. }, { value: 'a\na\n\n\n\nb\nc\n\n\n\n\n\n\nd\n\ne\nf' });
  596. // Operator tests
  597. testVim('dl', function(cm, vim, helpers) {
  598. var curStart = makeCursor(0, 0);
  599. cm.setCursor(curStart);
  600. helpers.doKeys('d', 'l');
  601. eq('word1 ', cm.getValue());
  602. var register = helpers.getRegisterController().getRegister();
  603. eq(' ', register.toString());
  604. is(!register.linewise);
  605. eqCursorPos(curStart, cm.getCursor());
  606. }, { value: ' word1 ' });
  607. testVim('dl_eol', function(cm, vim, helpers) {
  608. cm.setCursor(0, 6);
  609. helpers.doKeys('d', 'l');
  610. eq(' word1', cm.getValue());
  611. var register = helpers.getRegisterController().getRegister();
  612. eq(' ', register.toString());
  613. is(!register.linewise);
  614. helpers.assertCursorAt(0, 5);
  615. }, { value: ' word1 ' });
  616. testVim('dl_repeat', function(cm, vim, helpers) {
  617. var curStart = makeCursor(0, 0);
  618. cm.setCursor(curStart);
  619. helpers.doKeys('2', 'd', 'l');
  620. eq('ord1 ', cm.getValue());
  621. var register = helpers.getRegisterController().getRegister();
  622. eq(' w', register.toString());
  623. is(!register.linewise);
  624. eqCursorPos(curStart, cm.getCursor());
  625. }, { value: ' word1 ' });
  626. testVim('dh', function(cm, vim, helpers) {
  627. var curStart = makeCursor(0, 3);
  628. cm.setCursor(curStart);
  629. helpers.doKeys('d', 'h');
  630. eq(' wrd1 ', cm.getValue());
  631. var register = helpers.getRegisterController().getRegister();
  632. eq('o', register.toString());
  633. is(!register.linewise);
  634. eqCursorPos(offsetCursor(curStart, 0 , -1), cm.getCursor());
  635. }, { value: ' word1 ' });
  636. testVim('dj', function(cm, vim, helpers) {
  637. var curStart = makeCursor(0, 3);
  638. cm.setCursor(curStart);
  639. helpers.doKeys('d', 'j');
  640. eq(' word3', cm.getValue());
  641. var register = helpers.getRegisterController().getRegister();
  642. eq(' word1\nword2\n', register.toString());
  643. is(register.linewise);
  644. helpers.assertCursorAt(0, 1);
  645. }, { value: ' word1\nword2\n word3' });
  646. testVim('dj_end_of_document', function(cm, vim, helpers) {
  647. var curStart = makeCursor(0, 3);
  648. cm.setCursor(curStart);
  649. helpers.doKeys('d', 'j');
  650. eq('', cm.getValue());
  651. var register = helpers.getRegisterController().getRegister();
  652. eq(' word1 \n', register.toString());
  653. is(register.linewise);
  654. helpers.assertCursorAt(0, 0);
  655. }, { value: ' word1 ' });
  656. testVim('dk', function(cm, vim, helpers) {
  657. var curStart = makeCursor(1, 3);
  658. cm.setCursor(curStart);
  659. helpers.doKeys('d', 'k');
  660. eq(' word3', cm.getValue());
  661. var register = helpers.getRegisterController().getRegister();
  662. eq(' word1\nword2\n', register.toString());
  663. is(register.linewise);
  664. helpers.assertCursorAt(0, 1);
  665. }, { value: ' word1\nword2\n word3' });
  666. testVim('dk_start_of_document', function(cm, vim, helpers) {
  667. var curStart = makeCursor(0, 3);
  668. cm.setCursor(curStart);
  669. helpers.doKeys('d', 'k');
  670. eq('', cm.getValue());
  671. var register = helpers.getRegisterController().getRegister();
  672. eq(' word1 \n', register.toString());
  673. is(register.linewise);
  674. helpers.assertCursorAt(0, 0);
  675. }, { value: ' word1 ' });
  676. testVim('dw_space', function(cm, vim, helpers) {
  677. var curStart = makeCursor(0, 0);
  678. cm.setCursor(curStart);
  679. helpers.doKeys('d', 'w');
  680. eq('word1 ', cm.getValue());
  681. var register = helpers.getRegisterController().getRegister();
  682. eq(' ', register.toString());
  683. is(!register.linewise);
  684. eqCursorPos(curStart, cm.getCursor());
  685. }, { value: ' word1 ' });
  686. testVim('dw_word', function(cm, vim, helpers) {
  687. var curStart = makeCursor(0, 1);
  688. cm.setCursor(curStart);
  689. helpers.doKeys('d', 'w');
  690. eq(' word2', cm.getValue());
  691. var register = helpers.getRegisterController().getRegister();
  692. eq('word1 ', register.toString());
  693. is(!register.linewise);
  694. eqCursorPos(curStart, cm.getCursor());
  695. }, { value: ' word1 word2' });
  696. testVim('dw_unicode_word', function(cm, vim, helpers) {
  697. helpers.doKeys('d', 'w');
  698. eq(cm.getValue().length, 10);
  699. helpers.doKeys('d', 'w');
  700. eq(cm.getValue().length, 6);
  701. helpers.doKeys('d', 'w');
  702. eq(cm.getValue().length, 5);
  703. helpers.doKeys('d', 'e');
  704. eq(cm.getValue().length, 2);
  705. }, { value: ' \u0562\u0561\u0580\u0587\xbbe\xb5g ' });
  706. testVim('dw_only_word', function(cm, vim, helpers) {
  707. // Test that if there is only 1 word left, dw deletes till the end of the
  708. // line.
  709. cm.setCursor(0, 1);
  710. helpers.doKeys('d', 'w');
  711. eq(' ', cm.getValue());
  712. var register = helpers.getRegisterController().getRegister();
  713. eq('word1 ', register.toString());
  714. is(!register.linewise);
  715. helpers.assertCursorAt(0, 0);
  716. }, { value: ' word1 ' });
  717. testVim('dw_eol', function(cm, vim, helpers) {
  718. // Assert that dw does not delete the newline if last word to delete is at end
  719. // of line.
  720. cm.setCursor(0, 1);
  721. helpers.doKeys('d', 'w');
  722. eq(' \nword2', cm.getValue());
  723. var register = helpers.getRegisterController().getRegister();
  724. eq('word1', register.toString());
  725. is(!register.linewise);
  726. helpers.assertCursorAt(0, 0);
  727. }, { value: ' word1\nword2' });
  728. testVim('dw_eol_with_multiple_newlines', function(cm, vim, helpers) {
  729. // Assert that dw does not delete the newline if last word to delete is at end
  730. // of line and it is followed by multiple newlines.
  731. cm.setCursor(0, 1);
  732. helpers.doKeys('d', 'w');
  733. eq(' \n\nword2', cm.getValue());
  734. var register = helpers.getRegisterController().getRegister();
  735. eq('word1', register.toString());
  736. is(!register.linewise);
  737. helpers.assertCursorAt(0, 0);
  738. }, { value: ' word1\n\nword2' });
  739. testVim('dw_empty_line_followed_by_whitespace', function(cm, vim, helpers) {
  740. cm.setCursor(0, 0);
  741. helpers.doKeys('d', 'w');
  742. eq(' \nword', cm.getValue());
  743. }, { value: '\n \nword' });
  744. testVim('dw_empty_line_followed_by_word', function(cm, vim, helpers) {
  745. cm.setCursor(0, 0);
  746. helpers.doKeys('d', 'w');
  747. eq('word', cm.getValue());
  748. }, { value: '\nword' });
  749. testVim('dw_empty_line_followed_by_empty_line', function(cm, vim, helpers) {
  750. cm.setCursor(0, 0);
  751. helpers.doKeys('d', 'w');
  752. eq('\n', cm.getValue());
  753. }, { value: '\n\n' });
  754. testVim('dw_whitespace_followed_by_whitespace', function(cm, vim, helpers) {
  755. cm.setCursor(0, 0);
  756. helpers.doKeys('d', 'w');
  757. eq('\n \n', cm.getValue());
  758. }, { value: ' \n \n' });
  759. testVim('dw_whitespace_followed_by_empty_line', function(cm, vim, helpers) {
  760. cm.setCursor(0, 0);
  761. helpers.doKeys('d', 'w');
  762. eq('\n\n', cm.getValue());
  763. }, { value: ' \n\n' });
  764. testVim('dw_word_whitespace_word', function(cm, vim, helpers) {
  765. cm.setCursor(0, 0);
  766. helpers.doKeys('d', 'w');
  767. eq('\n \nword2', cm.getValue());
  768. }, { value: 'word1\n \nword2'})
  769. testVim('dw_end_of_document', function(cm, vim, helpers) {
  770. cm.setCursor(1, 2);
  771. helpers.doKeys('d', 'w');
  772. eq('\nab', cm.getValue());
  773. }, { value: '\nabc' });
  774. testVim('dw_repeat', function(cm, vim, helpers) {
  775. // Assert that dw does delete newline if it should go to the next line, and
  776. // that repeat works properly.
  777. cm.setCursor(0, 1);
  778. helpers.doKeys('d', '2', 'w');
  779. eq(' ', cm.getValue());
  780. var register = helpers.getRegisterController().getRegister();
  781. eq('word1\nword2', register.toString());
  782. is(!register.linewise);
  783. helpers.assertCursorAt(0, 0);
  784. }, { value: ' word1\nword2' });
  785. testVim('de_word_start_and_empty_lines', function(cm, vim, helpers) {
  786. cm.setCursor(0, 0);
  787. helpers.doKeys('d', 'e');
  788. eq('\n\n', cm.getValue());
  789. }, { value: 'word\n\n' });
  790. testVim('de_word_end_and_empty_lines', function(cm, vim, helpers) {
  791. cm.setCursor(0, 3);
  792. helpers.doKeys('d', 'e');
  793. eq('wor', cm.getValue());
  794. }, { value: 'word\n\n\n' });
  795. testVim('de_whitespace_and_empty_lines', function(cm, vim, helpers) {
  796. cm.setCursor(0, 0);
  797. helpers.doKeys('d', 'e');
  798. eq('', cm.getValue());
  799. }, { value: ' \n\n\n' });
  800. testVim('de_end_of_document', function(cm, vim, helpers) {
  801. cm.setCursor(1, 2);
  802. helpers.doKeys('d', 'e');
  803. eq('\nab', cm.getValue());
  804. }, { value: '\nabc' });
  805. testVim('db_empty_lines', function(cm, vim, helpers) {
  806. cm.setCursor(2, 0);
  807. helpers.doKeys('d', 'b');
  808. eq('\n\n', cm.getValue());
  809. }, { value: '\n\n\n' });
  810. testVim('db_word_start_and_empty_lines', function(cm, vim, helpers) {
  811. cm.setCursor(2, 0);
  812. helpers.doKeys('d', 'b');
  813. eq('\nword', cm.getValue());
  814. }, { value: '\n\nword' });
  815. testVim('db_word_end_and_empty_lines', function(cm, vim, helpers) {
  816. cm.setCursor(2, 3);
  817. helpers.doKeys('d', 'b');
  818. eq('\n\nd', cm.getValue());
  819. }, { value: '\n\nword' });
  820. testVim('db_whitespace_and_empty_lines', function(cm, vim, helpers) {
  821. cm.setCursor(2, 0);
  822. helpers.doKeys('d', 'b');
  823. eq('', cm.getValue());
  824. }, { value: '\n \n' });
  825. testVim('db_start_of_document', function(cm, vim, helpers) {
  826. cm.setCursor(0, 0);
  827. helpers.doKeys('d', 'b');
  828. eq('abc\n', cm.getValue());
  829. }, { value: 'abc\n' });
  830. testVim('dge_empty_lines', function(cm, vim, helpers) {
  831. cm.setCursor(1, 0);
  832. helpers.doKeys('d', 'g', 'e');
  833. // Note: In real VIM the result should be '', but it's not quite consistent,
  834. // since 2 newlines are deleted. But in the similar case of word\n\n, only
  835. // 1 newline is deleted. We'll diverge from VIM's behavior since it's much
  836. // easier this way.
  837. eq('\n', cm.getValue());
  838. }, { value: '\n\n' });
  839. testVim('dge_word_and_empty_lines', function(cm, vim, helpers) {
  840. cm.setCursor(1, 0);
  841. helpers.doKeys('d', 'g', 'e');
  842. eq('wor\n', cm.getValue());
  843. }, { value: 'word\n\n'});
  844. testVim('dge_whitespace_and_empty_lines', function(cm, vim, helpers) {
  845. cm.setCursor(2, 0);
  846. helpers.doKeys('d', 'g', 'e');
  847. eq('', cm.getValue());
  848. }, { value: '\n \n' });
  849. testVim('dge_start_of_document', function(cm, vim, helpers) {
  850. cm.setCursor(0, 0);
  851. helpers.doKeys('d', 'g', 'e');
  852. eq('bc\n', cm.getValue());
  853. }, { value: 'abc\n' });
  854. testVim('d_inclusive', function(cm, vim, helpers) {
  855. // Assert that when inclusive is set, the character the cursor is on gets
  856. // deleted too.
  857. var curStart = makeCursor(0, 1);
  858. cm.setCursor(curStart);
  859. helpers.doKeys('d', 'e');
  860. eq(' ', cm.getValue());
  861. var register = helpers.getRegisterController().getRegister();
  862. eq('word1', register.toString());
  863. is(!register.linewise);
  864. eqCursorPos(curStart, cm.getCursor());
  865. }, { value: ' word1 ' });
  866. testVim('d_reverse', function(cm, vim, helpers) {
  867. // Test that deleting in reverse works.
  868. cm.setCursor(1, 0);
  869. helpers.doKeys('d', 'b');
  870. eq(' word2 ', cm.getValue());
  871. var register = helpers.getRegisterController().getRegister();
  872. eq('word1\n', register.toString());
  873. is(!register.linewise);
  874. helpers.assertCursorAt(0, 1);
  875. }, { value: ' word1\nword2 ' });
  876. testVim('dd', function(cm, vim, helpers) {
  877. cm.setCursor(0, 3);
  878. var expectedBuffer = cm.getRange(new Pos(0, 0),
  879. new Pos(1, 0));
  880. var expectedLineCount = cm.lineCount() - 1;
  881. helpers.doKeys('d', 'd');
  882. eq(expectedLineCount, cm.lineCount());
  883. var register = helpers.getRegisterController().getRegister();
  884. eq(expectedBuffer, register.toString());
  885. is(register.linewise);
  886. helpers.assertCursorAt(0, lines[1].textStart);
  887. });
  888. testVim('dd_prefix_repeat', function(cm, vim, helpers) {
  889. cm.setCursor(0, 3);
  890. var expectedBuffer = cm.getRange(new Pos(0, 0),
  891. new Pos(2, 0));
  892. var expectedLineCount = cm.lineCount() - 2;
  893. helpers.doKeys('2', 'd', 'd');
  894. eq(expectedLineCount, cm.lineCount());
  895. var register = helpers.getRegisterController().getRegister();
  896. eq(expectedBuffer, register.toString());
  897. is(register.linewise);
  898. helpers.assertCursorAt(0, lines[2].textStart);
  899. });
  900. testVim('dd_motion_repeat', function(cm, vim, helpers) {
  901. cm.setCursor(0, 3);
  902. var expectedBuffer = cm.getRange(new Pos(0, 0),
  903. new Pos(2, 0));
  904. var expectedLineCount = cm.lineCount() - 2;
  905. helpers.doKeys('d', '2', 'd');
  906. eq(expectedLineCount, cm.lineCount());
  907. var register = helpers.getRegisterController().getRegister();
  908. eq(expectedBuffer, register.toString());
  909. is(register.linewise);
  910. helpers.assertCursorAt(0, lines[2].textStart);
  911. });
  912. testVim('dd_multiply_repeat', function(cm, vim, helpers) {
  913. cm.setCursor(0, 3);
  914. var expectedBuffer = cm.getRange(new Pos(0, 0),
  915. new Pos(6, 0));
  916. var expectedLineCount = cm.lineCount() - 6;
  917. helpers.doKeys('2', 'd', '3', 'd');
  918. eq(expectedLineCount, cm.lineCount());
  919. var register = helpers.getRegisterController().getRegister();
  920. eq(expectedBuffer, register.toString());
  921. is(register.linewise);
  922. helpers.assertCursorAt(0, lines[6].textStart);
  923. });
  924. testVim('dd_lastline', function(cm, vim, helpers) {
  925. cm.setCursor(cm.lineCount(), 0);
  926. var expectedLineCount = cm.lineCount() - 1;
  927. helpers.doKeys('d', 'd');
  928. eq(expectedLineCount, cm.lineCount());
  929. helpers.assertCursorAt(cm.lineCount() - 1, 0);
  930. });
  931. testVim('dd_only_line', function(cm, vim, helpers) {
  932. cm.setCursor(0, 0);
  933. var expectedRegister = cm.getValue() + "\n";
  934. helpers.doKeys('d','d');
  935. eq(1, cm.lineCount());
  936. eq('', cm.getValue());
  937. var register = helpers.getRegisterController().getRegister();
  938. eq(expectedRegister, register.toString());
  939. }, { value: "thisistheonlyline" });
  940. // Yank commands should behave the exact same as d commands, expect that nothing
  941. // gets deleted.
  942. testVim('yw_repeat', function(cm, vim, helpers) {
  943. // Assert that yw does yank newline if it should go to the next line, and
  944. // that repeat works properly.
  945. var curStart = makeCursor(0, 1);
  946. cm.setCursor(curStart);
  947. helpers.doKeys('y', '2', 'w');
  948. eq(' word1\nword2', cm.getValue());
  949. var register = helpers.getRegisterController().getRegister();
  950. eq('word1\nword2', register.toString());
  951. is(!register.linewise);
  952. eqCursorPos(curStart, cm.getCursor());
  953. }, { value: ' word1\nword2' });
  954. testVim('yy_multiply_repeat', function(cm, vim, helpers) {
  955. var curStart = makeCursor(0, 3);
  956. cm.setCursor(curStart);
  957. var expectedBuffer = cm.getRange(new Pos(0, 0),
  958. new Pos(6, 0));
  959. var expectedLineCount = cm.lineCount();
  960. helpers.doKeys('2', 'y', '3', 'y');
  961. eq(expectedLineCount, cm.lineCount());
  962. var register = helpers.getRegisterController().getRegister();
  963. eq(expectedBuffer, register.toString());
  964. is(register.linewise);
  965. eqCursorPos(curStart, cm.getCursor());
  966. });
  967. testVim('2dd_blank_P', function(cm, vim, helpers) {
  968. helpers.doKeys('2', 'd', 'd', 'P');
  969. eq('\na\n\n', cm.getValue());
  970. }, { value: '\na\n\n' });
  971. // Change commands behave like d commands except that it also enters insert
  972. // mode. In addition, when the change is linewise, an additional newline is
  973. // inserted so that insert mode starts on that line.
  974. testVim('cw', function(cm, vim, helpers) {
  975. cm.setCursor(0, 0);
  976. helpers.doKeys('c', '2', 'w');
  977. eq(' word3', cm.getValue());
  978. helpers.assertCursorAt(0, 0);
  979. }, { value: 'word1 word2 word3'});
  980. testVim('cw_repeat', function(cm, vim, helpers) {
  981. // Assert that cw does delete newline if it should go to the next line, and
  982. // that repeat works properly.
  983. var curStart = makeCursor(0, 1);
  984. cm.setCursor(curStart);
  985. helpers.doKeys('c', '2', 'w');
  986. eq(' ', cm.getValue());
  987. var register = helpers.getRegisterController().getRegister();
  988. eq('word1\nword2', register.toString());
  989. is(!register.linewise);
  990. eqCursorPos(curStart, cm.getCursor());
  991. eq('vim-insert', cm.getOption('keyMap'));
  992. }, { value: ' word1\nword2' });
  993. testVim('cc_multiply_repeat', function(cm, vim, helpers) {
  994. cm.setCursor(0, 3);
  995. var expectedBuffer = cm.getRange(new Pos(0, 0),
  996. new Pos(6, 0));
  997. var expectedLineCount = cm.lineCount() - 5;
  998. helpers.doKeys('2', 'c', '3', 'c');
  999. eq(expectedLineCount, cm.lineCount());
  1000. var register = helpers.getRegisterController().getRegister();
  1001. eq(expectedBuffer, register.toString());
  1002. is(register.linewise);
  1003. eq('vim-insert', cm.getOption('keyMap'));
  1004. });
  1005. testVim('ct', function(cm, vim, helpers) {
  1006. cm.setCursor(0, 9);
  1007. helpers.doKeys('c', 't', 'w');
  1008. eq(' word1 word3', cm.getValue());
  1009. helpers.doKeys('<Esc>', 'c', '|');
  1010. eq(' word3', cm.getValue());
  1011. helpers.assertCursorAt(0, 0);
  1012. helpers.doKeys('<Esc>', '2', 'u', 'w', 'h');
  1013. helpers.doKeys('c', '2', 'g', 'e');
  1014. eq(' wordword3', cm.getValue());
  1015. }, { value: ' word1 word2 word3'});
  1016. testVim('cc_should_not_append_to_document', function(cm, vim, helpers) {
  1017. var expectedLineCount = cm.lineCount();
  1018. cm.setCursor(cm.lastLine(), 0);
  1019. helpers.doKeys('c', 'c');
  1020. eq(expectedLineCount, cm.lineCount());
  1021. });
  1022. function fillArray(val, times) {
  1023. var arr = [];
  1024. for (var i = 0; i < times; i++) {
  1025. arr.push(val);
  1026. }
  1027. return arr;
  1028. }
  1029. testVim('c_visual_block', function(cm, vim, helpers) {
  1030. cm.setCursor(0, 1);
  1031. helpers.doKeys('<C-v>', '2', 'j', 'l', 'l', 'l', 'c');
  1032. var replacement = fillArray('hello', 3);
  1033. cm.replaceSelections(replacement);
  1034. eq('1hello\n5hello\nahellofg', cm.getValue());
  1035. helpers.doKeys('<Esc>');
  1036. cm.setCursor(2, 3);
  1037. helpers.doKeys('<C-v>', '2', 'k', 'h', 'C');
  1038. replacement = fillArray('world', 3);
  1039. cm.replaceSelections(replacement);
  1040. eq('1hworld\n5hworld\nahworld', cm.getValue());
  1041. }, {value: '1234\n5678\nabcdefg'});
  1042. testVim('c_visual_block_replay', function(cm, vim, helpers) {
  1043. cm.setCursor(0, 1);
  1044. helpers.doKeys('<C-v>', '2', 'j', 'l', 'c');
  1045. var replacement = fillArray('fo', 3);
  1046. cm.replaceSelections(replacement);
  1047. eq('1fo4\n5fo8\nafodefg', cm.getValue());
  1048. helpers.doKeys('<Esc>');
  1049. cm.setCursor(0, 0);
  1050. helpers.doKeys('.');
  1051. eq('foo4\nfoo8\nfoodefg', cm.getValue());
  1052. }, {value: '1234\n5678\nabcdefg'});
  1053. testVim('d_visual_block', function(cm, vim, helpers) {
  1054. cm.setCursor(0, 1);
  1055. helpers.doKeys('<C-v>', '2', 'j', 'l', 'l', 'l', 'd');
  1056. eq('1\n5\nafg', cm.getValue());
  1057. }, {value: '1234\n5678\nabcdefg'});
  1058. testVim('D_visual_block', function(cm, vim, helpers) {
  1059. cm.setCursor(0, 1);
  1060. helpers.doKeys('<C-v>', '2', 'j', 'l', 'D');
  1061. eq('1\n5\na', cm.getValue());
  1062. }, {value: '1234\n5678\nabcdefg'});
  1063. testVim('s_visual_block', function(cm, vim, helpers) {
  1064. cm.setCursor(0, 1);
  1065. helpers.doKeys('<C-v>', '2', 'j', 'l', 'l', 'l', 's');
  1066. var replacement = fillArray('hello{', 3);
  1067. cm.replaceSelections(replacement);
  1068. eq('1hello{\n5hello{\nahello{fg\n', cm.getValue());
  1069. helpers.doKeys('<Esc>');
  1070. cm.setCursor(2, 3);
  1071. helpers.doKeys('<C-v>', '1', 'k', 'h', 'S');
  1072. replacement = fillArray('world', 1);
  1073. cm.replaceSelections(replacement);
  1074. eq('1hello{\n world\n', cm.getValue());
  1075. }, {value: '1234\n5678\nabcdefg\n'});
  1076. // Swapcase commands edit in place and do not modify registers.
  1077. testVim('g~w_repeat', function(cm, vim, helpers) {
  1078. // Assert that dw does delete newline if it should go to the next line, and
  1079. // that repeat works properly.
  1080. var curStart = makeCursor(0, 1);
  1081. cm.setCursor(curStart);
  1082. helpers.doKeys('g', '~', '2', 'w');
  1083. eq(' WORD1\nWORD2', cm.getValue());
  1084. var register = helpers.getRegisterController().getRegister();
  1085. eq('', register.toString());
  1086. is(!register.linewise);
  1087. eqCursorPos(curStart, cm.getCursor());
  1088. }, { value: ' word1\nword2' });
  1089. testVim('g~g~', function(cm, vim, helpers) {
  1090. var curStart = makeCursor(0, 3);
  1091. cm.setCursor(curStart);
  1092. var expectedLineCount = cm.lineCount();
  1093. var expectedValue = cm.getValue().toUpperCase();
  1094. helpers.doKeys('2', 'g', '~', '3', 'g', '~');
  1095. eq(expectedValue, cm.getValue());
  1096. var register = helpers.getRegisterController().getRegister();
  1097. eq('', register.toString());
  1098. is(!register.linewise);
  1099. eqCursorPos(curStart, cm.getCursor());
  1100. }, { value: ' word1\nword2\nword3\nword4\nword5\nword6' });
  1101. testVim('gu_and_gU', function(cm, vim, helpers) {
  1102. var curStart = makeCursor(0, 7);
  1103. var value = cm.getValue();
  1104. cm.setCursor(curStart);
  1105. helpers.doKeys('2', 'g', 'U', 'w');
  1106. eq(cm.getValue(), 'wa wb xX WC wd');
  1107. eqCursorPos(curStart, cm.getCursor());
  1108. helpers.doKeys('2', 'g', 'u', 'w');
  1109. eq(cm.getValue(), value);
  1110. helpers.doKeys('2', 'g', 'U', 'B');
  1111. eq(cm.getValue(), 'wa WB Xx wc wd');
  1112. eqCursorPos(makeCursor(0, 3), cm.getCursor());
  1113. cm.setCursor(makeCursor(0, 4));
  1114. helpers.doKeys('g', 'u', 'i', 'w');
  1115. eq(cm.getValue(), 'wa wb Xx wc wd');
  1116. eqCursorPos(makeCursor(0, 3), cm.getCursor());
  1117. // TODO: support gUgU guu
  1118. // eqCursorPos(makeCursor(0, 0), cm.getCursor());
  1119. var register = helpers.getRegisterController().getRegister();
  1120. eq('', register.toString());
  1121. is(!register.linewise);
  1122. }, { value: 'wa wb xx wc wd' });
  1123. testVim('visual_block_~', function(cm, vim, helpers) {
  1124. cm.setCursor(1, 1);
  1125. helpers.doKeys('<C-v>', 'l', 'l', 'j', '~');
  1126. helpers.assertCursorAt(1, 1);
  1127. eq('hello\nwoRLd\naBCDe', cm.getValue());
  1128. cm.setCursor(2, 0);
  1129. helpers.doKeys('v', 'l', 'l', '~');
  1130. helpers.assertCursorAt(2, 0);
  1131. eq('hello\nwoRLd\nAbcDe', cm.getValue());
  1132. },{value: 'hello\nwOrld\nabcde' });
  1133. testVim('._swapCase_visualBlock', function(cm, vim, helpers) {
  1134. helpers.doKeys('<C-v>', 'j', 'j', 'l', '~');
  1135. cm.setCursor(0, 3);
  1136. helpers.doKeys('.');
  1137. eq('HelLO\nWorLd\nAbcdE', cm.getValue());
  1138. },{value: 'hEllo\nwOrlD\naBcDe' });
  1139. testVim('._delete_visualBlock', function(cm, vim, helpers) {
  1140. helpers.doKeys('<C-v>', 'j', 'x');
  1141. eq('ive\ne\nsome\nsugar', cm.getValue());
  1142. helpers.doKeys('.');
  1143. eq('ve\n\nsome\nsugar', cm.getValue());
  1144. helpers.doKeys('j', 'j', '.');
  1145. eq('ve\n\nome\nugar', cm.getValue());
  1146. helpers.doKeys('u', '<C-r>', '.');
  1147. eq('ve\n\nme\ngar', cm.getValue());
  1148. },{value: 'give\nme\nsome\nsugar' });
  1149. testVim('>{motion}', function(cm, vim, helpers) {
  1150. cm.setCursor(1, 3);
  1151. var expectedLineCount = cm.lineCount();
  1152. var expectedValue = ' word1\n word2\nword3 ';
  1153. helpers.doKeys('>', 'k');
  1154. eq(expectedValue, cm.getValue());
  1155. var register = helpers.getRegisterController().getRegister();
  1156. eq('', register.toString());
  1157. is(!register.linewise);
  1158. helpers.assertCursorAt(0, 3);
  1159. }, { value: ' word1\nword2\nword3 ', indentUnit: 2 });
  1160. testVim('>>', function(cm, vim, helpers) {
  1161. cm.setCursor(0, 3);
  1162. var expectedLineCount = cm.lineCount();
  1163. var expectedValue = ' word1\n word2\nword3 ';
  1164. helpers.doKeys('2', '>', '>');
  1165. eq(expectedValue, cm.getValue());
  1166. var register = helpers.getRegisterController().getRegister();
  1167. eq('', register.toString());
  1168. is(!register.linewise);
  1169. helpers.assertCursorAt(0, 3);
  1170. }, { value: ' word1\nword2\nword3 ', indentUnit: 2 });
  1171. testVim('<{motion}', function(cm, vim, helpers) {
  1172. cm.setCursor(1, 3);
  1173. var expectedLineCount = cm.lineCount();
  1174. var expectedValue = ' word1\nword2\nword3 ';
  1175. helpers.doKeys('<', 'k');
  1176. eq(expectedValue, cm.getValue());
  1177. var register = helpers.getRegisterController().getRegister();
  1178. eq('', register.toString());
  1179. is(!register.linewise);
  1180. helpers.assertCursorAt(0, 1);
  1181. }, { value: ' word1\n word2\nword3 ', indentUnit: 2 });
  1182. testVim('<<', function(cm, vim, helpers) {
  1183. cm.setCursor(0, 3);
  1184. var expectedLineCount = cm.lineCount();
  1185. var expectedValue = ' word1\nword2\nword3 ';
  1186. helpers.doKeys('2', '<', '<');
  1187. eq(expectedValue, cm.getValue());
  1188. var register = helpers.getRegisterController().getRegister();
  1189. eq('', register.toString());
  1190. is(!register.linewise);
  1191. helpers.assertCursorAt(0, 1);
  1192. }, { value: ' word1\n word2\nword3 ', indentUnit: 2 });
  1193. // Edit tests
  1194. function testEdit(name, before, pos, edit, after) {
  1195. return testVim(name, function(cm, vim, helpers) {
  1196. var ch = before.search(pos)
  1197. var line = before.substring(0, ch).split('\n').length - 1;
  1198. if (line) {
  1199. ch = before.substring(0, ch).split('\n').pop().length;
  1200. }
  1201. cm.setCursor(line, ch);
  1202. helpers.doKeys.apply(this, edit.split(''));
  1203. eq(after, cm.getValue());
  1204. }, {value: before});
  1205. }
  1206. // These Delete tests effectively cover word-wise Change, Visual & Yank.
  1207. // Tabs are used as differentiated whitespace to catch edge cases.
  1208. // Normal word:
  1209. testEdit('diw_mid_spc', 'foo \tbAr\t baz', /A/, 'diw', 'foo \t\t baz');
  1210. testEdit('daw_mid_spc', 'foo \tbAr\t baz', /A/, 'daw', 'foo \tbaz');
  1211. testEdit('diw_mid_punct', 'foo \tbAr.\t baz', /A/, 'diw', 'foo \t.\t baz');
  1212. testEdit('daw_mid_punct', 'foo \tbAr.\t baz', /A/, 'daw', 'foo.\t baz');
  1213. testEdit('diw_mid_punct2', 'foo \t,bAr.\t baz', /A/, 'diw', 'foo \t,.\t baz');
  1214. testEdit('daw_mid_punct2', 'foo \t,bAr.\t baz', /A/, 'daw', 'foo \t,.\t baz');
  1215. testEdit('diw_start_spc', 'bAr \tbaz', /A/, 'diw', ' \tbaz');
  1216. testEdit('daw_start_spc', 'bAr \tbaz', /A/, 'daw', 'baz');
  1217. testEdit('diw_start_punct', 'bAr. \tbaz', /A/, 'diw', '. \tbaz');
  1218. testEdit('daw_start_punct', 'bAr. \tbaz', /A/, 'daw', '. \tbaz');
  1219. testEdit('diw_end_spc', 'foo \tbAr', /A/, 'diw', 'foo \t');
  1220. testEdit('daw_end_spc', 'foo \tbAr', /A/, 'daw', 'foo');
  1221. testEdit('diw_end_punct', 'foo \tbAr.', /A/, 'diw', 'foo \t.');
  1222. testEdit('daw_end_punct', 'foo \tbAr.', /A/, 'daw', 'foo.');
  1223. // Big word:
  1224. testEdit('diW_mid_spc', 'foo \tbAr\t baz', /A/, 'diW', 'foo \t\t baz');
  1225. testEdit('daW_mid_spc', 'foo \tbAr\t baz', /A/, 'daW', 'foo \tbaz');
  1226. testEdit('diW_mid_punct', 'foo \tbAr.\t baz', /A/, 'diW', 'foo \t\t baz');
  1227. testEdit('daW_mid_punct', 'foo \tbAr.\t baz', /A/, 'daW', 'foo \tbaz');
  1228. testEdit('diW_mid_punct2', 'foo \t,bAr.\t baz', /A/, 'diW', 'foo \t\t baz');
  1229. testEdit('daW_mid_punct2', 'foo \t,bAr.\t baz', /A/, 'daW', 'foo \tbaz');
  1230. testEdit('diW_start_spc', 'bAr\t baz', /A/, 'diW', '\t baz');
  1231. testEdit('daW_start_spc', 'bAr\t baz', /A/, 'daW', 'baz');
  1232. testEdit('diW_start_punct', 'bAr.\t baz', /A/, 'diW', '\t baz');
  1233. testEdit('daW_start_punct', 'bAr.\t baz', /A/, 'daW', 'baz');
  1234. testEdit('diW_end_spc', 'foo \tbAr', /A/, 'diW', 'foo \t');
  1235. testEdit('daW_end_spc', 'foo \tbAr', /A/, 'daW', 'foo');
  1236. testEdit('diW_end_punct', 'foo \tbAr.', /A/, 'diW', 'foo \t');
  1237. testEdit('daW_end_punct', 'foo \tbAr.', /A/, 'daW', 'foo');
  1238. // Deleting text objects
  1239. // Open and close on same line
  1240. testEdit('di(_open_spc', 'foo (bAr) baz', /\(/, 'di(', 'foo () baz');
  1241. testEdit('di)_open_spc', 'foo (bAr) baz', /\(/, 'di)', 'foo () baz');
  1242. testEdit('dib_open_spc', 'foo (bAr) baz', /\(/, 'dib', 'foo () baz');
  1243. testEdit('da(_open_spc', 'foo (bAr) baz', /\(/, 'da(', 'foo baz');
  1244. testEdit('da)_open_spc', 'foo (bAr) baz', /\(/, 'da)', 'foo baz');
  1245. testEdit('di(_middle_spc', 'foo (bAr) baz', /A/, 'di(', 'foo () baz');
  1246. testEdit('di)_middle_spc', 'foo (bAr) baz', /A/, 'di)', 'foo () baz');
  1247. testEdit('da(_middle_spc', 'foo (bAr) baz', /A/, 'da(', 'foo baz');
  1248. testEdit('da)_middle_spc', 'foo (bAr) baz', /A/, 'da)', 'foo baz');
  1249. testEdit('di(_close_spc', 'foo (bAr) baz', /\)/, 'di(', 'foo () baz');
  1250. testEdit('di)_close_spc', 'foo (bAr) baz', /\)/, 'di)', 'foo () baz');
  1251. testEdit('da(_close_spc', 'foo (bAr) baz', /\)/, 'da(', 'foo baz');
  1252. testEdit('da)_close_spc', 'foo (bAr) baz', /\)/, 'da)', 'foo baz');
  1253. // delete around and inner b.
  1254. testEdit('dab_on_(_should_delete_around_()block', 'o( in(abc) )', /\(a/, 'dab', 'o( in )');
  1255. // delete around and inner B.
  1256. testEdit('daB_on_{_should_delete_around_{}block', 'o{ in{abc} }', /{a/, 'daB', 'o{ in }');
  1257. testEdit('diB_on_{_should_delete_inner_{}block', 'o{ in{abc} }', /{a/, 'diB', 'o{ in{} }');
  1258. testEdit('da{_on_{_should_delete_inner_block', 'o{ in{abc} }', /{a/, 'da{', 'o{ in }');
  1259. testEdit('di[_on_(_should_not_delete', 'foo (bAr) baz', /\(/, 'di[', 'foo (bAr) baz');
  1260. testEdit('di[_on_)_should_not_delete', 'foo (bAr) baz', /\)/, 'di[', 'foo (bAr) baz');
  1261. testEdit('da[_on_(_should_not_delete', 'foo (bAr) baz', /\(/, 'da[', 'foo (bAr) baz');
  1262. testEdit('da[_on_)_should_not_delete', 'foo (bAr) baz', /\)/, 'da[', 'foo (bAr) baz');
  1263. testMotion('di(_outside_should_stay', ['d', 'i', '('], new Pos(0, 0), new Pos(0, 0));
  1264. // Open and close on different lines, equally indented
  1265. testEdit('di{_middle_spc', 'a{\n\tbar\n}b', /r/, 'di{', 'a{}b');
  1266. testEdit('di}_middle_spc', 'a{\n\tbar\n}b', /r/, 'di}', 'a{}b');
  1267. testEdit('da{_middle_spc', 'a{\n\tbar\n}b', /r/, 'da{', 'ab');
  1268. testEdit('da}_middle_spc', 'a{\n\tbar\n}b', /r/, 'da}', 'ab');
  1269. testEdit('daB_middle_spc', 'a{\n\tbar\n}b', /r/, 'daB', 'ab');
  1270. // open and close on diff lines, open indented less than close
  1271. testEdit('di{_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'di{', 'a{}b');
  1272. testEdit('di}_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'di}', 'a{}b');
  1273. testEdit('da{_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'da{', 'ab');
  1274. testEdit('da}_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'da}', 'ab');
  1275. // open and close on diff lines, open indented more than close
  1276. testEdit('di[_middle_spc', 'a\t[\n\tbar\n]b', /r/, 'di[', 'a\t[]b');
  1277. testEdit('di]_middle_spc', 'a\t[\n\tbar\n]b', /r/, 'di]', 'a\t[]b');
  1278. testEdit('da[_middle_spc', 'a\t[\n\tbar\n]b', /r/, 'da[', 'a\tb');
  1279. testEdit('da]_middle_spc', 'a\t[\n\tbar\n]b', /r/, 'da]', 'a\tb');
  1280. function testSelection(name, before, pos, keys, sel) {
  1281. return testVim(name, function(cm, vim, helpers) {
  1282. var ch = before.search(pos)
  1283. var line = before.substring(0, ch).split('\n').length - 1;
  1284. if (line) {
  1285. ch = before.substring(0, ch).split('\n').pop().length;
  1286. }
  1287. cm.setCursor(line, ch);
  1288. helpers.doKeys.apply(this, keys.split(''));
  1289. eq(sel, cm.getSelection());
  1290. }, {value: before});
  1291. }
  1292. testSelection('viw_middle_spc', 'foo \tbAr\t baz', /A/, 'viw', 'bAr');
  1293. testSelection('vaw_middle_spc', 'foo \tbAr\t baz', /A/, 'vaw', 'bAr\t ');
  1294. testSelection('viw_middle_punct', 'foo \tbAr,\t baz', /A/, 'viw', 'bAr');
  1295. testSelection('vaW_middle_punct', 'foo \tbAr,\t baz', /A/, 'vaW', 'bAr,\t ');
  1296. testSelection('viw_start_spc', 'foo \tbAr\t baz', /b/, 'viw', 'bAr');
  1297. testSelection('viw_end_spc', 'foo \tbAr\t baz', /r/, 'viw', 'bAr');
  1298. testSelection('viw_eol', 'foo \tbAr', /r/, 'viw', 'bAr');
  1299. testSelection('vi{_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'vi{', '\n\tbar\n\t');
  1300. testSelection('va{_middle_spc', 'a{\n\tbar\n\t}b', /r/, 'va{', '{\n\tbar\n\t}');
  1301. testVim('mouse_select', function(cm, vim, helpers) {
  1302. cm.setSelection(Pos(0, 2), Pos(0, 4), {origin: '*mouse'});
  1303. is(cm.state.vim.visualMode);
  1304. is(!cm.state.vim.visualLine);
  1305. is(!cm.state.vim.visualBlock);
  1306. helpers.doKeys('<Esc>');
  1307. is(!cm.somethingSelected());
  1308. helpers.doKeys('g', 'v');
  1309. eq('cd', cm.getSelection());
  1310. }, {value: 'abcdef'});
  1311. // Operator-motion tests
  1312. testVim('D', function(cm, vim, helpers) {
  1313. cm.setCursor(0, 3);
  1314. helpers.doKeys('D');
  1315. eq(' wo\nword2\n word3', cm.getValue());
  1316. var register = helpers.getRegisterController().getRegister();
  1317. eq('rd1', register.toString());
  1318. is(!register.linewise);
  1319. helpers.assertCursorAt(0, 2);
  1320. }, { value: ' word1\nword2\n word3' });
  1321. testVim('C', function(cm, vim, helpers) {
  1322. var curStart = makeCursor(0, 3);
  1323. cm.setCursor(curStart);
  1324. helpers.doKeys('C');
  1325. eq(' wo\nword2\n word3', cm.getValue());
  1326. var register = helpers.getRegisterController().getRegister();
  1327. eq('rd1', register.toString());
  1328. is(!register.linewise);
  1329. eqCursorPos(curStart, cm.getCursor());
  1330. eq('vim-insert', cm.getOption('keyMap'));
  1331. }, { value: ' word1\nword2\n word3' });
  1332. testVim('Y', function(cm, vim, helpers) {
  1333. var curStart = makeCursor(0, 3);
  1334. cm.setCursor(curStart);
  1335. helpers.doKeys('Y');
  1336. eq(' word1\nword2\n word3', cm.getValue());
  1337. var register = helpers.getRegisterController().getRegister();
  1338. eq(' word1\n', register.toString());
  1339. is(register.linewise);
  1340. helpers.assertCursorAt(0, 3);
  1341. }, { value: ' word1\nword2\n word3' });
  1342. testVim('~', function(cm, vim, helpers) {
  1343. helpers.doKeys('3', '~');
  1344. eq('ABCdefg', cm.getValue());
  1345. helpers.assertCursorAt(0, 3);
  1346. }, { value: 'abcdefg' });
  1347. // Action tests
  1348. testVim('ctrl-a', function(cm, vim, helpers) {
  1349. cm.setCursor(0, 0);
  1350. helpers.doKeys('<C-a>');
  1351. eq('-9', cm.getValue());
  1352. helpers.assertCursorAt(0, 1);
  1353. helpers.doKeys('2','<C-a>');
  1354. eq('-7', cm.getValue());
  1355. }, {value: '-10'});
  1356. testVim('ctrl-x', function(cm, vim, helpers) {
  1357. cm.setCursor(0, 0);
  1358. helpers.doKeys('<C-x>');
  1359. eq('-1', cm.getValue());
  1360. helpers.assertCursorAt(0, 1);
  1361. helpers.doKeys('2','<C-x>');
  1362. eq('-3', cm.getValue());
  1363. }, {value: '0'});
  1364. testVim('<C-x>/<C-a> search forward', function(cm, vim, helpers) {
  1365. forEach(['<C-x>', '<C-a>'], function(key) {
  1366. cm.setCursor(0, 0);
  1367. helpers.doKeys(key);
  1368. helpers.assertCursorAt(0, 5);
  1369. helpers.doKeys('l');
  1370. helpers.doKeys(key);
  1371. helpers.assertCursorAt(0, 10);
  1372. cm.setCursor(0, 11);
  1373. helpers.doKeys(key);
  1374. helpers.assertCursorAt(0, 11);
  1375. });
  1376. }, {value: '__jmp1 jmp2 jmp'});
  1377. testVim('insert_ctrl_w', function(cm, vim, helpers) {
  1378. var curStart = makeCursor(0, 10);
  1379. cm.setCursor(curStart);
  1380. helpers.doKeys('a');
  1381. helpers.doKeys('<C-w>');
  1382. eq('word1/', cm.getValue());
  1383. var register = helpers.getRegisterController().getRegister();
  1384. eq('word2', register.toString());
  1385. is(!register.linewise);
  1386. var curEnd = makeCursor(0, 6);
  1387. eqCursorPos(curEnd, cm.getCursor());
  1388. eq('vim-insert', cm.getOption('keyMap'));
  1389. }, { value: 'word1/word2' });
  1390. testVim('a', function(cm, vim, helpers) {
  1391. cm.setCursor(0, 1);
  1392. helpers.doKeys('a');
  1393. helpers.assertCursorAt(0, 2);
  1394. eq('vim-insert', cm.getOption('keyMap'));
  1395. });
  1396. testVim('a_eol', function(cm, vim, helpers) {
  1397. cm.setCursor(0, lines[0].length - 1);
  1398. helpers.doKeys('a');
  1399. helpers.assertCursorAt(0, lines[0].length);
  1400. eq('vim-insert', cm.getOption('keyMap'));
  1401. });
  1402. testVim('A_endOfSelectedArea', function(cm, vim, helpers) {
  1403. cm.setCursor(0, 0);
  1404. helpers.doKeys('v', 'j', 'l');
  1405. helpers.doKeys('A');
  1406. helpers.assertCursorAt(1, 2);
  1407. eq('vim-insert', cm.getOption('keyMap'));
  1408. }, {value: 'foo\nbar'});
  1409. testVim('i', function(cm, vim, helpers) {
  1410. cm.setCursor(0, 1);
  1411. helpers.doKeys('i');
  1412. helpers.assertCursorAt(0, 1);
  1413. eq('vim-insert', cm.getOption('keyMap'));
  1414. });
  1415. testVim('i_repeat', function(cm, vim, helpers) {
  1416. helpers.doKeys('3', 'i');
  1417. cm.replaceRange('test', cm.getCursor());
  1418. helpers.doKeys('<Esc>');
  1419. eq('testtesttest', cm.getValue());
  1420. helpers.assertCursorAt(0, 11);
  1421. }, { value: '' });
  1422. testVim('i_repeat_delete', function(cm, vim, helpers) {
  1423. cm.setCursor(0, 4);
  1424. helpers.doKeys('2', 'i');
  1425. cm.replaceRange('z', cm.getCursor());
  1426. helpers.doInsertModeKeys('Backspace', 'Backspace');
  1427. helpers.doKeys('<Esc>');
  1428. eq('abe', cm.getValue());
  1429. helpers.assertCursorAt(0, 1);
  1430. }, { value: 'abcde' });
  1431. testVim('insert', function(cm, vim, helpers) {
  1432. helpers.doKeys('i');
  1433. eq('vim-insert', cm.getOption('keyMap'));
  1434. eq(false, cm.state.overwrite);
  1435. helpers.doKeys('<Ins>');
  1436. eq('vim-replace', cm.getOption('keyMap'));
  1437. eq(true, cm.state.overwrite);
  1438. helpers.doKeys('<Ins>');
  1439. eq('vim-insert', cm.getOption('keyMap'));
  1440. eq(false, cm.state.overwrite);
  1441. });
  1442. testVim('i_backspace', function(cm, vim, helpers) {
  1443. cm.setCursor(0, 10);
  1444. helpers.doKeys('i');
  1445. helpers.doInsertModeKeys('Backspace');
  1446. helpers.assertCursorAt(0, 9);
  1447. eq('012345678', cm.getValue());
  1448. }, { value: '0123456789'});
  1449. testVim('i_overwrite_backspace', function(cm, vim, helpers) {
  1450. cm.setCursor(0, 10);
  1451. helpers.doKeys('i');
  1452. helpers.doKeys('<Ins>');
  1453. helpers.doInsertModeKeys('Backspace');
  1454. helpers.assertCursorAt(Pos(0, 9, "after"));
  1455. eq('0123456789', cm.getValue());
  1456. }, { value: '0123456789'});
  1457. testVim('A', function(cm, vim, helpers) {
  1458. helpers.doKeys('A');
  1459. helpers.assertCursorAt(0, lines[0].length);
  1460. eq('vim-insert', cm.getOption('keyMap'));
  1461. });
  1462. testVim('A_visual_block', function(cm, vim, helpers) {
  1463. cm.setCursor(0, 1);
  1464. helpers.doKeys('<C-v>', '2', 'j', 'l', 'l', 'A');
  1465. var replacement = new Array(cm.listSelections().length+1).join('hello ').split(' ');
  1466. replacement.pop();
  1467. cm.replaceSelections(replacement);
  1468. eq('testhello\nmehello\npleahellose', cm.getValue());
  1469. helpers.doKeys('<Esc>');
  1470. cm.setCursor(0, 0);
  1471. helpers.doKeys('.');
  1472. // TODO this doesn't work yet
  1473. // eq('teshellothello\nme hello hello\nplehelloahellose', cm.getValue());
  1474. }, {value: 'test\nme\nplease'});
  1475. testVim('I', function(cm, vim, helpers) {
  1476. cm.setCursor(0, 4);
  1477. helpers.doKeys('I');
  1478. helpers.assertCursorAt(0, lines[0].textStart);
  1479. eq('vim-insert', cm.getOption('keyMap'));
  1480. });
  1481. testVim('I_repeat', function(cm, vim, helpers) {
  1482. cm.setCursor(0, 1);
  1483. helpers.doKeys('3', 'I');
  1484. cm.replaceRange('test', cm.getCursor());
  1485. helpers.doKeys('<Esc>');
  1486. eq('testtesttestblah', cm.getValue());
  1487. helpers.assertCursorAt(0, 11);
  1488. }, { value: 'blah' });
  1489. testVim('I_visual_block', function(cm, vim, helpers) {
  1490. cm.setCursor(0, 0);
  1491. helpers.doKeys('<C-v>', '2', 'j', 'l', 'l', 'I');
  1492. var replacement = new Array(cm.listSelections().length+1).join('hello ').split(' ');
  1493. replacement.pop();
  1494. cm.replaceSelections(replacement);
  1495. eq('hellotest\nhellome\nhelloplease', cm.getValue());
  1496. }, {value: 'test\nme\nplease'});
  1497. testVim('o', function(cm, vim, helpers) {
  1498. cm.setCursor(0, 4);
  1499. helpers.doKeys('o');
  1500. eq('word1\n\nword2', cm.getValue());
  1501. helpers.assertCursorAt(1, 0);
  1502. eq('vim-insert', cm.getOption('keyMap'));
  1503. }, { value: 'word1\nword2' });
  1504. testVim('o_repeat', function(cm, vim, helpers) {
  1505. cm.setCursor(0, 0);
  1506. helpers.doKeys('3', 'o');
  1507. cm.replaceRange('test', cm.getCursor());
  1508. helpers.doKeys('<Esc>');
  1509. eq('\ntest\ntest\ntest', cm.getValue());
  1510. helpers.assertCursorAt(3, 3);
  1511. }, { value: '' });
  1512. testVim('O', function(cm, vim, helpers) {
  1513. cm.setCursor(0, 4);
  1514. helpers.doKeys('O');
  1515. eq('\nword1\nword2', cm.getValue());
  1516. helpers.assertCursorAt(0, 0);
  1517. eq('vim-insert', cm.getOption('keyMap'));
  1518. }, { value: 'word1\nword2' });
  1519. testVim('J', function(cm, vim, helpers) {
  1520. cm.setCursor(0, 4);
  1521. helpers.doKeys('J');
  1522. var expectedValue = 'word1 word2\nword3\n word4';
  1523. eq(expectedValue, cm.getValue());
  1524. helpers.assertCursorAt(0, expectedValue.indexOf('word2') - 1);
  1525. }, { value: 'word1 \n word2\nword3\n word4' });
  1526. testVim('J_repeat', function(cm, vim, helpers) {
  1527. cm.setCursor(0, 4);
  1528. helpers.doKeys('3', 'J');
  1529. var expectedValue = 'word1 word2 word3\n word4';
  1530. eq(expectedValue, cm.getValue());
  1531. helpers.assertCursorAt(0, expectedValue.indexOf('word3') - 1);
  1532. }, { value: 'word1 \n word2\nword3\n word4' });
  1533. testVim('p', function(cm, vim, helpers) {
  1534. cm.setCursor(0, 1);
  1535. helpers.getRegisterController().pushText('"', 'yank', 'abc\ndef', false);
  1536. helpers.doKeys('p');
  1537. eq('__abc\ndef_', cm.getValue());
  1538. helpers.assertCursorAt(1, 2);
  1539. }, { value: '___' });
  1540. testVim('p_register', function(cm, vim, helpers) {
  1541. cm.setCursor(0, 1);
  1542. helpers.getRegisterController().getRegister('a').setText('abc\ndef', false);
  1543. helpers.doKeys('"', 'a', 'p');
  1544. eq('__abc\ndef_', cm.getValue());
  1545. helpers.assertCursorAt(1, 2);
  1546. }, { value: '___' });
  1547. testVim('p_wrong_register', function(cm, vim, helpers) {
  1548. cm.setCursor(0, 1);
  1549. helpers.getRegisterController().getRegister('a').setText('abc\ndef', false);
  1550. helpers.doKeys('p');
  1551. eq('___', cm.getValue());
  1552. helpers.assertCursorAt(0, 1);
  1553. }, { value: '___' });
  1554. testVim('p_line', function(cm, vim, helpers) {
  1555. cm.setCursor(0, 1);
  1556. helpers.getRegisterController().pushText('"', 'yank', ' a\nd\n', true);
  1557. helpers.doKeys('2', 'p');
  1558. eq('___\n a\nd\n a\nd', cm.getValue());
  1559. helpers.assertCursorAt(1, 2);
  1560. }, { value: '___' });
  1561. testVim('p_lastline', function(cm, vim, helpers) {
  1562. cm.setCursor(0, 1);
  1563. helpers.getRegisterController().pushText('"', 'yank', ' a\nd', true);
  1564. helpers.doKeys('2', 'p');
  1565. eq('___\n a\nd\n a\nd', cm.getValue());
  1566. helpers.assertCursorAt(1, 2);
  1567. }, { value: '___' });
  1568. testVim(']p_first_indent_is_smaller', function(cm, vim, helpers) {
  1569. helpers.getRegisterController().pushText('"', 'yank', ' abc\n def\n', true);
  1570. helpers.doKeys(']', 'p');
  1571. eq(' ___\n abc\n def', cm.getValue());
  1572. }, { value: ' ___' });
  1573. testVim(']p_first_indent_is_larger', function(cm, vim, helpers) {
  1574. helpers.getRegisterController().pushText('"', 'yank', ' abc\n def\n', true);
  1575. helpers.doKeys(']', 'p');
  1576. eq(' ___\n abc\ndef', cm.getValue());
  1577. }, { value: ' ___' });
  1578. testVim(']p_with_tab_indents', function(cm, vim, helpers) {
  1579. helpers.getRegisterController().pushText('"', 'yank', '\t\tabc\n\t\t\tdef\n', true);
  1580. helpers.doKeys(']', 'p');
  1581. eq('\t___\n\tabc\n\t\tdef', cm.getValue());
  1582. }, { value: '\t___', indentWithTabs: true});
  1583. testVim(']p_with_spaces_translated_to_tabs', function(cm, vim, helpers) {
  1584. helpers.getRegisterController().pushText('"', 'yank', ' abc\n def\n', true);
  1585. helpers.doKeys(']', 'p');
  1586. eq('\t___\n\tabc\n\t\tdef', cm.getValue());
  1587. }, { value: '\t___', indentWithTabs: true, tabSize: 2 });
  1588. testVim('[p', function(cm, vim, helpers) {
  1589. helpers.getRegisterController().pushText('"', 'yank', ' abc\n def\n', true);
  1590. helpers.doKeys('[', 'p');
  1591. eq(' abc\n def\n ___', cm.getValue());
  1592. }, { value: ' ___' });
  1593. testVim('P', function(cm, vim, helpers) {
  1594. cm.setCursor(0, 1);
  1595. helpers.getRegisterController().pushText('"', 'yank', 'abc\ndef', false);
  1596. helpers.doKeys('P');
  1597. eq('_abc\ndef__', cm.getValue());
  1598. helpers.assertCursorAt(1, 3);
  1599. }, { value: '___' });
  1600. testVim('P_line', function(cm, vim, helpers) {
  1601. cm.setCursor(0, 1);
  1602. helpers.getRegisterController().pushText('"', 'yank', ' a\nd\n', true);
  1603. helpers.doKeys('2', 'P');
  1604. eq(' a\nd\n a\nd\n___', cm.getValue());
  1605. helpers.assertCursorAt(0, 2);
  1606. }, { value: '___' });
  1607. testVim('r', function(cm, vim, helpers) {
  1608. cm.setCursor(0, 1);
  1609. helpers.doKeys('3', 'r', 'u');
  1610. eq('wuuuet\nanother', cm.getValue(),'3r failed');
  1611. helpers.assertCursorAt(0, 3);
  1612. cm.setCursor(0, 4);
  1613. helpers.doKeys('v', 'j', 'h', 'r', '<Space>');
  1614. eq('wuuu \n her', cm.getValue(),'Replacing selection by space-characters failed');
  1615. cm.setValue("ox");
  1616. helpers.doKeys('r', '<C-c>');
  1617. eq('ox', cm.getValue());
  1618. helpers.doKeys('r', '<Del>');
  1619. eq('ox', cm.getValue());
  1620. helpers.doKeys('r', '<CR>');
  1621. eq('\nx', cm.getValue());
  1622. }, { value: 'wordet\nanother' });
  1623. testVim('r_visual_block', function(cm, vim, helpers) {
  1624. cm.setCursor(2, 3);
  1625. helpers.doKeys('<C-v>', 'k', 'k', 'h', 'h', 'r', 'l');
  1626. eq('1lll\n5lll\nalllefg', cm.getValue());
  1627. helpers.doKeys('<C-v>', 'l', 'j', 'r', '<Space>');
  1628. eq('1 l\n5 l\nalllefg', cm.getValue());
  1629. cm.setCursor(2, 0);
  1630. helpers.doKeys('o');
  1631. helpers.doKeys('<Esc>');
  1632. cm.replaceRange('\t\t', cm.getCursor());
  1633. helpers.doKeys('<C-v>', 'h', 'h', 'r', 'r');
  1634. eq('1 l\n5 l\nalllefg\nrrrrrrrr', cm.getValue());
  1635. }, {value: '1234\n5678\nabcdefg'});
  1636. testVim('R', function(cm, vim, helpers) {
  1637. cm.setCursor(0, 1);
  1638. helpers.doKeys('R');
  1639. helpers.assertCursorAt(0, 1);
  1640. eq('vim-replace', cm.getOption('keyMap'));
  1641. is(cm.state.overwrite, 'Setting overwrite state failed');
  1642. });
  1643. testVim('mark', function(cm, vim, helpers) {
  1644. cm.setCursor(2, 2);
  1645. helpers.doKeys('m', 't');
  1646. cm.setCursor(0, 0);
  1647. helpers.doKeys('`', 't');
  1648. helpers.assertCursorAt(2, 2);
  1649. cm.setCursor(2, 0);
  1650. cm.replaceRange(' h', cm.getCursor());
  1651. cm.setCursor(0, 0);
  1652. helpers.doKeys('\'', 't');
  1653. helpers.assertCursorAt(2, 3);
  1654. });
  1655. testVim('mark\'', function(cm, vim, helpers) {
  1656. cm.setCursor(2, 2);
  1657. cm.setCursor(0, 0);
  1658. helpers.doKeys('`', '\'');
  1659. helpers.assertCursorAt(2, 2);
  1660. cm.setCursor(2, 0);
  1661. cm.replaceRange(' h', cm.getCursor());
  1662. cm.setCursor(0, 0);
  1663. helpers.doKeys('\'', '\'');
  1664. helpers.assertCursorAt(2, 3);
  1665. });
  1666. testVim('mark.', function(cm, vim, helpers) {
  1667. cm.setCursor(0, 0);
  1668. helpers.doKeys('O', 'testing', '<Esc>');
  1669. cm.setCursor(3, 3);
  1670. helpers.doKeys('\'', '.');
  1671. helpers.assertCursorAt(0, 0);
  1672. cm.setCursor(4, 4);
  1673. helpers.doKeys('`', '.');
  1674. helpers.assertCursorAt(0, 6);
  1675. });
  1676. testVim('jumpToMark_next', function(cm, vim, helpers) {
  1677. cm.setCursor(2, 2);
  1678. helpers.doKeys('m', 't');
  1679. cm.setCursor(0, 0);
  1680. helpers.doKeys(']', '`');
  1681. helpers.assertCursorAt(2, 2);
  1682. cm.setCursor(0, 0);
  1683. helpers.doKeys(']', '\'');
  1684. helpers.assertCursorAt(2, 0);
  1685. });
  1686. testVim('jumpToMark_next_repeat', function(cm, vim, helpers) {
  1687. cm.setCursor(2, 2);
  1688. helpers.doKeys('m', 'a');
  1689. cm.setCursor(3, 2);
  1690. helpers.doKeys('m', 'b');
  1691. cm.setCursor(4, 2);
  1692. helpers.doKeys('m', 'c');
  1693. cm.setCursor(0, 0);
  1694. helpers.doKeys('2', ']', '`');
  1695. helpers.assertCursorAt(3, 2);
  1696. cm.setCursor(0, 0);
  1697. helpers.doKeys('2', ']', '\'');
  1698. helpers.assertCursorAt(3, 1);
  1699. });
  1700. testVim('jumpToMark_next_sameline', function(cm, vim, helpers) {
  1701. cm.setCursor(2, 0);
  1702. helpers.doKeys('m', 'a');
  1703. cm.setCursor(2, 4);
  1704. helpers.doKeys('m', 'b');
  1705. cm.setCursor(2, 2);
  1706. helpers.doKeys(']', '`');
  1707. helpers.assertCursorAt(2, 4);
  1708. });
  1709. testVim('jumpToMark_next_onlyprev', function(cm, vim, helpers) {
  1710. cm.setCursor(2, 0);
  1711. helpers.doKeys('m', 'a');
  1712. cm.setCursor(4, 0);
  1713. helpers.doKeys(']', '`');
  1714. helpers.assertCursorAt(4, 0);
  1715. });
  1716. testVim('jumpToMark_next_nomark', function(cm, vim, helpers) {
  1717. cm.setCursor(2, 2);
  1718. helpers.doKeys(']', '`');
  1719. helpers.assertCursorAt(2, 2);
  1720. helpers.doKeys(']', '\'');
  1721. helpers.assertCursorAt(2, 0);
  1722. });
  1723. testVim('jumpToMark_next_linewise_over', function(cm, vim, helpers) {
  1724. cm.setCursor(2, 2);
  1725. helpers.doKeys('m', 'a');
  1726. cm.setCursor(3, 4);
  1727. helpers.doKeys('m', 'b');
  1728. cm.setCursor(2, 1);
  1729. helpers.doKeys(']', '\'');
  1730. helpers.assertCursorAt(3, 1);
  1731. });
  1732. testVim('jumpToMark_next_action', function(cm, vim, helpers) {
  1733. cm.setCursor(2, 2);
  1734. helpers.doKeys('m', 't');
  1735. cm.setCursor(0, 0);
  1736. helpers.doKeys('d', ']', '`');
  1737. helpers.assertCursorAt(0, 0);
  1738. var actual = cm.getLine(0);
  1739. var expected = 'pop pop 0 1 2 3 4';
  1740. eq(actual, expected, "Deleting while jumping to the next mark failed.");
  1741. });
  1742. testVim('jumpToMark_next_line_action', function(cm, vim, helpers) {
  1743. cm.setCursor(2, 2);
  1744. helpers.doKeys('m', 't');
  1745. cm.setCursor(0, 0);
  1746. helpers.doKeys('d', ']', '\'');
  1747. helpers.assertCursorAt(0, 1);
  1748. var actual = cm.getLine(0);
  1749. var expected = ' (a) [b] {c} '
  1750. eq(actual, expected, "Deleting while jumping to the next mark line failed.");
  1751. });
  1752. testVim('jumpToMark_prev', function(cm, vim, helpers) {
  1753. cm.setCursor(2, 2);
  1754. helpers.doKeys('m', 't');
  1755. cm.setCursor(4, 0);
  1756. helpers.doKeys('[', '`');
  1757. helpers.assertCursorAt(2, 2);
  1758. cm.setCursor(4, 0);
  1759. helpers.doKeys('[', '\'');
  1760. helpers.assertCursorAt(2, 0);
  1761. });
  1762. testVim('jumpToMark_prev_repeat', function(cm, vim, helpers) {
  1763. cm.setCursor(2, 2);
  1764. helpers.doKeys('m', 'a');
  1765. cm.setCursor(3, 2);
  1766. helpers.doKeys('m', 'b');
  1767. cm.setCursor(4, 2);
  1768. helpers.doKeys('m', 'c');
  1769. cm.setCursor(5, 0);
  1770. helpers.doKeys('2', '[', '`');
  1771. helpers.assertCursorAt(3, 2);
  1772. cm.setCursor(5, 0);
  1773. helpers.doKeys('2', '[', '\'');
  1774. helpers.assertCursorAt(3, 1);
  1775. });
  1776. testVim('jumpToMark_prev_sameline', function(cm, vim, helpers) {
  1777. cm.setCursor(2, 0);
  1778. helpers.doKeys('m', 'a');
  1779. cm.setCursor(2, 4);
  1780. helpers.doKeys('m', 'b');
  1781. cm.setCursor(2, 2);
  1782. helpers.doKeys('[', '`');
  1783. helpers.assertCursorAt(2, 0);
  1784. });
  1785. testVim('jumpToMark_prev_onlynext', function(cm, vim, helpers) {
  1786. cm.setCursor(4, 4);
  1787. helpers.doKeys('m', 'a');
  1788. cm.setCursor(2, 0);
  1789. helpers.doKeys('[', '`');
  1790. helpers.assertCursorAt(2, 0);
  1791. });
  1792. testVim('jumpToMark_prev_nomark', function(cm, vim, helpers) {
  1793. cm.setCursor(2, 2);
  1794. helpers.doKeys('[', '`');
  1795. helpers.assertCursorAt(2, 2);
  1796. helpers.doKeys('[', '\'');
  1797. helpers.assertCursorAt(2, 0);
  1798. });
  1799. testVim('jumpToMark_prev_linewise_over', function(cm, vim, helpers) {
  1800. cm.setCursor(2, 2);
  1801. helpers.doKeys('m', 'a');
  1802. cm.setCursor(3, 4);
  1803. helpers.doKeys('m', 'b');
  1804. cm.setCursor(3, 6);
  1805. helpers.doKeys('[', '\'');
  1806. helpers.assertCursorAt(2, 0);
  1807. });
  1808. testVim('delmark_single', function(cm, vim, helpers) {
  1809. cm.setCursor(1, 2);
  1810. helpers.doKeys('m', 't');
  1811. helpers.doEx('delmarks t');
  1812. cm.setCursor(0, 0);
  1813. helpers.doKeys('`', 't');
  1814. helpers.assertCursorAt(0, 0);
  1815. });
  1816. testVim('delmark_range', function(cm, vim, helpers) {
  1817. cm.setCursor(1, 2);
  1818. helpers.doKeys('m', 'a');
  1819. cm.setCursor(2, 2);
  1820. helpers.doKeys('m', 'b');
  1821. cm.setCursor(3, 2);
  1822. helpers.doKeys('m', 'c');
  1823. cm.setCursor(4, 2);
  1824. helpers.doKeys('m', 'd');
  1825. cm.setCursor(5, 2);
  1826. helpers.doKeys('m', 'e');
  1827. helpers.doEx('delmarks b-d');
  1828. cm.setCursor(0, 0);
  1829. helpers.doKeys('`', 'a');
  1830. helpers.assertCursorAt(1, 2);
  1831. helpers.doKeys('`', 'b');
  1832. helpers.assertCursorAt(1, 2);
  1833. helpers.doKeys('`', 'c');
  1834. helpers.assertCursorAt(1, 2);
  1835. helpers.doKeys('`', 'd');
  1836. helpers.assertCursorAt(1, 2);
  1837. helpers.doKeys('`', 'e');
  1838. helpers.assertCursorAt(5, 2);
  1839. });
  1840. testVim('delmark_multi', function(cm, vim, helpers) {
  1841. cm.setCursor(1, 2);
  1842. helpers.doKeys('m', 'a');
  1843. cm.setCursor(2, 2);
  1844. helpers.doKeys('m', 'b');
  1845. cm.setCursor(3, 2);
  1846. helpers.doKeys('m', 'c');
  1847. cm.setCursor(4, 2);
  1848. helpers.doKeys('m', 'd');
  1849. cm.setCursor(5, 2);
  1850. helpers.doKeys('m', 'e');
  1851. helpers.doEx('delmarks bcd');
  1852. cm.setCursor(0, 0);
  1853. helpers.doKeys('`', 'a');
  1854. helpers.assertCursorAt(1, 2);
  1855. helpers.doKeys('`', 'b');
  1856. helpers.assertCursorAt(1, 2);
  1857. helpers.doKeys('`', 'c');
  1858. helpers.assertCursorAt(1, 2);
  1859. helpers.doKeys('`', 'd');
  1860. helpers.assertCursorAt(1, 2);
  1861. helpers.doKeys('`', 'e');
  1862. helpers.assertCursorAt(5, 2);
  1863. });
  1864. testVim('delmark_multi_space', function(cm, vim, helpers) {
  1865. cm.setCursor(1, 2);
  1866. helpers.doKeys('m', 'a');
  1867. cm.setCursor(2, 2);
  1868. helpers.doKeys('m', 'b');
  1869. cm.setCursor(3, 2);
  1870. helpers.doKeys('m', 'c');
  1871. cm.setCursor(4, 2);
  1872. helpers.doKeys('m', 'd');
  1873. cm.setCursor(5, 2);
  1874. helpers.doKeys('m', 'e');
  1875. helpers.doEx('delmarks b c d');
  1876. cm.setCursor(0, 0);
  1877. helpers.doKeys('`', 'a');
  1878. helpers.assertCursorAt(1, 2);
  1879. helpers.doKeys('`', 'b');
  1880. helpers.assertCursorAt(1, 2);
  1881. helpers.doKeys('`', 'c');
  1882. helpers.assertCursorAt(1, 2);
  1883. helpers.doKeys('`', 'd');
  1884. helpers.assertCursorAt(1, 2);
  1885. helpers.doKeys('`', 'e');
  1886. helpers.assertCursorAt(5, 2);
  1887. });
  1888. testVim('delmark_all', function(cm, vim, helpers) {
  1889. cm.setCursor(1, 2);
  1890. helpers.doKeys('m', 'a');
  1891. cm.setCursor(2, 2);
  1892. helpers.doKeys('m', 'b');
  1893. cm.setCursor(3, 2);
  1894. helpers.doKeys('m', 'c');
  1895. cm.setCursor(4, 2);
  1896. helpers.doKeys('m', 'd');
  1897. cm.setCursor(5, 2);
  1898. helpers.doKeys('m', 'e');
  1899. helpers.doEx('delmarks a b-de');
  1900. cm.setCursor(0, 0);
  1901. helpers.doKeys('`', 'a');
  1902. helpers.assertCursorAt(0, 0);
  1903. helpers.doKeys('`', 'b');
  1904. helpers.assertCursorAt(0, 0);
  1905. helpers.doKeys('`', 'c');
  1906. helpers.assertCursorAt(0, 0);
  1907. helpers.doKeys('`', 'd');
  1908. helpers.assertCursorAt(0, 0);
  1909. helpers.doKeys('`', 'e');
  1910. helpers.assertCursorAt(0, 0);
  1911. });
  1912. testVim('visual', function(cm, vim, helpers) {
  1913. helpers.doKeys('l', 'v', 'l', 'l');
  1914. helpers.assertCursorAt(0, 4);
  1915. eqCursorPos(makeCursor(0, 1), cm.getCursor('anchor'));
  1916. helpers.doKeys('d');
  1917. eq('15', cm.getValue());
  1918. }, { value: '12345' });
  1919. testVim('visual_yank', function(cm, vim, helpers) {
  1920. helpers.doKeys('v', '3', 'l', 'y');
  1921. helpers.assertCursorAt(0, 0);
  1922. helpers.doKeys('p');
  1923. eq('aa te test for yank', cm.getValue());
  1924. }, { value: 'a test for yank' })
  1925. testVim('visual_w', function(cm, vim, helpers) {
  1926. helpers.doKeys('v', 'w');
  1927. eq(cm.getSelection(), 'motion t');
  1928. }, { value: 'motion test'});
  1929. testVim('visual_initial_selection', function(cm, vim, helpers) {
  1930. cm.setCursor(0, 1);
  1931. helpers.doKeys('v');
  1932. cm.getSelection('n');
  1933. }, { value: 'init'});
  1934. testVim('visual_crossover_left', function(cm, vim, helpers) {
  1935. cm.setCursor(0, 2);
  1936. helpers.doKeys('v', 'l', 'h', 'h');
  1937. cm.getSelection('ro');
  1938. }, { value: 'cross'});
  1939. testVim('visual_crossover_left', function(cm, vim, helpers) {
  1940. cm.setCursor(0, 2);
  1941. helpers.doKeys('v', 'h', 'l', 'l');
  1942. cm.getSelection('os');
  1943. }, { value: 'cross'});
  1944. testVim('visual_crossover_up', function(cm, vim, helpers) {
  1945. cm.setCursor(3, 2);
  1946. helpers.doKeys('v', 'j', 'k', 'k');
  1947. eqCursorPos(Pos(2, 2), cm.getCursor('head'));
  1948. eqCursorPos(Pos(3, 3), cm.getCursor('anchor'));
  1949. helpers.doKeys('k');
  1950. eqCursorPos(Pos(1, 2), cm.getCursor('head'));
  1951. eqCursorPos(Pos(3, 3), cm.getCursor('anchor'));
  1952. }, { value: 'cross\ncross\ncross\ncross\ncross\n'});
  1953. testVim('visual_crossover_down', function(cm, vim, helpers) {
  1954. cm.setCursor(1, 2);
  1955. helpers.doKeys('v', 'k', 'j', 'j');
  1956. eqCursorPos(Pos(2, 3), cm.getCursor('head'));
  1957. eqCursorPos(Pos(1, 2), cm.getCursor('anchor'));
  1958. helpers.doKeys('j');
  1959. eqCursorPos(Pos(3, 3), cm.getCursor('head'));
  1960. eqCursorPos(Pos(1, 2), cm.getCursor('anchor'));
  1961. }, { value: 'cross\ncross\ncross\ncross\ncross\n'});
  1962. testVim('visual_exit', function(cm, vim, helpers) {
  1963. helpers.doKeys('<C-v>', 'l', 'j', 'j', '<Esc>');
  1964. eqCursorPos(cm.getCursor('anchor'), cm.getCursor('head'));
  1965. eq(vim.visualMode, false);
  1966. }, { value: 'hello\nworld\nfoo' });
  1967. testVim('visual_line', function(cm, vim, helpers) {
  1968. helpers.doKeys('l', 'V', 'l', 'j', 'j', 'd');
  1969. eq(' 4\n 5', cm.getValue());
  1970. }, { value: ' 1\n 2\n 3\n 4\n 5' });
  1971. testVim('visual_block_move_to_eol', function(cm, vim, helpers) {
  1972. // moveToEol should move all block cursors to end of line
  1973. cm.setCursor(0, 0);
  1974. helpers.doKeys('<C-v>', 'G', '$');
  1975. var selections = cm.getSelections().join();
  1976. eq('123,45,6', selections);
  1977. // Checks that with cursor at Infinity, finding words backwards still works.
  1978. helpers.doKeys('2', 'k', 'b');
  1979. selections = cm.getSelections().join();
  1980. eq('1', selections);
  1981. }, {value: '123\n45\n6'});
  1982. testVim('visual_block_different_line_lengths', function(cm, vim, helpers) {
  1983. // test the block selection with lines of different length
  1984. // i.e. extending the selection
  1985. // till the end of the longest line.
  1986. helpers.doKeys('<C-v>', 'l', 'j', 'j', '6', 'l', 'd');
  1987. helpers.doKeys('d', 'd', 'd', 'd');
  1988. eq('', cm.getValue());
  1989. }, {value: '1234\n5678\nabcdefg'});
  1990. testVim('visual_block_truncate_on_short_line', function(cm, vim, helpers) {
  1991. // check for left side selection in case
  1992. // of moving up to a shorter line.
  1993. cm.replaceRange('', cm.getCursor());
  1994. cm.setCursor(3, 4);
  1995. helpers.doKeys('<C-v>', 'l', 'k', 'k', 'd');
  1996. eq('hello world\n{\ntis\nsa!', cm.getValue());
  1997. }, {value: 'hello world\n{\nthis is\nsparta!'});
  1998. testVim('visual_block_corners', function(cm, vim, helpers) {
  1999. cm.setCursor(1, 2);
  2000. helpers.doKeys('<C-v>', '2', 'l', 'k');
  2001. // circle around the anchor
  2002. // and check the selections
  2003. var selections = cm.getSelections();
  2004. eq('345891', selections.join(''));
  2005. helpers.doKeys('4', 'h');
  2006. selections = cm.getSelections();
  2007. eq('123678', selections.join(''));
  2008. helpers.doKeys('j', 'j');
  2009. selections = cm.getSelections();
  2010. eq('678abc', selections.join(''));
  2011. helpers.doKeys('4', 'l');
  2012. selections = cm.getSelections();
  2013. eq('891cde', selections.join(''));
  2014. }, {value: '12345\n67891\nabcde'});
  2015. testVim('visual_block_mode_switch', function(cm, vim, helpers) {
  2016. // switch between visual modes
  2017. cm.setCursor(1, 1);
  2018. // blockwise to characterwise visual
  2019. helpers.doKeys('<C-v>', 'j', 'l', 'v');
  2020. var selections = cm.getSelections();
  2021. eq('7891\nabc', selections.join(''));
  2022. // characterwise to blockwise
  2023. helpers.doKeys('<C-v>');
  2024. selections = cm.getSelections();
  2025. eq('78bc', selections.join(''));
  2026. // blockwise to linewise visual
  2027. helpers.doKeys('V');
  2028. selections = cm.getSelections();
  2029. eq('67891\nabcde', selections.join(''));
  2030. }, {value: '12345\n67891\nabcde'});
  2031. testVim('visual_block_crossing_short_line', function(cm, vim, helpers) {
  2032. // visual block with long and short lines
  2033. cm.setCursor(0, 3);
  2034. helpers.doKeys('<C-v>', 'j', 'j', 'j');
  2035. var selections = cm.getSelections().join();
  2036. eq('4,,d,b', selections);
  2037. helpers.doKeys('3', 'k');
  2038. selections = cm.getSelections().join();
  2039. eq('4', selections);
  2040. helpers.doKeys('5', 'j', 'k');
  2041. selections = cm.getSelections().join("");
  2042. eq(10, selections.length);
  2043. }, {value: '123456\n78\nabcdefg\nfoobar\n}\n'});
  2044. testVim('visual_block_curPos_on_exit', function(cm, vim, helpers) {
  2045. cm.setCursor(0, 0);
  2046. helpers.doKeys('<C-v>', '3' , 'l', '<Esc>');
  2047. eqCursorPos(makeCursor(0, 3), cm.getCursor());
  2048. helpers.doKeys('h', '<C-v>', '2' , 'j' ,'3' , 'l');
  2049. eq(cm.getSelections().join(), "3456,,cdef");
  2050. helpers.doKeys('4' , 'h');
  2051. eq(cm.getSelections().join(), "23,8,bc");
  2052. helpers.doKeys('2' , 'l');
  2053. eq(cm.getSelections().join(), "34,,cd");
  2054. }, {value: '123456\n78\nabcdefg\nfoobar'});
  2055. testVim('visual_marks', function(cm, vim, helpers) {
  2056. helpers.doKeys('l', 'v', 'l', 'l', 'j', 'j', 'v');
  2057. // Test visual mode marks
  2058. cm.setCursor(2, 1);
  2059. helpers.doKeys('\'', '<');
  2060. helpers.assertCursorAt(0, 1);
  2061. helpers.doKeys('\'', '>');
  2062. helpers.assertCursorAt(2, 0);
  2063. });
  2064. testVim('visual_join', function(cm, vim, helpers) {
  2065. helpers.doKeys('l', 'V', 'l', 'j', 'j', 'J');
  2066. eq(' 1 2 3\n 4\n 5', cm.getValue());
  2067. is(!vim.visualMode);
  2068. }, { value: ' 1\n 2\n 3\n 4\n 5' });
  2069. testVim('visual_join_2', function(cm, vim, helpers) {
  2070. helpers.doKeys('G', 'V', 'g', 'g', 'J');
  2071. eq('1 2 3 4 5 6 ', cm.getValue());
  2072. is(!vim.visualMode);
  2073. }, { value: '1\n2\n3\n4\n5\n6\n'});
  2074. testVim('visual_blank', function(cm, vim, helpers) {
  2075. helpers.doKeys('v', 'k');
  2076. eq(vim.visualMode, true);
  2077. }, { value: '\n' });
  2078. testVim('reselect_visual', function(cm, vim, helpers) {
  2079. helpers.doKeys('l', 'v', 'l', 'l', 'l', 'y', 'g', 'v');
  2080. helpers.assertCursorAt(0, 5);
  2081. eqCursorPos(makeCursor(0, 1), cm.getCursor('anchor'));
  2082. helpers.doKeys('v');
  2083. cm.setCursor(1, 0);
  2084. helpers.doKeys('v', 'l', 'l', 'p');
  2085. eq('123456\n2345\nbar', cm.getValue());
  2086. cm.setCursor(0, 0);
  2087. helpers.doKeys('g', 'v');
  2088. // here the fake cursor is at (1, 3)
  2089. helpers.assertCursorAt(1, 4);
  2090. eqCursorPos(makeCursor(1, 0), cm.getCursor('anchor'));
  2091. helpers.doKeys('v');
  2092. cm.setCursor(2, 0);
  2093. helpers.doKeys('v', 'l', 'l', 'g', 'v');
  2094. helpers.assertCursorAt(1, 4);
  2095. eqCursorPos(makeCursor(1, 0), cm.getCursor('anchor'));
  2096. helpers.doKeys('g', 'v');
  2097. helpers.assertCursorAt(2, 3);
  2098. eqCursorPos(makeCursor(2, 0), cm.getCursor('anchor'));
  2099. eq('123456\n2345\nbar', cm.getValue());
  2100. }, { value: '123456\nfoo\nbar' });
  2101. testVim('reselect_visual_line', function(cm, vim, helpers) {
  2102. helpers.doKeys('l', 'V', 'j', 'j', 'V', 'g', 'v', 'd');
  2103. eq('foo\nand\nbar', cm.getValue());
  2104. cm.setCursor(1, 0);
  2105. helpers.doKeys('V', 'y', 'j');
  2106. helpers.doKeys('V', 'p' , 'g', 'v', 'd');
  2107. eq('foo\nand', cm.getValue());
  2108. }, { value: 'hello\nthis\nis\nfoo\nand\nbar' });
  2109. testVim('reselect_visual_block', function(cm, vim, helpers) {
  2110. cm.setCursor(1, 2);
  2111. helpers.doKeys('<C-v>', 'k', 'h', '<C-v>');
  2112. cm.setCursor(2, 1);
  2113. helpers.doKeys('v', 'l', 'g', 'v');
  2114. eqCursorPos(Pos(1, 2), vim.sel.anchor);
  2115. eqCursorPos(Pos(0, 1), vim.sel.head);
  2116. // Ensure selection is done with visual block mode rather than one
  2117. // continuous range.
  2118. eq(cm.getSelections().join(''), '23oo')
  2119. helpers.doKeys('g', 'v');
  2120. eqCursorPos(Pos(2, 1), vim.sel.anchor);
  2121. eqCursorPos(Pos(2, 2), vim.sel.head);
  2122. helpers.doKeys('<Esc>');
  2123. // Ensure selection of deleted range
  2124. cm.setCursor(1, 1);
  2125. helpers.doKeys('v', '<C-v>', 'j', 'd', 'g', 'v');
  2126. eq(cm.getSelections().join(''), 'or');
  2127. }, { value: '123456\nfoo\nbar' });
  2128. testVim('s_normal', function(cm, vim, helpers) {
  2129. cm.setCursor(0, 1);
  2130. helpers.doKeys('s');
  2131. helpers.doKeys('<Esc>');
  2132. eq('ac', cm.getValue());
  2133. }, { value: 'abc'});
  2134. testVim('s_visual', function(cm, vim, helpers) {
  2135. cm.setCursor(0, 1);
  2136. helpers.doKeys('v', 's');
  2137. helpers.doKeys('<Esc>');
  2138. helpers.assertCursorAt(0, 0);
  2139. eq('ac', cm.getValue());
  2140. }, { value: 'abc'});
  2141. testVim('o_visual', function(cm, vim, helpers) {
  2142. cm.setCursor(0,0);
  2143. helpers.doKeys('v','l','l','l','o');
  2144. helpers.assertCursorAt(0,0);
  2145. helpers.doKeys('v','v','j','j','j','o');
  2146. helpers.assertCursorAt(0,0);
  2147. helpers.doKeys('O');
  2148. helpers.doKeys('l','l')
  2149. helpers.assertCursorAt(3, 3);
  2150. helpers.doKeys('d');
  2151. eq('p',cm.getValue());
  2152. }, { value: 'abcd\nefgh\nijkl\nmnop'});
  2153. testVim('o_visual_block', function(cm, vim, helpers) {
  2154. cm.setCursor(0, 1);
  2155. helpers.doKeys('<C-v>','3','j','l','l', 'o');
  2156. eqCursorPos(Pos(3, 3), vim.sel.anchor);
  2157. eqCursorPos(Pos(0, 1), vim.sel.head);
  2158. helpers.doKeys('O');
  2159. eqCursorPos(Pos(3, 1), vim.sel.anchor);
  2160. eqCursorPos(Pos(0, 3), vim.sel.head);
  2161. helpers.doKeys('o');
  2162. eqCursorPos(Pos(0, 3), vim.sel.anchor);
  2163. eqCursorPos(Pos(3, 1), vim.sel.head);
  2164. }, { value: 'abcd\nefgh\nijkl\nmnop'});
  2165. testVim('changeCase_visual', function(cm, vim, helpers) {
  2166. cm.setCursor(0, 0);
  2167. helpers.doKeys('v', 'l', 'l');
  2168. helpers.doKeys('U');
  2169. helpers.assertCursorAt(0, 0);
  2170. helpers.doKeys('v', 'l', 'l');
  2171. helpers.doKeys('u');
  2172. helpers.assertCursorAt(0, 0);
  2173. helpers.doKeys('l', 'l', 'l', '.');
  2174. helpers.assertCursorAt(0, 3);
  2175. cm.setCursor(0, 0);
  2176. helpers.doKeys('q', 'a', 'v', 'j', 'U', 'q');
  2177. helpers.assertCursorAt(0, 0);
  2178. helpers.doKeys('j', '@', 'a');
  2179. helpers.assertCursorAt(1, 0);
  2180. cm.setCursor(3, 0);
  2181. helpers.doKeys('V', 'U', 'j', '.');
  2182. eq('ABCDEF\nGHIJKL\nMnopq\nSHORT LINE\nLONG LINE OF TEXT', cm.getValue());
  2183. }, { value: 'abcdef\nghijkl\nmnopq\nshort line\nlong line of text'});
  2184. testVim('changeCase_visual_block', function(cm, vim, helpers) {
  2185. cm.setCursor(2, 1);
  2186. helpers.doKeys('<C-v>', 'k', 'k', 'h', 'U');
  2187. eq('ABcdef\nGHijkl\nMNopq\nfoo', cm.getValue());
  2188. cm.setCursor(0, 2);
  2189. helpers.doKeys('.');
  2190. eq('ABCDef\nGHIJkl\nMNOPq\nfoo', cm.getValue());
  2191. // check when last line is shorter.
  2192. cm.setCursor(2, 2);
  2193. helpers.doKeys('.');
  2194. eq('ABCDef\nGHIJkl\nMNOPq\nfoO', cm.getValue());
  2195. }, { value: 'abcdef\nghijkl\nmnopq\nfoo'});
  2196. testVim('visual_paste', function(cm, vim, helpers) {
  2197. cm.setCursor(0, 0);
  2198. helpers.doKeys('v', 'l', 'l', 'y');
  2199. helpers.assertCursorAt(0, 0);
  2200. helpers.doKeys('3', 'l', 'j', 'v', 'l', 'p');
  2201. helpers.assertCursorAt(1, 5);
  2202. eq('this is a\nunithitest for visual paste', cm.getValue());
  2203. cm.setCursor(0, 0);
  2204. // in case of pasting whole line
  2205. helpers.doKeys('y', 'y');
  2206. cm.setCursor(1, 6);
  2207. helpers.doKeys('v', 'l', 'l', 'l', 'p');
  2208. helpers.assertCursorAt(2, 0);
  2209. eq('this is a\nunithi\nthis is a\n for visual paste', cm.getValue());
  2210. }, { value: 'this is a\nunit test for visual paste'});
  2211. // This checks the contents of the register used to paste the text
  2212. testVim('v_paste_from_register', function(cm, vim, helpers) {
  2213. cm.setCursor(0, 0);
  2214. helpers.doKeys('"', 'a', 'y', 'w');
  2215. cm.setCursor(1, 0);
  2216. helpers.doKeys('v', 'p');
  2217. cm.openDialog = helpers.fakeOpenDialog('registers');
  2218. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2219. is(/a\s+register/.test(text));
  2220. });
  2221. }, { value: 'register contents\nare not erased'});
  2222. testVim('S_normal', function(cm, vim, helpers) {
  2223. cm.setCursor(0, 1);
  2224. helpers.doKeys('j', 'S');
  2225. helpers.doKeys('<Esc>');
  2226. helpers.assertCursorAt(1, 1);
  2227. eq('aa{\n \ncc', cm.getValue());
  2228. helpers.doKeys('j', 'S');
  2229. eq('aa{\n \n ', cm.getValue());
  2230. helpers.assertCursorAt(2, 2);
  2231. helpers.doKeys('<Esc>');
  2232. helpers.doKeys('d', 'd', 'd', 'd');
  2233. helpers.assertCursorAt(0, 0);
  2234. helpers.doKeys('S');
  2235. is(vim.insertMode);
  2236. eq('', cm.getValue());
  2237. }, { value: 'aa{\nbb\ncc'});
  2238. testVim('blockwise_paste', function(cm, vim, helpers) {
  2239. cm.setCursor(0, 0);
  2240. helpers.doKeys('<C-v>', '3', 'j', 'l', 'y');
  2241. cm.setCursor(0, 2);
  2242. // paste one char after the current cursor position
  2243. helpers.doKeys('p');
  2244. eq('helhelo\nworwold\nfoofo\nbarba', cm.getValue());
  2245. cm.setCursor(0, 0);
  2246. helpers.doKeys('v', '4', 'l', 'y');
  2247. cm.setCursor(0, 0);
  2248. helpers.doKeys('<C-v>', '3', 'j', 'p');
  2249. eq('helheelhelo\norwold\noofo\narba', cm.getValue());
  2250. }, { value: 'hello\nworld\nfoo\nbar'});
  2251. testVim('blockwise_paste_long/short_line', function(cm, vim, helpers) {
  2252. // extend short lines in case of different line lengths.
  2253. cm.setCursor(0, 0);
  2254. helpers.doKeys('<C-v>', 'j', 'j', 'y');
  2255. cm.setCursor(0, 3);
  2256. helpers.doKeys('p');
  2257. eq('hellho\nfoo f\nbar b', cm.getValue());
  2258. }, { value: 'hello\nfoo\nbar'});
  2259. testVim('blockwise_paste_cut_paste', function(cm, vim, helpers) {
  2260. cm.setCursor(0, 0);
  2261. helpers.doKeys('<C-v>', '2', 'j', 'x');
  2262. cm.setCursor(0, 0);
  2263. helpers.doKeys('P');
  2264. eq('cut\nand\npaste\nme', cm.getValue());
  2265. }, { value: 'cut\nand\npaste\nme'});
  2266. testVim('blockwise_paste_from_register', function(cm, vim, helpers) {
  2267. cm.setCursor(0, 0);
  2268. helpers.doKeys('<C-v>', '2', 'j', '"', 'a', 'y');
  2269. cm.setCursor(0, 3);
  2270. helpers.doKeys('"', 'a', 'p');
  2271. eq('foobfar\nhellho\nworlwd', cm.getValue());
  2272. }, { value: 'foobar\nhello\nworld'});
  2273. testVim('blockwise_paste_last_line', function(cm, vim, helpers) {
  2274. cm.setCursor(0, 0);
  2275. helpers.doKeys('<C-v>', '2', 'j', 'l', 'y');
  2276. cm.setCursor(3, 0);
  2277. helpers.doKeys('p');
  2278. eq('cut\nand\npaste\nmcue\n an\n pa', cm.getValue());
  2279. }, { value: 'cut\nand\npaste\nme'});
  2280. testVim('S_visual', function(cm, vim, helpers) {
  2281. cm.setCursor(0, 1);
  2282. helpers.doKeys('v', 'j', 'S');
  2283. helpers.doKeys('<Esc>');
  2284. helpers.assertCursorAt(0, 0);
  2285. eq('\ncc', cm.getValue());
  2286. }, { value: 'aa\nbb\ncc'});
  2287. testVim('d_/', function(cm, vim, helpers) {
  2288. cm.openDialog = helpers.fakeOpenDialog('match');
  2289. helpers.doKeys('2', 'd', '/');
  2290. helpers.assertCursorAt(0, 0);
  2291. eq('match \n next', cm.getValue());
  2292. cm.openDialog = helpers.fakeOpenDialog('2');
  2293. helpers.doKeys('d', ':');
  2294. // TODO eq(' next', cm.getValue());
  2295. }, { value: 'text match match \n next' });
  2296. testVim('/ and n/N', function(cm, vim, helpers) {
  2297. cm.openDialog = helpers.fakeOpenDialog('match');
  2298. helpers.doKeys('/');
  2299. helpers.assertCursorAt(0, 11);
  2300. helpers.doKeys('n');
  2301. helpers.assertCursorAt(1, 6);
  2302. helpers.doKeys('N');
  2303. helpers.assertCursorAt(0, 11);
  2304. cm.setCursor(0, 0);
  2305. helpers.doKeys('2', '/');
  2306. helpers.assertCursorAt(1, 6);
  2307. }, { value: 'match nope match \n nope Match' });
  2308. testVim('/_case', function(cm, vim, helpers) {
  2309. cm.openDialog = helpers.fakeOpenDialog('Match');
  2310. helpers.doKeys('/');
  2311. helpers.assertCursorAt(1, 6);
  2312. }, { value: 'match nope match \n nope Match' });
  2313. testVim('/_2_pcre', function(cm, vim, helpers) {
  2314. CodeMirror.Vim.setOption('pcre', true);
  2315. cm.openDialog = helpers.fakeOpenDialog('(word){2}');
  2316. helpers.doKeys('/');
  2317. helpers.assertCursorAt(1, 9);
  2318. helpers.doKeys('n');
  2319. helpers.assertCursorAt(2, 1);
  2320. }, { value: 'word\n another wordword\n wordwordword\n' });
  2321. testVim('/_2_nopcre', function(cm, vim, helpers) {
  2322. CodeMirror.Vim.setOption('pcre', false);
  2323. cm.openDialog = helpers.fakeOpenDialog('\\(word\\)\\{2}');
  2324. helpers.doKeys('/');
  2325. helpers.assertCursorAt(1, 9);
  2326. helpers.doKeys('n');
  2327. helpers.assertCursorAt(2, 1);
  2328. }, { value: 'word\n another wordword\n wordwordword\n' });
  2329. testVim('/_nongreedy', function(cm, vim, helpers) {
  2330. cm.openDialog = helpers.fakeOpenDialog('aa');
  2331. helpers.doKeys('/');
  2332. helpers.assertCursorAt(0, 4);
  2333. helpers.doKeys('n');
  2334. helpers.assertCursorAt(1, 3);
  2335. helpers.doKeys('n');
  2336. helpers.assertCursorAt(0, 0);
  2337. }, { value: 'aaa aa \n a aa'});
  2338. testVim('?_nongreedy', function(cm, vim, helpers) {
  2339. cm.openDialog = helpers.fakeOpenDialog('aa');
  2340. helpers.doKeys('?');
  2341. helpers.assertCursorAt(1, 3);
  2342. helpers.doKeys('n');
  2343. helpers.assertCursorAt(0, 4);
  2344. helpers.doKeys('n');
  2345. helpers.assertCursorAt(0, 0);
  2346. }, { value: 'aaa aa \n a aa'});
  2347. testVim('/_greedy', function(cm, vim, helpers) {
  2348. cm.openDialog = helpers.fakeOpenDialog('a+');
  2349. helpers.doKeys('/');
  2350. helpers.assertCursorAt(0, 4);
  2351. helpers.doKeys('n');
  2352. helpers.assertCursorAt(1, 1);
  2353. helpers.doKeys('n');
  2354. helpers.assertCursorAt(1, 3);
  2355. helpers.doKeys('n');
  2356. helpers.assertCursorAt(0, 0);
  2357. }, { value: 'aaa aa \n a aa'});
  2358. testVim('?_greedy', function(cm, vim, helpers) {
  2359. cm.openDialog = helpers.fakeOpenDialog('a+');
  2360. helpers.doKeys('?');
  2361. helpers.assertCursorAt(1, 3);
  2362. helpers.doKeys('n');
  2363. helpers.assertCursorAt(1, 1);
  2364. helpers.doKeys('n');
  2365. helpers.assertCursorAt(0, 4);
  2366. helpers.doKeys('n');
  2367. helpers.assertCursorAt(0, 0);
  2368. }, { value: 'aaa aa \n a aa'});
  2369. testVim('/_greedy_0_or_more', function(cm, vim, helpers) {
  2370. cm.openDialog = helpers.fakeOpenDialog('a*');
  2371. helpers.doKeys('/');
  2372. helpers.assertCursorAt(0, 3);
  2373. helpers.doKeys('n');
  2374. helpers.assertCursorAt(0, 4);
  2375. helpers.doKeys('n');
  2376. helpers.assertCursorAt(0, 5);
  2377. helpers.doKeys('n');
  2378. helpers.assertCursorAt(1, 0);
  2379. helpers.doKeys('n');
  2380. helpers.assertCursorAt(1, 1);
  2381. helpers.doKeys('n');
  2382. helpers.assertCursorAt(0, 0);
  2383. }, { value: 'aaa aa\n aa'});
  2384. testVim('?_greedy_0_or_more', function(cm, vim, helpers) {
  2385. cm.openDialog = helpers.fakeOpenDialog('a*');
  2386. helpers.doKeys('?');
  2387. helpers.assertCursorAt(1, 1);
  2388. helpers.doKeys('n');
  2389. helpers.assertCursorAt(0, 5);
  2390. helpers.doKeys('n');
  2391. helpers.assertCursorAt(0, 3);
  2392. helpers.doKeys('n');
  2393. helpers.assertCursorAt(0, 0);
  2394. }, { value: 'aaa aa\n aa'});
  2395. testVim('? and n/N', function(cm, vim, helpers) {
  2396. cm.openDialog = helpers.fakeOpenDialog('match');
  2397. helpers.doKeys('?');
  2398. helpers.assertCursorAt(1, 6);
  2399. helpers.doKeys('n');
  2400. helpers.assertCursorAt(0, 11);
  2401. helpers.doKeys('N');
  2402. helpers.assertCursorAt(1, 6);
  2403. cm.setCursor(0, 0);
  2404. helpers.doKeys('2', '?');
  2405. helpers.assertCursorAt(0, 11);
  2406. }, { value: 'match nope match \n nope Match' });
  2407. testVim('*', function(cm, vim, helpers) {
  2408. cm.setCursor(0, 9);
  2409. helpers.doKeys('*');
  2410. helpers.assertCursorAt(0, 22);
  2411. cm.setCursor(0, 9);
  2412. helpers.doKeys('2', '*');
  2413. helpers.assertCursorAt(1, 8);
  2414. }, { value: 'nomatch match nomatch match \nnomatch Match' });
  2415. testVim('*_no_word', function(cm, vim, helpers) {
  2416. cm.setCursor(0, 0);
  2417. helpers.doKeys('*');
  2418. helpers.assertCursorAt(0, 0);
  2419. }, { value: ' \n match \n' });
  2420. testVim('*_symbol', function(cm, vim, helpers) {
  2421. cm.setCursor(0, 0);
  2422. helpers.doKeys('*');
  2423. helpers.assertCursorAt(1, 0);
  2424. }, { value: ' /}\n/} match \n' });
  2425. testVim('#', function(cm, vim, helpers) {
  2426. cm.setCursor(0, 9);
  2427. helpers.doKeys('#');
  2428. helpers.assertCursorAt(1, 8);
  2429. cm.setCursor(0, 9);
  2430. helpers.doKeys('2', '#');
  2431. helpers.assertCursorAt(0, 22);
  2432. }, { value: 'nomatch match nomatch match \nnomatch Match' });
  2433. testVim('*_seek', function(cm, vim, helpers) {
  2434. // Should skip over space and symbols.
  2435. cm.setCursor(0, 3);
  2436. helpers.doKeys('*');
  2437. helpers.assertCursorAt(0, 22);
  2438. }, { value: ' := match nomatch match \nnomatch Match' });
  2439. testVim('#', function(cm, vim, helpers) {
  2440. // Should skip over space and symbols.
  2441. cm.setCursor(0, 3);
  2442. helpers.doKeys('#');
  2443. helpers.assertCursorAt(1, 8);
  2444. }, { value: ' := match nomatch match \nnomatch Match' });
  2445. testVim('g*', function(cm, vim, helpers) {
  2446. cm.setCursor(0, 8);
  2447. helpers.doKeys('g', '*');
  2448. helpers.assertCursorAt(0, 18);
  2449. cm.setCursor(0, 8);
  2450. helpers.doKeys('3', 'g', '*');
  2451. helpers.assertCursorAt(1, 8);
  2452. }, { value: 'matches match alsoMatch\nmatchme matching' });
  2453. testVim('g#', function(cm, vim, helpers) {
  2454. cm.setCursor(0, 8);
  2455. helpers.doKeys('g', '#');
  2456. helpers.assertCursorAt(0, 0);
  2457. cm.setCursor(0, 8);
  2458. helpers.doKeys('3', 'g', '#');
  2459. helpers.assertCursorAt(1, 0);
  2460. }, { value: 'matches match alsoMatch\nmatchme matching' });
  2461. testVim('macro_insert', function(cm, vim, helpers) {
  2462. cm.setCursor(0, 0);
  2463. helpers.doKeys('q', 'a', '0', 'i');
  2464. cm.replaceRange('foo', cm.getCursor());
  2465. helpers.doKeys('<Esc>');
  2466. helpers.doKeys('q', '@', 'a');
  2467. eq('foofoo', cm.getValue());
  2468. }, { value: ''});
  2469. testVim('macro_insert_repeat', function(cm, vim, helpers) {
  2470. cm.setCursor(0, 0);
  2471. helpers.doKeys('q', 'a', '$', 'a');
  2472. cm.replaceRange('larry.', cm.getCursor());
  2473. helpers.doKeys('<Esc>');
  2474. helpers.doKeys('a');
  2475. cm.replaceRange('curly.', cm.getCursor());
  2476. helpers.doKeys('<Esc>');
  2477. helpers.doKeys('q');
  2478. helpers.doKeys('a');
  2479. cm.replaceRange('moe.', cm.getCursor());
  2480. helpers.doKeys('<Esc>');
  2481. helpers.doKeys('@', 'a');
  2482. // At this point, the most recent edit should be the 2nd insert change
  2483. // inside the macro, i.e. "curly.".
  2484. helpers.doKeys('.');
  2485. eq('larry.curly.moe.larry.curly.curly.', cm.getValue());
  2486. }, { value: ''});
  2487. testVim('macro_space', function(cm, vim, helpers) {
  2488. cm.setCursor(0, 0);
  2489. helpers.doKeys('<Space>', '<Space>');
  2490. helpers.assertCursorAt(0, 2);
  2491. helpers.doKeys('q', 'a', '<Space>', '<Space>', 'q');
  2492. helpers.assertCursorAt(0, 4);
  2493. helpers.doKeys('@', 'a');
  2494. helpers.assertCursorAt(0, 6);
  2495. helpers.doKeys('@', 'a');
  2496. helpers.assertCursorAt(0, 8);
  2497. }, { value: 'one line of text.'});
  2498. testVim('macro_t_search', function(cm, vim, helpers) {
  2499. cm.setCursor(0, 0);
  2500. helpers.doKeys('q', 'a', 't', 'e', 'q');
  2501. helpers.assertCursorAt(0, 1);
  2502. helpers.doKeys('l', '@', 'a');
  2503. helpers.assertCursorAt(0, 6);
  2504. helpers.doKeys('l', ';');
  2505. helpers.assertCursorAt(0, 12);
  2506. }, { value: 'one line of text.'});
  2507. testVim('macro_f_search', function(cm, vim, helpers) {
  2508. cm.setCursor(0, 0);
  2509. helpers.doKeys('q', 'b', 'f', 'e', 'q');
  2510. helpers.assertCursorAt(0, 2);
  2511. helpers.doKeys('@', 'b');
  2512. helpers.assertCursorAt(0, 7);
  2513. helpers.doKeys(';');
  2514. helpers.assertCursorAt(0, 13);
  2515. }, { value: 'one line of text.'});
  2516. testVim('macro_slash_search', function(cm, vim, helpers) {
  2517. cm.setCursor(0, 0);
  2518. helpers.doKeys('q', 'c');
  2519. cm.openDialog = helpers.fakeOpenDialog('e');
  2520. helpers.doKeys('/', 'q');
  2521. helpers.assertCursorAt(0, 2);
  2522. helpers.doKeys('@', 'c');
  2523. helpers.assertCursorAt(0, 7);
  2524. helpers.doKeys('n');
  2525. helpers.assertCursorAt(0, 13);
  2526. }, { value: 'one line of text.'});
  2527. testVim('macro_multislash_search', function(cm, vim, helpers) {
  2528. cm.setCursor(0, 0);
  2529. helpers.doKeys('q', 'd');
  2530. cm.openDialog = helpers.fakeOpenDialog('e');
  2531. helpers.doKeys('/');
  2532. cm.openDialog = helpers.fakeOpenDialog('t');
  2533. helpers.doKeys('/', 'q');
  2534. helpers.assertCursorAt(0, 12);
  2535. helpers.doKeys('@', 'd');
  2536. helpers.assertCursorAt(0, 15);
  2537. }, { value: 'one line of text to rule them all.'});
  2538. testVim('macro_last_ex_command_register', function (cm, vim, helpers) {
  2539. cm.setCursor(0, 0);
  2540. helpers.doEx('s/a/b');
  2541. helpers.doKeys('2', '@', ':');
  2542. eq('bbbaa', cm.getValue());
  2543. helpers.assertCursorAt(0, 2);
  2544. }, { value: 'aaaaa'});
  2545. testVim('macro_parens', function(cm, vim, helpers) {
  2546. cm.setCursor(0, 0);
  2547. helpers.doKeys('q', 'z', 'i');
  2548. cm.replaceRange('(', cm.getCursor());
  2549. helpers.doKeys('<Esc>');
  2550. helpers.doKeys('e', 'a');
  2551. cm.replaceRange(')', cm.getCursor());
  2552. helpers.doKeys('<Esc>');
  2553. helpers.doKeys('q');
  2554. helpers.doKeys('w', '@', 'z');
  2555. helpers.doKeys('w', '@', 'z');
  2556. eq('(see) (spot) (run)', cm.getValue());
  2557. }, { value: 'see spot run'});
  2558. testVim('macro_overwrite', function(cm, vim, helpers) {
  2559. cm.setCursor(0, 0);
  2560. helpers.doKeys('q', 'z', '0', 'i');
  2561. cm.replaceRange('I ', cm.getCursor());
  2562. helpers.doKeys('<Esc>');
  2563. helpers.doKeys('q');
  2564. helpers.doKeys('e');
  2565. // Now replace the macro with something else.
  2566. helpers.doKeys('q', 'z', 'a');
  2567. cm.replaceRange('.', cm.getCursor());
  2568. helpers.doKeys('<Esc>');
  2569. helpers.doKeys('q');
  2570. helpers.doKeys('e', '@', 'z');
  2571. helpers.doKeys('e', '@', 'z');
  2572. eq('I see. spot. run.', cm.getValue());
  2573. }, { value: 'see spot run'});
  2574. testVim('macro_search_f', function(cm, vim, helpers) {
  2575. cm.setCursor(0, 0);
  2576. helpers.doKeys('q', 'a', 'f', ' ');
  2577. helpers.assertCursorAt(0,3);
  2578. helpers.doKeys('q', '0');
  2579. helpers.assertCursorAt(0,0);
  2580. helpers.doKeys('@', 'a');
  2581. helpers.assertCursorAt(0,3);
  2582. }, { value: 'The quick brown fox jumped over the lazy dog.'});
  2583. testVim('macro_search_2f', function(cm, vim, helpers) {
  2584. cm.setCursor(0, 0);
  2585. helpers.doKeys('q', 'a', '2', 'f', ' ');
  2586. helpers.assertCursorAt(0,9);
  2587. helpers.doKeys('q', '0');
  2588. helpers.assertCursorAt(0,0);
  2589. helpers.doKeys('@', 'a');
  2590. helpers.assertCursorAt(0,9);
  2591. }, { value: 'The quick brown fox jumped over the lazy dog.'});
  2592. testVim('macro_yank_tick', function(cm, vim, helpers) {
  2593. cm.setCursor(0, 0);
  2594. // Start recording a macro into the \' register.
  2595. helpers.doKeys('q', '\'');
  2596. helpers.doKeys('y', '<Right>', '<Right>', '<Right>', '<Right>', 'p');
  2597. helpers.assertCursorAt(0,4);
  2598. eq('the tex parrot', cm.getValue());
  2599. }, { value: 'the ex parrot'});
  2600. testVim('yank_register', function(cm, vim, helpers) {
  2601. cm.setCursor(0, 0);
  2602. helpers.doKeys('"', 'a', 'y', 'y');
  2603. helpers.doKeys('j', '"', 'b', 'y', 'y');
  2604. cm.openDialog = helpers.fakeOpenDialog('registers');
  2605. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2606. is(/a\s+foo/.test(text));
  2607. is(/b\s+bar/.test(text));
  2608. });
  2609. helpers.doKeys(':');
  2610. }, { value: 'foo\nbar'});
  2611. testVim('yank_visual_block', function(cm, vim, helpers) {
  2612. cm.setCursor(0, 1);
  2613. helpers.doKeys('<C-v>', 'l', 'j', '"', 'a', 'y');
  2614. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2615. is(/a\s+oo\nar/.test(text));
  2616. });
  2617. helpers.doKeys(':');
  2618. }, { value: 'foo\nbar'});
  2619. testVim('yank_append_line_to_line_register', function(cm, vim, helpers) {
  2620. cm.setCursor(0, 0);
  2621. helpers.doKeys('"', 'a', 'y', 'y');
  2622. helpers.doKeys('j', '"', 'A', 'y', 'y');
  2623. cm.openDialog = helpers.fakeOpenDialog('registers');
  2624. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2625. is(/a\s+foo\nbar/.test(text));
  2626. is(/"\s+foo\nbar/.test(text));
  2627. });
  2628. helpers.doKeys(':');
  2629. }, { value: 'foo\nbar'});
  2630. testVim('yank_append_word_to_word_register', function(cm, vim, helpers) {
  2631. cm.setCursor(0, 0);
  2632. helpers.doKeys('"', 'a', 'y', 'w');
  2633. helpers.doKeys('j', '"', 'A', 'y', 'w');
  2634. cm.openDialog = helpers.fakeOpenDialog('registers');
  2635. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2636. is(/a\s+foobar/.test(text));
  2637. is(/"\s+foobar/.test(text));
  2638. });
  2639. helpers.doKeys(':');
  2640. }, { value: 'foo\nbar'});
  2641. testVim('yank_append_line_to_word_register', function(cm, vim, helpers) {
  2642. cm.setCursor(0, 0);
  2643. helpers.doKeys('"', 'a', 'y', 'w');
  2644. helpers.doKeys('j', '"', 'A', 'y', 'y');
  2645. cm.openDialog = helpers.fakeOpenDialog('registers');
  2646. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2647. is(/a\s+foo\nbar/.test(text));
  2648. is(/"\s+foo\nbar/.test(text));
  2649. });
  2650. helpers.doKeys(':');
  2651. }, { value: 'foo\nbar'});
  2652. testVim('yank_append_word_to_line_register', function(cm, vim, helpers) {
  2653. cm.setCursor(0, 0);
  2654. helpers.doKeys('"', 'a', 'y', 'y');
  2655. helpers.doKeys('j', '"', 'A', 'y', 'w');
  2656. cm.openDialog = helpers.fakeOpenDialog('registers');
  2657. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2658. is(/a\s+foo\nbar/.test(text));
  2659. is(/"\s+foo\nbar/.test(text));
  2660. });
  2661. helpers.doKeys(':');
  2662. }, { value: 'foo\nbar'});
  2663. testVim('macro_register', function(cm, vim, helpers) {
  2664. cm.setCursor(0, 0);
  2665. helpers.doKeys('q', 'a', 'i');
  2666. cm.replaceRange('gangnam', cm.getCursor());
  2667. helpers.doKeys('<Esc>');
  2668. helpers.doKeys('q');
  2669. helpers.doKeys('q', 'b', 'o');
  2670. cm.replaceRange('style', cm.getCursor());
  2671. helpers.doKeys('<Esc>');
  2672. helpers.doKeys('q');
  2673. cm.openDialog = helpers.fakeOpenDialog('registers');
  2674. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2675. is(/a\s+i/.test(text));
  2676. is(/b\s+o/.test(text));
  2677. });
  2678. helpers.doKeys(':');
  2679. }, { value: ''});
  2680. testVim('._register', function(cm,vim,helpers) {
  2681. cm.setCursor(0,0);
  2682. helpers.doKeys('i');
  2683. cm.replaceRange('foo',cm.getCursor());
  2684. helpers.doKeys('<Esc>');
  2685. cm.openDialog = helpers.fakeOpenDialog('registers');
  2686. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2687. is(/\.\s+foo/.test(text));
  2688. });
  2689. helpers.doKeys(':');
  2690. }, {value: ''});
  2691. testVim(':_register', function(cm,vim,helpers) {
  2692. helpers.doEx('bar');
  2693. cm.openDialog = helpers.fakeOpenDialog('registers');
  2694. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2695. is(/:\s+bar/.test(text));
  2696. });
  2697. helpers.doKeys(':');
  2698. }, {value: ''});
  2699. testVim('search_register_escape', function(cm, vim, helpers) {
  2700. // Check that the register is restored if the user escapes rather than confirms.
  2701. cm.openDialog = helpers.fakeOpenDialog('waldo');
  2702. helpers.doKeys('/');
  2703. var onKeyDown;
  2704. var onKeyUp;
  2705. var KEYCODES = {
  2706. f: 70,
  2707. o: 79,
  2708. Esc: 27
  2709. };
  2710. cm.openDialog = function(template, callback, options) {
  2711. onKeyDown = options.onKeyDown;
  2712. onKeyUp = options.onKeyUp;
  2713. };
  2714. var close = function() {};
  2715. helpers.doKeys('/');
  2716. // Fake some keyboard events coming in.
  2717. onKeyDown({keyCode: KEYCODES.f}, '', close);
  2718. onKeyUp({keyCode: KEYCODES.f}, '', close);
  2719. onKeyDown({keyCode: KEYCODES.o}, 'f', close);
  2720. onKeyUp({keyCode: KEYCODES.o}, 'f', close);
  2721. onKeyDown({keyCode: KEYCODES.o}, 'fo', close);
  2722. onKeyUp({keyCode: KEYCODES.o}, 'fo', close);
  2723. onKeyDown({keyCode: KEYCODES.Esc}, 'foo', close);
  2724. cm.openDialog = helpers.fakeOpenDialog('registers');
  2725. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2726. is(/waldo/.test(text));
  2727. is(!/foo/.test(text));
  2728. });
  2729. helpers.doKeys(':');
  2730. }, {value: ''});
  2731. testVim('search_register', function(cm, vim, helpers) {
  2732. cm.openDialog = helpers.fakeOpenDialog('foo');
  2733. helpers.doKeys('/');
  2734. cm.openDialog = helpers.fakeOpenDialog('registers');
  2735. cm.openNotification = helpers.fakeOpenNotification(function(text) {
  2736. is(/\/\s+foo/.test(text));
  2737. });
  2738. helpers.doKeys(':');
  2739. }, {value: ''});
  2740. testVim('search_history', function(cm, vim, helpers) {
  2741. cm.openDialog = helpers.fakeOpenDialog('this');
  2742. helpers.doKeys('/');
  2743. cm.openDialog = helpers.fakeOpenDialog('checks');
  2744. helpers.doKeys('/');
  2745. cm.openDialog = helpers.fakeOpenDialog('search');
  2746. helpers.doKeys('/');
  2747. cm.openDialog = helpers.fakeOpenDialog('history');
  2748. helpers.doKeys('/');
  2749. cm.openDialog = helpers.fakeOpenDialog('checks');
  2750. helpers.doKeys('/');
  2751. var onKeyDown;
  2752. var onKeyUp;
  2753. var query = '';
  2754. var keyCodes = {
  2755. Up: 38,
  2756. Down: 40
  2757. };
  2758. cm.openDialog = function(template, callback, options) {
  2759. onKeyUp = options.onKeyUp;
  2760. onKeyDown = options.onKeyDown;
  2761. };
  2762. var close = function(newVal) {
  2763. if (typeof newVal == 'string') query = newVal;
  2764. }
  2765. helpers.doKeys('/');
  2766. onKeyDown({keyCode: keyCodes.Up}, query, close);
  2767. onKeyUp({keyCode: keyCodes.Up}, query, close);
  2768. eq(query, 'checks');
  2769. onKeyDown({keyCode: keyCodes.Up}, query, close);
  2770. onKeyUp({keyCode: keyCodes.Up}, query, close);
  2771. eq(query, 'history');
  2772. onKeyDown({keyCode: keyCodes.Up}, query, close);
  2773. onKeyUp({keyCode: keyCodes.Up}, query, close);
  2774. eq(query, 'search');
  2775. onKeyDown({keyCode: keyCodes.Up}, query, close);
  2776. onKeyUp({keyCode: keyCodes.Up}, query, close);
  2777. eq(query, 'this');
  2778. onKeyDown({keyCode: keyCodes.Down}, query, close);
  2779. onKeyUp({keyCode: keyCodes.Down}, query, close);
  2780. eq(query, 'search');
  2781. }, {value: ''});
  2782. testVim('exCommand_history', function(cm, vim, helpers) {
  2783. cm.openDialog = helpers.fakeOpenDialog('registers');
  2784. helpers.doKeys(':');
  2785. cm.openDialog = helpers.fakeOpenDialog('sort');
  2786. helpers.doKeys(':');
  2787. cm.openDialog = helpers.fakeOpenDialog('map');
  2788. helpers.doKeys(':');
  2789. cm.openDialog = helpers.fakeOpenDialog('invalid');
  2790. helpers.doKeys(':');
  2791. var onKeyDown;
  2792. var onKeyUp;
  2793. var input = '';
  2794. var keyCodes = {
  2795. Up: 38,
  2796. Down: 40,
  2797. s: 115
  2798. };
  2799. cm.openDialog = function(template, callback, options) {
  2800. onKeyUp = options.onKeyUp;
  2801. onKeyDown = options.onKeyDown;
  2802. };
  2803. var close = function(newVal) {
  2804. if (typeof newVal == 'string') input = newVal;
  2805. }
  2806. helpers.doKeys(':');
  2807. onKeyDown({keyCode: keyCodes.Up}, input, close);
  2808. eq(input, 'invalid');
  2809. onKeyDown({keyCode: keyCodes.Up}, input, close);
  2810. eq(input, 'map');
  2811. onKeyDown({keyCode: keyCodes.Up}, input, close);
  2812. eq(input, 'sort');
  2813. onKeyDown({keyCode: keyCodes.Up}, input, close);
  2814. eq(input, 'registers');
  2815. onKeyDown({keyCode: keyCodes.s}, '', close);
  2816. input = 's';
  2817. onKeyDown({keyCode: keyCodes.Up}, input, close);
  2818. eq(input, 'sort');
  2819. }, {value: ''});
  2820. testVim('search_clear', function(cm, vim, helpers) {
  2821. var onKeyDown;
  2822. var input = '';
  2823. var keyCodes = {
  2824. Ctrl: 17,
  2825. u: 85
  2826. };
  2827. cm.openDialog = function(template, callback, options) {
  2828. onKeyDown = options.onKeyDown;
  2829. };
  2830. var close = function(newVal) {
  2831. if (typeof newVal == 'string') input = newVal;
  2832. }
  2833. helpers.doKeys('/');
  2834. input = 'foo';
  2835. onKeyDown({keyCode: keyCodes.Ctrl}, input, close);
  2836. onKeyDown({keyCode: keyCodes.u, ctrlKey: true}, input, close);
  2837. eq(input, '');
  2838. });
  2839. testVim('exCommand_clear', function(cm, vim, helpers) {
  2840. var onKeyDown;
  2841. var input = '';
  2842. var keyCodes = {
  2843. Ctrl: 17,
  2844. u: 85
  2845. };
  2846. cm.openDialog = function(template, callback, options) {
  2847. onKeyDown = options.onKeyDown;
  2848. };
  2849. var close = function(newVal) {
  2850. if (typeof newVal == 'string') input = newVal;
  2851. }
  2852. helpers.doKeys(':');
  2853. input = 'foo';
  2854. onKeyDown({keyCode: keyCodes.Ctrl}, input, close);
  2855. onKeyDown({keyCode: keyCodes.u, ctrlKey: true}, input, close);
  2856. eq(input, '');
  2857. });
  2858. testVim('.', function(cm, vim, helpers) {
  2859. cm.setCursor(0, 0);
  2860. helpers.doKeys('2', 'd', 'w');
  2861. helpers.doKeys('.');
  2862. eq('5 6', cm.getValue());
  2863. }, { value: '1 2 3 4 5 6'});
  2864. testVim('._repeat', function(cm, vim, helpers) {
  2865. cm.setCursor(0, 0);
  2866. helpers.doKeys('2', 'd', 'w');
  2867. helpers.doKeys('3', '.');
  2868. eq('6', cm.getValue());
  2869. }, { value: '1 2 3 4 5 6'});
  2870. testVim('._insert', function(cm, vim, helpers) {
  2871. helpers.doKeys('i');
  2872. cm.replaceRange('test', cm.getCursor());
  2873. helpers.doKeys('<Esc>');
  2874. helpers.doKeys('.');
  2875. eq('testestt', cm.getValue());
  2876. helpers.assertCursorAt(0, 6);
  2877. helpers.doKeys('O');
  2878. cm.replaceRange('xyz', cm.getCursor());
  2879. helpers.doInsertModeKeys('Backspace');
  2880. helpers.doInsertModeKeys('Down');
  2881. helpers.doKeys('<Esc>');
  2882. helpers.doKeys('.');
  2883. eq('xy\nxy\ntestestt', cm.getValue());
  2884. helpers.assertCursorAt(1, 1);
  2885. }, { value: ''});
  2886. testVim('._insert_repeat', function(cm, vim, helpers) {
  2887. helpers.doKeys('i');
  2888. cm.replaceRange('test', cm.getCursor());
  2889. cm.setCursor(0, 4);
  2890. helpers.doKeys('<Esc>');
  2891. helpers.doKeys('2', '.');
  2892. eq('testesttestt', cm.getValue());
  2893. helpers.assertCursorAt(0, 10);
  2894. }, { value: ''});
  2895. testVim('._repeat_insert', function(cm, vim, helpers) {
  2896. helpers.doKeys('3', 'i');
  2897. cm.replaceRange('te', cm.getCursor());
  2898. cm.setCursor(0, 2);
  2899. helpers.doKeys('<Esc>');
  2900. helpers.doKeys('.');
  2901. eq('tetettetetee', cm.getValue());
  2902. helpers.assertCursorAt(0, 10);
  2903. }, { value: ''});
  2904. testVim('._insert_o', function(cm, vim, helpers) {
  2905. helpers.doKeys('o');
  2906. cm.replaceRange('z', cm.getCursor());
  2907. cm.setCursor(1, 1);
  2908. helpers.doKeys('<Esc>');
  2909. helpers.doKeys('.');
  2910. eq('\nz\nz', cm.getValue());
  2911. helpers.assertCursorAt(2, 0);
  2912. }, { value: ''});
  2913. testVim('._insert_o_repeat', function(cm, vim, helpers) {
  2914. helpers.doKeys('o');
  2915. cm.replaceRange('z', cm.getCursor());
  2916. helpers.doKeys('<Esc>');
  2917. cm.setCursor(1, 0);
  2918. helpers.doKeys('2', '.');
  2919. eq('\nz\nz\nz', cm.getValue());
  2920. helpers.assertCursorAt(3, 0);
  2921. }, { value: ''});
  2922. testVim('._insert_o_indent', function(cm, vim, helpers) {
  2923. helpers.doKeys('o');
  2924. cm.replaceRange('z', cm.getCursor());
  2925. helpers.doKeys('<Esc>');
  2926. cm.setCursor(1, 2);
  2927. helpers.doKeys('.');
  2928. eq('{\n z\n z', cm.getValue());
  2929. helpers.assertCursorAt(2, 2);
  2930. }, { value: '{'});
  2931. testVim('._insert_cw', function(cm, vim, helpers) {
  2932. helpers.doKeys('c', 'w');
  2933. cm.replaceRange('test', cm.getCursor());
  2934. helpers.doKeys('<Esc>');
  2935. cm.setCursor(0, 3);
  2936. helpers.doKeys('2', 'l');
  2937. helpers.doKeys('.');
  2938. eq('test test word3', cm.getValue());
  2939. helpers.assertCursorAt(0, 8);
  2940. }, { value: 'word1 word2 word3' });
  2941. testVim('._insert_cw_repeat', function(cm, vim, helpers) {
  2942. // For some reason, repeat cw in desktop VIM will does not repeat insert mode
  2943. // changes. Will conform to that behavior.
  2944. helpers.doKeys('c', 'w');
  2945. cm.replaceRange('test', cm.getCursor());
  2946. helpers.doKeys('<Esc>');
  2947. cm.setCursor(0, 4);
  2948. helpers.doKeys('l');
  2949. helpers.doKeys('2', '.');
  2950. eq('test test', cm.getValue());
  2951. helpers.assertCursorAt(0, 8);
  2952. }, { value: 'word1 word2 word3' });
  2953. testVim('._delete', function(cm, vim, helpers) {
  2954. cm.setCursor(0, 5);
  2955. helpers.doKeys('i');
  2956. helpers.doInsertModeKeys('Backspace');
  2957. helpers.doKeys('<Esc>');
  2958. helpers.doKeys('.');
  2959. eq('zace', cm.getValue());
  2960. helpers.assertCursorAt(0, 1);
  2961. }, { value: 'zabcde'});
  2962. testVim('._delete_repeat', function(cm, vim, helpers) {
  2963. cm.setCursor(0, 6);
  2964. helpers.doKeys('i');
  2965. helpers.doInsertModeKeys('Backspace');
  2966. helpers.doKeys('<Esc>');
  2967. helpers.doKeys('2', '.');
  2968. eq('zzce', cm.getValue());
  2969. helpers.assertCursorAt(0, 1);
  2970. }, { value: 'zzabcde'});
  2971. testVim('._visual_>', function(cm, vim, helpers) {
  2972. cm.setCursor(0, 0);
  2973. helpers.doKeys('V', 'j', '>');
  2974. cm.setCursor(2, 0)
  2975. helpers.doKeys('.');
  2976. eq(' 1\n 2\n 3\n 4', cm.getValue());
  2977. helpers.assertCursorAt(2, 2);
  2978. }, { value: '1\n2\n3\n4'});
  2979. testVim('._replace_repeat', function(cm, vim, helpers) {
  2980. helpers.doKeys('R');
  2981. cm.replaceRange('123', cm.getCursor(), offsetCursor(cm.getCursor(), 0, 3));
  2982. cm.setCursor(0, 3);
  2983. helpers.doKeys('<Esc>');
  2984. helpers.doKeys('2', '.');
  2985. eq('12123123\nabcdefg', cm.getValue());
  2986. helpers.assertCursorAt(0, 7);
  2987. cm.setCursor(1, 0);
  2988. helpers.doKeys('.');
  2989. eq('12123123\n123123g', cm.getValue());
  2990. helpers.doKeys('l', '"', '.', 'p');
  2991. eq('12123123\n123123g123', cm.getValue());
  2992. }, { value: 'abcdef\nabcdefg'});
  2993. testVim('f;', function(cm, vim, helpers) {
  2994. cm.setCursor(0, 0);
  2995. helpers.doKeys('f', 'x');
  2996. helpers.doKeys(';');
  2997. helpers.doKeys('2', ';');
  2998. eq(9, cm.getCursor().ch);
  2999. }, { value: '01x3xx678x'});
  3000. testVim('F;', function(cm, vim, helpers) {
  3001. cm.setCursor(0, 8);
  3002. helpers.doKeys('F', 'x');
  3003. helpers.doKeys(';');
  3004. helpers.doKeys('2', ';');
  3005. eq(2, cm.getCursor().ch);
  3006. }, { value: '01x3xx6x8x'});
  3007. testVim('t;', function(cm, vim, helpers) {
  3008. cm.setCursor(0, 0);
  3009. helpers.doKeys('t', 'x');
  3010. helpers.doKeys(';');
  3011. helpers.doKeys('2', ';');
  3012. eq(8, cm.getCursor().ch);
  3013. }, { value: '01x3xx678x'});
  3014. testVim('T;', function(cm, vim, helpers) {
  3015. cm.setCursor(0, 9);
  3016. helpers.doKeys('T', 'x');
  3017. helpers.doKeys(';');
  3018. helpers.doKeys('2', ';');
  3019. eq(2, cm.getCursor().ch);
  3020. }, { value: '0xx3xx678x'});
  3021. testVim('f,', function(cm, vim, helpers) {
  3022. cm.setCursor(0, 6);
  3023. helpers.doKeys('f', 'x');
  3024. helpers.doKeys(',');
  3025. helpers.doKeys('2', ',');
  3026. eq(2, cm.getCursor().ch);
  3027. }, { value: '01x3xx678x'});
  3028. testVim('F,', function(cm, vim, helpers) {
  3029. cm.setCursor(0, 3);
  3030. helpers.doKeys('F', 'x');
  3031. helpers.doKeys(',');
  3032. helpers.doKeys('2', ',');
  3033. eq(9, cm.getCursor().ch);
  3034. }, { value: '01x3xx678x'});
  3035. testVim('t,', function(cm, vim, helpers) {
  3036. cm.setCursor(0, 6);
  3037. helpers.doKeys('t', 'x');
  3038. helpers.doKeys(',');
  3039. helpers.doKeys('2', ',');
  3040. eq(3, cm.getCursor().ch);
  3041. }, { value: '01x3xx678x'});
  3042. testVim('T,', function(cm, vim, helpers) {
  3043. cm.setCursor(0, 4);
  3044. helpers.doKeys('T', 'x');
  3045. helpers.doKeys(',');
  3046. helpers.doKeys('2', ',');
  3047. eq(8, cm.getCursor().ch);
  3048. }, { value: '01x3xx67xx'});
  3049. testVim('fd,;', function(cm, vim, helpers) {
  3050. cm.setCursor(0, 0);
  3051. helpers.doKeys('f', '4');
  3052. cm.setCursor(0, 0);
  3053. helpers.doKeys('d', ';');
  3054. eq('56789', cm.getValue());
  3055. helpers.doKeys('u');
  3056. cm.setCursor(0, 9);
  3057. helpers.doKeys('d', ',');
  3058. eq('01239', cm.getValue());
  3059. }, { value: '0123456789'});
  3060. testVim('Fd,;', function(cm, vim, helpers) {
  3061. cm.setCursor(0, 9);
  3062. helpers.doKeys('F', '4');
  3063. cm.setCursor(0, 9);
  3064. helpers.doKeys('d', ';');
  3065. eq('01239', cm.getValue());
  3066. helpers.doKeys('u');
  3067. cm.setCursor(0, 0);
  3068. helpers.doKeys('d', ',');
  3069. eq('56789', cm.getValue());
  3070. }, { value: '0123456789'});
  3071. testVim('td,;', function(cm, vim, helpers) {
  3072. cm.setCursor(0, 0);
  3073. helpers.doKeys('t', '4');
  3074. cm.setCursor(0, 0);
  3075. helpers.doKeys('d', ';');
  3076. eq('456789', cm.getValue());
  3077. helpers.doKeys('u');
  3078. cm.setCursor(0, 9);
  3079. helpers.doKeys('d', ',');
  3080. eq('012349', cm.getValue());
  3081. }, { value: '0123456789'});
  3082. testVim('Td,;', function(cm, vim, helpers) {
  3083. cm.setCursor(0, 9);
  3084. helpers.doKeys('T', '4');
  3085. cm.setCursor(0, 9);
  3086. helpers.doKeys('d', ';');
  3087. eq('012349', cm.getValue());
  3088. helpers.doKeys('u');
  3089. cm.setCursor(0, 0);
  3090. helpers.doKeys('d', ',');
  3091. eq('456789', cm.getValue());
  3092. }, { value: '0123456789'});
  3093. testVim('fc,;', function(cm, vim, helpers) {
  3094. cm.setCursor(0, 0);
  3095. helpers.doKeys('f', '4');
  3096. cm.setCursor(0, 0);
  3097. helpers.doKeys('c', ';', '<Esc>');
  3098. eq('56789', cm.getValue());
  3099. helpers.doKeys('u');
  3100. cm.setCursor(0, 9);
  3101. helpers.doKeys('c', ',');
  3102. eq('01239', cm.getValue());
  3103. }, { value: '0123456789'});
  3104. testVim('Fc,;', function(cm, vim, helpers) {
  3105. cm.setCursor(0, 9);
  3106. helpers.doKeys('F', '4');
  3107. cm.setCursor(0, 9);
  3108. helpers.doKeys('c', ';', '<Esc>');
  3109. eq('01239', cm.getValue());
  3110. helpers.doKeys('u');
  3111. cm.setCursor(0, 0);
  3112. helpers.doKeys('c', ',');
  3113. eq('56789', cm.getValue());
  3114. }, { value: '0123456789'});
  3115. testVim('tc,;', function(cm, vim, helpers) {
  3116. cm.setCursor(0, 0);
  3117. helpers.doKeys('t', '4');
  3118. cm.setCursor(0, 0);
  3119. helpers.doKeys('c', ';', '<Esc>');
  3120. eq('456789', cm.getValue());
  3121. helpers.doKeys('u');
  3122. cm.setCursor(0, 9);
  3123. helpers.doKeys('c', ',');
  3124. eq('012349', cm.getValue());
  3125. }, { value: '0123456789'});
  3126. testVim('Tc,;', function(cm, vim, helpers) {
  3127. cm.setCursor(0, 9);
  3128. helpers.doKeys('T', '4');
  3129. cm.setCursor(0, 9);
  3130. helpers.doKeys('c', ';', '<Esc>');
  3131. eq('012349', cm.getValue());
  3132. helpers.doKeys('u');
  3133. cm.setCursor(0, 0);
  3134. helpers.doKeys('c', ',');
  3135. eq('456789', cm.getValue());
  3136. }, { value: '0123456789'});
  3137. testVim('fy,;', function(cm, vim, helpers) {
  3138. cm.setCursor(0, 0);
  3139. helpers.doKeys('f', '4');
  3140. cm.setCursor(0, 0);
  3141. helpers.doKeys('y', ';', 'P');
  3142. eq('012340123456789', cm.getValue());
  3143. helpers.doKeys('u');
  3144. cm.setCursor(0, 9);
  3145. helpers.doKeys('y', ',', 'P');
  3146. eq('012345678456789', cm.getValue());
  3147. }, { value: '0123456789'});
  3148. testVim('Fy,;', function(cm, vim, helpers) {
  3149. cm.setCursor(0, 9);
  3150. helpers.doKeys('F', '4');
  3151. cm.setCursor(0, 9);
  3152. helpers.doKeys('y', ';', 'p');
  3153. eq('012345678945678', cm.getValue());
  3154. helpers.doKeys('u');
  3155. cm.setCursor(0, 0);
  3156. helpers.doKeys('y', ',', 'P');
  3157. eq('012340123456789', cm.getValue());
  3158. }, { value: '0123456789'});
  3159. testVim('ty,;', function(cm, vim, helpers) {
  3160. cm.setCursor(0, 0);
  3161. helpers.doKeys('t', '4');
  3162. cm.setCursor(0, 0);
  3163. helpers.doKeys('y', ';', 'P');
  3164. eq('01230123456789', cm.getValue());
  3165. helpers.doKeys('u');
  3166. cm.setCursor(0, 9);
  3167. helpers.doKeys('y', ',', 'p');
  3168. eq('01234567895678', cm.getValue());
  3169. }, { value: '0123456789'});
  3170. testVim('Ty,;', function(cm, vim, helpers) {
  3171. cm.setCursor(0, 9);
  3172. helpers.doKeys('T', '4');
  3173. cm.setCursor(0, 9);
  3174. helpers.doKeys('y', ';', 'p');
  3175. eq('01234567895678', cm.getValue());
  3176. helpers.doKeys('u');
  3177. cm.setCursor(0, 0);
  3178. helpers.doKeys('y', ',', 'P');
  3179. eq('01230123456789', cm.getValue());
  3180. }, { value: '0123456789'});
  3181. testVim('HML', function(cm, vim, helpers) {
  3182. var lines = 35;
  3183. var textHeight = cm.defaultTextHeight();
  3184. cm.setSize(600, lines*textHeight);
  3185. cm.setCursor(120, 0);
  3186. helpers.doKeys('H');
  3187. helpers.assertCursorAt(86, 2);
  3188. helpers.doKeys('L');
  3189. helpers.assertCursorAt(120, 4);
  3190. helpers.doKeys('M');
  3191. helpers.assertCursorAt(103,4);
  3192. }, { value: (function(){
  3193. var lines = new Array(100);
  3194. var upper = ' xx\n';
  3195. var lower = ' xx\n';
  3196. upper = lines.join(upper);
  3197. lower = lines.join(lower);
  3198. return upper + lower;
  3199. })()});
  3200. var zVals = [];
  3201. forEach(['zb','zz','zt','z-','z.','z<CR>'], function(e, idx){
  3202. var lineNum = 250;
  3203. var lines = 35;
  3204. testVim(e, function(cm, vim, helpers) {
  3205. var k1 = e[0];
  3206. var k2 = e.substring(1);
  3207. var textHeight = cm.defaultTextHeight();
  3208. cm.setSize(600, lines*textHeight);
  3209. cm.setCursor(lineNum, 0);
  3210. helpers.doKeys(k1, k2);
  3211. zVals[idx] = cm.getScrollInfo().top;
  3212. }, { value: (function(){
  3213. return new Array(500).join('\n');
  3214. })()});
  3215. });
  3216. testVim('zb_to_bottom', function(cm, vim, helpers){
  3217. var lineNum = 250;
  3218. cm.setSize(600, 35*cm.defaultTextHeight());
  3219. cm.setCursor(lineNum, 0);
  3220. helpers.doKeys('z', 'b');
  3221. var scrollInfo = cm.getScrollInfo();
  3222. eq(scrollInfo.top + scrollInfo.clientHeight, cm.charCoords(Pos(lineNum, 0), 'local').bottom);
  3223. }, { value: (function(){
  3224. return new Array(500).join('\n');
  3225. })()});
  3226. testVim('zt_to_top', function(cm, vim, helpers){
  3227. var lineNum = 250;
  3228. cm.setSize(600, 35*cm.defaultTextHeight());
  3229. cm.setCursor(lineNum, 0);
  3230. helpers.doKeys('z', 't');
  3231. eq(cm.getScrollInfo().top, cm.charCoords(Pos(lineNum, 0), 'local').top);
  3232. }, { value: (function(){
  3233. return new Array(500).join('\n');
  3234. })()});
  3235. testVim('zb<zz', function(cm, vim, helpers){
  3236. eq(zVals[0]<zVals[1], true);
  3237. });
  3238. testVim('zz<zt', function(cm, vim, helpers){
  3239. eq(zVals[1]<zVals[2], true);
  3240. });
  3241. testVim('zb==z-', function(cm, vim, helpers){
  3242. eq(zVals[0], zVals[3]);
  3243. });
  3244. testVim('zz==z.', function(cm, vim, helpers){
  3245. eq(zVals[1], zVals[4]);
  3246. });
  3247. testVim('zt==z<CR>', function(cm, vim, helpers){
  3248. eq(zVals[2], zVals[5]);
  3249. });
  3250. var moveTillCharacterSandbox =
  3251. 'The quick brown fox \n';
  3252. testVim('moveTillCharacter', function(cm, vim, helpers){
  3253. cm.setCursor(0, 0);
  3254. // Search for the 'q'.
  3255. cm.openDialog = helpers.fakeOpenDialog('q');
  3256. helpers.doKeys('/');
  3257. eq(4, cm.getCursor().ch);
  3258. // Jump to just before the first o in the list.
  3259. helpers.doKeys('t');
  3260. helpers.doKeys('o');
  3261. eq('The quick brown fox \n', cm.getValue());
  3262. // Delete that one character.
  3263. helpers.doKeys('d');
  3264. helpers.doKeys('t');
  3265. helpers.doKeys('o');
  3266. eq('The quick bown fox \n', cm.getValue());
  3267. // Delete everything until the next 'o'.
  3268. helpers.doKeys('.');
  3269. eq('The quick box \n', cm.getValue());
  3270. // An unmatched character should have no effect.
  3271. helpers.doKeys('d');
  3272. helpers.doKeys('t');
  3273. helpers.doKeys('q');
  3274. eq('The quick box \n', cm.getValue());
  3275. // Matches should only be possible on single lines.
  3276. helpers.doKeys('d');
  3277. helpers.doKeys('t');
  3278. helpers.doKeys('z');
  3279. eq('The quick box \n', cm.getValue());
  3280. // After all that, the search for 'q' should still be active, so the 'N' command
  3281. // can run it again in reverse. Use that to delete everything back to the 'q'.
  3282. helpers.doKeys('d');
  3283. helpers.doKeys('N');
  3284. eq('The ox \n', cm.getValue());
  3285. eq(4, cm.getCursor().ch);
  3286. }, { value: moveTillCharacterSandbox});
  3287. testVim('searchForPipe', function(cm, vim, helpers){
  3288. CodeMirror.Vim.setOption('pcre', false);
  3289. cm.setCursor(0, 0);
  3290. // Search for the '|'.
  3291. cm.openDialog = helpers.fakeOpenDialog('|');
  3292. helpers.doKeys('/');
  3293. eq(4, cm.getCursor().ch);
  3294. }, { value: 'this|that'});
  3295. var scrollMotionSandbox =
  3296. '\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';
  3297. testVim('scrollMotion', function(cm, vim, helpers){
  3298. var prevCursor, prevScrollInfo;
  3299. cm.setCursor(0, 0);
  3300. // ctrl-y at the top of the file should have no effect.
  3301. helpers.doKeys('<C-y>');
  3302. eq(0, cm.getCursor().line);
  3303. prevScrollInfo = cm.getScrollInfo();
  3304. helpers.doKeys('<C-e>');
  3305. eq(1, cm.getCursor().line);
  3306. is(prevScrollInfo.top < cm.getScrollInfo().top);
  3307. // Jump to the end of the sandbox.
  3308. cm.setCursor(1000, 0);
  3309. prevCursor = cm.getCursor();
  3310. // ctrl-e at the bottom of the file should have no effect.
  3311. helpers.doKeys('<C-e>');
  3312. eq(prevCursor.line, cm.getCursor().line);
  3313. prevScrollInfo = cm.getScrollInfo();
  3314. helpers.doKeys('<C-y>');
  3315. eq(prevCursor.line - 1, cm.getCursor().line, "Y");
  3316. is(prevScrollInfo.top > cm.getScrollInfo().top);
  3317. }, { value: scrollMotionSandbox});
  3318. var squareBracketMotionSandbox = ''+
  3319. '({\n'+//0
  3320. ' ({\n'+//11
  3321. ' /*comment {\n'+//2
  3322. ' */(\n'+//3
  3323. '#else \n'+//4
  3324. ' /* )\n'+//5
  3325. '#if }\n'+//6
  3326. ' )}*/\n'+//7
  3327. ')}\n'+//8
  3328. '{}\n'+//9
  3329. '#else {{\n'+//10
  3330. '{}\n'+//11
  3331. '}\n'+//12
  3332. '{\n'+//13
  3333. '#endif\n'+//14
  3334. '}\n'+//15
  3335. '}\n'+//16
  3336. '#else';//17
  3337. testVim('[[, ]]', function(cm, vim, helpers) {
  3338. cm.setCursor(0, 0);
  3339. helpers.doKeys(']', ']');
  3340. helpers.assertCursorAt(9,0);
  3341. helpers.doKeys('2', ']', ']');
  3342. helpers.assertCursorAt(13,0);
  3343. helpers.doKeys(']', ']');
  3344. helpers.assertCursorAt(17,0);
  3345. helpers.doKeys('[', '[');
  3346. helpers.assertCursorAt(13,0);
  3347. helpers.doKeys('2', '[', '[');
  3348. helpers.assertCursorAt(9,0);
  3349. helpers.doKeys('[', '[');
  3350. helpers.assertCursorAt(0,0);
  3351. }, { value: squareBracketMotionSandbox});
  3352. testVim('[], ][', function(cm, vim, helpers) {
  3353. cm.setCursor(0, 0);
  3354. helpers.doKeys(']', '[');
  3355. helpers.assertCursorAt(12,0);
  3356. helpers.doKeys('2', ']', '[');
  3357. helpers.assertCursorAt(16,0);
  3358. helpers.doKeys(']', '[');
  3359. helpers.assertCursorAt(17,0);
  3360. helpers.doKeys('[', ']');
  3361. helpers.assertCursorAt(16,0);
  3362. helpers.doKeys('2', '[', ']');
  3363. helpers.assertCursorAt(12,0);
  3364. helpers.doKeys('[', ']');
  3365. helpers.assertCursorAt(0,0);
  3366. }, { value: squareBracketMotionSandbox});
  3367. testVim('[{, ]}', function(cm, vim, helpers) {
  3368. cm.setCursor(4, 10);
  3369. helpers.doKeys('[', '{');
  3370. helpers.assertCursorAt(2,12);
  3371. helpers.doKeys('2', '[', '{');
  3372. helpers.assertCursorAt(0,1);
  3373. cm.setCursor(4, 10);
  3374. helpers.doKeys(']', '}');
  3375. helpers.assertCursorAt(6,11);
  3376. helpers.doKeys('2', ']', '}');
  3377. helpers.assertCursorAt(8,1);
  3378. cm.setCursor(0,1);
  3379. helpers.doKeys(']', '}');
  3380. helpers.assertCursorAt(8,1);
  3381. helpers.doKeys('[', '{');
  3382. helpers.assertCursorAt(0,1);
  3383. }, { value: squareBracketMotionSandbox});
  3384. testVim('[(, ])', function(cm, vim, helpers) {
  3385. cm.setCursor(4, 10);
  3386. helpers.doKeys('[', '(');
  3387. helpers.assertCursorAt(3,14);
  3388. helpers.doKeys('2', '[', '(');
  3389. helpers.assertCursorAt(0,0);
  3390. cm.setCursor(4, 10);
  3391. helpers.doKeys(']', ')');
  3392. helpers.assertCursorAt(5,11);
  3393. helpers.doKeys('2', ']', ')');
  3394. helpers.assertCursorAt(8,0);
  3395. helpers.doKeys('[', '(');
  3396. helpers.assertCursorAt(0,0);
  3397. helpers.doKeys(']', ')');
  3398. helpers.assertCursorAt(8,0);
  3399. }, { value: squareBracketMotionSandbox});
  3400. testVim('[*, ]*, [/, ]/', function(cm, vim, helpers) {
  3401. forEach(['*', '/'], function(key){
  3402. cm.setCursor(7, 0);
  3403. helpers.doKeys('2', '[', key);
  3404. helpers.assertCursorAt(2,2);
  3405. helpers.doKeys('2', ']', key);
  3406. helpers.assertCursorAt(7,5);
  3407. });
  3408. }, { value: squareBracketMotionSandbox});
  3409. testVim('[#, ]#', function(cm, vim, helpers) {
  3410. cm.setCursor(10, 3);
  3411. helpers.doKeys('2', '[', '#');
  3412. helpers.assertCursorAt(4,0);
  3413. helpers.doKeys('5', ']', '#');
  3414. helpers.assertCursorAt(17,0);
  3415. cm.setCursor(10, 3);
  3416. helpers.doKeys(']', '#');
  3417. helpers.assertCursorAt(14,0);
  3418. }, { value: squareBracketMotionSandbox});
  3419. testVim('[m, ]m, [M, ]M', function(cm, vim, helpers) {
  3420. cm.setCursor(11, 0);
  3421. helpers.doKeys('[', 'm');
  3422. helpers.assertCursorAt(10,7);
  3423. helpers.doKeys('4', '[', 'm');
  3424. helpers.assertCursorAt(1,3);
  3425. helpers.doKeys('5', ']', 'm');
  3426. helpers.assertCursorAt(11,0);
  3427. helpers.doKeys('[', 'M');
  3428. helpers.assertCursorAt(9,1);
  3429. helpers.doKeys('3', ']', 'M');
  3430. helpers.assertCursorAt(15,0);
  3431. helpers.doKeys('5', '[', 'M');
  3432. helpers.assertCursorAt(7,3);
  3433. }, { value: squareBracketMotionSandbox});
  3434. testVim('i_indent_right', function(cm, vim, helpers) {
  3435. cm.setCursor(0, 3);
  3436. var expectedValue = ' word1\nword2\nword3 ';
  3437. helpers.doKeys('i', '<C-t>');
  3438. eq(expectedValue, cm.getValue());
  3439. helpers.assertCursorAt(0, 5);
  3440. }, { value: ' word1\nword2\nword3 ', indentUnit: 2 });
  3441. testVim('i_indent_left', function(cm, vim, helpers) {
  3442. cm.setCursor(0, 3);
  3443. var expectedValue = ' word1\nword2\nword3 ';
  3444. helpers.doKeys('i', '<C-d>');
  3445. eq(expectedValue, cm.getValue());
  3446. helpers.assertCursorAt(0, 1);
  3447. }, { value: ' word1\nword2\nword3 ', indentUnit: 2 });
  3448. // Ex mode tests
  3449. testVim('ex_go_to_line', function(cm, vim, helpers) {
  3450. cm.setCursor(0, 0);
  3451. helpers.doEx('4');
  3452. helpers.assertCursorAt(3, 0);
  3453. }, { value: 'a\nb\nc\nd\ne\n'});
  3454. testVim('ex_go_to_mark', function(cm, vim, helpers) {
  3455. cm.setCursor(3, 0);
  3456. helpers.doKeys('m', 'a');
  3457. cm.setCursor(0, 0);
  3458. helpers.doEx('\'a');
  3459. helpers.assertCursorAt(3, 0);
  3460. }, { value: 'a\nb\nc\nd\ne\n'});
  3461. testVim('ex_go_to_line_offset', function(cm, vim, helpers) {
  3462. cm.setCursor(0, 0);
  3463. helpers.doEx('+3');
  3464. helpers.assertCursorAt(3, 0);
  3465. helpers.doEx('-1');
  3466. helpers.assertCursorAt(2, 0);
  3467. helpers.doEx('.2');
  3468. helpers.assertCursorAt(4, 0);
  3469. helpers.doEx('.-3');
  3470. helpers.assertCursorAt(1, 0);
  3471. }, { value: 'a\nb\nc\nd\ne\n'});
  3472. testVim('ex_go_to_mark_offset', function(cm, vim, helpers) {
  3473. cm.setCursor(2, 0);
  3474. helpers.doKeys('m', 'a');
  3475. cm.setCursor(0, 0);
  3476. helpers.doEx('\'a1');
  3477. helpers.assertCursorAt(3, 0);
  3478. helpers.doEx('\'a-1');
  3479. helpers.assertCursorAt(1, 0);
  3480. helpers.doEx('\'a+2');
  3481. helpers.assertCursorAt(4, 0);
  3482. }, { value: 'a\nb\nc\nd\ne\n'});
  3483. testVim('ex_write', function(cm, vim, helpers) {
  3484. var tmp = CodeMirror.commands.save;
  3485. var written;
  3486. var actualCm;
  3487. CodeMirror.commands.save = function(cm) {
  3488. written = true;
  3489. actualCm = cm;
  3490. };
  3491. // Test that w, wr, wri ... write all trigger :write.
  3492. var command = 'write';
  3493. for (var i = 1; i < command.length; i++) {
  3494. written = false;
  3495. actualCm = null;
  3496. helpers.doEx(command.substring(0, i));
  3497. eq(written, true);
  3498. eq(actualCm, cm);
  3499. }
  3500. CodeMirror.commands.save = tmp;
  3501. });
  3502. testVim('ex_sort', function(cm, vim, helpers) {
  3503. helpers.doEx('sort');
  3504. eq('Z\na\nb\nc\nd', cm.getValue());
  3505. }, { value: 'b\nZ\nd\nc\na'});
  3506. testVim('ex_sort_reverse', function(cm, vim, helpers) {
  3507. helpers.doEx('sort!');
  3508. eq('d\nc\nb\na', cm.getValue());
  3509. }, { value: 'b\nd\nc\na'});
  3510. testVim('ex_sort_range', function(cm, vim, helpers) {
  3511. helpers.doEx('2,3sort');
  3512. eq('b\nc\nd\na', cm.getValue());
  3513. }, { value: 'b\nd\nc\na'});
  3514. testVim('ex_sort_oneline', function(cm, vim, helpers) {
  3515. helpers.doEx('2sort');
  3516. // Expect no change.
  3517. eq('b\nd\nc\na', cm.getValue());
  3518. }, { value: 'b\nd\nc\na'});
  3519. testVim('ex_sort_ignoreCase', function(cm, vim, helpers) {
  3520. helpers.doEx('sort i');
  3521. eq('a\nb\nc\nd\nZ', cm.getValue());
  3522. }, { value: 'b\nZ\nd\nc\na'});
  3523. testVim('ex_sort_unique', function(cm, vim, helpers) {
  3524. helpers.doEx('sort u');
  3525. eq('Z\na\nb\nc\nd', cm.getValue());
  3526. }, { value: 'b\nZ\na\na\nd\na\nc\na'});
  3527. testVim('ex_sort_decimal', function(cm, vim, helpers) {
  3528. helpers.doEx('sort d');
  3529. eq('d3\n s5\n6\n.9', cm.getValue());
  3530. }, { value: '6\nd3\n s5\n.9'});
  3531. testVim('ex_sort_decimal_negative', function(cm, vim, helpers) {
  3532. helpers.doEx('sort d');
  3533. eq('z-9\nd3\n s5\n6\n.9', cm.getValue());
  3534. }, { value: '6\nd3\n s5\n.9\nz-9'});
  3535. testVim('ex_sort_decimal_reverse', function(cm, vim, helpers) {
  3536. helpers.doEx('sort! d');
  3537. eq('.9\n6\n s5\nd3', cm.getValue());
  3538. }, { value: '6\nd3\n s5\n.9'});
  3539. testVim('ex_sort_hex', function(cm, vim, helpers) {
  3540. helpers.doEx('sort x');
  3541. eq(' s5\n6\n.9\n&0xB\nd3', cm.getValue());
  3542. }, { value: '6\nd3\n s5\n&0xB\n.9'});
  3543. testVim('ex_sort_octal', function(cm, vim, helpers) {
  3544. helpers.doEx('sort o');
  3545. eq('.9\n.8\nd3\n s5\n6', cm.getValue());
  3546. }, { value: '6\nd3\n s5\n.9\n.8'});
  3547. testVim('ex_sort_decimal_mixed', function(cm, vim, helpers) {
  3548. helpers.doEx('sort d');
  3549. eq('z\ny\nc1\nb2\na3', cm.getValue());
  3550. }, { value: 'a3\nz\nc1\ny\nb2'});
  3551. testVim('ex_sort_decimal_mixed_reverse', function(cm, vim, helpers) {
  3552. helpers.doEx('sort! d');
  3553. eq('a3\nb2\nc1\nz\ny', cm.getValue());
  3554. }, { value: 'a3\nz\nc1\ny\nb2'});
  3555. testVim('ex_sort_pattern_alpha', function(cm, vim, helpers) {
  3556. helpers.doEx('sort /[a-z]/');
  3557. eq('a3\nb2\nc1\ny\nz', cm.getValue());
  3558. }, { value: 'z\ny\nc1\nb2\na3'});
  3559. testVim('ex_sort_pattern_alpha_reverse', function(cm, vim, helpers) {
  3560. helpers.doEx('sort! /[a-z]/');
  3561. eq('z\ny\nc1\nb2\na3', cm.getValue());
  3562. }, { value: 'z\ny\nc1\nb2\na3'});
  3563. testVim('ex_sort_pattern_alpha_ignoreCase', function(cm, vim, helpers) {
  3564. helpers.doEx('sort i/[a-z]/');
  3565. eq('a3\nb2\nC1\nY\nz', cm.getValue());
  3566. }, { value: 'z\nY\nC1\nb2\na3'});
  3567. testVim('ex_sort_pattern_alpha_longer', function(cm, vim, helpers) {
  3568. helpers.doEx('sort /[a-z]+/');
  3569. eq('a\naa\nab\nade\nadele\nadelle\nadriana\nalex\nalexandra\nb\nc\ny\nz', cm.getValue());
  3570. }, { value: 'z\nab\naa\nade\nadelle\nalexandra\nalex\nadriana\nadele\ny\nc\nb\na'});
  3571. testVim('ex_sort_pattern_alpha_only', function(cm, vim, helpers) {
  3572. helpers.doEx('sort /^[a-z]$/');
  3573. eq('z1\ny2\na3\nb\nc', cm.getValue());
  3574. }, { value: 'z1\ny2\na3\nc\nb'});
  3575. testVim('ex_sort_pattern_alpha_only_reverse', function(cm, vim, helpers) {
  3576. helpers.doEx('sort! /^[a-z]$/');
  3577. eq('c\nb\nz1\ny2\na3', cm.getValue());
  3578. }, { value: 'z1\ny2\na3\nc\nb'});
  3579. testVim('ex_sort_pattern_alpha_num', function(cm, vim, helpers) {
  3580. helpers.doEx('sort /[a-z][0-9]/');
  3581. eq('c\nb\na3\ny2\nz1', cm.getValue());
  3582. }, { value: 'z1\ny2\na3\nc\nb'});
  3583. // test for :global command
  3584. testVim('ex_global', function(cm, vim, helpers) {
  3585. cm.setCursor(0, 0);
  3586. helpers.doEx('g/one/s//two');
  3587. eq('two two\n two two\n two two', cm.getValue());
  3588. helpers.doEx('1,2g/two/s//one');
  3589. eq('one one\n one one\n two two', cm.getValue());
  3590. }, {value: 'one one\n one one\n one one'});
  3591. testVim('ex_global_confirm', function(cm, vim, helpers) {
  3592. cm.setCursor(0, 0);
  3593. var onKeyDown;
  3594. var openDialogSave = cm.openDialog;
  3595. var KEYCODES = {
  3596. a: 65,
  3597. n: 78,
  3598. q: 81,
  3599. y: 89
  3600. };
  3601. // Intercept the ex command, 'global'
  3602. cm.openDialog = function(template, callback, options) {
  3603. // Intercept the prompt for the embedded ex command, 'substitute'
  3604. cm.openDialog = function(template, callback, options) {
  3605. onKeyDown = options.onKeyDown;
  3606. };
  3607. callback('g/one/s//two/gc');
  3608. };
  3609. helpers.doKeys(':');
  3610. var close = function() {};
  3611. onKeyDown({keyCode: KEYCODES.n}, '', close);
  3612. onKeyDown({keyCode: KEYCODES.y}, '', close);
  3613. onKeyDown({keyCode: KEYCODES.a}, '', close);
  3614. onKeyDown({keyCode: KEYCODES.q}, '', close);
  3615. onKeyDown({keyCode: KEYCODES.y}, '', close);
  3616. eq('one two\n two two\n one one\n two one\n one one', cm.getValue());
  3617. }, {value: 'one one\n one one\n one one\n one one\n one one'});
  3618. // Basic substitute tests.
  3619. testVim('ex_substitute_same_line', function(cm, vim, helpers) {
  3620. cm.setCursor(1, 0);
  3621. helpers.doEx('s/one/two/g');
  3622. eq('one one\n two two', cm.getValue());
  3623. }, { value: 'one one\n one one'});
  3624. testVim('ex_substitute_alternate_separator', function(cm, vim, helpers) {
  3625. cm.setCursor(1, 0);
  3626. helpers.doEx('s#o/e#two#g');
  3627. eq('o/e o/e\n two two', cm.getValue());
  3628. }, { value: 'o/e o/e\n o/e o/e'});
  3629. testVim('ex_substitute_full_file', function(cm, vim, helpers) {
  3630. cm.setCursor(1, 0);
  3631. helpers.doEx('%s/one/two/g');
  3632. eq('two two\n two two', cm.getValue());
  3633. }, { value: 'one one\n one one'});
  3634. testVim('ex_substitute_input_range', function(cm, vim, helpers) {
  3635. cm.setCursor(1, 0);
  3636. helpers.doEx('1,3s/\\d/0/g');
  3637. eq('0\n0\n0\n4', cm.getValue());
  3638. }, { value: '1\n2\n3\n4' });
  3639. testVim('ex_substitute_range_current_to_input', function(cm, vim, helpers) {
  3640. cm.setCursor(1, 0);
  3641. helpers.doEx('.,3s/\\d/0/g');
  3642. eq('1\n0\n0\n4', cm.getValue());
  3643. }, { value: '1\n2\n3\n4' });
  3644. testVim('ex_substitute_range_input_to_current', function(cm, vim, helpers) {
  3645. cm.setCursor(3, 0);
  3646. helpers.doEx('2,.s/\\d/0/g');
  3647. eq('1\n0\n0\n0\n5', cm.getValue());
  3648. }, { value: '1\n2\n3\n4\n5' });
  3649. testVim('ex_substitute_range_offset', function(cm, vim, helpers) {
  3650. cm.setCursor(2, 0);
  3651. helpers.doEx('-1,+1s/\\d/0/g');
  3652. eq('1\n0\n0\n0\n5', cm.getValue());
  3653. }, { value: '1\n2\n3\n4\n5' });
  3654. testVim('ex_substitute_range_implicit_offset', function(cm, vim, helpers) {
  3655. cm.setCursor(0, 0);
  3656. helpers.doEx('.1,.3s/\\d/0/g');
  3657. eq('1\n0\n0\n0\n5', cm.getValue());
  3658. }, { value: '1\n2\n3\n4\n5' });
  3659. testVim('ex_substitute_to_eof', function(cm, vim, helpers) {
  3660. cm.setCursor(2, 0);
  3661. helpers.doEx('.,$s/\\d/0/g');
  3662. eq('1\n2\n0\n0\n0', cm.getValue());
  3663. }, { value: '1\n2\n3\n4\n5' });
  3664. testVim('ex_substitute_to_relative_eof', function(cm, vim, helpers) {
  3665. cm.setCursor(4, 0);
  3666. helpers.doEx('2,$-2s/\\d/0/g');
  3667. eq('1\n0\n0\n4\n5', cm.getValue());
  3668. }, { value: '1\n2\n3\n4\n5' });
  3669. testVim('ex_substitute_range_mark', function(cm, vim, helpers) {
  3670. cm.setCursor(2, 0);
  3671. helpers.doKeys('ma');
  3672. cm.setCursor(0, 0);
  3673. helpers.doEx('.,\'as/\\d/0/g');
  3674. eq('0\n0\n0\n4\n5', cm.getValue());
  3675. }, { value: '1\n2\n3\n4\n5' });
  3676. testVim('ex_substitute_range_mark_offset', function(cm, vim, helpers) {
  3677. cm.setCursor(2, 0);
  3678. helpers.doKeys('ma');
  3679. cm.setCursor(0, 0);
  3680. helpers.doEx('\'a-1,\'a+1s/\\d/0/g');
  3681. eq('1\n0\n0\n0\n5', cm.getValue());
  3682. }, { value: '1\n2\n3\n4\n5' });
  3683. testVim('ex_substitute_visual_range', function(cm, vim, helpers) {
  3684. cm.setCursor(1, 0);
  3685. // Set last visual mode selection marks '< and '> at lines 2 and 4
  3686. helpers.doKeys('V', '2', 'j', 'v');
  3687. helpers.doEx('\'<,\'>s/\\d/0/g');
  3688. eq('1\n0\n0\n0\n5', cm.getValue());
  3689. }, { value: '1\n2\n3\n4\n5' });
  3690. testVim('ex_substitute_empty_query', function(cm, vim, helpers) {
  3691. // If the query is empty, use last query.
  3692. cm.setCursor(1, 0);
  3693. cm.openDialog = helpers.fakeOpenDialog('1');
  3694. helpers.doKeys('/');
  3695. helpers.doEx('s//b/g');
  3696. eq('abb ab2 ab3', cm.getValue());
  3697. }, { value: 'a11 a12 a13' });
  3698. testVim('ex_substitute_javascript', function(cm, vim, helpers) {
  3699. CodeMirror.Vim.setOption('pcre', false);
  3700. cm.setCursor(1, 0);
  3701. // Throw all the things that javascript likes to treat as special values
  3702. // into the replace part. All should be literal (this is VIM).
  3703. helpers.doEx('s/\\(\\d+\\)/$$ $\' $` $& \\1/g')
  3704. eq('a $$ $\' $` $& 0 b', cm.getValue());
  3705. }, { value: 'a 0 b' });
  3706. testVim('ex_substitute_empty_arguments', function(cm,vim,helpers) {
  3707. cm.setCursor(0, 0);
  3708. helpers.doEx('s/a/b/g');
  3709. cm.setCursor(1, 0);
  3710. helpers.doEx('s');
  3711. eq('b b\nb a', cm.getValue());
  3712. }, {value: 'a a\na a'});
  3713. // More complex substitute tests that test both pcre and nopcre options.
  3714. function testSubstitute(name, options) {
  3715. testVim(name + '_pcre', function(cm, vim, helpers) {
  3716. cm.setCursor(1, 0);
  3717. CodeMirror.Vim.setOption('pcre', true);
  3718. helpers.doEx(options.expr);
  3719. eq(options.expectedValue, cm.getValue());
  3720. }, options);
  3721. // If no noPcreExpr is defined, assume that it's the same as the expr.
  3722. var noPcreExpr = options.noPcreExpr ? options.noPcreExpr : options.expr;
  3723. testVim(name + '_nopcre', function(cm, vim, helpers) {
  3724. cm.setCursor(1, 0);
  3725. CodeMirror.Vim.setOption('pcre', false);
  3726. helpers.doEx(noPcreExpr);
  3727. eq(options.expectedValue, cm.getValue());
  3728. }, options);
  3729. }
  3730. testSubstitute('ex_substitute_capture', {
  3731. value: 'a11 a12 a13',
  3732. expectedValue: 'a1111 a1212 a1313',
  3733. // $n is a backreference
  3734. expr: 's/(\\d+)/$1$1/g',
  3735. // \n is a backreference.
  3736. noPcreExpr: 's/\\(\\d+\\)/\\1\\1/g'});
  3737. testSubstitute('ex_substitute_capture2', {
  3738. value: 'a 0 b',
  3739. expectedValue: 'a $00 b',
  3740. expr: 's/(\\d+)/$$$1$1/g',
  3741. noPcreExpr: 's/\\(\\d+\\)/$\\1\\1/g'});
  3742. testSubstitute('ex_substitute_nocapture', {
  3743. value: 'a11 a12 a13',
  3744. expectedValue: 'a$1$1 a$1$1 a$1$1',
  3745. expr: 's/(\\d+)/$$1$$1/g',
  3746. noPcreExpr: 's/\\(\\d+\\)/$1$1/g'});
  3747. testSubstitute('ex_substitute_nocapture2', {
  3748. value: 'a 0 b',
  3749. expectedValue: 'a $10 b',
  3750. expr: 's/(\\d+)/$$1$1/g',
  3751. noPcreExpr: 's/\\(\\d+\\)/\\$1\\1/g'});
  3752. testSubstitute('ex_substitute_nocapture', {
  3753. value: 'a b c',
  3754. expectedValue: 'a $ c',
  3755. expr: 's/b/$$/',
  3756. noPcreExpr: 's/b/$/'});
  3757. testSubstitute('ex_substitute_slash_regex', {
  3758. value: 'one/two \n three/four',
  3759. expectedValue: 'one|two \n three|four',
  3760. expr: '%s/\\//|'});
  3761. testSubstitute('ex_substitute_pipe_regex', {
  3762. value: 'one|two \n three|four',
  3763. expectedValue: 'one,two \n three,four',
  3764. expr: '%s/\\|/,/',
  3765. noPcreExpr: '%s/|/,/'});
  3766. testSubstitute('ex_substitute_or_regex', {
  3767. value: 'one|two \n three|four',
  3768. expectedValue: 'ana|twa \n thraa|faar',
  3769. expr: '%s/o|e|u/a/g',
  3770. noPcreExpr: '%s/o\\|e\\|u/a/g'});
  3771. testSubstitute('ex_substitute_or_word_regex', {
  3772. value: 'one|two \n three|four',
  3773. expectedValue: 'five|five \n three|four',
  3774. expr: '%s/(one|two)/five/g',
  3775. noPcreExpr: '%s/\\(one\\|two\\)/five/g'});
  3776. testSubstitute('ex_substitute_backslashslash_regex', {
  3777. value: 'one\\two \n three\\four',
  3778. expectedValue: 'one,two \n three,four',
  3779. expr: '%s/\\\\/,'});
  3780. testSubstitute('ex_substitute_slash_replacement', {
  3781. value: 'one,two \n three,four',
  3782. expectedValue: 'one/two \n three/four',
  3783. expr: '%s/,/\\/'});
  3784. testSubstitute('ex_substitute_backslash_replacement', {
  3785. value: 'one,two \n three,four',
  3786. expectedValue: 'one\\two \n three\\four',
  3787. expr: '%s/,/\\\\/g'});
  3788. testSubstitute('ex_substitute_multibackslash_replacement', {
  3789. value: 'one,two \n three,four',
  3790. expectedValue: 'one\\\\\\\\two \n three\\\\\\\\four', // 2*8 backslashes.
  3791. expr: '%s/,/\\\\\\\\\\\\\\\\/g'}); // 16 backslashes.
  3792. testSubstitute('ex_substitute_dollar_match', {
  3793. value: 'one,two \n three,four',
  3794. expectedValue: 'one,two ,\n three,four',
  3795. expr: '%s/$/,/g'});
  3796. testSubstitute('ex_substitute_newline_match', {
  3797. value: 'one,two \n three,four',
  3798. expectedValue: 'one,two , three,four',
  3799. expr: '%s/\\n/,/g'});
  3800. testSubstitute('ex_substitute_newline_replacement', {
  3801. value: 'one,two \n three,four',
  3802. expectedValue: 'one\ntwo \n three\nfour',
  3803. expr: '%s/,/\\n/g'});
  3804. testSubstitute('ex_substitute_braces_word', {
  3805. value: 'ababab abb ab{2}',
  3806. expectedValue: 'ab abb ab{2}',
  3807. expr: '%s/(ab){2}//g',
  3808. noPcreExpr: '%s/\\(ab\\)\\{2\\}//g'});
  3809. testSubstitute('ex_substitute_braces_range', {
  3810. value: 'a aa aaa aaaa',
  3811. expectedValue: 'a a',
  3812. expr: '%s/a{2,3}//g',
  3813. noPcreExpr: '%s/a\\{2,3\\}//g'});
  3814. testSubstitute('ex_substitute_braces_literal', {
  3815. value: 'ababab abb ab{2}',
  3816. expectedValue: 'ababab abb ',
  3817. expr: '%s/ab\\{2\\}//g',
  3818. noPcreExpr: '%s/ab{2}//g'});
  3819. testSubstitute('ex_substitute_braces_char', {
  3820. value: 'ababab abb ab{2}',
  3821. expectedValue: 'ababab ab{2}',
  3822. expr: '%s/ab{2}//g',
  3823. noPcreExpr: '%s/ab\\{2\\}//g'});
  3824. testSubstitute('ex_substitute_braces_no_escape', {
  3825. value: 'ababab abb ab{2}',
  3826. expectedValue: 'ababab ab{2}',
  3827. expr: '%s/ab{2}//g',
  3828. noPcreExpr: '%s/ab\\{2}//g'});
  3829. testSubstitute('ex_substitute_count', {
  3830. value: '1\n2\n3\n4',
  3831. expectedValue: '1\n0\n0\n4',
  3832. expr: 's/\\d/0/i 2'});
  3833. testSubstitute('ex_substitute_count_with_range', {
  3834. value: '1\n2\n3\n4',
  3835. expectedValue: '1\n2\n0\n0',
  3836. expr: '1,3s/\\d/0/ 3'});
  3837. testSubstitute('ex_substitute_not_global', {
  3838. value: 'aaa\nbaa\ncaa',
  3839. expectedValue: 'xaa\nbxa\ncxa',
  3840. expr: '%s/a/x/'});
  3841. function testSubstituteConfirm(name, command, initialValue, expectedValue, keys, finalPos) {
  3842. testVim(name, function(cm, vim, helpers) {
  3843. var savedOpenDialog = cm.openDialog;
  3844. var savedKeyName = CodeMirror.keyName;
  3845. var onKeyDown;
  3846. var recordedCallback;
  3847. var closed = true; // Start out closed, set false on second openDialog.
  3848. function close() {
  3849. closed = true;
  3850. }
  3851. // First openDialog should save callback.
  3852. cm.openDialog = function(template, callback, options) {
  3853. recordedCallback = callback;
  3854. }
  3855. // Do first openDialog.
  3856. helpers.doKeys(':');
  3857. // Second openDialog should save keyDown handler.
  3858. cm.openDialog = function(template, callback, options) {
  3859. onKeyDown = options.onKeyDown;
  3860. closed = false;
  3861. };
  3862. // Return the command to Vim and trigger second openDialog.
  3863. recordedCallback(command);
  3864. // The event should really use keyCode, but here just mock it out and use
  3865. // key and replace keyName to just return key.
  3866. CodeMirror.keyName = function (e) { return e.key; }
  3867. keys = keys.toUpperCase();
  3868. for (var i = 0; i < keys.length; i++) {
  3869. is(!closed);
  3870. onKeyDown({ key: keys.charAt(i) }, '', close);
  3871. }
  3872. try {
  3873. eq(expectedValue, cm.getValue());
  3874. helpers.assertCursorAt(finalPos);
  3875. is(closed);
  3876. } catch(e) {
  3877. throw e
  3878. } finally {
  3879. // Restore overridden functions.
  3880. CodeMirror.keyName = savedKeyName;
  3881. cm.openDialog = savedOpenDialog;
  3882. }
  3883. }, { value: initialValue });
  3884. }
  3885. testSubstituteConfirm('ex_substitute_confirm_emptydoc',
  3886. '%s/x/b/c', '', '', '', makeCursor(0, 0));
  3887. testSubstituteConfirm('ex_substitute_confirm_nomatch',
  3888. '%s/x/b/c', 'ba a\nbab', 'ba a\nbab', '', makeCursor(0, 0));
  3889. testSubstituteConfirm('ex_substitute_confirm_accept',
  3890. '%s/a/b/cg', 'ba a\nbab', 'bb b\nbbb', 'yyy', makeCursor(1, 1));
  3891. testSubstituteConfirm('ex_substitute_confirm_random_keys',
  3892. '%s/a/b/cg', 'ba a\nbab', 'bb b\nbbb', 'ysdkywerty', makeCursor(1, 1));
  3893. testSubstituteConfirm('ex_substitute_confirm_some',
  3894. '%s/a/b/cg', 'ba a\nbab', 'bb a\nbbb', 'yny', makeCursor(1, 1));
  3895. testSubstituteConfirm('ex_substitute_confirm_all',
  3896. '%s/a/b/cg', 'ba a\nbab', 'bb b\nbbb', 'a', makeCursor(1, 1));
  3897. testSubstituteConfirm('ex_substitute_confirm_accept_then_all',
  3898. '%s/a/b/cg', 'ba a\nbab', 'bb b\nbbb', 'ya', makeCursor(1, 1));
  3899. testSubstituteConfirm('ex_substitute_confirm_quit',
  3900. '%s/a/b/cg', 'ba a\nbab', 'bb a\nbab', 'yq', makeCursor(0, 3));
  3901. testSubstituteConfirm('ex_substitute_confirm_last',
  3902. '%s/a/b/cg', 'ba a\nbab', 'bb b\nbab', 'yl', makeCursor(0, 3));
  3903. testSubstituteConfirm('ex_substitute_confirm_oneline',
  3904. '1s/a/b/cg', 'ba a\nbab', 'bb b\nbab', 'yl', makeCursor(0, 3));
  3905. testSubstituteConfirm('ex_substitute_confirm_range_accept',
  3906. '1,2s/a/b/cg', 'aa\na \na\na', 'bb\nb \na\na', 'yyy', makeCursor(1, 0));
  3907. testSubstituteConfirm('ex_substitute_confirm_range_some',
  3908. '1,3s/a/b/cg', 'aa\na \na\na', 'ba\nb \nb\na', 'ynyy', makeCursor(2, 0));
  3909. testSubstituteConfirm('ex_substitute_confirm_range_all',
  3910. '1,3s/a/b/cg', 'aa\na \na\na', 'bb\nb \nb\na', 'a', makeCursor(2, 0));
  3911. testSubstituteConfirm('ex_substitute_confirm_range_last',
  3912. '1,3s/a/b/cg', 'aa\na \na\na', 'bb\nb \na\na', 'yyl', makeCursor(1, 0));
  3913. //:noh should clear highlighting of search-results but allow to resume search through n
  3914. testVim('ex_noh_clearSearchHighlight', function(cm, vim, helpers) {
  3915. cm.openDialog = helpers.fakeOpenDialog('match');
  3916. helpers.doKeys('?');
  3917. helpers.doEx('noh');
  3918. eq(vim.searchState_.getOverlay(),null,'match-highlighting wasn\'t cleared');
  3919. helpers.doKeys('n');
  3920. helpers.assertCursorAt(0, 11,'can\'t resume search after clearing highlighting');
  3921. }, { value: 'match nope match \n nope Match' });
  3922. testVim('ex_yank', function (cm, vim, helpers) {
  3923. var curStart = makeCursor(3, 0);
  3924. cm.setCursor(curStart);
  3925. helpers.doEx('y');
  3926. var register = helpers.getRegisterController().getRegister();
  3927. var line = cm.getLine(3);
  3928. eq(line + '\n', register.toString());
  3929. });
  3930. testVim('set_boolean', function(cm, vim, helpers) {
  3931. CodeMirror.Vim.defineOption('testoption', true, 'boolean');
  3932. // Test default value is set.
  3933. is(CodeMirror.Vim.getOption('testoption'));
  3934. try {
  3935. // Test fail to set to non-boolean
  3936. CodeMirror.Vim.setOption('testoption', '5');
  3937. fail();
  3938. } catch (expected) {}
  3939. // Test setOption
  3940. CodeMirror.Vim.setOption('testoption', false);
  3941. is(!CodeMirror.Vim.getOption('testoption'));
  3942. });
  3943. testVim('ex_set_boolean', function(cm, vim, helpers) {
  3944. CodeMirror.Vim.defineOption('testoption', true, 'boolean');
  3945. // Test default value is set.
  3946. is(CodeMirror.Vim.getOption('testoption'));
  3947. try {
  3948. // Test fail to set to non-boolean
  3949. helpers.doEx('set testoption=22');
  3950. fail();
  3951. } catch (expected) {}
  3952. // Test setOption
  3953. helpers.doEx('set notestoption');
  3954. is(!CodeMirror.Vim.getOption('testoption'));
  3955. });
  3956. testVim('set_string', function(cm, vim, helpers) {
  3957. CodeMirror.Vim.defineOption('testoption', 'a', 'string');
  3958. // Test default value is set.
  3959. eq('a', CodeMirror.Vim.getOption('testoption'));
  3960. try {
  3961. // Test fail to set non-string.
  3962. CodeMirror.Vim.setOption('testoption', true);
  3963. fail();
  3964. } catch (expected) {}
  3965. try {
  3966. // Test fail to set 'notestoption'
  3967. CodeMirror.Vim.setOption('notestoption', 'b');
  3968. fail();
  3969. } catch (expected) {}
  3970. // Test setOption
  3971. CodeMirror.Vim.setOption('testoption', 'c');
  3972. eq('c', CodeMirror.Vim.getOption('testoption'));
  3973. });
  3974. testVim('ex_set_string', function(cm, vim, helpers) {
  3975. CodeMirror.Vim.defineOption('testopt', 'a', 'string');
  3976. // Test default value is set.
  3977. eq('a', CodeMirror.Vim.getOption('testopt'));
  3978. try {
  3979. // Test fail to set 'notestopt'
  3980. helpers.doEx('set notestopt=b');
  3981. fail();
  3982. } catch (expected) {}
  3983. // Test setOption
  3984. helpers.doEx('set testopt=c')
  3985. eq('c', CodeMirror.Vim.getOption('testopt'));
  3986. helpers.doEx('set testopt=c')
  3987. eq('c', CodeMirror.Vim.getOption('testopt', cm)); //local || global
  3988. eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'})); // local
  3989. eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'})); // global
  3990. eq('c', CodeMirror.Vim.getOption('testopt')); // global
  3991. // Test setOption global
  3992. helpers.doEx('setg testopt=d')
  3993. eq('c', CodeMirror.Vim.getOption('testopt', cm));
  3994. eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'}));
  3995. eq('d', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'}));
  3996. eq('d', CodeMirror.Vim.getOption('testopt'));
  3997. // Test setOption local
  3998. helpers.doEx('setl testopt=e')
  3999. eq('e', CodeMirror.Vim.getOption('testopt', cm));
  4000. eq('e', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'}));
  4001. eq('d', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'}));
  4002. eq('d', CodeMirror.Vim.getOption('testopt'));
  4003. });
  4004. testVim('ex_set_callback', function(cm, vim, helpers) {
  4005. var global;
  4006. function cb(val, cm, cfg) {
  4007. if (val === undefined) {
  4008. // Getter
  4009. if (cm) {
  4010. return cm._local;
  4011. } else {
  4012. return global;
  4013. }
  4014. } else {
  4015. // Setter
  4016. if (cm) {
  4017. cm._local = val;
  4018. } else {
  4019. global = val;
  4020. }
  4021. }
  4022. }
  4023. CodeMirror.Vim.defineOption('testopt', 'a', 'string', cb);
  4024. // Test default value is set.
  4025. eq('a', CodeMirror.Vim.getOption('testopt'));
  4026. try {
  4027. // Test fail to set 'notestopt'
  4028. helpers.doEx('set notestopt=b');
  4029. fail();
  4030. } catch (expected) {}
  4031. // Test setOption (Identical to the string tests, but via callback instead)
  4032. helpers.doEx('set testopt=c')
  4033. eq('c', CodeMirror.Vim.getOption('testopt', cm)); //local || global
  4034. eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'})); // local
  4035. eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'})); // global
  4036. eq('c', CodeMirror.Vim.getOption('testopt')); // global
  4037. // Test setOption global
  4038. helpers.doEx('setg testopt=d')
  4039. eq('c', CodeMirror.Vim.getOption('testopt', cm));
  4040. eq('c', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'}));
  4041. eq('d', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'}));
  4042. eq('d', CodeMirror.Vim.getOption('testopt'));
  4043. // Test setOption local
  4044. helpers.doEx('setl testopt=e')
  4045. eq('e', CodeMirror.Vim.getOption('testopt', cm));
  4046. eq('e', CodeMirror.Vim.getOption('testopt', cm, {scope: 'local'}));
  4047. eq('d', CodeMirror.Vim.getOption('testopt', cm, {scope: 'global'}));
  4048. eq('d', CodeMirror.Vim.getOption('testopt'));
  4049. })
  4050. testVim('ex_set_filetype', function(cm, vim, helpers) {
  4051. CodeMirror.defineMode('test_mode', function() {
  4052. return {token: function(stream) {
  4053. stream.match(/^\s+|^\S+/);
  4054. }};
  4055. });
  4056. CodeMirror.defineMode('test_mode_2', function() {
  4057. return {token: function(stream) {
  4058. stream.match(/^\s+|^\S+/);
  4059. }};
  4060. });
  4061. // Test mode is set.
  4062. helpers.doEx('set filetype=test_mode');
  4063. eq('test_mode', cm.getMode().name);
  4064. // Test 'ft' alias also sets mode.
  4065. helpers.doEx('set ft=test_mode_2');
  4066. eq('test_mode_2', cm.getMode().name);
  4067. });
  4068. testVim('ex_set_filetype_null', function(cm, vim, helpers) {
  4069. CodeMirror.defineMode('test_mode', function() {
  4070. return {token: function(stream) {
  4071. stream.match(/^\s+|^\S+/);
  4072. }};
  4073. });
  4074. cm.setOption('mode', 'test_mode');
  4075. // Test mode is set to null.
  4076. helpers.doEx('set filetype=');
  4077. eq('null', cm.getMode().name);
  4078. });
  4079. // TODO: Reset key maps after each test.
  4080. testVim('ex_map_key2key', function(cm, vim, helpers) {
  4081. helpers.doEx('map a x');
  4082. helpers.doKeys('a');
  4083. helpers.assertCursorAt(0, 0);
  4084. eq('bc', cm.getValue());
  4085. }, { value: 'abc' });
  4086. testVim('ex_unmap_key2key', function(cm, vim, helpers) {
  4087. helpers.doEx('unmap a');
  4088. helpers.doKeys('a');
  4089. eq('vim-insert', cm.getOption('keyMap'));
  4090. }, { value: 'abc' });
  4091. testVim('ex_unmap_key2key_does_not_remove_default', function(cm, vim, helpers) {
  4092. try {
  4093. helpers.doEx('unmap a');
  4094. fail();
  4095. } catch (expected) {}
  4096. helpers.doKeys('a');
  4097. eq('vim-insert', cm.getOption('keyMap'));
  4098. }, { value: 'abc' });
  4099. testVim('ex_map_key2key_to_colon', function(cm, vim, helpers) {
  4100. helpers.doEx('map ; :');
  4101. var dialogOpened = false;
  4102. cm.openDialog = function() {
  4103. dialogOpened = true;
  4104. }
  4105. helpers.doKeys(';');
  4106. eq(dialogOpened, true);
  4107. });
  4108. testVim('ex_map_ex2key:', function(cm, vim, helpers) {
  4109. helpers.doEx('map :del x');
  4110. helpers.doEx('del');
  4111. helpers.assertCursorAt(0, 0);
  4112. eq('bc', cm.getValue());
  4113. }, { value: 'abc' });
  4114. testVim('ex_map_ex2ex', function(cm, vim, helpers) {
  4115. helpers.doEx('map :del :w');
  4116. var tmp = CodeMirror.commands.save;
  4117. var written = false;
  4118. var actualCm;
  4119. CodeMirror.commands.save = function(cm) {
  4120. written = true;
  4121. actualCm = cm;
  4122. };
  4123. helpers.doEx('del');
  4124. CodeMirror.commands.save = tmp;
  4125. eq(written, true);
  4126. eq(actualCm, cm);
  4127. });
  4128. testVim('ex_map_key2ex', function(cm, vim, helpers) {
  4129. helpers.doEx('map a :w');
  4130. var tmp = CodeMirror.commands.save;
  4131. var written = false;
  4132. var actualCm;
  4133. CodeMirror.commands.save = function(cm) {
  4134. written = true;
  4135. actualCm = cm;
  4136. };
  4137. helpers.doKeys('a');
  4138. CodeMirror.commands.save = tmp;
  4139. eq(written, true);
  4140. eq(actualCm, cm);
  4141. });
  4142. testVim('ex_map_key2key_visual_api', function(cm, vim, helpers) {
  4143. CodeMirror.Vim.map('b', ':w', 'visual');
  4144. var tmp = CodeMirror.commands.save;
  4145. var written = false;
  4146. var actualCm;
  4147. CodeMirror.commands.save = function(cm) {
  4148. written = true;
  4149. actualCm = cm;
  4150. };
  4151. // Mapping should not work in normal mode.
  4152. helpers.doKeys('b');
  4153. eq(written, false);
  4154. // Mapping should work in visual mode.
  4155. helpers.doKeys('v', 'b');
  4156. eq(written, true);
  4157. eq(actualCm, cm);
  4158. CodeMirror.commands.save = tmp;
  4159. });
  4160. testVim('ex_imap', function(cm, vim, helpers) {
  4161. CodeMirror.Vim.map('jk', '<Esc>', 'insert');
  4162. helpers.doKeys('i');
  4163. is(vim.insertMode);
  4164. helpers.doKeys('j', 'k');
  4165. is(!vim.insertMode);
  4166. cm.setCursor(0, 1);
  4167. CodeMirror.Vim.map('jj', '<Esc>', 'insert');
  4168. helpers.doKeys('<C-v>', '2', 'j', 'l', 'c');
  4169. var replacement = fillArray('fo', 3);
  4170. cm.replaceSelections(replacement);
  4171. eq('1fo4\n5fo8\nafodefg', cm.getValue());
  4172. helpers.doKeys('j', 'j');
  4173. cm.setCursor(0, 0);
  4174. helpers.doKeys('.');
  4175. eq('foo4\nfoo8\nfoodefg', cm.getValue());
  4176. }, { value: '1234\n5678\nabcdefg' });
  4177. testVim('ex_unmap_api', function(cm, vim, helpers) {
  4178. CodeMirror.Vim.map('<Alt-X>', 'gg', 'normal');
  4179. is(CodeMirror.Vim.handleKey(cm, "<Alt-X>", "normal"), "Alt-X key is mapped");
  4180. CodeMirror.Vim.unmap("<Alt-X>", "normal");
  4181. is(!CodeMirror.Vim.handleKey(cm, "<Alt-X>", "normal"), "Alt-X key is unmapped");
  4182. });
  4183. // Testing registration of functions as ex-commands and mapping to <Key>-keys
  4184. testVim('ex_api_test', function(cm, vim, helpers) {
  4185. var res=false;
  4186. var val='from';
  4187. CodeMirror.Vim.defineEx('extest','ext',function(cm,params){
  4188. if(params.args)val=params.args[0];
  4189. else res=true;
  4190. });
  4191. helpers.doEx(':ext to');
  4192. eq(val,'to','Defining ex-command failed');
  4193. CodeMirror.Vim.map('<C-CR><Space>',':ext');
  4194. helpers.doKeys('<C-CR>','<Space>');
  4195. is(res,'Mapping to key failed');
  4196. });
  4197. // For now, this test needs to be last because it messes up : for future tests.
  4198. testVim('ex_map_key2key_from_colon', function(cm, vim, helpers) {
  4199. helpers.doEx('map : x');
  4200. helpers.doKeys(':');
  4201. helpers.assertCursorAt(0, 0);
  4202. eq('bc', cm.getValue());
  4203. }, { value: 'abc' });
  4204. // Test event handlers
  4205. testVim('beforeSelectionChange', function(cm, vim, helpers) {
  4206. cm.setCursor(0, 100);
  4207. eqCursorPos(cm.getCursor('head'), cm.getCursor('anchor'));
  4208. }, { value: 'abc' });
  4209. testVim('increment_binary', function(cm, vim, helpers) {
  4210. cm.setCursor(0, 4);
  4211. helpers.doKeys('<C-a>');
  4212. eq('0b001', cm.getValue());
  4213. helpers.doKeys('<C-a>');
  4214. eq('0b010', cm.getValue());
  4215. helpers.doKeys('<C-x>');
  4216. eq('0b001', cm.getValue());
  4217. helpers.doKeys('<C-x>');
  4218. eq('0b000', cm.getValue());
  4219. cm.setCursor(0, 0);
  4220. helpers.doKeys('<C-a>');
  4221. eq('0b001', cm.getValue());
  4222. helpers.doKeys('<C-a>');
  4223. eq('0b010', cm.getValue());
  4224. helpers.doKeys('<C-x>');
  4225. eq('0b001', cm.getValue());
  4226. helpers.doKeys('<C-x>');
  4227. eq('0b000', cm.getValue());
  4228. }, { value: '0b000' });
  4229. testVim('increment_octal', function(cm, vim, helpers) {
  4230. cm.setCursor(0, 2);
  4231. helpers.doKeys('<C-a>');
  4232. eq('001', cm.getValue());
  4233. helpers.doKeys('<C-a>');
  4234. eq('002', cm.getValue());
  4235. helpers.doKeys('<C-a>');
  4236. eq('003', cm.getValue());
  4237. helpers.doKeys('<C-a>');
  4238. eq('004', cm.getValue());
  4239. helpers.doKeys('<C-a>');
  4240. eq('005', cm.getValue());
  4241. helpers.doKeys('<C-a>');
  4242. eq('006', cm.getValue());
  4243. helpers.doKeys('<C-a>');
  4244. eq('007', cm.getValue());
  4245. helpers.doKeys('<C-a>');
  4246. eq('010', cm.getValue());
  4247. helpers.doKeys('<C-x>');
  4248. eq('007', cm.getValue());
  4249. helpers.doKeys('<C-x>');
  4250. eq('006', cm.getValue());
  4251. helpers.doKeys('<C-x>');
  4252. eq('005', cm.getValue());
  4253. helpers.doKeys('<C-x>');
  4254. eq('004', cm.getValue());
  4255. helpers.doKeys('<C-x>');
  4256. eq('003', cm.getValue());
  4257. helpers.doKeys('<C-x>');
  4258. eq('002', cm.getValue());
  4259. helpers.doKeys('<C-x>');
  4260. eq('001', cm.getValue());
  4261. helpers.doKeys('<C-x>');
  4262. eq('000', cm.getValue());
  4263. cm.setCursor(0, 0);
  4264. helpers.doKeys('<C-a>');
  4265. eq('001', cm.getValue());
  4266. helpers.doKeys('<C-a>');
  4267. eq('002', cm.getValue());
  4268. helpers.doKeys('<C-x>');
  4269. eq('001', cm.getValue());
  4270. helpers.doKeys('<C-x>');
  4271. eq('000', cm.getValue());
  4272. }, { value: '000' });
  4273. testVim('increment_decimal', function(cm, vim, helpers) {
  4274. cm.setCursor(0, 2);
  4275. helpers.doKeys('<C-a>');
  4276. eq('101', cm.getValue());
  4277. helpers.doKeys('<C-a>');
  4278. eq('102', cm.getValue());
  4279. helpers.doKeys('<C-a>');
  4280. eq('103', cm.getValue());
  4281. helpers.doKeys('<C-a>');
  4282. eq('104', cm.getValue());
  4283. helpers.doKeys('<C-a>');
  4284. eq('105', cm.getValue());
  4285. helpers.doKeys('<C-a>');
  4286. eq('106', cm.getValue());
  4287. helpers.doKeys('<C-a>');
  4288. eq('107', cm.getValue());
  4289. helpers.doKeys('<C-a>');
  4290. eq('108', cm.getValue());
  4291. helpers.doKeys('<C-a>');
  4292. eq('109', cm.getValue());
  4293. helpers.doKeys('<C-a>');
  4294. eq('110', cm.getValue());
  4295. helpers.doKeys('<C-x>');
  4296. eq('109', cm.getValue());
  4297. helpers.doKeys('<C-x>');
  4298. eq('108', cm.getValue());
  4299. helpers.doKeys('<C-x>');
  4300. eq('107', cm.getValue());
  4301. helpers.doKeys('<C-x>');
  4302. eq('106', cm.getValue());
  4303. helpers.doKeys('<C-x>');
  4304. eq('105', cm.getValue());
  4305. helpers.doKeys('<C-x>');
  4306. eq('104', cm.getValue());
  4307. helpers.doKeys('<C-x>');
  4308. eq('103', cm.getValue());
  4309. helpers.doKeys('<C-x>');
  4310. eq('102', cm.getValue());
  4311. helpers.doKeys('<C-x>');
  4312. eq('101', cm.getValue());
  4313. helpers.doKeys('<C-x>');
  4314. eq('100', cm.getValue());
  4315. cm.setCursor(0, 0);
  4316. helpers.doKeys('<C-a>');
  4317. eq('101', cm.getValue());
  4318. helpers.doKeys('<C-a>');
  4319. eq('102', cm.getValue());
  4320. helpers.doKeys('<C-x>');
  4321. eq('101', cm.getValue());
  4322. helpers.doKeys('<C-x>');
  4323. eq('100', cm.getValue());
  4324. }, { value: '100' });
  4325. testVim('increment_decimal_single_zero', function(cm, vim, helpers) {
  4326. helpers.doKeys('<C-a>');
  4327. eq('1', cm.getValue());
  4328. helpers.doKeys('<C-a>');
  4329. eq('2', cm.getValue());
  4330. helpers.doKeys('<C-a>');
  4331. eq('3', cm.getValue());
  4332. helpers.doKeys('<C-a>');
  4333. eq('4', cm.getValue());
  4334. helpers.doKeys('<C-a>');
  4335. eq('5', cm.getValue());
  4336. helpers.doKeys('<C-a>');
  4337. eq('6', cm.getValue());
  4338. helpers.doKeys('<C-a>');
  4339. eq('7', cm.getValue());
  4340. helpers.doKeys('<C-a>');
  4341. eq('8', cm.getValue());
  4342. helpers.doKeys('<C-a>');
  4343. eq('9', cm.getValue());
  4344. helpers.doKeys('<C-a>');
  4345. eq('10', cm.getValue());
  4346. helpers.doKeys('<C-x>');
  4347. eq('9', cm.getValue());
  4348. helpers.doKeys('<C-x>');
  4349. eq('8', cm.getValue());
  4350. helpers.doKeys('<C-x>');
  4351. eq('7', cm.getValue());
  4352. helpers.doKeys('<C-x>');
  4353. eq('6', cm.getValue());
  4354. helpers.doKeys('<C-x>');
  4355. eq('5', cm.getValue());
  4356. helpers.doKeys('<C-x>');
  4357. eq('4', cm.getValue());
  4358. helpers.doKeys('<C-x>');
  4359. eq('3', cm.getValue());
  4360. helpers.doKeys('<C-x>');
  4361. eq('2', cm.getValue());
  4362. helpers.doKeys('<C-x>');
  4363. eq('1', cm.getValue());
  4364. helpers.doKeys('<C-x>');
  4365. eq('0', cm.getValue());
  4366. cm.setCursor(0, 0);
  4367. helpers.doKeys('<C-a>');
  4368. eq('1', cm.getValue());
  4369. helpers.doKeys('<C-a>');
  4370. eq('2', cm.getValue());
  4371. helpers.doKeys('<C-x>');
  4372. eq('1', cm.getValue());
  4373. helpers.doKeys('<C-x>');
  4374. eq('0', cm.getValue());
  4375. }, { value: '0' });
  4376. testVim('increment_hexadecimal', function(cm, vim, helpers) {
  4377. cm.setCursor(0, 2);
  4378. helpers.doKeys('<C-a>');
  4379. eq('0x1', cm.getValue());
  4380. helpers.doKeys('<C-a>');
  4381. eq('0x2', cm.getValue());
  4382. helpers.doKeys('<C-a>');
  4383. eq('0x3', cm.getValue());
  4384. helpers.doKeys('<C-a>');
  4385. eq('0x4', cm.getValue());
  4386. helpers.doKeys('<C-a>');
  4387. eq('0x5', cm.getValue());
  4388. helpers.doKeys('<C-a>');
  4389. eq('0x6', cm.getValue());
  4390. helpers.doKeys('<C-a>');
  4391. eq('0x7', cm.getValue());
  4392. helpers.doKeys('<C-a>');
  4393. eq('0x8', cm.getValue());
  4394. helpers.doKeys('<C-a>');
  4395. eq('0x9', cm.getValue());
  4396. helpers.doKeys('<C-a>');
  4397. eq('0xa', cm.getValue());
  4398. helpers.doKeys('<C-a>');
  4399. eq('0xb', cm.getValue());
  4400. helpers.doKeys('<C-a>');
  4401. eq('0xc', cm.getValue());
  4402. helpers.doKeys('<C-a>');
  4403. eq('0xd', cm.getValue());
  4404. helpers.doKeys('<C-a>');
  4405. eq('0xe', cm.getValue());
  4406. helpers.doKeys('<C-a>');
  4407. eq('0xf', cm.getValue());
  4408. helpers.doKeys('<C-a>');
  4409. eq('0x10', cm.getValue());
  4410. helpers.doKeys('<C-x>');
  4411. eq('0x0f', cm.getValue());
  4412. helpers.doKeys('<C-x>');
  4413. eq('0x0e', cm.getValue());
  4414. helpers.doKeys('<C-x>');
  4415. eq('0x0d', cm.getValue());
  4416. helpers.doKeys('<C-x>');
  4417. eq('0x0c', cm.getValue());
  4418. helpers.doKeys('<C-x>');
  4419. eq('0x0b', cm.getValue());
  4420. helpers.doKeys('<C-x>');
  4421. eq('0x0a', cm.getValue());
  4422. helpers.doKeys('<C-x>');
  4423. eq('0x09', cm.getValue());
  4424. helpers.doKeys('<C-x>');
  4425. eq('0x08', cm.getValue());
  4426. helpers.doKeys('<C-x>');
  4427. eq('0x07', cm.getValue());
  4428. helpers.doKeys('<C-x>');
  4429. eq('0x06', cm.getValue());
  4430. helpers.doKeys('<C-x>');
  4431. eq('0x05', cm.getValue());
  4432. helpers.doKeys('<C-x>');
  4433. eq('0x04', cm.getValue());
  4434. helpers.doKeys('<C-x>');
  4435. eq('0x03', cm.getValue());
  4436. helpers.doKeys('<C-x>');
  4437. eq('0x02', cm.getValue());
  4438. helpers.doKeys('<C-x>');
  4439. eq('0x01', cm.getValue());
  4440. helpers.doKeys('<C-x>');
  4441. eq('0x00', cm.getValue());
  4442. cm.setCursor(0, 0);
  4443. helpers.doKeys('<C-a>');
  4444. eq('0x01', cm.getValue());
  4445. helpers.doKeys('<C-a>');
  4446. eq('0x02', cm.getValue());
  4447. helpers.doKeys('<C-x>');
  4448. eq('0x01', cm.getValue());
  4449. helpers.doKeys('<C-x>');
  4450. eq('0x00', cm.getValue());
  4451. }, { value: '0x0' });