text_edit.cpp 212 KB

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