text_edit.cpp 215 KB

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