code_edit.cpp 131 KB

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