code_edit.cpp 86 KB

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