2
0

text_edit.cpp 207 KB

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