text_edit.cpp 222 KB

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