code_edit.cpp 104 KB

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