text_edit.cpp 177 KB

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