code_edit.cpp 99 KB

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