code_edit.cpp 105 KB

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