code_edit.cpp 96 KB

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