code_edit.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  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("breakpoint_color");
  48. breakpoint_icon = get_theme_icon("breakpoint");
  49. bookmark_color = get_theme_color("bookmark_color");
  50. bookmark_icon = get_theme_icon("bookmark");
  51. executing_line_color = get_theme_color("executing_line_color");
  52. executing_line_icon = get_theme_icon("executing_line");
  53. line_number_color = get_theme_color("line_number_color");
  54. folding_color = get_theme_color("code_folding_color");
  55. can_fold_icon = get_theme_icon("can_fold");
  56. folded_icon = get_theme_icon("folded");
  57. code_completion_max_width = get_theme_constant("completion_max_width") * cache.font->get_char_size('x').x;
  58. code_completion_max_lines = get_theme_constant("completion_lines");
  59. code_completion_scroll_width = get_theme_constant("completion_scroll_width");
  60. code_completion_scroll_color = get_theme_color("completion_scroll_color");
  61. code_completion_background_color = get_theme_color("completion_background_color");
  62. code_completion_selected_color = get_theme_color("completion_selected_color");
  63. code_completion_existing_color = get_theme_color("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("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("hseparation", "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("panel", "TooltipPanel");
  143. Color font_color = get_theme_color("font_color", "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_at_cursor();
  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. /* Override input to unfold lines where needed. */
  343. if (!is_readonly()) {
  344. if (k->is_action("ui_text_newline_above", true) || k->is_action("ui_text_newline_blank", true) || k->is_action("ui_text_newline", true)) {
  345. unfold_line(cursor_get_line());
  346. }
  347. if (cursor_get_line() > 0 && k->is_action("ui_text_backspace", true)) {
  348. unfold_line(cursor_get_line() - 1);
  349. }
  350. }
  351. /* Remove shift otherwise actions will not match. */
  352. k = k->duplicate();
  353. k->set_shift_pressed(false);
  354. if (k->is_action("ui_text_caret_up", true) ||
  355. k->is_action("ui_text_caret_down", true) ||
  356. k->is_action("ui_text_caret_line_start", true) ||
  357. k->is_action("ui_text_caret_line_end", true) ||
  358. k->is_action("ui_text_caret_page_up", true) ||
  359. k->is_action("ui_text_caret_page_down", true)) {
  360. set_code_hint("");
  361. }
  362. TextEdit::_gui_input(p_gui_input);
  363. if (update_code_completion) {
  364. _filter_code_completion_candidates();
  365. }
  366. }
  367. Control::CursorShape CodeEdit::get_cursor_shape(const Point2 &p_pos) const {
  368. if ((code_completion_active && code_completion_rect.has_point(p_pos)) || (is_readonly() && (!is_selecting_enabled() || get_line_count() == 0))) {
  369. return CURSOR_ARROW;
  370. }
  371. int line, col;
  372. _get_mouse_pos(p_pos, line, col);
  373. if (is_line_folded(line)) {
  374. int wrap_index = get_line_wrap_index_at_col(line, col);
  375. if (wrap_index == times_line_wraps(line)) {
  376. int eol_icon_width = cache.folded_eol_icon->get_width();
  377. int left_margin = get_total_gutter_width() + eol_icon_width + get_line_width(line, wrap_index) - get_h_scroll();
  378. if (p_pos.x > left_margin && p_pos.x <= left_margin + eol_icon_width + 3) {
  379. return CURSOR_POINTING_HAND;
  380. }
  381. }
  382. }
  383. return TextEdit::get_cursor_shape(p_pos);
  384. }
  385. /* Main Gutter */
  386. void CodeEdit::_update_draw_main_gutter() {
  387. set_gutter_draw(main_gutter, draw_breakpoints || draw_bookmarks || draw_executing_lines);
  388. }
  389. void CodeEdit::set_draw_breakpoints_gutter(bool p_draw) {
  390. draw_breakpoints = p_draw;
  391. set_gutter_clickable(main_gutter, p_draw);
  392. _update_draw_main_gutter();
  393. }
  394. bool CodeEdit::is_drawing_breakpoints_gutter() const {
  395. return draw_breakpoints;
  396. }
  397. void CodeEdit::set_draw_bookmarks_gutter(bool p_draw) {
  398. draw_bookmarks = p_draw;
  399. _update_draw_main_gutter();
  400. }
  401. bool CodeEdit::is_drawing_bookmarks_gutter() const {
  402. return draw_bookmarks;
  403. }
  404. void CodeEdit::set_draw_executing_lines_gutter(bool p_draw) {
  405. draw_executing_lines = p_draw;
  406. _update_draw_main_gutter();
  407. }
  408. bool CodeEdit::is_drawing_executing_lines_gutter() const {
  409. return draw_executing_lines;
  410. }
  411. void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  412. if (draw_breakpoints && is_line_breakpointed(p_line)) {
  413. int padding = p_region.size.x / 6;
  414. Rect2 breakpoint_region = p_region;
  415. breakpoint_region.position += Point2(padding, padding);
  416. breakpoint_region.size -= Point2(padding, padding) * 2;
  417. breakpoint_icon->draw_rect(get_canvas_item(), breakpoint_region, false, breakpoint_color);
  418. }
  419. if (draw_bookmarks && is_line_bookmarked(p_line)) {
  420. int horizontal_padding = p_region.size.x / 2;
  421. int vertical_padding = p_region.size.y / 4;
  422. Rect2 bookmark_region = p_region;
  423. bookmark_region.position += Point2(horizontal_padding, 0);
  424. bookmark_region.size -= Point2(horizontal_padding * 1.1, vertical_padding);
  425. bookmark_icon->draw_rect(get_canvas_item(), bookmark_region, false, bookmark_color);
  426. }
  427. if (draw_executing_lines && is_line_executing(p_line)) {
  428. int horizontal_padding = p_region.size.x / 10;
  429. int vertical_padding = p_region.size.y / 4;
  430. Rect2 executing_line_region = p_region;
  431. executing_line_region.position += Point2(horizontal_padding, vertical_padding);
  432. executing_line_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  433. executing_line_icon->draw_rect(get_canvas_item(), executing_line_region, false, executing_line_color);
  434. }
  435. }
  436. // Breakpoints
  437. void CodeEdit::set_line_as_breakpoint(int p_line, bool p_breakpointed) {
  438. int mask = get_line_gutter_metadata(p_line, main_gutter);
  439. set_line_gutter_metadata(p_line, main_gutter, p_breakpointed ? mask | MAIN_GUTTER_BREAKPOINT : mask & ~MAIN_GUTTER_BREAKPOINT);
  440. if (p_breakpointed) {
  441. breakpointed_lines[p_line] = true;
  442. } else if (breakpointed_lines.has(p_line)) {
  443. breakpointed_lines.erase(p_line);
  444. }
  445. emit_signal("breakpoint_toggled", p_line);
  446. update();
  447. }
  448. bool CodeEdit::is_line_breakpointed(int p_line) const {
  449. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BREAKPOINT;
  450. }
  451. void CodeEdit::clear_breakpointed_lines() {
  452. for (int i = 0; i < get_line_count(); i++) {
  453. if (is_line_breakpointed(i)) {
  454. set_line_as_breakpoint(i, false);
  455. }
  456. }
  457. }
  458. Array CodeEdit::get_breakpointed_lines() const {
  459. Array ret;
  460. for (int i = 0; i < get_line_count(); i++) {
  461. if (is_line_breakpointed(i)) {
  462. ret.append(i);
  463. }
  464. }
  465. return ret;
  466. }
  467. // Bookmarks
  468. void CodeEdit::set_line_as_bookmarked(int p_line, bool p_bookmarked) {
  469. int mask = get_line_gutter_metadata(p_line, main_gutter);
  470. set_line_gutter_metadata(p_line, main_gutter, p_bookmarked ? mask | MAIN_GUTTER_BOOKMARK : mask & ~MAIN_GUTTER_BOOKMARK);
  471. update();
  472. }
  473. bool CodeEdit::is_line_bookmarked(int p_line) const {
  474. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BOOKMARK;
  475. }
  476. void CodeEdit::clear_bookmarked_lines() {
  477. for (int i = 0; i < get_line_count(); i++) {
  478. if (is_line_bookmarked(i)) {
  479. set_line_as_bookmarked(i, false);
  480. }
  481. }
  482. }
  483. Array CodeEdit::get_bookmarked_lines() const {
  484. Array ret;
  485. for (int i = 0; i < get_line_count(); i++) {
  486. if (is_line_bookmarked(i)) {
  487. ret.append(i);
  488. }
  489. }
  490. return ret;
  491. }
  492. // executing lines
  493. void CodeEdit::set_line_as_executing(int p_line, bool p_executing) {
  494. int mask = get_line_gutter_metadata(p_line, main_gutter);
  495. set_line_gutter_metadata(p_line, main_gutter, p_executing ? mask | MAIN_GUTTER_EXECUTING : mask & ~MAIN_GUTTER_EXECUTING);
  496. update();
  497. }
  498. bool CodeEdit::is_line_executing(int p_line) const {
  499. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_EXECUTING;
  500. }
  501. void CodeEdit::clear_executing_lines() {
  502. for (int i = 0; i < get_line_count(); i++) {
  503. if (is_line_executing(i)) {
  504. set_line_as_executing(i, false);
  505. }
  506. }
  507. }
  508. Array CodeEdit::get_executing_lines() const {
  509. Array ret;
  510. for (int i = 0; i < get_line_count(); i++) {
  511. if (is_line_executing(i)) {
  512. ret.append(i);
  513. }
  514. }
  515. return ret;
  516. }
  517. /* Line numbers */
  518. void CodeEdit::set_draw_line_numbers(bool p_draw) {
  519. set_gutter_draw(line_number_gutter, p_draw);
  520. }
  521. bool CodeEdit::is_draw_line_numbers_enabled() const {
  522. return is_gutter_drawn(line_number_gutter);
  523. }
  524. void CodeEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
  525. p_zero_padded ? line_number_padding = "0" : line_number_padding = " ";
  526. update();
  527. }
  528. bool CodeEdit::is_line_numbers_zero_padded() const {
  529. return line_number_padding == "0";
  530. }
  531. void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  532. String fc = TS->format_number(String::num(p_line + 1).lpad(line_number_digits, line_number_padding));
  533. Ref<TextLine> tl;
  534. tl.instantiate();
  535. tl->add_string(fc, cache.font, cache.font_size);
  536. int yofs = p_region.position.y + (get_row_height() - tl->get_size().y) / 2;
  537. Color number_color = get_line_gutter_item_color(p_line, line_number_gutter);
  538. if (number_color == Color(1, 1, 1)) {
  539. number_color = line_number_color;
  540. }
  541. tl->draw(get_canvas_item(), Point2(p_region.position.x, yofs), number_color);
  542. }
  543. /* Fold Gutter */
  544. void CodeEdit::set_draw_fold_gutter(bool p_draw) {
  545. set_gutter_draw(fold_gutter, p_draw);
  546. }
  547. bool CodeEdit::is_drawing_fold_gutter() const {
  548. return is_gutter_drawn(fold_gutter);
  549. }
  550. void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region) {
  551. if (!can_fold_line(p_line) && !is_line_folded(p_line)) {
  552. set_line_gutter_clickable(p_line, fold_gutter, false);
  553. return;
  554. }
  555. set_line_gutter_clickable(p_line, fold_gutter, true);
  556. int horizontal_padding = p_region.size.x / 10;
  557. int vertical_padding = p_region.size.y / 6;
  558. p_region.position += Point2(horizontal_padding, vertical_padding);
  559. p_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  560. if (can_fold_line(p_line)) {
  561. can_fold_icon->draw_rect(get_canvas_item(), p_region, false, folding_color);
  562. return;
  563. }
  564. folded_icon->draw_rect(get_canvas_item(), p_region, false, folding_color);
  565. }
  566. /* Line Folding */
  567. void CodeEdit::set_line_folding_enabled(bool p_enabled) {
  568. line_folding_enabled = p_enabled;
  569. set_hiding_enabled(p_enabled);
  570. }
  571. bool CodeEdit::is_line_folding_enabled() const {
  572. return line_folding_enabled;
  573. }
  574. bool CodeEdit::can_fold_line(int p_line) const {
  575. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  576. if (!line_folding_enabled) {
  577. return false;
  578. }
  579. if (p_line + 1 >= get_line_count() || get_line(p_line).strip_edges().size() == 0) {
  580. return false;
  581. }
  582. if (is_line_hidden(p_line) || is_line_folded(p_line)) {
  583. return false;
  584. }
  585. /* Check for full multiline line or block strings / comments. */
  586. int in_comment = is_in_comment(p_line);
  587. int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
  588. if (in_string != -1 || in_comment != -1) {
  589. if (get_delimiter_start_position(p_line, get_line(p_line).size() - 1).y != p_line) {
  590. return false;
  591. }
  592. int delimter_end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
  593. /* No end line, therefore we have a multiline region over the rest of the file. */
  594. if (delimter_end_line == -1) {
  595. return true;
  596. }
  597. /* End line is the same therefore we have a block. */
  598. if (delimter_end_line == p_line) {
  599. /* Check we are the start of the block. */
  600. if (p_line - 1 >= 0) {
  601. if ((in_string != -1 && is_in_string(p_line - 1) != -1) || (in_comment != -1 && is_in_comment(p_line - 1) != -1)) {
  602. return false;
  603. }
  604. }
  605. /* Check it continues for at least one line. */
  606. return ((in_string != -1 && is_in_string(p_line + 1) != -1) || (in_comment != -1 && is_in_comment(p_line + 1) != -1));
  607. }
  608. return ((in_string != -1 && is_in_string(delimter_end_line) != -1) || (in_comment != -1 && is_in_comment(delimter_end_line) != -1));
  609. }
  610. /* Otherwise check indent levels. */
  611. int start_indent = get_indent_level(p_line);
  612. for (int i = p_line + 1; i < get_line_count(); i++) {
  613. if (is_in_string(i) != -1 || is_in_comment(i) != -1 || get_line(i).strip_edges().size() == 0) {
  614. continue;
  615. }
  616. return (get_indent_level(i) > start_indent);
  617. }
  618. return false;
  619. }
  620. void CodeEdit::fold_line(int p_line) {
  621. ERR_FAIL_INDEX(p_line, get_line_count());
  622. if (!is_line_folding_enabled() || !can_fold_line(p_line)) {
  623. return;
  624. }
  625. /* Find the last line to be hidden. */
  626. int end_line = get_line_count();
  627. int in_comment = is_in_comment(p_line);
  628. int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
  629. if (in_string != -1 || in_comment != -1) {
  630. end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
  631. /* End line is the same therefore we have a block. */
  632. if (end_line == p_line) {
  633. for (int i = p_line + 1; i < get_line_count(); i++) {
  634. if ((in_string != -1 && is_in_string(i) == -1) || (in_comment != -1 && is_in_comment(i) == -1)) {
  635. end_line = i - 1;
  636. break;
  637. }
  638. }
  639. }
  640. } else {
  641. int start_indent = get_indent_level(p_line);
  642. for (int i = p_line + 1; i < get_line_count(); i++) {
  643. if (get_line(p_line).strip_edges().size() == 0 || is_in_string(i) != -1 || is_in_comment(i) != -1) {
  644. end_line = i;
  645. continue;
  646. }
  647. if (get_indent_level(i) <= start_indent && get_line(i).strip_edges().size() != 0) {
  648. end_line = i - 1;
  649. break;
  650. }
  651. }
  652. }
  653. for (int i = p_line + 1; i <= end_line; i++) {
  654. set_line_as_hidden(i, true);
  655. }
  656. /* Fix selection. */
  657. if (is_selection_active()) {
  658. if (is_line_hidden(get_selection_from_line()) && is_line_hidden(get_selection_to_line())) {
  659. deselect();
  660. } else if (is_line_hidden(get_selection_from_line())) {
  661. select(p_line, 9999, get_selection_to_line(), get_selection_to_column());
  662. } else if (is_line_hidden(get_selection_to_line())) {
  663. select(get_selection_from_line(), get_selection_from_column(), p_line, 9999);
  664. }
  665. }
  666. /* Reset caret. */
  667. if (is_line_hidden(cursor_get_line())) {
  668. cursor_set_line(p_line, false, false);
  669. cursor_set_column(get_line(p_line).length(), false);
  670. }
  671. update();
  672. }
  673. void CodeEdit::unfold_line(int p_line) {
  674. ERR_FAIL_INDEX(p_line, get_line_count());
  675. if (!is_line_folded(p_line) && !is_line_hidden(p_line)) {
  676. return;
  677. }
  678. int fold_start = p_line;
  679. for (; fold_start > 0; fold_start--) {
  680. if (is_line_folded(fold_start)) {
  681. break;
  682. }
  683. }
  684. fold_start = is_line_folded(fold_start) ? fold_start : p_line;
  685. for (int i = fold_start + 1; i < get_line_count(); i++) {
  686. if (!is_line_hidden(i)) {
  687. break;
  688. }
  689. set_line_as_hidden(i, false);
  690. }
  691. update();
  692. }
  693. void CodeEdit::fold_all_lines() {
  694. for (int i = 0; i < get_line_count(); i++) {
  695. fold_line(i);
  696. }
  697. update();
  698. }
  699. void CodeEdit::unfold_all_lines() {
  700. unhide_all_lines();
  701. }
  702. void CodeEdit::toggle_foldable_line(int p_line) {
  703. ERR_FAIL_INDEX(p_line, get_line_count());
  704. if (is_line_folded(p_line)) {
  705. unfold_line(p_line);
  706. return;
  707. }
  708. fold_line(p_line);
  709. }
  710. bool CodeEdit::is_line_folded(int p_line) const {
  711. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  712. return p_line + 1 < get_line_count() && !is_line_hidden(p_line) && is_line_hidden(p_line + 1);
  713. }
  714. TypedArray<int> CodeEdit::get_folded_lines() const {
  715. TypedArray<int> folded_lines;
  716. for (int i = 0; i < get_line_count(); i++) {
  717. if (is_line_folded(i)) {
  718. folded_lines.push_back(i);
  719. }
  720. }
  721. return folded_lines;
  722. }
  723. /* Delimiters */
  724. // Strings
  725. void CodeEdit::add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  726. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_STRING);
  727. }
  728. void CodeEdit::remove_string_delimiter(const String &p_start_key) {
  729. _remove_delimiter(p_start_key, TYPE_STRING);
  730. }
  731. bool CodeEdit::has_string_delimiter(const String &p_start_key) const {
  732. return _has_delimiter(p_start_key, TYPE_STRING);
  733. }
  734. void CodeEdit::set_string_delimiters(const TypedArray<String> &p_string_delimiters) {
  735. _set_delimiters(p_string_delimiters, TYPE_STRING);
  736. }
  737. void CodeEdit::clear_string_delimiters() {
  738. _clear_delimiters(TYPE_STRING);
  739. }
  740. TypedArray<String> CodeEdit::get_string_delimiters() const {
  741. return _get_delimiters(TYPE_STRING);
  742. }
  743. int CodeEdit::is_in_string(int p_line, int p_column) const {
  744. return _is_in_delimiter(p_line, p_column, TYPE_STRING);
  745. }
  746. // Comments
  747. void CodeEdit::add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  748. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_COMMENT);
  749. }
  750. void CodeEdit::remove_comment_delimiter(const String &p_start_key) {
  751. _remove_delimiter(p_start_key, TYPE_COMMENT);
  752. }
  753. bool CodeEdit::has_comment_delimiter(const String &p_start_key) const {
  754. return _has_delimiter(p_start_key, TYPE_COMMENT);
  755. }
  756. void CodeEdit::set_comment_delimiters(const TypedArray<String> &p_comment_delimiters) {
  757. _set_delimiters(p_comment_delimiters, TYPE_COMMENT);
  758. }
  759. void CodeEdit::clear_comment_delimiters() {
  760. _clear_delimiters(TYPE_COMMENT);
  761. }
  762. TypedArray<String> CodeEdit::get_comment_delimiters() const {
  763. return _get_delimiters(TYPE_COMMENT);
  764. }
  765. int CodeEdit::is_in_comment(int p_line, int p_column) const {
  766. return _is_in_delimiter(p_line, p_column, TYPE_COMMENT);
  767. }
  768. String CodeEdit::get_delimiter_start_key(int p_delimiter_idx) const {
  769. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  770. return delimiters[p_delimiter_idx].start_key;
  771. }
  772. String CodeEdit::get_delimiter_end_key(int p_delimiter_idx) const {
  773. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  774. return delimiters[p_delimiter_idx].end_key;
  775. }
  776. Point2 CodeEdit::get_delimiter_start_position(int p_line, int p_column) const {
  777. if (delimiters.size() == 0) {
  778. return Point2(-1, -1);
  779. }
  780. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  781. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  782. Point2 start_position;
  783. start_position.y = -1;
  784. start_position.x = -1;
  785. bool in_region = ((p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value()) != -1;
  786. /* Check the keys for this line. */
  787. for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  788. if (E->key() > p_column) {
  789. break;
  790. }
  791. in_region = E->value() != -1;
  792. start_position.x = in_region ? E->key() : -1;
  793. }
  794. /* Region was found on this line and is not a multiline continuation. */
  795. if (start_position.x != -1 && start_position.x != get_line(p_line).length() + 1) {
  796. start_position.y = p_line;
  797. return start_position;
  798. }
  799. /* Not in a region */
  800. if (!in_region) {
  801. return start_position;
  802. }
  803. /* Region starts on a previous line */
  804. for (int i = p_line - 1; i >= 0; i--) {
  805. if (delimiter_cache[i].size() < 1) {
  806. continue;
  807. }
  808. start_position.y = i;
  809. start_position.x = delimiter_cache[i].back()->key();
  810. /* Make sure it's not a multiline continuation. */
  811. if (start_position.x != get_line(i).length() + 1) {
  812. break;
  813. }
  814. }
  815. return start_position;
  816. }
  817. Point2 CodeEdit::get_delimiter_end_position(int p_line, int p_column) const {
  818. if (delimiters.size() == 0) {
  819. return Point2(-1, -1);
  820. }
  821. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  822. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  823. Point2 end_position;
  824. end_position.y = -1;
  825. end_position.x = -1;
  826. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  827. /* Check the keys for this line. */
  828. for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  829. end_position.x = (E->value() == -1) ? E->key() : -1;
  830. if (E->key() > p_column) {
  831. break;
  832. }
  833. region = E->value();
  834. }
  835. /* Region was found on this line and is not a multiline continuation. */
  836. if (region != -1 && end_position.x != -1 && (delimiters[region].line_only || end_position.x != get_line(p_line).length() + 1)) {
  837. end_position.y = p_line;
  838. return end_position;
  839. }
  840. /* Not in a region */
  841. if (region == -1) {
  842. end_position.x = -1;
  843. return end_position;
  844. }
  845. /* Region ends on a later line */
  846. for (int i = p_line + 1; i < get_line_count(); i++) {
  847. if (delimiter_cache[i].size() < 1 || delimiter_cache[i].front()->value() != -1) {
  848. continue;
  849. }
  850. end_position.x = delimiter_cache[i].front()->key();
  851. /* Make sure it's not a multiline continuation. */
  852. if (get_line(i).length() > 0 && end_position.x != get_line(i).length() + 1) {
  853. end_position.y = i;
  854. break;
  855. }
  856. end_position.x = -1;
  857. }
  858. return end_position;
  859. }
  860. /* Code hint */
  861. void CodeEdit::set_code_hint(const String &p_hint) {
  862. code_hint = p_hint;
  863. code_hint_xpos = -0xFFFF;
  864. update();
  865. }
  866. void CodeEdit::set_code_hint_draw_below(bool p_below) {
  867. code_hint_draw_below = p_below;
  868. update();
  869. }
  870. /* Code Completion */
  871. void CodeEdit::set_code_completion_enabled(bool p_enable) {
  872. code_completion_enabled = p_enable;
  873. }
  874. bool CodeEdit::is_code_completion_enabled() const {
  875. return code_completion_enabled;
  876. }
  877. void CodeEdit::set_code_completion_prefixes(const TypedArray<String> &p_prefixes) {
  878. code_completion_prefixes.clear();
  879. for (int i = 0; i < p_prefixes.size(); i++) {
  880. code_completion_prefixes.insert(p_prefixes[i]);
  881. }
  882. }
  883. TypedArray<String> CodeEdit::get_code_completion_prefixes() const {
  884. TypedArray<String> prefixes;
  885. for (Set<String>::Element *E = code_completion_prefixes.front(); E; E = E->next()) {
  886. prefixes.push_back(E->get());
  887. }
  888. return prefixes;
  889. }
  890. String CodeEdit::get_text_for_code_completion() const {
  891. StringBuilder completion_text;
  892. const int text_size = get_line_count();
  893. for (int i = 0; i < text_size; i++) {
  894. String line = get_line(i);
  895. if (i == cursor_get_line()) {
  896. completion_text += line.substr(0, cursor_get_column());
  897. /* Not unicode, represents the caret. */
  898. completion_text += String::chr(0xFFFF);
  899. completion_text += line.substr(cursor_get_column(), line.size());
  900. } else {
  901. completion_text += line;
  902. }
  903. if (i != text_size - 1) {
  904. completion_text += "\n";
  905. }
  906. }
  907. return completion_text.as_string();
  908. }
  909. void CodeEdit::request_code_completion(bool p_force) {
  910. ScriptInstance *si = get_script_instance();
  911. if (si && si->has_method("_request_code_completion")) {
  912. si->call("_request_code_completion", p_force);
  913. return;
  914. }
  915. /* Don't re-query if all existing options are quoted types, eg path, signal. */
  916. bool ignored = code_completion_active && !code_completion_options.is_empty();
  917. if (ignored) {
  918. ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_PLAIN_TEXT;
  919. const ScriptCodeCompletionOption *previous_option = nullptr;
  920. for (int i = 0; i < code_completion_options.size(); i++) {
  921. const ScriptCodeCompletionOption &current_option = code_completion_options[i];
  922. if (!previous_option) {
  923. previous_option = &current_option;
  924. kind = current_option.kind;
  925. }
  926. if (previous_option->kind != current_option.kind) {
  927. ignored = false;
  928. break;
  929. }
  930. }
  931. ignored = ignored && (kind == ScriptCodeCompletionOption::KIND_FILE_PATH || kind == ScriptCodeCompletionOption::KIND_NODE_PATH || kind == ScriptCodeCompletionOption::KIND_SIGNAL);
  932. }
  933. if (ignored) {
  934. return;
  935. }
  936. if (p_force) {
  937. emit_signal("request_code_completion");
  938. return;
  939. }
  940. String line = get_line(cursor_get_line());
  941. int ofs = CLAMP(cursor_get_column(), 0, line.length());
  942. 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])))) {
  943. emit_signal("request_code_completion");
  944. } else if (ofs > 1 && line[ofs - 1] == ' ' && code_completion_prefixes.has(String::chr(line[ofs - 2]))) {
  945. emit_signal("request_code_completion");
  946. }
  947. }
  948. 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) {
  949. ScriptCodeCompletionOption completion_option;
  950. completion_option.kind = (ScriptCodeCompletionOption::Kind)p_type;
  951. completion_option.display = p_display_text;
  952. completion_option.insert_text = p_insert_text;
  953. completion_option.font_color = p_text_color;
  954. completion_option.icon = p_icon;
  955. completion_option.default_value = p_value;
  956. code_completion_option_submitted.push_back(completion_option);
  957. }
  958. void CodeEdit::update_code_completion_options(bool p_forced) {
  959. code_completion_forced = p_forced;
  960. code_completion_option_sources = code_completion_option_submitted;
  961. code_completion_option_submitted.clear();
  962. _filter_code_completion_candidates();
  963. }
  964. TypedArray<Dictionary> CodeEdit::get_code_completion_options() const {
  965. if (!code_completion_active) {
  966. return TypedArray<Dictionary>();
  967. }
  968. TypedArray<Dictionary> completion_options;
  969. completion_options.resize(code_completion_options.size());
  970. for (int i = 0; i < code_completion_options.size(); i++) {
  971. Dictionary option;
  972. option["kind"] = code_completion_options[i].kind;
  973. option["display_text"] = code_completion_options[i].display;
  974. option["insert_text"] = code_completion_options[i].insert_text;
  975. option["font_color"] = code_completion_options[i].font_color;
  976. option["icon"] = code_completion_options[i].icon;
  977. option["default_value"] = code_completion_options[i].default_value;
  978. completion_options[i] = option;
  979. }
  980. return completion_options;
  981. }
  982. Dictionary CodeEdit::get_code_completion_option(int p_index) const {
  983. if (!code_completion_active) {
  984. return Dictionary();
  985. }
  986. ERR_FAIL_INDEX_V(p_index, code_completion_options.size(), Dictionary());
  987. Dictionary option;
  988. option["kind"] = code_completion_options[p_index].kind;
  989. option["display_text"] = code_completion_options[p_index].display;
  990. option["insert_text"] = code_completion_options[p_index].insert_text;
  991. option["font_color"] = code_completion_options[p_index].font_color;
  992. option["icon"] = code_completion_options[p_index].icon;
  993. option["default_value"] = code_completion_options[p_index].default_value;
  994. return option;
  995. }
  996. int CodeEdit::get_code_completion_selected_index() const {
  997. return (code_completion_active) ? code_completion_current_selected : -1;
  998. }
  999. void CodeEdit::set_code_completion_selected_index(int p_index) {
  1000. if (!code_completion_active) {
  1001. return;
  1002. }
  1003. ERR_FAIL_INDEX(p_index, code_completion_options.size());
  1004. code_completion_current_selected = p_index;
  1005. update();
  1006. }
  1007. void CodeEdit::confirm_code_completion(bool p_replace) {
  1008. if (is_readonly() || !code_completion_active) {
  1009. return;
  1010. }
  1011. ScriptInstance *si = get_script_instance();
  1012. if (si && si->has_method("_confirm_code_completion")) {
  1013. si->call("_confirm_code_completion", p_replace);
  1014. return;
  1015. }
  1016. begin_complex_operation();
  1017. int caret_line = cursor_get_line();
  1018. const String &insert_text = code_completion_options[code_completion_current_selected].insert_text;
  1019. const String &display_text = code_completion_options[code_completion_current_selected].display;
  1020. if (p_replace) {
  1021. /* Find end of current section */
  1022. const String line = get_line(caret_line);
  1023. int caret_col = cursor_get_column();
  1024. int caret_remove_line = caret_line;
  1025. bool merge_text = true;
  1026. int in_string = is_in_string(caret_line, caret_col);
  1027. if (in_string != -1) {
  1028. Point2 string_end = get_delimiter_end_position(caret_line, caret_col);
  1029. if (string_end.x != -1) {
  1030. merge_text = false;
  1031. caret_remove_line = string_end.y;
  1032. caret_col = string_end.x - 1;
  1033. }
  1034. }
  1035. if (merge_text) {
  1036. for (; caret_col < line.length(); caret_col++) {
  1037. if (!_is_char(line[caret_col])) {
  1038. break;
  1039. }
  1040. }
  1041. }
  1042. /* Replace. */
  1043. _remove_text(caret_line, cursor_get_column() - code_completion_base.length(), caret_remove_line, caret_col);
  1044. cursor_set_column(cursor_get_column() - code_completion_base.length(), false);
  1045. insert_text_at_cursor(insert_text);
  1046. } else {
  1047. /* Get first non-matching char. */
  1048. const String line = get_line(caret_line);
  1049. int caret_col = cursor_get_column();
  1050. int matching_chars = code_completion_base.length();
  1051. for (; matching_chars <= insert_text.length(); matching_chars++) {
  1052. if (caret_col >= line.length() || line[caret_col] != insert_text[matching_chars]) {
  1053. break;
  1054. }
  1055. caret_col++;
  1056. }
  1057. /* Remove base completion text. */
  1058. _remove_text(caret_line, cursor_get_column() - code_completion_base.length(), caret_line, cursor_get_column());
  1059. cursor_set_column(cursor_get_column() - code_completion_base.length(), false);
  1060. /* Merge with text. */
  1061. insert_text_at_cursor(insert_text.substr(0, code_completion_base.length()));
  1062. cursor_set_column(caret_col, false);
  1063. insert_text_at_cursor(insert_text.substr(matching_chars));
  1064. }
  1065. /* TODO: merge with autobrace completion, when in CodeEdit. */
  1066. /* Handle merging of symbols eg strings, brackets. */
  1067. const String line = get_line(caret_line);
  1068. char32_t next_char = line[cursor_get_column()];
  1069. char32_t last_completion_char = insert_text[insert_text.length() - 1];
  1070. char32_t last_completion_char_display = display_text[display_text.length() - 1];
  1071. if ((last_completion_char == '"' || last_completion_char == '\'') && (last_completion_char == next_char || last_completion_char_display == next_char)) {
  1072. _remove_text(caret_line, cursor_get_column(), caret_line, cursor_get_column() + 1);
  1073. }
  1074. if (last_completion_char == '(') {
  1075. if (next_char == last_completion_char) {
  1076. _remove_text(caret_line, cursor_get_column() - 1, caret_line, cursor_get_column());
  1077. } else if (auto_brace_completion_enabled) {
  1078. insert_text_at_cursor(")");
  1079. cursor_set_column(cursor_get_column() - 1);
  1080. }
  1081. } else if (last_completion_char == ')' && next_char == '(') {
  1082. _remove_text(caret_line, cursor_get_column() - 2, caret_line, cursor_get_column());
  1083. if (line[cursor_get_column() + 1] != ')') {
  1084. cursor_set_column(cursor_get_column() - 1);
  1085. }
  1086. }
  1087. end_complex_operation();
  1088. cancel_code_completion();
  1089. if (last_completion_char == '(') {
  1090. request_code_completion();
  1091. }
  1092. }
  1093. void CodeEdit::cancel_code_completion() {
  1094. if (!code_completion_active) {
  1095. return;
  1096. }
  1097. code_completion_forced = false;
  1098. code_completion_active = false;
  1099. update();
  1100. }
  1101. void CodeEdit::_bind_methods() {
  1102. /* Main Gutter */
  1103. ClassDB::bind_method(D_METHOD("_main_gutter_draw_callback"), &CodeEdit::_main_gutter_draw_callback);
  1104. ClassDB::bind_method(D_METHOD("set_draw_breakpoints_gutter", "enable"), &CodeEdit::set_draw_breakpoints_gutter);
  1105. ClassDB::bind_method(D_METHOD("is_drawing_breakpoints_gutter"), &CodeEdit::is_drawing_breakpoints_gutter);
  1106. ClassDB::bind_method(D_METHOD("set_draw_bookmarks_gutter", "enable"), &CodeEdit::set_draw_bookmarks_gutter);
  1107. ClassDB::bind_method(D_METHOD("is_drawing_bookmarks_gutter"), &CodeEdit::is_drawing_bookmarks_gutter);
  1108. ClassDB::bind_method(D_METHOD("set_draw_executing_lines_gutter", "enable"), &CodeEdit::set_draw_executing_lines_gutter);
  1109. ClassDB::bind_method(D_METHOD("is_drawing_executing_lines_gutter"), &CodeEdit::is_drawing_executing_lines_gutter);
  1110. // Breakpoints
  1111. ClassDB::bind_method(D_METHOD("set_line_as_breakpoint", "line", "breakpointed"), &CodeEdit::set_line_as_breakpoint);
  1112. ClassDB::bind_method(D_METHOD("is_line_breakpointed", "line"), &CodeEdit::is_line_breakpointed);
  1113. ClassDB::bind_method(D_METHOD("clear_breakpointed_lines"), &CodeEdit::clear_breakpointed_lines);
  1114. ClassDB::bind_method(D_METHOD("get_breakpointed_lines"), &CodeEdit::get_breakpointed_lines);
  1115. // Bookmarks
  1116. ClassDB::bind_method(D_METHOD("set_line_as_bookmarked", "line", "bookmarked"), &CodeEdit::set_line_as_bookmarked);
  1117. ClassDB::bind_method(D_METHOD("is_line_bookmarked", "line"), &CodeEdit::is_line_bookmarked);
  1118. ClassDB::bind_method(D_METHOD("clear_bookmarked_lines"), &CodeEdit::clear_bookmarked_lines);
  1119. ClassDB::bind_method(D_METHOD("get_bookmarked_lines"), &CodeEdit::get_bookmarked_lines);
  1120. // executing lines
  1121. ClassDB::bind_method(D_METHOD("set_line_as_executing", "line", "executing"), &CodeEdit::set_line_as_executing);
  1122. ClassDB::bind_method(D_METHOD("is_line_executing", "line"), &CodeEdit::is_line_executing);
  1123. ClassDB::bind_method(D_METHOD("clear_executing_lines"), &CodeEdit::clear_executing_lines);
  1124. ClassDB::bind_method(D_METHOD("get_executing_lines"), &CodeEdit::get_executing_lines);
  1125. /* Line numbers */
  1126. ClassDB::bind_method(D_METHOD("_line_number_draw_callback"), &CodeEdit::_line_number_draw_callback);
  1127. ClassDB::bind_method(D_METHOD("set_draw_line_numbers", "enable"), &CodeEdit::set_draw_line_numbers);
  1128. ClassDB::bind_method(D_METHOD("is_draw_line_numbers_enabled"), &CodeEdit::is_draw_line_numbers_enabled);
  1129. ClassDB::bind_method(D_METHOD("set_line_numbers_zero_padded", "enable"), &CodeEdit::set_line_numbers_zero_padded);
  1130. ClassDB::bind_method(D_METHOD("is_line_numbers_zero_padded"), &CodeEdit::is_line_numbers_zero_padded);
  1131. /* Fold Gutter */
  1132. ClassDB::bind_method(D_METHOD("_fold_gutter_draw_callback"), &CodeEdit::_fold_gutter_draw_callback);
  1133. ClassDB::bind_method(D_METHOD("set_draw_fold_gutter", "enable"), &CodeEdit::set_draw_fold_gutter);
  1134. ClassDB::bind_method(D_METHOD("is_drawing_fold_gutter"), &CodeEdit::is_drawing_fold_gutter);
  1135. /* Line folding */
  1136. ClassDB::bind_method(D_METHOD("set_line_folding_enabled", "enabled"), &CodeEdit::set_line_folding_enabled);
  1137. ClassDB::bind_method(D_METHOD("is_line_folding_enabled"), &CodeEdit::is_line_folding_enabled);
  1138. ClassDB::bind_method(D_METHOD("can_fold_line", "line"), &CodeEdit::can_fold_line);
  1139. ClassDB::bind_method(D_METHOD("fold_line", "line"), &CodeEdit::fold_line);
  1140. ClassDB::bind_method(D_METHOD("unfold_line", "line"), &CodeEdit::unfold_line);
  1141. ClassDB::bind_method(D_METHOD("fold_all_lines"), &CodeEdit::fold_all_lines);
  1142. ClassDB::bind_method(D_METHOD("unfold_all_lines"), &CodeEdit::unfold_all_lines);
  1143. ClassDB::bind_method(D_METHOD("toggle_foldable_line", "line"), &CodeEdit::toggle_foldable_line);
  1144. ClassDB::bind_method(D_METHOD("is_line_folded", "line"), &CodeEdit::is_line_folded);
  1145. ClassDB::bind_method(D_METHOD("get_folded_lines"), &CodeEdit::get_folded_lines);
  1146. /* Delimiters */
  1147. // Strings
  1148. ClassDB::bind_method(D_METHOD("add_string_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_string_delimiter, DEFVAL(false));
  1149. ClassDB::bind_method(D_METHOD("remove_string_delimiter", "start_key"), &CodeEdit::remove_string_delimiter);
  1150. ClassDB::bind_method(D_METHOD("has_string_delimiter", "start_key"), &CodeEdit::has_string_delimiter);
  1151. ClassDB::bind_method(D_METHOD("set_string_delimiters", "string_delimiters"), &CodeEdit::set_string_delimiters);
  1152. ClassDB::bind_method(D_METHOD("clear_string_delimiters"), &CodeEdit::clear_string_delimiters);
  1153. ClassDB::bind_method(D_METHOD("get_string_delimiters"), &CodeEdit::get_string_delimiters);
  1154. ClassDB::bind_method(D_METHOD("is_in_string", "line", "column"), &CodeEdit::is_in_string, DEFVAL(-1));
  1155. // Comments
  1156. ClassDB::bind_method(D_METHOD("add_comment_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_comment_delimiter, DEFVAL(false));
  1157. ClassDB::bind_method(D_METHOD("remove_comment_delimiter", "start_key"), &CodeEdit::remove_comment_delimiter);
  1158. ClassDB::bind_method(D_METHOD("has_comment_delimiter", "start_key"), &CodeEdit::has_comment_delimiter);
  1159. ClassDB::bind_method(D_METHOD("set_comment_delimiters", "comment_delimiters"), &CodeEdit::set_comment_delimiters);
  1160. ClassDB::bind_method(D_METHOD("clear_comment_delimiters"), &CodeEdit::clear_comment_delimiters);
  1161. ClassDB::bind_method(D_METHOD("get_comment_delimiters"), &CodeEdit::get_comment_delimiters);
  1162. ClassDB::bind_method(D_METHOD("is_in_comment", "line", "column"), &CodeEdit::is_in_comment, DEFVAL(-1));
  1163. // Util
  1164. ClassDB::bind_method(D_METHOD("get_delimiter_start_key", "delimiter_index"), &CodeEdit::get_delimiter_start_key);
  1165. ClassDB::bind_method(D_METHOD("get_delimiter_end_key", "delimiter_index"), &CodeEdit::get_delimiter_end_key);
  1166. ClassDB::bind_method(D_METHOD("get_delimiter_start_position", "line", "column"), &CodeEdit::get_delimiter_start_position);
  1167. ClassDB::bind_method(D_METHOD("get_delimiter_end_position", "line", "column"), &CodeEdit::get_delimiter_end_position);
  1168. /* Code hint */
  1169. ClassDB::bind_method(D_METHOD("set_code_hint", "code_hint"), &CodeEdit::set_code_hint);
  1170. ClassDB::bind_method(D_METHOD("set_code_hint_draw_below", "draw_below"), &CodeEdit::set_code_hint_draw_below);
  1171. /* Code Completion */
  1172. BIND_ENUM_CONSTANT(KIND_CLASS);
  1173. BIND_ENUM_CONSTANT(KIND_FUNCTION);
  1174. BIND_ENUM_CONSTANT(KIND_SIGNAL);
  1175. BIND_ENUM_CONSTANT(KIND_VARIABLE);
  1176. BIND_ENUM_CONSTANT(KIND_MEMBER);
  1177. BIND_ENUM_CONSTANT(KIND_ENUM);
  1178. BIND_ENUM_CONSTANT(KIND_CONSTANT);
  1179. BIND_ENUM_CONSTANT(KIND_NODE_PATH);
  1180. BIND_ENUM_CONSTANT(KIND_FILE_PATH);
  1181. BIND_ENUM_CONSTANT(KIND_PLAIN_TEXT);
  1182. ClassDB::bind_method(D_METHOD("get_text_for_code_completion"), &CodeEdit::get_text_for_code_completion);
  1183. ClassDB::bind_method(D_METHOD("request_code_completion", "force"), &CodeEdit::request_code_completion, DEFVAL(false));
  1184. 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));
  1185. ClassDB::bind_method(D_METHOD("update_code_completion_options", "force"), &CodeEdit::update_code_completion_options);
  1186. ClassDB::bind_method(D_METHOD("get_code_completion_options"), &CodeEdit::get_code_completion_options);
  1187. ClassDB::bind_method(D_METHOD("get_code_completion_option", "index"), &CodeEdit::get_code_completion_option);
  1188. ClassDB::bind_method(D_METHOD("get_code_completion_selected_index"), &CodeEdit::get_code_completion_selected_index);
  1189. ClassDB::bind_method(D_METHOD("set_code_completion_selected_index", "index"), &CodeEdit::set_code_completion_selected_index);
  1190. ClassDB::bind_method(D_METHOD("confirm_code_completion", "replace"), &CodeEdit::confirm_code_completion, DEFVAL(false));
  1191. ClassDB::bind_method(D_METHOD("cancel_code_completion"), &CodeEdit::cancel_code_completion);
  1192. ClassDB::bind_method(D_METHOD("set_code_completion_enabled", "enable"), &CodeEdit::set_code_completion_enabled);
  1193. ClassDB::bind_method(D_METHOD("is_code_completion_enabled"), &CodeEdit::is_code_completion_enabled);
  1194. ClassDB::bind_method(D_METHOD("set_code_completion_prefixes", "prefixes"), &CodeEdit::set_code_completion_prefixes);
  1195. ClassDB::bind_method(D_METHOD("get_code_comletion_prefixes"), &CodeEdit::get_code_completion_prefixes);
  1196. // Overridable
  1197. BIND_VMETHOD(MethodInfo("_confirm_code_completion", PropertyInfo(Variant::BOOL, "replace")));
  1198. BIND_VMETHOD(MethodInfo("_request_code_completion", PropertyInfo(Variant::BOOL, "force")));
  1199. BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_filter_code_completion_candidates", PropertyInfo(Variant::ARRAY, "candidates")));
  1200. /* Inspector */
  1201. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_breakpoints_gutter"), "set_draw_breakpoints_gutter", "is_drawing_breakpoints_gutter");
  1202. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_bookmarks"), "set_draw_bookmarks_gutter", "is_drawing_bookmarks_gutter");
  1203. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_executing_lines"), "set_draw_executing_lines_gutter", "is_drawing_executing_lines_gutter");
  1204. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_line_numbers"), "set_draw_line_numbers", "is_draw_line_numbers_enabled");
  1205. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "zero_pad_line_numbers"), "set_line_numbers_zero_padded", "is_line_numbers_zero_padded");
  1206. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_fold_gutter"), "set_draw_fold_gutter", "is_drawing_fold_gutter");
  1207. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "line_folding"), "set_line_folding_enabled", "is_line_folding_enabled");
  1208. ADD_GROUP("Delimiters", "delimiter_");
  1209. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_strings"), "set_string_delimiters", "get_string_delimiters");
  1210. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_comments"), "set_comment_delimiters", "get_comment_delimiters");
  1211. ADD_GROUP("Code Completion", "code_completion_");
  1212. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "code_completion_enabled"), "set_code_completion_enabled", "is_code_completion_enabled");
  1213. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "code_completion_prefixes"), "set_code_completion_prefixes", "get_code_comletion_prefixes");
  1214. /* Signals */
  1215. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "line")));
  1216. ADD_SIGNAL(MethodInfo("request_code_completion"));
  1217. }
  1218. void CodeEdit::_gutter_clicked(int p_line, int p_gutter) {
  1219. if (p_gutter == main_gutter) {
  1220. if (draw_breakpoints) {
  1221. set_line_as_breakpoint(p_line, !is_line_breakpointed(p_line));
  1222. }
  1223. return;
  1224. }
  1225. if (p_gutter == line_number_gutter) {
  1226. set_selection_mode(TextEdit::SelectionMode::SELECTION_MODE_LINE, p_line, 0);
  1227. select(p_line, 0, p_line + 1, 0);
  1228. cursor_set_line(p_line + 1);
  1229. cursor_set_column(0);
  1230. return;
  1231. }
  1232. if (p_gutter == fold_gutter) {
  1233. if (is_line_folded(p_line)) {
  1234. unfold_line(p_line);
  1235. } else if (can_fold_line(p_line)) {
  1236. fold_line(p_line);
  1237. }
  1238. return;
  1239. }
  1240. }
  1241. void CodeEdit::_update_gutter_indexes() {
  1242. for (int i = 0; i < get_gutter_count(); i++) {
  1243. if (get_gutter_name(i) == "main_gutter") {
  1244. main_gutter = i;
  1245. continue;
  1246. }
  1247. if (get_gutter_name(i) == "line_numbers") {
  1248. line_number_gutter = i;
  1249. continue;
  1250. }
  1251. if (get_gutter_name(i) == "fold_gutter") {
  1252. fold_gutter = i;
  1253. continue;
  1254. }
  1255. }
  1256. }
  1257. /* Delimiters */
  1258. void CodeEdit::_update_delimiter_cache(int p_from_line, int p_to_line) {
  1259. if (delimiters.size() == 0) {
  1260. return;
  1261. }
  1262. int line_count = get_line_count();
  1263. if (p_to_line == -1) {
  1264. p_to_line = line_count;
  1265. }
  1266. int start_line = MIN(p_from_line, p_to_line);
  1267. int end_line = MAX(p_from_line, p_to_line);
  1268. /* Make sure delimiter_cache has all the lines. */
  1269. if (start_line != end_line) {
  1270. if (p_to_line < p_from_line) {
  1271. for (int i = end_line; i > start_line; i--) {
  1272. delimiter_cache.remove(i);
  1273. }
  1274. } else {
  1275. for (int i = start_line; i < end_line; i++) {
  1276. delimiter_cache.insert(i, Map<int, int>());
  1277. }
  1278. }
  1279. }
  1280. int in_region = -1;
  1281. for (int i = start_line; i < MIN(end_line + 1, line_count); i++) {
  1282. int current_end_region = (i <= 0 || delimiter_cache[i].size() < 1) ? -1 : delimiter_cache[i].back()->value();
  1283. in_region = (i <= 0 || delimiter_cache[i - 1].size() < 1) ? -1 : delimiter_cache[i - 1].back()->value();
  1284. const String &str = get_line(i);
  1285. const int line_length = str.length();
  1286. delimiter_cache.write[i].clear();
  1287. if (str.length() == 0) {
  1288. if (in_region != -1) {
  1289. delimiter_cache.write[i][0] = in_region;
  1290. }
  1291. if (i == end_line && current_end_region != in_region) {
  1292. end_line++;
  1293. end_line = MIN(end_line, line_count);
  1294. }
  1295. continue;
  1296. }
  1297. int end_region = -1;
  1298. for (int j = 0; j < line_length; j++) {
  1299. int from = j;
  1300. for (; from < line_length; from++) {
  1301. if (str[from] == '\\') {
  1302. from++;
  1303. continue;
  1304. }
  1305. break;
  1306. }
  1307. /* check if we are in entering a region */
  1308. bool same_line = false;
  1309. if (in_region == -1) {
  1310. for (int d = 0; d < delimiters.size(); d++) {
  1311. /* check there is enough room */
  1312. int chars_left = line_length - from;
  1313. int start_key_length = delimiters[d].start_key.length();
  1314. int end_key_length = delimiters[d].end_key.length();
  1315. if (chars_left < start_key_length) {
  1316. continue;
  1317. }
  1318. /* search the line */
  1319. bool match = true;
  1320. const char32_t *start_key = delimiters[d].start_key.get_data();
  1321. for (int k = 0; k < start_key_length; k++) {
  1322. if (start_key[k] != str[from + k]) {
  1323. match = false;
  1324. break;
  1325. }
  1326. }
  1327. if (!match) {
  1328. continue;
  1329. }
  1330. same_line = true;
  1331. in_region = d;
  1332. delimiter_cache.write[i][from + 1] = d;
  1333. from += start_key_length;
  1334. /* check if it's the whole line */
  1335. if (end_key_length == 0 || delimiters[d].line_only || from + end_key_length > line_length) {
  1336. j = line_length;
  1337. if (delimiters[d].line_only) {
  1338. delimiter_cache.write[i][line_length + 1] = -1;
  1339. } else {
  1340. end_region = in_region;
  1341. }
  1342. }
  1343. break;
  1344. }
  1345. if (j == line_length || in_region == -1) {
  1346. continue;
  1347. }
  1348. }
  1349. /* if we are in one find the end key */
  1350. /* search the line */
  1351. int region_end_index = -1;
  1352. int end_key_length = delimiters[in_region].end_key.length();
  1353. const char32_t *end_key = delimiters[in_region].end_key.get_data();
  1354. for (; from < line_length; from++) {
  1355. if (line_length - from < end_key_length) {
  1356. break;
  1357. }
  1358. if (!is_symbol(str[from])) {
  1359. continue;
  1360. }
  1361. if (str[from] == '\\') {
  1362. from++;
  1363. continue;
  1364. }
  1365. region_end_index = from;
  1366. for (int k = 0; k < end_key_length; k++) {
  1367. if (end_key[k] != str[from + k]) {
  1368. region_end_index = -1;
  1369. break;
  1370. }
  1371. }
  1372. if (region_end_index != -1) {
  1373. break;
  1374. }
  1375. }
  1376. j = from + (end_key_length - 1);
  1377. end_region = (region_end_index == -1) ? in_region : -1;
  1378. if (!same_line || region_end_index != -1) {
  1379. delimiter_cache.write[i][j + 1] = end_region;
  1380. }
  1381. in_region = -1;
  1382. }
  1383. if (i == end_line && current_end_region != end_region) {
  1384. end_line++;
  1385. end_line = MIN(end_line, line_count);
  1386. }
  1387. }
  1388. }
  1389. int CodeEdit::_is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const {
  1390. if (delimiters.size() == 0) {
  1391. return -1;
  1392. }
  1393. ERR_FAIL_INDEX_V(p_line, get_line_count(), 0);
  1394. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  1395. bool in_region = region != -1 && delimiters[region].type == p_type;
  1396. for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  1397. /* If column is specified, loop untill the key is larger then the column. */
  1398. if (p_column != -1) {
  1399. if (E->key() > p_column) {
  1400. break;
  1401. }
  1402. in_region = E->value() != -1 && delimiters[E->value()].type == p_type;
  1403. region = in_region ? E->value() : -1;
  1404. continue;
  1405. }
  1406. /* If no column, calulate if the entire line is a region */
  1407. /* excluding whitespace. */
  1408. const String line = get_line(p_line);
  1409. if (!in_region) {
  1410. if (E->value() == -1 || delimiters[E->value()].type != p_type) {
  1411. break;
  1412. }
  1413. region = E->value();
  1414. in_region = true;
  1415. for (int i = E->key() - 2; i >= 0; i--) {
  1416. if (!_is_whitespace(line[i])) {
  1417. return -1;
  1418. }
  1419. }
  1420. }
  1421. if (delimiters[region].line_only) {
  1422. return region;
  1423. }
  1424. int end_col = E->key();
  1425. if (E->value() != -1) {
  1426. if (!E->next()) {
  1427. return region;
  1428. }
  1429. end_col = E->next()->key();
  1430. }
  1431. for (int i = end_col; i < line.length(); i++) {
  1432. if (!_is_whitespace(line[i])) {
  1433. return -1;
  1434. }
  1435. }
  1436. return region;
  1437. }
  1438. return in_region ? region : -1;
  1439. }
  1440. void CodeEdit::_add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type) {
  1441. if (p_start_key.length() > 0) {
  1442. for (int i = 0; i < p_start_key.length(); i++) {
  1443. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "delimiter must start with a symbol");
  1444. }
  1445. }
  1446. if (p_end_key.length() > 0) {
  1447. for (int i = 0; i < p_end_key.length(); i++) {
  1448. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "delimiter must end with a symbol");
  1449. }
  1450. }
  1451. int at = 0;
  1452. for (int i = 0; i < delimiters.size(); i++) {
  1453. ERR_FAIL_COND_MSG(delimiters[i].start_key == p_start_key, "delimiter with start key '" + p_start_key + "' already exists.");
  1454. if (p_start_key.length() < delimiters[i].start_key.length()) {
  1455. at++;
  1456. }
  1457. }
  1458. Delimiter delimiter;
  1459. delimiter.type = p_type;
  1460. delimiter.start_key = p_start_key;
  1461. delimiter.end_key = p_end_key;
  1462. delimiter.line_only = p_line_only || p_end_key == "";
  1463. delimiters.insert(at, delimiter);
  1464. if (!setting_delimiters) {
  1465. delimiter_cache.clear();
  1466. _update_delimiter_cache();
  1467. }
  1468. }
  1469. void CodeEdit::_remove_delimiter(const String &p_start_key, DelimiterType p_type) {
  1470. for (int i = 0; i < delimiters.size(); i++) {
  1471. if (delimiters[i].start_key != p_start_key) {
  1472. continue;
  1473. }
  1474. if (delimiters[i].type != p_type) {
  1475. break;
  1476. }
  1477. delimiters.remove(i);
  1478. if (!setting_delimiters) {
  1479. delimiter_cache.clear();
  1480. _update_delimiter_cache();
  1481. }
  1482. break;
  1483. }
  1484. }
  1485. bool CodeEdit::_has_delimiter(const String &p_start_key, DelimiterType p_type) const {
  1486. for (int i = 0; i < delimiters.size(); i++) {
  1487. if (delimiters[i].start_key == p_start_key) {
  1488. return delimiters[i].type == p_type;
  1489. }
  1490. }
  1491. return false;
  1492. }
  1493. void CodeEdit::_set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type) {
  1494. setting_delimiters = true;
  1495. _clear_delimiters(p_type);
  1496. for (int i = 0; i < p_delimiters.size(); i++) {
  1497. String key = p_delimiters[i].is_null() ? "" : p_delimiters[i];
  1498. const String start_key = key.get_slice(" ", 0);
  1499. const String end_key = key.get_slice_count(" ") > 1 ? key.get_slice(" ", 1) : String();
  1500. _add_delimiter(start_key, end_key, end_key == "", p_type);
  1501. }
  1502. setting_delimiters = false;
  1503. _update_delimiter_cache();
  1504. }
  1505. void CodeEdit::_clear_delimiters(DelimiterType p_type) {
  1506. for (int i = delimiters.size() - 1; i >= 0; i--) {
  1507. if (delimiters[i].type == p_type) {
  1508. delimiters.remove(i);
  1509. }
  1510. }
  1511. delimiter_cache.clear();
  1512. if (!setting_delimiters) {
  1513. _update_delimiter_cache();
  1514. }
  1515. }
  1516. TypedArray<String> CodeEdit::_get_delimiters(DelimiterType p_type) const {
  1517. TypedArray<String> r_delimiters;
  1518. for (int i = 0; i < delimiters.size(); i++) {
  1519. if (delimiters[i].type != p_type) {
  1520. continue;
  1521. }
  1522. r_delimiters.push_back(delimiters[i].start_key + (delimiters[i].end_key.is_empty() ? "" : " " + delimiters[i].end_key));
  1523. }
  1524. return r_delimiters;
  1525. }
  1526. /* Code Completion */
  1527. void CodeEdit::_filter_code_completion_candidates() {
  1528. ScriptInstance *si = get_script_instance();
  1529. if (si && si->has_method("_filter_code_completion_candidates")) {
  1530. code_completion_options.clear();
  1531. code_completion_base = "";
  1532. /* Build options argument. */
  1533. TypedArray<Dictionary> completion_options_sources;
  1534. completion_options_sources.resize(code_completion_option_sources.size());
  1535. int i = 0;
  1536. for (List<ScriptCodeCompletionOption>::Element *E = code_completion_option_sources.front(); E; E = E->next()) {
  1537. Dictionary option;
  1538. option["kind"] = E->get().kind;
  1539. option["display_text"] = E->get().display;
  1540. option["insert_text"] = E->get().insert_text;
  1541. option["font_color"] = E->get().font_color;
  1542. option["icon"] = E->get().icon;
  1543. option["default_value"] = E->get().default_value;
  1544. completion_options_sources[i] = option;
  1545. i++;
  1546. }
  1547. TypedArray<Dictionary> completion_options = si->call("_filter_code_completion_candidates", completion_options_sources);
  1548. /* No options to complete, cancel. */
  1549. if (completion_options.size() == 0) {
  1550. cancel_code_completion();
  1551. return;
  1552. }
  1553. /* Convert back into options. */
  1554. int max_width = 0;
  1555. for (i = 0; i < completion_options.size(); i++) {
  1556. ScriptCodeCompletionOption option;
  1557. option.kind = (ScriptCodeCompletionOption::Kind)(int)completion_options[i].get("kind");
  1558. option.display = completion_options[i].get("display_text");
  1559. option.insert_text = completion_options[i].get("insert_text");
  1560. option.font_color = completion_options[i].get("font_color");
  1561. option.icon = completion_options[i].get("icon");
  1562. option.default_value = completion_options[i].get("default_value");
  1563. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  1564. code_completion_options.push_back(option);
  1565. }
  1566. code_completion_longest_line = MIN(max_width, code_completion_max_width);
  1567. code_completion_current_selected = 0;
  1568. code_completion_active = true;
  1569. update();
  1570. return;
  1571. }
  1572. const int caret_line = cursor_get_line();
  1573. const int caret_column = cursor_get_column();
  1574. const String line = get_line(caret_line);
  1575. if (caret_column > 0 && line[caret_column - 1] == '(' && !code_completion_forced) {
  1576. cancel_code_completion();
  1577. return;
  1578. }
  1579. /* Get string status, are we in one or at the close. */
  1580. int in_string = is_in_string(caret_line, caret_column);
  1581. int first_quote_col = -1;
  1582. if (in_string != -1) {
  1583. Point2 string_start_pos = get_delimiter_start_position(caret_line, caret_column);
  1584. first_quote_col = (string_start_pos.y == caret_line) ? string_start_pos.x : -1;
  1585. } else if (caret_column > 0) {
  1586. if (is_in_string(caret_line, caret_column - 1) != -1) {
  1587. first_quote_col = caret_column - 1;
  1588. }
  1589. }
  1590. int cofs = caret_column;
  1591. String string_to_complete;
  1592. bool prev_is_word = false;
  1593. /* Cancel if we are at the close of a string. */
  1594. if (in_string == -1 && first_quote_col == cofs - 1) {
  1595. cancel_code_completion();
  1596. return;
  1597. /* In a string, therefore we are trying to complete the string text. */
  1598. } else if (in_string != -1 && first_quote_col != -1) {
  1599. int key_length = delimiters[in_string].start_key.length();
  1600. string_to_complete = line.substr(first_quote_col - key_length, (cofs - first_quote_col) + key_length);
  1601. /* If we have a space, previous word might be a keyword. eg "func |". */
  1602. } else if (cofs > 0 && line[cofs - 1] == ' ') {
  1603. int ofs = cofs - 1;
  1604. while (ofs >= 0 && line[ofs] == ' ') {
  1605. ofs--;
  1606. }
  1607. prev_is_word = _is_char(line[ofs]);
  1608. /* Otherwise get current word and set cofs to the start. */
  1609. } else {
  1610. int start_cofs = cofs;
  1611. while (cofs > 0 && line[cofs - 1] > 32 && (line[cofs - 1] == '/' || _is_char(line[cofs - 1]))) {
  1612. cofs--;
  1613. }
  1614. string_to_complete = line.substr(cofs, start_cofs - cofs);
  1615. }
  1616. /* If all else fails, check for a prefix. */
  1617. /* Single space between caret and prefix is okay. */
  1618. bool prev_is_prefix = false;
  1619. if (cofs > 0 && code_completion_prefixes.has(String::chr(line[cofs - 1]))) {
  1620. prev_is_prefix = true;
  1621. } else if (cofs > 1 && line[cofs - 1] == ' ' && code_completion_prefixes.has(String::chr(line[cofs - 2]))) {
  1622. prev_is_prefix = true;
  1623. }
  1624. if (!prev_is_word && string_to_complete.is_empty() && (cofs == 0 || !prev_is_prefix)) {
  1625. cancel_code_completion();
  1626. return;
  1627. }
  1628. /* Filter Options. */
  1629. /* For now handle only tradional quoted strings. */
  1630. bool single_quote = in_string != -1 && first_quote_col > 0 && delimiters[in_string].start_key == "'";
  1631. code_completion_options.clear();
  1632. code_completion_base = string_to_complete;
  1633. Vector<ScriptCodeCompletionOption> completion_options_casei;
  1634. Vector<ScriptCodeCompletionOption> completion_options_subseq;
  1635. Vector<ScriptCodeCompletionOption> completion_options_subseq_casei;
  1636. int max_width = 0;
  1637. String string_to_complete_lower = string_to_complete.to_lower();
  1638. for (List<ScriptCodeCompletionOption>::Element *E = code_completion_option_sources.front(); E; E = E->next()) {
  1639. ScriptCodeCompletionOption &option = E->get();
  1640. if (single_quote && option.display.is_quoted()) {
  1641. option.display = option.display.unquote().quote("'");
  1642. }
  1643. if (in_string != -1) {
  1644. String quote = single_quote ? "'" : "\"";
  1645. option.display = option.display.unquote().quote(quote);
  1646. option.insert_text = option.insert_text.unquote().quote(quote);
  1647. }
  1648. if (option.display.length() == 0) {
  1649. continue;
  1650. }
  1651. if (string_to_complete.length() == 0) {
  1652. code_completion_options.push_back(option);
  1653. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  1654. continue;
  1655. }
  1656. /* This code works the same as:
  1657. if (option.display.begins_with(s)) {
  1658. completion_options.push_back(option);
  1659. } else if (option.display.to_lower().begins_with(s.to_lower())) {
  1660. completion_options_casei.push_back(option);
  1661. } else if (s.is_subsequence_of(option.display)) {
  1662. completion_options_subseq.push_back(option);
  1663. } else if (s.is_subsequence_ofi(option.display)) {
  1664. completion_options_subseq_casei.push_back(option);
  1665. }
  1666. But is more performant due to being inlined and looping over the characters only once
  1667. */
  1668. String display_lower = option.display.to_lower();
  1669. const char32_t *ssq = &string_to_complete[0];
  1670. const char32_t *ssq_lower = &string_to_complete_lower[0];
  1671. const char32_t *tgt = &option.display[0];
  1672. const char32_t *tgt_lower = &display_lower[0];
  1673. const char32_t *ssq_last_tgt = nullptr;
  1674. const char32_t *ssq_lower_last_tgt = nullptr;
  1675. for (; *tgt; tgt++, tgt_lower++) {
  1676. if (*ssq == *tgt) {
  1677. ssq++;
  1678. ssq_last_tgt = tgt;
  1679. }
  1680. if (*ssq_lower == *tgt_lower) {
  1681. ssq_lower++;
  1682. ssq_lower_last_tgt = tgt;
  1683. }
  1684. }
  1685. /* Matched the whole subsequence in s. */
  1686. if (!*ssq) {
  1687. /* Finished matching in the first s.length() characters. */
  1688. if (ssq_last_tgt == &option.display[string_to_complete.length() - 1]) {
  1689. code_completion_options.push_back(option);
  1690. } else {
  1691. completion_options_subseq.push_back(option);
  1692. }
  1693. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  1694. /* Matched the whole subsequence in s_lower. */
  1695. } else if (!*ssq_lower) {
  1696. /* Finished matching in the first s.length() characters. */
  1697. if (ssq_lower_last_tgt == &option.display[string_to_complete.length() - 1]) {
  1698. completion_options_casei.push_back(option);
  1699. } else {
  1700. completion_options_subseq_casei.push_back(option);
  1701. }
  1702. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  1703. }
  1704. }
  1705. code_completion_options.append_array(completion_options_casei);
  1706. code_completion_options.append_array(completion_options_subseq);
  1707. code_completion_options.append_array(completion_options_subseq_casei);
  1708. /* No options to complete, cancel. */
  1709. if (code_completion_options.size() == 0) {
  1710. cancel_code_completion();
  1711. return;
  1712. }
  1713. /* A perfect match, stop completion. */
  1714. if (code_completion_options.size() == 1 && string_to_complete == code_completion_options[0].display) {
  1715. cancel_code_completion();
  1716. return;
  1717. }
  1718. code_completion_longest_line = MIN(max_width, code_completion_max_width);
  1719. code_completion_current_selected = 0;
  1720. code_completion_active = true;
  1721. update();
  1722. }
  1723. void CodeEdit::_lines_edited_from(int p_from_line, int p_to_line) {
  1724. _update_delimiter_cache(p_from_line, p_to_line);
  1725. if (p_from_line == p_to_line) {
  1726. return;
  1727. }
  1728. int lc = get_line_count();
  1729. line_number_digits = 1;
  1730. while (lc /= 10) {
  1731. line_number_digits++;
  1732. }
  1733. set_gutter_width(line_number_gutter, (line_number_digits + 1) * cache.font->get_char_size('0', 0, cache.font_size).width);
  1734. int from_line = MIN(p_from_line, p_to_line);
  1735. int line_count = (p_to_line - p_from_line);
  1736. List<int> breakpoints;
  1737. breakpointed_lines.get_key_list(&breakpoints);
  1738. for (const List<int>::Element *E = breakpoints.front(); E; E = E->next()) {
  1739. int line = E->get();
  1740. if (line <= from_line) {
  1741. continue;
  1742. }
  1743. breakpointed_lines.erase(line);
  1744. emit_signal("breakpoint_toggled", line);
  1745. if (line_count > 0 || line >= p_from_line) {
  1746. emit_signal("breakpoint_toggled", line + line_count);
  1747. breakpointed_lines[line + line_count] = true;
  1748. continue;
  1749. }
  1750. }
  1751. }
  1752. CodeEdit::CodeEdit() {
  1753. /* Text Direction */
  1754. set_layout_direction(LAYOUT_DIRECTION_LTR);
  1755. set_text_direction(TEXT_DIRECTION_LTR);
  1756. /* Gutters */
  1757. int gutter_idx = 0;
  1758. /* Main Gutter */
  1759. add_gutter();
  1760. set_gutter_name(gutter_idx, "main_gutter");
  1761. set_gutter_draw(gutter_idx, false);
  1762. set_gutter_overwritable(gutter_idx, true);
  1763. set_gutter_type(gutter_idx, GUTTER_TPYE_CUSTOM);
  1764. set_gutter_custom_draw(gutter_idx, this, "_main_gutter_draw_callback");
  1765. gutter_idx++;
  1766. /* Line numbers */
  1767. add_gutter();
  1768. set_gutter_name(gutter_idx, "line_numbers");
  1769. set_gutter_draw(gutter_idx, false);
  1770. set_gutter_type(gutter_idx, GUTTER_TPYE_CUSTOM);
  1771. set_gutter_custom_draw(gutter_idx, this, "_line_number_draw_callback");
  1772. gutter_idx++;
  1773. /* Fold Gutter */
  1774. add_gutter();
  1775. set_gutter_name(gutter_idx, "fold_gutter");
  1776. set_gutter_draw(gutter_idx, false);
  1777. set_gutter_type(gutter_idx, GUTTER_TPYE_CUSTOM);
  1778. set_gutter_custom_draw(gutter_idx, this, "_fold_gutter_draw_callback");
  1779. gutter_idx++;
  1780. connect("lines_edited_from", callable_mp(this, &CodeEdit::_lines_edited_from));
  1781. connect("gutter_clicked", callable_mp(this, &CodeEdit::_gutter_clicked));
  1782. connect("gutter_added", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  1783. connect("gutter_removed", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  1784. _update_gutter_indexes();
  1785. }
  1786. CodeEdit::~CodeEdit() {
  1787. }