text_edit.cpp 193 KB

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