text_edit.cpp 178 KB

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