code_edit.cpp 128 KB

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