code_edit.cpp 111 KB

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