text_edit.cpp 179 KB

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