text_edit.cpp 210 KB

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