text_edit.cpp 182 KB

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