code_edit.cpp 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794
  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::set_highlight_matching_braces_enabled(bool p_enabled) {
  794. highlight_matching_braces_enabled = p_enabled;
  795. update();
  796. }
  797. bool CodeEdit::is_highlight_matching_braces_enabled() const {
  798. return highlight_matching_braces_enabled;
  799. }
  800. void CodeEdit::add_auto_brace_completion_pair(const String &p_open_key, const String &p_close_key) {
  801. ERR_FAIL_COND_MSG(p_open_key.is_empty(), "auto brace completion open key cannot be empty");
  802. ERR_FAIL_COND_MSG(p_close_key.is_empty(), "auto brace completion close key cannot be empty");
  803. for (int i = 0; i < p_open_key.length(); i++) {
  804. ERR_FAIL_COND_MSG(!is_symbol(p_open_key[i]), "auto brace completion open key must be a symbol");
  805. }
  806. for (int i = 0; i < p_close_key.length(); i++) {
  807. ERR_FAIL_COND_MSG(!is_symbol(p_close_key[i]), "auto brace completion close key must be a symbol");
  808. }
  809. int at = 0;
  810. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  811. ERR_FAIL_COND_MSG(auto_brace_completion_pairs[i].open_key == p_open_key, "auto brace completion open key '" + p_open_key + "' already exists.");
  812. if (p_open_key.length() < auto_brace_completion_pairs[i].open_key.length()) {
  813. at++;
  814. }
  815. }
  816. BracePair brace_pair;
  817. brace_pair.open_key = p_open_key;
  818. brace_pair.close_key = p_close_key;
  819. auto_brace_completion_pairs.insert(at, brace_pair);
  820. }
  821. void CodeEdit::set_auto_brace_completion_pairs(const Dictionary &p_auto_brace_completion_pairs) {
  822. auto_brace_completion_pairs.clear();
  823. Array keys = p_auto_brace_completion_pairs.keys();
  824. for (int i = 0; i < keys.size(); i++) {
  825. add_auto_brace_completion_pair(keys[i], p_auto_brace_completion_pairs[keys[i]]);
  826. }
  827. }
  828. Dictionary CodeEdit::get_auto_brace_completion_pairs() const {
  829. Dictionary brace_pairs;
  830. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  831. brace_pairs[auto_brace_completion_pairs[i].open_key] = auto_brace_completion_pairs[i].close_key;
  832. }
  833. return brace_pairs;
  834. }
  835. bool CodeEdit::has_auto_brace_completion_open_key(const String &p_open_key) const {
  836. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  837. if (auto_brace_completion_pairs[i].open_key == p_open_key) {
  838. return true;
  839. }
  840. }
  841. return false;
  842. }
  843. bool CodeEdit::has_auto_brace_completion_close_key(const String &p_close_key) const {
  844. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  845. if (auto_brace_completion_pairs[i].close_key == p_close_key) {
  846. return true;
  847. }
  848. }
  849. return false;
  850. }
  851. String CodeEdit::get_auto_brace_completion_close_key(const String &p_open_key) const {
  852. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  853. if (auto_brace_completion_pairs[i].open_key == p_open_key) {
  854. return auto_brace_completion_pairs[i].close_key;
  855. }
  856. }
  857. return String();
  858. }
  859. /* Main Gutter */
  860. void CodeEdit::_update_draw_main_gutter() {
  861. set_gutter_draw(main_gutter, draw_breakpoints || draw_bookmarks || draw_executing_lines);
  862. }
  863. void CodeEdit::set_draw_breakpoints_gutter(bool p_draw) {
  864. draw_breakpoints = p_draw;
  865. set_gutter_clickable(main_gutter, p_draw);
  866. _update_draw_main_gutter();
  867. }
  868. bool CodeEdit::is_drawing_breakpoints_gutter() const {
  869. return draw_breakpoints;
  870. }
  871. void CodeEdit::set_draw_bookmarks_gutter(bool p_draw) {
  872. draw_bookmarks = p_draw;
  873. _update_draw_main_gutter();
  874. }
  875. bool CodeEdit::is_drawing_bookmarks_gutter() const {
  876. return draw_bookmarks;
  877. }
  878. void CodeEdit::set_draw_executing_lines_gutter(bool p_draw) {
  879. draw_executing_lines = p_draw;
  880. _update_draw_main_gutter();
  881. }
  882. bool CodeEdit::is_drawing_executing_lines_gutter() const {
  883. return draw_executing_lines;
  884. }
  885. void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  886. if (draw_breakpoints && is_line_breakpointed(p_line)) {
  887. int padding = p_region.size.x / 6;
  888. Rect2 breakpoint_region = p_region;
  889. breakpoint_region.position += Point2(padding, padding);
  890. breakpoint_region.size -= Point2(padding, padding) * 2;
  891. breakpoint_icon->draw_rect(get_canvas_item(), breakpoint_region, false, breakpoint_color);
  892. }
  893. if (draw_bookmarks && is_line_bookmarked(p_line)) {
  894. int horizontal_padding = p_region.size.x / 2;
  895. int vertical_padding = p_region.size.y / 4;
  896. Rect2 bookmark_region = p_region;
  897. bookmark_region.position += Point2(horizontal_padding, 0);
  898. bookmark_region.size -= Point2(horizontal_padding * 1.1, vertical_padding);
  899. bookmark_icon->draw_rect(get_canvas_item(), bookmark_region, false, bookmark_color);
  900. }
  901. if (draw_executing_lines && is_line_executing(p_line)) {
  902. int horizontal_padding = p_region.size.x / 10;
  903. int vertical_padding = p_region.size.y / 4;
  904. Rect2 executing_line_region = p_region;
  905. executing_line_region.position += Point2(horizontal_padding, vertical_padding);
  906. executing_line_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  907. executing_line_icon->draw_rect(get_canvas_item(), executing_line_region, false, executing_line_color);
  908. }
  909. }
  910. // Breakpoints
  911. void CodeEdit::set_line_as_breakpoint(int p_line, bool p_breakpointed) {
  912. int mask = get_line_gutter_metadata(p_line, main_gutter);
  913. set_line_gutter_metadata(p_line, main_gutter, p_breakpointed ? mask | MAIN_GUTTER_BREAKPOINT : mask & ~MAIN_GUTTER_BREAKPOINT);
  914. if (p_breakpointed) {
  915. breakpointed_lines[p_line] = true;
  916. } else if (breakpointed_lines.has(p_line)) {
  917. breakpointed_lines.erase(p_line);
  918. }
  919. emit_signal(SNAME("breakpoint_toggled"), p_line);
  920. update();
  921. }
  922. bool CodeEdit::is_line_breakpointed(int p_line) const {
  923. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BREAKPOINT;
  924. }
  925. void CodeEdit::clear_breakpointed_lines() {
  926. for (int i = 0; i < get_line_count(); i++) {
  927. if (is_line_breakpointed(i)) {
  928. set_line_as_breakpoint(i, false);
  929. }
  930. }
  931. }
  932. Array CodeEdit::get_breakpointed_lines() const {
  933. Array ret;
  934. for (int i = 0; i < get_line_count(); i++) {
  935. if (is_line_breakpointed(i)) {
  936. ret.append(i);
  937. }
  938. }
  939. return ret;
  940. }
  941. // Bookmarks
  942. void CodeEdit::set_line_as_bookmarked(int p_line, bool p_bookmarked) {
  943. int mask = get_line_gutter_metadata(p_line, main_gutter);
  944. set_line_gutter_metadata(p_line, main_gutter, p_bookmarked ? mask | MAIN_GUTTER_BOOKMARK : mask & ~MAIN_GUTTER_BOOKMARK);
  945. update();
  946. }
  947. bool CodeEdit::is_line_bookmarked(int p_line) const {
  948. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BOOKMARK;
  949. }
  950. void CodeEdit::clear_bookmarked_lines() {
  951. for (int i = 0; i < get_line_count(); i++) {
  952. if (is_line_bookmarked(i)) {
  953. set_line_as_bookmarked(i, false);
  954. }
  955. }
  956. }
  957. Array CodeEdit::get_bookmarked_lines() const {
  958. Array ret;
  959. for (int i = 0; i < get_line_count(); i++) {
  960. if (is_line_bookmarked(i)) {
  961. ret.append(i);
  962. }
  963. }
  964. return ret;
  965. }
  966. // executing lines
  967. void CodeEdit::set_line_as_executing(int p_line, bool p_executing) {
  968. int mask = get_line_gutter_metadata(p_line, main_gutter);
  969. set_line_gutter_metadata(p_line, main_gutter, p_executing ? mask | MAIN_GUTTER_EXECUTING : mask & ~MAIN_GUTTER_EXECUTING);
  970. update();
  971. }
  972. bool CodeEdit::is_line_executing(int p_line) const {
  973. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_EXECUTING;
  974. }
  975. void CodeEdit::clear_executing_lines() {
  976. for (int i = 0; i < get_line_count(); i++) {
  977. if (is_line_executing(i)) {
  978. set_line_as_executing(i, false);
  979. }
  980. }
  981. }
  982. Array CodeEdit::get_executing_lines() const {
  983. Array ret;
  984. for (int i = 0; i < get_line_count(); i++) {
  985. if (is_line_executing(i)) {
  986. ret.append(i);
  987. }
  988. }
  989. return ret;
  990. }
  991. /* Line numbers */
  992. void CodeEdit::set_draw_line_numbers(bool p_draw) {
  993. set_gutter_draw(line_number_gutter, p_draw);
  994. }
  995. bool CodeEdit::is_draw_line_numbers_enabled() const {
  996. return is_gutter_drawn(line_number_gutter);
  997. }
  998. void CodeEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
  999. p_zero_padded ? line_number_padding = "0" : line_number_padding = " ";
  1000. update();
  1001. }
  1002. bool CodeEdit::is_line_numbers_zero_padded() const {
  1003. return line_number_padding == "0";
  1004. }
  1005. void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  1006. String fc = TS->format_number(String::num(p_line + 1).lpad(line_number_digits, line_number_padding));
  1007. Ref<TextLine> tl;
  1008. tl.instantiate();
  1009. tl->add_string(fc, cache.font, cache.font_size);
  1010. int yofs = p_region.position.y + (get_row_height() - tl->get_size().y) / 2;
  1011. Color number_color = get_line_gutter_item_color(p_line, line_number_gutter);
  1012. if (number_color == Color(1, 1, 1)) {
  1013. number_color = line_number_color;
  1014. }
  1015. tl->draw(get_canvas_item(), Point2(p_region.position.x, yofs), number_color);
  1016. }
  1017. /* Fold Gutter */
  1018. void CodeEdit::set_draw_fold_gutter(bool p_draw) {
  1019. set_gutter_draw(fold_gutter, p_draw);
  1020. }
  1021. bool CodeEdit::is_drawing_fold_gutter() const {
  1022. return is_gutter_drawn(fold_gutter);
  1023. }
  1024. void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region) {
  1025. if (!can_fold_line(p_line) && !is_line_folded(p_line)) {
  1026. set_line_gutter_clickable(p_line, fold_gutter, false);
  1027. return;
  1028. }
  1029. set_line_gutter_clickable(p_line, fold_gutter, true);
  1030. int horizontal_padding = p_region.size.x / 10;
  1031. int vertical_padding = p_region.size.y / 6;
  1032. p_region.position += Point2(horizontal_padding, vertical_padding);
  1033. p_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  1034. if (can_fold_line(p_line)) {
  1035. can_fold_icon->draw_rect(get_canvas_item(), p_region, false, folding_color);
  1036. return;
  1037. }
  1038. folded_icon->draw_rect(get_canvas_item(), p_region, false, folding_color);
  1039. }
  1040. /* Line Folding */
  1041. void CodeEdit::set_line_folding_enabled(bool p_enabled) {
  1042. line_folding_enabled = p_enabled;
  1043. set_hiding_enabled(p_enabled);
  1044. }
  1045. bool CodeEdit::is_line_folding_enabled() const {
  1046. return line_folding_enabled;
  1047. }
  1048. bool CodeEdit::can_fold_line(int p_line) const {
  1049. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1050. if (!line_folding_enabled) {
  1051. return false;
  1052. }
  1053. if (p_line + 1 >= get_line_count() || get_line(p_line).strip_edges().size() == 0) {
  1054. return false;
  1055. }
  1056. if (is_line_hidden(p_line) || is_line_folded(p_line)) {
  1057. return false;
  1058. }
  1059. /* Check for full multiline line or block strings / comments. */
  1060. int in_comment = is_in_comment(p_line);
  1061. int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
  1062. if (in_string != -1 || in_comment != -1) {
  1063. if (get_delimiter_start_position(p_line, get_line(p_line).size() - 1).y != p_line) {
  1064. return false;
  1065. }
  1066. int delimter_end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
  1067. /* No end line, therefore we have a multiline region over the rest of the file. */
  1068. if (delimter_end_line == -1) {
  1069. return true;
  1070. }
  1071. /* End line is the same therefore we have a block. */
  1072. if (delimter_end_line == p_line) {
  1073. /* Check we are the start of the block. */
  1074. if (p_line - 1 >= 0) {
  1075. if ((in_string != -1 && is_in_string(p_line - 1) != -1) || (in_comment != -1 && is_in_comment(p_line - 1) != -1)) {
  1076. return false;
  1077. }
  1078. }
  1079. /* Check it continues for at least one line. */
  1080. return ((in_string != -1 && is_in_string(p_line + 1) != -1) || (in_comment != -1 && is_in_comment(p_line + 1) != -1));
  1081. }
  1082. return ((in_string != -1 && is_in_string(delimter_end_line) != -1) || (in_comment != -1 && is_in_comment(delimter_end_line) != -1));
  1083. }
  1084. /* Otherwise check indent levels. */
  1085. int start_indent = get_indent_level(p_line);
  1086. for (int i = p_line + 1; i < get_line_count(); i++) {
  1087. if (is_in_string(i) != -1 || is_in_comment(i) != -1 || get_line(i).strip_edges().size() == 0) {
  1088. continue;
  1089. }
  1090. return (get_indent_level(i) > start_indent);
  1091. }
  1092. return false;
  1093. }
  1094. void CodeEdit::fold_line(int p_line) {
  1095. ERR_FAIL_INDEX(p_line, get_line_count());
  1096. if (!is_line_folding_enabled() || !can_fold_line(p_line)) {
  1097. return;
  1098. }
  1099. /* Find the last line to be hidden. */
  1100. int end_line = get_line_count();
  1101. int in_comment = is_in_comment(p_line);
  1102. int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
  1103. if (in_string != -1 || in_comment != -1) {
  1104. end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
  1105. /* End line is the same therefore we have a block. */
  1106. if (end_line == p_line) {
  1107. for (int i = p_line + 1; i < get_line_count(); i++) {
  1108. if ((in_string != -1 && is_in_string(i) == -1) || (in_comment != -1 && is_in_comment(i) == -1)) {
  1109. end_line = i - 1;
  1110. break;
  1111. }
  1112. }
  1113. }
  1114. } else {
  1115. int start_indent = get_indent_level(p_line);
  1116. for (int i = p_line + 1; i < get_line_count(); i++) {
  1117. if (get_line(p_line).strip_edges().size() == 0 || is_in_string(i) != -1 || is_in_comment(i) != -1) {
  1118. end_line = i;
  1119. continue;
  1120. }
  1121. if (get_indent_level(i) <= start_indent && get_line(i).strip_edges().size() != 0) {
  1122. end_line = i - 1;
  1123. break;
  1124. }
  1125. }
  1126. }
  1127. for (int i = p_line + 1; i <= end_line; i++) {
  1128. set_line_as_hidden(i, true);
  1129. }
  1130. /* Fix selection. */
  1131. if (is_selection_active()) {
  1132. if (is_line_hidden(get_selection_from_line()) && is_line_hidden(get_selection_to_line())) {
  1133. deselect();
  1134. } else if (is_line_hidden(get_selection_from_line())) {
  1135. select(p_line, 9999, get_selection_to_line(), get_selection_to_column());
  1136. } else if (is_line_hidden(get_selection_to_line())) {
  1137. select(get_selection_from_line(), get_selection_from_column(), p_line, 9999);
  1138. }
  1139. }
  1140. /* Reset caret. */
  1141. if (is_line_hidden(cursor_get_line())) {
  1142. cursor_set_line(p_line, false, false);
  1143. cursor_set_column(get_line(p_line).length(), false);
  1144. }
  1145. update();
  1146. }
  1147. void CodeEdit::unfold_line(int p_line) {
  1148. ERR_FAIL_INDEX(p_line, get_line_count());
  1149. if (!is_line_folded(p_line) && !is_line_hidden(p_line)) {
  1150. return;
  1151. }
  1152. int fold_start = p_line;
  1153. for (; fold_start > 0; fold_start--) {
  1154. if (is_line_folded(fold_start)) {
  1155. break;
  1156. }
  1157. }
  1158. fold_start = is_line_folded(fold_start) ? fold_start : p_line;
  1159. for (int i = fold_start + 1; i < get_line_count(); i++) {
  1160. if (!is_line_hidden(i)) {
  1161. break;
  1162. }
  1163. set_line_as_hidden(i, false);
  1164. }
  1165. update();
  1166. }
  1167. void CodeEdit::fold_all_lines() {
  1168. for (int i = 0; i < get_line_count(); i++) {
  1169. fold_line(i);
  1170. }
  1171. update();
  1172. }
  1173. void CodeEdit::unfold_all_lines() {
  1174. unhide_all_lines();
  1175. }
  1176. void CodeEdit::toggle_foldable_line(int p_line) {
  1177. ERR_FAIL_INDEX(p_line, get_line_count());
  1178. if (is_line_folded(p_line)) {
  1179. unfold_line(p_line);
  1180. return;
  1181. }
  1182. fold_line(p_line);
  1183. }
  1184. bool CodeEdit::is_line_folded(int p_line) const {
  1185. ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
  1186. return p_line + 1 < get_line_count() && !is_line_hidden(p_line) && is_line_hidden(p_line + 1);
  1187. }
  1188. TypedArray<int> CodeEdit::get_folded_lines() const {
  1189. TypedArray<int> folded_lines;
  1190. for (int i = 0; i < get_line_count(); i++) {
  1191. if (is_line_folded(i)) {
  1192. folded_lines.push_back(i);
  1193. }
  1194. }
  1195. return folded_lines;
  1196. }
  1197. /* Delimiters */
  1198. // Strings
  1199. void CodeEdit::add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  1200. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_STRING);
  1201. }
  1202. void CodeEdit::remove_string_delimiter(const String &p_start_key) {
  1203. _remove_delimiter(p_start_key, TYPE_STRING);
  1204. }
  1205. bool CodeEdit::has_string_delimiter(const String &p_start_key) const {
  1206. return _has_delimiter(p_start_key, TYPE_STRING);
  1207. }
  1208. void CodeEdit::set_string_delimiters(const TypedArray<String> &p_string_delimiters) {
  1209. _set_delimiters(p_string_delimiters, TYPE_STRING);
  1210. }
  1211. void CodeEdit::clear_string_delimiters() {
  1212. _clear_delimiters(TYPE_STRING);
  1213. }
  1214. TypedArray<String> CodeEdit::get_string_delimiters() const {
  1215. return _get_delimiters(TYPE_STRING);
  1216. }
  1217. int CodeEdit::is_in_string(int p_line, int p_column) const {
  1218. return _is_in_delimiter(p_line, p_column, TYPE_STRING);
  1219. }
  1220. // Comments
  1221. void CodeEdit::add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  1222. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_COMMENT);
  1223. }
  1224. void CodeEdit::remove_comment_delimiter(const String &p_start_key) {
  1225. _remove_delimiter(p_start_key, TYPE_COMMENT);
  1226. }
  1227. bool CodeEdit::has_comment_delimiter(const String &p_start_key) const {
  1228. return _has_delimiter(p_start_key, TYPE_COMMENT);
  1229. }
  1230. void CodeEdit::set_comment_delimiters(const TypedArray<String> &p_comment_delimiters) {
  1231. _set_delimiters(p_comment_delimiters, TYPE_COMMENT);
  1232. }
  1233. void CodeEdit::clear_comment_delimiters() {
  1234. _clear_delimiters(TYPE_COMMENT);
  1235. }
  1236. TypedArray<String> CodeEdit::get_comment_delimiters() const {
  1237. return _get_delimiters(TYPE_COMMENT);
  1238. }
  1239. int CodeEdit::is_in_comment(int p_line, int p_column) const {
  1240. return _is_in_delimiter(p_line, p_column, TYPE_COMMENT);
  1241. }
  1242. String CodeEdit::get_delimiter_start_key(int p_delimiter_idx) const {
  1243. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  1244. return delimiters[p_delimiter_idx].start_key;
  1245. }
  1246. String CodeEdit::get_delimiter_end_key(int p_delimiter_idx) const {
  1247. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  1248. return delimiters[p_delimiter_idx].end_key;
  1249. }
  1250. Point2 CodeEdit::get_delimiter_start_position(int p_line, int p_column) const {
  1251. if (delimiters.size() == 0) {
  1252. return Point2(-1, -1);
  1253. }
  1254. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  1255. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  1256. Point2 start_position;
  1257. start_position.y = -1;
  1258. start_position.x = -1;
  1259. bool in_region = ((p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value()) != -1;
  1260. /* Check the keys for this line. */
  1261. for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  1262. if (E->key() > p_column) {
  1263. break;
  1264. }
  1265. in_region = E->value() != -1;
  1266. start_position.x = in_region ? E->key() : -1;
  1267. }
  1268. /* Region was found on this line and is not a multiline continuation. */
  1269. if (start_position.x != -1 && start_position.x != get_line(p_line).length() + 1) {
  1270. start_position.y = p_line;
  1271. return start_position;
  1272. }
  1273. /* Not in a region */
  1274. if (!in_region) {
  1275. return start_position;
  1276. }
  1277. /* Region starts on a previous line */
  1278. for (int i = p_line - 1; i >= 0; i--) {
  1279. if (delimiter_cache[i].size() < 1) {
  1280. continue;
  1281. }
  1282. start_position.y = i;
  1283. start_position.x = delimiter_cache[i].back()->key();
  1284. /* Make sure it's not a multiline continuation. */
  1285. if (start_position.x != get_line(i).length() + 1) {
  1286. break;
  1287. }
  1288. }
  1289. return start_position;
  1290. }
  1291. Point2 CodeEdit::get_delimiter_end_position(int p_line, int p_column) const {
  1292. if (delimiters.size() == 0) {
  1293. return Point2(-1, -1);
  1294. }
  1295. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  1296. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  1297. Point2 end_position;
  1298. end_position.y = -1;
  1299. end_position.x = -1;
  1300. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  1301. /* Check the keys for this line. */
  1302. for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  1303. end_position.x = (E->value() == -1) ? E->key() : -1;
  1304. if (E->key() > p_column) {
  1305. break;
  1306. }
  1307. region = E->value();
  1308. }
  1309. /* Region was found on this line and is not a multiline continuation. */
  1310. if (region != -1 && end_position.x != -1 && (delimiters[region].line_only || end_position.x != get_line(p_line).length() + 1)) {
  1311. end_position.y = p_line;
  1312. return end_position;
  1313. }
  1314. /* Not in a region */
  1315. if (region == -1) {
  1316. end_position.x = -1;
  1317. return end_position;
  1318. }
  1319. /* Region ends on a later line */
  1320. for (int i = p_line + 1; i < get_line_count(); i++) {
  1321. if (delimiter_cache[i].size() < 1 || delimiter_cache[i].front()->value() != -1) {
  1322. continue;
  1323. }
  1324. end_position.x = delimiter_cache[i].front()->key();
  1325. /* Make sure it's not a multiline continuation. */
  1326. if (get_line(i).length() > 0 && end_position.x != get_line(i).length() + 1) {
  1327. end_position.y = i;
  1328. break;
  1329. }
  1330. end_position.x = -1;
  1331. }
  1332. return end_position;
  1333. }
  1334. /* Code hint */
  1335. void CodeEdit::set_code_hint(const String &p_hint) {
  1336. code_hint = p_hint;
  1337. code_hint_xpos = -0xFFFF;
  1338. update();
  1339. }
  1340. void CodeEdit::set_code_hint_draw_below(bool p_below) {
  1341. code_hint_draw_below = p_below;
  1342. update();
  1343. }
  1344. /* Code Completion */
  1345. void CodeEdit::set_code_completion_enabled(bool p_enable) {
  1346. code_completion_enabled = p_enable;
  1347. }
  1348. bool CodeEdit::is_code_completion_enabled() const {
  1349. return code_completion_enabled;
  1350. }
  1351. void CodeEdit::set_code_completion_prefixes(const TypedArray<String> &p_prefixes) {
  1352. code_completion_prefixes.clear();
  1353. for (int i = 0; i < p_prefixes.size(); i++) {
  1354. code_completion_prefixes.insert(p_prefixes[i]);
  1355. }
  1356. }
  1357. TypedArray<String> CodeEdit::get_code_completion_prefixes() const {
  1358. TypedArray<String> prefixes;
  1359. for (Set<String>::Element *E = code_completion_prefixes.front(); E; E = E->next()) {
  1360. prefixes.push_back(E->get());
  1361. }
  1362. return prefixes;
  1363. }
  1364. String CodeEdit::get_text_for_code_completion() const {
  1365. StringBuilder completion_text;
  1366. const int text_size = get_line_count();
  1367. for (int i = 0; i < text_size; i++) {
  1368. String line = get_line(i);
  1369. if (i == cursor_get_line()) {
  1370. completion_text += line.substr(0, cursor_get_column());
  1371. /* Not unicode, represents the caret. */
  1372. completion_text += String::chr(0xFFFF);
  1373. completion_text += line.substr(cursor_get_column(), line.size());
  1374. } else {
  1375. completion_text += line;
  1376. }
  1377. if (i != text_size - 1) {
  1378. completion_text += "\n";
  1379. }
  1380. }
  1381. return completion_text.as_string();
  1382. }
  1383. void CodeEdit::request_code_completion(bool p_force) {
  1384. ScriptInstance *si = get_script_instance();
  1385. if (si && si->has_method("_request_code_completion")) {
  1386. si->call("_request_code_completion", p_force);
  1387. return;
  1388. }
  1389. /* Don't re-query if all existing options are quoted types, eg path, signal. */
  1390. bool ignored = code_completion_active && !code_completion_options.is_empty();
  1391. if (ignored) {
  1392. ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_PLAIN_TEXT;
  1393. const ScriptCodeCompletionOption *previous_option = nullptr;
  1394. for (int i = 0; i < code_completion_options.size(); i++) {
  1395. const ScriptCodeCompletionOption &current_option = code_completion_options[i];
  1396. if (!previous_option) {
  1397. previous_option = &current_option;
  1398. kind = current_option.kind;
  1399. }
  1400. if (previous_option->kind != current_option.kind) {
  1401. ignored = false;
  1402. break;
  1403. }
  1404. }
  1405. ignored = ignored && (kind == ScriptCodeCompletionOption::KIND_FILE_PATH || kind == ScriptCodeCompletionOption::KIND_NODE_PATH || kind == ScriptCodeCompletionOption::KIND_SIGNAL);
  1406. }
  1407. if (ignored) {
  1408. return;
  1409. }
  1410. if (p_force) {
  1411. emit_signal(SNAME("request_code_completion"));
  1412. return;
  1413. }
  1414. String line = get_line(cursor_get_line());
  1415. int ofs = CLAMP(cursor_get_column(), 0, line.length());
  1416. 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])))) {
  1417. emit_signal(SNAME("request_code_completion"));
  1418. } else if (ofs > 1 && line[ofs - 1] == ' ' && code_completion_prefixes.has(String::chr(line[ofs - 2]))) {
  1419. emit_signal(SNAME("request_code_completion"));
  1420. }
  1421. }
  1422. 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) {
  1423. ScriptCodeCompletionOption completion_option;
  1424. completion_option.kind = (ScriptCodeCompletionOption::Kind)p_type;
  1425. completion_option.display = p_display_text;
  1426. completion_option.insert_text = p_insert_text;
  1427. completion_option.font_color = p_text_color;
  1428. completion_option.icon = p_icon;
  1429. completion_option.default_value = p_value;
  1430. code_completion_option_submitted.push_back(completion_option);
  1431. }
  1432. void CodeEdit::update_code_completion_options(bool p_forced) {
  1433. code_completion_forced = p_forced;
  1434. code_completion_option_sources = code_completion_option_submitted;
  1435. code_completion_option_submitted.clear();
  1436. _filter_code_completion_candidates();
  1437. }
  1438. TypedArray<Dictionary> CodeEdit::get_code_completion_options() const {
  1439. if (!code_completion_active) {
  1440. return TypedArray<Dictionary>();
  1441. }
  1442. TypedArray<Dictionary> completion_options;
  1443. completion_options.resize(code_completion_options.size());
  1444. for (int i = 0; i < code_completion_options.size(); i++) {
  1445. Dictionary option;
  1446. option["kind"] = code_completion_options[i].kind;
  1447. option["display_text"] = code_completion_options[i].display;
  1448. option["insert_text"] = code_completion_options[i].insert_text;
  1449. option["font_color"] = code_completion_options[i].font_color;
  1450. option["icon"] = code_completion_options[i].icon;
  1451. option["default_value"] = code_completion_options[i].default_value;
  1452. completion_options[i] = option;
  1453. }
  1454. return completion_options;
  1455. }
  1456. Dictionary CodeEdit::get_code_completion_option(int p_index) const {
  1457. if (!code_completion_active) {
  1458. return Dictionary();
  1459. }
  1460. ERR_FAIL_INDEX_V(p_index, code_completion_options.size(), Dictionary());
  1461. Dictionary option;
  1462. option["kind"] = code_completion_options[p_index].kind;
  1463. option["display_text"] = code_completion_options[p_index].display;
  1464. option["insert_text"] = code_completion_options[p_index].insert_text;
  1465. option["font_color"] = code_completion_options[p_index].font_color;
  1466. option["icon"] = code_completion_options[p_index].icon;
  1467. option["default_value"] = code_completion_options[p_index].default_value;
  1468. return option;
  1469. }
  1470. int CodeEdit::get_code_completion_selected_index() const {
  1471. return (code_completion_active) ? code_completion_current_selected : -1;
  1472. }
  1473. void CodeEdit::set_code_completion_selected_index(int p_index) {
  1474. if (!code_completion_active) {
  1475. return;
  1476. }
  1477. ERR_FAIL_INDEX(p_index, code_completion_options.size());
  1478. code_completion_current_selected = p_index;
  1479. update();
  1480. }
  1481. void CodeEdit::confirm_code_completion(bool p_replace) {
  1482. if (is_readonly() || !code_completion_active) {
  1483. return;
  1484. }
  1485. ScriptInstance *si = get_script_instance();
  1486. if (si && si->has_method("_confirm_code_completion")) {
  1487. si->call("_confirm_code_completion", p_replace);
  1488. return;
  1489. }
  1490. begin_complex_operation();
  1491. int caret_line = cursor_get_line();
  1492. const String &insert_text = code_completion_options[code_completion_current_selected].insert_text;
  1493. const String &display_text = code_completion_options[code_completion_current_selected].display;
  1494. if (p_replace) {
  1495. /* Find end of current section */
  1496. const String line = get_line(caret_line);
  1497. int caret_col = cursor_get_column();
  1498. int caret_remove_line = caret_line;
  1499. bool merge_text = true;
  1500. int in_string = is_in_string(caret_line, caret_col);
  1501. if (in_string != -1) {
  1502. Point2 string_end = get_delimiter_end_position(caret_line, caret_col);
  1503. if (string_end.x != -1) {
  1504. merge_text = false;
  1505. caret_remove_line = string_end.y;
  1506. caret_col = string_end.x - 1;
  1507. }
  1508. }
  1509. if (merge_text) {
  1510. for (; caret_col < line.length(); caret_col++) {
  1511. if (!_is_char(line[caret_col])) {
  1512. break;
  1513. }
  1514. }
  1515. }
  1516. /* Replace. */
  1517. _remove_text(caret_line, cursor_get_column() - code_completion_base.length(), caret_remove_line, caret_col);
  1518. cursor_set_column(cursor_get_column() - code_completion_base.length(), false);
  1519. insert_text_at_cursor(insert_text);
  1520. } else {
  1521. /* Get first non-matching char. */
  1522. const String line = get_line(caret_line);
  1523. int caret_col = cursor_get_column();
  1524. int matching_chars = code_completion_base.length();
  1525. for (; matching_chars <= insert_text.length(); matching_chars++) {
  1526. if (caret_col >= line.length() || line[caret_col] != insert_text[matching_chars]) {
  1527. break;
  1528. }
  1529. caret_col++;
  1530. }
  1531. /* Remove base completion text. */
  1532. _remove_text(caret_line, cursor_get_column() - code_completion_base.length(), caret_line, cursor_get_column());
  1533. cursor_set_column(cursor_get_column() - code_completion_base.length(), false);
  1534. /* Merge with text. */
  1535. insert_text_at_cursor(insert_text.substr(0, code_completion_base.length()));
  1536. cursor_set_column(caret_col, false);
  1537. insert_text_at_cursor(insert_text.substr(matching_chars));
  1538. }
  1539. /* Handle merging of symbols eg strings, brackets. */
  1540. const String line = get_line(caret_line);
  1541. char32_t next_char = line[cursor_get_column()];
  1542. char32_t last_completion_char = insert_text[insert_text.length() - 1];
  1543. char32_t last_completion_char_display = display_text[display_text.length() - 1];
  1544. int pre_brace_pair = cursor_get_column() > 0 ? _get_auto_brace_pair_open_at_pos(caret_line, cursor_get_column()) : -1;
  1545. 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;
  1546. if (post_brace_pair != -1 && (last_completion_char == next_char || last_completion_char_display == next_char)) {
  1547. _remove_text(caret_line, cursor_get_column(), caret_line, cursor_get_column() + 1);
  1548. }
  1549. if (pre_brace_pair != -1 && pre_brace_pair != post_brace_pair && (last_completion_char == next_char || last_completion_char_display == next_char)) {
  1550. _remove_text(caret_line, cursor_get_column(), caret_line, cursor_get_column() + 1);
  1551. } else if (auto_brace_completion_enabled && pre_brace_pair != -1 && post_brace_pair == -1) {
  1552. insert_text_at_cursor(auto_brace_completion_pairs[pre_brace_pair].close_key);
  1553. cursor_set_column(cursor_get_column() - auto_brace_completion_pairs[pre_brace_pair].close_key.length());
  1554. }
  1555. if (pre_brace_pair == -1 && post_brace_pair == -1 && cursor_get_column() > 0 && cursor_get_column() < get_line(caret_line).length()) {
  1556. pre_brace_pair = _get_auto_brace_pair_open_at_pos(caret_line, cursor_get_column() + 1);
  1557. if (pre_brace_pair == _get_auto_brace_pair_close_at_pos(caret_line, cursor_get_column() - 1)) {
  1558. _remove_text(caret_line, cursor_get_column() - 2, caret_line, cursor_get_column());
  1559. if (_get_auto_brace_pair_close_at_pos(caret_line, cursor_get_column() - 1) != pre_brace_pair) {
  1560. cursor_set_column(cursor_get_column() - 1);
  1561. }
  1562. }
  1563. }
  1564. end_complex_operation();
  1565. cancel_code_completion();
  1566. if (code_completion_prefixes.has(String::chr(last_completion_char))) {
  1567. request_code_completion();
  1568. }
  1569. }
  1570. void CodeEdit::cancel_code_completion() {
  1571. if (!code_completion_active) {
  1572. return;
  1573. }
  1574. code_completion_forced = false;
  1575. code_completion_active = false;
  1576. update();
  1577. }
  1578. void CodeEdit::_bind_methods() {
  1579. /* Indent management */
  1580. ClassDB::bind_method(D_METHOD("set_indent_size", "size"), &CodeEdit::set_indent_size);
  1581. ClassDB::bind_method(D_METHOD("get_indent_size"), &CodeEdit::get_indent_size);
  1582. ClassDB::bind_method(D_METHOD("set_indent_using_spaces", "use_spaces"), &CodeEdit::set_indent_using_spaces);
  1583. ClassDB::bind_method(D_METHOD("is_indent_using_spaces"), &CodeEdit::is_indent_using_spaces);
  1584. ClassDB::bind_method(D_METHOD("set_auto_indent_enabled", "enable"), &CodeEdit::set_auto_indent_enabled);
  1585. ClassDB::bind_method(D_METHOD("is_auto_indent_enabled"), &CodeEdit::is_auto_indent_enabled);
  1586. ClassDB::bind_method(D_METHOD("set_auto_indent_prefixes", "prefixes"), &CodeEdit::set_auto_indent_prefixes);
  1587. ClassDB::bind_method(D_METHOD("get_auto_indent_prefixes"), &CodeEdit::get_auto_indent_prefixes);
  1588. ClassDB::bind_method(D_METHOD("do_indent"), &CodeEdit::do_indent);
  1589. ClassDB::bind_method(D_METHOD("do_unindent"), &CodeEdit::do_unindent);
  1590. ClassDB::bind_method(D_METHOD("indent_lines"), &CodeEdit::indent_lines);
  1591. ClassDB::bind_method(D_METHOD("unindent_lines"), &CodeEdit::unindent_lines);
  1592. /* Auto brace completion */
  1593. ClassDB::bind_method(D_METHOD("set_auto_brace_completion_enabled", "enable"), &CodeEdit::set_auto_brace_completion_enabled);
  1594. ClassDB::bind_method(D_METHOD("is_auto_brace_completion_enabled"), &CodeEdit::is_auto_brace_completion_enabled);
  1595. ClassDB::bind_method(D_METHOD("set_highlight_matching_braces_enabled", "enable"), &CodeEdit::set_highlight_matching_braces_enabled);
  1596. ClassDB::bind_method(D_METHOD("is_highlight_matching_braces_enabled"), &CodeEdit::is_highlight_matching_braces_enabled);
  1597. ClassDB::bind_method(D_METHOD("add_auto_brace_completion_pair", "start_key", "end_key"), &CodeEdit::add_auto_brace_completion_pair);
  1598. ClassDB::bind_method(D_METHOD("set_auto_brace_completion_pairs", "pairs"), &CodeEdit::set_auto_brace_completion_pairs);
  1599. ClassDB::bind_method(D_METHOD("get_auto_brace_completion_pairs"), &CodeEdit::get_auto_brace_completion_pairs);
  1600. ClassDB::bind_method(D_METHOD("has_auto_brace_completion_open_key", "open_key"), &CodeEdit::has_auto_brace_completion_open_key);
  1601. ClassDB::bind_method(D_METHOD("has_auto_brace_completion_close_key", "close_key"), &CodeEdit::has_auto_brace_completion_close_key);
  1602. ClassDB::bind_method(D_METHOD("get_auto_brace_completion_close_key", "open_key"), &CodeEdit::get_auto_brace_completion_close_key);
  1603. /* Main Gutter */
  1604. ClassDB::bind_method(D_METHOD("_main_gutter_draw_callback"), &CodeEdit::_main_gutter_draw_callback);
  1605. ClassDB::bind_method(D_METHOD("set_draw_breakpoints_gutter", "enable"), &CodeEdit::set_draw_breakpoints_gutter);
  1606. ClassDB::bind_method(D_METHOD("is_drawing_breakpoints_gutter"), &CodeEdit::is_drawing_breakpoints_gutter);
  1607. ClassDB::bind_method(D_METHOD("set_draw_bookmarks_gutter", "enable"), &CodeEdit::set_draw_bookmarks_gutter);
  1608. ClassDB::bind_method(D_METHOD("is_drawing_bookmarks_gutter"), &CodeEdit::is_drawing_bookmarks_gutter);
  1609. ClassDB::bind_method(D_METHOD("set_draw_executing_lines_gutter", "enable"), &CodeEdit::set_draw_executing_lines_gutter);
  1610. ClassDB::bind_method(D_METHOD("is_drawing_executing_lines_gutter"), &CodeEdit::is_drawing_executing_lines_gutter);
  1611. // Breakpoints
  1612. ClassDB::bind_method(D_METHOD("set_line_as_breakpoint", "line", "breakpointed"), &CodeEdit::set_line_as_breakpoint);
  1613. ClassDB::bind_method(D_METHOD("is_line_breakpointed", "line"), &CodeEdit::is_line_breakpointed);
  1614. ClassDB::bind_method(D_METHOD("clear_breakpointed_lines"), &CodeEdit::clear_breakpointed_lines);
  1615. ClassDB::bind_method(D_METHOD("get_breakpointed_lines"), &CodeEdit::get_breakpointed_lines);
  1616. // Bookmarks
  1617. ClassDB::bind_method(D_METHOD("set_line_as_bookmarked", "line", "bookmarked"), &CodeEdit::set_line_as_bookmarked);
  1618. ClassDB::bind_method(D_METHOD("is_line_bookmarked", "line"), &CodeEdit::is_line_bookmarked);
  1619. ClassDB::bind_method(D_METHOD("clear_bookmarked_lines"), &CodeEdit::clear_bookmarked_lines);
  1620. ClassDB::bind_method(D_METHOD("get_bookmarked_lines"), &CodeEdit::get_bookmarked_lines);
  1621. // executing lines
  1622. ClassDB::bind_method(D_METHOD("set_line_as_executing", "line", "executing"), &CodeEdit::set_line_as_executing);
  1623. ClassDB::bind_method(D_METHOD("is_line_executing", "line"), &CodeEdit::is_line_executing);
  1624. ClassDB::bind_method(D_METHOD("clear_executing_lines"), &CodeEdit::clear_executing_lines);
  1625. ClassDB::bind_method(D_METHOD("get_executing_lines"), &CodeEdit::get_executing_lines);
  1626. /* Line numbers */
  1627. ClassDB::bind_method(D_METHOD("_line_number_draw_callback"), &CodeEdit::_line_number_draw_callback);
  1628. ClassDB::bind_method(D_METHOD("set_draw_line_numbers", "enable"), &CodeEdit::set_draw_line_numbers);
  1629. ClassDB::bind_method(D_METHOD("is_draw_line_numbers_enabled"), &CodeEdit::is_draw_line_numbers_enabled);
  1630. ClassDB::bind_method(D_METHOD("set_line_numbers_zero_padded", "enable"), &CodeEdit::set_line_numbers_zero_padded);
  1631. ClassDB::bind_method(D_METHOD("is_line_numbers_zero_padded"), &CodeEdit::is_line_numbers_zero_padded);
  1632. /* Fold Gutter */
  1633. ClassDB::bind_method(D_METHOD("_fold_gutter_draw_callback"), &CodeEdit::_fold_gutter_draw_callback);
  1634. ClassDB::bind_method(D_METHOD("set_draw_fold_gutter", "enable"), &CodeEdit::set_draw_fold_gutter);
  1635. ClassDB::bind_method(D_METHOD("is_drawing_fold_gutter"), &CodeEdit::is_drawing_fold_gutter);
  1636. /* Line folding */
  1637. ClassDB::bind_method(D_METHOD("set_line_folding_enabled", "enabled"), &CodeEdit::set_line_folding_enabled);
  1638. ClassDB::bind_method(D_METHOD("is_line_folding_enabled"), &CodeEdit::is_line_folding_enabled);
  1639. ClassDB::bind_method(D_METHOD("can_fold_line", "line"), &CodeEdit::can_fold_line);
  1640. ClassDB::bind_method(D_METHOD("fold_line", "line"), &CodeEdit::fold_line);
  1641. ClassDB::bind_method(D_METHOD("unfold_line", "line"), &CodeEdit::unfold_line);
  1642. ClassDB::bind_method(D_METHOD("fold_all_lines"), &CodeEdit::fold_all_lines);
  1643. ClassDB::bind_method(D_METHOD("unfold_all_lines"), &CodeEdit::unfold_all_lines);
  1644. ClassDB::bind_method(D_METHOD("toggle_foldable_line", "line"), &CodeEdit::toggle_foldable_line);
  1645. ClassDB::bind_method(D_METHOD("is_line_folded", "line"), &CodeEdit::is_line_folded);
  1646. ClassDB::bind_method(D_METHOD("get_folded_lines"), &CodeEdit::get_folded_lines);
  1647. /* Delimiters */
  1648. // Strings
  1649. ClassDB::bind_method(D_METHOD("add_string_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_string_delimiter, DEFVAL(false));
  1650. ClassDB::bind_method(D_METHOD("remove_string_delimiter", "start_key"), &CodeEdit::remove_string_delimiter);
  1651. ClassDB::bind_method(D_METHOD("has_string_delimiter", "start_key"), &CodeEdit::has_string_delimiter);
  1652. ClassDB::bind_method(D_METHOD("set_string_delimiters", "string_delimiters"), &CodeEdit::set_string_delimiters);
  1653. ClassDB::bind_method(D_METHOD("clear_string_delimiters"), &CodeEdit::clear_string_delimiters);
  1654. ClassDB::bind_method(D_METHOD("get_string_delimiters"), &CodeEdit::get_string_delimiters);
  1655. ClassDB::bind_method(D_METHOD("is_in_string", "line", "column"), &CodeEdit::is_in_string, DEFVAL(-1));
  1656. // Comments
  1657. ClassDB::bind_method(D_METHOD("add_comment_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_comment_delimiter, DEFVAL(false));
  1658. ClassDB::bind_method(D_METHOD("remove_comment_delimiter", "start_key"), &CodeEdit::remove_comment_delimiter);
  1659. ClassDB::bind_method(D_METHOD("has_comment_delimiter", "start_key"), &CodeEdit::has_comment_delimiter);
  1660. ClassDB::bind_method(D_METHOD("set_comment_delimiters", "comment_delimiters"), &CodeEdit::set_comment_delimiters);
  1661. ClassDB::bind_method(D_METHOD("clear_comment_delimiters"), &CodeEdit::clear_comment_delimiters);
  1662. ClassDB::bind_method(D_METHOD("get_comment_delimiters"), &CodeEdit::get_comment_delimiters);
  1663. ClassDB::bind_method(D_METHOD("is_in_comment", "line", "column"), &CodeEdit::is_in_comment, DEFVAL(-1));
  1664. // Util
  1665. ClassDB::bind_method(D_METHOD("get_delimiter_start_key", "delimiter_index"), &CodeEdit::get_delimiter_start_key);
  1666. ClassDB::bind_method(D_METHOD("get_delimiter_end_key", "delimiter_index"), &CodeEdit::get_delimiter_end_key);
  1667. ClassDB::bind_method(D_METHOD("get_delimiter_start_position", "line", "column"), &CodeEdit::get_delimiter_start_position);
  1668. ClassDB::bind_method(D_METHOD("get_delimiter_end_position", "line", "column"), &CodeEdit::get_delimiter_end_position);
  1669. /* Code hint */
  1670. ClassDB::bind_method(D_METHOD("set_code_hint", "code_hint"), &CodeEdit::set_code_hint);
  1671. ClassDB::bind_method(D_METHOD("set_code_hint_draw_below", "draw_below"), &CodeEdit::set_code_hint_draw_below);
  1672. /* Code Completion */
  1673. BIND_ENUM_CONSTANT(KIND_CLASS);
  1674. BIND_ENUM_CONSTANT(KIND_FUNCTION);
  1675. BIND_ENUM_CONSTANT(KIND_SIGNAL);
  1676. BIND_ENUM_CONSTANT(KIND_VARIABLE);
  1677. BIND_ENUM_CONSTANT(KIND_MEMBER);
  1678. BIND_ENUM_CONSTANT(KIND_ENUM);
  1679. BIND_ENUM_CONSTANT(KIND_CONSTANT);
  1680. BIND_ENUM_CONSTANT(KIND_NODE_PATH);
  1681. BIND_ENUM_CONSTANT(KIND_FILE_PATH);
  1682. BIND_ENUM_CONSTANT(KIND_PLAIN_TEXT);
  1683. ClassDB::bind_method(D_METHOD("get_text_for_code_completion"), &CodeEdit::get_text_for_code_completion);
  1684. ClassDB::bind_method(D_METHOD("request_code_completion", "force"), &CodeEdit::request_code_completion, DEFVAL(false));
  1685. 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));
  1686. ClassDB::bind_method(D_METHOD("update_code_completion_options", "force"), &CodeEdit::update_code_completion_options);
  1687. ClassDB::bind_method(D_METHOD("get_code_completion_options"), &CodeEdit::get_code_completion_options);
  1688. ClassDB::bind_method(D_METHOD("get_code_completion_option", "index"), &CodeEdit::get_code_completion_option);
  1689. ClassDB::bind_method(D_METHOD("get_code_completion_selected_index"), &CodeEdit::get_code_completion_selected_index);
  1690. ClassDB::bind_method(D_METHOD("set_code_completion_selected_index", "index"), &CodeEdit::set_code_completion_selected_index);
  1691. ClassDB::bind_method(D_METHOD("confirm_code_completion", "replace"), &CodeEdit::confirm_code_completion, DEFVAL(false));
  1692. ClassDB::bind_method(D_METHOD("cancel_code_completion"), &CodeEdit::cancel_code_completion);
  1693. ClassDB::bind_method(D_METHOD("set_code_completion_enabled", "enable"), &CodeEdit::set_code_completion_enabled);
  1694. ClassDB::bind_method(D_METHOD("is_code_completion_enabled"), &CodeEdit::is_code_completion_enabled);
  1695. ClassDB::bind_method(D_METHOD("set_code_completion_prefixes", "prefixes"), &CodeEdit::set_code_completion_prefixes);
  1696. ClassDB::bind_method(D_METHOD("get_code_comletion_prefixes"), &CodeEdit::get_code_completion_prefixes);
  1697. // Overridable
  1698. BIND_VMETHOD(MethodInfo("_confirm_code_completion", PropertyInfo(Variant::BOOL, "replace")));
  1699. BIND_VMETHOD(MethodInfo("_request_code_completion", PropertyInfo(Variant::BOOL, "force")));
  1700. BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_filter_code_completion_candidates", PropertyInfo(Variant::ARRAY, "candidates")));
  1701. /* Inspector */
  1702. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_breakpoints_gutter"), "set_draw_breakpoints_gutter", "is_drawing_breakpoints_gutter");
  1703. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_bookmarks"), "set_draw_bookmarks_gutter", "is_drawing_bookmarks_gutter");
  1704. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_executing_lines"), "set_draw_executing_lines_gutter", "is_drawing_executing_lines_gutter");
  1705. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_line_numbers"), "set_draw_line_numbers", "is_draw_line_numbers_enabled");
  1706. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "zero_pad_line_numbers"), "set_line_numbers_zero_padded", "is_line_numbers_zero_padded");
  1707. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_fold_gutter"), "set_draw_fold_gutter", "is_drawing_fold_gutter");
  1708. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "line_folding"), "set_line_folding_enabled", "is_line_folding_enabled");
  1709. ADD_GROUP("Delimiters", "delimiter_");
  1710. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_strings"), "set_string_delimiters", "get_string_delimiters");
  1711. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_comments"), "set_comment_delimiters", "get_comment_delimiters");
  1712. ADD_GROUP("Code Completion", "code_completion_");
  1713. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "code_completion_enabled"), "set_code_completion_enabled", "is_code_completion_enabled");
  1714. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "code_completion_prefixes"), "set_code_completion_prefixes", "get_code_comletion_prefixes");
  1715. ADD_GROUP("Indentation", "indent_");
  1716. ADD_PROPERTY(PropertyInfo(Variant::INT, "indent_size"), "set_indent_size", "get_indent_size");
  1717. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_use_spaces"), "set_indent_using_spaces", "is_indent_using_spaces");
  1718. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_automatic"), "set_auto_indent_enabled", "is_auto_indent_enabled");
  1719. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "indent_automatic_prefixes"), "set_auto_indent_prefixes", "get_auto_indent_prefixes");
  1720. ADD_GROUP("Auto brace completion", "auto_brace_completion_");
  1721. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_enabled"), "set_auto_brace_completion_enabled", "is_auto_brace_completion_enabled");
  1722. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_highlight_matching"), "set_highlight_matching_braces_enabled", "is_highlight_matching_braces_enabled");
  1723. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "auto_brace_completion_pairs"), "set_auto_brace_completion_pairs", "get_auto_brace_completion_pairs");
  1724. /* Signals */
  1725. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "line")));
  1726. ADD_SIGNAL(MethodInfo("request_code_completion"));
  1727. }
  1728. /* Auto brace completion */
  1729. int CodeEdit::_get_auto_brace_pair_open_at_pos(int p_line, int p_col) {
  1730. const String &line = get_line(p_line);
  1731. /* Should be fast enough, expecting low amount of pairs... */
  1732. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1733. const String &open_key = auto_brace_completion_pairs[i].open_key;
  1734. if (p_col - open_key.length() < 0) {
  1735. continue;
  1736. }
  1737. bool is_match = true;
  1738. for (int j = 0; j < open_key.length(); j++) {
  1739. if (line[(p_col - 1) - j] != open_key[(open_key.length() - 1) - j]) {
  1740. is_match = false;
  1741. break;
  1742. }
  1743. }
  1744. if (is_match) {
  1745. return i;
  1746. }
  1747. }
  1748. return -1;
  1749. }
  1750. int CodeEdit::_get_auto_brace_pair_close_at_pos(int p_line, int p_col) {
  1751. const String &line = get_line(p_line);
  1752. /* Should be fast enough, expecting low amount of pairs... */
  1753. for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
  1754. if (p_col + auto_brace_completion_pairs[i].close_key.length() > line.length()) {
  1755. continue;
  1756. }
  1757. bool is_match = true;
  1758. for (int j = 0; j < auto_brace_completion_pairs[i].close_key.length(); j++) {
  1759. if (line[p_col + j] != auto_brace_completion_pairs[i].close_key[j]) {
  1760. is_match = false;
  1761. break;
  1762. }
  1763. }
  1764. if (is_match) {
  1765. return i;
  1766. }
  1767. }
  1768. return -1;
  1769. }
  1770. /* Gutters */
  1771. void CodeEdit::_gutter_clicked(int p_line, int p_gutter) {
  1772. if (p_gutter == main_gutter) {
  1773. if (draw_breakpoints) {
  1774. set_line_as_breakpoint(p_line, !is_line_breakpointed(p_line));
  1775. }
  1776. return;
  1777. }
  1778. if (p_gutter == line_number_gutter) {
  1779. set_selection_mode(TextEdit::SelectionMode::SELECTION_MODE_LINE, p_line, 0);
  1780. select(p_line, 0, p_line + 1, 0);
  1781. cursor_set_line(p_line + 1);
  1782. cursor_set_column(0);
  1783. return;
  1784. }
  1785. if (p_gutter == fold_gutter) {
  1786. if (is_line_folded(p_line)) {
  1787. unfold_line(p_line);
  1788. } else if (can_fold_line(p_line)) {
  1789. fold_line(p_line);
  1790. }
  1791. return;
  1792. }
  1793. }
  1794. void CodeEdit::_update_gutter_indexes() {
  1795. for (int i = 0; i < get_gutter_count(); i++) {
  1796. if (get_gutter_name(i) == "main_gutter") {
  1797. main_gutter = i;
  1798. continue;
  1799. }
  1800. if (get_gutter_name(i) == "line_numbers") {
  1801. line_number_gutter = i;
  1802. continue;
  1803. }
  1804. if (get_gutter_name(i) == "fold_gutter") {
  1805. fold_gutter = i;
  1806. continue;
  1807. }
  1808. }
  1809. }
  1810. /* Delimiters */
  1811. void CodeEdit::_update_delimiter_cache(int p_from_line, int p_to_line) {
  1812. if (delimiters.size() == 0) {
  1813. return;
  1814. }
  1815. int line_count = get_line_count();
  1816. if (p_to_line == -1) {
  1817. p_to_line = line_count;
  1818. }
  1819. int start_line = MIN(p_from_line, p_to_line);
  1820. int end_line = MAX(p_from_line, p_to_line);
  1821. /* Make sure delimiter_cache has all the lines. */
  1822. if (start_line != end_line) {
  1823. if (p_to_line < p_from_line) {
  1824. for (int i = end_line; i > start_line; i--) {
  1825. delimiter_cache.remove(i);
  1826. }
  1827. } else {
  1828. for (int i = start_line; i < end_line; i++) {
  1829. delimiter_cache.insert(i, Map<int, int>());
  1830. }
  1831. }
  1832. }
  1833. int in_region = -1;
  1834. for (int i = start_line; i < MIN(end_line + 1, line_count); i++) {
  1835. int current_end_region = (i <= 0 || delimiter_cache[i].size() < 1) ? -1 : delimiter_cache[i].back()->value();
  1836. in_region = (i <= 0 || delimiter_cache[i - 1].size() < 1) ? -1 : delimiter_cache[i - 1].back()->value();
  1837. const String &str = get_line(i);
  1838. const int line_length = str.length();
  1839. delimiter_cache.write[i].clear();
  1840. if (str.length() == 0) {
  1841. if (in_region != -1) {
  1842. delimiter_cache.write[i][0] = in_region;
  1843. }
  1844. if (i == end_line && current_end_region != in_region) {
  1845. end_line++;
  1846. end_line = MIN(end_line, line_count);
  1847. }
  1848. continue;
  1849. }
  1850. int end_region = -1;
  1851. for (int j = 0; j < line_length; j++) {
  1852. int from = j;
  1853. for (; from < line_length; from++) {
  1854. if (str[from] == '\\') {
  1855. from++;
  1856. continue;
  1857. }
  1858. break;
  1859. }
  1860. /* check if we are in entering a region */
  1861. bool same_line = false;
  1862. if (in_region == -1) {
  1863. for (int d = 0; d < delimiters.size(); d++) {
  1864. /* check there is enough room */
  1865. int chars_left = line_length - from;
  1866. int start_key_length = delimiters[d].start_key.length();
  1867. int end_key_length = delimiters[d].end_key.length();
  1868. if (chars_left < start_key_length) {
  1869. continue;
  1870. }
  1871. /* search the line */
  1872. bool match = true;
  1873. const char32_t *start_key = delimiters[d].start_key.get_data();
  1874. for (int k = 0; k < start_key_length; k++) {
  1875. if (start_key[k] != str[from + k]) {
  1876. match = false;
  1877. break;
  1878. }
  1879. }
  1880. if (!match) {
  1881. continue;
  1882. }
  1883. same_line = true;
  1884. in_region = d;
  1885. delimiter_cache.write[i][from + 1] = d;
  1886. from += start_key_length;
  1887. /* check if it's the whole line */
  1888. if (end_key_length == 0 || delimiters[d].line_only || from + end_key_length > line_length) {
  1889. j = line_length;
  1890. if (delimiters[d].line_only) {
  1891. delimiter_cache.write[i][line_length + 1] = -1;
  1892. } else {
  1893. end_region = in_region;
  1894. }
  1895. }
  1896. break;
  1897. }
  1898. if (j == line_length || in_region == -1) {
  1899. continue;
  1900. }
  1901. }
  1902. /* if we are in one find the end key */
  1903. /* search the line */
  1904. int region_end_index = -1;
  1905. int end_key_length = delimiters[in_region].end_key.length();
  1906. const char32_t *end_key = delimiters[in_region].end_key.get_data();
  1907. for (; from < line_length; from++) {
  1908. if (line_length - from < end_key_length) {
  1909. break;
  1910. }
  1911. if (!is_symbol(str[from])) {
  1912. continue;
  1913. }
  1914. if (str[from] == '\\') {
  1915. from++;
  1916. continue;
  1917. }
  1918. region_end_index = from;
  1919. for (int k = 0; k < end_key_length; k++) {
  1920. if (end_key[k] != str[from + k]) {
  1921. region_end_index = -1;
  1922. break;
  1923. }
  1924. }
  1925. if (region_end_index != -1) {
  1926. break;
  1927. }
  1928. }
  1929. j = from + (end_key_length - 1);
  1930. end_region = (region_end_index == -1) ? in_region : -1;
  1931. if (!same_line || region_end_index != -1) {
  1932. delimiter_cache.write[i][j + 1] = end_region;
  1933. }
  1934. in_region = -1;
  1935. }
  1936. if (i == end_line && current_end_region != end_region) {
  1937. end_line++;
  1938. end_line = MIN(end_line, line_count);
  1939. }
  1940. }
  1941. }
  1942. int CodeEdit::_is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const {
  1943. if (delimiters.size() == 0) {
  1944. return -1;
  1945. }
  1946. ERR_FAIL_INDEX_V(p_line, get_line_count(), 0);
  1947. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  1948. bool in_region = region != -1 && delimiters[region].type == p_type;
  1949. for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  1950. /* If column is specified, loop until the key is larger then the column. */
  1951. if (p_column != -1) {
  1952. if (E->key() > p_column) {
  1953. break;
  1954. }
  1955. in_region = E->value() != -1 && delimiters[E->value()].type == p_type;
  1956. region = in_region ? E->value() : -1;
  1957. continue;
  1958. }
  1959. /* If no column, calculate if the entire line is a region */
  1960. /* excluding whitespace. */
  1961. const String line = get_line(p_line);
  1962. if (!in_region) {
  1963. if (E->value() == -1 || delimiters[E->value()].type != p_type) {
  1964. break;
  1965. }
  1966. region = E->value();
  1967. in_region = true;
  1968. for (int i = E->key() - 2; i >= 0; i--) {
  1969. if (!_is_whitespace(line[i])) {
  1970. return -1;
  1971. }
  1972. }
  1973. }
  1974. if (delimiters[region].line_only) {
  1975. return region;
  1976. }
  1977. int end_col = E->key();
  1978. if (E->value() != -1) {
  1979. if (!E->next()) {
  1980. return region;
  1981. }
  1982. end_col = E->next()->key();
  1983. }
  1984. for (int i = end_col; i < line.length(); i++) {
  1985. if (!_is_whitespace(line[i])) {
  1986. return -1;
  1987. }
  1988. }
  1989. return region;
  1990. }
  1991. return in_region ? region : -1;
  1992. }
  1993. void CodeEdit::_add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type) {
  1994. if (p_start_key.length() > 0) {
  1995. for (int i = 0; i < p_start_key.length(); i++) {
  1996. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "delimiter must start with a symbol");
  1997. }
  1998. }
  1999. if (p_end_key.length() > 0) {
  2000. for (int i = 0; i < p_end_key.length(); i++) {
  2001. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "delimiter must end with a symbol");
  2002. }
  2003. }
  2004. int at = 0;
  2005. for (int i = 0; i < delimiters.size(); i++) {
  2006. ERR_FAIL_COND_MSG(delimiters[i].start_key == p_start_key, "delimiter with start key '" + p_start_key + "' already exists.");
  2007. if (p_start_key.length() < delimiters[i].start_key.length()) {
  2008. at++;
  2009. }
  2010. }
  2011. Delimiter delimiter;
  2012. delimiter.type = p_type;
  2013. delimiter.start_key = p_start_key;
  2014. delimiter.end_key = p_end_key;
  2015. delimiter.line_only = p_line_only || p_end_key == "";
  2016. delimiters.insert(at, delimiter);
  2017. if (!setting_delimiters) {
  2018. delimiter_cache.clear();
  2019. _update_delimiter_cache();
  2020. }
  2021. }
  2022. void CodeEdit::_remove_delimiter(const String &p_start_key, DelimiterType p_type) {
  2023. for (int i = 0; i < delimiters.size(); i++) {
  2024. if (delimiters[i].start_key != p_start_key) {
  2025. continue;
  2026. }
  2027. if (delimiters[i].type != p_type) {
  2028. break;
  2029. }
  2030. delimiters.remove(i);
  2031. if (!setting_delimiters) {
  2032. delimiter_cache.clear();
  2033. _update_delimiter_cache();
  2034. }
  2035. break;
  2036. }
  2037. }
  2038. bool CodeEdit::_has_delimiter(const String &p_start_key, DelimiterType p_type) const {
  2039. for (int i = 0; i < delimiters.size(); i++) {
  2040. if (delimiters[i].start_key == p_start_key) {
  2041. return delimiters[i].type == p_type;
  2042. }
  2043. }
  2044. return false;
  2045. }
  2046. void CodeEdit::_set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type) {
  2047. setting_delimiters = true;
  2048. _clear_delimiters(p_type);
  2049. for (int i = 0; i < p_delimiters.size(); i++) {
  2050. String key = p_delimiters[i].is_null() ? "" : p_delimiters[i];
  2051. const String start_key = key.get_slice(" ", 0);
  2052. const String end_key = key.get_slice_count(" ") > 1 ? key.get_slice(" ", 1) : String();
  2053. _add_delimiter(start_key, end_key, end_key == "", p_type);
  2054. }
  2055. setting_delimiters = false;
  2056. _update_delimiter_cache();
  2057. }
  2058. void CodeEdit::_clear_delimiters(DelimiterType p_type) {
  2059. for (int i = delimiters.size() - 1; i >= 0; i--) {
  2060. if (delimiters[i].type == p_type) {
  2061. delimiters.remove(i);
  2062. }
  2063. }
  2064. delimiter_cache.clear();
  2065. if (!setting_delimiters) {
  2066. _update_delimiter_cache();
  2067. }
  2068. }
  2069. TypedArray<String> CodeEdit::_get_delimiters(DelimiterType p_type) const {
  2070. TypedArray<String> r_delimiters;
  2071. for (int i = 0; i < delimiters.size(); i++) {
  2072. if (delimiters[i].type != p_type) {
  2073. continue;
  2074. }
  2075. r_delimiters.push_back(delimiters[i].start_key + (delimiters[i].end_key.is_empty() ? "" : " " + delimiters[i].end_key));
  2076. }
  2077. return r_delimiters;
  2078. }
  2079. /* Code Completion */
  2080. void CodeEdit::_filter_code_completion_candidates() {
  2081. ScriptInstance *si = get_script_instance();
  2082. if (si && si->has_method("_filter_code_completion_candidates")) {
  2083. code_completion_options.clear();
  2084. code_completion_base = "";
  2085. /* Build options argument. */
  2086. TypedArray<Dictionary> completion_options_sources;
  2087. completion_options_sources.resize(code_completion_option_sources.size());
  2088. int i = 0;
  2089. for (const ScriptCodeCompletionOption &E : code_completion_option_sources) {
  2090. Dictionary option;
  2091. option["kind"] = E.kind;
  2092. option["display_text"] = E.display;
  2093. option["insert_text"] = E.insert_text;
  2094. option["font_color"] = E.font_color;
  2095. option["icon"] = E.icon;
  2096. option["default_value"] = E.default_value;
  2097. completion_options_sources[i] = option;
  2098. i++;
  2099. }
  2100. TypedArray<Dictionary> completion_options = si->call("_filter_code_completion_candidates", completion_options_sources);
  2101. /* No options to complete, cancel. */
  2102. if (completion_options.size() == 0) {
  2103. cancel_code_completion();
  2104. return;
  2105. }
  2106. /* Convert back into options. */
  2107. int max_width = 0;
  2108. for (i = 0; i < completion_options.size(); i++) {
  2109. ScriptCodeCompletionOption option;
  2110. option.kind = (ScriptCodeCompletionOption::Kind)(int)completion_options[i].get("kind");
  2111. option.display = completion_options[i].get("display_text");
  2112. option.insert_text = completion_options[i].get("insert_text");
  2113. option.font_color = completion_options[i].get("font_color");
  2114. option.icon = completion_options[i].get("icon");
  2115. option.default_value = completion_options[i].get("default_value");
  2116. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  2117. code_completion_options.push_back(option);
  2118. }
  2119. code_completion_longest_line = MIN(max_width, code_completion_max_width);
  2120. code_completion_current_selected = 0;
  2121. code_completion_active = true;
  2122. update();
  2123. return;
  2124. }
  2125. const int caret_line = cursor_get_line();
  2126. const int caret_column = cursor_get_column();
  2127. const String line = get_line(caret_line);
  2128. if (caret_column > 0 && line[caret_column - 1] == '(' && !code_completion_forced) {
  2129. cancel_code_completion();
  2130. return;
  2131. }
  2132. /* Get string status, are we in one or at the close. */
  2133. int in_string = is_in_string(caret_line, caret_column);
  2134. int first_quote_col = -1;
  2135. if (in_string != -1) {
  2136. Point2 string_start_pos = get_delimiter_start_position(caret_line, caret_column);
  2137. first_quote_col = (string_start_pos.y == caret_line) ? string_start_pos.x : -1;
  2138. } else if (caret_column > 0) {
  2139. if (is_in_string(caret_line, caret_column - 1) != -1) {
  2140. first_quote_col = caret_column - 1;
  2141. }
  2142. }
  2143. int cofs = caret_column;
  2144. String string_to_complete;
  2145. bool prev_is_word = false;
  2146. /* Cancel if we are at the close of a string. */
  2147. if (in_string == -1 && first_quote_col == cofs - 1) {
  2148. cancel_code_completion();
  2149. return;
  2150. /* In a string, therefore we are trying to complete the string text. */
  2151. } else if (in_string != -1 && first_quote_col != -1) {
  2152. int key_length = delimiters[in_string].start_key.length();
  2153. string_to_complete = line.substr(first_quote_col - key_length, (cofs - first_quote_col) + key_length);
  2154. /* If we have a space, previous word might be a keyword. eg "func |". */
  2155. } else if (cofs > 0 && line[cofs - 1] == ' ') {
  2156. int ofs = cofs - 1;
  2157. while (ofs >= 0 && line[ofs] == ' ') {
  2158. ofs--;
  2159. }
  2160. prev_is_word = _is_char(line[ofs]);
  2161. /* Otherwise get current word and set cofs to the start. */
  2162. } else {
  2163. int start_cofs = cofs;
  2164. while (cofs > 0 && line[cofs - 1] > 32 && (line[cofs - 1] == '/' || _is_char(line[cofs - 1]))) {
  2165. cofs--;
  2166. }
  2167. string_to_complete = line.substr(cofs, start_cofs - cofs);
  2168. }
  2169. /* If all else fails, check for a prefix. */
  2170. /* Single space between caret and prefix is okay. */
  2171. bool prev_is_prefix = false;
  2172. if (cofs > 0 && code_completion_prefixes.has(String::chr(line[cofs - 1]))) {
  2173. prev_is_prefix = true;
  2174. } else if (cofs > 1 && line[cofs - 1] == ' ' && code_completion_prefixes.has(String::chr(line[cofs - 2]))) {
  2175. prev_is_prefix = true;
  2176. }
  2177. if (!prev_is_word && string_to_complete.is_empty() && (cofs == 0 || !prev_is_prefix)) {
  2178. cancel_code_completion();
  2179. return;
  2180. }
  2181. /* Filter Options. */
  2182. /* For now handle only tradional quoted strings. */
  2183. bool single_quote = in_string != -1 && first_quote_col > 0 && delimiters[in_string].start_key == "'";
  2184. code_completion_options.clear();
  2185. code_completion_base = string_to_complete;
  2186. Vector<ScriptCodeCompletionOption> completion_options_casei;
  2187. Vector<ScriptCodeCompletionOption> completion_options_subseq;
  2188. Vector<ScriptCodeCompletionOption> completion_options_subseq_casei;
  2189. int max_width = 0;
  2190. String string_to_complete_lower = string_to_complete.to_lower();
  2191. for (ScriptCodeCompletionOption &option : code_completion_option_sources) {
  2192. if (single_quote && option.display.is_quoted()) {
  2193. option.display = option.display.unquote().quote("'");
  2194. }
  2195. if (in_string != -1) {
  2196. String quote = single_quote ? "'" : "\"";
  2197. option.display = option.display.unquote().quote(quote);
  2198. option.insert_text = option.insert_text.unquote().quote(quote);
  2199. }
  2200. if (option.display.length() == 0) {
  2201. continue;
  2202. }
  2203. if (string_to_complete.length() == 0) {
  2204. code_completion_options.push_back(option);
  2205. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  2206. continue;
  2207. }
  2208. /* This code works the same as:
  2209. if (option.display.begins_with(s)) {
  2210. completion_options.push_back(option);
  2211. } else if (option.display.to_lower().begins_with(s.to_lower())) {
  2212. completion_options_casei.push_back(option);
  2213. } else if (s.is_subsequence_of(option.display)) {
  2214. completion_options_subseq.push_back(option);
  2215. } else if (s.is_subsequence_ofi(option.display)) {
  2216. completion_options_subseq_casei.push_back(option);
  2217. }
  2218. But is more performant due to being inlined and looping over the characters only once
  2219. */
  2220. String display_lower = option.display.to_lower();
  2221. const char32_t *ssq = &string_to_complete[0];
  2222. const char32_t *ssq_lower = &string_to_complete_lower[0];
  2223. const char32_t *tgt = &option.display[0];
  2224. const char32_t *tgt_lower = &display_lower[0];
  2225. const char32_t *ssq_last_tgt = nullptr;
  2226. const char32_t *ssq_lower_last_tgt = nullptr;
  2227. for (; *tgt; tgt++, tgt_lower++) {
  2228. if (*ssq == *tgt) {
  2229. ssq++;
  2230. ssq_last_tgt = tgt;
  2231. }
  2232. if (*ssq_lower == *tgt_lower) {
  2233. ssq_lower++;
  2234. ssq_lower_last_tgt = tgt;
  2235. }
  2236. }
  2237. /* Matched the whole subsequence in s. */
  2238. if (!*ssq) {
  2239. /* Finished matching in the first s.length() characters. */
  2240. if (ssq_last_tgt == &option.display[string_to_complete.length() - 1]) {
  2241. code_completion_options.push_back(option);
  2242. } else {
  2243. completion_options_subseq.push_back(option);
  2244. }
  2245. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  2246. /* Matched the whole subsequence in s_lower. */
  2247. } else if (!*ssq_lower) {
  2248. /* Finished matching in the first s.length() characters. */
  2249. if (ssq_lower_last_tgt == &option.display[string_to_complete.length() - 1]) {
  2250. completion_options_casei.push_back(option);
  2251. } else {
  2252. completion_options_subseq_casei.push_back(option);
  2253. }
  2254. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  2255. }
  2256. }
  2257. code_completion_options.append_array(completion_options_casei);
  2258. code_completion_options.append_array(completion_options_subseq);
  2259. code_completion_options.append_array(completion_options_subseq_casei);
  2260. /* No options to complete, cancel. */
  2261. if (code_completion_options.size() == 0) {
  2262. cancel_code_completion();
  2263. return;
  2264. }
  2265. /* A perfect match, stop completion. */
  2266. if (code_completion_options.size() == 1 && string_to_complete == code_completion_options[0].display) {
  2267. cancel_code_completion();
  2268. return;
  2269. }
  2270. code_completion_longest_line = MIN(max_width, code_completion_max_width);
  2271. code_completion_current_selected = 0;
  2272. code_completion_active = true;
  2273. update();
  2274. }
  2275. void CodeEdit::_lines_edited_from(int p_from_line, int p_to_line) {
  2276. _update_delimiter_cache(p_from_line, p_to_line);
  2277. if (p_from_line == p_to_line) {
  2278. return;
  2279. }
  2280. int lc = get_line_count();
  2281. line_number_digits = 1;
  2282. while (lc /= 10) {
  2283. line_number_digits++;
  2284. }
  2285. set_gutter_width(line_number_gutter, (line_number_digits + 1) * cache.font->get_char_size('0', 0, cache.font_size).width);
  2286. int from_line = MIN(p_from_line, p_to_line);
  2287. int line_count = (p_to_line - p_from_line);
  2288. List<int> breakpoints;
  2289. breakpointed_lines.get_key_list(&breakpoints);
  2290. for (const int line : breakpoints) {
  2291. if (line <= from_line) {
  2292. continue;
  2293. }
  2294. breakpointed_lines.erase(line);
  2295. emit_signal(SNAME("breakpoint_toggled"), line);
  2296. if (line_count > 0 || line >= p_from_line) {
  2297. emit_signal(SNAME("breakpoint_toggled"), line + line_count);
  2298. breakpointed_lines[line + line_count] = true;
  2299. continue;
  2300. }
  2301. }
  2302. }
  2303. CodeEdit::CodeEdit() {
  2304. /* Indent management */
  2305. auto_indent_prefixes.insert(':');
  2306. auto_indent_prefixes.insert('{');
  2307. auto_indent_prefixes.insert('[');
  2308. auto_indent_prefixes.insert('(');
  2309. /* Auto brace completion */
  2310. add_auto_brace_completion_pair("(", ")");
  2311. add_auto_brace_completion_pair("{", "}");
  2312. add_auto_brace_completion_pair("[", "]");
  2313. add_auto_brace_completion_pair("\"", "\"");
  2314. add_auto_brace_completion_pair("\'", "\'");
  2315. /* Text Direction */
  2316. set_layout_direction(LAYOUT_DIRECTION_LTR);
  2317. set_text_direction(TEXT_DIRECTION_LTR);
  2318. /* Gutters */
  2319. int gutter_idx = 0;
  2320. /* Main Gutter */
  2321. add_gutter();
  2322. set_gutter_name(gutter_idx, "main_gutter");
  2323. set_gutter_draw(gutter_idx, false);
  2324. set_gutter_overwritable(gutter_idx, true);
  2325. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  2326. set_gutter_custom_draw(gutter_idx, this, "_main_gutter_draw_callback");
  2327. gutter_idx++;
  2328. /* Line numbers */
  2329. add_gutter();
  2330. set_gutter_name(gutter_idx, "line_numbers");
  2331. set_gutter_draw(gutter_idx, false);
  2332. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  2333. set_gutter_custom_draw(gutter_idx, this, "_line_number_draw_callback");
  2334. gutter_idx++;
  2335. /* Fold Gutter */
  2336. add_gutter();
  2337. set_gutter_name(gutter_idx, "fold_gutter");
  2338. set_gutter_draw(gutter_idx, false);
  2339. set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
  2340. set_gutter_custom_draw(gutter_idx, this, "_fold_gutter_draw_callback");
  2341. gutter_idx++;
  2342. connect("lines_edited_from", callable_mp(this, &CodeEdit::_lines_edited_from));
  2343. connect("gutter_clicked", callable_mp(this, &CodeEdit::_gutter_clicked));
  2344. connect("gutter_added", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  2345. connect("gutter_removed", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  2346. _update_gutter_indexes();
  2347. }
  2348. CodeEdit::~CodeEdit() {
  2349. }