code_edit.cpp 128 KB

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