2
0

text_edit.cpp 222 KB

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