code_edit.cpp 126 KB

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