code_edit.cpp 134 KB

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