code_edit.cpp 112 KB

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