code_edit.cpp 128 KB

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