text_edit.cpp 211 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487
  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. insert_text_at_cursor(ch_pair);
  1498. cursor_set_column(cursor_position_to_move);
  1499. }
  1500. void TextEdit::_consume_backspace_for_pair_symbol(int prev_line, int prev_column) {
  1501. bool remove_right_symbol = false;
  1502. if (cursor.column < text[cursor.line].length() && cursor.column > 0) {
  1503. CharType left_char = text[cursor.line][cursor.column - 1];
  1504. CharType right_char = text[cursor.line][cursor.column];
  1505. if (right_char == _get_right_pair_symbol(left_char)) {
  1506. remove_right_symbol = true;
  1507. }
  1508. }
  1509. if (remove_right_symbol) {
  1510. _remove_text(prev_line, prev_column, cursor.line, cursor.column + 1);
  1511. } else {
  1512. _remove_text(prev_line, prev_column, cursor.line, cursor.column);
  1513. }
  1514. }
  1515. void TextEdit::backspace_at_cursor() {
  1516. if (readonly)
  1517. return;
  1518. if (cursor.column == 0 && cursor.line == 0)
  1519. return;
  1520. int prev_line = cursor.column ? cursor.line : cursor.line - 1;
  1521. int prev_column = cursor.column ? (cursor.column - 1) : (text[cursor.line - 1].length());
  1522. if (is_line_hidden(cursor.line))
  1523. set_line_as_hidden(prev_line, true);
  1524. if (is_line_set_as_breakpoint(cursor.line)) {
  1525. if (!text.is_breakpoint(prev_line))
  1526. emit_signal("breakpoint_toggled", prev_line);
  1527. set_line_as_breakpoint(prev_line, true);
  1528. }
  1529. if (text.has_info_icon(cursor.line)) {
  1530. set_line_info_icon(prev_line, text.get_info_icon(cursor.line), text.get_info(cursor.line));
  1531. }
  1532. if (auto_brace_completion_enabled &&
  1533. cursor.column > 0 &&
  1534. _is_pair_left_symbol(text[cursor.line][cursor.column - 1])) {
  1535. _consume_backspace_for_pair_symbol(prev_line, prev_column);
  1536. } else {
  1537. // Handle space indentation.
  1538. if (cursor.column != 0 && indent_using_spaces) {
  1539. // Check if there are no other chars before cursor, just indentation.
  1540. bool unindent = true;
  1541. int i = 0;
  1542. while (i < cursor.column && i < text[cursor.line].length()) {
  1543. if (!_is_whitespace(text[cursor.line][i])) {
  1544. unindent = false;
  1545. break;
  1546. }
  1547. i++;
  1548. }
  1549. // Then we can remove all spaces as a single character.
  1550. if (unindent) {
  1551. // We want to remove spaces up to closest indent, or whole indent if cursor is pointing at it.
  1552. int spaces_to_delete = _calculate_spaces_till_next_left_indent(cursor.column);
  1553. prev_column = cursor.column - spaces_to_delete;
  1554. _remove_text(cursor.line, prev_column, cursor.line, cursor.column);
  1555. } else {
  1556. _remove_text(prev_line, prev_column, cursor.line, cursor.column);
  1557. }
  1558. } else {
  1559. _remove_text(prev_line, prev_column, cursor.line, cursor.column);
  1560. }
  1561. }
  1562. cursor_set_line(prev_line, true, true);
  1563. cursor_set_column(prev_column);
  1564. }
  1565. void TextEdit::indent_right() {
  1566. int start_line;
  1567. int end_line;
  1568. // This value informs us by how much we changed selection position by indenting right.
  1569. // Default is 1 for tab indentation.
  1570. int selection_offset = 1;
  1571. begin_complex_operation();
  1572. if (is_selection_active()) {
  1573. start_line = get_selection_from_line();
  1574. end_line = get_selection_to_line();
  1575. } else {
  1576. start_line = cursor.line;
  1577. end_line = start_line;
  1578. }
  1579. // Ignore if the cursor is not past the first column.
  1580. if (is_selection_active() && get_selection_to_column() == 0) {
  1581. selection_offset = 0;
  1582. end_line--;
  1583. }
  1584. for (int i = start_line; i <= end_line; i++) {
  1585. String line_text = get_line(i);
  1586. if (indent_using_spaces) {
  1587. // We don't really care where selection is - we just need to know indentation level at the beginning of the line.
  1588. int left = _find_first_non_whitespace_column_of_line(line_text);
  1589. int spaces_to_add = _calculate_spaces_till_next_right_indent(left);
  1590. // Since we will add this much spaces we want move whole selection and cursor by this much.
  1591. selection_offset = spaces_to_add;
  1592. for (int j = 0; j < spaces_to_add; j++)
  1593. line_text = ' ' + line_text;
  1594. } else {
  1595. line_text = '\t' + line_text;
  1596. }
  1597. set_line(i, line_text);
  1598. }
  1599. // Fix selection and cursor being off after shifting selection right.
  1600. if (is_selection_active()) {
  1601. select(selection.from_line, selection.from_column + selection_offset, selection.to_line, selection.to_column + selection_offset);
  1602. }
  1603. cursor_set_column(cursor.column + selection_offset, false);
  1604. end_complex_operation();
  1605. update();
  1606. }
  1607. void TextEdit::indent_left() {
  1608. int start_line;
  1609. int end_line;
  1610. // Moving cursor and selection after unindenting can get tricky because
  1611. // changing content of line can move cursor and selection on it's own (if new line ends before previous position of either),
  1612. // therefore we just remember initial values and at the end of the operation offset them by number of removed characters.
  1613. int removed_characters = 0;
  1614. int initial_selection_end_column = selection.to_column;
  1615. int initial_cursor_column = cursor.column;
  1616. begin_complex_operation();
  1617. if (is_selection_active()) {
  1618. start_line = get_selection_from_line();
  1619. end_line = get_selection_to_line();
  1620. } else {
  1621. start_line = cursor.line;
  1622. end_line = start_line;
  1623. }
  1624. // Ignore if the cursor is not past the first column.
  1625. if (is_selection_active() && get_selection_to_column() == 0) {
  1626. end_line--;
  1627. }
  1628. String last_line_text = get_line(end_line);
  1629. for (int i = start_line; i <= end_line; i++) {
  1630. String line_text = get_line(i);
  1631. if (line_text.begins_with("\t")) {
  1632. line_text = line_text.substr(1, line_text.length());
  1633. set_line(i, line_text);
  1634. removed_characters = 1;
  1635. } else if (line_text.begins_with(" ")) {
  1636. // When unindenting we aim to remove spaces before line that has selection no matter what is selected,
  1637. // so we start of by finding first non whitespace character of line
  1638. int left = _find_first_non_whitespace_column_of_line(line_text);
  1639. // Here we remove only enough spaces to align text to nearest full multiple of indentation_size.
  1640. // In case where selection begins at the start of indentation_size multiple we remove whole indentation level.
  1641. int spaces_to_remove = _calculate_spaces_till_next_left_indent(left);
  1642. line_text = line_text.substr(spaces_to_remove, line_text.length());
  1643. set_line(i, line_text);
  1644. removed_characters = spaces_to_remove;
  1645. }
  1646. }
  1647. // Fix selection and cursor being off by one on the last line.
  1648. if (is_selection_active() && last_line_text != get_line(end_line)) {
  1649. select(selection.from_line, selection.from_column - removed_characters,
  1650. selection.to_line, initial_selection_end_column - removed_characters);
  1651. }
  1652. cursor_set_column(initial_cursor_column - removed_characters, false);
  1653. end_complex_operation();
  1654. update();
  1655. }
  1656. int TextEdit::_calculate_spaces_till_next_left_indent(int column) {
  1657. int spaces_till_indent = column % indent_size;
  1658. if (spaces_till_indent == 0)
  1659. spaces_till_indent = indent_size;
  1660. return spaces_till_indent;
  1661. }
  1662. int TextEdit::_calculate_spaces_till_next_right_indent(int column) {
  1663. return indent_size - column % indent_size;
  1664. }
  1665. void TextEdit::_get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) const {
  1666. float rows = p_mouse.y;
  1667. rows -= cache.style_normal->get_margin(MARGIN_TOP);
  1668. rows /= get_row_height();
  1669. rows += get_v_scroll_offset();
  1670. int first_vis_line = get_first_visible_line();
  1671. int row = first_vis_line + Math::floor(rows);
  1672. int wrap_index = 0;
  1673. if (is_wrap_enabled() || is_hiding_enabled()) {
  1674. int f_ofs = num_lines_from_rows(first_vis_line, cursor.wrap_ofs, rows + (1 * SGN(rows)), wrap_index) - 1;
  1675. if (rows < 0)
  1676. row = first_vis_line - f_ofs;
  1677. else
  1678. row = first_vis_line + f_ofs;
  1679. }
  1680. if (row < 0)
  1681. row = 0; // TODO.
  1682. int col = 0;
  1683. if (row >= text.size()) {
  1684. row = text.size() - 1;
  1685. col = text[row].size();
  1686. } else {
  1687. 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);
  1688. colx += cursor.x_ofs;
  1689. col = get_char_pos_for_line(colx, row, wrap_index);
  1690. if (is_wrap_enabled() && wrap_index < times_line_wraps(row)) {
  1691. // Move back one if we are at the end of the row.
  1692. Vector<String> rows2 = get_wrap_rows_text(row);
  1693. int row_end_col = 0;
  1694. for (int i = 0; i < wrap_index + 1; i++) {
  1695. row_end_col += rows2[i].length();
  1696. }
  1697. if (col >= row_end_col)
  1698. col -= 1;
  1699. }
  1700. }
  1701. r_row = row;
  1702. r_col = col;
  1703. }
  1704. Vector2i TextEdit::_get_cursor_pixel_pos() {
  1705. adjust_viewport_to_cursor();
  1706. int row = (cursor.line - get_first_visible_line() - cursor.wrap_ofs);
  1707. // Correct for hidden and wrapped lines
  1708. for (int i = get_first_visible_line(); i < cursor.line; i++) {
  1709. if (is_line_hidden(i)) {
  1710. row -= 1;
  1711. continue;
  1712. }
  1713. row += times_line_wraps(i);
  1714. }
  1715. // Row might be wrapped. Adjust row and r_column
  1716. Vector<String> rows2 = get_wrap_rows_text(cursor.line);
  1717. while (rows2.size() > 1) {
  1718. if (cursor.column >= rows2[0].length()) {
  1719. cursor.column -= rows2[0].length();
  1720. rows2.remove(0);
  1721. row++;
  1722. } else {
  1723. break;
  1724. }
  1725. }
  1726. // Calculate final pixel position
  1727. int y = (row - get_v_scroll_offset() + 1 /*Bottom of line*/) * get_row_height();
  1728. 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;
  1729. int ix = 0;
  1730. while (ix < rows2[0].size() && ix < cursor.column) {
  1731. if (cache.font != NULL) {
  1732. x += cache.font->get_char_size(rows2[0].get(ix)).width;
  1733. }
  1734. ix++;
  1735. }
  1736. x += get_indent_level(cursor.line) * cache.font->get_char_size(' ').width;
  1737. return Vector2i(x, y);
  1738. }
  1739. void TextEdit::_get_minimap_mouse_row(const Point2i &p_mouse, int &r_row) const {
  1740. float rows = p_mouse.y;
  1741. rows -= cache.style_normal->get_margin(MARGIN_TOP);
  1742. rows /= (minimap_char_size.y + minimap_line_spacing);
  1743. rows += get_v_scroll_offset();
  1744. // calculate visible lines
  1745. int minimap_visible_lines = _get_minimap_visible_rows();
  1746. int visible_rows = get_visible_rows() + 1;
  1747. int first_visible_line = get_first_visible_line() - 1;
  1748. int draw_amount = visible_rows + (smooth_scroll_enabled ? 1 : 0);
  1749. draw_amount += times_line_wraps(first_visible_line + 1);
  1750. int minimap_line_height = (minimap_char_size.y + minimap_line_spacing);
  1751. // calculate viewport size and y offset
  1752. int viewport_height = (draw_amount - 1) * minimap_line_height;
  1753. int control_height = _get_control_height() - viewport_height;
  1754. 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));
  1755. // calculate the first line.
  1756. int num_lines_before = round((viewport_offset_y) / minimap_line_height);
  1757. int wi;
  1758. int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_visible_line;
  1759. if (first_visible_line > 0 && minimap_line >= 0) {
  1760. minimap_line -= num_lines_from_rows(first_visible_line, 0, -num_lines_before, wi);
  1761. minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0);
  1762. } else {
  1763. minimap_line = 0;
  1764. }
  1765. int row = minimap_line + Math::floor(rows);
  1766. int wrap_index = 0;
  1767. if (is_wrap_enabled() || is_hiding_enabled()) {
  1768. int f_ofs = num_lines_from_rows(minimap_line, cursor.wrap_ofs, rows + (1 * SGN(rows)), wrap_index) - 1;
  1769. if (rows < 0) {
  1770. row = minimap_line - f_ofs;
  1771. } else {
  1772. row = minimap_line + f_ofs;
  1773. }
  1774. }
  1775. if (row < 0) {
  1776. row = 0;
  1777. }
  1778. if (row >= text.size()) {
  1779. row = text.size() - 1;
  1780. }
  1781. r_row = row;
  1782. }
  1783. void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
  1784. double prev_v_scroll = v_scroll->get_value();
  1785. double prev_h_scroll = h_scroll->get_value();
  1786. Ref<InputEventMouseButton> mb = p_gui_input;
  1787. if (mb.is_valid()) {
  1788. if (completion_active && completion_rect.has_point(mb->get_position())) {
  1789. if (!mb->is_pressed())
  1790. return;
  1791. if (mb->get_button_index() == BUTTON_WHEEL_UP) {
  1792. if (completion_index > 0) {
  1793. completion_index--;
  1794. completion_current = completion_options[completion_index];
  1795. update();
  1796. }
  1797. }
  1798. if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  1799. if (completion_index < completion_options.size() - 1) {
  1800. completion_index++;
  1801. completion_current = completion_options[completion_index];
  1802. update();
  1803. }
  1804. }
  1805. if (mb->get_button_index() == BUTTON_LEFT) {
  1806. completion_index = CLAMP(completion_line_ofs + (mb->get_position().y - completion_rect.position.y) / get_row_height(), 0, completion_options.size() - 1);
  1807. completion_current = completion_options[completion_index];
  1808. update();
  1809. if (mb->is_doubleclick())
  1810. _confirm_completion();
  1811. }
  1812. return;
  1813. } else {
  1814. _cancel_completion();
  1815. _cancel_code_hint();
  1816. }
  1817. if (mb->is_pressed()) {
  1818. if (mb->get_button_index() == BUTTON_WHEEL_UP && !mb->get_command()) {
  1819. if (mb->get_shift()) {
  1820. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1821. } else {
  1822. _scroll_up(3 * mb->get_factor());
  1823. }
  1824. }
  1825. if (mb->get_button_index() == BUTTON_WHEEL_DOWN && !mb->get_command()) {
  1826. if (mb->get_shift()) {
  1827. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1828. } else {
  1829. _scroll_down(3 * mb->get_factor());
  1830. }
  1831. }
  1832. if (mb->get_button_index() == BUTTON_WHEEL_LEFT) {
  1833. h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
  1834. }
  1835. if (mb->get_button_index() == BUTTON_WHEEL_RIGHT) {
  1836. h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
  1837. }
  1838. if (mb->get_button_index() == BUTTON_LEFT) {
  1839. _reset_caret_blink_timer();
  1840. int row, col;
  1841. _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col);
  1842. // Toggle breakpoint on gutter click.
  1843. if (draw_breakpoint_gutter) {
  1844. int gutter = cache.style_normal->get_margin(MARGIN_LEFT);
  1845. if (mb->get_position().x > gutter - 6 && mb->get_position().x <= gutter + cache.breakpoint_gutter_width - 3) {
  1846. set_line_as_breakpoint(row, !is_line_set_as_breakpoint(row));
  1847. emit_signal("breakpoint_toggled", row);
  1848. return;
  1849. }
  1850. }
  1851. // Emit info clicked.
  1852. if (draw_info_gutter && text.has_info_icon(row)) {
  1853. int left_margin = cache.style_normal->get_margin(MARGIN_LEFT);
  1854. int gutter_left = left_margin + cache.breakpoint_gutter_width;
  1855. if (mb->get_position().x > gutter_left - 6 && mb->get_position().x <= gutter_left + cache.info_gutter_width - 3) {
  1856. emit_signal("info_clicked", row, text.get_info(row));
  1857. return;
  1858. }
  1859. }
  1860. // Toggle fold on gutter click if can.
  1861. if (draw_fold_gutter) {
  1862. int left_margin = cache.style_normal->get_margin(MARGIN_LEFT);
  1863. int gutter_left = left_margin + cache.breakpoint_gutter_width + cache.line_number_w + cache.info_gutter_width;
  1864. if (mb->get_position().x > gutter_left - 6 && mb->get_position().x <= gutter_left + cache.fold_gutter_width - 3) {
  1865. if (is_folded(row)) {
  1866. unfold_line(row);
  1867. } else if (can_fold(row)) {
  1868. fold_line(row);
  1869. }
  1870. return;
  1871. }
  1872. }
  1873. // Unfold on folded icon click.
  1874. if (is_folded(row)) {
  1875. int line_width = text.get_line_width(row);
  1876. 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;
  1877. if (mb->get_position().x > line_width - 3 && mb->get_position().x <= line_width + cache.folded_eol_icon->get_width() + 3) {
  1878. unfold_line(row);
  1879. return;
  1880. }
  1881. }
  1882. // minimap
  1883. if (draw_minimap) {
  1884. _update_minimap_click();
  1885. if (dragging_minimap) {
  1886. return;
  1887. }
  1888. }
  1889. int prev_col = cursor.column;
  1890. int prev_line = cursor.line;
  1891. cursor_set_line(row, true, false);
  1892. cursor_set_column(col);
  1893. if (mb->get_shift() && (cursor.column != prev_col || cursor.line != prev_line)) {
  1894. if (!selection.active) {
  1895. selection.active = true;
  1896. selection.selecting_mode = Selection::MODE_POINTER;
  1897. selection.from_column = prev_col;
  1898. selection.from_line = prev_line;
  1899. selection.to_column = cursor.column;
  1900. selection.to_line = cursor.line;
  1901. if (selection.from_line > selection.to_line || (selection.from_line == selection.to_line && selection.from_column > selection.to_column)) {
  1902. SWAP(selection.from_column, selection.to_column);
  1903. SWAP(selection.from_line, selection.to_line);
  1904. selection.shiftclick_left = false;
  1905. } else {
  1906. selection.shiftclick_left = true;
  1907. }
  1908. selection.selecting_line = prev_line;
  1909. selection.selecting_column = prev_col;
  1910. update();
  1911. } else {
  1912. if (cursor.line < selection.selecting_line || (cursor.line == selection.selecting_line && cursor.column < selection.selecting_column)) {
  1913. if (selection.shiftclick_left) {
  1914. SWAP(selection.from_column, selection.to_column);
  1915. SWAP(selection.from_line, selection.to_line);
  1916. selection.shiftclick_left = !selection.shiftclick_left;
  1917. }
  1918. selection.from_column = cursor.column;
  1919. selection.from_line = cursor.line;
  1920. } else if (cursor.line > selection.selecting_line || (cursor.line == selection.selecting_line && cursor.column > selection.selecting_column)) {
  1921. if (!selection.shiftclick_left) {
  1922. SWAP(selection.from_column, selection.to_column);
  1923. SWAP(selection.from_line, selection.to_line);
  1924. selection.shiftclick_left = !selection.shiftclick_left;
  1925. }
  1926. selection.to_column = cursor.column;
  1927. selection.to_line = cursor.line;
  1928. } else {
  1929. selection.active = false;
  1930. }
  1931. update();
  1932. }
  1933. } else {
  1934. selection.active = false;
  1935. selection.selecting_mode = Selection::MODE_POINTER;
  1936. selection.selecting_line = row;
  1937. selection.selecting_column = col;
  1938. }
  1939. if (!mb->is_doubleclick() && (OS::get_singleton()->get_ticks_msec() - last_dblclk) < 600 && cursor.line == prev_line) {
  1940. // Triple-click select line.
  1941. selection.selecting_mode = Selection::MODE_LINE;
  1942. _update_selection_mode_line();
  1943. last_dblclk = 0;
  1944. } else if (mb->is_doubleclick() && text[cursor.line].length()) {
  1945. // Double-click select word.
  1946. selection.selecting_mode = Selection::MODE_WORD;
  1947. _update_selection_mode_word();
  1948. last_dblclk = OS::get_singleton()->get_ticks_msec();
  1949. }
  1950. update();
  1951. }
  1952. if (mb->get_button_index() == BUTTON_RIGHT && context_menu_enabled) {
  1953. _reset_caret_blink_timer();
  1954. int row, col;
  1955. _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col);
  1956. if (is_right_click_moving_caret()) {
  1957. if (is_selection_active()) {
  1958. int from_line = get_selection_from_line();
  1959. int to_line = get_selection_to_line();
  1960. int from_column = get_selection_from_column();
  1961. int to_column = get_selection_to_column();
  1962. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  1963. // Right click is outside the selected text.
  1964. deselect();
  1965. }
  1966. }
  1967. if (!is_selection_active()) {
  1968. cursor_set_line(row, true, false);
  1969. cursor_set_column(col);
  1970. }
  1971. }
  1972. menu->set_position(get_global_transform().xform(get_local_mouse_position()));
  1973. menu->set_size(Vector2(1, 1));
  1974. menu->set_scale(get_global_transform().get_scale());
  1975. menu->popup();
  1976. grab_focus();
  1977. }
  1978. } else {
  1979. if (mb->get_button_index() == BUTTON_LEFT) {
  1980. if (mb->get_command() && highlighted_word != String()) {
  1981. int row, col;
  1982. _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col);
  1983. emit_signal("symbol_lookup", highlighted_word, row, col);
  1984. return;
  1985. }
  1986. dragging_minimap = false;
  1987. dragging_selection = false;
  1988. can_drag_minimap = false;
  1989. click_select_held->stop();
  1990. }
  1991. // Notify to show soft keyboard.
  1992. notification(NOTIFICATION_FOCUS_ENTER);
  1993. }
  1994. }
  1995. const Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  1996. if (pan_gesture.is_valid()) {
  1997. const real_t delta = pan_gesture->get_delta().y;
  1998. if (delta < 0) {
  1999. _scroll_up(-delta);
  2000. } else {
  2001. _scroll_down(delta);
  2002. }
  2003. h_scroll->set_value(h_scroll->get_value() + pan_gesture->get_delta().x * 100);
  2004. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll)
  2005. accept_event(); // Accept event if scroll changed.
  2006. return;
  2007. }
  2008. Ref<InputEventMouseMotion> mm = p_gui_input;
  2009. if (mm.is_valid()) {
  2010. if (select_identifiers_enabled) {
  2011. if (!dragging_minimap && !dragging_selection && mm->get_command() && mm->get_button_mask() == 0) {
  2012. String new_word = get_word_at_pos(mm->get_position());
  2013. if (new_word != highlighted_word) {
  2014. highlighted_word = new_word;
  2015. update();
  2016. }
  2017. } else {
  2018. if (highlighted_word != String()) {
  2019. highlighted_word = String();
  2020. update();
  2021. }
  2022. }
  2023. }
  2024. if (mm->get_button_mask() & BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data() == Variant()) { // Ignore if dragging.
  2025. _reset_caret_blink_timer();
  2026. if (draw_minimap && !dragging_selection) {
  2027. _update_minimap_drag();
  2028. }
  2029. if (!dragging_minimap) {
  2030. switch (selection.selecting_mode) {
  2031. case Selection::MODE_POINTER: {
  2032. _update_selection_mode_pointer();
  2033. } break;
  2034. case Selection::MODE_WORD: {
  2035. _update_selection_mode_word();
  2036. } break;
  2037. case Selection::MODE_LINE: {
  2038. _update_selection_mode_line();
  2039. } break;
  2040. default: {
  2041. break;
  2042. }
  2043. }
  2044. }
  2045. }
  2046. }
  2047. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll)
  2048. accept_event(); // Accept event if scroll changed.
  2049. Ref<InputEventKey> k = p_gui_input;
  2050. if (k.is_valid()) {
  2051. k = k->duplicate(); // It will be modified later on.
  2052. #ifdef OSX_ENABLED
  2053. if (k->get_scancode() == KEY_META) {
  2054. #else
  2055. if (k->get_scancode() == KEY_CONTROL) {
  2056. #endif
  2057. if (select_identifiers_enabled) {
  2058. if (k->is_pressed() && !dragging_minimap && !dragging_selection) {
  2059. highlighted_word = get_word_at_pos(get_local_mouse_position());
  2060. update();
  2061. } else {
  2062. highlighted_word = String();
  2063. update();
  2064. }
  2065. }
  2066. }
  2067. if (!k->is_pressed())
  2068. return;
  2069. if (completion_active) {
  2070. if (readonly)
  2071. return;
  2072. bool valid = true;
  2073. if (k->get_command() || k->get_metakey())
  2074. valid = false;
  2075. if (valid) {
  2076. if (!k->get_alt()) {
  2077. if (k->get_scancode() == KEY_UP) {
  2078. if (completion_index > 0) {
  2079. completion_index--;
  2080. } else {
  2081. completion_index = completion_options.size() - 1;
  2082. }
  2083. completion_current = completion_options[completion_index];
  2084. update();
  2085. accept_event();
  2086. return;
  2087. }
  2088. if (k->get_scancode() == KEY_DOWN) {
  2089. if (completion_index < completion_options.size() - 1) {
  2090. completion_index++;
  2091. } else {
  2092. completion_index = 0;
  2093. }
  2094. completion_current = completion_options[completion_index];
  2095. update();
  2096. accept_event();
  2097. return;
  2098. }
  2099. if (k->get_scancode() == KEY_PAGEUP) {
  2100. completion_index -= get_constant("completion_lines");
  2101. if (completion_index < 0)
  2102. completion_index = 0;
  2103. completion_current = completion_options[completion_index];
  2104. update();
  2105. accept_event();
  2106. return;
  2107. }
  2108. if (k->get_scancode() == KEY_PAGEDOWN) {
  2109. completion_index += get_constant("completion_lines");
  2110. if (completion_index >= completion_options.size())
  2111. completion_index = completion_options.size() - 1;
  2112. completion_current = completion_options[completion_index];
  2113. update();
  2114. accept_event();
  2115. return;
  2116. }
  2117. if (k->get_scancode() == KEY_HOME && completion_index > 0) {
  2118. completion_index = 0;
  2119. completion_current = completion_options[completion_index];
  2120. update();
  2121. accept_event();
  2122. return;
  2123. }
  2124. if (k->get_scancode() == KEY_END && completion_index < completion_options.size() - 1) {
  2125. completion_index = completion_options.size() - 1;
  2126. completion_current = completion_options[completion_index];
  2127. update();
  2128. accept_event();
  2129. return;
  2130. }
  2131. if (k->get_scancode() == KEY_KP_ENTER || k->get_scancode() == KEY_ENTER || k->get_scancode() == KEY_TAB) {
  2132. _confirm_completion();
  2133. accept_event();
  2134. return;
  2135. }
  2136. if (k->get_scancode() == KEY_BACKSPACE) {
  2137. _reset_caret_blink_timer();
  2138. backspace_at_cursor();
  2139. _update_completion_candidates();
  2140. accept_event();
  2141. return;
  2142. }
  2143. if (k->get_scancode() == KEY_SHIFT) {
  2144. accept_event();
  2145. return;
  2146. }
  2147. }
  2148. if (k->get_unicode() > 32) {
  2149. _reset_caret_blink_timer();
  2150. const CharType chr[2] = { (CharType)k->get_unicode(), 0 };
  2151. if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  2152. _consume_pair_symbol(chr[0]);
  2153. } else {
  2154. // Remove the old character if in insert mode.
  2155. if (insert_mode) {
  2156. begin_complex_operation();
  2157. // Make sure we don't try and remove empty space.
  2158. if (cursor.column < get_line(cursor.line).length()) {
  2159. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  2160. }
  2161. }
  2162. _insert_text_at_cursor(chr);
  2163. if (insert_mode) {
  2164. end_complex_operation();
  2165. }
  2166. }
  2167. _update_completion_candidates();
  2168. accept_event();
  2169. return;
  2170. }
  2171. }
  2172. _cancel_completion();
  2173. }
  2174. /* TEST CONTROL FIRST! */
  2175. // Some remaps for duplicate functions.
  2176. if (k->get_command() && !k->get_shift() && !k->get_alt() && !k->get_metakey() && k->get_scancode() == KEY_INSERT) {
  2177. k->set_scancode(KEY_C);
  2178. }
  2179. if (!k->get_command() && k->get_shift() && !k->get_alt() && !k->get_metakey() && k->get_scancode() == KEY_INSERT) {
  2180. k->set_scancode(KEY_V);
  2181. k->set_command(true);
  2182. k->set_shift(false);
  2183. }
  2184. #ifdef APPLE_STYLE_KEYS
  2185. if (k->get_control() && !k->get_shift() && !k->get_alt() && !k->get_command()) {
  2186. uint32_t remap_key = KEY_UNKNOWN;
  2187. switch (k->get_scancode()) {
  2188. case KEY_F: {
  2189. remap_key = KEY_RIGHT;
  2190. } break;
  2191. case KEY_B: {
  2192. remap_key = KEY_LEFT;
  2193. } break;
  2194. case KEY_P: {
  2195. remap_key = KEY_UP;
  2196. } break;
  2197. case KEY_N: {
  2198. remap_key = KEY_DOWN;
  2199. } break;
  2200. case KEY_D: {
  2201. remap_key = KEY_DELETE;
  2202. } break;
  2203. case KEY_H: {
  2204. remap_key = KEY_BACKSPACE;
  2205. } break;
  2206. }
  2207. if (remap_key != KEY_UNKNOWN) {
  2208. k->set_scancode(remap_key);
  2209. k->set_control(false);
  2210. }
  2211. }
  2212. #endif
  2213. _reset_caret_blink_timer();
  2214. // Save here for insert mode, just in case it is cleared in the following section.
  2215. bool had_selection = selection.active;
  2216. // Stuff to do when selection is active.
  2217. if (!readonly && selection.active) {
  2218. bool clear = false;
  2219. bool unselect = false;
  2220. bool dobreak = false;
  2221. switch (k->get_scancode()) {
  2222. case KEY_TAB: {
  2223. if (k->get_shift()) {
  2224. indent_left();
  2225. } else {
  2226. indent_right();
  2227. }
  2228. dobreak = true;
  2229. accept_event();
  2230. } break;
  2231. case KEY_X:
  2232. case KEY_C:
  2233. // Special keys often used with control, wait.
  2234. clear = (!k->get_command() || k->get_shift() || k->get_alt());
  2235. break;
  2236. case KEY_DELETE:
  2237. if (!k->get_shift()) {
  2238. accept_event();
  2239. clear = true;
  2240. dobreak = true;
  2241. } else if (k->get_command() || k->get_alt()) {
  2242. dobreak = true;
  2243. }
  2244. break;
  2245. case KEY_BACKSPACE:
  2246. accept_event();
  2247. clear = true;
  2248. dobreak = true;
  2249. break;
  2250. case KEY_LEFT:
  2251. case KEY_RIGHT:
  2252. case KEY_UP:
  2253. case KEY_DOWN:
  2254. case KEY_PAGEUP:
  2255. case KEY_PAGEDOWN:
  2256. case KEY_HOME:
  2257. case KEY_END:
  2258. // Ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys).
  2259. if (k->get_command() || k->get_shift() || k->get_alt())
  2260. break;
  2261. unselect = true;
  2262. break;
  2263. default:
  2264. if (k->get_unicode() >= 32 && !k->get_command() && !k->get_alt() && !k->get_metakey())
  2265. clear = true;
  2266. if (auto_brace_completion_enabled && _is_pair_left_symbol(k->get_unicode()))
  2267. clear = false;
  2268. }
  2269. if (unselect) {
  2270. selection.active = false;
  2271. selection.selecting_mode = Selection::MODE_NONE;
  2272. update();
  2273. }
  2274. if (clear) {
  2275. if (!dobreak) {
  2276. begin_complex_operation();
  2277. }
  2278. selection.active = false;
  2279. update();
  2280. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  2281. cursor_set_line(selection.from_line, true, false);
  2282. cursor_set_column(selection.from_column);
  2283. update();
  2284. }
  2285. if (dobreak)
  2286. return;
  2287. }
  2288. selection.selecting_text = false;
  2289. bool scancode_handled = true;
  2290. // Special scancode test.
  2291. switch (k->get_scancode()) {
  2292. case KEY_KP_ENTER:
  2293. case KEY_ENTER: {
  2294. if (readonly)
  2295. break;
  2296. String ins = "\n";
  2297. // Keep indentation.
  2298. int space_count = 0;
  2299. for (int i = 0; i < cursor.column; i++) {
  2300. if (text[cursor.line][i] == '\t') {
  2301. if (indent_using_spaces) {
  2302. ins += space_indent;
  2303. } else {
  2304. ins += "\t";
  2305. }
  2306. space_count = 0;
  2307. } else if (text[cursor.line][i] == ' ') {
  2308. space_count++;
  2309. if (space_count == indent_size) {
  2310. if (indent_using_spaces) {
  2311. ins += space_indent;
  2312. } else {
  2313. ins += "\t";
  2314. }
  2315. space_count = 0;
  2316. }
  2317. } else {
  2318. break;
  2319. }
  2320. }
  2321. if (is_folded(cursor.line))
  2322. unfold_line(cursor.line);
  2323. bool brace_indent = false;
  2324. // No need to indent if we are going upwards.
  2325. if (auto_indent && !(k->get_command() && k->get_shift())) {
  2326. // Indent once again if previous line will end with ':','{','[','(' and the line is not a comment
  2327. // (i.e. colon/brace precedes current cursor position).
  2328. if (cursor.column > 0) {
  2329. const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(cursor.line);
  2330. bool indent_char_found = false;
  2331. bool should_indent = false;
  2332. char indent_char = ':';
  2333. char c = text[cursor.line][cursor.column];
  2334. for (int i = 0; i < cursor.column; i++) {
  2335. c = text[cursor.line][i];
  2336. switch (c) {
  2337. case ':':
  2338. case '{':
  2339. case '[':
  2340. case '(':
  2341. indent_char_found = true;
  2342. should_indent = true;
  2343. indent_char = c;
  2344. continue;
  2345. }
  2346. if (indent_char_found && cri_map.has(i) && (color_regions[cri_map[i].region].begin_key == "#" || color_regions[cri_map[i].region].begin_key == "//")) {
  2347. should_indent = true;
  2348. break;
  2349. } else if (indent_char_found && !_is_whitespace(c)) {
  2350. should_indent = false;
  2351. indent_char_found = false;
  2352. }
  2353. }
  2354. if (!is_line_comment(cursor.line) && should_indent) {
  2355. if (indent_using_spaces) {
  2356. ins += space_indent;
  2357. } else {
  2358. ins += "\t";
  2359. }
  2360. // No need to move the brace below if we are not taking the text with us.
  2361. char closing_char = _get_right_pair_symbol(indent_char);
  2362. if ((closing_char != 0) && (closing_char == text[cursor.line][cursor.column]) && !k->get_command()) {
  2363. brace_indent = true;
  2364. ins += "\n" + ins.substr(1, ins.length() - 2);
  2365. }
  2366. }
  2367. }
  2368. }
  2369. begin_complex_operation();
  2370. bool first_line = false;
  2371. if (k->get_command()) {
  2372. if (k->get_shift()) {
  2373. if (cursor.line > 0) {
  2374. cursor_set_line(cursor.line - 1);
  2375. cursor_set_column(text[cursor.line].length());
  2376. } else {
  2377. cursor_set_column(0);
  2378. first_line = true;
  2379. }
  2380. } else {
  2381. cursor_set_column(text[cursor.line].length());
  2382. }
  2383. }
  2384. insert_text_at_cursor(ins);
  2385. if (first_line) {
  2386. cursor_set_line(0);
  2387. } else if (brace_indent) {
  2388. cursor_set_line(cursor.line - 1);
  2389. cursor_set_column(text[cursor.line].length());
  2390. }
  2391. end_complex_operation();
  2392. } break;
  2393. case KEY_ESCAPE: {
  2394. if (completion_hint != "") {
  2395. completion_hint = "";
  2396. update();
  2397. } else {
  2398. scancode_handled = false;
  2399. }
  2400. } break;
  2401. case KEY_TAB: {
  2402. if (k->get_command()) break; // Avoid tab when command.
  2403. if (readonly)
  2404. break;
  2405. if (is_selection_active()) {
  2406. if (k->get_shift()) {
  2407. indent_left();
  2408. } else {
  2409. indent_right();
  2410. }
  2411. } else {
  2412. if (k->get_shift()) {
  2413. // Simple unindent.
  2414. int cc = cursor.column;
  2415. const String &line = text[cursor.line];
  2416. int left = _find_first_non_whitespace_column_of_line(line);
  2417. cc = MIN(cc, left);
  2418. while (cc < indent_size && cc < left && line[cc] == ' ')
  2419. cc++;
  2420. if (cc > 0 && cc <= text[cursor.line].length()) {
  2421. if (text[cursor.line][cc - 1] == '\t') {
  2422. // Tabs unindentation.
  2423. _remove_text(cursor.line, cc - 1, cursor.line, cc);
  2424. if (cursor.column >= left)
  2425. cursor_set_column(MAX(0, cursor.column - 1));
  2426. update();
  2427. } else {
  2428. // Spaces unindentation.
  2429. int spaces_to_remove = _calculate_spaces_till_next_left_indent(cc);
  2430. if (spaces_to_remove > 0) {
  2431. _remove_text(cursor.line, cc - spaces_to_remove, cursor.line, cc);
  2432. if (cursor.column > left - spaces_to_remove) // Inside text?
  2433. cursor_set_column(MAX(0, cursor.column - spaces_to_remove));
  2434. update();
  2435. }
  2436. }
  2437. } else if (cc == 0 && line.length() > 0 && line[0] == '\t') {
  2438. _remove_text(cursor.line, 0, cursor.line, 1);
  2439. update();
  2440. }
  2441. } else {
  2442. // Simple indent.
  2443. if (indent_using_spaces) {
  2444. // Insert only as much spaces as needed till next indentation level.
  2445. int spaces_to_add = _calculate_spaces_till_next_right_indent(cursor.column);
  2446. String indent_to_insert = String();
  2447. for (int i = 0; i < spaces_to_add; i++)
  2448. indent_to_insert = ' ' + indent_to_insert;
  2449. _insert_text_at_cursor(indent_to_insert);
  2450. } else {
  2451. _insert_text_at_cursor("\t");
  2452. }
  2453. }
  2454. }
  2455. } break;
  2456. case KEY_BACKSPACE: {
  2457. if (readonly)
  2458. break;
  2459. #ifdef APPLE_STYLE_KEYS
  2460. if (k->get_alt() && cursor.column > 1) {
  2461. #else
  2462. if (k->get_alt()) {
  2463. scancode_handled = false;
  2464. break;
  2465. } else if (k->get_command() && cursor.column > 1) {
  2466. #endif
  2467. int line = cursor.line;
  2468. int column = cursor.column;
  2469. // Check if we are removing a single whitespace, if so remove it and the next char type,
  2470. // else we just remove the whitespace.
  2471. bool only_whitespace = false;
  2472. if (_is_whitespace(text[line][column - 1]) && _is_whitespace(text[line][column - 2])) {
  2473. only_whitespace = true;
  2474. } else if (_is_whitespace(text[line][column - 1])) {
  2475. // Remove the single whitespace.
  2476. column--;
  2477. }
  2478. // Check if its a text char.
  2479. bool only_char = (_is_text_char(text[line][column - 1]) && !only_whitespace);
  2480. // If its not whitespace or char then symbol.
  2481. bool only_symbols = !(only_whitespace || only_char);
  2482. while (column > 0) {
  2483. bool is_whitespace = _is_whitespace(text[line][column - 1]);
  2484. bool is_text_char = _is_text_char(text[line][column - 1]);
  2485. if (only_whitespace && !is_whitespace) {
  2486. break;
  2487. } else if (only_char && !is_text_char) {
  2488. break;
  2489. } else if (only_symbols && (is_whitespace || is_text_char)) {
  2490. break;
  2491. }
  2492. column--;
  2493. }
  2494. _remove_text(line, column, cursor.line, cursor.column);
  2495. cursor_set_line(line);
  2496. cursor_set_column(column);
  2497. #ifdef APPLE_STYLE_KEYS
  2498. } else if (k->get_command()) {
  2499. int cursor_current_column = cursor.column;
  2500. cursor.column = 0;
  2501. _remove_text(cursor.line, 0, cursor.line, cursor_current_column);
  2502. #endif
  2503. } else {
  2504. if (cursor.line > 0 && is_line_hidden(cursor.line - 1))
  2505. unfold_line(cursor.line - 1);
  2506. backspace_at_cursor();
  2507. }
  2508. } break;
  2509. case KEY_KP_4: {
  2510. if (k->get_unicode() != 0) {
  2511. scancode_handled = false;
  2512. break;
  2513. }
  2514. FALLTHROUGH;
  2515. }
  2516. case KEY_LEFT: {
  2517. if (k->get_shift())
  2518. _pre_shift_selection();
  2519. #ifdef APPLE_STYLE_KEYS
  2520. else
  2521. #else
  2522. else if (!k->get_alt())
  2523. #endif
  2524. deselect();
  2525. #ifdef APPLE_STYLE_KEYS
  2526. if (k->get_command()) {
  2527. // Start at first column (it's slightly faster that way) and look for the first non-whitespace character.
  2528. int new_cursor_pos = 0;
  2529. for (int i = 0; i < text[cursor.line].length(); ++i) {
  2530. if (!_is_whitespace(text[cursor.line][i])) {
  2531. new_cursor_pos = i;
  2532. break;
  2533. }
  2534. }
  2535. if (new_cursor_pos == cursor.column) {
  2536. // We're already at the first text character, so move to the very beginning of the line.
  2537. cursor_set_column(0);
  2538. } else {
  2539. // We're somewhere to the right of the first text character; move to the first one.
  2540. cursor_set_column(new_cursor_pos);
  2541. }
  2542. } else if (k->get_alt()) {
  2543. #else
  2544. if (k->get_alt()) {
  2545. scancode_handled = false;
  2546. break;
  2547. } else if (k->get_command()) {
  2548. #endif
  2549. int cc = cursor.column;
  2550. if (cc == 0 && cursor.line > 0) {
  2551. cursor_set_line(cursor.line - 1);
  2552. cursor_set_column(text[cursor.line].length());
  2553. } else {
  2554. bool prev_char = false;
  2555. while (cc > 0) {
  2556. bool ischar = _is_text_char(text[cursor.line][cc - 1]);
  2557. if (prev_char && !ischar)
  2558. break;
  2559. prev_char = ischar;
  2560. cc--;
  2561. }
  2562. cursor_set_column(cc);
  2563. }
  2564. } else if (cursor.column == 0) {
  2565. if (cursor.line > 0) {
  2566. cursor_set_line(cursor.line - num_lines_from(CLAMP(cursor.line - 1, 0, text.size() - 1), -1));
  2567. cursor_set_column(text[cursor.line].length());
  2568. }
  2569. } else {
  2570. cursor_set_column(cursor_get_column() - 1);
  2571. }
  2572. if (k->get_shift())
  2573. _post_shift_selection();
  2574. } break;
  2575. case KEY_KP_6: {
  2576. if (k->get_unicode() != 0) {
  2577. scancode_handled = false;
  2578. break;
  2579. }
  2580. FALLTHROUGH;
  2581. }
  2582. case KEY_RIGHT: {
  2583. if (k->get_shift())
  2584. _pre_shift_selection();
  2585. #ifdef APPLE_STYLE_KEYS
  2586. else
  2587. #else
  2588. else if (!k->get_alt())
  2589. #endif
  2590. deselect();
  2591. #ifdef APPLE_STYLE_KEYS
  2592. if (k->get_command()) {
  2593. cursor_set_column(text[cursor.line].length());
  2594. } else if (k->get_alt()) {
  2595. #else
  2596. if (k->get_alt()) {
  2597. scancode_handled = false;
  2598. break;
  2599. } else if (k->get_command()) {
  2600. #endif
  2601. int cc = cursor.column;
  2602. if (cc == text[cursor.line].length() && cursor.line < text.size() - 1) {
  2603. cursor_set_line(cursor.line + 1);
  2604. cursor_set_column(0);
  2605. } else {
  2606. bool prev_char = false;
  2607. while (cc < text[cursor.line].length()) {
  2608. bool ischar = _is_text_char(text[cursor.line][cc]);
  2609. if (prev_char && !ischar)
  2610. break;
  2611. prev_char = ischar;
  2612. cc++;
  2613. }
  2614. cursor_set_column(cc);
  2615. }
  2616. } else if (cursor.column == text[cursor.line].length()) {
  2617. if (cursor.line < text.size() - 1) {
  2618. cursor_set_line(cursor_get_line() + num_lines_from(CLAMP(cursor.line + 1, 0, text.size() - 1), 1), true, false);
  2619. cursor_set_column(0);
  2620. }
  2621. } else {
  2622. cursor_set_column(cursor_get_column() + 1);
  2623. }
  2624. if (k->get_shift())
  2625. _post_shift_selection();
  2626. } break;
  2627. case KEY_KP_8: {
  2628. if (k->get_unicode() != 0) {
  2629. scancode_handled = false;
  2630. break;
  2631. }
  2632. FALLTHROUGH;
  2633. }
  2634. case KEY_UP: {
  2635. if (k->get_alt()) {
  2636. scancode_handled = false;
  2637. break;
  2638. }
  2639. #ifndef APPLE_STYLE_KEYS
  2640. if (k->get_command()) {
  2641. #else
  2642. if (k->get_command() && k->get_alt()) {
  2643. #endif
  2644. _scroll_lines_up();
  2645. break;
  2646. }
  2647. if (k->get_shift()) {
  2648. _pre_shift_selection();
  2649. }
  2650. #ifdef APPLE_STYLE_KEYS
  2651. if (k->get_command()) {
  2652. cursor_set_line(0);
  2653. } else
  2654. #endif
  2655. {
  2656. int cur_wrap_index = get_cursor_wrap_index();
  2657. if (cur_wrap_index > 0) {
  2658. cursor_set_line(cursor.line, true, false, cur_wrap_index - 1);
  2659. } else if (cursor.line == 0) {
  2660. cursor_set_column(0);
  2661. } else {
  2662. int new_line = cursor.line - num_lines_from(cursor.line - 1, -1);
  2663. if (line_wraps(new_line)) {
  2664. cursor_set_line(new_line, true, false, times_line_wraps(new_line));
  2665. } else {
  2666. cursor_set_line(new_line, true, false);
  2667. }
  2668. }
  2669. }
  2670. if (k->get_shift())
  2671. _post_shift_selection();
  2672. _cancel_code_hint();
  2673. } break;
  2674. case KEY_KP_2: {
  2675. if (k->get_unicode() != 0) {
  2676. scancode_handled = false;
  2677. break;
  2678. }
  2679. FALLTHROUGH;
  2680. }
  2681. case KEY_DOWN: {
  2682. if (k->get_alt()) {
  2683. scancode_handled = false;
  2684. break;
  2685. }
  2686. #ifndef APPLE_STYLE_KEYS
  2687. if (k->get_command()) {
  2688. #else
  2689. if (k->get_command() && k->get_alt()) {
  2690. #endif
  2691. _scroll_lines_down();
  2692. break;
  2693. }
  2694. if (k->get_shift()) {
  2695. _pre_shift_selection();
  2696. }
  2697. #ifdef APPLE_STYLE_KEYS
  2698. if (k->get_command()) {
  2699. cursor_set_line(get_last_unhidden_line(), true, false, 9999);
  2700. } else
  2701. #endif
  2702. {
  2703. int cur_wrap_index = get_cursor_wrap_index();
  2704. if (cur_wrap_index < times_line_wraps(cursor.line)) {
  2705. cursor_set_line(cursor.line, true, false, cur_wrap_index + 1);
  2706. } else if (cursor.line == get_last_unhidden_line()) {
  2707. cursor_set_column(text[cursor.line].length());
  2708. } else {
  2709. int new_line = cursor.line + num_lines_from(CLAMP(cursor.line + 1, 0, text.size() - 1), 1);
  2710. cursor_set_line(new_line, true, false, 0);
  2711. }
  2712. }
  2713. if (k->get_shift())
  2714. _post_shift_selection();
  2715. _cancel_code_hint();
  2716. } break;
  2717. case KEY_DELETE: {
  2718. if (readonly)
  2719. break;
  2720. if (k->get_shift() && !k->get_command() && !k->get_alt() && is_shortcut_keys_enabled()) {
  2721. cut();
  2722. break;
  2723. }
  2724. int curline_len = text[cursor.line].length();
  2725. if (cursor.line == text.size() - 1 && cursor.column == curline_len)
  2726. break; // Nothing to do.
  2727. int next_line = cursor.column < curline_len ? cursor.line : cursor.line + 1;
  2728. int next_column;
  2729. #ifdef APPLE_STYLE_KEYS
  2730. if (k->get_alt() && cursor.column < curline_len - 1) {
  2731. #else
  2732. if (k->get_alt()) {
  2733. scancode_handled = false;
  2734. break;
  2735. } else if (k->get_command() && cursor.column < curline_len - 1) {
  2736. #endif
  2737. int line = cursor.line;
  2738. int column = cursor.column;
  2739. // Check if we are removing a single whitespace, if so remove it and the next char type,
  2740. // else we just remove the whitespace.
  2741. bool only_whitespace = false;
  2742. if (_is_whitespace(text[line][column]) && _is_whitespace(text[line][column + 1])) {
  2743. only_whitespace = true;
  2744. } else if (_is_whitespace(text[line][column])) {
  2745. // Remove the single whitespace.
  2746. column++;
  2747. }
  2748. // Check if its a text char.
  2749. bool only_char = (_is_text_char(text[line][column]) && !only_whitespace);
  2750. // If its not whitespace or char then symbol.
  2751. bool only_symbols = !(only_whitespace || only_char);
  2752. while (column < curline_len) {
  2753. bool is_whitespace = _is_whitespace(text[line][column]);
  2754. bool is_text_char = _is_text_char(text[line][column]);
  2755. if (only_whitespace && !is_whitespace) {
  2756. break;
  2757. } else if (only_char && !is_text_char) {
  2758. break;
  2759. } else if (only_symbols && (is_whitespace || is_text_char)) {
  2760. break;
  2761. }
  2762. column++;
  2763. }
  2764. next_line = line;
  2765. next_column = column;
  2766. #ifdef APPLE_STYLE_KEYS
  2767. } else if (k->get_command()) {
  2768. next_column = curline_len;
  2769. next_line = cursor.line;
  2770. #endif
  2771. } else {
  2772. next_column = cursor.column < curline_len ? (cursor.column + 1) : 0;
  2773. }
  2774. _remove_text(cursor.line, cursor.column, next_line, next_column);
  2775. update();
  2776. } break;
  2777. case KEY_KP_7: {
  2778. if (k->get_unicode() != 0) {
  2779. scancode_handled = false;
  2780. break;
  2781. }
  2782. FALLTHROUGH;
  2783. }
  2784. case KEY_HOME: {
  2785. #ifdef APPLE_STYLE_KEYS
  2786. if (k->get_shift())
  2787. _pre_shift_selection();
  2788. cursor_set_line(0);
  2789. if (k->get_shift())
  2790. _post_shift_selection();
  2791. else if (k->get_command() || k->get_control())
  2792. deselect();
  2793. #else
  2794. if (k->get_shift())
  2795. _pre_shift_selection();
  2796. if (k->get_command()) {
  2797. cursor_set_line(0);
  2798. cursor_set_column(0);
  2799. } else {
  2800. // Move cursor column to start of wrapped row and then to start of text.
  2801. Vector<String> rows = get_wrap_rows_text(cursor.line);
  2802. int wi = get_cursor_wrap_index();
  2803. int row_start_col = 0;
  2804. for (int i = 0; i < wi; i++) {
  2805. row_start_col += rows[i].length();
  2806. }
  2807. if (cursor.column == row_start_col || wi == 0) {
  2808. // Compute whitespace symbols seq length.
  2809. int current_line_whitespace_len = 0;
  2810. while (current_line_whitespace_len < text[cursor.line].length()) {
  2811. CharType c = text[cursor.line][current_line_whitespace_len];
  2812. if (c != '\t' && c != ' ')
  2813. break;
  2814. current_line_whitespace_len++;
  2815. }
  2816. if (cursor_get_column() == current_line_whitespace_len)
  2817. cursor_set_column(0);
  2818. else
  2819. cursor_set_column(current_line_whitespace_len);
  2820. } else {
  2821. cursor_set_column(row_start_col);
  2822. }
  2823. }
  2824. if (k->get_shift())
  2825. _post_shift_selection();
  2826. else if (k->get_command() || k->get_control())
  2827. deselect();
  2828. _cancel_completion();
  2829. completion_hint = "";
  2830. #endif
  2831. } break;
  2832. case KEY_KP_1: {
  2833. if (k->get_unicode() != 0) {
  2834. scancode_handled = false;
  2835. break;
  2836. }
  2837. FALLTHROUGH;
  2838. }
  2839. case KEY_END: {
  2840. #ifdef APPLE_STYLE_KEYS
  2841. if (k->get_shift())
  2842. _pre_shift_selection();
  2843. cursor_set_line(get_last_unhidden_line(), true, false, 9999);
  2844. if (k->get_shift())
  2845. _post_shift_selection();
  2846. else if (k->get_command() || k->get_control())
  2847. deselect();
  2848. #else
  2849. if (k->get_shift())
  2850. _pre_shift_selection();
  2851. if (k->get_command())
  2852. cursor_set_line(get_last_unhidden_line(), true, false, 9999);
  2853. // Move cursor column to end of wrapped row and then to end of text.
  2854. Vector<String> rows = get_wrap_rows_text(cursor.line);
  2855. int wi = get_cursor_wrap_index();
  2856. int row_end_col = -1;
  2857. for (int i = 0; i < wi + 1; i++) {
  2858. row_end_col += rows[i].length();
  2859. }
  2860. if (wi == rows.size() - 1 || cursor.column == row_end_col) {
  2861. cursor_set_column(text[cursor.line].length());
  2862. } else {
  2863. cursor_set_column(row_end_col);
  2864. }
  2865. if (k->get_shift())
  2866. _post_shift_selection();
  2867. else if (k->get_command() || k->get_control())
  2868. deselect();
  2869. _cancel_completion();
  2870. completion_hint = "";
  2871. #endif
  2872. } break;
  2873. case KEY_KP_9: {
  2874. if (k->get_unicode() != 0) {
  2875. scancode_handled = false;
  2876. break;
  2877. }
  2878. FALLTHROUGH;
  2879. }
  2880. case KEY_PAGEUP: {
  2881. if (k->get_shift())
  2882. _pre_shift_selection();
  2883. int wi;
  2884. int n_line = cursor.line - num_lines_from_rows(cursor.line, get_cursor_wrap_index(), -get_visible_rows(), wi) + 1;
  2885. cursor_set_line(n_line, true, false, wi);
  2886. if (k->get_shift())
  2887. _post_shift_selection();
  2888. _cancel_completion();
  2889. completion_hint = "";
  2890. } break;
  2891. case KEY_KP_3: {
  2892. if (k->get_unicode() != 0) {
  2893. scancode_handled = false;
  2894. break;
  2895. }
  2896. FALLTHROUGH;
  2897. }
  2898. case KEY_PAGEDOWN: {
  2899. if (k->get_shift())
  2900. _pre_shift_selection();
  2901. int wi;
  2902. int n_line = cursor.line + num_lines_from_rows(cursor.line, get_cursor_wrap_index(), get_visible_rows(), wi) - 1;
  2903. cursor_set_line(n_line, true, false, wi);
  2904. if (k->get_shift())
  2905. _post_shift_selection();
  2906. _cancel_completion();
  2907. completion_hint = "";
  2908. } break;
  2909. case KEY_A: {
  2910. #ifndef APPLE_STYLE_KEYS
  2911. if (!k->get_control() || k->get_shift() || k->get_alt()) {
  2912. scancode_handled = false;
  2913. break;
  2914. }
  2915. if (is_shortcut_keys_enabled()) {
  2916. select_all();
  2917. }
  2918. #else
  2919. if ((!k->get_command() && !k->get_control())) {
  2920. scancode_handled = false;
  2921. break;
  2922. }
  2923. if (!k->get_shift() && k->get_command() && is_shortcut_keys_enabled())
  2924. select_all();
  2925. else if (k->get_control()) {
  2926. if (k->get_shift())
  2927. _pre_shift_selection();
  2928. int current_line_whitespace_len = 0;
  2929. while (current_line_whitespace_len < text[cursor.line].length()) {
  2930. CharType c = text[cursor.line][current_line_whitespace_len];
  2931. if (c != '\t' && c != ' ')
  2932. break;
  2933. current_line_whitespace_len++;
  2934. }
  2935. if (cursor_get_column() == current_line_whitespace_len)
  2936. cursor_set_column(0);
  2937. else
  2938. cursor_set_column(current_line_whitespace_len);
  2939. if (k->get_shift())
  2940. _post_shift_selection();
  2941. else if (k->get_command() || k->get_control())
  2942. deselect();
  2943. }
  2944. } break;
  2945. case KEY_E: {
  2946. if (!k->get_control() || k->get_command() || k->get_alt()) {
  2947. scancode_handled = false;
  2948. break;
  2949. }
  2950. if (k->get_shift())
  2951. _pre_shift_selection();
  2952. if (k->get_command())
  2953. cursor_set_line(text.size() - 1, true, false);
  2954. cursor_set_column(text[cursor.line].length());
  2955. if (k->get_shift())
  2956. _post_shift_selection();
  2957. else if (k->get_command() || k->get_control())
  2958. deselect();
  2959. _cancel_completion();
  2960. completion_hint = "";
  2961. #endif
  2962. } break;
  2963. case KEY_X: {
  2964. if (readonly) {
  2965. break;
  2966. }
  2967. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  2968. scancode_handled = false;
  2969. break;
  2970. }
  2971. if (is_shortcut_keys_enabled()) {
  2972. cut();
  2973. }
  2974. } break;
  2975. case KEY_C: {
  2976. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  2977. scancode_handled = false;
  2978. break;
  2979. }
  2980. if (is_shortcut_keys_enabled()) {
  2981. copy();
  2982. }
  2983. } break;
  2984. case KEY_Z: {
  2985. if (readonly) {
  2986. break;
  2987. }
  2988. if (!k->get_command()) {
  2989. scancode_handled = false;
  2990. break;
  2991. }
  2992. if (is_shortcut_keys_enabled()) {
  2993. if (k->get_shift())
  2994. redo();
  2995. else
  2996. undo();
  2997. }
  2998. } break;
  2999. case KEY_Y: {
  3000. if (readonly) {
  3001. break;
  3002. }
  3003. if (!k->get_command()) {
  3004. scancode_handled = false;
  3005. break;
  3006. }
  3007. if (is_shortcut_keys_enabled()) {
  3008. redo();
  3009. }
  3010. } break;
  3011. case KEY_V: {
  3012. if (readonly) {
  3013. break;
  3014. }
  3015. if (!k->get_command() || k->get_shift() || k->get_alt()) {
  3016. scancode_handled = false;
  3017. break;
  3018. }
  3019. if (is_shortcut_keys_enabled()) {
  3020. paste();
  3021. }
  3022. } break;
  3023. case KEY_SPACE: {
  3024. #ifdef OSX_ENABLED
  3025. if (completion_enabled && k->get_metakey()) { // cmd-space is spotlight shortcut in OSX
  3026. #else
  3027. if (completion_enabled && k->get_command()) {
  3028. #endif
  3029. query_code_comple();
  3030. scancode_handled = true;
  3031. } else {
  3032. scancode_handled = false;
  3033. }
  3034. } break;
  3035. case KEY_MENU: {
  3036. if (context_menu_enabled) {
  3037. menu->set_position(get_global_transform().xform(_get_cursor_pixel_pos()));
  3038. menu->set_size(Vector2(1, 1));
  3039. menu->set_scale(get_global_transform().get_scale());
  3040. menu->popup();
  3041. menu->grab_focus();
  3042. }
  3043. } break;
  3044. default: {
  3045. scancode_handled = false;
  3046. } break;
  3047. }
  3048. if (scancode_handled)
  3049. accept_event();
  3050. if (k->get_scancode() == KEY_INSERT) {
  3051. set_insert_mode(!insert_mode);
  3052. accept_event();
  3053. return;
  3054. }
  3055. if (!scancode_handled && !k->get_command()) { // For German keyboards.
  3056. if (k->get_unicode() >= 32) {
  3057. if (readonly)
  3058. return;
  3059. // Remove the old character if in insert mode and no selection.
  3060. if (insert_mode && !had_selection) {
  3061. begin_complex_operation();
  3062. // Make sure we don't try and remove empty space.
  3063. if (cursor.column < get_line(cursor.line).length()) {
  3064. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  3065. }
  3066. }
  3067. const CharType chr[2] = { (CharType)k->get_unicode(), 0 };
  3068. if (completion_hint != "" && k->get_unicode() == ')') {
  3069. completion_hint = "";
  3070. }
  3071. if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
  3072. _consume_pair_symbol(chr[0]);
  3073. } else {
  3074. _insert_text_at_cursor(chr);
  3075. }
  3076. if (insert_mode && !had_selection) {
  3077. end_complex_operation();
  3078. }
  3079. if (selection.active != had_selection) {
  3080. end_complex_operation();
  3081. }
  3082. accept_event();
  3083. }
  3084. }
  3085. return;
  3086. }
  3087. }
  3088. void TextEdit::_scroll_up(real_t p_delta) {
  3089. if (scrolling && smooth_scroll_enabled && SGN(target_v_scroll - v_scroll->get_value()) != SGN(-p_delta)) {
  3090. scrolling = false;
  3091. minimap_clicked = false;
  3092. }
  3093. if (scrolling) {
  3094. target_v_scroll = (target_v_scroll - p_delta);
  3095. } else {
  3096. target_v_scroll = (get_v_scroll() - p_delta);
  3097. }
  3098. if (smooth_scroll_enabled) {
  3099. if (target_v_scroll <= 0) {
  3100. target_v_scroll = 0;
  3101. }
  3102. if (Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  3103. v_scroll->set_value(target_v_scroll);
  3104. } else {
  3105. scrolling = true;
  3106. set_physics_process_internal(true);
  3107. }
  3108. } else {
  3109. set_v_scroll(target_v_scroll);
  3110. }
  3111. }
  3112. void TextEdit::_scroll_down(real_t p_delta) {
  3113. if (scrolling && smooth_scroll_enabled && SGN(target_v_scroll - v_scroll->get_value()) != SGN(p_delta)) {
  3114. scrolling = false;
  3115. minimap_clicked = false;
  3116. }
  3117. if (scrolling) {
  3118. target_v_scroll = (target_v_scroll + p_delta);
  3119. } else {
  3120. target_v_scroll = (get_v_scroll() + p_delta);
  3121. }
  3122. if (smooth_scroll_enabled) {
  3123. int max_v_scroll = round(v_scroll->get_max() - v_scroll->get_page());
  3124. if (target_v_scroll > max_v_scroll) {
  3125. target_v_scroll = max_v_scroll;
  3126. }
  3127. if (Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
  3128. v_scroll->set_value(target_v_scroll);
  3129. } else {
  3130. scrolling = true;
  3131. set_physics_process_internal(true);
  3132. }
  3133. } else {
  3134. set_v_scroll(target_v_scroll);
  3135. }
  3136. }
  3137. void TextEdit::_pre_shift_selection() {
  3138. if (!selection.active || selection.selecting_mode == Selection::MODE_NONE) {
  3139. selection.selecting_line = cursor.line;
  3140. selection.selecting_column = cursor.column;
  3141. selection.active = true;
  3142. }
  3143. selection.selecting_mode = Selection::MODE_SHIFT;
  3144. }
  3145. void TextEdit::_post_shift_selection() {
  3146. if (selection.active && selection.selecting_mode == Selection::MODE_SHIFT) {
  3147. select(selection.selecting_line, selection.selecting_column, cursor.line, cursor.column);
  3148. update();
  3149. }
  3150. selection.selecting_text = true;
  3151. }
  3152. void TextEdit::_scroll_lines_up() {
  3153. scrolling = false;
  3154. minimap_clicked = false;
  3155. // Adjust the vertical scroll.
  3156. set_v_scroll(get_v_scroll() - 1);
  3157. // Adjust the cursor to viewport.
  3158. if (!selection.active) {
  3159. int cur_line = cursor.line;
  3160. int cur_wrap = get_cursor_wrap_index();
  3161. int last_vis_line = get_last_visible_line();
  3162. int last_vis_wrap = get_last_visible_line_wrap_index();
  3163. if (cur_line > last_vis_line || (cur_line == last_vis_line && cur_wrap > last_vis_wrap)) {
  3164. cursor_set_line(last_vis_line, false, false, last_vis_wrap);
  3165. }
  3166. }
  3167. }
  3168. void TextEdit::_scroll_lines_down() {
  3169. scrolling = false;
  3170. minimap_clicked = false;
  3171. // Adjust the vertical scroll.
  3172. set_v_scroll(get_v_scroll() + 1);
  3173. // Adjust the cursor to viewport.
  3174. if (!selection.active) {
  3175. int cur_line = cursor.line;
  3176. int cur_wrap = get_cursor_wrap_index();
  3177. int first_vis_line = get_first_visible_line();
  3178. int first_vis_wrap = cursor.wrap_ofs;
  3179. if (cur_line < first_vis_line || (cur_line == first_vis_line && cur_wrap < first_vis_wrap)) {
  3180. cursor_set_line(first_vis_line, false, false, first_vis_wrap);
  3181. }
  3182. }
  3183. }
  3184. /**** TEXT EDIT CORE API ****/
  3185. void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) {
  3186. // Save for undo.
  3187. ERR_FAIL_INDEX(p_line, text.size());
  3188. ERR_FAIL_COND(p_char < 0);
  3189. /* STEP 1: Remove \r from source text and separate in substrings. */
  3190. Vector<String> substrings = p_text.replace("\r", "").split("\n");
  3191. /* STEP 2: Fire breakpoint_toggled signals. */
  3192. // Is this just a new empty line?
  3193. bool shift_first_line = p_char == 0 && p_text.replace("\r", "") == "\n";
  3194. int i = p_line + !shift_first_line;
  3195. int lines = substrings.size() - 1;
  3196. for (; i < text.size(); i++) {
  3197. if (text.is_breakpoint(i)) {
  3198. if ((i - lines < p_line || !text.is_breakpoint(i - lines)) || (i - lines == p_line && !shift_first_line))
  3199. emit_signal("breakpoint_toggled", i);
  3200. if (i + lines >= text.size() || !text.is_breakpoint(i + lines))
  3201. emit_signal("breakpoint_toggled", i + lines);
  3202. }
  3203. }
  3204. /* STEP 3: Add spaces if the char is greater than the end of the line. */
  3205. while (p_char > text[p_line].length()) {
  3206. text.set(p_line, text[p_line] + String::chr(' '));
  3207. }
  3208. /* STEP 4: Separate dest string in pre and post text. */
  3209. String preinsert_text = text[p_line].substr(0, p_char);
  3210. String postinsert_text = text[p_line].substr(p_char, text[p_line].size());
  3211. for (int j = 0; j < substrings.size(); j++) {
  3212. // Insert the substrings.
  3213. if (j == 0) {
  3214. text.set(p_line, preinsert_text + substrings[j]);
  3215. } else {
  3216. text.insert(p_line + j, substrings[j]);
  3217. }
  3218. if (j == substrings.size() - 1) {
  3219. text.set(p_line + j, text[p_line + j] + postinsert_text);
  3220. }
  3221. }
  3222. if (shift_first_line) {
  3223. text.set_breakpoint(p_line + 1, text.is_breakpoint(p_line));
  3224. text.set_hidden(p_line + 1, text.is_hidden(p_line));
  3225. if (text.has_info_icon(p_line)) {
  3226. text.set_info_icon(p_line + 1, text.get_info_icon(p_line), text.get_info(p_line));
  3227. }
  3228. text.set_breakpoint(p_line, false);
  3229. text.set_hidden(p_line, false);
  3230. text.set_info_icon(p_line, NULL, "");
  3231. }
  3232. text.set_line_wrap_amount(p_line, -1);
  3233. r_end_line = p_line + substrings.size() - 1;
  3234. r_end_column = text[r_end_line].length() - postinsert_text.length();
  3235. if (!text_changed_dirty && !setting_text) {
  3236. if (is_inside_tree())
  3237. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  3238. text_changed_dirty = true;
  3239. }
  3240. _line_edited_from(p_line);
  3241. }
  3242. String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const {
  3243. ERR_FAIL_INDEX_V(p_from_line, text.size(), String());
  3244. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String());
  3245. ERR_FAIL_INDEX_V(p_to_line, text.size(), String());
  3246. ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String());
  3247. ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // 'from > to'.
  3248. ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // 'from > to'.
  3249. String ret;
  3250. for (int i = p_from_line; i <= p_to_line; i++) {
  3251. int begin = (i == p_from_line) ? p_from_column : 0;
  3252. int end = (i == p_to_line) ? p_to_column : text[i].length();
  3253. if (i > p_from_line)
  3254. ret += "\n";
  3255. ret += text[i].substr(begin, end - begin);
  3256. }
  3257. return ret;
  3258. }
  3259. void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  3260. ERR_FAIL_INDEX(p_from_line, text.size());
  3261. ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
  3262. ERR_FAIL_INDEX(p_to_line, text.size());
  3263. ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
  3264. ERR_FAIL_COND(p_to_line < p_from_line); // 'from > to'.
  3265. ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // 'from > to'.
  3266. String pre_text = text[p_from_line].substr(0, p_from_column);
  3267. String post_text = text[p_to_line].substr(p_to_column, text[p_to_line].length());
  3268. int lines = p_to_line - p_from_line;
  3269. for (int i = p_from_line + 1; i < text.size(); i++) {
  3270. if (text.is_breakpoint(i)) {
  3271. if (i + lines >= text.size() || !text.is_breakpoint(i + lines))
  3272. emit_signal("breakpoint_toggled", i);
  3273. if (i > p_to_line && (i - lines < 0 || !text.is_breakpoint(i - lines)))
  3274. emit_signal("breakpoint_toggled", i - lines);
  3275. }
  3276. }
  3277. for (int i = p_from_line; i < p_to_line; i++) {
  3278. text.remove(p_from_line + 1);
  3279. }
  3280. text.set(p_from_line, pre_text + post_text);
  3281. text.set_line_wrap_amount(p_from_line, -1);
  3282. if (!text_changed_dirty && !setting_text) {
  3283. if (is_inside_tree())
  3284. MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
  3285. text_changed_dirty = true;
  3286. }
  3287. _line_edited_from(p_from_line);
  3288. }
  3289. void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_char) {
  3290. if (!setting_text && idle_detect->is_inside_tree())
  3291. idle_detect->start();
  3292. if (undo_enabled) {
  3293. _clear_redo();
  3294. }
  3295. int retline, retchar;
  3296. _base_insert_text(p_line, p_char, p_text, retline, retchar);
  3297. if (r_end_line)
  3298. *r_end_line = retline;
  3299. if (r_end_char)
  3300. *r_end_char = retchar;
  3301. if (!undo_enabled)
  3302. return;
  3303. /* UNDO!! */
  3304. TextOperation op;
  3305. op.type = TextOperation::TYPE_INSERT;
  3306. op.from_line = p_line;
  3307. op.from_column = p_char;
  3308. op.to_line = retline;
  3309. op.to_column = retchar;
  3310. op.text = p_text;
  3311. op.version = ++version;
  3312. op.chain_forward = false;
  3313. op.chain_backward = false;
  3314. // See if it should just be set as current op.
  3315. if (current_op.type != op.type) {
  3316. op.prev_version = get_version();
  3317. _push_current_op();
  3318. current_op = op;
  3319. return; // Set as current op, return.
  3320. }
  3321. // See if it can be merged.
  3322. if (current_op.to_line != p_line || current_op.to_column != p_char) {
  3323. op.prev_version = get_version();
  3324. _push_current_op();
  3325. current_op = op;
  3326. return; // Set as current op, return.
  3327. }
  3328. // Merge current op.
  3329. current_op.text += p_text;
  3330. current_op.to_column = retchar;
  3331. current_op.to_line = retline;
  3332. current_op.version = op.version;
  3333. }
  3334. void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  3335. if (!setting_text && idle_detect->is_inside_tree())
  3336. idle_detect->start();
  3337. String text;
  3338. if (undo_enabled) {
  3339. _clear_redo();
  3340. text = _base_get_text(p_from_line, p_from_column, p_to_line, p_to_column);
  3341. }
  3342. _base_remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
  3343. if (!undo_enabled)
  3344. return;
  3345. /* UNDO! */
  3346. TextOperation op;
  3347. op.type = TextOperation::TYPE_REMOVE;
  3348. op.from_line = p_from_line;
  3349. op.from_column = p_from_column;
  3350. op.to_line = p_to_line;
  3351. op.to_column = p_to_column;
  3352. op.text = text;
  3353. op.version = ++version;
  3354. op.chain_forward = false;
  3355. op.chain_backward = false;
  3356. // See if it should just be set as current op.
  3357. if (current_op.type != op.type) {
  3358. op.prev_version = get_version();
  3359. _push_current_op();
  3360. current_op = op;
  3361. return; // Set as current op, return.
  3362. }
  3363. // See if it can be merged.
  3364. if (current_op.from_line == p_to_line && current_op.from_column == p_to_column) {
  3365. // Backspace or similar.
  3366. current_op.text = text + current_op.text;
  3367. current_op.from_line = p_from_line;
  3368. current_op.from_column = p_from_column;
  3369. return; // Update current op.
  3370. }
  3371. op.prev_version = get_version();
  3372. _push_current_op();
  3373. current_op = op;
  3374. }
  3375. void TextEdit::_insert_text_at_cursor(const String &p_text) {
  3376. int new_column, new_line;
  3377. _insert_text(cursor.line, cursor.column, p_text, &new_line, &new_column);
  3378. _update_scrollbars();
  3379. cursor_set_line(new_line);
  3380. cursor_set_column(new_column);
  3381. update();
  3382. }
  3383. void TextEdit::_line_edited_from(int p_line) {
  3384. int cache_size = color_region_cache.size();
  3385. for (int i = p_line; i < cache_size; i++) {
  3386. color_region_cache.erase(i);
  3387. }
  3388. if (syntax_highlighting_cache.size() > 0) {
  3389. cache_size = syntax_highlighting_cache.back()->key();
  3390. for (int i = p_line - 1; i <= cache_size; i++) {
  3391. if (syntax_highlighting_cache.has(i)) {
  3392. syntax_highlighting_cache.erase(i);
  3393. }
  3394. }
  3395. }
  3396. }
  3397. int TextEdit::get_char_count() {
  3398. int totalsize = 0;
  3399. for (int i = 0; i < text.size(); i++) {
  3400. if (i > 0)
  3401. totalsize++; // Include \n.
  3402. totalsize += text[i].length();
  3403. }
  3404. return totalsize; // Omit last \n.
  3405. }
  3406. Size2 TextEdit::get_minimum_size() const {
  3407. return cache.style_normal->get_minimum_size();
  3408. }
  3409. int TextEdit::_get_control_height() const {
  3410. int control_height = get_size().height;
  3411. control_height -= cache.style_normal->get_minimum_size().height;
  3412. if (h_scroll->is_visible_in_tree()) {
  3413. control_height -= h_scroll->get_size().height;
  3414. }
  3415. return control_height;
  3416. }
  3417. void TextEdit::_generate_context_menu() {
  3418. // Reorganize context menu.
  3419. menu->clear();
  3420. if (!readonly)
  3421. menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_X : 0);
  3422. menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_C : 0);
  3423. if (!readonly)
  3424. menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_V : 0);
  3425. menu->add_separator();
  3426. if (is_selecting_enabled())
  3427. menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_A : 0);
  3428. if (!readonly) {
  3429. menu->add_item(RTR("Clear"), MENU_CLEAR);
  3430. menu->add_separator();
  3431. menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_Z : 0);
  3432. menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z : 0);
  3433. }
  3434. }
  3435. int TextEdit::get_visible_rows() const {
  3436. return _get_control_height() / get_row_height();
  3437. }
  3438. int TextEdit::_get_minimap_visible_rows() const {
  3439. return _get_control_height() / (minimap_char_size.y + minimap_line_spacing);
  3440. }
  3441. int TextEdit::get_total_visible_rows() const {
  3442. // Returns the total amount of rows we need in the editor.
  3443. // This skips hidden lines and counts each wrapping of a line.
  3444. if (!is_hiding_enabled() && !is_wrap_enabled())
  3445. return text.size();
  3446. int total_rows = 0;
  3447. for (int i = 0; i < text.size(); i++) {
  3448. if (!text.is_hidden(i)) {
  3449. total_rows++;
  3450. total_rows += times_line_wraps(i);
  3451. }
  3452. }
  3453. return total_rows;
  3454. }
  3455. void TextEdit::_update_wrap_at() {
  3456. 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;
  3457. update_cursor_wrap_offset();
  3458. text.clear_wrap_cache();
  3459. for (int i = 0; i < text.size(); i++) {
  3460. // Update all values that wrap.
  3461. if (!line_wraps(i))
  3462. continue;
  3463. Vector<String> rows = get_wrap_rows_text(i);
  3464. text.set_line_wrap_amount(i, rows.size() - 1);
  3465. }
  3466. }
  3467. void TextEdit::adjust_viewport_to_cursor() {
  3468. // Make sure cursor is visible on the screen.
  3469. scrolling = false;
  3470. minimap_clicked = false;
  3471. int cur_line = cursor.line;
  3472. int cur_wrap = get_cursor_wrap_index();
  3473. int first_vis_line = get_first_visible_line();
  3474. int first_vis_wrap = cursor.wrap_ofs;
  3475. int last_vis_line = get_last_visible_line();
  3476. int last_vis_wrap = get_last_visible_line_wrap_index();
  3477. if (cur_line < first_vis_line || (cur_line == first_vis_line && cur_wrap < first_vis_wrap)) {
  3478. // Cursor is above screen.
  3479. set_line_as_first_visible(cur_line, cur_wrap);
  3480. } else if (cur_line > last_vis_line || (cur_line == last_vis_line && cur_wrap > last_vis_wrap)) {
  3481. // Cursor is below screen.
  3482. set_line_as_last_visible(cur_line, cur_wrap);
  3483. }
  3484. 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;
  3485. if (v_scroll->is_visible_in_tree())
  3486. visible_width -= v_scroll->get_combined_minimum_size().width;
  3487. visible_width -= 20; // Give it a little more space.
  3488. if (!is_wrap_enabled()) {
  3489. // Adjust x offset.
  3490. int cursor_x = get_column_x_offset(cursor.column, text[cursor.line]);
  3491. if (cursor_x > (cursor.x_ofs + visible_width))
  3492. cursor.x_ofs = cursor_x - visible_width + 1;
  3493. if (cursor_x < cursor.x_ofs)
  3494. cursor.x_ofs = cursor_x;
  3495. } else {
  3496. cursor.x_ofs = 0;
  3497. }
  3498. h_scroll->set_value(cursor.x_ofs);
  3499. update();
  3500. }
  3501. void TextEdit::center_viewport_to_cursor() {
  3502. // Move viewport so the cursor is in the center of the screen.
  3503. scrolling = false;
  3504. minimap_clicked = false;
  3505. if (is_line_hidden(cursor.line))
  3506. unfold_line(cursor.line);
  3507. set_line_as_center_visible(cursor.line, get_cursor_wrap_index());
  3508. 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;
  3509. if (v_scroll->is_visible_in_tree())
  3510. visible_width -= v_scroll->get_combined_minimum_size().width;
  3511. visible_width -= 20; // Give it a little more space.
  3512. if (is_wrap_enabled()) {
  3513. // Center x offset.
  3514. int cursor_x = get_column_x_offset_for_line(cursor.column, cursor.line);
  3515. if (cursor_x > (cursor.x_ofs + visible_width))
  3516. cursor.x_ofs = cursor_x - visible_width + 1;
  3517. if (cursor_x < cursor.x_ofs)
  3518. cursor.x_ofs = cursor_x;
  3519. } else {
  3520. cursor.x_ofs = 0;
  3521. }
  3522. h_scroll->set_value(cursor.x_ofs);
  3523. update();
  3524. }
  3525. void TextEdit::update_cursor_wrap_offset() {
  3526. int first_vis_line = get_first_visible_line();
  3527. if (line_wraps(first_vis_line)) {
  3528. cursor.wrap_ofs = MIN(cursor.wrap_ofs, times_line_wraps(first_vis_line));
  3529. } else {
  3530. cursor.wrap_ofs = 0;
  3531. }
  3532. set_line_as_first_visible(cursor.line_ofs, cursor.wrap_ofs);
  3533. }
  3534. bool TextEdit::line_wraps(int line) const {
  3535. ERR_FAIL_INDEX_V(line, text.size(), 0);
  3536. if (!is_wrap_enabled())
  3537. return false;
  3538. return text.get_line_width(line) > wrap_at;
  3539. }
  3540. int TextEdit::times_line_wraps(int line) const {
  3541. ERR_FAIL_INDEX_V(line, text.size(), 0);
  3542. if (!line_wraps(line))
  3543. return 0;
  3544. int wrap_amount = text.get_line_wrap_amount(line);
  3545. if (wrap_amount == -1) {
  3546. // Update the value.
  3547. Vector<String> rows = get_wrap_rows_text(line);
  3548. wrap_amount = rows.size() - 1;
  3549. text.set_line_wrap_amount(line, wrap_amount);
  3550. }
  3551. return wrap_amount;
  3552. }
  3553. Vector<String> TextEdit::get_wrap_rows_text(int p_line) const {
  3554. ERR_FAIL_INDEX_V(p_line, text.size(), Vector<String>());
  3555. Vector<String> lines;
  3556. if (!line_wraps(p_line)) {
  3557. lines.push_back(text[p_line]);
  3558. return lines;
  3559. }
  3560. int px = 0;
  3561. int col = 0;
  3562. String line_text = text[p_line];
  3563. String wrap_substring = "";
  3564. int word_px = 0;
  3565. String word_str = "";
  3566. int cur_wrap_index = 0;
  3567. int tab_offset_px = get_indent_level(p_line) * cache.font->get_char_size(' ').width;
  3568. if (tab_offset_px >= wrap_at) {
  3569. tab_offset_px = 0;
  3570. }
  3571. while (col < line_text.length()) {
  3572. CharType c = line_text[col];
  3573. int w = text.get_char_width(c, line_text[col + 1], px + word_px);
  3574. int indent_ofs = (cur_wrap_index != 0 ? tab_offset_px : 0);
  3575. if (indent_ofs + word_px + w > wrap_at) {
  3576. // Not enough space to add this char; start next line.
  3577. wrap_substring += word_str;
  3578. lines.push_back(wrap_substring);
  3579. cur_wrap_index++;
  3580. wrap_substring = "";
  3581. px = 0;
  3582. word_str = "";
  3583. word_str += c;
  3584. word_px = w;
  3585. } else {
  3586. word_str += c;
  3587. word_px += w;
  3588. if (c == ' ') {
  3589. // End of a word; add this word to the substring.
  3590. wrap_substring += word_str;
  3591. px += word_px;
  3592. word_str = "";
  3593. word_px = 0;
  3594. }
  3595. if (indent_ofs + px + word_px > wrap_at) {
  3596. // This word will be moved to the next line.
  3597. lines.push_back(wrap_substring);
  3598. // Reset for next wrap.
  3599. cur_wrap_index++;
  3600. wrap_substring = "";
  3601. px = 0;
  3602. }
  3603. }
  3604. col++;
  3605. }
  3606. // Line ends before hit wrap_at; add this word to the substring.
  3607. wrap_substring += word_str;
  3608. lines.push_back(wrap_substring);
  3609. // Update cache.
  3610. text.set_line_wrap_amount(p_line, lines.size() - 1);
  3611. return lines;
  3612. }
  3613. int TextEdit::get_cursor_wrap_index() const {
  3614. return get_line_wrap_index_at_col(cursor.line, cursor.column);
  3615. }
  3616. int TextEdit::get_line_wrap_index_at_col(int p_line, int p_column) const {
  3617. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3618. if (!line_wraps(p_line))
  3619. return 0;
  3620. // Loop through wraps in the line text until we get to the column.
  3621. int wrap_index = 0;
  3622. int col = 0;
  3623. Vector<String> rows = get_wrap_rows_text(p_line);
  3624. for (int i = 0; i < rows.size(); i++) {
  3625. wrap_index = i;
  3626. String s = rows[wrap_index];
  3627. col += s.length();
  3628. if (col > p_column)
  3629. break;
  3630. }
  3631. return wrap_index;
  3632. }
  3633. void TextEdit::cursor_set_column(int p_col, bool p_adjust_viewport) {
  3634. if (p_col < 0)
  3635. p_col = 0;
  3636. cursor.column = p_col;
  3637. if (cursor.column > get_line(cursor.line).length())
  3638. cursor.column = get_line(cursor.line).length();
  3639. cursor.last_fit_x = get_column_x_offset_for_line(cursor.column, cursor.line);
  3640. if (p_adjust_viewport)
  3641. adjust_viewport_to_cursor();
  3642. if (!cursor_changed_dirty) {
  3643. if (is_inside_tree())
  3644. MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
  3645. cursor_changed_dirty = true;
  3646. }
  3647. }
  3648. void TextEdit::cursor_set_line(int p_row, bool p_adjust_viewport, bool p_can_be_hidden, int p_wrap_index) {
  3649. if (setting_row)
  3650. return;
  3651. setting_row = true;
  3652. if (p_row < 0)
  3653. p_row = 0;
  3654. if (p_row >= text.size())
  3655. p_row = text.size() - 1;
  3656. if (!p_can_be_hidden) {
  3657. if (is_line_hidden(CLAMP(p_row, 0, text.size() - 1))) {
  3658. int move_down = num_lines_from(p_row, 1) - 1;
  3659. if (p_row + move_down <= text.size() - 1 && !is_line_hidden(p_row + move_down)) {
  3660. p_row += move_down;
  3661. } else {
  3662. int move_up = num_lines_from(p_row, -1) - 1;
  3663. if (p_row - move_up > 0 && !is_line_hidden(p_row - move_up)) {
  3664. p_row -= move_up;
  3665. } else {
  3666. WARN_PRINT(("Cursor set to hidden line " + itos(p_row) + " and there are no nonhidden lines."));
  3667. }
  3668. }
  3669. }
  3670. }
  3671. cursor.line = p_row;
  3672. int n_col = get_char_pos_for_line(cursor.last_fit_x, p_row, p_wrap_index);
  3673. if (n_col != 0 && is_wrap_enabled() && p_wrap_index < times_line_wraps(p_row)) {
  3674. Vector<String> rows = get_wrap_rows_text(p_row);
  3675. int row_end_col = 0;
  3676. for (int i = 0; i < p_wrap_index + 1; i++) {
  3677. row_end_col += rows[i].length();
  3678. }
  3679. if (n_col >= row_end_col)
  3680. n_col -= 1;
  3681. }
  3682. cursor.column = n_col;
  3683. if (p_adjust_viewport)
  3684. adjust_viewport_to_cursor();
  3685. setting_row = false;
  3686. if (!cursor_changed_dirty) {
  3687. if (is_inside_tree())
  3688. MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
  3689. cursor_changed_dirty = true;
  3690. }
  3691. }
  3692. int TextEdit::cursor_get_column() const {
  3693. return cursor.column;
  3694. }
  3695. int TextEdit::cursor_get_line() const {
  3696. return cursor.line;
  3697. }
  3698. bool TextEdit::cursor_get_blink_enabled() const {
  3699. return caret_blink_enabled;
  3700. }
  3701. void TextEdit::cursor_set_blink_enabled(const bool p_enabled) {
  3702. caret_blink_enabled = p_enabled;
  3703. if (has_focus()) {
  3704. if (p_enabled) {
  3705. caret_blink_timer->start();
  3706. } else {
  3707. caret_blink_timer->stop();
  3708. }
  3709. }
  3710. draw_caret = true;
  3711. }
  3712. float TextEdit::cursor_get_blink_speed() const {
  3713. return caret_blink_timer->get_wait_time();
  3714. }
  3715. void TextEdit::cursor_set_blink_speed(const float p_speed) {
  3716. ERR_FAIL_COND(p_speed <= 0);
  3717. caret_blink_timer->set_wait_time(p_speed);
  3718. }
  3719. void TextEdit::cursor_set_block_mode(const bool p_enable) {
  3720. block_caret = p_enable;
  3721. update();
  3722. }
  3723. bool TextEdit::cursor_is_block_mode() const {
  3724. return block_caret;
  3725. }
  3726. void TextEdit::set_right_click_moves_caret(bool p_enable) {
  3727. right_click_moves_caret = p_enable;
  3728. }
  3729. bool TextEdit::is_right_click_moving_caret() const {
  3730. return right_click_moves_caret;
  3731. }
  3732. void TextEdit::_v_scroll_input() {
  3733. scrolling = false;
  3734. minimap_clicked = false;
  3735. }
  3736. void TextEdit::_scroll_moved(double p_to_val) {
  3737. if (updating_scrolls)
  3738. return;
  3739. if (h_scroll->is_visible_in_tree())
  3740. cursor.x_ofs = h_scroll->get_value();
  3741. if (v_scroll->is_visible_in_tree()) {
  3742. // Set line ofs and wrap ofs.
  3743. int v_scroll_i = floor(get_v_scroll());
  3744. int sc = 0;
  3745. int n_line;
  3746. for (n_line = 0; n_line < text.size(); n_line++) {
  3747. if (!is_line_hidden(n_line)) {
  3748. sc++;
  3749. sc += times_line_wraps(n_line);
  3750. if (sc > v_scroll_i)
  3751. break;
  3752. }
  3753. }
  3754. n_line = MIN(n_line, text.size() - 1);
  3755. int line_wrap_amount = times_line_wraps(n_line);
  3756. int wi = line_wrap_amount - (sc - v_scroll_i - 1);
  3757. wi = CLAMP(wi, 0, line_wrap_amount);
  3758. cursor.line_ofs = n_line;
  3759. cursor.wrap_ofs = wi;
  3760. }
  3761. update();
  3762. }
  3763. int TextEdit::get_row_height() const {
  3764. return cache.font->get_height() + cache.line_spacing;
  3765. }
  3766. int TextEdit::get_char_pos_for_line(int p_px, int p_line, int p_wrap_index) const {
  3767. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3768. if (line_wraps(p_line)) {
  3769. int line_wrap_amount = times_line_wraps(p_line);
  3770. int wrap_offset_px = get_indent_level(p_line) * cache.font->get_char_size(' ').width;
  3771. if (wrap_offset_px >= wrap_at) {
  3772. wrap_offset_px = 0;
  3773. }
  3774. if (p_wrap_index > line_wrap_amount)
  3775. p_wrap_index = line_wrap_amount;
  3776. if (p_wrap_index > 0)
  3777. p_px -= wrap_offset_px;
  3778. else
  3779. p_wrap_index = 0;
  3780. Vector<String> rows = get_wrap_rows_text(p_line);
  3781. int c_pos = get_char_pos_for(p_px, rows[p_wrap_index]);
  3782. for (int i = 0; i < p_wrap_index; i++) {
  3783. String s = rows[i];
  3784. c_pos += s.length();
  3785. }
  3786. return c_pos;
  3787. } else {
  3788. return get_char_pos_for(p_px, text[p_line]);
  3789. }
  3790. }
  3791. int TextEdit::get_column_x_offset_for_line(int p_char, int p_line) const {
  3792. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  3793. if (line_wraps(p_line)) {
  3794. int n_char = p_char;
  3795. int col = 0;
  3796. Vector<String> rows = get_wrap_rows_text(p_line);
  3797. int wrap_index = 0;
  3798. for (int i = 0; i < rows.size(); i++) {
  3799. wrap_index = i;
  3800. String s = rows[wrap_index];
  3801. col += s.length();
  3802. if (col > p_char)
  3803. break;
  3804. n_char -= s.length();
  3805. }
  3806. int px = get_column_x_offset(n_char, rows[wrap_index]);
  3807. int wrap_offset_px = get_indent_level(p_line) * cache.font->get_char_size(' ').width;
  3808. if (wrap_offset_px >= wrap_at) {
  3809. wrap_offset_px = 0;
  3810. }
  3811. if (wrap_index != 0)
  3812. px += wrap_offset_px;
  3813. return px;
  3814. } else {
  3815. return get_column_x_offset(p_char, text[p_line]);
  3816. }
  3817. }
  3818. int TextEdit::get_char_pos_for(int p_px, String p_str) const {
  3819. int px = 0;
  3820. int c = 0;
  3821. while (c < p_str.length()) {
  3822. int w = text.get_char_width(p_str[c], p_str[c + 1], px);
  3823. if (p_px < (px + w / 2))
  3824. break;
  3825. px += w;
  3826. c++;
  3827. }
  3828. return c;
  3829. }
  3830. int TextEdit::get_column_x_offset(int p_char, String p_str) const {
  3831. int px = 0;
  3832. for (int i = 0; i < p_char; i++) {
  3833. if (i >= p_str.length())
  3834. break;
  3835. px += text.get_char_width(p_str[i], p_str[i + 1], px);
  3836. }
  3837. return px;
  3838. }
  3839. void TextEdit::insert_text_at_cursor(const String &p_text) {
  3840. if (selection.active) {
  3841. cursor_set_line(selection.from_line);
  3842. cursor_set_column(selection.from_column);
  3843. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  3844. selection.active = false;
  3845. selection.selecting_mode = Selection::MODE_NONE;
  3846. }
  3847. _insert_text_at_cursor(p_text);
  3848. update();
  3849. }
  3850. Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
  3851. if (highlighted_word != String())
  3852. return CURSOR_POINTING_HAND;
  3853. 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;
  3854. if ((completion_active && completion_rect.has_point(p_pos))) {
  3855. return CURSOR_ARROW;
  3856. }
  3857. if (p_pos.x < gutter) {
  3858. int row, col;
  3859. _get_mouse_pos(p_pos, row, col);
  3860. int left_margin = cache.style_normal->get_margin(MARGIN_LEFT);
  3861. // Breakpoint icon.
  3862. if (draw_breakpoint_gutter && p_pos.x > left_margin - 6 && p_pos.x <= left_margin + cache.breakpoint_gutter_width - 3) {
  3863. return CURSOR_POINTING_HAND;
  3864. }
  3865. // Info icons.
  3866. int gutter_left = left_margin + cache.breakpoint_gutter_width + cache.info_gutter_width;
  3867. if (draw_info_gutter && p_pos.x > left_margin + cache.breakpoint_gutter_width - 6 && p_pos.x <= gutter_left - 3) {
  3868. if (text.has_info_icon(row)) {
  3869. return CURSOR_POINTING_HAND;
  3870. }
  3871. return CURSOR_ARROW;
  3872. }
  3873. // Fold icon.
  3874. 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) {
  3875. if (is_folded(row) || can_fold(row))
  3876. return CURSOR_POINTING_HAND;
  3877. else
  3878. return CURSOR_ARROW;
  3879. }
  3880. return CURSOR_ARROW;
  3881. } else {
  3882. int xmargin_end = get_size().width - cache.style_normal->get_margin(MARGIN_RIGHT);
  3883. if (draw_minimap && p_pos.x > xmargin_end - minimap_width && p_pos.x <= xmargin_end) {
  3884. return CURSOR_ARROW;
  3885. }
  3886. int row, col;
  3887. _get_mouse_pos(p_pos, row, col);
  3888. // EOL fold icon.
  3889. if (is_folded(row)) {
  3890. int line_width = text.get_line_width(row);
  3891. 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;
  3892. if (p_pos.x > line_width - 3 && p_pos.x <= line_width + cache.folded_eol_icon->get_width() + 3) {
  3893. return CURSOR_POINTING_HAND;
  3894. }
  3895. }
  3896. }
  3897. return get_default_cursor_shape();
  3898. }
  3899. void TextEdit::set_text(String p_text) {
  3900. setting_text = true;
  3901. if (!undo_enabled) {
  3902. _clear();
  3903. _insert_text_at_cursor(p_text);
  3904. }
  3905. if (undo_enabled) {
  3906. cursor_set_line(0);
  3907. cursor_set_column(0);
  3908. begin_complex_operation();
  3909. _remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0));
  3910. _insert_text_at_cursor(p_text);
  3911. end_complex_operation();
  3912. selection.active = false;
  3913. }
  3914. cursor_set_line(0);
  3915. cursor_set_column(0);
  3916. update();
  3917. setting_text = false;
  3918. };
  3919. String TextEdit::get_text() {
  3920. String longthing;
  3921. int len = text.size();
  3922. for (int i = 0; i < len; i++) {
  3923. longthing += text[i];
  3924. if (i != len - 1)
  3925. longthing += "\n";
  3926. }
  3927. return longthing;
  3928. };
  3929. String TextEdit::get_text_for_lookup_completion() {
  3930. int row, col;
  3931. _get_mouse_pos(get_local_mouse_position(), row, col);
  3932. String longthing;
  3933. int len = text.size();
  3934. for (int i = 0; i < len; i++) {
  3935. if (i == row) {
  3936. longthing += text[i].substr(0, col);
  3937. longthing += String::chr(0xFFFF); // Not unicode, represents the cursor.
  3938. longthing += text[i].substr(col, text[i].size());
  3939. } else {
  3940. longthing += text[i];
  3941. }
  3942. if (i != len - 1)
  3943. longthing += "\n";
  3944. }
  3945. return longthing;
  3946. }
  3947. String TextEdit::get_text_for_completion() {
  3948. String longthing;
  3949. int len = text.size();
  3950. for (int i = 0; i < len; i++) {
  3951. if (i == cursor.line) {
  3952. longthing += text[i].substr(0, cursor.column);
  3953. longthing += String::chr(0xFFFF); // Not unicode, represents the cursor.
  3954. longthing += text[i].substr(cursor.column, text[i].size());
  3955. } else {
  3956. longthing += text[i];
  3957. }
  3958. if (i != len - 1)
  3959. longthing += "\n";
  3960. }
  3961. return longthing;
  3962. };
  3963. String TextEdit::get_line(int line) const {
  3964. if (line < 0 || line >= text.size())
  3965. return "";
  3966. return text[line];
  3967. };
  3968. void TextEdit::_clear() {
  3969. clear_undo_history();
  3970. text.clear();
  3971. cursor.column = 0;
  3972. cursor.line = 0;
  3973. cursor.x_ofs = 0;
  3974. cursor.line_ofs = 0;
  3975. cursor.wrap_ofs = 0;
  3976. cursor.last_fit_x = 0;
  3977. selection.active = false;
  3978. }
  3979. void TextEdit::clear() {
  3980. setting_text = true;
  3981. _clear();
  3982. setting_text = false;
  3983. };
  3984. void TextEdit::set_readonly(bool p_readonly) {
  3985. if (readonly == p_readonly)
  3986. return;
  3987. readonly = p_readonly;
  3988. _generate_context_menu();
  3989. // Reorganize context menu.
  3990. menu->clear();
  3991. if (!readonly) {
  3992. menu->add_item(RTR("Undo"), MENU_UNDO, KEY_MASK_CMD | KEY_Z);
  3993. menu->add_item(RTR("Redo"), MENU_REDO, KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z);
  3994. }
  3995. if (!readonly) {
  3996. menu->add_separator();
  3997. menu->add_item(RTR("Cut"), MENU_CUT, KEY_MASK_CMD | KEY_X);
  3998. }
  3999. menu->add_item(RTR("Copy"), MENU_COPY, KEY_MASK_CMD | KEY_C);
  4000. if (!readonly) {
  4001. menu->add_item(RTR("Paste"), MENU_PASTE, KEY_MASK_CMD | KEY_V);
  4002. }
  4003. menu->add_separator();
  4004. menu->add_item(RTR("Select All"), MENU_SELECT_ALL, KEY_MASK_CMD | KEY_A);
  4005. if (!readonly) {
  4006. menu->add_item(RTR("Clear"), MENU_CLEAR);
  4007. }
  4008. update();
  4009. }
  4010. bool TextEdit::is_readonly() const {
  4011. return readonly;
  4012. }
  4013. void TextEdit::set_wrap_enabled(bool p_wrap_enabled) {
  4014. wrap_enabled = p_wrap_enabled;
  4015. }
  4016. bool TextEdit::is_wrap_enabled() const {
  4017. return wrap_enabled;
  4018. }
  4019. void TextEdit::set_max_chars(int p_max_chars) {
  4020. max_chars = p_max_chars;
  4021. }
  4022. int TextEdit::get_max_chars() const {
  4023. return max_chars;
  4024. }
  4025. void TextEdit::_reset_caret_blink_timer() {
  4026. if (caret_blink_enabled) {
  4027. draw_caret = true;
  4028. if (has_focus()) {
  4029. caret_blink_timer->stop();
  4030. caret_blink_timer->start();
  4031. update();
  4032. }
  4033. }
  4034. }
  4035. void TextEdit::_toggle_draw_caret() {
  4036. draw_caret = !draw_caret;
  4037. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  4038. update();
  4039. }
  4040. }
  4041. void TextEdit::_update_caches() {
  4042. cache.style_normal = get_stylebox("normal");
  4043. cache.style_focus = get_stylebox("focus");
  4044. cache.style_readonly = get_stylebox("read_only");
  4045. cache.completion_background_color = get_color("completion_background_color");
  4046. cache.completion_selected_color = get_color("completion_selected_color");
  4047. cache.completion_existing_color = get_color("completion_existing_color");
  4048. cache.completion_font_color = get_color("completion_font_color");
  4049. cache.font = get_font("font");
  4050. cache.caret_color = get_color("caret_color");
  4051. cache.caret_background_color = get_color("caret_background_color");
  4052. cache.line_number_color = get_color("line_number_color");
  4053. cache.safe_line_number_color = get_color("safe_line_number_color");
  4054. cache.font_color = get_color("font_color");
  4055. cache.font_color_selected = get_color("font_color_selected");
  4056. cache.font_color_readonly = get_color("font_color_readonly");
  4057. cache.keyword_color = get_color("keyword_color");
  4058. cache.function_color = get_color("function_color");
  4059. cache.member_variable_color = get_color("member_variable_color");
  4060. cache.number_color = get_color("number_color");
  4061. cache.selection_color = get_color("selection_color");
  4062. cache.mark_color = get_color("mark_color");
  4063. cache.current_line_color = get_color("current_line_color");
  4064. cache.line_length_guideline_color = get_color("line_length_guideline_color");
  4065. cache.bookmark_color = get_color("bookmark_color");
  4066. cache.breakpoint_color = get_color("breakpoint_color");
  4067. cache.executing_line_color = get_color("executing_line_color");
  4068. cache.code_folding_color = get_color("code_folding_color");
  4069. cache.brace_mismatch_color = get_color("brace_mismatch_color");
  4070. cache.word_highlighted_color = get_color("word_highlighted_color");
  4071. cache.search_result_color = get_color("search_result_color");
  4072. cache.search_result_border_color = get_color("search_result_border_color");
  4073. cache.symbol_color = get_color("symbol_color");
  4074. cache.background_color = get_color("background_color");
  4075. #ifdef TOOLS_ENABLED
  4076. cache.line_spacing = get_constant("line_spacing") * EDSCALE;
  4077. #else
  4078. cache.line_spacing = get_constant("line_spacing");
  4079. #endif
  4080. cache.row_height = cache.font->get_height() + cache.line_spacing;
  4081. cache.tab_icon = get_icon("tab");
  4082. cache.space_icon = get_icon("space");
  4083. cache.folded_icon = get_icon("folded");
  4084. cache.can_fold_icon = get_icon("fold");
  4085. cache.folded_eol_icon = get_icon("GuiEllipsis", "EditorIcons");
  4086. cache.executing_icon = get_icon("MainPlay", "EditorIcons");
  4087. text.set_font(cache.font);
  4088. if (syntax_highlighter) {
  4089. syntax_highlighter->_update_cache();
  4090. }
  4091. }
  4092. SyntaxHighlighter *TextEdit::_get_syntax_highlighting() {
  4093. return syntax_highlighter;
  4094. }
  4095. void TextEdit::_set_syntax_highlighting(SyntaxHighlighter *p_syntax_highlighter) {
  4096. syntax_highlighter = p_syntax_highlighter;
  4097. if (syntax_highlighter) {
  4098. syntax_highlighter->set_text_editor(this);
  4099. syntax_highlighter->_update_cache();
  4100. }
  4101. syntax_highlighting_cache.clear();
  4102. update();
  4103. }
  4104. int TextEdit::_is_line_in_region(int p_line) {
  4105. // Do we have in cache?
  4106. if (color_region_cache.has(p_line)) {
  4107. return color_region_cache[p_line];
  4108. }
  4109. // If not find the closest line we have.
  4110. int previous_line = p_line - 1;
  4111. for (; previous_line > -1; previous_line--) {
  4112. if (color_region_cache.has(p_line)) {
  4113. break;
  4114. }
  4115. }
  4116. // Calculate up to line we need and update the cache along the way.
  4117. int in_region = color_region_cache[previous_line];
  4118. if (previous_line == -1) {
  4119. in_region = -1;
  4120. }
  4121. for (int i = previous_line; i < p_line; i++) {
  4122. const Map<int, Text::ColorRegionInfo> &cri_map = _get_line_color_region_info(i);
  4123. for (const Map<int, Text::ColorRegionInfo>::Element *E = cri_map.front(); E; E = E->next()) {
  4124. const Text::ColorRegionInfo &cri = E->get();
  4125. if (in_region == -1) {
  4126. if (!cri.end) {
  4127. in_region = cri.region;
  4128. }
  4129. } else if (in_region == cri.region && !_get_color_region(cri.region).line_only) {
  4130. if (cri.end || _get_color_region(cri.region).eq) {
  4131. in_region = -1;
  4132. }
  4133. }
  4134. }
  4135. if (in_region >= 0 && _get_color_region(in_region).line_only) {
  4136. in_region = -1;
  4137. }
  4138. color_region_cache[i + 1] = in_region;
  4139. }
  4140. return in_region;
  4141. }
  4142. TextEdit::ColorRegion TextEdit::_get_color_region(int p_region) const {
  4143. if (p_region < 0 || p_region >= color_regions.size()) {
  4144. return ColorRegion();
  4145. }
  4146. return color_regions[p_region];
  4147. }
  4148. Map<int, TextEdit::Text::ColorRegionInfo> TextEdit::_get_line_color_region_info(int p_line) const {
  4149. if (p_line < 0 || p_line > text.size() - 1) {
  4150. return Map<int, Text::ColorRegionInfo>();
  4151. }
  4152. return text.get_color_region_info(p_line);
  4153. }
  4154. void TextEdit::clear_colors() {
  4155. keywords.clear();
  4156. member_keywords.clear();
  4157. color_regions.clear();
  4158. color_region_cache.clear();
  4159. syntax_highlighting_cache.clear();
  4160. text.clear_width_cache();
  4161. update();
  4162. }
  4163. void TextEdit::add_keyword_color(const String &p_keyword, const Color &p_color) {
  4164. keywords[p_keyword] = p_color;
  4165. syntax_highlighting_cache.clear();
  4166. update();
  4167. }
  4168. bool TextEdit::has_keyword_color(String p_keyword) const {
  4169. return keywords.has(p_keyword);
  4170. }
  4171. Color TextEdit::get_keyword_color(String p_keyword) const {
  4172. ERR_FAIL_COND_V(!keywords.has(p_keyword), Color());
  4173. return keywords[p_keyword];
  4174. }
  4175. void TextEdit::add_color_region(const String &p_begin_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
  4176. color_regions.push_back(ColorRegion(p_begin_key, p_end_key, p_color, p_line_only));
  4177. syntax_highlighting_cache.clear();
  4178. text.clear_width_cache();
  4179. update();
  4180. }
  4181. void TextEdit::add_member_keyword(const String &p_keyword, const Color &p_color) {
  4182. member_keywords[p_keyword] = p_color;
  4183. syntax_highlighting_cache.clear();
  4184. update();
  4185. }
  4186. bool TextEdit::has_member_color(String p_member) const {
  4187. return member_keywords.has(p_member);
  4188. }
  4189. Color TextEdit::get_member_color(String p_member) const {
  4190. return member_keywords[p_member];
  4191. }
  4192. void TextEdit::clear_member_keywords() {
  4193. member_keywords.clear();
  4194. syntax_highlighting_cache.clear();
  4195. update();
  4196. }
  4197. void TextEdit::set_syntax_coloring(bool p_enabled) {
  4198. syntax_coloring = p_enabled;
  4199. update();
  4200. }
  4201. bool TextEdit::is_syntax_coloring_enabled() const {
  4202. return syntax_coloring;
  4203. }
  4204. void TextEdit::set_auto_indent(bool p_auto_indent) {
  4205. auto_indent = p_auto_indent;
  4206. }
  4207. void TextEdit::cut() {
  4208. if (!selection.active) {
  4209. String clipboard = text[cursor.line];
  4210. OS::get_singleton()->set_clipboard(clipboard);
  4211. cursor_set_line(cursor.line);
  4212. cursor_set_column(0);
  4213. if (cursor.line == 0 && get_line_count() > 1) {
  4214. _remove_text(cursor.line, 0, cursor.line + 1, 0);
  4215. } else {
  4216. _remove_text(cursor.line, 0, cursor.line, text[cursor.line].length());
  4217. backspace_at_cursor();
  4218. cursor_set_line(cursor.line + 1);
  4219. }
  4220. update();
  4221. cut_copy_line = clipboard;
  4222. } else {
  4223. String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  4224. OS::get_singleton()->set_clipboard(clipboard);
  4225. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  4226. cursor_set_line(selection.from_line); // Set afterwards else it causes the view to be offset.
  4227. cursor_set_column(selection.from_column);
  4228. selection.active = false;
  4229. selection.selecting_mode = Selection::MODE_NONE;
  4230. update();
  4231. cut_copy_line = "";
  4232. }
  4233. }
  4234. void TextEdit::copy() {
  4235. if (!selection.active) {
  4236. if (text[cursor.line].length() != 0) {
  4237. String clipboard = _base_get_text(cursor.line, 0, cursor.line, text[cursor.line].length());
  4238. OS::get_singleton()->set_clipboard(clipboard);
  4239. cut_copy_line = clipboard;
  4240. }
  4241. } else {
  4242. String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  4243. OS::get_singleton()->set_clipboard(clipboard);
  4244. cut_copy_line = "";
  4245. }
  4246. }
  4247. void TextEdit::paste() {
  4248. String clipboard = OS::get_singleton()->get_clipboard();
  4249. begin_complex_operation();
  4250. if (selection.active) {
  4251. selection.active = false;
  4252. selection.selecting_mode = Selection::MODE_NONE;
  4253. _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  4254. cursor_set_line(selection.from_line);
  4255. cursor_set_column(selection.from_column);
  4256. } else if (!cut_copy_line.empty() && cut_copy_line == clipboard) {
  4257. cursor_set_column(0);
  4258. String ins = "\n";
  4259. clipboard += ins;
  4260. }
  4261. _insert_text_at_cursor(clipboard);
  4262. end_complex_operation();
  4263. update();
  4264. }
  4265. void TextEdit::select_all() {
  4266. if (!selecting_enabled)
  4267. return;
  4268. if (text.size() == 1 && text[0].length() == 0)
  4269. return;
  4270. selection.active = true;
  4271. selection.from_line = 0;
  4272. selection.from_column = 0;
  4273. selection.selecting_line = 0;
  4274. selection.selecting_column = 0;
  4275. selection.to_line = text.size() - 1;
  4276. selection.to_column = text[selection.to_line].length();
  4277. selection.selecting_mode = Selection::MODE_SHIFT;
  4278. selection.shiftclick_left = true;
  4279. cursor_set_line(selection.to_line, false);
  4280. cursor_set_column(selection.to_column, false);
  4281. update();
  4282. }
  4283. void TextEdit::deselect() {
  4284. selection.active = false;
  4285. update();
  4286. }
  4287. void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
  4288. if (!selecting_enabled)
  4289. return;
  4290. if (p_from_line < 0)
  4291. p_from_line = 0;
  4292. else if (p_from_line >= text.size())
  4293. p_from_line = text.size() - 1;
  4294. if (p_from_column >= text[p_from_line].length())
  4295. p_from_column = text[p_from_line].length();
  4296. if (p_from_column < 0)
  4297. p_from_column = 0;
  4298. if (p_to_line < 0)
  4299. p_to_line = 0;
  4300. else if (p_to_line >= text.size())
  4301. p_to_line = text.size() - 1;
  4302. if (p_to_column >= text[p_to_line].length())
  4303. p_to_column = text[p_to_line].length();
  4304. if (p_to_column < 0)
  4305. p_to_column = 0;
  4306. selection.from_line = p_from_line;
  4307. selection.from_column = p_from_column;
  4308. selection.to_line = p_to_line;
  4309. selection.to_column = p_to_column;
  4310. selection.active = true;
  4311. if (selection.from_line == selection.to_line) {
  4312. if (selection.from_column == selection.to_column) {
  4313. selection.active = false;
  4314. } else if (selection.from_column > selection.to_column) {
  4315. selection.shiftclick_left = false;
  4316. SWAP(selection.from_column, selection.to_column);
  4317. } else {
  4318. selection.shiftclick_left = true;
  4319. }
  4320. } else if (selection.from_line > selection.to_line) {
  4321. selection.shiftclick_left = false;
  4322. SWAP(selection.from_line, selection.to_line);
  4323. SWAP(selection.from_column, selection.to_column);
  4324. } else {
  4325. selection.shiftclick_left = true;
  4326. }
  4327. update();
  4328. }
  4329. void TextEdit::swap_lines(int line1, int line2) {
  4330. String tmp = get_line(line1);
  4331. String tmp2 = get_line(line2);
  4332. set_line(line2, tmp);
  4333. set_line(line1, tmp2);
  4334. }
  4335. bool TextEdit::is_selection_active() const {
  4336. return selection.active;
  4337. }
  4338. int TextEdit::get_selection_from_line() const {
  4339. ERR_FAIL_COND_V(!selection.active, -1);
  4340. return selection.from_line;
  4341. }
  4342. int TextEdit::get_selection_from_column() const {
  4343. ERR_FAIL_COND_V(!selection.active, -1);
  4344. return selection.from_column;
  4345. }
  4346. int TextEdit::get_selection_to_line() const {
  4347. ERR_FAIL_COND_V(!selection.active, -1);
  4348. return selection.to_line;
  4349. }
  4350. int TextEdit::get_selection_to_column() const {
  4351. ERR_FAIL_COND_V(!selection.active, -1);
  4352. return selection.to_column;
  4353. }
  4354. String TextEdit::get_selection_text() const {
  4355. if (!selection.active)
  4356. return "";
  4357. return _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
  4358. }
  4359. String TextEdit::get_word_under_cursor() const {
  4360. int prev_cc = cursor.column;
  4361. while (prev_cc > 0) {
  4362. bool is_char = _is_text_char(text[cursor.line][prev_cc - 1]);
  4363. if (!is_char)
  4364. break;
  4365. --prev_cc;
  4366. }
  4367. int next_cc = cursor.column;
  4368. while (next_cc < text[cursor.line].length()) {
  4369. bool is_char = _is_text_char(text[cursor.line][next_cc]);
  4370. if (!is_char)
  4371. break;
  4372. ++next_cc;
  4373. }
  4374. if (prev_cc == cursor.column || next_cc == cursor.column)
  4375. return "";
  4376. return text[cursor.line].substr(prev_cc, next_cc - prev_cc);
  4377. }
  4378. void TextEdit::set_search_text(const String &p_search_text) {
  4379. search_text = p_search_text;
  4380. }
  4381. void TextEdit::set_search_flags(uint32_t p_flags) {
  4382. search_flags = p_flags;
  4383. }
  4384. void TextEdit::set_current_search_result(int line, int col) {
  4385. search_result_line = line;
  4386. search_result_col = col;
  4387. update();
  4388. }
  4389. void TextEdit::set_highlight_all_occurrences(const bool p_enabled) {
  4390. highlight_all_occurrences = p_enabled;
  4391. update();
  4392. }
  4393. bool TextEdit::is_highlight_all_occurrences_enabled() const {
  4394. return highlight_all_occurrences;
  4395. }
  4396. int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) {
  4397. int col = -1;
  4398. if (p_key.length() > 0 && p_search.length() > 0) {
  4399. if (p_from_column < 0 || p_from_column > p_search.length()) {
  4400. p_from_column = 0;
  4401. }
  4402. while (col == -1 && p_from_column <= p_search.length()) {
  4403. if (p_search_flags & SEARCH_MATCH_CASE) {
  4404. col = p_search.find(p_key, p_from_column);
  4405. } else {
  4406. col = p_search.findn(p_key, p_from_column);
  4407. }
  4408. // Whole words only.
  4409. if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) {
  4410. p_from_column = col;
  4411. if (col > 0 && _is_text_char(p_search[col - 1])) {
  4412. col = -1;
  4413. } else if ((col + p_key.length()) < p_search.length() && _is_text_char(p_search[col + p_key.length()])) {
  4414. col = -1;
  4415. }
  4416. }
  4417. p_from_column += 1;
  4418. }
  4419. }
  4420. return col;
  4421. }
  4422. PoolVector<int> TextEdit::_search_bind(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const {
  4423. int col, line;
  4424. if (search(p_key, p_search_flags, p_from_line, p_from_column, line, col)) {
  4425. PoolVector<int> result;
  4426. result.resize(2);
  4427. result.set(SEARCH_RESULT_COLUMN, col);
  4428. result.set(SEARCH_RESULT_LINE, line);
  4429. return result;
  4430. } else {
  4431. return PoolVector<int>();
  4432. }
  4433. }
  4434. 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 {
  4435. if (p_key.length() == 0)
  4436. return false;
  4437. ERR_FAIL_INDEX_V(p_from_line, text.size(), false);
  4438. ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, false);
  4439. // Search through the whole document, but start by current line.
  4440. int line = p_from_line;
  4441. int pos = -1;
  4442. for (int i = 0; i < text.size() + 1; i++) {
  4443. if (line < 0) {
  4444. line = text.size() - 1;
  4445. }
  4446. if (line == text.size()) {
  4447. line = 0;
  4448. }
  4449. String text_line = text[line];
  4450. int from_column = 0;
  4451. if (line == p_from_line) {
  4452. if (i == text.size()) {
  4453. // Wrapped.
  4454. if (p_search_flags & SEARCH_BACKWARDS) {
  4455. from_column = text_line.length();
  4456. } else {
  4457. from_column = 0;
  4458. }
  4459. } else {
  4460. from_column = p_from_column;
  4461. }
  4462. } else {
  4463. if (p_search_flags & SEARCH_BACKWARDS)
  4464. from_column = text_line.length() - 1;
  4465. else
  4466. from_column = 0;
  4467. }
  4468. pos = -1;
  4469. int pos_from = (p_search_flags & SEARCH_BACKWARDS) ? text_line.length() : 0;
  4470. int last_pos = -1;
  4471. while (true) {
  4472. if (p_search_flags & SEARCH_BACKWARDS) {
  4473. while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.rfind(p_key, pos_from) : text_line.rfindn(p_key, pos_from)) != -1) {
  4474. if (last_pos <= from_column) {
  4475. pos = last_pos;
  4476. break;
  4477. }
  4478. pos_from = last_pos - p_key.length();
  4479. if (pos_from < 0) {
  4480. break;
  4481. }
  4482. }
  4483. } else {
  4484. while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, pos_from) : text_line.findn(p_key, pos_from)) != -1) {
  4485. if (last_pos >= from_column) {
  4486. pos = last_pos;
  4487. break;
  4488. }
  4489. pos_from = last_pos + p_key.length();
  4490. }
  4491. }
  4492. bool is_match = true;
  4493. if (pos != -1 && (p_search_flags & SEARCH_WHOLE_WORDS)) {
  4494. // Validate for whole words.
  4495. if (pos > 0 && _is_text_char(text_line[pos - 1]))
  4496. is_match = false;
  4497. else if (pos + p_key.length() < text_line.length() && _is_text_char(text_line[pos + p_key.length()]))
  4498. is_match = false;
  4499. }
  4500. if (pos_from == -1) {
  4501. pos = -1;
  4502. }
  4503. if (is_match || last_pos == -1 || pos == -1) {
  4504. break;
  4505. }
  4506. pos_from = (p_search_flags & SEARCH_BACKWARDS) ? pos - 1 : pos + 1;
  4507. pos = -1;
  4508. }
  4509. if (pos != -1)
  4510. break;
  4511. if (p_search_flags & SEARCH_BACKWARDS)
  4512. line--;
  4513. else
  4514. line++;
  4515. }
  4516. if (pos == -1) {
  4517. r_line = -1;
  4518. r_column = -1;
  4519. return false;
  4520. }
  4521. r_line = line;
  4522. r_column = pos;
  4523. return true;
  4524. }
  4525. void TextEdit::_cursor_changed_emit() {
  4526. emit_signal("cursor_changed");
  4527. cursor_changed_dirty = false;
  4528. }
  4529. void TextEdit::_text_changed_emit() {
  4530. emit_signal("text_changed");
  4531. text_changed_dirty = false;
  4532. }
  4533. void TextEdit::set_line_as_marked(int p_line, bool p_marked) {
  4534. ERR_FAIL_INDEX(p_line, text.size());
  4535. text.set_marked(p_line, p_marked);
  4536. update();
  4537. }
  4538. void TextEdit::set_line_as_safe(int p_line, bool p_safe) {
  4539. ERR_FAIL_INDEX(p_line, text.size());
  4540. text.set_safe(p_line, p_safe);
  4541. update();
  4542. }
  4543. bool TextEdit::is_line_set_as_safe(int p_line) const {
  4544. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4545. return text.is_safe(p_line);
  4546. }
  4547. void TextEdit::set_executing_line(int p_line) {
  4548. ERR_FAIL_INDEX(p_line, text.size());
  4549. executing_line = p_line;
  4550. update();
  4551. }
  4552. void TextEdit::clear_executing_line() {
  4553. executing_line = -1;
  4554. update();
  4555. }
  4556. bool TextEdit::is_line_set_as_bookmark(int p_line) const {
  4557. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4558. return text.is_bookmark(p_line);
  4559. }
  4560. void TextEdit::set_line_as_bookmark(int p_line, bool p_bookmark) {
  4561. ERR_FAIL_INDEX(p_line, text.size());
  4562. text.set_bookmark(p_line, p_bookmark);
  4563. update();
  4564. }
  4565. void TextEdit::get_bookmarks(List<int> *p_bookmarks) const {
  4566. for (int i = 0; i < text.size(); i++) {
  4567. if (text.is_bookmark(i))
  4568. p_bookmarks->push_back(i);
  4569. }
  4570. }
  4571. Array TextEdit::get_bookmarks_array() const {
  4572. Array arr;
  4573. for (int i = 0; i < text.size(); i++) {
  4574. if (text.is_bookmark(i))
  4575. arr.append(i);
  4576. }
  4577. return arr;
  4578. }
  4579. bool TextEdit::is_line_set_as_breakpoint(int p_line) const {
  4580. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4581. return text.is_breakpoint(p_line);
  4582. }
  4583. void TextEdit::set_line_as_breakpoint(int p_line, bool p_breakpoint) {
  4584. ERR_FAIL_INDEX(p_line, text.size());
  4585. text.set_breakpoint(p_line, p_breakpoint);
  4586. update();
  4587. }
  4588. void TextEdit::get_breakpoints(List<int> *p_breakpoints) const {
  4589. for (int i = 0; i < text.size(); i++) {
  4590. if (text.is_breakpoint(i))
  4591. p_breakpoints->push_back(i);
  4592. }
  4593. }
  4594. Array TextEdit::get_breakpoints_array() const {
  4595. Array arr;
  4596. for (int i = 0; i < text.size(); i++) {
  4597. if (text.is_breakpoint(i))
  4598. arr.append(i);
  4599. }
  4600. return arr;
  4601. }
  4602. void TextEdit::remove_breakpoints() {
  4603. for (int i = 0; i < text.size(); i++) {
  4604. if (text.is_breakpoint(i))
  4605. /* Should "breakpoint_toggled" be fired when breakpoints are removed this way? */
  4606. text.set_breakpoint(i, false);
  4607. }
  4608. }
  4609. void TextEdit::set_line_info_icon(int p_line, Ref<Texture> p_icon, String p_info) {
  4610. ERR_FAIL_INDEX(p_line, text.size());
  4611. text.set_info_icon(p_line, p_icon, p_info);
  4612. update();
  4613. }
  4614. void TextEdit::clear_info_icons() {
  4615. text.clear_info_icons();
  4616. update();
  4617. }
  4618. void TextEdit::set_line_as_hidden(int p_line, bool p_hidden) {
  4619. ERR_FAIL_INDEX(p_line, text.size());
  4620. if (is_hiding_enabled() || !p_hidden)
  4621. text.set_hidden(p_line, p_hidden);
  4622. update();
  4623. }
  4624. bool TextEdit::is_line_hidden(int p_line) const {
  4625. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4626. return text.is_hidden(p_line);
  4627. }
  4628. void TextEdit::fold_all_lines() {
  4629. for (int i = 0; i < text.size(); i++) {
  4630. fold_line(i);
  4631. }
  4632. _update_scrollbars();
  4633. update();
  4634. }
  4635. void TextEdit::unhide_all_lines() {
  4636. for (int i = 0; i < text.size(); i++) {
  4637. text.set_hidden(i, false);
  4638. }
  4639. _update_scrollbars();
  4640. update();
  4641. }
  4642. int TextEdit::num_lines_from(int p_line_from, int visible_amount) const {
  4643. // Returns the number of lines (hidden and unhidden) from p_line_from to (p_line_from + visible_amount of unhidden lines).
  4644. ERR_FAIL_INDEX_V(p_line_from, text.size(), ABS(visible_amount));
  4645. if (!is_hiding_enabled())
  4646. return ABS(visible_amount);
  4647. int num_visible = 0;
  4648. int num_total = 0;
  4649. if (visible_amount >= 0) {
  4650. for (int i = p_line_from; i < text.size(); i++) {
  4651. num_total++;
  4652. if (!is_line_hidden(i)) {
  4653. num_visible++;
  4654. }
  4655. if (num_visible >= visible_amount)
  4656. break;
  4657. }
  4658. } else {
  4659. visible_amount = ABS(visible_amount);
  4660. for (int i = p_line_from; i >= 0; i--) {
  4661. num_total++;
  4662. if (!is_line_hidden(i)) {
  4663. num_visible++;
  4664. }
  4665. if (num_visible >= visible_amount)
  4666. break;
  4667. }
  4668. }
  4669. return num_total;
  4670. }
  4671. int TextEdit::num_lines_from_rows(int p_line_from, int p_wrap_index_from, int visible_amount, int &wrap_index) const {
  4672. // 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).
  4673. // Wrap index is set to the wrap index of the last line.
  4674. wrap_index = 0;
  4675. ERR_FAIL_INDEX_V(p_line_from, text.size(), ABS(visible_amount));
  4676. if (!is_hiding_enabled() && !is_wrap_enabled())
  4677. return ABS(visible_amount);
  4678. int num_visible = 0;
  4679. int num_total = 0;
  4680. if (visible_amount == 0) {
  4681. num_total = 0;
  4682. wrap_index = 0;
  4683. } else if (visible_amount > 0) {
  4684. int i;
  4685. num_visible -= p_wrap_index_from;
  4686. for (i = p_line_from; i < text.size(); i++) {
  4687. num_total++;
  4688. if (!is_line_hidden(i)) {
  4689. num_visible++;
  4690. num_visible += times_line_wraps(i);
  4691. }
  4692. if (num_visible >= visible_amount)
  4693. break;
  4694. }
  4695. wrap_index = times_line_wraps(MIN(i, text.size() - 1)) - (num_visible - visible_amount);
  4696. } else {
  4697. visible_amount = ABS(visible_amount);
  4698. int i;
  4699. num_visible -= times_line_wraps(p_line_from) - p_wrap_index_from;
  4700. for (i = p_line_from; i >= 0; i--) {
  4701. num_total++;
  4702. if (!is_line_hidden(i)) {
  4703. num_visible++;
  4704. num_visible += times_line_wraps(i);
  4705. }
  4706. if (num_visible >= visible_amount)
  4707. break;
  4708. }
  4709. wrap_index = (num_visible - visible_amount);
  4710. }
  4711. wrap_index = MAX(wrap_index, 0);
  4712. return num_total;
  4713. }
  4714. int TextEdit::get_last_unhidden_line() const {
  4715. // Returns the last line in the text that is not hidden.
  4716. if (!is_hiding_enabled())
  4717. return text.size() - 1;
  4718. int last_line;
  4719. for (last_line = text.size() - 1; last_line > 0; last_line--) {
  4720. if (!is_line_hidden(last_line)) {
  4721. break;
  4722. }
  4723. }
  4724. return last_line;
  4725. }
  4726. int TextEdit::get_indent_level(int p_line) const {
  4727. ERR_FAIL_INDEX_V(p_line, text.size(), 0);
  4728. // Counts number of tabs and spaces before line starts.
  4729. int tab_count = 0;
  4730. int whitespace_count = 0;
  4731. int line_length = text[p_line].size();
  4732. for (int i = 0; i < line_length - 1; i++) {
  4733. if (text[p_line][i] == '\t') {
  4734. tab_count++;
  4735. } else if (text[p_line][i] == ' ') {
  4736. whitespace_count++;
  4737. } else {
  4738. break;
  4739. }
  4740. }
  4741. return tab_count * indent_size + whitespace_count;
  4742. }
  4743. bool TextEdit::is_line_comment(int p_line) const {
  4744. // Checks to see if this line is the start of a comment.
  4745. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4746. const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(p_line);
  4747. int line_length = text[p_line].size();
  4748. for (int i = 0; i < line_length - 1; i++) {
  4749. if (_is_symbol(text[p_line][i]) && cri_map.has(i)) {
  4750. const Text::ColorRegionInfo &cri = cri_map[i];
  4751. return color_regions[cri.region].begin_key == "#" || color_regions[cri.region].begin_key == "//";
  4752. } else if (_is_whitespace(text[p_line][i])) {
  4753. continue;
  4754. } else {
  4755. break;
  4756. }
  4757. }
  4758. return false;
  4759. }
  4760. bool TextEdit::can_fold(int p_line) const {
  4761. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4762. if (!is_hiding_enabled())
  4763. return false;
  4764. if (p_line + 1 >= text.size())
  4765. return false;
  4766. if (text[p_line].strip_edges().size() == 0)
  4767. return false;
  4768. if (is_folded(p_line))
  4769. return false;
  4770. if (is_line_hidden(p_line))
  4771. return false;
  4772. if (is_line_comment(p_line))
  4773. return false;
  4774. int start_indent = get_indent_level(p_line);
  4775. for (int i = p_line + 1; i < text.size(); i++) {
  4776. if (text[i].strip_edges().size() == 0)
  4777. continue;
  4778. int next_indent = get_indent_level(i);
  4779. if (is_line_comment(i)) {
  4780. continue;
  4781. } else if (next_indent > start_indent) {
  4782. return true;
  4783. } else {
  4784. return false;
  4785. }
  4786. }
  4787. return false;
  4788. }
  4789. bool TextEdit::is_folded(int p_line) const {
  4790. ERR_FAIL_INDEX_V(p_line, text.size(), false);
  4791. if (p_line + 1 >= text.size())
  4792. return false;
  4793. return !is_line_hidden(p_line) && is_line_hidden(p_line + 1);
  4794. }
  4795. Vector<int> TextEdit::get_folded_lines() const {
  4796. Vector<int> folded_lines;
  4797. for (int i = 0; i < text.size(); i++) {
  4798. if (is_folded(i)) {
  4799. folded_lines.push_back(i);
  4800. }
  4801. }
  4802. return folded_lines;
  4803. }
  4804. void TextEdit::fold_line(int p_line) {
  4805. ERR_FAIL_INDEX(p_line, text.size());
  4806. if (!is_hiding_enabled())
  4807. return;
  4808. if (!can_fold(p_line))
  4809. return;
  4810. // Hide lines below this one.
  4811. int start_indent = get_indent_level(p_line);
  4812. int last_line = start_indent;
  4813. for (int i = p_line + 1; i < text.size(); i++) {
  4814. if (text[i].strip_edges().size() != 0) {
  4815. if (is_line_comment(i)) {
  4816. continue;
  4817. } else if (get_indent_level(i) > start_indent) {
  4818. last_line = i;
  4819. } else {
  4820. break;
  4821. }
  4822. }
  4823. }
  4824. for (int i = p_line + 1; i <= last_line; i++) {
  4825. set_line_as_hidden(i, true);
  4826. }
  4827. // Fix selection.
  4828. if (is_selection_active()) {
  4829. if (is_line_hidden(selection.from_line) && is_line_hidden(selection.to_line)) {
  4830. deselect();
  4831. } else if (is_line_hidden(selection.from_line)) {
  4832. select(p_line, 9999, selection.to_line, selection.to_column);
  4833. } else if (is_line_hidden(selection.to_line)) {
  4834. select(selection.from_line, selection.from_column, p_line, 9999);
  4835. }
  4836. }
  4837. // Reset cursor.
  4838. if (is_line_hidden(cursor.line)) {
  4839. cursor_set_line(p_line, false, false);
  4840. cursor_set_column(get_line(p_line).length(), false);
  4841. }
  4842. _update_scrollbars();
  4843. update();
  4844. }
  4845. void TextEdit::unfold_line(int p_line) {
  4846. ERR_FAIL_INDEX(p_line, text.size());
  4847. if (!is_folded(p_line) && !is_line_hidden(p_line))
  4848. return;
  4849. int fold_start;
  4850. for (fold_start = p_line; fold_start > 0; fold_start--) {
  4851. if (is_folded(fold_start))
  4852. break;
  4853. }
  4854. fold_start = is_folded(fold_start) ? fold_start : p_line;
  4855. for (int i = fold_start + 1; i < text.size(); i++) {
  4856. if (is_line_hidden(i)) {
  4857. set_line_as_hidden(i, false);
  4858. } else {
  4859. break;
  4860. }
  4861. }
  4862. _update_scrollbars();
  4863. update();
  4864. }
  4865. void TextEdit::toggle_fold_line(int p_line) {
  4866. ERR_FAIL_INDEX(p_line, text.size());
  4867. if (!is_folded(p_line))
  4868. fold_line(p_line);
  4869. else
  4870. unfold_line(p_line);
  4871. }
  4872. int TextEdit::get_line_count() const {
  4873. return text.size();
  4874. }
  4875. void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) {
  4876. ERR_FAIL_COND(p_op.type == TextOperation::TYPE_NONE);
  4877. bool insert = p_op.type == TextOperation::TYPE_INSERT;
  4878. if (p_reverse)
  4879. insert = !insert;
  4880. if (insert) {
  4881. int check_line;
  4882. int check_column;
  4883. _base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column);
  4884. ERR_FAIL_COND(check_line != p_op.to_line); // BUG.
  4885. ERR_FAIL_COND(check_column != p_op.to_column); // BUG.
  4886. } else {
  4887. _base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column);
  4888. }
  4889. }
  4890. void TextEdit::_clear_redo() {
  4891. if (undo_stack_pos == NULL)
  4892. return; // Nothing to clear.
  4893. _push_current_op();
  4894. while (undo_stack_pos) {
  4895. List<TextOperation>::Element *elem = undo_stack_pos;
  4896. undo_stack_pos = undo_stack_pos->next();
  4897. undo_stack.erase(elem);
  4898. }
  4899. }
  4900. void TextEdit::undo() {
  4901. _push_current_op();
  4902. if (undo_stack_pos == NULL) {
  4903. if (!undo_stack.size())
  4904. return; // Nothing to undo.
  4905. undo_stack_pos = undo_stack.back();
  4906. } else if (undo_stack_pos == undo_stack.front())
  4907. return; // At the bottom of the undo stack.
  4908. else
  4909. undo_stack_pos = undo_stack_pos->prev();
  4910. deselect();
  4911. TextOperation op = undo_stack_pos->get();
  4912. _do_text_op(op, true);
  4913. if (op.type != TextOperation::TYPE_INSERT && (op.from_line != op.to_line || op.to_column != op.from_column + 1))
  4914. select(op.from_line, op.from_column, op.to_line, op.to_column);
  4915. current_op.version = op.prev_version;
  4916. if (undo_stack_pos->get().chain_backward) {
  4917. while (true) {
  4918. ERR_BREAK(!undo_stack_pos->prev());
  4919. undo_stack_pos = undo_stack_pos->prev();
  4920. op = undo_stack_pos->get();
  4921. _do_text_op(op, true);
  4922. current_op.version = op.prev_version;
  4923. if (undo_stack_pos->get().chain_forward) {
  4924. break;
  4925. }
  4926. }
  4927. }
  4928. _update_scrollbars();
  4929. if (undo_stack_pos->get().type == TextOperation::TYPE_REMOVE) {
  4930. cursor_set_line(undo_stack_pos->get().to_line);
  4931. cursor_set_column(undo_stack_pos->get().to_column);
  4932. _cancel_code_hint();
  4933. } else {
  4934. cursor_set_line(undo_stack_pos->get().from_line);
  4935. cursor_set_column(undo_stack_pos->get().from_column);
  4936. }
  4937. update();
  4938. }
  4939. void TextEdit::redo() {
  4940. _push_current_op();
  4941. if (undo_stack_pos == NULL)
  4942. return; // Nothing to do.
  4943. deselect();
  4944. TextOperation op = undo_stack_pos->get();
  4945. _do_text_op(op, false);
  4946. current_op.version = op.version;
  4947. if (undo_stack_pos->get().chain_forward) {
  4948. while (true) {
  4949. ERR_BREAK(!undo_stack_pos->next());
  4950. undo_stack_pos = undo_stack_pos->next();
  4951. op = undo_stack_pos->get();
  4952. _do_text_op(op, false);
  4953. current_op.version = op.version;
  4954. if (undo_stack_pos->get().chain_backward)
  4955. break;
  4956. }
  4957. }
  4958. _update_scrollbars();
  4959. cursor_set_line(undo_stack_pos->get().to_line);
  4960. cursor_set_column(undo_stack_pos->get().to_column);
  4961. undo_stack_pos = undo_stack_pos->next();
  4962. update();
  4963. }
  4964. void TextEdit::clear_undo_history() {
  4965. saved_version = 0;
  4966. current_op.type = TextOperation::TYPE_NONE;
  4967. undo_stack_pos = NULL;
  4968. undo_stack.clear();
  4969. }
  4970. void TextEdit::begin_complex_operation() {
  4971. _push_current_op();
  4972. next_operation_is_complex = true;
  4973. }
  4974. void TextEdit::end_complex_operation() {
  4975. _push_current_op();
  4976. ERR_FAIL_COND(undo_stack.size() == 0);
  4977. if (undo_stack.back()->get().chain_forward) {
  4978. undo_stack.back()->get().chain_forward = false;
  4979. return;
  4980. }
  4981. undo_stack.back()->get().chain_backward = true;
  4982. }
  4983. void TextEdit::_push_current_op() {
  4984. if (current_op.type == TextOperation::TYPE_NONE)
  4985. return; // Nothing to do.
  4986. if (next_operation_is_complex) {
  4987. current_op.chain_forward = true;
  4988. next_operation_is_complex = false;
  4989. }
  4990. undo_stack.push_back(current_op);
  4991. current_op.type = TextOperation::TYPE_NONE;
  4992. current_op.text = "";
  4993. current_op.chain_forward = false;
  4994. }
  4995. void TextEdit::set_indent_using_spaces(const bool p_use_spaces) {
  4996. indent_using_spaces = p_use_spaces;
  4997. }
  4998. bool TextEdit::is_indent_using_spaces() const {
  4999. return indent_using_spaces;
  5000. }
  5001. void TextEdit::set_indent_size(const int p_size) {
  5002. ERR_FAIL_COND_MSG(p_size <= 0, "Indend size must be greater than 0.");
  5003. indent_size = p_size;
  5004. text.set_indent_size(p_size);
  5005. space_indent = "";
  5006. for (int i = 0; i < p_size; i++) {
  5007. space_indent += " ";
  5008. }
  5009. update();
  5010. }
  5011. int TextEdit::get_indent_size() {
  5012. return indent_size;
  5013. }
  5014. void TextEdit::set_draw_tabs(bool p_draw) {
  5015. draw_tabs = p_draw;
  5016. update();
  5017. }
  5018. bool TextEdit::is_drawing_tabs() const {
  5019. return draw_tabs;
  5020. }
  5021. void TextEdit::set_draw_spaces(bool p_draw) {
  5022. draw_spaces = p_draw;
  5023. }
  5024. bool TextEdit::is_drawing_spaces() const {
  5025. return draw_spaces;
  5026. }
  5027. void TextEdit::set_override_selected_font_color(bool p_override_selected_font_color) {
  5028. override_selected_font_color = p_override_selected_font_color;
  5029. }
  5030. bool TextEdit::is_overriding_selected_font_color() const {
  5031. return override_selected_font_color;
  5032. }
  5033. void TextEdit::set_insert_mode(bool p_enabled) {
  5034. insert_mode = p_enabled;
  5035. update();
  5036. }
  5037. bool TextEdit::is_insert_mode() const {
  5038. return insert_mode;
  5039. }
  5040. bool TextEdit::is_insert_text_operation() {
  5041. return (current_op.type == TextOperation::TYPE_INSERT);
  5042. }
  5043. uint32_t TextEdit::get_version() const {
  5044. return current_op.version;
  5045. }
  5046. uint32_t TextEdit::get_saved_version() const {
  5047. return saved_version;
  5048. }
  5049. void TextEdit::tag_saved_version() {
  5050. saved_version = get_version();
  5051. }
  5052. double TextEdit::get_scroll_pos_for_line(int p_line, int p_wrap_index) const {
  5053. if (!is_wrap_enabled() && !is_hiding_enabled())
  5054. return p_line;
  5055. // Count the number of visible lines up to this line.
  5056. double new_line_scroll_pos = 0;
  5057. int to = CLAMP(p_line, 0, text.size() - 1);
  5058. for (int i = 0; i < to; i++) {
  5059. if (!text.is_hidden(i)) {
  5060. new_line_scroll_pos++;
  5061. new_line_scroll_pos += times_line_wraps(i);
  5062. }
  5063. }
  5064. new_line_scroll_pos += p_wrap_index;
  5065. return new_line_scroll_pos;
  5066. }
  5067. void TextEdit::set_line_as_first_visible(int p_line, int p_wrap_index) {
  5068. set_v_scroll(get_scroll_pos_for_line(p_line, p_wrap_index));
  5069. }
  5070. void TextEdit::set_line_as_center_visible(int p_line, int p_wrap_index) {
  5071. int visible_rows = get_visible_rows();
  5072. int wi;
  5073. int first_line = p_line - num_lines_from_rows(p_line, p_wrap_index, -visible_rows / 2, wi) + 1;
  5074. set_v_scroll(get_scroll_pos_for_line(first_line, wi));
  5075. }
  5076. void TextEdit::set_line_as_last_visible(int p_line, int p_wrap_index) {
  5077. int wi;
  5078. int first_line = p_line - num_lines_from_rows(p_line, p_wrap_index, -get_visible_rows() - 1, wi) + 1;
  5079. set_v_scroll(get_scroll_pos_for_line(first_line, wi) + get_visible_rows_offset());
  5080. }
  5081. int TextEdit::get_first_visible_line() const {
  5082. return CLAMP(cursor.line_ofs, 0, text.size() - 1);
  5083. }
  5084. int TextEdit::get_last_visible_line() const {
  5085. int first_vis_line = get_first_visible_line();
  5086. int last_vis_line = 0;
  5087. int wi;
  5088. last_vis_line = first_vis_line + num_lines_from_rows(first_vis_line, cursor.wrap_ofs, get_visible_rows() + 1, wi) - 1;
  5089. last_vis_line = CLAMP(last_vis_line, 0, text.size() - 1);
  5090. return last_vis_line;
  5091. }
  5092. int TextEdit::get_last_visible_line_wrap_index() const {
  5093. int first_vis_line = get_first_visible_line();
  5094. int wi;
  5095. num_lines_from_rows(first_vis_line, cursor.wrap_ofs, get_visible_rows() + 1, wi);
  5096. return wi;
  5097. }
  5098. double TextEdit::get_visible_rows_offset() const {
  5099. double total = _get_control_height();
  5100. total /= (double)get_row_height();
  5101. total = total - floor(total);
  5102. total = -CLAMP(total, 0.001, 1) + 1;
  5103. return total;
  5104. }
  5105. double TextEdit::get_v_scroll_offset() const {
  5106. double val = get_v_scroll() - floor(get_v_scroll());
  5107. return CLAMP(val, 0, 1);
  5108. }
  5109. double TextEdit::get_v_scroll() const {
  5110. return v_scroll->get_value();
  5111. }
  5112. void TextEdit::set_v_scroll(double p_scroll) {
  5113. v_scroll->set_value(p_scroll);
  5114. int max_v_scroll = v_scroll->get_max() - v_scroll->get_page();
  5115. if (p_scroll >= max_v_scroll - 1.0)
  5116. _scroll_moved(v_scroll->get_value());
  5117. }
  5118. int TextEdit::get_h_scroll() const {
  5119. return h_scroll->get_value();
  5120. }
  5121. void TextEdit::set_h_scroll(int p_scroll) {
  5122. if (p_scroll < 0) {
  5123. p_scroll = 0;
  5124. }
  5125. h_scroll->set_value(p_scroll);
  5126. }
  5127. void TextEdit::set_smooth_scroll_enabled(bool p_enable) {
  5128. v_scroll->set_smooth_scroll_enabled(p_enable);
  5129. smooth_scroll_enabled = p_enable;
  5130. }
  5131. bool TextEdit::is_smooth_scroll_enabled() const {
  5132. return smooth_scroll_enabled;
  5133. }
  5134. void TextEdit::set_v_scroll_speed(float p_speed) {
  5135. v_scroll_speed = p_speed;
  5136. }
  5137. float TextEdit::get_v_scroll_speed() const {
  5138. return v_scroll_speed;
  5139. }
  5140. void TextEdit::set_completion(bool p_enabled, const Vector<String> &p_prefixes) {
  5141. completion_prefixes.clear();
  5142. completion_enabled = p_enabled;
  5143. for (int i = 0; i < p_prefixes.size(); i++)
  5144. completion_prefixes.insert(p_prefixes[i]);
  5145. }
  5146. void TextEdit::_confirm_completion() {
  5147. begin_complex_operation();
  5148. _remove_text(cursor.line, cursor.column - completion_base.length(), cursor.line, cursor.column);
  5149. cursor_set_column(cursor.column - completion_base.length(), false);
  5150. insert_text_at_cursor(completion_current.insert_text);
  5151. // When inserted into the middle of an existing string/method, don't add an unnecessary quote/bracket.
  5152. String line = text[cursor.line];
  5153. CharType next_char = line[cursor.column];
  5154. CharType last_completion_char = completion_current.insert_text[completion_current.insert_text.length() - 1];
  5155. CharType last_completion_char_display = completion_current.display[completion_current.display.length() - 1];
  5156. if ((last_completion_char == '"' || last_completion_char == '\'') && (last_completion_char == next_char || last_completion_char_display == next_char)) {
  5157. _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
  5158. }
  5159. if (last_completion_char == '(') {
  5160. if (next_char == last_completion_char) {
  5161. _base_remove_text(cursor.line, cursor.column - 1, cursor.line, cursor.column);
  5162. } else if (auto_brace_completion_enabled) {
  5163. insert_text_at_cursor(")");
  5164. cursor.column--;
  5165. }
  5166. } else if (last_completion_char == ')' && next_char == '(') {
  5167. _base_remove_text(cursor.line, cursor.column - 2, cursor.line, cursor.column);
  5168. if (line[cursor.column + 1] != ')') {
  5169. cursor.column--;
  5170. }
  5171. }
  5172. end_complex_operation();
  5173. _cancel_completion();
  5174. if (last_completion_char == '(') {
  5175. query_code_comple();
  5176. }
  5177. }
  5178. void TextEdit::_cancel_code_hint() {
  5179. completion_hint = "";
  5180. update();
  5181. }
  5182. void TextEdit::_cancel_completion() {
  5183. if (!completion_active)
  5184. return;
  5185. completion_active = false;
  5186. completion_forced = false;
  5187. update();
  5188. }
  5189. static bool _is_completable(CharType c) {
  5190. return !_is_symbol(c) || c == '"' || c == '\'';
  5191. }
  5192. void TextEdit::_update_completion_candidates() {
  5193. String l = text[cursor.line];
  5194. int cofs = CLAMP(cursor.column, 0, l.length());
  5195. String s;
  5196. // Look for keywords first.
  5197. bool inquote = false;
  5198. int first_quote = -1;
  5199. int restore_quotes = -1;
  5200. int c = cofs - 1;
  5201. while (c >= 0) {
  5202. if (l[c] == '"' || l[c] == '\'') {
  5203. inquote = !inquote;
  5204. if (first_quote == -1)
  5205. first_quote = c;
  5206. restore_quotes = 0;
  5207. } else if (restore_quotes == 0 && l[c] == '$') {
  5208. restore_quotes = 1;
  5209. } else if (restore_quotes == 0 && !_is_whitespace(l[c])) {
  5210. restore_quotes = -1;
  5211. }
  5212. c--;
  5213. }
  5214. bool pre_keyword = false;
  5215. bool cancel = false;
  5216. if (!inquote && first_quote == cofs - 1) {
  5217. // No completion here.
  5218. cancel = true;
  5219. } else if (inquote && first_quote != -1) {
  5220. s = l.substr(first_quote, cofs - first_quote);
  5221. } else if (cofs > 0 && l[cofs - 1] == ' ') {
  5222. int kofs = cofs - 1;
  5223. String kw;
  5224. while (kofs >= 0 && l[kofs] == ' ')
  5225. kofs--;
  5226. while (kofs >= 0 && l[kofs] > 32 && _is_completable(l[kofs])) {
  5227. kw = String::chr(l[kofs]) + kw;
  5228. kofs--;
  5229. }
  5230. pre_keyword = keywords.has(kw);
  5231. } else {
  5232. while (cofs > 0 && l[cofs - 1] > 32 && (l[cofs - 1] == '/' || _is_completable(l[cofs - 1]))) {
  5233. s = String::chr(l[cofs - 1]) + s;
  5234. if (l[cofs - 1] == '\'' || l[cofs - 1] == '"' || l[cofs - 1] == '$')
  5235. break;
  5236. cofs--;
  5237. }
  5238. }
  5239. if (cursor.column > 0 && l[cursor.column - 1] == '(' && !pre_keyword && !completion_forced) {
  5240. cancel = true;
  5241. }
  5242. update();
  5243. bool prev_is_prefix = false;
  5244. if (cofs > 0 && completion_prefixes.has(String::chr(l[cofs - 1])))
  5245. prev_is_prefix = true;
  5246. // Check with one space before prefix, to allow indent.
  5247. if (cofs > 1 && l[cofs - 1] == ' ' && completion_prefixes.has(String::chr(l[cofs - 2])))
  5248. prev_is_prefix = true;
  5249. if (cancel || (!pre_keyword && s == "" && (cofs == 0 || !prev_is_prefix))) {
  5250. // None to complete, cancel.
  5251. _cancel_completion();
  5252. return;
  5253. }
  5254. completion_options.clear();
  5255. completion_index = 0;
  5256. completion_base = s;
  5257. Vector<float> sim_cache;
  5258. bool single_quote = s.begins_with("'");
  5259. Vector<ScriptCodeCompletionOption> completion_options_casei;
  5260. for (List<ScriptCodeCompletionOption>::Element *E = completion_sources.front(); E; E = E->next()) {
  5261. ScriptCodeCompletionOption &option = E->get();
  5262. if (single_quote && option.display.is_quoted()) {
  5263. option.display = option.display.unquote().quote("'");
  5264. }
  5265. if (inquote && restore_quotes == 1 && !option.display.is_quoted()) {
  5266. String quote = single_quote ? "'" : "\"";
  5267. option.display = option.display.quote(quote);
  5268. option.insert_text = option.insert_text.quote(quote);
  5269. }
  5270. if (option.display.begins_with(s)) {
  5271. completion_options.push_back(option);
  5272. } else if (option.display.to_lower().begins_with(s.to_lower())) {
  5273. completion_options_casei.push_back(option);
  5274. }
  5275. }
  5276. completion_options.append_array(completion_options_casei);
  5277. if (completion_options.size() == 0) {
  5278. for (int i = 0; i < completion_sources.size(); i++) {
  5279. if (s.is_subsequence_of(completion_sources[i].display)) {
  5280. completion_options.push_back(completion_sources[i]);
  5281. }
  5282. }
  5283. }
  5284. if (completion_options.size() == 0) {
  5285. for (int i = 0; i < completion_sources.size(); i++) {
  5286. if (s.is_subsequence_ofi(completion_sources[i].display)) {
  5287. completion_options.push_back(completion_sources[i]);
  5288. }
  5289. }
  5290. }
  5291. if (completion_options.size() == 0) {
  5292. // No options to complete, cancel.
  5293. _cancel_completion();
  5294. return;
  5295. }
  5296. if (completion_options.size() == 1 && s == completion_options[0].display) {
  5297. // A perfect match, stop completion.
  5298. _cancel_completion();
  5299. return;
  5300. }
  5301. // The top of the list is the best match.
  5302. completion_current = completion_options[0];
  5303. completion_enabled = true;
  5304. }
  5305. void TextEdit::query_code_comple() {
  5306. String l = text[cursor.line];
  5307. int ofs = CLAMP(cursor.column, 0, l.length());
  5308. bool inquote = false;
  5309. int c = ofs - 1;
  5310. while (c >= 0) {
  5311. if (l[c] == '"' || l[c] == '\'')
  5312. inquote = !inquote;
  5313. c--;
  5314. }
  5315. bool ignored = completion_active && !completion_options.empty();
  5316. if (ignored) {
  5317. ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_PLAIN_TEXT;
  5318. const ScriptCodeCompletionOption *previous_option = NULL;
  5319. for (int i = 0; i < completion_options.size(); i++) {
  5320. const ScriptCodeCompletionOption &current_option = completion_options[i];
  5321. if (!previous_option) {
  5322. previous_option = &current_option;
  5323. kind = current_option.kind;
  5324. }
  5325. if (previous_option->kind != current_option.kind) {
  5326. ignored = false;
  5327. break;
  5328. }
  5329. }
  5330. ignored = ignored && (kind == ScriptCodeCompletionOption::KIND_FILE_PATH || kind == ScriptCodeCompletionOption::KIND_NODE_PATH || kind == ScriptCodeCompletionOption::KIND_SIGNAL);
  5331. }
  5332. if (!ignored) {
  5333. if (ofs > 0 && (inquote || _is_completable(l[ofs - 1]) || completion_prefixes.has(String::chr(l[ofs - 1]))))
  5334. emit_signal("request_completion");
  5335. 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.
  5336. emit_signal("request_completion");
  5337. }
  5338. }
  5339. void TextEdit::set_code_hint(const String &p_hint) {
  5340. completion_hint = p_hint;
  5341. completion_hint_offset = -0xFFFF;
  5342. update();
  5343. }
  5344. void TextEdit::code_complete(const List<ScriptCodeCompletionOption> &p_strings, bool p_forced) {
  5345. completion_sources = p_strings;
  5346. completion_active = true;
  5347. completion_forced = p_forced;
  5348. completion_current = ScriptCodeCompletionOption();
  5349. completion_index = 0;
  5350. _update_completion_candidates();
  5351. }
  5352. String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
  5353. int row, col;
  5354. _get_mouse_pos(p_pos, row, col);
  5355. String s = text[row];
  5356. if (s.length() == 0)
  5357. return "";
  5358. int beg, end;
  5359. if (select_word(s, col, beg, end)) {
  5360. bool inside_quotes = false;
  5361. CharType selected_quote = '\0';
  5362. int qbegin = 0, qend = 0;
  5363. for (int i = 0; i < s.length(); i++) {
  5364. if (s[i] == '"' || s[i] == '\'') {
  5365. if (i == 0 || s[i - 1] != '\\') {
  5366. if (inside_quotes && selected_quote == s[i]) {
  5367. qend = i;
  5368. inside_quotes = false;
  5369. selected_quote = '\0';
  5370. if (col >= qbegin && col <= qend) {
  5371. return s.substr(qbegin, qend - qbegin);
  5372. }
  5373. } else if (!inside_quotes) {
  5374. qbegin = i + 1;
  5375. inside_quotes = true;
  5376. selected_quote = s[i];
  5377. }
  5378. }
  5379. }
  5380. }
  5381. return s.substr(beg, end - beg);
  5382. }
  5383. return String();
  5384. }
  5385. String TextEdit::get_tooltip(const Point2 &p_pos) const {
  5386. if (!tooltip_obj)
  5387. return Control::get_tooltip(p_pos);
  5388. int row, col;
  5389. _get_mouse_pos(p_pos, row, col);
  5390. String s = text[row];
  5391. if (s.length() == 0)
  5392. return Control::get_tooltip(p_pos);
  5393. int beg, end;
  5394. if (select_word(s, col, beg, end)) {
  5395. String tt = tooltip_obj->call(tooltip_func, s.substr(beg, end - beg), tooltip_ud);
  5396. return tt;
  5397. }
  5398. return Control::get_tooltip(p_pos);
  5399. }
  5400. void TextEdit::set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata) {
  5401. tooltip_obj = p_obj;
  5402. tooltip_func = p_function;
  5403. tooltip_ud = p_udata;
  5404. }
  5405. void TextEdit::set_line(int line, String new_text) {
  5406. if (line < 0 || line > text.size())
  5407. return;
  5408. _remove_text(line, 0, line, text[line].length());
  5409. _insert_text(line, 0, new_text);
  5410. if (cursor.line == line) {
  5411. cursor.column = MIN(cursor.column, new_text.length());
  5412. }
  5413. if (is_selection_active() && line == selection.to_line && selection.to_column > text[line].length()) {
  5414. selection.to_column = text[line].length();
  5415. }
  5416. }
  5417. void TextEdit::insert_at(const String &p_text, int at) {
  5418. _insert_text(at, 0, p_text + "\n");
  5419. if (cursor.line >= at) {
  5420. // offset cursor when located after inserted line
  5421. ++cursor.line;
  5422. }
  5423. if (is_selection_active()) {
  5424. if (selection.from_line >= at) {
  5425. // offset selection when located after inserted line
  5426. ++selection.from_line;
  5427. ++selection.to_line;
  5428. } else if (selection.to_line >= at) {
  5429. // extend selection that includes inserted line
  5430. ++selection.to_line;
  5431. }
  5432. }
  5433. }
  5434. void TextEdit::set_show_line_numbers(bool p_show) {
  5435. line_numbers = p_show;
  5436. update();
  5437. }
  5438. void TextEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
  5439. line_numbers_zero_padded = p_zero_padded;
  5440. update();
  5441. }
  5442. bool TextEdit::is_show_line_numbers_enabled() const {
  5443. return line_numbers;
  5444. }
  5445. void TextEdit::set_show_line_length_guideline(bool p_show) {
  5446. line_length_guideline = p_show;
  5447. update();
  5448. }
  5449. void TextEdit::set_line_length_guideline_column(int p_column) {
  5450. line_length_guideline_col = p_column;
  5451. update();
  5452. }
  5453. void TextEdit::set_bookmark_gutter_enabled(bool p_draw) {
  5454. draw_bookmark_gutter = p_draw;
  5455. update();
  5456. }
  5457. bool TextEdit::is_bookmark_gutter_enabled() const {
  5458. return draw_bookmark_gutter;
  5459. }
  5460. void TextEdit::set_breakpoint_gutter_enabled(bool p_draw) {
  5461. draw_breakpoint_gutter = p_draw;
  5462. update();
  5463. }
  5464. bool TextEdit::is_breakpoint_gutter_enabled() const {
  5465. return draw_breakpoint_gutter;
  5466. }
  5467. void TextEdit::set_breakpoint_gutter_width(int p_gutter_width) {
  5468. breakpoint_gutter_width = p_gutter_width;
  5469. update();
  5470. }
  5471. int TextEdit::get_breakpoint_gutter_width() const {
  5472. return cache.breakpoint_gutter_width;
  5473. }
  5474. void TextEdit::set_draw_fold_gutter(bool p_draw) {
  5475. draw_fold_gutter = p_draw;
  5476. update();
  5477. }
  5478. bool TextEdit::is_drawing_fold_gutter() const {
  5479. return draw_fold_gutter;
  5480. }
  5481. void TextEdit::set_fold_gutter_width(int p_gutter_width) {
  5482. fold_gutter_width = p_gutter_width;
  5483. update();
  5484. }
  5485. int TextEdit::get_fold_gutter_width() const {
  5486. return cache.fold_gutter_width;
  5487. }
  5488. void TextEdit::set_draw_info_gutter(bool p_draw) {
  5489. draw_info_gutter = p_draw;
  5490. update();
  5491. }
  5492. bool TextEdit::is_drawing_info_gutter() const {
  5493. return draw_info_gutter;
  5494. }
  5495. void TextEdit::set_info_gutter_width(int p_gutter_width) {
  5496. info_gutter_width = p_gutter_width;
  5497. update();
  5498. }
  5499. int TextEdit::get_info_gutter_width() const {
  5500. return info_gutter_width;
  5501. }
  5502. void TextEdit::set_draw_minimap(bool p_draw) {
  5503. draw_minimap = p_draw;
  5504. update();
  5505. }
  5506. bool TextEdit::is_drawing_minimap() const {
  5507. return draw_minimap;
  5508. }
  5509. void TextEdit::set_minimap_width(int p_minimap_width) {
  5510. minimap_width = p_minimap_width;
  5511. update();
  5512. }
  5513. int TextEdit::get_minimap_width() const {
  5514. return minimap_width;
  5515. }
  5516. void TextEdit::set_hiding_enabled(bool p_enabled) {
  5517. if (!p_enabled)
  5518. unhide_all_lines();
  5519. hiding_enabled = p_enabled;
  5520. update();
  5521. }
  5522. bool TextEdit::is_hiding_enabled() const {
  5523. return hiding_enabled;
  5524. }
  5525. void TextEdit::set_highlight_current_line(bool p_enabled) {
  5526. highlight_current_line = p_enabled;
  5527. update();
  5528. }
  5529. bool TextEdit::is_highlight_current_line_enabled() const {
  5530. return highlight_current_line;
  5531. }
  5532. bool TextEdit::is_text_field() const {
  5533. return true;
  5534. }
  5535. void TextEdit::menu_option(int p_option) {
  5536. switch (p_option) {
  5537. case MENU_CUT: {
  5538. if (!readonly) {
  5539. cut();
  5540. }
  5541. } break;
  5542. case MENU_COPY: {
  5543. copy();
  5544. } break;
  5545. case MENU_PASTE: {
  5546. if (!readonly) {
  5547. paste();
  5548. }
  5549. } break;
  5550. case MENU_CLEAR: {
  5551. if (!readonly) {
  5552. clear();
  5553. }
  5554. } break;
  5555. case MENU_SELECT_ALL: {
  5556. select_all();
  5557. } break;
  5558. case MENU_UNDO: {
  5559. undo();
  5560. } break;
  5561. case MENU_REDO: {
  5562. redo();
  5563. }
  5564. }
  5565. }
  5566. void TextEdit::set_select_identifiers_on_hover(bool p_enable) {
  5567. select_identifiers_enabled = p_enable;
  5568. }
  5569. bool TextEdit::is_selecting_identifiers_on_hover_enabled() const {
  5570. return select_identifiers_enabled;
  5571. }
  5572. void TextEdit::set_context_menu_enabled(bool p_enable) {
  5573. context_menu_enabled = p_enable;
  5574. }
  5575. bool TextEdit::is_context_menu_enabled() {
  5576. return context_menu_enabled;
  5577. }
  5578. void TextEdit::set_shortcut_keys_enabled(bool p_enabled) {
  5579. shortcut_keys_enabled = p_enabled;
  5580. _generate_context_menu();
  5581. }
  5582. void TextEdit::set_selecting_enabled(bool p_enabled) {
  5583. selecting_enabled = p_enabled;
  5584. if (!selecting_enabled)
  5585. deselect();
  5586. _generate_context_menu();
  5587. }
  5588. bool TextEdit::is_selecting_enabled() const {
  5589. return selecting_enabled;
  5590. }
  5591. bool TextEdit::is_shortcut_keys_enabled() const {
  5592. return shortcut_keys_enabled;
  5593. }
  5594. PopupMenu *TextEdit::get_menu() const {
  5595. return menu;
  5596. }
  5597. void TextEdit::_bind_methods() {
  5598. ClassDB::bind_method(D_METHOD("_gui_input"), &TextEdit::_gui_input);
  5599. ClassDB::bind_method(D_METHOD("_scroll_moved"), &TextEdit::_scroll_moved);
  5600. ClassDB::bind_method(D_METHOD("_cursor_changed_emit"), &TextEdit::_cursor_changed_emit);
  5601. ClassDB::bind_method(D_METHOD("_text_changed_emit"), &TextEdit::_text_changed_emit);
  5602. ClassDB::bind_method(D_METHOD("_push_current_op"), &TextEdit::_push_current_op);
  5603. ClassDB::bind_method(D_METHOD("_click_selection_held"), &TextEdit::_click_selection_held);
  5604. ClassDB::bind_method(D_METHOD("_toggle_draw_caret"), &TextEdit::_toggle_draw_caret);
  5605. ClassDB::bind_method(D_METHOD("_v_scroll_input"), &TextEdit::_v_scroll_input);
  5606. ClassDB::bind_method(D_METHOD("_update_wrap_at"), &TextEdit::_update_wrap_at);
  5607. BIND_ENUM_CONSTANT(SEARCH_MATCH_CASE);
  5608. BIND_ENUM_CONSTANT(SEARCH_WHOLE_WORDS);
  5609. BIND_ENUM_CONSTANT(SEARCH_BACKWARDS);
  5610. BIND_ENUM_CONSTANT(SEARCH_RESULT_COLUMN);
  5611. BIND_ENUM_CONSTANT(SEARCH_RESULT_LINE);
  5612. /*
  5613. ClassDB::bind_method(D_METHOD("delete_char"),&TextEdit::delete_char);
  5614. ClassDB::bind_method(D_METHOD("delete_line"),&TextEdit::delete_line);
  5615. */
  5616. ClassDB::bind_method(D_METHOD("set_text", "text"), &TextEdit::set_text);
  5617. ClassDB::bind_method(D_METHOD("insert_text_at_cursor", "text"), &TextEdit::insert_text_at_cursor);
  5618. ClassDB::bind_method(D_METHOD("get_line_count"), &TextEdit::get_line_count);
  5619. ClassDB::bind_method(D_METHOD("get_text"), &TextEdit::get_text);
  5620. ClassDB::bind_method(D_METHOD("get_line", "line"), &TextEdit::get_line);
  5621. ClassDB::bind_method(D_METHOD("center_viewport_to_cursor"), &TextEdit::center_viewport_to_cursor);
  5622. ClassDB::bind_method(D_METHOD("cursor_set_column", "column", "adjust_viewport"), &TextEdit::cursor_set_column, DEFVAL(true));
  5623. 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));
  5624. ClassDB::bind_method(D_METHOD("cursor_get_column"), &TextEdit::cursor_get_column);
  5625. ClassDB::bind_method(D_METHOD("cursor_get_line"), &TextEdit::cursor_get_line);
  5626. ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enable"), &TextEdit::cursor_set_blink_enabled);
  5627. ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"), &TextEdit::cursor_get_blink_enabled);
  5628. ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"), &TextEdit::cursor_set_blink_speed);
  5629. ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"), &TextEdit::cursor_get_blink_speed);
  5630. ClassDB::bind_method(D_METHOD("cursor_set_block_mode", "enable"), &TextEdit::cursor_set_block_mode);
  5631. ClassDB::bind_method(D_METHOD("cursor_is_block_mode"), &TextEdit::cursor_is_block_mode);
  5632. ClassDB::bind_method(D_METHOD("set_right_click_moves_caret", "enable"), &TextEdit::set_right_click_moves_caret);
  5633. ClassDB::bind_method(D_METHOD("is_right_click_moving_caret"), &TextEdit::is_right_click_moving_caret);
  5634. ClassDB::bind_method(D_METHOD("set_readonly", "enable"), &TextEdit::set_readonly);
  5635. ClassDB::bind_method(D_METHOD("is_readonly"), &TextEdit::is_readonly);
  5636. ClassDB::bind_method(D_METHOD("set_wrap_enabled", "enable"), &TextEdit::set_wrap_enabled);
  5637. ClassDB::bind_method(D_METHOD("is_wrap_enabled"), &TextEdit::is_wrap_enabled);
  5638. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &TextEdit::set_context_menu_enabled);
  5639. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &TextEdit::is_context_menu_enabled);
  5640. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &TextEdit::set_shortcut_keys_enabled);
  5641. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &TextEdit::is_shortcut_keys_enabled);
  5642. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &TextEdit::set_selecting_enabled);
  5643. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &TextEdit::is_selecting_enabled);
  5644. ClassDB::bind_method(D_METHOD("cut"), &TextEdit::cut);
  5645. ClassDB::bind_method(D_METHOD("copy"), &TextEdit::copy);
  5646. ClassDB::bind_method(D_METHOD("paste"), &TextEdit::paste);
  5647. ClassDB::bind_method(D_METHOD("select", "from_line", "from_column", "to_line", "to_column"), &TextEdit::select);
  5648. ClassDB::bind_method(D_METHOD("select_all"), &TextEdit::select_all);
  5649. ClassDB::bind_method(D_METHOD("deselect"), &TextEdit::deselect);
  5650. ClassDB::bind_method(D_METHOD("is_selection_active"), &TextEdit::is_selection_active);
  5651. ClassDB::bind_method(D_METHOD("get_selection_from_line"), &TextEdit::get_selection_from_line);
  5652. ClassDB::bind_method(D_METHOD("get_selection_from_column"), &TextEdit::get_selection_from_column);
  5653. ClassDB::bind_method(D_METHOD("get_selection_to_line"), &TextEdit::get_selection_to_line);
  5654. ClassDB::bind_method(D_METHOD("get_selection_to_column"), &TextEdit::get_selection_to_column);
  5655. ClassDB::bind_method(D_METHOD("get_selection_text"), &TextEdit::get_selection_text);
  5656. ClassDB::bind_method(D_METHOD("get_word_under_cursor"), &TextEdit::get_word_under_cursor);
  5657. ClassDB::bind_method(D_METHOD("search", "key", "flags", "from_line", "from_column"), &TextEdit::_search_bind);
  5658. ClassDB::bind_method(D_METHOD("undo"), &TextEdit::undo);
  5659. ClassDB::bind_method(D_METHOD("redo"), &TextEdit::redo);
  5660. ClassDB::bind_method(D_METHOD("clear_undo_history"), &TextEdit::clear_undo_history);
  5661. ClassDB::bind_method(D_METHOD("set_show_line_numbers", "enable"), &TextEdit::set_show_line_numbers);
  5662. ClassDB::bind_method(D_METHOD("is_show_line_numbers_enabled"), &TextEdit::is_show_line_numbers_enabled);
  5663. ClassDB::bind_method(D_METHOD("set_draw_tabs"), &TextEdit::set_draw_tabs);
  5664. ClassDB::bind_method(D_METHOD("is_drawing_tabs"), &TextEdit::is_drawing_tabs);
  5665. ClassDB::bind_method(D_METHOD("set_draw_spaces"), &TextEdit::set_draw_spaces);
  5666. ClassDB::bind_method(D_METHOD("is_drawing_spaces"), &TextEdit::is_drawing_spaces);
  5667. ClassDB::bind_method(D_METHOD("set_breakpoint_gutter_enabled", "enable"), &TextEdit::set_breakpoint_gutter_enabled);
  5668. ClassDB::bind_method(D_METHOD("is_breakpoint_gutter_enabled"), &TextEdit::is_breakpoint_gutter_enabled);
  5669. ClassDB::bind_method(D_METHOD("set_draw_fold_gutter"), &TextEdit::set_draw_fold_gutter);
  5670. ClassDB::bind_method(D_METHOD("is_drawing_fold_gutter"), &TextEdit::is_drawing_fold_gutter);
  5671. ClassDB::bind_method(D_METHOD("set_hiding_enabled", "enable"), &TextEdit::set_hiding_enabled);
  5672. ClassDB::bind_method(D_METHOD("is_hiding_enabled"), &TextEdit::is_hiding_enabled);
  5673. ClassDB::bind_method(D_METHOD("set_line_as_hidden", "line", "enable"), &TextEdit::set_line_as_hidden);
  5674. ClassDB::bind_method(D_METHOD("is_line_hidden", "line"), &TextEdit::is_line_hidden);
  5675. ClassDB::bind_method(D_METHOD("fold_all_lines"), &TextEdit::fold_all_lines);
  5676. ClassDB::bind_method(D_METHOD("unhide_all_lines"), &TextEdit::unhide_all_lines);
  5677. ClassDB::bind_method(D_METHOD("fold_line", "line"), &TextEdit::fold_line);
  5678. ClassDB::bind_method(D_METHOD("unfold_line", "line"), &TextEdit::unfold_line);
  5679. ClassDB::bind_method(D_METHOD("toggle_fold_line", "line"), &TextEdit::toggle_fold_line);
  5680. ClassDB::bind_method(D_METHOD("can_fold", "line"), &TextEdit::can_fold);
  5681. ClassDB::bind_method(D_METHOD("is_folded", "line"), &TextEdit::is_folded);
  5682. ClassDB::bind_method(D_METHOD("set_highlight_all_occurrences", "enable"), &TextEdit::set_highlight_all_occurrences);
  5683. ClassDB::bind_method(D_METHOD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled);
  5684. ClassDB::bind_method(D_METHOD("set_override_selected_font_color", "override"), &TextEdit::set_override_selected_font_color);
  5685. ClassDB::bind_method(D_METHOD("is_overriding_selected_font_color"), &TextEdit::is_overriding_selected_font_color);
  5686. ClassDB::bind_method(D_METHOD("set_syntax_coloring", "enable"), &TextEdit::set_syntax_coloring);
  5687. ClassDB::bind_method(D_METHOD("is_syntax_coloring_enabled"), &TextEdit::is_syntax_coloring_enabled);
  5688. ClassDB::bind_method(D_METHOD("set_highlight_current_line", "enabled"), &TextEdit::set_highlight_current_line);
  5689. ClassDB::bind_method(D_METHOD("is_highlight_current_line_enabled"), &TextEdit::is_highlight_current_line_enabled);
  5690. ClassDB::bind_method(D_METHOD("set_smooth_scroll_enable", "enable"), &TextEdit::set_smooth_scroll_enabled);
  5691. ClassDB::bind_method(D_METHOD("is_smooth_scroll_enabled"), &TextEdit::is_smooth_scroll_enabled);
  5692. ClassDB::bind_method(D_METHOD("set_v_scroll_speed", "speed"), &TextEdit::set_v_scroll_speed);
  5693. ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed);
  5694. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &TextEdit::set_v_scroll);
  5695. ClassDB::bind_method(D_METHOD("get_v_scroll"), &TextEdit::get_v_scroll);
  5696. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &TextEdit::set_h_scroll);
  5697. ClassDB::bind_method(D_METHOD("get_h_scroll"), &TextEdit::get_h_scroll);
  5698. ClassDB::bind_method(D_METHOD("add_keyword_color", "keyword", "color"), &TextEdit::add_keyword_color);
  5699. ClassDB::bind_method(D_METHOD("has_keyword_color", "keyword"), &TextEdit::has_keyword_color);
  5700. ClassDB::bind_method(D_METHOD("get_keyword_color", "keyword"), &TextEdit::get_keyword_color);
  5701. ClassDB::bind_method(D_METHOD("add_color_region", "begin_key", "end_key", "color", "line_only"), &TextEdit::add_color_region, DEFVAL(false));
  5702. ClassDB::bind_method(D_METHOD("clear_colors"), &TextEdit::clear_colors);
  5703. ClassDB::bind_method(D_METHOD("menu_option", "option"), &TextEdit::menu_option);
  5704. ClassDB::bind_method(D_METHOD("get_menu"), &TextEdit::get_menu);
  5705. ClassDB::bind_method(D_METHOD("get_breakpoints"), &TextEdit::get_breakpoints_array);
  5706. ClassDB::bind_method(D_METHOD("remove_breakpoints"), &TextEdit::remove_breakpoints);
  5707. ClassDB::bind_method(D_METHOD("draw_minimap", "draw"), &TextEdit::set_draw_minimap);
  5708. ClassDB::bind_method(D_METHOD("is_drawing_minimap"), &TextEdit::is_drawing_minimap);
  5709. ClassDB::bind_method(D_METHOD("set_minimap_width", "width"), &TextEdit::set_minimap_width);
  5710. ClassDB::bind_method(D_METHOD("get_minimap_width"), &TextEdit::get_minimap_width);
  5711. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  5712. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "readonly"), "set_readonly", "is_readonly");
  5713. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled");
  5714. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "syntax_highlighting"), "set_syntax_coloring", "is_syntax_coloring_enabled");
  5715. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_line_numbers"), "set_show_line_numbers", "is_show_line_numbers_enabled");
  5716. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_tabs"), "set_draw_tabs", "is_drawing_tabs");
  5717. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_spaces"), "set_draw_spaces", "is_drawing_spaces");
  5718. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "breakpoint_gutter"), "set_breakpoint_gutter_enabled", "is_breakpoint_gutter_enabled");
  5719. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fold_gutter"), "set_draw_fold_gutter", "is_drawing_fold_gutter");
  5720. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled");
  5721. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_selected_font_color"), "set_override_selected_font_color", "is_overriding_selected_font_color");
  5722. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  5723. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  5724. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  5725. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_scrolling"), "set_smooth_scroll_enable", "is_smooth_scroll_enabled");
  5726. ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_scroll_speed"), "set_v_scroll_speed", "get_v_scroll_speed");
  5727. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hiding_enabled"), "set_hiding_enabled", "is_hiding_enabled");
  5728. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_enabled"), "set_wrap_enabled", "is_wrap_enabled");
  5729. ADD_PROPERTY(PropertyInfo(Variant::REAL, "scroll_vertical"), "set_v_scroll", "get_v_scroll");
  5730. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal"), "set_h_scroll", "get_h_scroll");
  5731. ADD_GROUP("Minimap", "minimap_");
  5732. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw"), "draw_minimap", "is_drawing_minimap");
  5733. ADD_PROPERTY(PropertyInfo(Variant::INT, "minimap_width"), "set_minimap_width", "get_minimap_width");
  5734. ADD_GROUP("Caret", "caret_");
  5735. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_block_mode"), "cursor_set_block_mode", "cursor_is_block_mode");
  5736. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "cursor_set_blink_enabled", "cursor_get_blink_enabled");
  5737. ADD_PROPERTY(PropertyInfo(Variant::REAL, "caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.01"), "cursor_set_blink_speed", "cursor_get_blink_speed");
  5738. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_moving_by_right_click"), "set_right_click_moves_caret", "is_right_click_moving_caret");
  5739. ADD_SIGNAL(MethodInfo("cursor_changed"));
  5740. ADD_SIGNAL(MethodInfo("text_changed"));
  5741. ADD_SIGNAL(MethodInfo("request_completion"));
  5742. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "row")));
  5743. ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "row"), PropertyInfo(Variant::INT, "column")));
  5744. ADD_SIGNAL(MethodInfo("info_clicked", PropertyInfo(Variant::INT, "row"), PropertyInfo(Variant::STRING, "info")));
  5745. BIND_ENUM_CONSTANT(MENU_CUT);
  5746. BIND_ENUM_CONSTANT(MENU_COPY);
  5747. BIND_ENUM_CONSTANT(MENU_PASTE);
  5748. BIND_ENUM_CONSTANT(MENU_CLEAR);
  5749. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  5750. BIND_ENUM_CONSTANT(MENU_UNDO);
  5751. BIND_ENUM_CONSTANT(MENU_REDO);
  5752. BIND_ENUM_CONSTANT(MENU_MAX);
  5753. GLOBAL_DEF("gui/timers/text_edit_idle_detect_sec", 3);
  5754. 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.
  5755. }
  5756. TextEdit::TextEdit() {
  5757. setting_row = false;
  5758. draw_tabs = false;
  5759. draw_spaces = false;
  5760. override_selected_font_color = false;
  5761. draw_caret = true;
  5762. max_chars = 0;
  5763. clear();
  5764. wrap_enabled = false;
  5765. wrap_at = 0;
  5766. wrap_right_offset = 10;
  5767. set_focus_mode(FOCUS_ALL);
  5768. syntax_highlighter = NULL;
  5769. _update_caches();
  5770. cache.row_height = 1;
  5771. cache.line_spacing = 1;
  5772. cache.line_number_w = 1;
  5773. cache.breakpoint_gutter_width = 0;
  5774. breakpoint_gutter_width = 0;
  5775. cache.fold_gutter_width = 0;
  5776. fold_gutter_width = 0;
  5777. info_gutter_width = 0;
  5778. cache.info_gutter_width = 0;
  5779. set_default_cursor_shape(CURSOR_IBEAM);
  5780. indent_size = 4;
  5781. text.set_indent_size(indent_size);
  5782. text.clear();
  5783. text.set_color_regions(&color_regions);
  5784. h_scroll = memnew(HScrollBar);
  5785. v_scroll = memnew(VScrollBar);
  5786. add_child(h_scroll);
  5787. add_child(v_scroll);
  5788. updating_scrolls = false;
  5789. selection.active = false;
  5790. h_scroll->connect("value_changed", this, "_scroll_moved");
  5791. v_scroll->connect("value_changed", this, "_scroll_moved");
  5792. v_scroll->connect("scrolling", this, "_v_scroll_input");
  5793. cursor_changed_dirty = false;
  5794. text_changed_dirty = false;
  5795. selection.selecting_mode = Selection::MODE_NONE;
  5796. selection.selecting_line = 0;
  5797. selection.selecting_column = 0;
  5798. selection.selecting_text = false;
  5799. selection.active = false;
  5800. syntax_coloring = false;
  5801. block_caret = false;
  5802. caret_blink_enabled = false;
  5803. caret_blink_timer = memnew(Timer);
  5804. add_child(caret_blink_timer);
  5805. caret_blink_timer->set_wait_time(0.65);
  5806. caret_blink_timer->connect("timeout", this, "_toggle_draw_caret");
  5807. cursor_set_blink_enabled(false);
  5808. right_click_moves_caret = true;
  5809. idle_detect = memnew(Timer);
  5810. add_child(idle_detect);
  5811. idle_detect->set_one_shot(true);
  5812. idle_detect->set_wait_time(GLOBAL_GET("gui/timers/text_edit_idle_detect_sec"));
  5813. idle_detect->connect("timeout", this, "_push_current_op");
  5814. click_select_held = memnew(Timer);
  5815. add_child(click_select_held);
  5816. click_select_held->set_wait_time(0.05);
  5817. click_select_held->connect("timeout", this, "_click_selection_held");
  5818. current_op.type = TextOperation::TYPE_NONE;
  5819. undo_enabled = true;
  5820. undo_stack_pos = NULL;
  5821. setting_text = false;
  5822. last_dblclk = 0;
  5823. current_op.version = 0;
  5824. version = 0;
  5825. saved_version = 0;
  5826. completion_enabled = false;
  5827. completion_active = false;
  5828. completion_line_ofs = 0;
  5829. tooltip_obj = NULL;
  5830. line_numbers = false;
  5831. line_numbers_zero_padded = false;
  5832. line_length_guideline = false;
  5833. line_length_guideline_col = 80;
  5834. draw_bookmark_gutter = false;
  5835. draw_breakpoint_gutter = false;
  5836. draw_fold_gutter = false;
  5837. draw_info_gutter = false;
  5838. hiding_enabled = false;
  5839. next_operation_is_complex = false;
  5840. scroll_past_end_of_file_enabled = false;
  5841. auto_brace_completion_enabled = false;
  5842. brace_matching_enabled = false;
  5843. highlight_all_occurrences = false;
  5844. highlight_current_line = false;
  5845. indent_using_spaces = false;
  5846. space_indent = " ";
  5847. auto_indent = false;
  5848. insert_mode = false;
  5849. window_has_focus = true;
  5850. select_identifiers_enabled = false;
  5851. smooth_scroll_enabled = false;
  5852. scrolling = false;
  5853. minimap_clicked = false;
  5854. dragging_minimap = false;
  5855. can_drag_minimap = false;
  5856. minimap_scroll_ratio = 0;
  5857. minimap_scroll_click_pos = 0;
  5858. dragging_selection = false;
  5859. target_v_scroll = 0;
  5860. v_scroll_speed = 80;
  5861. draw_minimap = false;
  5862. minimap_width = 80;
  5863. minimap_char_size = Point2(1, 2);
  5864. minimap_line_spacing = 1;
  5865. selecting_enabled = true;
  5866. context_menu_enabled = true;
  5867. shortcut_keys_enabled = true;
  5868. menu = memnew(PopupMenu);
  5869. add_child(menu);
  5870. readonly = true; // Initialise to opposite first, so we get past the early-out in set_readonly.
  5871. set_readonly(false);
  5872. menu->connect("id_pressed", this, "menu_option");
  5873. first_draw = true;
  5874. executing_line = -1;
  5875. }
  5876. TextEdit::~TextEdit() {
  5877. }
  5878. ///////////////////////////////////////////////////////////////////////////////
  5879. Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int p_line) {
  5880. if (syntax_highlighting_cache.has(p_line)) {
  5881. return syntax_highlighting_cache[p_line];
  5882. }
  5883. if (syntax_highlighter != NULL) {
  5884. Map<int, HighlighterInfo> color_map = syntax_highlighter->_get_line_syntax_highlighting(p_line);
  5885. syntax_highlighting_cache[p_line] = color_map;
  5886. return color_map;
  5887. }
  5888. Map<int, HighlighterInfo> color_map;
  5889. bool prev_is_char = false;
  5890. bool prev_is_number = false;
  5891. bool in_keyword = false;
  5892. bool in_word = false;
  5893. bool in_function_name = false;
  5894. bool in_member_variable = false;
  5895. bool is_hex_notation = false;
  5896. Color keyword_color;
  5897. Color color;
  5898. int in_region = _is_line_in_region(p_line);
  5899. int deregion = 0;
  5900. const Map<int, TextEdit::Text::ColorRegionInfo> cri_map = text.get_color_region_info(p_line);
  5901. const String &str = text[p_line];
  5902. Color prev_color;
  5903. for (int j = 0; j < str.length(); j++) {
  5904. HighlighterInfo highlighter_info;
  5905. if (deregion > 0) {
  5906. deregion--;
  5907. if (deregion == 0) {
  5908. in_region = -1;
  5909. }
  5910. }
  5911. if (deregion != 0) {
  5912. if (color != prev_color) {
  5913. prev_color = color;
  5914. highlighter_info.color = color;
  5915. color_map[j] = highlighter_info;
  5916. }
  5917. continue;
  5918. }
  5919. color = cache.font_color;
  5920. bool is_char = _is_text_char(str[j]);
  5921. bool is_symbol = _is_symbol(str[j]);
  5922. bool is_number = _is_number(str[j]);
  5923. // Allow ABCDEF in hex notation.
  5924. if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
  5925. is_number = true;
  5926. } else {
  5927. is_hex_notation = false;
  5928. }
  5929. // Check for dot or underscore or 'x' for hex notation in floating point number or 'e' for scientific notation.
  5930. if ((str[j] == '.' || str[j] == 'x' || str[j] == '_' || str[j] == 'f' || str[j] == 'e') && !in_word && prev_is_number && !is_number) {
  5931. is_number = true;
  5932. is_symbol = false;
  5933. is_char = false;
  5934. if (str[j] == 'x' && str[j - 1] == '0') {
  5935. is_hex_notation = true;
  5936. }
  5937. }
  5938. if (!in_word && _is_char(str[j]) && !is_number) {
  5939. in_word = true;
  5940. }
  5941. if ((in_keyword || in_word) && !is_hex_notation) {
  5942. is_number = false;
  5943. }
  5944. if (is_symbol && str[j] != '.' && in_word) {
  5945. in_word = false;
  5946. }
  5947. if (is_symbol && cri_map.has(j)) {
  5948. const TextEdit::Text::ColorRegionInfo &cri = cri_map[j];
  5949. if (in_region == -1) {
  5950. if (!cri.end) {
  5951. in_region = cri.region;
  5952. }
  5953. } else if (in_region == cri.region && !color_regions[cri.region].line_only) { // Ignore otherwise.
  5954. if (cri.end || color_regions[cri.region].eq) {
  5955. deregion = color_regions[cri.region].eq ? color_regions[cri.region].begin_key.length() : color_regions[cri.region].end_key.length();
  5956. }
  5957. }
  5958. }
  5959. if (!is_char) {
  5960. in_keyword = false;
  5961. }
  5962. if (in_region == -1 && !in_keyword && is_char && !prev_is_char) {
  5963. int to = j;
  5964. while (to < str.length() && _is_text_char(str[to]))
  5965. to++;
  5966. uint32_t hash = String::hash(&str[j], to - j);
  5967. StrRange range(&str[j], to - j);
  5968. const Color *col = keywords.custom_getptr(range, hash);
  5969. if (!col) {
  5970. col = member_keywords.custom_getptr(range, hash);
  5971. if (col) {
  5972. for (int k = j - 1; k >= 0; k--) {
  5973. if (str[k] == '.') {
  5974. col = NULL; // Member indexing not allowed.
  5975. break;
  5976. } else if (str[k] > 32) {
  5977. break;
  5978. }
  5979. }
  5980. }
  5981. }
  5982. if (col) {
  5983. in_keyword = true;
  5984. keyword_color = *col;
  5985. }
  5986. }
  5987. if (!in_function_name && in_word && !in_keyword) {
  5988. int k = j;
  5989. while (k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  5990. k++;
  5991. }
  5992. // Check for space between name and bracket.
  5993. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  5994. k++;
  5995. }
  5996. if (str[k] == '(') {
  5997. in_function_name = true;
  5998. }
  5999. }
  6000. if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
  6001. int k = j;
  6002. while (k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  6003. k--;
  6004. }
  6005. if (str[k] == '.') {
  6006. in_member_variable = true;
  6007. }
  6008. }
  6009. if (is_symbol) {
  6010. in_function_name = false;
  6011. in_member_variable = false;
  6012. }
  6013. if (in_region >= 0)
  6014. color = color_regions[in_region].color;
  6015. else if (in_keyword)
  6016. color = keyword_color;
  6017. else if (in_member_variable)
  6018. color = cache.member_variable_color;
  6019. else if (in_function_name)
  6020. color = cache.function_color;
  6021. else if (is_symbol)
  6022. color = cache.symbol_color;
  6023. else if (is_number)
  6024. color = cache.number_color;
  6025. prev_is_char = is_char;
  6026. prev_is_number = is_number;
  6027. if (color != prev_color) {
  6028. prev_color = color;
  6029. highlighter_info.color = color;
  6030. color_map[j] = highlighter_info;
  6031. }
  6032. }
  6033. syntax_highlighting_cache[p_line] = color_map;
  6034. return color_map;
  6035. }
  6036. void SyntaxHighlighter::set_text_editor(TextEdit *p_text_editor) {
  6037. text_editor = p_text_editor;
  6038. }
  6039. TextEdit *SyntaxHighlighter::get_text_editor() {
  6040. return text_editor;
  6041. }