code_edit.cpp 110 KB

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