code_edit.cpp 117 KB

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