code_edit.cpp 101 KB

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