text-editor-component-spec.js 231 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336
  1. const { conditionPromise } = require('./async-spec-helpers');
  2. const Random = require('random-seed');
  3. const { getRandomBufferRange, buildRandomLines } = require('./helpers/random');
  4. const TextEditorComponent = require('../src/text-editor-component');
  5. const TextEditorElement = require('../src/text-editor-element');
  6. const TextEditor = require('../src/text-editor');
  7. const TextBuffer = require('text-buffer');
  8. const { Point } = TextBuffer;
  9. const fs = require('fs');
  10. const path = require('path');
  11. const Grim = require('grim');
  12. const electron = require('electron');
  13. const clipboard = electron.clipboard;
  14. const SAMPLE_TEXT = fs.readFileSync(
  15. path.join(__dirname, 'fixtures', 'sample.js'),
  16. 'utf8'
  17. );
  18. class DummyElement extends HTMLElement {
  19. connectedCallback() {
  20. this.didAttach();
  21. }
  22. }
  23. window.customElements.define(
  24. 'text-editor-component-test-element',
  25. DummyElement
  26. );
  27. document.createElement('text-editor-component-test-element');
  28. const editors = [];
  29. let verticalScrollbarWidth, horizontalScrollbarHeight;
  30. describe('TextEditorComponent', () => {
  31. beforeEach(() => {
  32. jasmine.useRealClock();
  33. // Force scrollbars to be visible regardless of local system configuration
  34. const scrollbarStyle = document.createElement('style');
  35. scrollbarStyle.textContent =
  36. 'atom-text-editor ::-webkit-scrollbar { -webkit-appearance: none }';
  37. jasmine.attachToDOM(scrollbarStyle);
  38. if (verticalScrollbarWidth == null) {
  39. const { component, element } = buildComponent({
  40. text: 'abcdefgh\n'.repeat(10),
  41. width: 30,
  42. height: 30
  43. });
  44. verticalScrollbarWidth = getVerticalScrollbarWidth(component);
  45. horizontalScrollbarHeight = getHorizontalScrollbarHeight(component);
  46. element.remove();
  47. }
  48. });
  49. afterEach(() => {
  50. for (const editor of editors) {
  51. editor.destroy();
  52. }
  53. editors.length = 0;
  54. });
  55. describe('rendering', () => {
  56. it('renders lines and line numbers for the visible region', async () => {
  57. const { component, element, editor } = buildComponent({
  58. rowsPerTile: 3,
  59. autoHeight: false
  60. });
  61. expect(queryOnScreenLineNumberElements(element).length).toBe(13);
  62. expect(queryOnScreenLineElements(element).length).toBe(13);
  63. element.style.height = 4 * component.measurements.lineHeight + 'px';
  64. await component.getNextUpdatePromise();
  65. expect(queryOnScreenLineNumberElements(element).length).toBe(9);
  66. expect(queryOnScreenLineElements(element).length).toBe(9);
  67. await setScrollTop(component, 5 * component.getLineHeight());
  68. // After scrolling down beyond > 3 rows, the order of line numbers and lines
  69. // in the DOM is a bit weird because the first tile is recycled to the bottom
  70. // when it is scrolled out of view
  71. expect(
  72. queryOnScreenLineNumberElements(element).map(element =>
  73. element.textContent.trim()
  74. )
  75. ).toEqual(['10', '11', '12', '4', '5', '6', '7', '8', '9']);
  76. expect(
  77. queryOnScreenLineElements(element).map(
  78. element => element.dataset.screenRow
  79. )
  80. ).toEqual(['9', '10', '11', '3', '4', '5', '6', '7', '8']);
  81. expect(
  82. queryOnScreenLineElements(element).map(element => element.textContent)
  83. ).toEqual([
  84. editor.lineTextForScreenRow(9),
  85. ' ', // this line is blank in the model, but we render a space to prevent the line from collapsing vertically
  86. editor.lineTextForScreenRow(11),
  87. editor.lineTextForScreenRow(3),
  88. editor.lineTextForScreenRow(4),
  89. editor.lineTextForScreenRow(5),
  90. editor.lineTextForScreenRow(6),
  91. editor.lineTextForScreenRow(7),
  92. editor.lineTextForScreenRow(8)
  93. ]);
  94. await setScrollTop(component, 2.5 * component.getLineHeight());
  95. expect(
  96. queryOnScreenLineNumberElements(element).map(element =>
  97. element.textContent.trim()
  98. )
  99. ).toEqual(['1', '2', '3', '4', '5', '6', '7', '8', '9']);
  100. expect(
  101. queryOnScreenLineElements(element).map(
  102. element => element.dataset.screenRow
  103. )
  104. ).toEqual(['0', '1', '2', '3', '4', '5', '6', '7', '8']);
  105. expect(
  106. queryOnScreenLineElements(element).map(element => element.textContent)
  107. ).toEqual([
  108. editor.lineTextForScreenRow(0),
  109. editor.lineTextForScreenRow(1),
  110. editor.lineTextForScreenRow(2),
  111. editor.lineTextForScreenRow(3),
  112. editor.lineTextForScreenRow(4),
  113. editor.lineTextForScreenRow(5),
  114. editor.lineTextForScreenRow(6),
  115. editor.lineTextForScreenRow(7),
  116. editor.lineTextForScreenRow(8)
  117. ]);
  118. });
  119. it('bases the width of the lines div on the width of the longest initially-visible screen line', async () => {
  120. const { component, element, editor } = buildComponent({
  121. rowsPerTile: 2,
  122. height: 20,
  123. width: 100
  124. });
  125. {
  126. expect(editor.getApproximateLongestScreenRow()).toBe(3);
  127. const expectedWidth = Math.ceil(
  128. component.pixelPositionForScreenPosition(Point(3, Infinity)).left +
  129. component.getBaseCharacterWidth()
  130. );
  131. expect(element.querySelector('.lines').style.width).toBe(
  132. expectedWidth + 'px'
  133. );
  134. }
  135. {
  136. // Get the next update promise synchronously here to ensure we don't
  137. // miss the update while polling the condition.
  138. const nextUpdatePromise = component.getNextUpdatePromise();
  139. await conditionPromise(
  140. () => editor.getApproximateLongestScreenRow() === 6
  141. );
  142. await nextUpdatePromise;
  143. // Capture the width of the lines before requesting the width of
  144. // longest line, because making that request forces a DOM update
  145. const actualWidth = element.querySelector('.lines').style.width;
  146. const expectedWidth = Math.ceil(
  147. component.pixelPositionForScreenPosition(Point(6, Infinity)).left +
  148. component.getBaseCharacterWidth()
  149. );
  150. expect(actualWidth).toBe(expectedWidth + 'px');
  151. }
  152. // eslint-disable-next-line no-lone-blocks
  153. {
  154. // Make sure we do not throw an error if a synchronous update is
  155. // triggered before measuring the longest line from a
  156. // previously-scheduled update.
  157. editor.getBuffer().insert(Point(12, Infinity), 'x'.repeat(100));
  158. expect(editor.getLongestScreenRow()).toBe(12);
  159. TextEditorComponent.getScheduler().readDocument(() => {
  160. // This will happen before the measurement phase of the update
  161. // triggered above.
  162. component.pixelPositionForScreenPosition(Point(11, Infinity));
  163. });
  164. await component.getNextUpdatePromise();
  165. }
  166. });
  167. it('re-renders lines when their height changes', async () => {
  168. const { component, element } = buildComponent({
  169. rowsPerTile: 3,
  170. autoHeight: false
  171. });
  172. element.style.height = 4 * component.measurements.lineHeight + 'px';
  173. await component.getNextUpdatePromise();
  174. expect(queryOnScreenLineNumberElements(element).length).toBe(9);
  175. expect(queryOnScreenLineElements(element).length).toBe(9);
  176. element.style.lineHeight = '2.0';
  177. TextEditor.didUpdateStyles();
  178. await component.getNextUpdatePromise();
  179. expect(queryOnScreenLineNumberElements(element).length).toBe(6);
  180. expect(queryOnScreenLineElements(element).length).toBe(6);
  181. element.style.lineHeight = '0.7';
  182. TextEditor.didUpdateStyles();
  183. await component.getNextUpdatePromise();
  184. expect(queryOnScreenLineNumberElements(element).length).toBe(12);
  185. expect(queryOnScreenLineElements(element).length).toBe(12);
  186. element.style.lineHeight = '0.05';
  187. TextEditor.didUpdateStyles();
  188. await component.getNextUpdatePromise();
  189. expect(queryOnScreenLineNumberElements(element).length).toBe(13);
  190. expect(queryOnScreenLineElements(element).length).toBe(13);
  191. element.style.lineHeight = '0';
  192. TextEditor.didUpdateStyles();
  193. await component.getNextUpdatePromise();
  194. expect(queryOnScreenLineNumberElements(element).length).toBe(13);
  195. expect(queryOnScreenLineElements(element).length).toBe(13);
  196. element.style.lineHeight = '1';
  197. TextEditor.didUpdateStyles();
  198. await component.getNextUpdatePromise();
  199. expect(queryOnScreenLineNumberElements(element).length).toBe(9);
  200. expect(queryOnScreenLineElements(element).length).toBe(9);
  201. });
  202. it('makes the content at least as tall as the scroll container client height', async () => {
  203. const { component, editor } = buildComponent({
  204. text: 'a'.repeat(100),
  205. width: 50,
  206. height: 100
  207. });
  208. expect(component.refs.content.offsetHeight).toBe(
  209. 100 - getHorizontalScrollbarHeight(component)
  210. );
  211. editor.setText('a\n'.repeat(30));
  212. await component.getNextUpdatePromise();
  213. expect(component.refs.content.offsetHeight).toBeGreaterThan(100);
  214. expect(component.refs.content.offsetHeight).toBeNear(
  215. component.getContentHeight(),
  216. 2
  217. );
  218. });
  219. it('honors the scrollPastEnd option by adding empty space equivalent to the clientHeight to the end of the content area', async () => {
  220. const { component, editor } = buildComponent({
  221. autoHeight: false,
  222. autoWidth: false
  223. });
  224. await editor.update({ scrollPastEnd: true });
  225. await setEditorHeightInLines(component, 6);
  226. // scroll to end
  227. await setScrollTop(component, Infinity);
  228. expect(component.getFirstVisibleRow()).toBe(
  229. editor.getScreenLineCount() - 3
  230. );
  231. editor.update({ scrollPastEnd: false });
  232. await component.getNextUpdatePromise(); // wait for scrollable content resize
  233. expect(component.getFirstVisibleRow()).toBe(
  234. editor.getScreenLineCount() - 6
  235. );
  236. // Always allows at least 3 lines worth of overscroll if the editor is short
  237. await setEditorHeightInLines(component, 2);
  238. await editor.update({ scrollPastEnd: true });
  239. await setScrollTop(component, Infinity);
  240. expect(component.getFirstVisibleRow()).toBe(
  241. editor.getScreenLineCount() + 1
  242. );
  243. });
  244. it('does not fire onDidChangeScrollTop listeners when assigning the same maximal value and the content height has fractional pixels (regression)', async () => {
  245. const { component, element, editor } = buildComponent({
  246. autoHeight: false,
  247. autoWidth: false
  248. });
  249. await setEditorHeightInLines(component, 3);
  250. // Force a fractional content height with a block decoration
  251. const item = document.createElement('div');
  252. item.style.height = '10.6px';
  253. editor.decorateMarker(editor.markBufferPosition([0, 0]), {
  254. type: 'block',
  255. item
  256. });
  257. await component.getNextUpdatePromise();
  258. component.setScrollTop(Infinity);
  259. element.onDidChangeScrollTop(newScrollTop => {
  260. throw new Error('Scroll top should not have changed');
  261. });
  262. component.setScrollTop(component.getScrollTop());
  263. });
  264. it('gives the line number tiles an explicit width and height so their layout can be strictly contained', async () => {
  265. const { component, editor } = buildComponent({ rowsPerTile: 3 });
  266. const lineNumberGutterElement =
  267. component.refs.gutterContainer.refs.lineNumberGutter.element;
  268. expect(lineNumberGutterElement.offsetHeight).toBeNear(
  269. component.getScrollHeight()
  270. );
  271. for (const child of lineNumberGutterElement.children) {
  272. expect(child.offsetWidth).toBe(lineNumberGutterElement.offsetWidth);
  273. if (!child.classList.contains('line-number')) {
  274. for (const lineNumberElement of child.children) {
  275. expect(lineNumberElement.offsetWidth).toBe(
  276. lineNumberGutterElement.offsetWidth
  277. );
  278. }
  279. }
  280. }
  281. editor.setText('x\n'.repeat(99));
  282. await component.getNextUpdatePromise();
  283. expect(lineNumberGutterElement.offsetHeight).toBeNear(
  284. component.getScrollHeight()
  285. );
  286. for (const child of lineNumberGutterElement.children) {
  287. expect(child.offsetWidth).toBe(lineNumberGutterElement.offsetWidth);
  288. if (!child.classList.contains('line-number')) {
  289. for (const lineNumberElement of child.children) {
  290. expect(lineNumberElement.offsetWidth).toBe(
  291. lineNumberGutterElement.offsetWidth
  292. );
  293. }
  294. }
  295. }
  296. });
  297. it('keeps the number of tiles stable when the visible line count changes during vertical scrolling', async () => {
  298. const { component } = buildComponent({
  299. rowsPerTile: 3,
  300. autoHeight: false
  301. });
  302. await setEditorHeightInLines(component, 5.5);
  303. expect(component.refs.lineTiles.children.length).toBe(3 + 2); // account for cursors and highlights containers
  304. await setScrollTop(component, 0.5 * component.getLineHeight());
  305. expect(component.refs.lineTiles.children.length).toBe(3 + 2); // account for cursors and highlights containers
  306. await setScrollTop(component, 1 * component.getLineHeight());
  307. expect(component.refs.lineTiles.children.length).toBe(3 + 2); // account for cursors and highlights containers
  308. });
  309. it('recycles tiles on resize', async () => {
  310. const { component } = buildComponent({
  311. rowsPerTile: 2,
  312. autoHeight: false
  313. });
  314. await setEditorHeightInLines(component, 7);
  315. await setScrollTop(component, 3.5 * component.getLineHeight());
  316. const lineNode = lineNodeForScreenRow(component, 7);
  317. await setEditorHeightInLines(component, 4);
  318. expect(lineNodeForScreenRow(component, 7)).toBe(lineNode);
  319. });
  320. it("updates lines numbers when a row's foldability changes (regression)", async () => {
  321. const { component, editor } = buildComponent({ text: 'abc\n' });
  322. editor.setCursorBufferPosition([1, 0]);
  323. await component.getNextUpdatePromise();
  324. expect(
  325. lineNumberNodeForScreenRow(component, 0).querySelector('.foldable')
  326. ).toBeNull();
  327. editor.insertText(' def');
  328. await component.getNextUpdatePromise();
  329. expect(
  330. lineNumberNodeForScreenRow(component, 0).querySelector('.foldable')
  331. ).toBeDefined();
  332. editor.undo();
  333. await component.getNextUpdatePromise();
  334. expect(
  335. lineNumberNodeForScreenRow(component, 0).querySelector('.foldable')
  336. ).toBeNull();
  337. });
  338. it('shows the foldable icon on the last screen row of a buffer row that can be folded', async () => {
  339. const { component } = buildComponent({
  340. text: 'abc\n de\nfghijklm\n no',
  341. softWrapped: true
  342. });
  343. await setEditorWidthInCharacters(component, 5);
  344. expect(
  345. lineNumberNodeForScreenRow(component, 0).classList.contains('foldable')
  346. ).toBe(true);
  347. expect(
  348. lineNumberNodeForScreenRow(component, 1).classList.contains('foldable')
  349. ).toBe(false);
  350. expect(
  351. lineNumberNodeForScreenRow(component, 2).classList.contains('foldable')
  352. ).toBe(false);
  353. expect(
  354. lineNumberNodeForScreenRow(component, 3).classList.contains('foldable')
  355. ).toBe(true);
  356. expect(
  357. lineNumberNodeForScreenRow(component, 4).classList.contains('foldable')
  358. ).toBe(false);
  359. });
  360. it('renders dummy vertical and horizontal scrollbars when content overflows', async () => {
  361. const { component, editor } = buildComponent({
  362. height: 100,
  363. width: 100
  364. });
  365. const verticalScrollbar = component.refs.verticalScrollbar.element;
  366. const horizontalScrollbar = component.refs.horizontalScrollbar.element;
  367. expect(verticalScrollbar.scrollHeight).toBeNear(
  368. component.getContentHeight()
  369. );
  370. expect(horizontalScrollbar.scrollWidth).toBeNear(
  371. component.getContentWidth()
  372. );
  373. expect(getVerticalScrollbarWidth(component)).toBeGreaterThan(0);
  374. expect(getHorizontalScrollbarHeight(component)).toBeGreaterThan(0);
  375. expect(verticalScrollbar.style.bottom).toBe(
  376. getVerticalScrollbarWidth(component) + 'px'
  377. );
  378. expect(verticalScrollbar.style.visibility).toBe('');
  379. expect(horizontalScrollbar.style.right).toBe(
  380. getHorizontalScrollbarHeight(component) + 'px'
  381. );
  382. expect(horizontalScrollbar.style.visibility).toBe('');
  383. expect(component.refs.scrollbarCorner).toBeDefined();
  384. setScrollTop(component, 100);
  385. await setScrollLeft(component, 100);
  386. expect(verticalScrollbar.scrollTop).toBe(100);
  387. expect(horizontalScrollbar.scrollLeft).toBe(100);
  388. verticalScrollbar.scrollTop = 120;
  389. horizontalScrollbar.scrollLeft = 120;
  390. await component.getNextUpdatePromise();
  391. expect(component.getScrollTop()).toBe(120);
  392. expect(component.getScrollLeft()).toBe(120);
  393. editor.setText('a\n'.repeat(15));
  394. await component.getNextUpdatePromise();
  395. expect(getVerticalScrollbarWidth(component)).toBeGreaterThan(0);
  396. expect(getHorizontalScrollbarHeight(component)).toBe(0);
  397. expect(verticalScrollbar.style.visibility).toBe('');
  398. expect(horizontalScrollbar.style.visibility).toBe('hidden');
  399. editor.setText('a'.repeat(100));
  400. await component.getNextUpdatePromise();
  401. expect(getVerticalScrollbarWidth(component)).toBe(0);
  402. expect(getHorizontalScrollbarHeight(component)).toBeGreaterThan(0);
  403. expect(verticalScrollbar.style.visibility).toBe('hidden');
  404. expect(horizontalScrollbar.style.visibility).toBe('');
  405. editor.setText('');
  406. await component.getNextUpdatePromise();
  407. expect(getVerticalScrollbarWidth(component)).toBe(0);
  408. expect(getHorizontalScrollbarHeight(component)).toBe(0);
  409. expect(verticalScrollbar.style.visibility).toBe('hidden');
  410. expect(horizontalScrollbar.style.visibility).toBe('hidden');
  411. });
  412. describe('when scrollbar styles change or the editor element is detached and then reattached', () => {
  413. it('updates the bottom/right of dummy scrollbars and client height/width measurements', async () => {
  414. const { component, element, editor } = buildComponent({
  415. height: 100,
  416. width: 100
  417. });
  418. expect(getHorizontalScrollbarHeight(component)).toBeGreaterThan(10);
  419. expect(getVerticalScrollbarWidth(component)).toBeGreaterThan(10);
  420. setScrollTop(component, 20);
  421. setScrollLeft(component, 10);
  422. await component.getNextUpdatePromise();
  423. // Updating scrollbar styles.
  424. const style = document.createElement('style');
  425. style.textContent =
  426. '::-webkit-scrollbar { height: 10px; width: 10px; }';
  427. jasmine.attachToDOM(style);
  428. TextEditor.didUpdateScrollbarStyles();
  429. await component.getNextUpdatePromise();
  430. expect(getHorizontalScrollbarHeight(component)).toBeNear(10);
  431. expect(getVerticalScrollbarWidth(component)).toBeNear(10);
  432. expect(
  433. component.refs.horizontalScrollbar.element.style.right
  434. ).toHaveNearPixels('10px');
  435. expect(
  436. component.refs.verticalScrollbar.element.style.bottom
  437. ).toHaveNearPixels('10px');
  438. expect(component.refs.horizontalScrollbar.element.scrollLeft).toBeNear(
  439. 10
  440. );
  441. expect(component.refs.verticalScrollbar.element.scrollTop).toBeNear(20);
  442. expect(component.getScrollContainerClientHeight()).toBeNear(100 - 10);
  443. expect(component.getScrollContainerClientWidth()).toBeNear(
  444. 100 - component.getGutterContainerWidth() - 10
  445. );
  446. // Detaching and re-attaching the editor element.
  447. element.remove();
  448. jasmine.attachToDOM(element);
  449. expect(getHorizontalScrollbarHeight(component)).toBeNear(10);
  450. expect(getVerticalScrollbarWidth(component)).toBeNear(10);
  451. expect(
  452. component.refs.horizontalScrollbar.element.style.right
  453. ).toHaveNearPixels('10px');
  454. expect(
  455. component.refs.verticalScrollbar.element.style.bottom
  456. ).toHaveNearPixels('10px');
  457. expect(component.refs.horizontalScrollbar.element.scrollLeft).toBeNear(
  458. 10
  459. );
  460. expect(component.refs.verticalScrollbar.element.scrollTop).toBeNear(20);
  461. expect(component.getScrollContainerClientHeight()).toBeNear(100 - 10);
  462. expect(component.getScrollContainerClientWidth()).toBeNear(
  463. 100 - component.getGutterContainerWidth() - 10
  464. );
  465. // Ensure we don't throw an error trying to remeasure non-existent scrollbars for mini editors.
  466. await editor.update({ mini: true });
  467. TextEditor.didUpdateScrollbarStyles();
  468. component.scheduleUpdate();
  469. await component.getNextUpdatePromise();
  470. });
  471. });
  472. it('renders cursors within the visible row range', async () => {
  473. const { component, element, editor } = buildComponent({
  474. height: 40,
  475. rowsPerTile: 2
  476. });
  477. await setScrollTop(component, 100);
  478. expect(component.getRenderedStartRow()).toBe(4);
  479. expect(component.getRenderedEndRow()).toBe(10);
  480. editor.setCursorScreenPosition([0, 0], { autoscroll: false }); // out of view
  481. editor.addCursorAtScreenPosition([2, 2], { autoscroll: false }); // out of view
  482. editor.addCursorAtScreenPosition([4, 0], { autoscroll: false }); // line start
  483. editor.addCursorAtScreenPosition([4, 4], { autoscroll: false }); // at token boundary
  484. editor.addCursorAtScreenPosition([4, 6], { autoscroll: false }); // within token
  485. editor.addCursorAtScreenPosition([5, Infinity], { autoscroll: false }); // line end
  486. editor.addCursorAtScreenPosition([10, 2], { autoscroll: false }); // out of view
  487. await component.getNextUpdatePromise();
  488. let cursorNodes = Array.from(element.querySelectorAll('.cursor'));
  489. expect(cursorNodes.length).toBe(4);
  490. verifyCursorPosition(component, cursorNodes[0], 4, 0);
  491. verifyCursorPosition(component, cursorNodes[1], 4, 4);
  492. verifyCursorPosition(component, cursorNodes[2], 4, 6);
  493. verifyCursorPosition(component, cursorNodes[3], 5, 30);
  494. editor.setCursorScreenPosition([8, 11], { autoscroll: false });
  495. await component.getNextUpdatePromise();
  496. cursorNodes = Array.from(element.querySelectorAll('.cursor'));
  497. expect(cursorNodes.length).toBe(1);
  498. verifyCursorPosition(component, cursorNodes[0], 8, 11);
  499. editor.setCursorScreenPosition([0, 0], { autoscroll: false });
  500. await component.getNextUpdatePromise();
  501. cursorNodes = Array.from(element.querySelectorAll('.cursor'));
  502. expect(cursorNodes.length).toBe(0);
  503. editor.setSelectedScreenRange([[8, 0], [12, 0]], { autoscroll: false });
  504. await component.getNextUpdatePromise();
  505. cursorNodes = Array.from(element.querySelectorAll('.cursor'));
  506. expect(cursorNodes.length).toBe(0);
  507. });
  508. it('hides cursors with non-empty selections when showCursorOnSelection is false', async () => {
  509. const { component, element, editor } = buildComponent();
  510. editor.setSelectedScreenRanges([[[0, 0], [0, 3]], [[1, 0], [1, 0]]]);
  511. await component.getNextUpdatePromise();
  512. {
  513. const cursorNodes = Array.from(element.querySelectorAll('.cursor'));
  514. expect(cursorNodes.length).toBe(2);
  515. verifyCursorPosition(component, cursorNodes[0], 0, 3);
  516. verifyCursorPosition(component, cursorNodes[1], 1, 0);
  517. }
  518. editor.update({ showCursorOnSelection: false });
  519. await component.getNextUpdatePromise();
  520. {
  521. const cursorNodes = Array.from(element.querySelectorAll('.cursor'));
  522. expect(cursorNodes.length).toBe(1);
  523. verifyCursorPosition(component, cursorNodes[0], 1, 0);
  524. }
  525. editor.setSelectedScreenRanges([[[0, 0], [0, 3]], [[1, 0], [1, 4]]]);
  526. await component.getNextUpdatePromise();
  527. {
  528. const cursorNodes = Array.from(element.querySelectorAll('.cursor'));
  529. expect(cursorNodes.length).toBe(0);
  530. }
  531. });
  532. /**
  533. * TODO: FAILING TEST - This test fails with the following output:
  534. * Error: Timed out waiting on anonymous condition at
  535. * conditionPromise (/home/runner/work/pulsar/pulsar/spec/async-spec-helpers.js:20:13)
  536. */
  537. xit('blinks cursors when the editor is focused and the cursors are not moving', async () => {
  538. assertDocumentFocused();
  539. const { component, element, editor } = buildComponent();
  540. component.props.cursorBlinkPeriod = 30;
  541. component.props.cursorBlinkResumeDelay = 30;
  542. editor.addCursorAtScreenPosition([1, 0]);
  543. element.focus();
  544. await component.getNextUpdatePromise();
  545. const [cursor1, cursor2] = element.querySelectorAll('.cursor');
  546. await conditionPromise(
  547. () =>
  548. getComputedStyle(cursor1).opacity === '1' &&
  549. getComputedStyle(cursor2).opacity === '1'
  550. );
  551. await conditionPromise(
  552. () =>
  553. getComputedStyle(cursor1).opacity === '0' &&
  554. getComputedStyle(cursor2).opacity === '0'
  555. );
  556. await conditionPromise(
  557. () =>
  558. getComputedStyle(cursor1).opacity === '1' &&
  559. getComputedStyle(cursor2).opacity === '1'
  560. );
  561. editor.moveRight();
  562. await component.getNextUpdatePromise();
  563. expect(getComputedStyle(cursor1).opacity).toBe('1');
  564. expect(getComputedStyle(cursor2).opacity).toBe('1');
  565. });
  566. it('gives cursors at the end of lines the width of an "x" character', async () => {
  567. const { component, element, editor } = buildComponent();
  568. editor.setText('abcde');
  569. await setEditorWidthInCharacters(component, 5.5);
  570. editor.setCursorScreenPosition([0, Infinity]);
  571. await component.getNextUpdatePromise();
  572. expect(element.querySelector('.cursor').offsetWidth).toBe(
  573. Math.round(component.getBaseCharacterWidth())
  574. );
  575. // Clip cursor width when soft-wrap is on and the cursor is at the end of
  576. // the line. This prevents the parent tile from disabling sub-pixel
  577. // anti-aliasing. For some reason, adding overflow: hidden to the cursor
  578. // container doesn't solve this issue so we're adding this workaround instead.
  579. editor.setSoftWrapped(true);
  580. await component.getNextUpdatePromise();
  581. expect(element.querySelector('.cursor').offsetWidth).toBeLessThan(
  582. Math.round(component.getBaseCharacterWidth())
  583. );
  584. });
  585. it('positions and sizes cursors correctly when they are located next to a fold marker', async () => {
  586. const { component, element, editor } = buildComponent();
  587. editor.foldBufferRange([[0, 3], [0, 6]]);
  588. editor.setCursorScreenPosition([0, 3]);
  589. await component.getNextUpdatePromise();
  590. verifyCursorPosition(component, element.querySelector('.cursor'), 0, 3);
  591. editor.setCursorScreenPosition([0, 4]);
  592. await component.getNextUpdatePromise();
  593. verifyCursorPosition(component, element.querySelector('.cursor'), 0, 4);
  594. });
  595. it('positions cursors and placeholder text correctly when the lines container has a margin and/or is padded', async () => {
  596. const { component, element, editor } = buildComponent({
  597. placeholderText: 'testing'
  598. });
  599. component.refs.lineTiles.style.marginLeft = '10px';
  600. TextEditor.didUpdateStyles();
  601. await component.getNextUpdatePromise();
  602. editor.setCursorBufferPosition([0, 3]);
  603. await component.getNextUpdatePromise();
  604. verifyCursorPosition(component, element.querySelector('.cursor'), 0, 3);
  605. editor.setCursorScreenPosition([1, 0]);
  606. await component.getNextUpdatePromise();
  607. verifyCursorPosition(component, element.querySelector('.cursor'), 1, 0);
  608. component.refs.lineTiles.style.paddingTop = '5px';
  609. TextEditor.didUpdateStyles();
  610. await component.getNextUpdatePromise();
  611. verifyCursorPosition(component, element.querySelector('.cursor'), 1, 0);
  612. editor.setCursorScreenPosition([2, 2]);
  613. TextEditor.didUpdateStyles();
  614. await component.getNextUpdatePromise();
  615. verifyCursorPosition(component, element.querySelector('.cursor'), 2, 2);
  616. editor.setText('');
  617. await component.getNextUpdatePromise();
  618. const placeholderTextLeft = element
  619. .querySelector('.placeholder-text')
  620. .getBoundingClientRect().left;
  621. const linesLeft = component.refs.lineTiles.getBoundingClientRect().left;
  622. expect(placeholderTextLeft).toBe(linesLeft);
  623. });
  624. it('places the hidden input element at the location of the last cursor if it is visible', async () => {
  625. const { component, editor } = buildComponent({
  626. height: 60,
  627. width: 120,
  628. rowsPerTile: 2
  629. });
  630. const { hiddenInput } = component.refs.cursorsAndInput.refs;
  631. setScrollTop(component, 100);
  632. await setScrollLeft(component, 40);
  633. expect(component.getRenderedStartRow()).toBe(4);
  634. expect(component.getRenderedEndRow()).toBe(10);
  635. // When out of view, the hidden input is positioned at 0, 0
  636. expect(editor.getCursorScreenPosition()).toEqual([0, 0]);
  637. expect(hiddenInput.offsetTop).toBe(0);
  638. expect(hiddenInput.offsetLeft).toBe(0);
  639. // Otherwise it is positioned at the last cursor position
  640. editor.addCursorAtScreenPosition([7, 4]);
  641. await component.getNextUpdatePromise();
  642. expect(hiddenInput.getBoundingClientRect().top).toBe(
  643. clientTopForLine(component, 7)
  644. );
  645. expect(Math.round(hiddenInput.getBoundingClientRect().left)).toBeNear(
  646. clientLeftForCharacter(component, 7, 4)
  647. );
  648. });
  649. it('soft wraps lines based on the content width when soft wrap is enabled', async () => {
  650. let baseCharacterWidth, gutterContainerWidth;
  651. {
  652. const { component, editor } = buildComponent();
  653. baseCharacterWidth = component.getBaseCharacterWidth();
  654. gutterContainerWidth = component.getGutterContainerWidth();
  655. editor.destroy();
  656. }
  657. const { component, element, editor } = buildComponent({
  658. width: gutterContainerWidth + baseCharacterWidth * 55,
  659. attach: false
  660. });
  661. editor.setSoftWrapped(true);
  662. jasmine.attachToDOM(element);
  663. expect(getEditorWidthInBaseCharacters(component)).toBe(55);
  664. expect(lineNodeForScreenRow(component, 3).textContent).toBe(
  665. ' var pivot = items.shift(), current, left = [], '
  666. );
  667. expect(lineNodeForScreenRow(component, 4).textContent).toBe(
  668. ' right = [];'
  669. );
  670. const { scrollContainer } = component.refs;
  671. expect(scrollContainer.clientWidth).toBe(scrollContainer.scrollWidth);
  672. });
  673. it('correctly forces the display layer to index visible rows when resizing (regression)', async () => {
  674. const text = 'a'.repeat(30) + '\n' + 'b'.repeat(1000);
  675. const { component, element, editor } = buildComponent({
  676. height: 300,
  677. width: 800,
  678. attach: false,
  679. text
  680. });
  681. editor.setSoftWrapped(true);
  682. jasmine.attachToDOM(element);
  683. element.style.width = 200 + 'px';
  684. await component.getNextUpdatePromise();
  685. expect(queryOnScreenLineElements(element).length).toBe(24);
  686. });
  687. it('decorates the line numbers of folded lines', async () => {
  688. const { component, editor } = buildComponent();
  689. editor.foldBufferRow(1);
  690. await component.getNextUpdatePromise();
  691. expect(
  692. lineNumberNodeForScreenRow(component, 1).classList.contains('folded')
  693. ).toBe(true);
  694. });
  695. it('makes lines at least as wide as the scrollContainer', async () => {
  696. const { component, element, editor } = buildComponent();
  697. const { scrollContainer } = component.refs;
  698. editor.setText('a');
  699. await component.getNextUpdatePromise();
  700. expect(element.querySelector('.line').offsetWidth).toBe(
  701. scrollContainer.offsetWidth - verticalScrollbarWidth
  702. );
  703. });
  704. it('resizes based on the content when the autoHeight and/or autoWidth options are true', async () => {
  705. const { component, element, editor } = buildComponent({
  706. autoHeight: true,
  707. autoWidth: true
  708. });
  709. const editorPadding = 3;
  710. element.style.padding = editorPadding + 'px';
  711. const initialWidth = element.offsetWidth;
  712. const initialHeight = element.offsetHeight;
  713. expect(initialWidth).toBe(
  714. component.getGutterContainerWidth() +
  715. component.getContentWidth() +
  716. verticalScrollbarWidth +
  717. 2 * editorPadding
  718. );
  719. expect(initialHeight).toBeNear(
  720. component.getContentHeight() +
  721. horizontalScrollbarHeight +
  722. 2 * editorPadding
  723. );
  724. // When autoWidth is enabled, width adjusts to content
  725. editor.setCursorScreenPosition([6, Infinity]);
  726. editor.insertText('x'.repeat(50));
  727. await component.getNextUpdatePromise();
  728. expect(element.offsetWidth).toBe(
  729. component.getGutterContainerWidth() +
  730. component.getContentWidth() +
  731. verticalScrollbarWidth +
  732. 2 * editorPadding
  733. );
  734. expect(element.offsetWidth).toBeGreaterThan(initialWidth);
  735. // When autoHeight is enabled, height adjusts to content
  736. editor.insertText('\n'.repeat(5));
  737. await component.getNextUpdatePromise();
  738. expect(element.offsetHeight).toBeNear(
  739. component.getContentHeight() +
  740. horizontalScrollbarHeight +
  741. 2 * editorPadding
  742. );
  743. expect(element.offsetHeight).toBeGreaterThan(initialHeight);
  744. });
  745. it('does not render the line number gutter at all if the isLineNumberGutterVisible parameter is false', () => {
  746. const { element } = buildComponent({
  747. lineNumberGutterVisible: false
  748. });
  749. expect(element.querySelector('.line-number')).toBe(null);
  750. });
  751. it('does not render the line numbers but still renders the line number gutter if showLineNumbers is false', async () => {
  752. function checkScrollContainerLeft(component) {
  753. const { scrollContainer, gutterContainer } = component.refs;
  754. expect(scrollContainer.getBoundingClientRect().left).toBeNear(
  755. Math.round(gutterContainer.element.getBoundingClientRect().right)
  756. );
  757. }
  758. const { component, element, editor } = buildComponent({
  759. showLineNumbers: false
  760. });
  761. expect(
  762. Array.from(element.querySelectorAll('.line-number')).every(
  763. e => e.textContent === ''
  764. )
  765. ).toBe(true);
  766. checkScrollContainerLeft(component);
  767. await editor.update({ showLineNumbers: true });
  768. expect(
  769. Array.from(element.querySelectorAll('.line-number')).map(
  770. e => e.textContent
  771. )
  772. ).toEqual([
  773. '00',
  774. '1',
  775. '2',
  776. '3',
  777. '4',
  778. '5',
  779. '6',
  780. '7',
  781. '8',
  782. '9',
  783. '10',
  784. '11',
  785. '12',
  786. '13'
  787. ]);
  788. checkScrollContainerLeft(component);
  789. await editor.update({ showLineNumbers: false });
  790. expect(
  791. Array.from(element.querySelectorAll('.line-number')).every(
  792. e => e.textContent === ''
  793. )
  794. ).toBe(true);
  795. checkScrollContainerLeft(component);
  796. });
  797. it('supports the placeholderText parameter', () => {
  798. const placeholderText = 'Placeholder Test';
  799. const { element } = buildComponent({ placeholderText, text: '' });
  800. expect(element.textContent).toContain(placeholderText);
  801. });
  802. it('adds the data-grammar attribute and updates it when the grammar changes', async () => {
  803. await atom.packages.activatePackage('language-javascript');
  804. const { editor, element, component } = buildComponent();
  805. expect(element.dataset.grammar).toBe('text plain null-grammar');
  806. atom.grammars.assignLanguageMode(editor.getBuffer(), 'source.js');
  807. await component.getNextUpdatePromise();
  808. expect(element.dataset.grammar).toBe('source js');
  809. });
  810. it('adds the data-encoding attribute and updates it when the encoding changes', async () => {
  811. const { editor, element, component } = buildComponent();
  812. expect(element.dataset.encoding).toBe('utf8');
  813. editor.setEncoding('ascii');
  814. await component.getNextUpdatePromise();
  815. expect(element.dataset.encoding).toBe('ascii');
  816. });
  817. it('adds the has-selection class when the editor has a non-empty selection', async () => {
  818. const { editor, element, component } = buildComponent();
  819. expect(element.classList.contains('has-selection')).toBe(false);
  820. editor.setSelectedBufferRanges([[[0, 0], [0, 0]], [[1, 0], [1, 10]]]);
  821. await component.getNextUpdatePromise();
  822. expect(element.classList.contains('has-selection')).toBe(true);
  823. editor.setSelectedBufferRanges([[[0, 0], [0, 0]], [[1, 0], [1, 0]]]);
  824. await component.getNextUpdatePromise();
  825. expect(element.classList.contains('has-selection')).toBe(false);
  826. });
  827. it('assigns buffer-row and screen-row to each line number as data fields', async () => {
  828. const { editor, element, component } = buildComponent();
  829. editor.setSoftWrapped(true);
  830. await component.getNextUpdatePromise();
  831. await setEditorWidthInCharacters(component, 40);
  832. {
  833. const bufferRows = queryOnScreenLineNumberElements(element).map(
  834. e => e.dataset.bufferRow
  835. );
  836. const screenRows = queryOnScreenLineNumberElements(element).map(
  837. e => e.dataset.screenRow
  838. );
  839. expect(bufferRows).toEqual([
  840. '0',
  841. '1',
  842. '2',
  843. '2',
  844. '3',
  845. '3',
  846. '4',
  847. '5',
  848. '6',
  849. '6',
  850. '6',
  851. '7',
  852. '8',
  853. '8',
  854. '8',
  855. '9',
  856. '10',
  857. '11',
  858. '11',
  859. '12'
  860. ]);
  861. expect(screenRows).toEqual([
  862. '0',
  863. '1',
  864. '2',
  865. '3',
  866. '4',
  867. '5',
  868. '6',
  869. '7',
  870. '8',
  871. '9',
  872. '10',
  873. '11',
  874. '12',
  875. '13',
  876. '14',
  877. '15',
  878. '16',
  879. '17',
  880. '18',
  881. '19'
  882. ]);
  883. }
  884. editor.getBuffer().insert([2, 0], '\n');
  885. await component.getNextUpdatePromise();
  886. {
  887. const bufferRows = queryOnScreenLineNumberElements(element).map(
  888. e => e.dataset.bufferRow
  889. );
  890. const screenRows = queryOnScreenLineNumberElements(element).map(
  891. e => e.dataset.screenRow
  892. );
  893. expect(bufferRows).toEqual([
  894. '0',
  895. '1',
  896. '2',
  897. '3',
  898. '3',
  899. '4',
  900. '4',
  901. '5',
  902. '6',
  903. '7',
  904. '7',
  905. '7',
  906. '8',
  907. '9',
  908. '9',
  909. '9',
  910. '10',
  911. '11',
  912. '12',
  913. '12',
  914. '13'
  915. ]);
  916. expect(screenRows).toEqual([
  917. '0',
  918. '1',
  919. '2',
  920. '3',
  921. '4',
  922. '5',
  923. '6',
  924. '7',
  925. '8',
  926. '9',
  927. '10',
  928. '11',
  929. '12',
  930. '13',
  931. '14',
  932. '15',
  933. '16',
  934. '17',
  935. '18',
  936. '19',
  937. '20'
  938. ]);
  939. }
  940. });
  941. it('does not blow away class names added to the element by packages when changing the class name', async () => {
  942. assertDocumentFocused();
  943. const { component, element } = buildComponent();
  944. element.classList.add('a', 'b');
  945. expect(element.className).toBe('editor a b');
  946. element.focus();
  947. await component.getNextUpdatePromise();
  948. expect(element.className).toBe('editor a b is-focused');
  949. document.body.focus();
  950. await component.getNextUpdatePromise();
  951. expect(element.className).toBe('editor a b');
  952. });
  953. it('does not blow away class names managed by the component when packages change the element class name', async () => {
  954. assertDocumentFocused();
  955. const { component, element } = buildComponent({ mini: true });
  956. element.classList.add('a', 'b');
  957. element.focus();
  958. await component.getNextUpdatePromise();
  959. expect(element.className).toBe('editor mini a b is-focused');
  960. element.className = 'a c d';
  961. await component.getNextUpdatePromise();
  962. expect(element.className).toBe('a c d editor is-focused mini');
  963. });
  964. it('ignores resize events when the editor is hidden', async () => {
  965. const { component, element } = buildComponent({
  966. autoHeight: false
  967. });
  968. element.style.height = 5 * component.getLineHeight() + 'px';
  969. await component.getNextUpdatePromise();
  970. const originalClientContainerHeight = component.getClientContainerHeight();
  971. const originalGutterContainerWidth = component.getGutterContainerWidth();
  972. const originalLineNumberGutterWidth = component.getLineNumberGutterWidth();
  973. expect(originalClientContainerHeight).toBeGreaterThan(0);
  974. expect(originalGutterContainerWidth).toBeGreaterThan(0);
  975. expect(originalLineNumberGutterWidth).toBeGreaterThan(0);
  976. element.style.display = 'none';
  977. // In production, resize events are triggered before the intersection
  978. // observer detects the editor's visibility has changed. In tests, we are
  979. // unable to reproduce this scenario and so we simulate them.
  980. expect(component.visible).toBe(true);
  981. component.didResize();
  982. component.didResizeGutterContainer();
  983. expect(component.getClientContainerHeight()).toBe(
  984. originalClientContainerHeight
  985. );
  986. expect(component.getGutterContainerWidth()).toBe(
  987. originalGutterContainerWidth
  988. );
  989. expect(component.getLineNumberGutterWidth()).toBe(
  990. originalLineNumberGutterWidth
  991. );
  992. // Ensure measurements stay the same after receiving the intersection
  993. // observer events.
  994. await conditionPromise(() => !component.visible);
  995. expect(component.getClientContainerHeight()).toBe(
  996. originalClientContainerHeight
  997. );
  998. expect(component.getGutterContainerWidth()).toBe(
  999. originalGutterContainerWidth
  1000. );
  1001. expect(component.getLineNumberGutterWidth()).toBe(
  1002. originalLineNumberGutterWidth
  1003. );
  1004. });
  1005. describe('randomized tests', () => {
  1006. let originalTimeout;
  1007. beforeEach(() => {
  1008. originalTimeout = jasmine.getEnv().defaultTimeoutInterval;
  1009. jasmine.getEnv().defaultTimeoutInterval = 60 * 1000;
  1010. });
  1011. afterEach(() => {
  1012. jasmine.getEnv().defaultTimeoutInterval = originalTimeout;
  1013. });
  1014. it('renders the visible rows correctly after randomly mutating the editor', async () => {
  1015. const initialSeed = Date.now();
  1016. for (var i = 0; i < 20; i++) {
  1017. let seed = initialSeed + i;
  1018. // seed = 1520247533732
  1019. const failureMessage = 'Randomized test failed with seed: ' + seed;
  1020. const random = Random(seed);
  1021. const rowsPerTile = random.intBetween(1, 6);
  1022. const { component, element, editor } = buildComponent({
  1023. rowsPerTile,
  1024. autoHeight: false
  1025. });
  1026. editor.setSoftWrapped(Boolean(random(2)));
  1027. await setEditorWidthInCharacters(component, random(20));
  1028. await setEditorHeightInLines(component, random(10));
  1029. element.style.fontSize = random(20) + 'px';
  1030. element.style.lineHeight = random.floatBetween(0.1, 2.0);
  1031. TextEditor.didUpdateStyles();
  1032. await component.getNextUpdatePromise();
  1033. element.focus();
  1034. for (var j = 0; j < 5; j++) {
  1035. const k = random(100);
  1036. const range = getRandomBufferRange(random, editor.buffer);
  1037. if (k < 10) {
  1038. editor.setSoftWrapped(!editor.isSoftWrapped());
  1039. } else if (k < 15) {
  1040. if (random(2)) setEditorWidthInCharacters(component, random(20));
  1041. if (random(2)) setEditorHeightInLines(component, random(10));
  1042. } else if (k < 40) {
  1043. editor.setSelectedBufferRange(range);
  1044. editor.backspace();
  1045. } else if (k < 80) {
  1046. const linesToInsert = buildRandomLines(random, 5);
  1047. editor.setCursorBufferPosition(range.start);
  1048. editor.insertText(linesToInsert);
  1049. } else if (k < 90) {
  1050. if (random(2)) {
  1051. editor.foldBufferRange(range);
  1052. } else {
  1053. editor.destroyFoldsIntersectingBufferRange(range);
  1054. }
  1055. } else if (k < 95) {
  1056. editor.setSelectedBufferRange(range);
  1057. } else {
  1058. if (random(2)) {
  1059. component.setScrollTop(random(component.getScrollHeight()));
  1060. }
  1061. if (random(2)) {
  1062. component.setScrollLeft(random(component.getScrollWidth()));
  1063. }
  1064. }
  1065. component.scheduleUpdate();
  1066. await component.getNextUpdatePromise();
  1067. const renderedLines = queryOnScreenLineElements(element).sort(
  1068. (a, b) => a.dataset.screenRow - b.dataset.screenRow
  1069. );
  1070. const renderedLineNumbers = queryOnScreenLineNumberElements(
  1071. element
  1072. ).sort((a, b) => a.dataset.screenRow - b.dataset.screenRow);
  1073. const renderedStartRow = component.getRenderedStartRow();
  1074. const expectedLines = editor.displayLayer.getScreenLines(
  1075. renderedStartRow,
  1076. component.getRenderedEndRow()
  1077. );
  1078. expect(renderedLines.length).toBe(
  1079. expectedLines.length,
  1080. failureMessage
  1081. );
  1082. expect(renderedLineNumbers.length).toBe(
  1083. expectedLines.length,
  1084. failureMessage
  1085. );
  1086. for (let k = 0; k < renderedLines.length; k++) {
  1087. const expectedLine = expectedLines[k];
  1088. const expectedText = expectedLine.lineText || ' ';
  1089. const renderedLine = renderedLines[k];
  1090. const renderedLineNumber = renderedLineNumbers[k];
  1091. let renderedText = renderedLine.textContent;
  1092. // We append zero width NBSPs after folds at the end of the
  1093. // line in order to support measurement.
  1094. if (expectedText.endsWith(editor.displayLayer.foldCharacter)) {
  1095. renderedText = renderedText.substring(
  1096. 0,
  1097. renderedText.length - 1
  1098. );
  1099. }
  1100. expect(renderedText).toBe(expectedText, failureMessage);
  1101. expect(parseInt(renderedLine.dataset.screenRow)).toBe(
  1102. renderedStartRow + k,
  1103. failureMessage
  1104. );
  1105. expect(parseInt(renderedLineNumber.dataset.screenRow)).toBe(
  1106. renderedStartRow + k,
  1107. failureMessage
  1108. );
  1109. }
  1110. }
  1111. element.remove();
  1112. editor.destroy();
  1113. }
  1114. });
  1115. });
  1116. });
  1117. describe('mini editors', () => {
  1118. it('adds the mini attribute and class even when the element is not attached', () => {
  1119. {
  1120. const { element } = buildComponent({ mini: true });
  1121. expect(element.hasAttribute('mini')).toBe(true);
  1122. expect(element.classList.contains('mini')).toBe(true);
  1123. }
  1124. {
  1125. const { element } = buildComponent({
  1126. mini: true,
  1127. attach: false
  1128. });
  1129. expect(element.hasAttribute('mini')).toBe(true);
  1130. expect(element.classList.contains('mini')).toBe(true);
  1131. }
  1132. });
  1133. it('does not render the gutter container', () => {
  1134. const { component, element } = buildComponent({ mini: true });
  1135. expect(component.refs.gutterContainer).toBeUndefined();
  1136. expect(element.querySelector('gutter-container')).toBeNull();
  1137. });
  1138. it('does not render line decorations for the cursor line', async () => {
  1139. const { component, element, editor } = buildComponent({ mini: true });
  1140. expect(
  1141. element.querySelector('.line').classList.contains('cursor-line')
  1142. ).toBe(false);
  1143. editor.update({ mini: false });
  1144. await component.getNextUpdatePromise();
  1145. expect(
  1146. element.querySelector('.line').classList.contains('cursor-line')
  1147. ).toBe(true);
  1148. editor.update({ mini: true });
  1149. await component.getNextUpdatePromise();
  1150. expect(
  1151. element.querySelector('.line').classList.contains('cursor-line')
  1152. ).toBe(false);
  1153. });
  1154. it('does not render scrollbars', async () => {
  1155. const { component, editor } = buildComponent({
  1156. mini: true,
  1157. autoHeight: false
  1158. });
  1159. await setEditorWidthInCharacters(component, 10);
  1160. editor.setText('x'.repeat(20) + 'y'.repeat(20));
  1161. await component.getNextUpdatePromise();
  1162. expect(component.canScrollVertically()).toBe(false);
  1163. expect(component.canScrollHorizontally()).toBe(false);
  1164. expect(component.refs.horizontalScrollbar).toBeUndefined();
  1165. expect(component.refs.verticalScrollbar).toBeUndefined();
  1166. });
  1167. });
  1168. describe('focus', () => {
  1169. beforeEach(() => {
  1170. assertDocumentFocused();
  1171. });
  1172. it('focuses the hidden input element and adds the is-focused class when focused', async () => {
  1173. const { component, element } = buildComponent();
  1174. const { hiddenInput } = component.refs.cursorsAndInput.refs;
  1175. expect(document.activeElement).not.toBe(hiddenInput);
  1176. element.focus();
  1177. expect(document.activeElement).toBe(hiddenInput);
  1178. await component.getNextUpdatePromise();
  1179. expect(element.classList.contains('is-focused')).toBe(true);
  1180. element.focus(); // focusing back to the element does not blur
  1181. expect(document.activeElement).toBe(hiddenInput);
  1182. expect(element.classList.contains('is-focused')).toBe(true);
  1183. document.body.focus();
  1184. expect(document.activeElement).not.toBe(hiddenInput);
  1185. await component.getNextUpdatePromise();
  1186. expect(element.classList.contains('is-focused')).toBe(false);
  1187. });
  1188. it('updates the component when the hidden input is focused directly', async () => {
  1189. const { component, element } = buildComponent();
  1190. const { hiddenInput } = component.refs.cursorsAndInput.refs;
  1191. expect(element.classList.contains('is-focused')).toBe(false);
  1192. expect(document.activeElement).not.toBe(hiddenInput);
  1193. hiddenInput.focus();
  1194. await component.getNextUpdatePromise();
  1195. expect(element.classList.contains('is-focused')).toBe(true);
  1196. });
  1197. it('gracefully handles a focus event that occurs prior to the attachedCallback of the element', () => {
  1198. const { component, element } = buildComponent({ attach: false });
  1199. const parent = document.createElement(
  1200. 'text-editor-component-test-element'
  1201. );
  1202. parent.appendChild(element);
  1203. parent.didAttach = () => element.focus();
  1204. jasmine.attachToDOM(parent);
  1205. expect(document.activeElement).toBe(
  1206. component.refs.cursorsAndInput.refs.hiddenInput
  1207. );
  1208. });
  1209. it('gracefully handles a focus event that occurs prior to detecting the element has become visible', async () => {
  1210. const { component, element } = buildComponent({ attach: false });
  1211. element.style.display = 'none';
  1212. jasmine.attachToDOM(element);
  1213. element.style.display = 'block';
  1214. element.focus();
  1215. await component.getNextUpdatePromise();
  1216. expect(document.activeElement).toBe(
  1217. component.refs.cursorsAndInput.refs.hiddenInput
  1218. );
  1219. });
  1220. it('emits blur events only when focus shifts to something other than the editor itself or its hidden input', () => {
  1221. const { element } = buildComponent();
  1222. let blurEventCount = 0;
  1223. element.addEventListener('blur', () => blurEventCount++);
  1224. element.focus();
  1225. expect(blurEventCount).toBe(0);
  1226. element.focus();
  1227. expect(blurEventCount).toBe(0);
  1228. document.body.focus();
  1229. expect(blurEventCount).toBe(1);
  1230. });
  1231. });
  1232. describe('autoscroll', () => {
  1233. it('automatically scrolls vertically when the requested range is within the vertical scroll margin of the top or bottom', async () => {
  1234. const { component, editor } = buildComponent({
  1235. height: 120 + horizontalScrollbarHeight
  1236. });
  1237. expect(component.getLastVisibleRow()).toBe(7);
  1238. editor.scrollToScreenRange([[4, 0], [6, 0]]);
  1239. await component.getNextUpdatePromise();
  1240. expect(component.getScrollBottom()).toBeNear(
  1241. (6 + 1 + editor.verticalScrollMargin) * component.getLineHeight()
  1242. );
  1243. editor.scrollToScreenPosition([8, 0]);
  1244. await component.getNextUpdatePromise();
  1245. expect(component.getScrollBottom()).toBeNear(
  1246. (8 + 1 + editor.verticalScrollMargin) *
  1247. component.measurements.lineHeight
  1248. );
  1249. editor.scrollToScreenPosition([3, 0]);
  1250. await component.getNextUpdatePromise();
  1251. expect(component.getScrollTop()).toBeNear(
  1252. (3 - editor.verticalScrollMargin) * component.measurements.lineHeight
  1253. );
  1254. editor.scrollToScreenPosition([2, 0]);
  1255. await component.getNextUpdatePromise();
  1256. expect(component.getScrollTop()).toBe(0);
  1257. });
  1258. it('does not vertically autoscroll by more than half of the visible lines if the editor is shorter than twice the scroll margin', async () => {
  1259. const { component, element, editor } = buildComponent({
  1260. autoHeight: false
  1261. });
  1262. element.style.height =
  1263. 5.5 * component.measurements.lineHeight +
  1264. horizontalScrollbarHeight +
  1265. 'px';
  1266. await component.getNextUpdatePromise();
  1267. expect(component.getLastVisibleRow()).toBe(5);
  1268. const scrollMarginInLines = 2;
  1269. editor.scrollToScreenPosition([6, 0]);
  1270. await component.getNextUpdatePromise();
  1271. expect(component.getScrollBottom()).toBeNear(
  1272. (6 + 1 + scrollMarginInLines) * component.measurements.lineHeight
  1273. );
  1274. editor.scrollToScreenPosition([6, 4]);
  1275. await component.getNextUpdatePromise();
  1276. expect(component.getScrollBottom()).toBeNear(
  1277. (6 + 1 + scrollMarginInLines) * component.measurements.lineHeight
  1278. );
  1279. editor.scrollToScreenRange([[4, 4], [6, 4]]);
  1280. await component.getNextUpdatePromise();
  1281. expect(component.getScrollTop()).toBeNear(
  1282. (4 - scrollMarginInLines) * component.measurements.lineHeight
  1283. );
  1284. editor.scrollToScreenRange([[4, 4], [6, 4]], { reversed: false });
  1285. await component.getNextUpdatePromise();
  1286. expect(component.getScrollBottom()).toBeNear(
  1287. (6 + 1 + scrollMarginInLines) * component.measurements.lineHeight
  1288. );
  1289. });
  1290. it('autoscrolls the given range to the center of the screen if the `center` option is true', async () => {
  1291. const { component, editor } = buildComponent({ height: 50 });
  1292. expect(component.getLastVisibleRow()).toBe(2);
  1293. editor.scrollToScreenRange([[4, 0], [6, 0]], { center: true });
  1294. await component.getNextUpdatePromise();
  1295. const actualScrollCenter =
  1296. (component.getScrollTop() + component.getScrollBottom()) / 2;
  1297. const expectedScrollCenter = ((4 + 7) / 2) * component.getLineHeight();
  1298. expect(actualScrollCenter).toBeCloseTo(expectedScrollCenter, 0);
  1299. });
  1300. it('automatically scrolls horizontally when the requested range is within the horizontal scroll margin of the right edge of the gutter or right edge of the scroll container', async () => {
  1301. const { component, element, editor } = buildComponent();
  1302. element.style.width =
  1303. component.getGutterContainerWidth() +
  1304. 3 *
  1305. editor.horizontalScrollMargin *
  1306. component.measurements.baseCharacterWidth +
  1307. 'px';
  1308. await component.getNextUpdatePromise();
  1309. editor.scrollToScreenRange([[1, 12], [2, 28]]);
  1310. await component.getNextUpdatePromise();
  1311. let expectedScrollLeft =
  1312. clientLeftForCharacter(component, 1, 12) -
  1313. lineNodeForScreenRow(component, 1).getBoundingClientRect().left -
  1314. editor.horizontalScrollMargin *
  1315. component.measurements.baseCharacterWidth;
  1316. expect(component.getScrollLeft()).toBeNear(expectedScrollLeft);
  1317. editor.scrollToScreenRange([[1, 12], [2, 28]], { reversed: false });
  1318. await component.getNextUpdatePromise();
  1319. expectedScrollLeft =
  1320. component.getGutterContainerWidth() +
  1321. clientLeftForCharacter(component, 2, 28) -
  1322. lineNodeForScreenRow(component, 2).getBoundingClientRect().left +
  1323. editor.horizontalScrollMargin *
  1324. component.measurements.baseCharacterWidth -
  1325. component.getScrollContainerClientWidth();
  1326. expect(component.getScrollLeft()).toBeNear(expectedScrollLeft);
  1327. });
  1328. it('does not horizontally autoscroll by more than half of the visible "base-width" characters if the editor is narrower than twice the scroll margin', async () => {
  1329. const { component, editor } = buildComponent({ autoHeight: false });
  1330. await setEditorWidthInCharacters(
  1331. component,
  1332. 1.5 * editor.horizontalScrollMargin
  1333. );
  1334. const editorWidthInChars =
  1335. component.getScrollContainerClientWidth() /
  1336. component.getBaseCharacterWidth();
  1337. expect(Math.round(editorWidthInChars)).toBe(9);
  1338. editor.scrollToScreenRange([[6, 10], [6, 15]]);
  1339. await component.getNextUpdatePromise();
  1340. let expectedScrollLeft = Math.floor(
  1341. clientLeftForCharacter(component, 6, 10) -
  1342. lineNodeForScreenRow(component, 1).getBoundingClientRect().left -
  1343. Math.floor((editorWidthInChars - 1) / 2) *
  1344. component.getBaseCharacterWidth()
  1345. );
  1346. expect(component.getScrollLeft()).toBeNear(expectedScrollLeft);
  1347. });
  1348. it('correctly autoscrolls after inserting a line that exceeds the current content width', async () => {
  1349. const { component, element, editor } = buildComponent();
  1350. element.style.width =
  1351. component.getGutterContainerWidth() +
  1352. component.getContentWidth() +
  1353. 'px';
  1354. await component.getNextUpdatePromise();
  1355. editor.setCursorScreenPosition([0, Infinity]);
  1356. editor.insertText('x'.repeat(100));
  1357. await component.getNextUpdatePromise();
  1358. expect(component.getScrollLeft()).toBeNear(
  1359. component.getScrollWidth() - component.getScrollContainerClientWidth()
  1360. );
  1361. });
  1362. it('does not try to measure lines that do not exist when the animation frame is delivered', async () => {
  1363. const { component, editor } = buildComponent({
  1364. autoHeight: false,
  1365. height: 30,
  1366. rowsPerTile: 2
  1367. });
  1368. editor.scrollToBufferPosition([11, 5]);
  1369. editor.getBuffer().deleteRows(11, 12);
  1370. await component.getNextUpdatePromise();
  1371. expect(component.getScrollBottom()).toBeNear(
  1372. (10 + 1) * component.measurements.lineHeight
  1373. );
  1374. });
  1375. it('accounts for the presence of horizontal scrollbars that appear during the same frame as the autoscroll', async () => {
  1376. const { component, element, editor } = buildComponent({
  1377. autoHeight: false
  1378. });
  1379. element.style.height = component.getContentHeight() / 2 + 'px';
  1380. element.style.width = component.getScrollWidth() + 'px';
  1381. await component.getNextUpdatePromise();
  1382. editor.setCursorScreenPosition([10, Infinity]);
  1383. editor.insertText('\n\n' + 'x'.repeat(100));
  1384. await component.getNextUpdatePromise();
  1385. expect(component.getScrollTop()).toBeNear(
  1386. component.getScrollHeight() - component.getScrollContainerClientHeight()
  1387. );
  1388. expect(component.getScrollLeft()).toBeNear(
  1389. component.getScrollWidth() - component.getScrollContainerClientWidth()
  1390. );
  1391. // Scrolling to the top should not throw an error. This failed
  1392. // previously due to horizontalPositionsToMeasure not being empty after
  1393. // autoscrolling vertically to account for the horizontal scrollbar.
  1394. spyOn(window, 'onerror');
  1395. await setScrollTop(component, 0);
  1396. expect(window.onerror).not.toHaveBeenCalled();
  1397. });
  1398. });
  1399. describe('logical scroll positions', () => {
  1400. it('allows the scrollTop to be changed and queried in terms of rows via setScrollTopRow and getScrollTopRow', () => {
  1401. const { component, element } = buildComponent({
  1402. attach: false,
  1403. height: 80
  1404. });
  1405. // Caches the scrollTopRow if we don't have measurements
  1406. component.setScrollTopRow(6);
  1407. expect(component.getScrollTopRow()).toBe(6);
  1408. // Assigns the scrollTop based on the logical position when attached
  1409. jasmine.attachToDOM(element);
  1410. const expectedScrollTop = Math.round(6 * component.getLineHeight());
  1411. expect(component.getScrollTopRow()).toBeNear(6);
  1412. expect(component.getScrollTop()).toBeNear(expectedScrollTop);
  1413. expect(component.refs.content.style.transform).toBe(
  1414. `translate(0px, -${expectedScrollTop}px)`
  1415. );
  1416. // Allows the scrollTopRow to be updated while attached
  1417. component.setScrollTopRow(4);
  1418. expect(component.getScrollTopRow()).toBeNear(4);
  1419. expect(component.getScrollTop()).toBeNear(
  1420. Math.round(4 * component.getLineHeight())
  1421. );
  1422. // Preserves the scrollTopRow when detached
  1423. element.remove();
  1424. expect(component.getScrollTopRow()).toBeNear(4);
  1425. expect(component.getScrollTop()).toBeNear(
  1426. Math.round(4 * component.getLineHeight())
  1427. );
  1428. component.setScrollTopRow(6);
  1429. expect(component.getScrollTopRow()).toBeNear(6);
  1430. expect(component.getScrollTop()).toBeNear(
  1431. Math.round(6 * component.getLineHeight())
  1432. );
  1433. jasmine.attachToDOM(element);
  1434. element.style.height = '60px';
  1435. expect(component.getScrollTopRow()).toBeNear(6);
  1436. expect(component.getScrollTop()).toBeNear(
  1437. Math.round(6 * component.getLineHeight())
  1438. );
  1439. });
  1440. it('allows the scrollLeft to be changed and queried in terms of base character columns via setScrollLeftColumn and getScrollLeftColumn', () => {
  1441. const { component, element } = buildComponent({
  1442. attach: false,
  1443. width: 80
  1444. });
  1445. // Caches the scrollTopRow if we don't have measurements
  1446. component.setScrollLeftColumn(2);
  1447. expect(component.getScrollLeftColumn()).toBe(2);
  1448. // Assigns the scrollTop based on the logical position when attached
  1449. jasmine.attachToDOM(element);
  1450. expect(component.getScrollLeft()).toBeCloseTo(
  1451. 2 * component.getBaseCharacterWidth(),
  1452. 0
  1453. );
  1454. // Allows the scrollTopRow to be updated while attached
  1455. component.setScrollLeftColumn(4);
  1456. expect(component.getScrollLeft()).toBeCloseTo(
  1457. 4 * component.getBaseCharacterWidth(),
  1458. 0
  1459. );
  1460. // Preserves the scrollTopRow when detached
  1461. element.remove();
  1462. expect(component.getScrollLeft()).toBeCloseTo(
  1463. 4 * component.getBaseCharacterWidth(),
  1464. 0
  1465. );
  1466. component.setScrollLeftColumn(6);
  1467. expect(component.getScrollLeft()).toBeCloseTo(
  1468. 6 * component.getBaseCharacterWidth(),
  1469. 0
  1470. );
  1471. jasmine.attachToDOM(element);
  1472. element.style.width = '60px';
  1473. expect(component.getScrollLeft()).toBeCloseTo(
  1474. 6 * component.getBaseCharacterWidth(),
  1475. 0
  1476. );
  1477. });
  1478. });
  1479. describe('scrolling via the mouse wheel', () => {
  1480. it('scrolls vertically or horizontally depending on whether deltaX or deltaY is larger', () => {
  1481. const scrollSensitivity = 30;
  1482. const { component } = buildComponent({
  1483. height: 50,
  1484. width: 50,
  1485. scrollSensitivity
  1486. });
  1487. // stub in place for Event.preventDefault()
  1488. const eventPreventDefaultStub = function() {};
  1489. {
  1490. const expectedScrollTop = 20 * (scrollSensitivity / 100);
  1491. const expectedScrollLeft = component.getScrollLeft();
  1492. component.didMouseWheel({
  1493. wheelDeltaX: -5,
  1494. wheelDeltaY: -20,
  1495. preventDefault: eventPreventDefaultStub
  1496. });
  1497. expect(component.getScrollTop()).toBeNear(expectedScrollTop);
  1498. expect(component.getScrollLeft()).toBeNear(expectedScrollLeft);
  1499. expect(component.refs.content.style.transform).toBe(
  1500. `translate(${-expectedScrollLeft}px, ${-expectedScrollTop}px)`
  1501. );
  1502. }
  1503. {
  1504. const expectedScrollTop =
  1505. component.getScrollTop() - 10 * (scrollSensitivity / 100);
  1506. const expectedScrollLeft = component.getScrollLeft();
  1507. component.didMouseWheel({
  1508. wheelDeltaX: -5,
  1509. wheelDeltaY: 10,
  1510. preventDefault: eventPreventDefaultStub
  1511. });
  1512. expect(component.getScrollTop()).toBeNear(expectedScrollTop);
  1513. expect(component.getScrollLeft()).toBeNear(expectedScrollLeft);
  1514. expect(component.refs.content.style.transform).toBe(
  1515. `translate(${-expectedScrollLeft}px, ${-expectedScrollTop}px)`
  1516. );
  1517. }
  1518. {
  1519. const expectedScrollTop = component.getScrollTop();
  1520. const expectedScrollLeft = 20 * (scrollSensitivity / 100);
  1521. component.didMouseWheel({
  1522. wheelDeltaX: -20,
  1523. wheelDeltaY: 10,
  1524. preventDefault: eventPreventDefaultStub
  1525. });
  1526. expect(component.getScrollTop()).toBeNear(expectedScrollTop);
  1527. expect(component.getScrollLeft()).toBeNear(expectedScrollLeft);
  1528. expect(component.refs.content.style.transform).toBe(
  1529. `translate(${-expectedScrollLeft}px, ${-expectedScrollTop}px)`
  1530. );
  1531. }
  1532. {
  1533. const expectedScrollTop = component.getScrollTop();
  1534. const expectedScrollLeft =
  1535. component.getScrollLeft() - 10 * (scrollSensitivity / 100);
  1536. component.didMouseWheel({
  1537. wheelDeltaX: 10,
  1538. wheelDeltaY: -8,
  1539. preventDefault: eventPreventDefaultStub
  1540. });
  1541. expect(component.getScrollTop()).toBeNear(expectedScrollTop);
  1542. expect(component.getScrollLeft()).toBeNear(expectedScrollLeft);
  1543. expect(component.refs.content.style.transform).toBe(
  1544. `translate(${-expectedScrollLeft}px, ${-expectedScrollTop}px)`
  1545. );
  1546. }
  1547. });
  1548. it('inverts deltaX and deltaY when holding shift on Windows and Linux', async () => {
  1549. const scrollSensitivity = 50;
  1550. const { component } = buildComponent({
  1551. height: 50,
  1552. width: 50,
  1553. scrollSensitivity
  1554. });
  1555. // stub in place for Event.preventDefault()
  1556. const eventPreventDefaultStub = function() {};
  1557. component.props.platform = 'linux';
  1558. {
  1559. const expectedScrollTop = 20 * (scrollSensitivity / 100);
  1560. component.didMouseWheel({
  1561. wheelDeltaX: 0,
  1562. wheelDeltaY: -20,
  1563. preventDefault: eventPreventDefaultStub
  1564. });
  1565. expect(component.getScrollTop()).toBeNear(expectedScrollTop);
  1566. expect(component.refs.content.style.transform).toBe(
  1567. `translate(0px, -${expectedScrollTop}px)`
  1568. );
  1569. await setScrollTop(component, 0);
  1570. }
  1571. {
  1572. const expectedScrollLeft = 20 * (scrollSensitivity / 100);
  1573. component.didMouseWheel({
  1574. wheelDeltaX: 0,
  1575. wheelDeltaY: -20,
  1576. shiftKey: true,
  1577. preventDefault: eventPreventDefaultStub
  1578. });
  1579. expect(component.getScrollLeft()).toBeNear(expectedScrollLeft);
  1580. expect(component.refs.content.style.transform).toBe(
  1581. `translate(-${expectedScrollLeft}px, 0px)`
  1582. );
  1583. await setScrollLeft(component, 0);
  1584. }
  1585. {
  1586. const expectedScrollTop = 20 * (scrollSensitivity / 100);
  1587. component.didMouseWheel({
  1588. wheelDeltaX: -20,
  1589. wheelDeltaY: 0,
  1590. shiftKey: true,
  1591. preventDefault: eventPreventDefaultStub
  1592. });
  1593. expect(component.getScrollTop()).toBe(expectedScrollTop);
  1594. expect(component.refs.content.style.transform).toBe(
  1595. `translate(0px, -${expectedScrollTop}px)`
  1596. );
  1597. await setScrollTop(component, 0);
  1598. }
  1599. component.props.platform = 'win32';
  1600. {
  1601. const expectedScrollTop = 20 * (scrollSensitivity / 100);
  1602. component.didMouseWheel({
  1603. wheelDeltaX: 0,
  1604. wheelDeltaY: -20,
  1605. preventDefault: eventPreventDefaultStub
  1606. });
  1607. expect(component.getScrollTop()).toBe(expectedScrollTop);
  1608. expect(component.refs.content.style.transform).toBe(
  1609. `translate(0px, -${expectedScrollTop}px)`
  1610. );
  1611. await setScrollTop(component, 0);
  1612. }
  1613. {
  1614. const expectedScrollLeft = 20 * (scrollSensitivity / 100);
  1615. component.didMouseWheel({
  1616. wheelDeltaX: 0,
  1617. wheelDeltaY: -20,
  1618. shiftKey: true,
  1619. preventDefault: eventPreventDefaultStub
  1620. });
  1621. expect(component.getScrollLeft()).toBe(expectedScrollLeft);
  1622. expect(component.refs.content.style.transform).toBe(
  1623. `translate(-${expectedScrollLeft}px, 0px)`
  1624. );
  1625. await setScrollLeft(component, 0);
  1626. }
  1627. {
  1628. const expectedScrollTop = 20 * (scrollSensitivity / 100);
  1629. component.didMouseWheel({
  1630. wheelDeltaX: -20,
  1631. wheelDeltaY: 0,
  1632. shiftKey: true,
  1633. preventDefault: eventPreventDefaultStub
  1634. });
  1635. expect(component.getScrollTop()).toBe(expectedScrollTop);
  1636. expect(component.refs.content.style.transform).toBe(
  1637. `translate(0px, -${expectedScrollTop}px)`
  1638. );
  1639. await setScrollTop(component, 0);
  1640. }
  1641. component.props.platform = 'darwin';
  1642. {
  1643. const expectedScrollTop = 20 * (scrollSensitivity / 100);
  1644. component.didMouseWheel({
  1645. wheelDeltaX: 0,
  1646. wheelDeltaY: -20,
  1647. preventDefault: eventPreventDefaultStub
  1648. });
  1649. expect(component.getScrollTop()).toBe(expectedScrollTop);
  1650. expect(component.refs.content.style.transform).toBe(
  1651. `translate(0px, -${expectedScrollTop}px)`
  1652. );
  1653. await setScrollTop(component, 0);
  1654. }
  1655. {
  1656. const expectedScrollTop = 20 * (scrollSensitivity / 100);
  1657. component.didMouseWheel({
  1658. wheelDeltaX: 0,
  1659. wheelDeltaY: -20,
  1660. shiftKey: true,
  1661. preventDefault: eventPreventDefaultStub
  1662. });
  1663. expect(component.getScrollTop()).toBe(expectedScrollTop);
  1664. expect(component.refs.content.style.transform).toBe(
  1665. `translate(0px, -${expectedScrollTop}px)`
  1666. );
  1667. await setScrollTop(component, 0);
  1668. }
  1669. {
  1670. const expectedScrollLeft = 20 * (scrollSensitivity / 100);
  1671. component.didMouseWheel({
  1672. wheelDeltaX: -20,
  1673. wheelDeltaY: 0,
  1674. shiftKey: true,
  1675. preventDefault: eventPreventDefaultStub
  1676. });
  1677. expect(component.getScrollLeft()).toBe(expectedScrollLeft);
  1678. expect(component.refs.content.style.transform).toBe(
  1679. `translate(-${expectedScrollLeft}px, 0px)`
  1680. );
  1681. await setScrollLeft(component, 0);
  1682. }
  1683. });
  1684. });
  1685. describe('scrolling via the API', () => {
  1686. it('ignores scroll requests to NaN, null or undefined positions', async () => {
  1687. const { component } = buildComponent({
  1688. rowsPerTile: 2,
  1689. autoHeight: false
  1690. });
  1691. await setEditorHeightInLines(component, 3);
  1692. await setEditorWidthInCharacters(component, 10);
  1693. const initialScrollTop = Math.round(2 * component.getLineHeight());
  1694. const initialScrollLeft = Math.round(
  1695. 5 * component.getBaseCharacterWidth()
  1696. );
  1697. setScrollTop(component, initialScrollTop);
  1698. setScrollLeft(component, initialScrollLeft);
  1699. await component.getNextUpdatePromise();
  1700. setScrollTop(component, NaN);
  1701. setScrollLeft(component, NaN);
  1702. await component.getNextUpdatePromise();
  1703. expect(component.getScrollTop()).toBeNear(initialScrollTop);
  1704. expect(component.getScrollLeft()).toBeNear(initialScrollLeft);
  1705. setScrollTop(component, null);
  1706. setScrollLeft(component, null);
  1707. await component.getNextUpdatePromise();
  1708. expect(component.getScrollTop()).toBeNear(initialScrollTop);
  1709. expect(component.getScrollLeft()).toBeNear(initialScrollLeft);
  1710. setScrollTop(component, undefined);
  1711. setScrollLeft(component, undefined);
  1712. await component.getNextUpdatePromise();
  1713. expect(component.getScrollTop()).toBeNear(initialScrollTop);
  1714. expect(component.getScrollLeft()).toBeNear(initialScrollLeft);
  1715. });
  1716. });
  1717. describe('line and line number decorations', () => {
  1718. it('adds decoration classes on screen lines spanned by decorated markers', async () => {
  1719. const { component, editor } = buildComponent({
  1720. softWrapped: true
  1721. });
  1722. await setEditorWidthInCharacters(component, 55);
  1723. expect(lineNodeForScreenRow(component, 3).textContent).toBe(
  1724. ' var pivot = items.shift(), current, left = [], '
  1725. );
  1726. expect(lineNodeForScreenRow(component, 4).textContent).toBe(
  1727. ' right = [];'
  1728. );
  1729. const marker1 = editor.markScreenRange([[1, 10], [3, 10]]);
  1730. const layer = editor.addMarkerLayer();
  1731. layer.markScreenPosition([5, 0]);
  1732. layer.markScreenPosition([8, 0]);
  1733. const marker4 = layer.markScreenPosition([10, 0]);
  1734. editor.decorateMarker(marker1, {
  1735. type: ['line', 'line-number'],
  1736. class: 'a'
  1737. });
  1738. const layerDecoration = editor.decorateMarkerLayer(layer, {
  1739. type: ['line', 'line-number'],
  1740. class: 'b'
  1741. });
  1742. layerDecoration.setPropertiesForMarker(marker4, {
  1743. type: 'line',
  1744. class: 'c'
  1745. });
  1746. await component.getNextUpdatePromise();
  1747. expect(lineNodeForScreenRow(component, 1).classList.contains('a')).toBe(
  1748. true
  1749. );
  1750. expect(lineNodeForScreenRow(component, 2).classList.contains('a')).toBe(
  1751. true
  1752. );
  1753. expect(lineNodeForScreenRow(component, 3).classList.contains('a')).toBe(
  1754. true
  1755. );
  1756. expect(lineNodeForScreenRow(component, 4).classList.contains('a')).toBe(
  1757. false
  1758. );
  1759. expect(lineNodeForScreenRow(component, 5).classList.contains('b')).toBe(
  1760. true
  1761. );
  1762. expect(lineNodeForScreenRow(component, 8).classList.contains('b')).toBe(
  1763. true
  1764. );
  1765. expect(lineNodeForScreenRow(component, 10).classList.contains('b')).toBe(
  1766. false
  1767. );
  1768. expect(lineNodeForScreenRow(component, 10).classList.contains('c')).toBe(
  1769. true
  1770. );
  1771. expect(
  1772. lineNumberNodeForScreenRow(component, 1).classList.contains('a')
  1773. ).toBe(true);
  1774. expect(
  1775. lineNumberNodeForScreenRow(component, 2).classList.contains('a')
  1776. ).toBe(true);
  1777. expect(
  1778. lineNumberNodeForScreenRow(component, 3).classList.contains('a')
  1779. ).toBe(true);
  1780. expect(
  1781. lineNumberNodeForScreenRow(component, 4).classList.contains('a')
  1782. ).toBe(false);
  1783. expect(
  1784. lineNumberNodeForScreenRow(component, 5).classList.contains('b')
  1785. ).toBe(true);
  1786. expect(
  1787. lineNumberNodeForScreenRow(component, 8).classList.contains('b')
  1788. ).toBe(true);
  1789. expect(
  1790. lineNumberNodeForScreenRow(component, 10).classList.contains('b')
  1791. ).toBe(false);
  1792. expect(
  1793. lineNumberNodeForScreenRow(component, 10).classList.contains('c')
  1794. ).toBe(false);
  1795. marker1.setScreenRange([[5, 0], [8, 0]]);
  1796. await component.getNextUpdatePromise();
  1797. expect(lineNodeForScreenRow(component, 1).classList.contains('a')).toBe(
  1798. false
  1799. );
  1800. expect(lineNodeForScreenRow(component, 2).classList.contains('a')).toBe(
  1801. false
  1802. );
  1803. expect(lineNodeForScreenRow(component, 3).classList.contains('a')).toBe(
  1804. false
  1805. );
  1806. expect(lineNodeForScreenRow(component, 4).classList.contains('a')).toBe(
  1807. false
  1808. );
  1809. expect(lineNodeForScreenRow(component, 5).classList.contains('a')).toBe(
  1810. true
  1811. );
  1812. expect(lineNodeForScreenRow(component, 5).classList.contains('b')).toBe(
  1813. true
  1814. );
  1815. expect(lineNodeForScreenRow(component, 6).classList.contains('a')).toBe(
  1816. true
  1817. );
  1818. expect(lineNodeForScreenRow(component, 7).classList.contains('a')).toBe(
  1819. true
  1820. );
  1821. expect(lineNodeForScreenRow(component, 8).classList.contains('a')).toBe(
  1822. true
  1823. );
  1824. expect(lineNodeForScreenRow(component, 8).classList.contains('b')).toBe(
  1825. true
  1826. );
  1827. expect(
  1828. lineNumberNodeForScreenRow(component, 1).classList.contains('a')
  1829. ).toBe(false);
  1830. expect(
  1831. lineNumberNodeForScreenRow(component, 2).classList.contains('a')
  1832. ).toBe(false);
  1833. expect(
  1834. lineNumberNodeForScreenRow(component, 3).classList.contains('a')
  1835. ).toBe(false);
  1836. expect(
  1837. lineNumberNodeForScreenRow(component, 4).classList.contains('a')
  1838. ).toBe(false);
  1839. expect(
  1840. lineNumberNodeForScreenRow(component, 5).classList.contains('a')
  1841. ).toBe(true);
  1842. expect(
  1843. lineNumberNodeForScreenRow(component, 5).classList.contains('b')
  1844. ).toBe(true);
  1845. expect(
  1846. lineNumberNodeForScreenRow(component, 6).classList.contains('a')
  1847. ).toBe(true);
  1848. expect(
  1849. lineNumberNodeForScreenRow(component, 7).classList.contains('a')
  1850. ).toBe(true);
  1851. expect(
  1852. lineNumberNodeForScreenRow(component, 8).classList.contains('a')
  1853. ).toBe(true);
  1854. expect(
  1855. lineNumberNodeForScreenRow(component, 8).classList.contains('b')
  1856. ).toBe(true);
  1857. });
  1858. it('honors the onlyEmpty and onlyNonEmpty decoration options', async () => {
  1859. const { component, editor } = buildComponent();
  1860. const marker = editor.markScreenPosition([1, 0]);
  1861. editor.decorateMarker(marker, {
  1862. type: ['line', 'line-number'],
  1863. class: 'a',
  1864. onlyEmpty: true
  1865. });
  1866. editor.decorateMarker(marker, {
  1867. type: ['line', 'line-number'],
  1868. class: 'b',
  1869. onlyNonEmpty: true
  1870. });
  1871. editor.decorateMarker(marker, {
  1872. type: ['line', 'line-number'],
  1873. class: 'c'
  1874. });
  1875. await component.getNextUpdatePromise();
  1876. expect(lineNodeForScreenRow(component, 1).classList.contains('a')).toBe(
  1877. true
  1878. );
  1879. expect(lineNodeForScreenRow(component, 1).classList.contains('b')).toBe(
  1880. false
  1881. );
  1882. expect(lineNodeForScreenRow(component, 1).classList.contains('c')).toBe(
  1883. true
  1884. );
  1885. expect(
  1886. lineNumberNodeForScreenRow(component, 1).classList.contains('a')
  1887. ).toBe(true);
  1888. expect(
  1889. lineNumberNodeForScreenRow(component, 1).classList.contains('b')
  1890. ).toBe(false);
  1891. expect(
  1892. lineNumberNodeForScreenRow(component, 1).classList.contains('c')
  1893. ).toBe(true);
  1894. marker.setScreenRange([[1, 0], [2, 4]]);
  1895. await component.getNextUpdatePromise();
  1896. expect(lineNodeForScreenRow(component, 1).classList.contains('a')).toBe(
  1897. false
  1898. );
  1899. expect(lineNodeForScreenRow(component, 1).classList.contains('b')).toBe(
  1900. true
  1901. );
  1902. expect(lineNodeForScreenRow(component, 1).classList.contains('c')).toBe(
  1903. true
  1904. );
  1905. expect(lineNodeForScreenRow(component, 2).classList.contains('b')).toBe(
  1906. true
  1907. );
  1908. expect(lineNodeForScreenRow(component, 2).classList.contains('c')).toBe(
  1909. true
  1910. );
  1911. expect(
  1912. lineNumberNodeForScreenRow(component, 1).classList.contains('a')
  1913. ).toBe(false);
  1914. expect(
  1915. lineNumberNodeForScreenRow(component, 1).classList.contains('b')
  1916. ).toBe(true);
  1917. expect(
  1918. lineNumberNodeForScreenRow(component, 1).classList.contains('c')
  1919. ).toBe(true);
  1920. expect(
  1921. lineNumberNodeForScreenRow(component, 2).classList.contains('b')
  1922. ).toBe(true);
  1923. expect(
  1924. lineNumberNodeForScreenRow(component, 2).classList.contains('c')
  1925. ).toBe(true);
  1926. });
  1927. it('honors the onlyHead option', async () => {
  1928. const { component, editor } = buildComponent();
  1929. const marker = editor.markScreenRange([[1, 4], [3, 4]]);
  1930. editor.decorateMarker(marker, {
  1931. type: ['line', 'line-number'],
  1932. class: 'a',
  1933. onlyHead: true
  1934. });
  1935. await component.getNextUpdatePromise();
  1936. expect(lineNodeForScreenRow(component, 1).classList.contains('a')).toBe(
  1937. false
  1938. );
  1939. expect(lineNodeForScreenRow(component, 3).classList.contains('a')).toBe(
  1940. true
  1941. );
  1942. expect(
  1943. lineNumberNodeForScreenRow(component, 1).classList.contains('a')
  1944. ).toBe(false);
  1945. expect(
  1946. lineNumberNodeForScreenRow(component, 3).classList.contains('a')
  1947. ).toBe(true);
  1948. });
  1949. it('only decorates the last row of non-empty ranges that end at column 0 if omitEmptyLastRow is false', async () => {
  1950. const { component, editor } = buildComponent();
  1951. const marker = editor.markScreenRange([[1, 0], [3, 0]]);
  1952. editor.decorateMarker(marker, {
  1953. type: ['line', 'line-number'],
  1954. class: 'a'
  1955. });
  1956. editor.decorateMarker(marker, {
  1957. type: ['line', 'line-number'],
  1958. class: 'b',
  1959. omitEmptyLastRow: false
  1960. });
  1961. await component.getNextUpdatePromise();
  1962. expect(lineNodeForScreenRow(component, 1).classList.contains('a')).toBe(
  1963. true
  1964. );
  1965. expect(lineNodeForScreenRow(component, 2).classList.contains('a')).toBe(
  1966. true
  1967. );
  1968. expect(lineNodeForScreenRow(component, 3).classList.contains('a')).toBe(
  1969. false
  1970. );
  1971. expect(lineNodeForScreenRow(component, 1).classList.contains('b')).toBe(
  1972. true
  1973. );
  1974. expect(lineNodeForScreenRow(component, 2).classList.contains('b')).toBe(
  1975. true
  1976. );
  1977. expect(lineNodeForScreenRow(component, 3).classList.contains('b')).toBe(
  1978. true
  1979. );
  1980. });
  1981. it('does not decorate invalidated markers', async () => {
  1982. const { component, editor } = buildComponent();
  1983. const marker = editor.markScreenRange([[1, 0], [3, 0]], {
  1984. invalidate: 'touch'
  1985. });
  1986. editor.decorateMarker(marker, {
  1987. type: ['line', 'line-number'],
  1988. class: 'a'
  1989. });
  1990. await component.getNextUpdatePromise();
  1991. expect(lineNodeForScreenRow(component, 2).classList.contains('a')).toBe(
  1992. true
  1993. );
  1994. editor.getBuffer().insert([2, 0], 'x');
  1995. expect(marker.isValid()).toBe(false);
  1996. await component.getNextUpdatePromise();
  1997. expect(lineNodeForScreenRow(component, 2).classList.contains('a')).toBe(
  1998. false
  1999. );
  2000. });
  2001. });
  2002. describe('highlight decorations', () => {
  2003. it('renders single-line highlights', async () => {
  2004. const { component, element, editor } = buildComponent();
  2005. const marker = editor.markScreenRange([[1, 2], [1, 10]]);
  2006. editor.decorateMarker(marker, { type: 'highlight', class: 'a' });
  2007. await component.getNextUpdatePromise();
  2008. {
  2009. const regions = element.querySelectorAll('.highlight.a .region.a');
  2010. expect(regions.length).toBe(1);
  2011. const regionRect = regions[0].getBoundingClientRect();
  2012. expect(regionRect.top).toBe(
  2013. lineNodeForScreenRow(component, 1).getBoundingClientRect().top
  2014. );
  2015. expect(Math.round(regionRect.left)).toBeNear(
  2016. clientLeftForCharacter(component, 1, 2)
  2017. );
  2018. expect(Math.round(regionRect.right)).toBeNear(
  2019. clientLeftForCharacter(component, 1, 10)
  2020. );
  2021. }
  2022. marker.setScreenRange([[1, 4], [1, 8]]);
  2023. await component.getNextUpdatePromise();
  2024. {
  2025. const regions = element.querySelectorAll('.highlight.a .region.a');
  2026. expect(regions.length).toBe(1);
  2027. const regionRect = regions[0].getBoundingClientRect();
  2028. expect(regionRect.top).toBe(
  2029. lineNodeForScreenRow(component, 1).getBoundingClientRect().top
  2030. );
  2031. expect(regionRect.bottom).toBe(
  2032. lineNodeForScreenRow(component, 1).getBoundingClientRect().bottom
  2033. );
  2034. expect(Math.round(regionRect.left)).toBeNear(
  2035. clientLeftForCharacter(component, 1, 4)
  2036. );
  2037. expect(Math.round(regionRect.right)).toBeNear(
  2038. clientLeftForCharacter(component, 1, 8)
  2039. );
  2040. }
  2041. });
  2042. it('renders multi-line highlights', async () => {
  2043. const { component, element, editor } = buildComponent({ rowsPerTile: 3 });
  2044. const marker = editor.markScreenRange([[2, 4], [3, 4]]);
  2045. editor.decorateMarker(marker, { type: 'highlight', class: 'a' });
  2046. await component.getNextUpdatePromise();
  2047. {
  2048. expect(element.querySelectorAll('.highlight.a').length).toBe(1);
  2049. const regions = element.querySelectorAll('.highlight.a .region.a');
  2050. expect(regions.length).toBe(2);
  2051. const region0Rect = regions[0].getBoundingClientRect();
  2052. expect(region0Rect.top).toBe(
  2053. lineNodeForScreenRow(component, 2).getBoundingClientRect().top
  2054. );
  2055. expect(region0Rect.bottom).toBe(
  2056. lineNodeForScreenRow(component, 2).getBoundingClientRect().bottom
  2057. );
  2058. expect(Math.round(region0Rect.left)).toBeNear(
  2059. clientLeftForCharacter(component, 2, 4)
  2060. );
  2061. expect(Math.round(region0Rect.right)).toBeNear(
  2062. component.refs.content.getBoundingClientRect().right
  2063. );
  2064. const region1Rect = regions[1].getBoundingClientRect();
  2065. expect(region1Rect.top).toBeNear(
  2066. lineNodeForScreenRow(component, 3).getBoundingClientRect().top
  2067. );
  2068. expect(region1Rect.bottom).toBeNear(
  2069. lineNodeForScreenRow(component, 3).getBoundingClientRect().bottom
  2070. );
  2071. expect(Math.round(region1Rect.left)).toBeNear(
  2072. clientLeftForCharacter(component, 3, 0)
  2073. );
  2074. expect(Math.round(region1Rect.right)).toBeNear(
  2075. clientLeftForCharacter(component, 3, 4)
  2076. );
  2077. }
  2078. marker.setScreenRange([[2, 4], [5, 4]]);
  2079. await component.getNextUpdatePromise();
  2080. {
  2081. expect(element.querySelectorAll('.highlight.a').length).toBe(1);
  2082. const regions = element.querySelectorAll('.highlight.a .region.a');
  2083. expect(regions.length).toBe(3);
  2084. const region0Rect = regions[0].getBoundingClientRect();
  2085. expect(region0Rect.top).toBeNear(
  2086. lineNodeForScreenRow(component, 2).getBoundingClientRect().top
  2087. );
  2088. expect(region0Rect.bottom).toBeNear(
  2089. lineNodeForScreenRow(component, 2).getBoundingClientRect().bottom
  2090. );
  2091. expect(Math.round(region0Rect.left)).toBeNear(
  2092. clientLeftForCharacter(component, 2, 4)
  2093. );
  2094. expect(Math.round(region0Rect.right)).toBeNear(
  2095. component.refs.content.getBoundingClientRect().right
  2096. );
  2097. const region1Rect = regions[1].getBoundingClientRect();
  2098. expect(region1Rect.top).toBeNear(
  2099. lineNodeForScreenRow(component, 3).getBoundingClientRect().top
  2100. );
  2101. expect(region1Rect.bottom).toBeNear(
  2102. lineNodeForScreenRow(component, 5).getBoundingClientRect().top
  2103. );
  2104. expect(Math.round(region1Rect.left)).toBeNear(
  2105. component.refs.content.getBoundingClientRect().left
  2106. );
  2107. expect(Math.round(region1Rect.right)).toBeNear(
  2108. component.refs.content.getBoundingClientRect().right
  2109. );
  2110. const region2Rect = regions[2].getBoundingClientRect();
  2111. expect(region2Rect.top).toBeNear(
  2112. lineNodeForScreenRow(component, 5).getBoundingClientRect().top
  2113. );
  2114. expect(region2Rect.bottom).toBeNear(
  2115. lineNodeForScreenRow(component, 6).getBoundingClientRect().top
  2116. );
  2117. expect(Math.round(region2Rect.left)).toBeNear(
  2118. component.refs.content.getBoundingClientRect().left
  2119. );
  2120. expect(Math.round(region2Rect.right)).toBeNear(
  2121. clientLeftForCharacter(component, 5, 4)
  2122. );
  2123. }
  2124. });
  2125. it('can flash highlight decorations', async () => {
  2126. const { component, element, editor } = buildComponent({
  2127. rowsPerTile: 3,
  2128. height: 200
  2129. });
  2130. const marker = editor.markScreenRange([[2, 4], [3, 4]]);
  2131. const decoration = editor.decorateMarker(marker, {
  2132. type: 'highlight',
  2133. class: 'a'
  2134. });
  2135. decoration.flash('b', 10);
  2136. // Flash on initial appearance of highlight
  2137. await component.getNextUpdatePromise();
  2138. const highlights = element.querySelectorAll('.highlight.a');
  2139. expect(highlights.length).toBe(1);
  2140. expect(highlights[0].classList.contains('b')).toBe(true);
  2141. await conditionPromise(() => !highlights[0].classList.contains('b'));
  2142. // Don't flash on next update if another flash wasn't requested
  2143. await setScrollTop(component, 100);
  2144. expect(highlights[0].classList.contains('b')).toBe(false);
  2145. // Flashing the same class again before the first flash completes
  2146. // removes the flash class and adds it back on the next frame to ensure
  2147. // CSS transitions apply to the second flash.
  2148. decoration.flash('e', 100);
  2149. await component.getNextUpdatePromise();
  2150. expect(highlights[0].classList.contains('e')).toBe(true);
  2151. decoration.flash('e', 100);
  2152. await component.getNextUpdatePromise();
  2153. expect(highlights[0].classList.contains('e')).toBe(false);
  2154. await conditionPromise(() => highlights[0].classList.contains('e'));
  2155. await conditionPromise(() => !highlights[0].classList.contains('e'));
  2156. });
  2157. it("flashing a highlight decoration doesn't unflash other highlight decorations", async () => {
  2158. const { component, element, editor } = buildComponent({
  2159. rowsPerTile: 3,
  2160. height: 200
  2161. });
  2162. const marker = editor.markScreenRange([[2, 4], [3, 4]]);
  2163. const decoration = editor.decorateMarker(marker, {
  2164. type: 'highlight',
  2165. class: 'a'
  2166. });
  2167. // Flash one class
  2168. decoration.flash('c', 1000);
  2169. await component.getNextUpdatePromise();
  2170. const highlights = element.querySelectorAll('.highlight.a');
  2171. expect(highlights.length).toBe(1);
  2172. expect(highlights[0].classList.contains('c')).toBe(true);
  2173. // Flash another class while the previously-flashed class is still highlighted
  2174. decoration.flash('d', 100);
  2175. await component.getNextUpdatePromise();
  2176. expect(highlights[0].classList.contains('c')).toBe(true);
  2177. expect(highlights[0].classList.contains('d')).toBe(true);
  2178. });
  2179. it('supports layer decorations', async () => {
  2180. const { component, element, editor } = buildComponent({
  2181. rowsPerTile: 12
  2182. });
  2183. const markerLayer = editor.addMarkerLayer();
  2184. const marker1 = markerLayer.markScreenRange([[2, 4], [3, 4]]);
  2185. const marker2 = markerLayer.markScreenRange([[5, 6], [7, 8]]);
  2186. const decoration = editor.decorateMarkerLayer(markerLayer, {
  2187. type: 'highlight',
  2188. class: 'a'
  2189. });
  2190. await component.getNextUpdatePromise();
  2191. const highlights = element.querySelectorAll('.highlight');
  2192. expect(highlights[0].classList.contains('a')).toBe(true);
  2193. expect(highlights[1].classList.contains('a')).toBe(true);
  2194. decoration.setPropertiesForMarker(marker1, {
  2195. type: 'highlight',
  2196. class: 'b'
  2197. });
  2198. await component.getNextUpdatePromise();
  2199. expect(highlights[0].classList.contains('b')).toBe(true);
  2200. expect(highlights[1].classList.contains('a')).toBe(true);
  2201. decoration.setPropertiesForMarker(marker1, null);
  2202. decoration.setPropertiesForMarker(marker2, {
  2203. type: 'highlight',
  2204. class: 'c'
  2205. });
  2206. await component.getNextUpdatePromise();
  2207. expect(highlights[0].classList.contains('a')).toBe(true);
  2208. expect(highlights[1].classList.contains('c')).toBe(true);
  2209. });
  2210. it('clears highlights when recycling a tile that previously contained highlights and now does not', async () => {
  2211. const { component, element, editor } = buildComponent({
  2212. rowsPerTile: 2,
  2213. autoHeight: false
  2214. });
  2215. await setEditorHeightInLines(component, 2);
  2216. const marker = editor.markScreenRange([[1, 2], [1, 10]]);
  2217. editor.decorateMarker(marker, { type: 'highlight', class: 'a' });
  2218. await component.getNextUpdatePromise();
  2219. expect(element.querySelectorAll('.highlight.a').length).toBe(1);
  2220. await setScrollTop(component, component.getLineHeight() * 3);
  2221. expect(element.querySelectorAll('.highlight.a').length).toBe(0);
  2222. });
  2223. it('does not move existing highlights when adding or removing other highlight decorations (regression)', async () => {
  2224. const { component, element, editor } = buildComponent();
  2225. const marker1 = editor.markScreenRange([[1, 6], [1, 10]]);
  2226. editor.decorateMarker(marker1, { type: 'highlight', class: 'a' });
  2227. await component.getNextUpdatePromise();
  2228. const marker1Region = element.querySelector('.highlight.a');
  2229. expect(
  2230. Array.from(marker1Region.parentElement.children).indexOf(marker1Region)
  2231. ).toBe(0);
  2232. const marker2 = editor.markScreenRange([[1, 2], [1, 4]]);
  2233. editor.decorateMarker(marker2, { type: 'highlight', class: 'b' });
  2234. await component.getNextUpdatePromise();
  2235. const marker2Region = element.querySelector('.highlight.b');
  2236. expect(
  2237. Array.from(marker1Region.parentElement.children).indexOf(marker1Region)
  2238. ).toBe(0);
  2239. expect(
  2240. Array.from(marker2Region.parentElement.children).indexOf(marker2Region)
  2241. ).toBe(1);
  2242. marker2.destroy();
  2243. await component.getNextUpdatePromise();
  2244. expect(
  2245. Array.from(marker1Region.parentElement.children).indexOf(marker1Region)
  2246. ).toBe(0);
  2247. });
  2248. it('correctly positions highlights that end on rows preceding or following block decorations', async () => {
  2249. const { editor, element, component } = buildComponent();
  2250. const item1 = document.createElement('div');
  2251. item1.style.height = '30px';
  2252. item1.style.backgroundColor = 'blue';
  2253. editor.decorateMarker(editor.markBufferPosition([4, 0]), {
  2254. type: 'block',
  2255. position: 'after',
  2256. item: item1
  2257. });
  2258. const item2 = document.createElement('div');
  2259. item2.style.height = '30px';
  2260. item2.style.backgroundColor = 'yellow';
  2261. editor.decorateMarker(editor.markBufferPosition([4, 0]), {
  2262. type: 'block',
  2263. position: 'before',
  2264. item: item2
  2265. });
  2266. editor.decorateMarker(editor.markBufferRange([[3, 0], [4, Infinity]]), {
  2267. type: 'highlight',
  2268. class: 'highlight'
  2269. });
  2270. await component.getNextUpdatePromise();
  2271. const regions = element.querySelectorAll('.highlight .region');
  2272. expect(regions[0].offsetTop).toBeNear(3 * component.getLineHeight());
  2273. expect(regions[0].offsetHeight).toBeNear(component.getLineHeight());
  2274. expect(regions[1].offsetTop).toBeNear(4 * component.getLineHeight() + 30);
  2275. });
  2276. });
  2277. describe('overlay decorations', () => {
  2278. function attachFakeWindow(component) {
  2279. const fakeWindow = document.createElement('div');
  2280. fakeWindow.style.position = 'absolute';
  2281. fakeWindow.style.padding = 20 + 'px';
  2282. fakeWindow.style.backgroundColor = 'blue';
  2283. fakeWindow.appendChild(component.element);
  2284. jasmine.attachToDOM(fakeWindow);
  2285. spyOn(component, 'getWindowInnerWidth').andCallFake(
  2286. () => fakeWindow.getBoundingClientRect().width
  2287. );
  2288. spyOn(component, 'getWindowInnerHeight').andCallFake(
  2289. () => fakeWindow.getBoundingClientRect().height
  2290. );
  2291. return fakeWindow;
  2292. }
  2293. it('renders overlay elements at the specified screen position unless it would overflow the window', async () => {
  2294. const { component, editor } = buildComponent({
  2295. width: 200,
  2296. height: 100,
  2297. attach: false
  2298. });
  2299. const fakeWindow = attachFakeWindow(component);
  2300. await setScrollTop(component, 50);
  2301. await setScrollLeft(component, 100);
  2302. const marker = editor.markScreenPosition([4, 25]);
  2303. const overlayElement = document.createElement('div');
  2304. overlayElement.style.width = '50px';
  2305. overlayElement.style.height = '50px';
  2306. overlayElement.style.margin = '3px';
  2307. overlayElement.style.backgroundColor = 'red';
  2308. const decoration = editor.decorateMarker(marker, {
  2309. type: 'overlay',
  2310. item: overlayElement,
  2311. class: 'a'
  2312. });
  2313. await component.getNextUpdatePromise();
  2314. const overlayComponent = component.overlayComponents.values().next()
  2315. .value;
  2316. const overlayWrapper = overlayElement.parentElement;
  2317. expect(overlayWrapper.classList.contains('a')).toBe(true);
  2318. expect(overlayWrapper.getBoundingClientRect().top).toBeNear(
  2319. clientTopForLine(component, 5)
  2320. );
  2321. expect(overlayWrapper.getBoundingClientRect().left).toBeNear(
  2322. clientLeftForCharacter(component, 4, 25)
  2323. );
  2324. // Updates the horizontal position on scroll
  2325. await setScrollLeft(component, 150);
  2326. expect(overlayWrapper.getBoundingClientRect().left).toBeNear(
  2327. clientLeftForCharacter(component, 4, 25)
  2328. );
  2329. // Shifts the overlay horizontally to ensure the overlay element does not
  2330. // overflow the window
  2331. await setScrollLeft(component, 30);
  2332. expect(overlayElement.getBoundingClientRect().right).toBeNear(
  2333. fakeWindow.getBoundingClientRect().right
  2334. );
  2335. await setScrollLeft(component, 280);
  2336. expect(overlayElement.getBoundingClientRect().left).toBeNear(
  2337. fakeWindow.getBoundingClientRect().left
  2338. );
  2339. // Updates the vertical position on scroll
  2340. await setScrollTop(component, 60);
  2341. expect(overlayWrapper.getBoundingClientRect().top).toBeNear(
  2342. clientTopForLine(component, 5)
  2343. );
  2344. // Flips the overlay vertically to ensure the overlay element does not
  2345. // overflow the bottom of the window
  2346. setScrollLeft(component, 100);
  2347. await setScrollTop(component, 0);
  2348. expect(overlayWrapper.getBoundingClientRect().bottom).toBeNear(
  2349. clientTopForLine(component, 4)
  2350. );
  2351. // Flips the overlay vertically on overlay resize if necessary
  2352. await setScrollTop(component, 20);
  2353. expect(overlayWrapper.getBoundingClientRect().top).toBeNear(
  2354. clientTopForLine(component, 5)
  2355. );
  2356. overlayElement.style.height = 60 + 'px';
  2357. await overlayComponent.getNextUpdatePromise();
  2358. expect(overlayWrapper.getBoundingClientRect().bottom).toBeNear(
  2359. clientTopForLine(component, 4)
  2360. );
  2361. // Does not flip the overlay vertically if it would overflow the top of the window
  2362. overlayElement.style.height = 80 + 'px';
  2363. await overlayComponent.getNextUpdatePromise();
  2364. expect(overlayWrapper.getBoundingClientRect().top).toBeNear(
  2365. clientTopForLine(component, 5)
  2366. );
  2367. // Can update overlay wrapper class
  2368. decoration.setProperties({
  2369. type: 'overlay',
  2370. item: overlayElement,
  2371. class: 'b'
  2372. });
  2373. await component.getNextUpdatePromise();
  2374. expect(overlayWrapper.classList.contains('a')).toBe(false);
  2375. expect(overlayWrapper.classList.contains('b')).toBe(true);
  2376. decoration.setProperties({ type: 'overlay', item: overlayElement });
  2377. await component.getNextUpdatePromise();
  2378. expect(overlayWrapper.classList.contains('b')).toBe(false);
  2379. });
  2380. it('does not attempt to avoid overflowing the window if `avoidOverflow` is false on the decoration', async () => {
  2381. const { component, editor } = buildComponent({
  2382. width: 200,
  2383. height: 100,
  2384. attach: false
  2385. });
  2386. const fakeWindow = attachFakeWindow(component);
  2387. const overlayElement = document.createElement('div');
  2388. overlayElement.style.width = '50px';
  2389. overlayElement.style.height = '50px';
  2390. overlayElement.style.margin = '3px';
  2391. overlayElement.style.backgroundColor = 'red';
  2392. const marker = editor.markScreenPosition([4, 25]);
  2393. editor.decorateMarker(marker, {
  2394. type: 'overlay',
  2395. item: overlayElement,
  2396. avoidOverflow: false
  2397. });
  2398. await component.getNextUpdatePromise();
  2399. await setScrollLeft(component, 30);
  2400. expect(overlayElement.getBoundingClientRect().right).toBeGreaterThan(
  2401. fakeWindow.getBoundingClientRect().right
  2402. );
  2403. await setScrollLeft(component, 280);
  2404. expect(overlayElement.getBoundingClientRect().left).toBeLessThan(
  2405. fakeWindow.getBoundingClientRect().left
  2406. );
  2407. });
  2408. });
  2409. describe('custom gutter decorations', () => {
  2410. it('arranges custom gutters based on their priority', async () => {
  2411. const { component, editor } = buildComponent();
  2412. editor.addGutter({ name: 'e', priority: 2 });
  2413. editor.addGutter({ name: 'a', priority: -2 });
  2414. editor.addGutter({ name: 'd', priority: 1 });
  2415. editor.addGutter({ name: 'b', priority: -1 });
  2416. editor.addGutter({ name: 'c', priority: 0 });
  2417. await component.getNextUpdatePromise();
  2418. const gutters = component.refs.gutterContainer.element.querySelectorAll(
  2419. '.gutter'
  2420. );
  2421. expect(
  2422. Array.from(gutters).map(g => g.getAttribute('gutter-name'))
  2423. ).toEqual(['a', 'b', 'c', 'line-number', 'd', 'e']);
  2424. });
  2425. it('adjusts the left edge of the scroll container based on changes to the gutter container width', async () => {
  2426. const { component, editor } = buildComponent();
  2427. const { scrollContainer, gutterContainer } = component.refs;
  2428. function checkScrollContainerLeft() {
  2429. expect(scrollContainer.getBoundingClientRect().left).toBeNear(
  2430. Math.round(gutterContainer.element.getBoundingClientRect().right)
  2431. );
  2432. }
  2433. checkScrollContainerLeft();
  2434. const gutterA = editor.addGutter({ name: 'a' });
  2435. await component.getNextUpdatePromise();
  2436. checkScrollContainerLeft();
  2437. const gutterB = editor.addGutter({ name: 'b' });
  2438. await component.getNextUpdatePromise();
  2439. checkScrollContainerLeft();
  2440. gutterA.getElement().style.width = 100 + 'px';
  2441. await component.getNextUpdatePromise();
  2442. checkScrollContainerLeft();
  2443. gutterA.hide();
  2444. await component.getNextUpdatePromise();
  2445. checkScrollContainerLeft();
  2446. gutterA.show();
  2447. await component.getNextUpdatePromise();
  2448. checkScrollContainerLeft();
  2449. gutterA.destroy();
  2450. await component.getNextUpdatePromise();
  2451. checkScrollContainerLeft();
  2452. gutterB.destroy();
  2453. await component.getNextUpdatePromise();
  2454. checkScrollContainerLeft();
  2455. });
  2456. it('allows the element of custom gutters to be retrieved before being rendered in the editor component', async () => {
  2457. const { component, element, editor } = buildComponent();
  2458. const [lineNumberGutter] = editor.getGutters();
  2459. const gutterA = editor.addGutter({ name: 'a', priority: -1 });
  2460. const gutterB = editor.addGutter({ name: 'b', priority: 1 });
  2461. const lineNumberGutterElement = lineNumberGutter.getElement();
  2462. const gutterAElement = gutterA.getElement();
  2463. const gutterBElement = gutterB.getElement();
  2464. await component.getNextUpdatePromise();
  2465. expect(element.contains(lineNumberGutterElement)).toBe(true);
  2466. expect(element.contains(gutterAElement)).toBe(true);
  2467. expect(element.contains(gutterBElement)).toBe(true);
  2468. });
  2469. it('can show and hide custom gutters', async () => {
  2470. const { component, editor } = buildComponent();
  2471. const gutterA = editor.addGutter({ name: 'a', priority: -1 });
  2472. const gutterB = editor.addGutter({ name: 'b', priority: 1 });
  2473. const gutterAElement = gutterA.getElement();
  2474. const gutterBElement = gutterB.getElement();
  2475. await component.getNextUpdatePromise();
  2476. expect(gutterAElement.style.display).toBe('');
  2477. expect(gutterBElement.style.display).toBe('');
  2478. gutterA.hide();
  2479. await component.getNextUpdatePromise();
  2480. expect(gutterAElement.style.display).toBe('none');
  2481. expect(gutterBElement.style.display).toBe('');
  2482. gutterB.hide();
  2483. await component.getNextUpdatePromise();
  2484. expect(gutterAElement.style.display).toBe('none');
  2485. expect(gutterBElement.style.display).toBe('none');
  2486. gutterA.show();
  2487. await component.getNextUpdatePromise();
  2488. expect(gutterAElement.style.display).toBe('');
  2489. expect(gutterBElement.style.display).toBe('none');
  2490. });
  2491. it('renders decorations in custom gutters', async () => {
  2492. const { component, element, editor } = buildComponent();
  2493. const gutterA = editor.addGutter({ name: 'a', priority: -1 });
  2494. const gutterB = editor.addGutter({ name: 'b', priority: 1 });
  2495. const marker1 = editor.markScreenRange([[2, 0], [4, 0]]);
  2496. const marker2 = editor.markScreenRange([[6, 0], [7, 0]]);
  2497. const marker3 = editor.markScreenRange([[9, 0], [12, 0]]);
  2498. const decorationElement1 = document.createElement('div');
  2499. const decorationElement2 = document.createElement('div');
  2500. // Packages may adopt this class name for decorations to be styled the same as line numbers
  2501. decorationElement2.className = 'line-number';
  2502. const decoration1 = gutterA.decorateMarker(marker1, { class: 'a' });
  2503. const decoration2 = gutterA.decorateMarker(marker2, {
  2504. class: 'b',
  2505. item: decorationElement1
  2506. });
  2507. const decoration3 = gutterB.decorateMarker(marker3, {
  2508. item: decorationElement2
  2509. });
  2510. await component.getNextUpdatePromise();
  2511. let [
  2512. decorationNode1,
  2513. decorationNode2
  2514. ] = gutterA.getElement().firstChild.children;
  2515. const [decorationNode3] = gutterB.getElement().firstChild.children;
  2516. expect(decorationNode1.className).toBe('decoration a');
  2517. expect(decorationNode1.getBoundingClientRect().top).toBeNear(
  2518. clientTopForLine(component, 2)
  2519. );
  2520. expect(decorationNode1.getBoundingClientRect().bottom).toBeNear(
  2521. clientTopForLine(component, 5)
  2522. );
  2523. expect(decorationNode1.firstChild).toBeNull();
  2524. expect(decorationNode2.className).toBe('decoration b');
  2525. expect(decorationNode2.getBoundingClientRect().top).toBeNear(
  2526. clientTopForLine(component, 6)
  2527. );
  2528. expect(decorationNode2.getBoundingClientRect().bottom).toBeNear(
  2529. clientTopForLine(component, 8)
  2530. );
  2531. expect(decorationNode2.firstChild).toBe(decorationElement1);
  2532. expect(decorationElement1.offsetHeight).toBe(
  2533. decorationNode2.offsetHeight
  2534. );
  2535. expect(decorationElement1.offsetWidth).toBe(decorationNode2.offsetWidth);
  2536. expect(decorationNode3.className).toBe('decoration');
  2537. expect(decorationNode3.getBoundingClientRect().top).toBeNear(
  2538. clientTopForLine(component, 9)
  2539. );
  2540. expect(decorationNode3.getBoundingClientRect().bottom).toBeNear(
  2541. clientTopForLine(component, 12) + component.getLineHeight()
  2542. );
  2543. expect(decorationNode3.firstChild).toBe(decorationElement2);
  2544. expect(decorationElement2.offsetHeight).toBe(
  2545. decorationNode3.offsetHeight
  2546. );
  2547. expect(decorationElement2.offsetWidth).toBe(decorationNode3.offsetWidth);
  2548. // Inline styled height is updated when line height changes
  2549. element.style.fontSize =
  2550. parseInt(getComputedStyle(element).fontSize) + 10 + 'px';
  2551. TextEditor.didUpdateStyles();
  2552. await component.getNextUpdatePromise();
  2553. expect(decorationElement1.offsetHeight).toBe(
  2554. decorationNode2.offsetHeight
  2555. );
  2556. expect(decorationElement2.offsetHeight).toBe(
  2557. decorationNode3.offsetHeight
  2558. );
  2559. decoration1.setProperties({
  2560. type: 'gutter',
  2561. gutterName: 'a',
  2562. class: 'c',
  2563. item: decorationElement1
  2564. });
  2565. decoration2.setProperties({ type: 'gutter', gutterName: 'a' });
  2566. decoration3.destroy();
  2567. await component.getNextUpdatePromise();
  2568. expect(decorationNode1.className).toBe('decoration c');
  2569. expect(decorationNode1.firstChild).toBe(decorationElement1);
  2570. expect(decorationElement1.offsetHeight).toBe(
  2571. decorationNode1.offsetHeight
  2572. );
  2573. expect(decorationNode2.className).toBe('decoration');
  2574. expect(decorationNode2.firstChild).toBeNull();
  2575. expect(gutterB.getElement().firstChild.children.length).toBe(0);
  2576. });
  2577. it('renders custom line number gutters', async () => {
  2578. const { component, editor } = buildComponent();
  2579. const gutterA = editor.addGutter({
  2580. name: 'a',
  2581. priority: 1,
  2582. type: 'line-number',
  2583. class: 'a-number',
  2584. labelFn: ({ bufferRow }) => `a - ${bufferRow}`
  2585. });
  2586. const gutterB = editor.addGutter({
  2587. name: 'b',
  2588. priority: 1,
  2589. type: 'line-number',
  2590. class: 'b-number',
  2591. labelFn: ({ bufferRow }) => `b - ${bufferRow}`
  2592. });
  2593. editor.setText('0000\n0001\n0002\n0003\n0004\n');
  2594. await component.getNextUpdatePromise();
  2595. const gutterAElement = gutterA.getElement();
  2596. const aNumbers = gutterAElement.querySelectorAll(
  2597. 'div.line-number[data-buffer-row]'
  2598. );
  2599. const aLabels = Array.from(aNumbers, e => e.textContent);
  2600. expect(aLabels).toEqual([
  2601. 'a - 0',
  2602. 'a - 1',
  2603. 'a - 2',
  2604. 'a - 3',
  2605. 'a - 4',
  2606. 'a - 5'
  2607. ]);
  2608. const gutterBElement = gutterB.getElement();
  2609. const bNumbers = gutterBElement.querySelectorAll(
  2610. 'div.line-number[data-buffer-row]'
  2611. );
  2612. const bLabels = Array.from(bNumbers, e => e.textContent);
  2613. expect(bLabels).toEqual([
  2614. 'b - 0',
  2615. 'b - 1',
  2616. 'b - 2',
  2617. 'b - 3',
  2618. 'b - 4',
  2619. 'b - 5'
  2620. ]);
  2621. });
  2622. it("updates the editor's soft wrap width when a custom gutter's measurement is available", () => {
  2623. const { component, element, editor } = buildComponent({
  2624. lineNumberGutterVisible: false,
  2625. width: 400,
  2626. softWrapped: true,
  2627. attach: false
  2628. });
  2629. const gutter = editor.addGutter({ name: 'a', priority: 10 });
  2630. gutter.getElement().style.width = '100px';
  2631. jasmine.attachToDOM(element);
  2632. expect(component.getGutterContainerWidth()).toBe(100);
  2633. // Component client width - gutter container width - vertical scrollbar width
  2634. const softWrapColumn = Math.floor(
  2635. (400 - 100 - component.getVerticalScrollbarWidth()) /
  2636. component.getBaseCharacterWidth()
  2637. );
  2638. expect(editor.getSoftWrapColumn()).toBe(softWrapColumn);
  2639. });
  2640. });
  2641. describe('block decorations', () => {
  2642. it('renders visible block decorations between the appropriate lines, refreshing and measuring them as needed', async () => {
  2643. const editor = buildEditor({ autoHeight: false });
  2644. const {
  2645. item: item1,
  2646. decoration: decoration1
  2647. } = createBlockDecorationAtScreenRow(editor, 0, {
  2648. height: 11,
  2649. position: 'before'
  2650. });
  2651. const {
  2652. item: item2,
  2653. decoration: decoration2
  2654. } = createBlockDecorationAtScreenRow(editor, 2, {
  2655. height: 22,
  2656. margin: 10,
  2657. position: 'before'
  2658. });
  2659. // render an editor that already contains some block decorations
  2660. const { component, element } = buildComponent({ editor, rowsPerTile: 3 });
  2661. element.style.height =
  2662. 4 * component.getLineHeight() + horizontalScrollbarHeight + 'px';
  2663. await component.getNextUpdatePromise();
  2664. expect(component.getRenderedStartRow()).toBe(0);
  2665. expect(component.getRenderedEndRow()).toBe(9);
  2666. expect(component.getScrollHeight()).toBeNear(
  2667. editor.getScreenLineCount() * component.getLineHeight() +
  2668. getElementHeight(item1) +
  2669. getElementHeight(item2)
  2670. );
  2671. assertTilesAreSizedAndPositionedCorrectly(component, [
  2672. {
  2673. tileStartRow: 0,
  2674. height:
  2675. 3 * component.getLineHeight() +
  2676. getElementHeight(item1) +
  2677. getElementHeight(item2)
  2678. },
  2679. { tileStartRow: 3, height: 3 * component.getLineHeight() }
  2680. ]);
  2681. assertLinesAreAlignedWithLineNumbers(component);
  2682. expect(queryOnScreenLineElements(element).length).toBe(9);
  2683. expect(item1.previousSibling).toBeNull();
  2684. expect(item1.nextSibling).toBe(lineNodeForScreenRow(component, 0));
  2685. expect(item2.previousSibling).toBe(lineNodeForScreenRow(component, 1));
  2686. expect(item2.nextSibling).toBe(lineNodeForScreenRow(component, 2));
  2687. // add block decorations
  2688. const {
  2689. item: item3,
  2690. decoration: decoration3
  2691. } = createBlockDecorationAtScreenRow(editor, 4, {
  2692. height: 33,
  2693. position: 'before'
  2694. });
  2695. const { item: item4 } = createBlockDecorationAtScreenRow(editor, 7, {
  2696. height: 44,
  2697. position: 'before'
  2698. });
  2699. const { item: item5 } = createBlockDecorationAtScreenRow(editor, 7, {
  2700. height: 50,
  2701. marginBottom: 5,
  2702. position: 'after'
  2703. });
  2704. const { item: item6 } = createBlockDecorationAtScreenRow(editor, 12, {
  2705. height: 60,
  2706. marginTop: 6,
  2707. position: 'after'
  2708. });
  2709. await component.getNextUpdatePromise();
  2710. expect(component.getRenderedStartRow()).toBe(0);
  2711. expect(component.getRenderedEndRow()).toBe(9);
  2712. expect(component.getScrollHeight()).toBeNear(
  2713. editor.getScreenLineCount() * component.getLineHeight() +
  2714. getElementHeight(item1) +
  2715. getElementHeight(item2) +
  2716. getElementHeight(item3) +
  2717. getElementHeight(item4) +
  2718. getElementHeight(item5) +
  2719. getElementHeight(item6)
  2720. );
  2721. assertTilesAreSizedAndPositionedCorrectly(component, [
  2722. {
  2723. tileStartRow: 0,
  2724. height:
  2725. 3 * component.getLineHeight() +
  2726. getElementHeight(item1) +
  2727. getElementHeight(item2)
  2728. },
  2729. {
  2730. tileStartRow: 3,
  2731. height: 3 * component.getLineHeight() + getElementHeight(item3)
  2732. }
  2733. ]);
  2734. assertLinesAreAlignedWithLineNumbers(component);
  2735. expect(queryOnScreenLineElements(element).length).toBe(9);
  2736. expect(item1.previousSibling).toBeNull();
  2737. expect(item1.nextSibling).toBe(lineNodeForScreenRow(component, 0));
  2738. expect(item2.previousSibling).toBe(lineNodeForScreenRow(component, 1));
  2739. expect(item2.nextSibling).toBe(lineNodeForScreenRow(component, 2));
  2740. expect(item3.previousSibling).toBe(lineNodeForScreenRow(component, 3));
  2741. expect(item3.nextSibling).toBe(lineNodeForScreenRow(component, 4));
  2742. expect(item4.nextSibling).toBe(lineNodeForScreenRow(component, 7));
  2743. expect(item5.previousSibling).toBe(lineNodeForScreenRow(component, 7));
  2744. expect(element.contains(item6)).toBe(false);
  2745. // destroy decoration1
  2746. decoration1.destroy();
  2747. await component.getNextUpdatePromise();
  2748. expect(component.getRenderedStartRow()).toBe(0);
  2749. expect(component.getRenderedEndRow()).toBe(9);
  2750. expect(component.getScrollHeight()).toBeNear(
  2751. editor.getScreenLineCount() * component.getLineHeight() +
  2752. getElementHeight(item2) +
  2753. getElementHeight(item3) +
  2754. getElementHeight(item4) +
  2755. getElementHeight(item5) +
  2756. getElementHeight(item6)
  2757. );
  2758. assertTilesAreSizedAndPositionedCorrectly(component, [
  2759. {
  2760. tileStartRow: 0,
  2761. height: 3 * component.getLineHeight() + getElementHeight(item2)
  2762. },
  2763. {
  2764. tileStartRow: 3,
  2765. height: 3 * component.getLineHeight() + getElementHeight(item3)
  2766. }
  2767. ]);
  2768. assertLinesAreAlignedWithLineNumbers(component);
  2769. expect(queryOnScreenLineElements(element).length).toBe(9);
  2770. expect(element.contains(item1)).toBe(false);
  2771. expect(item2.previousSibling).toBe(lineNodeForScreenRow(component, 1));
  2772. expect(item2.nextSibling).toBe(lineNodeForScreenRow(component, 2));
  2773. expect(item3.previousSibling).toBe(lineNodeForScreenRow(component, 3));
  2774. expect(item3.nextSibling).toBe(lineNodeForScreenRow(component, 4));
  2775. expect(item4.nextSibling).toBe(lineNodeForScreenRow(component, 7));
  2776. expect(item5.previousSibling).toBe(lineNodeForScreenRow(component, 7));
  2777. expect(element.contains(item6)).toBe(false);
  2778. // move decoration2 and decoration3
  2779. decoration2.getMarker().setHeadScreenPosition([1, 0]);
  2780. decoration3.getMarker().setHeadScreenPosition([0, 0]);
  2781. await component.getNextUpdatePromise();
  2782. expect(component.getRenderedStartRow()).toBe(0);
  2783. expect(component.getRenderedEndRow()).toBe(9);
  2784. expect(component.getScrollHeight()).toBeNear(
  2785. editor.getScreenLineCount() * component.getLineHeight() +
  2786. getElementHeight(item2) +
  2787. getElementHeight(item3) +
  2788. getElementHeight(item4) +
  2789. getElementHeight(item5) +
  2790. getElementHeight(item6)
  2791. );
  2792. assertTilesAreSizedAndPositionedCorrectly(component, [
  2793. {
  2794. tileStartRow: 0,
  2795. height:
  2796. 3 * component.getLineHeight() +
  2797. getElementHeight(item2) +
  2798. getElementHeight(item3)
  2799. },
  2800. { tileStartRow: 3, height: 3 * component.getLineHeight() }
  2801. ]);
  2802. assertLinesAreAlignedWithLineNumbers(component);
  2803. expect(queryOnScreenLineElements(element).length).toBe(9);
  2804. expect(element.contains(item1)).toBe(false);
  2805. expect(item2.previousSibling).toBe(lineNodeForScreenRow(component, 0));
  2806. expect(item2.nextSibling).toBe(lineNodeForScreenRow(component, 1));
  2807. expect(item3.previousSibling).toBeNull();
  2808. expect(item3.nextSibling).toBe(lineNodeForScreenRow(component, 0));
  2809. expect(item4.nextSibling).toBe(lineNodeForScreenRow(component, 7));
  2810. expect(item5.previousSibling).toBe(lineNodeForScreenRow(component, 7));
  2811. expect(element.contains(item6)).toBe(false);
  2812. // change the text
  2813. editor.getBuffer().setTextInRange([[0, 5], [0, 5]], '\n\n');
  2814. await component.getNextUpdatePromise();
  2815. expect(component.getRenderedStartRow()).toBe(0);
  2816. expect(component.getRenderedEndRow()).toBe(9);
  2817. expect(component.getScrollHeight()).toBeNear(
  2818. editor.getScreenLineCount() * component.getLineHeight() +
  2819. getElementHeight(item2) +
  2820. getElementHeight(item3) +
  2821. getElementHeight(item4) +
  2822. getElementHeight(item5) +
  2823. getElementHeight(item6)
  2824. );
  2825. assertTilesAreSizedAndPositionedCorrectly(component, [
  2826. {
  2827. tileStartRow: 0,
  2828. height: 3 * component.getLineHeight() + getElementHeight(item3)
  2829. },
  2830. {
  2831. tileStartRow: 3,
  2832. height: 3 * component.getLineHeight() + getElementHeight(item2)
  2833. }
  2834. ]);
  2835. assertLinesAreAlignedWithLineNumbers(component);
  2836. expect(queryOnScreenLineElements(element).length).toBe(9);
  2837. expect(element.contains(item1)).toBe(false);
  2838. expect(item2.previousSibling).toBeNull();
  2839. expect(item2.nextSibling).toBe(lineNodeForScreenRow(component, 3));
  2840. expect(item3.previousSibling).toBeNull();
  2841. expect(item3.nextSibling).toBe(lineNodeForScreenRow(component, 0));
  2842. expect(element.contains(item4)).toBe(false);
  2843. expect(element.contains(item5)).toBe(false);
  2844. expect(element.contains(item6)).toBe(false);
  2845. // scroll past the first tile
  2846. await setScrollTop(
  2847. component,
  2848. 3 * component.getLineHeight() + getElementHeight(item3)
  2849. );
  2850. expect(component.getRenderedStartRow()).toBe(3);
  2851. expect(component.getRenderedEndRow()).toBe(12);
  2852. expect(component.getScrollHeight()).toBeNear(
  2853. editor.getScreenLineCount() * component.getLineHeight() +
  2854. getElementHeight(item2) +
  2855. getElementHeight(item3) +
  2856. getElementHeight(item4) +
  2857. getElementHeight(item5) +
  2858. getElementHeight(item6)
  2859. );
  2860. assertTilesAreSizedAndPositionedCorrectly(component, [
  2861. {
  2862. tileStartRow: 3,
  2863. height: 3 * component.getLineHeight() + getElementHeight(item2)
  2864. },
  2865. { tileStartRow: 6, height: 3 * component.getLineHeight() }
  2866. ]);
  2867. assertLinesAreAlignedWithLineNumbers(component);
  2868. expect(queryOnScreenLineElements(element).length).toBe(9);
  2869. expect(element.contains(item1)).toBe(false);
  2870. expect(item2.previousSibling).toBeNull();
  2871. expect(item2.nextSibling).toBe(lineNodeForScreenRow(component, 3));
  2872. expect(element.contains(item3)).toBe(false);
  2873. expect(item4.nextSibling).toBe(lineNodeForScreenRow(component, 9));
  2874. expect(item5.previousSibling).toBe(lineNodeForScreenRow(component, 9));
  2875. expect(element.contains(item6)).toBe(false);
  2876. await setScrollTop(component, 0);
  2877. // undo the previous change
  2878. editor.undo();
  2879. await component.getNextUpdatePromise();
  2880. expect(component.getRenderedStartRow()).toBe(0);
  2881. expect(component.getRenderedEndRow()).toBe(9);
  2882. expect(component.getScrollHeight()).toBeNear(
  2883. editor.getScreenLineCount() * component.getLineHeight() +
  2884. getElementHeight(item2) +
  2885. getElementHeight(item3) +
  2886. getElementHeight(item4) +
  2887. getElementHeight(item5) +
  2888. getElementHeight(item6)
  2889. );
  2890. assertTilesAreSizedAndPositionedCorrectly(component, [
  2891. {
  2892. tileStartRow: 0,
  2893. height:
  2894. 3 * component.getLineHeight() +
  2895. getElementHeight(item2) +
  2896. getElementHeight(item3)
  2897. },
  2898. { tileStartRow: 3, height: 3 * component.getLineHeight() }
  2899. ]);
  2900. assertLinesAreAlignedWithLineNumbers(component);
  2901. expect(queryOnScreenLineElements(element).length).toBe(9);
  2902. expect(element.contains(item1)).toBe(false);
  2903. expect(item2.previousSibling).toBe(lineNodeForScreenRow(component, 0));
  2904. expect(item2.nextSibling).toBe(lineNodeForScreenRow(component, 1));
  2905. expect(item3.previousSibling).toBeNull();
  2906. expect(item3.nextSibling).toBe(lineNodeForScreenRow(component, 0));
  2907. expect(item4.nextSibling).toBe(lineNodeForScreenRow(component, 7));
  2908. expect(item5.previousSibling).toBe(lineNodeForScreenRow(component, 7));
  2909. expect(element.contains(item6)).toBe(false);
  2910. // invalidate decorations. this also tests a case where two decorations in
  2911. // the same tile change their height without affecting the tile height nor
  2912. // the content height.
  2913. item3.style.height = '22px';
  2914. item3.style.margin = '10px';
  2915. item2.style.height = '33px';
  2916. item2.style.margin = '0px';
  2917. await component.getNextUpdatePromise();
  2918. expect(component.getRenderedStartRow()).toBe(0);
  2919. expect(component.getRenderedEndRow()).toBe(9);
  2920. expect(component.getScrollHeight()).toBeNear(
  2921. editor.getScreenLineCount() * component.getLineHeight() +
  2922. getElementHeight(item2) +
  2923. getElementHeight(item3) +
  2924. getElementHeight(item4) +
  2925. getElementHeight(item5) +
  2926. getElementHeight(item6)
  2927. );
  2928. assertTilesAreSizedAndPositionedCorrectly(component, [
  2929. {
  2930. tileStartRow: 0,
  2931. height:
  2932. 3 * component.getLineHeight() +
  2933. getElementHeight(item2) +
  2934. getElementHeight(item3)
  2935. },
  2936. { tileStartRow: 3, height: 3 * component.getLineHeight() }
  2937. ]);
  2938. assertLinesAreAlignedWithLineNumbers(component);
  2939. expect(queryOnScreenLineElements(element).length).toBe(9);
  2940. expect(element.contains(item1)).toBe(false);
  2941. expect(item2.previousSibling).toBe(lineNodeForScreenRow(component, 0));
  2942. expect(item2.nextSibling).toBe(lineNodeForScreenRow(component, 1));
  2943. expect(item3.previousSibling).toBeNull();
  2944. expect(item3.nextSibling).toBe(lineNodeForScreenRow(component, 0));
  2945. expect(item4.nextSibling).toBe(lineNodeForScreenRow(component, 7));
  2946. expect(item5.previousSibling).toBe(lineNodeForScreenRow(component, 7));
  2947. expect(element.contains(item6)).toBe(false);
  2948. // make decoration before row 0 as wide as the editor, and insert some text into it so that it wraps.
  2949. item3.style.height = '';
  2950. item3.style.margin = '';
  2951. item3.style.width = '';
  2952. item3.style.wordWrap = 'break-word';
  2953. const contentWidthInCharacters = Math.floor(
  2954. component.getScrollContainerClientWidth() /
  2955. component.getBaseCharacterWidth()
  2956. );
  2957. item3.textContent = 'x'.repeat(contentWidthInCharacters * 2);
  2958. await component.getNextUpdatePromise();
  2959. // make the editor wider, so that the decoration doesn't wrap anymore.
  2960. component.element.style.width =
  2961. component.getGutterContainerWidth() +
  2962. component.getScrollContainerClientWidth() * 2 +
  2963. verticalScrollbarWidth +
  2964. 'px';
  2965. await component.getNextUpdatePromise();
  2966. expect(component.getRenderedStartRow()).toBe(0);
  2967. expect(component.getRenderedEndRow()).toBe(9);
  2968. expect(component.getScrollHeight()).toBeNear(
  2969. editor.getScreenLineCount() * component.getLineHeight() +
  2970. getElementHeight(item2) +
  2971. getElementHeight(item3) +
  2972. getElementHeight(item4) +
  2973. getElementHeight(item5) +
  2974. getElementHeight(item6)
  2975. );
  2976. assertTilesAreSizedAndPositionedCorrectly(component, [
  2977. {
  2978. tileStartRow: 0,
  2979. height:
  2980. 3 * component.getLineHeight() +
  2981. getElementHeight(item2) +
  2982. getElementHeight(item3)
  2983. },
  2984. { tileStartRow: 3, height: 3 * component.getLineHeight() }
  2985. ]);
  2986. assertLinesAreAlignedWithLineNumbers(component);
  2987. expect(queryOnScreenLineElements(element).length).toBe(9);
  2988. expect(element.contains(item1)).toBe(false);
  2989. expect(item2.previousSibling).toBe(lineNodeForScreenRow(component, 0));
  2990. expect(item2.nextSibling).toBe(lineNodeForScreenRow(component, 1));
  2991. expect(item3.previousSibling).toBeNull();
  2992. expect(item3.nextSibling).toBe(lineNodeForScreenRow(component, 0));
  2993. expect(item3.nextSibling).toBe(lineNodeForScreenRow(component, 0));
  2994. expect(item4.nextSibling).toBe(lineNodeForScreenRow(component, 7));
  2995. expect(element.contains(item6)).toBe(false);
  2996. // make the editor taller and wider and the same time, ensuring the number
  2997. // of rendered lines is correct.
  2998. setEditorHeightInLines(component, 13);
  2999. setEditorWidthInCharacters(component, 50);
  3000. await conditionPromise(
  3001. () =>
  3002. component.getRenderedStartRow() === 0 &&
  3003. component.getRenderedEndRow() === 13
  3004. );
  3005. expect(component.getScrollHeight()).toBeNear(
  3006. editor.getScreenLineCount() * component.getLineHeight() +
  3007. getElementHeight(item2) +
  3008. getElementHeight(item3) +
  3009. getElementHeight(item4) +
  3010. getElementHeight(item5) +
  3011. getElementHeight(item6)
  3012. );
  3013. assertTilesAreSizedAndPositionedCorrectly(component, [
  3014. {
  3015. tileStartRow: 0,
  3016. height:
  3017. 3 * component.getLineHeight() +
  3018. getElementHeight(item2) +
  3019. getElementHeight(item3)
  3020. },
  3021. { tileStartRow: 3, height: 3 * component.getLineHeight() },
  3022. {
  3023. tileStartRow: 6,
  3024. height:
  3025. 3 * component.getLineHeight() +
  3026. getElementHeight(item4) +
  3027. getElementHeight(item5)
  3028. }
  3029. ]);
  3030. assertLinesAreAlignedWithLineNumbers(component);
  3031. expect(queryOnScreenLineElements(element).length).toBe(13);
  3032. expect(element.contains(item1)).toBe(false);
  3033. expect(item2.previousSibling).toBe(lineNodeForScreenRow(component, 0));
  3034. expect(item2.nextSibling).toBe(lineNodeForScreenRow(component, 1));
  3035. expect(item3.previousSibling).toBeNull();
  3036. expect(item3.nextSibling).toBe(lineNodeForScreenRow(component, 0));
  3037. expect(item4.previousSibling).toBe(lineNodeForScreenRow(component, 6));
  3038. expect(item4.nextSibling).toBe(lineNodeForScreenRow(component, 7));
  3039. expect(item5.previousSibling).toBe(lineNodeForScreenRow(component, 7));
  3040. expect(item5.nextSibling).toBe(lineNodeForScreenRow(component, 8));
  3041. expect(item6.previousSibling).toBe(lineNodeForScreenRow(component, 12));
  3042. });
  3043. it('correctly positions line numbers when block decorations are located at tile boundaries', async () => {
  3044. const { editor, component } = buildComponent({ rowsPerTile: 3 });
  3045. createBlockDecorationAtScreenRow(editor, 0, {
  3046. height: 5,
  3047. position: 'before'
  3048. });
  3049. createBlockDecorationAtScreenRow(editor, 2, {
  3050. height: 7,
  3051. position: 'after'
  3052. });
  3053. createBlockDecorationAtScreenRow(editor, 3, {
  3054. height: 9,
  3055. position: 'before'
  3056. });
  3057. createBlockDecorationAtScreenRow(editor, 3, {
  3058. height: 11,
  3059. position: 'after'
  3060. });
  3061. createBlockDecorationAtScreenRow(editor, 5, {
  3062. height: 13,
  3063. position: 'after'
  3064. });
  3065. await component.getNextUpdatePromise();
  3066. assertLinesAreAlignedWithLineNumbers(component);
  3067. assertTilesAreSizedAndPositionedCorrectly(component, [
  3068. { tileStartRow: 0, height: 3 * component.getLineHeight() + 5 + 7 },
  3069. {
  3070. tileStartRow: 3,
  3071. height: 3 * component.getLineHeight() + 9 + 11 + 13
  3072. },
  3073. { tileStartRow: 6, height: 3 * component.getLineHeight() }
  3074. ]);
  3075. });
  3076. it('removes block decorations whose markers have been destroyed', async () => {
  3077. const { editor, component } = buildComponent({ rowsPerTile: 3 });
  3078. const { marker } = createBlockDecorationAtScreenRow(editor, 2, {
  3079. height: 5,
  3080. position: 'before'
  3081. });
  3082. await component.getNextUpdatePromise();
  3083. assertLinesAreAlignedWithLineNumbers(component);
  3084. assertTilesAreSizedAndPositionedCorrectly(component, [
  3085. { tileStartRow: 0, height: 3 * component.getLineHeight() + 5 },
  3086. { tileStartRow: 3, height: 3 * component.getLineHeight() },
  3087. { tileStartRow: 6, height: 3 * component.getLineHeight() }
  3088. ]);
  3089. marker.destroy();
  3090. await component.getNextUpdatePromise();
  3091. assertLinesAreAlignedWithLineNumbers(component);
  3092. assertTilesAreSizedAndPositionedCorrectly(component, [
  3093. { tileStartRow: 0, height: 3 * component.getLineHeight() },
  3094. { tileStartRow: 3, height: 3 * component.getLineHeight() },
  3095. { tileStartRow: 6, height: 3 * component.getLineHeight() }
  3096. ]);
  3097. });
  3098. it('removes block decorations whose markers are invalidated, and adds them back when they become valid again', async () => {
  3099. const editor = buildEditor({ rowsPerTile: 3, autoHeight: false });
  3100. const { item, decoration, marker } = createBlockDecorationAtScreenRow(
  3101. editor,
  3102. 3,
  3103. { height: 44, position: 'before', invalidate: 'touch' }
  3104. );
  3105. const { component } = buildComponent({ editor, rowsPerTile: 3 });
  3106. // Invalidating the marker removes the block decoration.
  3107. editor.getBuffer().deleteRows(2, 3);
  3108. await component.getNextUpdatePromise();
  3109. expect(item.parentElement).toBeNull();
  3110. assertLinesAreAlignedWithLineNumbers(component);
  3111. assertTilesAreSizedAndPositionedCorrectly(component, [
  3112. { tileStartRow: 0, height: 3 * component.getLineHeight() },
  3113. { tileStartRow: 3, height: 3 * component.getLineHeight() },
  3114. { tileStartRow: 6, height: 3 * component.getLineHeight() }
  3115. ]);
  3116. // Moving invalid markers is ignored.
  3117. marker.setScreenRange([[2, 0], [2, 0]]);
  3118. await component.getNextUpdatePromise();
  3119. expect(item.parentElement).toBeNull();
  3120. assertLinesAreAlignedWithLineNumbers(component);
  3121. assertTilesAreSizedAndPositionedCorrectly(component, [
  3122. { tileStartRow: 0, height: 3 * component.getLineHeight() },
  3123. { tileStartRow: 3, height: 3 * component.getLineHeight() },
  3124. { tileStartRow: 6, height: 3 * component.getLineHeight() }
  3125. ]);
  3126. // Making the marker valid again adds back the block decoration.
  3127. marker.bufferMarker.valid = true;
  3128. marker.setScreenRange([[3, 0], [3, 0]]);
  3129. await component.getNextUpdatePromise();
  3130. expect(item.nextSibling).toBe(lineNodeForScreenRow(component, 3));
  3131. assertLinesAreAlignedWithLineNumbers(component);
  3132. assertTilesAreSizedAndPositionedCorrectly(component, [
  3133. { tileStartRow: 0, height: 3 * component.getLineHeight() },
  3134. { tileStartRow: 3, height: 3 * component.getLineHeight() + 44 },
  3135. { tileStartRow: 6, height: 3 * component.getLineHeight() }
  3136. ]);
  3137. // Destroying the decoration and invalidating the marker at the same time
  3138. // removes the block decoration correctly.
  3139. editor.getBuffer().deleteRows(2, 3);
  3140. decoration.destroy();
  3141. await component.getNextUpdatePromise();
  3142. expect(item.parentElement).toBeNull();
  3143. assertLinesAreAlignedWithLineNumbers(component);
  3144. assertTilesAreSizedAndPositionedCorrectly(component, [
  3145. { tileStartRow: 0, height: 3 * component.getLineHeight() },
  3146. { tileStartRow: 3, height: 3 * component.getLineHeight() },
  3147. { tileStartRow: 6, height: 3 * component.getLineHeight() }
  3148. ]);
  3149. });
  3150. it('does not render block decorations when decorating invalid markers', async () => {
  3151. const editor = buildEditor({ rowsPerTile: 3, autoHeight: false });
  3152. const { component } = buildComponent({ editor, rowsPerTile: 3 });
  3153. const marker = editor.markScreenPosition([3, 0], { invalidate: 'touch' });
  3154. const item = document.createElement('div');
  3155. item.style.height = 30 + 'px';
  3156. item.style.width = 30 + 'px';
  3157. editor.getBuffer().deleteRows(1, 4);
  3158. editor.decorateMarker(marker, {
  3159. type: 'block',
  3160. item,
  3161. position: 'before'
  3162. });
  3163. await component.getNextUpdatePromise();
  3164. expect(item.parentElement).toBeNull();
  3165. assertLinesAreAlignedWithLineNumbers(component);
  3166. assertTilesAreSizedAndPositionedCorrectly(component, [
  3167. { tileStartRow: 0, height: 3 * component.getLineHeight() },
  3168. { tileStartRow: 3, height: 3 * component.getLineHeight() },
  3169. { tileStartRow: 6, height: 3 * component.getLineHeight() }
  3170. ]);
  3171. // Making the marker valid again causes the corresponding block decoration
  3172. // to be added to the editor.
  3173. marker.bufferMarker.valid = true;
  3174. marker.setScreenRange([[2, 0], [2, 0]]);
  3175. await component.getNextUpdatePromise();
  3176. expect(item.nextSibling).toBe(lineNodeForScreenRow(component, 2));
  3177. assertLinesAreAlignedWithLineNumbers(component);
  3178. assertTilesAreSizedAndPositionedCorrectly(component, [
  3179. { tileStartRow: 0, height: 3 * component.getLineHeight() + 30 },
  3180. { tileStartRow: 3, height: 3 * component.getLineHeight() },
  3181. { tileStartRow: 6, height: 3 * component.getLineHeight() }
  3182. ]);
  3183. });
  3184. it('does not try to remeasure block decorations whose markers are invalid (regression)', async () => {
  3185. const editor = buildEditor({ rowsPerTile: 3, autoHeight: false });
  3186. const { component } = buildComponent({ editor, rowsPerTile: 3 });
  3187. createBlockDecorationAtScreenRow(editor, 2, {
  3188. height: '12px',
  3189. invalidate: 'touch'
  3190. });
  3191. editor.getBuffer().deleteRows(0, 3);
  3192. await component.getNextUpdatePromise();
  3193. // Trigger a re-measurement of all block decorations.
  3194. await setEditorWidthInCharacters(component, 20);
  3195. assertLinesAreAlignedWithLineNumbers(component);
  3196. assertTilesAreSizedAndPositionedCorrectly(component, [
  3197. { tileStartRow: 0, height: 3 * component.getLineHeight() },
  3198. { tileStartRow: 3, height: 3 * component.getLineHeight() },
  3199. { tileStartRow: 6, height: 3 * component.getLineHeight() }
  3200. ]);
  3201. });
  3202. it('does not throw exceptions when destroying a block decoration inside a marker change event (regression)', async () => {
  3203. const { editor, component } = buildComponent({ rowsPerTile: 3 });
  3204. const marker = editor.markScreenPosition([2, 0]);
  3205. marker.onDidChange(() => {
  3206. marker.destroy();
  3207. });
  3208. const item = document.createElement('div');
  3209. editor.decorateMarker(marker, { type: 'block', item });
  3210. await component.getNextUpdatePromise();
  3211. expect(item.nextSibling).toBe(lineNodeForScreenRow(component, 2));
  3212. marker.setBufferRange([[0, 0], [0, 0]]);
  3213. expect(marker.isDestroyed()).toBe(true);
  3214. await component.getNextUpdatePromise();
  3215. expect(item.parentElement).toBeNull();
  3216. });
  3217. it('does not attempt to render block decorations located outside the visible range', async () => {
  3218. const { editor, component } = buildComponent({
  3219. autoHeight: false,
  3220. rowsPerTile: 2
  3221. });
  3222. await setEditorHeightInLines(component, 2);
  3223. expect(component.getRenderedStartRow()).toBe(0);
  3224. expect(component.getRenderedEndRow()).toBe(4);
  3225. const marker1 = editor.markScreenRange([[3, 0], [5, 0]], {
  3226. reversed: false
  3227. });
  3228. const item1 = document.createElement('div');
  3229. editor.decorateMarker(marker1, { type: 'block', item: item1 });
  3230. const marker2 = editor.markScreenRange([[3, 0], [5, 0]], {
  3231. reversed: true
  3232. });
  3233. const item2 = document.createElement('div');
  3234. editor.decorateMarker(marker2, { type: 'block', item: item2 });
  3235. await component.getNextUpdatePromise();
  3236. expect(item1.parentElement).toBeNull();
  3237. expect(item2.nextSibling).toBe(lineNodeForScreenRow(component, 3));
  3238. await setScrollTop(component, 4 * component.getLineHeight());
  3239. expect(component.getRenderedStartRow()).toBe(4);
  3240. expect(component.getRenderedEndRow()).toBe(8);
  3241. expect(item1.nextSibling).toBe(lineNodeForScreenRow(component, 5));
  3242. expect(item2.parentElement).toBeNull();
  3243. });
  3244. it('measures block decorations correctly when they are added before the component width has been updated', async () => {
  3245. {
  3246. const { editor, component, element } = buildComponent({
  3247. autoHeight: false,
  3248. width: 500,
  3249. attach: false
  3250. });
  3251. const marker = editor.markScreenPosition([0, 0]);
  3252. const item = document.createElement('div');
  3253. item.textContent = 'block decoration';
  3254. editor.decorateMarker(marker, {
  3255. type: 'block',
  3256. item
  3257. });
  3258. jasmine.attachToDOM(element);
  3259. assertLinesAreAlignedWithLineNumbers(component);
  3260. }
  3261. {
  3262. const { editor, component, element } = buildComponent({
  3263. autoHeight: false,
  3264. width: 800
  3265. });
  3266. const marker = editor.markScreenPosition([0, 0]);
  3267. const item = document.createElement('div');
  3268. item.textContent = 'block decoration that could wrap many times';
  3269. editor.decorateMarker(marker, {
  3270. type: 'block',
  3271. item
  3272. });
  3273. element.style.width = '50px';
  3274. await component.getNextUpdatePromise();
  3275. assertLinesAreAlignedWithLineNumbers(component);
  3276. }
  3277. });
  3278. it('bases the width of the block decoration measurement area on the editor scroll width', async () => {
  3279. const { component, element } = buildComponent({
  3280. autoHeight: false,
  3281. width: 150
  3282. });
  3283. expect(component.refs.blockDecorationMeasurementArea.offsetWidth).toBe(
  3284. component.getScrollWidth()
  3285. );
  3286. element.style.width = '800px';
  3287. await component.getNextUpdatePromise();
  3288. expect(component.refs.blockDecorationMeasurementArea.offsetWidth).toBe(
  3289. component.getScrollWidth()
  3290. );
  3291. });
  3292. it('does not change the cursor position when clicking on a block decoration', async () => {
  3293. const { editor, component } = buildComponent();
  3294. const decorationElement = document.createElement('div');
  3295. decorationElement.textContent = 'Parent';
  3296. const childElement = document.createElement('div');
  3297. childElement.textContent = 'Child';
  3298. decorationElement.appendChild(childElement);
  3299. const marker = editor.markScreenPosition([4, 0]);
  3300. editor.decorateMarker(marker, { type: 'block', item: decorationElement });
  3301. await component.getNextUpdatePromise();
  3302. const decorationElementClientRect = decorationElement.getBoundingClientRect();
  3303. component.didMouseDownOnContent({
  3304. target: decorationElement,
  3305. detail: 1,
  3306. button: 0,
  3307. clientX: decorationElementClientRect.left,
  3308. clientY: decorationElementClientRect.top
  3309. });
  3310. expect(editor.getCursorScreenPosition()).toEqual([0, 0]);
  3311. const childElementClientRect = childElement.getBoundingClientRect();
  3312. component.didMouseDownOnContent({
  3313. target: childElement,
  3314. detail: 1,
  3315. button: 0,
  3316. clientX: childElementClientRect.left,
  3317. clientY: childElementClientRect.top
  3318. });
  3319. expect(editor.getCursorScreenPosition()).toEqual([0, 0]);
  3320. });
  3321. it('uses the order property to control the order of block decorations at the same screen row', async () => {
  3322. const editor = buildEditor({ autoHeight: false });
  3323. const { component, element } = buildComponent({ editor });
  3324. element.style.height =
  3325. 10 * component.getLineHeight() + horizontalScrollbarHeight + 'px';
  3326. await component.getNextUpdatePromise();
  3327. // Order parameters that differ from creation order; that collide; and that are not provided.
  3328. const [beforeItems, beforeDecorations] = [
  3329. 30,
  3330. 20,
  3331. undefined,
  3332. 20,
  3333. 10,
  3334. undefined
  3335. ]
  3336. .map(order => {
  3337. return createBlockDecorationAtScreenRow(editor, 2, {
  3338. height: 10,
  3339. position: 'before',
  3340. order
  3341. });
  3342. })
  3343. .reduce(
  3344. (lists, result) => {
  3345. lists[0].push(result.item);
  3346. lists[1].push(result.decoration);
  3347. return lists;
  3348. },
  3349. [[], []]
  3350. );
  3351. const [afterItems] = [undefined, 1, 6, undefined, 6, 2]
  3352. .map(order => {
  3353. return createBlockDecorationAtScreenRow(editor, 2, {
  3354. height: 10,
  3355. position: 'after',
  3356. order
  3357. });
  3358. })
  3359. .reduce(
  3360. (lists, result) => {
  3361. lists[0].push(result.item);
  3362. lists[1].push(result.decoration);
  3363. return lists;
  3364. },
  3365. [[], []]
  3366. );
  3367. await component.getNextUpdatePromise();
  3368. expect(beforeItems[4].previousSibling).toBe(
  3369. lineNodeForScreenRow(component, 1)
  3370. );
  3371. expect(beforeItems[4].nextSibling).toBe(beforeItems[1]);
  3372. expect(beforeItems[1].nextSibling).toBe(beforeItems[3]);
  3373. expect(beforeItems[3].nextSibling).toBe(beforeItems[0]);
  3374. expect(beforeItems[0].nextSibling).toBe(beforeItems[2]);
  3375. expect(beforeItems[2].nextSibling).toBe(beforeItems[5]);
  3376. expect(beforeItems[5].nextSibling).toBe(
  3377. lineNodeForScreenRow(component, 2)
  3378. );
  3379. expect(afterItems[1].previousSibling).toBe(
  3380. lineNodeForScreenRow(component, 2)
  3381. );
  3382. expect(afterItems[1].nextSibling).toBe(afterItems[5]);
  3383. expect(afterItems[5].nextSibling).toBe(afterItems[2]);
  3384. expect(afterItems[2].nextSibling).toBe(afterItems[4]);
  3385. expect(afterItems[4].nextSibling).toBe(afterItems[0]);
  3386. expect(afterItems[0].nextSibling).toBe(afterItems[3]);
  3387. // Create a decoration somewhere else and move it to the same screen row as the existing decorations
  3388. const { item: later, decoration } = createBlockDecorationAtScreenRow(
  3389. editor,
  3390. 4,
  3391. { height: 20, position: 'after', order: 3 }
  3392. );
  3393. await component.getNextUpdatePromise();
  3394. expect(later.previousSibling).toBe(lineNodeForScreenRow(component, 4));
  3395. expect(later.nextSibling).toBe(lineNodeForScreenRow(component, 5));
  3396. decoration.getMarker().setHeadScreenPosition([2, 0]);
  3397. await component.getNextUpdatePromise();
  3398. expect(later.previousSibling).toBe(afterItems[5]);
  3399. expect(later.nextSibling).toBe(afterItems[2]);
  3400. // Move a decoration away from its screen row and ensure the rest maintain their order
  3401. beforeDecorations[3].getMarker().setHeadScreenPosition([5, 0]);
  3402. await component.getNextUpdatePromise();
  3403. expect(beforeItems[3].previousSibling).toBe(
  3404. lineNodeForScreenRow(component, 4)
  3405. );
  3406. expect(beforeItems[3].nextSibling).toBe(
  3407. lineNodeForScreenRow(component, 5)
  3408. );
  3409. expect(beforeItems[4].previousSibling).toBe(
  3410. lineNodeForScreenRow(component, 1)
  3411. );
  3412. expect(beforeItems[4].nextSibling).toBe(beforeItems[1]);
  3413. expect(beforeItems[1].nextSibling).toBe(beforeItems[0]);
  3414. expect(beforeItems[0].nextSibling).toBe(beforeItems[2]);
  3415. expect(beforeItems[2].nextSibling).toBe(beforeItems[5]);
  3416. expect(beforeItems[5].nextSibling).toBe(
  3417. lineNodeForScreenRow(component, 2)
  3418. );
  3419. });
  3420. function createBlockDecorationAtScreenRow(
  3421. editor,
  3422. screenRow,
  3423. { height, margin, marginTop, marginBottom, position, order, invalidate }
  3424. ) {
  3425. const marker = editor.markScreenPosition([screenRow, 0], {
  3426. invalidate: invalidate || 'never'
  3427. });
  3428. const item = document.createElement('div');
  3429. item.style.height = height + 'px';
  3430. if (margin != null) item.style.margin = margin + 'px';
  3431. if (marginTop != null) item.style.marginTop = marginTop + 'px';
  3432. if (marginBottom != null) item.style.marginBottom = marginBottom + 'px';
  3433. item.style.width = 30 + 'px';
  3434. const decoration = editor.decorateMarker(marker, {
  3435. type: 'block',
  3436. item,
  3437. position,
  3438. order
  3439. });
  3440. return { item, decoration, marker };
  3441. }
  3442. function assertTilesAreSizedAndPositionedCorrectly(component, tiles) {
  3443. let top = 0;
  3444. for (let tile of tiles) {
  3445. const linesTileElement = lineNodeForScreenRow(
  3446. component,
  3447. tile.tileStartRow
  3448. ).parentElement;
  3449. const linesTileBoundingRect = linesTileElement.getBoundingClientRect();
  3450. expect(linesTileBoundingRect.height).toBeNear(tile.height);
  3451. expect(linesTileBoundingRect.top).toBeNear(top);
  3452. const lineNumbersTileElement = lineNumberNodeForScreenRow(
  3453. component,
  3454. tile.tileStartRow
  3455. ).parentElement;
  3456. const lineNumbersTileBoundingRect = lineNumbersTileElement.getBoundingClientRect();
  3457. expect(lineNumbersTileBoundingRect.height).toBeNear(tile.height);
  3458. expect(lineNumbersTileBoundingRect.top).toBeNear(top);
  3459. top += tile.height;
  3460. }
  3461. }
  3462. function assertLinesAreAlignedWithLineNumbers(component) {
  3463. const startRow = component.getRenderedStartRow();
  3464. const endRow = component.getRenderedEndRow();
  3465. for (let row = startRow; row < endRow; row++) {
  3466. const lineNode = lineNodeForScreenRow(component, row);
  3467. const lineNumberNode = lineNumberNodeForScreenRow(component, row);
  3468. expect(lineNumberNode.getBoundingClientRect().top).toBeNear(
  3469. lineNode.getBoundingClientRect().top
  3470. );
  3471. }
  3472. }
  3473. });
  3474. describe('cursor decorations', () => {
  3475. it('allows default cursors to be customized', async () => {
  3476. const { component, element, editor } = buildComponent();
  3477. editor.addCursorAtScreenPosition([1, 0]);
  3478. const [cursorMarker1, cursorMarker2] = editor
  3479. .getCursors()
  3480. .map(c => c.getMarker());
  3481. editor.decorateMarker(cursorMarker1, { type: 'cursor', class: 'a' });
  3482. editor.decorateMarker(cursorMarker2, {
  3483. type: 'cursor',
  3484. class: 'b',
  3485. style: { visibility: 'hidden' }
  3486. });
  3487. editor.decorateMarker(cursorMarker2, {
  3488. type: 'cursor',
  3489. style: { backgroundColor: 'red' }
  3490. });
  3491. await component.getNextUpdatePromise();
  3492. const cursorNodes = element.querySelectorAll('.cursor');
  3493. expect(cursorNodes.length).toBe(2);
  3494. expect(cursorNodes[0].className).toBe('cursor a');
  3495. expect(cursorNodes[1].className).toBe('cursor b');
  3496. expect(cursorNodes[1].style.visibility).toBe('hidden');
  3497. expect(cursorNodes[1].style.backgroundColor).toBe('red');
  3498. });
  3499. it('allows markers that are not actually associated with cursors to be decorated as if they were cursors', async () => {
  3500. const { component, element, editor } = buildComponent();
  3501. const marker = editor.markScreenPosition([1, 0]);
  3502. editor.decorateMarker(marker, { type: 'cursor', class: 'a' });
  3503. await component.getNextUpdatePromise();
  3504. const cursorNodes = element.querySelectorAll('.cursor');
  3505. expect(cursorNodes.length).toBe(2);
  3506. expect(cursorNodes[0].className).toBe('cursor');
  3507. expect(cursorNodes[1].className).toBe('cursor a');
  3508. });
  3509. });
  3510. describe('text decorations', () => {
  3511. it('injects spans with custom class names and inline styles based on text decorations', async () => {
  3512. const { component, element, editor } = buildComponent({ rowsPerTile: 2 });
  3513. const markerLayer = editor.addMarkerLayer();
  3514. const marker1 = markerLayer.markBufferRange([[0, 2], [2, 7]]);
  3515. const marker2 = markerLayer.markBufferRange([[0, 2], [3, 8]]);
  3516. const marker3 = markerLayer.markBufferRange([[1, 13], [2, 7]]);
  3517. editor.decorateMarker(marker1, {
  3518. type: 'text',
  3519. class: 'a',
  3520. style: { color: 'red' }
  3521. });
  3522. editor.decorateMarker(marker2, {
  3523. type: 'text',
  3524. class: 'b',
  3525. style: { color: 'blue' }
  3526. });
  3527. editor.decorateMarker(marker3, {
  3528. type: 'text',
  3529. class: 'c',
  3530. style: { color: 'green' }
  3531. });
  3532. await component.getNextUpdatePromise();
  3533. expect(textContentOnRowMatchingSelector(component, 0, '.a')).toBe(
  3534. editor.lineTextForScreenRow(0).slice(2)
  3535. );
  3536. expect(textContentOnRowMatchingSelector(component, 1, '.a')).toBe(
  3537. editor.lineTextForScreenRow(1)
  3538. );
  3539. expect(textContentOnRowMatchingSelector(component, 2, '.a')).toBe(
  3540. editor.lineTextForScreenRow(2).slice(0, 7)
  3541. );
  3542. expect(textContentOnRowMatchingSelector(component, 3, '.a')).toBe('');
  3543. expect(textContentOnRowMatchingSelector(component, 0, '.b')).toBe(
  3544. editor.lineTextForScreenRow(0).slice(2)
  3545. );
  3546. expect(textContentOnRowMatchingSelector(component, 1, '.b')).toBe(
  3547. editor.lineTextForScreenRow(1)
  3548. );
  3549. expect(textContentOnRowMatchingSelector(component, 2, '.b')).toBe(
  3550. editor.lineTextForScreenRow(2)
  3551. );
  3552. expect(textContentOnRowMatchingSelector(component, 3, '.b')).toBe(
  3553. editor.lineTextForScreenRow(3).slice(0, 8)
  3554. );
  3555. expect(textContentOnRowMatchingSelector(component, 0, '.c')).toBe('');
  3556. expect(textContentOnRowMatchingSelector(component, 1, '.c')).toBe(
  3557. editor.lineTextForScreenRow(1).slice(13)
  3558. );
  3559. expect(textContentOnRowMatchingSelector(component, 2, '.c')).toBe(
  3560. editor.lineTextForScreenRow(2).slice(0, 7)
  3561. );
  3562. expect(textContentOnRowMatchingSelector(component, 3, '.c')).toBe('');
  3563. for (const span of element.querySelectorAll('.a:not(.c)')) {
  3564. expect(span.style.color).toBe('red');
  3565. }
  3566. for (const span of element.querySelectorAll('.b:not(.c):not(.a)')) {
  3567. expect(span.style.color).toBe('blue');
  3568. }
  3569. for (const span of element.querySelectorAll('.c')) {
  3570. expect(span.style.color).toBe('green');
  3571. }
  3572. marker2.setHeadScreenPosition([3, 10]);
  3573. await component.getNextUpdatePromise();
  3574. expect(textContentOnRowMatchingSelector(component, 3, '.b')).toBe(
  3575. editor.lineTextForScreenRow(3).slice(0, 10)
  3576. );
  3577. });
  3578. it('correctly handles text decorations starting before the first rendered row and/or ending after the last rendered row', async () => {
  3579. const { component, element, editor } = buildComponent({
  3580. autoHeight: false,
  3581. rowsPerTile: 1
  3582. });
  3583. element.style.height = 4 * component.getLineHeight() + 'px';
  3584. await component.getNextUpdatePromise();
  3585. await setScrollTop(component, 4 * component.getLineHeight());
  3586. expect(component.getRenderedStartRow()).toBeNear(4);
  3587. expect(component.getRenderedEndRow()).toBeNear(9);
  3588. const markerLayer = editor.addMarkerLayer();
  3589. const marker1 = markerLayer.markBufferRange([[0, 0], [4, 5]]);
  3590. const marker2 = markerLayer.markBufferRange([[7, 2], [10, 8]]);
  3591. editor.decorateMarker(marker1, { type: 'text', class: 'a' });
  3592. editor.decorateMarker(marker2, { type: 'text', class: 'b' });
  3593. await component.getNextUpdatePromise();
  3594. expect(textContentOnRowMatchingSelector(component, 4, '.a')).toBe(
  3595. editor.lineTextForScreenRow(4).slice(0, 5)
  3596. );
  3597. expect(textContentOnRowMatchingSelector(component, 5, '.a')).toBe('');
  3598. expect(textContentOnRowMatchingSelector(component, 6, '.a')).toBe('');
  3599. expect(textContentOnRowMatchingSelector(component, 7, '.a')).toBe('');
  3600. expect(textContentOnRowMatchingSelector(component, 8, '.a')).toBe('');
  3601. expect(textContentOnRowMatchingSelector(component, 4, '.b')).toBe('');
  3602. expect(textContentOnRowMatchingSelector(component, 5, '.b')).toBe('');
  3603. expect(textContentOnRowMatchingSelector(component, 6, '.b')).toBe('');
  3604. expect(textContentOnRowMatchingSelector(component, 7, '.b')).toBe(
  3605. editor.lineTextForScreenRow(7).slice(2)
  3606. );
  3607. expect(textContentOnRowMatchingSelector(component, 8, '.b')).toBe(
  3608. editor.lineTextForScreenRow(8)
  3609. );
  3610. });
  3611. it('does not create empty spans when a text decoration contains a row but another text decoration starts or ends at the beginning of it', async () => {
  3612. const { component, element, editor } = buildComponent();
  3613. const markerLayer = editor.addMarkerLayer();
  3614. const marker1 = markerLayer.markBufferRange([[0, 2], [4, 0]]);
  3615. const marker2 = markerLayer.markBufferRange([[2, 0], [5, 8]]);
  3616. editor.decorateMarker(marker1, { type: 'text', class: 'a' });
  3617. editor.decorateMarker(marker2, { type: 'text', class: 'b' });
  3618. await component.getNextUpdatePromise();
  3619. for (const decorationSpan of element.querySelectorAll('.a, .b')) {
  3620. expect(decorationSpan.textContent).not.toBe('');
  3621. }
  3622. });
  3623. it('does not create empty text nodes when a text decoration ends right after a text tag', async () => {
  3624. const { component, editor } = buildComponent();
  3625. const marker = editor.markBufferRange([[0, 8], [0, 29]]);
  3626. editor.decorateMarker(marker, { type: 'text', class: 'a' });
  3627. await component.getNextUpdatePromise();
  3628. for (const textNode of textNodesForScreenRow(component, 0)) {
  3629. expect(textNode.textContent).not.toBe('');
  3630. }
  3631. });
  3632. function textContentOnRowMatchingSelector(component, row, selector) {
  3633. return Array.from(
  3634. lineNodeForScreenRow(component, row).querySelectorAll(selector)
  3635. )
  3636. .map(span => span.textContent)
  3637. .join('');
  3638. }
  3639. });
  3640. describe('mouse input', () => {
  3641. describe('on the lines', () => {
  3642. describe('when there is only one cursor', () => {
  3643. it('positions the cursor on single-click or when middle-clicking', async () => {
  3644. atom.config.set('editor.selectionClipboard', false);
  3645. for (const button of [0, 1]) {
  3646. const { component, editor } = buildComponent();
  3647. const { lineHeight } = component.measurements;
  3648. editor.setCursorScreenPosition([Infinity, Infinity], {
  3649. autoscroll: false
  3650. });
  3651. component.didMouseDownOnContent({
  3652. detail: 1,
  3653. button,
  3654. clientX: clientLeftForCharacter(component, 0, 0) - 1,
  3655. clientY: clientTopForLine(component, 0) - 1
  3656. });
  3657. expect(editor.getCursorScreenPosition()).toEqual([0, 0]);
  3658. const maxRow = editor.getLastScreenRow();
  3659. editor.setCursorScreenPosition([Infinity, Infinity], {
  3660. autoscroll: false
  3661. });
  3662. component.didMouseDownOnContent({
  3663. detail: 1,
  3664. button,
  3665. clientX:
  3666. clientLeftForCharacter(
  3667. component,
  3668. maxRow,
  3669. editor.lineLengthForScreenRow(maxRow)
  3670. ) + 1,
  3671. clientY: clientTopForLine(component, maxRow) + 1
  3672. });
  3673. expect(editor.getCursorScreenPosition()).toEqual([
  3674. maxRow,
  3675. editor.lineLengthForScreenRow(maxRow)
  3676. ]);
  3677. component.didMouseDownOnContent({
  3678. detail: 1,
  3679. button,
  3680. clientX:
  3681. clientLeftForCharacter(
  3682. component,
  3683. 0,
  3684. editor.lineLengthForScreenRow(0)
  3685. ) + 1,
  3686. clientY: clientTopForLine(component, 0) + lineHeight / 2
  3687. });
  3688. expect(editor.getCursorScreenPosition()).toEqual([
  3689. 0,
  3690. editor.lineLengthForScreenRow(0)
  3691. ]);
  3692. component.didMouseDownOnContent({
  3693. detail: 1,
  3694. button,
  3695. clientX:
  3696. (clientLeftForCharacter(component, 3, 0) +
  3697. clientLeftForCharacter(component, 3, 1)) /
  3698. 2,
  3699. clientY: clientTopForLine(component, 1) + lineHeight / 2
  3700. });
  3701. expect(editor.getCursorScreenPosition()).toEqual([1, 0]);
  3702. component.didMouseDownOnContent({
  3703. detail: 1,
  3704. button,
  3705. clientX:
  3706. (clientLeftForCharacter(component, 3, 14) +
  3707. clientLeftForCharacter(component, 3, 15)) /
  3708. 2,
  3709. clientY: clientTopForLine(component, 3) + lineHeight / 2
  3710. });
  3711. expect(editor.getCursorScreenPosition()).toEqual([3, 14]);
  3712. component.didMouseDownOnContent({
  3713. detail: 1,
  3714. button,
  3715. clientX:
  3716. (clientLeftForCharacter(component, 3, 14) +
  3717. clientLeftForCharacter(component, 3, 15)) /
  3718. 2 +
  3719. 1,
  3720. clientY: clientTopForLine(component, 3) + lineHeight / 2
  3721. });
  3722. expect(editor.getCursorScreenPosition()).toEqual([3, 15]);
  3723. editor.getBuffer().setTextInRange([[3, 14], [3, 15]], '🐣');
  3724. await component.getNextUpdatePromise();
  3725. component.didMouseDownOnContent({
  3726. detail: 1,
  3727. button,
  3728. clientX:
  3729. (clientLeftForCharacter(component, 3, 14) +
  3730. clientLeftForCharacter(component, 3, 16)) /
  3731. 2,
  3732. clientY: clientTopForLine(component, 3) + lineHeight / 2
  3733. });
  3734. expect(editor.getCursorScreenPosition()).toEqual([3, 14]);
  3735. component.didMouseDownOnContent({
  3736. detail: 1,
  3737. button,
  3738. clientX:
  3739. (clientLeftForCharacter(component, 3, 14) +
  3740. clientLeftForCharacter(component, 3, 16)) /
  3741. 2 +
  3742. 1,
  3743. clientY: clientTopForLine(component, 3) + lineHeight / 2
  3744. });
  3745. expect(editor.getCursorScreenPosition()).toEqual([3, 16]);
  3746. expect(editor.testAutoscrollRequests).toEqual([]);
  3747. }
  3748. });
  3749. });
  3750. describe('when the input is for the primary mouse button', () => {
  3751. it('selects words on double-click', () => {
  3752. const { component, editor } = buildComponent();
  3753. const { clientX, clientY } = clientPositionForCharacter(
  3754. component,
  3755. 1,
  3756. 16
  3757. );
  3758. component.didMouseDownOnContent({
  3759. detail: 1,
  3760. button: 0,
  3761. clientX,
  3762. clientY
  3763. });
  3764. component.didMouseDownOnContent({
  3765. detail: 2,
  3766. button: 0,
  3767. clientX,
  3768. clientY
  3769. });
  3770. expect(editor.getSelectedScreenRange()).toEqual([[1, 13], [1, 21]]);
  3771. expect(editor.testAutoscrollRequests).toEqual([]);
  3772. });
  3773. it('selects lines on triple-click', () => {
  3774. const { component, editor } = buildComponent();
  3775. const { clientX, clientY } = clientPositionForCharacter(
  3776. component,
  3777. 1,
  3778. 16
  3779. );
  3780. component.didMouseDownOnContent({
  3781. detail: 1,
  3782. button: 0,
  3783. clientX,
  3784. clientY
  3785. });
  3786. component.didMouseDownOnContent({
  3787. detail: 2,
  3788. button: 0,
  3789. clientX,
  3790. clientY
  3791. });
  3792. component.didMouseDownOnContent({
  3793. detail: 3,
  3794. button: 0,
  3795. clientX,
  3796. clientY
  3797. });
  3798. expect(editor.getSelectedScreenRange()).toEqual([[1, 0], [2, 0]]);
  3799. expect(editor.testAutoscrollRequests).toEqual([]);
  3800. });
  3801. it('adds or removes cursors when holding cmd or ctrl when single-clicking', () => {
  3802. atom.config.set('editor.multiCursorOnClick', true);
  3803. const { component, editor } = buildComponent({ platform: 'darwin' });
  3804. expect(editor.getCursorScreenPositions()).toEqual([[0, 0]]);
  3805. // add cursor at 1, 16
  3806. component.didMouseDownOnContent(
  3807. Object.assign(clientPositionForCharacter(component, 1, 16), {
  3808. detail: 1,
  3809. button: 0,
  3810. metaKey: true
  3811. })
  3812. );
  3813. expect(editor.getCursorScreenPositions()).toEqual([[0, 0], [1, 16]]);
  3814. // remove cursor at 0, 0
  3815. component.didMouseDownOnContent(
  3816. Object.assign(clientPositionForCharacter(component, 0, 0), {
  3817. detail: 1,
  3818. button: 0,
  3819. metaKey: true
  3820. })
  3821. );
  3822. expect(editor.getCursorScreenPositions()).toEqual([[1, 16]]);
  3823. // cmd-click cursor at 1, 16 but don't remove it because it's the last one
  3824. component.didMouseDownOnContent(
  3825. Object.assign(clientPositionForCharacter(component, 1, 16), {
  3826. detail: 1,
  3827. button: 0,
  3828. metaKey: true
  3829. })
  3830. );
  3831. expect(editor.getCursorScreenPositions()).toEqual([[1, 16]]);
  3832. // cmd-clicking within a selection destroys it
  3833. editor.addSelectionForScreenRange([[2, 10], [2, 15]], {
  3834. autoscroll: false
  3835. });
  3836. expect(editor.getSelectedScreenRanges()).toEqual([
  3837. [[1, 16], [1, 16]],
  3838. [[2, 10], [2, 15]]
  3839. ]);
  3840. component.didMouseDownOnContent(
  3841. Object.assign(clientPositionForCharacter(component, 2, 13), {
  3842. detail: 1,
  3843. button: 0,
  3844. metaKey: true
  3845. })
  3846. );
  3847. expect(editor.getSelectedScreenRanges()).toEqual([
  3848. [[1, 16], [1, 16]]
  3849. ]);
  3850. // ctrl-click does not add cursors on macOS, nor does it move the cursor
  3851. component.didMouseDownOnContent(
  3852. Object.assign(clientPositionForCharacter(component, 1, 4), {
  3853. detail: 1,
  3854. button: 0,
  3855. ctrlKey: true
  3856. })
  3857. );
  3858. expect(editor.getSelectedScreenRanges()).toEqual([
  3859. [[1, 16], [1, 16]]
  3860. ]);
  3861. // ctrl-click adds cursors on platforms *other* than macOS
  3862. component.props.platform = 'win32';
  3863. editor.setCursorScreenPosition([1, 4], { autoscroll: false });
  3864. component.didMouseDownOnContent(
  3865. Object.assign(clientPositionForCharacter(component, 1, 16), {
  3866. detail: 1,
  3867. button: 0,
  3868. ctrlKey: true
  3869. })
  3870. );
  3871. expect(editor.getCursorScreenPositions()).toEqual([[1, 4], [1, 16]]);
  3872. expect(editor.testAutoscrollRequests).toEqual([]);
  3873. });
  3874. it('adds word selections when holding cmd or ctrl when double-clicking', () => {
  3875. atom.config.set('editor.multiCursorOnClick', true);
  3876. const { component, editor } = buildComponent();
  3877. editor.addCursorAtScreenPosition([1, 16], { autoscroll: false });
  3878. expect(editor.getCursorScreenPositions()).toEqual([[0, 0], [1, 16]]);
  3879. component.didMouseDownOnContent(
  3880. Object.assign(clientPositionForCharacter(component, 1, 16), {
  3881. detail: 1,
  3882. button: 0,
  3883. metaKey: true
  3884. })
  3885. );
  3886. component.didMouseDownOnContent(
  3887. Object.assign(clientPositionForCharacter(component, 1, 16), {
  3888. detail: 2,
  3889. button: 0,
  3890. metaKey: true
  3891. })
  3892. );
  3893. expect(editor.getSelectedScreenRanges()).toEqual([
  3894. [[0, 0], [0, 0]],
  3895. [[1, 13], [1, 21]]
  3896. ]);
  3897. expect(editor.testAutoscrollRequests).toEqual([]);
  3898. });
  3899. it('adds line selections when holding cmd or ctrl when triple-clicking', () => {
  3900. atom.config.set('editor.multiCursorOnClick', true);
  3901. const { component, editor } = buildComponent();
  3902. editor.addCursorAtScreenPosition([1, 16], { autoscroll: false });
  3903. expect(editor.getCursorScreenPositions()).toEqual([[0, 0], [1, 16]]);
  3904. const { clientX, clientY } = clientPositionForCharacter(
  3905. component,
  3906. 1,
  3907. 16
  3908. );
  3909. component.didMouseDownOnContent({
  3910. detail: 1,
  3911. button: 0,
  3912. metaKey: true,
  3913. clientX,
  3914. clientY
  3915. });
  3916. component.didMouseDownOnContent({
  3917. detail: 2,
  3918. button: 0,
  3919. metaKey: true,
  3920. clientX,
  3921. clientY
  3922. });
  3923. component.didMouseDownOnContent({
  3924. detail: 3,
  3925. button: 0,
  3926. metaKey: true,
  3927. clientX,
  3928. clientY
  3929. });
  3930. expect(editor.getSelectedScreenRanges()).toEqual([
  3931. [[0, 0], [0, 0]],
  3932. [[1, 0], [2, 0]]
  3933. ]);
  3934. expect(editor.testAutoscrollRequests).toEqual([]);
  3935. });
  3936. it('does not add cursors when holding cmd or ctrl when single-clicking', () => {
  3937. atom.config.set('editor.multiCursorOnClick', false);
  3938. const { component, editor } = buildComponent({ platform: 'darwin' });
  3939. expect(editor.getCursorScreenPositions()).toEqual([[0, 0]]);
  3940. // moves cursor to 1, 16
  3941. component.didMouseDownOnContent(
  3942. Object.assign(clientPositionForCharacter(component, 1, 16), {
  3943. detail: 1,
  3944. button: 0,
  3945. metaKey: true
  3946. })
  3947. );
  3948. expect(editor.getCursorScreenPositions()).toEqual([[1, 16]]);
  3949. // ctrl-click does not add cursors on macOS, nor does it move the cursor
  3950. component.didMouseDownOnContent(
  3951. Object.assign(clientPositionForCharacter(component, 1, 4), {
  3952. detail: 1,
  3953. button: 0,
  3954. ctrlKey: true
  3955. })
  3956. );
  3957. expect(editor.getSelectedScreenRanges()).toEqual([
  3958. [[1, 16], [1, 16]]
  3959. ]);
  3960. // ctrl-click does not add cursors on platforms *other* than macOS
  3961. component.props.platform = 'win32';
  3962. editor.setCursorScreenPosition([1, 4], { autoscroll: false });
  3963. component.didMouseDownOnContent(
  3964. Object.assign(clientPositionForCharacter(component, 1, 16), {
  3965. detail: 1,
  3966. button: 0,
  3967. ctrlKey: true
  3968. })
  3969. );
  3970. expect(editor.getCursorScreenPositions()).toEqual([[1, 16]]);
  3971. expect(editor.testAutoscrollRequests).toEqual([]);
  3972. });
  3973. it('does not add word selections when holding cmd or ctrl when double-clicking', () => {
  3974. atom.config.set('editor.multiCursorOnClick', false);
  3975. const { component, editor } = buildComponent();
  3976. component.didMouseDownOnContent(
  3977. Object.assign(clientPositionForCharacter(component, 1, 16), {
  3978. detail: 1,
  3979. button: 0,
  3980. metaKey: true
  3981. })
  3982. );
  3983. component.didMouseDownOnContent(
  3984. Object.assign(clientPositionForCharacter(component, 1, 16), {
  3985. detail: 2,
  3986. button: 0,
  3987. metaKey: true
  3988. })
  3989. );
  3990. expect(editor.getSelectedScreenRanges()).toEqual([
  3991. [[1, 13], [1, 21]]
  3992. ]);
  3993. expect(editor.testAutoscrollRequests).toEqual([]);
  3994. });
  3995. it('does not add line selections when holding cmd or ctrl when triple-clicking', () => {
  3996. atom.config.set('editor.multiCursorOnClick', false);
  3997. const { component, editor } = buildComponent();
  3998. const { clientX, clientY } = clientPositionForCharacter(
  3999. component,
  4000. 1,
  4001. 16
  4002. );
  4003. component.didMouseDownOnContent({
  4004. detail: 1,
  4005. button: 0,
  4006. metaKey: true,
  4007. clientX,
  4008. clientY
  4009. });
  4010. component.didMouseDownOnContent({
  4011. detail: 2,
  4012. button: 0,
  4013. metaKey: true,
  4014. clientX,
  4015. clientY
  4016. });
  4017. component.didMouseDownOnContent({
  4018. detail: 3,
  4019. button: 0,
  4020. metaKey: true,
  4021. clientX,
  4022. clientY
  4023. });
  4024. expect(editor.getSelectedScreenRanges()).toEqual([[[1, 0], [2, 0]]]);
  4025. expect(editor.testAutoscrollRequests).toEqual([]);
  4026. });
  4027. it('expands the last selection on shift-click', () => {
  4028. const { component, editor } = buildComponent();
  4029. editor.setCursorScreenPosition([2, 18], { autoscroll: false });
  4030. component.didMouseDownOnContent(
  4031. Object.assign(
  4032. {
  4033. detail: 1,
  4034. button: 0,
  4035. shiftKey: true
  4036. },
  4037. clientPositionForCharacter(component, 1, 4)
  4038. )
  4039. );
  4040. expect(editor.getSelectedScreenRange()).toEqual([[1, 4], [2, 18]]);
  4041. component.didMouseDownOnContent(
  4042. Object.assign(
  4043. {
  4044. detail: 1,
  4045. button: 0,
  4046. shiftKey: true
  4047. },
  4048. clientPositionForCharacter(component, 4, 4)
  4049. )
  4050. );
  4051. expect(editor.getSelectedScreenRange()).toEqual([[2, 18], [4, 4]]);
  4052. // reorients word-wise selections to keep the word selected regardless of
  4053. // where the subsequent shift-click occurs
  4054. editor.setCursorScreenPosition([2, 18], { autoscroll: false });
  4055. editor.getLastSelection().selectWord({ autoscroll: false });
  4056. component.didMouseDownOnContent(
  4057. Object.assign(
  4058. {
  4059. detail: 1,
  4060. button: 0,
  4061. shiftKey: true
  4062. },
  4063. clientPositionForCharacter(component, 1, 4)
  4064. )
  4065. );
  4066. expect(editor.getSelectedScreenRange()).toEqual([[1, 2], [2, 20]]);
  4067. component.didMouseDownOnContent(
  4068. Object.assign(
  4069. {
  4070. detail: 1,
  4071. button: 0,
  4072. shiftKey: true
  4073. },
  4074. clientPositionForCharacter(component, 3, 11)
  4075. )
  4076. );
  4077. expect(editor.getSelectedScreenRange()).toEqual([[2, 14], [3, 13]]);
  4078. // reorients line-wise selections to keep the line selected regardless of
  4079. // where the subsequent shift-click occurs
  4080. editor.setCursorScreenPosition([2, 18], { autoscroll: false });
  4081. editor.getLastSelection().selectLine(null, { autoscroll: false });
  4082. component.didMouseDownOnContent(
  4083. Object.assign(
  4084. {
  4085. detail: 1,
  4086. button: 0,
  4087. shiftKey: true
  4088. },
  4089. clientPositionForCharacter(component, 1, 4)
  4090. )
  4091. );
  4092. expect(editor.getSelectedScreenRange()).toEqual([[1, 0], [3, 0]]);
  4093. component.didMouseDownOnContent(
  4094. Object.assign(
  4095. {
  4096. detail: 1,
  4097. button: 0,
  4098. shiftKey: true
  4099. },
  4100. clientPositionForCharacter(component, 3, 11)
  4101. )
  4102. );
  4103. expect(editor.getSelectedScreenRange()).toEqual([[2, 0], [4, 0]]);
  4104. expect(editor.testAutoscrollRequests).toEqual([]);
  4105. });
  4106. it('expands the last selection on drag', () => {
  4107. atom.config.set('editor.multiCursorOnClick', true);
  4108. const { component, editor } = buildComponent();
  4109. spyOn(component, 'handleMouseDragUntilMouseUp');
  4110. component.didMouseDownOnContent(
  4111. Object.assign(
  4112. {
  4113. detail: 1,
  4114. button: 0
  4115. },
  4116. clientPositionForCharacter(component, 1, 4)
  4117. )
  4118. );
  4119. {
  4120. const {
  4121. didDrag,
  4122. didStopDragging
  4123. } = component.handleMouseDragUntilMouseUp.argsForCall[0][0];
  4124. didDrag(clientPositionForCharacter(component, 8, 8));
  4125. expect(editor.getSelectedScreenRange()).toEqual([[1, 4], [8, 8]]);
  4126. didDrag(clientPositionForCharacter(component, 4, 8));
  4127. expect(editor.getSelectedScreenRange()).toEqual([[1, 4], [4, 8]]);
  4128. didStopDragging();
  4129. expect(editor.getSelectedScreenRange()).toEqual([[1, 4], [4, 8]]);
  4130. }
  4131. // Click-drag a second selection... selections are not merged until the
  4132. // drag stops.
  4133. component.didMouseDownOnContent(
  4134. Object.assign(
  4135. {
  4136. detail: 1,
  4137. button: 0,
  4138. metaKey: 1
  4139. },
  4140. clientPositionForCharacter(component, 8, 8)
  4141. )
  4142. );
  4143. {
  4144. const {
  4145. didDrag,
  4146. didStopDragging
  4147. } = component.handleMouseDragUntilMouseUp.argsForCall[1][0];
  4148. didDrag(clientPositionForCharacter(component, 2, 8));
  4149. expect(editor.getSelectedScreenRanges()).toEqual([
  4150. [[1, 4], [4, 8]],
  4151. [[2, 8], [8, 8]]
  4152. ]);
  4153. didDrag(clientPositionForCharacter(component, 6, 8));
  4154. expect(editor.getSelectedScreenRanges()).toEqual([
  4155. [[1, 4], [4, 8]],
  4156. [[6, 8], [8, 8]]
  4157. ]);
  4158. didDrag(clientPositionForCharacter(component, 2, 8));
  4159. expect(editor.getSelectedScreenRanges()).toEqual([
  4160. [[1, 4], [4, 8]],
  4161. [[2, 8], [8, 8]]
  4162. ]);
  4163. didStopDragging();
  4164. expect(editor.getSelectedScreenRanges()).toEqual([
  4165. [[1, 4], [8, 8]]
  4166. ]);
  4167. }
  4168. });
  4169. it('expands the selection word-wise on double-click-drag', () => {
  4170. const { component, editor } = buildComponent();
  4171. spyOn(component, 'handleMouseDragUntilMouseUp');
  4172. component.didMouseDownOnContent(
  4173. Object.assign(
  4174. {
  4175. detail: 1,
  4176. button: 0
  4177. },
  4178. clientPositionForCharacter(component, 1, 4)
  4179. )
  4180. );
  4181. component.didMouseDownOnContent(
  4182. Object.assign(
  4183. {
  4184. detail: 2,
  4185. button: 0
  4186. },
  4187. clientPositionForCharacter(component, 1, 4)
  4188. )
  4189. );
  4190. const {
  4191. didDrag
  4192. } = component.handleMouseDragUntilMouseUp.argsForCall[1][0];
  4193. didDrag(clientPositionForCharacter(component, 0, 8));
  4194. expect(editor.getSelectedScreenRange()).toEqual([[0, 4], [1, 5]]);
  4195. didDrag(clientPositionForCharacter(component, 2, 10));
  4196. expect(editor.getSelectedScreenRange()).toEqual([[1, 2], [2, 13]]);
  4197. });
  4198. it('expands the selection line-wise on triple-click-drag', () => {
  4199. const { component, editor } = buildComponent();
  4200. spyOn(component, 'handleMouseDragUntilMouseUp');
  4201. const tripleClickPosition = clientPositionForCharacter(
  4202. component,
  4203. 2,
  4204. 8
  4205. );
  4206. component.didMouseDownOnContent(
  4207. Object.assign({ detail: 1, button: 0 }, tripleClickPosition)
  4208. );
  4209. component.didMouseDownOnContent(
  4210. Object.assign({ detail: 2, button: 0 }, tripleClickPosition)
  4211. );
  4212. component.didMouseDownOnContent(
  4213. Object.assign({ detail: 3, button: 0 }, tripleClickPosition)
  4214. );
  4215. const {
  4216. didDrag
  4217. } = component.handleMouseDragUntilMouseUp.argsForCall[2][0];
  4218. didDrag(clientPositionForCharacter(component, 1, 8));
  4219. expect(editor.getSelectedScreenRange()).toEqual([[1, 0], [3, 0]]);
  4220. didDrag(clientPositionForCharacter(component, 4, 10));
  4221. expect(editor.getSelectedScreenRange()).toEqual([[2, 0], [5, 0]]);
  4222. });
  4223. it('destroys folds when clicking on their fold markers', async () => {
  4224. const { component, element, editor } = buildComponent();
  4225. editor.foldBufferRow(1);
  4226. await component.getNextUpdatePromise();
  4227. const target = element.querySelector('.fold-marker');
  4228. const { clientX, clientY } = clientPositionForCharacter(
  4229. component,
  4230. 1,
  4231. editor.lineLengthForScreenRow(1)
  4232. );
  4233. component.didMouseDownOnContent({
  4234. detail: 1,
  4235. button: 0,
  4236. target,
  4237. clientX,
  4238. clientY
  4239. });
  4240. expect(editor.isFoldedAtBufferRow(1)).toBe(false);
  4241. expect(editor.getCursorScreenPosition()).toEqual([0, 0]);
  4242. });
  4243. it('autoscrolls the content when dragging near the edge of the scroll container', async () => {
  4244. const { component } = buildComponent({
  4245. width: 200,
  4246. height: 200
  4247. });
  4248. spyOn(component, 'handleMouseDragUntilMouseUp');
  4249. let previousScrollTop = 0;
  4250. let previousScrollLeft = 0;
  4251. function assertScrolledDownAndRight() {
  4252. expect(component.getScrollTop()).toBeGreaterThan(previousScrollTop);
  4253. previousScrollTop = component.getScrollTop();
  4254. expect(component.getScrollLeft()).toBeGreaterThan(
  4255. previousScrollLeft
  4256. );
  4257. previousScrollLeft = component.getScrollLeft();
  4258. }
  4259. function assertScrolledUpAndLeft() {
  4260. expect(component.getScrollTop()).toBeLessThan(previousScrollTop);
  4261. previousScrollTop = component.getScrollTop();
  4262. expect(component.getScrollLeft()).toBeLessThan(previousScrollLeft);
  4263. previousScrollLeft = component.getScrollLeft();
  4264. }
  4265. component.didMouseDownOnContent({
  4266. detail: 1,
  4267. button: 0,
  4268. clientX: 100,
  4269. clientY: 100
  4270. });
  4271. const {
  4272. didDrag
  4273. } = component.handleMouseDragUntilMouseUp.argsForCall[0][0];
  4274. didDrag({ clientX: 199, clientY: 199 });
  4275. assertScrolledDownAndRight();
  4276. didDrag({ clientX: 199, clientY: 199 });
  4277. assertScrolledDownAndRight();
  4278. didDrag({ clientX: 199, clientY: 199 });
  4279. assertScrolledDownAndRight();
  4280. didDrag({
  4281. clientX: component.getGutterContainerWidth() + 1,
  4282. clientY: 1
  4283. });
  4284. assertScrolledUpAndLeft();
  4285. didDrag({
  4286. clientX: component.getGutterContainerWidth() + 1,
  4287. clientY: 1
  4288. });
  4289. assertScrolledUpAndLeft();
  4290. didDrag({
  4291. clientX: component.getGutterContainerWidth() + 1,
  4292. clientY: 1
  4293. });
  4294. assertScrolledUpAndLeft();
  4295. // Don't artificially update scroll position beyond possible values
  4296. expect(component.getScrollTop()).toBe(0);
  4297. expect(component.getScrollLeft()).toBe(0);
  4298. didDrag({
  4299. clientX: component.getGutterContainerWidth() + 1,
  4300. clientY: 1
  4301. });
  4302. expect(component.getScrollTop()).toBe(0);
  4303. expect(component.getScrollLeft()).toBe(0);
  4304. const maxScrollTop = component.getMaxScrollTop();
  4305. const maxScrollLeft = component.getMaxScrollLeft();
  4306. setScrollTop(component, maxScrollTop);
  4307. await setScrollLeft(component, maxScrollLeft);
  4308. didDrag({ clientX: 199, clientY: 199 });
  4309. didDrag({ clientX: 199, clientY: 199 });
  4310. didDrag({ clientX: 199, clientY: 199 });
  4311. expect(component.getScrollTop()).toBeNear(maxScrollTop);
  4312. expect(component.getScrollLeft()).toBeNear(maxScrollLeft);
  4313. });
  4314. });
  4315. it('pastes the previously selected text when clicking the middle mouse button on Linux', async () => {
  4316. spyOn(electron.ipcRenderer, 'send').andCallFake(function(
  4317. eventName,
  4318. selectedText
  4319. ) {
  4320. if (eventName === 'write-text-to-selection-clipboard') {
  4321. clipboard.writeText(selectedText, 'selection');
  4322. }
  4323. });
  4324. const { component, editor } = buildComponent({ platform: 'linux' });
  4325. // Middle mouse pasting.
  4326. atom.config.set('editor.selectionClipboard', true);
  4327. editor.setSelectedBufferRange([[1, 6], [1, 10]]);
  4328. await conditionPromise(() => TextEditor.clipboard.read() === 'sort');
  4329. component.didMouseDownOnContent({
  4330. button: 1,
  4331. clientX: clientLeftForCharacter(component, 10, 0),
  4332. clientY: clientTopForLine(component, 10)
  4333. });
  4334. expect(TextEditor.clipboard.read()).toBe('sort');
  4335. expect(editor.lineTextForBufferRow(10)).toBe('sort');
  4336. editor.undo();
  4337. // Doesn't paste when middle mouse button is clicked
  4338. atom.config.set('editor.selectionClipboard', false);
  4339. editor.setSelectedBufferRange([[1, 6], [1, 10]]);
  4340. component.didMouseDownOnContent({
  4341. button: 1,
  4342. clientX: clientLeftForCharacter(component, 10, 0),
  4343. clientY: clientTopForLine(component, 10)
  4344. });
  4345. expect(TextEditor.clipboard.read()).toBe('sort');
  4346. expect(editor.lineTextForBufferRow(10)).toBe('');
  4347. // Ensure left clicks don't interfere.
  4348. atom.config.set('editor.selectionClipboard', true);
  4349. editor.setSelectedBufferRange([[1, 2], [1, 5]]);
  4350. await conditionPromise(() => TextEditor.clipboard.read() === 'var');
  4351. component.didMouseDownOnContent({
  4352. button: 0,
  4353. detail: 1,
  4354. clientX: clientLeftForCharacter(component, 10, 0),
  4355. clientY: clientTopForLine(component, 10)
  4356. });
  4357. component.didMouseDownOnContent({
  4358. button: 1,
  4359. clientX: clientLeftForCharacter(component, 10, 0),
  4360. clientY: clientTopForLine(component, 10)
  4361. });
  4362. expect(editor.lineTextForBufferRow(10)).toBe('var');
  4363. });
  4364. it('does not paste into a read only editor when clicking the middle mouse button on Linux', async () => {
  4365. spyOn(electron.ipcRenderer, 'send').andCallFake(function(
  4366. eventName,
  4367. selectedText
  4368. ) {
  4369. if (eventName === 'write-text-to-selection-clipboard') {
  4370. clipboard.writeText(selectedText, 'selection');
  4371. }
  4372. });
  4373. const { component, editor } = buildComponent({
  4374. platform: 'linux',
  4375. readOnly: true
  4376. });
  4377. // Select the word 'sort' on line 2 and copy to clipboard
  4378. editor.setSelectedBufferRange([[1, 6], [1, 10]]);
  4379. await conditionPromise(() => TextEditor.clipboard.read() === 'sort');
  4380. // Middle-click in the buffer at line 11, column 1
  4381. component.didMouseDownOnContent({
  4382. button: 1,
  4383. clientX: clientLeftForCharacter(component, 10, 0),
  4384. clientY: clientTopForLine(component, 10)
  4385. });
  4386. // Ensure that the correct text was copied but not pasted
  4387. expect(TextEditor.clipboard.read()).toBe('sort');
  4388. expect(editor.lineTextForBufferRow(10)).toBe('');
  4389. });
  4390. });
  4391. describe('on the line number gutter', () => {
  4392. it('selects all buffer rows intersecting the clicked screen row when a line number is clicked', async () => {
  4393. const { component, editor } = buildComponent();
  4394. spyOn(component, 'handleMouseDragUntilMouseUp');
  4395. editor.setSoftWrapped(true);
  4396. await component.getNextUpdatePromise();
  4397. await setEditorWidthInCharacters(component, 50);
  4398. editor.foldBufferRange([[4, Infinity], [7, Infinity]]);
  4399. await component.getNextUpdatePromise();
  4400. // Selects entire buffer line when clicked screen line is soft-wrapped
  4401. component.didMouseDownOnLineNumberGutter({
  4402. button: 0,
  4403. clientY: clientTopForLine(component, 3)
  4404. });
  4405. expect(editor.getSelectedScreenRange()).toEqual([[3, 0], [5, 0]]);
  4406. expect(editor.getSelectedBufferRange()).toEqual([[3, 0], [4, 0]]);
  4407. // Selects entire screen line, even if folds cause that selection to
  4408. // span multiple buffer lines
  4409. component.didMouseDownOnLineNumberGutter({
  4410. button: 0,
  4411. clientY: clientTopForLine(component, 5)
  4412. });
  4413. expect(editor.getSelectedScreenRange()).toEqual([[5, 0], [6, 0]]);
  4414. expect(editor.getSelectedBufferRange()).toEqual([[4, 0], [8, 0]]);
  4415. });
  4416. it('adds new selections when a line number is meta-clicked', async () => {
  4417. const { component, editor } = buildComponent();
  4418. editor.setSoftWrapped(true);
  4419. await component.getNextUpdatePromise();
  4420. await setEditorWidthInCharacters(component, 50);
  4421. editor.foldBufferRange([[4, Infinity], [7, Infinity]]);
  4422. await component.getNextUpdatePromise();
  4423. // Selects entire buffer line when clicked screen line is soft-wrapped
  4424. component.didMouseDownOnLineNumberGutter({
  4425. button: 0,
  4426. metaKey: true,
  4427. clientY: clientTopForLine(component, 3)
  4428. });
  4429. expect(editor.getSelectedScreenRanges()).toEqual([
  4430. [[0, 0], [0, 0]],
  4431. [[3, 0], [5, 0]]
  4432. ]);
  4433. expect(editor.getSelectedBufferRanges()).toEqual([
  4434. [[0, 0], [0, 0]],
  4435. [[3, 0], [4, 0]]
  4436. ]);
  4437. // Selects entire screen line, even if folds cause that selection to
  4438. // span multiple buffer lines
  4439. component.didMouseDownOnLineNumberGutter({
  4440. button: 0,
  4441. metaKey: true,
  4442. clientY: clientTopForLine(component, 5)
  4443. });
  4444. expect(editor.getSelectedScreenRanges()).toEqual([
  4445. [[0, 0], [0, 0]],
  4446. [[3, 0], [5, 0]],
  4447. [[5, 0], [6, 0]]
  4448. ]);
  4449. expect(editor.getSelectedBufferRanges()).toEqual([
  4450. [[0, 0], [0, 0]],
  4451. [[3, 0], [4, 0]],
  4452. [[4, 0], [8, 0]]
  4453. ]);
  4454. });
  4455. it('expands the last selection when a line number is shift-clicked', async () => {
  4456. const { component, editor } = buildComponent();
  4457. spyOn(component, 'handleMouseDragUntilMouseUp');
  4458. editor.setSoftWrapped(true);
  4459. await component.getNextUpdatePromise();
  4460. await setEditorWidthInCharacters(component, 50);
  4461. editor.foldBufferRange([[4, Infinity], [7, Infinity]]);
  4462. await component.getNextUpdatePromise();
  4463. editor.setSelectedScreenRange([[3, 4], [3, 8]]);
  4464. editor.addCursorAtScreenPosition([2, 10]);
  4465. component.didMouseDownOnLineNumberGutter({
  4466. button: 0,
  4467. shiftKey: true,
  4468. clientY: clientTopForLine(component, 5)
  4469. });
  4470. expect(editor.getSelectedBufferRanges()).toEqual([
  4471. [[3, 4], [3, 8]],
  4472. [[2, 10], [8, 0]]
  4473. ]);
  4474. // Original selection is preserved when shift-click-dragging
  4475. const {
  4476. didDrag,
  4477. didStopDragging
  4478. } = component.handleMouseDragUntilMouseUp.argsForCall[0][0];
  4479. didDrag({
  4480. clientY: clientTopForLine(component, 1)
  4481. });
  4482. expect(editor.getSelectedBufferRanges()).toEqual([
  4483. [[3, 4], [3, 8]],
  4484. [[1, 0], [2, 10]]
  4485. ]);
  4486. didDrag({
  4487. clientY: clientTopForLine(component, 5)
  4488. });
  4489. didStopDragging();
  4490. expect(editor.getSelectedBufferRanges()).toEqual([[[2, 10], [8, 0]]]);
  4491. });
  4492. it('expands the selection when dragging', async () => {
  4493. const { component, editor } = buildComponent();
  4494. spyOn(component, 'handleMouseDragUntilMouseUp');
  4495. editor.setSoftWrapped(true);
  4496. await component.getNextUpdatePromise();
  4497. await setEditorWidthInCharacters(component, 50);
  4498. editor.foldBufferRange([[4, Infinity], [7, Infinity]]);
  4499. await component.getNextUpdatePromise();
  4500. editor.setSelectedScreenRange([[3, 4], [3, 6]]);
  4501. component.didMouseDownOnLineNumberGutter({
  4502. button: 0,
  4503. metaKey: true,
  4504. clientY: clientTopForLine(component, 2)
  4505. });
  4506. const {
  4507. didDrag,
  4508. didStopDragging
  4509. } = component.handleMouseDragUntilMouseUp.argsForCall[0][0];
  4510. didDrag({
  4511. clientY: clientTopForLine(component, 1)
  4512. });
  4513. expect(editor.getSelectedScreenRanges()).toEqual([
  4514. [[3, 4], [3, 6]],
  4515. [[1, 0], [3, 0]]
  4516. ]);
  4517. didDrag({
  4518. clientY: clientTopForLine(component, 5)
  4519. });
  4520. expect(editor.getSelectedScreenRanges()).toEqual([
  4521. [[3, 4], [3, 6]],
  4522. [[2, 0], [6, 0]]
  4523. ]);
  4524. expect(editor.isFoldedAtBufferRow(4)).toBe(true);
  4525. didDrag({
  4526. clientY: clientTopForLine(component, 3)
  4527. });
  4528. expect(editor.getSelectedScreenRanges()).toEqual([
  4529. [[3, 4], [3, 6]],
  4530. [[2, 0], [4, 4]]
  4531. ]);
  4532. didStopDragging();
  4533. expect(editor.getSelectedScreenRanges()).toEqual([[[2, 0], [4, 4]]]);
  4534. });
  4535. it('toggles folding when clicking on the right icon of a foldable line number', async () => {
  4536. const { component, element, editor } = buildComponent();
  4537. let target = element
  4538. .querySelectorAll('.line-number')[1]
  4539. .querySelector('.icon-right');
  4540. expect(editor.isFoldedAtScreenRow(1)).toBe(false);
  4541. component.didMouseDownOnLineNumberGutter({
  4542. target,
  4543. button: 0,
  4544. clientY: clientTopForLine(component, 1)
  4545. });
  4546. expect(editor.isFoldedAtScreenRow(1)).toBe(true);
  4547. await component.getNextUpdatePromise();
  4548. component.didMouseDownOnLineNumberGutter({
  4549. target,
  4550. button: 0,
  4551. clientY: clientTopForLine(component, 1)
  4552. });
  4553. await component.getNextUpdatePromise();
  4554. expect(editor.isFoldedAtScreenRow(1)).toBe(false);
  4555. editor.foldBufferRange([[5, 12], [5, 17]]);
  4556. await component.getNextUpdatePromise();
  4557. expect(editor.isFoldedAtScreenRow(5)).toBe(true);
  4558. target = element
  4559. .querySelectorAll('.line-number')[4]
  4560. .querySelector('.icon-right');
  4561. component.didMouseDownOnLineNumberGutter({
  4562. target,
  4563. button: 0,
  4564. clientY: clientTopForLine(component, 4)
  4565. });
  4566. expect(editor.isFoldedAtScreenRow(4)).toBe(false);
  4567. });
  4568. it('autoscrolls when dragging near the top or bottom of the gutter', async () => {
  4569. const { component } = buildComponent({
  4570. width: 200,
  4571. height: 200
  4572. });
  4573. spyOn(component, 'handleMouseDragUntilMouseUp');
  4574. let previousScrollTop = 0;
  4575. let previousScrollLeft = 0;
  4576. function assertScrolledDown() {
  4577. expect(component.getScrollTop()).toBeGreaterThan(previousScrollTop);
  4578. previousScrollTop = component.getScrollTop();
  4579. expect(component.getScrollLeft()).toBe(previousScrollLeft);
  4580. previousScrollLeft = component.getScrollLeft();
  4581. }
  4582. function assertScrolledUp() {
  4583. expect(component.getScrollTop()).toBeLessThan(previousScrollTop);
  4584. previousScrollTop = component.getScrollTop();
  4585. expect(component.getScrollLeft()).toBe(previousScrollLeft);
  4586. previousScrollLeft = component.getScrollLeft();
  4587. }
  4588. component.didMouseDownOnLineNumberGutter({
  4589. detail: 1,
  4590. button: 0,
  4591. clientX: 0,
  4592. clientY: 100
  4593. });
  4594. const {
  4595. didDrag
  4596. } = component.handleMouseDragUntilMouseUp.argsForCall[0][0];
  4597. didDrag({ clientX: 199, clientY: 199 });
  4598. assertScrolledDown();
  4599. didDrag({ clientX: 199, clientY: 199 });
  4600. assertScrolledDown();
  4601. didDrag({ clientX: 199, clientY: 199 });
  4602. assertScrolledDown();
  4603. didDrag({
  4604. clientX: component.getGutterContainerWidth() + 1,
  4605. clientY: 1
  4606. });
  4607. assertScrolledUp();
  4608. didDrag({
  4609. clientX: component.getGutterContainerWidth() + 1,
  4610. clientY: 1
  4611. });
  4612. assertScrolledUp();
  4613. didDrag({
  4614. clientX: component.getGutterContainerWidth() + 1,
  4615. clientY: 1
  4616. });
  4617. assertScrolledUp();
  4618. // Don't artificially update scroll measurements beyond the minimum or
  4619. // maximum possible scroll positions
  4620. expect(component.getScrollTop()).toBe(0);
  4621. expect(component.getScrollLeft()).toBe(0);
  4622. didDrag({
  4623. clientX: component.getGutterContainerWidth() + 1,
  4624. clientY: 1
  4625. });
  4626. expect(component.getScrollTop()).toBe(0);
  4627. expect(component.getScrollLeft()).toBe(0);
  4628. const maxScrollTop = component.getMaxScrollTop();
  4629. const maxScrollLeft = component.getMaxScrollLeft();
  4630. setScrollTop(component, maxScrollTop);
  4631. await setScrollLeft(component, maxScrollLeft);
  4632. didDrag({ clientX: 199, clientY: 199 });
  4633. didDrag({ clientX: 199, clientY: 199 });
  4634. didDrag({ clientX: 199, clientY: 199 });
  4635. expect(component.getScrollTop()).toBeNear(maxScrollTop);
  4636. expect(component.getScrollLeft()).toBeNear(maxScrollLeft);
  4637. });
  4638. });
  4639. describe('on the scrollbars', () => {
  4640. it('delegates the mousedown events to the parent component unless the mousedown was on the actual scrollbar', async () => {
  4641. const { component, editor } = buildComponent({ height: 100 });
  4642. await setEditorWidthInCharacters(component, 6);
  4643. const verticalScrollbar = component.refs.verticalScrollbar;
  4644. const horizontalScrollbar = component.refs.horizontalScrollbar;
  4645. const leftEdgeOfVerticalScrollbar =
  4646. verticalScrollbar.element.getBoundingClientRect().right -
  4647. verticalScrollbarWidth;
  4648. const topEdgeOfHorizontalScrollbar =
  4649. horizontalScrollbar.element.getBoundingClientRect().bottom -
  4650. horizontalScrollbarHeight;
  4651. verticalScrollbar.didMouseDown({
  4652. button: 0,
  4653. detail: 1,
  4654. clientY: clientTopForLine(component, 4),
  4655. clientX: leftEdgeOfVerticalScrollbar
  4656. });
  4657. expect(editor.getCursorScreenPosition()).toEqual([0, 0]);
  4658. verticalScrollbar.didMouseDown({
  4659. button: 0,
  4660. detail: 1,
  4661. clientY: clientTopForLine(component, 4),
  4662. clientX: leftEdgeOfVerticalScrollbar - 1
  4663. });
  4664. expect(editor.getCursorScreenPosition()).toEqual([4, 6]);
  4665. horizontalScrollbar.didMouseDown({
  4666. button: 0,
  4667. detail: 1,
  4668. clientY: topEdgeOfHorizontalScrollbar,
  4669. clientX: component.refs.content.getBoundingClientRect().left
  4670. });
  4671. expect(editor.getCursorScreenPosition()).toEqual([4, 6]);
  4672. horizontalScrollbar.didMouseDown({
  4673. button: 0,
  4674. detail: 1,
  4675. clientY: topEdgeOfHorizontalScrollbar - 1,
  4676. clientX: component.refs.content.getBoundingClientRect().left
  4677. });
  4678. expect(editor.getCursorScreenPosition()).toEqual([4, 0]);
  4679. });
  4680. });
  4681. });
  4682. describe('paste event', () => {
  4683. it("prevents the browser's default processing for the event on Linux", () => {
  4684. const { component } = buildComponent({ platform: 'linux' });
  4685. const event = { preventDefault: () => {} };
  4686. spyOn(event, 'preventDefault');
  4687. component.didPaste(event);
  4688. expect(event.preventDefault).toHaveBeenCalled();
  4689. });
  4690. });
  4691. describe('keyboard input', () => {
  4692. it('handles inserted accented characters via the press-and-hold menu on macOS correctly', () => {
  4693. const { editor, component } = buildComponent({
  4694. text: '',
  4695. chromeVersion: 57
  4696. });
  4697. editor.insertText('x');
  4698. editor.setCursorBufferPosition([0, 1]);
  4699. // Simulate holding the A key to open the press-and-hold menu,
  4700. // then closing it via ESC.
  4701. component.didKeydown({ code: 'KeyA' });
  4702. component.didKeypress({ code: 'KeyA' });
  4703. component.didTextInput({
  4704. data: 'a',
  4705. stopPropagation: () => {},
  4706. preventDefault: () => {}
  4707. });
  4708. component.didKeydown({ code: 'KeyA' });
  4709. component.didKeydown({ code: 'KeyA' });
  4710. component.didKeyup({ code: 'KeyA' });
  4711. component.didKeydown({ code: 'Escape' });
  4712. component.didKeyup({ code: 'Escape' });
  4713. expect(editor.getText()).toBe('xa');
  4714. // Ensure another "a" can be typed correctly.
  4715. component.didKeydown({ code: 'KeyA' });
  4716. component.didKeypress({ code: 'KeyA' });
  4717. component.didTextInput({
  4718. data: 'a',
  4719. stopPropagation: () => {},
  4720. preventDefault: () => {}
  4721. });
  4722. component.didKeyup({ code: 'KeyA' });
  4723. expect(editor.getText()).toBe('xaa');
  4724. editor.undo();
  4725. expect(editor.getText()).toBe('x');
  4726. // Simulate holding the A key to open the press-and-hold menu,
  4727. // then selecting an alternative by typing a number.
  4728. component.didKeydown({ code: 'KeyA' });
  4729. component.didKeypress({ code: 'KeyA' });
  4730. component.didTextInput({
  4731. data: 'a',
  4732. stopPropagation: () => {},
  4733. preventDefault: () => {}
  4734. });
  4735. component.didKeydown({ code: 'KeyA' });
  4736. component.didKeydown({ code: 'KeyA' });
  4737. component.didKeyup({ code: 'KeyA' });
  4738. component.didKeydown({ code: 'Digit2' });
  4739. component.didKeyup({ code: 'Digit2' });
  4740. component.didTextInput({
  4741. data: 'á',
  4742. stopPropagation: () => {},
  4743. preventDefault: () => {}
  4744. });
  4745. expect(editor.getText()).toBe('xá');
  4746. // Ensure another "a" can be typed correctly.
  4747. component.didKeydown({ code: 'KeyA' });
  4748. component.didKeypress({ code: 'KeyA' });
  4749. component.didTextInput({
  4750. data: 'a',
  4751. stopPropagation: () => {},
  4752. preventDefault: () => {}
  4753. });
  4754. component.didKeyup({ code: 'KeyA' });
  4755. expect(editor.getText()).toBe('xáa');
  4756. editor.undo();
  4757. expect(editor.getText()).toBe('x');
  4758. // Simulate holding the A key to open the press-and-hold menu,
  4759. // then selecting an alternative by clicking on it.
  4760. component.didKeydown({ code: 'KeyA' });
  4761. component.didKeypress({ code: 'KeyA' });
  4762. component.didTextInput({
  4763. data: 'a',
  4764. stopPropagation: () => {},
  4765. preventDefault: () => {}
  4766. });
  4767. component.didKeydown({ code: 'KeyA' });
  4768. component.didKeydown({ code: 'KeyA' });
  4769. component.didKeyup({ code: 'KeyA' });
  4770. component.didTextInput({
  4771. data: 'á',
  4772. stopPropagation: () => {},
  4773. preventDefault: () => {}
  4774. });
  4775. expect(editor.getText()).toBe('xá');
  4776. // Ensure another "a" can be typed correctly.
  4777. component.didKeydown({ code: 'KeyA' });
  4778. component.didKeypress({ code: 'KeyA' });
  4779. component.didTextInput({
  4780. data: 'a',
  4781. stopPropagation: () => {},
  4782. preventDefault: () => {}
  4783. });
  4784. component.didKeyup({ code: 'KeyA' });
  4785. expect(editor.getText()).toBe('xáa');
  4786. editor.undo();
  4787. expect(editor.getText()).toBe('x');
  4788. // Simulate holding the A key to open the press-and-hold menu,
  4789. // cycling through the alternatives with the arrows, then selecting one of them with Enter.
  4790. component.didKeydown({ code: 'KeyA' });
  4791. component.didKeypress({ code: 'KeyA' });
  4792. component.didTextInput({
  4793. data: 'a',
  4794. stopPropagation: () => {},
  4795. preventDefault: () => {}
  4796. });
  4797. component.didKeydown({ code: 'KeyA' });
  4798. component.didKeydown({ code: 'KeyA' });
  4799. component.didKeyup({ code: 'KeyA' });
  4800. component.didKeydown({ code: 'ArrowRight' });
  4801. component.didCompositionStart({ data: '' });
  4802. component.didCompositionUpdate({ data: 'à' });
  4803. component.didKeyup({ code: 'ArrowRight' });
  4804. expect(editor.getText()).toBe('xà');
  4805. component.didKeydown({ code: 'ArrowRight' });
  4806. component.didCompositionUpdate({ data: 'á' });
  4807. component.didKeyup({ code: 'ArrowRight' });
  4808. expect(editor.getText()).toBe('xá');
  4809. component.didKeydown({ code: 'Enter' });
  4810. component.didCompositionUpdate({ data: 'á' });
  4811. component.didTextInput({
  4812. data: 'á',
  4813. stopPropagation: () => {},
  4814. preventDefault: () => {}
  4815. });
  4816. component.didCompositionEnd({
  4817. data: 'á',
  4818. target: component.refs.cursorsAndInput.refs.hiddenInput
  4819. });
  4820. component.didKeyup({ code: 'Enter' });
  4821. expect(editor.getText()).toBe('xá');
  4822. // Ensure another "a" can be typed correctly.
  4823. component.didKeydown({ code: 'KeyA' });
  4824. component.didKeypress({ code: 'KeyA' });
  4825. component.didTextInput({
  4826. data: 'a',
  4827. stopPropagation: () => {},
  4828. preventDefault: () => {}
  4829. });
  4830. component.didKeyup({ code: 'KeyA' });
  4831. expect(editor.getText()).toBe('xáa');
  4832. editor.undo();
  4833. expect(editor.getText()).toBe('x');
  4834. // Simulate holding the A key to open the press-and-hold menu,
  4835. // cycling through the alternatives with the arrows, then closing it via ESC.
  4836. component.didKeydown({ code: 'KeyA' });
  4837. component.didKeypress({ code: 'KeyA' });
  4838. component.didTextInput({
  4839. data: 'a',
  4840. stopPropagation: () => {},
  4841. preventDefault: () => {}
  4842. });
  4843. component.didKeydown({ code: 'KeyA' });
  4844. component.didKeydown({ code: 'KeyA' });
  4845. component.didKeyup({ code: 'KeyA' });
  4846. component.didKeydown({ code: 'ArrowRight' });
  4847. component.didCompositionStart({ data: '' });
  4848. component.didCompositionUpdate({ data: 'à' });
  4849. component.didKeyup({ code: 'ArrowRight' });
  4850. expect(editor.getText()).toBe('xà');
  4851. component.didKeydown({ code: 'ArrowRight' });
  4852. component.didCompositionUpdate({ data: 'á' });
  4853. component.didKeyup({ code: 'ArrowRight' });
  4854. expect(editor.getText()).toBe('xá');
  4855. component.didKeydown({ code: 'Escape' });
  4856. component.didCompositionUpdate({ data: 'a' });
  4857. component.didTextInput({
  4858. data: 'a',
  4859. stopPropagation: () => {},
  4860. preventDefault: () => {}
  4861. });
  4862. component.didCompositionEnd({
  4863. data: 'a',
  4864. target: component.refs.cursorsAndInput.refs.hiddenInput
  4865. });
  4866. component.didKeyup({ code: 'Escape' });
  4867. expect(editor.getText()).toBe('xa');
  4868. // Ensure another "a" can be typed correctly.
  4869. component.didKeydown({ code: 'KeyA' });
  4870. component.didKeypress({ code: 'KeyA' });
  4871. component.didTextInput({
  4872. data: 'a',
  4873. stopPropagation: () => {},
  4874. preventDefault: () => {}
  4875. });
  4876. component.didKeyup({ code: 'KeyA' });
  4877. expect(editor.getText()).toBe('xaa');
  4878. editor.undo();
  4879. expect(editor.getText()).toBe('x');
  4880. // Simulate pressing the O key and holding the A key to open the press-and-hold menu right before releasing the O key,
  4881. // cycling through the alternatives with the arrows, then closing it via ESC.
  4882. component.didKeydown({ code: 'KeyO' });
  4883. component.didKeypress({ code: 'KeyO' });
  4884. component.didTextInput({
  4885. data: 'o',
  4886. stopPropagation: () => {},
  4887. preventDefault: () => {}
  4888. });
  4889. component.didKeydown({ code: 'KeyA' });
  4890. component.didKeypress({ code: 'KeyA' });
  4891. component.didTextInput({
  4892. data: 'a',
  4893. stopPropagation: () => {},
  4894. preventDefault: () => {}
  4895. });
  4896. component.didKeyup({ code: 'KeyO' });
  4897. component.didKeydown({ code: 'KeyA' });
  4898. component.didKeydown({ code: 'KeyA' });
  4899. component.didKeydown({ code: 'ArrowRight' });
  4900. component.didCompositionStart({ data: '' });
  4901. component.didCompositionUpdate({ data: 'à' });
  4902. component.didKeyup({ code: 'ArrowRight' });
  4903. expect(editor.getText()).toBe('xoà');
  4904. component.didKeydown({ code: 'ArrowRight' });
  4905. component.didCompositionUpdate({ data: 'á' });
  4906. component.didKeyup({ code: 'ArrowRight' });
  4907. expect(editor.getText()).toBe('xoá');
  4908. component.didKeydown({ code: 'Escape' });
  4909. component.didCompositionUpdate({ data: 'a' });
  4910. component.didTextInput({
  4911. data: 'a',
  4912. stopPropagation: () => {},
  4913. preventDefault: () => {}
  4914. });
  4915. component.didCompositionEnd({
  4916. data: 'a',
  4917. target: component.refs.cursorsAndInput.refs.hiddenInput
  4918. });
  4919. component.didKeyup({ code: 'Escape' });
  4920. expect(editor.getText()).toBe('xoa');
  4921. // Ensure another "a" can be typed correctly.
  4922. component.didKeydown({ code: 'KeyA' });
  4923. component.didKeypress({ code: 'KeyA' });
  4924. component.didTextInput({
  4925. data: 'a',
  4926. stopPropagation: () => {},
  4927. preventDefault: () => {}
  4928. });
  4929. component.didKeyup({ code: 'KeyA' });
  4930. editor.undo();
  4931. expect(editor.getText()).toBe('x');
  4932. // Simulate holding the A key to open the press-and-hold menu,
  4933. // cycling through the alternatives with the arrows, then closing it by changing focus.
  4934. component.didKeydown({ code: 'KeyA' });
  4935. component.didKeypress({ code: 'KeyA' });
  4936. component.didTextInput({
  4937. data: 'a',
  4938. stopPropagation: () => {},
  4939. preventDefault: () => {}
  4940. });
  4941. component.didKeydown({ code: 'KeyA' });
  4942. component.didKeydown({ code: 'KeyA' });
  4943. component.didKeyup({ code: 'KeyA' });
  4944. component.didKeydown({ code: 'ArrowRight' });
  4945. component.didCompositionStart({ data: '' });
  4946. component.didCompositionUpdate({ data: 'à' });
  4947. component.didKeyup({ code: 'ArrowRight' });
  4948. expect(editor.getText()).toBe('xà');
  4949. component.didKeydown({ code: 'ArrowRight' });
  4950. component.didCompositionUpdate({ data: 'á' });
  4951. component.didKeyup({ code: 'ArrowRight' });
  4952. expect(editor.getText()).toBe('xá');
  4953. component.didCompositionUpdate({ data: 'á' });
  4954. component.didTextInput({
  4955. data: 'á',
  4956. stopPropagation: () => {},
  4957. preventDefault: () => {}
  4958. });
  4959. component.didCompositionEnd({
  4960. data: 'á',
  4961. target: component.refs.cursorsAndInput.refs.hiddenInput
  4962. });
  4963. expect(editor.getText()).toBe('xá');
  4964. // Ensure another "a" can be typed correctly.
  4965. component.didKeydown({ code: 'KeyA' });
  4966. component.didKeypress({ code: 'KeyA' });
  4967. component.didTextInput({
  4968. data: 'a',
  4969. stopPropagation: () => {},
  4970. preventDefault: () => {}
  4971. });
  4972. component.didKeyup({ code: 'KeyA' });
  4973. expect(editor.getText()).toBe('xáa');
  4974. editor.undo();
  4975. expect(editor.getText()).toBe('x');
  4976. });
  4977. });
  4978. describe('styling changes', () => {
  4979. /**
  4980. * TODO: FAILING TEST - This test fails with the following output:
  4981. * Expected 7.234375 not to be 7.234375.
  4982. * Expected 7.234375 not to be 7.234375.
  4983. * Expected 7.234375 not to be 7.234375.
  4984. */
  4985. xit('updates the rendered content based on new measurements when the font dimensions change', async () => {
  4986. const { component, element, editor } = buildComponent({
  4987. rowsPerTile: 1,
  4988. autoHeight: false
  4989. });
  4990. await setEditorHeightInLines(component, 3);
  4991. editor.setCursorScreenPosition([1, 29], { autoscroll: false });
  4992. await component.getNextUpdatePromise();
  4993. let cursorNode = element.querySelector('.cursor');
  4994. const initialBaseCharacterWidth = editor.getDefaultCharWidth();
  4995. const initialDoubleCharacterWidth = editor.getDoubleWidthCharWidth();
  4996. const initialHalfCharacterWidth = editor.getHalfWidthCharWidth();
  4997. const initialKoreanCharacterWidth = editor.getKoreanCharWidth();
  4998. const initialRenderedLineCount = queryOnScreenLineElements(element)
  4999. .length;
  5000. const initialFontSize = parseInt(getComputedStyle(element).fontSize);
  5001. expect(initialKoreanCharacterWidth).toBeDefined();
  5002. expect(initialDoubleCharacterWidth).toBeDefined();
  5003. expect(initialHalfCharacterWidth).toBeDefined();
  5004. expect(initialBaseCharacterWidth).toBeDefined();
  5005. expect(initialDoubleCharacterWidth).not.toBe(initialBaseCharacterWidth);
  5006. expect(initialHalfCharacterWidth).not.toBe(initialBaseCharacterWidth);
  5007. expect(initialKoreanCharacterWidth).not.toBe(initialBaseCharacterWidth);
  5008. verifyCursorPosition(component, cursorNode, 1, 29);
  5009. element.style.fontSize = initialFontSize - 5 + 'px';
  5010. TextEditor.didUpdateStyles();
  5011. await component.getNextUpdatePromise();
  5012. expect(editor.getDefaultCharWidth()).toBeLessThan(
  5013. initialBaseCharacterWidth
  5014. );
  5015. expect(editor.getDoubleWidthCharWidth()).toBeLessThan(
  5016. initialDoubleCharacterWidth
  5017. );
  5018. expect(editor.getHalfWidthCharWidth()).toBeLessThan(
  5019. initialHalfCharacterWidth
  5020. );
  5021. expect(editor.getKoreanCharWidth()).toBeLessThan(
  5022. initialKoreanCharacterWidth
  5023. );
  5024. expect(queryOnScreenLineElements(element).length).toBeGreaterThan(
  5025. initialRenderedLineCount
  5026. );
  5027. verifyCursorPosition(component, cursorNode, 1, 29);
  5028. element.style.fontSize = initialFontSize + 10 + 'px';
  5029. TextEditor.didUpdateStyles();
  5030. await component.getNextUpdatePromise();
  5031. expect(editor.getDefaultCharWidth()).toBeGreaterThan(
  5032. initialBaseCharacterWidth
  5033. );
  5034. expect(editor.getDoubleWidthCharWidth()).toBeGreaterThan(
  5035. initialDoubleCharacterWidth
  5036. );
  5037. expect(editor.getHalfWidthCharWidth()).toBeGreaterThan(
  5038. initialHalfCharacterWidth
  5039. );
  5040. expect(editor.getKoreanCharWidth()).toBeGreaterThan(
  5041. initialKoreanCharacterWidth
  5042. );
  5043. expect(queryOnScreenLineElements(element).length).toBeLessThan(
  5044. initialRenderedLineCount
  5045. );
  5046. verifyCursorPosition(component, cursorNode, 1, 29);
  5047. });
  5048. it('maintains the scrollTopRow and scrollLeftColumn when the font size changes', async () => {
  5049. const { component, element } = buildComponent({
  5050. rowsPerTile: 1,
  5051. autoHeight: false
  5052. });
  5053. await setEditorHeightInLines(component, 3);
  5054. await setEditorWidthInCharacters(component, 20);
  5055. component.setScrollTopRow(4);
  5056. component.setScrollLeftColumn(10);
  5057. await component.getNextUpdatePromise();
  5058. const initialFontSize = parseInt(getComputedStyle(element).fontSize);
  5059. element.style.fontSize = initialFontSize - 5 + 'px';
  5060. TextEditor.didUpdateStyles();
  5061. await component.getNextUpdatePromise();
  5062. expect(component.getScrollTopRow()).toBe(4);
  5063. element.style.fontSize = initialFontSize + 5 + 'px';
  5064. TextEditor.didUpdateStyles();
  5065. await component.getNextUpdatePromise();
  5066. expect(component.getScrollTopRow()).toBe(4);
  5067. });
  5068. it('gracefully handles the editor being hidden after a styling change', async () => {
  5069. const { component, element } = buildComponent({
  5070. autoHeight: false
  5071. });
  5072. element.style.fontSize =
  5073. parseInt(getComputedStyle(element).fontSize) + 5 + 'px';
  5074. TextEditor.didUpdateStyles();
  5075. element.style.display = 'none';
  5076. await component.getNextUpdatePromise();
  5077. });
  5078. it('does not throw an exception when the editor is soft-wrapped and changing the font size changes also the longest screen line', async () => {
  5079. const { component, element, editor } = buildComponent({
  5080. rowsPerTile: 3,
  5081. autoHeight: false
  5082. });
  5083. editor.setText(
  5084. 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\n' +
  5085. 'eiusmod tempor incididunt ut labore et dolore magna' +
  5086. 'aliqua. Ut enim ad minim veniam, quis nostrud exercitation'
  5087. );
  5088. editor.setSoftWrapped(true);
  5089. await setEditorHeightInLines(component, 2);
  5090. await setEditorWidthInCharacters(component, 56);
  5091. await setScrollTop(component, 3 * component.getLineHeight());
  5092. element.style.fontSize = '20px';
  5093. TextEditor.didUpdateStyles();
  5094. await component.getNextUpdatePromise();
  5095. });
  5096. it('updates the width of the lines div based on the longest screen line', async () => {
  5097. const { component, element, editor } = buildComponent({
  5098. rowsPerTile: 1,
  5099. autoHeight: false
  5100. });
  5101. editor.setText(
  5102. 'Lorem ipsum dolor sit\n' +
  5103. 'amet, consectetur adipisicing\n' +
  5104. 'elit, sed do\n' +
  5105. 'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation'
  5106. );
  5107. await setEditorHeightInLines(component, 2);
  5108. element.style.fontSize = '20px';
  5109. TextEditor.didUpdateStyles();
  5110. await component.getNextUpdatePromise();
  5111. // Capture the width of the lines before requesting the width of
  5112. // longest line, because making that request forces a DOM update
  5113. const actualWidth = element.querySelector('.lines').style.width;
  5114. const expectedWidth = Math.ceil(
  5115. component.pixelPositionForScreenPosition(Point(3, Infinity)).left +
  5116. component.getBaseCharacterWidth()
  5117. );
  5118. expect(actualWidth).toBe(expectedWidth + 'px');
  5119. });
  5120. });
  5121. describe('synchronous updates', () => {
  5122. let editorElementWasUpdatedSynchronously;
  5123. beforeEach(() => {
  5124. editorElementWasUpdatedSynchronously =
  5125. TextEditorElement.prototype.updatedSynchronously;
  5126. });
  5127. afterEach(() => {
  5128. TextEditorElement.prototype.setUpdatedSynchronously(
  5129. editorElementWasUpdatedSynchronously
  5130. );
  5131. });
  5132. it('updates synchronously when updatedSynchronously is true', () => {
  5133. const editor = buildEditor();
  5134. const { element } = new TextEditorComponent({
  5135. model: editor,
  5136. updatedSynchronously: true
  5137. });
  5138. jasmine.attachToDOM(element);
  5139. editor.setText('Lorem ipsum dolor');
  5140. expect(
  5141. queryOnScreenLineElements(element).map(l => l.textContent)
  5142. ).toEqual([editor.lineTextForScreenRow(0)]);
  5143. });
  5144. it('does not throw an exception on attachment when setting the soft-wrap column', () => {
  5145. const { element, editor } = buildComponent({
  5146. width: 435,
  5147. attach: false,
  5148. updatedSynchronously: true
  5149. });
  5150. editor.setSoftWrapped(true);
  5151. spyOn(window, 'onerror').andCallThrough();
  5152. jasmine.attachToDOM(element); // should not throw an exception
  5153. expect(window.onerror).not.toHaveBeenCalled();
  5154. });
  5155. it('updates synchronously when creating a component via TextEditor and TextEditorElement.prototype.updatedSynchronously is true', () => {
  5156. TextEditorElement.prototype.setUpdatedSynchronously(true);
  5157. const editor = buildEditor();
  5158. const element = editor.element;
  5159. jasmine.attachToDOM(element);
  5160. editor.setText('Lorem ipsum dolor');
  5161. expect(
  5162. queryOnScreenLineElements(element).map(l => l.textContent)
  5163. ).toEqual([editor.lineTextForScreenRow(0)]);
  5164. });
  5165. it('measures dimensions synchronously when measureDimensions is called on the component', () => {
  5166. TextEditorElement.prototype.setUpdatedSynchronously(true);
  5167. const editor = buildEditor({ autoHeight: false });
  5168. const element = editor.element;
  5169. jasmine.attachToDOM(element);
  5170. element.style.height = '100px';
  5171. expect(element.component.getClientContainerHeight()).not.toBe(100);
  5172. element.component.measureDimensions();
  5173. expect(element.component.getClientContainerHeight()).toBe(100);
  5174. });
  5175. });
  5176. describe('pixelPositionForScreenPosition(point)', () => {
  5177. it('returns the pixel position for the given point, regardless of whether or not it is currently on screen', async () => {
  5178. const { component, editor } = buildComponent({
  5179. rowsPerTile: 2,
  5180. autoHeight: false
  5181. });
  5182. await setEditorHeightInLines(component, 3);
  5183. await setScrollTop(component, 3 * component.getLineHeight());
  5184. const { component: referenceComponent } = buildComponent();
  5185. const referenceContentRect = referenceComponent.refs.content.getBoundingClientRect();
  5186. {
  5187. const { top, left } = component.pixelPositionForScreenPosition({
  5188. row: 0,
  5189. column: 0
  5190. });
  5191. expect(top).toBe(
  5192. clientTopForLine(referenceComponent, 0) - referenceContentRect.top
  5193. );
  5194. expect(left).toBe(
  5195. clientLeftForCharacter(referenceComponent, 0, 0) -
  5196. referenceContentRect.left
  5197. );
  5198. }
  5199. {
  5200. const { top, left } = component.pixelPositionForScreenPosition({
  5201. row: 0,
  5202. column: 5
  5203. });
  5204. expect(top).toBe(
  5205. clientTopForLine(referenceComponent, 0) - referenceContentRect.top
  5206. );
  5207. expect(left).toBeNear(
  5208. clientLeftForCharacter(referenceComponent, 0, 5) -
  5209. referenceContentRect.left
  5210. );
  5211. }
  5212. {
  5213. const { top, left } = component.pixelPositionForScreenPosition({
  5214. row: 12,
  5215. column: 1
  5216. });
  5217. expect(top).toBeNear(
  5218. clientTopForLine(referenceComponent, 12) - referenceContentRect.top
  5219. );
  5220. expect(left).toBeNear(
  5221. clientLeftForCharacter(referenceComponent, 12, 1) -
  5222. referenceContentRect.left
  5223. );
  5224. }
  5225. // Measuring a currently rendered line while an autoscroll that causes
  5226. // that line to go off-screen is in progress.
  5227. {
  5228. editor.setCursorScreenPosition([10, 0]);
  5229. const { top, left } = component.pixelPositionForScreenPosition({
  5230. row: 3,
  5231. column: 5
  5232. });
  5233. expect(top).toBeNear(
  5234. clientTopForLine(referenceComponent, 3) - referenceContentRect.top
  5235. );
  5236. expect(left).toBeNear(
  5237. clientLeftForCharacter(referenceComponent, 3, 5) -
  5238. referenceContentRect.left
  5239. );
  5240. }
  5241. });
  5242. it('does not get the component into an inconsistent state when the model has unflushed changes (regression)', async () => {
  5243. const { component, editor } = buildComponent({
  5244. rowsPerTile: 2,
  5245. autoHeight: false,
  5246. text: ''
  5247. });
  5248. await setEditorHeightInLines(component, 10);
  5249. const updatePromise = editor.getBuffer().append('hi\n');
  5250. component.screenPositionForPixelPosition({ top: 800, left: 1 });
  5251. await updatePromise;
  5252. });
  5253. it('does not shift cursors downward or render off-screen content when measuring off-screen lines (regression)', async () => {
  5254. const { component, element } = buildComponent({
  5255. rowsPerTile: 2,
  5256. autoHeight: false
  5257. });
  5258. await setEditorHeightInLines(component, 3);
  5259. component.pixelPositionForScreenPosition({
  5260. row: 12,
  5261. column: 1
  5262. });
  5263. expect(element.querySelector('.cursor').getBoundingClientRect().top).toBe(
  5264. component.refs.lineTiles.getBoundingClientRect().top
  5265. );
  5266. expect(
  5267. element.querySelector('.line[data-screen-row="12"]').style.visibility
  5268. ).toBe('hidden');
  5269. // Ensure previously measured off screen lines don't have any weird
  5270. // styling when they come on screen in the next frame
  5271. await setEditorHeightInLines(component, 13);
  5272. const previouslyMeasuredLineElement = element.querySelector(
  5273. '.line[data-screen-row="12"]'
  5274. );
  5275. expect(previouslyMeasuredLineElement.style.display).toBe('');
  5276. expect(previouslyMeasuredLineElement.style.visibility).toBe('');
  5277. });
  5278. });
  5279. describe('screenPositionForPixelPosition', () => {
  5280. it('returns the screen position for the given pixel position, regardless of whether or not it is currently on screen', async () => {
  5281. const { component, editor } = buildComponent({
  5282. rowsPerTile: 2,
  5283. autoHeight: false
  5284. });
  5285. await setEditorHeightInLines(component, 3);
  5286. await setScrollTop(component, 3 * component.getLineHeight());
  5287. const { component: referenceComponent } = buildComponent();
  5288. {
  5289. const pixelPosition = referenceComponent.pixelPositionForScreenPosition(
  5290. { row: 0, column: 0 }
  5291. );
  5292. pixelPosition.top += component.getLineHeight() / 3;
  5293. pixelPosition.left += component.getBaseCharacterWidth() / 3;
  5294. expect(component.screenPositionForPixelPosition(pixelPosition)).toEqual(
  5295. [0, 0]
  5296. );
  5297. }
  5298. {
  5299. const pixelPosition = referenceComponent.pixelPositionForScreenPosition(
  5300. { row: 0, column: 5 }
  5301. );
  5302. pixelPosition.top += component.getLineHeight() / 3;
  5303. pixelPosition.left += component.getBaseCharacterWidth() / 3;
  5304. expect(component.screenPositionForPixelPosition(pixelPosition)).toEqual(
  5305. [0, 5]
  5306. );
  5307. }
  5308. {
  5309. const pixelPosition = referenceComponent.pixelPositionForScreenPosition(
  5310. { row: 5, column: 7 }
  5311. );
  5312. pixelPosition.top += component.getLineHeight() / 3;
  5313. pixelPosition.left += component.getBaseCharacterWidth() / 3;
  5314. expect(component.screenPositionForPixelPosition(pixelPosition)).toEqual(
  5315. [5, 7]
  5316. );
  5317. }
  5318. {
  5319. const pixelPosition = referenceComponent.pixelPositionForScreenPosition(
  5320. { row: 12, column: 1 }
  5321. );
  5322. pixelPosition.top += component.getLineHeight() / 3;
  5323. pixelPosition.left += component.getBaseCharacterWidth() / 3;
  5324. expect(component.screenPositionForPixelPosition(pixelPosition)).toEqual(
  5325. [12, 1]
  5326. );
  5327. }
  5328. // Measuring a currently rendered line while an autoscroll that causes
  5329. // that line to go off-screen is in progress.
  5330. {
  5331. const pixelPosition = referenceComponent.pixelPositionForScreenPosition(
  5332. { row: 3, column: 4 }
  5333. );
  5334. pixelPosition.top += component.getLineHeight() / 3;
  5335. pixelPosition.left += component.getBaseCharacterWidth() / 3;
  5336. editor.setCursorBufferPosition([10, 0]);
  5337. expect(component.screenPositionForPixelPosition(pixelPosition)).toEqual(
  5338. [3, 4]
  5339. );
  5340. }
  5341. });
  5342. });
  5343. describe('model methods that delegate to the component / element', () => {
  5344. it('delegates setHeight and getHeight to the component', async () => {
  5345. const { component, editor } = buildComponent({
  5346. autoHeight: false
  5347. });
  5348. spyOn(Grim, 'deprecate');
  5349. expect(editor.getHeight()).toBe(component.getScrollContainerHeight());
  5350. expect(Grim.deprecate.callCount).toBe(1);
  5351. editor.setHeight(100);
  5352. await component.getNextUpdatePromise();
  5353. expect(component.getScrollContainerHeight()).toBe(100);
  5354. expect(Grim.deprecate.callCount).toBe(2);
  5355. });
  5356. it('delegates setWidth and getWidth to the component', async () => {
  5357. const { component, editor } = buildComponent();
  5358. spyOn(Grim, 'deprecate');
  5359. expect(editor.getWidth()).toBe(component.getScrollContainerWidth());
  5360. expect(Grim.deprecate.callCount).toBe(1);
  5361. editor.setWidth(100);
  5362. await component.getNextUpdatePromise();
  5363. expect(component.getScrollContainerWidth()).toBe(100);
  5364. expect(Grim.deprecate.callCount).toBe(2);
  5365. });
  5366. it('delegates getFirstVisibleScreenRow, getLastVisibleScreenRow, and getVisibleRowRange to the component', async () => {
  5367. const { component, element, editor } = buildComponent({
  5368. rowsPerTile: 3,
  5369. autoHeight: false
  5370. });
  5371. element.style.height = 4 * component.measurements.lineHeight + 'px';
  5372. await component.getNextUpdatePromise();
  5373. await setScrollTop(component, 5 * component.getLineHeight());
  5374. expect(editor.getFirstVisibleScreenRow()).toBe(
  5375. component.getFirstVisibleRow()
  5376. );
  5377. expect(editor.getLastVisibleScreenRow()).toBe(
  5378. component.getLastVisibleRow()
  5379. );
  5380. expect(editor.getVisibleRowRange()).toEqual([
  5381. component.getFirstVisibleRow(),
  5382. component.getLastVisibleRow()
  5383. ]);
  5384. });
  5385. it('assigns scrollTop on the component when calling setFirstVisibleScreenRow', async () => {
  5386. const { component, element, editor } = buildComponent({
  5387. rowsPerTile: 3,
  5388. autoHeight: false
  5389. });
  5390. element.style.height =
  5391. 4 * component.measurements.lineHeight +
  5392. horizontalScrollbarHeight +
  5393. 'px';
  5394. await component.getNextUpdatePromise();
  5395. expect(component.getMaxScrollTop() / component.getLineHeight()).toBeNear(
  5396. 9
  5397. );
  5398. expect(component.refs.verticalScrollbar.element.scrollTop).toBe(
  5399. 0 * component.getLineHeight()
  5400. );
  5401. editor.setFirstVisibleScreenRow(1);
  5402. expect(component.getFirstVisibleRow()).toBe(1);
  5403. await component.getNextUpdatePromise();
  5404. expect(component.refs.verticalScrollbar.element.scrollTop).toBeNear(
  5405. 1 * component.getLineHeight()
  5406. );
  5407. editor.setFirstVisibleScreenRow(5);
  5408. expect(component.getFirstVisibleRow()).toBe(5);
  5409. await component.getNextUpdatePromise();
  5410. expect(component.refs.verticalScrollbar.element.scrollTop).toBeNear(
  5411. 5 * component.getLineHeight()
  5412. );
  5413. editor.setFirstVisibleScreenRow(11);
  5414. expect(component.getFirstVisibleRow()).toBe(9);
  5415. await component.getNextUpdatePromise();
  5416. expect(component.refs.verticalScrollbar.element.scrollTop).toBeNear(
  5417. 9 * component.getLineHeight()
  5418. );
  5419. });
  5420. it('delegates setFirstVisibleScreenColumn and getFirstVisibleScreenColumn to the component', async () => {
  5421. const { component, element, editor } = buildComponent({
  5422. rowsPerTile: 3,
  5423. autoHeight: false
  5424. });
  5425. element.style.width = 30 * component.getBaseCharacterWidth() + 'px';
  5426. await component.getNextUpdatePromise();
  5427. expect(editor.getFirstVisibleScreenColumn()).toBe(0);
  5428. expect(component.refs.horizontalScrollbar.element.scrollLeft).toBe(0);
  5429. setScrollLeft(component, 5.5 * component.getBaseCharacterWidth());
  5430. expect(editor.getFirstVisibleScreenColumn()).toBe(5);
  5431. await component.getNextUpdatePromise();
  5432. expect(component.refs.horizontalScrollbar.element.scrollLeft).toBeCloseTo(
  5433. 5.5 * component.getBaseCharacterWidth(),
  5434. -1
  5435. );
  5436. editor.setFirstVisibleScreenColumn(12);
  5437. expect(component.getScrollLeft()).toBeCloseTo(
  5438. 12 * component.getBaseCharacterWidth(),
  5439. -1
  5440. );
  5441. await component.getNextUpdatePromise();
  5442. expect(component.refs.horizontalScrollbar.element.scrollLeft).toBeCloseTo(
  5443. 12 * component.getBaseCharacterWidth(),
  5444. -1
  5445. );
  5446. });
  5447. });
  5448. describe('handleMouseDragUntilMouseUp', () => {
  5449. it('repeatedly schedules `didDrag` calls on new animation frames after moving the mouse, and calls `didStopDragging` on mouseup', async () => {
  5450. const { component } = buildComponent();
  5451. let dragEvents;
  5452. let dragging = false;
  5453. component.handleMouseDragUntilMouseUp({
  5454. didDrag: event => {
  5455. dragging = true;
  5456. dragEvents.push(event);
  5457. },
  5458. didStopDragging: () => {
  5459. dragging = false;
  5460. }
  5461. });
  5462. expect(dragging).toBe(false);
  5463. dragEvents = [];
  5464. const moveEvent1 = new MouseEvent('mousemove');
  5465. window.dispatchEvent(moveEvent1);
  5466. expect(dragging).toBe(false);
  5467. await getNextAnimationFramePromise();
  5468. expect(dragging).toBe(true);
  5469. expect(dragEvents).toEqual([moveEvent1]);
  5470. await getNextAnimationFramePromise();
  5471. expect(dragging).toBe(true);
  5472. expect(dragEvents).toEqual([moveEvent1, moveEvent1]);
  5473. dragEvents = [];
  5474. const moveEvent2 = new MouseEvent('mousemove');
  5475. window.dispatchEvent(moveEvent2);
  5476. expect(dragging).toBe(true);
  5477. expect(dragEvents).toEqual([]);
  5478. await getNextAnimationFramePromise();
  5479. expect(dragging).toBe(true);
  5480. expect(dragEvents).toEqual([moveEvent2]);
  5481. await getNextAnimationFramePromise();
  5482. expect(dragging).toBe(true);
  5483. expect(dragEvents).toEqual([moveEvent2, moveEvent2]);
  5484. dragEvents = [];
  5485. window.dispatchEvent(new MouseEvent('mouseup'));
  5486. expect(dragging).toBe(false);
  5487. expect(dragEvents).toEqual([]);
  5488. window.dispatchEvent(new MouseEvent('mousemove'));
  5489. await getNextAnimationFramePromise();
  5490. expect(dragging).toBe(false);
  5491. expect(dragEvents).toEqual([]);
  5492. });
  5493. it('calls `didStopDragging` if the user interacts with the keyboard while dragging', async () => {
  5494. const { component, editor } = buildComponent();
  5495. let dragging = false;
  5496. function startDragging() {
  5497. component.handleMouseDragUntilMouseUp({
  5498. didDrag: event => {
  5499. dragging = true;
  5500. },
  5501. didStopDragging: () => {
  5502. dragging = false;
  5503. }
  5504. });
  5505. }
  5506. startDragging();
  5507. window.dispatchEvent(new MouseEvent('mousemove'));
  5508. await getNextAnimationFramePromise();
  5509. expect(dragging).toBe(true);
  5510. // Buffer changes don't cause dragging to be stopped.
  5511. editor.insertText('X');
  5512. expect(dragging).toBe(true);
  5513. // Keyboard interaction prevents users from dragging further.
  5514. component.didKeydown({ code: 'KeyX' });
  5515. expect(dragging).toBe(false);
  5516. window.dispatchEvent(new MouseEvent('mousemove'));
  5517. await getNextAnimationFramePromise();
  5518. expect(dragging).toBe(false);
  5519. // Pressing a modifier key does not terminate dragging, (to ensure we can add new selections with the mouse)
  5520. startDragging();
  5521. window.dispatchEvent(new MouseEvent('mousemove'));
  5522. await getNextAnimationFramePromise();
  5523. expect(dragging).toBe(true);
  5524. component.didKeydown({ key: 'Control' });
  5525. component.didKeydown({ key: 'Alt' });
  5526. component.didKeydown({ key: 'Shift' });
  5527. component.didKeydown({ key: 'Meta' });
  5528. expect(dragging).toBe(true);
  5529. });
  5530. function getNextAnimationFramePromise() {
  5531. return new Promise(resolve => requestAnimationFrame(resolve));
  5532. }
  5533. });
  5534. });
  5535. function buildEditor(params = {}) {
  5536. const text = params.text != null ? params.text : SAMPLE_TEXT;
  5537. const buffer = new TextBuffer({ text });
  5538. const editorParams = { buffer, readOnly: params.readOnly };
  5539. if (params.height != null) params.autoHeight = false;
  5540. for (const paramName of [
  5541. 'mini',
  5542. 'autoHeight',
  5543. 'autoWidth',
  5544. 'lineNumberGutterVisible',
  5545. 'showLineNumbers',
  5546. 'placeholderText',
  5547. 'softWrapped',
  5548. 'scrollSensitivity'
  5549. ]) {
  5550. if (params[paramName] != null) editorParams[paramName] = params[paramName];
  5551. }
  5552. atom.grammars.autoAssignLanguageMode(buffer);
  5553. const editor = new TextEditor(editorParams);
  5554. editor.testAutoscrollRequests = [];
  5555. editor.onDidRequestAutoscroll(request => {
  5556. editor.testAutoscrollRequests.push(request);
  5557. });
  5558. editors.push(editor);
  5559. return editor;
  5560. }
  5561. function buildComponent(params = {}) {
  5562. const editor = params.editor || buildEditor(params);
  5563. const component = new TextEditorComponent({
  5564. model: editor,
  5565. rowsPerTile: params.rowsPerTile,
  5566. updatedSynchronously: params.updatedSynchronously || false,
  5567. platform: params.platform,
  5568. chromeVersion: params.chromeVersion
  5569. });
  5570. const { element } = component;
  5571. if (!editor.getAutoHeight()) {
  5572. element.style.height = params.height ? params.height + 'px' : '600px';
  5573. }
  5574. if (!editor.getAutoWidth()) {
  5575. element.style.width = params.width ? params.width + 'px' : '800px';
  5576. }
  5577. if (params.attach !== false) jasmine.attachToDOM(element);
  5578. return { component, element, editor };
  5579. }
  5580. function getEditorWidthInBaseCharacters(component) {
  5581. return Math.round(
  5582. component.getScrollContainerWidth() / component.getBaseCharacterWidth()
  5583. );
  5584. }
  5585. async function setEditorHeightInLines(component, heightInLines) {
  5586. component.element.style.height =
  5587. component.getLineHeight() * heightInLines + 'px';
  5588. await component.getNextUpdatePromise();
  5589. }
  5590. async function setEditorWidthInCharacters(component, widthInCharacters) {
  5591. component.element.style.width =
  5592. component.getGutterContainerWidth() +
  5593. widthInCharacters * component.measurements.baseCharacterWidth +
  5594. verticalScrollbarWidth +
  5595. 'px';
  5596. await component.getNextUpdatePromise();
  5597. }
  5598. function verifyCursorPosition(component, cursorNode, row, column) {
  5599. const rect = cursorNode.getBoundingClientRect();
  5600. expect(Math.round(rect.top)).toBeNear(clientTopForLine(component, row));
  5601. expect(Math.round(rect.left)).toBe(
  5602. Math.round(clientLeftForCharacter(component, row, column))
  5603. );
  5604. }
  5605. function clientTopForLine(component, row) {
  5606. return lineNodeForScreenRow(component, row).getBoundingClientRect().top;
  5607. }
  5608. function clientLeftForCharacter(component, row, column) {
  5609. const textNodes = textNodesForScreenRow(component, row);
  5610. let textNodeStartColumn = 0;
  5611. for (const textNode of textNodes) {
  5612. const textNodeEndColumn = textNodeStartColumn + textNode.textContent.length;
  5613. if (column < textNodeEndColumn) {
  5614. const range = document.createRange();
  5615. range.setStart(textNode, column - textNodeStartColumn);
  5616. range.setEnd(textNode, column - textNodeStartColumn);
  5617. return range.getBoundingClientRect().left;
  5618. }
  5619. textNodeStartColumn = textNodeEndColumn;
  5620. }
  5621. const lastTextNode = textNodes[textNodes.length - 1];
  5622. const range = document.createRange();
  5623. range.setStart(lastTextNode, 0);
  5624. range.setEnd(lastTextNode, lastTextNode.textContent.length);
  5625. return range.getBoundingClientRect().right;
  5626. }
  5627. function clientPositionForCharacter(component, row, column) {
  5628. return {
  5629. clientX: clientLeftForCharacter(component, row, column),
  5630. clientY: clientTopForLine(component, row)
  5631. };
  5632. }
  5633. function lineNumberNodeForScreenRow(component, row) {
  5634. const gutterElement =
  5635. component.refs.gutterContainer.refs.lineNumberGutter.element;
  5636. const tileStartRow = component.tileStartRowForRow(row);
  5637. const tileIndex = component.renderedTileStartRows.indexOf(tileStartRow);
  5638. return gutterElement.children[tileIndex + 1].children[row - tileStartRow];
  5639. }
  5640. function lineNodeForScreenRow(component, row) {
  5641. const renderedScreenLine = component.renderedScreenLineForRow(row);
  5642. return component.lineComponentsByScreenLineId.get(renderedScreenLine.id)
  5643. .element;
  5644. }
  5645. function textNodesForScreenRow(component, row) {
  5646. const screenLine = component.renderedScreenLineForRow(row);
  5647. return component.lineComponentsByScreenLineId.get(screenLine.id).textNodes;
  5648. }
  5649. function setScrollTop(component, scrollTop) {
  5650. component.setScrollTop(scrollTop);
  5651. component.scheduleUpdate();
  5652. return component.getNextUpdatePromise();
  5653. }
  5654. function setScrollLeft(component, scrollLeft) {
  5655. component.setScrollLeft(scrollLeft);
  5656. component.scheduleUpdate();
  5657. return component.getNextUpdatePromise();
  5658. }
  5659. function getHorizontalScrollbarHeight(component) {
  5660. const element = component.refs.horizontalScrollbar.element;
  5661. return element.offsetHeight - element.clientHeight;
  5662. }
  5663. function getVerticalScrollbarWidth(component) {
  5664. const element = component.refs.verticalScrollbar.element;
  5665. return element.offsetWidth - element.clientWidth;
  5666. }
  5667. function assertDocumentFocused() {
  5668. if (!document.hasFocus()) {
  5669. throw new Error('The document needs to be focused to run this test');
  5670. }
  5671. }
  5672. function getElementHeight(element) {
  5673. const topRuler = document.createElement('div');
  5674. const bottomRuler = document.createElement('div');
  5675. let height;
  5676. if (document.body.contains(element)) {
  5677. element.parentElement.insertBefore(topRuler, element);
  5678. element.parentElement.insertBefore(bottomRuler, element.nextSibling);
  5679. height = bottomRuler.offsetTop - topRuler.offsetTop;
  5680. } else {
  5681. jasmine.attachToDOM(topRuler);
  5682. jasmine.attachToDOM(element);
  5683. jasmine.attachToDOM(bottomRuler);
  5684. height = bottomRuler.offsetTop - topRuler.offsetTop;
  5685. element.remove();
  5686. }
  5687. topRuler.remove();
  5688. bottomRuler.remove();
  5689. return height;
  5690. }
  5691. function queryOnScreenLineNumberElements(element) {
  5692. return Array.from(element.querySelectorAll('.line-number:not(.dummy)'));
  5693. }
  5694. function queryOnScreenLineElements(element) {
  5695. return Array.from(
  5696. element.querySelectorAll('.line:not(.dummy):not([data-off-screen])')
  5697. );
  5698. }