code_edit.cpp 107 KB

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