code_edit.cpp 128 KB

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