text_edit.cpp 150 KB

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