text_edit.cpp 178 KB

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