text_edit.cpp 222 KB

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