text_edit.cpp 187 KB

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