text_edit.cpp 217 KB

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