code_edit.cpp 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935
  1. /**************************************************************************/
  2. /* code_edit.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "code_edit.h"
  31. #include "code_edit.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/string/string_builder.h"
  35. #include "core/string/ustring.h"
  36. #include "scene/theme/theme_db.h"
  37. void CodeEdit::_apply_project_settings() {
  38. symbol_tooltip_timer->set_wait_time(GLOBAL_GET_CACHED(double, "gui/timers/tooltip_delay_sec"));
  39. }
  40. void CodeEdit::_notification(int p_what) {
  41. switch (p_what) {
  42. case NOTIFICATION_READY: {
  43. _apply_project_settings();
  44. #ifdef TOOLS_ENABLED
  45. if (Engine::get_singleton()->is_editor_hint()) {
  46. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &CodeEdit::_apply_project_settings));
  47. }
  48. #endif // TOOLS_ENABLED
  49. } break;
  50. case NOTIFICATION_THEME_CHANGED: {
  51. set_gutter_width(main_gutter, get_line_height());
  52. _update_line_number_gutter_width();
  53. set_gutter_width(fold_gutter, get_line_height() / 1.2);
  54. _clear_line_number_text_cache();
  55. } break;
  56. case NOTIFICATION_TRANSLATION_CHANGED:
  57. [[fallthrough]];
  58. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  59. [[fallthrough]];
  60. case NOTIFICATION_VISIBILITY_CHANGED: {
  61. // Avoid having many hidden text editors with unused cache filling up memory.
  62. _clear_line_number_text_cache();
  63. } break;
  64. case NOTIFICATION_DRAW: {
  65. RID ci = get_text_canvas_item();
  66. const bool caret_visible = is_caret_visible();
  67. const bool rtl = is_layout_rtl();
  68. const int row_height = get_line_height();
  69. if (caret_visible) {
  70. const bool draw_code_completion = code_completion_active && !code_completion_options.is_empty();
  71. const bool draw_code_hint = !code_hint.is_empty();
  72. /* Code hint */
  73. Size2 code_hint_minsize;
  74. if (draw_code_hint) {
  75. const int font_height = theme_cache.font->get_height(theme_cache.font_size);
  76. Vector<String> code_hint_lines = code_hint.split("\n");
  77. int line_count = code_hint_lines.size();
  78. int max_width = 0;
  79. for (int i = 0; i < line_count; i++) {
  80. max_width = MAX(max_width, theme_cache.font->get_string_size(code_hint_lines[i], HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x);
  81. }
  82. code_hint_minsize = theme_cache.code_hint_style->get_minimum_size() + Size2(max_width, line_count * font_height + (theme_cache.line_spacing * line_count - 1));
  83. int offset = theme_cache.font->get_string_size(code_hint_lines[0].substr(0, code_hint_lines[0].find(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
  84. if (code_hint_xpos == -0xFFFF) {
  85. code_hint_xpos = get_caret_draw_pos().x - offset;
  86. }
  87. Point2 hint_ofs = Vector2(code_hint_xpos, get_caret_draw_pos().y);
  88. if (code_hint_draw_below) {
  89. hint_ofs.y += theme_cache.line_spacing / 2.0f;
  90. } else {
  91. hint_ofs.y -= (code_hint_minsize.y + row_height) - theme_cache.line_spacing;
  92. }
  93. theme_cache.code_hint_style->draw(ci, Rect2(hint_ofs, code_hint_minsize));
  94. int yofs = 0;
  95. for (int i = 0; i < line_count; i++) {
  96. const String &line = code_hint_lines[i];
  97. int begin = 0;
  98. int end = 0;
  99. if (line.contains(String::chr(0xFFFF))) {
  100. begin = theme_cache.font->get_string_size(line.substr(0, line.find(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
  101. end = theme_cache.font->get_string_size(line.substr(0, line.rfind(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
  102. }
  103. Point2 round_ofs = hint_ofs + theme_cache.code_hint_style->get_offset() + Vector2(0, theme_cache.font->get_ascent(theme_cache.font_size) + font_height * i + yofs);
  104. round_ofs = round_ofs.round();
  105. theme_cache.font->draw_string(ci, round_ofs, line.remove_char(0xFFFF), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size, theme_cache.code_hint_color);
  106. if (end > 0) {
  107. // Draw an underline for the currently edited function parameter.
  108. const Vector2 b = hint_ofs + theme_cache.code_hint_style->get_offset() + Vector2(begin, font_height + font_height * i + yofs);
  109. RS::get_singleton()->canvas_item_add_line(ci, b, b + Vector2(end - begin, 0), theme_cache.code_hint_color, 2);
  110. // Draw a translucent text highlight as well.
  111. const Rect2 highlight_rect = Rect2(
  112. b - Vector2(0, font_height),
  113. Vector2(end - begin, font_height));
  114. RS::get_singleton()->canvas_item_add_rect(ci, highlight_rect, theme_cache.code_hint_color * Color(1, 1, 1, 0.2));
  115. }
  116. yofs += theme_cache.line_spacing;
  117. }
  118. }
  119. /* Code completion */
  120. if (draw_code_completion) {
  121. const int code_completion_options_count = code_completion_options.size();
  122. const int lines = MIN(code_completion_options_count, theme_cache.code_completion_max_lines);
  123. const Size2 icon_area_size(row_height, row_height);
  124. code_completion_rect.size.width = code_completion_longest_line + theme_cache.code_completion_icon_separation + icon_area_size.width + 2;
  125. code_completion_rect.size.height = lines * row_height;
  126. const Point2 caret_pos = get_caret_draw_pos();
  127. const int total_height = theme_cache.code_completion_style->get_minimum_size().y + code_completion_rect.size.height;
  128. int min_y = caret_pos.y - row_height;
  129. int max_y = caret_pos.y + row_height + total_height;
  130. if (draw_code_hint) {
  131. if (code_hint_draw_below) {
  132. max_y += code_hint_minsize.y;
  133. } else {
  134. min_y -= code_hint_minsize.y;
  135. }
  136. }
  137. const bool can_fit_completion_above = (min_y > total_height);
  138. const bool can_fit_completion_below = (max_y <= get_size().height);
  139. if (!can_fit_completion_below && can_fit_completion_above) {
  140. code_completion_rect.position.y = (caret_pos.y - total_height - row_height) + theme_cache.line_spacing;
  141. if (draw_code_hint && !code_hint_draw_below) {
  142. code_completion_rect.position.y -= code_hint_minsize.y;
  143. }
  144. } else {
  145. code_completion_rect.position.y = caret_pos.y + (theme_cache.line_spacing / 2.0f);
  146. if (draw_code_hint && code_hint_draw_below) {
  147. code_completion_rect.position.y += code_hint_minsize.y;
  148. }
  149. }
  150. const int scroll_width = code_completion_options_count > theme_cache.code_completion_max_lines ? theme_cache.code_completion_scroll_width : 0;
  151. const int code_completion_base_width = theme_cache.font->get_string_size(code_completion_base, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;
  152. if (caret_pos.x - code_completion_base_width + code_completion_rect.size.width + scroll_width > get_size().width) {
  153. code_completion_rect.position.x = get_size().width - code_completion_rect.size.width - scroll_width;
  154. } else {
  155. code_completion_rect.position.x = caret_pos.x - code_completion_base_width;
  156. }
  157. theme_cache.code_completion_style->draw(ci, Rect2(code_completion_rect.position - theme_cache.code_completion_style->get_offset(), code_completion_rect.size + theme_cache.code_completion_style->get_minimum_size() + Size2(scroll_width, 0)));
  158. if (theme_cache.code_completion_background_color.a > 0.01) {
  159. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(code_completion_rect.position, code_completion_rect.size + Size2(scroll_width, 0)), theme_cache.code_completion_background_color);
  160. }
  161. code_completion_scroll_rect.position = code_completion_rect.position + Vector2(code_completion_rect.size.width, 0);
  162. code_completion_scroll_rect.size = Vector2(scroll_width, code_completion_rect.size.height);
  163. code_completion_line_ofs = CLAMP((code_completion_force_item_center < 0 ? code_completion_current_selected : code_completion_force_item_center) - lines / 2, 0, code_completion_options_count - lines);
  164. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(code_completion_rect.position.x, code_completion_rect.position.y + (code_completion_current_selected - code_completion_line_ofs) * row_height), Size2(code_completion_rect.size.width, row_height)), theme_cache.code_completion_selected_color);
  165. for (int i = 0; i < lines; i++) {
  166. int l = code_completion_line_ofs + i;
  167. ERR_CONTINUE(l < 0 || l >= code_completion_options_count);
  168. Ref<TextLine> tl;
  169. tl.instantiate();
  170. tl->add_string(code_completion_options[l].display, theme_cache.font, theme_cache.font_size);
  171. int yofs = (row_height - tl->get_size().y) / 2;
  172. Point2 title_pos(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height + yofs);
  173. /* Draw completion icon if it is valid. */
  174. const Ref<Texture2D> &icon = code_completion_options[l].icon;
  175. Rect2 icon_area(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height, icon_area_size.width, icon_area_size.height);
  176. if (icon.is_valid()) {
  177. Size2 icon_size = icon_area.size * 0.7;
  178. icon->draw_rect(ci, Rect2(icon_area.position + (icon_area.size - icon_size) / 2, icon_size));
  179. }
  180. title_pos.x = icon_area.position.x + icon_area.size.width + theme_cache.code_completion_icon_separation;
  181. tl->set_width(code_completion_rect.size.width - (icon_area_size.x + theme_cache.code_completion_icon_separation));
  182. if (rtl) {
  183. if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
  184. RS::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(code_completion_rect.position.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value);
  185. }
  186. tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  187. } else {
  188. if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
  189. const Color color = code_completion_options[l].default_value;
  190. const Rect2 rect = Rect2(Point2(code_completion_rect.position.x + code_completion_rect.size.width - icon_area_size.x, icon_area.position.y), icon_area_size);
  191. if (color.a < 1.0) {
  192. theme_cache.completion_color_bg->draw_rect(ci, rect, true);
  193. }
  194. RS::get_singleton()->canvas_item_add_rect(ci, rect, color);
  195. }
  196. tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  197. }
  198. Point2 match_pos = Point2(code_completion_rect.position.x + icon_area_size.x + theme_cache.code_completion_icon_separation, code_completion_rect.position.y + i * row_height);
  199. for (int j = 0; j < code_completion_options[l].matches.size(); j++) {
  200. Pair<int, int> match_segment = code_completion_options[l].matches[j];
  201. int match_offset = theme_cache.font->get_string_size(code_completion_options[l].display.substr(0, match_segment.first), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;
  202. int match_len = theme_cache.font->get_string_size(code_completion_options[l].display.substr(match_segment.first, match_segment.second), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;
  203. RS::get_singleton()->canvas_item_add_rect(ci, Rect2(match_pos + Point2(match_offset, 0), Size2(match_len, row_height)), theme_cache.code_completion_existing_color);
  204. }
  205. tl->draw(ci, title_pos, code_completion_options[l].font_color);
  206. }
  207. /* Draw a small scroll rectangle to show a position in the options. */
  208. if (scroll_width) {
  209. Color scroll_color = is_code_completion_scroll_hovered || is_code_completion_scroll_pressed ? theme_cache.code_completion_scroll_hovered_color : theme_cache.code_completion_scroll_color;
  210. float r = (float)theme_cache.code_completion_max_lines / code_completion_options_count;
  211. float o = (float)code_completion_line_ofs / code_completion_options_count;
  212. RS::get_singleton()->canvas_item_add_rect(ci, Rect2(code_completion_rect.position.x + code_completion_rect.size.width, code_completion_rect.position.y + o * code_completion_rect.size.y, scroll_width, code_completion_rect.size.y * r), scroll_color);
  213. }
  214. }
  215. }
  216. } break;
  217. case NOTIFICATION_DRAG_BEGIN: {
  218. cancel_code_completion();
  219. } break;
  220. case NOTIFICATION_MOUSE_EXIT: {
  221. symbol_tooltip_timer->stop();
  222. } break;
  223. }
  224. }
  225. void CodeEdit::_draw_guidelines() {
  226. if (line_length_guideline_columns.is_empty()) {
  227. return;
  228. }
  229. RID ci = get_canvas_item();
  230. const Size2 size = get_size();
  231. const bool rtl = is_layout_rtl();
  232. const int xmargin_beg = theme_cache.style_normal->get_margin(SIDE_LEFT) + get_total_gutter_width();
  233. const int xmargin_end = size.width - theme_cache.style_normal->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0);
  234. for (int i = 0; i < line_length_guideline_columns.size(); i++) {
  235. const int column_pos = theme_cache.font->get_string_size(String("0").repeat((int)line_length_guideline_columns[i]), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
  236. const int xoffset = xmargin_beg + column_pos - get_h_scroll();
  237. if (xoffset > xmargin_beg && xoffset < xmargin_end) {
  238. Color guideline_color = (i == 0) ? theme_cache.line_length_guideline_color : theme_cache.line_length_guideline_color * Color(1, 1, 1, 0.5);
  239. if (rtl) {
  240. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(size.width - xoffset, 0), Point2(size.width - xoffset, size.height), guideline_color);
  241. continue;
  242. }
  243. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(xoffset, 0), Point2(xoffset, size.height), guideline_color);
  244. }
  245. }
  246. }
  247. void CodeEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
  248. Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  249. if (pan_gesture.is_valid() && code_completion_active && code_completion_rect.has_point(pan_gesture->get_position())) {
  250. const real_t delta = pan_gesture->get_delta().y;
  251. code_completion_pan_offset += delta;
  252. if (code_completion_pan_offset <= -1.0) {
  253. if (code_completion_current_selected > 0) {
  254. code_completion_current_selected--;
  255. code_completion_force_item_center = -1;
  256. queue_redraw();
  257. }
  258. code_completion_pan_offset = 0;
  259. } else if (code_completion_pan_offset >= +1.0) {
  260. if (code_completion_current_selected < code_completion_options.size() - 1) {
  261. code_completion_current_selected++;
  262. code_completion_force_item_center = -1;
  263. queue_redraw();
  264. }
  265. code_completion_pan_offset = 0;
  266. }
  267. accept_event();
  268. return;
  269. }
  270. Ref<InputEventMouseButton> mb = p_gui_input;
  271. if (mb.is_valid()) {
  272. // Ignore mouse clicks in IME input mode, let TextEdit handle it.
  273. if (has_ime_text()) {
  274. TextEdit::gui_input(p_gui_input);
  275. return;
  276. }
  277. if (is_code_completion_scroll_pressed && mb->get_button_index() == MouseButton::LEFT) {
  278. is_code_completion_scroll_pressed = false;
  279. accept_event();
  280. queue_redraw();
  281. return;
  282. }
  283. if (is_code_completion_drag_started && !mb->is_pressed()) {
  284. is_code_completion_drag_started = false;
  285. accept_event();
  286. queue_redraw();
  287. return;
  288. }
  289. if (code_completion_active && code_completion_rect.has_point(mb->get_position())) {
  290. if (!mb->is_pressed()) {
  291. accept_event();
  292. return;
  293. }
  294. is_code_completion_drag_started = true;
  295. switch (mb->get_button_index()) {
  296. case MouseButton::WHEEL_UP: {
  297. if (code_completion_current_selected > 0) {
  298. code_completion_current_selected--;
  299. code_completion_force_item_center = -1;
  300. code_completion_pan_offset = 0.0f;
  301. queue_redraw();
  302. }
  303. } break;
  304. case MouseButton::WHEEL_DOWN: {
  305. if (code_completion_current_selected < code_completion_options.size() - 1) {
  306. code_completion_current_selected++;
  307. code_completion_force_item_center = -1;
  308. code_completion_pan_offset = 0.0f;
  309. queue_redraw();
  310. }
  311. } break;
  312. case MouseButton::LEFT: {
  313. if (code_completion_force_item_center == -1) {
  314. code_completion_force_item_center = code_completion_current_selected;
  315. }
  316. code_completion_current_selected = CLAMP(code_completion_line_ofs + (mb->get_position().y - code_completion_rect.position.y) / get_line_height(), 0, code_completion_options.size() - 1);
  317. code_completion_pan_offset = 0.0f;
  318. if (mb->is_double_click()) {
  319. confirm_code_completion();
  320. }
  321. queue_redraw();
  322. } break;
  323. default:
  324. break;
  325. }
  326. accept_event();
  327. return;
  328. } else if (code_completion_active && code_completion_scroll_rect.has_point(mb->get_position())) {
  329. if (mb->get_button_index() != MouseButton::LEFT) {
  330. accept_event();
  331. return;
  332. }
  333. if (mb->is_pressed()) {
  334. is_code_completion_drag_started = true;
  335. is_code_completion_scroll_pressed = true;
  336. _update_scroll_selected_line(mb->get_position().y);
  337. queue_redraw();
  338. }
  339. accept_event();
  340. return;
  341. }
  342. cancel_code_completion();
  343. set_code_hint("");
  344. if (mb->is_pressed()) {
  345. Vector2i mpos = mb->get_position();
  346. if (is_layout_rtl()) {
  347. mpos.x = get_size().x - mpos.x;
  348. }
  349. Point2i pos = get_line_column_at_pos(mpos, false);
  350. int line = pos.y;
  351. int col = pos.x;
  352. if (line != -1 && mb->get_button_index() == MouseButton::LEFT) {
  353. if (is_line_folded(line)) {
  354. int wrap_index = get_line_wrap_index_at_column(line, col);
  355. if (wrap_index == get_line_wrap_count(line)) {
  356. int eol_icon_width = theme_cache.folded_eol_icon->get_width();
  357. int left_margin = get_total_gutter_width() + eol_icon_width + get_line_width(line, wrap_index) - get_h_scroll();
  358. if (mpos.x > left_margin && mpos.x <= left_margin + eol_icon_width + 3) {
  359. unfold_line(line);
  360. return;
  361. }
  362. }
  363. }
  364. }
  365. } else {
  366. if (mb->get_button_index() == MouseButton::LEFT) {
  367. if (mb->is_command_or_control_pressed() && !symbol_lookup_word.is_empty()) {
  368. Vector2i mpos = mb->get_position();
  369. if (is_layout_rtl()) {
  370. mpos.x = get_size().x - mpos.x;
  371. }
  372. Point2i pos = get_line_column_at_pos(mpos, false, false);
  373. int line = pos.y;
  374. int col = pos.x;
  375. if (line != -1) {
  376. emit_signal(SNAME("symbol_lookup"), symbol_lookup_word, line, col);
  377. }
  378. return;
  379. }
  380. }
  381. }
  382. }
  383. Ref<InputEventMouseMotion> mm = p_gui_input;
  384. if (mm.is_valid()) {
  385. Vector2i mpos = mm->get_position();
  386. if (is_layout_rtl()) {
  387. mpos.x = get_size().x - mpos.x;
  388. }
  389. if (symbol_lookup_on_click_enabled) {
  390. if (mm->is_command_or_control_pressed() && mm->get_button_mask().is_empty()) {
  391. symbol_lookup_pos = get_line_column_at_pos(mpos, false, false);
  392. symbol_lookup_new_word = get_lookup_word(symbol_lookup_pos.y, symbol_lookup_pos.x);
  393. if (symbol_lookup_new_word != symbol_lookup_word) {
  394. emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
  395. }
  396. } else if (!mm->is_command_or_control_pressed() || (!mm->get_button_mask().is_empty() && symbol_lookup_pos != get_line_column_at_pos(mpos, false, false))) {
  397. set_symbol_lookup_word_as_valid(false);
  398. }
  399. }
  400. if (symbol_tooltip_on_hover_enabled) {
  401. symbol_tooltip_pos = get_line_column_at_pos(mpos, false, false);
  402. symbol_tooltip_word = get_lookup_word(symbol_tooltip_pos.y, symbol_tooltip_pos.x);
  403. symbol_tooltip_timer->start();
  404. }
  405. bool scroll_hovered = code_completion_scroll_rect.has_point(mpos);
  406. if (is_code_completion_scroll_hovered != scroll_hovered) {
  407. is_code_completion_scroll_hovered = scroll_hovered;
  408. accept_event();
  409. queue_redraw();
  410. }
  411. if (is_code_completion_scroll_pressed) {
  412. _update_scroll_selected_line(mpos.y);
  413. accept_event();
  414. queue_redraw();
  415. return;
  416. }
  417. if (code_completion_active && code_completion_rect.has_point(mm->get_position())) {
  418. accept_event();
  419. return;
  420. }
  421. }
  422. Ref<InputEventKey> k = p_gui_input;
  423. if (TextEdit::alt_input(p_gui_input)) {
  424. accept_event();
  425. return;
  426. }
  427. bool update_code_completion = false;
  428. if (k.is_null()) {
  429. // MouseMotion events should not be handled by TextEdit logic if we're
  430. // currently clicking and dragging from the code completion panel.
  431. if (mm.is_null() || !is_code_completion_drag_started) {
  432. TextEdit::gui_input(p_gui_input);
  433. }
  434. return;
  435. }
  436. /* Ctrl + Hover symbols */
  437. bool mac_keys = OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios");
  438. if ((mac_keys && k->get_keycode() == Key::META) || (!mac_keys && k->get_keycode() == Key::CTRL)) {
  439. if (symbol_lookup_on_click_enabled) {
  440. if (k->is_pressed() && !is_dragging_cursor()) {
  441. Point2i lookup_pos = get_line_column_at_pos(get_local_mouse_pos(), false, false);
  442. symbol_lookup_new_word = get_lookup_word(lookup_pos.y, lookup_pos.x);
  443. if (symbol_lookup_new_word != symbol_lookup_word) {
  444. emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
  445. }
  446. } else {
  447. set_symbol_lookup_word_as_valid(false);
  448. }
  449. }
  450. return;
  451. }
  452. /* If a modifier has been pressed, and nothing else, return. */
  453. if (!k->is_pressed() || k->get_keycode() == Key::CTRL || k->get_keycode() == Key::ALT || k->get_keycode() == Key::SHIFT || k->get_keycode() == Key::META || k->get_keycode() == Key::CAPSLOCK) {
  454. return;
  455. }
  456. // Allow unicode handling if:
  457. // No modifiers are pressed (except Shift and CapsLock)
  458. bool allow_unicode_handling = !(k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());
  459. /* AUTO-COMPLETE */
  460. if (code_completion_enabled && k->is_action("ui_text_completion_query", true)) {
  461. request_code_completion(true);
  462. accept_event();
  463. return;
  464. }
  465. if (code_completion_active) {
  466. if (k->is_action("ui_up", true)) {
  467. if (code_completion_current_selected > 0) {
  468. code_completion_current_selected--;
  469. } else {
  470. code_completion_current_selected = code_completion_options.size() - 1;
  471. }
  472. code_completion_force_item_center = -1;
  473. code_completion_pan_offset = 0.0f;
  474. queue_redraw();
  475. accept_event();
  476. return;
  477. }
  478. if (k->is_action("ui_down", true)) {
  479. if (code_completion_current_selected < code_completion_options.size() - 1) {
  480. code_completion_current_selected++;
  481. } else {
  482. code_completion_current_selected = 0;
  483. }
  484. code_completion_force_item_center = -1;
  485. code_completion_pan_offset = 0.0f;
  486. queue_redraw();
  487. accept_event();
  488. return;
  489. }
  490. if (k->is_action("ui_page_up", true)) {
  491. code_completion_current_selected = MAX(0, code_completion_current_selected - theme_cache.code_completion_max_lines);
  492. code_completion_force_item_center = -1;
  493. code_completion_pan_offset = 0.0f;
  494. queue_redraw();
  495. accept_event();
  496. return;
  497. }
  498. if (k->is_action("ui_page_down", true)) {
  499. code_completion_current_selected = MIN(code_completion_options.size() - 1, code_completion_current_selected + theme_cache.code_completion_max_lines);
  500. code_completion_force_item_center = -1;
  501. code_completion_pan_offset = 0.0f;
  502. queue_redraw();
  503. accept_event();
  504. return;
  505. }
  506. if (k->is_action("ui_text_caret_line_start", true) || k->is_action("ui_text_caret_line_end", true)) {
  507. cancel_code_completion();
  508. }
  509. if (k->is_action("ui_text_completion_replace", true) || k->is_action("ui_text_completion_accept", true)) {
  510. confirm_code_completion(k->is_action("ui_text_completion_replace", true));
  511. accept_event();
  512. return;
  513. }
  514. if (k->is_action("ui_cancel", true)) {
  515. cancel_code_completion();
  516. accept_event();
  517. return;
  518. }
  519. if (k->is_action("ui_text_backspace", true)) {
  520. backspace();
  521. _filter_code_completion_candidates_impl();
  522. accept_event();
  523. return;
  524. }
  525. if (k->is_action("ui_left", true) || k->is_action("ui_right", true)) {
  526. update_code_completion = true;
  527. } else {
  528. update_code_completion = (allow_unicode_handling && k->get_unicode() >= 32);
  529. }
  530. }
  531. /* MISC */
  532. if (!code_hint.is_empty() && k->is_action("ui_cancel", true)) {
  533. set_code_hint("");
  534. accept_event();
  535. return;
  536. }
  537. if (allow_unicode_handling && k->get_unicode() == ')') {
  538. set_code_hint("");
  539. }
  540. /* Indentation */
  541. if (k->is_action("ui_text_indent", true)) {
  542. do_indent();
  543. accept_event();
  544. return;
  545. }
  546. if (k->is_action("ui_text_dedent", true)) {
  547. unindent_lines();
  548. accept_event();
  549. return;
  550. }
  551. // Override new line actions, for auto indent.
  552. if (k->is_action("ui_text_newline_above", true)) {
  553. _new_line(false, true);
  554. accept_event();
  555. return;
  556. }
  557. if (k->is_action("ui_text_newline_blank", true)) {
  558. _new_line(false);
  559. accept_event();
  560. return;
  561. }
  562. if (k->is_action("ui_text_newline", true)) {
  563. _new_line();
  564. accept_event();
  565. return;
  566. }
  567. // Remove shift, otherwise actions will not match.
  568. k = k->duplicate();
  569. k->set_shift_pressed(false);
  570. if (k->is_action("ui_text_caret_up", true) ||
  571. k->is_action("ui_text_caret_down", true) ||
  572. k->is_action("ui_text_caret_line_start", true) ||
  573. k->is_action("ui_text_caret_line_end", true) ||
  574. k->is_action("ui_text_caret_page_up", true) ||
  575. k->is_action("ui_text_caret_page_down", true)) {
  576. set_code_hint("");
  577. }
  578. TextEdit::gui_input(p_gui_input);
  579. if (update_code_completion) {
  580. _filter_code_completion_candidates_impl();
  581. }
  582. }
  583. /* General overrides */
  584. Control::CursorShape CodeEdit::get_cursor_shape(const Point2 &p_pos) const {
  585. if (!symbol_lookup_word.is_empty()) {
  586. return CURSOR_POINTING_HAND;
  587. }
  588. if (is_dragging_cursor()) {
  589. return TextEdit::get_cursor_shape(p_pos);
  590. }
  591. if ((code_completion_active && code_completion_rect.has_point(p_pos)) || (!is_editable() && (!is_selecting_enabled() || get_line_count() == 0))) {
  592. return CURSOR_ARROW;
  593. }
  594. if (code_completion_active && code_completion_scroll_rect.has_point(p_pos)) {
  595. return CURSOR_ARROW;
  596. }
  597. Point2i pos = get_line_column_at_pos(p_pos, false);
  598. int line = pos.y;
  599. int col = pos.x;
  600. if (line != -1 && is_line_folded(line)) {
  601. int wrap_index = get_line_wrap_index_at_column(line, col);
  602. if (wrap_index == get_line_wrap_count(line)) {
  603. int eol_icon_width = theme_cache.folded_eol_icon->get_width();
  604. int left_margin = get_total_gutter_width() + eol_icon_width + get_line_width(line, wrap_index) - get_h_scroll();
  605. if (p_pos.x > left_margin && p_pos.x <= left_margin + eol_icon_width + 3) {
  606. return CURSOR_POINTING_HAND;
  607. }
  608. }
  609. }
  610. return TextEdit::get_cursor_shape(p_pos);
  611. }
  612. void CodeEdit::_unhide_carets() {
  613. // Unfold caret and selection origin.
  614. for (int i = 0; i < get_caret_count(); i++) {
  615. if (_is_line_hidden(get_caret_line(i))) {
  616. unfold_line(get_caret_line(i));
  617. }
  618. if (has_selection(i) && _is_line_hidden(get_selection_origin_line(i))) {
  619. unfold_line(get_selection_origin_line(i));
  620. }
  621. }
  622. }
  623. /* Text manipulation */
  624. // Overridable actions
  625. void CodeEdit::_handle_unicode_input_internal(const uint32_t p_unicode, int p_caret) {
  626. start_action(EditAction::ACTION_TYPING);
  627. begin_multicaret_edit();
  628. for (int i = 0; i < get_caret_count(); i++) {
  629. if (p_caret != -1 && p_caret != i) {
  630. continue;
  631. }
  632. if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
  633. continue;
  634. }
  635. bool had_selection = has_selection(i);
  636. String selection_text = (had_selection ? get_selected_text(i) : "");
  637. if (had_selection) {
  638. delete_selection(i);
  639. }
  640. // Remove the old character if in overtype mode and no selection.
  641. if (is_overtype_mode_enabled() && !had_selection) {
  642. // Make sure we don't try and remove empty space.
  643. if (get_caret_column(i) < get_line(get_caret_line(i)).length()) {
  644. remove_text(get_caret_line(i), get_caret_column(i), get_caret_line(i), get_caret_column(i) + 1);
  645. }
  646. }
  647. const char32_t chr[2] = { (char32_t)p_unicode, 0 };
  648. if (auto_brace_completion_enabled) {
  649. int cl = get_caret_line(i);
  650. int cc = get_caret_column(i);
  651. if (had_selection) {
  652. insert_text_at_caret(chr, i);
  653. String close_key = get_auto_brace_completion_close_key(chr);
  654. if (!close_key.is_empty()) {
  655. insert_text_at_caret(selection_text + close_key, i);
  656. set_caret_column(get_caret_column(i) - 1, i == 0, i);
  657. }
  658. } else {
  659. int caret_move_offset = 1;
  660. int post_brace_pair = cc < get_line(cl).length() ? _get_auto_brace_pair_close_at_pos(cl, cc) : -1;
  661. if (has_string_delimiter(chr) && cc > 0 && !is_symbol(get_line(cl)[cc - 1]) && post_brace_pair == -1) {
  662. insert_text_at_caret(chr, i);
  663. } else if (cc < get_line(cl).length() && !is_symbol(get_line(cl)[cc])) {
  664. insert_text_at_caret(chr, i);
  665. } else if (post_brace_pair != -1 && auto_brace_completion_pairs[post_brace_pair].close_key[0] == chr[0]) {
  666. caret_move_offset = auto_brace_completion_pairs[post_brace_pair].close_key.length();
  667. } else if (is_in_comment(cl, cc) != -1 || (is_in_string(cl, cc) != -1 && has_string_delimiter(chr))) {
  668. insert_text_at_caret(chr, i);
  669. } else {
  670. insert_text_at_caret(chr, i);
  671. int pre_brace_pair = _get_auto_brace_pair_open_at_pos(cl, cc + 1);
  672. if (pre_brace_pair != -1) {
  673. insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key, i);
  674. }
  675. }
  676. set_caret_column(cc + caret_move_offset, i == 0, i);
  677. }
  678. } else {
  679. insert_text_at_caret(chr, i);
  680. }
  681. }
  682. end_multicaret_edit();
  683. end_action();
  684. }
  685. void CodeEdit::_backspace_internal(int p_caret) {
  686. if (!is_editable()) {
  687. return;
  688. }
  689. if (has_selection(p_caret)) {
  690. delete_selection(p_caret);
  691. return;
  692. }
  693. begin_complex_operation();
  694. begin_multicaret_edit();
  695. for (int i = 0; i < get_caret_count(); i++) {
  696. if (p_caret != -1 && p_caret != i) {
  697. continue;
  698. }
  699. if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
  700. continue;
  701. }
  702. int to_line = get_caret_line(i);
  703. int to_column = get_caret_column(i);
  704. if (to_column == 0 && to_line == 0) {
  705. continue;
  706. }
  707. if (to_line > 0 && to_column == 0 && _is_line_hidden(to_line - 1)) {
  708. unfold_line(to_line - 1);
  709. }
  710. int from_line = to_column > 0 ? to_line : to_line - 1;
  711. int from_column = 0;
  712. if (to_column == 0) {
  713. from_column = get_line(to_line - 1).length();
  714. } else if (TextEdit::is_caret_mid_grapheme_enabled() || !TextEdit::is_backspace_deletes_composite_character_enabled()) {
  715. from_column = to_column - 1;
  716. } else {
  717. from_column = TextEdit::get_previous_composite_character_column(to_line, to_column);
  718. }
  719. merge_gutters(from_line, to_line);
  720. if (auto_brace_completion_enabled && to_column > 0) {
  721. int idx = _get_auto_brace_pair_open_at_pos(to_line, to_column);
  722. if (idx != -1) {
  723. from_column = to_column - auto_brace_completion_pairs[idx].open_key.length();
  724. if (_get_auto_brace_pair_close_at_pos(to_line, to_column) == idx) {
  725. to_column += auto_brace_completion_pairs[idx].close_key.length();
  726. }
  727. }
  728. }
  729. // For space indentation we need to do a basic unindent if there are no chars to the left, acting the same way as tabs.
  730. if (indent_using_spaces && to_column != 0) {
  731. if (get_first_non_whitespace_column(to_line) >= to_column) {
  732. from_column = to_column - _calculate_spaces_till_next_left_indent(to_column);
  733. from_line = to_line;
  734. }
  735. }
  736. remove_text(from_line, from_column, to_line, to_column);
  737. set_caret_line(from_line, false, true, -1, i);
  738. set_caret_column(from_column, i == 0, i);
  739. }
  740. end_multicaret_edit();
  741. end_complex_operation();
  742. }
  743. void CodeEdit::_cut_internal(int p_caret) {
  744. // Overridden to unfold lines.
  745. _copy_internal(p_caret);
  746. if (!is_editable()) {
  747. return;
  748. }
  749. if (has_selection(p_caret)) {
  750. delete_selection(p_caret);
  751. return;
  752. }
  753. if (!is_empty_selection_clipboard_enabled()) {
  754. return;
  755. }
  756. if (p_caret == -1) {
  757. delete_lines();
  758. } else {
  759. unfold_line(get_caret_line(p_caret));
  760. remove_line_at(get_caret_line(p_caret));
  761. }
  762. }
  763. /* Indent management */
  764. void CodeEdit::set_indent_size(const int p_size) {
  765. ERR_FAIL_COND_MSG(p_size <= 0, "Indend size must be greater than 0.");
  766. if (indent_size == p_size) {
  767. return;
  768. }
  769. indent_size = p_size;
  770. if (indent_using_spaces) {
  771. indent_text = String(" ").repeat(p_size);
  772. } else {
  773. indent_text = "\t";
  774. }
  775. set_tab_size(p_size);
  776. }
  777. int CodeEdit::get_indent_size() const {
  778. return indent_size;
  779. }
  780. void CodeEdit::set_indent_using_spaces(const bool p_use_spaces) {
  781. indent_using_spaces = p_use_spaces;
  782. if (indent_using_spaces) {
  783. indent_text = String(" ").repeat(indent_size);
  784. } else {
  785. indent_text = "\t";
  786. }
  787. }
  788. bool CodeEdit::is_indent_using_spaces() const {
  789. return indent_using_spaces;
  790. }
  791. void CodeEdit::set_auto_indent_enabled(bool p_enabled) {
  792. auto_indent = p_enabled;
  793. }
  794. bool CodeEdit::is_auto_indent_enabled() const {
  795. return auto_indent;
  796. }
  797. void CodeEdit::set_auto_indent_prefixes(const TypedArray<String> &p_prefixes) {
  798. auto_indent_prefixes.clear();
  799. for (int i = 0; i < p_prefixes.size(); i++) {
  800. const String prefix = p_prefixes[i];
  801. auto_indent_prefixes.insert(prefix[0]);
  802. }
  803. }
  804. TypedArray<String> CodeEdit::get_auto_indent_prefixes() const {
  805. TypedArray<String> prefixes;
  806. for (const char32_t &E : auto_indent_prefixes) {
  807. prefixes.push_back(String::chr(E));
  808. }
  809. return prefixes;
  810. }
  811. void CodeEdit::do_indent() {
  812. if (!is_editable()) {
  813. return;
  814. }
  815. if (has_selection()) {
  816. indent_lines();
  817. return;
  818. }
  819. if (!indent_using_spaces) {
  820. insert_text_at_caret("\t");
  821. return;
  822. }
  823. begin_complex_operation();
  824. begin_multicaret_edit();
  825. for (int i = 0; i < get_caret_count(); i++) {
  826. if (multicaret_edit_ignore_caret(i)) {
  827. continue;
  828. }
  829. int spaces_to_add = _calculate_spaces_till_next_right_indent(get_caret_column(i));
  830. if (spaces_to_add > 0) {
  831. insert_text_at_caret(String(" ").repeat(spaces_to_add), i);
  832. }
  833. }
  834. end_multicaret_edit();
  835. end_complex_operation();
  836. }
  837. void CodeEdit::indent_lines() {
  838. if (!is_editable()) {
  839. return;
  840. }
  841. begin_complex_operation();
  842. begin_multicaret_edit();
  843. Vector<Point2i> line_ranges = get_line_ranges_from_carets();
  844. for (Point2i line_range : line_ranges) {
  845. for (int i = line_range.x; i <= line_range.y; i++) {
  846. const String line_text = get_line(i);
  847. if (line_text.is_empty()) {
  848. // Ignore empty lines.
  849. continue;
  850. }
  851. if (indent_using_spaces) {
  852. int spaces_to_add = _calculate_spaces_till_next_right_indent(get_first_non_whitespace_column(i));
  853. insert_text(String(" ").repeat(spaces_to_add), i, 0, false);
  854. } else {
  855. insert_text("\t", i, 0, false);
  856. }
  857. }
  858. }
  859. end_multicaret_edit();
  860. end_complex_operation();
  861. }
  862. void CodeEdit::unindent_lines() {
  863. if (!is_editable()) {
  864. return;
  865. }
  866. begin_complex_operation();
  867. begin_multicaret_edit();
  868. Vector<Point2i> line_ranges = get_line_ranges_from_carets();
  869. for (Point2i line_range : line_ranges) {
  870. for (int i = line_range.x; i <= line_range.y; i++) {
  871. const String line_text = get_line(i);
  872. if (line_text.begins_with("\t")) {
  873. remove_text(i, 0, i, 1);
  874. } else if (line_text.begins_with(" ")) {
  875. // Remove only enough spaces to align text to nearest full multiple of indentation_size.
  876. int spaces_to_remove = _calculate_spaces_till_next_left_indent(get_first_non_whitespace_column(i));
  877. remove_text(i, 0, i, spaces_to_remove);
  878. }
  879. }
  880. }
  881. end_multicaret_edit();
  882. end_complex_operation();
  883. }
  884. void CodeEdit::convert_indent(int p_from_line, int p_to_line) {
  885. if (!is_editable()) {
  886. return;
  887. }
  888. // Check line range.
  889. p_from_line = (p_from_line < 0) ? 0 : p_from_line;
  890. p_to_line = (p_to_line < 0) ? get_line_count() - 1 : p_to_line;
  891. ERR_FAIL_COND(p_from_line >= get_line_count());
  892. ERR_FAIL_COND(p_to_line >= get_line_count());
  893. ERR_FAIL_COND(p_to_line < p_from_line);
  894. // Check lines within range.
  895. const char32_t from_indent_char = indent_using_spaces ? '\t' : ' ';
  896. int size_diff = indent_using_spaces ? indent_size - 1 : -(indent_size - 1);
  897. bool changed_indentation = false;
  898. for (int i = p_from_line; i <= p_to_line; i++) {
  899. String line = get_line(i);
  900. if (line.length() <= 0) {
  901. continue;
  902. }
  903. if (is_in_string(i) != -1) {
  904. continue;
  905. }
  906. // Check chars in the line.
  907. int j = 0;
  908. int space_count = 0;
  909. bool line_changed = false;
  910. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  911. if (line[j] != from_indent_char) {
  912. space_count = 0;
  913. j++;
  914. continue;
  915. }
  916. space_count++;
  917. if (!indent_using_spaces && space_count != indent_size) {
  918. j++;
  919. continue;
  920. }
  921. line_changed = true;
  922. if (!changed_indentation) {
  923. begin_complex_operation();
  924. begin_multicaret_edit();
  925. changed_indentation = true;
  926. }
  927. // Calculate new line.
  928. line = line.left(j + ((size_diff < 0) ? size_diff : 0)) + indent_text + line.substr(j + 1);
  929. space_count = 0;
  930. j += size_diff;
  931. }
  932. if (line_changed) {
  933. // Use set line to preserve carets visual position.
  934. set_line(i, line);
  935. }
  936. }
  937. if (!changed_indentation) {
  938. return;
  939. }
  940. merge_overlapping_carets();
  941. end_multicaret_edit();
  942. end_complex_operation();
  943. }
  944. int CodeEdit::_calculate_spaces_till_next_left_indent(int p_column) const {
  945. int spaces_till_indent = p_column % indent_size;
  946. if (spaces_till_indent == 0) {
  947. spaces_till_indent = indent_size;
  948. }
  949. return spaces_till_indent;
  950. }
  951. int CodeEdit::_calculate_spaces_till_next_right_indent(int p_column) const {
  952. return indent_size - p_column % indent_size;
  953. }
  954. void CodeEdit::_new_line(bool p_split_current_line, bool p_above) {
  955. if (!is_editable()) {
  956. return;
  957. }
  958. begin_complex_operation();
  959. begin_multicaret_edit();
  960. for (int i = 0; i < get_caret_count(); i++) {
  961. if (multicaret_edit_ignore_caret(i)) {
  962. continue;
  963. }
  964. // When not splitting the line, we need to factor in indentation from the end of the current line.
  965. const int cc = p_split_current_line ? get_caret_column(i) : get_line(get_caret_line(i)).length();
  966. const int cl = get_caret_line(i);
  967. const String line = get_line(cl);
  968. String ins = "";
  969. if (!p_above) {
  970. ins = "\n";
  971. }
  972. // Append current indentation.
  973. int space_count = 0;
  974. int line_col = 0;
  975. for (; line_col < cc; line_col++) {
  976. if (line[line_col] == '\t') {
  977. ins += indent_text;
  978. space_count = 0;
  979. continue;
  980. }
  981. if (line[line_col] == ' ') {
  982. space_count++;
  983. if (space_count == indent_size) {
  984. ins += indent_text;
  985. space_count = 0;
  986. }
  987. continue;
  988. }
  989. break;
  990. }
  991. if (p_above) {
  992. ins += "\n";
  993. }
  994. if (is_line_folded(cl)) {
  995. unfold_line(cl);
  996. }
  997. // Indent once again if the previous line needs it, ie ':'.
  998. // Then add an addition new line for any closing pairs aka '()'.
  999. // Skip this in comments or if we are going above.
  1000. bool brace_indent = false;
  1001. if (auto_indent && !p_above && cc > 0 && is_in_comment(cl) == -1) {
  1002. bool should_indent = false;
  1003. char32_t indent_char = ' ';
  1004. for (; line_col < cc; line_col++) {
  1005. char32_t c = line[line_col];
  1006. if (auto_indent_prefixes.has(c) && is_in_comment(cl, line_col) == -1) {
  1007. should_indent = true;
  1008. indent_char = c;
  1009. continue;
  1010. }
  1011. // Make sure this is the last char, trailing whitespace or comments are okay.
  1012. // Increment column for comments because the delimiter (#) should be ignored.
  1013. if (should_indent && (!is_whitespace(c) && is_in_comment(cl, line_col + 1) == -1)) {
  1014. should_indent = false;
  1015. }
  1016. }
  1017. if (should_indent) {
  1018. ins += indent_text;
  1019. String closing_pair = get_auto_brace_completion_close_key(String::chr(indent_char));
  1020. if (!closing_pair.is_empty() && line.find(closing_pair, cc) == cc) {
  1021. // No need to move the brace below if we are not taking the text with us.
  1022. if (p_split_current_line) {
  1023. brace_indent = true;
  1024. ins += "\n" + ins.substr(indent_text.size(), ins.length() - 2);
  1025. } else {
  1026. brace_indent = false;
  1027. ins = "\n" + ins.substr(indent_text.size(), ins.length() - 2);
  1028. }
  1029. }
  1030. }
  1031. }
  1032. if (p_split_current_line) {
  1033. insert_text_at_caret(ins, i);
  1034. } else {
  1035. insert_text(ins, cl, p_above ? 0 : get_line(cl).length(), p_above, p_above);
  1036. deselect(i);
  1037. set_caret_line(p_above ? cl : cl + 1, false, true, -1, i);
  1038. set_caret_column(get_line(get_caret_line(i)).length(), i == 0, i);
  1039. }
  1040. if (brace_indent) {
  1041. // Move to inner indented line.
  1042. set_caret_line(get_caret_line(i) - 1, false, true, 0, i);
  1043. set_caret_column(get_line(get_caret_line(i)).length(), i == 0, i);
  1044. }
  1045. }
  1046. end_multicaret_edit();
  1047. end_complex_operation();
  1048. }
  1049. /* Auto brace completion */
  1050. void CodeEdit::set_auto_brace_completion_enabled(bool p_enabled) {
  1051. auto_brace_completion_enabled = p_enabled;
  1052. }
  1053. bool CodeEdit::is_auto_brace_completion_enabled() const {
  1054. return auto_brace_completion_enabled;
  1055. }
  1056. void CodeEdit::set_highlight_matching_braces_enabled(bool p_enabled) {
  1057. highlight_matching_braces_enabled = p_enabled;
  1058. queue_redraw();
  1059. }
  1060. bool CodeEdit::is_highlight_matching_braces_enabled() const {
  1061. return highlight_matching_braces_enabled;
  1062. }
  1063. void CodeEdit::add_auto_brace_completion_pair(const String &p_open_key, const String &p_close_key) {
  1064. ERR_FAIL_COND_MSG(p_open_key.is_empty(), "auto brace completion open key cannot be empty");
  1065. ERR_FAIL_COND_MSG(p_close_key.is_empty(), "auto brace completion close key cannot be empty");
  1066. for (int i = 0; i < p_open_key.length(); i++) {
  1067. ERR_FAIL_COND_MSG(!is_symbol(p_open_key[i]), "auto brace completion open key must be a symbol");
  1068. }
  1069. for (int i = 0; i < p_close_key.length(); i++) {
  1070. ERR_FAIL_COND_MSG(!is_symbol(p_close_key[i]), "auto brace completion close key must be a symbol");
  1071. }
  1072. int at = 0;
  1073. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1074. ERR_FAIL_COND_MSG(auto_brace_completion_pairs[i].open_key == p_open_key, "auto brace completion open key '" + p_open_key + "' already exists.");
  1075. if (p_open_key.length() < auto_brace_completion_pairs[i].open_key.length()) {
  1076. at++;
  1077. }
  1078. }
  1079. BracePair brace_pair;
  1080. brace_pair.open_key = p_open_key;
  1081. brace_pair.close_key = p_close_key;
  1082. auto_brace_completion_pairs.insert(at, brace_pair);
  1083. }
  1084. void CodeEdit::set_auto_brace_completion_pairs(const Dictionary &p_auto_brace_completion_pairs) {
  1085. auto_brace_completion_pairs.clear();
  1086. for (const KeyValue<Variant, Variant> &kv : p_auto_brace_completion_pairs) {
  1087. add_auto_brace_completion_pair(kv.key, kv.value);
  1088. }
  1089. }
  1090. Dictionary CodeEdit::get_auto_brace_completion_pairs() const {
  1091. Dictionary brace_pairs;
  1092. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1093. brace_pairs[auto_brace_completion_pairs[i].open_key] = auto_brace_completion_pairs[i].close_key;
  1094. }
  1095. return brace_pairs;
  1096. }
  1097. bool CodeEdit::has_auto_brace_completion_open_key(const String &p_open_key) const {
  1098. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1099. if (auto_brace_completion_pairs[i].open_key == p_open_key) {
  1100. return true;
  1101. }
  1102. }
  1103. return false;
  1104. }
  1105. bool CodeEdit::has_auto_brace_completion_close_key(const String &p_close_key) const {
  1106. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1107. if (auto_brace_completion_pairs[i].close_key == p_close_key) {
  1108. return true;
  1109. }
  1110. }
  1111. return false;
  1112. }
  1113. String CodeEdit::get_auto_brace_completion_close_key(const String &p_open_key) const {
  1114. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1115. if (auto_brace_completion_pairs[i].open_key == p_open_key) {
  1116. return auto_brace_completion_pairs[i].close_key;
  1117. }
  1118. }
  1119. return String();
  1120. }
  1121. /* Main Gutter */
  1122. void CodeEdit::_update_draw_main_gutter() {
  1123. set_gutter_draw(main_gutter, draw_breakpoints || draw_bookmarks || draw_executing_lines);
  1124. }
  1125. void CodeEdit::set_draw_breakpoints_gutter(bool p_draw) {
  1126. draw_breakpoints = p_draw;
  1127. set_gutter_clickable(main_gutter, p_draw);
  1128. _update_draw_main_gutter();
  1129. }
  1130. bool CodeEdit::is_drawing_breakpoints_gutter() const {
  1131. return draw_breakpoints;
  1132. }
  1133. void CodeEdit::set_draw_bookmarks_gutter(bool p_draw) {
  1134. draw_bookmarks = p_draw;
  1135. _update_draw_main_gutter();
  1136. }
  1137. bool CodeEdit::is_drawing_bookmarks_gutter() const {
  1138. return draw_bookmarks;
  1139. }
  1140. void CodeEdit::set_draw_executing_lines_gutter(bool p_draw) {
  1141. draw_executing_lines = p_draw;
  1142. _update_draw_main_gutter();
  1143. }
  1144. bool CodeEdit::is_drawing_executing_lines_gutter() const {
  1145. return draw_executing_lines;
  1146. }
  1147. void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  1148. bool hovering = get_hovered_gutter() == Vector2i(main_gutter, p_line);
  1149. RID ci = get_text_canvas_item();
  1150. if (draw_breakpoints && theme_cache.breakpoint_icon.is_valid()) {
  1151. bool breakpointed = is_line_breakpointed(p_line);
  1152. bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  1153. if (breakpointed || (hovering && !is_dragging_cursor() && !shift_pressed)) {
  1154. int padding = p_region.size.x / 6;
  1155. Color use_color = theme_cache.breakpoint_color;
  1156. if (hovering && !shift_pressed) {
  1157. use_color = breakpointed ? use_color.lightened(0.3) : use_color.darkened(0.5);
  1158. }
  1159. Rect2 icon_region = p_region;
  1160. icon_region.position += Point2(padding, padding);
  1161. icon_region.size -= Point2(padding, padding) * 2;
  1162. theme_cache.breakpoint_icon->draw_rect(ci, icon_region, false, use_color);
  1163. }
  1164. }
  1165. if (draw_bookmarks && theme_cache.bookmark_icon.is_valid()) {
  1166. bool bookmarked = is_line_bookmarked(p_line);
  1167. bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  1168. if (bookmarked || (hovering && !is_dragging_cursor() && shift_pressed)) {
  1169. int horizontal_padding = p_region.size.x / 2;
  1170. int vertical_padding = p_region.size.y / 4;
  1171. Color use_color = theme_cache.bookmark_color;
  1172. if (hovering && shift_pressed) {
  1173. use_color = bookmarked ? use_color.lightened(0.3) : use_color.darkened(0.5);
  1174. }
  1175. Rect2 icon_region = p_region;
  1176. icon_region.position += Point2(horizontal_padding, 0);
  1177. icon_region.size -= Point2(horizontal_padding * 1.1, vertical_padding);
  1178. theme_cache.bookmark_icon->draw_rect(ci, icon_region, false, use_color);
  1179. }
  1180. }
  1181. if (draw_executing_lines && is_line_executing(p_line) && theme_cache.executing_line_icon.is_valid()) {
  1182. int horizontal_padding = p_region.size.x / 10;
  1183. int vertical_padding = p_region.size.y / 4;
  1184. Rect2 icon_region = p_region;
  1185. icon_region.position += Point2(horizontal_padding, vertical_padding);
  1186. icon_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  1187. theme_cache.executing_line_icon->draw_rect(ci, icon_region, false, theme_cache.executing_line_color);
  1188. }
  1189. }
  1190. // Breakpoints
  1191. void CodeEdit::set_line_as_breakpoint(int p_line, bool p_breakpointed) {
  1192. ERR_FAIL_INDEX(p_line, get_line_count());
  1193. int mask = get_line_gutter_metadata(p_line, main_gutter);
  1194. set_line_gutter_metadata(p_line, main_gutter, p_breakpointed ? mask | MAIN_GUTTER_BREAKPOINT : mask & ~MAIN_GUTTER_BREAKPOINT);
  1195. if (p_breakpointed) {
  1196. breakpointed_lines[p_line] = true;
  1197. } else if (breakpointed_lines.has(p_line)) {
  1198. breakpointed_lines.erase(p_line);
  1199. }
  1200. emit_signal(SNAME("breakpoint_toggled"), p_line);
  1201. queue_redraw();
  1202. }
  1203. bool CodeEdit::is_line_breakpointed(int p_line) const {
  1204. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BREAKPOINT;
  1205. }
  1206. void CodeEdit::clear_breakpointed_lines() {
  1207. for (int i = 0; i < get_line_count(); i++) {
  1208. if (is_line_breakpointed(i)) {
  1209. set_line_as_breakpoint(i, false);
  1210. }
  1211. }
  1212. }
  1213. PackedInt32Array CodeEdit::get_breakpointed_lines() const {
  1214. PackedInt32Array ret;
  1215. for (int i = 0; i < get_line_count(); i++) {
  1216. if (is_line_breakpointed(i)) {
  1217. ret.append(i);
  1218. }
  1219. }
  1220. return ret;
  1221. }
  1222. // Bookmarks
  1223. void CodeEdit::set_line_as_bookmarked(int p_line, bool p_bookmarked) {
  1224. int mask = get_line_gutter_metadata(p_line, main_gutter);
  1225. set_line_gutter_metadata(p_line, main_gutter, p_bookmarked ? mask | MAIN_GUTTER_BOOKMARK : mask & ~MAIN_GUTTER_BOOKMARK);
  1226. queue_redraw();
  1227. }
  1228. bool CodeEdit::is_line_bookmarked(int p_line) const {
  1229. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BOOKMARK;
  1230. }
  1231. void CodeEdit::clear_bookmarked_lines() {
  1232. for (int i = 0; i < get_line_count(); i++) {
  1233. if (is_line_bookmarked(i)) {
  1234. set_line_as_bookmarked(i, false);
  1235. }
  1236. }
  1237. }
  1238. PackedInt32Array CodeEdit::get_bookmarked_lines() const {
  1239. PackedInt32Array ret;
  1240. for (int i = 0; i < get_line_count(); i++) {
  1241. if (is_line_bookmarked(i)) {
  1242. ret.append(i);
  1243. }
  1244. }
  1245. return ret;
  1246. }
  1247. // Executing lines
  1248. void CodeEdit::set_line_as_executing(int p_line, bool p_executing) {
  1249. int mask = get_line_gutter_metadata(p_line, main_gutter);
  1250. set_line_gutter_metadata(p_line, main_gutter, p_executing ? mask | MAIN_GUTTER_EXECUTING : mask & ~MAIN_GUTTER_EXECUTING);
  1251. queue_redraw();
  1252. }
  1253. bool CodeEdit::is_line_executing(int p_line) const {
  1254. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_EXECUTING;
  1255. }
  1256. void CodeEdit::clear_executing_lines() {
  1257. for (int i = 0; i < get_line_count(); i++) {
  1258. if (is_line_executing(i)) {
  1259. set_line_as_executing(i, false);
  1260. }
  1261. }
  1262. }
  1263. PackedInt32Array CodeEdit::get_executing_lines() const {
  1264. PackedInt32Array ret;
  1265. for (int i = 0; i < get_line_count(); i++) {
  1266. if (is_line_executing(i)) {
  1267. ret.append(i);
  1268. }
  1269. }
  1270. return ret;
  1271. }
  1272. /* Line numbers */
  1273. void CodeEdit::set_draw_line_numbers(bool p_draw) {
  1274. set_gutter_draw(line_number_gutter, p_draw);
  1275. }
  1276. bool CodeEdit::is_draw_line_numbers_enabled() const {
  1277. return is_gutter_drawn(line_number_gutter);
  1278. }
  1279. void CodeEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
  1280. String new_line_number_padding = p_zero_padded ? "0" : " ";
  1281. if (line_number_padding == new_line_number_padding) {
  1282. return;
  1283. }
  1284. line_number_padding = new_line_number_padding;
  1285. _clear_line_number_text_cache();
  1286. queue_redraw();
  1287. }
  1288. bool CodeEdit::is_line_numbers_zero_padded() const {
  1289. return line_number_padding == "0";
  1290. }
  1291. void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  1292. if (!Rect2(Vector2(0, 0), get_size()).intersects(p_region)) {
  1293. return;
  1294. }
  1295. bool rtl = is_layout_rtl();
  1296. HashMap<int, RID>::Iterator E = line_number_text_cache.find(p_line);
  1297. RID text_rid;
  1298. if (E) {
  1299. text_rid = E->value;
  1300. } else {
  1301. String fc = String::num_int64(p_line + 1).lpad(line_number_digits, line_number_padding);
  1302. if (is_localizing_numeral_system()) {
  1303. fc = TS->format_number(fc);
  1304. }
  1305. text_rid = TS->create_shaped_text();
  1306. if (theme_cache.font.is_valid()) {
  1307. TS->shaped_text_add_string(text_rid, fc, theme_cache.font->get_rids(), theme_cache.font_size, theme_cache.font->get_opentype_features());
  1308. }
  1309. line_number_text_cache.insert(p_line, text_rid);
  1310. }
  1311. Size2 text_size = TS->shaped_text_get_size(text_rid);
  1312. Point2 ofs = p_region.get_center() - text_size / 2;
  1313. ofs.y += TS->shaped_text_get_ascent(text_rid);
  1314. if (rtl) {
  1315. ofs.x = p_region.get_end().x - text_size.width;
  1316. } else {
  1317. ofs.x = p_region.position.x;
  1318. }
  1319. Color number_color = get_line_gutter_item_color(p_line, line_number_gutter);
  1320. if (number_color == Color(1, 1, 1)) {
  1321. number_color = theme_cache.line_number_color;
  1322. }
  1323. TS->shaped_text_draw(text_rid, get_text_canvas_item(), ofs, -1, -1, number_color);
  1324. }
  1325. void CodeEdit::_clear_line_number_text_cache() {
  1326. for (const KeyValue<int, RID> &KV : line_number_text_cache) {
  1327. TS->free_rid(KV.value);
  1328. }
  1329. line_number_text_cache.clear();
  1330. }
  1331. void CodeEdit::_update_line_number_gutter_width() {
  1332. set_gutter_width(line_number_gutter, (line_number_digits + 1) * theme_cache.font->get_char_size('0', theme_cache.font_size).width);
  1333. }
  1334. /* Fold Gutter */
  1335. void CodeEdit::set_draw_fold_gutter(bool p_draw) {
  1336. set_gutter_draw(fold_gutter, p_draw);
  1337. }
  1338. bool CodeEdit::is_drawing_fold_gutter() const {
  1339. return is_gutter_drawn(fold_gutter);
  1340. }
  1341. void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region) {
  1342. if (!can_fold_line(p_line) && !is_line_folded(p_line)) {
  1343. set_line_gutter_clickable(p_line, fold_gutter, false);
  1344. return;
  1345. }
  1346. set_line_gutter_clickable(p_line, fold_gutter, true);
  1347. RID ci = get_text_canvas_item();
  1348. int horizontal_padding = p_region.size.x / 10;
  1349. int vertical_padding = p_region.size.y / 6;
  1350. p_region.position += Point2(horizontal_padding, vertical_padding);
  1351. p_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  1352. bool can_fold = can_fold_line(p_line);
  1353. if (is_line_code_region_start(p_line)) {
  1354. Color region_icon_color = theme_cache.folded_code_region_color;
  1355. region_icon_color.a = MAX(region_icon_color.a, 0.4f);
  1356. if (can_fold) {
  1357. theme_cache.can_fold_code_region_icon->draw_rect(ci, p_region, false, region_icon_color);
  1358. } else {
  1359. theme_cache.folded_code_region_icon->draw_rect(ci, p_region, false, region_icon_color);
  1360. }
  1361. return;
  1362. }
  1363. if (can_fold) {
  1364. theme_cache.can_fold_icon->draw_rect(ci, p_region, false, theme_cache.code_folding_color);
  1365. return;
  1366. }
  1367. theme_cache.folded_icon->draw_rect(ci, p_region, false, theme_cache.code_folding_color);
  1368. }
  1369. /* Line Folding */
  1370. void CodeEdit::set_line_folding_enabled(bool p_enabled) {
  1371. line_folding_enabled = p_enabled;
  1372. _set_hiding_enabled(p_enabled);
  1373. }
  1374. bool CodeEdit::is_line_folding_enabled() const {
  1375. return line_folding_enabled;
  1376. }
  1377. bool CodeEdit::can_fold_line(int p_line) const {
  1378. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1379. if (!line_folding_enabled) {
  1380. return false;
  1381. }
  1382. if (p_line + 1 >= get_line_count() || get_line(p_line).strip_edges().is_empty()) {
  1383. return false;
  1384. }
  1385. if (_is_line_hidden(p_line) || is_line_folded(p_line)) {
  1386. return false;
  1387. }
  1388. // Check for code region.
  1389. if (is_line_code_region_end(p_line)) {
  1390. return false;
  1391. }
  1392. if (is_line_code_region_start(p_line)) {
  1393. int region_level = 0;
  1394. // Check if there is a valid end region tag.
  1395. for (int next_line = p_line + 1; next_line < get_line_count(); next_line++) {
  1396. if (is_line_code_region_end(next_line)) {
  1397. region_level -= 1;
  1398. if (region_level == -1) {
  1399. return true;
  1400. }
  1401. }
  1402. if (is_line_code_region_start(next_line)) {
  1403. region_level += 1;
  1404. }
  1405. }
  1406. return false;
  1407. }
  1408. /* Check for full multiline line or block strings / comments. */
  1409. int in_comment = is_in_comment(p_line);
  1410. int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
  1411. if (in_string != -1 || in_comment != -1) {
  1412. if (get_delimiter_start_position(p_line, get_line(p_line).size() - 1).y != p_line) {
  1413. return false;
  1414. }
  1415. int delimiter_end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
  1416. /* No end line, therefore we have a multiline region over the rest of the file. */
  1417. if (delimiter_end_line == -1) {
  1418. return true;
  1419. }
  1420. /* End line is the same therefore we have a block. */
  1421. if (delimiter_end_line == p_line) {
  1422. /* Check we are the start of the block. */
  1423. if (p_line - 1 >= 0) {
  1424. if ((in_string != -1 && is_in_string(p_line - 1) != -1) || (in_comment != -1 && is_in_comment(p_line - 1) != -1 && !is_line_code_region_start(p_line - 1) && !is_line_code_region_end(p_line - 1))) {
  1425. return false;
  1426. }
  1427. }
  1428. /* Check it continues for at least one line. */
  1429. return ((in_string != -1 && is_in_string(p_line + 1) != -1) || (in_comment != -1 && is_in_comment(p_line + 1) != -1 && !is_line_code_region_start(p_line + 1) && !is_line_code_region_end(p_line + 1)));
  1430. }
  1431. return ((in_string != -1 && is_in_string(delimiter_end_line) != -1) || (in_comment != -1 && is_in_comment(delimiter_end_line) != -1));
  1432. }
  1433. /* Otherwise check indent levels. */
  1434. int start_indent = get_indent_level(p_line);
  1435. for (int i = p_line + 1; i < get_line_count(); i++) {
  1436. if (is_in_string(i) != -1 || is_in_comment(i) != -1 || get_line(i).strip_edges().is_empty()) {
  1437. continue;
  1438. }
  1439. return (get_indent_level(i) > start_indent);
  1440. }
  1441. return false;
  1442. }
  1443. bool CodeEdit::_fold_line(int p_line) {
  1444. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1445. if (!is_line_folding_enabled() || !can_fold_line(p_line)) {
  1446. return false;
  1447. }
  1448. /* Find the last line to be hidden. */
  1449. const int line_count = get_line_count() - 1;
  1450. int end_line = line_count;
  1451. // Fold code region.
  1452. if (is_line_code_region_start(p_line)) {
  1453. int region_level = 0;
  1454. for (int endregion_line = p_line + 1; endregion_line < get_line_count(); endregion_line++) {
  1455. if (is_line_code_region_start(endregion_line)) {
  1456. region_level += 1;
  1457. }
  1458. if (is_line_code_region_end(endregion_line)) {
  1459. region_level -= 1;
  1460. if (region_level == -1) {
  1461. end_line = endregion_line;
  1462. break;
  1463. }
  1464. }
  1465. }
  1466. set_line_background_color(p_line, theme_cache.folded_code_region_color);
  1467. }
  1468. int in_comment = is_in_comment(p_line);
  1469. int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
  1470. if (!is_line_code_region_start(p_line)) {
  1471. if (in_string != -1 || in_comment != -1) {
  1472. end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
  1473. // End line is the same therefore we have a block of single line delimiters.
  1474. if (end_line == p_line) {
  1475. for (int i = p_line + 1; i <= line_count; i++) {
  1476. if ((in_string != -1 && is_in_string(i) == -1) || (in_comment != -1 && is_in_comment(i) == -1)) {
  1477. break;
  1478. }
  1479. if (in_comment != -1 && (is_line_code_region_start(i) || is_line_code_region_end(i))) {
  1480. // A code region tag should split a comment block, ending it early.
  1481. break;
  1482. }
  1483. end_line = i;
  1484. }
  1485. }
  1486. } else {
  1487. int start_indent = get_indent_level(p_line);
  1488. for (int i = p_line + 1; i <= line_count; i++) {
  1489. if (get_line(i).strip_edges().is_empty()) {
  1490. continue;
  1491. }
  1492. if (get_indent_level(i) > start_indent) {
  1493. end_line = i;
  1494. continue;
  1495. }
  1496. if (is_in_string(i) == -1 && is_in_comment(i) == -1) {
  1497. break;
  1498. }
  1499. }
  1500. }
  1501. }
  1502. for (int i = p_line + 1; i <= end_line; i++) {
  1503. _set_line_as_hidden(i, true);
  1504. }
  1505. // Collapse any carets in the hidden area.
  1506. collapse_carets(p_line, get_line(p_line).length(), end_line, get_line(end_line).length(), true);
  1507. return true;
  1508. }
  1509. bool CodeEdit::_unfold_line(int p_line) {
  1510. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1511. if (!is_line_folded(p_line) && !_is_line_hidden(p_line)) {
  1512. return false;
  1513. }
  1514. int fold_start = p_line;
  1515. for (; fold_start > 0; fold_start--) {
  1516. if (is_line_folded(fold_start)) {
  1517. break;
  1518. }
  1519. }
  1520. fold_start = is_line_folded(fold_start) ? fold_start : p_line;
  1521. for (int i = fold_start + 1; i < get_line_count(); i++) {
  1522. if (!_is_line_hidden(i)) {
  1523. break;
  1524. }
  1525. _set_line_as_hidden(i, false);
  1526. if (is_line_code_region_start(i - 1)) {
  1527. set_line_background_color(i - 1, Color(0.0, 0.0, 0.0, 0.0));
  1528. }
  1529. }
  1530. return true;
  1531. }
  1532. void CodeEdit::fold_all_lines() {
  1533. bool any_line_folded = false;
  1534. for (int i = 0; i < get_line_count(); i++) {
  1535. any_line_folded |= _fold_line(i);
  1536. }
  1537. if (any_line_folded) {
  1538. emit_signal(SNAME("_fold_line_updated"));
  1539. }
  1540. }
  1541. void CodeEdit::fold_line(int p_line) {
  1542. bool line_folded = _fold_line(p_line);
  1543. if (line_folded) {
  1544. emit_signal(SNAME("_fold_line_updated"));
  1545. }
  1546. }
  1547. void CodeEdit::unfold_all_lines() {
  1548. bool any_line_unfolded = false;
  1549. for (int i = 0; i < get_line_count(); i++) {
  1550. any_line_unfolded |= _unfold_line(i);
  1551. }
  1552. if (any_line_unfolded) {
  1553. emit_signal(SNAME("_fold_line_updated"));
  1554. }
  1555. }
  1556. void CodeEdit::unfold_line(int p_line) {
  1557. bool line_unfolded = _unfold_line(p_line);
  1558. if (line_unfolded) {
  1559. emit_signal(SNAME("_fold_line_updated"));
  1560. }
  1561. }
  1562. void CodeEdit::toggle_foldable_line(int p_line) {
  1563. ERR_FAIL_INDEX(p_line, get_line_count());
  1564. if (is_line_folded(p_line)) {
  1565. unfold_line(p_line);
  1566. return;
  1567. }
  1568. fold_line(p_line);
  1569. }
  1570. void CodeEdit::toggle_foldable_lines_at_carets() {
  1571. begin_multicaret_edit();
  1572. int previous_line = -1;
  1573. Vector<int> sorted = get_sorted_carets();
  1574. for (int caret_idx : sorted) {
  1575. if (multicaret_edit_ignore_caret(caret_idx)) {
  1576. continue;
  1577. }
  1578. int line_idx = get_caret_line(caret_idx);
  1579. if (line_idx != previous_line) {
  1580. toggle_foldable_line(line_idx);
  1581. previous_line = line_idx;
  1582. }
  1583. }
  1584. end_multicaret_edit();
  1585. }
  1586. int CodeEdit::get_folded_line_header(int p_line) const {
  1587. ERR_FAIL_INDEX_V(p_line, get_line_count(), 0);
  1588. // Search for the first non hidden line.
  1589. while (p_line > 0) {
  1590. if (!_is_line_hidden(p_line)) {
  1591. break;
  1592. }
  1593. p_line--;
  1594. }
  1595. return p_line;
  1596. }
  1597. bool CodeEdit::is_line_folded(int p_line) const {
  1598. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1599. return p_line + 1 < get_line_count() && !_is_line_hidden(p_line) && _is_line_hidden(p_line + 1);
  1600. }
  1601. TypedArray<int> CodeEdit::get_folded_lines() const {
  1602. TypedArray<int> folded_lines;
  1603. for (int i = 0; i < get_line_count(); i++) {
  1604. if (is_line_folded(i)) {
  1605. folded_lines.push_back(i);
  1606. }
  1607. }
  1608. return folded_lines;
  1609. }
  1610. /* Code region */
  1611. void CodeEdit::create_code_region() {
  1612. // Abort if there is no selected text.
  1613. if (!has_selection()) {
  1614. return;
  1615. }
  1616. // Check that region tag find a comment delimiter and is valid.
  1617. if (code_region_start_string.is_empty()) {
  1618. WARN_PRINT_ONCE("Cannot create code region without any one line comment delimiters");
  1619. return;
  1620. }
  1621. String region_name = atr(ETR("New Code Region"));
  1622. begin_complex_operation();
  1623. begin_multicaret_edit();
  1624. Vector<Point2i> line_ranges = get_line_ranges_from_carets(true, false);
  1625. // Add start and end region tags.
  1626. int line_offset = 0;
  1627. for (Point2i line_range : line_ranges) {
  1628. insert_text("\n" + code_region_end_string, line_range.y + line_offset, get_line(line_range.y + line_offset).length());
  1629. insert_line_at(line_range.x + line_offset, code_region_start_string + " " + region_name);
  1630. fold_line(line_range.x + line_offset);
  1631. line_offset += 2;
  1632. }
  1633. int first_region_start = line_ranges[0].x;
  1634. // Select name of the first region to allow quick edit.
  1635. remove_secondary_carets();
  1636. int tag_length = code_region_start_string.length() + region_name.length() + 1;
  1637. select(first_region_start, code_region_start_string.length() + 1, first_region_start, tag_length);
  1638. end_multicaret_edit();
  1639. end_complex_operation();
  1640. }
  1641. String CodeEdit::get_code_region_start_tag() const {
  1642. return code_region_start_tag;
  1643. }
  1644. String CodeEdit::get_code_region_end_tag() const {
  1645. return code_region_end_tag;
  1646. }
  1647. void CodeEdit::set_code_region_tags(const String &p_start, const String &p_end) {
  1648. ERR_FAIL_COND_MSG(p_start == p_end, "Starting and ending region tags cannot be identical.");
  1649. ERR_FAIL_COND_MSG(p_start.is_empty(), "Starting region tag cannot be empty.");
  1650. ERR_FAIL_COND_MSG(p_end.is_empty(), "Ending region tag cannot be empty.");
  1651. code_region_start_tag = p_start;
  1652. code_region_end_tag = p_end;
  1653. _update_code_region_tags();
  1654. }
  1655. bool CodeEdit::is_line_code_region_start(int p_line) const {
  1656. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1657. if (code_region_start_string.is_empty()) {
  1658. return false;
  1659. }
  1660. if (is_in_string(p_line) != -1) {
  1661. return false;
  1662. }
  1663. Vector<String> split = get_line(p_line).strip_edges().split_spaces(1);
  1664. return split.size() > 0 && split[0] == code_region_start_string;
  1665. }
  1666. bool CodeEdit::is_line_code_region_end(int p_line) const {
  1667. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1668. if (code_region_start_string.is_empty()) {
  1669. return false;
  1670. }
  1671. if (is_in_string(p_line) != -1) {
  1672. return false;
  1673. }
  1674. Vector<String> split = get_line(p_line).strip_edges().split_spaces(1);
  1675. return split.size() > 0 && split[0] == code_region_end_string;
  1676. }
  1677. /* Delimiters */
  1678. // Strings
  1679. void CodeEdit::add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  1680. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_STRING);
  1681. }
  1682. void CodeEdit::remove_string_delimiter(const String &p_start_key) {
  1683. _remove_delimiter(p_start_key, TYPE_STRING);
  1684. }
  1685. bool CodeEdit::has_string_delimiter(const String &p_start_key) const {
  1686. return _has_delimiter(p_start_key, TYPE_STRING);
  1687. }
  1688. void CodeEdit::set_string_delimiters(const TypedArray<String> &p_string_delimiters) {
  1689. _set_delimiters(p_string_delimiters, TYPE_STRING);
  1690. }
  1691. void CodeEdit::clear_string_delimiters() {
  1692. _clear_delimiters(TYPE_STRING);
  1693. }
  1694. TypedArray<String> CodeEdit::get_string_delimiters() const {
  1695. return _get_delimiters(TYPE_STRING);
  1696. }
  1697. int CodeEdit::is_in_string(int p_line, int p_column) const {
  1698. return _is_in_delimiter(p_line, p_column, TYPE_STRING);
  1699. }
  1700. // Comments
  1701. void CodeEdit::add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  1702. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_COMMENT);
  1703. }
  1704. void CodeEdit::remove_comment_delimiter(const String &p_start_key) {
  1705. _remove_delimiter(p_start_key, TYPE_COMMENT);
  1706. }
  1707. bool CodeEdit::has_comment_delimiter(const String &p_start_key) const {
  1708. return _has_delimiter(p_start_key, TYPE_COMMENT);
  1709. }
  1710. void CodeEdit::set_comment_delimiters(const TypedArray<String> &p_comment_delimiters) {
  1711. _set_delimiters(p_comment_delimiters, TYPE_COMMENT);
  1712. }
  1713. void CodeEdit::clear_comment_delimiters() {
  1714. _clear_delimiters(TYPE_COMMENT);
  1715. }
  1716. TypedArray<String> CodeEdit::get_comment_delimiters() const {
  1717. return _get_delimiters(TYPE_COMMENT);
  1718. }
  1719. int CodeEdit::is_in_comment(int p_line, int p_column) const {
  1720. return _is_in_delimiter(p_line, p_column, TYPE_COMMENT);
  1721. }
  1722. String CodeEdit::get_delimiter_start_key(int p_delimiter_idx) const {
  1723. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  1724. return delimiters[p_delimiter_idx].start_key;
  1725. }
  1726. String CodeEdit::get_delimiter_end_key(int p_delimiter_idx) const {
  1727. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  1728. return delimiters[p_delimiter_idx].end_key;
  1729. }
  1730. Point2 CodeEdit::get_delimiter_start_position(int p_line, int p_column) const {
  1731. if (delimiters.is_empty()) {
  1732. return Point2(-1, -1);
  1733. }
  1734. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  1735. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  1736. Point2 start_position;
  1737. start_position.y = -1;
  1738. start_position.x = -1;
  1739. bool in_region = ((p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->get()) != -1;
  1740. /* Check the keys for this line. */
  1741. for (const KeyValue<int, int> &E : delimiter_cache[p_line]) {
  1742. if (E.key > p_column) {
  1743. break;
  1744. }
  1745. in_region = E.value != -1;
  1746. start_position.x = in_region ? E.key : -1;
  1747. }
  1748. /* Region was found on this line and is not a multiline continuation. */
  1749. int line_length = get_line(p_line).length();
  1750. if (start_position.x != -1 && line_length > 0 && start_position.x != line_length + 1) {
  1751. start_position.y = p_line;
  1752. return start_position;
  1753. }
  1754. /* Not in a region */
  1755. if (!in_region) {
  1756. return start_position;
  1757. }
  1758. /* Region starts on a previous line */
  1759. for (int i = p_line - 1; i >= 0; i--) {
  1760. if (delimiter_cache[i].size() < 1) {
  1761. continue;
  1762. }
  1763. start_position.y = i;
  1764. start_position.x = delimiter_cache[i].back()->key();
  1765. /* Make sure it's not a multiline continuation. */
  1766. line_length = get_line(i).length();
  1767. if (line_length > 0 && start_position.x != line_length + 1) {
  1768. break;
  1769. }
  1770. }
  1771. return start_position;
  1772. }
  1773. Point2 CodeEdit::get_delimiter_end_position(int p_line, int p_column) const {
  1774. if (delimiters.is_empty()) {
  1775. return Point2(-1, -1);
  1776. }
  1777. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  1778. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  1779. Point2 end_position;
  1780. end_position.y = -1;
  1781. end_position.x = -1;
  1782. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  1783. /* Check the keys for this line. */
  1784. for (const KeyValue<int, int> &E : delimiter_cache[p_line]) {
  1785. end_position.x = (E.value == -1) ? E.key : -1;
  1786. if (E.key > p_column) {
  1787. break;
  1788. }
  1789. region = E.value;
  1790. }
  1791. /* Region was found on this line and is not a multiline continuation. */
  1792. if (region != -1 && end_position.x != -1 && (delimiters[region].line_only || end_position.x != get_line(p_line).length() + 1)) {
  1793. end_position.y = p_line;
  1794. return end_position;
  1795. }
  1796. /* Not in a region */
  1797. if (region == -1) {
  1798. end_position.x = -1;
  1799. return end_position;
  1800. }
  1801. /* Region ends on a later line */
  1802. for (int i = p_line + 1; i < get_line_count(); i++) {
  1803. if (delimiter_cache[i].size() < 1 || delimiter_cache[i].front()->value() != -1) {
  1804. continue;
  1805. }
  1806. end_position.x = delimiter_cache[i].front()->key();
  1807. /* Make sure it's not a multiline continuation. */
  1808. if (get_line(i).length() > 0 && end_position.x != get_line(i).length() + 1) {
  1809. end_position.y = i;
  1810. break;
  1811. }
  1812. end_position.x = -1;
  1813. }
  1814. return end_position;
  1815. }
  1816. /* Code hint */
  1817. void CodeEdit::set_code_hint(const String &p_hint) {
  1818. if (code_hint == p_hint) {
  1819. return;
  1820. }
  1821. code_hint = p_hint;
  1822. code_hint_xpos = -0xFFFF;
  1823. queue_redraw();
  1824. }
  1825. void CodeEdit::set_code_hint_draw_below(bool p_below) {
  1826. if (code_hint_draw_below == p_below) {
  1827. return;
  1828. }
  1829. code_hint_draw_below = p_below;
  1830. queue_redraw();
  1831. }
  1832. /* Code Completion */
  1833. void CodeEdit::set_code_completion_enabled(bool p_enable) {
  1834. code_completion_enabled = p_enable;
  1835. }
  1836. bool CodeEdit::is_code_completion_enabled() const {
  1837. return code_completion_enabled;
  1838. }
  1839. void CodeEdit::set_code_completion_prefixes(const TypedArray<String> &p_prefixes) {
  1840. code_completion_prefixes.clear();
  1841. for (int i = 0; i < p_prefixes.size(); i++) {
  1842. const String prefix = p_prefixes[i];
  1843. ERR_CONTINUE_MSG(prefix.is_empty(), "Code completion prefix cannot be empty.");
  1844. code_completion_prefixes.insert(prefix[0]);
  1845. }
  1846. }
  1847. TypedArray<String> CodeEdit::get_code_completion_prefixes() const {
  1848. TypedArray<String> prefixes;
  1849. for (const char32_t &E : code_completion_prefixes) {
  1850. prefixes.push_back(String::chr(E));
  1851. }
  1852. return prefixes;
  1853. }
  1854. String CodeEdit::get_text_for_code_completion() const {
  1855. StringBuilder completion_text;
  1856. const int text_size = get_line_count();
  1857. for (int i = 0; i < text_size; i++) {
  1858. String line = get_line(i);
  1859. if (i == get_caret_line()) {
  1860. completion_text += line.substr(0, get_caret_column());
  1861. /* Not unicode, represents the caret. */
  1862. completion_text += String::chr(0xFFFF);
  1863. completion_text += line.substr(get_caret_column());
  1864. } else {
  1865. completion_text += line;
  1866. }
  1867. if (i != text_size - 1) {
  1868. completion_text += "\n";
  1869. }
  1870. }
  1871. return completion_text.as_string();
  1872. }
  1873. void CodeEdit::request_code_completion(bool p_force) {
  1874. if (GDVIRTUAL_CALL(_request_code_completion, p_force)) {
  1875. return;
  1876. }
  1877. /* Don't re-query if all existing options are quoted types, eg path, signal. */
  1878. bool ignored = code_completion_active && !code_completion_options.is_empty();
  1879. if (ignored) {
  1880. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT;
  1881. const ScriptLanguage::CodeCompletionOption *previous_option = nullptr;
  1882. for (int i = 0; i < code_completion_options.size(); i++) {
  1883. const ScriptLanguage::CodeCompletionOption &current_option = code_completion_options[i];
  1884. if (!previous_option) {
  1885. previous_option = &current_option;
  1886. kind = current_option.kind;
  1887. }
  1888. if (previous_option->kind != current_option.kind) {
  1889. ignored = false;
  1890. break;
  1891. }
  1892. }
  1893. ignored = ignored && (kind == ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH || kind == ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH || kind == ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL);
  1894. }
  1895. if (ignored) {
  1896. return;
  1897. }
  1898. if (p_force) {
  1899. emit_signal(SNAME("code_completion_requested"));
  1900. return;
  1901. }
  1902. String line = get_line(get_caret_line());
  1903. int ofs = CLAMP(get_caret_column(), 0, line.length());
  1904. if (ofs > 0 && (is_in_string(get_caret_line(), ofs) != -1 || !is_symbol(line[ofs - 1]) || code_completion_prefixes.has(line[ofs - 1]))) {
  1905. emit_signal(SNAME("code_completion_requested"));
  1906. } else if (ofs > 1 && line[ofs - 1] == ' ' && code_completion_prefixes.has(line[ofs - 2])) {
  1907. emit_signal(SNAME("code_completion_requested"));
  1908. }
  1909. }
  1910. void CodeEdit::add_code_completion_option(CodeCompletionKind p_type, const String &p_display_text, const String &p_insert_text, const Color &p_text_color, const Ref<Resource> &p_icon, const Variant &p_value, int p_location) {
  1911. ScriptLanguage::CodeCompletionOption completion_option;
  1912. completion_option.kind = (ScriptLanguage::CodeCompletionKind)p_type;
  1913. completion_option.display = p_display_text;
  1914. completion_option.insert_text = p_insert_text;
  1915. completion_option.font_color = p_text_color;
  1916. completion_option.icon = p_icon;
  1917. completion_option.default_value = p_value;
  1918. completion_option.location = p_location;
  1919. code_completion_option_submitted.push_back(completion_option);
  1920. }
  1921. void CodeEdit::update_code_completion_options(bool p_forced) {
  1922. code_completion_forced = p_forced;
  1923. code_completion_option_sources = code_completion_option_submitted;
  1924. code_completion_option_submitted.clear();
  1925. _filter_code_completion_candidates_impl();
  1926. }
  1927. TypedArray<Dictionary> CodeEdit::get_code_completion_options() const {
  1928. if (!code_completion_active) {
  1929. return TypedArray<Dictionary>();
  1930. }
  1931. TypedArray<Dictionary> completion_options;
  1932. completion_options.resize(code_completion_options.size());
  1933. for (int i = 0; i < code_completion_options.size(); i++) {
  1934. Dictionary option;
  1935. option["kind"] = code_completion_options[i].kind;
  1936. option["display_text"] = code_completion_options[i].display;
  1937. option["insert_text"] = code_completion_options[i].insert_text;
  1938. option["font_color"] = code_completion_options[i].font_color;
  1939. option["icon"] = code_completion_options[i].icon;
  1940. option["location"] = code_completion_options[i].location;
  1941. option["default_value"] = code_completion_options[i].default_value;
  1942. completion_options[i] = option;
  1943. }
  1944. return completion_options;
  1945. }
  1946. Dictionary CodeEdit::get_code_completion_option(int p_index) const {
  1947. if (!code_completion_active) {
  1948. return Dictionary();
  1949. }
  1950. ERR_FAIL_INDEX_V(p_index, code_completion_options.size(), Dictionary());
  1951. Dictionary option;
  1952. option["kind"] = code_completion_options[p_index].kind;
  1953. option["display_text"] = code_completion_options[p_index].display;
  1954. option["insert_text"] = code_completion_options[p_index].insert_text;
  1955. option["font_color"] = code_completion_options[p_index].font_color;
  1956. option["icon"] = code_completion_options[p_index].icon;
  1957. option["location"] = code_completion_options[p_index].location;
  1958. option["default_value"] = code_completion_options[p_index].default_value;
  1959. return option;
  1960. }
  1961. int CodeEdit::get_code_completion_selected_index() const {
  1962. return (code_completion_active) ? code_completion_current_selected : -1;
  1963. }
  1964. void CodeEdit::set_code_completion_selected_index(int p_index) {
  1965. if (!code_completion_active) {
  1966. return;
  1967. }
  1968. ERR_FAIL_INDEX(p_index, code_completion_options.size());
  1969. code_completion_current_selected = p_index;
  1970. code_completion_force_item_center = -1;
  1971. code_completion_pan_offset = 0.0f;
  1972. queue_redraw();
  1973. }
  1974. void CodeEdit::confirm_code_completion(bool p_replace) {
  1975. if (!is_editable() || !code_completion_active) {
  1976. return;
  1977. }
  1978. if (GDVIRTUAL_CALL(_confirm_code_completion, p_replace)) {
  1979. return;
  1980. }
  1981. char32_t caret_last_completion_char = 0;
  1982. begin_complex_operation();
  1983. begin_multicaret_edit();
  1984. for (int i = 0; i < get_caret_count(); i++) {
  1985. if (multicaret_edit_ignore_caret(i)) {
  1986. continue;
  1987. }
  1988. int caret_line = get_caret_line(i);
  1989. const String &insert_text = code_completion_options[code_completion_current_selected].insert_text;
  1990. const String &display_text = code_completion_options[code_completion_current_selected].display;
  1991. if (p_replace) {
  1992. // Find end of current section.
  1993. const String line = get_line(caret_line);
  1994. int caret_col = get_caret_column(i);
  1995. int caret_remove_line = caret_line;
  1996. bool merge_text = true;
  1997. int in_string = is_in_string(caret_line, caret_col);
  1998. if (in_string != -1) {
  1999. Point2 string_end = get_delimiter_end_position(caret_line, caret_col);
  2000. if (string_end.x != -1) {
  2001. merge_text = false;
  2002. caret_remove_line = string_end.y;
  2003. caret_col = string_end.x - 1;
  2004. }
  2005. }
  2006. if (merge_text) {
  2007. for (; caret_col < line.length(); caret_col++) {
  2008. if (is_symbol(line[caret_col])) {
  2009. break;
  2010. }
  2011. }
  2012. }
  2013. // Replace.
  2014. remove_text(caret_line, get_caret_column(i) - code_completion_base.length(), caret_remove_line, caret_col);
  2015. insert_text_at_caret(insert_text, i);
  2016. } else {
  2017. // Get first non-matching char.
  2018. const String line = get_line(caret_line);
  2019. int caret_col = get_caret_column(i);
  2020. int matching_chars = code_completion_base.length();
  2021. for (; matching_chars <= insert_text.length(); matching_chars++) {
  2022. if (caret_col >= line.length() || line[caret_col] != insert_text[matching_chars]) {
  2023. break;
  2024. }
  2025. caret_col++;
  2026. }
  2027. // Remove base completion text.
  2028. remove_text(caret_line, get_caret_column(i) - code_completion_base.length(), caret_line, get_caret_column(i));
  2029. // Merge with text.
  2030. insert_text_at_caret(insert_text.substr(0, code_completion_base.length()), i);
  2031. set_caret_column(caret_col, false, i);
  2032. insert_text_at_caret(insert_text.substr(matching_chars), i);
  2033. }
  2034. // Handle merging of symbols eg strings, brackets.
  2035. const String line = get_line(caret_line);
  2036. char32_t next_char = line[get_caret_column(i)];
  2037. char32_t last_completion_char = insert_text[insert_text.length() - 1];
  2038. if (i == 0) {
  2039. caret_last_completion_char = last_completion_char;
  2040. }
  2041. char32_t last_completion_char_display = display_text[display_text.length() - 1];
  2042. bool last_char_matches = (last_completion_char == next_char || last_completion_char_display == next_char);
  2043. int pre_brace_pair = get_caret_column(i) > 0 ? _get_auto_brace_pair_open_at_pos(caret_line, get_caret_column(i)) : -1;
  2044. int post_brace_pair = get_caret_column(i) < get_line(caret_line).length() ? _get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i)) : -1;
  2045. // Strings do not nest like brackets, so ensure we don't add an additional closing pair.
  2046. if (has_string_delimiter(String::chr(last_completion_char))) {
  2047. if (post_brace_pair != -1 && last_char_matches) {
  2048. remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
  2049. }
  2050. } else {
  2051. if (pre_brace_pair != -1 && pre_brace_pair != post_brace_pair && last_char_matches) {
  2052. remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
  2053. } else if (auto_brace_completion_enabled && pre_brace_pair != -1) {
  2054. insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key, i);
  2055. set_caret_column(get_caret_column(i) - auto_brace_completion_pairs[pre_brace_pair].close_key.length(), i == 0, i);
  2056. }
  2057. }
  2058. if (pre_brace_pair == -1 && post_brace_pair == -1 && get_caret_column(i) > 0 && get_caret_column(i) < get_line(caret_line).length()) {
  2059. pre_brace_pair = _get_auto_brace_pair_open_at_pos(caret_line, get_caret_column(i) + 1);
  2060. if (pre_brace_pair != -1 && pre_brace_pair == _get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i) - 1)) {
  2061. remove_text(caret_line, get_caret_column(i) - 2, caret_line, get_caret_column(i));
  2062. if (_get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i) + 1) != pre_brace_pair) {
  2063. set_caret_column(get_caret_column(i) + 1, i == 0, i);
  2064. } else {
  2065. set_caret_column(get_caret_column(i) + 2, i == 0, i);
  2066. }
  2067. }
  2068. }
  2069. }
  2070. end_multicaret_edit();
  2071. end_complex_operation();
  2072. cancel_code_completion();
  2073. if (code_completion_prefixes.has(caret_last_completion_char)) {
  2074. request_code_completion();
  2075. }
  2076. }
  2077. void CodeEdit::cancel_code_completion() {
  2078. if (!code_completion_active) {
  2079. return;
  2080. }
  2081. code_completion_forced = false;
  2082. code_completion_active = false;
  2083. is_code_completion_drag_started = false;
  2084. queue_redraw();
  2085. }
  2086. /* Line length guidelines */
  2087. void CodeEdit::set_line_length_guidelines(TypedArray<int> p_guideline_columns) {
  2088. line_length_guideline_columns = p_guideline_columns;
  2089. queue_redraw();
  2090. }
  2091. TypedArray<int> CodeEdit::get_line_length_guidelines() const {
  2092. return line_length_guideline_columns;
  2093. }
  2094. /* Symbol lookup */
  2095. void CodeEdit::set_symbol_lookup_on_click_enabled(bool p_enabled) {
  2096. symbol_lookup_on_click_enabled = p_enabled;
  2097. set_symbol_lookup_word_as_valid(false);
  2098. }
  2099. bool CodeEdit::is_symbol_lookup_on_click_enabled() const {
  2100. return symbol_lookup_on_click_enabled;
  2101. }
  2102. String CodeEdit::get_text_for_symbol_lookup() const {
  2103. Point2i mp = get_local_mouse_pos();
  2104. Point2i pos = get_line_column_at_pos(mp, false, false);
  2105. int line = pos.y;
  2106. int col = pos.x;
  2107. if (line == -1) {
  2108. return String();
  2109. }
  2110. return get_text_with_cursor_char(line, col);
  2111. }
  2112. String CodeEdit::get_text_with_cursor_char(int p_line, int p_column) const {
  2113. const int text_size = get_line_count();
  2114. StringBuilder result;
  2115. for (int i = 0; i < text_size; i++) {
  2116. String line_text = get_line(i);
  2117. if (i == p_line && p_column >= 0 && p_column <= line_text.size()) {
  2118. result += line_text.substr(0, p_column);
  2119. /* Not unicode, represents the cursor. */
  2120. result += String::chr(0xFFFF);
  2121. result += line_text.substr(p_column);
  2122. } else {
  2123. result += line_text;
  2124. }
  2125. if (i != text_size - 1) {
  2126. result += "\n";
  2127. }
  2128. }
  2129. return result.as_string();
  2130. }
  2131. String CodeEdit::get_lookup_word(int p_line, int p_column) const {
  2132. if (p_line < 0 || p_column < 0) {
  2133. return String();
  2134. }
  2135. if (is_in_string(p_line, p_column) != -1) {
  2136. // Return the string in case it is a path.
  2137. Point2 start_pos = get_delimiter_start_position(p_line, p_column);
  2138. Point2 end_pos = get_delimiter_end_position(p_line, p_column);
  2139. int start_line = start_pos.y;
  2140. int start_column = start_pos.x;
  2141. int end_line = end_pos.y;
  2142. int end_column = end_pos.x;
  2143. if (start_line == end_line && start_column >= 0 && end_column >= 0) {
  2144. return get_line(start_line).substr(start_column, end_column - start_column - 1);
  2145. }
  2146. }
  2147. return get_word(p_line, p_column);
  2148. }
  2149. void CodeEdit::set_symbol_lookup_word_as_valid(bool p_valid) {
  2150. symbol_lookup_word = p_valid ? symbol_lookup_new_word : "";
  2151. symbol_lookup_new_word = "";
  2152. if (lookup_symbol_word != symbol_lookup_word) {
  2153. _set_symbol_lookup_word(symbol_lookup_word);
  2154. }
  2155. }
  2156. /* Symbol tooltip */
  2157. void CodeEdit::set_symbol_tooltip_on_hover_enabled(bool p_enabled) {
  2158. symbol_tooltip_on_hover_enabled = p_enabled;
  2159. if (!p_enabled) {
  2160. symbol_tooltip_timer->stop();
  2161. }
  2162. }
  2163. bool CodeEdit::is_symbol_tooltip_on_hover_enabled() const {
  2164. return symbol_tooltip_on_hover_enabled;
  2165. }
  2166. void CodeEdit::_on_symbol_tooltip_timer_timeout() {
  2167. const int line = symbol_tooltip_pos.y;
  2168. const int column = symbol_tooltip_pos.x;
  2169. if (line >= 0 && column >= 0 && !symbol_tooltip_word.is_empty() && !Input::get_singleton()->is_anything_pressed()) {
  2170. emit_signal(SNAME("symbol_hovered"), symbol_tooltip_word, line, column);
  2171. }
  2172. }
  2173. /* Text manipulation */
  2174. void CodeEdit::move_lines_up() {
  2175. begin_complex_operation();
  2176. begin_multicaret_edit();
  2177. // Move lines up by swapping each line with the one above it.
  2178. Vector<Point2i> line_ranges = get_line_ranges_from_carets();
  2179. for (Point2i line_range : line_ranges) {
  2180. if (line_range.x == 0) {
  2181. continue;
  2182. }
  2183. unfold_line(line_range.x - 1);
  2184. for (int line = line_range.x; line <= line_range.y; line++) {
  2185. unfold_line(line);
  2186. swap_lines(line - 1, line);
  2187. }
  2188. // Fix selection if the last one ends at column 0, since it wasn't moved.
  2189. for (int i = 0; i < get_caret_count(); i++) {
  2190. if (has_selection(i) && get_selection_to_column(i) == 0 && get_selection_to_line(i) == line_range.y + 1) {
  2191. if (is_caret_after_selection_origin(i)) {
  2192. set_caret_line(get_caret_line(i) - 1, false, true, -1, i);
  2193. } else {
  2194. set_selection_origin_line(get_selection_origin_line(i) - 1, true, -1, i);
  2195. }
  2196. break;
  2197. }
  2198. }
  2199. }
  2200. adjust_viewport_to_caret();
  2201. end_multicaret_edit();
  2202. end_complex_operation();
  2203. }
  2204. void CodeEdit::move_lines_down() {
  2205. begin_complex_operation();
  2206. begin_multicaret_edit();
  2207. // Move lines down by swapping each line with the one below it.
  2208. Vector<Point2i> line_ranges = get_line_ranges_from_carets();
  2209. // Reverse in case line ranges are adjacent, if the first ends at column 0.
  2210. line_ranges.reverse();
  2211. for (Point2i line_range : line_ranges) {
  2212. if (line_range.y == get_line_count() - 1) {
  2213. continue;
  2214. }
  2215. // Fix selection if the last one ends at column 0, since it won't be moved.
  2216. bool selection_to_line_at_end = false;
  2217. for (int i = 0; i < get_caret_count(); i++) {
  2218. if (has_selection(i) && get_selection_to_column(i) == 0 && get_selection_to_line(i) == line_range.y + 1) {
  2219. selection_to_line_at_end = get_selection_to_line(i) == get_line_count() - 1;
  2220. if (selection_to_line_at_end) {
  2221. break;
  2222. }
  2223. if (is_caret_after_selection_origin(i)) {
  2224. set_caret_line(get_caret_line(i) + 1, false, true, -1, i);
  2225. } else {
  2226. set_selection_origin_line(get_selection_origin_line(i) + 1, true, -1, i);
  2227. }
  2228. break;
  2229. }
  2230. }
  2231. if (selection_to_line_at_end) {
  2232. continue;
  2233. }
  2234. unfold_line(line_range.y + 1);
  2235. for (int line = line_range.y; line >= line_range.x; line--) {
  2236. unfold_line(line);
  2237. swap_lines(line + 1, line);
  2238. }
  2239. }
  2240. adjust_viewport_to_caret();
  2241. end_multicaret_edit();
  2242. end_complex_operation();
  2243. }
  2244. void CodeEdit::delete_lines() {
  2245. begin_complex_operation();
  2246. begin_multicaret_edit();
  2247. Vector<Point2i> line_ranges = get_line_ranges_from_carets();
  2248. int line_offset = 0;
  2249. for (Point2i line_range : line_ranges) {
  2250. // Remove last line of range separately to preserve carets.
  2251. unfold_line(line_range.y + line_offset);
  2252. remove_line_at(line_range.y + line_offset);
  2253. if (line_range.x != line_range.y) {
  2254. remove_text(line_range.x + line_offset, 0, line_range.y + line_offset, 0);
  2255. }
  2256. line_offset += line_range.x - line_range.y - 1;
  2257. }
  2258. // Deselect all.
  2259. deselect();
  2260. end_multicaret_edit();
  2261. end_complex_operation();
  2262. }
  2263. void CodeEdit::duplicate_selection() {
  2264. begin_complex_operation();
  2265. begin_multicaret_edit();
  2266. // Duplicate lines from carets without selections first.
  2267. for (int i = 0; i < get_caret_count(); i++) {
  2268. if (multicaret_edit_ignore_caret(i)) {
  2269. continue;
  2270. }
  2271. for (int l = get_selection_from_line(i); l <= get_selection_to_line(i); l++) {
  2272. unfold_line(l);
  2273. }
  2274. if (has_selection(i)) {
  2275. continue;
  2276. }
  2277. String text_to_insert = get_line(get_caret_line(i)) + "\n";
  2278. // Insert new text before the line, so the caret is on the second one.
  2279. insert_text(text_to_insert, get_caret_line(i), 0);
  2280. }
  2281. // Duplicate selections.
  2282. for (int i = 0; i < get_caret_count(); i++) {
  2283. if (multicaret_edit_ignore_caret(i)) {
  2284. continue;
  2285. }
  2286. if (!has_selection(i)) {
  2287. continue;
  2288. }
  2289. // Insert new text before the selection, so the caret is on the second one.
  2290. insert_text(get_selected_text(i), get_selection_from_line(i), get_selection_from_column(i));
  2291. }
  2292. end_multicaret_edit();
  2293. end_complex_operation();
  2294. }
  2295. void CodeEdit::duplicate_lines() {
  2296. begin_complex_operation();
  2297. begin_multicaret_edit();
  2298. Vector<Point2i> line_ranges = get_line_ranges_from_carets(false, false);
  2299. int line_offset = 0;
  2300. for (Point2i line_range : line_ranges) {
  2301. // The text that will be inserted. All lines in one string.
  2302. String text_to_insert;
  2303. for (int i = line_range.x + line_offset; i <= line_range.y + line_offset; i++) {
  2304. text_to_insert += get_line(i) + "\n";
  2305. unfold_line(i);
  2306. }
  2307. // Insert new text before the line.
  2308. insert_text(text_to_insert, line_range.x + line_offset, 0);
  2309. line_offset += line_range.y - line_range.x + 1;
  2310. }
  2311. end_multicaret_edit();
  2312. end_complex_operation();
  2313. }
  2314. /* Visual */
  2315. Color CodeEdit::_get_brace_mismatch_color() const {
  2316. return theme_cache.brace_mismatch_color;
  2317. }
  2318. Color CodeEdit::_get_code_folding_color() const {
  2319. return theme_cache.code_folding_color;
  2320. }
  2321. Ref<Texture2D> CodeEdit::_get_folded_eol_icon() const {
  2322. return theme_cache.folded_eol_icon;
  2323. }
  2324. void CodeEdit::_bind_methods() {
  2325. /* Indent management */
  2326. ClassDB::bind_method(D_METHOD("set_indent_size", "size"), &CodeEdit::set_indent_size);
  2327. ClassDB::bind_method(D_METHOD("get_indent_size"), &CodeEdit::get_indent_size);
  2328. ClassDB::bind_method(D_METHOD("set_indent_using_spaces", "use_spaces"), &CodeEdit::set_indent_using_spaces);
  2329. ClassDB::bind_method(D_METHOD("is_indent_using_spaces"), &CodeEdit::is_indent_using_spaces);
  2330. ClassDB::bind_method(D_METHOD("set_auto_indent_enabled", "enable"), &CodeEdit::set_auto_indent_enabled);
  2331. ClassDB::bind_method(D_METHOD("is_auto_indent_enabled"), &CodeEdit::is_auto_indent_enabled);
  2332. ClassDB::bind_method(D_METHOD("set_auto_indent_prefixes", "prefixes"), &CodeEdit::set_auto_indent_prefixes);
  2333. ClassDB::bind_method(D_METHOD("get_auto_indent_prefixes"), &CodeEdit::get_auto_indent_prefixes);
  2334. ClassDB::bind_method(D_METHOD("do_indent"), &CodeEdit::do_indent);
  2335. ClassDB::bind_method(D_METHOD("indent_lines"), &CodeEdit::indent_lines);
  2336. ClassDB::bind_method(D_METHOD("unindent_lines"), &CodeEdit::unindent_lines);
  2337. ClassDB::bind_method(D_METHOD("convert_indent", "from_line", "to_line"), &CodeEdit::convert_indent, DEFVAL(-1), DEFVAL(-1));
  2338. /* Auto brace completion */
  2339. ClassDB::bind_method(D_METHOD("set_auto_brace_completion_enabled", "enable"), &CodeEdit::set_auto_brace_completion_enabled);
  2340. ClassDB::bind_method(D_METHOD("is_auto_brace_completion_enabled"), &CodeEdit::is_auto_brace_completion_enabled);
  2341. ClassDB::bind_method(D_METHOD("set_highlight_matching_braces_enabled", "enable"), &CodeEdit::set_highlight_matching_braces_enabled);
  2342. ClassDB::bind_method(D_METHOD("is_highlight_matching_braces_enabled"), &CodeEdit::is_highlight_matching_braces_enabled);
  2343. ClassDB::bind_method(D_METHOD("add_auto_brace_completion_pair", "start_key", "end_key"), &CodeEdit::add_auto_brace_completion_pair);
  2344. ClassDB::bind_method(D_METHOD("set_auto_brace_completion_pairs", "pairs"), &CodeEdit::set_auto_brace_completion_pairs);
  2345. ClassDB::bind_method(D_METHOD("get_auto_brace_completion_pairs"), &CodeEdit::get_auto_brace_completion_pairs);
  2346. ClassDB::bind_method(D_METHOD("has_auto_brace_completion_open_key", "open_key"), &CodeEdit::has_auto_brace_completion_open_key);
  2347. ClassDB::bind_method(D_METHOD("has_auto_brace_completion_close_key", "close_key"), &CodeEdit::has_auto_brace_completion_close_key);
  2348. ClassDB::bind_method(D_METHOD("get_auto_brace_completion_close_key", "open_key"), &CodeEdit::get_auto_brace_completion_close_key);
  2349. /* Main Gutter */
  2350. ClassDB::bind_method(D_METHOD("set_draw_breakpoints_gutter", "enable"), &CodeEdit::set_draw_breakpoints_gutter);
  2351. ClassDB::bind_method(D_METHOD("is_drawing_breakpoints_gutter"), &CodeEdit::is_drawing_breakpoints_gutter);
  2352. ClassDB::bind_method(D_METHOD("set_draw_bookmarks_gutter", "enable"), &CodeEdit::set_draw_bookmarks_gutter);
  2353. ClassDB::bind_method(D_METHOD("is_drawing_bookmarks_gutter"), &CodeEdit::is_drawing_bookmarks_gutter);
  2354. ClassDB::bind_method(D_METHOD("set_draw_executing_lines_gutter", "enable"), &CodeEdit::set_draw_executing_lines_gutter);
  2355. ClassDB::bind_method(D_METHOD("is_drawing_executing_lines_gutter"), &CodeEdit::is_drawing_executing_lines_gutter);
  2356. // Breakpoints
  2357. ClassDB::bind_method(D_METHOD("set_line_as_breakpoint", "line", "breakpointed"), &CodeEdit::set_line_as_breakpoint);
  2358. ClassDB::bind_method(D_METHOD("is_line_breakpointed", "line"), &CodeEdit::is_line_breakpointed);
  2359. ClassDB::bind_method(D_METHOD("clear_breakpointed_lines"), &CodeEdit::clear_breakpointed_lines);
  2360. ClassDB::bind_method(D_METHOD("get_breakpointed_lines"), &CodeEdit::get_breakpointed_lines);
  2361. // Bookmarks
  2362. ClassDB::bind_method(D_METHOD("set_line_as_bookmarked", "line", "bookmarked"), &CodeEdit::set_line_as_bookmarked);
  2363. ClassDB::bind_method(D_METHOD("is_line_bookmarked", "line"), &CodeEdit::is_line_bookmarked);
  2364. ClassDB::bind_method(D_METHOD("clear_bookmarked_lines"), &CodeEdit::clear_bookmarked_lines);
  2365. ClassDB::bind_method(D_METHOD("get_bookmarked_lines"), &CodeEdit::get_bookmarked_lines);
  2366. // Executing lines
  2367. ClassDB::bind_method(D_METHOD("set_line_as_executing", "line", "executing"), &CodeEdit::set_line_as_executing);
  2368. ClassDB::bind_method(D_METHOD("is_line_executing", "line"), &CodeEdit::is_line_executing);
  2369. ClassDB::bind_method(D_METHOD("clear_executing_lines"), &CodeEdit::clear_executing_lines);
  2370. ClassDB::bind_method(D_METHOD("get_executing_lines"), &CodeEdit::get_executing_lines);
  2371. /* Line numbers */
  2372. ClassDB::bind_method(D_METHOD("set_draw_line_numbers", "enable"), &CodeEdit::set_draw_line_numbers);
  2373. ClassDB::bind_method(D_METHOD("is_draw_line_numbers_enabled"), &CodeEdit::is_draw_line_numbers_enabled);
  2374. ClassDB::bind_method(D_METHOD("set_line_numbers_zero_padded", "enable"), &CodeEdit::set_line_numbers_zero_padded);
  2375. ClassDB::bind_method(D_METHOD("is_line_numbers_zero_padded"), &CodeEdit::is_line_numbers_zero_padded);
  2376. /* Fold Gutter */
  2377. ClassDB::bind_method(D_METHOD("set_draw_fold_gutter", "enable"), &CodeEdit::set_draw_fold_gutter);
  2378. ClassDB::bind_method(D_METHOD("is_drawing_fold_gutter"), &CodeEdit::is_drawing_fold_gutter);
  2379. /* Line folding */
  2380. ClassDB::bind_method(D_METHOD("set_line_folding_enabled", "enabled"), &CodeEdit::set_line_folding_enabled);
  2381. ClassDB::bind_method(D_METHOD("is_line_folding_enabled"), &CodeEdit::is_line_folding_enabled);
  2382. ClassDB::bind_method(D_METHOD("can_fold_line", "line"), &CodeEdit::can_fold_line);
  2383. ClassDB::bind_method(D_METHOD("fold_line", "line"), &CodeEdit::fold_line);
  2384. ClassDB::bind_method(D_METHOD("unfold_line", "line"), &CodeEdit::unfold_line);
  2385. ClassDB::bind_method(D_METHOD("fold_all_lines"), &CodeEdit::fold_all_lines);
  2386. ClassDB::bind_method(D_METHOD("unfold_all_lines"), &CodeEdit::unfold_all_lines);
  2387. ClassDB::bind_method(D_METHOD("toggle_foldable_line", "line"), &CodeEdit::toggle_foldable_line);
  2388. ClassDB::bind_method(D_METHOD("toggle_foldable_lines_at_carets"), &CodeEdit::toggle_foldable_lines_at_carets);
  2389. ClassDB::bind_method(D_METHOD("is_line_folded", "line"), &CodeEdit::is_line_folded);
  2390. ClassDB::bind_method(D_METHOD("get_folded_lines"), &CodeEdit::get_folded_lines);
  2391. /* Code region */
  2392. ClassDB::bind_method(D_METHOD("create_code_region"), &CodeEdit::create_code_region);
  2393. ClassDB::bind_method(D_METHOD("get_code_region_start_tag"), &CodeEdit::get_code_region_start_tag);
  2394. ClassDB::bind_method(D_METHOD("get_code_region_end_tag"), &CodeEdit::get_code_region_end_tag);
  2395. ClassDB::bind_method(D_METHOD("set_code_region_tags", "start", "end"), &CodeEdit::set_code_region_tags, DEFVAL("region"), DEFVAL("endregion"));
  2396. ClassDB::bind_method(D_METHOD("is_line_code_region_start", "line"), &CodeEdit::is_line_code_region_start);
  2397. ClassDB::bind_method(D_METHOD("is_line_code_region_end", "line"), &CodeEdit::is_line_code_region_end);
  2398. /* Delimiters */
  2399. // Strings
  2400. ClassDB::bind_method(D_METHOD("add_string_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_string_delimiter, DEFVAL(false));
  2401. ClassDB::bind_method(D_METHOD("remove_string_delimiter", "start_key"), &CodeEdit::remove_string_delimiter);
  2402. ClassDB::bind_method(D_METHOD("has_string_delimiter", "start_key"), &CodeEdit::has_string_delimiter);
  2403. ClassDB::bind_method(D_METHOD("set_string_delimiters", "string_delimiters"), &CodeEdit::set_string_delimiters);
  2404. ClassDB::bind_method(D_METHOD("clear_string_delimiters"), &CodeEdit::clear_string_delimiters);
  2405. ClassDB::bind_method(D_METHOD("get_string_delimiters"), &CodeEdit::get_string_delimiters);
  2406. ClassDB::bind_method(D_METHOD("is_in_string", "line", "column"), &CodeEdit::is_in_string, DEFVAL(-1));
  2407. // Comments
  2408. ClassDB::bind_method(D_METHOD("add_comment_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_comment_delimiter, DEFVAL(false));
  2409. ClassDB::bind_method(D_METHOD("remove_comment_delimiter", "start_key"), &CodeEdit::remove_comment_delimiter);
  2410. ClassDB::bind_method(D_METHOD("has_comment_delimiter", "start_key"), &CodeEdit::has_comment_delimiter);
  2411. ClassDB::bind_method(D_METHOD("set_comment_delimiters", "comment_delimiters"), &CodeEdit::set_comment_delimiters);
  2412. ClassDB::bind_method(D_METHOD("clear_comment_delimiters"), &CodeEdit::clear_comment_delimiters);
  2413. ClassDB::bind_method(D_METHOD("get_comment_delimiters"), &CodeEdit::get_comment_delimiters);
  2414. ClassDB::bind_method(D_METHOD("is_in_comment", "line", "column"), &CodeEdit::is_in_comment, DEFVAL(-1));
  2415. // Util
  2416. ClassDB::bind_method(D_METHOD("get_delimiter_start_key", "delimiter_index"), &CodeEdit::get_delimiter_start_key);
  2417. ClassDB::bind_method(D_METHOD("get_delimiter_end_key", "delimiter_index"), &CodeEdit::get_delimiter_end_key);
  2418. ClassDB::bind_method(D_METHOD("get_delimiter_start_position", "line", "column"), &CodeEdit::get_delimiter_start_position);
  2419. ClassDB::bind_method(D_METHOD("get_delimiter_end_position", "line", "column"), &CodeEdit::get_delimiter_end_position);
  2420. /* Code hint */
  2421. ClassDB::bind_method(D_METHOD("set_code_hint", "code_hint"), &CodeEdit::set_code_hint);
  2422. ClassDB::bind_method(D_METHOD("set_code_hint_draw_below", "draw_below"), &CodeEdit::set_code_hint_draw_below);
  2423. /* Code Completion */
  2424. BIND_ENUM_CONSTANT(KIND_CLASS);
  2425. BIND_ENUM_CONSTANT(KIND_FUNCTION);
  2426. BIND_ENUM_CONSTANT(KIND_SIGNAL);
  2427. BIND_ENUM_CONSTANT(KIND_VARIABLE);
  2428. BIND_ENUM_CONSTANT(KIND_MEMBER);
  2429. BIND_ENUM_CONSTANT(KIND_ENUM);
  2430. BIND_ENUM_CONSTANT(KIND_CONSTANT);
  2431. BIND_ENUM_CONSTANT(KIND_NODE_PATH);
  2432. BIND_ENUM_CONSTANT(KIND_FILE_PATH);
  2433. BIND_ENUM_CONSTANT(KIND_PLAIN_TEXT);
  2434. BIND_ENUM_CONSTANT(LOCATION_LOCAL);
  2435. BIND_ENUM_CONSTANT(LOCATION_PARENT_MASK);
  2436. BIND_ENUM_CONSTANT(LOCATION_OTHER_USER_CODE)
  2437. BIND_ENUM_CONSTANT(LOCATION_OTHER);
  2438. ClassDB::bind_method(D_METHOD("get_text_for_code_completion"), &CodeEdit::get_text_for_code_completion);
  2439. ClassDB::bind_method(D_METHOD("request_code_completion", "force"), &CodeEdit::request_code_completion, DEFVAL(false));
  2440. ClassDB::bind_method(D_METHOD("add_code_completion_option", "type", "display_text", "insert_text", "text_color", "icon", "value", "location"), &CodeEdit::add_code_completion_option, DEFVAL(Color(1, 1, 1)), DEFVAL(Ref<Resource>()), DEFVAL(Variant()), DEFVAL(LOCATION_OTHER));
  2441. ClassDB::bind_method(D_METHOD("update_code_completion_options", "force"), &CodeEdit::update_code_completion_options);
  2442. ClassDB::bind_method(D_METHOD("get_code_completion_options"), &CodeEdit::get_code_completion_options);
  2443. ClassDB::bind_method(D_METHOD("get_code_completion_option", "index"), &CodeEdit::get_code_completion_option);
  2444. ClassDB::bind_method(D_METHOD("get_code_completion_selected_index"), &CodeEdit::get_code_completion_selected_index);
  2445. ClassDB::bind_method(D_METHOD("set_code_completion_selected_index", "index"), &CodeEdit::set_code_completion_selected_index);
  2446. ClassDB::bind_method(D_METHOD("confirm_code_completion", "replace"), &CodeEdit::confirm_code_completion, DEFVAL(false));
  2447. ClassDB::bind_method(D_METHOD("cancel_code_completion"), &CodeEdit::cancel_code_completion);
  2448. ClassDB::bind_method(D_METHOD("set_code_completion_enabled", "enable"), &CodeEdit::set_code_completion_enabled);
  2449. ClassDB::bind_method(D_METHOD("is_code_completion_enabled"), &CodeEdit::is_code_completion_enabled);
  2450. ClassDB::bind_method(D_METHOD("set_code_completion_prefixes", "prefixes"), &CodeEdit::set_code_completion_prefixes);
  2451. ClassDB::bind_method(D_METHOD("get_code_completion_prefixes"), &CodeEdit::get_code_completion_prefixes);
  2452. // Overridable
  2453. GDVIRTUAL_BIND(_confirm_code_completion, "replace")
  2454. GDVIRTUAL_BIND(_request_code_completion, "force")
  2455. GDVIRTUAL_BIND(_filter_code_completion_candidates, "candidates")
  2456. /* Line length guidelines */
  2457. ClassDB::bind_method(D_METHOD("set_line_length_guidelines", "guideline_columns"), &CodeEdit::set_line_length_guidelines);
  2458. ClassDB::bind_method(D_METHOD("get_line_length_guidelines"), &CodeEdit::get_line_length_guidelines);
  2459. /* Symbol lookup */
  2460. ClassDB::bind_method(D_METHOD("set_symbol_lookup_on_click_enabled", "enable"), &CodeEdit::set_symbol_lookup_on_click_enabled);
  2461. ClassDB::bind_method(D_METHOD("is_symbol_lookup_on_click_enabled"), &CodeEdit::is_symbol_lookup_on_click_enabled);
  2462. ClassDB::bind_method(D_METHOD("get_text_for_symbol_lookup"), &CodeEdit::get_text_for_symbol_lookup);
  2463. ClassDB::bind_method(D_METHOD("get_text_with_cursor_char", "line", "column"), &CodeEdit::get_text_with_cursor_char);
  2464. ClassDB::bind_method(D_METHOD("set_symbol_lookup_word_as_valid", "valid"), &CodeEdit::set_symbol_lookup_word_as_valid);
  2465. /* Symbol tooltip */
  2466. ClassDB::bind_method(D_METHOD("set_symbol_tooltip_on_hover_enabled", "enable"), &CodeEdit::set_symbol_tooltip_on_hover_enabled);
  2467. ClassDB::bind_method(D_METHOD("is_symbol_tooltip_on_hover_enabled"), &CodeEdit::is_symbol_tooltip_on_hover_enabled);
  2468. /* Text manipulation */
  2469. ClassDB::bind_method(D_METHOD("move_lines_up"), &CodeEdit::move_lines_up);
  2470. ClassDB::bind_method(D_METHOD("move_lines_down"), &CodeEdit::move_lines_down);
  2471. ClassDB::bind_method(D_METHOD("delete_lines"), &CodeEdit::delete_lines);
  2472. ClassDB::bind_method(D_METHOD("duplicate_selection"), &CodeEdit::duplicate_selection);
  2473. ClassDB::bind_method(D_METHOD("duplicate_lines"), &CodeEdit::duplicate_lines);
  2474. /* Inspector */
  2475. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "symbol_lookup_on_click"), "set_symbol_lookup_on_click_enabled", "is_symbol_lookup_on_click_enabled");
  2476. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "symbol_tooltip_on_hover"), "set_symbol_tooltip_on_hover_enabled", "is_symbol_tooltip_on_hover_enabled");
  2477. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "line_folding"), "set_line_folding_enabled", "is_line_folding_enabled");
  2478. ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "line_length_guidelines"), "set_line_length_guidelines", "get_line_length_guidelines");
  2479. ADD_GROUP("Gutters", "gutters_");
  2480. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_breakpoints_gutter"), "set_draw_breakpoints_gutter", "is_drawing_breakpoints_gutter");
  2481. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_bookmarks"), "set_draw_bookmarks_gutter", "is_drawing_bookmarks_gutter");
  2482. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_executing_lines"), "set_draw_executing_lines_gutter", "is_drawing_executing_lines_gutter");
  2483. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_line_numbers"), "set_draw_line_numbers", "is_draw_line_numbers_enabled");
  2484. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_zero_pad_line_numbers"), "set_line_numbers_zero_padded", "is_line_numbers_zero_padded");
  2485. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_fold_gutter"), "set_draw_fold_gutter", "is_drawing_fold_gutter");
  2486. ADD_GROUP("Delimiters", "delimiter_");
  2487. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_strings"), "set_string_delimiters", "get_string_delimiters");
  2488. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_comments"), "set_comment_delimiters", "get_comment_delimiters");
  2489. ADD_GROUP("Code Completion", "code_completion_");
  2490. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "code_completion_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_code_completion_enabled", "is_code_completion_enabled");
  2491. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "code_completion_prefixes"), "set_code_completion_prefixes", "get_code_completion_prefixes");
  2492. ADD_GROUP("Indentation", "indent_");
  2493. ADD_PROPERTY(PropertyInfo(Variant::INT, "indent_size"), "set_indent_size", "get_indent_size");
  2494. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_use_spaces"), "set_indent_using_spaces", "is_indent_using_spaces");
  2495. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_automatic"), "set_auto_indent_enabled", "is_auto_indent_enabled");
  2496. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "indent_automatic_prefixes"), "set_auto_indent_prefixes", "get_auto_indent_prefixes");
  2497. ADD_GROUP("Auto Brace Completion", "auto_brace_completion_");
  2498. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_auto_brace_completion_enabled", "is_auto_brace_completion_enabled");
  2499. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_highlight_matching"), "set_highlight_matching_braces_enabled", "is_highlight_matching_braces_enabled");
  2500. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "auto_brace_completion_pairs", PROPERTY_HINT_TYPE_STRING, "String;String"), "set_auto_brace_completion_pairs", "get_auto_brace_completion_pairs");
  2501. /* Signals */
  2502. /* Gutters */
  2503. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "line")));
  2504. /* Code Completion */
  2505. ADD_SIGNAL(MethodInfo("code_completion_requested"));
  2506. /* Symbol lookup */
  2507. ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "column")));
  2508. ADD_SIGNAL(MethodInfo("symbol_validate", PropertyInfo(Variant::STRING, "symbol")));
  2509. /* Symbol tooltip */
  2510. ADD_SIGNAL(MethodInfo("symbol_hovered", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "column")));
  2511. /* Theme items */
  2512. /* Gutters */
  2513. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, code_folding_color);
  2514. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, folded_code_region_color);
  2515. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, can_fold_icon, "can_fold");
  2516. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, folded_icon, "folded");
  2517. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, can_fold_code_region_icon, "can_fold_code_region");
  2518. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, folded_code_region_icon, "folded_code_region");
  2519. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CodeEdit, folded_eol_icon);
  2520. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CodeEdit, completion_color_bg);
  2521. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, breakpoint_color);
  2522. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, breakpoint_icon, "breakpoint");
  2523. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, bookmark_color);
  2524. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, bookmark_icon, "bookmark");
  2525. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, executing_line_color);
  2526. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, executing_line_icon, "executing_line");
  2527. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, line_number_color);
  2528. /* Code Completion */
  2529. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CodeEdit, code_completion_style, "completion");
  2530. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_icon_separation, "h_separation", "ItemList");
  2531. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_max_width, "completion_max_width");
  2532. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_max_lines, "completion_lines");
  2533. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_scroll_width, "completion_scroll_width");
  2534. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_scroll_color, "completion_scroll_color");
  2535. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_scroll_hovered_color, "completion_scroll_hovered_color");
  2536. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_background_color, "completion_background_color");
  2537. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_selected_color, "completion_selected_color");
  2538. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_existing_color, "completion_existing_color");
  2539. /* Code hint */
  2540. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, CodeEdit, code_hint_style, "panel", "TooltipPanel");
  2541. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, CodeEdit, code_hint_color, "font_color", "TooltipLabel");
  2542. /* Line length guideline */
  2543. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, line_length_guideline_color);
  2544. /* Other visuals */
  2545. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CodeEdit, style_normal, "normal");
  2546. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, brace_mismatch_color);
  2547. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, CodeEdit, font);
  2548. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, CodeEdit, font_size);
  2549. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CodeEdit, line_spacing);
  2550. }
  2551. /* Auto brace completion */
  2552. int CodeEdit::_get_auto_brace_pair_open_at_pos(int p_line, int p_col) {
  2553. const String &line = get_line(p_line);
  2554. /* Should be fast enough, expecting low amount of pairs... */
  2555. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  2556. const String &open_key = auto_brace_completion_pairs[i].open_key;
  2557. if (p_col - open_key.length() < 0) {
  2558. continue;
  2559. }
  2560. bool is_match = true;
  2561. for (int j = 0; j < open_key.length(); j++) {
  2562. if (line[(p_col - 1) - j] != open_key[(open_key.length() - 1) - j]) {
  2563. is_match = false;
  2564. break;
  2565. }
  2566. }
  2567. if (is_match) {
  2568. return i;
  2569. }
  2570. }
  2571. return -1;
  2572. }
  2573. int CodeEdit::_get_auto_brace_pair_close_at_pos(int p_line, int p_col) {
  2574. const String &line = get_line(p_line);
  2575. /* Should be fast enough, expecting low amount of pairs... */
  2576. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  2577. if (p_col + auto_brace_completion_pairs[i].close_key.length() > line.length()) {
  2578. continue;
  2579. }
  2580. bool is_match = true;
  2581. for (int j = 0; j < auto_brace_completion_pairs[i].close_key.length(); j++) {
  2582. if (line[p_col + j] != auto_brace_completion_pairs[i].close_key[j]) {
  2583. is_match = false;
  2584. break;
  2585. }
  2586. }
  2587. if (is_match) {
  2588. return i;
  2589. }
  2590. }
  2591. return -1;
  2592. }
  2593. /* Gutters */
  2594. void CodeEdit::_gutter_clicked(int p_line, int p_gutter) {
  2595. bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  2596. if (p_gutter == main_gutter) {
  2597. if (draw_breakpoints && !shift_pressed) {
  2598. set_line_as_breakpoint(p_line, !is_line_breakpointed(p_line));
  2599. } else if (draw_bookmarks && shift_pressed) {
  2600. set_line_as_bookmarked(p_line, !is_line_bookmarked(p_line));
  2601. }
  2602. return;
  2603. }
  2604. if (p_gutter == line_number_gutter) {
  2605. remove_secondary_carets();
  2606. set_selection_mode(TextEdit::SelectionMode::SELECTION_MODE_LINE);
  2607. if (p_line == get_line_count() - 1) {
  2608. select(p_line, 0, p_line, INT_MAX);
  2609. } else {
  2610. select(p_line, 0, p_line + 1, 0);
  2611. }
  2612. return;
  2613. }
  2614. if (p_gutter == fold_gutter) {
  2615. if (is_line_folded(p_line)) {
  2616. unfold_line(p_line);
  2617. } else if (can_fold_line(p_line)) {
  2618. fold_line(p_line);
  2619. }
  2620. return;
  2621. }
  2622. }
  2623. void CodeEdit::_update_gutter_indexes() {
  2624. for (int i = 0; i < get_gutter_count(); i++) {
  2625. if (get_gutter_name(i) == "main_gutter") {
  2626. main_gutter = i;
  2627. continue;
  2628. }
  2629. if (get_gutter_name(i) == "line_numbers") {
  2630. line_number_gutter = i;
  2631. continue;
  2632. }
  2633. if (get_gutter_name(i) == "fold_gutter") {
  2634. fold_gutter = i;
  2635. continue;
  2636. }
  2637. }
  2638. }
  2639. /* Code Region */
  2640. void CodeEdit::_update_code_region_tags() {
  2641. code_region_start_string = "";
  2642. code_region_end_string = "";
  2643. if (code_region_start_tag.is_empty() || code_region_end_tag.is_empty()) {
  2644. return;
  2645. }
  2646. // A shorter delimiter has higher priority.
  2647. for (int i = delimiters.size() - 1; i >= 0; i--) {
  2648. if (delimiters[i].type != DelimiterType::TYPE_COMMENT) {
  2649. continue;
  2650. }
  2651. if (delimiters[i].end_key.is_empty() && delimiters[i].line_only == true) {
  2652. code_region_start_string = delimiters[i].start_key + code_region_start_tag;
  2653. code_region_end_string = delimiters[i].start_key + code_region_end_tag;
  2654. return;
  2655. }
  2656. }
  2657. }
  2658. /* Delimiters */
  2659. void CodeEdit::_update_delimiter_cache(int p_from_line, int p_to_line) {
  2660. if (delimiters.is_empty()) {
  2661. return;
  2662. }
  2663. int line_count = get_line_count();
  2664. if (p_to_line == -1) {
  2665. p_to_line = line_count;
  2666. }
  2667. int start_line = MIN(p_from_line, p_to_line);
  2668. int end_line = MAX(p_from_line, p_to_line);
  2669. /* Make sure delimiter_cache has all the lines. */
  2670. if (start_line != end_line) {
  2671. if (p_to_line < p_from_line) {
  2672. for (int i = end_line; i > start_line; i--) {
  2673. delimiter_cache.remove_at(i);
  2674. }
  2675. } else {
  2676. for (int i = start_line; i < end_line; i++) {
  2677. delimiter_cache.insert(i, RBMap<int, int>());
  2678. }
  2679. }
  2680. }
  2681. int in_region = -1;
  2682. for (int i = start_line; i < MIN(end_line + 1, line_count); i++) {
  2683. int current_end_region = (i < 0 || delimiter_cache[i].size() < 1) ? -1 : delimiter_cache[i].back()->value();
  2684. in_region = (i <= 0 || delimiter_cache[i - 1].size() < 1) ? -1 : delimiter_cache[i - 1].back()->value();
  2685. const String &str = get_line(i);
  2686. const int line_length = str.length();
  2687. delimiter_cache.write[i].clear();
  2688. if (str.length() == 0) {
  2689. if (in_region != -1) {
  2690. delimiter_cache.write[i][0] = in_region;
  2691. }
  2692. if (i == end_line && current_end_region != in_region) {
  2693. end_line++;
  2694. end_line = MIN(end_line, line_count);
  2695. }
  2696. continue;
  2697. }
  2698. int end_region = -1;
  2699. for (int j = 0; j < line_length; j++) {
  2700. int from = j;
  2701. for (; from < line_length; from++) {
  2702. if (str[from] == '\\') {
  2703. from++;
  2704. continue;
  2705. }
  2706. break;
  2707. }
  2708. /* check if we are in entering a region */
  2709. bool same_line = false;
  2710. if (in_region == -1) {
  2711. for (int d = 0; d < delimiters.size(); d++) {
  2712. /* check there is enough room */
  2713. int chars_left = line_length - from;
  2714. int start_key_length = delimiters[d].start_key.length();
  2715. int end_key_length = delimiters[d].end_key.length();
  2716. if (chars_left < start_key_length) {
  2717. continue;
  2718. }
  2719. /* search the line */
  2720. bool match = true;
  2721. const char32_t *start_key = delimiters[d].start_key.get_data();
  2722. for (int k = 0; k < start_key_length; k++) {
  2723. if (start_key[k] != str[from + k]) {
  2724. match = false;
  2725. break;
  2726. }
  2727. }
  2728. if (!match) {
  2729. continue;
  2730. }
  2731. same_line = true;
  2732. in_region = d;
  2733. delimiter_cache.write[i][from + 1] = d;
  2734. from += start_key_length;
  2735. /* check if it's the whole line */
  2736. if (end_key_length == 0 || delimiters[d].line_only || from + end_key_length > line_length) {
  2737. j = line_length;
  2738. if (delimiters[d].line_only) {
  2739. delimiter_cache.write[i][line_length + 1] = -1;
  2740. } else {
  2741. end_region = in_region;
  2742. }
  2743. }
  2744. break;
  2745. }
  2746. if (j == line_length || in_region == -1) {
  2747. continue;
  2748. }
  2749. }
  2750. /* if we are in one find the end key */
  2751. /* search the line */
  2752. int region_end_index = -1;
  2753. int end_key_length = delimiters[in_region].end_key.length();
  2754. const char32_t *end_key = delimiters[in_region].end_key.get_data();
  2755. for (; from < line_length; from++) {
  2756. if (line_length - from < end_key_length) {
  2757. break;
  2758. }
  2759. if (!is_symbol(str[from])) {
  2760. continue;
  2761. }
  2762. if (str[from] == '\\') {
  2763. from++;
  2764. continue;
  2765. }
  2766. region_end_index = from;
  2767. for (int k = 0; k < end_key_length; k++) {
  2768. if (end_key[k] != str[from + k]) {
  2769. region_end_index = -1;
  2770. break;
  2771. }
  2772. }
  2773. if (region_end_index != -1) {
  2774. break;
  2775. }
  2776. }
  2777. j = from + (end_key_length - 1);
  2778. end_region = (region_end_index == -1) ? in_region : -1;
  2779. if (!same_line || region_end_index != -1) {
  2780. delimiter_cache.write[i][j + 1] = end_region;
  2781. }
  2782. in_region = -1;
  2783. }
  2784. if (i == end_line && current_end_region != end_region) {
  2785. end_line++;
  2786. end_line = MIN(end_line, line_count);
  2787. }
  2788. }
  2789. }
  2790. int CodeEdit::_is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const {
  2791. if (delimiters.is_empty() || p_line >= delimiter_cache.size()) {
  2792. return -1;
  2793. }
  2794. ERR_FAIL_INDEX_V(p_line, get_line_count(), 0);
  2795. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  2796. bool in_region = region != -1 && delimiters[region].type == p_type;
  2797. for (RBMap<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  2798. /* If column is specified, loop until the key is larger then the column. */
  2799. if (p_column != -1) {
  2800. if (E->key() > p_column) {
  2801. break;
  2802. }
  2803. in_region = E->value() != -1 && delimiters[E->value()].type == p_type;
  2804. region = in_region ? E->value() : -1;
  2805. continue;
  2806. }
  2807. /* If no column, calculate if the entire line is a region */
  2808. /* excluding whitespace. */
  2809. const String line = get_line(p_line);
  2810. if (!in_region) {
  2811. if (E->value() == -1 || delimiters[E->value()].type != p_type) {
  2812. break;
  2813. }
  2814. region = E->value();
  2815. in_region = true;
  2816. for (int i = E->key() - 2; i >= 0; i--) {
  2817. if (!is_whitespace(line[i])) {
  2818. return -1;
  2819. }
  2820. }
  2821. }
  2822. if (delimiters[region].line_only) {
  2823. return region;
  2824. }
  2825. int end_col = E->key();
  2826. if (E->value() != -1) {
  2827. if (!E->next()) {
  2828. return region;
  2829. }
  2830. end_col = E->next()->key();
  2831. }
  2832. for (int i = end_col; i < line.length(); i++) {
  2833. if (!is_whitespace(line[i])) {
  2834. return -1;
  2835. }
  2836. }
  2837. return region;
  2838. }
  2839. return in_region ? region : -1;
  2840. }
  2841. void CodeEdit::_add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type) {
  2842. // If we are the editor allow "null" as a valid start key, otherwise users cannot add delimiters via the inspector.
  2843. if (!(Engine::get_singleton()->is_editor_hint() && p_start_key == "null")) {
  2844. ERR_FAIL_COND_MSG(p_start_key.is_empty(), "delimiter start key cannot be empty");
  2845. for (int i = 0; i < p_start_key.length(); i++) {
  2846. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "delimiter must start with a symbol");
  2847. }
  2848. }
  2849. if (p_end_key.length() > 0) {
  2850. for (int i = 0; i < p_end_key.length(); i++) {
  2851. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "delimiter must end with a symbol");
  2852. }
  2853. }
  2854. int at = 0;
  2855. for (int i = 0; i < delimiters.size(); i++) {
  2856. ERR_FAIL_COND_MSG(delimiters[i].start_key == p_start_key, "delimiter with start key '" + p_start_key + "' already exists.");
  2857. if (p_start_key.length() < delimiters[i].start_key.length()) {
  2858. at++;
  2859. } else {
  2860. break;
  2861. }
  2862. }
  2863. Delimiter delimiter;
  2864. delimiter.type = p_type;
  2865. delimiter.start_key = p_start_key;
  2866. delimiter.end_key = p_end_key;
  2867. delimiter.line_only = p_line_only || p_end_key.is_empty();
  2868. delimiters.insert(at, delimiter);
  2869. if (!setting_delimiters) {
  2870. delimiter_cache.clear();
  2871. _update_delimiter_cache();
  2872. }
  2873. if (p_type == DelimiterType::TYPE_COMMENT) {
  2874. _update_code_region_tags();
  2875. }
  2876. }
  2877. void CodeEdit::_remove_delimiter(const String &p_start_key, DelimiterType p_type) {
  2878. for (int i = 0; i < delimiters.size(); i++) {
  2879. if (delimiters[i].start_key != p_start_key) {
  2880. continue;
  2881. }
  2882. if (delimiters[i].type != p_type) {
  2883. break;
  2884. }
  2885. delimiters.remove_at(i);
  2886. if (!setting_delimiters) {
  2887. delimiter_cache.clear();
  2888. _update_delimiter_cache();
  2889. }
  2890. if (p_type == DelimiterType::TYPE_COMMENT) {
  2891. _update_code_region_tags();
  2892. }
  2893. break;
  2894. }
  2895. }
  2896. bool CodeEdit::_has_delimiter(const String &p_start_key, DelimiterType p_type) const {
  2897. for (int i = 0; i < delimiters.size(); i++) {
  2898. if (delimiters[i].start_key == p_start_key) {
  2899. return delimiters[i].type == p_type;
  2900. }
  2901. }
  2902. return false;
  2903. }
  2904. void CodeEdit::_set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type) {
  2905. setting_delimiters = true;
  2906. _clear_delimiters(p_type);
  2907. for (int i = 0; i < p_delimiters.size(); i++) {
  2908. String key = p_delimiters[i];
  2909. if (key.is_empty()) {
  2910. continue;
  2911. }
  2912. const String start_key = key.get_slicec(' ', 0);
  2913. const String end_key = key.get_slice_count(" ") > 1 ? key.get_slicec(' ', 1) : String();
  2914. _add_delimiter(start_key, end_key, end_key.is_empty(), p_type);
  2915. }
  2916. setting_delimiters = false;
  2917. _update_delimiter_cache();
  2918. }
  2919. void CodeEdit::_clear_delimiters(DelimiterType p_type) {
  2920. for (int i = delimiters.size() - 1; i >= 0; i--) {
  2921. if (delimiters[i].type == p_type) {
  2922. delimiters.remove_at(i);
  2923. }
  2924. }
  2925. delimiter_cache.clear();
  2926. if (!setting_delimiters) {
  2927. _update_delimiter_cache();
  2928. }
  2929. if (p_type == DelimiterType::TYPE_COMMENT) {
  2930. _update_code_region_tags();
  2931. }
  2932. }
  2933. TypedArray<String> CodeEdit::_get_delimiters(DelimiterType p_type) const {
  2934. TypedArray<String> r_delimiters;
  2935. for (int i = 0; i < delimiters.size(); i++) {
  2936. if (delimiters[i].type != p_type) {
  2937. continue;
  2938. }
  2939. r_delimiters.push_back(delimiters[i].start_key + (delimiters[i].end_key.is_empty() ? "" : " " + delimiters[i].end_key));
  2940. }
  2941. return r_delimiters;
  2942. }
  2943. /* Code Completion */
  2944. void CodeEdit::_update_scroll_selected_line(float p_mouse_y) {
  2945. float percent = (float)(p_mouse_y - code_completion_scroll_rect.position.y) / code_completion_scroll_rect.size.height;
  2946. percent = CLAMP(percent, 0.0f, 1.0f);
  2947. code_completion_current_selected = (int)(percent * (code_completion_options.size() - 1));
  2948. code_completion_force_item_center = -1;
  2949. code_completion_pan_offset = 0.0f;
  2950. }
  2951. void CodeEdit::_filter_code_completion_candidates_impl() {
  2952. int line_height = get_line_height();
  2953. if (GDVIRTUAL_IS_OVERRIDDEN(_filter_code_completion_candidates)) {
  2954. Vector<ScriptLanguage::CodeCompletionOption> code_completion_options_new;
  2955. code_completion_base = "";
  2956. /* Build options argument. */
  2957. TypedArray<Dictionary> completion_options_sources;
  2958. completion_options_sources.resize(code_completion_option_sources.size());
  2959. int i = 0;
  2960. for (const ScriptLanguage::CodeCompletionOption &E : code_completion_option_sources) {
  2961. Dictionary option;
  2962. option["kind"] = E.kind;
  2963. option["display_text"] = E.display;
  2964. option["insert_text"] = E.insert_text;
  2965. option["font_color"] = E.font_color;
  2966. option["icon"] = E.icon;
  2967. option["default_value"] = E.default_value;
  2968. option["location"] = E.location;
  2969. completion_options_sources[i] = option;
  2970. i++;
  2971. }
  2972. TypedArray<Dictionary> completion_options;
  2973. GDVIRTUAL_CALL(_filter_code_completion_candidates, completion_options_sources, completion_options);
  2974. /* No options to complete, cancel. */
  2975. if (completion_options.is_empty()) {
  2976. cancel_code_completion();
  2977. return;
  2978. }
  2979. /* Convert back into options. */
  2980. int max_width = 0;
  2981. for (i = 0; i < completion_options.size(); i++) {
  2982. ScriptLanguage::CodeCompletionOption option;
  2983. option.kind = (ScriptLanguage::CodeCompletionKind)(int)completion_options[i].get("kind");
  2984. option.display = completion_options[i].get("display_text");
  2985. option.insert_text = completion_options[i].get("insert_text");
  2986. option.font_color = completion_options[i].get("font_color");
  2987. option.icon = completion_options[i].get("icon");
  2988. option.location = completion_options[i].get("location");
  2989. option.default_value = completion_options[i].get("default_value");
  2990. int offset = 0;
  2991. if (option.default_value.get_type() == Variant::COLOR) {
  2992. offset = line_height;
  2993. }
  2994. if (theme_cache.font.is_valid()) {
  2995. max_width = MAX(max_width, theme_cache.font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + offset);
  2996. }
  2997. code_completion_options_new.push_back(option);
  2998. }
  2999. if (_should_reset_selected_option_for_new_options(code_completion_options_new)) {
  3000. code_completion_current_selected = 0;
  3001. code_completion_pan_offset = 0.0f;
  3002. }
  3003. code_completion_options = code_completion_options_new;
  3004. code_completion_longest_line = MIN(max_width, theme_cache.code_completion_max_width * theme_cache.font_size);
  3005. code_completion_force_item_center = -1;
  3006. code_completion_active = true;
  3007. queue_redraw();
  3008. return;
  3009. }
  3010. const int caret_line = get_caret_line();
  3011. const int caret_column = get_caret_column();
  3012. const String line = get_line(caret_line);
  3013. ERR_FAIL_INDEX_MSG(caret_column, line.length() + 1, "Caret column exceeds line length.");
  3014. if (caret_column > 0 && line[caret_column - 1] == '(' && !code_completion_forced) {
  3015. cancel_code_completion();
  3016. return;
  3017. }
  3018. /* Get string status, are we in one or at the close. */
  3019. int in_string = is_in_string(caret_line, caret_column);
  3020. int first_quote_col = -1;
  3021. if (in_string != -1) {
  3022. Point2 string_start_pos = get_delimiter_start_position(caret_line, caret_column);
  3023. first_quote_col = (string_start_pos.y == caret_line) ? string_start_pos.x : -1;
  3024. } else if (caret_column > 0) {
  3025. if (is_in_string(caret_line, caret_column - 1) != -1) {
  3026. first_quote_col = caret_column - 1;
  3027. }
  3028. }
  3029. int cofs = caret_column;
  3030. String string_to_complete;
  3031. bool prev_is_word = false;
  3032. /* Cancel if we are at the close of a string. */
  3033. if (caret_column > 0 && in_string == -1 && first_quote_col == cofs - 1) {
  3034. cancel_code_completion();
  3035. return;
  3036. /* In a string, therefore we are trying to complete the string text. */
  3037. } else if (in_string != -1 && first_quote_col != -1) {
  3038. int key_length = delimiters[in_string].start_key.length();
  3039. string_to_complete = line.substr(first_quote_col - key_length, (cofs - first_quote_col) + key_length);
  3040. /* If we have a space, previous word might be a keyword. eg "func |". */
  3041. } else if (cofs > 0 && line[cofs - 1] == ' ') {
  3042. int ofs = cofs - 1;
  3043. while (ofs > 0 && line[ofs] == ' ') {
  3044. ofs--;
  3045. }
  3046. prev_is_word = !is_symbol(line[ofs]);
  3047. /* Otherwise get current word and set cofs to the start. */
  3048. } else {
  3049. int start_cofs = cofs;
  3050. while (cofs > 0 && line[cofs - 1] > 32 && (line[cofs - 1] == '/' || !is_symbol(line[cofs - 1]))) {
  3051. cofs--;
  3052. }
  3053. string_to_complete = line.substr(cofs, start_cofs - cofs);
  3054. }
  3055. /* If all else fails, check for a prefix. */
  3056. /* Single space between caret and prefix is okay. */
  3057. bool prev_is_prefix = false;
  3058. if (cofs > 0 && code_completion_prefixes.has(line[cofs - 1])) {
  3059. prev_is_prefix = true;
  3060. } else if (cofs > 1 && line[cofs - 1] == ' ' && code_completion_prefixes.has(line[cofs - 2])) {
  3061. prev_is_prefix = true;
  3062. }
  3063. if (!prev_is_word && string_to_complete.is_empty() && (cofs == 0 || !prev_is_prefix)) {
  3064. cancel_code_completion();
  3065. return;
  3066. }
  3067. /* Filter Options. */
  3068. /* For now handle only traditional quoted strings. */
  3069. bool single_quote = in_string != -1 && first_quote_col > 0 && delimiters[in_string].start_key == "'";
  3070. Vector<ScriptLanguage::CodeCompletionOption> code_completion_options_new;
  3071. code_completion_base = string_to_complete;
  3072. /* Don't autocomplete setting numerical values. */
  3073. if (code_completion_base.is_numeric()) {
  3074. cancel_code_completion();
  3075. return;
  3076. }
  3077. int max_width = 0;
  3078. String string_to_complete_lower = string_to_complete.to_lower();
  3079. for (ScriptLanguage::CodeCompletionOption &option : code_completion_option_sources) {
  3080. option.matches.clear();
  3081. if (single_quote && option.display.is_quoted()) {
  3082. option.display = option.display.unquote().quote("'");
  3083. }
  3084. int offset = option.default_value.get_type() == Variant::COLOR ? line_height : 0;
  3085. if (in_string != -1) {
  3086. // The completion string may have a literal behind it, which should be removed before re-quoting.
  3087. String literal;
  3088. if (option.insert_text.substr(1).is_quoted()) {
  3089. literal = option.display.left(1);
  3090. option.display = option.display.substr(1);
  3091. option.insert_text = option.insert_text.substr(1);
  3092. }
  3093. String quote = single_quote ? "'" : "\"";
  3094. option.display = literal + (option.display.unquote().quote(quote));
  3095. option.insert_text = literal + (option.insert_text.unquote().quote(quote));
  3096. }
  3097. if (option.display.length() == 0) {
  3098. continue;
  3099. }
  3100. if (string_to_complete.length() == 0) {
  3101. option.get_option_characteristics(string_to_complete);
  3102. code_completion_options_new.push_back(option);
  3103. if (theme_cache.font.is_valid()) {
  3104. max_width = MAX(max_width, theme_cache.font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + offset);
  3105. }
  3106. continue;
  3107. }
  3108. String target_lower = option.display.to_lower();
  3109. int long_option = target_lower.size() > 50;
  3110. const char32_t *string_to_complete_char_lower = &string_to_complete_lower[0];
  3111. const char32_t *target_char_lower = &target_lower[0];
  3112. Vector<Vector<Pair<int, int>>> all_possible_subsequence_matches;
  3113. for (int i = 0; *target_char_lower; i++, target_char_lower++) {
  3114. if (*target_char_lower == *string_to_complete_char_lower) {
  3115. all_possible_subsequence_matches.push_back({ { i, 1 } });
  3116. }
  3117. }
  3118. string_to_complete_char_lower++;
  3119. for (int i = 1; *string_to_complete_char_lower && (all_possible_subsequence_matches.size() > 0); i++, string_to_complete_char_lower++) {
  3120. // find all occurrences of ssq_lower to avoid looking everywhere each time
  3121. Vector<int> all_occurrences;
  3122. if (long_option) {
  3123. all_occurrences.push_back(target_lower.find_char(*string_to_complete_char_lower));
  3124. } else {
  3125. for (int j = i; j < target_lower.length(); j++) {
  3126. if (target_lower[j] == *string_to_complete_char_lower) {
  3127. all_occurrences.push_back(j);
  3128. }
  3129. }
  3130. }
  3131. Vector<Vector<Pair<int, int>>> next_subsequence_matches;
  3132. for (Vector<Pair<int, int>> &subsequence_match : all_possible_subsequence_matches) {
  3133. Pair<int, int> match_last_segment = subsequence_match[subsequence_match.size() - 1];
  3134. int next_index = match_last_segment.first + match_last_segment.second;
  3135. // get the last index from current sequence
  3136. // and look for next char starting from that index
  3137. if (target_lower[next_index] == *string_to_complete_char_lower) {
  3138. Vector<Pair<int, int>> new_match = subsequence_match;
  3139. new_match.write[new_match.size() - 1].second++;
  3140. next_subsequence_matches.push_back(new_match);
  3141. if (long_option) {
  3142. continue;
  3143. }
  3144. }
  3145. for (int index : all_occurrences) {
  3146. if (index > next_index) {
  3147. Vector<Pair<int, int>> new_match = subsequence_match;
  3148. new_match.push_back({ index, 1 });
  3149. next_subsequence_matches.push_back(new_match);
  3150. }
  3151. }
  3152. }
  3153. all_possible_subsequence_matches = next_subsequence_matches;
  3154. }
  3155. // go through all possible matches to get the best one as defined by CodeCompletionOptionCompare
  3156. if (all_possible_subsequence_matches.size() > 0) {
  3157. option.matches = all_possible_subsequence_matches[0];
  3158. option.get_option_characteristics(string_to_complete);
  3159. all_possible_subsequence_matches = all_possible_subsequence_matches.slice(1);
  3160. if (all_possible_subsequence_matches.size() > 0) {
  3161. CodeCompletionOptionCompare compare;
  3162. ScriptLanguage::CodeCompletionOption compared_option = option;
  3163. compared_option.clear_characteristics();
  3164. for (Vector<Pair<int, int>> &matches : all_possible_subsequence_matches) {
  3165. compared_option.matches = matches;
  3166. compared_option.get_option_characteristics(string_to_complete);
  3167. if (compare(compared_option, option)) {
  3168. option = compared_option;
  3169. compared_option.clear_characteristics();
  3170. }
  3171. }
  3172. }
  3173. code_completion_options_new.push_back(option);
  3174. if (theme_cache.font.is_valid()) {
  3175. max_width = MAX(max_width, theme_cache.font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + offset);
  3176. }
  3177. }
  3178. }
  3179. /* No options to complete, cancel. */
  3180. if (code_completion_options_new.is_empty()) {
  3181. cancel_code_completion();
  3182. return;
  3183. }
  3184. /* A perfect match, stop completion. */
  3185. if (code_completion_options_new.size() == 1 && string_to_complete == code_completion_options_new[0].display) {
  3186. cancel_code_completion();
  3187. return;
  3188. }
  3189. code_completion_options_new.sort_custom<CodeCompletionOptionCompare>();
  3190. if (_should_reset_selected_option_for_new_options(code_completion_options_new)) {
  3191. code_completion_current_selected = 0;
  3192. code_completion_pan_offset = 0.0f;
  3193. }
  3194. code_completion_options = code_completion_options_new;
  3195. code_completion_longest_line = MIN(max_width, theme_cache.code_completion_max_width * theme_cache.font_size);
  3196. code_completion_force_item_center = -1;
  3197. code_completion_active = true;
  3198. queue_redraw();
  3199. }
  3200. // Assumes both the new_options and the code_completion_options are sorted.
  3201. bool CodeEdit::_should_reset_selected_option_for_new_options(const Vector<ScriptLanguage::CodeCompletionOption> &p_new_options) {
  3202. if (code_completion_current_selected >= p_new_options.size()) {
  3203. return true;
  3204. }
  3205. for (int i = 0; i < code_completion_options.size() && i < p_new_options.size(); i++) {
  3206. if (i > code_completion_current_selected) {
  3207. return false;
  3208. }
  3209. if (code_completion_options[i].display != p_new_options[i].display) {
  3210. return true;
  3211. }
  3212. }
  3213. return false;
  3214. }
  3215. void CodeEdit::_lines_edited_from(int p_from_line, int p_to_line) {
  3216. _update_delimiter_cache(p_from_line, p_to_line);
  3217. if (p_from_line == p_to_line) {
  3218. return;
  3219. }
  3220. lines_edited_changed += p_to_line - p_from_line;
  3221. lines_edited_from = (lines_edited_from == -1) ? MIN(p_from_line, p_to_line) : MIN(lines_edited_from, MIN(p_from_line, p_to_line));
  3222. lines_edited_to = (lines_edited_to == -1) ? MAX(p_from_line, p_to_line) : MAX(lines_edited_from, MAX(p_from_line, p_to_line));
  3223. }
  3224. void CodeEdit::_text_set() {
  3225. lines_edited_from = 0;
  3226. lines_edited_to = 9999;
  3227. _text_changed();
  3228. }
  3229. void CodeEdit::_text_changed() {
  3230. if (lines_edited_from == -1) {
  3231. return;
  3232. }
  3233. int lc = get_line_count();
  3234. int new_line_number_digits = std::log10(lc) + 1;
  3235. if (line_number_digits != new_line_number_digits) {
  3236. _clear_line_number_text_cache();
  3237. }
  3238. line_number_digits = new_line_number_digits;
  3239. _update_line_number_gutter_width();
  3240. List<int> breakpoints;
  3241. for (const KeyValue<int, bool> &E : breakpointed_lines) {
  3242. breakpoints.push_back(E.key);
  3243. }
  3244. for (const int &line : breakpoints) {
  3245. if (line < lines_edited_from || (line < lc && is_line_breakpointed(line))) {
  3246. continue;
  3247. }
  3248. breakpointed_lines.erase(line);
  3249. emit_signal(SNAME("breakpoint_toggled"), line);
  3250. int next_line = line + lines_edited_changed;
  3251. if (next_line > -1 && next_line < lc && is_line_breakpointed(next_line)) {
  3252. emit_signal(SNAME("breakpoint_toggled"), next_line);
  3253. breakpointed_lines[next_line] = true;
  3254. continue;
  3255. }
  3256. }
  3257. lines_edited_from = -1;
  3258. lines_edited_to = -1;
  3259. lines_edited_changed = 0;
  3260. }
  3261. CodeEdit::CodeEdit() {
  3262. /* Indent management */
  3263. auto_indent_prefixes.insert(':');
  3264. auto_indent_prefixes.insert('{');
  3265. auto_indent_prefixes.insert('[');
  3266. auto_indent_prefixes.insert('(');
  3267. /* Auto brace completion */
  3268. add_auto_brace_completion_pair("(", ")");
  3269. add_auto_brace_completion_pair("{", "}");
  3270. add_auto_brace_completion_pair("[", "]");
  3271. add_auto_brace_completion_pair("\"", "\"");
  3272. add_auto_brace_completion_pair("\'", "\'");
  3273. /* Delimiter tracking */
  3274. add_string_delimiter("\"", "\"", false);
  3275. add_string_delimiter("\'", "\'", false);
  3276. /* Text Direction */
  3277. set_layout_direction(LAYOUT_DIRECTION_LTR);
  3278. set_text_direction(TEXT_DIRECTION_LTR);
  3279. /* Gutters */
  3280. int gutter_idx = 0;
  3281. /* Main Gutter */
  3282. add_gutter();
  3283. set_gutter_name(gutter_idx, "main_gutter");
  3284. set_gutter_draw(gutter_idx, false);
  3285. set_gutter_overwritable(gutter_idx, true);
  3286. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  3287. set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_main_gutter_draw_callback));
  3288. gutter_idx++;
  3289. /* Line numbers */
  3290. add_gutter();
  3291. set_gutter_name(gutter_idx, "line_numbers");
  3292. set_gutter_draw(gutter_idx, false);
  3293. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  3294. set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_line_number_draw_callback));
  3295. gutter_idx++;
  3296. /* Fold Gutter */
  3297. add_gutter();
  3298. set_gutter_name(gutter_idx, "fold_gutter");
  3299. set_gutter_draw(gutter_idx, false);
  3300. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  3301. set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_fold_gutter_draw_callback));
  3302. gutter_idx++;
  3303. /* Symbol tooltip */
  3304. symbol_tooltip_timer = memnew(Timer);
  3305. symbol_tooltip_timer->set_wait_time(0.5); // See `_apply_project_settings()`.
  3306. symbol_tooltip_timer->set_one_shot(true);
  3307. symbol_tooltip_timer->connect("timeout", callable_mp(this, &CodeEdit::_on_symbol_tooltip_timer_timeout));
  3308. add_child(symbol_tooltip_timer, false, INTERNAL_MODE_FRONT);
  3309. /* Fold Lines Private signal */
  3310. add_user_signal(MethodInfo("_fold_line_updated"));
  3311. connect("lines_edited_from", callable_mp(this, &CodeEdit::_lines_edited_from));
  3312. connect("text_set", callable_mp(this, &CodeEdit::_text_set));
  3313. connect(SceneStringName(text_changed), callable_mp(this, &CodeEdit::_text_changed));
  3314. connect("gutter_clicked", callable_mp(this, &CodeEdit::_gutter_clicked));
  3315. connect("gutter_added", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  3316. connect("gutter_removed", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  3317. _update_gutter_indexes();
  3318. }
  3319. CodeEdit::~CodeEdit() {
  3320. _clear_line_number_text_cache();
  3321. }
  3322. // Return true if l should come before r
  3323. bool CodeCompletionOptionCompare::operator()(const ScriptLanguage::CodeCompletionOption &l, const ScriptLanguage::CodeCompletionOption &r) const {
  3324. TypedArray<int> lcharac = l.get_option_cached_characteristics();
  3325. TypedArray<int> rcharac = r.get_option_cached_characteristics();
  3326. if (lcharac != rcharac) {
  3327. return lcharac < rcharac;
  3328. }
  3329. // to get here they need to have the same size so we can take the size of whichever we want
  3330. for (int i = 0; i < l.matches.size(); ++i) {
  3331. if (l.matches[i].first != r.matches[i].first) {
  3332. return l.matches[i].first < r.matches[i].first;
  3333. }
  3334. if (l.matches[i].second != r.matches[i].second) {
  3335. return l.matches[i].second > r.matches[i].second;
  3336. }
  3337. }
  3338. return l.display.naturalnocasecmp_to(r.display) < 0;
  3339. }