code_edit.cpp 117 KB

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