2
0

text_edit.cpp 212 KB

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