text_edit.cpp 214 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585
  1. /*************************************************************************/
  2. /* text_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "text_edit.h"
  31. #include "core/message_queue.h"
  32. #include "core/os/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/os/os.h"
  35. #include "core/project_settings.h"
  36. #include "core/script_language.h"
  37. #include "scene/main/viewport.h"
  38. #ifdef TOOLS_ENABLED
  39. #include "editor/editor_scale.h"
  40. #endif
  41. #define TAB_PIXELS
  42. inline bool _is_symbol(CharType c) {
  43. return is_symbol(c);
  44. }
  45. static bool _is_text_char(CharType c) {
  46. return !is_symbol(c);
  47. }
  48. static bool _is_whitespace(CharType c) {
  49. return c == '\t' || c == ' ';
  50. }
  51. static bool _is_char(CharType c) {
  52. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
  53. }
  54. static bool _is_number(CharType c) {
  55. return (c >= '0' && c <= '9');
  56. }
  57. static bool _is_hex_symbol(CharType c) {
  58. return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
  59. }
  60. static bool _is_pair_right_symbol(CharType c) {
  61. return c == '"' ||
  62. c == '\'' ||
  63. c == ')' ||
  64. c == ']' ||
  65. c == '}';
  66. }
  67. static bool _is_pair_left_symbol(CharType c) {
  68. return c == '"' ||
  69. c == '\'' ||
  70. c == '(' ||
  71. c == '[' ||
  72. c == '{';
  73. }
  74. static bool _is_pair_symbol(CharType c) {
  75. return _is_pair_left_symbol(c) || _is_pair_right_symbol(c);
  76. }
  77. static CharType _get_right_pair_symbol(CharType c) {
  78. if (c == '"')
  79. return '"';
  80. if (c == '\'')
  81. return '\'';
  82. if (c == '(')
  83. return ')';
  84. if (c == '[')
  85. return ']';
  86. if (c == '{')
  87. return '}';
  88. return 0;
  89. }
  90. static int _find_first_non_whitespace_column_of_line(const String &line) {
  91. int left = 0;
  92. while (left < line.length() && _is_whitespace(line[left]))
  93. left++;
  94. return left;
  95. }
  96. void TextEdit::Text::set_font(const Ref<Font> &p_font) {
  97. font = p_font;
  98. }
  99. void TextEdit::Text::set_indent_size(int p_indent_size) {
  100. indent_size = p_indent_size;
  101. }
  102. void TextEdit::Text::_update_line_cache(int p_line) const {
  103. int w = 0;
  104. int len = text[p_line].data.length();
  105. const CharType *str = text[p_line].data.c_str();
  106. // Update width.
  107. for (int i = 0; i < len; i++) {
  108. w += get_char_width(str[i], str[i + 1], w);
  109. }
  110. text.write[p_line].width_cache = w;
  111. text.write[p_line].wrap_amount_cache = -1;
  112. // Update regions.
  113. text.write[p_line].region_info.clear();
  114. for (int i = 0; i < len; i++) {
  115. if (!_is_symbol(str[i]))
  116. continue;
  117. if (str[i] == '\\') {
  118. i++; // Skip quoted anything.
  119. continue;
  120. }
  121. int left = len - i;
  122. for (int j = 0; j < color_regions->size(); j++) {
  123. const ColorRegion &cr = color_regions->operator[](j);
  124. /* BEGIN */
  125. int lr = cr.begin_key.length();
  126. const CharType *kc;
  127. bool match;
  128. if (lr != 0 && lr <= left) {
  129. kc = cr.begin_key.c_str();
  130. match = true;
  131. for (int k = 0; k < lr; k++) {
  132. if (kc[k] != str[i + k]) {
  133. match = false;
  134. break;
  135. }
  136. }
  137. if (match) {
  138. ColorRegionInfo cri;
  139. cri.end = false;
  140. cri.region = j;
  141. text.write[p_line].region_info[i] = cri;
  142. i += lr - 1;
  143. break;
  144. }
  145. }
  146. /* END */
  147. lr = cr.end_key.length();
  148. if (lr != 0 && lr <= left) {
  149. kc = cr.end_key.c_str();
  150. match = true;
  151. for (int k = 0; k < lr; k++) {
  152. if (kc[k] != str[i + k]) {
  153. match = false;
  154. break;
  155. }
  156. }
  157. if (match) {
  158. ColorRegionInfo cri;
  159. cri.end = true;
  160. cri.region = j;
  161. text.write[p_line].region_info[i] = cri;
  162. i += lr - 1;
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. const Map<int, TextEdit::Text::ColorRegionInfo> &TextEdit::Text::get_color_region_info(int p_line) const {
  170. static Map<int, ColorRegionInfo> cri;
  171. ERR_FAIL_INDEX_V(p_line, text.size(), cri);
  172. if (text[p_line].width_cache == -1) {
  173. _update_line_cache(p_line);
  174. }
  175. return text[p_line].region_info;
  176. }
  177. int TextEdit::Text::get_line_width(int p_line) const {
  178. ERR_FAIL_INDEX_V(p_line, text.size(), -1);
  179. if (text[p_line].width_cache == -1) {
  180. _update_line_cache(p_line);
  181. }
  182. return text[p_line].width_cache;
  183. }
  184. void TextEdit::Text::set_line_wrap_amount(int p_line, int p_wrap_amount) const {
  185. ERR_FAIL_INDEX(p_line, text.size());
  186. text.write[p_line].wrap_amount_cache = p_wrap_amount;
  187. }
  188. int TextEdit::Text::get_line_wrap_amount(int p_line) const {
  189. ERR_FAIL_INDEX_V(p_line, text.size(), -1);
  190. return text[p_line].wrap_amount_cache;
  191. }
  192. void TextEdit::Text::clear_width_cache() {
  193. for (int i = 0; i < text.size(); i++) {
  194. text.write[i].width_cache = -1;
  195. }
  196. }
  197. void TextEdit::Text::clear_wrap_cache() {
  198. for (int i = 0; i < text.size(); i++) {
  199. text.write[i].wrap_amount_cache = -1;
  200. }
  201. }
  202. void TextEdit::Text::clear_info_icons() {
  203. for (int i = 0; i < text.size(); i++) {
  204. text.write[i].has_info = false;
  205. }
  206. }
  207. void TextEdit::Text::clear() {
  208. text.clear();
  209. insert(0, "");
  210. }
  211. int TextEdit::Text::get_max_width(bool p_exclude_hidden) const {
  212. // Quite some work, but should be fast enough.
  213. int max = 0;
  214. for (int i = 0; i < text.size(); i++) {
  215. if (!p_exclude_hidden || !is_hidden(i))
  216. max = MAX(max, get_line_width(i));
  217. }
  218. return max;
  219. }
  220. void TextEdit::Text::set(int p_line, const String &p_text) {
  221. ERR_FAIL_INDEX(p_line, text.size());
  222. text.write[p_line].width_cache = -1;
  223. text.write[p_line].wrap_amount_cache = -1;
  224. text.write[p_line].data = p_text;
  225. }
  226. void TextEdit::Text::insert(int p_at, const String &p_text) {
  227. Line line;
  228. line.marked = false;
  229. line.safe = false;
  230. line.breakpoint = false;
  231. line.bookmark = false;
  232. line.hidden = false;
  233. line.has_info = false;
  234. line.width_cache = -1;
  235. line.wrap_amount_cache = -1;
  236. line.data = p_text;
  237. text.insert(p_at, line);
  238. }
  239. void TextEdit::Text::remove(int p_at) {
  240. text.remove(p_at);
  241. }
  242. int TextEdit::Text::get_char_width(CharType c, CharType next_c, int px) const {
  243. int tab_w = font->get_char_size(' ').width * indent_size;
  244. int w = 0;
  245. if (c == '\t') {
  246. int left = px % tab_w;
  247. if (left == 0)
  248. w = tab_w;
  249. else
  250. w = tab_w - px % tab_w; // Is right.
  251. } else {
  252. w = font->get_char_size(c, next_c).width;
  253. }
  254. return w;
  255. }
  256. void TextEdit::_update_scrollbars() {
  257. Size2 size = get_size();
  258. Size2 hmin = h_scroll->get_combined_minimum_size();
  259. Size2 vmin = v_scroll->get_combined_minimum_size();
  260. v_scroll->set_begin(Point2(size.width - vmin.width, cache.style_normal->get_margin(MARGIN_TOP)));
  261. v_scroll->set_end(Point2(size.width, size.height - cache.style_normal->get_margin(MARGIN_TOP) - cache.style_normal->get_margin(MARGIN_BOTTOM)));
  262. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  263. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  264. int visible_rows = get_visible_rows();
  265. int total_rows = get_total_visible_rows();
  266. if (scroll_past_end_of_file_enabled) {
  267. total_rows += visible_rows - 1;
  268. }
  269. int visible_width = size.width - cache.style_normal->get_minimum_size().width;
  270. int total_width = text.get_max_width(true) + vmin.x;
  271. if (line_numbers)
  272. total_width += cache.line_number_w;
  273. if (draw_breakpoint_gutter || draw_bookmark_gutter) {
  274. total_width += cache.breakpoint_gutter_width;
  275. }
  276. if (draw_info_gutter) {
  277. total_width += cache.info_gutter_width;
  278. }
  279. if (draw_fold_gutter) {
  280. total_width += cache.fold_gutter_width;
  281. }
  282. if (draw_minimap) {
  283. total_width += cache.minimap_width;
  284. }
  285. bool use_hscroll = true;
  286. bool use_vscroll = true;
  287. // Thanks yessopie for this clever bit of logic.
  288. if (total_rows <= visible_rows && total_width <= visible_width) {
  289. use_hscroll = false;
  290. use_vscroll = false;
  291. } else {
  292. if (total_rows > visible_rows && total_width <= visible_width) {
  293. use_hscroll = false;
  294. }
  295. if (total_rows <= visible_rows && total_width > visible_width) {
  296. use_vscroll = false;
  297. }
  298. }
  299. updating_scrolls = true;
  300. if (use_vscroll) {
  301. v_scroll->show();
  302. v_scroll->set_max(total_rows + get_visible_rows_offset());
  303. v_scroll->set_page(visible_rows + get_visible_rows_offset());
  304. if (smooth_scroll_enabled) {
  305. v_scroll->set_step(0.25);
  306. } else {
  307. v_scroll->set_step(1);
  308. }
  309. set_v_scroll(get_v_scroll());
  310. } else {
  311. cursor.line_ofs = 0;
  312. cursor.wrap_ofs = 0;
  313. v_scroll->set_value(0);
  314. v_scroll->hide();
  315. }
  316. if (use_hscroll && !is_wrap_enabled()) {
  317. h_scroll->show();
  318. h_scroll->set_max(total_width);
  319. h_scroll->set_page(visible_width);
  320. if (cursor.x_ofs > (total_width - visible_width))
  321. cursor.x_ofs = (total_width - visible_width);
  322. if (fabs(h_scroll->get_value() - (double)cursor.x_ofs) >= 1) {
  323. h_scroll->set_value(cursor.x_ofs);
  324. }
  325. } else {
  326. cursor.x_ofs = 0;
  327. h_scroll->set_value(0);
  328. h_scroll->hide();
  329. }
  330. updating_scrolls = false;
  331. }
  332. void TextEdit::_click_selection_held() {
  333. // Warning: is_mouse_button_pressed(BUTTON_LEFT) returns false for double+ clicks, so this doesn't work for MODE_WORD
  334. // and MODE_LINE. However, moving the mouse triggers _gui_input, which calls these functions too, so that's not a huge problem.
  335. // I'm unsure if there's an actual fix that doesn't have a ton of side effects.
  336. if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT) && selection.selecting_mode != Selection::MODE_NONE) {
  337. switch (selection.selecting_mode) {
  338. case Selection::MODE_POINTER: {
  339. _update_selection_mode_pointer();
  340. } break;
  341. case Selection::MODE_WORD: {
  342. _update_selection_mode_word();
  343. } break;
  344. case Selection::MODE_LINE: {
  345. _update_selection_mode_line();
  346. } break;
  347. default: {
  348. break;
  349. }
  350. }
  351. } else {
  352. click_select_held->stop();
  353. }
  354. }
  355. void TextEdit::_update_selection_mode_pointer() {
  356. dragging_selection = true;
  357. Point2 mp = get_local_mouse_position();
  358. int row, col;
  359. _get_mouse_pos(Point2i(mp.x, mp.y), row, col);
  360. select(selection.selecting_line, selection.selecting_column, row, col);
  361. cursor_set_line(row, false);
  362. cursor_set_column(col);
  363. update();
  364. click_select_held->start();
  365. }
  366. void TextEdit::_update_selection_mode_word() {
  367. dragging_selection = true;
  368. Point2 mp = get_local_mouse_position();
  369. int row, col;
  370. _get_mouse_pos(Point2i(mp.x, mp.y), row, col);
  371. String line = text[row];
  372. int beg = CLAMP(col, 0, line.length());
  373. // If its the first selection and on whitespace make sure we grab the word instead.
  374. if (!selection.active) {
  375. while (beg > 0 && line[beg] <= 32) {
  376. beg--;
  377. }
  378. }
  379. int end = beg;
  380. bool symbol = beg < line.length() && _is_symbol(line[beg]);
  381. // Get the word end and begin points.
  382. while (beg > 0 && line[beg - 1] > 32 && (symbol == _is_symbol(line[beg - 1]))) {
  383. beg--;
  384. }
  385. while (end < line.length() && line[end + 1] > 32 && (symbol == _is_symbol(line[end + 1]))) {
  386. end++;
  387. }
  388. if (end < line.length()) {
  389. end += 1;
  390. }
  391. // Initial selection.
  392. if (!selection.active) {
  393. select(row, beg, row, end);
  394. selection.selecting_column = beg;
  395. selection.selected_word_beg = beg;
  396. selection.selected_word_end = end;
  397. selection.selected_word_origin = beg;
  398. cursor_set_line(selection.to_line, false);
  399. cursor_set_column(selection.to_column);
  400. } else {
  401. if ((col <= selection.selected_word_origin && row == selection.selecting_line) || row < selection.selecting_line) {
  402. selection.selecting_column = selection.selected_word_end;
  403. select(row, beg, selection.selecting_line, selection.selected_word_end);
  404. cursor_set_line(selection.from_line, false);
  405. cursor_set_column(selection.from_column);
  406. } else {
  407. selection.selecting_column = selection.selected_word_beg;
  408. select(selection.selecting_line, selection.selected_word_beg, row, end);
  409. cursor_set_line(selection.to_line, false);
  410. cursor_set_column(selection.to_column);
  411. }
  412. }
  413. update();
  414. click_select_held->start();
  415. }
  416. void TextEdit::_update_selection_mode_line() {
  417. dragging_selection = true;
  418. Point2 mp = get_local_mouse_position();
  419. int row, col;
  420. _get_mouse_pos(Point2i(mp.x, mp.y), row, col);
  421. col = 0;
  422. if (row < selection.selecting_line) {
  423. // Cursor is above us.
  424. cursor_set_line(row - 1, false);
  425. selection.selecting_column = text[selection.selecting_line].length();
  426. } else {
  427. // Cursor is below us.
  428. cursor_set_line(row + 1, false);
  429. selection.selecting_column = 0;
  430. col = text[row].length();
  431. }
  432. cursor_set_column(0);
  433. select(selection.selecting_line, selection.selecting_column, row, col);
  434. update();
  435. click_select_held->start();
  436. }
  437. void TextEdit::_update_minimap_click() {
  438. Point2 mp = get_local_mouse_position();
  439. int xmargin_end = get_size().width - cache.style_normal->get_margin(MARGIN_RIGHT);
  440. if (!dragging_minimap && (mp.x < xmargin_end - minimap_width || mp.y > xmargin_end)) {
  441. minimap_clicked = false;
  442. return;
  443. }
  444. minimap_clicked = true;
  445. dragging_minimap = true;
  446. int row;
  447. _get_minimap_mouse_row(Point2i(mp.x, mp.y), row);
  448. if (row >= get_first_visible_line() && (row < get_last_visible_line() || row >= (text.size() - 1))) {
  449. minimap_scroll_ratio = v_scroll->get_as_ratio();
  450. minimap_scroll_click_pos = mp.y;
  451. can_drag_minimap = true;
  452. return;
  453. }
  454. int wi;
  455. int first_line = row - num_lines_from_rows(row, 0, -get_visible_rows() / 2, wi) + 1;
  456. double delta = get_scroll_pos_for_line(first_line, wi) - get_v_scroll();
  457. if (delta < 0) {
  458. _scroll_up(-delta);
  459. } else {
  460. _scroll_down(delta);
  461. }
  462. }
  463. void TextEdit::_update_minimap_drag() {
  464. if (!can_drag_minimap) {
  465. return;
  466. }
  467. int control_height = _get_control_height();
  468. int scroll_height = v_scroll->get_max() * (minimap_char_size.y + minimap_line_spacing);
  469. if (control_height > scroll_height) {
  470. control_height = scroll_height;
  471. }
  472. Point2 mp = get_local_mouse_position();
  473. double diff = (mp.y - minimap_scroll_click_pos) / control_height;
  474. v_scroll->set_as_ratio(minimap_scroll_ratio + diff);
  475. }
  476. void TextEdit::_notification(int p_what) {
  477. switch (p_what) {
  478. case NOTIFICATION_ENTER_TREE: {
  479. _update_caches();
  480. if (cursor_changed_dirty)
  481. MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
  482. if (text_changed_dirty)
  483. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  484. _update_wrap_at();
  485. } break;
  486. case NOTIFICATION_RESIZED: {
  487. _update_scrollbars();
  488. _update_wrap_at();
  489. } break;
  490. case NOTIFICATION_VISIBILITY_CHANGED: {
  491. if (is_visible()) {
  492. call_deferred("_update_scrollbars");
  493. call_deferred("_update_wrap_at");
  494. }
  495. } break;
  496. case NOTIFICATION_THEME_CHANGED: {
  497. _update_caches();
  498. _update_wrap_at();
  499. syntax_highlighting_cache.clear();
  500. } break;
  501. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  502. window_has_focus = true;
  503. draw_caret = true;
  504. update();
  505. } break;
  506. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  507. window_has_focus = false;
  508. draw_caret = false;
  509. update();
  510. } break;
  511. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  512. if (scrolling && get_v_scroll() != target_v_scroll) {
  513. double target_y = target_v_scroll - get_v_scroll();
  514. double dist = sqrt(target_y * target_y);
  515. // To ensure minimap is responsive override the speed setting.
  516. double vel = ((target_y / dist) * ((minimap_clicked) ? 3000 : v_scroll_speed)) * get_physics_process_delta_time();
  517. if (Math::abs(vel) >= dist) {
  518. set_v_scroll(target_v_scroll);
  519. scrolling = false;
  520. minimap_clicked = false;
  521. set_physics_process_internal(false);
  522. } else {
  523. set_v_scroll(get_v_scroll() + vel);
  524. }
  525. } else {
  526. scrolling = false;
  527. minimap_clicked = false;
  528. set_physics_process_internal(false);
  529. }
  530. } break;
  531. case NOTIFICATION_DRAW: {
  532. if (first_draw) {
  533. // Size may not be the final one, so attempts to ensure cursor was visible may have failed.
  534. adjust_viewport_to_cursor();
  535. first_draw = false;
  536. }
  537. Size2 size = get_size();
  538. if ((!has_focus() && !menu->has_focus()) || !window_has_focus) {
  539. draw_caret = false;
  540. }
  541. if (draw_breakpoint_gutter || draw_bookmark_gutter) {
  542. breakpoint_gutter_width = (get_row_height() * 55) / 100;
  543. cache.breakpoint_gutter_width = breakpoint_gutter_width;
  544. } else {
  545. cache.breakpoint_gutter_width = 0;
  546. }
  547. if (draw_info_gutter) {
  548. info_gutter_width = (get_row_height());
  549. cache.info_gutter_width = info_gutter_width;
  550. } else {
  551. cache.info_gutter_width = 0;
  552. }
  553. if (draw_fold_gutter) {
  554. fold_gutter_width = (get_row_height() * 55) / 100;
  555. cache.fold_gutter_width = fold_gutter_width;
  556. } else {
  557. cache.fold_gutter_width = 0;
  558. }
  559. cache.minimap_width = 0;
  560. if (draw_minimap) {
  561. cache.minimap_width = minimap_width;
  562. }
  563. int line_number_char_count = 0;
  564. {
  565. int lc = text.size();
  566. cache.line_number_w = 0;
  567. while (lc) {
  568. cache.line_number_w += 1;
  569. lc /= 10;
  570. };
  571. if (line_numbers) {
  572. line_number_char_count = cache.line_number_w;
  573. cache.line_number_w = (cache.line_number_w + 1) * cache.font->get_char_size('0').width;
  574. } else {
  575. cache.line_number_w = 0;
  576. }
  577. }
  578. _update_scrollbars();
  579. RID ci = get_canvas_item();
  580. VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true);
  581. int xmargin_beg = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width + cache.info_gutter_width;
  582. int xmargin_end = size.width - cache.style_normal->get_margin(MARGIN_RIGHT) - cache.minimap_width;
  583. // Let's do it easy for now.
  584. cache.style_normal->draw(ci, Rect2(Point2(), size));
  585. if (readonly) {
  586. cache.style_readonly->draw(ci, Rect2(Point2(), size));
  587. draw_caret = false;
  588. }
  589. if (has_focus())
  590. cache.style_focus->draw(ci, Rect2(Point2(), size));
  591. int ascent = cache.font->get_ascent();
  592. int visible_rows = get_visible_rows() + 1;
  593. Color color = readonly ? cache.font_color_readonly : cache.font_color;
  594. if (syntax_coloring) {
  595. if (cache.background_color.a > 0.01) {
  596. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(), get_size()), cache.background_color);
  597. }
  598. }
  599. if (line_length_guidelines) {
  600. const int hard_x = xmargin_beg + (int)cache.font->get_char_size('0').width * line_length_guideline_hard_col - cursor.x_ofs;
  601. if (hard_x > xmargin_beg && hard_x < xmargin_end) {
  602. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2(hard_x, 0), Point2(hard_x, size.height), cache.line_length_guideline_color);
  603. }
  604. // Draw a "Soft" line length guideline, less visible than the hard line length guideline.
  605. // It's usually set to a lower column compared to the hard line length guideline.
  606. // Only drawn if its column differs from the hard line length guideline.
  607. const int soft_x = xmargin_beg + (int)cache.font->get_char_size('0').width * line_length_guideline_soft_col - cursor.x_ofs;
  608. if (hard_x != soft_x && soft_x > xmargin_beg && soft_x < xmargin_end) {
  609. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2(soft_x, 0), Point2(soft_x, size.height), cache.line_length_guideline_color * Color(1, 1, 1, 0.5));
  610. }
  611. }
  612. int brace_open_match_line = -1;
  613. int brace_open_match_column = -1;
  614. bool brace_open_matching = false;
  615. bool brace_open_mismatch = false;
  616. int brace_close_match_line = -1;
  617. int brace_close_match_column = -1;
  618. bool brace_close_matching = false;
  619. bool brace_close_mismatch = false;
  620. if (brace_matching_enabled && cursor.line >= 0 && cursor.line < text.size() && cursor.column >= 0) {
  621. if (cursor.column < text[cursor.line].length()) {
  622. // Check for open.
  623. CharType c = text[cursor.line][cursor.column];
  624. CharType closec = 0;
  625. if (c == '[') {
  626. closec = ']';
  627. } else if (c == '{') {
  628. closec = '}';
  629. } else if (c == '(') {
  630. closec = ')';
  631. }
  632. if (closec != 0) {
  633. int stack = 1;
  634. for (int i = cursor.line; i < text.size(); i++) {
  635. int from = i == cursor.line ? cursor.column + 1 : 0;
  636. for (int j = from; j < text[i].length(); j++) {
  637. CharType cc = text[i][j];
  638. // Ignore any brackets inside a string.
  639. if (cc == '"' || cc == '\'') {
  640. CharType quotation = cc;
  641. do {
  642. j++;
  643. if (!(j < text[i].length())) {
  644. break;
  645. }
  646. cc = text[i][j];
  647. // Skip over escaped quotation marks inside strings.
  648. if (cc == '\\') {
  649. bool escaped = true;
  650. while (j + 1 < text[i].length() && text[i][j + 1] == '\\') {
  651. escaped = !escaped;
  652. j++;
  653. }
  654. if (escaped) {
  655. j++;
  656. continue;
  657. }
  658. }
  659. } while (cc != quotation);
  660. } else if (cc == c)
  661. stack++;
  662. else if (cc == closec)
  663. stack--;
  664. if (stack == 0) {
  665. brace_open_match_line = i;
  666. brace_open_match_column = j;
  667. brace_open_matching = true;
  668. break;
  669. }
  670. }
  671. if (brace_open_match_line != -1)
  672. break;
  673. }
  674. if (!brace_open_matching)
  675. brace_open_mismatch = true;
  676. }
  677. }
  678. if (cursor.column > 0) {
  679. CharType c = text[cursor.line][cursor.column - 1];
  680. CharType closec = 0;
  681. if (c == ']') {
  682. closec = '[';
  683. } else if (c == '}') {
  684. closec = '{';
  685. } else if (c == ')') {
  686. closec = '(';
  687. }
  688. if (closec != 0) {
  689. int stack = 1;
  690. for (int i = cursor.line; i >= 0; i--) {
  691. int from = i == cursor.line ? cursor.column - 2 : text[i].length() - 1;
  692. for (int j = from; j >= 0; j--) {
  693. CharType cc = text[i][j];
  694. // Ignore any brackets inside a string.
  695. if (cc == '"' || cc == '\'') {
  696. CharType quotation = cc;
  697. do {
  698. j--;
  699. if (!(j >= 0)) {
  700. break;
  701. }
  702. cc = text[i][j];
  703. // Skip over escaped quotation marks inside strings.
  704. if (cc == quotation) {
  705. bool escaped = false;
  706. while (j - 1 >= 0 && text[i][j - 1] == '\\') {
  707. escaped = !escaped;
  708. j--;
  709. }
  710. if (escaped) {
  711. cc = '\\';
  712. continue;
  713. }
  714. }
  715. } while (cc != quotation);
  716. } else if (cc == c)
  717. stack++;
  718. else if (cc == closec)
  719. stack--;
  720. if (stack == 0) {
  721. brace_close_match_line = i;
  722. brace_close_match_column = j;
  723. brace_close_matching = true;
  724. break;
  725. }
  726. }
  727. if (brace_close_match_line != -1)
  728. break;
  729. }
  730. if (!brace_close_matching)
  731. brace_close_mismatch = true;
  732. }
  733. }
  734. }
  735. Point2 cursor_pos;
  736. int cursor_insert_offset_y = 0;
  737. // Get the highlighted words.
  738. String highlighted_text = get_selection_text();
  739. // Check if highlighted words contains only whitespaces (tabs or spaces).
  740. bool only_whitespaces_highlighted = highlighted_text.strip_edges() == String();
  741. String line_num_padding = line_numbers_zero_padded ? "0" : " ";
  742. int cursor_wrap_index = get_cursor_wrap_index();
  743. FontDrawer drawer(cache.font, Color(1, 1, 1));
  744. int first_visible_line = get_first_visible_line() - 1;
  745. int draw_amount = visible_rows + (smooth_scroll_enabled ? 1 : 0);
  746. draw_amount += times_line_wraps(first_visible_line + 1);
  747. // minimap
  748. if (draw_minimap) {
  749. int minimap_visible_lines = _get_minimap_visible_rows();
  750. int minimap_line_height = (minimap_char_size.y + minimap_line_spacing);
  751. int minimap_tab_size = minimap_char_size.x * indent_size;
  752. // calculate viewport size and y offset
  753. int viewport_height = (draw_amount - 1) * minimap_line_height;
  754. int control_height = _get_control_height() - viewport_height;
  755. int viewport_offset_y = round(get_scroll_pos_for_line(first_visible_line) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount));
  756. // calculate the first line.
  757. int num_lines_before = round((viewport_offset_y) / minimap_line_height);
  758. int wi;
  759. int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_visible_line;
  760. if (minimap_line >= 0) {
  761. minimap_line -= num_lines_from_rows(first_visible_line, 0, -num_lines_before, wi);
  762. minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0);
  763. }
  764. int minimap_draw_amount = minimap_visible_lines + times_line_wraps(minimap_line + 1);
  765. // draw the minimap
  766. Color viewport_color = (cache.background_color.get_v() < 0.5) ? Color(1, 1, 1, 0.1) : Color(0, 0, 0, 0.1);
  767. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), viewport_offset_y, cache.minimap_width, viewport_height), viewport_color);
  768. for (int i = 0; i < minimap_draw_amount; i++) {
  769. minimap_line++;
  770. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  771. break;
  772. }
  773. while (is_line_hidden(minimap_line)) {
  774. minimap_line++;
  775. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  776. break;
  777. }
  778. }
  779. if (minimap_line < 0 || minimap_line >= (int)text.size()) {
  780. break;
  781. }
  782. Map<int, HighlighterInfo> color_map;
  783. if (syntax_coloring) {
  784. color_map = _get_line_syntax_highlighting(minimap_line);
  785. }
  786. Color current_color = cache.font_color;
  787. if (readonly) {
  788. current_color = cache.font_color_readonly;
  789. }
  790. Vector<String> wrap_rows = get_wrap_rows_text(minimap_line);
  791. int line_wrap_amount = times_line_wraps(minimap_line);
  792. int last_wrap_column = 0;
  793. for (int line_wrap_index = 0; line_wrap_index < line_wrap_amount + 1; line_wrap_index++) {
  794. if (line_wrap_index != 0) {
  795. i++;
  796. if (i >= minimap_draw_amount)
  797. break;
  798. }
  799. const String &str = wrap_rows[line_wrap_index];
  800. int indent_px = line_wrap_index != 0 ? get_indent_level(minimap_line) : 0;
  801. if (indent_px >= wrap_at) {
  802. indent_px = 0;
  803. }
  804. indent_px = minimap_char_size.x * indent_px;
  805. if (line_wrap_index > 0) {
  806. last_wrap_column += wrap_rows[line_wrap_index - 1].length();
  807. }
  808. if (minimap_line == cursor.line && cursor_wrap_index == line_wrap_index && highlight_current_line) {
  809. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), i * 3, cache.minimap_width, 2), cache.current_line_color);
  810. }
  811. Color previous_color;
  812. int characters = 0;
  813. int tabs = 0;
  814. for (int j = 0; j < str.length(); j++) {
  815. if (syntax_coloring) {
  816. if (color_map.has(last_wrap_column + j)) {
  817. current_color = color_map[last_wrap_column + j].color;
  818. if (readonly) {
  819. current_color.a = cache.font_color_readonly.a;
  820. }
  821. }
  822. color = current_color;
  823. }
  824. if (j == 0) {
  825. previous_color = color;
  826. }
  827. int xpos = indent_px + ((xmargin_end + minimap_char_size.x) + (minimap_char_size.x * j)) + tabs;
  828. bool out_of_bounds = (xpos >= xmargin_end + cache.minimap_width);
  829. bool is_whitespace = _is_whitespace(str[j]);
  830. if (!is_whitespace) {
  831. characters++;
  832. if (j < str.length() - 1 && color == previous_color && !out_of_bounds) {
  833. continue;
  834. }
  835. // If we've changed colour we are at the start of a new section, therefore we need to go back to the end
  836. // of the previous section to draw it, we'll also add the character back on.
  837. if (color != previous_color) {
  838. characters--;
  839. j--;
  840. if (str[j] == '\t') {
  841. tabs -= minimap_tab_size;
  842. }
  843. }
  844. }
  845. if (characters > 0) {
  846. previous_color.a *= 0.6;
  847. // take one for zero indexing, and if we hit whitespace / the end of a word.
  848. int chars = MAX(0, (j - (characters - 1)) - (is_whitespace ? 1 : 0)) + 1;
  849. int char_x_ofs = indent_px + ((xmargin_end + minimap_char_size.x) + (minimap_char_size.x * chars)) + tabs;
  850. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_x_ofs, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), previous_color);
  851. }
  852. if (out_of_bounds) {
  853. break;
  854. }
  855. // re-adjust if we went backwards.
  856. if (color != previous_color && !is_whitespace) {
  857. characters++;
  858. }
  859. if (str[j] == '\t') {
  860. tabs += minimap_tab_size;
  861. }
  862. previous_color = color;
  863. characters = 0;
  864. }
  865. }
  866. }
  867. }
  868. // draw main text
  869. int line = first_visible_line;
  870. for (int i = 0; i < draw_amount; i++) {
  871. line++;
  872. if (line < 0 || line >= (int)text.size())
  873. continue;
  874. while (is_line_hidden(line)) {
  875. line++;
  876. if (line < 0 || line >= (int)text.size()) {
  877. break;
  878. }
  879. }
  880. if (line < 0 || line >= (int)text.size())
  881. continue;
  882. const String &fullstr = text[line];
  883. Map<int, HighlighterInfo> color_map;
  884. if (syntax_coloring) {
  885. color_map = _get_line_syntax_highlighting(line);
  886. }
  887. // Ensure we at least use the font color.
  888. Color current_color = readonly ? cache.font_color_readonly : cache.font_color;
  889. bool underlined = false;
  890. Vector<String> wrap_rows = get_wrap_rows_text(line);
  891. int line_wrap_amount = times_line_wraps(line);
  892. int last_wrap_column = 0;
  893. for (int line_wrap_index = 0; line_wrap_index < line_wrap_amount + 1; line_wrap_index++) {
  894. if (line_wrap_index != 0) {
  895. i++;
  896. if (i >= draw_amount)
  897. break;
  898. }
  899. const String &str = wrap_rows[line_wrap_index];
  900. int indent_px = line_wrap_index != 0 ? get_indent_level(line) * cache.font->get_char_size(' ').width : 0;
  901. if (indent_px >= wrap_at) {
  902. indent_px = 0;
  903. }
  904. if (line_wrap_index > 0)
  905. last_wrap_column += wrap_rows[line_wrap_index - 1].length();
  906. int char_margin = xmargin_beg - cursor.x_ofs;
  907. char_margin += indent_px;
  908. int char_ofs = 0;
  909. int ofs_readonly = 0;
  910. int ofs_x = 0;
  911. if (readonly) {
  912. ofs_readonly = cache.style_readonly->get_offset().y / 2;
  913. ofs_x = cache.style_readonly->get_offset().x / 2;
  914. }
  915. int ofs_y = (i * get_row_height() + cache.line_spacing / 2) + ofs_readonly;
  916. ofs_y -= cursor.wrap_ofs * get_row_height();
  917. if (smooth_scroll_enabled)
  918. ofs_y += (-get_v_scroll_offset()) * get_row_height();
  919. // Check if line contains highlighted word.
  920. int highlighted_text_col = -1;
  921. int search_text_col = -1;
  922. int highlighted_word_col = -1;
  923. if (!search_text.empty())
  924. search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0);
  925. if (highlighted_text.length() != 0 && highlighted_text != search_text)
  926. highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
  927. if (select_identifiers_enabled && highlighted_word.length() != 0) {
  928. if (_is_char(highlighted_word[0])) {
  929. highlighted_word_col = _get_column_pos_of_word(highlighted_word, fullstr, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
  930. }
  931. }
  932. if (text.is_marked(line)) {
  933. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, xmargin_end - xmargin_beg, get_row_height()), cache.mark_color);
  934. }
  935. if (str.length() == 0) {
  936. // Draw line background if empty as we won't loop at at all.
  937. if (line == cursor.line && cursor_wrap_index == line_wrap_index && highlight_current_line) {
  938. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs_x, ofs_y, xmargin_end, get_row_height()), cache.current_line_color);
  939. }
  940. // Give visual indication of empty selected line.
  941. if (selection.active && line >= selection.from_line && line <= selection.to_line && char_margin >= xmargin_beg) {
  942. int char_w = cache.font->get_char_size(' ').width;
  943. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, char_w, get_row_height()), cache.selection_color);
  944. }
  945. } else {
  946. // If it has text, then draw current line marker in the margin, as line number etc will draw over it, draw the rest of line marker later.
  947. if (line == cursor.line && cursor_wrap_index == line_wrap_index && highlight_current_line) {
  948. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(0, ofs_y, xmargin_beg + ofs_x, get_row_height()), cache.current_line_color);
  949. }
  950. }
  951. if (line_wrap_index == 0) {
  952. // Only do these if we are on the first wrapped part of a line.
  953. if (text.is_breakpoint(line) && !draw_breakpoint_gutter) {
  954. #ifdef TOOLS_ENABLED
  955. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y + get_row_height() - EDSCALE, xmargin_end - xmargin_beg, EDSCALE), cache.breakpoint_color);
  956. #else
  957. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, xmargin_end - xmargin_beg, get_row_height()), cache.breakpoint_color);
  958. #endif
  959. }
  960. // Draw bookmark marker.
  961. if (text.is_bookmark(line)) {
  962. if (draw_bookmark_gutter) {
  963. int vertical_gap = (get_row_height() * 40) / 100;
  964. int horizontal_gap = (cache.breakpoint_gutter_width * 30) / 100;
  965. int marker_radius = get_row_height() - (vertical_gap * 2);
  966. VisualServer::get_singleton()->canvas_item_add_circle(ci, Point2(cache.style_normal->get_margin(MARGIN_LEFT) + horizontal_gap - 2 + marker_radius / 2, ofs_y + vertical_gap + marker_radius / 2), marker_radius, Color(cache.bookmark_color.r, cache.bookmark_color.g, cache.bookmark_color.b));
  967. }
  968. }
  969. // Draw breakpoint marker.
  970. if (text.is_breakpoint(line)) {
  971. if (draw_breakpoint_gutter) {
  972. int vertical_gap = (get_row_height() * 40) / 100;
  973. int horizontal_gap = (cache.breakpoint_gutter_width * 30) / 100;
  974. int marker_height = get_row_height() - (vertical_gap * 2);
  975. int marker_width = cache.breakpoint_gutter_width - (horizontal_gap * 2);
  976. // No transparency on marker.
  977. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cache.style_normal->get_margin(MARGIN_LEFT) + horizontal_gap - 2, ofs_y + vertical_gap, marker_width, marker_height), Color(cache.breakpoint_color.r, cache.breakpoint_color.g, cache.breakpoint_color.b));
  978. }
  979. }
  980. // Draw info icons.
  981. if (draw_info_gutter && text.has_info_icon(line)) {
  982. int vertical_gap = (get_row_height() * 40) / 100;
  983. int horizontal_gap = (cache.info_gutter_width * 30) / 100;
  984. int gutter_left = cache.style_normal->get_margin(MARGIN_LEFT) + cache.breakpoint_gutter_width;
  985. Ref<Texture2D> info_icon = text.get_info_icon(line);
  986. // Ensure the icon fits the gutter size.
  987. Size2i icon_size = info_icon->get_size();
  988. if (icon_size.width > cache.info_gutter_width - horizontal_gap) {
  989. icon_size.width = cache.info_gutter_width - horizontal_gap;
  990. }
  991. if (icon_size.height > get_row_height() - horizontal_gap) {
  992. icon_size.height = get_row_height() - horizontal_gap;
  993. }
  994. Size2i icon_pos;
  995. int xofs = horizontal_gap - (info_icon->get_width() / 4);
  996. int yofs = vertical_gap - (info_icon->get_height() / 4);
  997. icon_pos.x = gutter_left + xofs + ofs_x;
  998. icon_pos.y = ofs_y + yofs;
  999. draw_texture_rect(info_icon, Rect2(icon_pos, icon_size));
  1000. }
  1001. // Draw execution marker.
  1002. if (executing_line == line) {
  1003. if (draw_breakpoint_gutter) {
  1004. int icon_extra_size = 4;
  1005. int vertical_gap = (get_row_height() * 40) / 100;
  1006. int horizontal_gap = (cache.breakpoint_gutter_width * 30) / 100;
  1007. int marker_height = get_row_height() - (vertical_gap * 2) + icon_extra_size;
  1008. int marker_width = cache.breakpoint_gutter_width - (horizontal_gap * 2) + icon_extra_size;
  1009. cache.executing_icon->draw_rect(ci, Rect2(cache.style_normal->get_margin(MARGIN_LEFT) + horizontal_gap - 2 - icon_extra_size / 2, ofs_y + vertical_gap - icon_extra_size / 2, marker_width, marker_height), false, Color(cache.executing_line_color.r, cache.executing_line_color.g, cache.executing_line_color.b));
  1010. } else {
  1011. #ifdef TOOLS_ENABLED
  1012. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y + get_row_height() - EDSCALE, xmargin_end - xmargin_beg, EDSCALE), cache.executing_line_color);
  1013. #else
  1014. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, xmargin_end - xmargin_beg, get_row_height()), cache.executing_line_color);
  1015. #endif
  1016. }
  1017. }
  1018. // Draw fold markers.
  1019. if (draw_fold_gutter) {
  1020. int horizontal_gap = (cache.fold_gutter_width * 30) / 100;
  1021. int gutter_left = cache.style_normal->get_margin(MARGIN_LEFT) + cache.breakpoint_gutter_width + cache.line_number_w + cache.info_gutter_width;
  1022. if (is_folded(line)) {
  1023. int xofs = horizontal_gap - (cache.can_fold_icon->get_width()) / 2;
  1024. int yofs = (get_row_height() - cache.folded_icon->get_height()) / 2;
  1025. cache.folded_icon->draw(ci, Point2(gutter_left + xofs + ofs_x, ofs_y + yofs), cache.code_folding_color);
  1026. } else if (can_fold(line)) {
  1027. int xofs = -cache.can_fold_icon->get_width() / 2 - horizontal_gap + 3;
  1028. int yofs = (get_row_height() - cache.can_fold_icon->get_height()) / 2;
  1029. cache.can_fold_icon->draw(ci, Point2(gutter_left + xofs + ofs_x, ofs_y + yofs), cache.code_folding_color);
  1030. }
  1031. }
  1032. // Draw line numbers.
  1033. if (cache.line_number_w) {
  1034. int yofs = ofs_y + (get_row_height() - cache.font->get_height()) / 2;
  1035. String fc = String::num(line + 1);
  1036. while (fc.length() < line_number_char_count) {
  1037. fc = line_num_padding + fc;
  1038. }
  1039. cache.font->draw(ci, Point2(cache.style_normal->get_margin(MARGIN_LEFT) + cache.breakpoint_gutter_width + cache.info_gutter_width + ofs_x, yofs + cache.font->get_ascent()), fc, text.is_safe(line) ? cache.safe_line_number_color : cache.line_number_color);
  1040. }
  1041. }
  1042. // Loop through characters in one line.
  1043. int j = 0;
  1044. for (; j < str.length(); j++) {
  1045. if (syntax_coloring) {
  1046. if (color_map.has(last_wrap_column + j)) {
  1047. current_color = color_map[last_wrap_column + j].color;
  1048. if (readonly && current_color.a > cache.font_color_readonly.a) {
  1049. current_color.a = cache.font_color_readonly.a;
  1050. }
  1051. }
  1052. color = current_color;
  1053. }
  1054. int char_w;
  1055. // Handle tabulator.
  1056. char_w = text.get_char_width(str[j], str[j + 1], char_ofs);
  1057. if ((char_ofs + char_margin) < xmargin_beg) {
  1058. char_ofs += char_w;
  1059. // Line highlighting handle horizontal clipping.
  1060. if (line == cursor.line && cursor_wrap_index == line_wrap_index && highlight_current_line) {
  1061. if (j == str.length() - 1) {
  1062. // End of line when last char is skipped.
  1063. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, xmargin_end - (char_ofs + char_margin + char_w), get_row_height()), cache.current_line_color);
  1064. } else if ((char_ofs + char_margin) > xmargin_beg) {
  1065. // Char next to margin is skipped.
  1066. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, (char_ofs + char_margin) - (xmargin_beg + ofs_x), get_row_height()), cache.current_line_color);
  1067. }
  1068. }
  1069. continue;
  1070. }
  1071. if ((char_ofs + char_margin + char_w) >= xmargin_end) {
  1072. break;
  1073. }
  1074. bool in_search_result = false;
  1075. if (search_text_col != -1) {
  1076. // If we are at the end check for new search result on same line.
  1077. if (j >= search_text_col + search_text.length())
  1078. search_text_col = _get_column_pos_of_word(search_text, str, search_flags, j);
  1079. in_search_result = j >= search_text_col && j < search_text_col + search_text.length();
  1080. if (in_search_result) {
  1081. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(char_w, get_row_height())), cache.search_result_color);
  1082. }
  1083. }
  1084. // Current line highlighting.
  1085. bool in_selection = (selection.active && line >= selection.from_line && line <= selection.to_line && (line > selection.from_line || last_wrap_column + j >= selection.from_column) && (line < selection.to_line || last_wrap_column + j < selection.to_column));
  1086. if (line == cursor.line && cursor_wrap_index == line_wrap_index && highlight_current_line) {
  1087. // Draw the wrap indent offset highlight.
  1088. if (line_wrap_index != 0 && j == 0) {
  1089. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(char_ofs + char_margin + ofs_x - indent_px, ofs_y, indent_px, get_row_height()), cache.current_line_color);
  1090. }
  1091. // If its the last char draw to end of the line.
  1092. if (j == str.length() - 1) {
  1093. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(char_ofs + char_margin + char_w + ofs_x, ofs_y, xmargin_end - (char_ofs + char_margin + char_w), get_row_height()), cache.current_line_color);
  1094. }
  1095. // Actual text.
  1096. if (!in_selection) {
  1097. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y), Size2i(char_w, get_row_height())), cache.current_line_color);
  1098. }
  1099. }
  1100. if (in_selection) {
  1101. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y), Size2i(char_w, get_row_height())), cache.selection_color);
  1102. }
  1103. if (in_search_result) {
  1104. Color border_color = (line == search_result_line && j >= search_result_col && j < search_result_col + search_text.length()) ? cache.font_color : cache.search_result_border_color;
  1105. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y), Size2i(char_w, 1)), border_color);
  1106. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y + get_row_height() - 1), Size2i(char_w, 1)), border_color);
  1107. if (j == search_text_col)
  1108. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y), Size2i(1, get_row_height())), border_color);
  1109. if (j == search_text_col + search_text.length() - 1)
  1110. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + char_w + ofs_x - 1, ofs_y), Size2i(1, get_row_height())), border_color);
  1111. }
  1112. if (highlight_all_occurrences && !only_whitespaces_highlighted) {
  1113. if (highlighted_text_col != -1) {
  1114. // If we are at the end check for new word on same line.
  1115. if (j > highlighted_text_col + highlighted_text.length()) {
  1116. highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, j);
  1117. }
  1118. bool in_highlighted_word = (j >= highlighted_text_col && j < highlighted_text_col + highlighted_text.length());
  1119. // If this is the original highlighted text we don't want to highlight it again.
  1120. if (cursor.line == line && cursor_wrap_index == line_wrap_index && (cursor.column >= highlighted_text_col && cursor.column <= highlighted_text_col + highlighted_text.length())) {
  1121. in_highlighted_word = false;
  1122. }
  1123. if (in_highlighted_word) {
  1124. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y), Size2i(char_w, get_row_height())), cache.word_highlighted_color);
  1125. }
  1126. }
  1127. }
  1128. if (highlighted_word_col != -1) {
  1129. if (j + last_wrap_column > highlighted_word_col + highlighted_word.length()) {
  1130. highlighted_word_col = _get_column_pos_of_word(highlighted_word, fullstr, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, j + last_wrap_column);
  1131. }
  1132. underlined = (j + last_wrap_column >= highlighted_word_col && j + last_wrap_column < highlighted_word_col + highlighted_word.length());
  1133. }
  1134. if (brace_matching_enabled) {
  1135. int yofs = ofs_y + (get_row_height() - cache.font->get_height()) / 2;
  1136. if ((brace_open_match_line == line && brace_open_match_column == last_wrap_column + j) ||
  1137. (cursor.column == last_wrap_column + j && cursor.line == line && cursor_wrap_index == line_wrap_index && (brace_open_matching || brace_open_mismatch))) {
  1138. if (brace_open_mismatch)
  1139. color = cache.brace_mismatch_color;
  1140. drawer.draw_char(ci, Point2i(char_ofs + char_margin + ofs_x, yofs + ascent), '_', str[j + 1], in_selection && override_selected_font_color ? cache.font_color_selected : color);
  1141. }
  1142. if ((brace_close_match_line == line && brace_close_match_column == last_wrap_column + j) ||
  1143. (cursor.column == last_wrap_column + j + 1 && cursor.line == line && cursor_wrap_index == line_wrap_index && (brace_close_matching || brace_close_mismatch))) {
  1144. if (brace_close_mismatch)
  1145. color = cache.brace_mismatch_color;
  1146. drawer.draw_char(ci, Point2i(char_ofs + char_margin + ofs_x, yofs + ascent), '_', str[j + 1], in_selection && override_selected_font_color ? cache.font_color_selected : color);
  1147. }
  1148. }
  1149. if (cursor.column == last_wrap_column + j && cursor.line == line && cursor_wrap_index == line_wrap_index) {
  1150. cursor_pos = Point2i(char_ofs + char_margin + ofs_x, ofs_y);
  1151. cursor_pos.y += (get_row_height() - cache.font->get_height()) / 2;
  1152. if (insert_mode) {
  1153. cursor_insert_offset_y = (cache.font->get_height() - 3);
  1154. cursor_pos.y += cursor_insert_offset_y;
  1155. }
  1156. int caret_w = (str[j] == '\t') ? cache.font->get_char_size(' ').width : char_w;
  1157. if (ime_text.length() > 0) {
  1158. int ofs = 0;
  1159. while (true) {
  1160. if (ofs >= ime_text.length())
  1161. break;
  1162. CharType cchar = ime_text[ofs];
  1163. CharType next = ime_text[ofs + 1];
  1164. int im_char_width = cache.font->get_char_size(cchar, next).width;
  1165. if ((char_ofs + char_margin + im_char_width) >= xmargin_end)
  1166. break;
  1167. bool selected = ofs >= ime_selection.x && ofs < ime_selection.x + ime_selection.y;
  1168. if (selected) {
  1169. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_ofs + char_margin, ofs_y + get_row_height()), Size2(im_char_width, 3)), color);
  1170. } else {
  1171. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_ofs + char_margin, ofs_y + get_row_height()), Size2(im_char_width, 1)), color);
  1172. }
  1173. drawer.draw_char(ci, Point2(char_ofs + char_margin + ofs_x, ofs_y + ascent), cchar, next, color);
  1174. char_ofs += im_char_width;
  1175. ofs++;
  1176. }
  1177. }
  1178. if (ime_text.length() == 0) {
  1179. if (draw_caret) {
  1180. if (insert_mode) {
  1181. #ifdef TOOLS_ENABLED
  1182. int caret_h = (block_caret) ? 4 : 2 * EDSCALE;
  1183. #else
  1184. int caret_h = (block_caret) ? 4 : 2;
  1185. #endif
  1186. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, caret_h)), cache.caret_color);
  1187. } else {
  1188. #ifdef TOOLS_ENABLED
  1189. caret_w = (block_caret) ? caret_w : 2 * EDSCALE;
  1190. #else
  1191. caret_w = (block_caret) ? caret_w : 2;
  1192. #endif
  1193. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, cache.font->get_height())), cache.caret_color);
  1194. }
  1195. }
  1196. }
  1197. }
  1198. if (cursor.column == last_wrap_column + j && cursor.line == line && cursor_wrap_index == line_wrap_index && block_caret && draw_caret && !insert_mode) {
  1199. color = cache.caret_background_color;
  1200. } else if (!syntax_coloring && block_caret) {
  1201. color = readonly ? cache.font_color_readonly : cache.font_color;
  1202. }
  1203. if (str[j] >= 32) {
  1204. int yofs = ofs_y + (get_row_height() - cache.font->get_height()) / 2;
  1205. int w = drawer.draw_char(ci, Point2i(char_ofs + char_margin + ofs_x, yofs + ascent), str[j], str[j + 1], in_selection && override_selected_font_color ? cache.font_color_selected : color);
  1206. if (underlined) {
  1207. float line_width = 1.0;
  1208. #ifdef TOOLS_ENABLED
  1209. line_width *= EDSCALE;
  1210. #endif
  1211. draw_rect(Rect2(char_ofs + char_margin + ofs_x, yofs + ascent + 2, w, line_width), in_selection && override_selected_font_color ? cache.font_color_selected : color);
  1212. }
  1213. } else if (draw_tabs && str[j] == '\t') {
  1214. int yofs = (get_row_height() - cache.tab_icon->get_height()) / 2;
  1215. cache.tab_icon->draw(ci, Point2(char_ofs + char_margin + ofs_x, ofs_y + yofs), in_selection && override_selected_font_color ? cache.font_color_selected : color);
  1216. }
  1217. if (draw_spaces && str[j] == ' ') {
  1218. int yofs = (get_row_height() - cache.space_icon->get_height()) / 2;
  1219. cache.space_icon->draw(ci, Point2(char_ofs + char_margin + ofs_x, ofs_y + yofs), in_selection && override_selected_font_color ? cache.font_color_selected : color);
  1220. }
  1221. char_ofs += char_w;
  1222. if (line_wrap_index == line_wrap_amount && j == str.length() - 1 && is_folded(line)) {
  1223. int yofs = (get_row_height() - cache.folded_eol_icon->get_height()) / 2;
  1224. int xofs = cache.folded_eol_icon->get_width() / 2;
  1225. Color eol_color = cache.code_folding_color;
  1226. eol_color.a = 1;
  1227. cache.folded_eol_icon->draw(ci, Point2(char_ofs + char_margin + xofs + ofs_x, ofs_y + yofs), eol_color);
  1228. }
  1229. }
  1230. if (cursor.column == (last_wrap_column + j) && cursor.line == line && cursor_wrap_index == line_wrap_index && (char_ofs + char_margin) >= xmargin_beg) {
  1231. cursor_pos = Point2i(char_ofs + char_margin + ofs_x, ofs_y);
  1232. cursor_pos.y += (get_row_height() - cache.font->get_height()) / 2;
  1233. if (insert_mode) {
  1234. cursor_insert_offset_y = cache.font->get_height() - 3;
  1235. cursor_pos.y += cursor_insert_offset_y;
  1236. }
  1237. if (ime_text.length() > 0) {
  1238. int ofs = 0;
  1239. while (true) {
  1240. if (ofs >= ime_text.length())
  1241. break;
  1242. CharType cchar = ime_text[ofs];
  1243. CharType next = ime_text[ofs + 1];
  1244. int im_char_width = cache.font->get_char_size(cchar, next).width;
  1245. if ((char_ofs + char_margin + im_char_width) >= xmargin_end)
  1246. break;
  1247. bool selected = ofs >= ime_selection.x && ofs < ime_selection.x + ime_selection.y;
  1248. if (selected) {
  1249. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_ofs + char_margin, ofs_y + get_row_height()), Size2(im_char_width, 3)), color);
  1250. } else {
  1251. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_ofs + char_margin, ofs_y + get_row_height()), Size2(im_char_width, 1)), color);
  1252. }
  1253. drawer.draw_char(ci, Point2(char_ofs + char_margin + ofs_x, ofs_y + ascent), cchar, next, color);
  1254. char_ofs += im_char_width;
  1255. ofs++;
  1256. }
  1257. }
  1258. if (ime_text.length() == 0) {
  1259. if (draw_caret) {
  1260. if (insert_mode) {
  1261. int char_w = cache.font->get_char_size(' ').width;
  1262. #ifdef TOOLS_ENABLED
  1263. int caret_h = (block_caret) ? 4 : 2 * EDSCALE;
  1264. #else
  1265. int caret_h = (block_caret) ? 4 : 2;
  1266. #endif
  1267. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(char_w, caret_h)), cache.caret_color);
  1268. } else {
  1269. int char_w = cache.font->get_char_size(' ').width;
  1270. #ifdef TOOLS_ENABLED
  1271. int caret_w = (block_caret) ? char_w : 2 * EDSCALE;
  1272. #else
  1273. int caret_w = (block_caret) ? char_w : 2;
  1274. #endif
  1275. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, cache.font->get_height())), cache.caret_color);
  1276. }
  1277. }
  1278. }
  1279. }
  1280. }
  1281. }
  1282. bool completion_below = false;
  1283. if (completion_active) {
  1284. // Code completion box.
  1285. Ref<StyleBox> csb = get_stylebox("completion");
  1286. int maxlines = get_constant("completion_lines");
  1287. int cmax_width = get_constant("completion_max_width") * cache.font->get_char_size('x').x;
  1288. int scrollw = get_constant("completion_scroll_width");
  1289. Color scrollc = get_color("completion_scroll_color");
  1290. int lines = MIN(completion_options.size(), maxlines);
  1291. int w = 0;
  1292. int h = lines * get_row_height();
  1293. int nofs = cache.font->get_string_size(completion_base).width;
  1294. if (completion_options.size() < 50) {
  1295. for (int i = 0; i < completion_options.size(); i++) {
  1296. int w2 = MIN(cache.font->get_string_size(completion_options[i].display).x, cmax_width);
  1297. if (w2 > w)
  1298. w = w2;
  1299. }
  1300. } else {
  1301. w = cmax_width;
  1302. }
  1303. // Add space for completion icons.
  1304. const int icon_hsep = get_constant("hseparation", "ItemList");
  1305. Size2 icon_area_size(get_row_height(), get_row_height());
  1306. w += icon_area_size.width + icon_hsep;
  1307. int th = h + csb->get_minimum_size().y;
  1308. if (cursor_pos.y + get_row_height() + th > get_size().height) {
  1309. completion_rect.position.y = cursor_pos.y - th - (cache.line_spacing / 2.0f) - cursor_insert_offset_y;
  1310. } else {
  1311. completion_rect.position.y = cursor_pos.y + cache.font->get_height() + (cache.line_spacing / 2.0f) + csb->get_offset().y - cursor_insert_offset_y;
  1312. completion_below = true;
  1313. }
  1314. if (cursor_pos.x - nofs + w + scrollw > get_size().width) {
  1315. completion_rect.position.x = get_size().width - w - scrollw;
  1316. } else {
  1317. completion_rect.position.x = cursor_pos.x - nofs;
  1318. }
  1319. completion_rect.size.width = w + 2;
  1320. completion_rect.size.height = h;
  1321. if (completion_options.size() <= maxlines)
  1322. scrollw = 0;
  1323. draw_style_box(csb, Rect2(completion_rect.position - csb->get_offset(), completion_rect.size + csb->get_minimum_size() + Size2(scrollw, 0)));
  1324. if (cache.completion_background_color.a > 0.01) {
  1325. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(completion_rect.position, completion_rect.size + Size2(scrollw, 0)), cache.completion_background_color);
  1326. }
  1327. int line_from = CLAMP(completion_index - lines / 2, 0, completion_options.size() - lines);
  1328. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(completion_rect.position.x, completion_rect.position.y + (completion_index - line_from) * get_row_height()), Size2(completion_rect.size.width, get_row_height())), cache.completion_selected_color);
  1329. draw_rect(Rect2(completion_rect.position + Vector2(icon_area_size.x + icon_hsep, 0), Size2(MIN(nofs, completion_rect.size.width - (icon_area_size.x + icon_hsep)), completion_rect.size.height)), cache.completion_existing_color);
  1330. for (int i = 0; i < lines; i++) {
  1331. int l = line_from + i;
  1332. ERR_CONTINUE(l < 0 || l >= completion_options.size());
  1333. Color text_color = cache.completion_font_color;
  1334. for (int j = 0; j < color_regions.size(); j++) {
  1335. if (completion_options[l].insert_text.begins_with(color_regions[j].begin_key)) {
  1336. text_color = color_regions[j].color;
  1337. }
  1338. }
  1339. int yofs = (get_row_height() - cache.font->get_height()) / 2;
  1340. Point2 title_pos(completion_rect.position.x, completion_rect.position.y + i * get_row_height() + cache.font->get_ascent() + yofs);
  1341. // Draw completion icon if it is valid.
  1342. Ref<Texture2D> icon = completion_options[l].icon;
  1343. Rect2 icon_area(completion_rect.position.x, completion_rect.position.y + i * get_row_height(), icon_area_size.width, icon_area_size.height);
  1344. if (icon.is_valid()) {
  1345. const real_t max_scale = 0.7f;
  1346. const real_t side = max_scale * icon_area.size.width;
  1347. real_t scale = MIN(side / icon->get_width(), side / icon->get_height());
  1348. Size2 icon_size = icon->get_size() * scale;
  1349. draw_texture_rect(icon, Rect2(icon_area.position + (icon_area.size - icon_size) / 2, icon_size));
  1350. }
  1351. title_pos.x = icon_area.position.x + icon_area.size.width + icon_hsep;
  1352. draw_string(cache.font, title_pos, completion_options[l].display, text_color, completion_rect.size.width - (icon_area_size.x + icon_hsep));
  1353. }
  1354. if (scrollw) {
  1355. // Draw a small scroll rectangle to show a position in the options.
  1356. float r = maxlines / (float)completion_options.size();
  1357. float o = line_from / (float)completion_options.size();
  1358. draw_rect(Rect2(completion_rect.position.x + completion_rect.size.width, completion_rect.position.y + o * completion_rect.size.y, scrollw, completion_rect.size.y * r), scrollc);
  1359. }
  1360. completion_line_ofs = line_from;
  1361. }
  1362. // Check to see if the hint should be drawn.
  1363. bool show_hint = false;
  1364. if (completion_hint != "") {
  1365. if (completion_active) {
  1366. if (completion_below && !callhint_below) {
  1367. show_hint = true;
  1368. } else if (!completion_below && callhint_below) {
  1369. show_hint = true;
  1370. }
  1371. } else {
  1372. show_hint = true;
  1373. }
  1374. }
  1375. if (show_hint) {
  1376. Ref<StyleBox> sb = get_stylebox("panel", "TooltipPanel");
  1377. Ref<Font> font = cache.font;
  1378. Color font_color = get_color("font_color", "TooltipLabel");
  1379. int max_w = 0;
  1380. int sc = completion_hint.get_slice_count("\n");
  1381. int offset = 0;
  1382. int spacing = 0;
  1383. for (int i = 0; i < sc; i++) {
  1384. String l = completion_hint.get_slice("\n", i);
  1385. int len = font->get_string_size(l).x;
  1386. max_w = MAX(len, max_w);
  1387. if (i == 0) {
  1388. offset = font->get_string_size(l.substr(0, l.find(String::chr(0xFFFF)))).x;
  1389. } else {
  1390. spacing += cache.line_spacing;
  1391. }
  1392. }
  1393. Size2 size2 = Size2(max_w, sc * font->get_height() + spacing);
  1394. Size2 minsize = size2 + sb->get_minimum_size();
  1395. if (completion_hint_offset == -0xFFFF) {
  1396. completion_hint_offset = cursor_pos.x - offset;
  1397. }
  1398. Point2 hint_ofs = Vector2(completion_hint_offset, cursor_pos.y) + callhint_offset;
  1399. if (callhint_below) {
  1400. hint_ofs.y += get_row_height() + sb->get_offset().y;
  1401. } else {
  1402. hint_ofs.y -= minsize.y + sb->get_offset().y;
  1403. }
  1404. draw_style_box(sb, Rect2(hint_ofs, minsize));
  1405. spacing = 0;
  1406. for (int i = 0; i < sc; i++) {
  1407. int begin = 0;
  1408. int end = 0;
  1409. String l = completion_hint.get_slice("\n", i);
  1410. if (l.find(String::chr(0xFFFF)) != -1) {
  1411. begin = font->get_string_size(l.substr(0, l.find(String::chr(0xFFFF)))).x;
  1412. end = font->get_string_size(l.substr(0, l.rfind(String::chr(0xFFFF)))).x;
  1413. }
  1414. Point2 round_ofs = hint_ofs + sb->get_offset() + Vector2(0, font->get_ascent() + font->get_height() * i + spacing);
  1415. round_ofs = round_ofs.round();
  1416. draw_string(font, round_ofs, l.replace(String::chr(0xFFFF), ""), font_color);
  1417. if (end > 0) {
  1418. Vector2 b = hint_ofs + sb->get_offset() + Vector2(begin, font->get_height() + font->get_height() * i + spacing - 1);
  1419. draw_line(b, b + Vector2(end - begin, 0), font_color);
  1420. }
  1421. spacing += cache.line_spacing;
  1422. }
  1423. }
  1424. if (has_focus()) {
  1425. OS::get_singleton()->set_ime_active(true);
  1426. OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos + Point2(0, get_row_height()));
  1427. }
  1428. } break;
  1429. case NOTIFICATION_FOCUS_ENTER: {
  1430. if (caret_blink_enabled) {
  1431. caret_blink_timer->start();
  1432. } else {
  1433. draw_caret = true;
  1434. }
  1435. OS::get_singleton()->set_ime_active(true);
  1436. Point2 cursor_pos = Point2(cursor_get_column(), cursor_get_line()) * get_row_height();
  1437. OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos);
  1438. if (OS::get_singleton()->has_virtual_keyboard())
  1439. OS::get_singleton()->show_virtual_keyboard(get_text(), get_global_rect());
  1440. } break;
  1441. case NOTIFICATION_FOCUS_EXIT: {
  1442. if (caret_blink_enabled) {
  1443. caret_blink_timer->stop();
  1444. }
  1445. OS::get_singleton()->set_ime_position(Point2());
  1446. OS::get_singleton()->set_ime_active(false);
  1447. ime_text = "";
  1448. ime_selection = Point2();
  1449. if (OS::get_singleton()->has_virtual_keyboard())
  1450. OS::get_singleton()->hide_virtual_keyboard();
  1451. } break;
  1452. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  1453. if (has_focus()) {
  1454. ime_text = OS::get_singleton()->get_ime_text();
  1455. ime_selection = OS::get_singleton()->get_ime_selection();
  1456. update();
  1457. }
  1458. } break;
  1459. }
  1460. }
  1461. void TextEdit::_consume_pair_symbol(CharType ch) {
  1462. int cursor_position_to_move = cursor_get_column() + 1;
  1463. CharType ch_single[2] = { ch, 0 };
  1464. CharType ch_single_pair[2] = { _get_right_pair_symbol(ch), 0 };
  1465. CharType ch_pair[3] = { ch, _get_right_pair_symbol(ch), 0 };
  1466. if (is_selection_active()) {
  1467. int new_column, new_line;
  1468. begin_complex_operation();
  1469. _insert_text(get_selection_from_line(), get_selection_from_column(),
  1470. ch_single,
  1471. &new_line, &new_column);
  1472. int to_col_offset = 0;
  1473. if (get_selection_from_line() == get_selection_to_line())
  1474. to_col_offset = 1;
  1475. _insert_text(get_selection_to_line(),
  1476. get_selection_to_column() + to_col_offset,
  1477. ch_single_pair,
  1478. &new_line, &new_column);
  1479. end_complex_operation();
  1480. cursor_set_line(get_selection_to_line());
  1481. cursor_set_column(get_selection_to_column() + to_col_offset);
  1482. deselect();
  1483. update();
  1484. return;
  1485. }
  1486. if ((ch == '\'' || ch == '"') &&
  1487. cursor_get_column() > 0 && _is_text_char(text[cursor.line][cursor_get_column() - 1]) && !_is_pair_right_symbol(text[cursor.line][cursor_get_column()])) {
  1488. insert_text_at_cursor(ch_single);
  1489. cursor_set_column(cursor_position_to_move);
  1490. return;
  1491. }
  1492. if (cursor_get_column() < text[cursor.line].length()) {
  1493. if (_is_text_char(text[cursor.line][cursor_get_column()])) {
  1494. insert_text_at_cursor(ch_single);
  1495. cursor_set_column(cursor_position_to_move);
  1496. return;
  1497. }
  1498. if (_is_pair_right_symbol(ch) &&
  1499. text[cursor.line][cursor_get_column()] == ch) {
  1500. cursor_set_column(cursor_position_to_move);
  1501. return;
  1502. }
  1503. }
  1504. String line = text[cursor.line];
  1505. bool in_single_quote = false;
  1506. bool in_double_quote = false;
  1507. bool found_comment = false;
  1508. int c = 0;
  1509. while (c < line.length()) {
  1510. if (line[c] == '\\') {
  1511. c++; // Skip quoted anything.
  1512. if (cursor.column == c) {
  1513. break;
  1514. }
  1515. } else if (!in_single_quote && !in_double_quote && line[c] == '#') {
  1516. found_comment = true;
  1517. break;
  1518. } else {
  1519. if (line[c] == '\'' && !in_double_quote) {
  1520. in_single_quote = !in_single_quote;
  1521. } else if (line[c] == '"' && !in_single_quote) {
  1522. in_double_quote = !in_double_quote;
  1523. }
  1524. }
  1525. c++;
  1526. if (cursor.column == c) {
  1527. break;
  1528. }
  1529. }
  1530. // Do not need to duplicate quotes while in comments
  1531. if (found_comment) {
  1532. insert_text_at_cursor(ch_single);
  1533. cursor_set_column(cursor_position_to_move);
  1534. return;
  1535. }
  1536. // Disallow inserting duplicated quotes while already in string
  1537. if ((in_single_quote || in_double_quote) && (ch == '"' || ch == '\'')) {
  1538. insert_text_at_cursor(ch_single);
  1539. cursor_set_column(cursor_position_to_move);
  1540. return;
  1541. }
  1542. insert_text_at_cursor(ch_pair);
  1543. cursor_set_column(cursor_position_to_move);
  1544. }
  1545. void TextEdit::_consume_backspace_for_pair_symbol(int prev_line, int prev_column) {
  1546. bool remove_right_symbol = false;
  1547. if (cursor.column < text[cursor.line].length() && cursor.column > 0) {
  1548. CharType left_char = text[cursor.line][cursor.column - 1];
  1549. CharType right_char = text[cursor.line][cursor.column];
  1550. if (right_char == _get_right_pair_symbol(left_char)) {
  1551. remove_right_symbol = true;
  1552. }
  1553. }
  1554. if (remove_right_symbol) {
  1555. _remove_text(prev_line, prev_column, cursor.line, cursor.column + 1);
  1556. } else {
  1557. _remove_text(prev_line, prev_column, cursor.line, cursor.column);
  1558. }
  1559. }
  1560. void TextEdit::backspace_at_cursor() {
  1561. if (readonly)
  1562. return;
  1563. if (cursor.column == 0 && cursor.line == 0)
  1564. return;
  1565. int prev_line = cursor.column ? cursor.line : cursor.line - 1;
  1566. int prev_column = cursor.column ? (cursor.column - 1) : (text[cursor.line - 1].length());
  1567. if (is_line_hidden(cursor.line))
  1568. set_line_as_hidden(prev_line, true);
  1569. if (is_line_set_as_breakpoint(cursor.line)) {
  1570. if (!text.is_breakpoint(prev_line))
  1571. emit_signal("breakpoint_toggled", prev_line);
  1572. set_line_as_breakpoint(prev_line, true);
  1573. }
  1574. if (text.has_info_icon(cursor.line)) {
  1575. set_line_info_icon(prev_line, text.get_info_icon(cursor.line), text.get_info(cursor.line));
  1576. }
  1577. if (auto_brace_completion_enabled &&
  1578. cursor.column > 0 &&
  1579. _is_pair_left_symbol(text[cursor.line][cursor.column - 1])) {
  1580. _consume_backspace_for_pair_symbol(prev_line, prev_column);
  1581. } else {
  1582. // Handle space indentation.
  1583. if (cursor.column != 0 && indent_using_spaces) {
  1584. // Check if there are no other chars before cursor, just indentation.
  1585. bool unindent = true;
  1586. int i = 0;
  1587. while (i < cursor.column && i < text[cursor.line].length()) {
  1588. if (!_is_whitespace(text[cursor.line][i])) {
  1589. unindent = false;
  1590. break;
  1591. }
  1592. i++;
  1593. }
  1594. // Then we can remove all spaces as a single character.
  1595. if (unindent) {
  1596. // We want to remove spaces up to closest indent, or whole indent if cursor is pointing at it.
  1597. int spaces_to_delete = _calculate_spaces_till_next_left_indent(cursor.column);
  1598. prev_column = cursor.column - spaces_to_delete;
  1599. _remove_text(cursor.line, prev_column, cursor.line, cursor.column);
  1600. } else {
  1601. _remove_text(prev_line, prev_column, cursor.line, cursor.column);
  1602. }
  1603. } else {
  1604. _remove_text(prev_line, prev_column, cursor.line, cursor.column);
  1605. }
  1606. }
  1607. cursor_set_line(prev_line, true, true);
  1608. cursor_set_column(prev_column);
  1609. }
  1610. void TextEdit::indent_right() {
  1611. int start_line;
  1612. int end_line;
  1613. // This value informs us by how much we changed selection position by indenting right.
  1614. // Default is 1 for tab indentation.
  1615. int selection_offset = 1;
  1616. begin_complex_operation();
  1617. if (is_selection_active()) {
  1618. start_line = get_selection_from_line();
  1619. end_line = get_selection_to_line();
  1620. } else {
  1621. start_line = cursor.line;
  1622. end_line = start_line;
  1623. }
  1624. // Ignore if the cursor is not past the first column.
  1625. if (is_selection_active() && get_selection_to_column() == 0) {
  1626. selection_offset = 0;
  1627. end_line--;
  1628. }
  1629. for (int i = start_line; i <= end_line; i++) {
  1630. String line_text = get_line(i);
  1631. if (indent_using_spaces) {
  1632. // We don't really care where selection is - we just need to know indentation level at the beginning of the line.
  1633. int left = _find_first_non_whitespace_column_of_line(line_text);
  1634. int spaces_to_add = _calculate_spaces_till_next_right_indent(left);
  1635. // Since we will add this much spaces we want move whole selection and cursor by this much.
  1636. selection_offset = spaces_to_add;
  1637. for (int j = 0; j < spaces_to_add; j++)
  1638. line_text = ' ' + line_text;
  1639. } else {
  1640. line_text = '\t' + line_text;
  1641. }
  1642. set_line(i, line_text);
  1643. }
  1644. // Fix selection and cursor being off after shifting selection right.
  1645. if (is_selection_active()) {
  1646. select(selection.from_line, selection.from_column + selection_offset, selection.to_line, selection.to_column + selection_offset);
  1647. }
  1648. cursor_set_column(cursor.column + selection_offset, false);
  1649. end_complex_operation();
  1650. update();
  1651. }
  1652. void TextEdit::indent_left() {
  1653. int start_line;
  1654. int end_line;
  1655. // Moving cursor and selection after unindenting can get tricky because
  1656. // changing content of line can move cursor and selection on it's own (if new line ends before previous position of either),
  1657. // therefore we just remember initial values and at the end of the operation offset them by number of removed characters.
  1658. int removed_characters = 0;
  1659. int initial_selection_end_column = selection.to_column;
  1660. int initial_cursor_column = cursor.column;
  1661. begin_complex_operation();
  1662. if (is_selection_active()) {
  1663. start_line = get_selection_from_line();
  1664. end_line = get_selection_to_line();
  1665. } else {
  1666. start_line = cursor.line;
  1667. end_line = start_line;
  1668. }
  1669. // Ignore if the cursor is not past the first column.
  1670. if (is_selection_active() && get_selection_to_column() == 0) {
  1671. end_line--;
  1672. }
  1673. String last_line_text = get_line(end_line);
  1674. for (int i = start_line; i <= end_line; i++) {
  1675. String line_text = get_line(i);
  1676. if (line_text.begins_with("\t")) {
  1677. line_text = line_text.substr(1, line_text.length());
  1678. set_line(i, line_text);
  1679. removed_characters = 1;
  1680. } else if (line_text.begins_with(" ")) {
  1681. // When unindenting we aim to remove spaces before line that has selection no matter what is selected,
  1682. // so we start of by finding first non whitespace character of line
  1683. int left = _find_first_non_whitespace_column_of_line(line_text);
  1684. // Here we remove only enough spaces to align text to nearest full multiple of indentation_size.
  1685. // In case where selection begins at the start of indentation_size multiple we remove whole indentation level.
  1686. int spaces_to_remove = _calculate_spaces_till_next_left_indent(left);
  1687. line_text = line_text.substr(spaces_to_remove, line_text.length());
  1688. set_line(i, line_text);
  1689. removed_characters = spaces_to_remove;
  1690. }
  1691. }
  1692. // Fix selection and cursor being off by one on the last line.
  1693. if (is_selection_active() && last_line_text != get_line(end_line)) {
  1694. select(selection.from_line, selection.from_column - removed_characters,
  1695. selection.to_line, initial_selection_end_column - removed_characters);
  1696. }
  1697. cursor_set_column(initial_cursor_column - removed_characters, false);
  1698. end_complex_operation();
  1699. update();
  1700. }
  1701. int TextEdit::_calculate_spaces_till_next_left_indent(int column) {
  1702. int spaces_till_indent = column % indent_size;
  1703. if (spaces_till_indent == 0)
  1704. spaces_till_indent = indent_size;
  1705. return spaces_till_indent;
  1706. }
  1707. int TextEdit::_calculate_spaces_till_next_right_indent(int column) {
  1708. return indent_size - column % indent_size;
  1709. }
  1710. void TextEdit::_get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) const {
  1711. float rows = p_mouse.y;
  1712. rows -= cache.style_normal->get_margin(MARGIN_TOP);
  1713. rows /= get_row_height();
  1714. rows += get_v_scroll_offset();
  1715. int first_vis_line = get_first_visible_line();
  1716. int row = first_vis_line + Math::floor(rows);
  1717. int wrap_index = 0;
  1718. if (is_wrap_enabled() || is_hiding_enabled()) {
  1719. int f_ofs = num_lines_from_rows(first_vis_line, cursor.wrap_ofs, rows + (1 * SGN(rows)), wrap_index) - 1;
  1720. if (rows < 0)
  1721. row = first_vis_line - f_ofs;
  1722. else
  1723. row = first_vis_line + f_ofs;
  1724. }
  1725. if (row < 0)
  1726. row = 0; // TODO.
  1727. int col = 0;
  1728. if (row >= text.size()) {
  1729. row = text.size() - 1;
  1730. col = text[row].size();
  1731. } else {
  1732. int colx = p_mouse.x - (cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width + cache.info_gutter_width);
  1733. colx += cursor.x_ofs;
  1734. col = get_char_pos_for_line(colx, row, wrap_index);
  1735. if (is_wrap_enabled() && wrap_index < times_line_wraps(row)) {
  1736. // Move back one if we are at the end of the row.
  1737. Vector<String> rows2 = get_wrap_rows_text(row);
  1738. int row_end_col = 0;
  1739. for (int i = 0; i < wrap_index + 1; i++) {
  1740. row_end_col += rows2[i].length();
  1741. }
  1742. if (col >= row_end_col)
  1743. col -= 1;
  1744. }
  1745. }
  1746. r_row = row;
  1747. r_col = col;
  1748. }
  1749. Vector2i TextEdit::_get_cursor_pixel_pos() {
  1750. adjust_viewport_to_cursor();
  1751. int row = (cursor.line - get_first_visible_line() - cursor.wrap_ofs);
  1752. // Correct for hidden and wrapped lines
  1753. for (int i = get_first_visible_line(); i < cursor.line; i++) {
  1754. if (is_line_hidden(i)) {
  1755. row -= 1;
  1756. continue;
  1757. }
  1758. row += times_line_wraps(i);
  1759. }
  1760. // Row might be wrapped. Adjust row and r_column
  1761. Vector<String> rows2 = get_wrap_rows_text(cursor.line);
  1762. while (rows2.size() > 1) {
  1763. if (cursor.column >= rows2[0].length()) {
  1764. cursor.column -= rows2[0].length();
  1765. rows2.remove(0);
  1766. row++;
  1767. } else {
  1768. break;
  1769. }
  1770. }
  1771. // Calculate final pixel position
  1772. int y = (row - get_v_scroll_offset() + 1 /*Bottom of line*/) * get_row_height();
  1773. int x = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width + cache.info_gutter_width - cursor.x_ofs;
  1774. int ix = 0;
  1775. while (ix < rows2[0].size() && ix < cursor.column) {
  1776. if (cache.font != NULL) {
  1777. x += cache.font->get_char_size(rows2[0].get(ix)).width;
  1778. }
  1779. ix++;
  1780. }
  1781. x += get_indent_level(cursor.line) * cache.font->get_char_size(' ').width;
  1782. return Vector2i(x, y);
  1783. }
  1784. void TextEdit::_get_minimap_mouse_row(const Point2i &p_mouse, int &r_row) const {
  1785. float rows = p_mouse.y;
  1786. rows -= cache.style_normal->get_margin(MARGIN_TOP);
  1787. rows /= (minimap_char_size.y + minimap_line_spacing);
  1788. rows += get_v_scroll_offset();
  1789. // calculate visible lines
  1790. int minimap_visible_lines = _get_minimap_visible_rows();
  1791. int visible_rows = get_visible_rows() + 1;
  1792. int first_visible_line = get_first_visible_line() - 1;
  1793. int draw_amount = visible_rows + (smooth_scroll_enabled ? 1 : 0);
  1794. draw_amount += times_line_wraps(first_visible_line + 1);
  1795. int minimap_line_height = (minimap_char_size.y + minimap_line_spacing);
  1796. // calculate viewport size and y offset
  1797. int viewport_height = (draw_amount - 1) * minimap_line_height;
  1798. int control_height = _get_control_height() - viewport_height;
  1799. int viewport_offset_y = round(get_scroll_pos_for_line(first_visible_line) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount));
  1800. // calculate the first line.
  1801. int num_lines_before = round((viewport_offset_y) / minimap_line_height);
  1802. int wi;
  1803. int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_visible_line;
  1804. if (first_visible_line > 0 && minimap_line >= 0) {
  1805. minimap_line -= num_lines_from_rows(first_visible_line, 0, -num_lines_before, wi);
  1806. minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0);
  1807. } else {
  1808. minimap_line = 0;
  1809. }
  1810. int row = minimap_line + Math::floor(rows);
  1811. int wrap_index = 0;
  1812. if (is_wrap_enabled() || is_hiding_enabled()) {
  1813. int f_ofs = num_lines_from_rows(minimap_line, cursor.wrap_ofs, rows + (1 * SGN(rows)), wrap_index) - 1;
  1814. if (rows < 0) {
  1815. row = minimap_line - f_ofs;
  1816. } else {
  1817. row = minimap_line + f_ofs;
  1818. }
  1819. }
  1820. if (row < 0) {
  1821. row = 0;
  1822. }
  1823. if (row >= text.size()) {
  1824. row = text.size() - 1;
  1825. }
  1826. r_row = row;
  1827. }
  1828. void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
  1829. double prev_v_scroll = v_scroll->get_value();
  1830. double prev_h_scroll = h_scroll->get_value();
  1831. Ref<InputEventMouseButton> mb = p_gui_input;
  1832. if (mb.is_valid()) {
  1833. if (completion_active && completion_rect.has_point(mb->get_position())) {
  1834. if (!mb->is_pressed())
  1835. return;
  1836. if (mb->get_button_index() == BUTTON_WHEEL_UP) {
  1837. if (completion_index > 0) {
  1838. completion_index--;
  1839. completion_current = completion_options[completion_index];
  1840. update();
  1841. }
  1842. }
  1843. if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  1844. if (completion_index < completion_options.size() - 1) {
  1845. completion_index++;
  1846. completion_current = completion_options[completion_index];
  1847. update();
  1848. }
  1849. }
  1850. if (mb->get_button_index() == BUTTON_LEFT) {
  1851. completion_index = CLAMP(completion_line_ofs + (mb->get_position().y - completion_rect.position.y) / get_row_height(), 0, completion_options.size() - 1);
  1852. completion_current = completion_options[completion_index];
  1853. update();
  1854. if (mb->is_doubleclick())
  1855. _confirm_completion();
  1856. }
  1857. return;
  1858. } else {
  1859. _cancel_completion();
  1860. _cancel_code_hint();
  1861. }
  1862. if (mb->is_pressed()) {
  1863. if (mb->get_button_index() == BUTTON_WHEEL_UP && !mb->get_command()) {
  1864. if (mb->get_shift()) {
  1865. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1866. } else {
  1867. _scroll_up(3 * mb->get_factor());
  1868. }
  1869. }
  1870. if (mb->get_button_index() == BUTTON_WHEEL_DOWN && !mb->get_command()) {
  1871. if (mb->get_shift()) {
  1872. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1873. } else {
  1874. _scroll_down(3 * mb->get_factor());
  1875. }
  1876. }
  1877. if (mb->get_button_index() == BUTTON_WHEEL_LEFT) {
  1878. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1879. }
  1880. if (mb->get_button_index() == BUTTON_WHEEL_RIGHT) {
  1881. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1882. }
  1883. if (mb->get_button_index() == BUTTON_LEFT) {
  1884. _reset_caret_blink_timer();
  1885. int row, col;
  1886. _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col);
  1887. // Toggle breakpoint on gutter click.
  1888. if (draw_breakpoint_gutter) {
  1889. int gutter = cache.style_normal->get_margin(MARGIN_LEFT);
  1890. if (mb->get_position().x > gutter - 6 && mb->get_position().x <= gutter + cache.breakpoint_gutter_width - 3) {
  1891. set_line_as_breakpoint(row, !is_line_set_as_breakpoint(row));
  1892. emit_signal("breakpoint_toggled", row);
  1893. return;
  1894. }
  1895. }
  1896. // Emit info clicked.
  1897. if (draw_info_gutter && text.has_info_icon(row)) {
  1898. int left_margin = cache.style_normal->get_margin(MARGIN_LEFT);
  1899. int gutter_left = left_margin + cache.breakpoint_gutter_width;
  1900. if (mb->get_position().x > gutter_left - 6 && mb->get_position().x <= gutter_left + cache.info_gutter_width - 3) {
  1901. emit_signal("info_clicked", row, text.get_info(row));
  1902. return;
  1903. }
  1904. }
  1905. // Toggle fold on gutter click if can.
  1906. if (draw_fold_gutter) {
  1907. int left_margin = cache.style_normal->get_margin(MARGIN_LEFT);
  1908. int gutter_left = left_margin + cache.breakpoint_gutter_width + cache.line_number_w + cache.info_gutter_width;
  1909. if (mb->get_position().x > gutter_left - 6 && mb->get_position().x <= gutter_left + cache.fold_gutter_width - 3) {
  1910. if (is_folded(row)) {
  1911. unfold_line(row);
  1912. } else if (can_fold(row)) {
  1913. fold_line(row);
  1914. }
  1915. return;
  1916. }
  1917. }
  1918. // Unfold on folded icon click.
  1919. if (is_folded(row)) {
  1920. int line_width = text.get_line_width(row);
  1921. line_width += cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.info_gutter_width + cache.fold_gutter_width - cursor.x_ofs;
  1922. if (mb->get_position().x > line_width - 3 && mb->get_position().x <= line_width + cache.folded_eol_icon->get_width() + 3) {
  1923. unfold_line(row);
  1924. return;
  1925. }
  1926. }
  1927. // minimap
  1928. if (draw_minimap) {
  1929. _update_minimap_click();
  1930. if (dragging_minimap) {
  1931. return;
  1932. }
  1933. }
  1934. int prev_col = cursor.column;
  1935. int prev_line = cursor.line;
  1936. cursor_set_line(row, true, false);
  1937. cursor_set_column(col);
  1938. if (mb->get_shift() && (cursor.column != prev_col || cursor.line != prev_line)) {
  1939. if (!selection.active) {
  1940. selection.active = true;
  1941. selection.selecting_mode = Selection::MODE_POINTER;
  1942. selection.from_column = prev_col;
  1943. selection.from_line = prev_line;
  1944. selection.to_column = cursor.column;
  1945. selection.to_line = cursor.line;
  1946. if (selection.from_line > selection.to_line || (selection.from_line == selection.to_line && selection.from_column > selection.to_column)) {
  1947. SWAP(selection.from_column, selection.to_column);
  1948. SWAP(selection.from_line, selection.to_line);
  1949. selection.shiftclick_left = false;
  1950. } else {
  1951. selection.shiftclick_left = true;
  1952. }
  1953. selection.selecting_line = prev_line;
  1954. selection.selecting_column = prev_col;
  1955. update();
  1956. } else {
  1957. if (cursor.line < selection.selecting_line || (cursor.line == selection.selecting_line && cursor.column < selection.selecting_column)) {
  1958. if (selection.shiftclick_left) {
  1959. SWAP(selection.from_column, selection.to_column);
  1960. SWAP(selection.from_line, selection.to_line);
  1961. selection.shiftclick_left = !selection.shiftclick_left;
  1962. }
  1963. selection.from_column = cursor.column;
  1964. selection.from_line = cursor.line;
  1965. } else if (cursor.line > selection.selecting_line || (cursor.line == selection.selecting_line && cursor.column > selection.selecting_column)) {
  1966. if (!selection.shiftclick_left) {
  1967. SWAP(selection.from_column, selection.to_column);
  1968. SWAP(selection.from_line, selection.to_line);
  1969. selection.shiftclick_left = !selection.shiftclick_left;
  1970. }
  1971. selection.to_column = cursor.column;
  1972. selection.to_line = cursor.line;
  1973. } else {
  1974. selection.active = false;
  1975. }
  1976. update();
  1977. }
  1978. } else {
  1979. selection.active = false;
  1980. selection.selecting_mode = Selection::MODE_POINTER;
  1981. selection.selecting_line = row;
  1982. selection.selecting_column = col;
  1983. }
  1984. if (!mb->is_doubleclick() && (OS::get_singleton()->get_ticks_msec() - last_dblclk) < 600 && cursor.line == prev_line) {
  1985. // Triple-click select line.
  1986. selection.selecting_mode = Selection::MODE_LINE;
  1987. _update_selection_mode_line();
  1988. last_dblclk = 0;
  1989. } else if (mb->is_doubleclick() && text[cursor.line].length()) {
  1990. // Double-click select word.
  1991. selection.selecting_mode = Selection::MODE_WORD;
  1992. _update_selection_mode_word();
  1993. last_dblclk = OS::get_singleton()->get_ticks_msec();
  1994. }
  1995. update();
  1996. }
  1997. if (mb->get_button_index() == BUTTON_RIGHT && context_menu_enabled) {
  1998. _reset_caret_blink_timer();
  1999. int row, col;
  2000. _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col);
  2001. if (is_right_click_moving_caret()) {
  2002. if (is_selection_active()) {
  2003. int from_line = get_selection_from_line();
  2004. int to_line = get_selection_to_line();
  2005. int from_column = get_selection_from_column();
  2006. int to_column = get_selection_to_column();
  2007. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  2008. // Right click is outside the selected text.
  2009. deselect();
  2010. }
  2011. }
  2012. if (!is_selection_active()) {
  2013. cursor_set_line(row, true, false);
  2014. cursor_set_column(col);
  2015. }
  2016. }
  2017. menu->set_position(get_global_transform().xform(get_local_mouse_position()));
  2018. menu->set_size(Vector2(1, 1));
  2019. menu->set_scale(get_global_transform().get_scale());
  2020. menu->popup();
  2021. grab_focus();
  2022. }
  2023. } else {
  2024. if (mb->get_button_index() == BUTTON_LEFT) {
  2025. if (mb->get_command() && highlighted_word != String()) {
  2026. int row, col;
  2027. _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col);
  2028. emit_signal("symbol_lookup", highlighted_word, row, col);
  2029. return;
  2030. }
  2031. dragging_minimap = false;
  2032. dragging_selection = false;
  2033. can_drag_minimap = false;
  2034. click_select_held->stop();
  2035. }
  2036. // Notify to show soft keyboard.
  2037. notification(NOTIFICATION_FOCUS_ENTER);
  2038. }
  2039. }
  2040. const Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  2041. if (pan_gesture.is_valid()) {
  2042. const real_t delta = pan_gesture->get_delta().y;
  2043. if (delta < 0) {
  2044. _scroll_up(-delta);
  2045. } else {
  2046. _scroll_down(delta);
  2047. }
  2048. h_scroll->set_value(h_scroll->get_value() + pan_gesture->get_delta().x * 100);
  2049. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll)
  2050. accept_event(); // Accept event if scroll changed.
  2051. return;
  2052. }
  2053. Ref<InputEventMouseMotion> mm = p_gui_input;
  2054. if (mm.is_valid()) {
  2055. if (select_identifiers_enabled) {
  2056. if (!dragging_minimap && !dragging_selection && mm->get_command() && mm->get_button_mask() == 0) {
  2057. String new_word = get_word_at_pos(mm->get_position());
  2058. if (new_word != highlighted_word) {
  2059. emit_signal("symbol_validate", new_word);
  2060. }
  2061. } else {
  2062. if (highlighted_word != String()) {
  2063. set_highlighted_word(String());
  2064. }
  2065. }
  2066. }
  2067. if (mm->get_button_mask() & BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data() == Variant()) { // Ignore if dragging.
  2068. _reset_caret_blink_timer();
  2069. if (draw_minimap && !dragging_selection) {
  2070. _update_minimap_drag();
  2071. }
  2072. if (!dragging_minimap) {
  2073. switch (selection.selecting_mode) {
  2074. case Selection::MODE_POINTER: {
  2075. _update_selection_mode_pointer();
  2076. } break;
  2077. case Selection::MODE_WORD: {
  2078. _update_selection_mode_word();
  2079. } break;
  2080. case Selection::MODE_LINE: {
  2081. _update_selection_mode_line();
  2082. } break;
  2083. default: {
  2084. break;
  2085. }
  2086. }
  2087. }
  2088. }
  2089. }
  2090. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll)
  2091. accept_event(); // Accept event if scroll changed.
  2092. Ref<InputEventKey> k = p_gui_input;
  2093. if (k.is_valid()) {
  2094. k = k->duplicate(); // It will be modified later on.
  2095. #ifdef OSX_ENABLED
  2096. if (k->get_keycode() == KEY_META) {
  2097. #else
  2098. if (k->get_keycode() == KEY_CONTROL) {
  2099. #endif
  2100. if (select_identifiers_enabled) {
  2101. if (k->is_pressed() && !dragging_minimap && !dragging_selection) {
  2102. emit_signal("symbol_validate", get_word_at_pos(get_local_mouse_position()));
  2103. } else {
  2104. set_highlighted_word(String());
  2105. }
  2106. }
  2107. }
  2108. if (!k->is_pressed())
  2109. return;
  2110. if (completion_active) {
  2111. if (readonly)
  2112. return;
  2113. bool valid = true;
  2114. if (k->get_command() || k->get_metakey())
  2115. valid = false;
  2116. if (valid) {
  2117. if (!k->get_alt()) {
  2118. if (k->get_keycode() == KEY_UP) {
  2119. if (completion_index > 0) {
  2120. completion_index--;
  2121. } else {
  2122. completion_index = completion_options.size() - 1;
  2123. }
  2124. completion_current = completion_options[completion_index];
  2125. update();
  2126. accept_event();
  2127. return;
  2128. }
  2129. if (k->get_keycode() == KEY_DOWN) {
  2130. if (completion_index < completion_options.size() - 1) {
  2131. completion_index++;
  2132. } else {
  2133. completion_index = 0;
  2134. }
  2135. completion_current = completion_options[completion_index];
  2136. update();
  2137. accept_event();
  2138. return;
  2139. }
  2140. if (k->get_keycode() == KEY_PAGEUP) {
  2141. completion_index -= get_constant("completion_lines");
  2142. if (completion_index < 0)
  2143. completion_index = 0;
  2144. completion_current = completion_options[completion_index];
  2145. update();
  2146. accept_event();
  2147. return;
  2148. }
  2149. if (k->get_keycode() == KEY_PAGEDOWN) {
  2150. completion_index += get_constant("completion_lines");
  2151. if (completion_index >= completion_options.size())
  2152. completion_index = completion_options.size() - 1;
  2153. completion_current = completion_options[completion_index];
  2154. update();
  2155. accept_event();
  2156. return;
  2157. }
  2158. if (k->get_keycode() == KEY_HOME && completion_index > 0) {
  2159. completion_index = 0;
  2160. completion_current = completion_options[completion_index];
  2161. update();
  2162. accept_event();
  2163. return;
  2164. }
  2165. if (k->get_keycode() == KEY_END && completion_index < completion_options.size() - 1) {
  2166. completion_index = completion_options.size() - 1;
  2167. completion_current = completion_options[completion_index];
  2168. update();
  2169. accept_event();
  2170. return;
  2171. }
  2172. if (k->get_keycode() == KEY_KP_ENTER || k->get_keycode() == KEY_ENTER || k->get_keycode() == KEY_TAB) {
  2173. _confirm_completion();
  2174. accept_event();
  2175. return;
  2176. }
  2177. if (k->get_keycode() == KEY_BACKSPACE) {
  2178. _reset_caret_blink_timer();
  2179. backspace_at_cursor();
  2180. _update_completion_candidates();
  2181. accept_event();
  2182. return;
  2183. }
  2184. if (k->get_keycode() == KEY_SHIFT) {
  2185. accept_event();
  2186. return;
  2187. }
  2188. }
  2189. if (k->get_unicode() > 32) {
  2190. _reset_caret_blink_timer();
  2191. const CharType chr[2] = { (CharType)k->get_unicode(), 0 };
  2192. if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  2193. _consume_pair_symbol(chr[0]);
  2194. } else {
  2195. // Remove the old character if in insert mode.
  2196. if (insert_mode) {
  2197. begin_complex_operation();
  2198. // Make sure we don't try and remove empty space.
  2199. if (cursor.column < get_line(cursor.line).length()) {
  2200. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  2201. }
  2202. }
  2203. _insert_text_at_cursor(chr);
  2204. if (insert_mode) {
  2205. end_complex_operation();
  2206. }
  2207. }
  2208. _update_completion_candidates();
  2209. accept_event();
  2210. return;
  2211. }
  2212. }
  2213. _cancel_completion();
  2214. }
  2215. /* TEST CONTROL FIRST! */
  2216. // Some remaps for duplicate functions.
  2217. if (k->get_command() && !k->get_shift() && !k->get_alt() && !k->get_metakey() && k->get_keycode() == KEY_INSERT) {
  2218. k->set_keycode(KEY_C);
  2219. }
  2220. if (!k->get_command() && k->get_shift() && !k->get_alt() && !k->get_metakey() && k->get_keycode() == KEY_INSERT) {
  2221. k->set_keycode(KEY_V);
  2222. k->set_command(true);
  2223. k->set_shift(false);
  2224. }
  2225. #ifdef APPLE_STYLE_KEYS
  2226. if (k->get_control() && !k->get_shift() && !k->get_alt() && !k->get_command()) {
  2227. uint32_t remap_key = KEY_UNKNOWN;
  2228. switch (k->get_keycode()) {
  2229. case KEY_F: {
  2230. remap_key = KEY_RIGHT;
  2231. } break;
  2232. case KEY_B: {
  2233. remap_key = KEY_LEFT;
  2234. } break;
  2235. case KEY_P: {
  2236. remap_key = KEY_UP;
  2237. } break;
  2238. case KEY_N: {
  2239. remap_key = KEY_DOWN;
  2240. } break;
  2241. case KEY_D: {
  2242. remap_key = KEY_DELETE;
  2243. } break;
  2244. case KEY_H: {
  2245. remap_key = KEY_BACKSPACE;
  2246. } break;
  2247. }
  2248. if (remap_key != KEY_UNKNOWN) {
  2249. k->set_keycode(remap_key);
  2250. k->set_control(false);
  2251. }
  2252. }
  2253. #endif
  2254. _reset_caret_blink_timer();
  2255. // Save here for insert mode, just in case it is cleared in the following section.
  2256. bool had_selection = selection.active;
  2257. // Stuff to do when selection is active.
  2258. if (!readonly && selection.active) {
  2259. bool clear = false;
  2260. bool unselect = false;
  2261. bool dobreak = false;
  2262. switch (k->get_keycode()) {
  2263. case KEY_TAB: {
  2264. if (k->get_shift()) {
  2265. indent_left();
  2266. } else {
  2267. indent_right();
  2268. }
  2269. dobreak = true;
  2270. accept_event();
  2271. } break;
  2272. case KEY_X:
  2273. case KEY_C:
  2274. // Special keys often used with control, wait.
  2275. clear = (!k->get_command() || k->get_shift() || k->get_alt());
  2276. break;
  2277. case KEY_DELETE:
  2278. if (!k->get_shift()) {
  2279. accept_event();
  2280. clear = true;
  2281. dobreak = true;
  2282. } else if (k->get_command() || k->get_alt()) {
  2283. dobreak = true;
  2284. }
  2285. break;
  2286. case KEY_BACKSPACE:
  2287. accept_event();
  2288. clear = true;
  2289. dobreak = true;
  2290. break;
  2291. case KEY_LEFT:
  2292. case KEY_RIGHT:
  2293. case KEY_UP:
  2294. case KEY_DOWN:
  2295. case KEY_PAGEUP:
  2296. case KEY_PAGEDOWN:
  2297. case KEY_HOME:
  2298. case KEY_END:
  2299. // Ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys).
  2300. if (k->get_command() || k->get_shift() || k->get_alt())
  2301. break;
  2302. unselect = true;
  2303. break;
  2304. default:
  2305. if (k->get_unicode() >= 32 && !k->get_command() && !k->get_alt() && !k->get_metakey())
  2306. clear = true;
  2307. if (auto_brace_completion_enabled && _is_pair_left_symbol(k->get_unicode()))
  2308. clear = false;
  2309. }
  2310. if (unselect) {
  2311. selection.active = false;
  2312. selection.selecting_mode = Selection::MODE_NONE;
  2313. update();
  2314. }
  2315. if (clear) {
  2316. if (!dobreak) {
  2317. begin_complex_operation();
  2318. }
  2319. selection.active = false;
  2320. update();
  2321. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  2322. cursor_set_line(selection.from_line, true, false);
  2323. cursor_set_column(selection.from_column);
  2324. update();
  2325. }
  2326. if (dobreak)
  2327. return;
  2328. }
  2329. selection.selecting_text = false;
  2330. bool keycode_handled = true;
  2331. // Special keycode test.
  2332. switch (k->get_keycode()) {
  2333. case KEY_KP_ENTER:
  2334. case KEY_ENTER: {
  2335. if (readonly)
  2336. break;
  2337. String ins = "\n";
  2338. // Keep indentation.
  2339. int space_count = 0;
  2340. for (int i = 0; i < cursor.column; i++) {
  2341. if (text[cursor.line][i] == '\t') {
  2342. if (indent_using_spaces) {
  2343. ins += space_indent;
  2344. } else {
  2345. ins += "\t";
  2346. }
  2347. space_count = 0;
  2348. } else if (text[cursor.line][i] == ' ') {
  2349. space_count++;
  2350. if (space_count == indent_size) {
  2351. if (indent_using_spaces) {
  2352. ins += space_indent;
  2353. } else {
  2354. ins += "\t";
  2355. }
  2356. space_count = 0;
  2357. }
  2358. } else {
  2359. break;
  2360. }
  2361. }
  2362. if (is_folded(cursor.line))
  2363. unfold_line(cursor.line);
  2364. bool brace_indent = false;
  2365. // No need to indent if we are going upwards.
  2366. if (auto_indent && !(k->get_command() && k->get_shift())) {
  2367. // Indent once again if previous line will end with ':','{','[','(' and the line is not a comment
  2368. // (i.e. colon/brace precedes current cursor position).
  2369. if (cursor.column > 0) {
  2370. const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(cursor.line);
  2371. bool indent_char_found = false;
  2372. bool should_indent = false;
  2373. char indent_char = ':';
  2374. char c = text[cursor.line][cursor.column];
  2375. for (int i = 0; i < cursor.column; i++) {
  2376. c = text[cursor.line][i];
  2377. switch (c) {
  2378. case ':':
  2379. case '{':
  2380. case '[':
  2381. case '(':
  2382. indent_char_found = true;
  2383. should_indent = true;
  2384. indent_char = c;
  2385. continue;
  2386. }
  2387. if (indent_char_found && cri_map.has(i) && (color_regions[cri_map[i].region].begin_key == "#" || color_regions[cri_map[i].region].begin_key == "//")) {
  2388. should_indent = true;
  2389. break;
  2390. } else if (indent_char_found && !_is_whitespace(c)) {
  2391. should_indent = false;
  2392. indent_char_found = false;
  2393. }
  2394. }
  2395. if (!is_line_comment(cursor.line) && should_indent) {
  2396. if (indent_using_spaces) {
  2397. ins += space_indent;
  2398. } else {
  2399. ins += "\t";
  2400. }
  2401. // No need to move the brace below if we are not taking the text with us.
  2402. char closing_char = _get_right_pair_symbol(indent_char);
  2403. if ((closing_char != 0) && (closing_char == text[cursor.line][cursor.column]) && !k->get_command()) {
  2404. brace_indent = true;
  2405. ins += "\n" + ins.substr(1, ins.length() - 2);
  2406. }
  2407. }
  2408. }
  2409. }
  2410. begin_complex_operation();
  2411. bool first_line = false;
  2412. if (k->get_command()) {
  2413. if (k->get_shift()) {
  2414. if (cursor.line > 0) {
  2415. cursor_set_line(cursor.line - 1);
  2416. cursor_set_column(text[cursor.line].length());
  2417. } else {
  2418. cursor_set_column(0);
  2419. first_line = true;
  2420. }
  2421. } else {
  2422. cursor_set_column(text[cursor.line].length());
  2423. }
  2424. }
  2425. insert_text_at_cursor(ins);
  2426. if (first_line) {
  2427. cursor_set_line(0);
  2428. } else if (brace_indent) {
  2429. cursor_set_line(cursor.line - 1);
  2430. cursor_set_column(text[cursor.line].length());
  2431. }
  2432. end_complex_operation();
  2433. } break;
  2434. case KEY_ESCAPE: {
  2435. if (completion_hint != "") {
  2436. completion_hint = "";
  2437. update();
  2438. } else {
  2439. keycode_handled = false;
  2440. }
  2441. } break;
  2442. case KEY_TAB: {
  2443. if (k->get_command()) break; // Avoid tab when command.
  2444. if (readonly)
  2445. break;
  2446. if (is_selection_active()) {
  2447. if (k->get_shift()) {
  2448. indent_left();
  2449. } else {
  2450. indent_right();
  2451. }
  2452. } else {
  2453. if (k->get_shift()) {
  2454. // Simple unindent.
  2455. int cc = cursor.column;
  2456. const String &line = text[cursor.line];
  2457. int left = _find_first_non_whitespace_column_of_line(line);
  2458. cc = MIN(cc, left);
  2459. while (cc < indent_size && cc < left && line[cc] == ' ')
  2460. cc++;
  2461. if (cc > 0 && cc <= text[cursor.line].length()) {
  2462. if (text[cursor.line][cc - 1] == '\t') {
  2463. // Tabs unindentation.
  2464. _remove_text(cursor.line, cc - 1, cursor.line, cc);
  2465. if (cursor.column >= left)
  2466. cursor_set_column(MAX(0, cursor.column - 1));
  2467. update();
  2468. } else {
  2469. // Spaces unindentation.
  2470. int spaces_to_remove = _calculate_spaces_till_next_left_indent(cc);
  2471. if (spaces_to_remove > 0) {
  2472. _remove_text(cursor.line, cc - spaces_to_remove, cursor.line, cc);
  2473. if (cursor.column > left - spaces_to_remove) // Inside text?
  2474. cursor_set_column(MAX(0, cursor.column - spaces_to_remove));
  2475. update();
  2476. }
  2477. }
  2478. } else if (cc == 0 && line.length() > 0 && line[0] == '\t') {
  2479. _remove_text(cursor.line, 0, cursor.line, 1);
  2480. update();
  2481. }
  2482. } else {
  2483. // Simple indent.
  2484. if (indent_using_spaces) {
  2485. // Insert only as much spaces as needed till next indentation level.
  2486. int spaces_to_add = _calculate_spaces_till_next_right_indent(cursor.column);
  2487. String indent_to_insert = String();
  2488. for (int i = 0; i < spaces_to_add; i++)
  2489. indent_to_insert = ' ' + indent_to_insert;
  2490. _insert_text_at_cursor(indent_to_insert);
  2491. } else {
  2492. _insert_text_at_cursor("\t");
  2493. }
  2494. }
  2495. }
  2496. } break;
  2497. case KEY_BACKSPACE: {
  2498. if (readonly)
  2499. break;
  2500. #ifdef APPLE_STYLE_KEYS
  2501. if (k->get_alt() && cursor.column > 1) {
  2502. #else
  2503. if (k->get_alt()) {
  2504. keycode_handled = false;
  2505. break;
  2506. } else if (k->get_command() && cursor.column > 1) {
  2507. #endif
  2508. int line = cursor.line;
  2509. int column = cursor.column;
  2510. // Check if we are removing a single whitespace, if so remove it and the next char type,
  2511. // else we just remove the whitespace.
  2512. bool only_whitespace = false;
  2513. if (_is_whitespace(text[line][column - 1]) && _is_whitespace(text[line][column - 2])) {
  2514. only_whitespace = true;
  2515. } else if (_is_whitespace(text[line][column - 1])) {
  2516. // Remove the single whitespace.
  2517. column--;
  2518. }
  2519. // Check if its a text char.
  2520. bool only_char = (_is_text_char(text[line][column - 1]) && !only_whitespace);
  2521. // If its not whitespace or char then symbol.
  2522. bool only_symbols = !(only_whitespace || only_char);
  2523. while (column > 0) {
  2524. bool is_whitespace = _is_whitespace(text[line][column - 1]);
  2525. bool is_text_char = _is_text_char(text[line][column - 1]);
  2526. if (only_whitespace && !is_whitespace) {
  2527. break;
  2528. } else if (only_char && !is_text_char) {
  2529. break;
  2530. } else if (only_symbols && (is_whitespace || is_text_char)) {
  2531. break;
  2532. }
  2533. column--;
  2534. }
  2535. _remove_text(line, column, cursor.line, cursor.column);
  2536. cursor_set_line(line);
  2537. cursor_set_column(column);
  2538. #ifdef APPLE_STYLE_KEYS
  2539. } else if (k->get_command()) {
  2540. int cursor_current_column = cursor.column;
  2541. cursor.column = 0;
  2542. _remove_text(cursor.line, 0, cursor.line, cursor_current_column);
  2543. #endif
  2544. } else {
  2545. if (cursor.line > 0 && is_line_hidden(cursor.line - 1))
  2546. unfold_line(cursor.line - 1);
  2547. backspace_at_cursor();
  2548. }
  2549. } break;
  2550. case KEY_KP_4: {
  2551. if (k->get_unicode() != 0) {
  2552. keycode_handled = false;
  2553. break;
  2554. }
  2555. [[fallthrough]];
  2556. }
  2557. case KEY_LEFT: {
  2558. if (k->get_shift())
  2559. _pre_shift_selection();
  2560. #ifdef APPLE_STYLE_KEYS
  2561. else
  2562. #else
  2563. else if (!k->get_alt())
  2564. #endif
  2565. deselect();
  2566. #ifdef APPLE_STYLE_KEYS
  2567. if (k->get_command()) {
  2568. // Start at first column (it's slightly faster that way) and look for the first non-whitespace character.
  2569. int new_cursor_pos = 0;
  2570. for (int i = 0; i < text[cursor.line].length(); ++i) {
  2571. if (!_is_whitespace(text[cursor.line][i])) {
  2572. new_cursor_pos = i;
  2573. break;
  2574. }
  2575. }
  2576. if (new_cursor_pos == cursor.column) {
  2577. // We're already at the first text character, so move to the very beginning of the line.
  2578. cursor_set_column(0);
  2579. } else {
  2580. // We're somewhere to the right of the first text character; move to the first one.
  2581. cursor_set_column(new_cursor_pos);
  2582. }
  2583. } else if (k->get_alt()) {
  2584. #else
  2585. if (k->get_alt()) {
  2586. keycode_handled = false;
  2587. break;
  2588. } else if (k->get_command()) {
  2589. #endif
  2590. int cc = cursor.column;
  2591. if (cc == 0 && cursor.line > 0) {
  2592. cursor_set_line(cursor.line - 1);
  2593. cursor_set_column(text[cursor.line].length());
  2594. } else {
  2595. bool prev_char = false;
  2596. while (cc > 0) {
  2597. bool ischar = _is_text_char(text[cursor.line][cc - 1]);
  2598. if (prev_char && !ischar)
  2599. break;
  2600. prev_char = ischar;
  2601. cc--;
  2602. }
  2603. cursor_set_column(cc);
  2604. }
  2605. } else if (cursor.column == 0) {
  2606. if (cursor.line > 0) {
  2607. cursor_set_line(cursor.line - num_lines_from(CLAMP(cursor.line - 1, 0, text.size() - 1), -1));
  2608. cursor_set_column(text[cursor.line].length());
  2609. }
  2610. } else {
  2611. cursor_set_column(cursor_get_column() - 1);
  2612. }
  2613. if (k->get_shift())
  2614. _post_shift_selection();
  2615. } break;
  2616. case KEY_KP_6: {
  2617. if (k->get_unicode() != 0) {
  2618. keycode_handled = false;
  2619. break;
  2620. }
  2621. [[fallthrough]];
  2622. }
  2623. case KEY_RIGHT: {
  2624. if (k->get_shift())
  2625. _pre_shift_selection();
  2626. #ifdef APPLE_STYLE_KEYS
  2627. else
  2628. #else
  2629. else if (!k->get_alt())
  2630. #endif
  2631. deselect();
  2632. #ifdef APPLE_STYLE_KEYS
  2633. if (k->get_command()) {
  2634. cursor_set_column(text[cursor.line].length());
  2635. } else if (k->get_alt()) {
  2636. #else
  2637. if (k->get_alt()) {
  2638. keycode_handled = false;
  2639. break;
  2640. } else if (k->get_command()) {
  2641. #endif
  2642. int cc = cursor.column;
  2643. if (cc == text[cursor.line].length() && cursor.line < text.size() - 1) {
  2644. cursor_set_line(cursor.line + 1);
  2645. cursor_set_column(0);
  2646. } else {
  2647. bool prev_char = false;
  2648. while (cc < text[cursor.line].length()) {
  2649. bool ischar = _is_text_char(text[cursor.line][cc]);
  2650. if (prev_char && !ischar)
  2651. break;
  2652. prev_char = ischar;
  2653. cc++;
  2654. }
  2655. cursor_set_column(cc);
  2656. }
  2657. } else if (cursor.column == text[cursor.line].length()) {
  2658. if (cursor.line < text.size() - 1) {
  2659. cursor_set_line(cursor_get_line() + num_lines_from(CLAMP(cursor.line + 1, 0, text.size() - 1), 1), true, false);
  2660. cursor_set_column(0);
  2661. }
  2662. } else {
  2663. cursor_set_column(cursor_get_column() + 1);
  2664. }
  2665. if (k->get_shift())
  2666. _post_shift_selection();
  2667. } break;
  2668. case KEY_KP_8: {
  2669. if (k->get_unicode() != 0) {
  2670. keycode_handled = false;
  2671. break;
  2672. }
  2673. [[fallthrough]];
  2674. }
  2675. case KEY_UP: {
  2676. if (k->get_alt()) {
  2677. keycode_handled = false;
  2678. break;
  2679. }
  2680. #ifndef APPLE_STYLE_KEYS
  2681. if (k->get_command()) {
  2682. #else
  2683. if (k->get_command() && k->get_alt()) {
  2684. #endif
  2685. _scroll_lines_up();
  2686. break;
  2687. }
  2688. if (k->get_shift()) {
  2689. _pre_shift_selection();
  2690. }
  2691. #ifdef APPLE_STYLE_KEYS
  2692. if (k->get_command()) {
  2693. cursor_set_line(0);
  2694. } else
  2695. #endif
  2696. {
  2697. int cur_wrap_index = get_cursor_wrap_index();
  2698. if (cur_wrap_index > 0) {
  2699. cursor_set_line(cursor.line, true, false, cur_wrap_index - 1);
  2700. } else if (cursor.line == 0) {
  2701. cursor_set_column(0);
  2702. } else {
  2703. int new_line = cursor.line - num_lines_from(cursor.line - 1, -1);
  2704. if (line_wraps(new_line)) {
  2705. cursor_set_line(new_line, true, false, times_line_wraps(new_line));
  2706. } else {
  2707. cursor_set_line(new_line, true, false);
  2708. }
  2709. }
  2710. }
  2711. if (k->get_shift())
  2712. _post_shift_selection();
  2713. _cancel_code_hint();
  2714. } break;
  2715. case KEY_KP_2: {
  2716. if (k->get_unicode() != 0) {
  2717. keycode_handled = false;
  2718. break;
  2719. }
  2720. [[fallthrough]];
  2721. }
  2722. case KEY_DOWN: {
  2723. if (k->get_alt()) {
  2724. keycode_handled = false;
  2725. break;
  2726. }
  2727. #ifndef APPLE_STYLE_KEYS
  2728. if (k->get_command()) {
  2729. #else
  2730. if (k->get_command() && k->get_alt()) {
  2731. #endif
  2732. _scroll_lines_down();
  2733. break;
  2734. }
  2735. if (k->get_shift()) {
  2736. _pre_shift_selection();
  2737. }
  2738. #ifdef APPLE_STYLE_KEYS
  2739. if (k->get_command()) {
  2740. cursor_set_line(get_last_unhidden_line(), true, false, 9999);
  2741. } else
  2742. #endif
  2743. {
  2744. int cur_wrap_index = get_cursor_wrap_index();
  2745. if (cur_wrap_index < times_line_wraps(cursor.line)) {
  2746. cursor_set_line(cursor.line, true, false, cur_wrap_index + 1);
  2747. } else if (cursor.line == get_last_unhidden_line()) {
  2748. cursor_set_column(text[cursor.line].length());
  2749. } else {
  2750. int new_line = cursor.line + num_lines_from(CLAMP(cursor.line + 1, 0, text.size() - 1), 1);
  2751. cursor_set_line(new_line, true, false, 0);
  2752. }
  2753. }
  2754. if (k->get_shift())
  2755. _post_shift_selection();
  2756. _cancel_code_hint();
  2757. } break;
  2758. case KEY_DELETE: {
  2759. if (readonly)
  2760. break;
  2761. if (k->get_shift() && !k->get_command() && !k->get_alt() && is_shortcut_keys_enabled()) {
  2762. cut();
  2763. break;
  2764. }
  2765. int curline_len = text[cursor.line].length();
  2766. if (cursor.line == text.size() - 1 && cursor.column == curline_len)
  2767. break; // Nothing to do.
  2768. int next_line = cursor.column < curline_len ? cursor.line : cursor.line + 1;
  2769. int next_column;
  2770. #ifdef APPLE_STYLE_KEYS
  2771. if (k->get_alt() && cursor.column < curline_len - 1) {
  2772. #else
  2773. if (k->get_alt()) {
  2774. keycode_handled = false;
  2775. break;
  2776. } else if (k->get_command() && cursor.column < curline_len - 1) {
  2777. #endif
  2778. int line = cursor.line;
  2779. int column = cursor.column;
  2780. // Check if we are removing a single whitespace, if so remove it and the next char type,
  2781. // else we just remove the whitespace.
  2782. bool only_whitespace = false;
  2783. if (_is_whitespace(text[line][column]) && _is_whitespace(text[line][column + 1])) {
  2784. only_whitespace = true;
  2785. } else if (_is_whitespace(text[line][column])) {
  2786. // Remove the single whitespace.
  2787. column++;
  2788. }
  2789. // Check if its a text char.
  2790. bool only_char = (_is_text_char(text[line][column]) && !only_whitespace);
  2791. // If its not whitespace or char then symbol.
  2792. bool only_symbols = !(only_whitespace || only_char);
  2793. while (column < curline_len) {
  2794. bool is_whitespace = _is_whitespace(text[line][column]);
  2795. bool is_text_char = _is_text_char(text[line][column]);
  2796. if (only_whitespace && !is_whitespace) {
  2797. break;
  2798. } else if (only_char && !is_text_char) {
  2799. break;
  2800. } else if (only_symbols && (is_whitespace || is_text_char)) {
  2801. break;
  2802. }
  2803. column++;
  2804. }
  2805. next_line = line;
  2806. next_column = column;
  2807. #ifdef APPLE_STYLE_KEYS
  2808. } else if (k->get_command()) {
  2809. next_column = curline_len;
  2810. next_line = cursor.line;
  2811. #endif
  2812. } else {
  2813. next_column = cursor.column < curline_len ? (cursor.column + 1) : 0;
  2814. }
  2815. _remove_text(cursor.line, cursor.column, next_line, next_column);
  2816. update();
  2817. } break;
  2818. case KEY_KP_7: {
  2819. if (k->get_unicode() != 0) {
  2820. keycode_handled = false;
  2821. break;
  2822. }
  2823. [[fallthrough]];
  2824. }
  2825. case KEY_HOME: {
  2826. #ifdef APPLE_STYLE_KEYS
  2827. if (k->get_shift())
  2828. _pre_shift_selection();
  2829. cursor_set_line(0);
  2830. if (k->get_shift())
  2831. _post_shift_selection();
  2832. else if (k->get_command() || k->get_control())
  2833. deselect();
  2834. #else
  2835. if (k->get_shift())
  2836. _pre_shift_selection();
  2837. if (k->get_command()) {
  2838. cursor_set_line(0);
  2839. cursor_set_column(0);
  2840. } else {
  2841. // Move cursor column to start of wrapped row and then to start of text.
  2842. Vector<String> rows = get_wrap_rows_text(cursor.line);
  2843. int wi = get_cursor_wrap_index();
  2844. int row_start_col = 0;
  2845. for (int i = 0; i < wi; i++) {
  2846. row_start_col += rows[i].length();
  2847. }
  2848. if (cursor.column == row_start_col || wi == 0) {
  2849. // Compute whitespace symbols seq length.
  2850. int current_line_whitespace_len = 0;
  2851. while (current_line_whitespace_len < text[cursor.line].length()) {
  2852. CharType c = text[cursor.line][current_line_whitespace_len];
  2853. if (c != '\t' && c != ' ')
  2854. break;
  2855. current_line_whitespace_len++;
  2856. }
  2857. if (cursor_get_column() == current_line_whitespace_len)
  2858. cursor_set_column(0);
  2859. else
  2860. cursor_set_column(current_line_whitespace_len);
  2861. } else {
  2862. cursor_set_column(row_start_col);
  2863. }
  2864. }
  2865. if (k->get_shift())
  2866. _post_shift_selection();
  2867. else if (k->get_command() || k->get_control())
  2868. deselect();
  2869. _cancel_completion();
  2870. completion_hint = "";
  2871. #endif
  2872. } break;
  2873. case KEY_KP_1: {
  2874. if (k->get_unicode() != 0) {
  2875. keycode_handled = false;
  2876. break;
  2877. }
  2878. [[fallthrough]];
  2879. }
  2880. case KEY_END: {
  2881. #ifdef APPLE_STYLE_KEYS
  2882. if (k->get_shift())
  2883. _pre_shift_selection();
  2884. cursor_set_line(get_last_unhidden_line(), true, false, 9999);
  2885. if (k->get_shift())
  2886. _post_shift_selection();
  2887. else if (k->get_command() || k->get_control())
  2888. deselect();
  2889. #else
  2890. if (k->get_shift())
  2891. _pre_shift_selection();
  2892. if (k->get_command())
  2893. cursor_set_line(get_last_unhidden_line(), true, false, 9999);
  2894. // Move cursor column to end of wrapped row and then to end of text.
  2895. Vector<String> rows = get_wrap_rows_text(cursor.line);
  2896. int wi = get_cursor_wrap_index();
  2897. int row_end_col = -1;
  2898. for (int i = 0; i < wi + 1; i++) {
  2899. row_end_col += rows[i].length();
  2900. }
  2901. if (wi == rows.size() - 1 || cursor.column == row_end_col) {
  2902. cursor_set_column(text[cursor.line].length());
  2903. } else {
  2904. cursor_set_column(row_end_col);
  2905. }
  2906. if (k->get_shift())
  2907. _post_shift_selection();
  2908. else if (k->get_command() || k->get_control())
  2909. deselect();
  2910. _cancel_completion();
  2911. completion_hint = "";
  2912. #endif
  2913. } break;
  2914. case KEY_KP_9: {
  2915. if (k->get_unicode() != 0) {
  2916. keycode_handled = false;
  2917. break;
  2918. }
  2919. [[fallthrough]];
  2920. }
  2921. case KEY_PAGEUP: {
  2922. if (k->get_shift())
  2923. _pre_shift_selection();
  2924. int wi;
  2925. int n_line = cursor.line - num_lines_from_rows(cursor.line, get_cursor_wrap_index(), -get_visible_rows(), wi) + 1;
  2926. cursor_set_line(n_line, true, false, wi);
  2927. if (k->get_shift())
  2928. _post_shift_selection();
  2929. _cancel_completion();
  2930. completion_hint = "";
  2931. } break;
  2932. case KEY_KP_3: {
  2933. if (k->get_unicode() != 0) {
  2934. keycode_handled = false;
  2935. break;
  2936. }
  2937. [[fallthrough]];
  2938. }
  2939. case KEY_PAGEDOWN: {
  2940. if (k->get_shift())
  2941. _pre_shift_selection();
  2942. int wi;
  2943. int n_line = cursor.line + num_lines_from_rows(cursor.line, get_cursor_wrap_index(), get_visible_rows(), wi) - 1;
  2944. cursor_set_line(n_line, true, false, wi);
  2945. if (k->get_shift())
  2946. _post_shift_selection();
  2947. _cancel_completion();
  2948. completion_hint = "";
  2949. } break;
  2950. case KEY_A: {
  2951. #ifndef APPLE_STYLE_KEYS
  2952. if (!k->get_control() || k->get_shift() || k->get_alt()) {
  2953. keycode_handled = false;
  2954. break;
  2955. }
  2956. if (is_shortcut_keys_enabled()) {
  2957. select_all();
  2958. }
  2959. #else
  2960. if ((!k->get_command() && !k->get_control())) {
  2961. keycode_handled = false;
  2962. break;
  2963. }
  2964. if (!k->get_shift() && k->get_command() && is_shortcut_keys_enabled())
  2965. select_all();
  2966. else if (k->get_control()) {
  2967. if (k->get_shift())
  2968. _pre_shift_selection();
  2969. int current_line_whitespace_len = 0;
  2970. while (current_line_whitespace_len < text[cursor.line].length()) {
  2971. CharType c = text[cursor.line][current_line_whitespace_len];
  2972. if (c != '\t' && c != ' ')
  2973. break;
  2974. current_line_whitespace_len++;
  2975. }
  2976. if (cursor_get_column() == current_line_whitespace_len)
  2977. cursor_set_column(0);
  2978. else
  2979. cursor_set_column(current_line_whitespace_len);
  2980. if (k->get_shift())
  2981. _post_shift_selection();
  2982. else if (k->get_command() || k->get_control())
  2983. deselect();
  2984. }
  2985. } break;
  2986. case KEY_E: {
  2987. if (!k->get_control() || k->get_command() || k->get_alt()) {
  2988. keycode_handled = false;
  2989. break;
  2990. }
  2991. if (k->get_shift())
  2992. _pre_shift_selection();
  2993. if (k->get_command())
  2994. cursor_set_line(text.size() - 1, true, false);
  2995. cursor_set_column(text[cursor.line].length());
  2996. if (k->get_shift())
  2997. _post_shift_selection();
  2998. else if (k->get_command() || k->get_control())
  2999. deselect();
  3000. _cancel_completion();
  3001. completion_hint = "";
  3002. #endif
  3003. } break;
  3004. case KEY_X: {
  3005. if (readonly) {
  3006. break;
  3007. }
  3008. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  3009. keycode_handled = false;
  3010. break;
  3011. }
  3012. if (is_shortcut_keys_enabled()) {
  3013. cut();
  3014. }
  3015. } break;
  3016. case KEY_C: {
  3017. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  3018. keycode_handled = false;
  3019. break;
  3020. }
  3021. if (is_shortcut_keys_enabled()) {
  3022. copy();
  3023. }
  3024. } break;
  3025. case KEY_Z: {
  3026. if (readonly) {
  3027. break;
  3028. }
  3029. if (!k->get_command()) {
  3030. keycode_handled = false;
  3031. break;
  3032. }
  3033. if (is_shortcut_keys_enabled()) {
  3034. if (k->get_shift())
  3035. redo();
  3036. else
  3037. undo();
  3038. }
  3039. } break;
  3040. case KEY_Y: {
  3041. if (readonly) {
  3042. break;
  3043. }
  3044. if (!k->get_command()) {
  3045. keycode_handled = false;
  3046. break;
  3047. }
  3048. if (is_shortcut_keys_enabled()) {
  3049. redo();
  3050. }
  3051. } break;
  3052. case KEY_V: {
  3053. if (readonly) {
  3054. break;
  3055. }
  3056. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  3057. keycode_handled = false;
  3058. break;
  3059. }
  3060. if (is_shortcut_keys_enabled()) {
  3061. paste();
  3062. }
  3063. } break;
  3064. case KEY_SPACE: {
  3065. #ifdef OSX_ENABLED
  3066. if (completion_enabled && k->get_metakey()) { // cmd-space is spotlight shortcut in OSX
  3067. #else
  3068. if (completion_enabled && k->get_command()) {
  3069. #endif
  3070. query_code_comple();
  3071. keycode_handled = true;
  3072. } else {
  3073. keycode_handled = false;
  3074. }
  3075. } break;
  3076. case KEY_MENU: {
  3077. if (context_menu_enabled) {
  3078. menu->set_position(get_global_transform().xform(_get_cursor_pixel_pos()));
  3079. menu->set_size(Vector2(1, 1));
  3080. menu->set_scale(get_global_transform().get_scale());
  3081. menu->popup();
  3082. menu->grab_focus();
  3083. }
  3084. } break;
  3085. default: {
  3086. keycode_handled = false;
  3087. } break;
  3088. }
  3089. if (keycode_handled)
  3090. accept_event();
  3091. if (k->get_keycode() == KEY_INSERT) {
  3092. set_insert_mode(!insert_mode);
  3093. accept_event();
  3094. return;
  3095. }
  3096. if (!keycode_handled && !k->get_command()) { // For German keyboards.
  3097. if (k->get_unicode() >= 32) {
  3098. if (readonly)
  3099. return;
  3100. // Remove the old character if in insert mode and no selection.
  3101. if (insert_mode && !had_selection) {
  3102. begin_complex_operation();
  3103. // Make sure we don't try and remove empty space.
  3104. if (cursor.column < get_line(cursor.line).length()) {
  3105. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  3106. }
  3107. }
  3108. const CharType chr[2] = { (CharType)k->get_unicode(), 0 };
  3109. if (completion_hint != "" && k->get_unicode() == ')') {
  3110. completion_hint = "";
  3111. }
  3112. if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  3113. _consume_pair_symbol(chr[0]);
  3114. } else {
  3115. _insert_text_at_cursor(chr);
  3116. }
  3117. if (insert_mode && !had_selection) {
  3118. end_complex_operation();
  3119. }
  3120. if (selection.active != had_selection) {
  3121. end_complex_operation();
  3122. }
  3123. accept_event();
  3124. }
  3125. }
  3126. return;
  3127. }
  3128. }
  3129. void TextEdit::_scroll_up(real_t p_delta) {
  3130. if (scrolling && smooth_scroll_enabled && SGN(target_v_scroll - v_scroll->get_value()) != SGN(-p_delta)) {
  3131. scrolling = false;
  3132. minimap_clicked = false;
  3133. }
  3134. if (scrolling) {
  3135. target_v_scroll = (target_v_scroll - p_delta);
  3136. } else {
  3137. target_v_scroll = (get_v_scroll() - p_delta);
  3138. }
  3139. if (smooth_scroll_enabled) {
  3140. if (target_v_scroll <= 0) {
  3141. target_v_scroll = 0;
  3142. }
  3143. if (Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  3144. v_scroll->set_value(target_v_scroll);
  3145. } else {
  3146. scrolling = true;
  3147. set_physics_process_internal(true);
  3148. }
  3149. } else {
  3150. set_v_scroll(target_v_scroll);
  3151. }
  3152. }
  3153. void TextEdit::_scroll_down(real_t p_delta) {
  3154. if (scrolling && smooth_scroll_enabled && SGN(target_v_scroll - v_scroll->get_value()) != SGN(p_delta)) {
  3155. scrolling = false;
  3156. minimap_clicked = false;
  3157. }
  3158. if (scrolling) {
  3159. target_v_scroll = (target_v_scroll + p_delta);
  3160. } else {
  3161. target_v_scroll = (get_v_scroll() + p_delta);
  3162. }
  3163. if (smooth_scroll_enabled) {
  3164. int max_v_scroll = round(v_scroll->get_max() - v_scroll->get_page());
  3165. if (target_v_scroll > max_v_scroll) {
  3166. target_v_scroll = max_v_scroll;
  3167. }
  3168. if (Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  3169. v_scroll->set_value(target_v_scroll);
  3170. } else {
  3171. scrolling = true;
  3172. set_physics_process_internal(true);
  3173. }
  3174. } else {
  3175. set_v_scroll(target_v_scroll);
  3176. }
  3177. }
  3178. void TextEdit::_pre_shift_selection() {
  3179. if (!selection.active || selection.selecting_mode == Selection::MODE_NONE) {
  3180. selection.selecting_line = cursor.line;
  3181. selection.selecting_column = cursor.column;
  3182. selection.active = true;
  3183. }
  3184. selection.selecting_mode = Selection::MODE_SHIFT;
  3185. }
  3186. void TextEdit::_post_shift_selection() {
  3187. if (selection.active && selection.selecting_mode == Selection::MODE_SHIFT) {
  3188. select(selection.selecting_line, selection.selecting_column, cursor.line, cursor.column);
  3189. update();
  3190. }
  3191. selection.selecting_text = true;
  3192. }
  3193. void TextEdit::_scroll_lines_up() {
  3194. scrolling = false;
  3195. minimap_clicked = false;
  3196. // Adjust the vertical scroll.
  3197. set_v_scroll(get_v_scroll() - 1);
  3198. // Adjust the cursor to viewport.
  3199. if (!selection.active) {
  3200. int cur_line = cursor.line;
  3201. int cur_wrap = get_cursor_wrap_index();
  3202. int last_vis_line = get_last_visible_line();
  3203. int last_vis_wrap = get_last_visible_line_wrap_index();
  3204. if (cur_line > last_vis_line || (cur_line == last_vis_line && cur_wrap > last_vis_wrap)) {
  3205. cursor_set_line(last_vis_line, false, false, last_vis_wrap);
  3206. }
  3207. }
  3208. }
  3209. void TextEdit::_scroll_lines_down() {
  3210. scrolling = false;
  3211. minimap_clicked = false;
  3212. // Adjust the vertical scroll.
  3213. set_v_scroll(get_v_scroll() + 1);
  3214. // Adjust the cursor to viewport.
  3215. if (!selection.active) {
  3216. int cur_line = cursor.line;
  3217. int cur_wrap = get_cursor_wrap_index();
  3218. int first_vis_line = get_first_visible_line();
  3219. int first_vis_wrap = cursor.wrap_ofs;
  3220. if (cur_line < first_vis_line || (cur_line == first_vis_line && cur_wrap < first_vis_wrap)) {
  3221. cursor_set_line(first_vis_line, false, false, first_vis_wrap);
  3222. }
  3223. }
  3224. }
  3225. /**** TEXT EDIT CORE API ****/
  3226. void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) {
  3227. // Save for undo.
  3228. ERR_FAIL_INDEX(p_line, text.size());
  3229. ERR_FAIL_COND(p_char < 0);
  3230. /* STEP 1: Remove \r from source text and separate in substrings. */
  3231. Vector<String> substrings = p_text.replace("\r", "").split("\n");
  3232. /* STEP 2: Fire breakpoint_toggled signals. */
  3233. // Is this just a new empty line?
  3234. bool shift_first_line = p_char == 0 && p_text.replace("\r", "") == "\n";
  3235. int i = p_line + !shift_first_line;
  3236. int lines = substrings.size() - 1;
  3237. for (; i < text.size(); i++) {
  3238. if (text.is_breakpoint(i)) {
  3239. if ((i - lines < p_line || !text.is_breakpoint(i - lines)) || (i - lines == p_line && !shift_first_line))
  3240. emit_signal("breakpoint_toggled", i);
  3241. if (i + lines >= text.size() || !text.is_breakpoint(i + lines))
  3242. emit_signal("breakpoint_toggled", i + lines);
  3243. }
  3244. }
  3245. /* STEP 3: Add spaces if the char is greater than the end of the line. */
  3246. while (p_char > text[p_line].length()) {
  3247. text.set(p_line, text[p_line] + String::chr(' '));
  3248. }
  3249. /* STEP 4: Separate dest string in pre and post text. */
  3250. String preinsert_text = text[p_line].substr(0, p_char);
  3251. String postinsert_text = text[p_line].substr(p_char, text[p_line].size());
  3252. for (int j = 0; j < substrings.size(); j++) {
  3253. // Insert the substrings.
  3254. if (j == 0) {
  3255. text.set(p_line, preinsert_text + substrings[j]);
  3256. } else {
  3257. text.insert(p_line + j, substrings[j]);
  3258. }
  3259. if (j == substrings.size() - 1) {
  3260. text.set(p_line + j, text[p_line + j] + postinsert_text);
  3261. }
  3262. }
  3263. if (shift_first_line) {
  3264. text.set_breakpoint(p_line + 1, text.is_breakpoint(p_line));
  3265. text.set_hidden(p_line + 1, text.is_hidden(p_line));
  3266. if (text.has_info_icon(p_line)) {
  3267. text.set_info_icon(p_line + 1, text.get_info_icon(p_line), text.get_info(p_line));
  3268. }
  3269. text.set_breakpoint(p_line, false);
  3270. text.set_hidden(p_line, false);
  3271. text.set_info_icon(p_line, NULL, "");
  3272. }
  3273. text.set_line_wrap_amount(p_line, -1);
  3274. r_end_line = p_line + substrings.size() - 1;
  3275. r_end_column = text[r_end_line].length() - postinsert_text.length();
  3276. if (!text_changed_dirty && !setting_text) {
  3277. if (is_inside_tree())
  3278. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  3279. text_changed_dirty = true;
  3280. }
  3281. _line_edited_from(p_line);
  3282. }
  3283. String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const {
  3284. ERR_FAIL_INDEX_V(p_from_line, text.size(), String());
  3285. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String());
  3286. ERR_FAIL_INDEX_V(p_to_line, text.size(), String());
  3287. ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String());
  3288. ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // 'from > to'.
  3289. ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // 'from > to'.
  3290. String ret;
  3291. for (int i = p_from_line; i <= p_to_line; i++) {
  3292. int begin = (i == p_from_line) ? p_from_column : 0;
  3293. int end = (i == p_to_line) ? p_to_column : text[i].length();
  3294. if (i > p_from_line)
  3295. ret += "\n";
  3296. ret += text[i].substr(begin, end - begin);
  3297. }
  3298. return ret;
  3299. }
  3300. void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  3301. ERR_FAIL_INDEX(p_from_line, text.size());
  3302. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  3303. ERR_FAIL_INDEX(p_to_line, text.size());
  3304. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  3305. ERR_FAIL_COND(p_to_line < p_from_line); // 'from > to'.
  3306. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // 'from > to'.
  3307. String pre_text = text[p_from_line].substr(0, p_from_column);
  3308. String post_text = text[p_to_line].substr(p_to_column, text[p_to_line].length());
  3309. int lines = p_to_line - p_from_line;
  3310. for (int i = p_from_line + 1; i < text.size(); i++) {
  3311. if (text.is_breakpoint(i)) {
  3312. if (i + lines >= text.size() || !text.is_breakpoint(i + lines))
  3313. emit_signal("breakpoint_toggled", i);
  3314. if (i > p_to_line && (i - lines < 0 || !text.is_breakpoint(i - lines)))
  3315. emit_signal("breakpoint_toggled", i - lines);
  3316. }
  3317. }
  3318. for (int i = p_from_line; i < p_to_line; i++) {
  3319. text.remove(p_from_line + 1);
  3320. }
  3321. text.set(p_from_line, pre_text + post_text);
  3322. text.set_line_wrap_amount(p_from_line, -1);
  3323. if (!text_changed_dirty && !setting_text) {
  3324. if (is_inside_tree())
  3325. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  3326. text_changed_dirty = true;
  3327. }
  3328. _line_edited_from(p_from_line);
  3329. }
  3330. void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_char) {
  3331. if (!setting_text && idle_detect->is_inside_tree())
  3332. idle_detect->start();
  3333. if (undo_enabled) {
  3334. _clear_redo();
  3335. }
  3336. int retline, retchar;
  3337. _base_insert_text(p_line, p_char, p_text, retline, retchar);
  3338. if (r_end_line)
  3339. *r_end_line = retline;
  3340. if (r_end_char)
  3341. *r_end_char = retchar;
  3342. if (!undo_enabled)
  3343. return;
  3344. /* UNDO!! */
  3345. TextOperation op;
  3346. op.type = TextOperation::TYPE_INSERT;
  3347. op.from_line = p_line;
  3348. op.from_column = p_char;
  3349. op.to_line = retline;
  3350. op.to_column = retchar;
  3351. op.text = p_text;
  3352. op.version = ++version;
  3353. op.chain_forward = false;
  3354. op.chain_backward = false;
  3355. // See if it should just be set as current op.
  3356. if (current_op.type != op.type) {
  3357. op.prev_version = get_version();
  3358. _push_current_op();
  3359. current_op = op;
  3360. return; // Set as current op, return.
  3361. }
  3362. // See if it can be merged.
  3363. if (current_op.to_line != p_line || current_op.to_column != p_char) {
  3364. op.prev_version = get_version();
  3365. _push_current_op();
  3366. current_op = op;
  3367. return; // Set as current op, return.
  3368. }
  3369. // Merge current op.
  3370. current_op.text += p_text;
  3371. current_op.to_column = retchar;
  3372. current_op.to_line = retline;
  3373. current_op.version = op.version;
  3374. }
  3375. void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  3376. if (!setting_text && idle_detect->is_inside_tree())
  3377. idle_detect->start();
  3378. String text;
  3379. if (undo_enabled) {
  3380. _clear_redo();
  3381. text = _base_get_text(p_from_line, p_from_column, p_to_line, p_to_column);
  3382. }
  3383. _base_remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  3384. if (!undo_enabled)
  3385. return;
  3386. /* UNDO! */
  3387. TextOperation op;
  3388. op.type = TextOperation::TYPE_REMOVE;
  3389. op.from_line = p_from_line;
  3390. op.from_column = p_from_column;
  3391. op.to_line = p_to_line;
  3392. op.to_column = p_to_column;
  3393. op.text = text;
  3394. op.version = ++version;
  3395. op.chain_forward = false;
  3396. op.chain_backward = false;
  3397. // See if it should just be set as current op.
  3398. if (current_op.type != op.type) {
  3399. op.prev_version = get_version();
  3400. _push_current_op();
  3401. current_op = op;
  3402. return; // Set as current op, return.
  3403. }
  3404. // See if it can be merged.
  3405. if (current_op.from_line == p_to_line && current_op.from_column == p_to_column) {
  3406. // Backspace or similar.
  3407. current_op.text = text + current_op.text;
  3408. current_op.from_line = p_from_line;
  3409. current_op.from_column = p_from_column;
  3410. return; // Update current op.
  3411. }
  3412. op.prev_version = get_version();
  3413. _push_current_op();
  3414. current_op = op;
  3415. }
  3416. void TextEdit::_insert_text_at_cursor(const String &p_text) {
  3417. int new_column, new_line;
  3418. _insert_text(cursor.line, cursor.column, p_text, &new_line, &new_column);
  3419. _update_scrollbars();
  3420. cursor_set_line(new_line);
  3421. cursor_set_column(new_column);
  3422. update();
  3423. }
  3424. void TextEdit::_line_edited_from(int p_line) {
  3425. int cache_size = color_region_cache.size();
  3426. for (int i = p_line; i < cache_size; i++) {
  3427. color_region_cache.erase(i);
  3428. }
  3429. if (syntax_highlighting_cache.size() > 0) {
  3430. cache_size = syntax_highlighting_cache.back()->key();
  3431. for (int i = p_line - 1; i <= cache_size; i++) {
  3432. if (syntax_highlighting_cache.has(i)) {
  3433. syntax_highlighting_cache.erase(i);
  3434. }
  3435. }
  3436. }
  3437. }
  3438. int TextEdit::get_char_count() {
  3439. int totalsize = 0;
  3440. for (int i = 0; i < text.size(); i++) {
  3441. if (i > 0)
  3442. totalsize++; // Include \n.
  3443. totalsize += text[i].length();
  3444. }
  3445. return totalsize; // Omit last \n.
  3446. }
  3447. Size2 TextEdit::get_minimum_size() const {
  3448. return cache.style_normal->get_minimum_size();
  3449. }
  3450. int TextEdit::_get_control_height() const {
  3451. int control_height = get_size().height;
  3452. control_height -= cache.style_normal->get_minimum_size().height;
  3453. if (h_scroll->is_visible_in_tree()) {
  3454. control_height -= h_scroll->get_size().height;
  3455. }
  3456. return control_height;
  3457. }
  3458. void TextEdit::_generate_context_menu() {
  3459. // Reorganize context menu.
  3460. menu->clear();
  3461. if (!readonly)
  3462. menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_X : 0);
  3463. menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_C : 0);
  3464. if (!readonly)
  3465. menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_V : 0);
  3466. menu->add_separator();
  3467. if (is_selecting_enabled())
  3468. menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_A : 0);
  3469. if (!readonly) {
  3470. menu->add_item(RTR("Clear"), MENU_CLEAR);
  3471. menu->add_separator();
  3472. menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_Z : 0);
  3473. menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z : 0);
  3474. }
  3475. }
  3476. int TextEdit::get_visible_rows() const {
  3477. return _get_control_height() / get_row_height();
  3478. }
  3479. int TextEdit::_get_minimap_visible_rows() const {
  3480. return _get_control_height() / (minimap_char_size.y + minimap_line_spacing);
  3481. }
  3482. int TextEdit::get_total_visible_rows() const {
  3483. // Returns the total amount of rows we need in the editor.
  3484. // This skips hidden lines and counts each wrapping of a line.
  3485. if (!is_hiding_enabled() && !is_wrap_enabled())
  3486. return text.size();
  3487. int total_rows = 0;
  3488. for (int i = 0; i < text.size(); i++) {
  3489. if (!text.is_hidden(i)) {
  3490. total_rows++;
  3491. total_rows += times_line_wraps(i);
  3492. }
  3493. }
  3494. return total_rows;
  3495. }
  3496. void TextEdit::_update_wrap_at() {
  3497. wrap_at = get_size().width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width - cache.fold_gutter_width - cache.info_gutter_width - cache.minimap_width - wrap_right_offset;
  3498. update_cursor_wrap_offset();
  3499. text.clear_wrap_cache();
  3500. for (int i = 0; i < text.size(); i++) {
  3501. // Update all values that wrap.
  3502. if (!line_wraps(i))
  3503. continue;
  3504. Vector<String> rows = get_wrap_rows_text(i);
  3505. text.set_line_wrap_amount(i, rows.size() - 1);
  3506. }
  3507. }
  3508. void TextEdit::adjust_viewport_to_cursor() {
  3509. // Make sure cursor is visible on the screen.
  3510. scrolling = false;
  3511. minimap_clicked = false;
  3512. int cur_line = cursor.line;
  3513. int cur_wrap = get_cursor_wrap_index();
  3514. int first_vis_line = get_first_visible_line();
  3515. int first_vis_wrap = cursor.wrap_ofs;
  3516. int last_vis_line = get_last_visible_line();
  3517. int last_vis_wrap = get_last_visible_line_wrap_index();
  3518. if (cur_line < first_vis_line || (cur_line == first_vis_line && cur_wrap < first_vis_wrap)) {
  3519. // Cursor is above screen.
  3520. set_line_as_first_visible(cur_line, cur_wrap);
  3521. } else if (cur_line > last_vis_line || (cur_line == last_vis_line && cur_wrap > last_vis_wrap)) {
  3522. // Cursor is below screen.
  3523. set_line_as_last_visible(cur_line, cur_wrap);
  3524. }
  3525. int visible_width = get_size().width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width - cache.fold_gutter_width - cache.info_gutter_width - cache.minimap_width;
  3526. if (v_scroll->is_visible_in_tree())
  3527. visible_width -= v_scroll->get_combined_minimum_size().width;
  3528. visible_width -= 20; // Give it a little more space.
  3529. if (!is_wrap_enabled()) {
  3530. // Adjust x offset.
  3531. int cursor_x = get_column_x_offset(cursor.column, text[cursor.line]);
  3532. if (cursor_x > (cursor.x_ofs + visible_width))
  3533. cursor.x_ofs = cursor_x - visible_width + 1;
  3534. if (cursor_x < cursor.x_ofs)
  3535. cursor.x_ofs = cursor_x;
  3536. } else {
  3537. cursor.x_ofs = 0;
  3538. }
  3539. h_scroll->set_value(cursor.x_ofs);
  3540. update();
  3541. }
  3542. void TextEdit::center_viewport_to_cursor() {
  3543. // Move viewport so the cursor is in the center of the screen.
  3544. scrolling = false;
  3545. minimap_clicked = false;
  3546. if (is_line_hidden(cursor.line))
  3547. unfold_line(cursor.line);
  3548. set_line_as_center_visible(cursor.line, get_cursor_wrap_index());
  3549. int visible_width = get_size().width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width - cache.fold_gutter_width - cache.info_gutter_width - cache.minimap_width;
  3550. if (v_scroll->is_visible_in_tree())
  3551. visible_width -= v_scroll->get_combined_minimum_size().width;
  3552. visible_width -= 20; // Give it a little more space.
  3553. if (is_wrap_enabled()) {
  3554. // Center x offset.
  3555. int cursor_x = get_column_x_offset_for_line(cursor.column, cursor.line);
  3556. if (cursor_x > (cursor.x_ofs + visible_width))
  3557. cursor.x_ofs = cursor_x - visible_width + 1;
  3558. if (cursor_x < cursor.x_ofs)
  3559. cursor.x_ofs = cursor_x;
  3560. } else {
  3561. cursor.x_ofs = 0;
  3562. }
  3563. h_scroll->set_value(cursor.x_ofs);
  3564. update();
  3565. }
  3566. void TextEdit::update_cursor_wrap_offset() {
  3567. int first_vis_line = get_first_visible_line();
  3568. if (line_wraps(first_vis_line)) {
  3569. cursor.wrap_ofs = MIN(cursor.wrap_ofs, times_line_wraps(first_vis_line));
  3570. } else {
  3571. cursor.wrap_ofs = 0;
  3572. }
  3573. set_line_as_first_visible(cursor.line_ofs, cursor.wrap_ofs);
  3574. }
  3575. bool TextEdit::line_wraps(int line) const {
  3576. ERR_FAIL_INDEX_V(line, text.size(), 0);
  3577. if (!is_wrap_enabled())
  3578. return false;
  3579. return text.get_line_width(line) > wrap_at;
  3580. }
  3581. int TextEdit::times_line_wraps(int line) const {
  3582. ERR_FAIL_INDEX_V(line, text.size(), 0);
  3583. if (!line_wraps(line))
  3584. return 0;
  3585. int wrap_amount = text.get_line_wrap_amount(line);
  3586. if (wrap_amount == -1) {
  3587. // Update the value.
  3588. Vector<String> rows = get_wrap_rows_text(line);
  3589. wrap_amount = rows.size() - 1;
  3590. text.set_line_wrap_amount(line, wrap_amount);
  3591. }
  3592. return wrap_amount;
  3593. }
  3594. Vector<String> TextEdit::get_wrap_rows_text(int p_line) const {
  3595. ERR_FAIL_INDEX_V(p_line, text.size(), Vector<String>());
  3596. Vector<String> lines;
  3597. if (!line_wraps(p_line)) {
  3598. lines.push_back(text[p_line]);
  3599. return lines;
  3600. }
  3601. int px = 0;
  3602. int col = 0;
  3603. String line_text = text[p_line];
  3604. String wrap_substring = "";
  3605. int word_px = 0;
  3606. String word_str = "";
  3607. int cur_wrap_index = 0;
  3608. int tab_offset_px = get_indent_level(p_line) * cache.font->get_char_size(' ').width;
  3609. if (tab_offset_px >= wrap_at) {
  3610. tab_offset_px = 0;
  3611. }
  3612. while (col < line_text.length()) {
  3613. CharType c = line_text[col];
  3614. int w = text.get_char_width(c, line_text[col + 1], px + word_px);
  3615. int indent_ofs = (cur_wrap_index != 0 ? tab_offset_px : 0);
  3616. if (indent_ofs + word_px + w > wrap_at) {
  3617. // Not enough space to add this char; start next line.
  3618. wrap_substring += word_str;
  3619. lines.push_back(wrap_substring);
  3620. cur_wrap_index++;
  3621. wrap_substring = "";
  3622. px = 0;
  3623. word_str = "";
  3624. word_str += c;
  3625. word_px = w;
  3626. } else {
  3627. word_str += c;
  3628. word_px += w;
  3629. if (c == ' ') {
  3630. // End of a word; add this word to the substring.
  3631. wrap_substring += word_str;
  3632. px += word_px;
  3633. word_str = "";
  3634. word_px = 0;
  3635. }
  3636. if (indent_ofs + px + word_px > wrap_at) {
  3637. // This word will be moved to the next line.
  3638. lines.push_back(wrap_substring);
  3639. // Reset for next wrap.
  3640. cur_wrap_index++;
  3641. wrap_substring = "";
  3642. px = 0;
  3643. }
  3644. }
  3645. col++;
  3646. }
  3647. // Line ends before hit wrap_at; add this word to the substring.
  3648. wrap_substring += word_str;
  3649. lines.push_back(wrap_substring);
  3650. // Update cache.
  3651. text.set_line_wrap_amount(p_line, lines.size() - 1);
  3652. return lines;
  3653. }
  3654. int TextEdit::get_cursor_wrap_index() const {
  3655. return get_line_wrap_index_at_col(cursor.line, cursor.column);
  3656. }
  3657. int TextEdit::get_line_wrap_index_at_col(int p_line, int p_column) const {
  3658. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3659. if (!line_wraps(p_line))
  3660. return 0;
  3661. // Loop through wraps in the line text until we get to the column.
  3662. int wrap_index = 0;
  3663. int col = 0;
  3664. Vector<String> rows = get_wrap_rows_text(p_line);
  3665. for (int i = 0; i < rows.size(); i++) {
  3666. wrap_index = i;
  3667. String s = rows[wrap_index];
  3668. col += s.length();
  3669. if (col > p_column)
  3670. break;
  3671. }
  3672. return wrap_index;
  3673. }
  3674. void TextEdit::cursor_set_column(int p_col, bool p_adjust_viewport) {
  3675. if (p_col < 0)
  3676. p_col = 0;
  3677. cursor.column = p_col;
  3678. if (cursor.column > get_line(cursor.line).length())
  3679. cursor.column = get_line(cursor.line).length();
  3680. cursor.last_fit_x = get_column_x_offset_for_line(cursor.column, cursor.line);
  3681. if (p_adjust_viewport)
  3682. adjust_viewport_to_cursor();
  3683. if (!cursor_changed_dirty) {
  3684. if (is_inside_tree())
  3685. MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
  3686. cursor_changed_dirty = true;
  3687. }
  3688. }
  3689. void TextEdit::cursor_set_line(int p_row, bool p_adjust_viewport, bool p_can_be_hidden, int p_wrap_index) {
  3690. if (setting_row)
  3691. return;
  3692. setting_row = true;
  3693. if (p_row < 0)
  3694. p_row = 0;
  3695. if (p_row >= text.size())
  3696. p_row = text.size() - 1;
  3697. if (!p_can_be_hidden) {
  3698. if (is_line_hidden(CLAMP(p_row, 0, text.size() - 1))) {
  3699. int move_down = num_lines_from(p_row, 1) - 1;
  3700. if (p_row + move_down <= text.size() - 1 && !is_line_hidden(p_row + move_down)) {
  3701. p_row += move_down;
  3702. } else {
  3703. int move_up = num_lines_from(p_row, -1) - 1;
  3704. if (p_row - move_up > 0 && !is_line_hidden(p_row - move_up)) {
  3705. p_row -= move_up;
  3706. } else {
  3707. WARN_PRINT(("Cursor set to hidden line " + itos(p_row) + " and there are no nonhidden lines."));
  3708. }
  3709. }
  3710. }
  3711. }
  3712. cursor.line = p_row;
  3713. int n_col = get_char_pos_for_line(cursor.last_fit_x, p_row, p_wrap_index);
  3714. if (n_col != 0 && is_wrap_enabled() && p_wrap_index < times_line_wraps(p_row)) {
  3715. Vector<String> rows = get_wrap_rows_text(p_row);
  3716. int row_end_col = 0;
  3717. for (int i = 0; i < p_wrap_index + 1; i++) {
  3718. row_end_col += rows[i].length();
  3719. }
  3720. if (n_col >= row_end_col)
  3721. n_col -= 1;
  3722. }
  3723. cursor.column = n_col;
  3724. if (p_adjust_viewport)
  3725. adjust_viewport_to_cursor();
  3726. setting_row = false;
  3727. if (!cursor_changed_dirty) {
  3728. if (is_inside_tree())
  3729. MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
  3730. cursor_changed_dirty = true;
  3731. }
  3732. }
  3733. int TextEdit::cursor_get_column() const {
  3734. return cursor.column;
  3735. }
  3736. int TextEdit::cursor_get_line() const {
  3737. return cursor.line;
  3738. }
  3739. bool TextEdit::cursor_get_blink_enabled() const {
  3740. return caret_blink_enabled;
  3741. }
  3742. void TextEdit::cursor_set_blink_enabled(const bool p_enabled) {
  3743. caret_blink_enabled = p_enabled;
  3744. if (has_focus()) {
  3745. if (p_enabled) {
  3746. caret_blink_timer->start();
  3747. } else {
  3748. caret_blink_timer->stop();
  3749. }
  3750. }
  3751. draw_caret = true;
  3752. }
  3753. float TextEdit::cursor_get_blink_speed() const {
  3754. return caret_blink_timer->get_wait_time();
  3755. }
  3756. void TextEdit::cursor_set_blink_speed(const float p_speed) {
  3757. ERR_FAIL_COND(p_speed <= 0);
  3758. caret_blink_timer->set_wait_time(p_speed);
  3759. }
  3760. void TextEdit::cursor_set_block_mode(const bool p_enable) {
  3761. block_caret = p_enable;
  3762. update();
  3763. }
  3764. bool TextEdit::cursor_is_block_mode() const {
  3765. return block_caret;
  3766. }
  3767. void TextEdit::set_right_click_moves_caret(bool p_enable) {
  3768. right_click_moves_caret = p_enable;
  3769. }
  3770. bool TextEdit::is_right_click_moving_caret() const {
  3771. return right_click_moves_caret;
  3772. }
  3773. void TextEdit::_v_scroll_input() {
  3774. scrolling = false;
  3775. minimap_clicked = false;
  3776. }
  3777. void TextEdit::_scroll_moved(double p_to_val) {
  3778. if (updating_scrolls)
  3779. return;
  3780. if (h_scroll->is_visible_in_tree())
  3781. cursor.x_ofs = h_scroll->get_value();
  3782. if (v_scroll->is_visible_in_tree()) {
  3783. // Set line ofs and wrap ofs.
  3784. int v_scroll_i = floor(get_v_scroll());
  3785. int sc = 0;
  3786. int n_line;
  3787. for (n_line = 0; n_line < text.size(); n_line++) {
  3788. if (!is_line_hidden(n_line)) {
  3789. sc++;
  3790. sc += times_line_wraps(n_line);
  3791. if (sc > v_scroll_i)
  3792. break;
  3793. }
  3794. }
  3795. n_line = MIN(n_line, text.size() - 1);
  3796. int line_wrap_amount = times_line_wraps(n_line);
  3797. int wi = line_wrap_amount - (sc - v_scroll_i - 1);
  3798. wi = CLAMP(wi, 0, line_wrap_amount);
  3799. cursor.line_ofs = n_line;
  3800. cursor.wrap_ofs = wi;
  3801. }
  3802. update();
  3803. }
  3804. int TextEdit::get_row_height() const {
  3805. return cache.font->get_height() + cache.line_spacing;
  3806. }
  3807. int TextEdit::get_char_pos_for_line(int p_px, int p_line, int p_wrap_index) const {
  3808. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3809. if (line_wraps(p_line)) {
  3810. int line_wrap_amount = times_line_wraps(p_line);
  3811. int wrap_offset_px = get_indent_level(p_line) * cache.font->get_char_size(' ').width;
  3812. if (wrap_offset_px >= wrap_at) {
  3813. wrap_offset_px = 0;
  3814. }
  3815. if (p_wrap_index > line_wrap_amount)
  3816. p_wrap_index = line_wrap_amount;
  3817. if (p_wrap_index > 0)
  3818. p_px -= wrap_offset_px;
  3819. else
  3820. p_wrap_index = 0;
  3821. Vector<String> rows = get_wrap_rows_text(p_line);
  3822. int c_pos = get_char_pos_for(p_px, rows[p_wrap_index]);
  3823. for (int i = 0; i < p_wrap_index; i++) {
  3824. String s = rows[i];
  3825. c_pos += s.length();
  3826. }
  3827. return c_pos;
  3828. } else {
  3829. return get_char_pos_for(p_px, text[p_line]);
  3830. }
  3831. }
  3832. int TextEdit::get_column_x_offset_for_line(int p_char, int p_line) const {
  3833. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3834. if (line_wraps(p_line)) {
  3835. int n_char = p_char;
  3836. int col = 0;
  3837. Vector<String> rows = get_wrap_rows_text(p_line);
  3838. int wrap_index = 0;
  3839. for (int i = 0; i < rows.size(); i++) {
  3840. wrap_index = i;
  3841. String s = rows[wrap_index];
  3842. col += s.length();
  3843. if (col > p_char)
  3844. break;
  3845. n_char -= s.length();
  3846. }
  3847. int px = get_column_x_offset(n_char, rows[wrap_index]);
  3848. int wrap_offset_px = get_indent_level(p_line) * cache.font->get_char_size(' ').width;
  3849. if (wrap_offset_px >= wrap_at) {
  3850. wrap_offset_px = 0;
  3851. }
  3852. if (wrap_index != 0)
  3853. px += wrap_offset_px;
  3854. return px;
  3855. } else {
  3856. return get_column_x_offset(p_char, text[p_line]);
  3857. }
  3858. }
  3859. int TextEdit::get_char_pos_for(int p_px, String p_str) const {
  3860. int px = 0;
  3861. int c = 0;
  3862. while (c < p_str.length()) {
  3863. int w = text.get_char_width(p_str[c], p_str[c + 1], px);
  3864. if (p_px < (px + w / 2))
  3865. break;
  3866. px += w;
  3867. c++;
  3868. }
  3869. return c;
  3870. }
  3871. int TextEdit::get_column_x_offset(int p_char, String p_str) const {
  3872. int px = 0;
  3873. for (int i = 0; i < p_char; i++) {
  3874. if (i >= p_str.length())
  3875. break;
  3876. px += text.get_char_width(p_str[i], p_str[i + 1], px);
  3877. }
  3878. return px;
  3879. }
  3880. void TextEdit::insert_text_at_cursor(const String &p_text) {
  3881. if (selection.active) {
  3882. cursor_set_line(selection.from_line);
  3883. cursor_set_column(selection.from_column);
  3884. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3885. selection.active = false;
  3886. selection.selecting_mode = Selection::MODE_NONE;
  3887. }
  3888. _insert_text_at_cursor(p_text);
  3889. update();
  3890. }
  3891. Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
  3892. if (highlighted_word != String())
  3893. return CURSOR_POINTING_HAND;
  3894. int gutter = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width + cache.info_gutter_width;
  3895. if ((completion_active && completion_rect.has_point(p_pos))) {
  3896. return CURSOR_ARROW;
  3897. }
  3898. if (p_pos.x < gutter) {
  3899. int row, col;
  3900. _get_mouse_pos(p_pos, row, col);
  3901. int left_margin = cache.style_normal->get_margin(MARGIN_LEFT);
  3902. // Breakpoint icon.
  3903. if (draw_breakpoint_gutter && p_pos.x > left_margin - 6 && p_pos.x <= left_margin + cache.breakpoint_gutter_width - 3) {
  3904. return CURSOR_POINTING_HAND;
  3905. }
  3906. // Info icons.
  3907. int gutter_left = left_margin + cache.breakpoint_gutter_width + cache.info_gutter_width;
  3908. if (draw_info_gutter && p_pos.x > left_margin + cache.breakpoint_gutter_width - 6 && p_pos.x <= gutter_left - 3) {
  3909. if (text.has_info_icon(row)) {
  3910. return CURSOR_POINTING_HAND;
  3911. }
  3912. return CURSOR_ARROW;
  3913. }
  3914. // Fold icon.
  3915. if (draw_fold_gutter && p_pos.x > gutter_left + cache.line_number_w - 6 && p_pos.x <= gutter_left + cache.line_number_w + cache.fold_gutter_width - 3) {
  3916. if (is_folded(row) || can_fold(row))
  3917. return CURSOR_POINTING_HAND;
  3918. else
  3919. return CURSOR_ARROW;
  3920. }
  3921. return CURSOR_ARROW;
  3922. } else {
  3923. int xmargin_end = get_size().width - cache.style_normal->get_margin(MARGIN_RIGHT);
  3924. if (draw_minimap && p_pos.x > xmargin_end - minimap_width && p_pos.x <= xmargin_end) {
  3925. return CURSOR_ARROW;
  3926. }
  3927. int row, col;
  3928. _get_mouse_pos(p_pos, row, col);
  3929. // EOL fold icon.
  3930. if (is_folded(row)) {
  3931. int line_width = text.get_line_width(row);
  3932. line_width += cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width + cache.info_gutter_width - cursor.x_ofs;
  3933. if (p_pos.x > line_width - 3 && p_pos.x <= line_width + cache.folded_eol_icon->get_width() + 3) {
  3934. return CURSOR_POINTING_HAND;
  3935. }
  3936. }
  3937. }
  3938. return get_default_cursor_shape();
  3939. }
  3940. void TextEdit::set_text(String p_text) {
  3941. setting_text = true;
  3942. if (!undo_enabled) {
  3943. _clear();
  3944. _insert_text_at_cursor(p_text);
  3945. }
  3946. if (undo_enabled) {
  3947. cursor_set_line(0);
  3948. cursor_set_column(0);
  3949. begin_complex_operation();
  3950. _remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0));
  3951. _insert_text_at_cursor(p_text);
  3952. end_complex_operation();
  3953. selection.active = false;
  3954. }
  3955. cursor_set_line(0);
  3956. cursor_set_column(0);
  3957. update();
  3958. setting_text = false;
  3959. };
  3960. String TextEdit::get_text() {
  3961. String longthing;
  3962. int len = text.size();
  3963. for (int i = 0; i < len; i++) {
  3964. longthing += text[i];
  3965. if (i != len - 1)
  3966. longthing += "\n";
  3967. }
  3968. return longthing;
  3969. };
  3970. String TextEdit::get_text_for_lookup_completion() {
  3971. int row, col;
  3972. _get_mouse_pos(get_local_mouse_position(), row, col);
  3973. String longthing;
  3974. int len = text.size();
  3975. for (int i = 0; i < len; i++) {
  3976. if (i == row) {
  3977. longthing += text[i].substr(0, col);
  3978. longthing += String::chr(0xFFFF); // Not unicode, represents the cursor.
  3979. longthing += text[i].substr(col, text[i].size());
  3980. } else {
  3981. longthing += text[i];
  3982. }
  3983. if (i != len - 1)
  3984. longthing += "\n";
  3985. }
  3986. return longthing;
  3987. }
  3988. String TextEdit::get_text_for_completion() {
  3989. String longthing;
  3990. int len = text.size();
  3991. for (int i = 0; i < len; i++) {
  3992. if (i == cursor.line) {
  3993. longthing += text[i].substr(0, cursor.column);
  3994. longthing += String::chr(0xFFFF); // Not unicode, represents the cursor.
  3995. longthing += text[i].substr(cursor.column, text[i].size());
  3996. } else {
  3997. longthing += text[i];
  3998. }
  3999. if (i != len - 1)
  4000. longthing += "\n";
  4001. }
  4002. return longthing;
  4003. };
  4004. String TextEdit::get_line(int line) const {
  4005. if (line < 0 || line >= text.size())
  4006. return "";
  4007. return text[line];
  4008. };
  4009. void TextEdit::_clear() {
  4010. clear_undo_history();
  4011. text.clear();
  4012. cursor.column = 0;
  4013. cursor.line = 0;
  4014. cursor.x_ofs = 0;
  4015. cursor.line_ofs = 0;
  4016. cursor.wrap_ofs = 0;
  4017. cursor.last_fit_x = 0;
  4018. selection.active = false;
  4019. }
  4020. void TextEdit::clear() {
  4021. setting_text = true;
  4022. _clear();
  4023. setting_text = false;
  4024. };
  4025. void TextEdit::set_readonly(bool p_readonly) {
  4026. if (readonly == p_readonly)
  4027. return;
  4028. readonly = p_readonly;
  4029. _generate_context_menu();
  4030. // Reorganize context menu.
  4031. menu->clear();
  4032. if (!readonly) {
  4033. menu->add_item(RTR("Undo"), MENU_UNDO, KEY_MASK_CMD | KEY_Z);
  4034. menu->add_item(RTR("Redo"), MENU_REDO, KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z);
  4035. }
  4036. if (!readonly) {
  4037. menu->add_separator();
  4038. menu->add_item(RTR("Cut"), MENU_CUT, KEY_MASK_CMD | KEY_X);
  4039. }
  4040. menu->add_item(RTR("Copy"), MENU_COPY, KEY_MASK_CMD | KEY_C);
  4041. if (!readonly) {
  4042. menu->add_item(RTR("Paste"), MENU_PASTE, KEY_MASK_CMD | KEY_V);
  4043. }
  4044. menu->add_separator();
  4045. menu->add_item(RTR("Select All"), MENU_SELECT_ALL, KEY_MASK_CMD | KEY_A);
  4046. if (!readonly) {
  4047. menu->add_item(RTR("Clear"), MENU_CLEAR);
  4048. }
  4049. update();
  4050. }
  4051. bool TextEdit::is_readonly() const {
  4052. return readonly;
  4053. }
  4054. void TextEdit::set_wrap_enabled(bool p_wrap_enabled) {
  4055. wrap_enabled = p_wrap_enabled;
  4056. }
  4057. bool TextEdit::is_wrap_enabled() const {
  4058. return wrap_enabled;
  4059. }
  4060. void TextEdit::set_max_chars(int p_max_chars) {
  4061. max_chars = p_max_chars;
  4062. }
  4063. int TextEdit::get_max_chars() const {
  4064. return max_chars;
  4065. }
  4066. void TextEdit::_reset_caret_blink_timer() {
  4067. if (caret_blink_enabled) {
  4068. draw_caret = true;
  4069. if (has_focus()) {
  4070. caret_blink_timer->stop();
  4071. caret_blink_timer->start();
  4072. update();
  4073. }
  4074. }
  4075. }
  4076. void TextEdit::_toggle_draw_caret() {
  4077. draw_caret = !draw_caret;
  4078. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  4079. update();
  4080. }
  4081. }
  4082. void TextEdit::_update_caches() {
  4083. cache.style_normal = get_stylebox("normal");
  4084. cache.style_focus = get_stylebox("focus");
  4085. cache.style_readonly = get_stylebox("read_only");
  4086. cache.completion_background_color = get_color("completion_background_color");
  4087. cache.completion_selected_color = get_color("completion_selected_color");
  4088. cache.completion_existing_color = get_color("completion_existing_color");
  4089. cache.completion_font_color = get_color("completion_font_color");
  4090. cache.font = get_font("font");
  4091. cache.caret_color = get_color("caret_color");
  4092. cache.caret_background_color = get_color("caret_background_color");
  4093. cache.line_number_color = get_color("line_number_color");
  4094. cache.safe_line_number_color = get_color("safe_line_number_color");
  4095. cache.font_color = get_color("font_color");
  4096. cache.font_color_selected = get_color("font_color_selected");
  4097. cache.font_color_readonly = get_color("font_color_readonly");
  4098. cache.keyword_color = get_color("keyword_color");
  4099. cache.function_color = get_color("function_color");
  4100. cache.member_variable_color = get_color("member_variable_color");
  4101. cache.number_color = get_color("number_color");
  4102. cache.selection_color = get_color("selection_color");
  4103. cache.mark_color = get_color("mark_color");
  4104. cache.current_line_color = get_color("current_line_color");
  4105. cache.line_length_guideline_color = get_color("line_length_guideline_color");
  4106. cache.bookmark_color = get_color("bookmark_color");
  4107. cache.breakpoint_color = get_color("breakpoint_color");
  4108. cache.executing_line_color = get_color("executing_line_color");
  4109. cache.code_folding_color = get_color("code_folding_color");
  4110. cache.brace_mismatch_color = get_color("brace_mismatch_color");
  4111. cache.word_highlighted_color = get_color("word_highlighted_color");
  4112. cache.search_result_color = get_color("search_result_color");
  4113. cache.search_result_border_color = get_color("search_result_border_color");
  4114. cache.symbol_color = get_color("symbol_color");
  4115. cache.background_color = get_color("background_color");
  4116. #ifdef TOOLS_ENABLED
  4117. cache.line_spacing = get_constant("line_spacing") * EDSCALE;
  4118. #else
  4119. cache.line_spacing = get_constant("line_spacing");
  4120. #endif
  4121. cache.row_height = cache.font->get_height() + cache.line_spacing;
  4122. cache.tab_icon = get_icon("tab");
  4123. cache.space_icon = get_icon("space");
  4124. cache.folded_icon = get_icon("folded");
  4125. cache.can_fold_icon = get_icon("fold");
  4126. cache.folded_eol_icon = get_icon("GuiEllipsis", "EditorIcons");
  4127. cache.executing_icon = get_icon("MainPlay", "EditorIcons");
  4128. text.set_font(cache.font);
  4129. if (syntax_highlighter) {
  4130. syntax_highlighter->_update_cache();
  4131. }
  4132. }
  4133. SyntaxHighlighter *TextEdit::_get_syntax_highlighting() {
  4134. return syntax_highlighter;
  4135. }
  4136. void TextEdit::_set_syntax_highlighting(SyntaxHighlighter *p_syntax_highlighter) {
  4137. syntax_highlighter = p_syntax_highlighter;
  4138. if (syntax_highlighter) {
  4139. syntax_highlighter->set_text_editor(this);
  4140. syntax_highlighter->_update_cache();
  4141. }
  4142. syntax_highlighting_cache.clear();
  4143. update();
  4144. }
  4145. int TextEdit::_is_line_in_region(int p_line) {
  4146. // Do we have in cache?
  4147. if (color_region_cache.has(p_line)) {
  4148. return color_region_cache[p_line];
  4149. }
  4150. // If not find the closest line we have.
  4151. int previous_line = p_line - 1;
  4152. for (; previous_line > -1; previous_line--) {
  4153. if (color_region_cache.has(p_line)) {
  4154. break;
  4155. }
  4156. }
  4157. // Calculate up to line we need and update the cache along the way.
  4158. int in_region = color_region_cache[previous_line];
  4159. if (previous_line == -1) {
  4160. in_region = -1;
  4161. }
  4162. for (int i = previous_line; i < p_line; i++) {
  4163. const Map<int, Text::ColorRegionInfo> &cri_map = _get_line_color_region_info(i);
  4164. for (const Map<int, Text::ColorRegionInfo>::Element *E = cri_map.front(); E; E = E->next()) {
  4165. const Text::ColorRegionInfo &cri = E->get();
  4166. if (in_region == -1) {
  4167. if (!cri.end) {
  4168. in_region = cri.region;
  4169. }
  4170. } else if (in_region == cri.region && !_get_color_region(cri.region).line_only) {
  4171. if (cri.end || _get_color_region(cri.region).eq) {
  4172. in_region = -1;
  4173. }
  4174. }
  4175. }
  4176. if (in_region >= 0 && _get_color_region(in_region).line_only) {
  4177. in_region = -1;
  4178. }
  4179. color_region_cache[i + 1] = in_region;
  4180. }
  4181. return in_region;
  4182. }
  4183. TextEdit::ColorRegion TextEdit::_get_color_region(int p_region) const {
  4184. if (p_region < 0 || p_region >= color_regions.size()) {
  4185. return ColorRegion();
  4186. }
  4187. return color_regions[p_region];
  4188. }
  4189. Map<int, TextEdit::Text::ColorRegionInfo> TextEdit::_get_line_color_region_info(int p_line) const {
  4190. if (p_line < 0 || p_line > text.size() - 1) {
  4191. return Map<int, Text::ColorRegionInfo>();
  4192. }
  4193. return text.get_color_region_info(p_line);
  4194. }
  4195. void TextEdit::clear_colors() {
  4196. keywords.clear();
  4197. member_keywords.clear();
  4198. color_regions.clear();
  4199. color_region_cache.clear();
  4200. syntax_highlighting_cache.clear();
  4201. text.clear_width_cache();
  4202. update();
  4203. }
  4204. void TextEdit::add_keyword_color(const String &p_keyword, const Color &p_color) {
  4205. keywords[p_keyword] = p_color;
  4206. syntax_highlighting_cache.clear();
  4207. update();
  4208. }
  4209. bool TextEdit::has_keyword_color(String p_keyword) const {
  4210. return keywords.has(p_keyword);
  4211. }
  4212. Color TextEdit::get_keyword_color(String p_keyword) const {
  4213. ERR_FAIL_COND_V(!keywords.has(p_keyword), Color());
  4214. return keywords[p_keyword];
  4215. }
  4216. void TextEdit::add_color_region(const String &p_begin_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
  4217. color_regions.push_back(ColorRegion(p_begin_key, p_end_key, p_color, p_line_only));
  4218. syntax_highlighting_cache.clear();
  4219. text.clear_width_cache();
  4220. update();
  4221. }
  4222. void TextEdit::add_member_keyword(const String &p_keyword, const Color &p_color) {
  4223. member_keywords[p_keyword] = p_color;
  4224. syntax_highlighting_cache.clear();
  4225. update();
  4226. }
  4227. bool TextEdit::has_member_color(String p_member) const {
  4228. return member_keywords.has(p_member);
  4229. }
  4230. Color TextEdit::get_member_color(String p_member) const {
  4231. return member_keywords[p_member];
  4232. }
  4233. void TextEdit::clear_member_keywords() {
  4234. member_keywords.clear();
  4235. syntax_highlighting_cache.clear();
  4236. update();
  4237. }
  4238. void TextEdit::set_syntax_coloring(bool p_enabled) {
  4239. syntax_coloring = p_enabled;
  4240. update();
  4241. }
  4242. bool TextEdit::is_syntax_coloring_enabled() const {
  4243. return syntax_coloring;
  4244. }
  4245. void TextEdit::set_auto_indent(bool p_auto_indent) {
  4246. auto_indent = p_auto_indent;
  4247. }
  4248. void TextEdit::cut() {
  4249. if (!selection.active) {
  4250. String clipboard = text[cursor.line];
  4251. OS::get_singleton()->set_clipboard(clipboard);
  4252. cursor_set_line(cursor.line);
  4253. cursor_set_column(0);
  4254. if (cursor.line == 0 && get_line_count() > 1) {
  4255. _remove_text(cursor.line, 0, cursor.line + 1, 0);
  4256. } else {
  4257. _remove_text(cursor.line, 0, cursor.line, text[cursor.line].length());
  4258. backspace_at_cursor();
  4259. cursor_set_line(cursor.line + 1);
  4260. }
  4261. update();
  4262. cut_copy_line = clipboard;
  4263. } else {
  4264. String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  4265. OS::get_singleton()->set_clipboard(clipboard);
  4266. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  4267. cursor_set_line(selection.from_line); // Set afterwards else it causes the view to be offset.
  4268. cursor_set_column(selection.from_column);
  4269. selection.active = false;
  4270. selection.selecting_mode = Selection::MODE_NONE;
  4271. update();
  4272. cut_copy_line = "";
  4273. }
  4274. }
  4275. void TextEdit::copy() {
  4276. if (!selection.active) {
  4277. if (text[cursor.line].length() != 0) {
  4278. String clipboard = _base_get_text(cursor.line, 0, cursor.line, text[cursor.line].length());
  4279. OS::get_singleton()->set_clipboard(clipboard);
  4280. cut_copy_line = clipboard;
  4281. }
  4282. } else {
  4283. String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  4284. OS::get_singleton()->set_clipboard(clipboard);
  4285. cut_copy_line = "";
  4286. }
  4287. }
  4288. void TextEdit::paste() {
  4289. String clipboard = OS::get_singleton()->get_clipboard();
  4290. begin_complex_operation();
  4291. if (selection.active) {
  4292. selection.active = false;
  4293. selection.selecting_mode = Selection::MODE_NONE;
  4294. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  4295. cursor_set_line(selection.from_line);
  4296. cursor_set_column(selection.from_column);
  4297. } else if (!cut_copy_line.empty() && cut_copy_line == clipboard) {
  4298. cursor_set_column(0);
  4299. String ins = "\n";
  4300. clipboard += ins;
  4301. }
  4302. _insert_text_at_cursor(clipboard);
  4303. end_complex_operation();
  4304. update();
  4305. }
  4306. void TextEdit::select_all() {
  4307. if (!selecting_enabled)
  4308. return;
  4309. if (text.size() == 1 && text[0].length() == 0)
  4310. return;
  4311. selection.active = true;
  4312. selection.from_line = 0;
  4313. selection.from_column = 0;
  4314. selection.selecting_line = 0;
  4315. selection.selecting_column = 0;
  4316. selection.to_line = text.size() - 1;
  4317. selection.to_column = text[selection.to_line].length();
  4318. selection.selecting_mode = Selection::MODE_SHIFT;
  4319. selection.shiftclick_left = true;
  4320. cursor_set_line(selection.to_line, false);
  4321. cursor_set_column(selection.to_column, false);
  4322. update();
  4323. }
  4324. void TextEdit::deselect() {
  4325. selection.active = false;
  4326. update();
  4327. }
  4328. void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  4329. if (!selecting_enabled)
  4330. return;
  4331. if (p_from_line < 0)
  4332. p_from_line = 0;
  4333. else if (p_from_line >= text.size())
  4334. p_from_line = text.size() - 1;
  4335. if (p_from_column >= text[p_from_line].length())
  4336. p_from_column = text[p_from_line].length();
  4337. if (p_from_column < 0)
  4338. p_from_column = 0;
  4339. if (p_to_line < 0)
  4340. p_to_line = 0;
  4341. else if (p_to_line >= text.size())
  4342. p_to_line = text.size() - 1;
  4343. if (p_to_column >= text[p_to_line].length())
  4344. p_to_column = text[p_to_line].length();
  4345. if (p_to_column < 0)
  4346. p_to_column = 0;
  4347. selection.from_line = p_from_line;
  4348. selection.from_column = p_from_column;
  4349. selection.to_line = p_to_line;
  4350. selection.to_column = p_to_column;
  4351. selection.active = true;
  4352. if (selection.from_line == selection.to_line) {
  4353. if (selection.from_column == selection.to_column) {
  4354. selection.active = false;
  4355. } else if (selection.from_column > selection.to_column) {
  4356. selection.shiftclick_left = false;
  4357. SWAP(selection.from_column, selection.to_column);
  4358. } else {
  4359. selection.shiftclick_left = true;
  4360. }
  4361. } else if (selection.from_line > selection.to_line) {
  4362. selection.shiftclick_left = false;
  4363. SWAP(selection.from_line, selection.to_line);
  4364. SWAP(selection.from_column, selection.to_column);
  4365. } else {
  4366. selection.shiftclick_left = true;
  4367. }
  4368. update();
  4369. }
  4370. void TextEdit::swap_lines(int line1, int line2) {
  4371. String tmp = get_line(line1);
  4372. String tmp2 = get_line(line2);
  4373. set_line(line2, tmp);
  4374. set_line(line1, tmp2);
  4375. }
  4376. bool TextEdit::is_selection_active() const {
  4377. return selection.active;
  4378. }
  4379. int TextEdit::get_selection_from_line() const {
  4380. ERR_FAIL_COND_V(!selection.active, -1);
  4381. return selection.from_line;
  4382. }
  4383. int TextEdit::get_selection_from_column() const {
  4384. ERR_FAIL_COND_V(!selection.active, -1);
  4385. return selection.from_column;
  4386. }
  4387. int TextEdit::get_selection_to_line() const {
  4388. ERR_FAIL_COND_V(!selection.active, -1);
  4389. return selection.to_line;
  4390. }
  4391. int TextEdit::get_selection_to_column() const {
  4392. ERR_FAIL_COND_V(!selection.active, -1);
  4393. return selection.to_column;
  4394. }
  4395. String TextEdit::get_selection_text() const {
  4396. if (!selection.active)
  4397. return "";
  4398. return _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  4399. }
  4400. String TextEdit::get_word_under_cursor() const {
  4401. int prev_cc = cursor.column;
  4402. while (prev_cc > 0) {
  4403. bool is_char = _is_text_char(text[cursor.line][prev_cc - 1]);
  4404. if (!is_char)
  4405. break;
  4406. --prev_cc;
  4407. }
  4408. int next_cc = cursor.column;
  4409. while (next_cc < text[cursor.line].length()) {
  4410. bool is_char = _is_text_char(text[cursor.line][next_cc]);
  4411. if (!is_char)
  4412. break;
  4413. ++next_cc;
  4414. }
  4415. if (prev_cc == cursor.column || next_cc == cursor.column)
  4416. return "";
  4417. return text[cursor.line].substr(prev_cc, next_cc - prev_cc);
  4418. }
  4419. void TextEdit::set_search_text(const String &p_search_text) {
  4420. search_text = p_search_text;
  4421. }
  4422. void TextEdit::set_search_flags(uint32_t p_flags) {
  4423. search_flags = p_flags;
  4424. }
  4425. void TextEdit::set_current_search_result(int line, int col) {
  4426. search_result_line = line;
  4427. search_result_col = col;
  4428. update();
  4429. }
  4430. void TextEdit::set_highlight_all_occurrences(const bool p_enabled) {
  4431. highlight_all_occurrences = p_enabled;
  4432. update();
  4433. }
  4434. bool TextEdit::is_highlight_all_occurrences_enabled() const {
  4435. return highlight_all_occurrences;
  4436. }
  4437. int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) {
  4438. int col = -1;
  4439. if (p_key.length() > 0 && p_search.length() > 0) {
  4440. if (p_from_column < 0 || p_from_column > p_search.length()) {
  4441. p_from_column = 0;
  4442. }
  4443. while (col == -1 && p_from_column <= p_search.length()) {
  4444. if (p_search_flags & SEARCH_MATCH_CASE) {
  4445. col = p_search.find(p_key, p_from_column);
  4446. } else {
  4447. col = p_search.findn(p_key, p_from_column);
  4448. }
  4449. // Whole words only.
  4450. if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) {
  4451. p_from_column = col;
  4452. if (col > 0 && _is_text_char(p_search[col - 1])) {
  4453. col = -1;
  4454. } else if ((col + p_key.length()) < p_search.length() && _is_text_char(p_search[col + p_key.length()])) {
  4455. col = -1;
  4456. }
  4457. }
  4458. p_from_column += 1;
  4459. }
  4460. }
  4461. return col;
  4462. }
  4463. Vector<int> TextEdit::_search_bind(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const {
  4464. int col, line;
  4465. if (search(p_key, p_search_flags, p_from_line, p_from_column, line, col)) {
  4466. Vector<int> result;
  4467. result.resize(2);
  4468. result.set(SEARCH_RESULT_COLUMN, col);
  4469. result.set(SEARCH_RESULT_LINE, line);
  4470. return result;
  4471. } else {
  4472. return Vector<int>();
  4473. }
  4474. }
  4475. bool TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column, int &r_line, int &r_column) const {
  4476. if (p_key.length() == 0)
  4477. return false;
  4478. ERR_FAIL_INDEX_V(p_from_line, text.size(), false);
  4479. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, false);
  4480. // Search through the whole document, but start by current line.
  4481. int line = p_from_line;
  4482. int pos = -1;
  4483. for (int i = 0; i < text.size() + 1; i++) {
  4484. if (line < 0) {
  4485. line = text.size() - 1;
  4486. }
  4487. if (line == text.size()) {
  4488. line = 0;
  4489. }
  4490. String text_line = text[line];
  4491. int from_column = 0;
  4492. if (line == p_from_line) {
  4493. if (i == text.size()) {
  4494. // Wrapped.
  4495. if (p_search_flags & SEARCH_BACKWARDS) {
  4496. from_column = text_line.length();
  4497. } else {
  4498. from_column = 0;
  4499. }
  4500. } else {
  4501. from_column = p_from_column;
  4502. }
  4503. } else {
  4504. if (p_search_flags & SEARCH_BACKWARDS)
  4505. from_column = text_line.length() - 1;
  4506. else
  4507. from_column = 0;
  4508. }
  4509. pos = -1;
  4510. int pos_from = (p_search_flags & SEARCH_BACKWARDS) ? text_line.length() : 0;
  4511. int last_pos = -1;
  4512. while (true) {
  4513. if (p_search_flags & SEARCH_BACKWARDS) {
  4514. while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.rfind(p_key, pos_from) : text_line.rfindn(p_key, pos_from)) != -1) {
  4515. if (last_pos <= from_column) {
  4516. pos = last_pos;
  4517. break;
  4518. }
  4519. pos_from = last_pos - p_key.length();
  4520. if (pos_from < 0) {
  4521. break;
  4522. }
  4523. }
  4524. } else {
  4525. while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, pos_from) : text_line.findn(p_key, pos_from)) != -1) {
  4526. if (last_pos >= from_column) {
  4527. pos = last_pos;
  4528. break;
  4529. }
  4530. pos_from = last_pos + p_key.length();
  4531. }
  4532. }
  4533. bool is_match = true;
  4534. if (pos != -1 && (p_search_flags & SEARCH_WHOLE_WORDS)) {
  4535. // Validate for whole words.
  4536. if (pos > 0 && _is_text_char(text_line[pos - 1]))
  4537. is_match = false;
  4538. else if (pos + p_key.length() < text_line.length() && _is_text_char(text_line[pos + p_key.length()]))
  4539. is_match = false;
  4540. }
  4541. if (pos_from == -1) {
  4542. pos = -1;
  4543. }
  4544. if (is_match || last_pos == -1 || pos == -1) {
  4545. break;
  4546. }
  4547. pos_from = (p_search_flags & SEARCH_BACKWARDS) ? pos - 1 : pos + 1;
  4548. pos = -1;
  4549. }
  4550. if (pos != -1)
  4551. break;
  4552. if (p_search_flags & SEARCH_BACKWARDS)
  4553. line--;
  4554. else
  4555. line++;
  4556. }
  4557. if (pos == -1) {
  4558. r_line = -1;
  4559. r_column = -1;
  4560. return false;
  4561. }
  4562. r_line = line;
  4563. r_column = pos;
  4564. return true;
  4565. }
  4566. void TextEdit::_cursor_changed_emit() {
  4567. emit_signal("cursor_changed");
  4568. cursor_changed_dirty = false;
  4569. }
  4570. void TextEdit::_text_changed_emit() {
  4571. emit_signal("text_changed");
  4572. text_changed_dirty = false;
  4573. }
  4574. void TextEdit::set_line_as_marked(int p_line, bool p_marked) {
  4575. ERR_FAIL_INDEX(p_line, text.size());
  4576. text.set_marked(p_line, p_marked);
  4577. update();
  4578. }
  4579. void TextEdit::set_line_as_safe(int p_line, bool p_safe) {
  4580. ERR_FAIL_INDEX(p_line, text.size());
  4581. text.set_safe(p_line, p_safe);
  4582. update();
  4583. }
  4584. bool TextEdit::is_line_set_as_safe(int p_line) const {
  4585. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4586. return text.is_safe(p_line);
  4587. }
  4588. void TextEdit::set_executing_line(int p_line) {
  4589. ERR_FAIL_INDEX(p_line, text.size());
  4590. executing_line = p_line;
  4591. update();
  4592. }
  4593. void TextEdit::clear_executing_line() {
  4594. executing_line = -1;
  4595. update();
  4596. }
  4597. bool TextEdit::is_line_set_as_bookmark(int p_line) const {
  4598. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4599. return text.is_bookmark(p_line);
  4600. }
  4601. void TextEdit::set_line_as_bookmark(int p_line, bool p_bookmark) {
  4602. ERR_FAIL_INDEX(p_line, text.size());
  4603. text.set_bookmark(p_line, p_bookmark);
  4604. update();
  4605. }
  4606. void TextEdit::get_bookmarks(List<int> *p_bookmarks) const {
  4607. for (int i = 0; i < text.size(); i++) {
  4608. if (text.is_bookmark(i))
  4609. p_bookmarks->push_back(i);
  4610. }
  4611. }
  4612. Array TextEdit::get_bookmarks_array() const {
  4613. Array arr;
  4614. for (int i = 0; i < text.size(); i++) {
  4615. if (text.is_bookmark(i))
  4616. arr.append(i);
  4617. }
  4618. return arr;
  4619. }
  4620. bool TextEdit::is_line_set_as_breakpoint(int p_line) const {
  4621. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4622. return text.is_breakpoint(p_line);
  4623. }
  4624. void TextEdit::set_line_as_breakpoint(int p_line, bool p_breakpoint) {
  4625. ERR_FAIL_INDEX(p_line, text.size());
  4626. text.set_breakpoint(p_line, p_breakpoint);
  4627. update();
  4628. }
  4629. void TextEdit::get_breakpoints(List<int> *p_breakpoints) const {
  4630. for (int i = 0; i < text.size(); i++) {
  4631. if (text.is_breakpoint(i))
  4632. p_breakpoints->push_back(i);
  4633. }
  4634. }
  4635. Array TextEdit::get_breakpoints_array() const {
  4636. Array arr;
  4637. for (int i = 0; i < text.size(); i++) {
  4638. if (text.is_breakpoint(i))
  4639. arr.append(i);
  4640. }
  4641. return arr;
  4642. }
  4643. void TextEdit::remove_breakpoints() {
  4644. for (int i = 0; i < text.size(); i++) {
  4645. if (text.is_breakpoint(i))
  4646. /* Should "breakpoint_toggled" be fired when breakpoints are removed this way? */
  4647. text.set_breakpoint(i, false);
  4648. }
  4649. }
  4650. void TextEdit::set_line_info_icon(int p_line, Ref<Texture2D> p_icon, String p_info) {
  4651. ERR_FAIL_INDEX(p_line, text.size());
  4652. text.set_info_icon(p_line, p_icon, p_info);
  4653. update();
  4654. }
  4655. void TextEdit::clear_info_icons() {
  4656. text.clear_info_icons();
  4657. update();
  4658. }
  4659. void TextEdit::set_line_as_hidden(int p_line, bool p_hidden) {
  4660. ERR_FAIL_INDEX(p_line, text.size());
  4661. if (is_hiding_enabled() || !p_hidden)
  4662. text.set_hidden(p_line, p_hidden);
  4663. update();
  4664. }
  4665. bool TextEdit::is_line_hidden(int p_line) const {
  4666. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4667. return text.is_hidden(p_line);
  4668. }
  4669. void TextEdit::fold_all_lines() {
  4670. for (int i = 0; i < text.size(); i++) {
  4671. fold_line(i);
  4672. }
  4673. _update_scrollbars();
  4674. update();
  4675. }
  4676. void TextEdit::unhide_all_lines() {
  4677. for (int i = 0; i < text.size(); i++) {
  4678. text.set_hidden(i, false);
  4679. }
  4680. _update_scrollbars();
  4681. update();
  4682. }
  4683. int TextEdit::num_lines_from(int p_line_from, int visible_amount) const {
  4684. // Returns the number of lines (hidden and unhidden) from p_line_from to (p_line_from + visible_amount of unhidden lines).
  4685. ERR_FAIL_INDEX_V(p_line_from, text.size(), ABS(visible_amount));
  4686. if (!is_hiding_enabled())
  4687. return ABS(visible_amount);
  4688. int num_visible = 0;
  4689. int num_total = 0;
  4690. if (visible_amount >= 0) {
  4691. for (int i = p_line_from; i < text.size(); i++) {
  4692. num_total++;
  4693. if (!is_line_hidden(i)) {
  4694. num_visible++;
  4695. }
  4696. if (num_visible >= visible_amount)
  4697. break;
  4698. }
  4699. } else {
  4700. visible_amount = ABS(visible_amount);
  4701. for (int i = p_line_from; i >= 0; i--) {
  4702. num_total++;
  4703. if (!is_line_hidden(i)) {
  4704. num_visible++;
  4705. }
  4706. if (num_visible >= visible_amount)
  4707. break;
  4708. }
  4709. }
  4710. return num_total;
  4711. }
  4712. int TextEdit::num_lines_from_rows(int p_line_from, int p_wrap_index_from, int visible_amount, int &wrap_index) const {
  4713. // Returns the number of lines (hidden and unhidden) from (p_line_from + p_wrap_index_from) row to (p_line_from + visible_amount of unhidden and wrapped rows).
  4714. // Wrap index is set to the wrap index of the last line.
  4715. wrap_index = 0;
  4716. ERR_FAIL_INDEX_V(p_line_from, text.size(), ABS(visible_amount));
  4717. if (!is_hiding_enabled() && !is_wrap_enabled())
  4718. return ABS(visible_amount);
  4719. int num_visible = 0;
  4720. int num_total = 0;
  4721. if (visible_amount == 0) {
  4722. num_total = 0;
  4723. wrap_index = 0;
  4724. } else if (visible_amount > 0) {
  4725. int i;
  4726. num_visible -= p_wrap_index_from;
  4727. for (i = p_line_from; i < text.size(); i++) {
  4728. num_total++;
  4729. if (!is_line_hidden(i)) {
  4730. num_visible++;
  4731. num_visible += times_line_wraps(i);
  4732. }
  4733. if (num_visible >= visible_amount)
  4734. break;
  4735. }
  4736. wrap_index = times_line_wraps(MIN(i, text.size() - 1)) - (num_visible - visible_amount);
  4737. } else {
  4738. visible_amount = ABS(visible_amount);
  4739. int i;
  4740. num_visible -= times_line_wraps(p_line_from) - p_wrap_index_from;
  4741. for (i = p_line_from; i >= 0; i--) {
  4742. num_total++;
  4743. if (!is_line_hidden(i)) {
  4744. num_visible++;
  4745. num_visible += times_line_wraps(i);
  4746. }
  4747. if (num_visible >= visible_amount)
  4748. break;
  4749. }
  4750. wrap_index = (num_visible - visible_amount);
  4751. }
  4752. wrap_index = MAX(wrap_index, 0);
  4753. return num_total;
  4754. }
  4755. int TextEdit::get_last_unhidden_line() const {
  4756. // Returns the last line in the text that is not hidden.
  4757. if (!is_hiding_enabled())
  4758. return text.size() - 1;
  4759. int last_line;
  4760. for (last_line = text.size() - 1; last_line > 0; last_line--) {
  4761. if (!is_line_hidden(last_line)) {
  4762. break;
  4763. }
  4764. }
  4765. return last_line;
  4766. }
  4767. int TextEdit::get_indent_level(int p_line) const {
  4768. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  4769. // Counts number of tabs and spaces before line starts.
  4770. int tab_count = 0;
  4771. int whitespace_count = 0;
  4772. int line_length = text[p_line].size();
  4773. for (int i = 0; i < line_length - 1; i++) {
  4774. if (text[p_line][i] == '\t') {
  4775. tab_count++;
  4776. } else if (text[p_line][i] == ' ') {
  4777. whitespace_count++;
  4778. } else {
  4779. break;
  4780. }
  4781. }
  4782. return tab_count * indent_size + whitespace_count;
  4783. }
  4784. bool TextEdit::is_line_comment(int p_line) const {
  4785. // Checks to see if this line is the start of a comment.
  4786. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4787. const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(p_line);
  4788. int line_length = text[p_line].size();
  4789. for (int i = 0; i < line_length - 1; i++) {
  4790. if (_is_symbol(text[p_line][i]) && cri_map.has(i)) {
  4791. const Text::ColorRegionInfo &cri = cri_map[i];
  4792. return color_regions[cri.region].begin_key == "#" || color_regions[cri.region].begin_key == "//";
  4793. } else if (_is_whitespace(text[p_line][i])) {
  4794. continue;
  4795. } else {
  4796. break;
  4797. }
  4798. }
  4799. return false;
  4800. }
  4801. bool TextEdit::can_fold(int p_line) const {
  4802. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4803. if (!is_hiding_enabled())
  4804. return false;
  4805. if (p_line + 1 >= text.size())
  4806. return false;
  4807. if (text[p_line].strip_edges().size() == 0)
  4808. return false;
  4809. if (is_folded(p_line))
  4810. return false;
  4811. if (is_line_hidden(p_line))
  4812. return false;
  4813. if (is_line_comment(p_line))
  4814. return false;
  4815. int start_indent = get_indent_level(p_line);
  4816. for (int i = p_line + 1; i < text.size(); i++) {
  4817. if (text[i].strip_edges().size() == 0)
  4818. continue;
  4819. int next_indent = get_indent_level(i);
  4820. if (is_line_comment(i)) {
  4821. continue;
  4822. } else if (next_indent > start_indent) {
  4823. return true;
  4824. } else {
  4825. return false;
  4826. }
  4827. }
  4828. return false;
  4829. }
  4830. bool TextEdit::is_folded(int p_line) const {
  4831. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4832. if (p_line + 1 >= text.size())
  4833. return false;
  4834. return !is_line_hidden(p_line) && is_line_hidden(p_line + 1);
  4835. }
  4836. Vector<int> TextEdit::get_folded_lines() const {
  4837. Vector<int> folded_lines;
  4838. for (int i = 0; i < text.size(); i++) {
  4839. if (is_folded(i)) {
  4840. folded_lines.push_back(i);
  4841. }
  4842. }
  4843. return folded_lines;
  4844. }
  4845. void TextEdit::fold_line(int p_line) {
  4846. ERR_FAIL_INDEX(p_line, text.size());
  4847. if (!is_hiding_enabled())
  4848. return;
  4849. if (!can_fold(p_line))
  4850. return;
  4851. // Hide lines below this one.
  4852. int start_indent = get_indent_level(p_line);
  4853. int last_line = start_indent;
  4854. for (int i = p_line + 1; i < text.size(); i++) {
  4855. if (text[i].strip_edges().size() != 0) {
  4856. if (is_line_comment(i)) {
  4857. continue;
  4858. } else if (get_indent_level(i) > start_indent) {
  4859. last_line = i;
  4860. } else {
  4861. break;
  4862. }
  4863. }
  4864. }
  4865. for (int i = p_line + 1; i <= last_line; i++) {
  4866. set_line_as_hidden(i, true);
  4867. }
  4868. // Fix selection.
  4869. if (is_selection_active()) {
  4870. if (is_line_hidden(selection.from_line) && is_line_hidden(selection.to_line)) {
  4871. deselect();
  4872. } else if (is_line_hidden(selection.from_line)) {
  4873. select(p_line, 9999, selection.to_line, selection.to_column);
  4874. } else if (is_line_hidden(selection.to_line)) {
  4875. select(selection.from_line, selection.from_column, p_line, 9999);
  4876. }
  4877. }
  4878. // Reset cursor.
  4879. if (is_line_hidden(cursor.line)) {
  4880. cursor_set_line(p_line, false, false);
  4881. cursor_set_column(get_line(p_line).length(), false);
  4882. }
  4883. _update_scrollbars();
  4884. update();
  4885. }
  4886. void TextEdit::unfold_line(int p_line) {
  4887. ERR_FAIL_INDEX(p_line, text.size());
  4888. if (!is_folded(p_line) && !is_line_hidden(p_line))
  4889. return;
  4890. int fold_start;
  4891. for (fold_start = p_line; fold_start > 0; fold_start--) {
  4892. if (is_folded(fold_start))
  4893. break;
  4894. }
  4895. fold_start = is_folded(fold_start) ? fold_start : p_line;
  4896. for (int i = fold_start + 1; i < text.size(); i++) {
  4897. if (is_line_hidden(i)) {
  4898. set_line_as_hidden(i, false);
  4899. } else {
  4900. break;
  4901. }
  4902. }
  4903. _update_scrollbars();
  4904. update();
  4905. }
  4906. void TextEdit::toggle_fold_line(int p_line) {
  4907. ERR_FAIL_INDEX(p_line, text.size());
  4908. if (!is_folded(p_line))
  4909. fold_line(p_line);
  4910. else
  4911. unfold_line(p_line);
  4912. }
  4913. int TextEdit::get_line_count() const {
  4914. return text.size();
  4915. }
  4916. void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) {
  4917. ERR_FAIL_COND(p_op.type == TextOperation::TYPE_NONE);
  4918. bool insert = p_op.type == TextOperation::TYPE_INSERT;
  4919. if (p_reverse)
  4920. insert = !insert;
  4921. if (insert) {
  4922. int check_line;
  4923. int check_column;
  4924. _base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column);
  4925. ERR_FAIL_COND(check_line != p_op.to_line); // BUG.
  4926. ERR_FAIL_COND(check_column != p_op.to_column); // BUG.
  4927. } else {
  4928. _base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column);
  4929. }
  4930. }
  4931. void TextEdit::_clear_redo() {
  4932. if (undo_stack_pos == NULL)
  4933. return; // Nothing to clear.
  4934. _push_current_op();
  4935. while (undo_stack_pos) {
  4936. List<TextOperation>::Element *elem = undo_stack_pos;
  4937. undo_stack_pos = undo_stack_pos->next();
  4938. undo_stack.erase(elem);
  4939. }
  4940. }
  4941. void TextEdit::undo() {
  4942. _push_current_op();
  4943. if (undo_stack_pos == NULL) {
  4944. if (!undo_stack.size())
  4945. return; // Nothing to undo.
  4946. undo_stack_pos = undo_stack.back();
  4947. } else if (undo_stack_pos == undo_stack.front())
  4948. return; // At the bottom of the undo stack.
  4949. else
  4950. undo_stack_pos = undo_stack_pos->prev();
  4951. deselect();
  4952. TextOperation op = undo_stack_pos->get();
  4953. _do_text_op(op, true);
  4954. if (op.type != TextOperation::TYPE_INSERT && (op.from_line != op.to_line || op.to_column != op.from_column + 1))
  4955. select(op.from_line, op.from_column, op.to_line, op.to_column);
  4956. current_op.version = op.prev_version;
  4957. if (undo_stack_pos->get().chain_backward) {
  4958. while (true) {
  4959. ERR_BREAK(!undo_stack_pos->prev());
  4960. undo_stack_pos = undo_stack_pos->prev();
  4961. op = undo_stack_pos->get();
  4962. _do_text_op(op, true);
  4963. current_op.version = op.prev_version;
  4964. if (undo_stack_pos->get().chain_forward) {
  4965. break;
  4966. }
  4967. }
  4968. }
  4969. _update_scrollbars();
  4970. if (undo_stack_pos->get().type == TextOperation::TYPE_REMOVE) {
  4971. cursor_set_line(undo_stack_pos->get().to_line);
  4972. cursor_set_column(undo_stack_pos->get().to_column);
  4973. _cancel_code_hint();
  4974. } else {
  4975. cursor_set_line(undo_stack_pos->get().from_line);
  4976. cursor_set_column(undo_stack_pos->get().from_column);
  4977. }
  4978. update();
  4979. }
  4980. void TextEdit::redo() {
  4981. _push_current_op();
  4982. if (undo_stack_pos == NULL)
  4983. return; // Nothing to do.
  4984. deselect();
  4985. TextOperation op = undo_stack_pos->get();
  4986. _do_text_op(op, false);
  4987. current_op.version = op.version;
  4988. if (undo_stack_pos->get().chain_forward) {
  4989. while (true) {
  4990. ERR_BREAK(!undo_stack_pos->next());
  4991. undo_stack_pos = undo_stack_pos->next();
  4992. op = undo_stack_pos->get();
  4993. _do_text_op(op, false);
  4994. current_op.version = op.version;
  4995. if (undo_stack_pos->get().chain_backward)
  4996. break;
  4997. }
  4998. }
  4999. _update_scrollbars();
  5000. cursor_set_line(undo_stack_pos->get().to_line);
  5001. cursor_set_column(undo_stack_pos->get().to_column);
  5002. undo_stack_pos = undo_stack_pos->next();
  5003. update();
  5004. }
  5005. void TextEdit::clear_undo_history() {
  5006. saved_version = 0;
  5007. current_op.type = TextOperation::TYPE_NONE;
  5008. undo_stack_pos = NULL;
  5009. undo_stack.clear();
  5010. }
  5011. void TextEdit::begin_complex_operation() {
  5012. _push_current_op();
  5013. next_operation_is_complex = true;
  5014. }
  5015. void TextEdit::end_complex_operation() {
  5016. _push_current_op();
  5017. ERR_FAIL_COND(undo_stack.size() == 0);
  5018. if (undo_stack.back()->get().chain_forward) {
  5019. undo_stack.back()->get().chain_forward = false;
  5020. return;
  5021. }
  5022. undo_stack.back()->get().chain_backward = true;
  5023. }
  5024. void TextEdit::_push_current_op() {
  5025. if (current_op.type == TextOperation::TYPE_NONE)
  5026. return; // Nothing to do.
  5027. if (next_operation_is_complex) {
  5028. current_op.chain_forward = true;
  5029. next_operation_is_complex = false;
  5030. }
  5031. undo_stack.push_back(current_op);
  5032. current_op.type = TextOperation::TYPE_NONE;
  5033. current_op.text = "";
  5034. current_op.chain_forward = false;
  5035. }
  5036. void TextEdit::set_indent_using_spaces(const bool p_use_spaces) {
  5037. indent_using_spaces = p_use_spaces;
  5038. }
  5039. bool TextEdit::is_indent_using_spaces() const {
  5040. return indent_using_spaces;
  5041. }
  5042. void TextEdit::set_indent_size(const int p_size) {
  5043. ERR_FAIL_COND_MSG(p_size <= 0, "Indend size must be greater than 0.");
  5044. indent_size = p_size;
  5045. text.set_indent_size(p_size);
  5046. space_indent = "";
  5047. for (int i = 0; i < p_size; i++) {
  5048. space_indent += " ";
  5049. }
  5050. update();
  5051. }
  5052. int TextEdit::get_indent_size() {
  5053. return indent_size;
  5054. }
  5055. void TextEdit::set_draw_tabs(bool p_draw) {
  5056. draw_tabs = p_draw;
  5057. update();
  5058. }
  5059. bool TextEdit::is_drawing_tabs() const {
  5060. return draw_tabs;
  5061. }
  5062. void TextEdit::set_draw_spaces(bool p_draw) {
  5063. draw_spaces = p_draw;
  5064. }
  5065. bool TextEdit::is_drawing_spaces() const {
  5066. return draw_spaces;
  5067. }
  5068. void TextEdit::set_override_selected_font_color(bool p_override_selected_font_color) {
  5069. override_selected_font_color = p_override_selected_font_color;
  5070. }
  5071. bool TextEdit::is_overriding_selected_font_color() const {
  5072. return override_selected_font_color;
  5073. }
  5074. void TextEdit::set_insert_mode(bool p_enabled) {
  5075. insert_mode = p_enabled;
  5076. update();
  5077. }
  5078. bool TextEdit::is_insert_mode() const {
  5079. return insert_mode;
  5080. }
  5081. bool TextEdit::is_insert_text_operation() {
  5082. return (current_op.type == TextOperation::TYPE_INSERT);
  5083. }
  5084. uint32_t TextEdit::get_version() const {
  5085. return current_op.version;
  5086. }
  5087. uint32_t TextEdit::get_saved_version() const {
  5088. return saved_version;
  5089. }
  5090. void TextEdit::tag_saved_version() {
  5091. saved_version = get_version();
  5092. }
  5093. double TextEdit::get_scroll_pos_for_line(int p_line, int p_wrap_index) const {
  5094. if (!is_wrap_enabled() && !is_hiding_enabled())
  5095. return p_line;
  5096. // Count the number of visible lines up to this line.
  5097. double new_line_scroll_pos = 0;
  5098. int to = CLAMP(p_line, 0, text.size() - 1);
  5099. for (int i = 0; i < to; i++) {
  5100. if (!text.is_hidden(i)) {
  5101. new_line_scroll_pos++;
  5102. new_line_scroll_pos += times_line_wraps(i);
  5103. }
  5104. }
  5105. new_line_scroll_pos += p_wrap_index;
  5106. return new_line_scroll_pos;
  5107. }
  5108. void TextEdit::set_line_as_first_visible(int p_line, int p_wrap_index) {
  5109. set_v_scroll(get_scroll_pos_for_line(p_line, p_wrap_index));
  5110. }
  5111. void TextEdit::set_line_as_center_visible(int p_line, int p_wrap_index) {
  5112. int visible_rows = get_visible_rows();
  5113. int wi;
  5114. int first_line = p_line - num_lines_from_rows(p_line, p_wrap_index, -visible_rows / 2, wi) + 1;
  5115. set_v_scroll(get_scroll_pos_for_line(first_line, wi));
  5116. }
  5117. void TextEdit::set_line_as_last_visible(int p_line, int p_wrap_index) {
  5118. int wi;
  5119. int first_line = p_line - num_lines_from_rows(p_line, p_wrap_index, -get_visible_rows() - 1, wi) + 1;
  5120. set_v_scroll(get_scroll_pos_for_line(first_line, wi) + get_visible_rows_offset());
  5121. }
  5122. int TextEdit::get_first_visible_line() const {
  5123. return CLAMP(cursor.line_ofs, 0, text.size() - 1);
  5124. }
  5125. int TextEdit::get_last_visible_line() const {
  5126. int first_vis_line = get_first_visible_line();
  5127. int last_vis_line = 0;
  5128. int wi;
  5129. last_vis_line = first_vis_line + num_lines_from_rows(first_vis_line, cursor.wrap_ofs, get_visible_rows() + 1, wi) - 1;
  5130. last_vis_line = CLAMP(last_vis_line, 0, text.size() - 1);
  5131. return last_vis_line;
  5132. }
  5133. int TextEdit::get_last_visible_line_wrap_index() const {
  5134. int first_vis_line = get_first_visible_line();
  5135. int wi;
  5136. num_lines_from_rows(first_vis_line, cursor.wrap_ofs, get_visible_rows() + 1, wi);
  5137. return wi;
  5138. }
  5139. double TextEdit::get_visible_rows_offset() const {
  5140. double total = _get_control_height();
  5141. total /= (double)get_row_height();
  5142. total = total - floor(total);
  5143. total = -CLAMP(total, 0.001, 1) + 1;
  5144. return total;
  5145. }
  5146. double TextEdit::get_v_scroll_offset() const {
  5147. double val = get_v_scroll() - floor(get_v_scroll());
  5148. return CLAMP(val, 0, 1);
  5149. }
  5150. double TextEdit::get_v_scroll() const {
  5151. return v_scroll->get_value();
  5152. }
  5153. void TextEdit::set_v_scroll(double p_scroll) {
  5154. v_scroll->set_value(p_scroll);
  5155. int max_v_scroll = v_scroll->get_max() - v_scroll->get_page();
  5156. if (p_scroll >= max_v_scroll - 1.0)
  5157. _scroll_moved(v_scroll->get_value());
  5158. }
  5159. int TextEdit::get_h_scroll() const {
  5160. return h_scroll->get_value();
  5161. }
  5162. void TextEdit::set_h_scroll(int p_scroll) {
  5163. if (p_scroll < 0) {
  5164. p_scroll = 0;
  5165. }
  5166. h_scroll->set_value(p_scroll);
  5167. }
  5168. void TextEdit::set_smooth_scroll_enabled(bool p_enable) {
  5169. v_scroll->set_smooth_scroll_enabled(p_enable);
  5170. smooth_scroll_enabled = p_enable;
  5171. }
  5172. bool TextEdit::is_smooth_scroll_enabled() const {
  5173. return smooth_scroll_enabled;
  5174. }
  5175. void TextEdit::set_v_scroll_speed(float p_speed) {
  5176. v_scroll_speed = p_speed;
  5177. }
  5178. float TextEdit::get_v_scroll_speed() const {
  5179. return v_scroll_speed;
  5180. }
  5181. void TextEdit::set_completion(bool p_enabled, const Vector<String> &p_prefixes) {
  5182. completion_prefixes.clear();
  5183. completion_enabled = p_enabled;
  5184. for (int i = 0; i < p_prefixes.size(); i++)
  5185. completion_prefixes.insert(p_prefixes[i]);
  5186. }
  5187. void TextEdit::_confirm_completion() {
  5188. begin_complex_operation();
  5189. _remove_text(cursor.line, cursor.column - completion_base.length(), cursor.line, cursor.column);
  5190. cursor_set_column(cursor.column - completion_base.length(), false);
  5191. insert_text_at_cursor(completion_current.insert_text);
  5192. // When inserted into the middle of an existing string/method, don't add an unnecessary quote/bracket.
  5193. String line = text[cursor.line];
  5194. CharType next_char = line[cursor.column];
  5195. CharType last_completion_char = completion_current.insert_text[completion_current.insert_text.length() - 1];
  5196. CharType last_completion_char_display = completion_current.display[completion_current.display.length() - 1];
  5197. if ((last_completion_char == '"' || last_completion_char == '\'') && (last_completion_char == next_char || last_completion_char_display == next_char)) {
  5198. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  5199. }
  5200. if (last_completion_char == '(') {
  5201. if (next_char == last_completion_char) {
  5202. _base_remove_text(cursor.line, cursor.column - 1, cursor.line, cursor.column);
  5203. } else if (auto_brace_completion_enabled) {
  5204. insert_text_at_cursor(")");
  5205. cursor.column--;
  5206. }
  5207. } else if (last_completion_char == ')' && next_char == '(') {
  5208. _base_remove_text(cursor.line, cursor.column - 2, cursor.line, cursor.column);
  5209. if (line[cursor.column + 1] != ')') {
  5210. cursor.column--;
  5211. }
  5212. }
  5213. end_complex_operation();
  5214. _cancel_completion();
  5215. if (last_completion_char == '(') {
  5216. query_code_comple();
  5217. }
  5218. }
  5219. void TextEdit::_cancel_code_hint() {
  5220. completion_hint = "";
  5221. update();
  5222. }
  5223. void TextEdit::_cancel_completion() {
  5224. if (!completion_active)
  5225. return;
  5226. completion_active = false;
  5227. completion_forced = false;
  5228. update();
  5229. }
  5230. static bool _is_completable(CharType c) {
  5231. return !_is_symbol(c) || c == '"' || c == '\'';
  5232. }
  5233. void TextEdit::_update_completion_candidates() {
  5234. String l = text[cursor.line];
  5235. int cofs = CLAMP(cursor.column, 0, l.length());
  5236. String s;
  5237. // Look for keywords first.
  5238. bool inquote = false;
  5239. int first_quote = -1;
  5240. int restore_quotes = -1;
  5241. int c = cofs - 1;
  5242. while (c >= 0) {
  5243. if (l[c] == '"' || l[c] == '\'') {
  5244. inquote = !inquote;
  5245. if (first_quote == -1)
  5246. first_quote = c;
  5247. restore_quotes = 0;
  5248. } else if (restore_quotes == 0 && l[c] == '$') {
  5249. restore_quotes = 1;
  5250. } else if (restore_quotes == 0 && !_is_whitespace(l[c])) {
  5251. restore_quotes = -1;
  5252. }
  5253. c--;
  5254. }
  5255. bool pre_keyword = false;
  5256. bool cancel = false;
  5257. if (!inquote && first_quote == cofs - 1) {
  5258. // No completion here.
  5259. cancel = true;
  5260. } else if (inquote && first_quote != -1) {
  5261. s = l.substr(first_quote, cofs - first_quote);
  5262. } else if (cofs > 0 && l[cofs - 1] == ' ') {
  5263. int kofs = cofs - 1;
  5264. String kw;
  5265. while (kofs >= 0 && l[kofs] == ' ')
  5266. kofs--;
  5267. while (kofs >= 0 && l[kofs] > 32 && _is_completable(l[kofs])) {
  5268. kw = String::chr(l[kofs]) + kw;
  5269. kofs--;
  5270. }
  5271. pre_keyword = keywords.has(kw);
  5272. } else {
  5273. while (cofs > 0 && l[cofs - 1] > 32 && (l[cofs - 1] == '/' || _is_completable(l[cofs - 1]))) {
  5274. s = String::chr(l[cofs - 1]) + s;
  5275. if (l[cofs - 1] == '\'' || l[cofs - 1] == '"' || l[cofs - 1] == '$')
  5276. break;
  5277. cofs--;
  5278. }
  5279. }
  5280. if (cursor.column > 0 && l[cursor.column - 1] == '(' && !pre_keyword && !completion_forced) {
  5281. cancel = true;
  5282. }
  5283. update();
  5284. bool prev_is_prefix = false;
  5285. if (cofs > 0 && completion_prefixes.has(String::chr(l[cofs - 1])))
  5286. prev_is_prefix = true;
  5287. // Check with one space before prefix, to allow indent.
  5288. if (cofs > 1 && l[cofs - 1] == ' ' && completion_prefixes.has(String::chr(l[cofs - 2])))
  5289. prev_is_prefix = true;
  5290. if (cancel || (!pre_keyword && s == "" && (cofs == 0 || !prev_is_prefix))) {
  5291. // None to complete, cancel.
  5292. _cancel_completion();
  5293. return;
  5294. }
  5295. completion_options.clear();
  5296. completion_index = 0;
  5297. completion_base = s;
  5298. Vector<float> sim_cache;
  5299. bool single_quote = s.begins_with("'");
  5300. Vector<ScriptCodeCompletionOption> completion_options_casei;
  5301. Vector<ScriptCodeCompletionOption> completion_options_subseq;
  5302. Vector<ScriptCodeCompletionOption> completion_options_subseq_casei;
  5303. String s_lower = s.to_lower();
  5304. for (List<ScriptCodeCompletionOption>::Element *E = completion_sources.front(); E; E = E->next()) {
  5305. ScriptCodeCompletionOption &option = E->get();
  5306. if (single_quote && option.display.is_quoted()) {
  5307. option.display = option.display.unquote().quote("'");
  5308. }
  5309. if (inquote && restore_quotes == 1 && !option.display.is_quoted()) {
  5310. String quote = single_quote ? "'" : "\"";
  5311. option.display = option.display.quote(quote);
  5312. option.insert_text = option.insert_text.quote(quote);
  5313. }
  5314. if (option.display.length() == 0) {
  5315. continue;
  5316. } else if (s.length() == 0) {
  5317. completion_options.push_back(option);
  5318. } else {
  5319. // This code works the same as:
  5320. /*
  5321. if (option.display.begins_with(s)) {
  5322. completion_options.push_back(option);
  5323. } else if (option.display.to_lower().begins_with(s.to_lower())) {
  5324. completion_options_casei.push_back(option);
  5325. } else if (s.is_subsequence_of(option.display)) {
  5326. completion_options_subseq.push_back(option);
  5327. } else if (s.is_subsequence_ofi(option.display)) {
  5328. completion_options_subseq_casei.push_back(option);
  5329. }
  5330. */
  5331. // But is more performant due to being inlined and looping over the characters only once
  5332. String display_lower = option.display.to_lower();
  5333. const CharType *ssq = &s[0];
  5334. const CharType *ssq_lower = &s_lower[0];
  5335. const CharType *tgt = &option.display[0];
  5336. const CharType *tgt_lower = &display_lower[0];
  5337. const CharType *ssq_last_tgt = NULL;
  5338. const CharType *ssq_lower_last_tgt = NULL;
  5339. for (; *tgt; tgt++, tgt_lower++) {
  5340. if (*ssq == *tgt) {
  5341. ssq++;
  5342. ssq_last_tgt = tgt;
  5343. }
  5344. if (*ssq_lower == *tgt_lower) {
  5345. ssq_lower++;
  5346. ssq_lower_last_tgt = tgt;
  5347. }
  5348. }
  5349. if (!*ssq) { // Matched the whole subsequence in s
  5350. if (ssq_last_tgt == &option.display[s.length() - 1]) { // Finished matching in the first s.length() characters
  5351. completion_options.push_back(option);
  5352. } else {
  5353. completion_options_subseq.push_back(option);
  5354. }
  5355. } else if (!*ssq_lower) { // Matched the whole subsequence in s_lower
  5356. if (ssq_lower_last_tgt == &option.display[s.length() - 1]) { // Finished matching in the first s.length() characters
  5357. completion_options_casei.push_back(option);
  5358. } else {
  5359. completion_options_subseq_casei.push_back(option);
  5360. }
  5361. }
  5362. }
  5363. }
  5364. completion_options.append_array(completion_options_casei);
  5365. completion_options.append_array(completion_options_subseq);
  5366. completion_options.append_array(completion_options_subseq_casei);
  5367. if (completion_options.size() == 0) {
  5368. // No options to complete, cancel.
  5369. _cancel_completion();
  5370. return;
  5371. }
  5372. if (completion_options.size() == 1 && s == completion_options[0].display) {
  5373. // A perfect match, stop completion.
  5374. _cancel_completion();
  5375. return;
  5376. }
  5377. // The top of the list is the best match.
  5378. completion_current = completion_options[0];
  5379. completion_enabled = true;
  5380. }
  5381. void TextEdit::query_code_comple() {
  5382. String l = text[cursor.line];
  5383. int ofs = CLAMP(cursor.column, 0, l.length());
  5384. bool inquote = false;
  5385. int c = ofs - 1;
  5386. while (c >= 0) {
  5387. if (l[c] == '"' || l[c] == '\'')
  5388. inquote = !inquote;
  5389. c--;
  5390. }
  5391. bool ignored = completion_active && !completion_options.empty();
  5392. if (ignored) {
  5393. ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_PLAIN_TEXT;
  5394. const ScriptCodeCompletionOption *previous_option = NULL;
  5395. for (int i = 0; i < completion_options.size(); i++) {
  5396. const ScriptCodeCompletionOption &current_option = completion_options[i];
  5397. if (!previous_option) {
  5398. previous_option = &current_option;
  5399. kind = current_option.kind;
  5400. }
  5401. if (previous_option->kind != current_option.kind) {
  5402. ignored = false;
  5403. break;
  5404. }
  5405. }
  5406. ignored = ignored && (kind == ScriptCodeCompletionOption::KIND_FILE_PATH || kind == ScriptCodeCompletionOption::KIND_NODE_PATH || kind == ScriptCodeCompletionOption::KIND_SIGNAL);
  5407. }
  5408. if (!ignored) {
  5409. if (ofs > 0 && (inquote || _is_completable(l[ofs - 1]) || completion_prefixes.has(String::chr(l[ofs - 1]))))
  5410. emit_signal("request_completion");
  5411. else if (ofs > 1 && l[ofs - 1] == ' ' && completion_prefixes.has(String::chr(l[ofs - 2]))) // Make it work with a space too, it's good enough.
  5412. emit_signal("request_completion");
  5413. }
  5414. }
  5415. void TextEdit::set_code_hint(const String &p_hint) {
  5416. completion_hint = p_hint;
  5417. completion_hint_offset = -0xFFFF;
  5418. update();
  5419. }
  5420. void TextEdit::code_complete(const List<ScriptCodeCompletionOption> &p_strings, bool p_forced) {
  5421. completion_sources = p_strings;
  5422. completion_active = true;
  5423. completion_forced = p_forced;
  5424. completion_current = ScriptCodeCompletionOption();
  5425. completion_index = 0;
  5426. _update_completion_candidates();
  5427. }
  5428. String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
  5429. int row, col;
  5430. _get_mouse_pos(p_pos, row, col);
  5431. String s = text[row];
  5432. if (s.length() == 0)
  5433. return "";
  5434. int beg, end;
  5435. if (select_word(s, col, beg, end)) {
  5436. bool inside_quotes = false;
  5437. CharType selected_quote = '\0';
  5438. int qbegin = 0, qend = 0;
  5439. for (int i = 0; i < s.length(); i++) {
  5440. if (s[i] == '"' || s[i] == '\'') {
  5441. if (i == 0 || s[i - 1] != '\\') {
  5442. if (inside_quotes && selected_quote == s[i]) {
  5443. qend = i;
  5444. inside_quotes = false;
  5445. selected_quote = '\0';
  5446. if (col >= qbegin && col <= qend) {
  5447. return s.substr(qbegin, qend - qbegin);
  5448. }
  5449. } else if (!inside_quotes) {
  5450. qbegin = i + 1;
  5451. inside_quotes = true;
  5452. selected_quote = s[i];
  5453. }
  5454. }
  5455. }
  5456. }
  5457. return s.substr(beg, end - beg);
  5458. }
  5459. return String();
  5460. }
  5461. String TextEdit::get_tooltip(const Point2 &p_pos) const {
  5462. if (!tooltip_obj)
  5463. return Control::get_tooltip(p_pos);
  5464. int row, col;
  5465. _get_mouse_pos(p_pos, row, col);
  5466. String s = text[row];
  5467. if (s.length() == 0)
  5468. return Control::get_tooltip(p_pos);
  5469. int beg, end;
  5470. if (select_word(s, col, beg, end)) {
  5471. String tt = tooltip_obj->call(tooltip_func, s.substr(beg, end - beg), tooltip_ud);
  5472. return tt;
  5473. }
  5474. return Control::get_tooltip(p_pos);
  5475. }
  5476. void TextEdit::set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata) {
  5477. tooltip_obj = p_obj;
  5478. tooltip_func = p_function;
  5479. tooltip_ud = p_udata;
  5480. }
  5481. void TextEdit::set_line(int line, String new_text) {
  5482. if (line < 0 || line > text.size())
  5483. return;
  5484. _remove_text(line, 0, line, text[line].length());
  5485. _insert_text(line, 0, new_text);
  5486. if (cursor.line == line) {
  5487. cursor.column = MIN(cursor.column, new_text.length());
  5488. }
  5489. if (is_selection_active() && line == selection.to_line && selection.to_column > text[line].length()) {
  5490. selection.to_column = text[line].length();
  5491. }
  5492. }
  5493. void TextEdit::insert_at(const String &p_text, int at) {
  5494. _insert_text(at, 0, p_text + "\n");
  5495. if (cursor.line >= at) {
  5496. // offset cursor when located after inserted line
  5497. ++cursor.line;
  5498. }
  5499. if (is_selection_active()) {
  5500. if (selection.from_line >= at) {
  5501. // offset selection when located after inserted line
  5502. ++selection.from_line;
  5503. ++selection.to_line;
  5504. } else if (selection.to_line >= at) {
  5505. // extend selection that includes inserted line
  5506. ++selection.to_line;
  5507. }
  5508. }
  5509. }
  5510. void TextEdit::set_show_line_numbers(bool p_show) {
  5511. line_numbers = p_show;
  5512. update();
  5513. }
  5514. void TextEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
  5515. line_numbers_zero_padded = p_zero_padded;
  5516. update();
  5517. }
  5518. bool TextEdit::is_show_line_numbers_enabled() const {
  5519. return line_numbers;
  5520. }
  5521. void TextEdit::set_show_line_length_guidelines(bool p_show) {
  5522. line_length_guidelines = p_show;
  5523. update();
  5524. }
  5525. void TextEdit::set_line_length_guideline_soft_column(int p_column) {
  5526. line_length_guideline_soft_col = p_column;
  5527. update();
  5528. }
  5529. void TextEdit::set_line_length_guideline_hard_column(int p_column) {
  5530. line_length_guideline_hard_col = p_column;
  5531. update();
  5532. }
  5533. void TextEdit::set_bookmark_gutter_enabled(bool p_draw) {
  5534. draw_bookmark_gutter = p_draw;
  5535. update();
  5536. }
  5537. bool TextEdit::is_bookmark_gutter_enabled() const {
  5538. return draw_bookmark_gutter;
  5539. }
  5540. void TextEdit::set_breakpoint_gutter_enabled(bool p_draw) {
  5541. draw_breakpoint_gutter = p_draw;
  5542. update();
  5543. }
  5544. bool TextEdit::is_breakpoint_gutter_enabled() const {
  5545. return draw_breakpoint_gutter;
  5546. }
  5547. void TextEdit::set_breakpoint_gutter_width(int p_gutter_width) {
  5548. breakpoint_gutter_width = p_gutter_width;
  5549. update();
  5550. }
  5551. int TextEdit::get_breakpoint_gutter_width() const {
  5552. return cache.breakpoint_gutter_width;
  5553. }
  5554. void TextEdit::set_draw_fold_gutter(bool p_draw) {
  5555. draw_fold_gutter = p_draw;
  5556. update();
  5557. }
  5558. bool TextEdit::is_drawing_fold_gutter() const {
  5559. return draw_fold_gutter;
  5560. }
  5561. void TextEdit::set_fold_gutter_width(int p_gutter_width) {
  5562. fold_gutter_width = p_gutter_width;
  5563. update();
  5564. }
  5565. int TextEdit::get_fold_gutter_width() const {
  5566. return cache.fold_gutter_width;
  5567. }
  5568. void TextEdit::set_draw_info_gutter(bool p_draw) {
  5569. draw_info_gutter = p_draw;
  5570. update();
  5571. }
  5572. bool TextEdit::is_drawing_info_gutter() const {
  5573. return draw_info_gutter;
  5574. }
  5575. void TextEdit::set_info_gutter_width(int p_gutter_width) {
  5576. info_gutter_width = p_gutter_width;
  5577. update();
  5578. }
  5579. int TextEdit::get_info_gutter_width() const {
  5580. return info_gutter_width;
  5581. }
  5582. void TextEdit::set_draw_minimap(bool p_draw) {
  5583. draw_minimap = p_draw;
  5584. update();
  5585. }
  5586. bool TextEdit::is_drawing_minimap() const {
  5587. return draw_minimap;
  5588. }
  5589. void TextEdit::set_minimap_width(int p_minimap_width) {
  5590. minimap_width = p_minimap_width;
  5591. update();
  5592. }
  5593. int TextEdit::get_minimap_width() const {
  5594. return minimap_width;
  5595. }
  5596. void TextEdit::set_hiding_enabled(bool p_enabled) {
  5597. if (!p_enabled)
  5598. unhide_all_lines();
  5599. hiding_enabled = p_enabled;
  5600. update();
  5601. }
  5602. bool TextEdit::is_hiding_enabled() const {
  5603. return hiding_enabled;
  5604. }
  5605. void TextEdit::set_highlight_current_line(bool p_enabled) {
  5606. highlight_current_line = p_enabled;
  5607. update();
  5608. }
  5609. bool TextEdit::is_highlight_current_line_enabled() const {
  5610. return highlight_current_line;
  5611. }
  5612. bool TextEdit::is_text_field() const {
  5613. return true;
  5614. }
  5615. void TextEdit::menu_option(int p_option) {
  5616. switch (p_option) {
  5617. case MENU_CUT: {
  5618. if (!readonly) {
  5619. cut();
  5620. }
  5621. } break;
  5622. case MENU_COPY: {
  5623. copy();
  5624. } break;
  5625. case MENU_PASTE: {
  5626. if (!readonly) {
  5627. paste();
  5628. }
  5629. } break;
  5630. case MENU_CLEAR: {
  5631. if (!readonly) {
  5632. clear();
  5633. }
  5634. } break;
  5635. case MENU_SELECT_ALL: {
  5636. select_all();
  5637. } break;
  5638. case MENU_UNDO: {
  5639. undo();
  5640. } break;
  5641. case MENU_REDO: {
  5642. redo();
  5643. }
  5644. }
  5645. }
  5646. void TextEdit::set_highlighted_word(const String &new_word) {
  5647. highlighted_word = new_word;
  5648. update();
  5649. }
  5650. void TextEdit::set_select_identifiers_on_hover(bool p_enable) {
  5651. select_identifiers_enabled = p_enable;
  5652. }
  5653. bool TextEdit::is_selecting_identifiers_on_hover_enabled() const {
  5654. return select_identifiers_enabled;
  5655. }
  5656. void TextEdit::set_context_menu_enabled(bool p_enable) {
  5657. context_menu_enabled = p_enable;
  5658. }
  5659. bool TextEdit::is_context_menu_enabled() {
  5660. return context_menu_enabled;
  5661. }
  5662. void TextEdit::set_shortcut_keys_enabled(bool p_enabled) {
  5663. shortcut_keys_enabled = p_enabled;
  5664. _generate_context_menu();
  5665. }
  5666. void TextEdit::set_selecting_enabled(bool p_enabled) {
  5667. selecting_enabled = p_enabled;
  5668. if (!selecting_enabled)
  5669. deselect();
  5670. _generate_context_menu();
  5671. }
  5672. bool TextEdit::is_selecting_enabled() const {
  5673. return selecting_enabled;
  5674. }
  5675. bool TextEdit::is_shortcut_keys_enabled() const {
  5676. return shortcut_keys_enabled;
  5677. }
  5678. PopupMenu *TextEdit::get_menu() const {
  5679. return menu;
  5680. }
  5681. void TextEdit::_bind_methods() {
  5682. ClassDB::bind_method(D_METHOD("_gui_input"), &TextEdit::_gui_input);
  5683. ClassDB::bind_method(D_METHOD("_cursor_changed_emit"), &TextEdit::_cursor_changed_emit);
  5684. ClassDB::bind_method(D_METHOD("_text_changed_emit"), &TextEdit::_text_changed_emit);
  5685. ClassDB::bind_method(D_METHOD("_update_wrap_at"), &TextEdit::_update_wrap_at);
  5686. BIND_ENUM_CONSTANT(SEARCH_MATCH_CASE);
  5687. BIND_ENUM_CONSTANT(SEARCH_WHOLE_WORDS);
  5688. BIND_ENUM_CONSTANT(SEARCH_BACKWARDS);
  5689. BIND_ENUM_CONSTANT(SEARCH_RESULT_COLUMN);
  5690. BIND_ENUM_CONSTANT(SEARCH_RESULT_LINE);
  5691. /*
  5692. ClassDB::bind_method(D_METHOD("delete_char"),&TextEdit::delete_char);
  5693. ClassDB::bind_method(D_METHOD("delete_line"),&TextEdit::delete_line);
  5694. */
  5695. ClassDB::bind_method(D_METHOD("set_text", "text"), &TextEdit::set_text);
  5696. ClassDB::bind_method(D_METHOD("insert_text_at_cursor", "text"), &TextEdit::insert_text_at_cursor);
  5697. ClassDB::bind_method(D_METHOD("get_line_count"), &TextEdit::get_line_count);
  5698. ClassDB::bind_method(D_METHOD("get_text"), &TextEdit::get_text);
  5699. ClassDB::bind_method(D_METHOD("get_line", "line"), &TextEdit::get_line);
  5700. ClassDB::bind_method(D_METHOD("center_viewport_to_cursor"), &TextEdit::center_viewport_to_cursor);
  5701. ClassDB::bind_method(D_METHOD("cursor_set_column", "column", "adjust_viewport"), &TextEdit::cursor_set_column, DEFVAL(true));
  5702. ClassDB::bind_method(D_METHOD("cursor_set_line", "line", "adjust_viewport", "can_be_hidden", "wrap_index"), &TextEdit::cursor_set_line, DEFVAL(true), DEFVAL(true), DEFVAL(0));
  5703. ClassDB::bind_method(D_METHOD("cursor_get_column"), &TextEdit::cursor_get_column);
  5704. ClassDB::bind_method(D_METHOD("cursor_get_line"), &TextEdit::cursor_get_line);
  5705. ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enable"), &TextEdit::cursor_set_blink_enabled);
  5706. ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"), &TextEdit::cursor_get_blink_enabled);
  5707. ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"), &TextEdit::cursor_set_blink_speed);
  5708. ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"), &TextEdit::cursor_get_blink_speed);
  5709. ClassDB::bind_method(D_METHOD("cursor_set_block_mode", "enable"), &TextEdit::cursor_set_block_mode);
  5710. ClassDB::bind_method(D_METHOD("cursor_is_block_mode"), &TextEdit::cursor_is_block_mode);
  5711. ClassDB::bind_method(D_METHOD("set_right_click_moves_caret", "enable"), &TextEdit::set_right_click_moves_caret);
  5712. ClassDB::bind_method(D_METHOD("is_right_click_moving_caret"), &TextEdit::is_right_click_moving_caret);
  5713. ClassDB::bind_method(D_METHOD("set_readonly", "enable"), &TextEdit::set_readonly);
  5714. ClassDB::bind_method(D_METHOD("is_readonly"), &TextEdit::is_readonly);
  5715. ClassDB::bind_method(D_METHOD("set_wrap_enabled", "enable"), &TextEdit::set_wrap_enabled);
  5716. ClassDB::bind_method(D_METHOD("is_wrap_enabled"), &TextEdit::is_wrap_enabled);
  5717. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &TextEdit::set_context_menu_enabled);
  5718. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &TextEdit::is_context_menu_enabled);
  5719. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &TextEdit::set_shortcut_keys_enabled);
  5720. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &TextEdit::is_shortcut_keys_enabled);
  5721. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &TextEdit::set_selecting_enabled);
  5722. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &TextEdit::is_selecting_enabled);
  5723. ClassDB::bind_method(D_METHOD("cut"), &TextEdit::cut);
  5724. ClassDB::bind_method(D_METHOD("copy"), &TextEdit::copy);
  5725. ClassDB::bind_method(D_METHOD("paste"), &TextEdit::paste);
  5726. ClassDB::bind_method(D_METHOD("select", "from_line", "from_column", "to_line", "to_column"), &TextEdit::select);
  5727. ClassDB::bind_method(D_METHOD("select_all"), &TextEdit::select_all);
  5728. ClassDB::bind_method(D_METHOD("deselect"), &TextEdit::deselect);
  5729. ClassDB::bind_method(D_METHOD("is_selection_active"), &TextEdit::is_selection_active);
  5730. ClassDB::bind_method(D_METHOD("get_selection_from_line"), &TextEdit::get_selection_from_line);
  5731. ClassDB::bind_method(D_METHOD("get_selection_from_column"), &TextEdit::get_selection_from_column);
  5732. ClassDB::bind_method(D_METHOD("get_selection_to_line"), &TextEdit::get_selection_to_line);
  5733. ClassDB::bind_method(D_METHOD("get_selection_to_column"), &TextEdit::get_selection_to_column);
  5734. ClassDB::bind_method(D_METHOD("get_selection_text"), &TextEdit::get_selection_text);
  5735. ClassDB::bind_method(D_METHOD("get_word_under_cursor"), &TextEdit::get_word_under_cursor);
  5736. ClassDB::bind_method(D_METHOD("search", "key", "flags", "from_line", "from_column"), &TextEdit::_search_bind);
  5737. ClassDB::bind_method(D_METHOD("undo"), &TextEdit::undo);
  5738. ClassDB::bind_method(D_METHOD("redo"), &TextEdit::redo);
  5739. ClassDB::bind_method(D_METHOD("clear_undo_history"), &TextEdit::clear_undo_history);
  5740. ClassDB::bind_method(D_METHOD("set_show_line_numbers", "enable"), &TextEdit::set_show_line_numbers);
  5741. ClassDB::bind_method(D_METHOD("is_show_line_numbers_enabled"), &TextEdit::is_show_line_numbers_enabled);
  5742. ClassDB::bind_method(D_METHOD("set_draw_tabs"), &TextEdit::set_draw_tabs);
  5743. ClassDB::bind_method(D_METHOD("is_drawing_tabs"), &TextEdit::is_drawing_tabs);
  5744. ClassDB::bind_method(D_METHOD("set_draw_spaces"), &TextEdit::set_draw_spaces);
  5745. ClassDB::bind_method(D_METHOD("is_drawing_spaces"), &TextEdit::is_drawing_spaces);
  5746. ClassDB::bind_method(D_METHOD("set_breakpoint_gutter_enabled", "enable"), &TextEdit::set_breakpoint_gutter_enabled);
  5747. ClassDB::bind_method(D_METHOD("is_breakpoint_gutter_enabled"), &TextEdit::is_breakpoint_gutter_enabled);
  5748. ClassDB::bind_method(D_METHOD("set_draw_fold_gutter"), &TextEdit::set_draw_fold_gutter);
  5749. ClassDB::bind_method(D_METHOD("is_drawing_fold_gutter"), &TextEdit::is_drawing_fold_gutter);
  5750. ClassDB::bind_method(D_METHOD("set_hiding_enabled", "enable"), &TextEdit::set_hiding_enabled);
  5751. ClassDB::bind_method(D_METHOD("is_hiding_enabled"), &TextEdit::is_hiding_enabled);
  5752. ClassDB::bind_method(D_METHOD("set_line_as_hidden", "line", "enable"), &TextEdit::set_line_as_hidden);
  5753. ClassDB::bind_method(D_METHOD("is_line_hidden", "line"), &TextEdit::is_line_hidden);
  5754. ClassDB::bind_method(D_METHOD("fold_all_lines"), &TextEdit::fold_all_lines);
  5755. ClassDB::bind_method(D_METHOD("unhide_all_lines"), &TextEdit::unhide_all_lines);
  5756. ClassDB::bind_method(D_METHOD("fold_line", "line"), &TextEdit::fold_line);
  5757. ClassDB::bind_method(D_METHOD("unfold_line", "line"), &TextEdit::unfold_line);
  5758. ClassDB::bind_method(D_METHOD("toggle_fold_line", "line"), &TextEdit::toggle_fold_line);
  5759. ClassDB::bind_method(D_METHOD("can_fold", "line"), &TextEdit::can_fold);
  5760. ClassDB::bind_method(D_METHOD("is_folded", "line"), &TextEdit::is_folded);
  5761. ClassDB::bind_method(D_METHOD("set_highlight_all_occurrences", "enable"), &TextEdit::set_highlight_all_occurrences);
  5762. ClassDB::bind_method(D_METHOD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled);
  5763. ClassDB::bind_method(D_METHOD("set_override_selected_font_color", "override"), &TextEdit::set_override_selected_font_color);
  5764. ClassDB::bind_method(D_METHOD("is_overriding_selected_font_color"), &TextEdit::is_overriding_selected_font_color);
  5765. ClassDB::bind_method(D_METHOD("set_syntax_coloring", "enable"), &TextEdit::set_syntax_coloring);
  5766. ClassDB::bind_method(D_METHOD("is_syntax_coloring_enabled"), &TextEdit::is_syntax_coloring_enabled);
  5767. ClassDB::bind_method(D_METHOD("set_highlight_current_line", "enabled"), &TextEdit::set_highlight_current_line);
  5768. ClassDB::bind_method(D_METHOD("is_highlight_current_line_enabled"), &TextEdit::is_highlight_current_line_enabled);
  5769. ClassDB::bind_method(D_METHOD("set_smooth_scroll_enable", "enable"), &TextEdit::set_smooth_scroll_enabled);
  5770. ClassDB::bind_method(D_METHOD("is_smooth_scroll_enabled"), &TextEdit::is_smooth_scroll_enabled);
  5771. ClassDB::bind_method(D_METHOD("set_v_scroll_speed", "speed"), &TextEdit::set_v_scroll_speed);
  5772. ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed);
  5773. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &TextEdit::set_v_scroll);
  5774. ClassDB::bind_method(D_METHOD("get_v_scroll"), &TextEdit::get_v_scroll);
  5775. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &TextEdit::set_h_scroll);
  5776. ClassDB::bind_method(D_METHOD("get_h_scroll"), &TextEdit::get_h_scroll);
  5777. ClassDB::bind_method(D_METHOD("add_keyword_color", "keyword", "color"), &TextEdit::add_keyword_color);
  5778. ClassDB::bind_method(D_METHOD("has_keyword_color", "keyword"), &TextEdit::has_keyword_color);
  5779. ClassDB::bind_method(D_METHOD("get_keyword_color", "keyword"), &TextEdit::get_keyword_color);
  5780. ClassDB::bind_method(D_METHOD("add_color_region", "begin_key", "end_key", "color", "line_only"), &TextEdit::add_color_region, DEFVAL(false));
  5781. ClassDB::bind_method(D_METHOD("clear_colors"), &TextEdit::clear_colors);
  5782. ClassDB::bind_method(D_METHOD("menu_option", "option"), &TextEdit::menu_option);
  5783. ClassDB::bind_method(D_METHOD("get_menu"), &TextEdit::get_menu);
  5784. ClassDB::bind_method(D_METHOD("get_breakpoints"), &TextEdit::get_breakpoints_array);
  5785. ClassDB::bind_method(D_METHOD("remove_breakpoints"), &TextEdit::remove_breakpoints);
  5786. ClassDB::bind_method(D_METHOD("draw_minimap", "draw"), &TextEdit::set_draw_minimap);
  5787. ClassDB::bind_method(D_METHOD("is_drawing_minimap"), &TextEdit::is_drawing_minimap);
  5788. ClassDB::bind_method(D_METHOD("set_minimap_width", "width"), &TextEdit::set_minimap_width);
  5789. ClassDB::bind_method(D_METHOD("get_minimap_width"), &TextEdit::get_minimap_width);
  5790. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  5791. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "readonly"), "set_readonly", "is_readonly");
  5792. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled");
  5793. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "syntax_highlighting"), "set_syntax_coloring", "is_syntax_coloring_enabled");
  5794. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_line_numbers"), "set_show_line_numbers", "is_show_line_numbers_enabled");
  5795. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_tabs"), "set_draw_tabs", "is_drawing_tabs");
  5796. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_spaces"), "set_draw_spaces", "is_drawing_spaces");
  5797. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "breakpoint_gutter"), "set_breakpoint_gutter_enabled", "is_breakpoint_gutter_enabled");
  5798. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fold_gutter"), "set_draw_fold_gutter", "is_drawing_fold_gutter");
  5799. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled");
  5800. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_selected_font_color"), "set_override_selected_font_color", "is_overriding_selected_font_color");
  5801. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  5802. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  5803. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  5804. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_scrolling"), "set_smooth_scroll_enable", "is_smooth_scroll_enabled");
  5805. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_scroll_speed"), "set_v_scroll_speed", "get_v_scroll_speed");
  5806. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hiding_enabled"), "set_hiding_enabled", "is_hiding_enabled");
  5807. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_enabled"), "set_wrap_enabled", "is_wrap_enabled");
  5808. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical"), "set_v_scroll", "get_v_scroll");
  5809. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal"), "set_h_scroll", "get_h_scroll");
  5810. ADD_GROUP("Minimap", "minimap_");
  5811. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw"), "draw_minimap", "is_drawing_minimap");
  5812. ADD_PROPERTY(PropertyInfo(Variant::INT, "minimap_width"), "set_minimap_width", "get_minimap_width");
  5813. ADD_GROUP("Caret", "caret_");
  5814. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_block_mode"), "cursor_set_block_mode", "cursor_is_block_mode");
  5815. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "cursor_set_blink_enabled", "cursor_get_blink_enabled");
  5816. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.01"), "cursor_set_blink_speed", "cursor_get_blink_speed");
  5817. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_moving_by_right_click"), "set_right_click_moves_caret", "is_right_click_moving_caret");
  5818. ADD_SIGNAL(MethodInfo("cursor_changed"));
  5819. ADD_SIGNAL(MethodInfo("text_changed"));
  5820. ADD_SIGNAL(MethodInfo("request_completion"));
  5821. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "row")));
  5822. ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "row"), PropertyInfo(Variant::INT, "column")));
  5823. ADD_SIGNAL(MethodInfo("info_clicked", PropertyInfo(Variant::INT, "row"), PropertyInfo(Variant::STRING, "info")));
  5824. ADD_SIGNAL(MethodInfo("symbol_validate", PropertyInfo(Variant::STRING, "symbol")));
  5825. BIND_ENUM_CONSTANT(MENU_CUT);
  5826. BIND_ENUM_CONSTANT(MENU_COPY);
  5827. BIND_ENUM_CONSTANT(MENU_PASTE);
  5828. BIND_ENUM_CONSTANT(MENU_CLEAR);
  5829. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  5830. BIND_ENUM_CONSTANT(MENU_UNDO);
  5831. BIND_ENUM_CONSTANT(MENU_REDO);
  5832. BIND_ENUM_CONSTANT(MENU_MAX);
  5833. GLOBAL_DEF("gui/timers/text_edit_idle_detect_sec", 3);
  5834. ProjectSettings::get_singleton()->set_custom_property_info("gui/timers/text_edit_idle_detect_sec", PropertyInfo(Variant::FLOAT, "gui/timers/text_edit_idle_detect_sec", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater")); // No negative numbers.
  5835. }
  5836. TextEdit::TextEdit() {
  5837. setting_row = false;
  5838. draw_tabs = false;
  5839. draw_spaces = false;
  5840. override_selected_font_color = false;
  5841. draw_caret = true;
  5842. max_chars = 0;
  5843. clear();
  5844. wrap_enabled = false;
  5845. wrap_at = 0;
  5846. wrap_right_offset = 10;
  5847. set_focus_mode(FOCUS_ALL);
  5848. syntax_highlighter = NULL;
  5849. _update_caches();
  5850. cache.row_height = 1;
  5851. cache.line_spacing = 1;
  5852. cache.line_number_w = 1;
  5853. cache.breakpoint_gutter_width = 0;
  5854. breakpoint_gutter_width = 0;
  5855. cache.fold_gutter_width = 0;
  5856. fold_gutter_width = 0;
  5857. info_gutter_width = 0;
  5858. cache.info_gutter_width = 0;
  5859. set_default_cursor_shape(CURSOR_IBEAM);
  5860. indent_size = 4;
  5861. text.set_indent_size(indent_size);
  5862. text.clear();
  5863. text.set_color_regions(&color_regions);
  5864. h_scroll = memnew(HScrollBar);
  5865. v_scroll = memnew(VScrollBar);
  5866. add_child(h_scroll);
  5867. add_child(v_scroll);
  5868. updating_scrolls = false;
  5869. selection.active = false;
  5870. h_scroll->connect("value_changed", callable_mp(this, &TextEdit::_scroll_moved));
  5871. v_scroll->connect("value_changed", callable_mp(this, &TextEdit::_scroll_moved));
  5872. v_scroll->connect("scrolling", callable_mp(this, &TextEdit::_v_scroll_input));
  5873. cursor_changed_dirty = false;
  5874. text_changed_dirty = false;
  5875. selection.selecting_mode = Selection::MODE_NONE;
  5876. selection.selecting_line = 0;
  5877. selection.selecting_column = 0;
  5878. selection.selecting_text = false;
  5879. selection.active = false;
  5880. syntax_coloring = false;
  5881. block_caret = false;
  5882. caret_blink_enabled = false;
  5883. caret_blink_timer = memnew(Timer);
  5884. add_child(caret_blink_timer);
  5885. caret_blink_timer->set_wait_time(0.65);
  5886. caret_blink_timer->connect("timeout", callable_mp(this, &TextEdit::_toggle_draw_caret));
  5887. cursor_set_blink_enabled(false);
  5888. right_click_moves_caret = true;
  5889. idle_detect = memnew(Timer);
  5890. add_child(idle_detect);
  5891. idle_detect->set_one_shot(true);
  5892. idle_detect->set_wait_time(GLOBAL_GET("gui/timers/text_edit_idle_detect_sec"));
  5893. idle_detect->connect("timeout", callable_mp(this, &TextEdit::_push_current_op));
  5894. click_select_held = memnew(Timer);
  5895. add_child(click_select_held);
  5896. click_select_held->set_wait_time(0.05);
  5897. click_select_held->connect("timeout", callable_mp(this, &TextEdit::_click_selection_held));
  5898. current_op.type = TextOperation::TYPE_NONE;
  5899. undo_enabled = true;
  5900. undo_stack_pos = NULL;
  5901. setting_text = false;
  5902. last_dblclk = 0;
  5903. current_op.version = 0;
  5904. version = 0;
  5905. saved_version = 0;
  5906. completion_enabled = false;
  5907. completion_active = false;
  5908. completion_line_ofs = 0;
  5909. tooltip_obj = NULL;
  5910. line_numbers = false;
  5911. line_numbers_zero_padded = false;
  5912. line_length_guidelines = false;
  5913. line_length_guideline_soft_col = 80;
  5914. line_length_guideline_hard_col = 100;
  5915. draw_bookmark_gutter = false;
  5916. draw_breakpoint_gutter = false;
  5917. draw_fold_gutter = false;
  5918. draw_info_gutter = false;
  5919. hiding_enabled = false;
  5920. next_operation_is_complex = false;
  5921. scroll_past_end_of_file_enabled = false;
  5922. auto_brace_completion_enabled = false;
  5923. brace_matching_enabled = false;
  5924. highlight_all_occurrences = false;
  5925. highlight_current_line = false;
  5926. indent_using_spaces = false;
  5927. space_indent = " ";
  5928. auto_indent = false;
  5929. insert_mode = false;
  5930. window_has_focus = true;
  5931. select_identifiers_enabled = false;
  5932. smooth_scroll_enabled = false;
  5933. scrolling = false;
  5934. minimap_clicked = false;
  5935. dragging_minimap = false;
  5936. can_drag_minimap = false;
  5937. minimap_scroll_ratio = 0;
  5938. minimap_scroll_click_pos = 0;
  5939. dragging_selection = false;
  5940. target_v_scroll = 0;
  5941. v_scroll_speed = 80;
  5942. draw_minimap = false;
  5943. minimap_width = 80;
  5944. minimap_char_size = Point2(1, 2);
  5945. minimap_line_spacing = 1;
  5946. selecting_enabled = true;
  5947. context_menu_enabled = true;
  5948. shortcut_keys_enabled = true;
  5949. menu = memnew(PopupMenu);
  5950. add_child(menu);
  5951. readonly = true; // Initialise to opposite first, so we get past the early-out in set_readonly.
  5952. set_readonly(false);
  5953. menu->connect("id_pressed", callable_mp(this, &TextEdit::menu_option));
  5954. first_draw = true;
  5955. executing_line = -1;
  5956. }
  5957. TextEdit::~TextEdit() {
  5958. }
  5959. ///////////////////////////////////////////////////////////////////////////////
  5960. Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int p_line) {
  5961. if (syntax_highlighting_cache.has(p_line)) {
  5962. return syntax_highlighting_cache[p_line];
  5963. }
  5964. if (syntax_highlighter != NULL) {
  5965. Map<int, HighlighterInfo> color_map = syntax_highlighter->_get_line_syntax_highlighting(p_line);
  5966. syntax_highlighting_cache[p_line] = color_map;
  5967. return color_map;
  5968. }
  5969. Map<int, HighlighterInfo> color_map;
  5970. bool prev_is_char = false;
  5971. bool prev_is_number = false;
  5972. bool in_keyword = false;
  5973. bool in_word = false;
  5974. bool in_function_name = false;
  5975. bool in_member_variable = false;
  5976. bool is_hex_notation = false;
  5977. Color keyword_color;
  5978. Color color;
  5979. int in_region = _is_line_in_region(p_line);
  5980. int deregion = 0;
  5981. const Map<int, TextEdit::Text::ColorRegionInfo> cri_map = text.get_color_region_info(p_line);
  5982. const String &str = text[p_line];
  5983. Color prev_color;
  5984. for (int j = 0; j < str.length(); j++) {
  5985. HighlighterInfo highlighter_info;
  5986. if (deregion > 0) {
  5987. deregion--;
  5988. if (deregion == 0) {
  5989. in_region = -1;
  5990. }
  5991. }
  5992. if (deregion != 0) {
  5993. if (color != prev_color) {
  5994. prev_color = color;
  5995. highlighter_info.color = color;
  5996. color_map[j] = highlighter_info;
  5997. }
  5998. continue;
  5999. }
  6000. color = cache.font_color;
  6001. bool is_char = _is_text_char(str[j]);
  6002. bool is_symbol = _is_symbol(str[j]);
  6003. bool is_number = _is_number(str[j]);
  6004. // Allow ABCDEF in hex notation.
  6005. if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
  6006. is_number = true;
  6007. } else {
  6008. is_hex_notation = false;
  6009. }
  6010. // Check for dot or underscore or 'x' for hex notation in floating point number or 'e' for scientific notation.
  6011. if ((str[j] == '.' || str[j] == 'x' || str[j] == '_' || str[j] == 'f' || str[j] == 'e') && !in_word && prev_is_number && !is_number) {
  6012. is_number = true;
  6013. is_symbol = false;
  6014. is_char = false;
  6015. if (str[j] == 'x' && str[j - 1] == '0') {
  6016. is_hex_notation = true;
  6017. }
  6018. }
  6019. if (!in_word && _is_char(str[j]) && !is_number) {
  6020. in_word = true;
  6021. }
  6022. if ((in_keyword || in_word) && !is_hex_notation) {
  6023. is_number = false;
  6024. }
  6025. if (is_symbol && str[j] != '.' && in_word) {
  6026. in_word = false;
  6027. }
  6028. if (is_symbol && cri_map.has(j)) {
  6029. const TextEdit::Text::ColorRegionInfo &cri = cri_map[j];
  6030. if (in_region == -1) {
  6031. if (!cri.end) {
  6032. in_region = cri.region;
  6033. }
  6034. } else if (in_region == cri.region && !color_regions[cri.region].line_only) { // Ignore otherwise.
  6035. if (cri.end || color_regions[cri.region].eq) {
  6036. deregion = color_regions[cri.region].eq ? color_regions[cri.region].begin_key.length() : color_regions[cri.region].end_key.length();
  6037. }
  6038. }
  6039. }
  6040. if (!is_char) {
  6041. in_keyword = false;
  6042. }
  6043. if (in_region == -1 && !in_keyword && is_char && !prev_is_char) {
  6044. int to = j;
  6045. while (to < str.length() && _is_text_char(str[to]))
  6046. to++;
  6047. uint32_t hash = String::hash(&str[j], to - j);
  6048. StrRange range(&str[j], to - j);
  6049. const Color *col = keywords.custom_getptr(range, hash);
  6050. if (!col) {
  6051. col = member_keywords.custom_getptr(range, hash);
  6052. if (col) {
  6053. for (int k = j - 1; k >= 0; k--) {
  6054. if (str[k] == '.') {
  6055. col = NULL; // Member indexing not allowed.
  6056. break;
  6057. } else if (str[k] > 32) {
  6058. break;
  6059. }
  6060. }
  6061. }
  6062. }
  6063. if (col) {
  6064. in_keyword = true;
  6065. keyword_color = *col;
  6066. }
  6067. }
  6068. if (!in_function_name && in_word && !in_keyword) {
  6069. int k = j;
  6070. while (k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  6071. k++;
  6072. }
  6073. // Check for space between name and bracket.
  6074. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  6075. k++;
  6076. }
  6077. if (str[k] == '(') {
  6078. in_function_name = true;
  6079. }
  6080. }
  6081. if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
  6082. int k = j;
  6083. while (k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  6084. k--;
  6085. }
  6086. if (str[k] == '.') {
  6087. in_member_variable = true;
  6088. }
  6089. }
  6090. if (is_symbol) {
  6091. in_function_name = false;
  6092. in_member_variable = false;
  6093. }
  6094. if (in_region >= 0)
  6095. color = color_regions[in_region].color;
  6096. else if (in_keyword)
  6097. color = keyword_color;
  6098. else if (in_member_variable)
  6099. color = cache.member_variable_color;
  6100. else if (in_function_name)
  6101. color = cache.function_color;
  6102. else if (is_symbol)
  6103. color = cache.symbol_color;
  6104. else if (is_number)
  6105. color = cache.number_color;
  6106. prev_is_char = is_char;
  6107. prev_is_number = is_number;
  6108. if (color != prev_color) {
  6109. prev_color = color;
  6110. highlighter_info.color = color;
  6111. color_map[j] = highlighter_info;
  6112. }
  6113. }
  6114. syntax_highlighting_cache[p_line] = color_map;
  6115. return color_map;
  6116. }
  6117. void SyntaxHighlighter::set_text_editor(TextEdit *p_text_editor) {
  6118. text_editor = p_text_editor;
  6119. }
  6120. TextEdit *SyntaxHighlighter::get_text_editor() {
  6121. return text_editor;
  6122. }