code_edit.cpp 133 KB

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