test_code_edit.h 215 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655
  1. /**************************************************************************/
  2. /* test_code_edit.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_CODE_EDIT_H
  31. #define TEST_CODE_EDIT_H
  32. #include "scene/gui/code_edit.h"
  33. #include "tests/test_macros.h"
  34. namespace TestCodeEdit {
  35. static inline Array build_array() {
  36. return Array();
  37. }
  38. template <typename... Targs>
  39. static inline Array build_array(Variant item, Targs... Fargs) {
  40. Array a = build_array(Fargs...);
  41. a.push_front(item);
  42. return a;
  43. }
  44. TEST_CASE("[SceneTree][CodeEdit] line gutters") {
  45. CodeEdit *code_edit = memnew(CodeEdit);
  46. SceneTree::get_singleton()->get_root()->add_child(code_edit);
  47. code_edit->grab_focus();
  48. SUBCASE("[CodeEdit] breakpoints") {
  49. SIGNAL_WATCH(code_edit, "breakpoint_toggled");
  50. SUBCASE("[CodeEdit] draw breakpoints gutter") {
  51. code_edit->set_draw_breakpoints_gutter(false);
  52. CHECK_FALSE(code_edit->is_drawing_breakpoints_gutter());
  53. code_edit->set_draw_breakpoints_gutter(true);
  54. CHECK(code_edit->is_drawing_breakpoints_gutter());
  55. }
  56. SUBCASE("[CodeEdit] set line as breakpoint") {
  57. /* Out of bounds. */
  58. ERR_PRINT_OFF;
  59. code_edit->set_line_as_breakpoint(-1, true);
  60. CHECK_FALSE(code_edit->is_line_breakpointed(-1));
  61. SIGNAL_CHECK_FALSE("breakpoint_toggled");
  62. code_edit->set_line_as_breakpoint(1, true);
  63. CHECK_FALSE(code_edit->is_line_breakpointed(1));
  64. SIGNAL_CHECK_FALSE("breakpoint_toggled");
  65. ERR_PRINT_ON;
  66. Array args = build_array(build_array(0));
  67. code_edit->set_line_as_breakpoint(0, true);
  68. CHECK(code_edit->is_line_breakpointed(0));
  69. CHECK(code_edit->get_breakpointed_lines()[0] == 0);
  70. SIGNAL_CHECK("breakpoint_toggled", args);
  71. code_edit->set_line_as_breakpoint(0, false);
  72. CHECK_FALSE(code_edit->is_line_breakpointed(0));
  73. SIGNAL_CHECK("breakpoint_toggled", args);
  74. }
  75. SUBCASE("[CodeEdit] clear breakpointed lines") {
  76. code_edit->clear_breakpointed_lines();
  77. SIGNAL_CHECK_FALSE("breakpoint_toggled");
  78. Array args = build_array(build_array(0));
  79. code_edit->set_line_as_breakpoint(0, true);
  80. CHECK(code_edit->is_line_breakpointed(0));
  81. SIGNAL_CHECK("breakpoint_toggled", args);
  82. code_edit->clear_breakpointed_lines();
  83. CHECK_FALSE(code_edit->is_line_breakpointed(0));
  84. SIGNAL_CHECK("breakpoint_toggled", args);
  85. }
  86. SUBCASE("[CodeEdit] breakpoints and set text") {
  87. Array args = build_array(build_array(0));
  88. code_edit->set_text("test\nline");
  89. code_edit->set_line_as_breakpoint(0, true);
  90. CHECK(code_edit->is_line_breakpointed(0));
  91. SIGNAL_CHECK("breakpoint_toggled", args);
  92. /* breakpoint on lines that still exist are kept. */
  93. code_edit->set_text("");
  94. MessageQueue::get_singleton()->flush();
  95. CHECK(code_edit->is_line_breakpointed(0));
  96. SIGNAL_CHECK_FALSE("breakpoint_toggled");
  97. /* breakpoint on lines that are removed should also be removed. */
  98. code_edit->clear_breakpointed_lines();
  99. SIGNAL_DISCARD("breakpoint_toggled")
  100. args = build_array(build_array(1));
  101. code_edit->set_text("test\nline");
  102. code_edit->set_line_as_breakpoint(1, true);
  103. CHECK(code_edit->is_line_breakpointed(1));
  104. SIGNAL_CHECK("breakpoint_toggled", args);
  105. code_edit->set_text("");
  106. MessageQueue::get_singleton()->flush();
  107. CHECK_FALSE(code_edit->is_line_breakpointed(0));
  108. ERR_PRINT_OFF;
  109. CHECK_FALSE(code_edit->is_line_breakpointed(1));
  110. ERR_PRINT_ON;
  111. SIGNAL_CHECK("breakpoint_toggled", args);
  112. }
  113. SUBCASE("[CodeEdit] breakpoints and clear") {
  114. Array args = build_array(build_array(0));
  115. code_edit->set_text("test\nline");
  116. code_edit->set_line_as_breakpoint(0, true);
  117. CHECK(code_edit->is_line_breakpointed(0));
  118. SIGNAL_CHECK("breakpoint_toggled", args);
  119. /* breakpoint on lines that still exist are removed. */
  120. code_edit->clear();
  121. MessageQueue::get_singleton()->flush();
  122. CHECK_FALSE(code_edit->is_line_breakpointed(0));
  123. SIGNAL_CHECK("breakpoint_toggled", args);
  124. /* breakpoint on lines that are removed should also be removed. */
  125. code_edit->clear_breakpointed_lines();
  126. SIGNAL_DISCARD("breakpoint_toggled")
  127. args = build_array(build_array(1));
  128. code_edit->set_text("test\nline");
  129. code_edit->set_line_as_breakpoint(1, true);
  130. CHECK(code_edit->is_line_breakpointed(1));
  131. SIGNAL_CHECK("breakpoint_toggled", args);
  132. code_edit->clear();
  133. MessageQueue::get_singleton()->flush();
  134. CHECK_FALSE(code_edit->is_line_breakpointed(0));
  135. ERR_PRINT_OFF;
  136. CHECK_FALSE(code_edit->is_line_breakpointed(1));
  137. ERR_PRINT_ON;
  138. SIGNAL_CHECK("breakpoint_toggled", args);
  139. }
  140. SUBCASE("[CodeEdit] breakpoints and new lines no text") {
  141. Array args = build_array(build_array(0));
  142. /* No text moves breakpoint. */
  143. code_edit->set_line_as_breakpoint(0, true);
  144. CHECK(code_edit->is_line_breakpointed(0));
  145. SIGNAL_CHECK("breakpoint_toggled", args);
  146. // Normal.
  147. args = build_array(build_array(0), build_array(1));
  148. SEND_GUI_ACTION("ui_text_newline");
  149. CHECK(code_edit->get_line_count() == 2);
  150. CHECK_FALSE(code_edit->is_line_breakpointed(0));
  151. CHECK(code_edit->is_line_breakpointed(1));
  152. SIGNAL_CHECK("breakpoint_toggled", args);
  153. // Non-Breaking.
  154. args = build_array(build_array(1), build_array(2));
  155. SEND_GUI_ACTION("ui_text_newline_blank");
  156. CHECK(code_edit->get_line_count() == 3);
  157. CHECK_FALSE(code_edit->is_line_breakpointed(1));
  158. CHECK(code_edit->is_line_breakpointed(2));
  159. SIGNAL_CHECK("breakpoint_toggled", args);
  160. // Above.
  161. args = build_array(build_array(2), build_array(3));
  162. SEND_GUI_ACTION("ui_text_newline_above");
  163. CHECK(code_edit->get_line_count() == 4);
  164. CHECK_FALSE(code_edit->is_line_breakpointed(2));
  165. CHECK(code_edit->is_line_breakpointed(3));
  166. SIGNAL_CHECK("breakpoint_toggled", args);
  167. }
  168. SUBCASE("[CodeEdit] breakpoints and new lines with text") {
  169. Array args = build_array(build_array(0));
  170. /* Having text does not move breakpoint. */
  171. code_edit->insert_text_at_caret("text");
  172. code_edit->set_line_as_breakpoint(0, true);
  173. CHECK(code_edit->is_line_breakpointed(0));
  174. SIGNAL_CHECK("breakpoint_toggled", args);
  175. /* Normal. */
  176. SEND_GUI_ACTION("ui_text_newline");
  177. CHECK(code_edit->get_line_count() == 2);
  178. CHECK(code_edit->is_line_breakpointed(0));
  179. CHECK_FALSE(code_edit->is_line_breakpointed(1));
  180. SIGNAL_CHECK_FALSE("breakpoint_toggled");
  181. /* Non-Breaking. */
  182. code_edit->set_caret_line(0);
  183. SEND_GUI_ACTION("ui_text_newline_blank");
  184. CHECK(code_edit->get_line_count() == 3);
  185. CHECK(code_edit->is_line_breakpointed(0));
  186. CHECK_FALSE(code_edit->is_line_breakpointed(1));
  187. SIGNAL_CHECK_FALSE("breakpoint_toggled");
  188. // Above does move.
  189. args = build_array(build_array(0), build_array(1));
  190. code_edit->set_caret_line(0);
  191. SEND_GUI_ACTION("ui_text_newline_above");
  192. CHECK(code_edit->get_line_count() == 4);
  193. CHECK_FALSE(code_edit->is_line_breakpointed(0));
  194. CHECK(code_edit->is_line_breakpointed(1));
  195. SIGNAL_CHECK("breakpoint_toggled", args);
  196. }
  197. SUBCASE("[CodeEdit] breakpoints and backspace") {
  198. Array args = build_array(build_array(1));
  199. code_edit->set_text("\n\n");
  200. code_edit->set_line_as_breakpoint(1, true);
  201. CHECK(code_edit->is_line_breakpointed(1));
  202. SIGNAL_CHECK("breakpoint_toggled", args);
  203. code_edit->set_caret_line(2);
  204. /* backspace onto line does not remove breakpoint */
  205. SEND_GUI_ACTION("ui_text_backspace");
  206. CHECK(code_edit->is_line_breakpointed(1));
  207. SIGNAL_CHECK_FALSE("breakpoint_toggled");
  208. /* backspace on breakpointed line removes it */
  209. SEND_GUI_ACTION("ui_text_backspace");
  210. CHECK_FALSE(code_edit->is_line_breakpointed(0));
  211. ERR_PRINT_OFF;
  212. CHECK_FALSE(code_edit->is_line_breakpointed(1));
  213. ERR_PRINT_ON;
  214. SIGNAL_CHECK("breakpoint_toggled", args);
  215. // Backspace above breakpointed line moves it.
  216. args = build_array(build_array(2));
  217. code_edit->set_text("\n\n");
  218. code_edit->set_line_as_breakpoint(2, true);
  219. CHECK(code_edit->is_line_breakpointed(2));
  220. SIGNAL_CHECK("breakpoint_toggled", args);
  221. code_edit->set_caret_line(1);
  222. args = build_array(build_array(2), build_array(1));
  223. SEND_GUI_ACTION("ui_text_backspace");
  224. ERR_PRINT_OFF;
  225. CHECK_FALSE(code_edit->is_line_breakpointed(2));
  226. ERR_PRINT_ON;
  227. CHECK(code_edit->is_line_breakpointed(1));
  228. SIGNAL_CHECK("breakpoint_toggled", args);
  229. }
  230. SUBCASE("[CodeEdit] breakpoints and delete") {
  231. Array args = build_array(build_array(1));
  232. code_edit->set_text("\n\n");
  233. code_edit->set_line_as_breakpoint(1, true);
  234. CHECK(code_edit->is_line_breakpointed(1));
  235. SIGNAL_CHECK("breakpoint_toggled", args);
  236. code_edit->set_caret_line(1);
  237. /* Delete onto breakpointed lines does not remove it. */
  238. SEND_GUI_ACTION("ui_text_delete");
  239. CHECK(code_edit->get_line_count() == 2);
  240. CHECK(code_edit->is_line_breakpointed(1));
  241. SIGNAL_CHECK_FALSE("breakpoint_toggled");
  242. /* Delete moving breakpointed line up removes it. */
  243. code_edit->set_caret_line(0);
  244. SEND_GUI_ACTION("ui_text_delete");
  245. CHECK(code_edit->get_line_count() == 1);
  246. ERR_PRINT_OFF;
  247. CHECK_FALSE(code_edit->is_line_breakpointed(1));
  248. ERR_PRINT_ON;
  249. SIGNAL_CHECK("breakpoint_toggled", args);
  250. // Delete above breakpointed line moves it.
  251. args = build_array(build_array(2));
  252. code_edit->set_text("\n\n");
  253. code_edit->set_line_as_breakpoint(2, true);
  254. CHECK(code_edit->is_line_breakpointed(2));
  255. SIGNAL_CHECK("breakpoint_toggled", args);
  256. code_edit->set_caret_line(0);
  257. args = build_array(build_array(2), build_array(1));
  258. SEND_GUI_ACTION("ui_text_delete");
  259. ERR_PRINT_OFF;
  260. CHECK_FALSE(code_edit->is_line_breakpointed(2));
  261. ERR_PRINT_ON;
  262. CHECK(code_edit->is_line_breakpointed(1));
  263. SIGNAL_CHECK("breakpoint_toggled", args);
  264. }
  265. SUBCASE("[CodeEdit] breakpoints and delete selection") {
  266. Array args = build_array(build_array(1));
  267. code_edit->set_text("\n\n");
  268. code_edit->set_line_as_breakpoint(1, true);
  269. CHECK(code_edit->is_line_breakpointed(1));
  270. SIGNAL_CHECK("breakpoint_toggled", args);
  271. code_edit->select(0, 0, 2, 0);
  272. code_edit->delete_selection();
  273. MessageQueue::get_singleton()->flush();
  274. CHECK_FALSE(code_edit->is_line_breakpointed(0));
  275. SIGNAL_CHECK("breakpoint_toggled", args);
  276. // Should handle breakpoint move when deleting selection by adding less text then removed.
  277. args = build_array(build_array(9));
  278. code_edit->set_text("\n\n\n\n\n\n\n\n\n");
  279. code_edit->set_line_as_breakpoint(9, true);
  280. CHECK(code_edit->is_line_breakpointed(9));
  281. SIGNAL_CHECK("breakpoint_toggled", args);
  282. code_edit->select(0, 0, 6, 0);
  283. args = build_array(build_array(9), build_array(4));
  284. SEND_GUI_ACTION("ui_text_newline");
  285. ERR_PRINT_OFF;
  286. CHECK_FALSE(code_edit->is_line_breakpointed(9));
  287. ERR_PRINT_ON;
  288. CHECK(code_edit->is_line_breakpointed(4));
  289. SIGNAL_CHECK("breakpoint_toggled", args);
  290. // Should handle breakpoint move when deleting selection by adding more text then removed.
  291. args = build_array(build_array(9), build_array(14));
  292. code_edit->insert_text_at_caret("\n\n\n\n\n");
  293. MessageQueue::get_singleton()->flush();
  294. SIGNAL_DISCARD("breakpoint_toggled")
  295. CHECK(code_edit->is_line_breakpointed(9));
  296. code_edit->select(0, 0, 6, 0);
  297. code_edit->insert_text_at_caret("\n\n\n\n\n\n\n\n\n\n\n");
  298. MessageQueue::get_singleton()->flush();
  299. CHECK(code_edit->is_line_breakpointed(14));
  300. SIGNAL_CHECK("breakpoint_toggled", args);
  301. }
  302. SUBCASE("[CodeEdit] breakpoints and undo") {
  303. Array args = build_array(build_array(1));
  304. code_edit->set_text("\n\n");
  305. code_edit->set_line_as_breakpoint(1, true);
  306. CHECK(code_edit->is_line_breakpointed(1));
  307. SIGNAL_CHECK("breakpoint_toggled", args);
  308. code_edit->select(0, 0, 2, 0);
  309. code_edit->delete_selection();
  310. MessageQueue::get_singleton()->flush();
  311. CHECK_FALSE(code_edit->is_line_breakpointed(0));
  312. SIGNAL_CHECK("breakpoint_toggled", args);
  313. /* Undo does not restore breakpoint. */
  314. code_edit->undo();
  315. CHECK_FALSE(code_edit->is_line_breakpointed(1));
  316. SIGNAL_CHECK_FALSE("breakpoint_toggled");
  317. }
  318. SIGNAL_UNWATCH(code_edit, "breakpoint_toggled");
  319. }
  320. SUBCASE("[CodeEdit] bookmarks") {
  321. SUBCASE("[CodeEdit] draw bookmarks gutter") {
  322. code_edit->set_draw_bookmarks_gutter(false);
  323. CHECK_FALSE(code_edit->is_drawing_bookmarks_gutter());
  324. code_edit->set_draw_bookmarks_gutter(true);
  325. CHECK(code_edit->is_drawing_bookmarks_gutter());
  326. }
  327. SUBCASE("[CodeEdit] set line as bookmarks") {
  328. /* Out of bounds. */
  329. ERR_PRINT_OFF;
  330. code_edit->set_line_as_bookmarked(-1, true);
  331. CHECK_FALSE(code_edit->is_line_bookmarked(-1));
  332. code_edit->set_line_as_bookmarked(1, true);
  333. CHECK_FALSE(code_edit->is_line_bookmarked(1));
  334. ERR_PRINT_ON;
  335. code_edit->set_line_as_bookmarked(0, true);
  336. CHECK(code_edit->get_bookmarked_lines()[0] == 0);
  337. CHECK(code_edit->is_line_bookmarked(0));
  338. code_edit->set_line_as_bookmarked(0, false);
  339. CHECK_FALSE(code_edit->is_line_bookmarked(0));
  340. }
  341. SUBCASE("[CodeEdit] clear bookmarked lines") {
  342. code_edit->clear_bookmarked_lines();
  343. code_edit->set_line_as_bookmarked(0, true);
  344. CHECK(code_edit->is_line_bookmarked(0));
  345. code_edit->clear_bookmarked_lines();
  346. CHECK_FALSE(code_edit->is_line_bookmarked(0));
  347. }
  348. SUBCASE("[CodeEdit] bookmarks and set text") {
  349. code_edit->set_text("test\nline");
  350. code_edit->set_line_as_bookmarked(0, true);
  351. CHECK(code_edit->is_line_bookmarked(0));
  352. /* bookmarks on lines that still exist are kept. */
  353. code_edit->set_text("");
  354. MessageQueue::get_singleton()->flush();
  355. CHECK(code_edit->is_line_bookmarked(0));
  356. /* bookmarks on lines that are removed should also be removed. */
  357. code_edit->clear_bookmarked_lines();
  358. code_edit->set_text("test\nline");
  359. code_edit->set_line_as_bookmarked(1, true);
  360. CHECK(code_edit->is_line_bookmarked(1));
  361. code_edit->set_text("");
  362. MessageQueue::get_singleton()->flush();
  363. CHECK_FALSE(code_edit->is_line_bookmarked(0));
  364. ERR_PRINT_OFF;
  365. CHECK_FALSE(code_edit->is_line_bookmarked(1));
  366. ERR_PRINT_ON;
  367. }
  368. SUBCASE("[CodeEdit] bookmarks and clear") {
  369. code_edit->set_text("test\nline");
  370. code_edit->set_line_as_bookmarked(0, true);
  371. CHECK(code_edit->is_line_bookmarked(0));
  372. /* bookmarks on lines that still exist are removed. */
  373. code_edit->clear();
  374. MessageQueue::get_singleton()->flush();
  375. CHECK_FALSE(code_edit->is_line_bookmarked(0));
  376. /* bookmarks on lines that are removed should also be removed. */
  377. code_edit->clear_bookmarked_lines();
  378. code_edit->set_text("test\nline");
  379. code_edit->set_line_as_bookmarked(1, true);
  380. CHECK(code_edit->is_line_bookmarked(1));
  381. code_edit->clear();
  382. MessageQueue::get_singleton()->flush();
  383. CHECK_FALSE(code_edit->is_line_bookmarked(0));
  384. ERR_PRINT_OFF;
  385. CHECK_FALSE(code_edit->is_line_bookmarked(1));
  386. ERR_PRINT_ON;
  387. }
  388. SUBCASE("[CodeEdit] bookmarks and new lines no text") {
  389. /* No text moves bookmarks. */
  390. code_edit->set_line_as_bookmarked(0, true);
  391. CHECK(code_edit->is_line_bookmarked(0));
  392. /* Normal. */
  393. SEND_GUI_ACTION("ui_text_newline");
  394. CHECK(code_edit->get_line_count() == 2);
  395. CHECK_FALSE(code_edit->is_line_bookmarked(0));
  396. CHECK(code_edit->is_line_bookmarked(1));
  397. /* Non-Breaking. */
  398. SEND_GUI_ACTION("ui_text_newline_blank");
  399. CHECK(code_edit->get_line_count() == 3);
  400. CHECK_FALSE(code_edit->is_line_bookmarked(1));
  401. CHECK(code_edit->is_line_bookmarked(2));
  402. /* Above. */
  403. SEND_GUI_ACTION("ui_text_newline_above");
  404. CHECK(code_edit->get_line_count() == 4);
  405. CHECK_FALSE(code_edit->is_line_bookmarked(2));
  406. CHECK(code_edit->is_line_bookmarked(3));
  407. }
  408. SUBCASE("[CodeEdit] bookmarks and new lines with text") {
  409. /* Having text does not move bookmark. */
  410. code_edit->insert_text_at_caret("text");
  411. code_edit->set_line_as_bookmarked(0, true);
  412. CHECK(code_edit->is_line_bookmarked(0));
  413. /* Normal. */
  414. SEND_GUI_ACTION("ui_text_newline");
  415. CHECK(code_edit->get_line_count() == 2);
  416. CHECK(code_edit->is_line_bookmarked(0));
  417. CHECK_FALSE(code_edit->is_line_bookmarked(1));
  418. /* Non-Breaking. */
  419. code_edit->set_caret_line(0);
  420. SEND_GUI_ACTION("ui_text_newline_blank");
  421. CHECK(code_edit->get_line_count() == 3);
  422. CHECK(code_edit->is_line_bookmarked(0));
  423. CHECK_FALSE(code_edit->is_line_bookmarked(1));
  424. /* Above does move. */
  425. code_edit->set_caret_line(0);
  426. SEND_GUI_ACTION("ui_text_newline_above");
  427. CHECK(code_edit->get_line_count() == 4);
  428. CHECK_FALSE(code_edit->is_line_bookmarked(0));
  429. CHECK(code_edit->is_line_bookmarked(1));
  430. }
  431. SUBCASE("[CodeEdit] bookmarks and backspace") {
  432. code_edit->set_text("\n\n");
  433. code_edit->set_line_as_bookmarked(1, true);
  434. CHECK(code_edit->is_line_bookmarked(1));
  435. code_edit->set_caret_line(2);
  436. /* backspace onto line does not remove bookmark */
  437. SEND_GUI_ACTION("ui_text_backspace");
  438. CHECK(code_edit->is_line_bookmarked(1));
  439. /* backspace on bookmarked line removes it */
  440. SEND_GUI_ACTION("ui_text_backspace");
  441. CHECK_FALSE(code_edit->is_line_bookmarked(0));
  442. ERR_PRINT_OFF;
  443. CHECK_FALSE(code_edit->is_line_bookmarked(1));
  444. ERR_PRINT_ON;
  445. }
  446. SUBCASE("[CodeEdit] bookmarks and delete") {
  447. code_edit->set_text("\n\n");
  448. code_edit->set_line_as_bookmarked(1, true);
  449. CHECK(code_edit->is_line_bookmarked(1));
  450. code_edit->set_caret_line(1);
  451. /* Delete onto bookmarked lines does not remove it. */
  452. SEND_GUI_ACTION("ui_text_delete");
  453. CHECK(code_edit->get_line_count() == 2);
  454. CHECK(code_edit->is_line_bookmarked(1));
  455. /* Delete moving bookmarked line up removes it. */
  456. code_edit->set_caret_line(0);
  457. SEND_GUI_ACTION("ui_text_delete");
  458. CHECK(code_edit->get_line_count() == 1);
  459. ERR_PRINT_OFF;
  460. CHECK_FALSE(code_edit->is_line_bookmarked(1));
  461. ERR_PRINT_ON;
  462. }
  463. SUBCASE("[CodeEdit] bookmarks and delete selection") {
  464. code_edit->set_text("\n\n");
  465. code_edit->set_line_as_bookmarked(1, true);
  466. CHECK(code_edit->is_line_bookmarked(1));
  467. code_edit->select(0, 0, 2, 0);
  468. code_edit->delete_selection();
  469. MessageQueue::get_singleton()->flush();
  470. CHECK_FALSE(code_edit->is_line_bookmarked(0));
  471. }
  472. SUBCASE("[CodeEdit] bookmarks and undo") {
  473. code_edit->set_text("\n\n");
  474. code_edit->set_line_as_bookmarked(1, true);
  475. CHECK(code_edit->is_line_bookmarked(1));
  476. code_edit->select(0, 0, 2, 0);
  477. code_edit->delete_selection();
  478. MessageQueue::get_singleton()->flush();
  479. CHECK_FALSE(code_edit->is_line_bookmarked(0));
  480. /* Undo does not restore bookmark. */
  481. code_edit->undo();
  482. CHECK_FALSE(code_edit->is_line_bookmarked(1));
  483. }
  484. }
  485. SUBCASE("[CodeEdit] executing lines") {
  486. SUBCASE("[CodeEdit] draw executing lines gutter") {
  487. code_edit->set_draw_executing_lines_gutter(false);
  488. CHECK_FALSE(code_edit->is_drawing_executing_lines_gutter());
  489. code_edit->set_draw_executing_lines_gutter(true);
  490. CHECK(code_edit->is_drawing_executing_lines_gutter());
  491. }
  492. SUBCASE("[CodeEdit] set line as executing lines") {
  493. /* Out of bounds. */
  494. ERR_PRINT_OFF;
  495. code_edit->set_line_as_executing(-1, true);
  496. CHECK_FALSE(code_edit->is_line_executing(-1));
  497. code_edit->set_line_as_executing(1, true);
  498. CHECK_FALSE(code_edit->is_line_executing(1));
  499. ERR_PRINT_ON;
  500. code_edit->set_line_as_executing(0, true);
  501. CHECK(code_edit->get_executing_lines()[0] == 0);
  502. CHECK(code_edit->is_line_executing(0));
  503. code_edit->set_line_as_executing(0, false);
  504. CHECK_FALSE(code_edit->is_line_executing(0));
  505. }
  506. SUBCASE("[CodeEdit] clear executing lines lines") {
  507. code_edit->clear_executing_lines();
  508. code_edit->set_line_as_executing(0, true);
  509. CHECK(code_edit->is_line_executing(0));
  510. code_edit->clear_executing_lines();
  511. CHECK_FALSE(code_edit->is_line_executing(0));
  512. }
  513. SUBCASE("[CodeEdit] executing lines and set text") {
  514. code_edit->set_text("test\nline");
  515. code_edit->set_line_as_executing(0, true);
  516. CHECK(code_edit->is_line_executing(0));
  517. /* executing on lines that still exist are kept. */
  518. code_edit->set_text("");
  519. MessageQueue::get_singleton()->flush();
  520. CHECK(code_edit->is_line_executing(0));
  521. /* executing on lines that are removed should also be removed. */
  522. code_edit->clear_executing_lines();
  523. code_edit->set_text("test\nline");
  524. code_edit->set_line_as_executing(1, true);
  525. CHECK(code_edit->is_line_executing(1));
  526. code_edit->set_text("");
  527. MessageQueue::get_singleton()->flush();
  528. CHECK_FALSE(code_edit->is_line_executing(0));
  529. ERR_PRINT_OFF;
  530. CHECK_FALSE(code_edit->is_line_executing(1));
  531. ERR_PRINT_ON;
  532. }
  533. SUBCASE("[CodeEdit] executing lines and clear") {
  534. code_edit->set_text("test\nline");
  535. code_edit->set_line_as_executing(0, true);
  536. CHECK(code_edit->is_line_executing(0));
  537. /* executing on lines that still exist are removed. */
  538. code_edit->clear();
  539. MessageQueue::get_singleton()->flush();
  540. CHECK_FALSE(code_edit->is_line_executing(0));
  541. /* executing on lines that are removed should also be removed. */
  542. code_edit->clear_executing_lines();
  543. code_edit->set_text("test\nline");
  544. code_edit->set_line_as_executing(1, true);
  545. CHECK(code_edit->is_line_executing(1));
  546. code_edit->clear();
  547. MessageQueue::get_singleton()->flush();
  548. CHECK_FALSE(code_edit->is_line_executing(0));
  549. ERR_PRINT_OFF;
  550. CHECK_FALSE(code_edit->is_line_executing(1));
  551. ERR_PRINT_ON;
  552. }
  553. SUBCASE("[CodeEdit] executing lines and new lines no text") {
  554. /* No text moves executing lines. */
  555. code_edit->set_line_as_executing(0, true);
  556. CHECK(code_edit->is_line_executing(0));
  557. /* Normal. */
  558. SEND_GUI_ACTION("ui_text_newline");
  559. CHECK(code_edit->get_line_count() == 2);
  560. CHECK_FALSE(code_edit->is_line_executing(0));
  561. CHECK(code_edit->is_line_executing(1));
  562. /* Non-Breaking. */
  563. SEND_GUI_ACTION("ui_text_newline_blank");
  564. CHECK(code_edit->get_line_count() == 3);
  565. CHECK_FALSE(code_edit->is_line_executing(1));
  566. CHECK(code_edit->is_line_executing(2));
  567. /* Above. */
  568. SEND_GUI_ACTION("ui_text_newline_above");
  569. CHECK(code_edit->get_line_count() == 4);
  570. CHECK_FALSE(code_edit->is_line_executing(2));
  571. CHECK(code_edit->is_line_executing(3));
  572. }
  573. SUBCASE("[CodeEdit] executing lines and new lines with text") {
  574. /* Having text does not move executing lines. */
  575. code_edit->insert_text_at_caret("text");
  576. code_edit->set_line_as_executing(0, true);
  577. CHECK(code_edit->is_line_executing(0));
  578. /* Normal. */
  579. SEND_GUI_ACTION("ui_text_newline");
  580. CHECK(code_edit->get_line_count() == 2);
  581. CHECK(code_edit->is_line_executing(0));
  582. CHECK_FALSE(code_edit->is_line_executing(1));
  583. /* Non-Breaking. */
  584. code_edit->set_caret_line(0);
  585. SEND_GUI_ACTION("ui_text_newline_blank");
  586. CHECK(code_edit->get_line_count() == 3);
  587. CHECK(code_edit->is_line_executing(0));
  588. CHECK_FALSE(code_edit->is_line_executing(1));
  589. /* Above does move. */
  590. code_edit->set_caret_line(0);
  591. SEND_GUI_ACTION("ui_text_newline_above");
  592. CHECK(code_edit->get_line_count() == 4);
  593. CHECK_FALSE(code_edit->is_line_executing(0));
  594. CHECK(code_edit->is_line_executing(1));
  595. }
  596. SUBCASE("[CodeEdit] executing lines and backspace") {
  597. code_edit->set_text("\n\n");
  598. code_edit->set_line_as_executing(1, true);
  599. CHECK(code_edit->is_line_executing(1));
  600. code_edit->set_caret_line(2);
  601. /* backspace onto line does not remove executing lines. */
  602. SEND_GUI_ACTION("ui_text_backspace");
  603. CHECK(code_edit->is_line_executing(1));
  604. /* backspace on executing line removes it */
  605. SEND_GUI_ACTION("ui_text_backspace");
  606. CHECK_FALSE(code_edit->is_line_executing(0));
  607. ERR_PRINT_OFF;
  608. CHECK_FALSE(code_edit->is_line_executing(1));
  609. ERR_PRINT_ON;
  610. }
  611. SUBCASE("[CodeEdit] executing lines and delete") {
  612. code_edit->set_text("\n\n");
  613. code_edit->set_line_as_executing(1, true);
  614. CHECK(code_edit->is_line_executing(1));
  615. code_edit->set_caret_line(1);
  616. /* Delete onto executing lines does not remove it. */
  617. SEND_GUI_ACTION("ui_text_delete");
  618. CHECK(code_edit->get_line_count() == 2);
  619. CHECK(code_edit->is_line_executing(1));
  620. /* Delete moving executing line up removes it. */
  621. code_edit->set_caret_line(0);
  622. SEND_GUI_ACTION("ui_text_delete");
  623. CHECK(code_edit->get_line_count() == 1);
  624. ERR_PRINT_OFF;
  625. CHECK_FALSE(code_edit->is_line_executing(1));
  626. ERR_PRINT_ON;
  627. }
  628. SUBCASE("[CodeEdit] executing lines and delete selection") {
  629. code_edit->set_text("\n\n");
  630. code_edit->set_line_as_executing(1, true);
  631. CHECK(code_edit->is_line_executing(1));
  632. code_edit->select(0, 0, 2, 0);
  633. code_edit->delete_selection();
  634. MessageQueue::get_singleton()->flush();
  635. CHECK_FALSE(code_edit->is_line_executing(0));
  636. }
  637. SUBCASE("[CodeEdit] executing lines and undo") {
  638. code_edit->set_text("\n\n");
  639. code_edit->set_line_as_executing(1, true);
  640. CHECK(code_edit->is_line_executing(1));
  641. code_edit->select(0, 0, 2, 0);
  642. code_edit->delete_selection();
  643. MessageQueue::get_singleton()->flush();
  644. CHECK_FALSE(code_edit->is_line_executing(0));
  645. /* Undo does not restore executing lines. */
  646. code_edit->undo();
  647. CHECK_FALSE(code_edit->is_line_executing(1));
  648. }
  649. }
  650. SUBCASE("[CodeEdit] line numbers") {
  651. SUBCASE("[CodeEdit] draw line numbers gutter and padding") {
  652. code_edit->set_draw_line_numbers(false);
  653. CHECK_FALSE(code_edit->is_draw_line_numbers_enabled());
  654. code_edit->set_draw_line_numbers(true);
  655. CHECK(code_edit->is_draw_line_numbers_enabled());
  656. code_edit->set_line_numbers_zero_padded(false);
  657. CHECK_FALSE(code_edit->is_line_numbers_zero_padded());
  658. code_edit->set_line_numbers_zero_padded(true);
  659. CHECK(code_edit->is_line_numbers_zero_padded());
  660. code_edit->set_line_numbers_zero_padded(false);
  661. CHECK_FALSE(code_edit->is_line_numbers_zero_padded());
  662. code_edit->set_draw_line_numbers(false);
  663. CHECK_FALSE(code_edit->is_draw_line_numbers_enabled());
  664. code_edit->set_line_numbers_zero_padded(true);
  665. CHECK(code_edit->is_line_numbers_zero_padded());
  666. }
  667. }
  668. SUBCASE("[CodeEdit] line folding") {
  669. SUBCASE("[CodeEdit] draw line folding gutter") {
  670. code_edit->set_draw_fold_gutter(false);
  671. CHECK_FALSE(code_edit->is_drawing_fold_gutter());
  672. code_edit->set_draw_fold_gutter(true);
  673. CHECK(code_edit->is_drawing_fold_gutter());
  674. }
  675. }
  676. memdelete(code_edit);
  677. }
  678. TEST_CASE("[SceneTree][CodeEdit] delimiters") {
  679. CodeEdit *code_edit = memnew(CodeEdit);
  680. SceneTree::get_singleton()->get_root()->add_child(code_edit);
  681. code_edit->grab_focus();
  682. const Point2 OUTSIDE_DELIMETER = Point2(-1, -1);
  683. code_edit->clear_string_delimiters();
  684. code_edit->clear_comment_delimiters();
  685. SUBCASE("[CodeEdit] add and remove delimiters") {
  686. SUBCASE("[CodeEdit] add and remove string delimiters") {
  687. /* Add a delimiter.*/
  688. code_edit->add_string_delimiter("\"", "\"", false);
  689. CHECK(code_edit->has_string_delimiter("\""));
  690. CHECK(code_edit->get_string_delimiters().size() == 1);
  691. ERR_PRINT_OFF;
  692. /* Adding a duplicate start key is not allowed. */
  693. code_edit->add_string_delimiter("\"", "\'", false);
  694. CHECK(code_edit->get_string_delimiters().size() == 1);
  695. /* Adding a duplicate end key is allowed. */
  696. code_edit->add_string_delimiter("'", "\"", false);
  697. CHECK(code_edit->has_string_delimiter("'"));
  698. CHECK(code_edit->get_string_delimiters().size() == 2);
  699. /* Both start and end keys have to be symbols. */
  700. code_edit->add_string_delimiter("f", "\"", false);
  701. CHECK_FALSE(code_edit->has_string_delimiter("f"));
  702. CHECK(code_edit->get_string_delimiters().size() == 2);
  703. code_edit->add_string_delimiter("f", "\"", false);
  704. CHECK_FALSE(code_edit->has_string_delimiter("f"));
  705. CHECK(code_edit->get_string_delimiters().size() == 2);
  706. code_edit->add_string_delimiter("@", "f", false);
  707. CHECK_FALSE(code_edit->has_string_delimiter("@"));
  708. CHECK(code_edit->get_string_delimiters().size() == 2);
  709. code_edit->add_string_delimiter("f", "f", false);
  710. CHECK_FALSE(code_edit->has_string_delimiter("f"));
  711. CHECK(code_edit->get_string_delimiters().size() == 2);
  712. /* Blank start keys are not allowed */
  713. code_edit->add_string_delimiter("", "#", false);
  714. CHECK_FALSE(code_edit->has_string_delimiter("#"));
  715. CHECK(code_edit->get_string_delimiters().size() == 2);
  716. ERR_PRINT_ON;
  717. /* Blank end keys are allowed. */
  718. code_edit->add_string_delimiter("#", "", false);
  719. CHECK(code_edit->has_string_delimiter("#"));
  720. CHECK(code_edit->get_string_delimiters().size() == 3);
  721. /* Remove a delimiter. */
  722. code_edit->remove_string_delimiter("#");
  723. CHECK_FALSE(code_edit->has_string_delimiter("#"));
  724. CHECK(code_edit->get_string_delimiters().size() == 2);
  725. /* Set should override existing, and test multiline */
  726. TypedArray<String> delimiters;
  727. delimiters.push_back("^^ ^^");
  728. code_edit->set_string_delimiters(delimiters);
  729. CHECK_FALSE(code_edit->has_string_delimiter("\""));
  730. CHECK(code_edit->has_string_delimiter("^^"));
  731. CHECK(code_edit->get_string_delimiters().size() == 1);
  732. /* clear should remove all. */
  733. code_edit->clear_string_delimiters();
  734. CHECK_FALSE(code_edit->has_string_delimiter("^^"));
  735. CHECK(code_edit->get_string_delimiters().size() == 0);
  736. }
  737. SUBCASE("[CodeEdit] add and remove comment delimiters") {
  738. /* Add a delimiter.*/
  739. code_edit->add_comment_delimiter("\"", "\"", false);
  740. CHECK(code_edit->has_comment_delimiter("\""));
  741. CHECK(code_edit->get_comment_delimiters().size() == 1);
  742. ERR_PRINT_OFF;
  743. /* Adding a duplicate start key is not allowed. */
  744. code_edit->add_comment_delimiter("\"", "\'", false);
  745. CHECK(code_edit->get_comment_delimiters().size() == 1);
  746. /* Adding a duplicate end key is allowed. */
  747. code_edit->add_comment_delimiter("'", "\"", false);
  748. CHECK(code_edit->has_comment_delimiter("'"));
  749. CHECK(code_edit->get_comment_delimiters().size() == 2);
  750. /* Both start and end keys have to be symbols. */
  751. code_edit->add_comment_delimiter("f", "\"", false);
  752. CHECK_FALSE(code_edit->has_comment_delimiter("f"));
  753. CHECK(code_edit->get_comment_delimiters().size() == 2);
  754. code_edit->add_comment_delimiter("f", "\"", false);
  755. CHECK_FALSE(code_edit->has_comment_delimiter("f"));
  756. CHECK(code_edit->get_comment_delimiters().size() == 2);
  757. code_edit->add_comment_delimiter("@", "f", false);
  758. CHECK_FALSE(code_edit->has_comment_delimiter("@"));
  759. CHECK(code_edit->get_comment_delimiters().size() == 2);
  760. code_edit->add_comment_delimiter("f", "f", false);
  761. CHECK_FALSE(code_edit->has_comment_delimiter("f"));
  762. CHECK(code_edit->get_comment_delimiters().size() == 2);
  763. /* Blank start keys are not allowed. */
  764. code_edit->add_comment_delimiter("", "#", false);
  765. CHECK_FALSE(code_edit->has_comment_delimiter("#"));
  766. CHECK(code_edit->get_comment_delimiters().size() == 2);
  767. ERR_PRINT_ON;
  768. /* Blank end keys are allowed. */
  769. code_edit->add_comment_delimiter("#", "", false);
  770. CHECK(code_edit->has_comment_delimiter("#"));
  771. CHECK(code_edit->get_comment_delimiters().size() == 3);
  772. /* Remove a delimiter. */
  773. code_edit->remove_comment_delimiter("#");
  774. CHECK_FALSE(code_edit->has_comment_delimiter("#"));
  775. CHECK(code_edit->get_comment_delimiters().size() == 2);
  776. /* Set should override existing, and test multiline. */
  777. TypedArray<String> delimiters;
  778. delimiters.push_back("^^ ^^");
  779. code_edit->set_comment_delimiters(delimiters);
  780. CHECK_FALSE(code_edit->has_comment_delimiter("\""));
  781. CHECK(code_edit->has_comment_delimiter("^^"));
  782. CHECK(code_edit->get_comment_delimiters().size() == 1);
  783. /* clear should remove all. */
  784. code_edit->clear_comment_delimiters();
  785. CHECK_FALSE(code_edit->has_comment_delimiter("^^"));
  786. CHECK(code_edit->get_comment_delimiters().size() == 0);
  787. }
  788. SUBCASE("[CodeEdit] add and remove mixed delimiters") {
  789. code_edit->add_comment_delimiter("#", "", false);
  790. CHECK(code_edit->has_comment_delimiter("#"));
  791. CHECK(code_edit->get_comment_delimiters().size() == 1);
  792. ERR_PRINT_OFF;
  793. /* Disallow adding a string with the same start key as comment. */
  794. code_edit->add_string_delimiter("#", "", false);
  795. CHECK_FALSE(code_edit->has_string_delimiter("#"));
  796. CHECK(code_edit->get_string_delimiters().size() == 0);
  797. code_edit->add_string_delimiter("\"", "\"", false);
  798. CHECK(code_edit->has_string_delimiter("\""));
  799. CHECK(code_edit->get_comment_delimiters().size() == 1);
  800. /* Disallow adding a comment with the same start key as string. */
  801. code_edit->add_comment_delimiter("\"", "", false);
  802. CHECK_FALSE(code_edit->has_comment_delimiter("\""));
  803. CHECK(code_edit->get_comment_delimiters().size() == 1);
  804. ERR_PRINT_ON;
  805. /* Cannot remove string with remove comment. */
  806. code_edit->remove_comment_delimiter("\"");
  807. CHECK(code_edit->has_string_delimiter("\""));
  808. CHECK(code_edit->get_string_delimiters().size() == 1);
  809. /* Cannot remove comment with remove string. */
  810. code_edit->remove_string_delimiter("#");
  811. CHECK(code_edit->has_comment_delimiter("#"));
  812. CHECK(code_edit->get_comment_delimiters().size() == 1);
  813. /* Clear comments leave strings. */
  814. code_edit->clear_comment_delimiters();
  815. CHECK(code_edit->has_string_delimiter("\""));
  816. CHECK(code_edit->get_string_delimiters().size() == 1);
  817. /* Clear string leave comments. */
  818. code_edit->add_comment_delimiter("#", "", false);
  819. CHECK(code_edit->has_comment_delimiter("#"));
  820. CHECK(code_edit->get_comment_delimiters().size() == 1);
  821. code_edit->clear_string_delimiters();
  822. CHECK(code_edit->has_comment_delimiter("#"));
  823. CHECK(code_edit->get_comment_delimiters().size() == 1);
  824. }
  825. }
  826. SUBCASE("[CodeEdit] single line delimiters") {
  827. SUBCASE("[CodeEdit] single line string delimiters") {
  828. /* Blank end key should set lineonly to true. */
  829. code_edit->add_string_delimiter("#", "", false);
  830. CHECK(code_edit->has_string_delimiter("#"));
  831. CHECK(code_edit->get_string_delimiters().size() == 1);
  832. /* Insert line above, line with string then line below. */
  833. code_edit->insert_text_at_caret(" \n#\n ");
  834. /* Check line above is not in string. */
  835. CHECK(code_edit->is_in_string(0, 1) == -1);
  836. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  837. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  838. /* Check column before start key is not in string. */
  839. CHECK(code_edit->is_in_string(1, 0) == -1);
  840. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  841. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  842. /* Check column after start key is in string and start / end positions are correct. */
  843. CHECK(code_edit->is_in_string(1, 1) != -1);
  844. CHECK(code_edit->get_delimiter_start_position(1, 1) == Point2(1, 1));
  845. CHECK(code_edit->get_delimiter_end_position(1, 1) == Point2(2, 1));
  846. /* Check line after is not in string. */
  847. CHECK(code_edit->is_in_string(2, 1) == -1);
  848. CHECK(code_edit->get_delimiter_start_position(2, 1) == OUTSIDE_DELIMETER);
  849. CHECK(code_edit->get_delimiter_end_position(2, 1) == OUTSIDE_DELIMETER);
  850. /* Check region metadata. */
  851. int idx = code_edit->is_in_string(1, 1);
  852. CHECK(code_edit->get_delimiter_start_key(idx) == "#");
  853. CHECK(code_edit->get_delimiter_end_key(idx) == "");
  854. /* Check nested strings are handled correctly. */
  855. code_edit->set_text(" \n# # \n ");
  856. /* Check line above is not in string. */
  857. CHECK(code_edit->is_in_string(0, 1) == -1);
  858. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  859. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  860. /* Check column before first start key is not in string. */
  861. CHECK(code_edit->is_in_string(1, 0) == -1);
  862. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  863. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  864. /* Check column after the first start key is in string and start / end positions are correct. */
  865. CHECK(code_edit->is_in_string(1, 1) != -1);
  866. CHECK(code_edit->get_delimiter_start_position(1, 1) == Point2(1, 1));
  867. CHECK(code_edit->get_delimiter_end_position(1, 1) == Point2(6, 1));
  868. /* Check column after the second start key returns data for the first. */
  869. CHECK(code_edit->is_in_string(1, 5) != -1);
  870. CHECK(code_edit->get_delimiter_start_position(1, 5) == Point2(1, 1));
  871. CHECK(code_edit->get_delimiter_end_position(1, 5) == Point2(6, 1));
  872. /* Check line after is not in string. */
  873. CHECK(code_edit->is_in_string(2, 1) == -1);
  874. CHECK(code_edit->get_delimiter_start_position(2, 1) == OUTSIDE_DELIMETER);
  875. CHECK(code_edit->get_delimiter_end_position(2, 1) == OUTSIDE_DELIMETER);
  876. /* Check is in string with no column returns true if entire line is comment excluding whitespace. */
  877. code_edit->set_text(" \n # # \n ");
  878. CHECK(code_edit->is_in_string(1) != -1);
  879. code_edit->set_text(" \n text # # \n ");
  880. CHECK(code_edit->is_in_string(1) == -1);
  881. /* Removing delimiter should update. */
  882. code_edit->set_text(" \n # # \n ");
  883. code_edit->remove_string_delimiter("#");
  884. CHECK_FALSE(code_edit->has_string_delimiter("$"));
  885. CHECK(code_edit->get_string_delimiters().size() == 0);
  886. CHECK(code_edit->is_in_string(1) == -1);
  887. /* Adding and clear should update. */
  888. code_edit->add_string_delimiter("#", "", false);
  889. CHECK(code_edit->has_string_delimiter("#"));
  890. CHECK(code_edit->get_string_delimiters().size() == 1);
  891. CHECK(code_edit->is_in_string(1) != -1);
  892. code_edit->clear_string_delimiters();
  893. CHECK_FALSE(code_edit->has_string_delimiter("$"));
  894. CHECK(code_edit->get_string_delimiters().size() == 0);
  895. CHECK(code_edit->is_in_string(1) == -1);
  896. }
  897. SUBCASE("[CodeEdit] single line comment delimiters") {
  898. /* Blank end key should set lineonly to true. */
  899. code_edit->add_comment_delimiter("#", "", false);
  900. CHECK(code_edit->has_comment_delimiter("#"));
  901. CHECK(code_edit->get_comment_delimiters().size() == 1);
  902. /* Insert line above, line with comment then line below. */
  903. code_edit->insert_text_at_caret(" \n#\n ");
  904. /* Check line above is not in comment. */
  905. CHECK(code_edit->is_in_comment(0, 1) == -1);
  906. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  907. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  908. /* Check column before start key is not in comment. */
  909. CHECK(code_edit->is_in_comment(1, 0) == -1);
  910. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  911. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  912. /* Check column after start key is in comment and start / end positions are correct. */
  913. CHECK(code_edit->is_in_comment(1, 1) != -1);
  914. CHECK(code_edit->get_delimiter_start_position(1, 1) == Point2(1, 1));
  915. CHECK(code_edit->get_delimiter_end_position(1, 1) == Point2(2, 1));
  916. /* Check line after is not in comment. */
  917. CHECK(code_edit->is_in_comment(2, 1) == -1);
  918. CHECK(code_edit->get_delimiter_start_position(2, 1) == OUTSIDE_DELIMETER);
  919. CHECK(code_edit->get_delimiter_end_position(2, 1) == OUTSIDE_DELIMETER);
  920. /* Check region metadata. */
  921. int idx = code_edit->is_in_comment(1, 1);
  922. CHECK(code_edit->get_delimiter_start_key(idx) == "#");
  923. CHECK(code_edit->get_delimiter_end_key(idx) == "");
  924. /* Check nested comments are handled correctly. */
  925. code_edit->set_text(" \n# # \n ");
  926. /* Check line above is not in comment. */
  927. CHECK(code_edit->is_in_comment(0, 1) == -1);
  928. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  929. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  930. /* Check column before first start key is not in comment. */
  931. CHECK(code_edit->is_in_comment(1, 0) == -1);
  932. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  933. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  934. /* Check column after the first start key is in comment and start / end positions are correct. */
  935. CHECK(code_edit->is_in_comment(1, 1) != -1);
  936. CHECK(code_edit->get_delimiter_start_position(1, 1) == Point2(1, 1));
  937. CHECK(code_edit->get_delimiter_end_position(1, 1) == Point2(6, 1));
  938. /* Check column after the second start key returns data for the first. */
  939. CHECK(code_edit->is_in_comment(1, 5) != -1);
  940. CHECK(code_edit->get_delimiter_start_position(1, 5) == Point2(1, 1));
  941. CHECK(code_edit->get_delimiter_end_position(1, 5) == Point2(6, 1));
  942. /* Check line after is not in comment. */
  943. CHECK(code_edit->is_in_comment(2, 1) == -1);
  944. CHECK(code_edit->get_delimiter_start_position(2, 1) == OUTSIDE_DELIMETER);
  945. CHECK(code_edit->get_delimiter_end_position(2, 1) == OUTSIDE_DELIMETER);
  946. /* Check is in comment with no column returns true if entire line is comment excluding whitespace. */
  947. code_edit->set_text(" \n # # \n ");
  948. CHECK(code_edit->is_in_comment(1) != -1);
  949. code_edit->set_text(" \n text # # \n ");
  950. CHECK(code_edit->is_in_comment(1) == -1);
  951. /* Removing delimiter should update. */
  952. code_edit->set_text(" \n # # \n ");
  953. code_edit->remove_comment_delimiter("#");
  954. CHECK_FALSE(code_edit->has_comment_delimiter("$"));
  955. CHECK(code_edit->get_comment_delimiters().size() == 0);
  956. CHECK(code_edit->is_in_comment(1) == -1);
  957. /* Adding and clear should update. */
  958. code_edit->add_comment_delimiter("#", "", false);
  959. CHECK(code_edit->has_comment_delimiter("#"));
  960. CHECK(code_edit->get_comment_delimiters().size() == 1);
  961. CHECK(code_edit->is_in_comment(1) != -1);
  962. code_edit->clear_comment_delimiters();
  963. CHECK_FALSE(code_edit->has_comment_delimiter("$"));
  964. CHECK(code_edit->get_comment_delimiters().size() == 0);
  965. CHECK(code_edit->is_in_comment(1) == -1);
  966. }
  967. SUBCASE("[CodeEdit] single line mixed delimiters") {
  968. /* Blank end key should set lineonly to true. */
  969. /* Add string delimiter. */
  970. code_edit->add_string_delimiter("&", "", false);
  971. CHECK(code_edit->has_string_delimiter("&"));
  972. CHECK(code_edit->get_string_delimiters().size() == 1);
  973. /* Add comment delimiter. */
  974. code_edit->add_comment_delimiter("#", "", false);
  975. CHECK(code_edit->has_comment_delimiter("#"));
  976. CHECK(code_edit->get_comment_delimiters().size() == 1);
  977. /* Nest a string delimiter inside a comment. */
  978. code_edit->set_text(" \n# & \n ");
  979. /* Check line above is not in comment. */
  980. CHECK(code_edit->is_in_comment(0, 1) == -1);
  981. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  982. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  983. /* Check column before first start key is not in comment. */
  984. CHECK(code_edit->is_in_comment(1, 0) == -1);
  985. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  986. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  987. /* Check column after the first start key is in comment and start / end positions are correct. */
  988. CHECK(code_edit->is_in_comment(1, 1) != -1);
  989. CHECK(code_edit->get_delimiter_start_position(1, 1) == Point2(1, 1));
  990. CHECK(code_edit->get_delimiter_end_position(1, 1) == Point2(6, 1));
  991. /* Check column after the second start key returns data for the first, and does not state string. */
  992. CHECK(code_edit->is_in_comment(1, 5) != -1);
  993. CHECK(code_edit->get_delimiter_start_position(1, 5) == Point2(1, 1));
  994. CHECK(code_edit->get_delimiter_end_position(1, 5) == Point2(6, 1));
  995. CHECK(code_edit->is_in_string(1, 5) == -1);
  996. /* Check line after is not in comment. */
  997. CHECK(code_edit->is_in_comment(2, 1) == -1);
  998. CHECK(code_edit->get_delimiter_start_position(2, 1) == OUTSIDE_DELIMETER);
  999. CHECK(code_edit->get_delimiter_end_position(2, 1) == OUTSIDE_DELIMETER);
  1000. /* Remove the comment delimiter. */
  1001. code_edit->remove_comment_delimiter("#");
  1002. CHECK_FALSE(code_edit->has_comment_delimiter("$"));
  1003. CHECK(code_edit->get_comment_delimiters().size() == 0);
  1004. /* The "first" comment region is no longer valid. */
  1005. CHECK(code_edit->is_in_comment(1, 1) == -1);
  1006. CHECK(code_edit->get_delimiter_start_position(1, 1) == OUTSIDE_DELIMETER);
  1007. CHECK(code_edit->get_delimiter_end_position(1, 1) == OUTSIDE_DELIMETER);
  1008. /* The "second" region as string is now valid. */
  1009. CHECK(code_edit->is_in_string(1, 5) != -1);
  1010. CHECK(code_edit->get_delimiter_start_position(1, 5) == Point2(4, 1));
  1011. CHECK(code_edit->get_delimiter_end_position(1, 5) == Point2(6, 1));
  1012. }
  1013. }
  1014. SUBCASE("[CodeEdit] multiline delimiters") {
  1015. SUBCASE("[CodeEdit] multiline string delimiters") {
  1016. code_edit->clear_string_delimiters();
  1017. code_edit->clear_comment_delimiters();
  1018. /* Add string delimiter. */
  1019. code_edit->add_string_delimiter("#", "#", false);
  1020. CHECK(code_edit->has_string_delimiter("#"));
  1021. CHECK(code_edit->get_string_delimiters().size() == 1);
  1022. /* First test over a single line. */
  1023. code_edit->set_text(" \n # # \n ");
  1024. /* Check line above is not in string. */
  1025. CHECK(code_edit->is_in_string(0, 1) == -1);
  1026. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  1027. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  1028. /* Check column before start key is not in string. */
  1029. CHECK(code_edit->is_in_string(1, 0) == -1);
  1030. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  1031. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  1032. /* Check column before closing delimiter is in string. */
  1033. CHECK(code_edit->is_in_string(1, 2) != -1);
  1034. CHECK(code_edit->get_delimiter_start_position(1, 2) == Point2(2, 1));
  1035. CHECK(code_edit->get_delimiter_end_position(1, 2) == Point2(5, 1));
  1036. /* Check column after end key is not in string. */
  1037. CHECK(code_edit->is_in_string(1, 6) == -1);
  1038. CHECK(code_edit->get_delimiter_start_position(1, 6) == OUTSIDE_DELIMETER);
  1039. CHECK(code_edit->get_delimiter_end_position(1, 6) == OUTSIDE_DELIMETER);
  1040. /* Check line after is not in string. */
  1041. CHECK(code_edit->is_in_string(2, 1) == -1);
  1042. CHECK(code_edit->get_delimiter_start_position(2, 1) == OUTSIDE_DELIMETER);
  1043. CHECK(code_edit->get_delimiter_end_position(2, 1) == OUTSIDE_DELIMETER);
  1044. /* Check the region metadata. */
  1045. int idx = code_edit->is_in_string(1, 2);
  1046. CHECK(code_edit->get_delimiter_start_key(idx) == "#");
  1047. CHECK(code_edit->get_delimiter_end_key(idx) == "#");
  1048. /* Next test over a multiple blank lines. */
  1049. code_edit->set_text(" \n # \n\n # \n ");
  1050. /* Check line above is not in string. */
  1051. CHECK(code_edit->is_in_string(0, 1) == -1);
  1052. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  1053. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  1054. /* Check column before start key is not in string. */
  1055. CHECK(code_edit->is_in_string(1, 0) == -1);
  1056. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  1057. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  1058. /* Check column just after start key is in string. */
  1059. CHECK(code_edit->is_in_string(1, 2) != -1);
  1060. CHECK(code_edit->get_delimiter_start_position(1, 2) == Point2(2, 1));
  1061. CHECK(code_edit->get_delimiter_end_position(1, 2) == Point2(2, 3));
  1062. /* Check blank middle line. */
  1063. CHECK(code_edit->is_in_string(2, 0) != -1);
  1064. CHECK(code_edit->get_delimiter_start_position(2, 0) == Point2(2, 1));
  1065. CHECK(code_edit->get_delimiter_end_position(2, 0) == Point2(2, 3));
  1066. /* Check column just before end key is in string. */
  1067. CHECK(code_edit->is_in_string(3, 0) != -1);
  1068. CHECK(code_edit->get_delimiter_start_position(3, 0) == Point2(2, 1));
  1069. CHECK(code_edit->get_delimiter_end_position(3, 0) == Point2(2, 3));
  1070. /* Check column after end key is not in string. */
  1071. CHECK(code_edit->is_in_string(3, 3) == -1);
  1072. CHECK(code_edit->get_delimiter_start_position(3, 3) == OUTSIDE_DELIMETER);
  1073. CHECK(code_edit->get_delimiter_end_position(3, 3) == OUTSIDE_DELIMETER);
  1074. /* Check line after is not in string. */
  1075. CHECK(code_edit->is_in_string(4, 1) == -1);
  1076. CHECK(code_edit->get_delimiter_start_position(4, 1) == OUTSIDE_DELIMETER);
  1077. CHECK(code_edit->get_delimiter_end_position(4, 1) == OUTSIDE_DELIMETER);
  1078. /* Next test over a multiple non-blank lines. */
  1079. code_edit->set_text(" \n # \n \n # \n ");
  1080. /* Check line above is not in string. */
  1081. CHECK(code_edit->is_in_string(0, 1) == -1);
  1082. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  1083. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  1084. /* Check column before start key is not in string. */
  1085. CHECK(code_edit->is_in_string(1, 0) == -1);
  1086. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  1087. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  1088. /* Check column just after start key is in string. */
  1089. CHECK(code_edit->is_in_string(1, 2) != -1);
  1090. CHECK(code_edit->get_delimiter_start_position(1, 2) == Point2(2, 1));
  1091. CHECK(code_edit->get_delimiter_end_position(1, 2) == Point2(2, 3));
  1092. /* Check middle line. */
  1093. CHECK(code_edit->is_in_string(2, 0) != -1);
  1094. CHECK(code_edit->get_delimiter_start_position(2, 0) == Point2(2, 1));
  1095. CHECK(code_edit->get_delimiter_end_position(2, 0) == Point2(2, 3));
  1096. /* Check column just before end key is in string. */
  1097. CHECK(code_edit->is_in_string(3, 0) != -1);
  1098. CHECK(code_edit->get_delimiter_start_position(3, 0) == Point2(2, 1));
  1099. CHECK(code_edit->get_delimiter_end_position(3, 0) == Point2(2, 3));
  1100. /* Check column after end key is not in string. */
  1101. CHECK(code_edit->is_in_string(3, 3) == -1);
  1102. CHECK(code_edit->get_delimiter_start_position(3, 3) == OUTSIDE_DELIMETER);
  1103. CHECK(code_edit->get_delimiter_end_position(3, 3) == OUTSIDE_DELIMETER);
  1104. /* Check line after is not in string. */
  1105. CHECK(code_edit->is_in_string(4, 1) == -1);
  1106. CHECK(code_edit->get_delimiter_start_position(4, 1) == OUTSIDE_DELIMETER);
  1107. CHECK(code_edit->get_delimiter_end_position(4, 1) == OUTSIDE_DELIMETER);
  1108. /* check the region metadata. */
  1109. idx = code_edit->is_in_string(1, 2);
  1110. CHECK(code_edit->get_delimiter_start_key(idx) == "#");
  1111. CHECK(code_edit->get_delimiter_end_key(idx) == "#");
  1112. /* Next test nested strings. */
  1113. code_edit->add_string_delimiter("^", "^", false);
  1114. CHECK(code_edit->has_string_delimiter("^"));
  1115. CHECK(code_edit->get_string_delimiters().size() == 2);
  1116. code_edit->set_text(" \n # ^\n \n^ # \n ");
  1117. /* Check line above is not in string. */
  1118. CHECK(code_edit->is_in_string(0, 1) == -1);
  1119. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  1120. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  1121. /* Check column before start key is not in string. */
  1122. CHECK(code_edit->is_in_string(1, 0) == -1);
  1123. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  1124. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  1125. /* Check column just after start key is in string. */
  1126. CHECK(code_edit->is_in_string(1, 2) != -1);
  1127. CHECK(code_edit->get_delimiter_start_position(1, 2) == Point2(2, 1));
  1128. CHECK(code_edit->get_delimiter_end_position(1, 2) == Point2(3, 3));
  1129. /* Check middle line. */
  1130. CHECK(code_edit->is_in_string(2, 0) != -1);
  1131. CHECK(code_edit->get_delimiter_start_position(2, 0) == Point2(2, 1));
  1132. CHECK(code_edit->get_delimiter_end_position(2, 0) == Point2(3, 3));
  1133. /* Check column just before end key is in string. */
  1134. CHECK(code_edit->is_in_string(3, 0) != -1);
  1135. CHECK(code_edit->get_delimiter_start_position(3, 0) == Point2(2, 1));
  1136. CHECK(code_edit->get_delimiter_end_position(3, 0) == Point2(3, 3));
  1137. /* Check column after end key is not in string. */
  1138. CHECK(code_edit->is_in_string(3, 3) == -1);
  1139. CHECK(code_edit->get_delimiter_start_position(3, 3) == OUTSIDE_DELIMETER);
  1140. CHECK(code_edit->get_delimiter_end_position(3, 3) == OUTSIDE_DELIMETER);
  1141. /* Check line after is not in string. */
  1142. CHECK(code_edit->is_in_string(4, 1) == -1);
  1143. CHECK(code_edit->get_delimiter_start_position(4, 1) == OUTSIDE_DELIMETER);
  1144. CHECK(code_edit->get_delimiter_end_position(4, 1) == OUTSIDE_DELIMETER);
  1145. /* check the region metadata. */
  1146. idx = code_edit->is_in_string(1, 2);
  1147. CHECK(code_edit->get_delimiter_start_key(idx) == "#");
  1148. CHECK(code_edit->get_delimiter_end_key(idx) == "#");
  1149. /* Next test no end key. */
  1150. code_edit->set_text(" \n # \n ");
  1151. /* check the region metadata. */
  1152. idx = code_edit->is_in_string(1, 2);
  1153. CHECK(code_edit->get_delimiter_start_position(1, 2) == Point2(2, 1));
  1154. CHECK(code_edit->get_delimiter_end_position(1, 2) == Point2(-1, -1));
  1155. CHECK(code_edit->get_delimiter_start_key(idx) == "#");
  1156. CHECK(code_edit->get_delimiter_end_key(idx) == "#");
  1157. /* Check is in string with no column returns true if entire line is string excluding whitespace. */
  1158. code_edit->set_text(" \n # \n\n #\n ");
  1159. CHECK(code_edit->is_in_string(1) != -1);
  1160. CHECK(code_edit->is_in_string(2) != -1);
  1161. CHECK(code_edit->is_in_string(3) != -1);
  1162. code_edit->set_text(" \n test # \n\n # test \n ");
  1163. CHECK(code_edit->is_in_string(1) == -1);
  1164. CHECK(code_edit->is_in_string(2) != -1);
  1165. CHECK(code_edit->is_in_string(3) == -1);
  1166. /* Next check updating the delimiter cache while typing. */
  1167. code_edit->set_text("\n\n");
  1168. code_edit->set_caret_line(0);
  1169. code_edit->set_caret_column(0);
  1170. CHECK(code_edit->is_in_string(0) == -1);
  1171. CHECK(code_edit->is_in_string(1) == -1);
  1172. code_edit->insert_text_at_caret("#");
  1173. CHECK(code_edit->is_in_string(0) != -1);
  1174. CHECK(code_edit->is_in_string(1) != -1);
  1175. code_edit->insert_text_at_caret("#");
  1176. CHECK(code_edit->is_in_string(0) != -1);
  1177. CHECK(code_edit->is_in_string(1) == -1);
  1178. }
  1179. SUBCASE("[CodeEdit] multiline comment delimiters") {
  1180. /* Add comment delimiter. */
  1181. code_edit->add_comment_delimiter("#", "#", false);
  1182. CHECK(code_edit->has_comment_delimiter("#"));
  1183. CHECK(code_edit->get_comment_delimiters().size() == 1);
  1184. /* First test over a single line. */
  1185. code_edit->set_text(" \n # # \n ");
  1186. /* Check line above is not in comment. */
  1187. CHECK(code_edit->is_in_comment(0, 1) == -1);
  1188. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  1189. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  1190. /* Check column before start key is not in comment. */
  1191. CHECK(code_edit->is_in_comment(1, 0) == -1);
  1192. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  1193. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  1194. /* Check column before closing delimiter is in comment. */
  1195. CHECK(code_edit->is_in_comment(1, 2) != -1);
  1196. CHECK(code_edit->get_delimiter_start_position(1, 2) == Point2(2, 1));
  1197. CHECK(code_edit->get_delimiter_end_position(1, 2) == Point2(5, 1));
  1198. /* Check column after end key is not in comment. */
  1199. CHECK(code_edit->is_in_comment(1, 6) == -1);
  1200. CHECK(code_edit->get_delimiter_start_position(1, 6) == OUTSIDE_DELIMETER);
  1201. CHECK(code_edit->get_delimiter_end_position(1, 6) == OUTSIDE_DELIMETER);
  1202. /* Check line after is not in comment. */
  1203. CHECK(code_edit->is_in_comment(2, 1) == -1);
  1204. CHECK(code_edit->get_delimiter_start_position(2, 1) == OUTSIDE_DELIMETER);
  1205. CHECK(code_edit->get_delimiter_end_position(2, 1) == OUTSIDE_DELIMETER);
  1206. /* Check the region metadata. */
  1207. int idx = code_edit->is_in_comment(1, 2);
  1208. CHECK(code_edit->get_delimiter_start_key(idx) == "#");
  1209. CHECK(code_edit->get_delimiter_end_key(idx) == "#");
  1210. /* Next test over a multiple blank lines. */
  1211. code_edit->set_text(" \n # \n\n # \n ");
  1212. /* Check line above is not in comment. */
  1213. CHECK(code_edit->is_in_comment(0, 1) == -1);
  1214. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  1215. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  1216. /* Check column before start key is not in comment. */
  1217. CHECK(code_edit->is_in_comment(1, 0) == -1);
  1218. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  1219. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  1220. /* Check column just after start key is in comment. */
  1221. CHECK(code_edit->is_in_comment(1, 2) != -1);
  1222. CHECK(code_edit->get_delimiter_start_position(1, 2) == Point2(2, 1));
  1223. CHECK(code_edit->get_delimiter_end_position(1, 2) == Point2(2, 3));
  1224. /* Check blank middle line. */
  1225. CHECK(code_edit->is_in_comment(2, 0) != -1);
  1226. CHECK(code_edit->get_delimiter_start_position(2, 0) == Point2(2, 1));
  1227. CHECK(code_edit->get_delimiter_end_position(2, 0) == Point2(2, 3));
  1228. /* Check column just before end key is in comment. */
  1229. CHECK(code_edit->is_in_comment(3, 0) != -1);
  1230. CHECK(code_edit->get_delimiter_start_position(3, 0) == Point2(2, 1));
  1231. CHECK(code_edit->get_delimiter_end_position(3, 0) == Point2(2, 3));
  1232. /* Check column after end key is not in comment. */
  1233. CHECK(code_edit->is_in_comment(3, 3) == -1);
  1234. CHECK(code_edit->get_delimiter_start_position(3, 3) == OUTSIDE_DELIMETER);
  1235. CHECK(code_edit->get_delimiter_end_position(3, 3) == OUTSIDE_DELIMETER);
  1236. /* Check line after is not in comment. */
  1237. CHECK(code_edit->is_in_comment(4, 1) == -1);
  1238. CHECK(code_edit->get_delimiter_start_position(4, 1) == OUTSIDE_DELIMETER);
  1239. CHECK(code_edit->get_delimiter_end_position(4, 1) == OUTSIDE_DELIMETER);
  1240. /* Next test over a multiple non-blank lines. */
  1241. code_edit->set_text(" \n # \n \n # \n ");
  1242. /* Check line above is not in comment. */
  1243. CHECK(code_edit->is_in_comment(0, 1) == -1);
  1244. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  1245. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  1246. /* Check column before start key is not in comment. */
  1247. CHECK(code_edit->is_in_comment(1, 0) == -1);
  1248. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  1249. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  1250. /* Check column just after start key is in comment. */
  1251. CHECK(code_edit->is_in_comment(1, 2) != -1);
  1252. CHECK(code_edit->get_delimiter_start_position(1, 2) == Point2(2, 1));
  1253. CHECK(code_edit->get_delimiter_end_position(1, 2) == Point2(2, 3));
  1254. /* Check middle line. */
  1255. CHECK(code_edit->is_in_comment(2, 0) != -1);
  1256. CHECK(code_edit->get_delimiter_start_position(2, 0) == Point2(2, 1));
  1257. CHECK(code_edit->get_delimiter_end_position(2, 0) == Point2(2, 3));
  1258. /* Check column just before end key is in comment. */
  1259. CHECK(code_edit->is_in_comment(3, 0) != -1);
  1260. CHECK(code_edit->get_delimiter_start_position(3, 0) == Point2(2, 1));
  1261. CHECK(code_edit->get_delimiter_end_position(3, 0) == Point2(2, 3));
  1262. /* Check column after end key is not in comment. */
  1263. CHECK(code_edit->is_in_comment(3, 3) == -1);
  1264. CHECK(code_edit->get_delimiter_start_position(3, 3) == OUTSIDE_DELIMETER);
  1265. CHECK(code_edit->get_delimiter_end_position(3, 3) == OUTSIDE_DELIMETER);
  1266. /* Check line after is not in comment. */
  1267. CHECK(code_edit->is_in_comment(4, 1) == -1);
  1268. CHECK(code_edit->get_delimiter_start_position(4, 1) == OUTSIDE_DELIMETER);
  1269. CHECK(code_edit->get_delimiter_end_position(4, 1) == OUTSIDE_DELIMETER);
  1270. /* check the region metadata. */
  1271. idx = code_edit->is_in_comment(1, 2);
  1272. CHECK(code_edit->get_delimiter_start_key(idx) == "#");
  1273. CHECK(code_edit->get_delimiter_end_key(idx) == "#");
  1274. /* Next test nested comments. */
  1275. code_edit->add_comment_delimiter("^", "^", false);
  1276. CHECK(code_edit->has_comment_delimiter("^"));
  1277. CHECK(code_edit->get_comment_delimiters().size() == 2);
  1278. code_edit->set_text(" \n # ^\n \n^ # \n ");
  1279. /* Check line above is not in comment. */
  1280. CHECK(code_edit->is_in_comment(0, 1) == -1);
  1281. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  1282. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  1283. /* Check column before start key is not in comment. */
  1284. CHECK(code_edit->is_in_comment(1, 0) == -1);
  1285. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  1286. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  1287. /* Check column just after start key is in comment. */
  1288. CHECK(code_edit->is_in_comment(1, 2) != -1);
  1289. CHECK(code_edit->get_delimiter_start_position(1, 2) == Point2(2, 1));
  1290. CHECK(code_edit->get_delimiter_end_position(1, 2) == Point2(3, 3));
  1291. /* Check middle line. */
  1292. CHECK(code_edit->is_in_comment(2, 0) != -1);
  1293. CHECK(code_edit->get_delimiter_start_position(2, 0) == Point2(2, 1));
  1294. CHECK(code_edit->get_delimiter_end_position(2, 0) == Point2(3, 3));
  1295. /* Check column just before end key is in comment. */
  1296. CHECK(code_edit->is_in_comment(3, 0) != -1);
  1297. CHECK(code_edit->get_delimiter_start_position(3, 0) == Point2(2, 1));
  1298. CHECK(code_edit->get_delimiter_end_position(3, 0) == Point2(3, 3));
  1299. /* Check column after end key is not in comment. */
  1300. CHECK(code_edit->is_in_comment(3, 3) == -1);
  1301. CHECK(code_edit->get_delimiter_start_position(3, 3) == OUTSIDE_DELIMETER);
  1302. CHECK(code_edit->get_delimiter_end_position(3, 3) == OUTSIDE_DELIMETER);
  1303. /* Check line after is not in comment. */
  1304. CHECK(code_edit->is_in_comment(4, 1) == -1);
  1305. CHECK(code_edit->get_delimiter_start_position(4, 1) == OUTSIDE_DELIMETER);
  1306. CHECK(code_edit->get_delimiter_end_position(4, 1) == OUTSIDE_DELIMETER);
  1307. /* check the region metadata. */
  1308. idx = code_edit->is_in_comment(1, 2);
  1309. CHECK(code_edit->get_delimiter_start_key(idx) == "#");
  1310. CHECK(code_edit->get_delimiter_end_key(idx) == "#");
  1311. /* Next test no end key. */
  1312. code_edit->set_text(" \n # \n ");
  1313. /* check the region metadata. */
  1314. idx = code_edit->is_in_comment(1, 2);
  1315. CHECK(code_edit->get_delimiter_start_position(1, 2) == Point2(2, 1));
  1316. CHECK(code_edit->get_delimiter_end_position(1, 2) == Point2(-1, -1));
  1317. CHECK(code_edit->get_delimiter_start_key(idx) == "#");
  1318. CHECK(code_edit->get_delimiter_end_key(idx) == "#");
  1319. /* Check is in comment with no column returns true if entire line is comment excluding whitespace. */
  1320. code_edit->set_text(" \n # \n\n #\n ");
  1321. CHECK(code_edit->is_in_comment(1) != -1);
  1322. CHECK(code_edit->is_in_comment(2) != -1);
  1323. CHECK(code_edit->is_in_comment(3) != -1);
  1324. code_edit->set_text(" \n test # \n\n # test \n ");
  1325. CHECK(code_edit->is_in_comment(1) == -1);
  1326. CHECK(code_edit->is_in_comment(2) != -1);
  1327. CHECK(code_edit->is_in_comment(3) == -1);
  1328. /* Next check updating the delimiter cache while typing. */
  1329. code_edit->set_text("\n\n");
  1330. code_edit->set_caret_line(0);
  1331. code_edit->set_caret_column(0);
  1332. CHECK(code_edit->is_in_comment(0) == -1);
  1333. CHECK(code_edit->is_in_comment(1) == -1);
  1334. code_edit->insert_text_at_caret("#");
  1335. CHECK(code_edit->is_in_comment(0) != -1);
  1336. CHECK(code_edit->is_in_comment(1) != -1);
  1337. code_edit->insert_text_at_caret("#");
  1338. CHECK(code_edit->is_in_comment(0) != -1);
  1339. CHECK(code_edit->is_in_comment(1) == -1);
  1340. }
  1341. SUBCASE("[CodeEdit] multiline mixed delimiters") {
  1342. /* Add comment delimiter. */
  1343. code_edit->add_comment_delimiter("#", "#", false);
  1344. CHECK(code_edit->has_comment_delimiter("#"));
  1345. CHECK(code_edit->get_comment_delimiters().size() == 1);
  1346. /* Add string delimiter. */
  1347. code_edit->add_string_delimiter("^", "^", false);
  1348. CHECK(code_edit->has_string_delimiter("^"));
  1349. CHECK(code_edit->get_string_delimiters().size() == 1);
  1350. /* Nest a string inside a comment. */
  1351. code_edit->set_text(" \n # ^\n \n^ # \n ");
  1352. /* Check line above is not in comment. */
  1353. CHECK(code_edit->is_in_comment(0, 1) == -1);
  1354. CHECK(code_edit->get_delimiter_start_position(0, 1) == OUTSIDE_DELIMETER);
  1355. CHECK(code_edit->get_delimiter_end_position(0, 1) == OUTSIDE_DELIMETER);
  1356. /* Check column before start key is not in comment. */
  1357. CHECK(code_edit->is_in_comment(1, 0) == -1);
  1358. CHECK(code_edit->get_delimiter_start_position(1, 0) == OUTSIDE_DELIMETER);
  1359. CHECK(code_edit->get_delimiter_end_position(1, 0) == OUTSIDE_DELIMETER);
  1360. /* Check column just after start key is in comment. */
  1361. CHECK(code_edit->is_in_comment(1, 2) != -1);
  1362. CHECK(code_edit->get_delimiter_start_position(1, 2) == Point2(2, 1));
  1363. CHECK(code_edit->get_delimiter_end_position(1, 2) == Point2(3, 3));
  1364. /* Check middle line. */
  1365. CHECK(code_edit->is_in_comment(2, 0) != -1);
  1366. CHECK(code_edit->get_delimiter_start_position(2, 0) == Point2(2, 1));
  1367. CHECK(code_edit->get_delimiter_end_position(2, 0) == Point2(3, 3));
  1368. /* Check column just before end key is in comment. */
  1369. CHECK(code_edit->is_in_comment(3, 0) != -1);
  1370. CHECK(code_edit->get_delimiter_start_position(3, 0) == Point2(2, 1));
  1371. CHECK(code_edit->get_delimiter_end_position(3, 0) == Point2(3, 3));
  1372. /* Check column after end key is not in comment. */
  1373. CHECK(code_edit->is_in_comment(3, 3) == -1);
  1374. CHECK(code_edit->get_delimiter_start_position(3, 3) == OUTSIDE_DELIMETER);
  1375. CHECK(code_edit->get_delimiter_end_position(3, 3) == OUTSIDE_DELIMETER);
  1376. /* Check line after is not in comment. */
  1377. CHECK(code_edit->is_in_comment(4, 1) == -1);
  1378. CHECK(code_edit->get_delimiter_start_position(4, 1) == OUTSIDE_DELIMETER);
  1379. CHECK(code_edit->get_delimiter_end_position(4, 1) == OUTSIDE_DELIMETER);
  1380. /* check the region metadata. */
  1381. int idx = code_edit->is_in_comment(1, 2);
  1382. CHECK(code_edit->get_delimiter_start_key(idx) == "#");
  1383. CHECK(code_edit->get_delimiter_end_key(idx) == "#");
  1384. /* Check is in comment with no column returns true as inner delimiter should not be counted. */
  1385. CHECK(code_edit->is_in_comment(1) != -1);
  1386. CHECK(code_edit->is_in_comment(2) != -1);
  1387. CHECK(code_edit->is_in_comment(3) != -1);
  1388. }
  1389. }
  1390. memdelete(code_edit);
  1391. }
  1392. TEST_CASE("[SceneTree][CodeEdit] indent") {
  1393. CodeEdit *code_edit = memnew(CodeEdit);
  1394. SceneTree::get_singleton()->get_root()->add_child(code_edit);
  1395. code_edit->grab_focus();
  1396. SUBCASE("[CodeEdit] indent settings") {
  1397. code_edit->set_indent_size(10);
  1398. CHECK(code_edit->get_indent_size() == 10);
  1399. CHECK(code_edit->get_tab_size() == 10);
  1400. code_edit->set_auto_indent_enabled(false);
  1401. CHECK_FALSE(code_edit->is_auto_indent_enabled());
  1402. code_edit->set_auto_indent_enabled(true);
  1403. CHECK(code_edit->is_auto_indent_enabled());
  1404. code_edit->set_indent_using_spaces(false);
  1405. CHECK_FALSE(code_edit->is_indent_using_spaces());
  1406. code_edit->set_indent_using_spaces(true);
  1407. CHECK(code_edit->is_indent_using_spaces());
  1408. /* Only the first char is registered. */
  1409. TypedArray<String> auto_indent_prefixes;
  1410. auto_indent_prefixes.push_back("::");
  1411. auto_indent_prefixes.push_back("s");
  1412. auto_indent_prefixes.push_back("1");
  1413. code_edit->set_auto_indent_prefixes(auto_indent_prefixes);
  1414. auto_indent_prefixes = code_edit->get_auto_indent_prefixes();
  1415. CHECK(auto_indent_prefixes.has(":"));
  1416. CHECK(auto_indent_prefixes.has("s"));
  1417. CHECK(auto_indent_prefixes.has("1"));
  1418. }
  1419. SUBCASE("[CodeEdit] indent tabs") {
  1420. code_edit->set_indent_size(4);
  1421. code_edit->set_auto_indent_enabled(true);
  1422. code_edit->set_indent_using_spaces(false);
  1423. /* Do nothing if not editable. */
  1424. code_edit->set_editable(false);
  1425. code_edit->do_indent();
  1426. CHECK(code_edit->get_line(0).is_empty());
  1427. code_edit->indent_lines();
  1428. CHECK(code_edit->get_line(0).is_empty());
  1429. code_edit->set_editable(true);
  1430. /* Simple indent. */
  1431. code_edit->do_indent();
  1432. CHECK(code_edit->get_line(0) == "\t");
  1433. /* Check input action. */
  1434. SEND_GUI_ACTION("ui_text_indent");
  1435. CHECK(code_edit->get_line(0) == "\t\t");
  1436. /* Insert in place. */
  1437. code_edit->set_text("");
  1438. code_edit->insert_text_at_caret("test");
  1439. code_edit->do_indent();
  1440. CHECK(code_edit->get_line(0) == "test\t");
  1441. // Insert in place with multiple carets.
  1442. code_edit->set_text("test text");
  1443. code_edit->set_caret_column(5);
  1444. code_edit->add_caret(0, 7);
  1445. code_edit->add_caret(0, 2);
  1446. code_edit->do_indent();
  1447. CHECK(code_edit->get_line(0) == "te\tst \tte\txt");
  1448. CHECK(code_edit->get_caret_count() == 3);
  1449. CHECK(code_edit->get_caret_column(0) == 7);
  1450. CHECK(code_edit->get_caret_column(1) == 10);
  1451. CHECK(code_edit->get_caret_column(2) == 3);
  1452. code_edit->remove_secondary_carets();
  1453. // Indent lines does entire line and works without selection.
  1454. code_edit->set_text("");
  1455. code_edit->insert_text_at_caret("test");
  1456. code_edit->indent_lines();
  1457. CHECK(code_edit->get_line(0) == "\ttest");
  1458. CHECK(code_edit->get_caret_column() == 5);
  1459. /* Selection does entire line. */
  1460. code_edit->set_text("test");
  1461. code_edit->select_all();
  1462. code_edit->do_indent();
  1463. CHECK(code_edit->get_line(0) == "\ttest");
  1464. CHECK(code_edit->has_selection());
  1465. CHECK(code_edit->get_selection_origin_line() == 0);
  1466. CHECK(code_edit->get_selection_origin_column() == 0);
  1467. CHECK(code_edit->get_caret_line() == 0);
  1468. CHECK(code_edit->get_caret_column() == 5);
  1469. // Selection does entire line, right to left selection.
  1470. code_edit->set_text("test");
  1471. code_edit->select(0, 4, 0, 0);
  1472. code_edit->do_indent();
  1473. CHECK(code_edit->get_line(0) == "\ttest");
  1474. CHECK(code_edit->has_selection());
  1475. CHECK(code_edit->get_selection_origin_line() == 0);
  1476. CHECK(code_edit->get_selection_origin_column() == 5);
  1477. CHECK(code_edit->get_caret_line() == 0);
  1478. CHECK(code_edit->get_caret_column() == 0);
  1479. /* Handles multiple lines. */
  1480. code_edit->set_text("test\ntext");
  1481. code_edit->select_all();
  1482. code_edit->do_indent();
  1483. CHECK(code_edit->get_line(0) == "\ttest");
  1484. CHECK(code_edit->get_line(1) == "\ttext");
  1485. CHECK(code_edit->has_selection());
  1486. CHECK(code_edit->get_selection_origin_line() == 0);
  1487. CHECK(code_edit->get_selection_origin_column() == 0);
  1488. CHECK(code_edit->get_caret_line() == 1);
  1489. CHECK(code_edit->get_caret_column() == 5);
  1490. /* Do not indent line if last col is zero. */
  1491. code_edit->set_text("test\ntext");
  1492. code_edit->select(0, 0, 1, 0);
  1493. code_edit->do_indent();
  1494. CHECK(code_edit->get_line(0) == "\ttest");
  1495. CHECK(code_edit->get_line(1) == "text");
  1496. CHECK(code_edit->has_selection());
  1497. CHECK(code_edit->get_selection_origin_line() == 0);
  1498. CHECK(code_edit->get_selection_origin_column() == 0);
  1499. CHECK(code_edit->get_caret_line() == 1);
  1500. CHECK(code_edit->get_caret_column() == 0);
  1501. /* Indent even if last column of first line. */
  1502. code_edit->set_text("test\ntext");
  1503. code_edit->select(0, 4, 1, 0);
  1504. code_edit->do_indent();
  1505. CHECK(code_edit->get_line(0) == "\ttest");
  1506. CHECK(code_edit->get_line(1) == "text");
  1507. CHECK(code_edit->has_selection());
  1508. CHECK(code_edit->get_selection_origin_line() == 0);
  1509. CHECK(code_edit->get_selection_origin_column() == 5);
  1510. CHECK(code_edit->get_caret_line() == 1);
  1511. CHECK(code_edit->get_caret_column() == 0);
  1512. // Indent even if last column of first line, reversed.
  1513. code_edit->set_text("test\ntext");
  1514. code_edit->select(1, 0, 0, 4);
  1515. code_edit->do_indent();
  1516. CHECK(code_edit->get_line(0) == "\ttest");
  1517. CHECK(code_edit->get_line(1) == "text");
  1518. CHECK(code_edit->has_selection());
  1519. CHECK(code_edit->get_selection_origin_line() == 1);
  1520. CHECK(code_edit->get_selection_origin_column() == 0);
  1521. CHECK(code_edit->get_caret_line() == 0);
  1522. CHECK(code_edit->get_caret_column() == 5);
  1523. /* Check selection is adjusted. */
  1524. code_edit->set_text("test");
  1525. code_edit->select(0, 1, 0, 2);
  1526. code_edit->do_indent();
  1527. CHECK(code_edit->get_line(0) == "\ttest");
  1528. CHECK(code_edit->has_selection());
  1529. CHECK(code_edit->get_selection_origin_line() == 0);
  1530. CHECK(code_edit->get_selection_origin_column() == 2);
  1531. CHECK(code_edit->get_caret_line() == 0);
  1532. CHECK(code_edit->get_caret_column() == 3);
  1533. // Indent once with multiple selections.
  1534. code_edit->set_text("test");
  1535. code_edit->select(0, 1, 0, 2);
  1536. code_edit->add_caret(0, 4);
  1537. code_edit->select(0, 4, 0, 3, 1);
  1538. code_edit->do_indent();
  1539. CHECK(code_edit->get_line(0) == "\ttest");
  1540. CHECK(code_edit->get_caret_count() == 2);
  1541. CHECK(code_edit->has_selection(0));
  1542. CHECK(code_edit->get_selection_origin_line(0) == 0);
  1543. CHECK(code_edit->get_selection_origin_column(0) == 2);
  1544. CHECK(code_edit->get_caret_line(0) == 0);
  1545. CHECK(code_edit->get_caret_column(0) == 3);
  1546. CHECK(code_edit->has_selection(1));
  1547. CHECK(code_edit->get_selection_origin_line(1) == 0);
  1548. CHECK(code_edit->get_selection_origin_column(1) == 5);
  1549. CHECK(code_edit->get_caret_line(1) == 0);
  1550. CHECK(code_edit->get_caret_column(1) == 4);
  1551. }
  1552. SUBCASE("[CodeEdit] indent spaces") {
  1553. code_edit->set_indent_size(4);
  1554. code_edit->set_auto_indent_enabled(true);
  1555. code_edit->set_indent_using_spaces(true);
  1556. /* Do nothing if not editable. */
  1557. code_edit->set_editable(false);
  1558. code_edit->do_indent();
  1559. CHECK(code_edit->get_line(0).is_empty());
  1560. code_edit->indent_lines();
  1561. CHECK(code_edit->get_line(0).is_empty());
  1562. code_edit->set_editable(true);
  1563. /* Simple indent. */
  1564. code_edit->do_indent();
  1565. CHECK(code_edit->get_line(0) == " ");
  1566. /* Check input action. */
  1567. SEND_GUI_ACTION("ui_text_indent");
  1568. CHECK(code_edit->get_line(0) == " ");
  1569. /* Insert in place. */
  1570. code_edit->set_text("");
  1571. code_edit->insert_text_at_caret("test");
  1572. code_edit->do_indent();
  1573. CHECK(code_edit->get_line(0) == "test ");
  1574. // Insert in place with multiple carets.
  1575. code_edit->set_text("test text");
  1576. code_edit->set_caret_column(5);
  1577. code_edit->add_caret(0, 7);
  1578. code_edit->add_caret(0, 2);
  1579. code_edit->do_indent();
  1580. CHECK(code_edit->get_line(0) == "te st te xt");
  1581. CHECK(code_edit->get_caret_count() == 3);
  1582. CHECK(code_edit->get_caret_column(0) == 10);
  1583. CHECK(code_edit->get_caret_column(1) == 14);
  1584. CHECK(code_edit->get_caret_column(2) == 4);
  1585. code_edit->remove_secondary_carets();
  1586. // Indent lines does entire line and works without selection.
  1587. code_edit->set_text("");
  1588. code_edit->insert_text_at_caret("test");
  1589. code_edit->indent_lines();
  1590. CHECK(code_edit->get_line(0) == " test");
  1591. CHECK(code_edit->get_caret_column() == 8);
  1592. /* Selection does entire line. */
  1593. code_edit->set_text("test");
  1594. code_edit->select_all();
  1595. code_edit->do_indent();
  1596. CHECK(code_edit->get_line(0) == " test");
  1597. CHECK(code_edit->has_selection());
  1598. CHECK(code_edit->get_selection_origin_line() == 0);
  1599. CHECK(code_edit->get_selection_origin_column() == 0);
  1600. CHECK(code_edit->get_caret_line() == 0);
  1601. CHECK(code_edit->get_caret_column() == 8);
  1602. // Selection does entire line, right to left selection.
  1603. code_edit->set_text("test");
  1604. code_edit->select(0, 4, 0, 0);
  1605. code_edit->do_indent();
  1606. CHECK(code_edit->get_line(0) == " test");
  1607. CHECK(code_edit->has_selection());
  1608. CHECK(code_edit->get_selection_origin_line() == 0);
  1609. CHECK(code_edit->get_selection_origin_column() == 8);
  1610. CHECK(code_edit->get_caret_line() == 0);
  1611. CHECK(code_edit->get_caret_column() == 0);
  1612. /* single indent only add required spaces. */
  1613. code_edit->set_text(" test");
  1614. code_edit->select_all();
  1615. code_edit->do_indent();
  1616. CHECK(code_edit->get_line(0) == " test");
  1617. CHECK(code_edit->has_selection());
  1618. CHECK(code_edit->get_selection_origin_line() == 0);
  1619. CHECK(code_edit->get_selection_origin_column() == 0);
  1620. CHECK(code_edit->get_caret_line() == 0);
  1621. CHECK(code_edit->get_caret_column() == 8);
  1622. /* Handles multiple lines. */
  1623. code_edit->set_text("test\ntext");
  1624. code_edit->select_all();
  1625. code_edit->do_indent();
  1626. CHECK(code_edit->get_line(0) == " test");
  1627. CHECK(code_edit->get_line(1) == " text");
  1628. CHECK(code_edit->has_selection());
  1629. CHECK(code_edit->get_selection_origin_line() == 0);
  1630. CHECK(code_edit->get_selection_origin_column() == 0);
  1631. CHECK(code_edit->get_caret_line() == 1);
  1632. CHECK(code_edit->get_caret_column() == 8);
  1633. /* Do not indent line if last col is zero. */
  1634. code_edit->set_text("test\ntext");
  1635. code_edit->select(0, 0, 1, 0);
  1636. code_edit->do_indent();
  1637. CHECK(code_edit->get_line(0) == " test");
  1638. CHECK(code_edit->get_line(1) == "text");
  1639. CHECK(code_edit->has_selection());
  1640. CHECK(code_edit->get_selection_origin_line() == 0);
  1641. CHECK(code_edit->get_selection_origin_column() == 0);
  1642. CHECK(code_edit->get_caret_line() == 1);
  1643. CHECK(code_edit->get_caret_column() == 0);
  1644. /* Indent even if last column of first line. */
  1645. code_edit->set_text("test\ntext");
  1646. code_edit->select(0, 4, 1, 0);
  1647. code_edit->do_indent();
  1648. CHECK(code_edit->get_line(0) == " test");
  1649. CHECK(code_edit->get_line(1) == "text");
  1650. CHECK(code_edit->has_selection());
  1651. CHECK(code_edit->get_selection_origin_line() == 0);
  1652. CHECK(code_edit->get_selection_origin_column() == 8);
  1653. CHECK(code_edit->get_caret_line() == 1);
  1654. CHECK(code_edit->get_caret_column() == 0);
  1655. // Indent even if last column of first line, right to left selection.
  1656. code_edit->set_text("test\ntext");
  1657. code_edit->select(1, 0, 0, 4);
  1658. code_edit->do_indent();
  1659. CHECK(code_edit->get_line(0) == " test");
  1660. CHECK(code_edit->get_line(1) == "text");
  1661. CHECK(code_edit->has_selection());
  1662. CHECK(code_edit->get_selection_origin_line() == 1);
  1663. CHECK(code_edit->get_selection_origin_column() == 0);
  1664. CHECK(code_edit->get_caret_line() == 0);
  1665. CHECK(code_edit->get_caret_column() == 8);
  1666. /* Check selection is adjusted. */
  1667. code_edit->set_text("test");
  1668. code_edit->select(0, 1, 0, 2);
  1669. code_edit->do_indent();
  1670. CHECK(code_edit->get_line(0) == " test");
  1671. CHECK(code_edit->has_selection());
  1672. CHECK(code_edit->get_selection_origin_line() == 0);
  1673. CHECK(code_edit->get_selection_origin_column() == 5);
  1674. CHECK(code_edit->get_caret_line() == 0);
  1675. CHECK(code_edit->get_caret_column() == 6);
  1676. // Indent once with multiple selections.
  1677. code_edit->set_text("test");
  1678. code_edit->select(0, 1, 0, 2);
  1679. code_edit->add_caret(0, 4);
  1680. code_edit->select(0, 4, 0, 3, 1);
  1681. code_edit->do_indent();
  1682. CHECK(code_edit->get_line(0) == " test");
  1683. CHECK(code_edit->get_caret_count() == 2);
  1684. CHECK(code_edit->has_selection(0));
  1685. CHECK(code_edit->get_selection_origin_line(0) == 0);
  1686. CHECK(code_edit->get_selection_origin_column(0) == 5);
  1687. CHECK(code_edit->get_caret_line(0) == 0);
  1688. CHECK(code_edit->get_caret_column(0) == 6);
  1689. CHECK(code_edit->has_selection(1));
  1690. CHECK(code_edit->get_selection_origin_line(1) == 0);
  1691. CHECK(code_edit->get_selection_origin_column(1) == 8);
  1692. CHECK(code_edit->get_caret_line(1) == 0);
  1693. CHECK(code_edit->get_caret_column(1) == 7);
  1694. }
  1695. SUBCASE("[CodeEdit] unindent tabs") {
  1696. code_edit->set_indent_size(4);
  1697. code_edit->set_auto_indent_enabled(true);
  1698. code_edit->set_indent_using_spaces(false);
  1699. /* Do nothing if not editable. */
  1700. code_edit->set_text("\t");
  1701. code_edit->set_editable(false);
  1702. code_edit->unindent_lines();
  1703. CHECK(code_edit->get_line(0) == "\t");
  1704. code_edit->unindent_lines();
  1705. CHECK(code_edit->get_line(0) == "\t");
  1706. code_edit->set_editable(true);
  1707. /* Simple unindent. */
  1708. code_edit->unindent_lines();
  1709. CHECK(code_edit->get_line(0) == "");
  1710. /* Backspace does a simple unindent. */
  1711. code_edit->set_text("");
  1712. code_edit->insert_text_at_caret("\t");
  1713. code_edit->backspace();
  1714. CHECK(code_edit->get_line(0) == "");
  1715. /* Unindent lines does entire line and works without selection. */
  1716. code_edit->set_text("");
  1717. code_edit->insert_text_at_caret("\ttest");
  1718. code_edit->unindent_lines();
  1719. CHECK(code_edit->get_line(0) == "test");
  1720. CHECK(code_edit->get_caret_column() == 4);
  1721. // Unindent lines once with multiple carets.
  1722. code_edit->set_text("\t\ttest");
  1723. code_edit->set_caret_column(1);
  1724. code_edit->add_caret(0, 3);
  1725. code_edit->unindent_lines();
  1726. CHECK(code_edit->get_line(0) == "\ttest");
  1727. CHECK(code_edit->get_caret_count() == 2);
  1728. CHECK_FALSE(code_edit->has_selection());
  1729. CHECK(code_edit->get_caret_line(0) == 0);
  1730. CHECK(code_edit->get_caret_column(0) == 0);
  1731. CHECK(code_edit->get_caret_line(1) == 0);
  1732. CHECK(code_edit->get_caret_column(1) == 2);
  1733. code_edit->remove_secondary_carets();
  1734. /* Caret on col zero unindent line. */
  1735. code_edit->set_text("\t\ttest");
  1736. code_edit->set_caret_column(0);
  1737. code_edit->unindent_lines();
  1738. CHECK(code_edit->get_line(0) == "\ttest");
  1739. CHECK(code_edit->get_caret_column() == 0);
  1740. /* Check input action. */
  1741. code_edit->set_text("\t\ttest");
  1742. SEND_GUI_ACTION("ui_text_dedent");
  1743. CHECK(code_edit->get_line(0) == "\ttest");
  1744. /* Selection does entire line. */
  1745. code_edit->set_text("\t\ttest");
  1746. code_edit->select_all();
  1747. code_edit->unindent_lines();
  1748. CHECK(code_edit->get_line(0) == "\ttest");
  1749. CHECK(code_edit->has_selection());
  1750. CHECK(code_edit->get_selection_origin_line() == 0);
  1751. CHECK(code_edit->get_selection_origin_column() == 0);
  1752. CHECK(code_edit->get_caret_line() == 0);
  1753. CHECK(code_edit->get_caret_column() == 5);
  1754. // Selection does entire line, right to left selection.
  1755. code_edit->set_text("\t\ttest");
  1756. code_edit->select(0, 6, 0, 0);
  1757. code_edit->unindent_lines();
  1758. CHECK(code_edit->get_line(0) == "\ttest");
  1759. CHECK(code_edit->has_selection());
  1760. CHECK(code_edit->get_selection_origin_line() == 0);
  1761. CHECK(code_edit->get_selection_origin_column() == 5);
  1762. CHECK(code_edit->get_caret_line() == 0);
  1763. CHECK(code_edit->get_caret_column() == 0);
  1764. // Handles multiple lines.
  1765. code_edit->set_text("\t\ttest\n\t\ttext");
  1766. code_edit->select_all();
  1767. code_edit->unindent_lines();
  1768. CHECK(code_edit->get_line(0) == "\ttest");
  1769. CHECK(code_edit->get_line(1) == "\ttext");
  1770. CHECK(code_edit->has_selection());
  1771. CHECK(code_edit->get_selection_origin_line() == 0);
  1772. CHECK(code_edit->get_selection_origin_column() == 0);
  1773. CHECK(code_edit->get_caret_line() == 1);
  1774. CHECK(code_edit->get_caret_column() == 5);
  1775. /* Do not unindent line if last col is zero. */
  1776. code_edit->set_text("\ttest\n\ttext");
  1777. code_edit->select(0, 0, 1, 0);
  1778. code_edit->unindent_lines();
  1779. CHECK(code_edit->get_line(0) == "test");
  1780. CHECK(code_edit->get_line(1) == "\ttext");
  1781. CHECK(code_edit->has_selection());
  1782. CHECK(code_edit->get_selection_origin_line() == 0);
  1783. CHECK(code_edit->get_selection_origin_column() == 0);
  1784. CHECK(code_edit->get_caret_line() == 1);
  1785. CHECK(code_edit->get_caret_column() == 0);
  1786. // Do not unindent line if last col is zero, right to left selection.
  1787. code_edit->set_text("\ttest\n\ttext");
  1788. code_edit->select(1, 0, 0, 0);
  1789. code_edit->unindent_lines();
  1790. CHECK(code_edit->get_line(0) == "test");
  1791. CHECK(code_edit->get_line(1) == "\ttext");
  1792. CHECK(code_edit->has_selection());
  1793. CHECK(code_edit->get_selection_origin_line() == 1);
  1794. CHECK(code_edit->get_selection_origin_column() == 0);
  1795. CHECK(code_edit->get_caret_line() == 0);
  1796. CHECK(code_edit->get_caret_column() == 0);
  1797. /* Unindent even if last column of first line. */
  1798. code_edit->set_text("\ttest\n\ttext");
  1799. code_edit->select(0, 5, 1, 1);
  1800. code_edit->unindent_lines();
  1801. CHECK(code_edit->get_line(0) == "test");
  1802. CHECK(code_edit->get_line(1) == "text");
  1803. CHECK(code_edit->has_selection());
  1804. CHECK(code_edit->get_selection_origin_line() == 0);
  1805. CHECK(code_edit->get_selection_origin_column() == 4);
  1806. CHECK(code_edit->get_caret_line() == 1);
  1807. CHECK(code_edit->get_caret_column() == 0);
  1808. /* Check selection is adjusted. */
  1809. code_edit->set_text("\ttest");
  1810. code_edit->select(0, 1, 0, 2);
  1811. code_edit->unindent_lines();
  1812. CHECK(code_edit->get_line(0) == "test");
  1813. CHECK(code_edit->has_selection());
  1814. CHECK(code_edit->get_selection_origin_line() == 0);
  1815. CHECK(code_edit->get_selection_origin_column() == 0);
  1816. CHECK(code_edit->get_caret_line() == 0);
  1817. CHECK(code_edit->get_caret_column() == 1);
  1818. // Deselect if only the tab was selected.
  1819. code_edit->set_text("\ttest");
  1820. code_edit->select(0, 0, 0, 1);
  1821. code_edit->unindent_lines();
  1822. CHECK(code_edit->get_line(0) == "test");
  1823. CHECK_FALSE(code_edit->has_selection());
  1824. CHECK(code_edit->get_caret_line() == 0);
  1825. CHECK(code_edit->get_caret_column() == 0);
  1826. // Unindent once with multiple selections.
  1827. code_edit->set_text("\t\ttest");
  1828. code_edit->select(0, 1, 0, 2);
  1829. code_edit->add_caret(0, 4);
  1830. code_edit->select(0, 4, 0, 3, 1);
  1831. code_edit->unindent_lines();
  1832. CHECK(code_edit->get_line(0) == "\ttest");
  1833. CHECK(code_edit->get_caret_count() == 2);
  1834. CHECK(code_edit->has_selection(0));
  1835. CHECK(code_edit->get_selection_origin_line(0) == 0);
  1836. CHECK(code_edit->get_selection_origin_column(0) == 0);
  1837. CHECK(code_edit->get_caret_line(0) == 0);
  1838. CHECK(code_edit->get_caret_column(0) == 1);
  1839. CHECK(code_edit->has_selection(1));
  1840. CHECK(code_edit->get_selection_origin_line(1) == 0);
  1841. CHECK(code_edit->get_selection_origin_column(1) == 3);
  1842. CHECK(code_edit->get_caret_line(1) == 0);
  1843. CHECK(code_edit->get_caret_column(1) == 2);
  1844. }
  1845. SUBCASE("[CodeEdit] unindent spaces") {
  1846. code_edit->set_indent_size(4);
  1847. code_edit->set_auto_indent_enabled(true);
  1848. code_edit->set_indent_using_spaces(true);
  1849. /* Do nothing if not editable. */
  1850. code_edit->set_text(" ");
  1851. code_edit->set_editable(false);
  1852. code_edit->unindent_lines();
  1853. CHECK(code_edit->get_line(0) == " ");
  1854. code_edit->unindent_lines();
  1855. CHECK(code_edit->get_line(0) == " ");
  1856. code_edit->set_editable(true);
  1857. /* Simple unindent. */
  1858. code_edit->unindent_lines();
  1859. CHECK(code_edit->get_line(0) == "");
  1860. /* Backspace does a simple unindent. */
  1861. code_edit->set_text("");
  1862. code_edit->insert_text_at_caret(" ");
  1863. code_edit->backspace();
  1864. CHECK(code_edit->get_line(0) == "");
  1865. /* Backspace with letter. */
  1866. code_edit->set_text("");
  1867. code_edit->insert_text_at_caret(" a");
  1868. code_edit->backspace();
  1869. CHECK(code_edit->get_line(0) == " ");
  1870. /* Unindent lines does entire line and works without selection. */
  1871. code_edit->set_text("");
  1872. code_edit->insert_text_at_caret(" test");
  1873. code_edit->unindent_lines();
  1874. CHECK(code_edit->get_line(0) == "test");
  1875. CHECK(code_edit->get_caret_column() == 4);
  1876. // Unindent lines once with multiple carets.
  1877. code_edit->set_text(" test");
  1878. code_edit->set_caret_column(1);
  1879. code_edit->add_caret(0, 9);
  1880. code_edit->unindent_lines();
  1881. CHECK(code_edit->get_line(0) == " test");
  1882. CHECK(code_edit->get_caret_count() == 2);
  1883. CHECK_FALSE(code_edit->has_selection());
  1884. CHECK(code_edit->get_caret_line(0) == 0);
  1885. CHECK(code_edit->get_caret_column(0) == 0);
  1886. CHECK(code_edit->get_caret_line(1) == 0);
  1887. CHECK(code_edit->get_caret_column(1) == 5);
  1888. code_edit->remove_secondary_carets();
  1889. /* Caret on col zero unindent line. */
  1890. code_edit->set_text(" test");
  1891. code_edit->set_caret_column(0);
  1892. code_edit->unindent_lines();
  1893. CHECK(code_edit->get_line(0) == " test");
  1894. CHECK(code_edit->get_caret_column() == 0);
  1895. /* Only as far as needed */
  1896. code_edit->set_text(" test");
  1897. code_edit->unindent_lines();
  1898. CHECK(code_edit->get_line(0) == " test");
  1899. /* Check input action. */
  1900. code_edit->set_text(" test");
  1901. SEND_GUI_ACTION("ui_text_dedent");
  1902. CHECK(code_edit->get_line(0) == " test");
  1903. /* Selection does entire line. */
  1904. code_edit->set_text(" test");
  1905. code_edit->select_all();
  1906. code_edit->unindent_lines();
  1907. CHECK(code_edit->get_line(0) == " test");
  1908. CHECK(code_edit->has_selection());
  1909. CHECK(code_edit->get_selection_origin_line() == 0);
  1910. CHECK(code_edit->get_selection_origin_column() == 0);
  1911. CHECK(code_edit->get_caret_line() == 0);
  1912. CHECK(code_edit->get_caret_column() == 8);
  1913. // Selection does entire line, right to left selection.
  1914. code_edit->set_text(" test");
  1915. code_edit->select(0, 12, 0, 0);
  1916. code_edit->unindent_lines();
  1917. CHECK(code_edit->get_line(0) == " test");
  1918. CHECK(code_edit->has_selection());
  1919. CHECK(code_edit->get_selection_origin_line() == 0);
  1920. CHECK(code_edit->get_selection_origin_column() == 8);
  1921. CHECK(code_edit->get_caret_line() == 0);
  1922. CHECK(code_edit->get_caret_column() == 0);
  1923. // Handles multiple lines.
  1924. code_edit->set_text(" test\n text");
  1925. code_edit->select_all();
  1926. code_edit->unindent_lines();
  1927. CHECK(code_edit->get_line(0) == " test");
  1928. CHECK(code_edit->get_line(1) == " text");
  1929. CHECK(code_edit->has_selection());
  1930. CHECK(code_edit->get_selection_origin_line() == 0);
  1931. CHECK(code_edit->get_selection_origin_column() == 0);
  1932. CHECK(code_edit->get_caret_line() == 1);
  1933. CHECK(code_edit->get_caret_column() == 8);
  1934. /* Do not unindent line if last col is zero. */
  1935. code_edit->set_text(" test\n text");
  1936. code_edit->select(0, 0, 1, 0);
  1937. code_edit->unindent_lines();
  1938. CHECK(code_edit->get_line(0) == "test");
  1939. CHECK(code_edit->get_line(1) == " text");
  1940. CHECK(code_edit->has_selection());
  1941. CHECK(code_edit->get_selection_origin_line() == 0);
  1942. CHECK(code_edit->get_selection_origin_column() == 0);
  1943. CHECK(code_edit->get_caret_line() == 1);
  1944. CHECK(code_edit->get_caret_column() == 0);
  1945. // Do not unindent line if last col is zero, right to left selection.
  1946. code_edit->set_text(" test\n text");
  1947. code_edit->select(1, 0, 0, 0);
  1948. code_edit->unindent_lines();
  1949. CHECK(code_edit->get_line(0) == "test");
  1950. CHECK(code_edit->get_line(1) == " text");
  1951. CHECK(code_edit->has_selection());
  1952. CHECK(code_edit->get_selection_origin_line() == 1);
  1953. CHECK(code_edit->get_selection_origin_column() == 0);
  1954. CHECK(code_edit->get_caret_line() == 0);
  1955. CHECK(code_edit->get_caret_column() == 0);
  1956. /* Unindent even if last column of first line. */
  1957. code_edit->set_text(" test\n text");
  1958. code_edit->select(0, 5, 1, 1);
  1959. code_edit->unindent_lines();
  1960. CHECK(code_edit->get_line(0) == "test");
  1961. CHECK(code_edit->get_line(1) == "text");
  1962. CHECK(code_edit->has_selection());
  1963. CHECK(code_edit->get_selection_origin_line() == 0);
  1964. CHECK(code_edit->get_selection_origin_column() == 1);
  1965. CHECK(code_edit->get_caret_line() == 1);
  1966. CHECK(code_edit->get_caret_column() == 0);
  1967. /* Check selection is adjusted. */
  1968. code_edit->set_text(" test");
  1969. code_edit->select(0, 4, 0, 5);
  1970. code_edit->unindent_lines();
  1971. CHECK(code_edit->get_line(0) == "test");
  1972. CHECK(code_edit->has_selection());
  1973. CHECK(code_edit->get_selection_origin_line() == 0);
  1974. CHECK(code_edit->get_selection_origin_column() == 0);
  1975. CHECK(code_edit->get_caret_line() == 0);
  1976. CHECK(code_edit->get_caret_column() == 1);
  1977. // Deselect if only the tab was selected.
  1978. code_edit->set_text(" test");
  1979. code_edit->select(0, 0, 0, 4);
  1980. code_edit->unindent_lines();
  1981. CHECK(code_edit->get_line(0) == "test");
  1982. CHECK_FALSE(code_edit->has_selection());
  1983. CHECK(code_edit->get_caret_line() == 0);
  1984. CHECK(code_edit->get_caret_column() == 0);
  1985. // Unindent once with multiple selections.
  1986. code_edit->set_text(" test");
  1987. code_edit->select(0, 1, 0, 2);
  1988. code_edit->add_caret(0, 4);
  1989. code_edit->select(0, 12, 0, 10, 1);
  1990. code_edit->unindent_lines();
  1991. CHECK(code_edit->get_line(0) == " test");
  1992. CHECK(code_edit->get_caret_count() == 2);
  1993. CHECK_FALSE(code_edit->has_selection(0));
  1994. CHECK(code_edit->get_caret_line(0) == 0);
  1995. CHECK(code_edit->get_caret_column(0) == 0);
  1996. CHECK(code_edit->has_selection(1));
  1997. CHECK(code_edit->get_selection_origin_line(1) == 0);
  1998. CHECK(code_edit->get_selection_origin_column(1) == 8);
  1999. CHECK(code_edit->get_caret_line(1) == 0);
  2000. CHECK(code_edit->get_caret_column(1) == 6);
  2001. }
  2002. SUBCASE("[CodeEdit] auto indent") {
  2003. SUBCASE("[CodeEdit] auto indent tabs") {
  2004. code_edit->set_indent_size(4);
  2005. code_edit->set_auto_indent_enabled(true);
  2006. code_edit->set_indent_using_spaces(false);
  2007. /* Simple indent on new line. */
  2008. code_edit->set_text("");
  2009. code_edit->insert_text_at_caret("test:");
  2010. SEND_GUI_ACTION("ui_text_newline");
  2011. CHECK(code_edit->get_line(0) == "test:");
  2012. CHECK(code_edit->get_line(1) == "\t");
  2013. CHECK(code_edit->get_caret_line() == 1);
  2014. CHECK(code_edit->get_caret_column() == 1);
  2015. /* new blank line should still indent. */
  2016. code_edit->set_text("");
  2017. code_edit->insert_text_at_caret("test:");
  2018. SEND_GUI_ACTION("ui_text_newline_blank");
  2019. CHECK(code_edit->get_line(0) == "test:");
  2020. CHECK(code_edit->get_line(1) == "\t");
  2021. CHECK(code_edit->get_caret_line() == 1);
  2022. CHECK(code_edit->get_caret_column() == 1);
  2023. /* new line above should not indent. */
  2024. code_edit->set_text("");
  2025. code_edit->insert_text_at_caret("test:");
  2026. SEND_GUI_ACTION("ui_text_newline_above");
  2027. CHECK(code_edit->get_line(0) == "");
  2028. CHECK(code_edit->get_line(1) == "test:");
  2029. CHECK(code_edit->get_caret_line() == 0);
  2030. CHECK(code_edit->get_caret_column() == 0);
  2031. /* Whitespace between symbol and caret is okay. */
  2032. code_edit->set_text("");
  2033. code_edit->insert_text_at_caret("test: ");
  2034. SEND_GUI_ACTION("ui_text_newline");
  2035. CHECK(code_edit->get_line(0) == "test: ");
  2036. CHECK(code_edit->get_line(1) == "\t");
  2037. CHECK(code_edit->get_caret_line() == 1);
  2038. CHECK(code_edit->get_caret_column() == 1);
  2039. /* Comment between symbol and caret is okay. */
  2040. code_edit->add_comment_delimiter("#", "");
  2041. code_edit->set_text("");
  2042. code_edit->insert_text_at_caret("test: # comment");
  2043. SEND_GUI_ACTION("ui_text_newline");
  2044. CHECK(code_edit->get_line(0) == "test: # comment");
  2045. CHECK(code_edit->get_line(1) == "\t");
  2046. code_edit->remove_comment_delimiter("#");
  2047. CHECK(code_edit->get_caret_line() == 1);
  2048. CHECK(code_edit->get_caret_column() == 1);
  2049. /* Strings between symbol and caret are not okay. */
  2050. code_edit->add_string_delimiter("#", "");
  2051. code_edit->set_text("");
  2052. code_edit->insert_text_at_caret("test: # string");
  2053. SEND_GUI_ACTION("ui_text_newline");
  2054. CHECK(code_edit->get_line(0) == "test: # string");
  2055. CHECK(code_edit->get_line(1) == "");
  2056. code_edit->remove_string_delimiter("#");
  2057. CHECK(code_edit->get_caret_line() == 1);
  2058. CHECK(code_edit->get_caret_column() == 0);
  2059. /* Non-whitespace prevents auto-indentation. */
  2060. code_edit->add_comment_delimiter("#", "");
  2061. code_edit->set_text("");
  2062. code_edit->insert_text_at_caret("test := 0 # comment");
  2063. SEND_GUI_ACTION("ui_text_newline");
  2064. CHECK(code_edit->get_line(0) == "test := 0 # comment");
  2065. CHECK(code_edit->get_line(1) == "");
  2066. code_edit->remove_comment_delimiter("#");
  2067. CHECK(code_edit->get_caret_line() == 1);
  2068. CHECK(code_edit->get_caret_column() == 0);
  2069. /* Even when there's no comments. */
  2070. code_edit->set_text("");
  2071. code_edit->insert_text_at_caret("test := 0");
  2072. SEND_GUI_ACTION("ui_text_newline");
  2073. CHECK(code_edit->get_line(0) == "test := 0");
  2074. CHECK(code_edit->get_line(1) == "");
  2075. CHECK(code_edit->get_caret_line() == 1);
  2076. CHECK(code_edit->get_caret_column() == 0);
  2077. // Preserve current indentation.
  2078. code_edit->set_text("\ttest");
  2079. code_edit->set_caret_column(3);
  2080. SEND_GUI_ACTION("ui_text_newline");
  2081. CHECK(code_edit->get_line(0) == "\tte");
  2082. CHECK(code_edit->get_line(1) == "\tst");
  2083. CHECK(code_edit->get_caret_line() == 1);
  2084. CHECK(code_edit->get_caret_column() == 1);
  2085. // Preserve current indentation blank.
  2086. code_edit->set_text("\ttest");
  2087. code_edit->set_caret_column(3);
  2088. SEND_GUI_ACTION("ui_text_newline_blank");
  2089. CHECK(code_edit->get_line(0) == "\ttest");
  2090. CHECK(code_edit->get_line(1) == "\t");
  2091. CHECK(code_edit->get_caret_line() == 1);
  2092. CHECK(code_edit->get_caret_column() == 1);
  2093. // Preserve current indentation above.
  2094. code_edit->set_text("\ttest");
  2095. code_edit->set_caret_column(3);
  2096. SEND_GUI_ACTION("ui_text_newline_above");
  2097. CHECK(code_edit->get_line(0) == "\t");
  2098. CHECK(code_edit->get_line(1) == "\ttest");
  2099. CHECK(code_edit->get_caret_line() == 0);
  2100. CHECK(code_edit->get_caret_column() == 1);
  2101. // Increase existing indentation.
  2102. code_edit->set_text("\ttest:");
  2103. code_edit->set_caret_column(6);
  2104. SEND_GUI_ACTION("ui_text_newline");
  2105. CHECK(code_edit->get_line(0) == "\ttest:");
  2106. CHECK(code_edit->get_line(1) == "\t\t");
  2107. CHECK(code_edit->get_caret_line() == 1);
  2108. CHECK(code_edit->get_caret_column() == 2);
  2109. // Increase existing indentation blank.
  2110. code_edit->set_text("\ttest:");
  2111. code_edit->set_caret_column(3);
  2112. SEND_GUI_ACTION("ui_text_newline_blank");
  2113. CHECK(code_edit->get_line(0) == "\ttest:");
  2114. CHECK(code_edit->get_line(1) == "\t\t");
  2115. CHECK(code_edit->get_caret_line() == 1);
  2116. CHECK(code_edit->get_caret_column() == 2);
  2117. /* If between brace pairs an extra line is added. */
  2118. code_edit->set_text("");
  2119. code_edit->insert_text_at_caret("test{}");
  2120. code_edit->set_caret_column(5);
  2121. SEND_GUI_ACTION("ui_text_newline");
  2122. CHECK(code_edit->get_line(0) == "test{");
  2123. CHECK(code_edit->get_line(1) == "\t");
  2124. CHECK(code_edit->get_line(2) == "}");
  2125. CHECK(code_edit->get_caret_line() == 1);
  2126. CHECK(code_edit->get_caret_column() == 1);
  2127. /* Except when we are going above. */
  2128. code_edit->set_text("");
  2129. code_edit->insert_text_at_caret("test{}");
  2130. code_edit->set_caret_column(5);
  2131. SEND_GUI_ACTION("ui_text_newline_above");
  2132. CHECK(code_edit->get_line(0) == "");
  2133. CHECK(code_edit->get_line(1) == "test{}");
  2134. CHECK(code_edit->get_caret_line() == 0);
  2135. CHECK(code_edit->get_caret_column() == 0);
  2136. /* or below. */
  2137. code_edit->set_text("");
  2138. code_edit->insert_text_at_caret("test{}");
  2139. code_edit->set_caret_column(5);
  2140. SEND_GUI_ACTION("ui_text_newline_blank");
  2141. CHECK(code_edit->get_line(0) == "test{}");
  2142. CHECK(code_edit->get_line(1) == "");
  2143. CHECK(code_edit->get_caret_line() == 1);
  2144. CHECK(code_edit->get_caret_column() == 0);
  2145. }
  2146. SUBCASE("[CodeEdit] auto indent spaces") {
  2147. code_edit->set_indent_size(4);
  2148. code_edit->set_auto_indent_enabled(true);
  2149. code_edit->set_indent_using_spaces(true);
  2150. /* Simple indent on new line. */
  2151. code_edit->set_text("");
  2152. code_edit->insert_text_at_caret("test:");
  2153. SEND_GUI_ACTION("ui_text_newline");
  2154. CHECK(code_edit->get_line(0) == "test:");
  2155. CHECK(code_edit->get_line(1) == " ");
  2156. CHECK(code_edit->get_caret_line() == 1);
  2157. CHECK(code_edit->get_caret_column() == 4);
  2158. /* new blank line should still indent. */
  2159. code_edit->set_text("");
  2160. code_edit->insert_text_at_caret("test:");
  2161. SEND_GUI_ACTION("ui_text_newline_blank");
  2162. CHECK(code_edit->get_line(0) == "test:");
  2163. CHECK(code_edit->get_line(1) == " ");
  2164. CHECK(code_edit->get_caret_line() == 1);
  2165. CHECK(code_edit->get_caret_column() == 4);
  2166. /* new line above should not indent. */
  2167. code_edit->set_text("");
  2168. code_edit->insert_text_at_caret("test:");
  2169. SEND_GUI_ACTION("ui_text_newline_above");
  2170. CHECK(code_edit->get_line(0) == "");
  2171. CHECK(code_edit->get_line(1) == "test:");
  2172. CHECK(code_edit->get_caret_line() == 0);
  2173. CHECK(code_edit->get_caret_column() == 0);
  2174. /* Whitespace between symbol and caret is okay. */
  2175. code_edit->set_text("");
  2176. code_edit->insert_text_at_caret("test: ");
  2177. SEND_GUI_ACTION("ui_text_newline");
  2178. CHECK(code_edit->get_line(0) == "test: ");
  2179. CHECK(code_edit->get_line(1) == " ");
  2180. CHECK(code_edit->get_caret_line() == 1);
  2181. CHECK(code_edit->get_caret_column() == 4);
  2182. /* Comment between symbol and caret is okay. */
  2183. code_edit->add_comment_delimiter("#", "");
  2184. code_edit->set_text("");
  2185. code_edit->insert_text_at_caret("test: # comment");
  2186. SEND_GUI_ACTION("ui_text_newline");
  2187. CHECK(code_edit->get_line(0) == "test: # comment");
  2188. CHECK(code_edit->get_line(1) == " ");
  2189. code_edit->remove_comment_delimiter("#");
  2190. CHECK(code_edit->get_caret_line() == 1);
  2191. CHECK(code_edit->get_caret_column() == 4);
  2192. /* Strings between symbol and caret are not okay. */
  2193. code_edit->add_string_delimiter("#", "");
  2194. code_edit->set_text("");
  2195. code_edit->insert_text_at_caret("test: # string");
  2196. SEND_GUI_ACTION("ui_text_newline");
  2197. CHECK(code_edit->get_line(0) == "test: # string");
  2198. CHECK(code_edit->get_line(1) == "");
  2199. code_edit->remove_string_delimiter("#");
  2200. CHECK(code_edit->get_caret_line() == 1);
  2201. CHECK(code_edit->get_caret_column() == 0);
  2202. /* Non-whitespace prevents auto-indentation. */
  2203. code_edit->add_comment_delimiter("#", "");
  2204. code_edit->set_text("");
  2205. code_edit->insert_text_at_caret("test := 0 # comment");
  2206. SEND_GUI_ACTION("ui_text_newline");
  2207. CHECK(code_edit->get_line(0) == "test := 0 # comment");
  2208. CHECK(code_edit->get_line(1) == "");
  2209. code_edit->remove_comment_delimiter("#");
  2210. CHECK(code_edit->get_caret_line() == 1);
  2211. CHECK(code_edit->get_caret_column() == 0);
  2212. /* Even when there's no comments. */
  2213. code_edit->set_text("");
  2214. code_edit->insert_text_at_caret("test := 0");
  2215. SEND_GUI_ACTION("ui_text_newline");
  2216. CHECK(code_edit->get_line(0) == "test := 0");
  2217. CHECK(code_edit->get_line(1) == "");
  2218. CHECK(code_edit->get_caret_line() == 1);
  2219. CHECK(code_edit->get_caret_column() == 0);
  2220. // Preserve current indentation.
  2221. code_edit->set_text(" test");
  2222. code_edit->set_caret_column(6);
  2223. SEND_GUI_ACTION("ui_text_newline");
  2224. CHECK(code_edit->get_line(0) == " te");
  2225. CHECK(code_edit->get_line(1) == " st");
  2226. CHECK(code_edit->get_caret_line() == 1);
  2227. CHECK(code_edit->get_caret_column() == 4);
  2228. // Preserve current indentation blank.
  2229. code_edit->set_text(" test");
  2230. code_edit->set_caret_column(6);
  2231. SEND_GUI_ACTION("ui_text_newline_blank");
  2232. CHECK(code_edit->get_line(0) == " test");
  2233. CHECK(code_edit->get_line(1) == " ");
  2234. CHECK(code_edit->get_caret_line() == 1);
  2235. CHECK(code_edit->get_caret_column() == 4);
  2236. // Preserve current indentation above.
  2237. code_edit->set_text(" test");
  2238. code_edit->set_caret_column(6);
  2239. SEND_GUI_ACTION("ui_text_newline_above");
  2240. CHECK(code_edit->get_line(0) == " ");
  2241. CHECK(code_edit->get_line(1) == " test");
  2242. CHECK(code_edit->get_caret_line() == 0);
  2243. CHECK(code_edit->get_caret_column() == 4);
  2244. // Increase existing indentation.
  2245. code_edit->set_text(" test:");
  2246. code_edit->set_caret_column(9);
  2247. SEND_GUI_ACTION("ui_text_newline");
  2248. CHECK(code_edit->get_line(0) == " test:");
  2249. CHECK(code_edit->get_line(1) == " ");
  2250. CHECK(code_edit->get_caret_line() == 1);
  2251. CHECK(code_edit->get_caret_column() == 8);
  2252. // Increase existing indentation blank.
  2253. code_edit->set_text(" test:");
  2254. code_edit->set_caret_column(9);
  2255. SEND_GUI_ACTION("ui_text_newline");
  2256. CHECK(code_edit->get_line(0) == " test:");
  2257. CHECK(code_edit->get_line(1) == " ");
  2258. CHECK(code_edit->get_caret_line() == 1);
  2259. CHECK(code_edit->get_caret_column() == 8);
  2260. /* If between brace pairs an extra line is added. */
  2261. code_edit->set_text("");
  2262. code_edit->insert_text_at_caret("test{}");
  2263. code_edit->set_caret_column(5);
  2264. SEND_GUI_ACTION("ui_text_newline");
  2265. CHECK(code_edit->get_line(0) == "test{");
  2266. CHECK(code_edit->get_line(1) == " ");
  2267. CHECK(code_edit->get_line(2) == "}");
  2268. CHECK(code_edit->get_caret_line() == 1);
  2269. CHECK(code_edit->get_caret_column() == 4);
  2270. /* Except when we are going above. */
  2271. code_edit->set_text("");
  2272. code_edit->insert_text_at_caret("test{}");
  2273. code_edit->set_caret_column(5);
  2274. SEND_GUI_ACTION("ui_text_newline_above");
  2275. CHECK(code_edit->get_line(0) == "");
  2276. CHECK(code_edit->get_line(1) == "test{}");
  2277. CHECK(code_edit->get_caret_line() == 0);
  2278. CHECK(code_edit->get_caret_column() == 0);
  2279. /* or below. */
  2280. code_edit->set_text("");
  2281. code_edit->insert_text_at_caret("test{}");
  2282. code_edit->set_caret_column(5);
  2283. SEND_GUI_ACTION("ui_text_newline_blank");
  2284. CHECK(code_edit->get_line(0) == "test{}");
  2285. CHECK(code_edit->get_line(1) == "");
  2286. CHECK(code_edit->get_caret_line() == 1);
  2287. CHECK(code_edit->get_caret_column() == 0);
  2288. /* If there is something after a colon
  2289. and there is a colon in the comment it
  2290. should not indent. */
  2291. code_edit->add_comment_delimiter("#", "");
  2292. code_edit->set_text("");
  2293. code_edit->insert_text_at_caret("test:test#:");
  2294. SEND_GUI_ACTION("ui_text_newline");
  2295. CHECK(code_edit->get_line(0) == "test:test#:");
  2296. CHECK(code_edit->get_line(1) == "");
  2297. code_edit->remove_comment_delimiter("#");
  2298. CHECK(code_edit->get_caret_line() == 1);
  2299. CHECK(code_edit->get_caret_column() == 0);
  2300. }
  2301. }
  2302. SUBCASE("[CodeEdit] convert indent to tabs") {
  2303. code_edit->set_indent_size(4);
  2304. code_edit->set_indent_using_spaces(false);
  2305. // Only line.
  2306. code_edit->set_text(" test");
  2307. code_edit->select(0, 9, 0, 8);
  2308. code_edit->convert_indent();
  2309. CHECK(code_edit->get_line(0) == "\t\ttest");
  2310. CHECK(code_edit->has_selection());
  2311. CHECK(code_edit->get_selection_origin_column() == 3);
  2312. CHECK(code_edit->get_caret_column() == 2);
  2313. // First line.
  2314. code_edit->set_text(" test\n");
  2315. code_edit->select(0, 8, 0, 9);
  2316. code_edit->convert_indent();
  2317. CHECK(code_edit->get_line(0) == "\t\ttest");
  2318. CHECK(code_edit->has_selection());
  2319. CHECK(code_edit->get_selection_origin_column() == 2);
  2320. CHECK(code_edit->get_caret_column() == 3);
  2321. // Middle line.
  2322. code_edit->set_text("\n test\n");
  2323. code_edit->select(1, 8, 1, 9);
  2324. code_edit->convert_indent();
  2325. CHECK(code_edit->get_line(1) == "\t\ttest");
  2326. CHECK(code_edit->has_selection());
  2327. CHECK(code_edit->get_selection_origin_column() == 2);
  2328. CHECK(code_edit->get_caret_column() == 3);
  2329. // End line.
  2330. code_edit->set_text("\n test");
  2331. code_edit->select(1, 8, 1, 9);
  2332. code_edit->convert_indent();
  2333. CHECK(code_edit->get_line(1) == "\t\ttest");
  2334. CHECK(code_edit->has_selection());
  2335. CHECK(code_edit->get_selection_origin_column() == 2);
  2336. CHECK(code_edit->get_caret_column() == 3);
  2337. // Within provided range.
  2338. code_edit->set_text(" test\n test\n");
  2339. code_edit->select(1, 8, 1, 9);
  2340. code_edit->convert_indent(1, 1);
  2341. CHECK(code_edit->get_line(0) == " test");
  2342. CHECK(code_edit->get_line(1) == "\t\ttest");
  2343. CHECK(code_edit->has_selection());
  2344. CHECK(code_edit->get_selection_origin_column() == 2);
  2345. CHECK(code_edit->get_caret_column() == 3);
  2346. }
  2347. SUBCASE("[CodeEdit] convert indent to spaces") {
  2348. code_edit->set_indent_size(4);
  2349. code_edit->set_indent_using_spaces(true);
  2350. // Only line.
  2351. code_edit->set_text("\t\ttest");
  2352. code_edit->select(0, 3, 0, 2);
  2353. code_edit->convert_indent();
  2354. CHECK(code_edit->get_line(0) == " test");
  2355. CHECK(code_edit->has_selection());
  2356. CHECK(code_edit->get_selection_origin_column() == 9);
  2357. CHECK(code_edit->get_caret_column() == 8);
  2358. // First line.
  2359. code_edit->set_text("\t\ttest\n");
  2360. code_edit->select(0, 2, 0, 3);
  2361. code_edit->convert_indent();
  2362. CHECK(code_edit->get_line(0) == " test");
  2363. CHECK(code_edit->has_selection());
  2364. CHECK(code_edit->get_selection_origin_column() == 8);
  2365. CHECK(code_edit->get_caret_column() == 9);
  2366. // Middle line.
  2367. code_edit->set_text("\n\t\ttest\n");
  2368. code_edit->select(1, 2, 1, 3);
  2369. code_edit->convert_indent();
  2370. CHECK(code_edit->get_line(1) == " test");
  2371. CHECK(code_edit->has_selection());
  2372. CHECK(code_edit->get_selection_origin_column() == 8);
  2373. CHECK(code_edit->get_caret_column() == 9);
  2374. // End line.
  2375. code_edit->set_text("\n\t\ttest");
  2376. code_edit->select(1, 2, 1, 3);
  2377. code_edit->convert_indent();
  2378. CHECK(code_edit->get_line(1) == " test");
  2379. CHECK(code_edit->has_selection());
  2380. CHECK(code_edit->get_selection_origin_column() == 8);
  2381. CHECK(code_edit->get_caret_column() == 9);
  2382. // Within provided range.
  2383. code_edit->set_text("\ttest\n\t\ttest\n");
  2384. code_edit->select(1, 2, 1, 3);
  2385. code_edit->convert_indent(1, 1);
  2386. CHECK(code_edit->get_line(0) == "\ttest");
  2387. CHECK(code_edit->get_line(1) == " test");
  2388. CHECK(code_edit->has_selection());
  2389. CHECK(code_edit->get_selection_origin_column() == 8);
  2390. CHECK(code_edit->get_caret_column() == 9);
  2391. // Outside of range.
  2392. ERR_PRINT_OFF;
  2393. code_edit->convert_indent(0, 4);
  2394. code_edit->convert_indent(4, 5);
  2395. code_edit->convert_indent(4, 1);
  2396. ERR_PRINT_ON;
  2397. }
  2398. memdelete(code_edit);
  2399. }
  2400. TEST_CASE("[SceneTree][CodeEdit] folding") {
  2401. CodeEdit *code_edit = memnew(CodeEdit);
  2402. SceneTree::get_singleton()->get_root()->add_child(code_edit);
  2403. code_edit->grab_focus();
  2404. code_edit->set_line_folding_enabled(true);
  2405. SUBCASE("[CodeEdit] folding settings") {
  2406. code_edit->set_line_folding_enabled(true);
  2407. CHECK(code_edit->is_line_folding_enabled());
  2408. code_edit->set_line_folding_enabled(false);
  2409. CHECK_FALSE(code_edit->is_line_folding_enabled());
  2410. }
  2411. SUBCASE("[CodeEdit] folding") {
  2412. // No indent.
  2413. code_edit->set_text("line1\nline2\nline3");
  2414. for (int i = 0; i < 2; i++) {
  2415. CHECK_FALSE(code_edit->can_fold_line(i));
  2416. code_edit->fold_line(i);
  2417. CHECK_FALSE(code_edit->is_line_folded(i));
  2418. }
  2419. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2420. // Indented lines.
  2421. code_edit->set_text("\tline1\n\tline2\n\tline3");
  2422. for (int i = 0; i < 2; i++) {
  2423. CHECK_FALSE(code_edit->can_fold_line(i));
  2424. code_edit->fold_line(i);
  2425. CHECK_FALSE(code_edit->is_line_folded(i));
  2426. }
  2427. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2428. // Indent.
  2429. code_edit->set_text("line1\n\tline2\nline3");
  2430. CHECK(code_edit->can_fold_line(0));
  2431. for (int i = 1; i < 2; i++) {
  2432. CHECK_FALSE(code_edit->can_fold_line(i));
  2433. code_edit->fold_line(i);
  2434. CHECK_FALSE(code_edit->is_line_folded(i));
  2435. }
  2436. code_edit->fold_line(0);
  2437. CHECK(code_edit->is_line_folded(0));
  2438. CHECK_FALSE(code_edit->is_line_folded(1));
  2439. CHECK_FALSE(code_edit->is_line_folded(2));
  2440. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 2);
  2441. // Indent with blank lines.
  2442. code_edit->set_text("line1\n\tline2\n\n\nline3");
  2443. CHECK(code_edit->can_fold_line(0));
  2444. for (int i = 1; i < 2; i++) {
  2445. CHECK_FALSE(code_edit->can_fold_line(i));
  2446. code_edit->fold_line(i);
  2447. CHECK_FALSE(code_edit->is_line_folded(i));
  2448. }
  2449. code_edit->fold_line(0);
  2450. CHECK(code_edit->is_line_folded(0));
  2451. CHECK_FALSE(code_edit->is_line_folded(1));
  2452. CHECK_FALSE(code_edit->is_line_folded(2));
  2453. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 2);
  2454. // Nested indents.
  2455. code_edit->set_text("line1\n\tline2\n\t\tline3\nline4");
  2456. CHECK(code_edit->can_fold_line(0));
  2457. CHECK(code_edit->can_fold_line(1));
  2458. for (int i = 2; i < 3; i++) {
  2459. CHECK_FALSE(code_edit->can_fold_line(i));
  2460. code_edit->fold_line(i);
  2461. CHECK_FALSE(code_edit->is_line_folded(i));
  2462. }
  2463. code_edit->fold_line(1);
  2464. CHECK_FALSE(code_edit->is_line_folded(0));
  2465. CHECK(code_edit->is_line_folded(1));
  2466. CHECK_FALSE(code_edit->is_line_folded(2));
  2467. CHECK_FALSE(code_edit->is_line_folded(3));
  2468. CHECK(code_edit->get_next_visible_line_offset_from(2, 1) == 2);
  2469. code_edit->fold_line(0);
  2470. CHECK(code_edit->is_line_folded(0));
  2471. CHECK_FALSE(code_edit->is_line_folded(1));
  2472. CHECK_FALSE(code_edit->is_line_folded(2));
  2473. CHECK_FALSE(code_edit->is_line_folded(3));
  2474. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 3);
  2475. // Check metadata.
  2476. CHECK(code_edit->get_folded_lines().size() == 1);
  2477. CHECK((int)code_edit->get_folded_lines()[0] == 0);
  2478. // Cannot unfold nested.
  2479. code_edit->unfold_line(1);
  2480. CHECK_FALSE(code_edit->is_line_folded(0));
  2481. CHECK_FALSE(code_edit->is_line_folded(1));
  2482. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2483. // (un)Fold all / toggle.
  2484. code_edit->unfold_line(0);
  2485. CHECK_FALSE(code_edit->is_line_folded(0));
  2486. CHECK_FALSE(code_edit->is_line_folded(1));
  2487. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2488. // Check metadata.
  2489. CHECK(code_edit->get_folded_lines().size() == 0);
  2490. code_edit->fold_all_lines();
  2491. CHECK(code_edit->is_line_folded(0));
  2492. CHECK_FALSE(code_edit->is_line_folded(1));
  2493. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 3);
  2494. code_edit->unfold_all_lines();
  2495. CHECK_FALSE(code_edit->is_line_folded(0));
  2496. CHECK_FALSE(code_edit->is_line_folded(1));
  2497. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2498. code_edit->toggle_foldable_line(0);
  2499. CHECK(code_edit->is_line_folded(0));
  2500. CHECK_FALSE(code_edit->is_line_folded(1));
  2501. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 3);
  2502. // Can also unfold from hidden line.
  2503. code_edit->unfold_line(1);
  2504. CHECK_FALSE(code_edit->is_line_folded(0));
  2505. CHECK_FALSE(code_edit->is_line_folded(1));
  2506. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2507. // Blank lines.
  2508. code_edit->set_text("line1\n\tline2\n\n\n\ttest\n\nline3");
  2509. CHECK(code_edit->can_fold_line(0));
  2510. for (int i = 1; i < code_edit->get_line_count(); i++) {
  2511. CHECK_FALSE(code_edit->can_fold_line(i));
  2512. code_edit->fold_line(i);
  2513. CHECK_FALSE(code_edit->is_line_folded(i));
  2514. }
  2515. code_edit->fold_line(0);
  2516. CHECK(code_edit->is_line_folded(0));
  2517. for (int i = 1; i < code_edit->get_line_count(); i++) {
  2518. CHECK_FALSE(code_edit->is_line_folded(i));
  2519. }
  2520. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 5);
  2521. // End of file.
  2522. code_edit->set_text("line1\n\tline2");
  2523. CHECK(code_edit->can_fold_line(0));
  2524. CHECK_FALSE(code_edit->can_fold_line(1));
  2525. code_edit->fold_line(1);
  2526. CHECK_FALSE(code_edit->is_line_folded(1));
  2527. code_edit->fold_line(0);
  2528. CHECK(code_edit->is_line_folded(0));
  2529. CHECK_FALSE(code_edit->is_line_folded(1));
  2530. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2531. // Comment & string blocks.
  2532. // Single line block
  2533. code_edit->add_comment_delimiter("#", "", true);
  2534. code_edit->set_text("#line1\n#\tline2");
  2535. CHECK(code_edit->can_fold_line(0));
  2536. CHECK_FALSE(code_edit->can_fold_line(1));
  2537. code_edit->fold_line(1);
  2538. CHECK_FALSE(code_edit->is_line_folded(1));
  2539. code_edit->fold_line(0);
  2540. CHECK(code_edit->is_line_folded(0));
  2541. CHECK_FALSE(code_edit->is_line_folded(1));
  2542. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2543. // Has to be full line.
  2544. code_edit->set_text("test #line1\n#\tline2");
  2545. CHECK_FALSE(code_edit->can_fold_line(0));
  2546. CHECK_FALSE(code_edit->can_fold_line(1));
  2547. code_edit->fold_line(1);
  2548. CHECK_FALSE(code_edit->is_line_folded(1));
  2549. code_edit->fold_line(0);
  2550. CHECK_FALSE(code_edit->is_line_folded(0));
  2551. CHECK_FALSE(code_edit->is_line_folded(1));
  2552. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2553. code_edit->set_text("#line1\ntest #\tline2");
  2554. CHECK_FALSE(code_edit->can_fold_line(0));
  2555. CHECK_FALSE(code_edit->can_fold_line(1));
  2556. code_edit->fold_line(1);
  2557. CHECK_FALSE(code_edit->is_line_folded(1));
  2558. code_edit->fold_line(0);
  2559. CHECK_FALSE(code_edit->is_line_folded(0));
  2560. CHECK_FALSE(code_edit->is_line_folded(1));
  2561. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2562. // String.
  2563. code_edit->add_string_delimiter("^", "", true);
  2564. code_edit->set_text("^line1\n^\tline2");
  2565. CHECK(code_edit->can_fold_line(0));
  2566. CHECK_FALSE(code_edit->can_fold_line(1));
  2567. code_edit->fold_line(1);
  2568. CHECK_FALSE(code_edit->is_line_folded(1));
  2569. code_edit->fold_line(0);
  2570. CHECK(code_edit->is_line_folded(0));
  2571. CHECK_FALSE(code_edit->is_line_folded(1));
  2572. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2573. // Has to be full line.
  2574. code_edit->set_text("test ^line1\n^\tline2");
  2575. CHECK_FALSE(code_edit->can_fold_line(0));
  2576. CHECK_FALSE(code_edit->can_fold_line(1));
  2577. code_edit->fold_line(1);
  2578. CHECK_FALSE(code_edit->is_line_folded(1));
  2579. code_edit->fold_line(0);
  2580. CHECK_FALSE(code_edit->is_line_folded(0));
  2581. CHECK_FALSE(code_edit->is_line_folded(1));
  2582. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2583. code_edit->set_text("^line1\ntest ^\tline2");
  2584. CHECK_FALSE(code_edit->can_fold_line(0));
  2585. CHECK_FALSE(code_edit->can_fold_line(1));
  2586. code_edit->fold_line(1);
  2587. CHECK_FALSE(code_edit->is_line_folded(1));
  2588. code_edit->fold_line(0);
  2589. CHECK_FALSE(code_edit->is_line_folded(0));
  2590. CHECK_FALSE(code_edit->is_line_folded(1));
  2591. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2592. // Multiline blocks.
  2593. code_edit->add_comment_delimiter("&", "&", false);
  2594. code_edit->set_text("&line1\n\tline2&\nline3");
  2595. CHECK(code_edit->can_fold_line(0));
  2596. CHECK_FALSE(code_edit->can_fold_line(1));
  2597. code_edit->fold_line(1);
  2598. CHECK_FALSE(code_edit->is_line_folded(1));
  2599. code_edit->fold_line(0);
  2600. CHECK(code_edit->is_line_folded(0));
  2601. CHECK_FALSE(code_edit->is_line_folded(1));
  2602. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 2);
  2603. // Multiline comment before last line.
  2604. code_edit->set_text("&line1\nline2&\ntest");
  2605. CHECK(code_edit->can_fold_line(0));
  2606. CHECK_FALSE(code_edit->can_fold_line(2));
  2607. code_edit->fold_line(1);
  2608. CHECK_FALSE(code_edit->is_line_folded(1));
  2609. code_edit->fold_line(0);
  2610. CHECK(code_edit->is_line_folded(0));
  2611. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 2);
  2612. // Has to be full line.
  2613. code_edit->set_text("test &line1\n\tline2&");
  2614. CHECK_FALSE(code_edit->can_fold_line(0));
  2615. CHECK_FALSE(code_edit->can_fold_line(1));
  2616. code_edit->fold_line(1);
  2617. CHECK_FALSE(code_edit->is_line_folded(1));
  2618. code_edit->fold_line(0);
  2619. CHECK_FALSE(code_edit->is_line_folded(0));
  2620. CHECK_FALSE(code_edit->is_line_folded(1));
  2621. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2622. code_edit->set_text("&line1\n\tline2& test");
  2623. CHECK_FALSE(code_edit->can_fold_line(0));
  2624. CHECK_FALSE(code_edit->can_fold_line(1));
  2625. code_edit->fold_line(1);
  2626. CHECK_FALSE(code_edit->is_line_folded(1));
  2627. code_edit->fold_line(0);
  2628. CHECK_FALSE(code_edit->is_line_folded(0));
  2629. CHECK_FALSE(code_edit->is_line_folded(1));
  2630. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2631. // Strings.
  2632. code_edit->add_string_delimiter("$", "$", false);
  2633. code_edit->set_text("$line1\n\tline2$");
  2634. CHECK(code_edit->can_fold_line(0));
  2635. CHECK_FALSE(code_edit->can_fold_line(1));
  2636. code_edit->fold_line(1);
  2637. CHECK_FALSE(code_edit->is_line_folded(1));
  2638. code_edit->fold_line(0);
  2639. CHECK(code_edit->is_line_folded(0));
  2640. CHECK_FALSE(code_edit->is_line_folded(1));
  2641. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2642. // Has to be full line.
  2643. code_edit->set_text("test $line1\n\tline2$");
  2644. CHECK_FALSE(code_edit->can_fold_line(0));
  2645. CHECK_FALSE(code_edit->can_fold_line(1));
  2646. code_edit->fold_line(1);
  2647. CHECK_FALSE(code_edit->is_line_folded(1));
  2648. code_edit->fold_line(0);
  2649. CHECK_FALSE(code_edit->is_line_folded(0));
  2650. CHECK_FALSE(code_edit->is_line_folded(1));
  2651. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2652. code_edit->set_text("$line1\n\tline2$ test");
  2653. CHECK_FALSE(code_edit->can_fold_line(0));
  2654. CHECK_FALSE(code_edit->can_fold_line(1));
  2655. code_edit->fold_line(1);
  2656. CHECK_FALSE(code_edit->is_line_folded(1));
  2657. code_edit->fold_line(0);
  2658. CHECK_FALSE(code_edit->is_line_folded(0));
  2659. CHECK_FALSE(code_edit->is_line_folded(1));
  2660. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 1);
  2661. // Non-indented comments/strings.
  2662. // Single line
  2663. code_edit->set_text("test\n\tline1\n#line1\n#line2\n\ttest");
  2664. CHECK(code_edit->can_fold_line(0));
  2665. CHECK_FALSE(code_edit->can_fold_line(1));
  2666. code_edit->fold_line(1);
  2667. CHECK_FALSE(code_edit->is_line_folded(1));
  2668. code_edit->fold_line(0);
  2669. CHECK(code_edit->is_line_folded(0));
  2670. CHECK_FALSE(code_edit->is_line_folded(1));
  2671. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 4);
  2672. code_edit->set_text("test\n\tline1\n^line1\n^line2\n\ttest");
  2673. CHECK(code_edit->can_fold_line(0));
  2674. CHECK_FALSE(code_edit->can_fold_line(1));
  2675. code_edit->fold_line(1);
  2676. CHECK_FALSE(code_edit->is_line_folded(1));
  2677. code_edit->fold_line(0);
  2678. CHECK(code_edit->is_line_folded(0));
  2679. CHECK_FALSE(code_edit->is_line_folded(1));
  2680. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 4);
  2681. // Indent level 0->1, comment after lines
  2682. code_edit->set_text("line1\n\tline2\n#test");
  2683. CHECK(code_edit->can_fold_line(0));
  2684. CHECK_FALSE(code_edit->can_fold_line(1));
  2685. code_edit->fold_line(1);
  2686. CHECK_FALSE(code_edit->is_line_folded(1));
  2687. code_edit->fold_line(0);
  2688. CHECK(code_edit->is_line_folded(0));
  2689. CHECK_FALSE(code_edit->is_line_folded(1));
  2690. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 2);
  2691. // Indent level 0->1, comment between lines
  2692. code_edit->set_text("line1\n#test\n\tline2\nline3");
  2693. CHECK(code_edit->can_fold_line(0));
  2694. CHECK_FALSE(code_edit->can_fold_line(2));
  2695. code_edit->fold_line(2);
  2696. CHECK_FALSE(code_edit->is_line_folded(2));
  2697. code_edit->fold_line(0);
  2698. CHECK(code_edit->is_line_folded(0));
  2699. CHECK_FALSE(code_edit->is_line_folded(2));
  2700. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 3);
  2701. // Indent level 1->2, comment after lines
  2702. code_edit->set_text("\tline1\n\t\tline2\n#test");
  2703. CHECK(code_edit->can_fold_line(0));
  2704. CHECK_FALSE(code_edit->can_fold_line(1));
  2705. code_edit->fold_line(1);
  2706. CHECK_FALSE(code_edit->is_line_folded(1));
  2707. code_edit->fold_line(0);
  2708. CHECK(code_edit->is_line_folded(0));
  2709. CHECK_FALSE(code_edit->is_line_folded(1));
  2710. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 2);
  2711. // Indent level 1->2, comment between lines
  2712. code_edit->set_text("\tline1\n#test\n\t\tline2\nline3");
  2713. CHECK(code_edit->can_fold_line(0));
  2714. CHECK_FALSE(code_edit->can_fold_line(2));
  2715. code_edit->fold_line(2);
  2716. CHECK_FALSE(code_edit->is_line_folded(2));
  2717. code_edit->fold_line(0);
  2718. CHECK(code_edit->is_line_folded(0));
  2719. CHECK_FALSE(code_edit->is_line_folded(2));
  2720. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 3);
  2721. // Multiline
  2722. code_edit->set_text("test\n\tline1\n&line1\nline2&\n\ttest");
  2723. CHECK(code_edit->can_fold_line(0));
  2724. CHECK_FALSE(code_edit->can_fold_line(1));
  2725. code_edit->fold_line(1);
  2726. CHECK_FALSE(code_edit->is_line_folded(1));
  2727. code_edit->fold_line(0);
  2728. CHECK(code_edit->is_line_folded(0));
  2729. CHECK_FALSE(code_edit->is_line_folded(1));
  2730. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 4);
  2731. code_edit->set_text("test\n\tline1\n$line1\nline2$\n\ttest");
  2732. CHECK(code_edit->can_fold_line(0));
  2733. CHECK_FALSE(code_edit->can_fold_line(1));
  2734. code_edit->fold_line(1);
  2735. CHECK_FALSE(code_edit->is_line_folded(1));
  2736. code_edit->fold_line(0);
  2737. CHECK(code_edit->is_line_folded(0));
  2738. CHECK_FALSE(code_edit->is_line_folded(1));
  2739. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 4);
  2740. }
  2741. SUBCASE("[CodeEdit] folding carets") {
  2742. // Folding a line moves all carets that would be hidden.
  2743. code_edit->set_text("test\n\tline1\n\t\tline 2\n");
  2744. code_edit->set_caret_line(1);
  2745. code_edit->set_caret_column(0);
  2746. code_edit->add_caret(1, 3);
  2747. code_edit->add_caret(2, 8);
  2748. code_edit->add_caret(2, 1);
  2749. code_edit->select(2, 0, 2, 1, 3);
  2750. code_edit->fold_line(0);
  2751. CHECK(code_edit->is_line_folded(0));
  2752. CHECK_FALSE(code_edit->is_line_folded(1));
  2753. CHECK(code_edit->get_caret_count() == 1);
  2754. CHECK_FALSE(code_edit->has_selection());
  2755. CHECK(code_edit->get_caret_line() == 0);
  2756. CHECK(code_edit->get_caret_column() == 4);
  2757. // Undoing an action that puts the caret on a folded line unfolds it.
  2758. code_edit->set_text("test\n\tline1");
  2759. code_edit->select(1, 1, 1, 2);
  2760. code_edit->duplicate_selection();
  2761. CHECK(code_edit->get_text() == "test\n\tlline1");
  2762. CHECK(code_edit->has_selection());
  2763. CHECK(code_edit->get_caret_line() == 1);
  2764. CHECK(code_edit->get_caret_column() == 3);
  2765. CHECK(code_edit->get_selection_origin_line() == 1);
  2766. CHECK(code_edit->get_selection_origin_column() == 2);
  2767. code_edit->fold_line(0);
  2768. CHECK(code_edit->is_line_folded(0));
  2769. CHECK_FALSE(code_edit->is_line_folded(1));
  2770. CHECK_FALSE(code_edit->has_selection());
  2771. CHECK(code_edit->get_caret_line() == 0);
  2772. CHECK(code_edit->get_caret_column() == 4);
  2773. code_edit->undo();
  2774. CHECK(code_edit->get_text() == "test\n\tline1");
  2775. CHECK(code_edit->has_selection());
  2776. CHECK(code_edit->get_caret_line() == 1);
  2777. CHECK(code_edit->get_caret_column() == 2);
  2778. CHECK(code_edit->get_selection_origin_line() == 1);
  2779. CHECK(code_edit->get_selection_origin_column() == 1);
  2780. CHECK_FALSE(code_edit->is_line_folded(0));
  2781. CHECK_FALSE(code_edit->is_line_folded(1));
  2782. // Redoing doesn't refold.
  2783. code_edit->redo();
  2784. CHECK(code_edit->has_selection());
  2785. CHECK(code_edit->get_caret_line() == 1);
  2786. CHECK(code_edit->get_caret_column() == 3);
  2787. CHECK(code_edit->get_selection_origin_line() == 1);
  2788. CHECK(code_edit->get_selection_origin_column() == 2);
  2789. CHECK_FALSE(code_edit->is_line_folded(0));
  2790. CHECK_FALSE(code_edit->is_line_folded(1));
  2791. }
  2792. SUBCASE("[CodeEdit] actions unfold") {
  2793. // add_selection_for_next_occurrence unfolds.
  2794. code_edit->set_text("test\n\tline1 test\n\t\tline 2\ntest2");
  2795. code_edit->select(0, 0, 0, 4);
  2796. code_edit->fold_line(0);
  2797. CHECK(code_edit->is_line_folded(0));
  2798. code_edit->add_selection_for_next_occurrence();
  2799. CHECK(code_edit->get_caret_count() == 2);
  2800. CHECK(code_edit->has_selection(0));
  2801. CHECK(code_edit->get_caret_line() == 0);
  2802. CHECK(code_edit->get_selection_origin_line() == 0);
  2803. CHECK(code_edit->get_caret_column() == 4);
  2804. CHECK(code_edit->get_selection_origin_column() == 0);
  2805. CHECK(code_edit->has_selection(1));
  2806. CHECK(code_edit->get_caret_line(1) == 1);
  2807. CHECK(code_edit->get_selection_origin_line(1) == 1);
  2808. CHECK(code_edit->get_caret_column(1) == 11);
  2809. CHECK(code_edit->get_selection_origin_column(1) == 7);
  2810. CHECK_FALSE(code_edit->is_line_folded(0));
  2811. code_edit->remove_secondary_carets();
  2812. // skip_selection_for_next_occurrence unfolds.
  2813. code_edit->select(0, 0, 0, 4);
  2814. code_edit->fold_line(0);
  2815. CHECK(code_edit->is_line_folded(0));
  2816. code_edit->skip_selection_for_next_occurrence();
  2817. CHECK(code_edit->get_caret_count() == 1);
  2818. CHECK(code_edit->has_selection(0));
  2819. CHECK(code_edit->get_caret_line() == 1);
  2820. CHECK(code_edit->get_selection_origin_line() == 1);
  2821. CHECK(code_edit->get_caret_column() == 11);
  2822. CHECK(code_edit->get_selection_origin_column() == 7);
  2823. CHECK_FALSE(code_edit->is_line_folded(0));
  2824. code_edit->remove_secondary_carets();
  2825. code_edit->deselect();
  2826. }
  2827. SUBCASE("[CodeEdit] toggle folding carets") {
  2828. code_edit->set_text("test\n\tline1\ntest2\n\tline2");
  2829. // Fold lines with carets on them.
  2830. code_edit->set_caret_line(0);
  2831. code_edit->set_caret_column(1);
  2832. code_edit->toggle_foldable_lines_at_carets();
  2833. CHECK(code_edit->is_line_folded(0));
  2834. CHECK_FALSE(code_edit->is_line_folded(2));
  2835. // Toggle fold on lines with carets.
  2836. code_edit->add_caret(2, 0);
  2837. code_edit->toggle_foldable_lines_at_carets();
  2838. CHECK_FALSE(code_edit->is_line_folded(0));
  2839. CHECK(code_edit->is_line_folded(2));
  2840. CHECK(code_edit->get_caret_count() == 2);
  2841. CHECK(code_edit->get_caret_line(0) == 0);
  2842. CHECK(code_edit->get_caret_column(0) == 1);
  2843. CHECK(code_edit->get_caret_line(1) == 2);
  2844. CHECK(code_edit->get_caret_column(1) == 0);
  2845. // Multiple carets as part of one fold.
  2846. code_edit->unfold_all_lines();
  2847. code_edit->remove_secondary_carets();
  2848. code_edit->set_caret_line(0);
  2849. code_edit->set_caret_column(1);
  2850. code_edit->add_caret(0, 4);
  2851. code_edit->add_caret(1, 2);
  2852. code_edit->toggle_foldable_lines_at_carets();
  2853. CHECK(code_edit->is_line_folded(0));
  2854. CHECK_FALSE(code_edit->is_line_folded(2));
  2855. CHECK(code_edit->get_caret_count() == 2);
  2856. CHECK(code_edit->get_caret_line(0) == 0);
  2857. CHECK(code_edit->get_caret_column(0) == 1);
  2858. CHECK(code_edit->get_caret_line(1) == 0);
  2859. CHECK(code_edit->get_caret_column(1) == 4);
  2860. }
  2861. memdelete(code_edit);
  2862. }
  2863. TEST_CASE("[SceneTree][CodeEdit] region folding") {
  2864. CodeEdit *code_edit = memnew(CodeEdit);
  2865. SceneTree::get_singleton()->get_root()->add_child(code_edit);
  2866. code_edit->grab_focus();
  2867. SUBCASE("[CodeEdit] region tags") {
  2868. code_edit->set_line_folding_enabled(true);
  2869. // Region tag detection.
  2870. code_edit->set_text("#region region_name\nline2\n#endregion");
  2871. code_edit->clear_comment_delimiters();
  2872. code_edit->add_comment_delimiter("#", "");
  2873. CHECK(code_edit->is_line_code_region_start(0));
  2874. CHECK_FALSE(code_edit->is_line_code_region_start(1));
  2875. CHECK_FALSE(code_edit->is_line_code_region_start(2));
  2876. CHECK_FALSE(code_edit->is_line_code_region_end(0));
  2877. CHECK_FALSE(code_edit->is_line_code_region_end(1));
  2878. CHECK(code_edit->is_line_code_region_end(2));
  2879. // Region tag customization.
  2880. code_edit->set_text("#region region_name\nline2\n#endregion\n#open region_name\nline2\n#close");
  2881. code_edit->clear_comment_delimiters();
  2882. code_edit->add_comment_delimiter("#", "");
  2883. CHECK(code_edit->is_line_code_region_start(0));
  2884. CHECK(code_edit->is_line_code_region_end(2));
  2885. CHECK_FALSE(code_edit->is_line_code_region_start(3));
  2886. CHECK_FALSE(code_edit->is_line_code_region_end(5));
  2887. code_edit->set_code_region_tags("open", "close");
  2888. CHECK_FALSE(code_edit->is_line_code_region_start(0));
  2889. CHECK_FALSE(code_edit->is_line_code_region_end(2));
  2890. CHECK(code_edit->is_line_code_region_start(3));
  2891. CHECK(code_edit->is_line_code_region_end(5));
  2892. code_edit->set_code_region_tags("region", "endregion");
  2893. // Setting identical start and end region tags should fail.
  2894. CHECK(code_edit->get_code_region_start_tag() == "region");
  2895. CHECK(code_edit->get_code_region_end_tag() == "endregion");
  2896. ERR_PRINT_OFF;
  2897. code_edit->set_code_region_tags("same_tag", "same_tag");
  2898. ERR_PRINT_ON;
  2899. CHECK(code_edit->get_code_region_start_tag() == "region");
  2900. CHECK(code_edit->get_code_region_end_tag() == "endregion");
  2901. }
  2902. SUBCASE("[CodeEdit] create code region") {
  2903. code_edit->set_line_folding_enabled(true);
  2904. // Region creation with selection adds start and close region lines. Region name is selected and the region is folded.
  2905. code_edit->set_text("line1\nline2\nline3");
  2906. code_edit->clear_comment_delimiters();
  2907. code_edit->add_comment_delimiter("#", "");
  2908. code_edit->select(1, 0, 1, 4);
  2909. code_edit->create_code_region();
  2910. CHECK(code_edit->is_line_code_region_start(1));
  2911. CHECK(code_edit->is_line_code_region_end(3));
  2912. CHECK(code_edit->get_text() == "line1\n#region New Code Region\nline2\n#endregion\nline3");
  2913. CHECK(code_edit->get_caret_count() == 1);
  2914. CHECK(code_edit->has_selection());
  2915. CHECK(code_edit->get_selected_text() == "New Code Region");
  2916. CHECK(code_edit->get_caret_line() == 1);
  2917. CHECK(code_edit->get_caret_column() == 23);
  2918. CHECK(code_edit->get_selection_origin_line() == 1);
  2919. CHECK(code_edit->get_selection_origin_column() == 8);
  2920. CHECK(code_edit->is_line_folded(1));
  2921. // Undo region creation. Line get unfolded.
  2922. code_edit->undo();
  2923. CHECK(code_edit->get_text() == "line1\nline2\nline3");
  2924. CHECK(code_edit->get_caret_count() == 1);
  2925. CHECK(code_edit->has_selection());
  2926. CHECK(code_edit->get_caret_line() == 1);
  2927. CHECK(code_edit->get_caret_column() == 4);
  2928. CHECK(code_edit->get_selection_origin_line() == 1);
  2929. CHECK(code_edit->get_selection_origin_column() == 0);
  2930. CHECK_FALSE(code_edit->is_line_folded(1));
  2931. // Redo region creation.
  2932. code_edit->redo();
  2933. CHECK(code_edit->get_text() == "line1\n#region New Code Region\nline2\n#endregion\nline3");
  2934. CHECK(code_edit->get_caret_count() == 1);
  2935. CHECK(code_edit->has_selection());
  2936. CHECK(code_edit->get_selected_text() == "New Code Region");
  2937. CHECK(code_edit->get_caret_line() == 1);
  2938. CHECK(code_edit->get_caret_column() == 23);
  2939. CHECK(code_edit->get_selection_origin_line() == 1);
  2940. CHECK(code_edit->get_selection_origin_column() == 8);
  2941. CHECK_FALSE(code_edit->is_line_folded(1));
  2942. // Region creation without any selection has no effect.
  2943. code_edit->set_text("line1\nline2\nline3");
  2944. code_edit->clear_comment_delimiters();
  2945. code_edit->add_comment_delimiter("#", "");
  2946. code_edit->create_code_region();
  2947. CHECK(code_edit->get_text() == "line1\nline2\nline3");
  2948. // Region creation with multiple selections. Secondary carets are removed and the first region name is selected.
  2949. code_edit->set_text("line1\nline2\nline3");
  2950. code_edit->clear_comment_delimiters();
  2951. code_edit->add_comment_delimiter("#", "");
  2952. code_edit->select(0, 0, 0, 4, 0);
  2953. code_edit->add_caret(2, 5);
  2954. code_edit->select(2, 0, 2, 5, 1);
  2955. code_edit->create_code_region();
  2956. CHECK(code_edit->get_text() == "#region New Code Region\nline1\n#endregion\nline2\n#region New Code Region\nline3\n#endregion");
  2957. CHECK(code_edit->get_caret_count() == 1);
  2958. CHECK(code_edit->has_selection());
  2959. CHECK(code_edit->get_selected_text() == "New Code Region");
  2960. CHECK(code_edit->get_caret_line() == 0);
  2961. CHECK(code_edit->get_caret_column() == 23);
  2962. CHECK(code_edit->get_selection_origin_line() == 0);
  2963. CHECK(code_edit->get_selection_origin_column() == 8);
  2964. // Region creation with mixed selection and non-selection carets. Regular carets are ignored.
  2965. code_edit->set_text("line1\nline2\nline3");
  2966. code_edit->clear_comment_delimiters();
  2967. code_edit->add_comment_delimiter("#", "");
  2968. code_edit->select(0, 0, 0, 4, 0);
  2969. code_edit->add_caret(2, 5);
  2970. code_edit->create_code_region();
  2971. CHECK(code_edit->get_text() == "#region New Code Region\nline1\n#endregion\nline2\nline3");
  2972. CHECK(code_edit->get_caret_count() == 1);
  2973. CHECK(code_edit->has_selection());
  2974. CHECK(code_edit->get_selected_text() == "New Code Region");
  2975. // Two selections on the same line create only one region.
  2976. code_edit->set_text("test line1\ntest line2\ntest line3");
  2977. code_edit->clear_comment_delimiters();
  2978. code_edit->add_comment_delimiter("#", "");
  2979. code_edit->select(0, 0, 1, 2, 0);
  2980. code_edit->add_caret(1, 4);
  2981. code_edit->select(1, 4, 2, 5, 1);
  2982. code_edit->create_code_region();
  2983. CHECK(code_edit->get_text() == "#region New Code Region\ntest line1\ntest line2\ntest line3\n#endregion");
  2984. // Region tag with // comment delimiter.
  2985. code_edit->set_text("//region region_name\nline2\n//endregion");
  2986. code_edit->clear_comment_delimiters();
  2987. code_edit->add_comment_delimiter("//", "");
  2988. CHECK(code_edit->is_line_code_region_start(0));
  2989. CHECK(code_edit->is_line_code_region_end(2));
  2990. // Creating region with no valid one line comment delimiter has no effect.
  2991. code_edit->set_text("line1\nline2\nline3");
  2992. code_edit->clear_comment_delimiters();
  2993. code_edit->create_code_region();
  2994. CHECK(code_edit->get_text() == "line1\nline2\nline3");
  2995. code_edit->add_comment_delimiter("/*", "*/");
  2996. code_edit->create_code_region();
  2997. CHECK(code_edit->get_text() == "line1\nline2\nline3");
  2998. }
  2999. SUBCASE("[CodeEdit] region comment delimiters") {
  3000. code_edit->set_line_folding_enabled(true);
  3001. // Choose one line comment delimiter.
  3002. code_edit->set_text("//region region_name\nline2\n//endregion");
  3003. code_edit->clear_comment_delimiters();
  3004. code_edit->add_comment_delimiter("/*", "*/");
  3005. code_edit->add_comment_delimiter("//", "");
  3006. CHECK(code_edit->is_line_code_region_start(0));
  3007. CHECK(code_edit->is_line_code_region_end(2));
  3008. // Update code region delimiter when removing comment delimiter.
  3009. code_edit->set_text("#region region_name\nline2\n#endregion\n//region region_name\nline2\n//endregion");
  3010. code_edit->clear_comment_delimiters();
  3011. code_edit->add_comment_delimiter("//", "");
  3012. code_edit->add_comment_delimiter("#", ""); // A shorter delimiter has higher priority.
  3013. CHECK(code_edit->is_line_code_region_start(0));
  3014. CHECK(code_edit->is_line_code_region_end(2));
  3015. CHECK_FALSE(code_edit->is_line_code_region_start(3));
  3016. CHECK_FALSE(code_edit->is_line_code_region_end(5));
  3017. code_edit->remove_comment_delimiter("#");
  3018. CHECK_FALSE(code_edit->is_line_code_region_start(0));
  3019. CHECK_FALSE(code_edit->is_line_code_region_end(2));
  3020. CHECK(code_edit->is_line_code_region_start(3));
  3021. CHECK(code_edit->is_line_code_region_end(5));
  3022. // Update code region delimiter when clearing comment delimiters.
  3023. code_edit->set_text("//region region_name\nline2\n//endregion");
  3024. code_edit->clear_comment_delimiters();
  3025. code_edit->add_comment_delimiter("//", "");
  3026. CHECK(code_edit->is_line_code_region_start(0));
  3027. CHECK(code_edit->is_line_code_region_end(2));
  3028. code_edit->clear_comment_delimiters();
  3029. CHECK_FALSE(code_edit->is_line_code_region_start(0));
  3030. CHECK_FALSE(code_edit->is_line_code_region_end(2));
  3031. }
  3032. SUBCASE("[CodeEdit] fold region") {
  3033. code_edit->set_line_folding_enabled(true);
  3034. // Fold region.
  3035. code_edit->clear_comment_delimiters();
  3036. code_edit->add_comment_delimiter("#", "");
  3037. code_edit->set_text("#region region_name\nline2\nline3\n#endregion\nvisible line");
  3038. CHECK(code_edit->can_fold_line(0));
  3039. for (int i = 1; i < 5; i++) {
  3040. CHECK_FALSE(code_edit->can_fold_line(i));
  3041. }
  3042. for (int i = 0; i < 5; i++) {
  3043. CHECK_FALSE(code_edit->is_line_folded(i));
  3044. }
  3045. code_edit->fold_line(0);
  3046. CHECK(code_edit->is_line_folded(0));
  3047. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 4);
  3048. // Region with no end can't be folded.
  3049. ERR_PRINT_OFF;
  3050. code_edit->clear_comment_delimiters();
  3051. code_edit->add_comment_delimiter("#", "");
  3052. code_edit->set_text("#region region_name\nline2\nline3\n#bad_end_tag\nvisible line");
  3053. CHECK_FALSE(code_edit->can_fold_line(0));
  3054. ERR_PRINT_ON;
  3055. // Bad nested region can't be folded.
  3056. ERR_PRINT_OFF;
  3057. code_edit->clear_comment_delimiters();
  3058. code_edit->add_comment_delimiter("#", "");
  3059. code_edit->set_text("#region without end\n#region region2\nline3\n#endregion\n#no_end");
  3060. CHECK_FALSE(code_edit->can_fold_line(0));
  3061. CHECK(code_edit->can_fold_line(1));
  3062. ERR_PRINT_ON;
  3063. // Nested region folding.
  3064. ERR_PRINT_OFF;
  3065. code_edit->clear_comment_delimiters();
  3066. code_edit->add_comment_delimiter("#", "");
  3067. code_edit->set_text("#region region1\n#region region2\nline3\n#endregion\n#endregion");
  3068. CHECK(code_edit->can_fold_line(0));
  3069. CHECK(code_edit->can_fold_line(1));
  3070. code_edit->fold_line(1);
  3071. CHECK(code_edit->get_next_visible_line_offset_from(2, 1) == 3);
  3072. code_edit->fold_line(0);
  3073. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 4);
  3074. ERR_PRINT_ON;
  3075. // Unfolding a line inside a region unfold whole region.
  3076. code_edit->clear_comment_delimiters();
  3077. code_edit->add_comment_delimiter("#", "");
  3078. code_edit->set_text("#region region\ninside\nline3\n#endregion\nvisible");
  3079. code_edit->fold_line(0);
  3080. CHECK(code_edit->is_line_folded(0));
  3081. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 4);
  3082. code_edit->unfold_line(1);
  3083. CHECK_FALSE(code_edit->is_line_folded(0));
  3084. // Region start and end tags are ignored if in a string and at the start of the line.
  3085. code_edit->clear_comment_delimiters();
  3086. code_edit->add_comment_delimiter("#", "");
  3087. code_edit->clear_string_delimiters();
  3088. code_edit->add_string_delimiter("\"", "\"");
  3089. code_edit->set_text("#region region_name1\nline2\n\"\n#region region_name2\n#endregion\n\"\n#endregion\nvisible");
  3090. CHECK(code_edit->is_line_code_region_start(0));
  3091. CHECK(code_edit->is_line_code_region_end(6));
  3092. CHECK(code_edit->can_fold_line(0));
  3093. for (int i = 1; i < 7; i++) {
  3094. if (i == 2) {
  3095. continue;
  3096. }
  3097. CHECK_FALSE(code_edit->can_fold_line(i));
  3098. }
  3099. for (int i = 0; i < 7; i++) {
  3100. CHECK_FALSE(code_edit->is_line_folded(i));
  3101. }
  3102. code_edit->fold_line(0);
  3103. CHECK(code_edit->is_line_folded(0));
  3104. CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 7);
  3105. }
  3106. memdelete(code_edit);
  3107. }
  3108. TEST_CASE("[SceneTree][CodeEdit] completion") {
  3109. CodeEdit *code_edit = memnew(CodeEdit);
  3110. SceneTree::get_singleton()->get_root()->add_child(code_edit);
  3111. code_edit->grab_focus();
  3112. SUBCASE("[CodeEdit] auto brace completion") {
  3113. code_edit->set_auto_brace_completion_enabled(true);
  3114. CHECK(code_edit->is_auto_brace_completion_enabled());
  3115. code_edit->set_highlight_matching_braces_enabled(true);
  3116. CHECK(code_edit->is_highlight_matching_braces_enabled());
  3117. /* Try setters, any length. */
  3118. Dictionary auto_brace_completion_pairs;
  3119. auto_brace_completion_pairs["["] = "]";
  3120. auto_brace_completion_pairs["'"] = "'";
  3121. auto_brace_completion_pairs[";"] = "'";
  3122. auto_brace_completion_pairs["'''"] = "'''";
  3123. code_edit->set_auto_brace_completion_pairs(auto_brace_completion_pairs);
  3124. CHECK(code_edit->get_auto_brace_completion_pairs().size() == 4);
  3125. CHECK(code_edit->get_auto_brace_completion_pairs()["["] == "]");
  3126. CHECK(code_edit->get_auto_brace_completion_pairs()["'"] == "'");
  3127. CHECK(code_edit->get_auto_brace_completion_pairs()[";"] == "'");
  3128. CHECK(code_edit->get_auto_brace_completion_pairs()["'''"] == "'''");
  3129. ERR_PRINT_OFF;
  3130. /* No duplicate start keys. */
  3131. code_edit->add_auto_brace_completion_pair("[", "]");
  3132. CHECK(code_edit->get_auto_brace_completion_pairs().size() == 4);
  3133. /* No empty keys. */
  3134. code_edit->add_auto_brace_completion_pair("[", "");
  3135. CHECK(code_edit->get_auto_brace_completion_pairs().size() == 4);
  3136. code_edit->add_auto_brace_completion_pair("", "]");
  3137. CHECK(code_edit->get_auto_brace_completion_pairs().size() == 4);
  3138. code_edit->add_auto_brace_completion_pair("", "");
  3139. CHECK(code_edit->get_auto_brace_completion_pairs().size() == 4);
  3140. /* Must be a symbol. */
  3141. code_edit->add_auto_brace_completion_pair("a", "]");
  3142. CHECK(code_edit->get_auto_brace_completion_pairs().size() == 4);
  3143. code_edit->add_auto_brace_completion_pair("[", "a");
  3144. CHECK(code_edit->get_auto_brace_completion_pairs().size() == 4);
  3145. code_edit->add_auto_brace_completion_pair("a", "a");
  3146. CHECK(code_edit->get_auto_brace_completion_pairs().size() == 4);
  3147. ERR_PRINT_ON;
  3148. /* Check metadata. */
  3149. CHECK(code_edit->has_auto_brace_completion_open_key("["));
  3150. CHECK(code_edit->has_auto_brace_completion_open_key("'"));
  3151. CHECK(code_edit->has_auto_brace_completion_open_key(";"));
  3152. CHECK(code_edit->has_auto_brace_completion_open_key("'''"));
  3153. CHECK_FALSE(code_edit->has_auto_brace_completion_open_key("("));
  3154. CHECK(code_edit->has_auto_brace_completion_close_key("]"));
  3155. CHECK(code_edit->has_auto_brace_completion_close_key("'"));
  3156. CHECK(code_edit->has_auto_brace_completion_close_key("'''"));
  3157. CHECK_FALSE(code_edit->has_auto_brace_completion_close_key(")"));
  3158. CHECK(code_edit->get_auto_brace_completion_close_key("[") == "]");
  3159. CHECK(code_edit->get_auto_brace_completion_close_key("'") == "'");
  3160. CHECK(code_edit->get_auto_brace_completion_close_key(";") == "'");
  3161. CHECK(code_edit->get_auto_brace_completion_close_key("'''") == "'''");
  3162. CHECK(code_edit->get_auto_brace_completion_close_key("(").is_empty());
  3163. /* Check typing inserts closing pair. */
  3164. code_edit->clear();
  3165. SEND_GUI_KEY_EVENT(Key::BRACKETLEFT);
  3166. CHECK(code_edit->get_line(0) == "[]");
  3167. /* Should first match and insert smaller key. */
  3168. code_edit->clear();
  3169. SEND_GUI_KEY_EVENT(Key::APOSTROPHE);
  3170. CHECK(code_edit->get_line(0) == "''");
  3171. CHECK(code_edit->get_caret_column() == 1);
  3172. /* Move out from center, Should match and insert larger key. */
  3173. SEND_GUI_ACTION("ui_text_caret_right");
  3174. SEND_GUI_KEY_EVENT(Key::APOSTROPHE);
  3175. CHECK(code_edit->get_line(0) == "''''''");
  3176. CHECK(code_edit->get_caret_column() == 3);
  3177. /* Backspace should remove all. */
  3178. SEND_GUI_ACTION("ui_text_backspace");
  3179. CHECK(code_edit->get_line(0).is_empty());
  3180. /* If in between and typing close key should "skip". */
  3181. SEND_GUI_KEY_EVENT(Key::BRACKETLEFT);
  3182. CHECK(code_edit->get_line(0) == "[]");
  3183. CHECK(code_edit->get_caret_column() == 1);
  3184. SEND_GUI_KEY_EVENT(Key::BRACKETRIGHT);
  3185. CHECK(code_edit->get_line(0) == "[]");
  3186. CHECK(code_edit->get_caret_column() == 2);
  3187. /* If current is char and inserting a string, do not autocomplete. */
  3188. code_edit->clear();
  3189. SEND_GUI_KEY_EVENT(Key::A);
  3190. SEND_GUI_KEY_EVENT(Key::APOSTROPHE);
  3191. CHECK(code_edit->get_line(0) == "A'");
  3192. /* If in comment, do not complete. */
  3193. code_edit->add_comment_delimiter("#", "");
  3194. code_edit->clear();
  3195. SEND_GUI_KEY_EVENT(Key::NUMBERSIGN);
  3196. SEND_GUI_KEY_EVENT(Key::APOSTROPHE);
  3197. CHECK(code_edit->get_line(0) == "#'");
  3198. /* If in string, and inserting string do not complete. */
  3199. code_edit->clear();
  3200. SEND_GUI_KEY_EVENT(Key::APOSTROPHE);
  3201. SEND_GUI_KEY_EVENT(Key::QUOTEDBL);
  3202. CHECK(code_edit->get_line(0) == "'\"'");
  3203. /* Wrap single line selection with brackets */
  3204. code_edit->clear();
  3205. code_edit->insert_text_at_caret("abc");
  3206. code_edit->select_all();
  3207. SEND_GUI_KEY_EVENT(Key::BRACKETLEFT);
  3208. CHECK(code_edit->get_line(0) == "[abc]");
  3209. /* Caret should be after the last character of the single line selection */
  3210. CHECK(code_edit->get_caret_column() == 4);
  3211. /* Wrap multi line selection with brackets */
  3212. code_edit->clear();
  3213. code_edit->insert_text_at_caret("abc\nabc");
  3214. code_edit->select_all();
  3215. SEND_GUI_KEY_EVENT(Key::BRACKETLEFT);
  3216. CHECK(code_edit->get_text() == "[abc\nabc]");
  3217. /* Caret should be after the last character of the multi line selection */
  3218. CHECK(code_edit->get_caret_line() == 1);
  3219. CHECK(code_edit->get_caret_column() == 3);
  3220. /* If inserted character is not a auto brace completion open key, replace selected text with the inserted character */
  3221. code_edit->clear();
  3222. code_edit->insert_text_at_caret("abc");
  3223. code_edit->select_all();
  3224. SEND_GUI_KEY_EVENT(Key::KEY_1);
  3225. CHECK(code_edit->get_text() == "1");
  3226. /* If potential multichar and single brace completion is matched, it should wrap the single. */
  3227. code_edit->clear();
  3228. code_edit->insert_text_at_caret("\'\'abc");
  3229. code_edit->select(0, 2, 0, 5);
  3230. SEND_GUI_KEY_EVENT(Key::APOSTROPHE);
  3231. CHECK(code_edit->get_text() == "\'\'\'abc\'");
  3232. /* If only the potential multichar brace completion is matched, it does not wrap or complete. */
  3233. auto_brace_completion_pairs.erase("\'");
  3234. code_edit->set_auto_brace_completion_pairs(auto_brace_completion_pairs);
  3235. CHECK_FALSE(code_edit->has_auto_brace_completion_open_key("\'"));
  3236. code_edit->clear();
  3237. code_edit->insert_text_at_caret("\'\'abc");
  3238. code_edit->select(0, 2, 0, 5);
  3239. SEND_GUI_KEY_EVENT(Key::APOSTROPHE);
  3240. CHECK(code_edit->get_text() == "\'\'\'");
  3241. }
  3242. SUBCASE("[CodeEdit] autocomplete with brace completion") {
  3243. code_edit->set_auto_brace_completion_enabled(true);
  3244. CHECK(code_edit->is_auto_brace_completion_enabled());
  3245. code_edit->insert_text_at_caret("(te)");
  3246. code_edit->set_caret_column(3);
  3247. // Full completion.
  3248. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_FUNCTION, "test()", "test()");
  3249. code_edit->update_code_completion_options();
  3250. code_edit->confirm_code_completion();
  3251. CHECK(code_edit->get_line(0) == "(test())");
  3252. CHECK(code_edit->get_caret_column() == 7);
  3253. code_edit->undo();
  3254. // With "arg".
  3255. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_FUNCTION, "test(", "test(");
  3256. code_edit->update_code_completion_options();
  3257. code_edit->confirm_code_completion();
  3258. CHECK(code_edit->get_line(0) == "(test())");
  3259. CHECK(code_edit->get_caret_column() == 6);
  3260. code_edit->undo();
  3261. // brace completion disabled
  3262. code_edit->set_auto_brace_completion_enabled(false);
  3263. // Full completion.
  3264. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_FUNCTION, "test()", "test()");
  3265. code_edit->update_code_completion_options();
  3266. code_edit->confirm_code_completion();
  3267. CHECK(code_edit->get_line(0) == "(test())");
  3268. CHECK(code_edit->get_caret_column() == 7);
  3269. code_edit->undo();
  3270. // With "arg".
  3271. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_FUNCTION, "test(", "test(");
  3272. code_edit->update_code_completion_options();
  3273. code_edit->confirm_code_completion();
  3274. CHECK(code_edit->get_line(0) == "(test()");
  3275. CHECK(code_edit->get_caret_column() == 6);
  3276. // String
  3277. code_edit->set_auto_brace_completion_enabled(true);
  3278. code_edit->clear();
  3279. code_edit->insert_text_at_caret("\"\"");
  3280. code_edit->set_caret_column(1);
  3281. // Full completion.
  3282. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_NODE_PATH, "\"test\"", "\"test\"");
  3283. code_edit->update_code_completion_options();
  3284. code_edit->confirm_code_completion();
  3285. CHECK(code_edit->get_line(0) == "\"test\"");
  3286. CHECK(code_edit->get_caret_column() == 6);
  3287. code_edit->undo();
  3288. // With "arg".
  3289. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_NODE_PATH, "\"test", "\"test");
  3290. code_edit->update_code_completion_options();
  3291. code_edit->confirm_code_completion();
  3292. CHECK(code_edit->get_line(0) == "\"\"test\"");
  3293. CHECK(code_edit->get_caret_column() == 7);
  3294. code_edit->undo();
  3295. // brace completion disabled
  3296. code_edit->set_auto_brace_completion_enabled(false);
  3297. // Full completion.
  3298. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_NODE_PATH, "\"test\"", "\"test\"");
  3299. code_edit->update_code_completion_options();
  3300. code_edit->confirm_code_completion();
  3301. CHECK(code_edit->get_line(0) == "\"test\"");
  3302. CHECK(code_edit->get_caret_column() == 6);
  3303. code_edit->undo();
  3304. // With "arg".
  3305. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_NODE_PATH, "\"test", "\"test");
  3306. code_edit->update_code_completion_options();
  3307. code_edit->confirm_code_completion();
  3308. CHECK(code_edit->get_line(0) == "\"\"test\"");
  3309. CHECK(code_edit->get_caret_column() == 7);
  3310. code_edit->undo();
  3311. }
  3312. SUBCASE("[CodeEdit] autocomplete") {
  3313. code_edit->set_code_completion_enabled(true);
  3314. CHECK(code_edit->is_code_completion_enabled());
  3315. /* Set prefixes, single char only, disallow empty. */
  3316. TypedArray<String> completion_prefixes;
  3317. completion_prefixes.push_back("");
  3318. completion_prefixes.push_back(".");
  3319. completion_prefixes.push_back(".");
  3320. completion_prefixes.push_back(",,");
  3321. ERR_PRINT_OFF;
  3322. code_edit->set_code_completion_prefixes(completion_prefixes);
  3323. ERR_PRINT_ON;
  3324. completion_prefixes = code_edit->get_code_completion_prefixes();
  3325. CHECK(completion_prefixes.size() == 2);
  3326. CHECK(completion_prefixes.has("."));
  3327. CHECK(completion_prefixes.has(","));
  3328. code_edit->set_text("test\ntest");
  3329. CHECK(code_edit->get_text_for_code_completion() == String::chr(0xFFFF) + "test\ntest");
  3330. }
  3331. SUBCASE("[CodeEdit] autocomplete request") {
  3332. SIGNAL_WATCH(code_edit, "code_completion_requested");
  3333. code_edit->set_code_completion_enabled(true);
  3334. Array signal_args;
  3335. signal_args.push_back(Array());
  3336. /* Force request. */
  3337. code_edit->request_code_completion();
  3338. SIGNAL_CHECK_FALSE("code_completion_requested");
  3339. code_edit->request_code_completion(true);
  3340. SIGNAL_CHECK("code_completion_requested", signal_args);
  3341. /* Manual request should force. */
  3342. SEND_GUI_ACTION("ui_text_completion_query");
  3343. SIGNAL_CHECK("code_completion_requested", signal_args);
  3344. /* Insert prefix. */
  3345. TypedArray<String> completion_prefixes;
  3346. completion_prefixes.push_back(".");
  3347. code_edit->set_code_completion_prefixes(completion_prefixes);
  3348. code_edit->insert_text_at_caret(".");
  3349. code_edit->request_code_completion();
  3350. SIGNAL_CHECK("code_completion_requested", signal_args);
  3351. /* Should work with space too. */
  3352. code_edit->insert_text_at_caret(" ");
  3353. code_edit->request_code_completion();
  3354. SIGNAL_CHECK("code_completion_requested", signal_args);
  3355. /* Should work when complete ends with prefix. */
  3356. code_edit->clear();
  3357. code_edit->insert_text_at_caret("t");
  3358. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "test.", "test.");
  3359. code_edit->update_code_completion_options();
  3360. code_edit->confirm_code_completion();
  3361. CHECK(code_edit->get_line(0) == "test.");
  3362. SIGNAL_CHECK("code_completion_requested", signal_args);
  3363. SIGNAL_UNWATCH(code_edit, "code_completion_requested");
  3364. }
  3365. SUBCASE("[CodeEdit] autocomplete completion") {
  3366. if (TS->has_feature(TextServer::FEATURE_FONT_DYNAMIC) && TS->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  3367. CHECK(code_edit->get_code_completion_selected_index() == -1);
  3368. code_edit->set_code_completion_enabled(true);
  3369. CHECK(code_edit->get_code_completion_selected_index() == -1);
  3370. code_edit->update_code_completion_options();
  3371. code_edit->set_code_completion_selected_index(1);
  3372. CHECK(code_edit->get_code_completion_selected_index() == -1);
  3373. CHECK(code_edit->get_code_completion_option(0).size() == 0);
  3374. CHECK(code_edit->get_code_completion_options().size() == 0);
  3375. /* Adding does not update the list. */
  3376. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_0.", "item_0");
  3377. code_edit->set_code_completion_selected_index(1);
  3378. CHECK(code_edit->get_code_completion_selected_index() == -1);
  3379. CHECK(code_edit->get_code_completion_option(0).size() == 0);
  3380. CHECK(code_edit->get_code_completion_options().size() == 0);
  3381. /* After update, pending add should not be counted, */
  3382. /* also does not work on col 0 */
  3383. int before_text_caret_column = code_edit->get_caret_column();
  3384. code_edit->insert_text_at_caret("i");
  3385. code_edit->update_code_completion_options();
  3386. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0", Color(1, 0, 0), Ref<Resource>(), Color(1, 0, 0));
  3387. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_1.", "item_1");
  3388. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_2.", "item_2");
  3389. ERR_PRINT_OFF;
  3390. code_edit->set_code_completion_selected_index(1);
  3391. ERR_PRINT_ON;
  3392. CHECK(code_edit->get_code_completion_selected_index() == 0);
  3393. CHECK(code_edit->get_code_completion_option(0).size() == 7);
  3394. CHECK(code_edit->get_code_completion_options().size() == 1);
  3395. /* Check cancel closes completion. */
  3396. SEND_GUI_ACTION("ui_cancel");
  3397. CHECK(code_edit->get_code_completion_selected_index() == -1);
  3398. code_edit->update_code_completion_options();
  3399. CHECK(code_edit->get_code_completion_selected_index() == 0);
  3400. code_edit->set_code_completion_selected_index(1);
  3401. CHECK(code_edit->get_code_completion_selected_index() == 1);
  3402. CHECK(code_edit->get_code_completion_option(0).size() == 7);
  3403. CHECK(code_edit->get_code_completion_options().size() == 3);
  3404. /* Check data. */
  3405. Dictionary option = code_edit->get_code_completion_option(0);
  3406. CHECK((int)option["kind"] == (int)CodeEdit::CodeCompletionKind::KIND_CLASS);
  3407. CHECK(option["display_text"] == "item_0.");
  3408. CHECK(option["insert_text"] == "item_0");
  3409. CHECK(option["font_color"] == Color(1, 0, 0));
  3410. CHECK(option["icon"] == Ref<Resource>());
  3411. CHECK(option["default_value"] == Color(1, 0, 0));
  3412. /* Set size for mouse input. */
  3413. code_edit->set_size(Size2(100, 100));
  3414. /* Test home and end keys close the completion and move the caret */
  3415. /* => ui_text_caret_line_start */
  3416. code_edit->set_caret_column(before_text_caret_column + 1);
  3417. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0", Color(1, 0, 0), Ref<Resource>(), Color(1, 0, 0));
  3418. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_1.", "item_1");
  3419. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_2.", "item_2");
  3420. code_edit->update_code_completion_options();
  3421. SEND_GUI_ACTION("ui_text_caret_line_start");
  3422. code_edit->update_code_completion_options();
  3423. CHECK(code_edit->get_code_completion_selected_index() == -1);
  3424. CHECK(code_edit->get_caret_column() == 0);
  3425. /* => ui_text_caret_line_end */
  3426. code_edit->set_caret_column(before_text_caret_column + 1);
  3427. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0", Color(1, 0, 0), Ref<Resource>(), Color(1, 0, 0));
  3428. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_1.", "item_1");
  3429. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_2.", "item_2");
  3430. code_edit->update_code_completion_options();
  3431. SEND_GUI_ACTION("ui_text_caret_line_end");
  3432. code_edit->update_code_completion_options();
  3433. CHECK(code_edit->get_code_completion_selected_index() == -1);
  3434. CHECK(code_edit->get_caret_column() == before_text_caret_column + 1);
  3435. /* Check input. */
  3436. code_edit->set_caret_column(before_text_caret_column + 1);
  3437. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0", Color(1, 0, 0), Ref<Resource>(), Color(1, 0, 0));
  3438. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_1.", "item_1");
  3439. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "item_2.", "item_2");
  3440. code_edit->update_code_completion_options();
  3441. SEND_GUI_ACTION("ui_page_down");
  3442. CHECK(code_edit->get_code_completion_selected_index() == 2);
  3443. SEND_GUI_ACTION("ui_page_up");
  3444. CHECK(code_edit->get_code_completion_selected_index() == 0);
  3445. SEND_GUI_ACTION("ui_up");
  3446. CHECK(code_edit->get_code_completion_selected_index() == 2);
  3447. SEND_GUI_ACTION("ui_down");
  3448. CHECK(code_edit->get_code_completion_selected_index() == 0);
  3449. SEND_GUI_KEY_EVENT(Key::T);
  3450. CHECK(code_edit->get_code_completion_selected_index() == 0);
  3451. SEND_GUI_ACTION("ui_left");
  3452. CHECK(code_edit->get_code_completion_selected_index() == 0);
  3453. SEND_GUI_ACTION("ui_right");
  3454. CHECK(code_edit->get_code_completion_selected_index() == 0);
  3455. SEND_GUI_ACTION("ui_text_backspace");
  3456. CHECK(code_edit->get_code_completion_selected_index() == 0);
  3457. Point2 caret_pos = code_edit->get_caret_draw_pos();
  3458. caret_pos.y += code_edit->get_line_height();
  3459. SEND_GUI_MOUSE_BUTTON_EVENT(caret_pos, MouseButton::WHEEL_DOWN, 0, Key::NONE);
  3460. CHECK(code_edit->get_code_completion_selected_index() == 1);
  3461. SEND_GUI_MOUSE_BUTTON_EVENT(caret_pos, MouseButton::WHEEL_UP, 0, Key::NONE);
  3462. CHECK(code_edit->get_code_completion_selected_index() == 0);
  3463. /* Single click selects. */
  3464. caret_pos.y += code_edit->get_line_height() * 2;
  3465. SEND_GUI_MOUSE_BUTTON_EVENT(caret_pos, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  3466. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(caret_pos, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  3467. CHECK(code_edit->get_code_completion_selected_index() == 2);
  3468. /* Double click inserts. */
  3469. SEND_GUI_DOUBLE_CLICK(caret_pos, Key::NONE);
  3470. CHECK(code_edit->get_code_completion_selected_index() == -1);
  3471. CHECK(code_edit->get_line(0) == "item_2");
  3472. code_edit->set_auto_brace_completion_enabled(false);
  3473. /* Does nothing in readonly. */
  3474. code_edit->undo();
  3475. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0");
  3476. code_edit->update_code_completion_options();
  3477. code_edit->set_editable(false);
  3478. code_edit->confirm_code_completion();
  3479. code_edit->set_editable(true);
  3480. CHECK(code_edit->get_line(0) == "i");
  3481. /* Replace */
  3482. code_edit->clear();
  3483. code_edit->insert_text_at_caret("item_1 test");
  3484. code_edit->set_caret_column(2);
  3485. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0");
  3486. code_edit->update_code_completion_options();
  3487. SEND_GUI_ACTION("ui_text_completion_replace");
  3488. CHECK(code_edit->get_line(0) == "item_0 test");
  3489. /* Replace string. */
  3490. code_edit->clear();
  3491. code_edit->insert_text_at_caret("\"item_1 test\"");
  3492. code_edit->set_caret_column(2);
  3493. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0");
  3494. code_edit->update_code_completion_options();
  3495. SEND_GUI_ACTION("ui_text_completion_replace");
  3496. CHECK(code_edit->get_line(0) == "\"item_0\"");
  3497. /* Normal replace if no end is given. */
  3498. code_edit->clear();
  3499. code_edit->insert_text_at_caret("\"item_1 test");
  3500. code_edit->set_caret_column(2);
  3501. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0");
  3502. code_edit->update_code_completion_options();
  3503. SEND_GUI_ACTION("ui_text_completion_replace");
  3504. CHECK(code_edit->get_line(0) == "\"item_0\" test");
  3505. /* Insert at completion. */
  3506. code_edit->clear();
  3507. code_edit->insert_text_at_caret("item_1 test");
  3508. code_edit->set_caret_column(2);
  3509. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0");
  3510. code_edit->update_code_completion_options();
  3511. SEND_GUI_ACTION("ui_text_completion_accept");
  3512. CHECK(code_edit->get_line(0) == "item_01 test");
  3513. /* Insert at completion with string should have same output. */
  3514. code_edit->clear();
  3515. code_edit->insert_text_at_caret("\"item_1 test\"");
  3516. code_edit->set_caret_column(2);
  3517. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0.", "item_0");
  3518. code_edit->update_code_completion_options();
  3519. SEND_GUI_ACTION("ui_text_completion_accept");
  3520. CHECK(code_edit->get_line(0) == "\"item_0\"1 test\"");
  3521. /* Merge symbol at end on insert text. */
  3522. /* End on completion entry. */
  3523. code_edit->clear();
  3524. code_edit->insert_text_at_caret("item_1 test");
  3525. code_edit->set_caret_column(2);
  3526. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0(", "item_0(");
  3527. code_edit->update_code_completion_options();
  3528. SEND_GUI_ACTION("ui_text_completion_replace");
  3529. CHECK(code_edit->get_line(0) == "item_0( test");
  3530. CHECK(code_edit->get_caret_column() == 7);
  3531. /* End of text*/
  3532. code_edit->clear();
  3533. code_edit->insert_text_at_caret("item_1( test");
  3534. code_edit->set_caret_column(2);
  3535. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0", "item_0");
  3536. code_edit->update_code_completion_options();
  3537. SEND_GUI_ACTION("ui_text_completion_replace");
  3538. CHECK(code_edit->get_line(0) == "item_0( test");
  3539. CHECK(code_edit->get_caret_column() == 6);
  3540. /* End of both. */
  3541. code_edit->clear();
  3542. code_edit->insert_text_at_caret("item_1( test");
  3543. code_edit->set_caret_column(2);
  3544. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0(", "item_0(");
  3545. code_edit->update_code_completion_options();
  3546. SEND_GUI_ACTION("ui_text_completion_replace");
  3547. CHECK(code_edit->get_line(0) == "item_0( test");
  3548. CHECK(code_edit->get_caret_column() == 7);
  3549. /* Full set. */
  3550. /* End on completion entry. */
  3551. code_edit->clear();
  3552. code_edit->insert_text_at_caret("item_1 test");
  3553. code_edit->set_caret_column(2);
  3554. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0()", "item_0()");
  3555. code_edit->update_code_completion_options();
  3556. SEND_GUI_ACTION("ui_text_completion_replace");
  3557. CHECK(code_edit->get_line(0) == "item_0() test");
  3558. CHECK(code_edit->get_caret_column() == 8);
  3559. /* End of text*/
  3560. code_edit->clear();
  3561. code_edit->insert_text_at_caret("item_1() test");
  3562. code_edit->set_caret_column(2);
  3563. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0", "item_0");
  3564. code_edit->update_code_completion_options();
  3565. SEND_GUI_ACTION("ui_text_completion_replace");
  3566. CHECK(code_edit->get_line(0) == "item_0() test");
  3567. CHECK(code_edit->get_caret_column() == 6);
  3568. /* End of both. */
  3569. code_edit->clear();
  3570. code_edit->insert_text_at_caret("item_1() test");
  3571. code_edit->set_caret_column(2);
  3572. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0()", "item_0()");
  3573. code_edit->update_code_completion_options();
  3574. SEND_GUI_ACTION("ui_text_completion_replace");
  3575. CHECK(code_edit->get_line(0) == "item_0() test");
  3576. CHECK(code_edit->get_caret_column() == 8);
  3577. /* Autobrace completion. */
  3578. code_edit->set_auto_brace_completion_enabled(true);
  3579. /* End on completion entry. */
  3580. code_edit->clear();
  3581. code_edit->insert_text_at_caret("item_1 test");
  3582. code_edit->set_caret_column(2);
  3583. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0(", "item_0(");
  3584. code_edit->update_code_completion_options();
  3585. SEND_GUI_ACTION("ui_text_completion_replace");
  3586. CHECK(code_edit->get_line(0) == "item_0() test");
  3587. CHECK(code_edit->get_caret_column() == 7);
  3588. /* End of text*/
  3589. code_edit->clear();
  3590. code_edit->insert_text_at_caret("item_1( test");
  3591. code_edit->set_caret_column(2);
  3592. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0", "item_0");
  3593. code_edit->update_code_completion_options();
  3594. SEND_GUI_ACTION("ui_text_completion_replace");
  3595. CHECK(code_edit->get_line(0) == "item_0( test");
  3596. CHECK(code_edit->get_caret_column() == 6);
  3597. /* End of both. */
  3598. code_edit->clear();
  3599. code_edit->insert_text_at_caret("item_1( test");
  3600. code_edit->set_caret_column(2);
  3601. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0(", "item_0(");
  3602. code_edit->update_code_completion_options();
  3603. SEND_GUI_ACTION("ui_text_completion_replace");
  3604. CHECK(code_edit->get_line(0) == "item_0( test");
  3605. CHECK(code_edit->get_caret_column() == 7);
  3606. /* Full set. */
  3607. /* End on completion entry. */
  3608. code_edit->clear();
  3609. code_edit->insert_text_at_caret("item_1 test");
  3610. code_edit->set_caret_column(2);
  3611. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0()", "item_0()");
  3612. code_edit->update_code_completion_options();
  3613. SEND_GUI_ACTION("ui_text_completion_replace");
  3614. CHECK(code_edit->get_line(0) == "item_0() test");
  3615. CHECK(code_edit->get_caret_column() == 8);
  3616. /* End of text*/
  3617. code_edit->clear();
  3618. code_edit->insert_text_at_caret("item_1() test");
  3619. code_edit->set_caret_column(2);
  3620. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0", "item_0");
  3621. code_edit->update_code_completion_options();
  3622. SEND_GUI_ACTION("ui_text_completion_replace");
  3623. CHECK(code_edit->get_line(0) == "item_0() test");
  3624. CHECK(code_edit->get_caret_column() == 6);
  3625. /* End of both. */
  3626. code_edit->clear();
  3627. code_edit->insert_text_at_caret("item_1() test");
  3628. code_edit->set_caret_column(2);
  3629. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_CLASS, "item_0()", "item_0()");
  3630. code_edit->update_code_completion_options();
  3631. SEND_GUI_ACTION("ui_text_completion_replace");
  3632. CHECK(code_edit->get_line(0) == "item_0() test");
  3633. CHECK(code_edit->get_caret_column() == 8);
  3634. }
  3635. }
  3636. SUBCASE("[CodeEdit] autocomplete suggestion order") {
  3637. /* Prefer less fragmented suggestion. */
  3638. code_edit->clear();
  3639. code_edit->insert_text_at_caret("te");
  3640. code_edit->set_caret_column(2);
  3641. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "test", "test");
  3642. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "tset", "tset");
  3643. code_edit->update_code_completion_options();
  3644. code_edit->confirm_code_completion();
  3645. CHECK(code_edit->get_line(0) == "test");
  3646. /* Prefer suggestion starting with the string to complete (matching start). */
  3647. code_edit->clear();
  3648. code_edit->insert_text_at_caret("te");
  3649. code_edit->set_caret_column(2);
  3650. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "test", "test");
  3651. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "stest", "stest");
  3652. code_edit->update_code_completion_options();
  3653. code_edit->confirm_code_completion();
  3654. CHECK(code_edit->get_line(0) == "test");
  3655. /* Prefer less fragment over matching start. */
  3656. code_edit->clear();
  3657. code_edit->insert_text_at_caret("te");
  3658. code_edit->set_caret_column(2);
  3659. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "tset", "tset");
  3660. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "stest", "stest");
  3661. code_edit->update_code_completion_options();
  3662. code_edit->confirm_code_completion();
  3663. CHECK(code_edit->get_line(0) == "stest");
  3664. /* Prefer good capitalization. */
  3665. code_edit->clear();
  3666. code_edit->insert_text_at_caret("te");
  3667. code_edit->set_caret_column(2);
  3668. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "test", "test");
  3669. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "Test", "Test");
  3670. code_edit->update_code_completion_options();
  3671. code_edit->confirm_code_completion();
  3672. CHECK(code_edit->get_line(0) == "test");
  3673. /* Prefer matching start over good capitalization. */
  3674. code_edit->clear();
  3675. code_edit->insert_text_at_caret("te");
  3676. code_edit->set_caret_column(2);
  3677. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "Test", "Test");
  3678. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "stest_bis", "test_bis");
  3679. code_edit->update_code_completion_options();
  3680. code_edit->confirm_code_completion();
  3681. CHECK(code_edit->get_line(0) == "Test");
  3682. /* Prefer closer location. */
  3683. code_edit->clear();
  3684. code_edit->insert_text_at_caret("te");
  3685. code_edit->set_caret_column(2);
  3686. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "test", "test");
  3687. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "test_bis", "test_bis", Color(1, 1, 1), Ref<Resource>(), Variant::NIL, CodeEdit::LOCATION_LOCAL);
  3688. code_edit->update_code_completion_options();
  3689. code_edit->confirm_code_completion();
  3690. CHECK(code_edit->get_line(0) == "test_bis");
  3691. /* Prefer good capitalization over location. */
  3692. code_edit->clear();
  3693. code_edit->insert_text_at_caret("te");
  3694. code_edit->set_caret_column(2);
  3695. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "test", "test");
  3696. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "Test", "Test", Color(1, 1, 1), Ref<Resource>(), Variant::NIL, CodeEdit::LOCATION_LOCAL);
  3697. code_edit->update_code_completion_options();
  3698. code_edit->confirm_code_completion();
  3699. CHECK(code_edit->get_line(0) == "test");
  3700. /* Prefer the start of the string to complete being closest to the start of the suggestion (closest to start). */
  3701. code_edit->clear();
  3702. code_edit->insert_text_at_caret("te");
  3703. code_edit->set_caret_column(2);
  3704. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "stest", "stest");
  3705. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "sstest", "sstest");
  3706. code_edit->update_code_completion_options();
  3707. code_edit->confirm_code_completion();
  3708. CHECK(code_edit->get_line(0) == "stest");
  3709. /* Prefer location over closest to start. */
  3710. code_edit->clear();
  3711. code_edit->insert_text_at_caret("te");
  3712. code_edit->set_caret_column(2);
  3713. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "stest", "stest");
  3714. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "sstest", "sstest", Color(1, 1, 1), Ref<Resource>(), Variant::NIL, CodeEdit::LOCATION_LOCAL);
  3715. code_edit->update_code_completion_options();
  3716. code_edit->confirm_code_completion();
  3717. CHECK(code_edit->get_line(0) == "sstest");
  3718. }
  3719. SUBCASE("[CodeEdit] autocomplete currently selected option") {
  3720. code_edit->set_code_completion_enabled(true);
  3721. REQUIRE(code_edit->is_code_completion_enabled());
  3722. // Initially select item 0.
  3723. code_edit->insert_text_at_caret("te");
  3724. code_edit->set_caret_column(2);
  3725. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te1", "te1");
  3726. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te3", "te3");
  3727. code_edit->update_code_completion_options();
  3728. CHECK_MESSAGE(code_edit->get_code_completion_selected_index() == 0, "Initially selected item should be 0.");
  3729. // After adding later options shouldn't update selection.
  3730. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te1", "te1");
  3731. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te3", "te3");
  3732. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te4", "te4"); // Added te4.
  3733. code_edit->update_code_completion_options();
  3734. CHECK_MESSAGE(code_edit->get_code_completion_selected_index() == 0, "Adding later options shouldn't update selection.");
  3735. code_edit->set_code_completion_selected_index(2);
  3736. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te1", "te1");
  3737. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te3", "te3");
  3738. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te4", "te4");
  3739. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te5", "te5"); // Added te5.
  3740. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te6", "te6"); // Added te6.
  3741. code_edit->update_code_completion_options();
  3742. CHECK_MESSAGE(code_edit->get_code_completion_selected_index() == 2, "Adding later options shouldn't update selection.");
  3743. // Removing elements after selected element shouldn't update selection.
  3744. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te1", "te1");
  3745. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te3", "te3");
  3746. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te4", "te4");
  3747. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te5", "te5"); // Removed te6.
  3748. code_edit->update_code_completion_options();
  3749. CHECK_MESSAGE(code_edit->get_code_completion_selected_index() == 2, "Removing elements after selected element shouldn't update selection.");
  3750. // Changing elements after selected element shouldn't update selection.
  3751. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te1", "te1");
  3752. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te3", "te3");
  3753. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te4", "te4");
  3754. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te6", "te6"); // Changed te5->te6.
  3755. code_edit->update_code_completion_options();
  3756. CHECK_MESSAGE(code_edit->get_code_completion_selected_index() == 2, "Changing elements after selected element shouldn't update selection.");
  3757. // Changing elements before selected element should reset selection.
  3758. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te2", "te2"); // Changed te1->te2.
  3759. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te3", "te3");
  3760. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te4", "te4");
  3761. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te6", "te6");
  3762. code_edit->update_code_completion_options();
  3763. CHECK_MESSAGE(code_edit->get_code_completion_selected_index() == 0, "Changing elements before selected element should reset selection.");
  3764. // Removing elements before selected element should reset selection.
  3765. code_edit->set_code_completion_selected_index(2);
  3766. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te3", "te3"); // Removed te2.
  3767. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te4", "te4");
  3768. code_edit->add_code_completion_option(CodeEdit::CodeCompletionKind::KIND_VARIABLE, "te6", "te6");
  3769. code_edit->update_code_completion_options();
  3770. CHECK_MESSAGE(code_edit->get_code_completion_selected_index() == 0, "Removing elements before selected element should reset selection.");
  3771. }
  3772. memdelete(code_edit);
  3773. }
  3774. TEST_CASE("[SceneTree][CodeEdit] symbol lookup") {
  3775. CodeEdit *code_edit = memnew(CodeEdit);
  3776. SceneTree::get_singleton()->get_root()->add_child(code_edit);
  3777. code_edit->grab_focus();
  3778. code_edit->set_symbol_lookup_on_click_enabled(true);
  3779. CHECK(code_edit->is_symbol_lookup_on_click_enabled());
  3780. if (TS->has_feature(TextServer::FEATURE_FONT_DYNAMIC) && TS->has_feature(TextServer::FEATURE_SIMPLE_LAYOUT)) {
  3781. /* Set size for mouse input. */
  3782. code_edit->set_size(Size2(100, 100));
  3783. code_edit->set_text("this is some text");
  3784. Point2 caret_pos = code_edit->get_caret_draw_pos();
  3785. caret_pos.x += 60;
  3786. SEND_GUI_MOUSE_BUTTON_EVENT(caret_pos, MouseButton::NONE, 0, Key::NONE);
  3787. CHECK(code_edit->get_text_for_symbol_lookup() == "this is s" + String::chr(0xFFFF) + "ome text");
  3788. SIGNAL_WATCH(code_edit, "symbol_validate");
  3789. #ifdef MACOS_ENABLED
  3790. SEND_GUI_KEY_EVENT(Key::META);
  3791. #else
  3792. SEND_GUI_KEY_EVENT(Key::CTRL);
  3793. #endif
  3794. Array signal_args = build_array(build_array("some"));
  3795. SIGNAL_CHECK("symbol_validate", signal_args);
  3796. SIGNAL_UNWATCH(code_edit, "symbol_validate");
  3797. }
  3798. memdelete(code_edit);
  3799. }
  3800. TEST_CASE("[SceneTree][CodeEdit] line length guidelines") {
  3801. CodeEdit *code_edit = memnew(CodeEdit);
  3802. SceneTree::get_singleton()->get_root()->add_child(code_edit);
  3803. code_edit->grab_focus();
  3804. TypedArray<int> guide_lines;
  3805. code_edit->set_line_length_guidelines(guide_lines);
  3806. CHECK(code_edit->get_line_length_guidelines().size() == 0);
  3807. guide_lines.push_back(80);
  3808. guide_lines.push_back(120);
  3809. /* Order should be preserved. */
  3810. code_edit->set_line_length_guidelines(guide_lines);
  3811. CHECK((int)code_edit->get_line_length_guidelines()[0] == 80);
  3812. CHECK((int)code_edit->get_line_length_guidelines()[1] == 120);
  3813. memdelete(code_edit);
  3814. }
  3815. TEST_CASE("[SceneTree][CodeEdit] text manipulation") {
  3816. CodeEdit *code_edit = memnew(CodeEdit);
  3817. SceneTree::get_singleton()->get_root()->add_child(code_edit);
  3818. code_edit->grab_focus();
  3819. SUBCASE("[SceneTree][CodeEdit] backspace") {
  3820. // Backspace with selection on first line.
  3821. code_edit->set_text("test backspace");
  3822. code_edit->select(0, 0, 0, 5);
  3823. code_edit->backspace();
  3824. CHECK(code_edit->get_line(0) == "backspace");
  3825. CHECK(code_edit->get_caret_line() == 0);
  3826. CHECK(code_edit->get_caret_column() == 0);
  3827. // Backspace with selection on first line and caret at the beginning of file.
  3828. code_edit->set_text("test backspace");
  3829. code_edit->select(0, 5, 0, 0);
  3830. code_edit->backspace();
  3831. CHECK(code_edit->get_line(0) == "backspace");
  3832. CHECK(code_edit->get_caret_line() == 0);
  3833. CHECK(code_edit->get_caret_column() == 0);
  3834. // Move caret up to the previous line on backspace if caret is at the first column.
  3835. code_edit->set_text("line 1\nline 2");
  3836. code_edit->set_caret_line(1);
  3837. code_edit->set_caret_column(0);
  3838. code_edit->backspace();
  3839. CHECK(code_edit->get_line(0) == "line 1line 2");
  3840. CHECK(code_edit->get_caret_line() == 0);
  3841. CHECK(code_edit->get_caret_column() == 6);
  3842. // Multiple carets with a caret at the first column.
  3843. code_edit->set_text("line 1\nline 2");
  3844. code_edit->set_caret_line(1);
  3845. code_edit->set_caret_column(2);
  3846. code_edit->add_caret(1, 0);
  3847. code_edit->add_caret(1, 5);
  3848. code_edit->backspace();
  3849. CHECK(code_edit->get_text() == "line 1lne2");
  3850. CHECK(code_edit->get_caret_count() == 3);
  3851. CHECK(code_edit->get_caret_line(0) == 0);
  3852. CHECK(code_edit->get_caret_column(0) == 7);
  3853. CHECK(code_edit->get_caret_line(1) == 0);
  3854. CHECK(code_edit->get_caret_column(1) == 6);
  3855. CHECK(code_edit->get_caret_line(2) == 0);
  3856. CHECK(code_edit->get_caret_column(2) == 9);
  3857. code_edit->remove_secondary_carets();
  3858. // Multiple carets close together.
  3859. code_edit->set_text("line 1\nline 2");
  3860. code_edit->set_caret_line(1);
  3861. code_edit->set_caret_column(2);
  3862. code_edit->add_caret(1, 1);
  3863. code_edit->backspace();
  3864. CHECK(code_edit->get_text() == "line 1\nne 2");
  3865. CHECK(code_edit->get_caret_count() == 1);
  3866. CHECK(code_edit->get_caret_line() == 1);
  3867. CHECK(code_edit->get_caret_column() == 0);
  3868. // Backspace delete all text if all text is selected.
  3869. code_edit->set_text("line 1\nline 2\nline 3");
  3870. code_edit->select_all();
  3871. code_edit->backspace();
  3872. CHECK(code_edit->get_text().is_empty());
  3873. CHECK_FALSE(code_edit->has_selection());
  3874. CHECK(code_edit->get_caret_line() == 0);
  3875. CHECK(code_edit->get_caret_column() == 0);
  3876. // Backspace at the beginning without selection has no effect.
  3877. code_edit->set_text("line 1\nline 2\nline 3");
  3878. code_edit->set_caret_line(0);
  3879. code_edit->set_caret_column(0);
  3880. code_edit->backspace();
  3881. CHECK(code_edit->get_text() == "line 1\nline 2\nline 3");
  3882. CHECK(code_edit->get_caret_line() == 0);
  3883. CHECK(code_edit->get_caret_column() == 0);
  3884. // Unfold previous folded line on backspace if the caret is at the first column.
  3885. code_edit->set_line_folding_enabled(true);
  3886. code_edit->set_text("line 1\n\tline 2\nline 3");
  3887. code_edit->set_caret_line(2);
  3888. code_edit->set_caret_column(0);
  3889. code_edit->fold_line(0);
  3890. code_edit->backspace();
  3891. CHECK_FALSE(code_edit->is_line_folded(0));
  3892. code_edit->set_line_folding_enabled(false);
  3893. // Do not unfold previous line on backspace if the caret is not at the first column.
  3894. code_edit->set_line_folding_enabled(true);
  3895. code_edit->set_text("line 1\n\tline 2\nline 3");
  3896. code_edit->set_caret_line(2);
  3897. code_edit->set_caret_column(4);
  3898. code_edit->fold_line(0);
  3899. code_edit->backspace();
  3900. CHECK(code_edit->is_line_folded(0));
  3901. code_edit->set_line_folding_enabled(false);
  3902. }
  3903. SUBCASE("[TextEdit] cut") {
  3904. DisplayServerMock *DS = (DisplayServerMock *)(DisplayServer::get_singleton());
  3905. code_edit->set_line_folding_enabled(true);
  3906. // Cut without a selection removes the entire line.
  3907. code_edit->set_text("this is\nsome\n");
  3908. code_edit->set_caret_line(0);
  3909. code_edit->set_caret_column(6);
  3910. code_edit->cut();
  3911. CHECK(DS->clipboard_get() == "this is\n");
  3912. CHECK(code_edit->get_text() == "some\n");
  3913. CHECK(code_edit->get_caret_line() == 0);
  3914. CHECK(code_edit->get_caret_column() == 3); // In the default font, this is the same position.
  3915. // Undo restores the cut text.
  3916. code_edit->undo();
  3917. CHECK(DS->clipboard_get() == "this is\n");
  3918. CHECK(code_edit->get_text() == "this is\nsome\n");
  3919. CHECK(code_edit->get_caret_line() == 0);
  3920. CHECK(code_edit->get_caret_column() == 6);
  3921. // Redo.
  3922. code_edit->redo();
  3923. CHECK(DS->clipboard_get() == "this is\n");
  3924. CHECK(code_edit->get_text() == "some\n");
  3925. CHECK(code_edit->get_caret_line() == 0);
  3926. CHECK(code_edit->get_caret_column() == 3);
  3927. // Cut unfolds the line.
  3928. code_edit->set_text("this is\n\tsome\n");
  3929. code_edit->fold_line(0);
  3930. CHECK(code_edit->is_line_folded(0));
  3931. code_edit->cut();
  3932. CHECK_FALSE(code_edit->is_line_folded(0));
  3933. CHECK(DS->clipboard_get() == "this is\n");
  3934. CHECK(code_edit->get_text() == "\tsome\n");
  3935. CHECK(code_edit->get_caret_line() == 0);
  3936. // Cut with a selection removes just the selection.
  3937. code_edit->set_text("this is\nsome\n");
  3938. code_edit->select(0, 5, 0, 7);
  3939. SEND_GUI_ACTION("ui_cut");
  3940. CHECK(code_edit->get_viewport()->is_input_handled());
  3941. CHECK(DS->clipboard_get() == "is");
  3942. CHECK(code_edit->get_text() == "this \nsome\n");
  3943. CHECK_FALSE(code_edit->get_caret_line());
  3944. CHECK(code_edit->get_caret_line() == 0);
  3945. CHECK(code_edit->get_caret_column() == 5);
  3946. // Cut does not change the text if not editable. Text is still added to clipboard.
  3947. code_edit->set_text("this is\nsome\n");
  3948. code_edit->set_caret_line(0);
  3949. code_edit->set_caret_column(5);
  3950. code_edit->set_editable(false);
  3951. code_edit->cut();
  3952. code_edit->set_editable(true);
  3953. CHECK(DS->clipboard_get() == "this is\n");
  3954. CHECK(code_edit->get_text() == "this is\nsome\n");
  3955. CHECK(code_edit->get_caret_line() == 0);
  3956. CHECK(code_edit->get_caret_column() == 5);
  3957. // Cut line with multiple carets.
  3958. code_edit->set_text("this is\nsome\n");
  3959. code_edit->set_caret_line(0);
  3960. code_edit->set_caret_column(3);
  3961. code_edit->add_caret(0, 2);
  3962. code_edit->add_caret(0, 4);
  3963. code_edit->add_caret(2, 0);
  3964. code_edit->cut();
  3965. CHECK(DS->clipboard_get() == "this is\n\n");
  3966. CHECK(code_edit->get_text() == "some");
  3967. CHECK(code_edit->get_caret_count() == 3);
  3968. CHECK_FALSE(code_edit->has_selection(0));
  3969. CHECK(code_edit->get_caret_line(0) == 0);
  3970. CHECK(code_edit->get_caret_column(0) == 2); // In the default font, this is the same position.
  3971. // The previous caret at index 1 was merged.
  3972. CHECK_FALSE(code_edit->has_selection(1));
  3973. CHECK(code_edit->get_caret_line(1) == 0);
  3974. CHECK(code_edit->get_caret_column(1) == 3); // In the default font, this is the same position.
  3975. CHECK_FALSE(code_edit->has_selection(2));
  3976. CHECK(code_edit->get_caret_line(2) == 0);
  3977. CHECK(code_edit->get_caret_column(2) == 4);
  3978. code_edit->remove_secondary_carets();
  3979. // Cut on the only line removes the contents.
  3980. code_edit->set_caret_line(0);
  3981. code_edit->set_caret_column(2);
  3982. code_edit->cut();
  3983. CHECK(DS->clipboard_get() == "some\n");
  3984. CHECK(code_edit->get_text() == "");
  3985. CHECK(code_edit->get_line_count() == 1);
  3986. CHECK(code_edit->get_caret_line() == 0);
  3987. CHECK(code_edit->get_caret_column() == 0);
  3988. // Cut empty line.
  3989. code_edit->cut();
  3990. CHECK(DS->clipboard_get() == "\n");
  3991. CHECK(code_edit->get_text() == "");
  3992. CHECK(code_edit->get_caret_line() == 0);
  3993. CHECK(code_edit->get_caret_column() == 0);
  3994. // Cut multiple lines, in order.
  3995. code_edit->set_text("this is\nsome\ntext to\nbe\n\ncut");
  3996. code_edit->set_caret_line(2);
  3997. code_edit->set_caret_column(7);
  3998. code_edit->add_caret(3, 0);
  3999. code_edit->add_caret(0, 2);
  4000. code_edit->cut();
  4001. CHECK(DS->clipboard_get() == "this is\ntext to\nbe\n");
  4002. CHECK(code_edit->get_text() == "some\n\ncut");
  4003. CHECK(code_edit->get_caret_count() == 2);
  4004. CHECK(code_edit->get_caret_line(0) == 1);
  4005. CHECK(code_edit->get_caret_column(0) == 0);
  4006. CHECK(code_edit->get_caret_line(1) == 0);
  4007. CHECK(code_edit->get_caret_column(1) == 2);
  4008. code_edit->remove_secondary_carets();
  4009. // Cut multiple selections, in order. Ignores regular carets.
  4010. code_edit->set_text("this is\nsome\ntext to\nbe\n\ncut");
  4011. code_edit->add_caret(3, 0);
  4012. code_edit->add_caret(0, 2);
  4013. code_edit->add_caret(2, 0);
  4014. code_edit->select(1, 0, 1, 2, 0);
  4015. code_edit->select(3, 0, 4, 0, 1);
  4016. code_edit->select(0, 5, 0, 3, 2);
  4017. code_edit->cut();
  4018. CHECK(DS->clipboard_get() == "s \nso\nbe\n");
  4019. CHECK(code_edit->get_text() == "thiis\nme\ntext to\n\ncut");
  4020. CHECK(code_edit->get_caret_count() == 4);
  4021. CHECK_FALSE(code_edit->has_selection());
  4022. CHECK(code_edit->get_caret_line(0) == 1);
  4023. CHECK(code_edit->get_caret_column(0) == 0);
  4024. CHECK(code_edit->get_caret_line(1) == 3);
  4025. CHECK(code_edit->get_caret_column(1) == 0);
  4026. CHECK(code_edit->get_caret_line(2) == 0);
  4027. CHECK(code_edit->get_caret_column(2) == 3);
  4028. CHECK(code_edit->get_caret_line(3) == 2);
  4029. CHECK(code_edit->get_caret_column(3) == 0);
  4030. }
  4031. SUBCASE("[SceneTree][CodeEdit] new line") {
  4032. // Add a new line.
  4033. code_edit->set_text("test new line");
  4034. code_edit->set_caret_line(0);
  4035. code_edit->set_caret_column(13);
  4036. SEND_GUI_ACTION("ui_text_newline");
  4037. CHECK(code_edit->get_line(0) == "test new line");
  4038. CHECK(code_edit->get_line(1) == "");
  4039. CHECK(code_edit->get_caret_line() == 1);
  4040. CHECK(code_edit->get_caret_column() == 0);
  4041. // Split line with new line.
  4042. code_edit->set_text("test new line");
  4043. code_edit->set_caret_line(0);
  4044. code_edit->set_caret_column(5);
  4045. SEND_GUI_ACTION("ui_text_newline");
  4046. CHECK(code_edit->get_line(0) == "test ");
  4047. CHECK(code_edit->get_line(1) == "new line");
  4048. CHECK(code_edit->get_caret_line() == 1);
  4049. CHECK(code_edit->get_caret_column() == 0);
  4050. // Delete selection and split with new line.
  4051. code_edit->set_text("test new line");
  4052. code_edit->select(0, 0, 0, 5);
  4053. SEND_GUI_ACTION("ui_text_newline");
  4054. CHECK(code_edit->get_line(0) == "");
  4055. CHECK(code_edit->get_line(1) == "new line");
  4056. CHECK(code_edit->get_caret_line() == 1);
  4057. CHECK(code_edit->get_caret_column() == 0);
  4058. // Blank new line below with selection should not split.
  4059. code_edit->set_text("test new line");
  4060. code_edit->select(0, 0, 0, 5);
  4061. SEND_GUI_ACTION("ui_text_newline_blank");
  4062. CHECK(code_edit->get_line(0) == "test new line");
  4063. CHECK(code_edit->get_line(1) == "");
  4064. CHECK(code_edit->get_caret_line() == 1);
  4065. CHECK(code_edit->get_caret_column() == 0);
  4066. // Blank new line above with selection should not split.
  4067. code_edit->set_text("test new line");
  4068. code_edit->select(0, 0, 0, 5);
  4069. SEND_GUI_ACTION("ui_text_newline_above");
  4070. CHECK(code_edit->get_line(0) == "");
  4071. CHECK(code_edit->get_line(1) == "test new line");
  4072. CHECK(code_edit->get_caret_line() == 0);
  4073. CHECK(code_edit->get_caret_column() == 0);
  4074. // Multiple new lines with multiple carets.
  4075. code_edit->set_text("test new line");
  4076. code_edit->set_caret_line(0);
  4077. code_edit->set_caret_column(5);
  4078. code_edit->add_caret(0, 8);
  4079. SEND_GUI_ACTION("ui_text_newline");
  4080. CHECK(code_edit->get_line(0) == "test ");
  4081. CHECK(code_edit->get_line(1) == "new");
  4082. CHECK(code_edit->get_line(2) == " line");
  4083. CHECK(code_edit->get_caret_count() == 2);
  4084. CHECK(code_edit->get_caret_line(0) == 1);
  4085. CHECK(code_edit->get_caret_column(0) == 0);
  4086. CHECK(code_edit->get_caret_line(1) == 2);
  4087. CHECK(code_edit->get_caret_column(1) == 0);
  4088. // Multiple blank new lines with multiple carets.
  4089. code_edit->set_text("test new line");
  4090. code_edit->remove_secondary_carets();
  4091. code_edit->set_caret_line(0);
  4092. code_edit->set_caret_column(5);
  4093. code_edit->add_caret(0, 8);
  4094. SEND_GUI_ACTION("ui_text_newline_blank");
  4095. CHECK(code_edit->get_line(0) == "test new line");
  4096. CHECK(code_edit->get_line(1) == "");
  4097. CHECK(code_edit->get_line(2) == "");
  4098. CHECK(code_edit->get_caret_count() == 2);
  4099. CHECK(code_edit->get_caret_line(0) == 2);
  4100. CHECK(code_edit->get_caret_column(0) == 0);
  4101. CHECK(code_edit->get_caret_line(1) == 1);
  4102. CHECK(code_edit->get_caret_column(1) == 0);
  4103. // Multiple new lines above with multiple carets.
  4104. code_edit->set_text("test new line");
  4105. code_edit->remove_secondary_carets();
  4106. code_edit->set_caret_line(0);
  4107. code_edit->set_caret_column(5);
  4108. code_edit->add_caret(0, 8);
  4109. SEND_GUI_ACTION("ui_text_newline_above");
  4110. CHECK(code_edit->get_line(0) == "");
  4111. CHECK(code_edit->get_line(1) == "");
  4112. CHECK(code_edit->get_line(2) == "test new line");
  4113. CHECK(code_edit->get_caret_count() == 2);
  4114. CHECK(code_edit->get_caret_line(0) == 0);
  4115. CHECK(code_edit->get_caret_column(0) == 0);
  4116. CHECK(code_edit->get_caret_line(1) == 1);
  4117. CHECK(code_edit->get_caret_column(1) == 0);
  4118. // See '[CodeEdit] auto indent' tests for tests about new line with indentation.
  4119. }
  4120. SUBCASE("[SceneTree][CodeEdit] move lines up") {
  4121. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4122. // Move line up with caret on it.
  4123. code_edit->set_caret_line(2);
  4124. code_edit->set_caret_column(1);
  4125. code_edit->move_lines_up();
  4126. CHECK(code_edit->get_text() == "test\nto\nlines\n\nmove\naround");
  4127. CHECK(code_edit->get_caret_line() == 1);
  4128. CHECK(code_edit->get_caret_column() == 1);
  4129. // Undo.
  4130. code_edit->undo();
  4131. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4132. CHECK(code_edit->get_caret_line() == 2);
  4133. CHECK(code_edit->get_caret_column() == 1);
  4134. // Redo.
  4135. code_edit->redo();
  4136. CHECK(code_edit->get_text() == "test\nto\nlines\n\nmove\naround");
  4137. CHECK(code_edit->get_caret_line() == 1);
  4138. CHECK(code_edit->get_caret_column() == 1);
  4139. // Does nothing at the first line.
  4140. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4141. code_edit->set_caret_line(0);
  4142. code_edit->set_caret_column(1);
  4143. code_edit->move_lines_up();
  4144. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4145. CHECK(code_edit->get_caret_line() == 0);
  4146. CHECK(code_edit->get_caret_column() == 1);
  4147. // Does nothing at the first line when selection ends at column 0.
  4148. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4149. code_edit->select(0, 0, 1, 0);
  4150. code_edit->move_lines_up();
  4151. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4152. CHECK(code_edit->has_selection());
  4153. CHECK(code_edit->get_selection_origin_line() == 0);
  4154. CHECK(code_edit->get_selection_origin_column() == 0);
  4155. CHECK(code_edit->get_caret_line() == 1);
  4156. CHECK(code_edit->get_caret_column() == 0);
  4157. // Works on empty line.
  4158. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4159. code_edit->set_caret_line(3);
  4160. code_edit->set_caret_column(0);
  4161. code_edit->move_lines_up();
  4162. CHECK(code_edit->get_text() == "test\nlines\n\nto\nmove\naround");
  4163. CHECK(code_edit->get_caret_line() == 2);
  4164. CHECK(code_edit->get_caret_column() == 0);
  4165. // Move multiple lines up with selection.
  4166. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4167. code_edit->select(4, 0, 5, 1);
  4168. code_edit->move_lines_up();
  4169. CHECK(code_edit->get_text() == "test\nlines\nto\nmove\naround\n");
  4170. CHECK(code_edit->has_selection());
  4171. CHECK(code_edit->get_selection_origin_line() == 3);
  4172. CHECK(code_edit->get_selection_origin_column() == 0);
  4173. CHECK(code_edit->get_caret_line() == 4);
  4174. CHECK(code_edit->get_caret_column() == 1);
  4175. // Does not affect line with selection end at column 0.
  4176. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4177. code_edit->select(4, 0, 5, 0);
  4178. code_edit->move_lines_up();
  4179. CHECK(code_edit->get_text() == "test\nlines\nto\nmove\n\naround");
  4180. CHECK(code_edit->has_selection());
  4181. CHECK(code_edit->get_selection_origin_line() == 3);
  4182. CHECK(code_edit->get_selection_origin_column() == 0);
  4183. CHECK(code_edit->get_caret_line() == 4);
  4184. CHECK(code_edit->get_caret_column() == 0);
  4185. // Move multiple lines up with selection, right to left selection.
  4186. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4187. code_edit->select(5, 2, 4, 1);
  4188. code_edit->move_lines_up();
  4189. CHECK(code_edit->get_text() == "test\nlines\nto\nmove\naround\n");
  4190. CHECK(code_edit->has_selection());
  4191. CHECK(code_edit->get_selection_origin_line() == 4);
  4192. CHECK(code_edit->get_selection_origin_column() == 2);
  4193. CHECK(code_edit->get_caret_line() == 3);
  4194. CHECK(code_edit->get_caret_column() == 1);
  4195. // Move multiple lines with multiple carets. A line with multiple carets is only moved once.
  4196. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4197. code_edit->select(5, 2, 5, 4);
  4198. code_edit->add_caret(4, 0);
  4199. code_edit->add_caret(4, 4);
  4200. code_edit->move_lines_up();
  4201. CHECK(code_edit->get_text() == "test\nlines\nto\nmove\naround\n");
  4202. CHECK(code_edit->get_caret_count() == 3);
  4203. CHECK(code_edit->has_selection(0));
  4204. CHECK(code_edit->get_selection_origin_line(0) == 4);
  4205. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4206. CHECK(code_edit->get_caret_line(0) == 4);
  4207. CHECK(code_edit->get_caret_column(0) == 4);
  4208. CHECK_FALSE(code_edit->has_selection(1));
  4209. CHECK(code_edit->get_caret_line(1) == 3);
  4210. CHECK(code_edit->get_caret_column(1) == 0);
  4211. CHECK_FALSE(code_edit->has_selection(2));
  4212. CHECK(code_edit->get_caret_line(2) == 3);
  4213. CHECK(code_edit->get_caret_column(2) == 4);
  4214. code_edit->remove_secondary_carets();
  4215. // Move multiple separate lines with multiple selections.
  4216. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4217. code_edit->select(2, 2, 1, 4);
  4218. code_edit->add_caret(5, 0);
  4219. code_edit->select(5, 0, 5, 1, 1);
  4220. code_edit->move_lines_up();
  4221. CHECK(code_edit->get_text() == "lines\nto\ntest\n\naround\nmove");
  4222. CHECK(code_edit->get_caret_count() == 2);
  4223. CHECK(code_edit->has_selection(0));
  4224. CHECK(code_edit->get_selection_origin_line(0) == 1);
  4225. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4226. CHECK(code_edit->get_caret_line(0) == 0);
  4227. CHECK(code_edit->get_caret_column(0) == 4);
  4228. CHECK(code_edit->has_selection(1));
  4229. CHECK(code_edit->get_selection_origin_line(1) == 4);
  4230. CHECK(code_edit->get_selection_origin_column(1) == 0);
  4231. CHECK(code_edit->get_caret_line(1) == 4);
  4232. CHECK(code_edit->get_caret_column(1) == 1);
  4233. code_edit->remove_secondary_carets();
  4234. // Move lines with adjacent selections that end at column 0.
  4235. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4236. code_edit->select(1, 2, 2, 0);
  4237. code_edit->add_caret(2, 2);
  4238. code_edit->select(2, 2, 3, 0, 1);
  4239. code_edit->move_lines_up();
  4240. CHECK(code_edit->get_text() == "lines\nto\ntest\n\nmove\naround");
  4241. CHECK(code_edit->get_caret_count() == 2);
  4242. CHECK(code_edit->has_selection(0));
  4243. CHECK(code_edit->get_selection_origin_line(0) == 0);
  4244. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4245. CHECK(code_edit->get_caret_line(0) == 1);
  4246. CHECK(code_edit->get_caret_column(0) == 0);
  4247. CHECK(code_edit->has_selection(1));
  4248. CHECK(code_edit->get_selection_origin_line(1) == 1);
  4249. CHECK(code_edit->get_selection_origin_column(1) == 2);
  4250. CHECK(code_edit->get_caret_line(1) == 2);
  4251. CHECK(code_edit->get_caret_column(1) == 0);
  4252. code_edit->remove_secondary_carets();
  4253. code_edit->deselect();
  4254. code_edit->set_line_folding_enabled(true);
  4255. // Move line up into a folded region unfolds it.
  4256. code_edit->set_text("test\n\tline1 test\n\t\tline 2\ntest2");
  4257. code_edit->set_caret_line(3);
  4258. code_edit->set_caret_column(0);
  4259. code_edit->fold_line(0);
  4260. CHECK(code_edit->is_line_folded(0));
  4261. code_edit->move_lines_up();
  4262. CHECK(code_edit->get_caret_count() == 1);
  4263. CHECK_FALSE(code_edit->has_selection(0));
  4264. CHECK(code_edit->get_caret_line() == 2);
  4265. CHECK(code_edit->get_caret_column() == 0);
  4266. CHECK(code_edit->get_text() == "test\n\tline1 test\ntest2\n\t\tline 2");
  4267. CHECK_FALSE(code_edit->is_line_folded(0));
  4268. }
  4269. SUBCASE("[SceneTree][CodeEdit] move lines down") {
  4270. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4271. // Move line down with caret on it.
  4272. code_edit->set_caret_line(1);
  4273. code_edit->set_caret_column(1);
  4274. code_edit->move_lines_down();
  4275. CHECK(code_edit->get_text() == "test\nto\nlines\n\nmove\naround");
  4276. CHECK(code_edit->get_caret_line() == 2);
  4277. CHECK(code_edit->get_caret_column() == 1);
  4278. // Undo.
  4279. code_edit->undo();
  4280. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4281. CHECK(code_edit->get_caret_line() == 1);
  4282. CHECK(code_edit->get_caret_column() == 1);
  4283. // Redo.
  4284. code_edit->redo();
  4285. CHECK(code_edit->get_text() == "test\nto\nlines\n\nmove\naround");
  4286. CHECK(code_edit->get_caret_line() == 2);
  4287. CHECK(code_edit->get_caret_column() == 1);
  4288. // Does nothing at the last line.
  4289. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4290. code_edit->set_caret_line(5);
  4291. code_edit->set_caret_column(1);
  4292. code_edit->move_lines_down();
  4293. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4294. CHECK(code_edit->get_caret_line() == 5);
  4295. CHECK(code_edit->get_caret_column() == 1);
  4296. // Does nothing at the last line when selection ends at column 0.
  4297. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4298. code_edit->select(4, 0, 5, 0);
  4299. code_edit->move_lines_down();
  4300. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4301. CHECK(code_edit->has_selection());
  4302. CHECK(code_edit->get_selection_origin_line() == 4);
  4303. CHECK(code_edit->get_selection_origin_column() == 0);
  4304. CHECK(code_edit->get_caret_line() == 5);
  4305. CHECK(code_edit->get_caret_column() == 0);
  4306. // Works on empty line.
  4307. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4308. code_edit->set_caret_line(3);
  4309. code_edit->set_caret_column(0);
  4310. code_edit->move_lines_down();
  4311. CHECK(code_edit->get_text() == "test\nlines\nto\nmove\n\naround");
  4312. CHECK(code_edit->get_caret_line() == 4);
  4313. CHECK(code_edit->get_caret_column() == 0);
  4314. // Move multiple lines down with selection.
  4315. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4316. code_edit->select(1, 0, 2, 1);
  4317. code_edit->move_lines_down();
  4318. CHECK(code_edit->get_text() == "test\n\nlines\nto\nmove\naround");
  4319. CHECK(code_edit->has_selection());
  4320. CHECK(code_edit->get_selection_origin_line() == 2);
  4321. CHECK(code_edit->get_selection_origin_column() == 0);
  4322. CHECK(code_edit->get_caret_line() == 3);
  4323. CHECK(code_edit->get_caret_column() == 1);
  4324. // Does not affect line with selection end at column 0.
  4325. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4326. code_edit->select(1, 0, 2, 0);
  4327. code_edit->move_lines_down();
  4328. CHECK(code_edit->get_text() == "test\nto\nlines\n\nmove\naround");
  4329. CHECK(code_edit->has_selection());
  4330. CHECK(code_edit->get_selection_origin_line() == 2);
  4331. CHECK(code_edit->get_selection_origin_column() == 0);
  4332. CHECK(code_edit->get_caret_line() == 3);
  4333. CHECK(code_edit->get_caret_column() == 0);
  4334. // Move multiple lines down with selection, right to left selection.
  4335. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4336. code_edit->select(2, 2, 1, 1);
  4337. code_edit->move_lines_down();
  4338. CHECK(code_edit->get_text() == "test\n\nlines\nto\nmove\naround");
  4339. CHECK(code_edit->has_selection());
  4340. CHECK(code_edit->get_selection_origin_line() == 3);
  4341. CHECK(code_edit->get_selection_origin_column() == 2);
  4342. CHECK(code_edit->get_caret_line() == 2);
  4343. CHECK(code_edit->get_caret_column() == 1);
  4344. // Move multiple lines with multiple carets. A line with multiple carets is only moved once.
  4345. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4346. code_edit->select(1, 2, 1, 4);
  4347. code_edit->add_caret(0, 0);
  4348. code_edit->add_caret(0, 1);
  4349. code_edit->move_lines_down();
  4350. CHECK(code_edit->get_text() == "to\ntest\nlines\n\nmove\naround");
  4351. CHECK(code_edit->get_caret_count() == 3);
  4352. CHECK(code_edit->has_selection(0));
  4353. CHECK(code_edit->get_selection_origin_line(0) == 2);
  4354. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4355. CHECK(code_edit->get_caret_line(0) == 2);
  4356. CHECK(code_edit->get_caret_column(0) == 4);
  4357. CHECK_FALSE(code_edit->has_selection(1));
  4358. CHECK(code_edit->get_caret_line(1) == 1);
  4359. CHECK(code_edit->get_caret_column(1) == 0);
  4360. CHECK_FALSE(code_edit->has_selection(2));
  4361. CHECK(code_edit->get_caret_line(2) == 1);
  4362. CHECK(code_edit->get_caret_column(2) == 1);
  4363. // Move multiple separate lines with multiple selections.
  4364. code_edit->remove_secondary_carets();
  4365. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4366. code_edit->select(0, 2, 1, 4);
  4367. code_edit->add_caret(4, 0);
  4368. code_edit->select(4, 0, 4, 2, 1);
  4369. code_edit->move_lines_down();
  4370. CHECK(code_edit->get_text() == "to\ntest\nlines\n\naround\nmove");
  4371. CHECK(code_edit->get_caret_count() == 2);
  4372. CHECK(code_edit->has_selection(0));
  4373. CHECK(code_edit->get_selection_origin_line(0) == 1);
  4374. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4375. CHECK(code_edit->get_caret_line(0) == 2);
  4376. CHECK(code_edit->get_caret_column(0) == 4);
  4377. CHECK(code_edit->has_selection(1));
  4378. CHECK(code_edit->get_selection_origin_line(1) == 5);
  4379. CHECK(code_edit->get_selection_origin_column(1) == 0);
  4380. CHECK(code_edit->get_caret_line(1) == 5);
  4381. CHECK(code_edit->get_caret_column(1) == 2);
  4382. // Move lines with adjacent selections that end at column 0.
  4383. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4384. code_edit->select(1, 2, 2, 0);
  4385. code_edit->add_caret(2, 2);
  4386. code_edit->select(2, 2, 3, 0, 1);
  4387. code_edit->move_lines_down();
  4388. CHECK(code_edit->get_text() == "test\n\nlines\nto\nmove\naround");
  4389. CHECK(code_edit->get_caret_count() == 2);
  4390. CHECK(code_edit->has_selection(0));
  4391. CHECK(code_edit->get_selection_origin_line(0) == 2);
  4392. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4393. CHECK(code_edit->get_caret_line(0) == 3);
  4394. CHECK(code_edit->get_caret_column(0) == 0);
  4395. CHECK(code_edit->has_selection(1));
  4396. CHECK(code_edit->get_selection_origin_line(1) == 3);
  4397. CHECK(code_edit->get_selection_origin_column(1) == 2);
  4398. CHECK(code_edit->get_caret_line(1) == 4);
  4399. CHECK(code_edit->get_caret_column(1) == 0);
  4400. code_edit->remove_secondary_carets();
  4401. // Move lines with disconnected adjacent selections that end at column 0.
  4402. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4403. code_edit->select(0, 2, 1, 0);
  4404. code_edit->add_caret(2, 2);
  4405. code_edit->select(2, 0, 3, 0, 1);
  4406. code_edit->move_lines_down();
  4407. CHECK(code_edit->get_text() == "lines\ntest\n\nto\nmove\naround");
  4408. CHECK(code_edit->get_caret_count() == 2);
  4409. CHECK(code_edit->has_selection(0));
  4410. CHECK(code_edit->get_selection_origin_line(0) == 1);
  4411. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4412. CHECK(code_edit->get_caret_line(0) == 2);
  4413. CHECK(code_edit->get_caret_column(0) == 0);
  4414. CHECK(code_edit->has_selection(1));
  4415. CHECK(code_edit->get_selection_origin_line(1) == 3);
  4416. CHECK(code_edit->get_selection_origin_column(1) == 0);
  4417. CHECK(code_edit->get_caret_line(1) == 4);
  4418. CHECK(code_edit->get_caret_column(1) == 0);
  4419. code_edit->remove_secondary_carets();
  4420. code_edit->deselect();
  4421. code_edit->set_line_folding_enabled(true);
  4422. // Move line down into a folded region unfolds it.
  4423. code_edit->set_text("test\ntest2\n\tline1 test\n\t\tline 2\ntest2");
  4424. code_edit->set_caret_line(0);
  4425. code_edit->set_caret_column(0);
  4426. code_edit->fold_line(1);
  4427. CHECK(code_edit->is_line_folded(1));
  4428. code_edit->move_lines_down();
  4429. CHECK(code_edit->get_caret_count() == 1);
  4430. CHECK_FALSE(code_edit->has_selection(0));
  4431. CHECK(code_edit->get_caret_line() == 1);
  4432. CHECK(code_edit->get_caret_column() == 0);
  4433. CHECK(code_edit->get_text() == "test2\ntest\n\tline1 test\n\t\tline 2\ntest2");
  4434. CHECK_FALSE(code_edit->is_line_folded(0));
  4435. CHECK_FALSE(code_edit->is_line_folded(1));
  4436. }
  4437. SUBCASE("[SceneTree][CodeEdit] delete lines") {
  4438. code_edit->set_text("test\nlines\nto\n\ndelete");
  4439. // Delete line with caret on it.
  4440. code_edit->set_caret_line(1);
  4441. code_edit->set_caret_column(1);
  4442. code_edit->delete_lines();
  4443. CHECK(code_edit->get_text() == "test\nto\n\ndelete");
  4444. CHECK(code_edit->get_caret_line() == 1);
  4445. CHECK(code_edit->get_caret_column() == 1);
  4446. // Undo.
  4447. code_edit->undo();
  4448. CHECK(code_edit->get_text() == "test\nlines\nto\n\ndelete");
  4449. CHECK(code_edit->get_caret_line() == 1);
  4450. CHECK(code_edit->get_caret_column() == 1);
  4451. // Redo.
  4452. code_edit->redo();
  4453. CHECK(code_edit->get_text() == "test\nto\n\ndelete");
  4454. CHECK(code_edit->get_caret_line() == 1);
  4455. CHECK(code_edit->get_caret_column() == 1);
  4456. // Delete empty line.
  4457. code_edit->set_caret_line(2);
  4458. code_edit->set_caret_column(0);
  4459. code_edit->delete_lines();
  4460. CHECK(code_edit->get_text() == "test\nto\ndelete");
  4461. CHECK(code_edit->get_caret_line() == 2);
  4462. CHECK(code_edit->get_caret_column() == 0);
  4463. // Deletes only one line when there are multiple carets on it. Carets move down and the column gets clamped.
  4464. code_edit->set_caret_line(0);
  4465. code_edit->set_caret_column(0);
  4466. code_edit->add_caret(0, 1);
  4467. code_edit->add_caret(0, 4);
  4468. code_edit->delete_lines();
  4469. CHECK(code_edit->get_text() == "to\ndelete");
  4470. CHECK(code_edit->get_caret_count() == 3);
  4471. CHECK(code_edit->get_caret_line(0) == 0);
  4472. CHECK(code_edit->get_caret_column(0) == 0);
  4473. CHECK(code_edit->get_caret_line(1) == 0);
  4474. CHECK(code_edit->get_caret_column(1) == 1);
  4475. CHECK(code_edit->get_caret_line(2) == 0);
  4476. CHECK(code_edit->get_caret_column(2) == 2);
  4477. // Delete multiple lines with selection.
  4478. code_edit->remove_secondary_carets();
  4479. code_edit->set_text("test\nlines\nto\n\ndelete");
  4480. code_edit->select(0, 1, 2, 1);
  4481. code_edit->delete_lines();
  4482. CHECK(code_edit->get_text() == "\ndelete");
  4483. CHECK_FALSE(code_edit->has_selection());
  4484. CHECK(code_edit->get_caret_line() == 0);
  4485. CHECK(code_edit->get_caret_column() == 0);
  4486. // Does not affect line with selection end at column 0.
  4487. code_edit->set_text("test\nlines\nto\n\ndelete");
  4488. code_edit->select(0, 1, 1, 0);
  4489. code_edit->delete_lines();
  4490. CHECK(code_edit->get_text() == "lines\nto\n\ndelete");
  4491. CHECK_FALSE(code_edit->has_selection());
  4492. CHECK(code_edit->get_caret_line() == 0);
  4493. CHECK(code_edit->get_caret_column() == 0);
  4494. // Delete multiple lines with multiple carets.
  4495. code_edit->set_text("test\nlines\nto\n\ndelete");
  4496. code_edit->set_caret_line(0);
  4497. code_edit->set_caret_column(2);
  4498. code_edit->add_caret(1, 0);
  4499. code_edit->add_caret(4, 5);
  4500. code_edit->delete_lines();
  4501. CHECK(code_edit->get_text() == "to\n");
  4502. CHECK(code_edit->get_caret_count() == 2);
  4503. CHECK(code_edit->get_caret_line(0) == 0);
  4504. CHECK(code_edit->get_caret_column(0) == 0);
  4505. CHECK(code_edit->get_caret_line(1) == 1);
  4506. CHECK(code_edit->get_caret_column(1) == 0);
  4507. // Delete multiple separate lines with multiple selections.
  4508. code_edit->remove_secondary_carets();
  4509. code_edit->set_text("test\nlines\nto\n\ndelete");
  4510. code_edit->add_caret(4, 5);
  4511. code_edit->select(0, 1, 1, 1);
  4512. code_edit->select(5, 5, 4, 0, 1);
  4513. code_edit->delete_lines();
  4514. CHECK(code_edit->get_text() == "to\n");
  4515. CHECK_FALSE(code_edit->has_selection());
  4516. CHECK(code_edit->get_caret_count() == 2);
  4517. CHECK(code_edit->get_caret_line(0) == 0);
  4518. CHECK(code_edit->get_caret_column(0) == 1);
  4519. CHECK(code_edit->get_caret_line(1) == 1);
  4520. CHECK(code_edit->get_caret_column(1) == 0);
  4521. // Deletes contents when there is only one line.
  4522. code_edit->remove_secondary_carets();
  4523. code_edit->set_text("test");
  4524. code_edit->set_caret_line(0);
  4525. code_edit->set_caret_column(4);
  4526. code_edit->delete_lines();
  4527. CHECK(code_edit->get_text() == "");
  4528. CHECK_FALSE(code_edit->has_selection());
  4529. CHECK(code_edit->get_caret_line() == 0);
  4530. CHECK(code_edit->get_caret_column() == 0);
  4531. }
  4532. SUBCASE("[SceneTree][CodeEdit] duplicate selection") {
  4533. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4534. // Duplicate selected text.
  4535. code_edit->select(0, 1, 1, 2);
  4536. code_edit->duplicate_selection();
  4537. CHECK(code_edit->get_text() == "test\nliest\nlines\nto\n\nduplicate");
  4538. CHECK(code_edit->has_selection());
  4539. CHECK(code_edit->get_selection_origin_line() == 1);
  4540. CHECK(code_edit->get_selection_origin_column() == 2);
  4541. CHECK(code_edit->get_caret_line() == 2);
  4542. CHECK(code_edit->get_caret_column() == 2);
  4543. // Undo.
  4544. code_edit->undo();
  4545. CHECK(code_edit->get_text() == "test\nlines\nto\n\nduplicate");
  4546. CHECK(code_edit->has_selection());
  4547. CHECK(code_edit->get_selection_origin_line() == 0);
  4548. CHECK(code_edit->get_selection_origin_column() == 1);
  4549. CHECK(code_edit->get_caret_line() == 1);
  4550. CHECK(code_edit->get_caret_column() == 2);
  4551. // Redo.
  4552. code_edit->redo();
  4553. CHECK(code_edit->get_text() == "test\nliest\nlines\nto\n\nduplicate");
  4554. CHECK(code_edit->has_selection());
  4555. CHECK(code_edit->get_selection_origin_line() == 1);
  4556. CHECK(code_edit->get_selection_origin_column() == 2);
  4557. CHECK(code_edit->get_caret_line() == 2);
  4558. CHECK(code_edit->get_caret_column() == 2);
  4559. // Duplicate selected text, right to left selection.
  4560. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4561. code_edit->select(1, 1, 0, 2);
  4562. code_edit->duplicate_selection();
  4563. CHECK(code_edit->get_text() == "test\nlst\nlines\nto\n\nduplicate");
  4564. CHECK(code_edit->has_selection());
  4565. CHECK(code_edit->get_selection_origin_line() == 2);
  4566. CHECK(code_edit->get_selection_origin_column() == 1);
  4567. CHECK(code_edit->get_caret_line() == 1);
  4568. CHECK(code_edit->get_caret_column() == 1);
  4569. // Duplicate line if there is no selection.
  4570. code_edit->deselect();
  4571. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4572. code_edit->set_caret_line(1);
  4573. code_edit->set_caret_column(2);
  4574. code_edit->duplicate_selection();
  4575. CHECK(code_edit->get_text() == "test\nlines\nlines\nto\n\nduplicate");
  4576. CHECK_FALSE(code_edit->has_selection());
  4577. CHECK(code_edit->get_caret_line() == 2);
  4578. CHECK(code_edit->get_caret_column() == 2);
  4579. // Duplicate multiple lines.
  4580. code_edit->deselect();
  4581. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4582. code_edit->set_caret_line(1);
  4583. code_edit->set_caret_column(2);
  4584. code_edit->add_caret(5, 0);
  4585. code_edit->add_caret(0, 4);
  4586. code_edit->duplicate_selection();
  4587. CHECK(code_edit->get_text() == "test\ntest\nlines\nlines\nto\n\nduplicate\nduplicate");
  4588. CHECK(code_edit->get_caret_count() == 3);
  4589. CHECK_FALSE(code_edit->has_selection());
  4590. CHECK(code_edit->get_caret_line(0) == 3);
  4591. CHECK(code_edit->get_caret_column(0) == 2);
  4592. CHECK(code_edit->get_caret_line(1) == 7);
  4593. CHECK(code_edit->get_caret_column(1) == 0);
  4594. CHECK(code_edit->get_caret_line(2) == 1);
  4595. CHECK(code_edit->get_caret_column(2) == 4);
  4596. // Duplicate multiple separate selections.
  4597. code_edit->remove_secondary_carets();
  4598. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4599. code_edit->add_caret(4, 4);
  4600. code_edit->add_caret(0, 1);
  4601. code_edit->add_caret(0, 4);
  4602. code_edit->select(2, 0, 2, 1, 0);
  4603. code_edit->select(3, 0, 4, 4, 1);
  4604. code_edit->select(0, 1, 0, 0, 2);
  4605. code_edit->select(0, 2, 0, 4, 3);
  4606. code_edit->duplicate_selection();
  4607. CHECK(code_edit->get_text() == "ttestst\nlines\ntto\n\ndupl\nduplicate");
  4608. CHECK(code_edit->get_caret_count() == 4);
  4609. CHECK(code_edit->has_selection(0));
  4610. CHECK(code_edit->get_selection_origin_line(0) == 2);
  4611. CHECK(code_edit->get_selection_origin_column(0) == 1);
  4612. CHECK(code_edit->get_caret_line(0) == 2);
  4613. CHECK(code_edit->get_caret_column(0) == 2);
  4614. CHECK(code_edit->has_selection(1));
  4615. CHECK(code_edit->get_selection_origin_line(1) == 4);
  4616. CHECK(code_edit->get_selection_origin_column(1) == 4);
  4617. CHECK(code_edit->get_caret_line(1) == 5);
  4618. CHECK(code_edit->get_caret_column(1) == 4);
  4619. CHECK(code_edit->has_selection(2));
  4620. CHECK(code_edit->get_selection_origin_line(2) == 0);
  4621. CHECK(code_edit->get_selection_origin_column(2) == 2);
  4622. CHECK(code_edit->get_caret_line(2) == 0);
  4623. CHECK(code_edit->get_caret_column(2) == 1);
  4624. CHECK(code_edit->has_selection(3));
  4625. CHECK(code_edit->get_selection_origin_line(3) == 0);
  4626. CHECK(code_edit->get_selection_origin_column(3) == 5);
  4627. CHECK(code_edit->get_caret_line(3) == 0);
  4628. CHECK(code_edit->get_caret_column(3) == 7);
  4629. // Duplicate adjacent selections.
  4630. code_edit->remove_secondary_carets();
  4631. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4632. code_edit->add_caret(1, 2);
  4633. code_edit->select(1, 0, 1, 1, 0);
  4634. code_edit->select(1, 1, 1, 4, 1);
  4635. code_edit->duplicate_selection();
  4636. CHECK(code_edit->get_text() == "test\nllineines\nto\n\nduplicate");
  4637. CHECK(code_edit->get_caret_count() == 2);
  4638. CHECK(code_edit->has_selection(0));
  4639. CHECK(code_edit->get_selection_origin_line(0) == 1);
  4640. CHECK(code_edit->get_selection_origin_column(0) == 1);
  4641. CHECK(code_edit->get_caret_line(0) == 1);
  4642. CHECK(code_edit->get_caret_column(0) == 2);
  4643. CHECK(code_edit->has_selection(1));
  4644. CHECK(code_edit->get_selection_origin_line(1) == 1);
  4645. CHECK(code_edit->get_selection_origin_column(1) == 5);
  4646. CHECK(code_edit->get_caret_line(1) == 1);
  4647. CHECK(code_edit->get_caret_column(1) == 8);
  4648. // Duplicate lines then duplicate selections when there are both selections and non-selections.
  4649. code_edit->remove_secondary_carets();
  4650. code_edit->set_text("test duplicate");
  4651. code_edit->select(0, 14, 0, 13, 0);
  4652. code_edit->add_caret(0, 8);
  4653. code_edit->add_caret(0, 4);
  4654. code_edit->select(0, 2, 0, 4, 2);
  4655. code_edit->duplicate_selection();
  4656. CHECK(code_edit->get_text() == "test duplicate\ntestst duplicatee");
  4657. CHECK(code_edit->get_caret_count() == 3);
  4658. CHECK(code_edit->has_selection(0));
  4659. CHECK(code_edit->get_selection_origin_line(0) == 1);
  4660. CHECK(code_edit->get_selection_origin_column(0) == 17);
  4661. CHECK(code_edit->get_caret_line(0) == 1);
  4662. CHECK(code_edit->get_caret_column(0) == 16);
  4663. CHECK_FALSE(code_edit->has_selection(1));
  4664. CHECK(code_edit->get_caret_line(1) == 1);
  4665. CHECK(code_edit->get_caret_column(1) == 10);
  4666. CHECK(code_edit->has_selection(2));
  4667. CHECK(code_edit->get_selection_origin_line(2) == 1);
  4668. CHECK(code_edit->get_selection_origin_column(2) == 4);
  4669. CHECK(code_edit->get_caret_line(2) == 1);
  4670. CHECK(code_edit->get_caret_column(2) == 6);
  4671. }
  4672. SUBCASE("[SceneTree][CodeEdit] duplicate lines") {
  4673. String reset_text = R"(extends Node
  4674. func _ready():
  4675. var a := len(OS.get_cmdline_args())
  4676. var b := get_child_count()
  4677. var c := a + b
  4678. for i in range(c):
  4679. print("This is the solution: ", sin(i))
  4680. var pos = get_index() - 1
  4681. print("Make sure this exits: %b" % pos)
  4682. )";
  4683. code_edit->set_text(reset_text);
  4684. // Duplicate a single line without selection.
  4685. code_edit->set_caret_line(0);
  4686. code_edit->duplicate_lines();
  4687. CHECK(code_edit->get_line(0) == "extends Node");
  4688. CHECK(code_edit->get_line(1) == "extends Node");
  4689. CHECK(code_edit->get_line(2) == "");
  4690. CHECK(code_edit->get_caret_line() == 1);
  4691. CHECK(code_edit->get_caret_column() == 0);
  4692. // Duplicate multiple lines with selection.
  4693. code_edit->set_text(reset_text);
  4694. code_edit->select(4, 8, 6, 15);
  4695. code_edit->duplicate_lines();
  4696. CHECK(code_edit->get_text() == R"(extends Node
  4697. func _ready():
  4698. var a := len(OS.get_cmdline_args())
  4699. var b := get_child_count()
  4700. var c := a + b
  4701. for i in range(c):
  4702. var b := get_child_count()
  4703. var c := a + b
  4704. for i in range(c):
  4705. print("This is the solution: ", sin(i))
  4706. var pos = get_index() - 1
  4707. print("Make sure this exits: %b" % pos)
  4708. )");
  4709. CHECK(code_edit->has_selection());
  4710. CHECK(code_edit->get_selection_origin_line() == 7);
  4711. CHECK(code_edit->get_selection_origin_column() == 8);
  4712. CHECK(code_edit->get_caret_line() == 9);
  4713. CHECK(code_edit->get_caret_column() == 15);
  4714. // Duplicate multiple lines with right to left selection.
  4715. code_edit->set_text(reset_text);
  4716. code_edit->select(6, 15, 4, 8);
  4717. code_edit->duplicate_lines();
  4718. CHECK(code_edit->get_text() == R"(extends Node
  4719. func _ready():
  4720. var a := len(OS.get_cmdline_args())
  4721. var b := get_child_count()
  4722. var c := a + b
  4723. for i in range(c):
  4724. var b := get_child_count()
  4725. var c := a + b
  4726. for i in range(c):
  4727. print("This is the solution: ", sin(i))
  4728. var pos = get_index() - 1
  4729. print("Make sure this exits: %b" % pos)
  4730. )");
  4731. CHECK(code_edit->has_selection());
  4732. CHECK(code_edit->get_selection_origin_line() == 9);
  4733. CHECK(code_edit->get_selection_origin_column() == 15);
  4734. CHECK(code_edit->get_caret_line() == 7);
  4735. CHECK(code_edit->get_caret_column() == 8);
  4736. // Duplicate single lines with multiple carets. Multiple carets on a single line only duplicate once.
  4737. code_edit->remove_secondary_carets();
  4738. code_edit->deselect();
  4739. code_edit->set_text(reset_text);
  4740. code_edit->set_caret_line(3);
  4741. code_edit->set_caret_column(1);
  4742. code_edit->add_caret(5, 1);
  4743. code_edit->add_caret(5, 5);
  4744. code_edit->add_caret(4, 2);
  4745. code_edit->duplicate_lines();
  4746. CHECK(code_edit->get_text() == R"(extends Node
  4747. func _ready():
  4748. var a := len(OS.get_cmdline_args())
  4749. var a := len(OS.get_cmdline_args())
  4750. var b := get_child_count()
  4751. var b := get_child_count()
  4752. var c := a + b
  4753. var c := a + b
  4754. for i in range(c):
  4755. print("This is the solution: ", sin(i))
  4756. var pos = get_index() - 1
  4757. print("Make sure this exits: %b" % pos)
  4758. )");
  4759. CHECK(code_edit->get_caret_count() == 4);
  4760. CHECK_FALSE(code_edit->has_selection(0));
  4761. CHECK(code_edit->get_caret_line(0) == 4);
  4762. CHECK(code_edit->get_caret_column(0) == 1);
  4763. CHECK_FALSE(code_edit->has_selection(1));
  4764. CHECK(code_edit->get_caret_line(1) == 8);
  4765. CHECK(code_edit->get_caret_column(1) == 1);
  4766. CHECK_FALSE(code_edit->has_selection(2));
  4767. CHECK(code_edit->get_caret_line(2) == 8);
  4768. CHECK(code_edit->get_caret_column(2) == 5);
  4769. CHECK_FALSE(code_edit->has_selection(3));
  4770. CHECK(code_edit->get_caret_line(3) == 6);
  4771. CHECK(code_edit->get_caret_column(3) == 2);
  4772. // Duplicate multiple lines with multiple selections.
  4773. code_edit->remove_secondary_carets();
  4774. code_edit->set_text(reset_text);
  4775. code_edit->add_caret(4, 2);
  4776. code_edit->add_caret(6, 0);
  4777. code_edit->add_caret(7, 8);
  4778. code_edit->select(0, 0, 2, 5, 0);
  4779. code_edit->select(3, 0, 4, 2, 1);
  4780. code_edit->select(7, 1, 6, 0, 2);
  4781. code_edit->select(7, 3, 7, 8, 3);
  4782. code_edit->duplicate_lines();
  4783. CHECK(code_edit->get_text() == R"(extends Node
  4784. func _ready():
  4785. extends Node
  4786. func _ready():
  4787. var a := len(OS.get_cmdline_args())
  4788. var b := get_child_count()
  4789. var a := len(OS.get_cmdline_args())
  4790. var b := get_child_count()
  4791. var c := a + b
  4792. for i in range(c):
  4793. print("This is the solution: ", sin(i))
  4794. for i in range(c):
  4795. print("This is the solution: ", sin(i))
  4796. var pos = get_index() - 1
  4797. print("Make sure this exits: %b" % pos)
  4798. )");
  4799. CHECK(code_edit->get_caret_count() == 4);
  4800. CHECK(code_edit->has_selection(0));
  4801. CHECK(code_edit->get_selection_origin_line(0) == 3);
  4802. CHECK(code_edit->get_selection_origin_column(0) == 0);
  4803. CHECK(code_edit->get_caret_line(0) == 5);
  4804. CHECK(code_edit->get_caret_column(0) == 5);
  4805. CHECK(code_edit->has_selection(1));
  4806. CHECK(code_edit->get_selection_origin_line(1) == 8);
  4807. CHECK(code_edit->get_selection_origin_column(1) == 0);
  4808. CHECK(code_edit->get_caret_line(1) == 9);
  4809. CHECK(code_edit->get_caret_column(1) == 2);
  4810. CHECK(code_edit->has_selection(2));
  4811. CHECK(code_edit->get_selection_origin_line(2) == 14);
  4812. CHECK(code_edit->get_selection_origin_column(2) == 1);
  4813. CHECK(code_edit->get_caret_line(2) == 13);
  4814. CHECK(code_edit->get_caret_column(2) == 0);
  4815. CHECK(code_edit->has_selection(3));
  4816. CHECK(code_edit->get_selection_origin_line(3) == 14);
  4817. CHECK(code_edit->get_selection_origin_column(3) == 3);
  4818. CHECK(code_edit->get_caret_line(3) == 14);
  4819. CHECK(code_edit->get_caret_column(3) == 8);
  4820. }
  4821. memdelete(code_edit);
  4822. }
  4823. } // namespace TestCodeEdit
  4824. #endif // TEST_CODE_EDIT_H