code_edit.cpp 132 KB

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