text_edit.cpp 185 KB

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