text_edit.cpp 227 KB

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