code_edit.cpp 117 KB

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