code_edit.cpp 117 KB

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