code_edit.cpp 111 KB

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