code_edit.cpp 93 KB

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