code_edit.cpp 136 KB

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