code_edit.cpp 110 KB

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