code_edit.cpp 100 KB

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