text_edit.cpp 207 KB

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