code_edit.cpp 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276
  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. do_unindent();
  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. }
  765. void CodeEdit::do_unindent() {
  766. if (!is_editable()) {
  767. return;
  768. }
  769. int cc = get_caret_column();
  770. if (has_selection() || cc <= 0) {
  771. unindent_lines();
  772. return;
  773. }
  774. begin_complex_operation();
  775. Vector<int> caret_edit_order = get_caret_index_edit_order();
  776. for (const int &c : caret_edit_order) {
  777. int cl = get_caret_line(c);
  778. const String &line = get_line(cl);
  779. if (line[cc - 1] == '\t') {
  780. remove_text(cl, cc - 1, cl, cc);
  781. set_caret_column(MAX(0, cc - 1), c == 0, c);
  782. adjust_carets_after_edit(c, cl, cc, cl, cc - 1);
  783. continue;
  784. }
  785. if (line[cc - 1] != ' ') {
  786. continue;
  787. }
  788. int spaces_to_remove = _calculate_spaces_till_next_left_indent(cc);
  789. if (spaces_to_remove > 0) {
  790. for (int i = 1; i <= spaces_to_remove; i++) {
  791. if (line[cc - i] != ' ') {
  792. spaces_to_remove = i - 1;
  793. break;
  794. }
  795. }
  796. remove_text(cl, cc - spaces_to_remove, cl, cc);
  797. set_caret_column(MAX(0, cc - spaces_to_remove), c == 0, c);
  798. }
  799. }
  800. end_complex_operation();
  801. }
  802. void CodeEdit::unindent_lines() {
  803. if (!is_editable()) {
  804. return;
  805. }
  806. begin_complex_operation();
  807. Vector<int> caret_edit_order = get_caret_index_edit_order();
  808. for (const int &c : caret_edit_order) {
  809. // Moving caret and selection after unindenting can get tricky because
  810. // changing content of line can move caret and selection on its own (if new line ends before previous position of either)
  811. // therefore we just remember initial values and at the end of the operation offset them by number of removed characters.
  812. int removed_characters = 0;
  813. int initial_selection_end_column = 0;
  814. int initial_cursor_column = get_caret_column(c);
  815. int start_line = get_caret_line(c);
  816. int end_line = start_line;
  817. if (has_selection(c)) {
  818. start_line = get_selection_from_line(c);
  819. end_line = get_selection_to_line(c);
  820. // Ignore the last line if the selection is not past the first column.
  821. initial_selection_end_column = get_selection_to_column(c);
  822. if (initial_selection_end_column == 0) {
  823. end_line--;
  824. }
  825. }
  826. bool first_line_edited = false;
  827. bool last_line_edited = false;
  828. for (int i = start_line; i <= end_line; i++) {
  829. String line_text = get_line(i);
  830. if (line_text.begins_with("\t")) {
  831. line_text = line_text.substr(1, line_text.length());
  832. set_line(i, line_text);
  833. removed_characters = 1;
  834. first_line_edited = (i == start_line) ? true : first_line_edited;
  835. last_line_edited = (i == end_line) ? true : last_line_edited;
  836. continue;
  837. }
  838. if (line_text.begins_with(" ")) {
  839. // When unindenting we aim to remove spaces before line that has selection no matter what is selected.
  840. // Here we remove only enough spaces to align text to nearest full multiple of indentation_size.
  841. // In case where selection begins at the start of indentation_size multiple we remove whole indentation level.
  842. int spaces_to_remove = _calculate_spaces_till_next_left_indent(get_first_non_whitespace_column(i));
  843. line_text = line_text.substr(spaces_to_remove, line_text.length());
  844. set_line(i, line_text);
  845. removed_characters = spaces_to_remove;
  846. first_line_edited = (i == start_line) ? true : first_line_edited;
  847. last_line_edited = (i == end_line) ? true : last_line_edited;
  848. }
  849. }
  850. if (has_selection(c)) {
  851. // Fix selection being off by one on the first line.
  852. if (first_line_edited) {
  853. select(get_selection_from_line(c), get_selection_from_column(c) - removed_characters, get_selection_to_line(c), initial_selection_end_column, c);
  854. }
  855. // Fix selection being off by one on the last line.
  856. if (last_line_edited) {
  857. select(get_selection_from_line(c), get_selection_from_column(c), get_selection_to_line(c), initial_selection_end_column - removed_characters, c);
  858. }
  859. }
  860. set_caret_column(initial_cursor_column - removed_characters, false, c);
  861. }
  862. end_complex_operation();
  863. }
  864. int CodeEdit::_calculate_spaces_till_next_left_indent(int p_column) const {
  865. int spaces_till_indent = p_column % indent_size;
  866. if (spaces_till_indent == 0) {
  867. spaces_till_indent = indent_size;
  868. }
  869. return spaces_till_indent;
  870. }
  871. int CodeEdit::_calculate_spaces_till_next_right_indent(int p_column) const {
  872. return indent_size - p_column % indent_size;
  873. }
  874. void CodeEdit::_new_line(bool p_split_current_line, bool p_above) {
  875. if (!is_editable()) {
  876. return;
  877. }
  878. begin_complex_operation();
  879. Vector<int> caret_edit_order = get_caret_index_edit_order();
  880. for (const int &i : caret_edit_order) {
  881. // When not splitting the line, we need to factor in indentation from the end of the current line.
  882. const int cc = p_split_current_line ? get_caret_column(i) : get_line(get_caret_line(i)).length();
  883. const int cl = get_caret_line(i);
  884. const String line = get_line(cl);
  885. String ins = "\n";
  886. // Append current indentation.
  887. int space_count = 0;
  888. int line_col = 0;
  889. for (; line_col < cc; line_col++) {
  890. if (line[line_col] == '\t') {
  891. ins += indent_text;
  892. space_count = 0;
  893. continue;
  894. }
  895. if (line[line_col] == ' ') {
  896. space_count++;
  897. if (space_count == indent_size) {
  898. ins += indent_text;
  899. space_count = 0;
  900. }
  901. continue;
  902. }
  903. break;
  904. }
  905. if (is_line_folded(cl)) {
  906. unfold_line(cl);
  907. }
  908. // Indent once again if the previous line needs it, ie ':'.
  909. // Then add an addition new line for any closing pairs aka '()'.
  910. // Skip this in comments or if we are going above.
  911. bool brace_indent = false;
  912. if (auto_indent && !p_above && cc > 0 && is_in_comment(cl) == -1) {
  913. bool should_indent = false;
  914. char32_t indent_char = ' ';
  915. for (; line_col < cc; line_col++) {
  916. char32_t c = line[line_col];
  917. if (auto_indent_prefixes.has(c)) {
  918. should_indent = true;
  919. indent_char = c;
  920. continue;
  921. }
  922. // Make sure this is the last char, trailing whitespace or comments are okay.
  923. // Increment column for comments because the delimiter (#) should be ignored.
  924. if (should_indent && (!is_whitespace(c) && is_in_comment(cl, line_col + 1) == -1)) {
  925. should_indent = false;
  926. }
  927. }
  928. if (should_indent) {
  929. ins += indent_text;
  930. String closing_pair = get_auto_brace_completion_close_key(String::chr(indent_char));
  931. if (!closing_pair.is_empty() && line.find(closing_pair, cc) == cc) {
  932. // No need to move the brace below if we are not taking the text with us.
  933. if (p_split_current_line) {
  934. brace_indent = true;
  935. ins += "\n" + ins.substr(indent_text.size(), ins.length() - 2);
  936. } else {
  937. brace_indent = false;
  938. ins = "\n" + ins.substr(indent_text.size(), ins.length() - 2);
  939. }
  940. }
  941. }
  942. }
  943. bool first_line = false;
  944. if (!p_split_current_line) {
  945. deselect(i);
  946. if (p_above) {
  947. if (cl > 0) {
  948. set_caret_line(cl - 1, false, true, 0, i);
  949. set_caret_column(get_line(get_caret_line(i)).length(), i == 0, i);
  950. } else {
  951. set_caret_column(0, i == 0, i);
  952. first_line = true;
  953. }
  954. } else {
  955. set_caret_column(line.length(), i == 0, i);
  956. }
  957. }
  958. insert_text_at_caret(ins, i);
  959. if (first_line) {
  960. set_caret_line(0, i == 0, true, 0, i);
  961. } else if (brace_indent) {
  962. set_caret_line(get_caret_line(i) - 1, false, true, 0, i);
  963. set_caret_column(get_line(get_caret_line(i)).length(), i == 0, i);
  964. }
  965. }
  966. end_complex_operation();
  967. }
  968. /* Auto brace completion */
  969. void CodeEdit::set_auto_brace_completion_enabled(bool p_enabled) {
  970. auto_brace_completion_enabled = p_enabled;
  971. }
  972. bool CodeEdit::is_auto_brace_completion_enabled() const {
  973. return auto_brace_completion_enabled;
  974. }
  975. void CodeEdit::set_highlight_matching_braces_enabled(bool p_enabled) {
  976. highlight_matching_braces_enabled = p_enabled;
  977. queue_redraw();
  978. }
  979. bool CodeEdit::is_highlight_matching_braces_enabled() const {
  980. return highlight_matching_braces_enabled;
  981. }
  982. void CodeEdit::add_auto_brace_completion_pair(const String &p_open_key, const String &p_close_key) {
  983. ERR_FAIL_COND_MSG(p_open_key.is_empty(), "auto brace completion open key cannot be empty");
  984. ERR_FAIL_COND_MSG(p_close_key.is_empty(), "auto brace completion close key cannot be empty");
  985. for (int i = 0; i < p_open_key.length(); i++) {
  986. ERR_FAIL_COND_MSG(!is_symbol(p_open_key[i]), "auto brace completion open key must be a symbol");
  987. }
  988. for (int i = 0; i < p_close_key.length(); i++) {
  989. ERR_FAIL_COND_MSG(!is_symbol(p_close_key[i]), "auto brace completion close key must be a symbol");
  990. }
  991. int at = 0;
  992. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  993. ERR_FAIL_COND_MSG(auto_brace_completion_pairs[i].open_key == p_open_key, "auto brace completion open key '" + p_open_key + "' already exists.");
  994. if (p_open_key.length() < auto_brace_completion_pairs[i].open_key.length()) {
  995. at++;
  996. }
  997. }
  998. BracePair brace_pair;
  999. brace_pair.open_key = p_open_key;
  1000. brace_pair.close_key = p_close_key;
  1001. auto_brace_completion_pairs.insert(at, brace_pair);
  1002. }
  1003. void CodeEdit::set_auto_brace_completion_pairs(const Dictionary &p_auto_brace_completion_pairs) {
  1004. auto_brace_completion_pairs.clear();
  1005. Array keys = p_auto_brace_completion_pairs.keys();
  1006. for (int i = 0; i < keys.size(); i++) {
  1007. add_auto_brace_completion_pair(keys[i], p_auto_brace_completion_pairs[keys[i]]);
  1008. }
  1009. }
  1010. Dictionary CodeEdit::get_auto_brace_completion_pairs() const {
  1011. Dictionary brace_pairs;
  1012. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1013. brace_pairs[auto_brace_completion_pairs[i].open_key] = auto_brace_completion_pairs[i].close_key;
  1014. }
  1015. return brace_pairs;
  1016. }
  1017. bool CodeEdit::has_auto_brace_completion_open_key(const String &p_open_key) const {
  1018. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1019. if (auto_brace_completion_pairs[i].open_key == p_open_key) {
  1020. return true;
  1021. }
  1022. }
  1023. return false;
  1024. }
  1025. bool CodeEdit::has_auto_brace_completion_close_key(const String &p_close_key) const {
  1026. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1027. if (auto_brace_completion_pairs[i].close_key == p_close_key) {
  1028. return true;
  1029. }
  1030. }
  1031. return false;
  1032. }
  1033. String CodeEdit::get_auto_brace_completion_close_key(const String &p_open_key) const {
  1034. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1035. if (auto_brace_completion_pairs[i].open_key == p_open_key) {
  1036. return auto_brace_completion_pairs[i].close_key;
  1037. }
  1038. }
  1039. return String();
  1040. }
  1041. /* Main Gutter */
  1042. void CodeEdit::_update_draw_main_gutter() {
  1043. set_gutter_draw(main_gutter, draw_breakpoints || draw_bookmarks || draw_executing_lines);
  1044. }
  1045. void CodeEdit::set_draw_breakpoints_gutter(bool p_draw) {
  1046. draw_breakpoints = p_draw;
  1047. set_gutter_clickable(main_gutter, p_draw);
  1048. _update_draw_main_gutter();
  1049. }
  1050. bool CodeEdit::is_drawing_breakpoints_gutter() const {
  1051. return draw_breakpoints;
  1052. }
  1053. void CodeEdit::set_draw_bookmarks_gutter(bool p_draw) {
  1054. draw_bookmarks = p_draw;
  1055. _update_draw_main_gutter();
  1056. }
  1057. bool CodeEdit::is_drawing_bookmarks_gutter() const {
  1058. return draw_bookmarks;
  1059. }
  1060. void CodeEdit::set_draw_executing_lines_gutter(bool p_draw) {
  1061. draw_executing_lines = p_draw;
  1062. _update_draw_main_gutter();
  1063. }
  1064. bool CodeEdit::is_drawing_executing_lines_gutter() const {
  1065. return draw_executing_lines;
  1066. }
  1067. void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  1068. if (draw_breakpoints && breakpoint_icon.is_valid()) {
  1069. bool breakpointed = is_line_breakpointed(p_line);
  1070. bool hovering = p_region.has_point(get_local_mouse_pos());
  1071. bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  1072. if (breakpointed || (hovering && !is_dragging_cursor() && !shift_pressed)) {
  1073. int padding = p_region.size.x / 6;
  1074. Color use_color = breakpoint_color;
  1075. if (hovering && !shift_pressed) {
  1076. use_color = breakpointed ? use_color.lightened(0.3) : use_color.darkened(0.5);
  1077. }
  1078. Rect2 icon_region = p_region;
  1079. icon_region.position += Point2(padding, padding);
  1080. icon_region.size -= Point2(padding, padding) * 2;
  1081. breakpoint_icon->draw_rect(get_canvas_item(), icon_region, false, use_color);
  1082. }
  1083. }
  1084. if (draw_bookmarks && bookmark_icon.is_valid()) {
  1085. bool bookmarked = is_line_bookmarked(p_line);
  1086. bool hovering = p_region.has_point(get_local_mouse_pos());
  1087. bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  1088. if (bookmarked || (hovering && !is_dragging_cursor() && shift_pressed)) {
  1089. int horizontal_padding = p_region.size.x / 2;
  1090. int vertical_padding = p_region.size.y / 4;
  1091. Color use_color = bookmark_color;
  1092. if (hovering && shift_pressed) {
  1093. use_color = bookmarked ? use_color.lightened(0.3) : use_color.darkened(0.5);
  1094. }
  1095. Rect2 icon_region = p_region;
  1096. icon_region.position += Point2(horizontal_padding, 0);
  1097. icon_region.size -= Point2(horizontal_padding * 1.1, vertical_padding);
  1098. bookmark_icon->draw_rect(get_canvas_item(), icon_region, false, use_color);
  1099. }
  1100. }
  1101. if (draw_executing_lines && is_line_executing(p_line) && executing_line_icon.is_valid()) {
  1102. int horizontal_padding = p_region.size.x / 10;
  1103. int vertical_padding = p_region.size.y / 4;
  1104. Rect2 icon_region = p_region;
  1105. icon_region.position += Point2(horizontal_padding, vertical_padding);
  1106. icon_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  1107. executing_line_icon->draw_rect(get_canvas_item(), icon_region, false, executing_line_color);
  1108. }
  1109. }
  1110. // Breakpoints
  1111. void CodeEdit::set_line_as_breakpoint(int p_line, bool p_breakpointed) {
  1112. ERR_FAIL_INDEX(p_line, get_line_count());
  1113. int mask = get_line_gutter_metadata(p_line, main_gutter);
  1114. set_line_gutter_metadata(p_line, main_gutter, p_breakpointed ? mask | MAIN_GUTTER_BREAKPOINT : mask & ~MAIN_GUTTER_BREAKPOINT);
  1115. if (p_breakpointed) {
  1116. breakpointed_lines[p_line] = true;
  1117. } else if (breakpointed_lines.has(p_line)) {
  1118. breakpointed_lines.erase(p_line);
  1119. }
  1120. emit_signal(SNAME("breakpoint_toggled"), p_line);
  1121. queue_redraw();
  1122. }
  1123. bool CodeEdit::is_line_breakpointed(int p_line) const {
  1124. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BREAKPOINT;
  1125. }
  1126. void CodeEdit::clear_breakpointed_lines() {
  1127. for (int i = 0; i < get_line_count(); i++) {
  1128. if (is_line_breakpointed(i)) {
  1129. set_line_as_breakpoint(i, false);
  1130. }
  1131. }
  1132. }
  1133. PackedInt32Array CodeEdit::get_breakpointed_lines() const {
  1134. PackedInt32Array ret;
  1135. for (int i = 0; i < get_line_count(); i++) {
  1136. if (is_line_breakpointed(i)) {
  1137. ret.append(i);
  1138. }
  1139. }
  1140. return ret;
  1141. }
  1142. // Bookmarks
  1143. void CodeEdit::set_line_as_bookmarked(int p_line, bool p_bookmarked) {
  1144. int mask = get_line_gutter_metadata(p_line, main_gutter);
  1145. set_line_gutter_metadata(p_line, main_gutter, p_bookmarked ? mask | MAIN_GUTTER_BOOKMARK : mask & ~MAIN_GUTTER_BOOKMARK);
  1146. queue_redraw();
  1147. }
  1148. bool CodeEdit::is_line_bookmarked(int p_line) const {
  1149. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BOOKMARK;
  1150. }
  1151. void CodeEdit::clear_bookmarked_lines() {
  1152. for (int i = 0; i < get_line_count(); i++) {
  1153. if (is_line_bookmarked(i)) {
  1154. set_line_as_bookmarked(i, false);
  1155. }
  1156. }
  1157. }
  1158. PackedInt32Array CodeEdit::get_bookmarked_lines() const {
  1159. PackedInt32Array ret;
  1160. for (int i = 0; i < get_line_count(); i++) {
  1161. if (is_line_bookmarked(i)) {
  1162. ret.append(i);
  1163. }
  1164. }
  1165. return ret;
  1166. }
  1167. // executing lines
  1168. void CodeEdit::set_line_as_executing(int p_line, bool p_executing) {
  1169. int mask = get_line_gutter_metadata(p_line, main_gutter);
  1170. set_line_gutter_metadata(p_line, main_gutter, p_executing ? mask | MAIN_GUTTER_EXECUTING : mask & ~MAIN_GUTTER_EXECUTING);
  1171. queue_redraw();
  1172. }
  1173. bool CodeEdit::is_line_executing(int p_line) const {
  1174. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_EXECUTING;
  1175. }
  1176. void CodeEdit::clear_executing_lines() {
  1177. for (int i = 0; i < get_line_count(); i++) {
  1178. if (is_line_executing(i)) {
  1179. set_line_as_executing(i, false);
  1180. }
  1181. }
  1182. }
  1183. PackedInt32Array CodeEdit::get_executing_lines() const {
  1184. PackedInt32Array ret;
  1185. for (int i = 0; i < get_line_count(); i++) {
  1186. if (is_line_executing(i)) {
  1187. ret.append(i);
  1188. }
  1189. }
  1190. return ret;
  1191. }
  1192. /* Line numbers */
  1193. void CodeEdit::set_draw_line_numbers(bool p_draw) {
  1194. set_gutter_draw(line_number_gutter, p_draw);
  1195. }
  1196. bool CodeEdit::is_draw_line_numbers_enabled() const {
  1197. return is_gutter_drawn(line_number_gutter);
  1198. }
  1199. void CodeEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
  1200. p_zero_padded ? line_number_padding = "0" : line_number_padding = " ";
  1201. queue_redraw();
  1202. }
  1203. bool CodeEdit::is_line_numbers_zero_padded() const {
  1204. return line_number_padding == "0";
  1205. }
  1206. void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  1207. String fc = String::num(p_line + 1).lpad(line_number_digits, line_number_padding);
  1208. if (is_localizing_numeral_system()) {
  1209. fc = TS->format_number(fc);
  1210. }
  1211. Ref<TextLine> tl;
  1212. tl.instantiate();
  1213. tl->add_string(fc, font, font_size);
  1214. int yofs = p_region.position.y + (get_line_height() - tl->get_size().y) / 2;
  1215. Color number_color = get_line_gutter_item_color(p_line, line_number_gutter);
  1216. if (number_color == Color(1, 1, 1)) {
  1217. number_color = line_number_color;
  1218. }
  1219. tl->draw(get_canvas_item(), Point2(p_region.position.x, yofs), number_color);
  1220. }
  1221. /* Fold Gutter */
  1222. void CodeEdit::set_draw_fold_gutter(bool p_draw) {
  1223. set_gutter_draw(fold_gutter, p_draw);
  1224. }
  1225. bool CodeEdit::is_drawing_fold_gutter() const {
  1226. return is_gutter_drawn(fold_gutter);
  1227. }
  1228. void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region) {
  1229. if (!can_fold_line(p_line) && !is_line_folded(p_line)) {
  1230. set_line_gutter_clickable(p_line, fold_gutter, false);
  1231. return;
  1232. }
  1233. set_line_gutter_clickable(p_line, fold_gutter, true);
  1234. int horizontal_padding = p_region.size.x / 10;
  1235. int vertical_padding = p_region.size.y / 6;
  1236. p_region.position += Point2(horizontal_padding, vertical_padding);
  1237. p_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  1238. if (can_fold_line(p_line)) {
  1239. can_fold_icon->draw_rect(get_canvas_item(), p_region, false, folding_color);
  1240. return;
  1241. }
  1242. folded_icon->draw_rect(get_canvas_item(), p_region, false, folding_color);
  1243. }
  1244. /* Line Folding */
  1245. void CodeEdit::set_line_folding_enabled(bool p_enabled) {
  1246. line_folding_enabled = p_enabled;
  1247. _set_hiding_enabled(p_enabled);
  1248. }
  1249. bool CodeEdit::is_line_folding_enabled() const {
  1250. return line_folding_enabled;
  1251. }
  1252. bool CodeEdit::can_fold_line(int p_line) const {
  1253. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1254. if (!line_folding_enabled) {
  1255. return false;
  1256. }
  1257. if (p_line + 1 >= get_line_count() || get_line(p_line).strip_edges().size() == 0) {
  1258. return false;
  1259. }
  1260. if (_is_line_hidden(p_line) || is_line_folded(p_line)) {
  1261. return false;
  1262. }
  1263. /* Check for full multiline line or block strings / comments. */
  1264. int in_comment = is_in_comment(p_line);
  1265. int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
  1266. if (in_string != -1 || in_comment != -1) {
  1267. if (get_delimiter_start_position(p_line, get_line(p_line).size() - 1).y != p_line) {
  1268. return false;
  1269. }
  1270. int delimter_end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
  1271. /* No end line, therefore we have a multiline region over the rest of the file. */
  1272. if (delimter_end_line == -1) {
  1273. return true;
  1274. }
  1275. /* End line is the same therefore we have a block. */
  1276. if (delimter_end_line == p_line) {
  1277. /* Check we are the start of the block. */
  1278. if (p_line - 1 >= 0) {
  1279. if ((in_string != -1 && is_in_string(p_line - 1) != -1) || (in_comment != -1 && is_in_comment(p_line - 1) != -1)) {
  1280. return false;
  1281. }
  1282. }
  1283. /* Check it continues for at least one line. */
  1284. return ((in_string != -1 && is_in_string(p_line + 1) != -1) || (in_comment != -1 && is_in_comment(p_line + 1) != -1));
  1285. }
  1286. return ((in_string != -1 && is_in_string(delimter_end_line) != -1) || (in_comment != -1 && is_in_comment(delimter_end_line) != -1));
  1287. }
  1288. /* Otherwise check indent levels. */
  1289. int start_indent = get_indent_level(p_line);
  1290. for (int i = p_line + 1; i < get_line_count(); i++) {
  1291. if (is_in_string(i) != -1 || is_in_comment(i) != -1 || get_line(i).strip_edges().size() == 0) {
  1292. continue;
  1293. }
  1294. return (get_indent_level(i) > start_indent);
  1295. }
  1296. return false;
  1297. }
  1298. void CodeEdit::fold_line(int p_line) {
  1299. ERR_FAIL_INDEX(p_line, get_line_count());
  1300. if (!is_line_folding_enabled() || !can_fold_line(p_line)) {
  1301. return;
  1302. }
  1303. /* Find the last line to be hidden. */
  1304. const int line_count = get_line_count() - 1;
  1305. int end_line = line_count;
  1306. int in_comment = is_in_comment(p_line);
  1307. int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
  1308. if (in_string != -1 || in_comment != -1) {
  1309. end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
  1310. /* End line is the same therefore we have a block of single line delimiters. */
  1311. if (end_line == p_line) {
  1312. for (int i = p_line + 1; i <= line_count; i++) {
  1313. if ((in_string != -1 && is_in_string(i) == -1) || (in_comment != -1 && is_in_comment(i) == -1)) {
  1314. break;
  1315. }
  1316. end_line = i;
  1317. }
  1318. }
  1319. } else {
  1320. int start_indent = get_indent_level(p_line);
  1321. for (int i = p_line + 1; i <= line_count; i++) {
  1322. if (get_line(i).strip_edges().size() == 0) {
  1323. continue;
  1324. }
  1325. if (get_indent_level(i) > start_indent) {
  1326. end_line = i;
  1327. continue;
  1328. }
  1329. if (is_in_string(i) == -1 && is_in_comment(i) == -1) {
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. for (int i = p_line + 1; i <= end_line; i++) {
  1335. _set_line_as_hidden(i, true);
  1336. }
  1337. for (int i = 0; i < get_caret_count(); i++) {
  1338. // Fix selection.
  1339. if (has_selection(i)) {
  1340. if (_is_line_hidden(get_selection_from_line(i)) && _is_line_hidden(get_selection_to_line(i))) {
  1341. deselect(i);
  1342. } else if (_is_line_hidden(get_selection_from_line(i))) {
  1343. select(p_line, 9999, get_selection_to_line(i), get_selection_to_column(i), i);
  1344. } else if (_is_line_hidden(get_selection_to_line(i))) {
  1345. select(get_selection_from_line(i), get_selection_from_column(i), p_line, 9999, i);
  1346. }
  1347. }
  1348. // Reset caret.
  1349. if (_is_line_hidden(get_caret_line(i))) {
  1350. set_caret_line(p_line, false, false, 0, i);
  1351. set_caret_column(get_line(p_line).length(), false, i);
  1352. }
  1353. }
  1354. merge_overlapping_carets();
  1355. queue_redraw();
  1356. }
  1357. void CodeEdit::unfold_line(int p_line) {
  1358. ERR_FAIL_INDEX(p_line, get_line_count());
  1359. if (!is_line_folded(p_line) && !_is_line_hidden(p_line)) {
  1360. return;
  1361. }
  1362. int fold_start = p_line;
  1363. for (; fold_start > 0; fold_start--) {
  1364. if (is_line_folded(fold_start)) {
  1365. break;
  1366. }
  1367. }
  1368. fold_start = is_line_folded(fold_start) ? fold_start : p_line;
  1369. for (int i = fold_start + 1; i < get_line_count(); i++) {
  1370. if (!_is_line_hidden(i)) {
  1371. break;
  1372. }
  1373. _set_line_as_hidden(i, false);
  1374. }
  1375. queue_redraw();
  1376. }
  1377. void CodeEdit::fold_all_lines() {
  1378. for (int i = 0; i < get_line_count(); i++) {
  1379. fold_line(i);
  1380. }
  1381. queue_redraw();
  1382. }
  1383. void CodeEdit::unfold_all_lines() {
  1384. _unhide_all_lines();
  1385. }
  1386. void CodeEdit::toggle_foldable_line(int p_line) {
  1387. ERR_FAIL_INDEX(p_line, get_line_count());
  1388. if (is_line_folded(p_line)) {
  1389. unfold_line(p_line);
  1390. return;
  1391. }
  1392. fold_line(p_line);
  1393. }
  1394. bool CodeEdit::is_line_folded(int p_line) const {
  1395. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1396. return p_line + 1 < get_line_count() && !_is_line_hidden(p_line) && _is_line_hidden(p_line + 1);
  1397. }
  1398. TypedArray<int> CodeEdit::get_folded_lines() const {
  1399. TypedArray<int> folded_lines;
  1400. for (int i = 0; i < get_line_count(); i++) {
  1401. if (is_line_folded(i)) {
  1402. folded_lines.push_back(i);
  1403. }
  1404. }
  1405. return folded_lines;
  1406. }
  1407. /* Delimiters */
  1408. // Strings
  1409. void CodeEdit::add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  1410. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_STRING);
  1411. }
  1412. void CodeEdit::remove_string_delimiter(const String &p_start_key) {
  1413. _remove_delimiter(p_start_key, TYPE_STRING);
  1414. }
  1415. bool CodeEdit::has_string_delimiter(const String &p_start_key) const {
  1416. return _has_delimiter(p_start_key, TYPE_STRING);
  1417. }
  1418. void CodeEdit::set_string_delimiters(const TypedArray<String> &p_string_delimiters) {
  1419. _set_delimiters(p_string_delimiters, TYPE_STRING);
  1420. }
  1421. void CodeEdit::clear_string_delimiters() {
  1422. _clear_delimiters(TYPE_STRING);
  1423. }
  1424. TypedArray<String> CodeEdit::get_string_delimiters() const {
  1425. return _get_delimiters(TYPE_STRING);
  1426. }
  1427. int CodeEdit::is_in_string(int p_line, int p_column) const {
  1428. return _is_in_delimiter(p_line, p_column, TYPE_STRING);
  1429. }
  1430. // Comments
  1431. void CodeEdit::add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  1432. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_COMMENT);
  1433. }
  1434. void CodeEdit::remove_comment_delimiter(const String &p_start_key) {
  1435. _remove_delimiter(p_start_key, TYPE_COMMENT);
  1436. }
  1437. bool CodeEdit::has_comment_delimiter(const String &p_start_key) const {
  1438. return _has_delimiter(p_start_key, TYPE_COMMENT);
  1439. }
  1440. void CodeEdit::set_comment_delimiters(const TypedArray<String> &p_comment_delimiters) {
  1441. _set_delimiters(p_comment_delimiters, TYPE_COMMENT);
  1442. }
  1443. void CodeEdit::clear_comment_delimiters() {
  1444. _clear_delimiters(TYPE_COMMENT);
  1445. }
  1446. TypedArray<String> CodeEdit::get_comment_delimiters() const {
  1447. return _get_delimiters(TYPE_COMMENT);
  1448. }
  1449. int CodeEdit::is_in_comment(int p_line, int p_column) const {
  1450. return _is_in_delimiter(p_line, p_column, TYPE_COMMENT);
  1451. }
  1452. String CodeEdit::get_delimiter_start_key(int p_delimiter_idx) const {
  1453. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  1454. return delimiters[p_delimiter_idx].start_key;
  1455. }
  1456. String CodeEdit::get_delimiter_end_key(int p_delimiter_idx) const {
  1457. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  1458. return delimiters[p_delimiter_idx].end_key;
  1459. }
  1460. Point2 CodeEdit::get_delimiter_start_position(int p_line, int p_column) const {
  1461. if (delimiters.size() == 0) {
  1462. return Point2(-1, -1);
  1463. }
  1464. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  1465. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  1466. Point2 start_position;
  1467. start_position.y = -1;
  1468. start_position.x = -1;
  1469. bool in_region = ((p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->get()) != -1;
  1470. /* Check the keys for this line. */
  1471. for (const KeyValue<int, int> &E : delimiter_cache[p_line]) {
  1472. if (E.key > p_column) {
  1473. break;
  1474. }
  1475. in_region = E.value != -1;
  1476. start_position.x = in_region ? E.key : -1;
  1477. }
  1478. /* Region was found on this line and is not a multiline continuation. */
  1479. int line_length = get_line(p_line).length();
  1480. if (start_position.x != -1 && line_length > 0 && start_position.x != line_length + 1) {
  1481. start_position.y = p_line;
  1482. return start_position;
  1483. }
  1484. /* Not in a region */
  1485. if (!in_region) {
  1486. return start_position;
  1487. }
  1488. /* Region starts on a previous line */
  1489. for (int i = p_line - 1; i >= 0; i--) {
  1490. if (delimiter_cache[i].size() < 1) {
  1491. continue;
  1492. }
  1493. start_position.y = i;
  1494. start_position.x = delimiter_cache[i].back()->key();
  1495. /* Make sure it's not a multiline continuation. */
  1496. line_length = get_line(i).length();
  1497. if (line_length > 0 && start_position.x != line_length + 1) {
  1498. break;
  1499. }
  1500. }
  1501. return start_position;
  1502. }
  1503. Point2 CodeEdit::get_delimiter_end_position(int p_line, int p_column) const {
  1504. if (delimiters.size() == 0) {
  1505. return Point2(-1, -1);
  1506. }
  1507. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  1508. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  1509. Point2 end_position;
  1510. end_position.y = -1;
  1511. end_position.x = -1;
  1512. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  1513. /* Check the keys for this line. */
  1514. for (const KeyValue<int, int> &E : delimiter_cache[p_line]) {
  1515. end_position.x = (E.value == -1) ? E.key : -1;
  1516. if (E.key > p_column) {
  1517. break;
  1518. }
  1519. region = E.value;
  1520. }
  1521. /* Region was found on this line and is not a multiline continuation. */
  1522. if (region != -1 && end_position.x != -1 && (delimiters[region].line_only || end_position.x != get_line(p_line).length() + 1)) {
  1523. end_position.y = p_line;
  1524. return end_position;
  1525. }
  1526. /* Not in a region */
  1527. if (region == -1) {
  1528. end_position.x = -1;
  1529. return end_position;
  1530. }
  1531. /* Region ends on a later line */
  1532. for (int i = p_line + 1; i < get_line_count(); i++) {
  1533. if (delimiter_cache[i].size() < 1 || delimiter_cache[i].front()->value() != -1) {
  1534. continue;
  1535. }
  1536. end_position.x = delimiter_cache[i].front()->key();
  1537. /* Make sure it's not a multiline continuation. */
  1538. if (get_line(i).length() > 0 && end_position.x != get_line(i).length() + 1) {
  1539. end_position.y = i;
  1540. break;
  1541. }
  1542. end_position.x = -1;
  1543. }
  1544. return end_position;
  1545. }
  1546. /* Code hint */
  1547. void CodeEdit::set_code_hint(const String &p_hint) {
  1548. code_hint = p_hint;
  1549. code_hint_xpos = -0xFFFF;
  1550. queue_redraw();
  1551. }
  1552. void CodeEdit::set_code_hint_draw_below(bool p_below) {
  1553. code_hint_draw_below = p_below;
  1554. queue_redraw();
  1555. }
  1556. /* Code Completion */
  1557. void CodeEdit::set_code_completion_enabled(bool p_enable) {
  1558. code_completion_enabled = p_enable;
  1559. }
  1560. bool CodeEdit::is_code_completion_enabled() const {
  1561. return code_completion_enabled;
  1562. }
  1563. void CodeEdit::set_code_completion_prefixes(const TypedArray<String> &p_prefixes) {
  1564. code_completion_prefixes.clear();
  1565. for (int i = 0; i < p_prefixes.size(); i++) {
  1566. const String prefix = p_prefixes[i];
  1567. ERR_CONTINUE_MSG(prefix.is_empty(), "Code completion prefix cannot be empty.");
  1568. code_completion_prefixes.insert(prefix[0]);
  1569. }
  1570. }
  1571. TypedArray<String> CodeEdit::get_code_completion_prefixes() const {
  1572. TypedArray<String> prefixes;
  1573. for (const char32_t &E : code_completion_prefixes) {
  1574. prefixes.push_back(String::chr(E));
  1575. }
  1576. return prefixes;
  1577. }
  1578. String CodeEdit::get_text_for_code_completion() const {
  1579. StringBuilder completion_text;
  1580. const int text_size = get_line_count();
  1581. for (int i = 0; i < text_size; i++) {
  1582. String line = get_line(i);
  1583. if (i == get_caret_line()) {
  1584. completion_text += line.substr(0, get_caret_column());
  1585. /* Not unicode, represents the caret. */
  1586. completion_text += String::chr(0xFFFF);
  1587. completion_text += line.substr(get_caret_column(), line.size());
  1588. } else {
  1589. completion_text += line;
  1590. }
  1591. if (i != text_size - 1) {
  1592. completion_text += "\n";
  1593. }
  1594. }
  1595. return completion_text.as_string();
  1596. }
  1597. void CodeEdit::request_code_completion(bool p_force) {
  1598. if (GDVIRTUAL_CALL(_request_code_completion, p_force)) {
  1599. return;
  1600. }
  1601. /* Don't re-query if all existing options are quoted types, eg path, signal. */
  1602. bool ignored = code_completion_active && !code_completion_options.is_empty();
  1603. if (ignored) {
  1604. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT;
  1605. const ScriptLanguage::CodeCompletionOption *previous_option = nullptr;
  1606. for (int i = 0; i < code_completion_options.size(); i++) {
  1607. const ScriptLanguage::CodeCompletionOption &current_option = code_completion_options[i];
  1608. if (!previous_option) {
  1609. previous_option = &current_option;
  1610. kind = current_option.kind;
  1611. }
  1612. if (previous_option->kind != current_option.kind) {
  1613. ignored = false;
  1614. break;
  1615. }
  1616. }
  1617. ignored = ignored && (kind == ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH || kind == ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH || kind == ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL);
  1618. }
  1619. if (ignored) {
  1620. return;
  1621. }
  1622. if (p_force) {
  1623. emit_signal(SNAME("code_completion_requested"));
  1624. return;
  1625. }
  1626. String line = get_line(get_caret_line());
  1627. int ofs = CLAMP(get_caret_column(), 0, line.length());
  1628. if (ofs > 0 && (is_in_string(get_caret_line(), ofs) != -1 || !is_symbol(line[ofs - 1]) || code_completion_prefixes.has(line[ofs - 1]))) {
  1629. emit_signal(SNAME("code_completion_requested"));
  1630. } else if (ofs > 1 && line[ofs - 1] == ' ' && code_completion_prefixes.has(line[ofs - 2])) {
  1631. emit_signal(SNAME("code_completion_requested"));
  1632. }
  1633. }
  1634. 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) {
  1635. ScriptLanguage::CodeCompletionOption completion_option;
  1636. completion_option.kind = (ScriptLanguage::CodeCompletionKind)p_type;
  1637. completion_option.display = p_display_text;
  1638. completion_option.insert_text = p_insert_text;
  1639. completion_option.font_color = p_text_color;
  1640. completion_option.icon = p_icon;
  1641. completion_option.default_value = p_value;
  1642. code_completion_option_submitted.push_back(completion_option);
  1643. }
  1644. void CodeEdit::update_code_completion_options(bool p_forced) {
  1645. code_completion_forced = p_forced;
  1646. code_completion_option_sources = code_completion_option_submitted;
  1647. code_completion_option_submitted.clear();
  1648. _filter_code_completion_candidates_impl();
  1649. }
  1650. TypedArray<Dictionary> CodeEdit::get_code_completion_options() const {
  1651. if (!code_completion_active) {
  1652. return TypedArray<Dictionary>();
  1653. }
  1654. TypedArray<Dictionary> completion_options;
  1655. completion_options.resize(code_completion_options.size());
  1656. for (int i = 0; i < code_completion_options.size(); i++) {
  1657. Dictionary option;
  1658. option["kind"] = code_completion_options[i].kind;
  1659. option["display_text"] = code_completion_options[i].display;
  1660. option["insert_text"] = code_completion_options[i].insert_text;
  1661. option["font_color"] = code_completion_options[i].font_color;
  1662. option["icon"] = code_completion_options[i].icon;
  1663. option["default_value"] = code_completion_options[i].default_value;
  1664. completion_options[i] = option;
  1665. }
  1666. return completion_options;
  1667. }
  1668. Dictionary CodeEdit::get_code_completion_option(int p_index) const {
  1669. if (!code_completion_active) {
  1670. return Dictionary();
  1671. }
  1672. ERR_FAIL_INDEX_V(p_index, code_completion_options.size(), Dictionary());
  1673. Dictionary option;
  1674. option["kind"] = code_completion_options[p_index].kind;
  1675. option["display_text"] = code_completion_options[p_index].display;
  1676. option["insert_text"] = code_completion_options[p_index].insert_text;
  1677. option["font_color"] = code_completion_options[p_index].font_color;
  1678. option["icon"] = code_completion_options[p_index].icon;
  1679. option["default_value"] = code_completion_options[p_index].default_value;
  1680. return option;
  1681. }
  1682. int CodeEdit::get_code_completion_selected_index() const {
  1683. return (code_completion_active) ? code_completion_current_selected : -1;
  1684. }
  1685. void CodeEdit::set_code_completion_selected_index(int p_index) {
  1686. if (!code_completion_active) {
  1687. return;
  1688. }
  1689. ERR_FAIL_INDEX(p_index, code_completion_options.size());
  1690. code_completion_current_selected = p_index;
  1691. code_completion_force_item_center = -1;
  1692. queue_redraw();
  1693. }
  1694. void CodeEdit::confirm_code_completion(bool p_replace) {
  1695. if (!is_editable() || !code_completion_active) {
  1696. return;
  1697. }
  1698. if (GDVIRTUAL_CALL(_confirm_code_completion, p_replace)) {
  1699. return;
  1700. }
  1701. char32_t caret_last_completion_char;
  1702. begin_complex_operation();
  1703. Vector<int> caret_edit_order = get_caret_index_edit_order();
  1704. for (const int &i : caret_edit_order) {
  1705. int caret_line = get_caret_line(i);
  1706. const String &insert_text = code_completion_options[code_completion_current_selected].insert_text;
  1707. const String &display_text = code_completion_options[code_completion_current_selected].display;
  1708. if (p_replace) {
  1709. // Find end of current section.
  1710. const String line = get_line(caret_line);
  1711. int caret_col = get_caret_column(i);
  1712. int caret_remove_line = caret_line;
  1713. bool merge_text = true;
  1714. int in_string = is_in_string(caret_line, caret_col);
  1715. if (in_string != -1) {
  1716. Point2 string_end = get_delimiter_end_position(caret_line, caret_col);
  1717. if (string_end.x != -1) {
  1718. merge_text = false;
  1719. caret_remove_line = string_end.y;
  1720. caret_col = string_end.x - 1;
  1721. }
  1722. }
  1723. if (merge_text) {
  1724. for (; caret_col < line.length(); caret_col++) {
  1725. if (is_symbol(line[caret_col])) {
  1726. break;
  1727. }
  1728. }
  1729. }
  1730. // Replace.
  1731. remove_text(caret_line, get_caret_column(i) - code_completion_base.length(), caret_remove_line, caret_col);
  1732. adjust_carets_after_edit(i, caret_line, caret_col - code_completion_base.length(), caret_remove_line, caret_col);
  1733. set_caret_column(get_caret_column(i) - code_completion_base.length(), false, i);
  1734. insert_text_at_caret(insert_text, i);
  1735. } else {
  1736. // Get first non-matching char.
  1737. const String line = get_line(caret_line);
  1738. int caret_col = get_caret_column(i);
  1739. int matching_chars = code_completion_base.length();
  1740. for (; matching_chars <= insert_text.length(); matching_chars++) {
  1741. if (caret_col >= line.length() || line[caret_col] != insert_text[matching_chars]) {
  1742. break;
  1743. }
  1744. caret_col++;
  1745. }
  1746. // Remove base completion text.
  1747. remove_text(caret_line, get_caret_column(i) - code_completion_base.length(), caret_line, get_caret_column(i));
  1748. adjust_carets_after_edit(i, caret_line, get_caret_column(i) - code_completion_base.length(), caret_line, get_caret_column(i));
  1749. set_caret_column(get_caret_column(i) - code_completion_base.length(), false, i);
  1750. // Merge with text.
  1751. insert_text_at_caret(insert_text.substr(0, code_completion_base.length()), i);
  1752. set_caret_column(caret_col, false, i);
  1753. insert_text_at_caret(insert_text.substr(matching_chars), i);
  1754. }
  1755. //* Handle merging of symbols eg strings, brackets.
  1756. const String line = get_line(caret_line);
  1757. char32_t next_char = line[get_caret_column(i)];
  1758. char32_t last_completion_char = insert_text[insert_text.length() - 1];
  1759. if (i == 0) {
  1760. caret_last_completion_char = last_completion_char;
  1761. }
  1762. char32_t last_completion_char_display = display_text[display_text.length() - 1];
  1763. bool last_char_matches = (last_completion_char == next_char || last_completion_char_display == next_char);
  1764. int pre_brace_pair = get_caret_column(i) > 0 ? _get_auto_brace_pair_open_at_pos(caret_line, get_caret_column(i)) : -1;
  1765. 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;
  1766. // Strings do not nest like brackets, so ensure we don't add an additional closing pair.
  1767. if (has_string_delimiter(String::chr(last_completion_char)) && post_brace_pair != -1 && last_char_matches) {
  1768. remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
  1769. adjust_carets_after_edit(i, caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
  1770. } else {
  1771. if (pre_brace_pair != -1 && pre_brace_pair != post_brace_pair && last_char_matches) {
  1772. remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
  1773. adjust_carets_after_edit(i, caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
  1774. } else if (auto_brace_completion_enabled && pre_brace_pair != -1) {
  1775. insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key, i);
  1776. set_caret_column(get_caret_column(i) - auto_brace_completion_pairs[pre_brace_pair].close_key.length(), i == 0, i);
  1777. }
  1778. }
  1779. if (pre_brace_pair == -1 && post_brace_pair == -1 && get_caret_column(i) > 0 && get_caret_column(i) < get_line(caret_line).length()) {
  1780. pre_brace_pair = _get_auto_brace_pair_open_at_pos(caret_line, get_caret_column(i) + 1);
  1781. if (pre_brace_pair != -1 && pre_brace_pair == _get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i) - 1)) {
  1782. remove_text(caret_line, get_caret_column(i) - 2, caret_line, get_caret_column(i));
  1783. adjust_carets_after_edit(i, caret_line, get_caret_column(i) - 2, caret_line, get_caret_column(i));
  1784. if (_get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i) - 1) != pre_brace_pair) {
  1785. set_caret_column(get_caret_column(i) - 1, i == 0, i);
  1786. }
  1787. }
  1788. }
  1789. }
  1790. end_complex_operation();
  1791. cancel_code_completion();
  1792. if (code_completion_prefixes.has(caret_last_completion_char)) {
  1793. request_code_completion();
  1794. }
  1795. }
  1796. void CodeEdit::cancel_code_completion() {
  1797. if (!code_completion_active) {
  1798. return;
  1799. }
  1800. code_completion_forced = false;
  1801. code_completion_active = false;
  1802. queue_redraw();
  1803. }
  1804. /* Line length guidelines */
  1805. void CodeEdit::set_line_length_guidelines(TypedArray<int> p_guideline_columns) {
  1806. line_length_guideline_columns = p_guideline_columns;
  1807. queue_redraw();
  1808. }
  1809. TypedArray<int> CodeEdit::get_line_length_guidelines() const {
  1810. return line_length_guideline_columns;
  1811. }
  1812. /* Symbol lookup */
  1813. void CodeEdit::set_symbol_lookup_on_click_enabled(bool p_enabled) {
  1814. symbol_lookup_on_click_enabled = p_enabled;
  1815. set_symbol_lookup_word_as_valid(false);
  1816. }
  1817. bool CodeEdit::is_symbol_lookup_on_click_enabled() const {
  1818. return symbol_lookup_on_click_enabled;
  1819. }
  1820. String CodeEdit::get_text_for_symbol_lookup() {
  1821. Point2i mp = get_local_mouse_pos();
  1822. Point2i pos = get_line_column_at_pos(mp, false);
  1823. int line = pos.y;
  1824. int col = pos.x;
  1825. if (line == -1) {
  1826. return String();
  1827. }
  1828. StringBuilder lookup_text;
  1829. const int text_size = get_line_count();
  1830. for (int i = 0; i < text_size; i++) {
  1831. String line_text = get_line(i);
  1832. if (i == line) {
  1833. lookup_text += line_text.substr(0, col);
  1834. /* Not unicode, represents the cursor. */
  1835. lookup_text += String::chr(0xFFFF);
  1836. lookup_text += line_text.substr(col, line_text.size());
  1837. } else {
  1838. lookup_text += line_text;
  1839. }
  1840. if (i != text_size - 1) {
  1841. lookup_text += "\n";
  1842. }
  1843. }
  1844. return lookup_text.as_string();
  1845. }
  1846. void CodeEdit::set_symbol_lookup_word_as_valid(bool p_valid) {
  1847. symbol_lookup_word = p_valid ? symbol_lookup_new_word : "";
  1848. symbol_lookup_new_word = "";
  1849. if (lookup_symbol_word != symbol_lookup_word) {
  1850. _set_symbol_lookup_word(symbol_lookup_word);
  1851. }
  1852. }
  1853. void CodeEdit::_bind_methods() {
  1854. /* Indent management */
  1855. ClassDB::bind_method(D_METHOD("set_indent_size", "size"), &CodeEdit::set_indent_size);
  1856. ClassDB::bind_method(D_METHOD("get_indent_size"), &CodeEdit::get_indent_size);
  1857. ClassDB::bind_method(D_METHOD("set_indent_using_spaces", "use_spaces"), &CodeEdit::set_indent_using_spaces);
  1858. ClassDB::bind_method(D_METHOD("is_indent_using_spaces"), &CodeEdit::is_indent_using_spaces);
  1859. ClassDB::bind_method(D_METHOD("set_auto_indent_enabled", "enable"), &CodeEdit::set_auto_indent_enabled);
  1860. ClassDB::bind_method(D_METHOD("is_auto_indent_enabled"), &CodeEdit::is_auto_indent_enabled);
  1861. ClassDB::bind_method(D_METHOD("set_auto_indent_prefixes", "prefixes"), &CodeEdit::set_auto_indent_prefixes);
  1862. ClassDB::bind_method(D_METHOD("get_auto_indent_prefixes"), &CodeEdit::get_auto_indent_prefixes);
  1863. ClassDB::bind_method(D_METHOD("do_indent"), &CodeEdit::do_indent);
  1864. ClassDB::bind_method(D_METHOD("do_unindent"), &CodeEdit::do_unindent);
  1865. ClassDB::bind_method(D_METHOD("indent_lines"), &CodeEdit::indent_lines);
  1866. ClassDB::bind_method(D_METHOD("unindent_lines"), &CodeEdit::unindent_lines);
  1867. /* Auto brace completion */
  1868. ClassDB::bind_method(D_METHOD("set_auto_brace_completion_enabled", "enable"), &CodeEdit::set_auto_brace_completion_enabled);
  1869. ClassDB::bind_method(D_METHOD("is_auto_brace_completion_enabled"), &CodeEdit::is_auto_brace_completion_enabled);
  1870. ClassDB::bind_method(D_METHOD("set_highlight_matching_braces_enabled", "enable"), &CodeEdit::set_highlight_matching_braces_enabled);
  1871. ClassDB::bind_method(D_METHOD("is_highlight_matching_braces_enabled"), &CodeEdit::is_highlight_matching_braces_enabled);
  1872. ClassDB::bind_method(D_METHOD("add_auto_brace_completion_pair", "start_key", "end_key"), &CodeEdit::add_auto_brace_completion_pair);
  1873. ClassDB::bind_method(D_METHOD("set_auto_brace_completion_pairs", "pairs"), &CodeEdit::set_auto_brace_completion_pairs);
  1874. ClassDB::bind_method(D_METHOD("get_auto_brace_completion_pairs"), &CodeEdit::get_auto_brace_completion_pairs);
  1875. ClassDB::bind_method(D_METHOD("has_auto_brace_completion_open_key", "open_key"), &CodeEdit::has_auto_brace_completion_open_key);
  1876. ClassDB::bind_method(D_METHOD("has_auto_brace_completion_close_key", "close_key"), &CodeEdit::has_auto_brace_completion_close_key);
  1877. ClassDB::bind_method(D_METHOD("get_auto_brace_completion_close_key", "open_key"), &CodeEdit::get_auto_brace_completion_close_key);
  1878. /* Main Gutter */
  1879. ClassDB::bind_method(D_METHOD("set_draw_breakpoints_gutter", "enable"), &CodeEdit::set_draw_breakpoints_gutter);
  1880. ClassDB::bind_method(D_METHOD("is_drawing_breakpoints_gutter"), &CodeEdit::is_drawing_breakpoints_gutter);
  1881. ClassDB::bind_method(D_METHOD("set_draw_bookmarks_gutter", "enable"), &CodeEdit::set_draw_bookmarks_gutter);
  1882. ClassDB::bind_method(D_METHOD("is_drawing_bookmarks_gutter"), &CodeEdit::is_drawing_bookmarks_gutter);
  1883. ClassDB::bind_method(D_METHOD("set_draw_executing_lines_gutter", "enable"), &CodeEdit::set_draw_executing_lines_gutter);
  1884. ClassDB::bind_method(D_METHOD("is_drawing_executing_lines_gutter"), &CodeEdit::is_drawing_executing_lines_gutter);
  1885. // Breakpoints
  1886. ClassDB::bind_method(D_METHOD("set_line_as_breakpoint", "line", "breakpointed"), &CodeEdit::set_line_as_breakpoint);
  1887. ClassDB::bind_method(D_METHOD("is_line_breakpointed", "line"), &CodeEdit::is_line_breakpointed);
  1888. ClassDB::bind_method(D_METHOD("clear_breakpointed_lines"), &CodeEdit::clear_breakpointed_lines);
  1889. ClassDB::bind_method(D_METHOD("get_breakpointed_lines"), &CodeEdit::get_breakpointed_lines);
  1890. // Bookmarks
  1891. ClassDB::bind_method(D_METHOD("set_line_as_bookmarked", "line", "bookmarked"), &CodeEdit::set_line_as_bookmarked);
  1892. ClassDB::bind_method(D_METHOD("is_line_bookmarked", "line"), &CodeEdit::is_line_bookmarked);
  1893. ClassDB::bind_method(D_METHOD("clear_bookmarked_lines"), &CodeEdit::clear_bookmarked_lines);
  1894. ClassDB::bind_method(D_METHOD("get_bookmarked_lines"), &CodeEdit::get_bookmarked_lines);
  1895. // executing lines
  1896. ClassDB::bind_method(D_METHOD("set_line_as_executing", "line", "executing"), &CodeEdit::set_line_as_executing);
  1897. ClassDB::bind_method(D_METHOD("is_line_executing", "line"), &CodeEdit::is_line_executing);
  1898. ClassDB::bind_method(D_METHOD("clear_executing_lines"), &CodeEdit::clear_executing_lines);
  1899. ClassDB::bind_method(D_METHOD("get_executing_lines"), &CodeEdit::get_executing_lines);
  1900. /* Line numbers */
  1901. ClassDB::bind_method(D_METHOD("set_draw_line_numbers", "enable"), &CodeEdit::set_draw_line_numbers);
  1902. ClassDB::bind_method(D_METHOD("is_draw_line_numbers_enabled"), &CodeEdit::is_draw_line_numbers_enabled);
  1903. ClassDB::bind_method(D_METHOD("set_line_numbers_zero_padded", "enable"), &CodeEdit::set_line_numbers_zero_padded);
  1904. ClassDB::bind_method(D_METHOD("is_line_numbers_zero_padded"), &CodeEdit::is_line_numbers_zero_padded);
  1905. /* Fold Gutter */
  1906. ClassDB::bind_method(D_METHOD("set_draw_fold_gutter", "enable"), &CodeEdit::set_draw_fold_gutter);
  1907. ClassDB::bind_method(D_METHOD("is_drawing_fold_gutter"), &CodeEdit::is_drawing_fold_gutter);
  1908. /* Line folding */
  1909. ClassDB::bind_method(D_METHOD("set_line_folding_enabled", "enabled"), &CodeEdit::set_line_folding_enabled);
  1910. ClassDB::bind_method(D_METHOD("is_line_folding_enabled"), &CodeEdit::is_line_folding_enabled);
  1911. ClassDB::bind_method(D_METHOD("can_fold_line", "line"), &CodeEdit::can_fold_line);
  1912. ClassDB::bind_method(D_METHOD("fold_line", "line"), &CodeEdit::fold_line);
  1913. ClassDB::bind_method(D_METHOD("unfold_line", "line"), &CodeEdit::unfold_line);
  1914. ClassDB::bind_method(D_METHOD("fold_all_lines"), &CodeEdit::fold_all_lines);
  1915. ClassDB::bind_method(D_METHOD("unfold_all_lines"), &CodeEdit::unfold_all_lines);
  1916. ClassDB::bind_method(D_METHOD("toggle_foldable_line", "line"), &CodeEdit::toggle_foldable_line);
  1917. ClassDB::bind_method(D_METHOD("is_line_folded", "line"), &CodeEdit::is_line_folded);
  1918. ClassDB::bind_method(D_METHOD("get_folded_lines"), &CodeEdit::get_folded_lines);
  1919. /* Delimiters */
  1920. // Strings
  1921. ClassDB::bind_method(D_METHOD("add_string_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_string_delimiter, DEFVAL(false));
  1922. ClassDB::bind_method(D_METHOD("remove_string_delimiter", "start_key"), &CodeEdit::remove_string_delimiter);
  1923. ClassDB::bind_method(D_METHOD("has_string_delimiter", "start_key"), &CodeEdit::has_string_delimiter);
  1924. ClassDB::bind_method(D_METHOD("set_string_delimiters", "string_delimiters"), &CodeEdit::set_string_delimiters);
  1925. ClassDB::bind_method(D_METHOD("clear_string_delimiters"), &CodeEdit::clear_string_delimiters);
  1926. ClassDB::bind_method(D_METHOD("get_string_delimiters"), &CodeEdit::get_string_delimiters);
  1927. ClassDB::bind_method(D_METHOD("is_in_string", "line", "column"), &CodeEdit::is_in_string, DEFVAL(-1));
  1928. // Comments
  1929. ClassDB::bind_method(D_METHOD("add_comment_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_comment_delimiter, DEFVAL(false));
  1930. ClassDB::bind_method(D_METHOD("remove_comment_delimiter", "start_key"), &CodeEdit::remove_comment_delimiter);
  1931. ClassDB::bind_method(D_METHOD("has_comment_delimiter", "start_key"), &CodeEdit::has_comment_delimiter);
  1932. ClassDB::bind_method(D_METHOD("set_comment_delimiters", "comment_delimiters"), &CodeEdit::set_comment_delimiters);
  1933. ClassDB::bind_method(D_METHOD("clear_comment_delimiters"), &CodeEdit::clear_comment_delimiters);
  1934. ClassDB::bind_method(D_METHOD("get_comment_delimiters"), &CodeEdit::get_comment_delimiters);
  1935. ClassDB::bind_method(D_METHOD("is_in_comment", "line", "column"), &CodeEdit::is_in_comment, DEFVAL(-1));
  1936. // Util
  1937. ClassDB::bind_method(D_METHOD("get_delimiter_start_key", "delimiter_index"), &CodeEdit::get_delimiter_start_key);
  1938. ClassDB::bind_method(D_METHOD("get_delimiter_end_key", "delimiter_index"), &CodeEdit::get_delimiter_end_key);
  1939. ClassDB::bind_method(D_METHOD("get_delimiter_start_position", "line", "column"), &CodeEdit::get_delimiter_start_position);
  1940. ClassDB::bind_method(D_METHOD("get_delimiter_end_position", "line", "column"), &CodeEdit::get_delimiter_end_position);
  1941. /* Code hint */
  1942. ClassDB::bind_method(D_METHOD("set_code_hint", "code_hint"), &CodeEdit::set_code_hint);
  1943. ClassDB::bind_method(D_METHOD("set_code_hint_draw_below", "draw_below"), &CodeEdit::set_code_hint_draw_below);
  1944. /* Code Completion */
  1945. BIND_ENUM_CONSTANT(KIND_CLASS);
  1946. BIND_ENUM_CONSTANT(KIND_FUNCTION);
  1947. BIND_ENUM_CONSTANT(KIND_SIGNAL);
  1948. BIND_ENUM_CONSTANT(KIND_VARIABLE);
  1949. BIND_ENUM_CONSTANT(KIND_MEMBER);
  1950. BIND_ENUM_CONSTANT(KIND_ENUM);
  1951. BIND_ENUM_CONSTANT(KIND_CONSTANT);
  1952. BIND_ENUM_CONSTANT(KIND_NODE_PATH);
  1953. BIND_ENUM_CONSTANT(KIND_FILE_PATH);
  1954. BIND_ENUM_CONSTANT(KIND_PLAIN_TEXT);
  1955. ClassDB::bind_method(D_METHOD("get_text_for_code_completion"), &CodeEdit::get_text_for_code_completion);
  1956. ClassDB::bind_method(D_METHOD("request_code_completion", "force"), &CodeEdit::request_code_completion, DEFVAL(false));
  1957. 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));
  1958. ClassDB::bind_method(D_METHOD("update_code_completion_options", "force"), &CodeEdit::update_code_completion_options);
  1959. ClassDB::bind_method(D_METHOD("get_code_completion_options"), &CodeEdit::get_code_completion_options);
  1960. ClassDB::bind_method(D_METHOD("get_code_completion_option", "index"), &CodeEdit::get_code_completion_option);
  1961. ClassDB::bind_method(D_METHOD("get_code_completion_selected_index"), &CodeEdit::get_code_completion_selected_index);
  1962. ClassDB::bind_method(D_METHOD("set_code_completion_selected_index", "index"), &CodeEdit::set_code_completion_selected_index);
  1963. ClassDB::bind_method(D_METHOD("confirm_code_completion", "replace"), &CodeEdit::confirm_code_completion, DEFVAL(false));
  1964. ClassDB::bind_method(D_METHOD("cancel_code_completion"), &CodeEdit::cancel_code_completion);
  1965. ClassDB::bind_method(D_METHOD("set_code_completion_enabled", "enable"), &CodeEdit::set_code_completion_enabled);
  1966. ClassDB::bind_method(D_METHOD("is_code_completion_enabled"), &CodeEdit::is_code_completion_enabled);
  1967. ClassDB::bind_method(D_METHOD("set_code_completion_prefixes", "prefixes"), &CodeEdit::set_code_completion_prefixes);
  1968. ClassDB::bind_method(D_METHOD("get_code_completion_prefixes"), &CodeEdit::get_code_completion_prefixes);
  1969. // Overridable
  1970. GDVIRTUAL_BIND(_confirm_code_completion, "replace")
  1971. GDVIRTUAL_BIND(_request_code_completion, "force")
  1972. GDVIRTUAL_BIND(_filter_code_completion_candidates, "candidates")
  1973. /* Line length guidelines */
  1974. ClassDB::bind_method(D_METHOD("set_line_length_guidelines", "guideline_columns"), &CodeEdit::set_line_length_guidelines);
  1975. ClassDB::bind_method(D_METHOD("get_line_length_guidelines"), &CodeEdit::get_line_length_guidelines);
  1976. /* Symbol lookup */
  1977. ClassDB::bind_method(D_METHOD("set_symbol_lookup_on_click_enabled", "enable"), &CodeEdit::set_symbol_lookup_on_click_enabled);
  1978. ClassDB::bind_method(D_METHOD("is_symbol_lookup_on_click_enabled"), &CodeEdit::is_symbol_lookup_on_click_enabled);
  1979. ClassDB::bind_method(D_METHOD("get_text_for_symbol_lookup"), &CodeEdit::get_text_for_symbol_lookup);
  1980. ClassDB::bind_method(D_METHOD("set_symbol_lookup_word_as_valid", "valid"), &CodeEdit::set_symbol_lookup_word_as_valid);
  1981. /* Inspector */
  1982. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "symbol_lookup_on_click"), "set_symbol_lookup_on_click_enabled", "is_symbol_lookup_on_click_enabled");
  1983. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "line_folding"), "set_line_folding_enabled", "is_line_folding_enabled");
  1984. ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "line_length_guidelines"), "set_line_length_guidelines", "get_line_length_guidelines");
  1985. ADD_GROUP("Gutters", "gutters_");
  1986. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_breakpoints_gutter"), "set_draw_breakpoints_gutter", "is_drawing_breakpoints_gutter");
  1987. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_bookmarks"), "set_draw_bookmarks_gutter", "is_drawing_bookmarks_gutter");
  1988. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_executing_lines"), "set_draw_executing_lines_gutter", "is_drawing_executing_lines_gutter");
  1989. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_line_numbers"), "set_draw_line_numbers", "is_draw_line_numbers_enabled");
  1990. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_zero_pad_line_numbers"), "set_line_numbers_zero_padded", "is_line_numbers_zero_padded");
  1991. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_fold_gutter"), "set_draw_fold_gutter", "is_drawing_fold_gutter");
  1992. ADD_GROUP("Delimiters", "delimiter_");
  1993. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_strings"), "set_string_delimiters", "get_string_delimiters");
  1994. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_comments"), "set_comment_delimiters", "get_comment_delimiters");
  1995. ADD_GROUP("Code Completion", "code_completion_");
  1996. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "code_completion_enabled"), "set_code_completion_enabled", "is_code_completion_enabled");
  1997. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "code_completion_prefixes"), "set_code_completion_prefixes", "get_code_completion_prefixes");
  1998. ADD_GROUP("Indentation", "indent_");
  1999. ADD_PROPERTY(PropertyInfo(Variant::INT, "indent_size"), "set_indent_size", "get_indent_size");
  2000. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_use_spaces"), "set_indent_using_spaces", "is_indent_using_spaces");
  2001. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_automatic"), "set_auto_indent_enabled", "is_auto_indent_enabled");
  2002. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "indent_automatic_prefixes"), "set_auto_indent_prefixes", "get_auto_indent_prefixes");
  2003. ADD_GROUP("Auto Brace Completion", "auto_brace_completion_");
  2004. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_enabled"), "set_auto_brace_completion_enabled", "is_auto_brace_completion_enabled");
  2005. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_highlight_matching"), "set_highlight_matching_braces_enabled", "is_highlight_matching_braces_enabled");
  2006. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "auto_brace_completion_pairs"), "set_auto_brace_completion_pairs", "get_auto_brace_completion_pairs");
  2007. /* Signals */
  2008. /* Gutters */
  2009. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "line")));
  2010. /* Code Completion */
  2011. ADD_SIGNAL(MethodInfo("code_completion_requested"));
  2012. /* Symbol lookup */
  2013. ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "column")));
  2014. ADD_SIGNAL(MethodInfo("symbol_validate", PropertyInfo(Variant::STRING, "symbol")));
  2015. }
  2016. /* Auto brace completion */
  2017. int CodeEdit::_get_auto_brace_pair_open_at_pos(int p_line, int p_col) {
  2018. const String &line = get_line(p_line);
  2019. /* Should be fast enough, expecting low amount of pairs... */
  2020. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  2021. const String &open_key = auto_brace_completion_pairs[i].open_key;
  2022. if (p_col - open_key.length() < 0) {
  2023. continue;
  2024. }
  2025. bool is_match = true;
  2026. for (int j = 0; j < open_key.length(); j++) {
  2027. if (line[(p_col - 1) - j] != open_key[(open_key.length() - 1) - j]) {
  2028. is_match = false;
  2029. break;
  2030. }
  2031. }
  2032. if (is_match) {
  2033. return i;
  2034. }
  2035. }
  2036. return -1;
  2037. }
  2038. int CodeEdit::_get_auto_brace_pair_close_at_pos(int p_line, int p_col) {
  2039. const String &line = get_line(p_line);
  2040. /* Should be fast enough, expecting low amount of pairs... */
  2041. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  2042. if (p_col + auto_brace_completion_pairs[i].close_key.length() > line.length()) {
  2043. continue;
  2044. }
  2045. bool is_match = true;
  2046. for (int j = 0; j < auto_brace_completion_pairs[i].close_key.length(); j++) {
  2047. if (line[p_col + j] != auto_brace_completion_pairs[i].close_key[j]) {
  2048. is_match = false;
  2049. break;
  2050. }
  2051. }
  2052. if (is_match) {
  2053. return i;
  2054. }
  2055. }
  2056. return -1;
  2057. }
  2058. /* Gutters */
  2059. void CodeEdit::_gutter_clicked(int p_line, int p_gutter) {
  2060. bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  2061. if (p_gutter == main_gutter) {
  2062. if (draw_breakpoints && !shift_pressed) {
  2063. set_line_as_breakpoint(p_line, !is_line_breakpointed(p_line));
  2064. } else if (draw_bookmarks && shift_pressed) {
  2065. set_line_as_bookmarked(p_line, !is_line_bookmarked(p_line));
  2066. }
  2067. return;
  2068. }
  2069. if (p_gutter == line_number_gutter) {
  2070. remove_secondary_carets();
  2071. set_selection_mode(TextEdit::SelectionMode::SELECTION_MODE_LINE, p_line, 0);
  2072. select(p_line, 0, p_line + 1, 0);
  2073. set_caret_line(p_line + 1);
  2074. set_caret_column(0);
  2075. return;
  2076. }
  2077. if (p_gutter == fold_gutter) {
  2078. if (is_line_folded(p_line)) {
  2079. unfold_line(p_line);
  2080. } else if (can_fold_line(p_line)) {
  2081. fold_line(p_line);
  2082. }
  2083. return;
  2084. }
  2085. }
  2086. void CodeEdit::_update_gutter_indexes() {
  2087. for (int i = 0; i < get_gutter_count(); i++) {
  2088. if (get_gutter_name(i) == "main_gutter") {
  2089. main_gutter = i;
  2090. continue;
  2091. }
  2092. if (get_gutter_name(i) == "line_numbers") {
  2093. line_number_gutter = i;
  2094. continue;
  2095. }
  2096. if (get_gutter_name(i) == "fold_gutter") {
  2097. fold_gutter = i;
  2098. continue;
  2099. }
  2100. }
  2101. }
  2102. /* Delimiters */
  2103. void CodeEdit::_update_delimiter_cache(int p_from_line, int p_to_line) {
  2104. if (delimiters.size() == 0) {
  2105. return;
  2106. }
  2107. int line_count = get_line_count();
  2108. if (p_to_line == -1) {
  2109. p_to_line = line_count;
  2110. }
  2111. int start_line = MIN(p_from_line, p_to_line);
  2112. int end_line = MAX(p_from_line, p_to_line);
  2113. /* Make sure delimiter_cache has all the lines. */
  2114. if (start_line != end_line) {
  2115. if (p_to_line < p_from_line) {
  2116. for (int i = end_line; i > start_line; i--) {
  2117. delimiter_cache.remove_at(i);
  2118. }
  2119. } else {
  2120. for (int i = start_line; i < end_line; i++) {
  2121. delimiter_cache.insert(i, RBMap<int, int>());
  2122. }
  2123. }
  2124. }
  2125. int in_region = -1;
  2126. for (int i = start_line; i < MIN(end_line + 1, line_count); i++) {
  2127. int current_end_region = (i <= 0 || delimiter_cache[i].size() < 1) ? -1 : delimiter_cache[i].back()->value();
  2128. in_region = (i <= 0 || delimiter_cache[i - 1].size() < 1) ? -1 : delimiter_cache[i - 1].back()->value();
  2129. const String &str = get_line(i);
  2130. const int line_length = str.length();
  2131. delimiter_cache.write[i].clear();
  2132. if (str.length() == 0) {
  2133. if (in_region != -1) {
  2134. delimiter_cache.write[i][0] = in_region;
  2135. }
  2136. if (i == end_line && current_end_region != in_region) {
  2137. end_line++;
  2138. end_line = MIN(end_line, line_count);
  2139. }
  2140. continue;
  2141. }
  2142. int end_region = -1;
  2143. for (int j = 0; j < line_length; j++) {
  2144. int from = j;
  2145. for (; from < line_length; from++) {
  2146. if (str[from] == '\\') {
  2147. from++;
  2148. continue;
  2149. }
  2150. break;
  2151. }
  2152. /* check if we are in entering a region */
  2153. bool same_line = false;
  2154. if (in_region == -1) {
  2155. for (int d = 0; d < delimiters.size(); d++) {
  2156. /* check there is enough room */
  2157. int chars_left = line_length - from;
  2158. int start_key_length = delimiters[d].start_key.length();
  2159. int end_key_length = delimiters[d].end_key.length();
  2160. if (chars_left < start_key_length) {
  2161. continue;
  2162. }
  2163. /* search the line */
  2164. bool match = true;
  2165. const char32_t *start_key = delimiters[d].start_key.get_data();
  2166. for (int k = 0; k < start_key_length; k++) {
  2167. if (start_key[k] != str[from + k]) {
  2168. match = false;
  2169. break;
  2170. }
  2171. }
  2172. if (!match) {
  2173. continue;
  2174. }
  2175. same_line = true;
  2176. in_region = d;
  2177. delimiter_cache.write[i][from + 1] = d;
  2178. from += start_key_length;
  2179. /* check if it's the whole line */
  2180. if (end_key_length == 0 || delimiters[d].line_only || from + end_key_length > line_length) {
  2181. j = line_length;
  2182. if (delimiters[d].line_only) {
  2183. delimiter_cache.write[i][line_length + 1] = -1;
  2184. } else {
  2185. end_region = in_region;
  2186. }
  2187. }
  2188. break;
  2189. }
  2190. if (j == line_length || in_region == -1) {
  2191. continue;
  2192. }
  2193. }
  2194. /* if we are in one find the end key */
  2195. /* search the line */
  2196. int region_end_index = -1;
  2197. int end_key_length = delimiters[in_region].end_key.length();
  2198. const char32_t *end_key = delimiters[in_region].end_key.get_data();
  2199. for (; from < line_length; from++) {
  2200. if (line_length - from < end_key_length) {
  2201. break;
  2202. }
  2203. if (!is_symbol(str[from])) {
  2204. continue;
  2205. }
  2206. if (str[from] == '\\') {
  2207. from++;
  2208. continue;
  2209. }
  2210. region_end_index = from;
  2211. for (int k = 0; k < end_key_length; k++) {
  2212. if (end_key[k] != str[from + k]) {
  2213. region_end_index = -1;
  2214. break;
  2215. }
  2216. }
  2217. if (region_end_index != -1) {
  2218. break;
  2219. }
  2220. }
  2221. j = from + (end_key_length - 1);
  2222. end_region = (region_end_index == -1) ? in_region : -1;
  2223. if (!same_line || region_end_index != -1) {
  2224. delimiter_cache.write[i][j + 1] = end_region;
  2225. }
  2226. in_region = -1;
  2227. }
  2228. if (i == end_line && current_end_region != end_region) {
  2229. end_line++;
  2230. end_line = MIN(end_line, line_count);
  2231. }
  2232. }
  2233. }
  2234. int CodeEdit::_is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const {
  2235. if (delimiters.size() == 0) {
  2236. return -1;
  2237. }
  2238. ERR_FAIL_INDEX_V(p_line, get_line_count(), 0);
  2239. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  2240. bool in_region = region != -1 && delimiters[region].type == p_type;
  2241. for (RBMap<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  2242. /* If column is specified, loop until the key is larger then the column. */
  2243. if (p_column != -1) {
  2244. if (E->key() > p_column) {
  2245. break;
  2246. }
  2247. in_region = E->value() != -1 && delimiters[E->value()].type == p_type;
  2248. region = in_region ? E->value() : -1;
  2249. continue;
  2250. }
  2251. /* If no column, calculate if the entire line is a region */
  2252. /* excluding whitespace. */
  2253. const String line = get_line(p_line);
  2254. if (!in_region) {
  2255. if (E->value() == -1 || delimiters[E->value()].type != p_type) {
  2256. break;
  2257. }
  2258. region = E->value();
  2259. in_region = true;
  2260. for (int i = E->key() - 2; i >= 0; i--) {
  2261. if (!is_whitespace(line[i])) {
  2262. return -1;
  2263. }
  2264. }
  2265. }
  2266. if (delimiters[region].line_only) {
  2267. return region;
  2268. }
  2269. int end_col = E->key();
  2270. if (E->value() != -1) {
  2271. if (!E->next()) {
  2272. return region;
  2273. }
  2274. end_col = E->next()->key();
  2275. }
  2276. for (int i = end_col; i < line.length(); i++) {
  2277. if (!is_whitespace(line[i])) {
  2278. return -1;
  2279. }
  2280. }
  2281. return region;
  2282. }
  2283. return in_region ? region : -1;
  2284. }
  2285. void CodeEdit::_add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type) {
  2286. // If we are the editor allow "null" as a valid start key, otherwise users cannot add delimiters via the inspector.
  2287. if (!(Engine::get_singleton()->is_editor_hint() && p_start_key == "null")) {
  2288. ERR_FAIL_COND_MSG(p_start_key.is_empty(), "delimiter start key cannot be empty");
  2289. for (int i = 0; i < p_start_key.length(); i++) {
  2290. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "delimiter must start with a symbol");
  2291. }
  2292. }
  2293. if (p_end_key.length() > 0) {
  2294. for (int i = 0; i < p_end_key.length(); i++) {
  2295. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "delimiter must end with a symbol");
  2296. }
  2297. }
  2298. int at = 0;
  2299. for (int i = 0; i < delimiters.size(); i++) {
  2300. ERR_FAIL_COND_MSG(delimiters[i].start_key == p_start_key, "delimiter with start key '" + p_start_key + "' already exists.");
  2301. if (p_start_key.length() < delimiters[i].start_key.length()) {
  2302. at++;
  2303. }
  2304. }
  2305. Delimiter delimiter;
  2306. delimiter.type = p_type;
  2307. delimiter.start_key = p_start_key;
  2308. delimiter.end_key = p_end_key;
  2309. delimiter.line_only = p_line_only || p_end_key.is_empty();
  2310. delimiters.insert(at, delimiter);
  2311. if (!setting_delimiters) {
  2312. delimiter_cache.clear();
  2313. _update_delimiter_cache();
  2314. }
  2315. }
  2316. void CodeEdit::_remove_delimiter(const String &p_start_key, DelimiterType p_type) {
  2317. for (int i = 0; i < delimiters.size(); i++) {
  2318. if (delimiters[i].start_key != p_start_key) {
  2319. continue;
  2320. }
  2321. if (delimiters[i].type != p_type) {
  2322. break;
  2323. }
  2324. delimiters.remove_at(i);
  2325. if (!setting_delimiters) {
  2326. delimiter_cache.clear();
  2327. _update_delimiter_cache();
  2328. }
  2329. break;
  2330. }
  2331. }
  2332. bool CodeEdit::_has_delimiter(const String &p_start_key, DelimiterType p_type) const {
  2333. for (int i = 0; i < delimiters.size(); i++) {
  2334. if (delimiters[i].start_key == p_start_key) {
  2335. return delimiters[i].type == p_type;
  2336. }
  2337. }
  2338. return false;
  2339. }
  2340. void CodeEdit::_set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type) {
  2341. setting_delimiters = true;
  2342. _clear_delimiters(p_type);
  2343. for (int i = 0; i < p_delimiters.size(); i++) {
  2344. String key = p_delimiters[i];
  2345. if (key.is_empty()) {
  2346. continue;
  2347. }
  2348. const String start_key = key.get_slice(" ", 0);
  2349. const String end_key = key.get_slice_count(" ") > 1 ? key.get_slice(" ", 1) : String();
  2350. _add_delimiter(start_key, end_key, end_key.is_empty(), p_type);
  2351. }
  2352. setting_delimiters = false;
  2353. _update_delimiter_cache();
  2354. }
  2355. void CodeEdit::_clear_delimiters(DelimiterType p_type) {
  2356. for (int i = delimiters.size() - 1; i >= 0; i--) {
  2357. if (delimiters[i].type == p_type) {
  2358. delimiters.remove_at(i);
  2359. }
  2360. }
  2361. delimiter_cache.clear();
  2362. if (!setting_delimiters) {
  2363. _update_delimiter_cache();
  2364. }
  2365. }
  2366. TypedArray<String> CodeEdit::_get_delimiters(DelimiterType p_type) const {
  2367. TypedArray<String> r_delimiters;
  2368. for (int i = 0; i < delimiters.size(); i++) {
  2369. if (delimiters[i].type != p_type) {
  2370. continue;
  2371. }
  2372. r_delimiters.push_back(delimiters[i].start_key + (delimiters[i].end_key.is_empty() ? "" : " " + delimiters[i].end_key));
  2373. }
  2374. return r_delimiters;
  2375. }
  2376. /* Code Completion */
  2377. void CodeEdit::_update_scroll_selected_line(float p_mouse_y) {
  2378. float percent = (float)(p_mouse_y - code_completion_scroll_rect.position.y) / code_completion_scroll_rect.size.height;
  2379. percent = CLAMP(percent, 0.0f, 1.0f);
  2380. code_completion_current_selected = (int)(percent * (code_completion_options.size() - 1));
  2381. code_completion_force_item_center = -1;
  2382. }
  2383. void CodeEdit::_filter_code_completion_candidates_impl() {
  2384. int line_height = get_line_height();
  2385. if (GDVIRTUAL_IS_OVERRIDDEN(_filter_code_completion_candidates)) {
  2386. code_completion_options.clear();
  2387. code_completion_base = "";
  2388. /* Build options argument. */
  2389. TypedArray<Dictionary> completion_options_sources;
  2390. completion_options_sources.resize(code_completion_option_sources.size());
  2391. int i = 0;
  2392. for (const ScriptLanguage::CodeCompletionOption &E : code_completion_option_sources) {
  2393. Dictionary option;
  2394. option["kind"] = E.kind;
  2395. option["display_text"] = E.display;
  2396. option["insert_text"] = E.insert_text;
  2397. option["font_color"] = E.font_color;
  2398. option["icon"] = E.icon;
  2399. option["default_value"] = E.default_value;
  2400. completion_options_sources[i] = option;
  2401. i++;
  2402. }
  2403. TypedArray<Dictionary> completion_options;
  2404. GDVIRTUAL_CALL(_filter_code_completion_candidates, completion_options_sources, completion_options);
  2405. /* No options to complete, cancel. */
  2406. if (completion_options.size() == 0) {
  2407. cancel_code_completion();
  2408. return;
  2409. }
  2410. /* Convert back into options. */
  2411. int max_width = 0;
  2412. for (i = 0; i < completion_options.size(); i++) {
  2413. ScriptLanguage::CodeCompletionOption option;
  2414. option.kind = (ScriptLanguage::CodeCompletionKind)(int)completion_options[i].get("kind");
  2415. option.display = completion_options[i].get("display_text");
  2416. option.insert_text = completion_options[i].get("insert_text");
  2417. option.font_color = completion_options[i].get("font_color");
  2418. option.icon = completion_options[i].get("icon");
  2419. option.default_value = completion_options[i].get("default_value");
  2420. int offset = 0;
  2421. if (option.default_value.get_type() == Variant::COLOR) {
  2422. offset = line_height;
  2423. }
  2424. if (font.is_valid()) {
  2425. max_width = MAX(max_width, font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width + offset);
  2426. }
  2427. code_completion_options.push_back(option);
  2428. }
  2429. code_completion_longest_line = MIN(max_width, code_completion_max_width * font_size);
  2430. code_completion_current_selected = 0;
  2431. code_completion_force_item_center = -1;
  2432. code_completion_active = true;
  2433. queue_redraw();
  2434. return;
  2435. }
  2436. const int caret_line = get_caret_line();
  2437. const int caret_column = get_caret_column();
  2438. const String line = get_line(caret_line);
  2439. ERR_FAIL_INDEX_MSG(caret_column - 1, line.length(), "Caret column exceeds line length.");
  2440. if (caret_column > 0 && line[caret_column - 1] == '(' && !code_completion_forced) {
  2441. cancel_code_completion();
  2442. return;
  2443. }
  2444. /* Get string status, are we in one or at the close. */
  2445. int in_string = is_in_string(caret_line, caret_column);
  2446. int first_quote_col = -1;
  2447. if (in_string != -1) {
  2448. Point2 string_start_pos = get_delimiter_start_position(caret_line, caret_column);
  2449. first_quote_col = (string_start_pos.y == caret_line) ? string_start_pos.x : -1;
  2450. } else if (caret_column > 0) {
  2451. if (is_in_string(caret_line, caret_column - 1) != -1) {
  2452. first_quote_col = caret_column - 1;
  2453. }
  2454. }
  2455. int cofs = caret_column;
  2456. String string_to_complete;
  2457. bool prev_is_word = false;
  2458. /* Cancel if we are at the close of a string. */
  2459. if (caret_column > 0 && in_string == -1 && first_quote_col == cofs - 1) {
  2460. cancel_code_completion();
  2461. return;
  2462. /* In a string, therefore we are trying to complete the string text. */
  2463. } else if (in_string != -1 && first_quote_col != -1) {
  2464. int key_length = delimiters[in_string].start_key.length();
  2465. string_to_complete = line.substr(first_quote_col - key_length, (cofs - first_quote_col) + key_length);
  2466. /* If we have a space, previous word might be a keyword. eg "func |". */
  2467. } else if (cofs > 0 && line[cofs - 1] == ' ') {
  2468. int ofs = cofs - 1;
  2469. while (ofs > 0 && line[ofs] == ' ') {
  2470. ofs--;
  2471. }
  2472. prev_is_word = !is_symbol(line[ofs]);
  2473. /* Otherwise get current word and set cofs to the start. */
  2474. } else {
  2475. int start_cofs = cofs;
  2476. while (cofs > 0 && line[cofs - 1] > 32 && (line[cofs - 1] == '/' || !is_symbol(line[cofs - 1]))) {
  2477. cofs--;
  2478. }
  2479. string_to_complete = line.substr(cofs, start_cofs - cofs);
  2480. }
  2481. /* If all else fails, check for a prefix. */
  2482. /* Single space between caret and prefix is okay. */
  2483. bool prev_is_prefix = false;
  2484. if (cofs > 0 && code_completion_prefixes.has(line[cofs - 1])) {
  2485. prev_is_prefix = true;
  2486. } else if (cofs > 1 && line[cofs - 1] == ' ' && code_completion_prefixes.has(line[cofs - 2])) {
  2487. prev_is_prefix = true;
  2488. }
  2489. if (!prev_is_word && string_to_complete.is_empty() && (cofs == 0 || !prev_is_prefix)) {
  2490. cancel_code_completion();
  2491. return;
  2492. }
  2493. /* Filter Options. */
  2494. /* For now handle only tradional quoted strings. */
  2495. bool single_quote = in_string != -1 && first_quote_col > 0 && delimiters[in_string].start_key == "'";
  2496. code_completion_options.clear();
  2497. code_completion_base = string_to_complete;
  2498. Vector<ScriptLanguage::CodeCompletionOption> completion_options_casei;
  2499. Vector<ScriptLanguage::CodeCompletionOption> completion_options_substr;
  2500. Vector<ScriptLanguage::CodeCompletionOption> completion_options_substr_casei;
  2501. Vector<ScriptLanguage::CodeCompletionOption> completion_options_subseq;
  2502. Vector<ScriptLanguage::CodeCompletionOption> completion_options_subseq_casei;
  2503. int max_width = 0;
  2504. String string_to_complete_lower = string_to_complete.to_lower();
  2505. for (ScriptLanguage::CodeCompletionOption &option : code_completion_option_sources) {
  2506. if (single_quote && option.display.is_quoted()) {
  2507. option.display = option.display.unquote().quote("'");
  2508. }
  2509. int offset = 0;
  2510. if (option.default_value.get_type() == Variant::COLOR) {
  2511. offset = line_height;
  2512. }
  2513. if (in_string != -1) {
  2514. String quote = single_quote ? "'" : "\"";
  2515. option.display = option.display.unquote().quote(quote);
  2516. option.insert_text = option.insert_text.unquote().quote(quote);
  2517. }
  2518. if (option.display.length() == 0) {
  2519. continue;
  2520. }
  2521. if (string_to_complete.length() == 0) {
  2522. code_completion_options.push_back(option);
  2523. if (font.is_valid()) {
  2524. max_width = MAX(max_width, font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width + offset);
  2525. }
  2526. continue;
  2527. }
  2528. /* This code works the same as:
  2529. if (option.display.begins_with(s)) {
  2530. completion_options.push_back(option);
  2531. } else if (option.display.to_lower().begins_with(s.to_lower())) {
  2532. completion_options_casei.push_back(option);
  2533. } else if (s.is_subsequence_of(option.display)) {
  2534. completion_options_subseq.push_back(option);
  2535. } else if (s.is_subsequence_ofn(option.display)) {
  2536. completion_options_subseq_casei.push_back(option);
  2537. }
  2538. But is more performant due to being inlined and looping over the characters only once
  2539. */
  2540. String display_lower = option.display.to_lower();
  2541. const char32_t *ssq = &string_to_complete[0];
  2542. const char32_t *ssq_lower = &string_to_complete_lower[0];
  2543. const char32_t *tgt = &option.display[0];
  2544. const char32_t *tgt_lower = &display_lower[0];
  2545. const char32_t *sst = &string_to_complete[0];
  2546. const char32_t *sst_lower = &display_lower[0];
  2547. Vector<Pair<int, int>> ssq_matches;
  2548. int ssq_match_start = 0;
  2549. int ssq_match_len = 0;
  2550. Vector<Pair<int, int>> ssq_lower_matches;
  2551. int ssq_lower_match_start = 0;
  2552. int ssq_lower_match_len = 0;
  2553. int sst_start = -1;
  2554. int sst_lower_start = -1;
  2555. for (int i = 0; *tgt; tgt++, tgt_lower++, i++) {
  2556. // Check substring.
  2557. if (*sst == *tgt) {
  2558. sst++;
  2559. if (sst_start == -1) {
  2560. sst_start = i;
  2561. }
  2562. } else if (sst_start != -1 && *sst) {
  2563. sst = &string_to_complete[0];
  2564. sst_start = -1;
  2565. }
  2566. // Check subsequence.
  2567. if (*ssq == *tgt) {
  2568. ssq++;
  2569. if (ssq_match_len == 0) {
  2570. ssq_match_start = i;
  2571. }
  2572. ssq_match_len++;
  2573. } else if (ssq_match_len > 0) {
  2574. ssq_matches.push_back(Pair<int, int>(ssq_match_start, ssq_match_len));
  2575. ssq_match_len = 0;
  2576. }
  2577. // Check lower substring.
  2578. if (*sst_lower == *tgt) {
  2579. sst_lower++;
  2580. if (sst_lower_start == -1) {
  2581. sst_lower_start = i;
  2582. }
  2583. } else if (sst_lower_start != -1 && *sst_lower) {
  2584. sst_lower = &string_to_complete[0];
  2585. sst_lower_start = -1;
  2586. }
  2587. // Check lower subsequence.
  2588. if (*ssq_lower == *tgt_lower) {
  2589. ssq_lower++;
  2590. if (ssq_lower_match_len == 0) {
  2591. ssq_lower_match_start = i;
  2592. }
  2593. ssq_lower_match_len++;
  2594. } else if (ssq_lower_match_len > 0) {
  2595. ssq_lower_matches.push_back(Pair<int, int>(ssq_lower_match_start, ssq_lower_match_len));
  2596. ssq_lower_match_len = 0;
  2597. }
  2598. }
  2599. /* Matched the whole subsequence in s. */
  2600. if (!*ssq) { // Matched the whole subsequence in s.
  2601. option.matches.clear();
  2602. if (sst_start == 0) { // Matched substring in beginning of s.
  2603. option.matches.push_back(Pair<int, int>(sst_start, string_to_complete.length()));
  2604. code_completion_options.push_back(option);
  2605. } else if (sst_start > 0) { // Matched substring in s.
  2606. option.matches.push_back(Pair<int, int>(sst_start, string_to_complete.length()));
  2607. completion_options_substr.push_back(option);
  2608. } else {
  2609. if (ssq_match_len > 0) {
  2610. ssq_matches.push_back(Pair<int, int>(ssq_match_start, ssq_match_len));
  2611. }
  2612. option.matches.append_array(ssq_matches);
  2613. completion_options_subseq.push_back(option);
  2614. }
  2615. if (font.is_valid()) {
  2616. max_width = MAX(max_width, font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width + offset);
  2617. }
  2618. } else if (!*ssq_lower) { // Matched the whole subsequence in s_lower.
  2619. option.matches.clear();
  2620. if (sst_lower_start == 0) { // Matched substring in beginning of s_lower.
  2621. option.matches.push_back(Pair<int, int>(sst_lower_start, string_to_complete.length()));
  2622. completion_options_casei.push_back(option);
  2623. } else if (sst_lower_start > 0) { // Matched substring in s_lower.
  2624. option.matches.push_back(Pair<int, int>(sst_lower_start, string_to_complete.length()));
  2625. completion_options_substr_casei.push_back(option);
  2626. } else {
  2627. if (ssq_lower_match_len > 0) {
  2628. ssq_lower_matches.push_back(Pair<int, int>(ssq_lower_match_start, ssq_lower_match_len));
  2629. }
  2630. option.matches.append_array(ssq_lower_matches);
  2631. completion_options_subseq_casei.push_back(option);
  2632. }
  2633. if (font.is_valid()) {
  2634. max_width = MAX(max_width, font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width + offset);
  2635. }
  2636. }
  2637. }
  2638. code_completion_options.append_array(completion_options_casei);
  2639. code_completion_options.append_array(completion_options_subseq);
  2640. code_completion_options.append_array(completion_options_subseq_casei);
  2641. /* No options to complete, cancel. */
  2642. if (code_completion_options.size() == 0) {
  2643. cancel_code_completion();
  2644. return;
  2645. }
  2646. /* A perfect match, stop completion. */
  2647. if (code_completion_options.size() == 1 && string_to_complete == code_completion_options[0].display) {
  2648. cancel_code_completion();
  2649. return;
  2650. }
  2651. code_completion_longest_line = MIN(max_width, code_completion_max_width * font_size);
  2652. code_completion_current_selected = 0;
  2653. code_completion_force_item_center = -1;
  2654. code_completion_active = true;
  2655. queue_redraw();
  2656. }
  2657. void CodeEdit::_lines_edited_from(int p_from_line, int p_to_line) {
  2658. _update_delimiter_cache(p_from_line, p_to_line);
  2659. if (p_from_line == p_to_line) {
  2660. return;
  2661. }
  2662. lines_edited_changed += p_to_line - p_from_line;
  2663. 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));
  2664. 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));
  2665. }
  2666. void CodeEdit::_text_set() {
  2667. lines_edited_from = 0;
  2668. lines_edited_to = 9999;
  2669. _text_changed();
  2670. }
  2671. void CodeEdit::_text_changed() {
  2672. if (lines_edited_from == -1) {
  2673. return;
  2674. }
  2675. int lc = get_line_count();
  2676. line_number_digits = 1;
  2677. while (lc /= 10) {
  2678. line_number_digits++;
  2679. }
  2680. if (font.is_valid()) {
  2681. set_gutter_width(line_number_gutter, (line_number_digits + 1) * font->get_char_size('0', font_size).width);
  2682. }
  2683. lc = get_line_count();
  2684. List<int> breakpoints;
  2685. for (const KeyValue<int, bool> &E : breakpointed_lines) {
  2686. breakpoints.push_back(E.key);
  2687. }
  2688. for (const int &line : breakpoints) {
  2689. if (line < lines_edited_from || (line < lc && is_line_breakpointed(line))) {
  2690. continue;
  2691. }
  2692. breakpointed_lines.erase(line);
  2693. emit_signal(SNAME("breakpoint_toggled"), line);
  2694. int next_line = line + lines_edited_changed;
  2695. if (next_line > -1 && next_line < lc && is_line_breakpointed(next_line)) {
  2696. emit_signal(SNAME("breakpoint_toggled"), next_line);
  2697. breakpointed_lines[next_line] = true;
  2698. continue;
  2699. }
  2700. }
  2701. lines_edited_from = -1;
  2702. lines_edited_to = -1;
  2703. lines_edited_changed = 0;
  2704. }
  2705. CodeEdit::CodeEdit() {
  2706. /* Indent management */
  2707. auto_indent_prefixes.insert(':');
  2708. auto_indent_prefixes.insert('{');
  2709. auto_indent_prefixes.insert('[');
  2710. auto_indent_prefixes.insert('(');
  2711. /* Auto brace completion */
  2712. add_auto_brace_completion_pair("(", ")");
  2713. add_auto_brace_completion_pair("{", "}");
  2714. add_auto_brace_completion_pair("[", "]");
  2715. add_auto_brace_completion_pair("\"", "\"");
  2716. add_auto_brace_completion_pair("\'", "\'");
  2717. /* Delimiter tracking */
  2718. add_string_delimiter("\"", "\"", false);
  2719. add_string_delimiter("\'", "\'", false);
  2720. /* Text Direction */
  2721. set_layout_direction(LAYOUT_DIRECTION_LTR);
  2722. set_text_direction(TEXT_DIRECTION_LTR);
  2723. /* Gutters */
  2724. int gutter_idx = 0;
  2725. /* Main Gutter */
  2726. add_gutter();
  2727. set_gutter_name(gutter_idx, "main_gutter");
  2728. set_gutter_draw(gutter_idx, false);
  2729. set_gutter_overwritable(gutter_idx, true);
  2730. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  2731. set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_main_gutter_draw_callback));
  2732. gutter_idx++;
  2733. /* Line numbers */
  2734. add_gutter();
  2735. set_gutter_name(gutter_idx, "line_numbers");
  2736. set_gutter_draw(gutter_idx, false);
  2737. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  2738. set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_line_number_draw_callback));
  2739. gutter_idx++;
  2740. /* Fold Gutter */
  2741. add_gutter();
  2742. set_gutter_name(gutter_idx, "fold_gutter");
  2743. set_gutter_draw(gutter_idx, false);
  2744. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  2745. set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_fold_gutter_draw_callback));
  2746. gutter_idx++;
  2747. connect("lines_edited_from", callable_mp(this, &CodeEdit::_lines_edited_from));
  2748. connect("text_set", callable_mp(this, &CodeEdit::_text_set));
  2749. connect("text_changed", callable_mp(this, &CodeEdit::_text_changed));
  2750. connect("gutter_clicked", callable_mp(this, &CodeEdit::_gutter_clicked));
  2751. connect("gutter_added", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  2752. connect("gutter_removed", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  2753. _update_gutter_indexes();
  2754. }
  2755. CodeEdit::~CodeEdit() {
  2756. }