test_code_edit.h 216 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680
  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] cut when empty selection clipboard disabled") {
  4032. DisplayServerMock *DS = (DisplayServerMock *)(DisplayServer::get_singleton());
  4033. code_edit->set_empty_selection_clipboard_enabled(false);
  4034. DS->clipboard_set("");
  4035. code_edit->set_text("this is\nsome\n");
  4036. code_edit->set_caret_line(0);
  4037. code_edit->set_caret_column(6);
  4038. MessageQueue::get_singleton()->flush();
  4039. SIGNAL_DISCARD("text_set");
  4040. SIGNAL_DISCARD("text_changed");
  4041. SIGNAL_DISCARD("lines_edited_from");
  4042. SIGNAL_DISCARD("caret_changed");
  4043. code_edit->cut();
  4044. MessageQueue::get_singleton()->flush();
  4045. CHECK(DS->clipboard_get() == "");
  4046. CHECK(code_edit->get_text() == "this is\nsome\n");
  4047. CHECK(code_edit->get_caret_line() == 0);
  4048. CHECK(code_edit->get_caret_column() == 6);
  4049. SIGNAL_CHECK_FALSE("caret_changed");
  4050. SIGNAL_CHECK_FALSE("text_changed");
  4051. SIGNAL_CHECK_FALSE("lines_edited_from");
  4052. }
  4053. SUBCASE("[SceneTree][CodeEdit] new line") {
  4054. // Add a new line.
  4055. code_edit->set_text("test new line");
  4056. code_edit->set_caret_line(0);
  4057. code_edit->set_caret_column(13);
  4058. SEND_GUI_ACTION("ui_text_newline");
  4059. CHECK(code_edit->get_line(0) == "test new line");
  4060. CHECK(code_edit->get_line(1) == "");
  4061. CHECK(code_edit->get_caret_line() == 1);
  4062. CHECK(code_edit->get_caret_column() == 0);
  4063. // Split line with new line.
  4064. code_edit->set_text("test new line");
  4065. code_edit->set_caret_line(0);
  4066. code_edit->set_caret_column(5);
  4067. SEND_GUI_ACTION("ui_text_newline");
  4068. CHECK(code_edit->get_line(0) == "test ");
  4069. CHECK(code_edit->get_line(1) == "new line");
  4070. CHECK(code_edit->get_caret_line() == 1);
  4071. CHECK(code_edit->get_caret_column() == 0);
  4072. // Delete selection and split with new line.
  4073. code_edit->set_text("test new line");
  4074. code_edit->select(0, 0, 0, 5);
  4075. SEND_GUI_ACTION("ui_text_newline");
  4076. CHECK(code_edit->get_line(0) == "");
  4077. CHECK(code_edit->get_line(1) == "new line");
  4078. CHECK(code_edit->get_caret_line() == 1);
  4079. CHECK(code_edit->get_caret_column() == 0);
  4080. // Blank new line below with selection should not split.
  4081. code_edit->set_text("test new line");
  4082. code_edit->select(0, 0, 0, 5);
  4083. SEND_GUI_ACTION("ui_text_newline_blank");
  4084. CHECK(code_edit->get_line(0) == "test new line");
  4085. CHECK(code_edit->get_line(1) == "");
  4086. CHECK(code_edit->get_caret_line() == 1);
  4087. CHECK(code_edit->get_caret_column() == 0);
  4088. // Blank new line above with selection should not split.
  4089. code_edit->set_text("test new line");
  4090. code_edit->select(0, 0, 0, 5);
  4091. SEND_GUI_ACTION("ui_text_newline_above");
  4092. CHECK(code_edit->get_line(0) == "");
  4093. CHECK(code_edit->get_line(1) == "test new line");
  4094. CHECK(code_edit->get_caret_line() == 0);
  4095. CHECK(code_edit->get_caret_column() == 0);
  4096. // Multiple new lines with multiple carets.
  4097. code_edit->set_text("test new line");
  4098. code_edit->set_caret_line(0);
  4099. code_edit->set_caret_column(5);
  4100. code_edit->add_caret(0, 8);
  4101. SEND_GUI_ACTION("ui_text_newline");
  4102. CHECK(code_edit->get_line(0) == "test ");
  4103. CHECK(code_edit->get_line(1) == "new");
  4104. CHECK(code_edit->get_line(2) == " line");
  4105. CHECK(code_edit->get_caret_count() == 2);
  4106. CHECK(code_edit->get_caret_line(0) == 1);
  4107. CHECK(code_edit->get_caret_column(0) == 0);
  4108. CHECK(code_edit->get_caret_line(1) == 2);
  4109. CHECK(code_edit->get_caret_column(1) == 0);
  4110. // Multiple blank new lines with multiple carets.
  4111. code_edit->set_text("test new line");
  4112. code_edit->remove_secondary_carets();
  4113. code_edit->set_caret_line(0);
  4114. code_edit->set_caret_column(5);
  4115. code_edit->add_caret(0, 8);
  4116. SEND_GUI_ACTION("ui_text_newline_blank");
  4117. CHECK(code_edit->get_line(0) == "test new line");
  4118. CHECK(code_edit->get_line(1) == "");
  4119. CHECK(code_edit->get_line(2) == "");
  4120. CHECK(code_edit->get_caret_count() == 2);
  4121. CHECK(code_edit->get_caret_line(0) == 2);
  4122. CHECK(code_edit->get_caret_column(0) == 0);
  4123. CHECK(code_edit->get_caret_line(1) == 1);
  4124. CHECK(code_edit->get_caret_column(1) == 0);
  4125. // Multiple new lines above with multiple carets.
  4126. code_edit->set_text("test new line");
  4127. code_edit->remove_secondary_carets();
  4128. code_edit->set_caret_line(0);
  4129. code_edit->set_caret_column(5);
  4130. code_edit->add_caret(0, 8);
  4131. SEND_GUI_ACTION("ui_text_newline_above");
  4132. CHECK(code_edit->get_line(0) == "");
  4133. CHECK(code_edit->get_line(1) == "");
  4134. CHECK(code_edit->get_line(2) == "test new line");
  4135. CHECK(code_edit->get_caret_count() == 2);
  4136. CHECK(code_edit->get_caret_line(0) == 0);
  4137. CHECK(code_edit->get_caret_column(0) == 0);
  4138. CHECK(code_edit->get_caret_line(1) == 1);
  4139. CHECK(code_edit->get_caret_column(1) == 0);
  4140. // See '[CodeEdit] auto indent' tests for tests about new line with indentation.
  4141. }
  4142. SUBCASE("[SceneTree][CodeEdit] move lines up") {
  4143. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4144. // Move line up with caret on it.
  4145. code_edit->set_caret_line(2);
  4146. code_edit->set_caret_column(1);
  4147. code_edit->move_lines_up();
  4148. CHECK(code_edit->get_text() == "test\nto\nlines\n\nmove\naround");
  4149. CHECK(code_edit->get_caret_line() == 1);
  4150. CHECK(code_edit->get_caret_column() == 1);
  4151. // Undo.
  4152. code_edit->undo();
  4153. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4154. CHECK(code_edit->get_caret_line() == 2);
  4155. CHECK(code_edit->get_caret_column() == 1);
  4156. // Redo.
  4157. code_edit->redo();
  4158. CHECK(code_edit->get_text() == "test\nto\nlines\n\nmove\naround");
  4159. CHECK(code_edit->get_caret_line() == 1);
  4160. CHECK(code_edit->get_caret_column() == 1);
  4161. // Does nothing at the first line.
  4162. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4163. code_edit->set_caret_line(0);
  4164. code_edit->set_caret_column(1);
  4165. code_edit->move_lines_up();
  4166. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4167. CHECK(code_edit->get_caret_line() == 0);
  4168. CHECK(code_edit->get_caret_column() == 1);
  4169. // Does nothing at the first line when selection ends at column 0.
  4170. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4171. code_edit->select(0, 0, 1, 0);
  4172. code_edit->move_lines_up();
  4173. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4174. CHECK(code_edit->has_selection());
  4175. CHECK(code_edit->get_selection_origin_line() == 0);
  4176. CHECK(code_edit->get_selection_origin_column() == 0);
  4177. CHECK(code_edit->get_caret_line() == 1);
  4178. CHECK(code_edit->get_caret_column() == 0);
  4179. // Works on empty line.
  4180. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4181. code_edit->set_caret_line(3);
  4182. code_edit->set_caret_column(0);
  4183. code_edit->move_lines_up();
  4184. CHECK(code_edit->get_text() == "test\nlines\n\nto\nmove\naround");
  4185. CHECK(code_edit->get_caret_line() == 2);
  4186. CHECK(code_edit->get_caret_column() == 0);
  4187. // Move multiple lines up with selection.
  4188. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4189. code_edit->select(4, 0, 5, 1);
  4190. code_edit->move_lines_up();
  4191. CHECK(code_edit->get_text() == "test\nlines\nto\nmove\naround\n");
  4192. CHECK(code_edit->has_selection());
  4193. CHECK(code_edit->get_selection_origin_line() == 3);
  4194. CHECK(code_edit->get_selection_origin_column() == 0);
  4195. CHECK(code_edit->get_caret_line() == 4);
  4196. CHECK(code_edit->get_caret_column() == 1);
  4197. // Does not affect line with selection end at column 0.
  4198. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4199. code_edit->select(4, 0, 5, 0);
  4200. code_edit->move_lines_up();
  4201. CHECK(code_edit->get_text() == "test\nlines\nto\nmove\n\naround");
  4202. CHECK(code_edit->has_selection());
  4203. CHECK(code_edit->get_selection_origin_line() == 3);
  4204. CHECK(code_edit->get_selection_origin_column() == 0);
  4205. CHECK(code_edit->get_caret_line() == 4);
  4206. CHECK(code_edit->get_caret_column() == 0);
  4207. // Move multiple lines up with selection, right to left selection.
  4208. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4209. code_edit->select(5, 2, 4, 1);
  4210. code_edit->move_lines_up();
  4211. CHECK(code_edit->get_text() == "test\nlines\nto\nmove\naround\n");
  4212. CHECK(code_edit->has_selection());
  4213. CHECK(code_edit->get_selection_origin_line() == 4);
  4214. CHECK(code_edit->get_selection_origin_column() == 2);
  4215. CHECK(code_edit->get_caret_line() == 3);
  4216. CHECK(code_edit->get_caret_column() == 1);
  4217. // Move multiple lines with multiple carets. A line with multiple carets is only moved once.
  4218. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4219. code_edit->select(5, 2, 5, 4);
  4220. code_edit->add_caret(4, 0);
  4221. code_edit->add_caret(4, 4);
  4222. code_edit->move_lines_up();
  4223. CHECK(code_edit->get_text() == "test\nlines\nto\nmove\naround\n");
  4224. CHECK(code_edit->get_caret_count() == 3);
  4225. CHECK(code_edit->has_selection(0));
  4226. CHECK(code_edit->get_selection_origin_line(0) == 4);
  4227. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4228. CHECK(code_edit->get_caret_line(0) == 4);
  4229. CHECK(code_edit->get_caret_column(0) == 4);
  4230. CHECK_FALSE(code_edit->has_selection(1));
  4231. CHECK(code_edit->get_caret_line(1) == 3);
  4232. CHECK(code_edit->get_caret_column(1) == 0);
  4233. CHECK_FALSE(code_edit->has_selection(2));
  4234. CHECK(code_edit->get_caret_line(2) == 3);
  4235. CHECK(code_edit->get_caret_column(2) == 4);
  4236. code_edit->remove_secondary_carets();
  4237. // Move multiple separate lines with multiple selections.
  4238. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4239. code_edit->select(2, 2, 1, 4);
  4240. code_edit->add_caret(5, 0);
  4241. code_edit->select(5, 0, 5, 1, 1);
  4242. code_edit->move_lines_up();
  4243. CHECK(code_edit->get_text() == "lines\nto\ntest\n\naround\nmove");
  4244. CHECK(code_edit->get_caret_count() == 2);
  4245. CHECK(code_edit->has_selection(0));
  4246. CHECK(code_edit->get_selection_origin_line(0) == 1);
  4247. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4248. CHECK(code_edit->get_caret_line(0) == 0);
  4249. CHECK(code_edit->get_caret_column(0) == 4);
  4250. CHECK(code_edit->has_selection(1));
  4251. CHECK(code_edit->get_selection_origin_line(1) == 4);
  4252. CHECK(code_edit->get_selection_origin_column(1) == 0);
  4253. CHECK(code_edit->get_caret_line(1) == 4);
  4254. CHECK(code_edit->get_caret_column(1) == 1);
  4255. code_edit->remove_secondary_carets();
  4256. // Move lines with adjacent selections that end at column 0.
  4257. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4258. code_edit->select(1, 2, 2, 0);
  4259. code_edit->add_caret(2, 2);
  4260. code_edit->select(2, 2, 3, 0, 1);
  4261. code_edit->move_lines_up();
  4262. CHECK(code_edit->get_text() == "lines\nto\ntest\n\nmove\naround");
  4263. CHECK(code_edit->get_caret_count() == 2);
  4264. CHECK(code_edit->has_selection(0));
  4265. CHECK(code_edit->get_selection_origin_line(0) == 0);
  4266. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4267. CHECK(code_edit->get_caret_line(0) == 1);
  4268. CHECK(code_edit->get_caret_column(0) == 0);
  4269. CHECK(code_edit->has_selection(1));
  4270. CHECK(code_edit->get_selection_origin_line(1) == 1);
  4271. CHECK(code_edit->get_selection_origin_column(1) == 2);
  4272. CHECK(code_edit->get_caret_line(1) == 2);
  4273. CHECK(code_edit->get_caret_column(1) == 0);
  4274. code_edit->remove_secondary_carets();
  4275. code_edit->deselect();
  4276. code_edit->set_line_folding_enabled(true);
  4277. // Move line up into a folded region unfolds it.
  4278. code_edit->set_text("test\n\tline1 test\n\t\tline 2\ntest2");
  4279. code_edit->set_caret_line(3);
  4280. code_edit->set_caret_column(0);
  4281. code_edit->fold_line(0);
  4282. CHECK(code_edit->is_line_folded(0));
  4283. code_edit->move_lines_up();
  4284. CHECK(code_edit->get_caret_count() == 1);
  4285. CHECK_FALSE(code_edit->has_selection(0));
  4286. CHECK(code_edit->get_caret_line() == 2);
  4287. CHECK(code_edit->get_caret_column() == 0);
  4288. CHECK(code_edit->get_text() == "test\n\tline1 test\ntest2\n\t\tline 2");
  4289. CHECK_FALSE(code_edit->is_line_folded(0));
  4290. }
  4291. SUBCASE("[SceneTree][CodeEdit] move lines down") {
  4292. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4293. // Move line down with caret on it.
  4294. code_edit->set_caret_line(1);
  4295. code_edit->set_caret_column(1);
  4296. code_edit->move_lines_down();
  4297. CHECK(code_edit->get_text() == "test\nto\nlines\n\nmove\naround");
  4298. CHECK(code_edit->get_caret_line() == 2);
  4299. CHECK(code_edit->get_caret_column() == 1);
  4300. // Undo.
  4301. code_edit->undo();
  4302. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4303. CHECK(code_edit->get_caret_line() == 1);
  4304. CHECK(code_edit->get_caret_column() == 1);
  4305. // Redo.
  4306. code_edit->redo();
  4307. CHECK(code_edit->get_text() == "test\nto\nlines\n\nmove\naround");
  4308. CHECK(code_edit->get_caret_line() == 2);
  4309. CHECK(code_edit->get_caret_column() == 1);
  4310. // Does nothing at the last line.
  4311. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4312. code_edit->set_caret_line(5);
  4313. code_edit->set_caret_column(1);
  4314. code_edit->move_lines_down();
  4315. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4316. CHECK(code_edit->get_caret_line() == 5);
  4317. CHECK(code_edit->get_caret_column() == 1);
  4318. // Does nothing at the last line when selection ends at column 0.
  4319. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4320. code_edit->select(4, 0, 5, 0);
  4321. code_edit->move_lines_down();
  4322. CHECK(code_edit->get_text() == "test\nlines\nto\n\nmove\naround");
  4323. CHECK(code_edit->has_selection());
  4324. CHECK(code_edit->get_selection_origin_line() == 4);
  4325. CHECK(code_edit->get_selection_origin_column() == 0);
  4326. CHECK(code_edit->get_caret_line() == 5);
  4327. CHECK(code_edit->get_caret_column() == 0);
  4328. // Works on empty line.
  4329. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4330. code_edit->set_caret_line(3);
  4331. code_edit->set_caret_column(0);
  4332. code_edit->move_lines_down();
  4333. CHECK(code_edit->get_text() == "test\nlines\nto\nmove\n\naround");
  4334. CHECK(code_edit->get_caret_line() == 4);
  4335. CHECK(code_edit->get_caret_column() == 0);
  4336. // Move multiple lines down with selection.
  4337. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4338. code_edit->select(1, 0, 2, 1);
  4339. code_edit->move_lines_down();
  4340. CHECK(code_edit->get_text() == "test\n\nlines\nto\nmove\naround");
  4341. CHECK(code_edit->has_selection());
  4342. CHECK(code_edit->get_selection_origin_line() == 2);
  4343. CHECK(code_edit->get_selection_origin_column() == 0);
  4344. CHECK(code_edit->get_caret_line() == 3);
  4345. CHECK(code_edit->get_caret_column() == 1);
  4346. // Does not affect line with selection end at column 0.
  4347. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4348. code_edit->select(1, 0, 2, 0);
  4349. code_edit->move_lines_down();
  4350. CHECK(code_edit->get_text() == "test\nto\nlines\n\nmove\naround");
  4351. CHECK(code_edit->has_selection());
  4352. CHECK(code_edit->get_selection_origin_line() == 2);
  4353. CHECK(code_edit->get_selection_origin_column() == 0);
  4354. CHECK(code_edit->get_caret_line() == 3);
  4355. CHECK(code_edit->get_caret_column() == 0);
  4356. // Move multiple lines down with selection, right to left selection.
  4357. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4358. code_edit->select(2, 2, 1, 1);
  4359. code_edit->move_lines_down();
  4360. CHECK(code_edit->get_text() == "test\n\nlines\nto\nmove\naround");
  4361. CHECK(code_edit->has_selection());
  4362. CHECK(code_edit->get_selection_origin_line() == 3);
  4363. CHECK(code_edit->get_selection_origin_column() == 2);
  4364. CHECK(code_edit->get_caret_line() == 2);
  4365. CHECK(code_edit->get_caret_column() == 1);
  4366. // Move multiple lines with multiple carets. A line with multiple carets is only moved once.
  4367. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4368. code_edit->select(1, 2, 1, 4);
  4369. code_edit->add_caret(0, 0);
  4370. code_edit->add_caret(0, 1);
  4371. code_edit->move_lines_down();
  4372. CHECK(code_edit->get_text() == "to\ntest\nlines\n\nmove\naround");
  4373. CHECK(code_edit->get_caret_count() == 3);
  4374. CHECK(code_edit->has_selection(0));
  4375. CHECK(code_edit->get_selection_origin_line(0) == 2);
  4376. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4377. CHECK(code_edit->get_caret_line(0) == 2);
  4378. CHECK(code_edit->get_caret_column(0) == 4);
  4379. CHECK_FALSE(code_edit->has_selection(1));
  4380. CHECK(code_edit->get_caret_line(1) == 1);
  4381. CHECK(code_edit->get_caret_column(1) == 0);
  4382. CHECK_FALSE(code_edit->has_selection(2));
  4383. CHECK(code_edit->get_caret_line(2) == 1);
  4384. CHECK(code_edit->get_caret_column(2) == 1);
  4385. // Move multiple separate lines with multiple selections.
  4386. code_edit->remove_secondary_carets();
  4387. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4388. code_edit->select(0, 2, 1, 4);
  4389. code_edit->add_caret(4, 0);
  4390. code_edit->select(4, 0, 4, 2, 1);
  4391. code_edit->move_lines_down();
  4392. CHECK(code_edit->get_text() == "to\ntest\nlines\n\naround\nmove");
  4393. CHECK(code_edit->get_caret_count() == 2);
  4394. CHECK(code_edit->has_selection(0));
  4395. CHECK(code_edit->get_selection_origin_line(0) == 1);
  4396. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4397. CHECK(code_edit->get_caret_line(0) == 2);
  4398. CHECK(code_edit->get_caret_column(0) == 4);
  4399. CHECK(code_edit->has_selection(1));
  4400. CHECK(code_edit->get_selection_origin_line(1) == 5);
  4401. CHECK(code_edit->get_selection_origin_column(1) == 0);
  4402. CHECK(code_edit->get_caret_line(1) == 5);
  4403. CHECK(code_edit->get_caret_column(1) == 2);
  4404. // Move lines with adjacent selections that end at column 0.
  4405. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4406. code_edit->select(1, 2, 2, 0);
  4407. code_edit->add_caret(2, 2);
  4408. code_edit->select(2, 2, 3, 0, 1);
  4409. code_edit->move_lines_down();
  4410. CHECK(code_edit->get_text() == "test\n\nlines\nto\nmove\naround");
  4411. CHECK(code_edit->get_caret_count() == 2);
  4412. CHECK(code_edit->has_selection(0));
  4413. CHECK(code_edit->get_selection_origin_line(0) == 2);
  4414. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4415. CHECK(code_edit->get_caret_line(0) == 3);
  4416. CHECK(code_edit->get_caret_column(0) == 0);
  4417. CHECK(code_edit->has_selection(1));
  4418. CHECK(code_edit->get_selection_origin_line(1) == 3);
  4419. CHECK(code_edit->get_selection_origin_column(1) == 2);
  4420. CHECK(code_edit->get_caret_line(1) == 4);
  4421. CHECK(code_edit->get_caret_column(1) == 0);
  4422. code_edit->remove_secondary_carets();
  4423. // Move lines with disconnected adjacent selections that end at column 0.
  4424. code_edit->set_text("test\nlines\nto\n\nmove\naround");
  4425. code_edit->select(0, 2, 1, 0);
  4426. code_edit->add_caret(2, 2);
  4427. code_edit->select(2, 0, 3, 0, 1);
  4428. code_edit->move_lines_down();
  4429. CHECK(code_edit->get_text() == "lines\ntest\n\nto\nmove\naround");
  4430. CHECK(code_edit->get_caret_count() == 2);
  4431. CHECK(code_edit->has_selection(0));
  4432. CHECK(code_edit->get_selection_origin_line(0) == 1);
  4433. CHECK(code_edit->get_selection_origin_column(0) == 2);
  4434. CHECK(code_edit->get_caret_line(0) == 2);
  4435. CHECK(code_edit->get_caret_column(0) == 0);
  4436. CHECK(code_edit->has_selection(1));
  4437. CHECK(code_edit->get_selection_origin_line(1) == 3);
  4438. CHECK(code_edit->get_selection_origin_column(1) == 0);
  4439. CHECK(code_edit->get_caret_line(1) == 4);
  4440. CHECK(code_edit->get_caret_column(1) == 0);
  4441. code_edit->remove_secondary_carets();
  4442. code_edit->deselect();
  4443. code_edit->set_line_folding_enabled(true);
  4444. // Move line down into a folded region unfolds it.
  4445. code_edit->set_text("test\ntest2\n\tline1 test\n\t\tline 2\ntest2");
  4446. code_edit->set_caret_line(0);
  4447. code_edit->set_caret_column(0);
  4448. code_edit->fold_line(1);
  4449. CHECK(code_edit->is_line_folded(1));
  4450. code_edit->move_lines_down();
  4451. CHECK(code_edit->get_caret_count() == 1);
  4452. CHECK_FALSE(code_edit->has_selection(0));
  4453. CHECK(code_edit->get_caret_line() == 1);
  4454. CHECK(code_edit->get_caret_column() == 0);
  4455. CHECK(code_edit->get_text() == "test2\ntest\n\tline1 test\n\t\tline 2\ntest2");
  4456. CHECK_FALSE(code_edit->is_line_folded(0));
  4457. CHECK_FALSE(code_edit->is_line_folded(1));
  4458. }
  4459. SUBCASE("[SceneTree][CodeEdit] delete lines") {
  4460. code_edit->set_text("test\nlines\nto\n\ndelete");
  4461. // Delete line with caret on it.
  4462. code_edit->set_caret_line(1);
  4463. code_edit->set_caret_column(1);
  4464. code_edit->delete_lines();
  4465. CHECK(code_edit->get_text() == "test\nto\n\ndelete");
  4466. CHECK(code_edit->get_caret_line() == 1);
  4467. CHECK(code_edit->get_caret_column() == 1);
  4468. // Undo.
  4469. code_edit->undo();
  4470. CHECK(code_edit->get_text() == "test\nlines\nto\n\ndelete");
  4471. CHECK(code_edit->get_caret_line() == 1);
  4472. CHECK(code_edit->get_caret_column() == 1);
  4473. // Redo.
  4474. code_edit->redo();
  4475. CHECK(code_edit->get_text() == "test\nto\n\ndelete");
  4476. CHECK(code_edit->get_caret_line() == 1);
  4477. CHECK(code_edit->get_caret_column() == 1);
  4478. // Delete empty line.
  4479. code_edit->set_caret_line(2);
  4480. code_edit->set_caret_column(0);
  4481. code_edit->delete_lines();
  4482. CHECK(code_edit->get_text() == "test\nto\ndelete");
  4483. CHECK(code_edit->get_caret_line() == 2);
  4484. CHECK(code_edit->get_caret_column() == 0);
  4485. // Deletes only one line when there are multiple carets on it. Carets move down and the column gets clamped.
  4486. code_edit->set_caret_line(0);
  4487. code_edit->set_caret_column(0);
  4488. code_edit->add_caret(0, 1);
  4489. code_edit->add_caret(0, 4);
  4490. code_edit->delete_lines();
  4491. CHECK(code_edit->get_text() == "to\ndelete");
  4492. CHECK(code_edit->get_caret_count() == 3);
  4493. CHECK(code_edit->get_caret_line(0) == 0);
  4494. CHECK(code_edit->get_caret_column(0) == 0);
  4495. CHECK(code_edit->get_caret_line(1) == 0);
  4496. CHECK(code_edit->get_caret_column(1) == 1);
  4497. CHECK(code_edit->get_caret_line(2) == 0);
  4498. CHECK(code_edit->get_caret_column(2) == 2);
  4499. // Delete multiple lines with selection.
  4500. code_edit->remove_secondary_carets();
  4501. code_edit->set_text("test\nlines\nto\n\ndelete");
  4502. code_edit->select(0, 1, 2, 1);
  4503. code_edit->delete_lines();
  4504. CHECK(code_edit->get_text() == "\ndelete");
  4505. CHECK_FALSE(code_edit->has_selection());
  4506. CHECK(code_edit->get_caret_line() == 0);
  4507. CHECK(code_edit->get_caret_column() == 0);
  4508. // Does not affect line with selection end at column 0.
  4509. code_edit->set_text("test\nlines\nto\n\ndelete");
  4510. code_edit->select(0, 1, 1, 0);
  4511. code_edit->delete_lines();
  4512. CHECK(code_edit->get_text() == "lines\nto\n\ndelete");
  4513. CHECK_FALSE(code_edit->has_selection());
  4514. CHECK(code_edit->get_caret_line() == 0);
  4515. CHECK(code_edit->get_caret_column() == 0);
  4516. // Delete multiple lines with multiple carets.
  4517. code_edit->set_text("test\nlines\nto\n\ndelete");
  4518. code_edit->set_caret_line(0);
  4519. code_edit->set_caret_column(2);
  4520. code_edit->add_caret(1, 0);
  4521. code_edit->add_caret(4, 5);
  4522. code_edit->delete_lines();
  4523. CHECK(code_edit->get_text() == "to\n");
  4524. CHECK(code_edit->get_caret_count() == 2);
  4525. CHECK(code_edit->get_caret_line(0) == 0);
  4526. CHECK(code_edit->get_caret_column(0) == 0);
  4527. CHECK(code_edit->get_caret_line(1) == 1);
  4528. CHECK(code_edit->get_caret_column(1) == 0);
  4529. // Delete multiple separate lines with multiple selections.
  4530. code_edit->remove_secondary_carets();
  4531. code_edit->set_text("test\nlines\nto\n\ndelete");
  4532. code_edit->add_caret(4, 5);
  4533. code_edit->select(0, 1, 1, 1);
  4534. code_edit->select(5, 5, 4, 0, 1);
  4535. code_edit->delete_lines();
  4536. CHECK(code_edit->get_text() == "to\n");
  4537. CHECK_FALSE(code_edit->has_selection());
  4538. CHECK(code_edit->get_caret_count() == 2);
  4539. CHECK(code_edit->get_caret_line(0) == 0);
  4540. CHECK(code_edit->get_caret_column(0) == 1);
  4541. CHECK(code_edit->get_caret_line(1) == 1);
  4542. CHECK(code_edit->get_caret_column(1) == 0);
  4543. // Deletes contents when there is only one line.
  4544. code_edit->remove_secondary_carets();
  4545. code_edit->set_text("test");
  4546. code_edit->set_caret_line(0);
  4547. code_edit->set_caret_column(4);
  4548. code_edit->delete_lines();
  4549. CHECK(code_edit->get_text() == "");
  4550. CHECK_FALSE(code_edit->has_selection());
  4551. CHECK(code_edit->get_caret_line() == 0);
  4552. CHECK(code_edit->get_caret_column() == 0);
  4553. }
  4554. SUBCASE("[SceneTree][CodeEdit] duplicate selection") {
  4555. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4556. // Duplicate selected text.
  4557. code_edit->select(0, 1, 1, 2);
  4558. code_edit->duplicate_selection();
  4559. CHECK(code_edit->get_text() == "test\nliest\nlines\nto\n\nduplicate");
  4560. CHECK(code_edit->has_selection());
  4561. CHECK(code_edit->get_selection_origin_line() == 1);
  4562. CHECK(code_edit->get_selection_origin_column() == 2);
  4563. CHECK(code_edit->get_caret_line() == 2);
  4564. CHECK(code_edit->get_caret_column() == 2);
  4565. // Undo.
  4566. code_edit->undo();
  4567. CHECK(code_edit->get_text() == "test\nlines\nto\n\nduplicate");
  4568. CHECK(code_edit->has_selection());
  4569. CHECK(code_edit->get_selection_origin_line() == 0);
  4570. CHECK(code_edit->get_selection_origin_column() == 1);
  4571. CHECK(code_edit->get_caret_line() == 1);
  4572. CHECK(code_edit->get_caret_column() == 2);
  4573. // Redo.
  4574. code_edit->redo();
  4575. CHECK(code_edit->get_text() == "test\nliest\nlines\nto\n\nduplicate");
  4576. CHECK(code_edit->has_selection());
  4577. CHECK(code_edit->get_selection_origin_line() == 1);
  4578. CHECK(code_edit->get_selection_origin_column() == 2);
  4579. CHECK(code_edit->get_caret_line() == 2);
  4580. CHECK(code_edit->get_caret_column() == 2);
  4581. // Duplicate selected text, right to left selection.
  4582. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4583. code_edit->select(1, 1, 0, 2);
  4584. code_edit->duplicate_selection();
  4585. CHECK(code_edit->get_text() == "test\nlst\nlines\nto\n\nduplicate");
  4586. CHECK(code_edit->has_selection());
  4587. CHECK(code_edit->get_selection_origin_line() == 2);
  4588. CHECK(code_edit->get_selection_origin_column() == 1);
  4589. CHECK(code_edit->get_caret_line() == 1);
  4590. CHECK(code_edit->get_caret_column() == 1);
  4591. // Duplicate line if there is no selection.
  4592. code_edit->deselect();
  4593. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4594. code_edit->set_caret_line(1);
  4595. code_edit->set_caret_column(2);
  4596. code_edit->duplicate_selection();
  4597. CHECK(code_edit->get_text() == "test\nlines\nlines\nto\n\nduplicate");
  4598. CHECK_FALSE(code_edit->has_selection());
  4599. CHECK(code_edit->get_caret_line() == 2);
  4600. CHECK(code_edit->get_caret_column() == 2);
  4601. // Duplicate multiple lines.
  4602. code_edit->deselect();
  4603. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4604. code_edit->set_caret_line(1);
  4605. code_edit->set_caret_column(2);
  4606. code_edit->add_caret(5, 0);
  4607. code_edit->add_caret(0, 4);
  4608. code_edit->duplicate_selection();
  4609. CHECK(code_edit->get_text() == "test\ntest\nlines\nlines\nto\n\nduplicate\nduplicate");
  4610. CHECK(code_edit->get_caret_count() == 3);
  4611. CHECK_FALSE(code_edit->has_selection());
  4612. CHECK(code_edit->get_caret_line(0) == 3);
  4613. CHECK(code_edit->get_caret_column(0) == 2);
  4614. CHECK(code_edit->get_caret_line(1) == 7);
  4615. CHECK(code_edit->get_caret_column(1) == 0);
  4616. CHECK(code_edit->get_caret_line(2) == 1);
  4617. CHECK(code_edit->get_caret_column(2) == 4);
  4618. // Duplicate multiple separate selections.
  4619. code_edit->remove_secondary_carets();
  4620. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4621. code_edit->add_caret(4, 4);
  4622. code_edit->add_caret(0, 1);
  4623. code_edit->add_caret(0, 4);
  4624. code_edit->select(2, 0, 2, 1, 0);
  4625. code_edit->select(3, 0, 4, 4, 1);
  4626. code_edit->select(0, 1, 0, 0, 2);
  4627. code_edit->select(0, 2, 0, 4, 3);
  4628. code_edit->duplicate_selection();
  4629. CHECK(code_edit->get_text() == "ttestst\nlines\ntto\n\ndupl\nduplicate");
  4630. CHECK(code_edit->get_caret_count() == 4);
  4631. CHECK(code_edit->has_selection(0));
  4632. CHECK(code_edit->get_selection_origin_line(0) == 2);
  4633. CHECK(code_edit->get_selection_origin_column(0) == 1);
  4634. CHECK(code_edit->get_caret_line(0) == 2);
  4635. CHECK(code_edit->get_caret_column(0) == 2);
  4636. CHECK(code_edit->has_selection(1));
  4637. CHECK(code_edit->get_selection_origin_line(1) == 4);
  4638. CHECK(code_edit->get_selection_origin_column(1) == 4);
  4639. CHECK(code_edit->get_caret_line(1) == 5);
  4640. CHECK(code_edit->get_caret_column(1) == 4);
  4641. CHECK(code_edit->has_selection(2));
  4642. CHECK(code_edit->get_selection_origin_line(2) == 0);
  4643. CHECK(code_edit->get_selection_origin_column(2) == 2);
  4644. CHECK(code_edit->get_caret_line(2) == 0);
  4645. CHECK(code_edit->get_caret_column(2) == 1);
  4646. CHECK(code_edit->has_selection(3));
  4647. CHECK(code_edit->get_selection_origin_line(3) == 0);
  4648. CHECK(code_edit->get_selection_origin_column(3) == 5);
  4649. CHECK(code_edit->get_caret_line(3) == 0);
  4650. CHECK(code_edit->get_caret_column(3) == 7);
  4651. // Duplicate adjacent selections.
  4652. code_edit->remove_secondary_carets();
  4653. code_edit->set_text("test\nlines\nto\n\nduplicate");
  4654. code_edit->add_caret(1, 2);
  4655. code_edit->select(1, 0, 1, 1, 0);
  4656. code_edit->select(1, 1, 1, 4, 1);
  4657. code_edit->duplicate_selection();
  4658. CHECK(code_edit->get_text() == "test\nllineines\nto\n\nduplicate");
  4659. CHECK(code_edit->get_caret_count() == 2);
  4660. CHECK(code_edit->has_selection(0));
  4661. CHECK(code_edit->get_selection_origin_line(0) == 1);
  4662. CHECK(code_edit->get_selection_origin_column(0) == 1);
  4663. CHECK(code_edit->get_caret_line(0) == 1);
  4664. CHECK(code_edit->get_caret_column(0) == 2);
  4665. CHECK(code_edit->has_selection(1));
  4666. CHECK(code_edit->get_selection_origin_line(1) == 1);
  4667. CHECK(code_edit->get_selection_origin_column(1) == 5);
  4668. CHECK(code_edit->get_caret_line(1) == 1);
  4669. CHECK(code_edit->get_caret_column(1) == 8);
  4670. // Duplicate lines then duplicate selections when there are both selections and non-selections.
  4671. code_edit->remove_secondary_carets();
  4672. code_edit->set_text("test duplicate");
  4673. code_edit->select(0, 14, 0, 13, 0);
  4674. code_edit->add_caret(0, 8);
  4675. code_edit->add_caret(0, 4);
  4676. code_edit->select(0, 2, 0, 4, 2);
  4677. code_edit->duplicate_selection();
  4678. CHECK(code_edit->get_text() == "test duplicate\ntestst duplicatee");
  4679. CHECK(code_edit->get_caret_count() == 3);
  4680. CHECK(code_edit->has_selection(0));
  4681. CHECK(code_edit->get_selection_origin_line(0) == 1);
  4682. CHECK(code_edit->get_selection_origin_column(0) == 17);
  4683. CHECK(code_edit->get_caret_line(0) == 1);
  4684. CHECK(code_edit->get_caret_column(0) == 16);
  4685. CHECK_FALSE(code_edit->has_selection(1));
  4686. CHECK(code_edit->get_caret_line(1) == 1);
  4687. CHECK(code_edit->get_caret_column(1) == 10);
  4688. CHECK(code_edit->has_selection(2));
  4689. CHECK(code_edit->get_selection_origin_line(2) == 1);
  4690. CHECK(code_edit->get_selection_origin_column(2) == 4);
  4691. CHECK(code_edit->get_caret_line(2) == 1);
  4692. CHECK(code_edit->get_caret_column(2) == 6);
  4693. }
  4694. SUBCASE("[SceneTree][CodeEdit] duplicate lines") {
  4695. String reset_text = R"(extends Node
  4696. func _ready():
  4697. var a := len(OS.get_cmdline_args())
  4698. var b := get_child_count()
  4699. var c := a + b
  4700. for i in range(c):
  4701. print("This is the solution: ", sin(i))
  4702. var pos = get_index() - 1
  4703. print("Make sure this exits: %b" % pos)
  4704. )";
  4705. code_edit->set_text(reset_text);
  4706. // Duplicate a single line without selection.
  4707. code_edit->set_caret_line(0);
  4708. code_edit->duplicate_lines();
  4709. CHECK(code_edit->get_line(0) == "extends Node");
  4710. CHECK(code_edit->get_line(1) == "extends Node");
  4711. CHECK(code_edit->get_line(2) == "");
  4712. CHECK(code_edit->get_caret_line() == 1);
  4713. CHECK(code_edit->get_caret_column() == 0);
  4714. // Duplicate multiple lines with selection.
  4715. code_edit->set_text(reset_text);
  4716. code_edit->select(4, 8, 6, 15);
  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() == 7);
  4733. CHECK(code_edit->get_selection_origin_column() == 8);
  4734. CHECK(code_edit->get_caret_line() == 9);
  4735. CHECK(code_edit->get_caret_column() == 15);
  4736. // Duplicate multiple lines with right to left selection.
  4737. code_edit->set_text(reset_text);
  4738. code_edit->select(6, 15, 4, 8);
  4739. code_edit->duplicate_lines();
  4740. CHECK(code_edit->get_text() == R"(extends Node
  4741. func _ready():
  4742. var a := len(OS.get_cmdline_args())
  4743. var b := get_child_count()
  4744. var c := a + b
  4745. for i in range(c):
  4746. var b := get_child_count()
  4747. var c := a + b
  4748. for i in range(c):
  4749. print("This is the solution: ", sin(i))
  4750. var pos = get_index() - 1
  4751. print("Make sure this exits: %b" % pos)
  4752. )");
  4753. CHECK(code_edit->has_selection());
  4754. CHECK(code_edit->get_selection_origin_line() == 9);
  4755. CHECK(code_edit->get_selection_origin_column() == 15);
  4756. CHECK(code_edit->get_caret_line() == 7);
  4757. CHECK(code_edit->get_caret_column() == 8);
  4758. // Duplicate single lines with multiple carets. Multiple carets on a single line only duplicate once.
  4759. code_edit->remove_secondary_carets();
  4760. code_edit->deselect();
  4761. code_edit->set_text(reset_text);
  4762. code_edit->set_caret_line(3);
  4763. code_edit->set_caret_column(1);
  4764. code_edit->add_caret(5, 1);
  4765. code_edit->add_caret(5, 5);
  4766. code_edit->add_caret(4, 2);
  4767. code_edit->duplicate_lines();
  4768. CHECK(code_edit->get_text() == R"(extends Node
  4769. func _ready():
  4770. var a := len(OS.get_cmdline_args())
  4771. var a := len(OS.get_cmdline_args())
  4772. var b := get_child_count()
  4773. var b := get_child_count()
  4774. var c := a + b
  4775. var c := a + b
  4776. for i in range(c):
  4777. print("This is the solution: ", sin(i))
  4778. var pos = get_index() - 1
  4779. print("Make sure this exits: %b" % pos)
  4780. )");
  4781. CHECK(code_edit->get_caret_count() == 4);
  4782. CHECK_FALSE(code_edit->has_selection(0));
  4783. CHECK(code_edit->get_caret_line(0) == 4);
  4784. CHECK(code_edit->get_caret_column(0) == 1);
  4785. CHECK_FALSE(code_edit->has_selection(1));
  4786. CHECK(code_edit->get_caret_line(1) == 8);
  4787. CHECK(code_edit->get_caret_column(1) == 1);
  4788. CHECK_FALSE(code_edit->has_selection(2));
  4789. CHECK(code_edit->get_caret_line(2) == 8);
  4790. CHECK(code_edit->get_caret_column(2) == 5);
  4791. CHECK_FALSE(code_edit->has_selection(3));
  4792. CHECK(code_edit->get_caret_line(3) == 6);
  4793. CHECK(code_edit->get_caret_column(3) == 2);
  4794. // Duplicate multiple lines with multiple selections.
  4795. code_edit->remove_secondary_carets();
  4796. code_edit->set_text(reset_text);
  4797. code_edit->add_caret(4, 2);
  4798. code_edit->add_caret(6, 0);
  4799. code_edit->add_caret(7, 8);
  4800. code_edit->select(0, 0, 2, 5, 0);
  4801. code_edit->select(3, 0, 4, 2, 1);
  4802. code_edit->select(7, 1, 6, 0, 2);
  4803. code_edit->select(7, 3, 7, 8, 3);
  4804. code_edit->duplicate_lines();
  4805. CHECK(code_edit->get_text() == R"(extends Node
  4806. func _ready():
  4807. extends Node
  4808. func _ready():
  4809. var a := len(OS.get_cmdline_args())
  4810. var b := get_child_count()
  4811. var a := len(OS.get_cmdline_args())
  4812. var b := get_child_count()
  4813. var c := a + b
  4814. for i in range(c):
  4815. print("This is the solution: ", sin(i))
  4816. for i in range(c):
  4817. print("This is the solution: ", sin(i))
  4818. var pos = get_index() - 1
  4819. print("Make sure this exits: %b" % pos)
  4820. )");
  4821. CHECK(code_edit->get_caret_count() == 4);
  4822. CHECK(code_edit->has_selection(0));
  4823. CHECK(code_edit->get_selection_origin_line(0) == 3);
  4824. CHECK(code_edit->get_selection_origin_column(0) == 0);
  4825. CHECK(code_edit->get_caret_line(0) == 5);
  4826. CHECK(code_edit->get_caret_column(0) == 5);
  4827. CHECK(code_edit->has_selection(1));
  4828. CHECK(code_edit->get_selection_origin_line(1) == 8);
  4829. CHECK(code_edit->get_selection_origin_column(1) == 0);
  4830. CHECK(code_edit->get_caret_line(1) == 9);
  4831. CHECK(code_edit->get_caret_column(1) == 2);
  4832. CHECK(code_edit->has_selection(2));
  4833. CHECK(code_edit->get_selection_origin_line(2) == 14);
  4834. CHECK(code_edit->get_selection_origin_column(2) == 1);
  4835. CHECK(code_edit->get_caret_line(2) == 13);
  4836. CHECK(code_edit->get_caret_column(2) == 0);
  4837. CHECK(code_edit->has_selection(3));
  4838. CHECK(code_edit->get_selection_origin_line(3) == 14);
  4839. CHECK(code_edit->get_selection_origin_column(3) == 3);
  4840. CHECK(code_edit->get_caret_line(3) == 14);
  4841. CHECK(code_edit->get_caret_column(3) == 8);
  4842. }
  4843. memdelete(code_edit);
  4844. }
  4845. } // namespace TestCodeEdit
  4846. #endif // TEST_CODE_EDIT_H