code_edit.cpp 127 KB

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