text_edit.cpp 219 KB

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