text_edit.cpp 219 KB

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