code_edit.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  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("breakpoint_color");
  48. breakpoint_icon = get_theme_icon("breakpoint");
  49. bookmark_color = get_theme_color("bookmark_color");
  50. bookmark_icon = get_theme_icon("bookmark");
  51. executing_line_color = get_theme_color("executing_line_color");
  52. executing_line_icon = get_theme_icon("executing_line");
  53. line_number_color = get_theme_color("line_number_color");
  54. folding_color = get_theme_color("code_folding_color");
  55. can_fold_icon = get_theme_icon("can_fold");
  56. folded_icon = get_theme_icon("folded");
  57. code_completion_max_width = get_theme_constant("completion_max_width") * cache.font->get_char_size('x').x;
  58. code_completion_max_lines = get_theme_constant("completion_lines");
  59. code_completion_scroll_width = get_theme_constant("completion_scroll_width");
  60. code_completion_scroll_color = get_theme_color("completion_scroll_color");
  61. code_completion_background_color = get_theme_color("completion_background_color");
  62. code_completion_selected_color = get_theme_color("completion_selected_color");
  63. code_completion_existing_color = get_theme_color("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("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("hseparation", "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.instance();
  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("panel", "TooltipPanel");
  143. Color font_color = get_theme_color("font_color", "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. if (code_completion_active && code_completion_rect.has_point(mb->get_position())) {
  188. if (!mb->is_pressed()) {
  189. return;
  190. }
  191. switch (mb->get_button_index()) {
  192. case MOUSE_BUTTON_WHEEL_UP: {
  193. if (code_completion_current_selected > 0) {
  194. code_completion_current_selected--;
  195. update();
  196. }
  197. } break;
  198. case MOUSE_BUTTON_WHEEL_DOWN: {
  199. if (code_completion_current_selected < code_completion_options.size() - 1) {
  200. code_completion_current_selected++;
  201. update();
  202. }
  203. } break;
  204. case MOUSE_BUTTON_LEFT: {
  205. 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);
  206. if (mb->is_double_click()) {
  207. confirm_code_completion();
  208. }
  209. update();
  210. } break;
  211. }
  212. return;
  213. }
  214. cancel_code_completion();
  215. set_code_hint("");
  216. }
  217. Ref<InputEventKey> k = p_gui_input;
  218. bool update_code_completion = false;
  219. if (!k.is_valid()) {
  220. TextEdit::_gui_input(p_gui_input);
  221. return;
  222. }
  223. /* If a modifier has been pressed, and nothing else, return. */
  224. if (!k->is_pressed() || k->get_keycode() == KEY_CTRL || k->get_keycode() == KEY_ALT || k->get_keycode() == KEY_SHIFT || k->get_keycode() == KEY_META) {
  225. return;
  226. }
  227. /* Allow unicode handling if: */
  228. /* No Modifiers are pressed (except shift) */
  229. bool allow_unicode_handling = !(k->is_command_pressed() || k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());
  230. /* AUTO-COMPLETE */
  231. if (code_completion_enabled && k->is_action("ui_text_completion_query", true)) {
  232. request_code_completion(true);
  233. accept_event();
  234. return;
  235. }
  236. if (code_completion_active) {
  237. if (k->is_action("ui_up", true)) {
  238. if (code_completion_current_selected > 0) {
  239. code_completion_current_selected--;
  240. } else {
  241. code_completion_current_selected = code_completion_options.size() - 1;
  242. }
  243. update();
  244. accept_event();
  245. return;
  246. }
  247. if (k->is_action("ui_down", true)) {
  248. if (code_completion_current_selected < code_completion_options.size() - 1) {
  249. code_completion_current_selected++;
  250. } else {
  251. code_completion_current_selected = 0;
  252. }
  253. update();
  254. accept_event();
  255. return;
  256. }
  257. if (k->is_action("ui_page_up", true)) {
  258. code_completion_current_selected = MAX(0, code_completion_current_selected - code_completion_max_lines);
  259. update();
  260. accept_event();
  261. return;
  262. }
  263. if (k->is_action("ui_page_down", true)) {
  264. code_completion_current_selected = MIN(code_completion_options.size() - 1, code_completion_current_selected + code_completion_max_lines);
  265. update();
  266. accept_event();
  267. return;
  268. }
  269. if (k->is_action("ui_home", true)) {
  270. code_completion_current_selected = 0;
  271. update();
  272. accept_event();
  273. return;
  274. }
  275. if (k->is_action("ui_end", true)) {
  276. code_completion_current_selected = MIN(code_completion_options.size() - 1, code_completion_current_selected + code_completion_max_lines);
  277. update();
  278. accept_event();
  279. return;
  280. }
  281. if (k->is_action("ui_text_completion_replace", true) || k->is_action("ui_text_completion_accept", true)) {
  282. confirm_code_completion(k->is_action("ui_text_completion_replace", true));
  283. accept_event();
  284. return;
  285. }
  286. if (k->is_action("ui_cancel", true)) {
  287. cancel_code_completion();
  288. accept_event();
  289. return;
  290. }
  291. if (k->is_action("ui_text_backspace", true)) {
  292. backspace_at_cursor();
  293. _filter_code_completion_candidates();
  294. accept_event();
  295. return;
  296. }
  297. if (k->is_action("ui_left", true) || k->is_action("ui_right", true)) {
  298. update_code_completion = true;
  299. } else {
  300. update_code_completion = (allow_unicode_handling && k->get_unicode() >= 32);
  301. }
  302. if (!update_code_completion) {
  303. cancel_code_completion();
  304. }
  305. }
  306. /* MISC */
  307. if (k->is_action("ui_cancel", true)) {
  308. set_code_hint("");
  309. accept_event();
  310. return;
  311. }
  312. if (allow_unicode_handling && k->get_unicode() == ')') {
  313. set_code_hint("");
  314. }
  315. /* Remove shift otherwise actions will not match. */
  316. k = k->duplicate();
  317. k->set_shift_pressed(false);
  318. if (k->is_action("ui_text_caret_up", true) ||
  319. k->is_action("ui_text_caret_down", true) ||
  320. k->is_action("ui_text_caret_line_start", true) ||
  321. k->is_action("ui_text_caret_line_end", true) ||
  322. k->is_action("ui_text_caret_page_up", true) ||
  323. k->is_action("ui_text_caret_page_down", true)) {
  324. set_code_hint("");
  325. }
  326. TextEdit::_gui_input(p_gui_input);
  327. if (update_code_completion) {
  328. _filter_code_completion_candidates();
  329. }
  330. }
  331. Control::CursorShape CodeEdit::get_cursor_shape(const Point2 &p_pos) const {
  332. if ((code_completion_active && code_completion_rect.has_point(p_pos)) || (is_readonly() && (!is_selecting_enabled() || get_line_count() == 0))) {
  333. return CURSOR_ARROW;
  334. }
  335. return TextEdit::get_cursor_shape(p_pos);
  336. }
  337. /* Main Gutter */
  338. void CodeEdit::_update_draw_main_gutter() {
  339. set_gutter_draw(main_gutter, draw_breakpoints || draw_bookmarks || draw_executing_lines);
  340. }
  341. void CodeEdit::set_draw_breakpoints_gutter(bool p_draw) {
  342. draw_breakpoints = p_draw;
  343. set_gutter_clickable(main_gutter, p_draw);
  344. _update_draw_main_gutter();
  345. }
  346. bool CodeEdit::is_drawing_breakpoints_gutter() const {
  347. return draw_breakpoints;
  348. }
  349. void CodeEdit::set_draw_bookmarks_gutter(bool p_draw) {
  350. draw_bookmarks = p_draw;
  351. _update_draw_main_gutter();
  352. }
  353. bool CodeEdit::is_drawing_bookmarks_gutter() const {
  354. return draw_bookmarks;
  355. }
  356. void CodeEdit::set_draw_executing_lines_gutter(bool p_draw) {
  357. draw_executing_lines = p_draw;
  358. _update_draw_main_gutter();
  359. }
  360. bool CodeEdit::is_drawing_executing_lines_gutter() const {
  361. return draw_executing_lines;
  362. }
  363. void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  364. if (draw_breakpoints && is_line_breakpointed(p_line)) {
  365. int padding = p_region.size.x / 6;
  366. Rect2 breakpoint_region = p_region;
  367. breakpoint_region.position += Point2(padding, padding);
  368. breakpoint_region.size -= Point2(padding, padding) * 2;
  369. breakpoint_icon->draw_rect(get_canvas_item(), breakpoint_region, false, breakpoint_color);
  370. }
  371. if (draw_bookmarks && is_line_bookmarked(p_line)) {
  372. int horizontal_padding = p_region.size.x / 2;
  373. int vertical_padding = p_region.size.y / 4;
  374. Rect2 bookmark_region = p_region;
  375. bookmark_region.position += Point2(horizontal_padding, 0);
  376. bookmark_region.size -= Point2(horizontal_padding * 1.1, vertical_padding);
  377. bookmark_icon->draw_rect(get_canvas_item(), bookmark_region, false, bookmark_color);
  378. }
  379. if (draw_executing_lines && is_line_executing(p_line)) {
  380. int horizontal_padding = p_region.size.x / 10;
  381. int vertical_padding = p_region.size.y / 4;
  382. Rect2 executing_line_region = p_region;
  383. executing_line_region.position += Point2(horizontal_padding, vertical_padding);
  384. executing_line_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  385. executing_line_icon->draw_rect(get_canvas_item(), executing_line_region, false, executing_line_color);
  386. }
  387. }
  388. // Breakpoints
  389. void CodeEdit::set_line_as_breakpoint(int p_line, bool p_breakpointed) {
  390. int mask = get_line_gutter_metadata(p_line, main_gutter);
  391. set_line_gutter_metadata(p_line, main_gutter, p_breakpointed ? mask | MAIN_GUTTER_BREAKPOINT : mask & ~MAIN_GUTTER_BREAKPOINT);
  392. if (p_breakpointed) {
  393. breakpointed_lines[p_line] = true;
  394. } else if (breakpointed_lines.has(p_line)) {
  395. breakpointed_lines.erase(p_line);
  396. }
  397. emit_signal("breakpoint_toggled", p_line);
  398. update();
  399. }
  400. bool CodeEdit::is_line_breakpointed(int p_line) const {
  401. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BREAKPOINT;
  402. }
  403. void CodeEdit::clear_breakpointed_lines() {
  404. for (int i = 0; i < get_line_count(); i++) {
  405. if (is_line_breakpointed(i)) {
  406. set_line_as_breakpoint(i, false);
  407. }
  408. }
  409. }
  410. Array CodeEdit::get_breakpointed_lines() const {
  411. Array ret;
  412. for (int i = 0; i < get_line_count(); i++) {
  413. if (is_line_breakpointed(i)) {
  414. ret.append(i);
  415. }
  416. }
  417. return ret;
  418. }
  419. // Bookmarks
  420. void CodeEdit::set_line_as_bookmarked(int p_line, bool p_bookmarked) {
  421. int mask = get_line_gutter_metadata(p_line, main_gutter);
  422. set_line_gutter_metadata(p_line, main_gutter, p_bookmarked ? mask | MAIN_GUTTER_BOOKMARK : mask & ~MAIN_GUTTER_BOOKMARK);
  423. update();
  424. }
  425. bool CodeEdit::is_line_bookmarked(int p_line) const {
  426. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BOOKMARK;
  427. }
  428. void CodeEdit::clear_bookmarked_lines() {
  429. for (int i = 0; i < get_line_count(); i++) {
  430. if (is_line_bookmarked(i)) {
  431. set_line_as_bookmarked(i, false);
  432. }
  433. }
  434. }
  435. Array CodeEdit::get_bookmarked_lines() const {
  436. Array ret;
  437. for (int i = 0; i < get_line_count(); i++) {
  438. if (is_line_bookmarked(i)) {
  439. ret.append(i);
  440. }
  441. }
  442. return ret;
  443. }
  444. // executing lines
  445. void CodeEdit::set_line_as_executing(int p_line, bool p_executing) {
  446. int mask = get_line_gutter_metadata(p_line, main_gutter);
  447. set_line_gutter_metadata(p_line, main_gutter, p_executing ? mask | MAIN_GUTTER_EXECUTING : mask & ~MAIN_GUTTER_EXECUTING);
  448. update();
  449. }
  450. bool CodeEdit::is_line_executing(int p_line) const {
  451. return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_EXECUTING;
  452. }
  453. void CodeEdit::clear_executing_lines() {
  454. for (int i = 0; i < get_line_count(); i++) {
  455. if (is_line_executing(i)) {
  456. set_line_as_executing(i, false);
  457. }
  458. }
  459. }
  460. Array CodeEdit::get_executing_lines() const {
  461. Array ret;
  462. for (int i = 0; i < get_line_count(); i++) {
  463. if (is_line_executing(i)) {
  464. ret.append(i);
  465. }
  466. }
  467. return ret;
  468. }
  469. /* Line numbers */
  470. void CodeEdit::set_draw_line_numbers(bool p_draw) {
  471. set_gutter_draw(line_number_gutter, p_draw);
  472. }
  473. bool CodeEdit::is_draw_line_numbers_enabled() const {
  474. return is_gutter_drawn(line_number_gutter);
  475. }
  476. void CodeEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
  477. p_zero_padded ? line_number_padding = "0" : line_number_padding = " ";
  478. update();
  479. }
  480. bool CodeEdit::is_line_numbers_zero_padded() const {
  481. return line_number_padding == "0";
  482. }
  483. void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
  484. String fc = TS->format_number(String::num(p_line + 1).lpad(line_number_digits, line_number_padding));
  485. Ref<TextLine> tl;
  486. tl.instance();
  487. tl->add_string(fc, cache.font, cache.font_size);
  488. int yofs = p_region.position.y + (get_row_height() - tl->get_size().y) / 2;
  489. Color number_color = get_line_gutter_item_color(p_line, line_number_gutter);
  490. if (number_color == Color(1, 1, 1)) {
  491. number_color = line_number_color;
  492. }
  493. tl->draw(get_canvas_item(), Point2(p_region.position.x, yofs), number_color);
  494. }
  495. /* Fold Gutter */
  496. void CodeEdit::set_draw_fold_gutter(bool p_draw) {
  497. set_gutter_draw(fold_gutter, p_draw);
  498. }
  499. bool CodeEdit::is_drawing_fold_gutter() const {
  500. return is_gutter_drawn(fold_gutter);
  501. }
  502. void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region) {
  503. if (!can_fold(p_line) && !is_folded(p_line)) {
  504. set_line_gutter_clickable(p_line, fold_gutter, false);
  505. return;
  506. }
  507. set_line_gutter_clickable(p_line, fold_gutter, true);
  508. int horizontal_padding = p_region.size.x / 10;
  509. int vertical_padding = p_region.size.y / 6;
  510. p_region.position += Point2(horizontal_padding, vertical_padding);
  511. p_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
  512. if (can_fold(p_line)) {
  513. can_fold_icon->draw_rect(get_canvas_item(), p_region, false, folding_color);
  514. return;
  515. }
  516. folded_icon->draw_rect(get_canvas_item(), p_region, false, folding_color);
  517. }
  518. /* Delimiters */
  519. // Strings
  520. void CodeEdit::add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  521. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_STRING);
  522. }
  523. void CodeEdit::remove_string_delimiter(const String &p_start_key) {
  524. _remove_delimiter(p_start_key, TYPE_STRING);
  525. }
  526. bool CodeEdit::has_string_delimiter(const String &p_start_key) const {
  527. return _has_delimiter(p_start_key, TYPE_STRING);
  528. }
  529. void CodeEdit::set_string_delimiters(const TypedArray<String> &p_string_delimiters) {
  530. _set_delimiters(p_string_delimiters, TYPE_STRING);
  531. }
  532. void CodeEdit::clear_string_delimiters() {
  533. _clear_delimiters(TYPE_STRING);
  534. }
  535. TypedArray<String> CodeEdit::get_string_delimiters() const {
  536. return _get_delimiters(TYPE_STRING);
  537. }
  538. int CodeEdit::is_in_string(int p_line, int p_column) const {
  539. return _is_in_delimiter(p_line, p_column, TYPE_STRING);
  540. }
  541. // Comments
  542. void CodeEdit::add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
  543. _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_COMMENT);
  544. }
  545. void CodeEdit::remove_comment_delimiter(const String &p_start_key) {
  546. _remove_delimiter(p_start_key, TYPE_COMMENT);
  547. }
  548. bool CodeEdit::has_comment_delimiter(const String &p_start_key) const {
  549. return _has_delimiter(p_start_key, TYPE_COMMENT);
  550. }
  551. void CodeEdit::set_comment_delimiters(const TypedArray<String> &p_comment_delimiters) {
  552. _set_delimiters(p_comment_delimiters, TYPE_COMMENT);
  553. }
  554. void CodeEdit::clear_comment_delimiters() {
  555. _clear_delimiters(TYPE_COMMENT);
  556. }
  557. TypedArray<String> CodeEdit::get_comment_delimiters() const {
  558. return _get_delimiters(TYPE_COMMENT);
  559. }
  560. int CodeEdit::is_in_comment(int p_line, int p_column) const {
  561. return _is_in_delimiter(p_line, p_column, TYPE_COMMENT);
  562. }
  563. String CodeEdit::get_delimiter_start_key(int p_delimiter_idx) const {
  564. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  565. return delimiters[p_delimiter_idx].start_key;
  566. }
  567. String CodeEdit::get_delimiter_end_key(int p_delimiter_idx) const {
  568. ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
  569. return delimiters[p_delimiter_idx].end_key;
  570. }
  571. Point2 CodeEdit::get_delimiter_start_position(int p_line, int p_column) const {
  572. if (delimiters.size() == 0) {
  573. return Point2(-1, -1);
  574. }
  575. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  576. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  577. Point2 start_position;
  578. start_position.y = -1;
  579. start_position.x = -1;
  580. bool in_region = ((p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value()) != -1;
  581. /* Check the keys for this line. */
  582. for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  583. if (E->key() > p_column) {
  584. break;
  585. }
  586. in_region = E->value() != -1;
  587. start_position.x = in_region ? E->key() : -1;
  588. }
  589. /* Region was found on this line and is not a multiline continuation. */
  590. if (start_position.x != -1 && start_position.x != get_line(p_line).length() + 1) {
  591. start_position.y = p_line;
  592. return start_position;
  593. }
  594. /* Not in a region */
  595. if (!in_region) {
  596. return start_position;
  597. }
  598. /* Region starts on a previous line */
  599. for (int i = p_line - 1; i >= 0; i--) {
  600. if (delimiter_cache[i].size() < 1) {
  601. continue;
  602. }
  603. start_position.y = i;
  604. start_position.x = delimiter_cache[i].back()->key();
  605. /* Make sure it's not a multiline continuation. */
  606. if (start_position.x != get_line(i).length() + 1) {
  607. break;
  608. }
  609. }
  610. return start_position;
  611. }
  612. Point2 CodeEdit::get_delimiter_end_position(int p_line, int p_column) const {
  613. if (delimiters.size() == 0) {
  614. return Point2(-1, -1);
  615. }
  616. ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
  617. ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
  618. Point2 end_position;
  619. end_position.y = -1;
  620. end_position.x = -1;
  621. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  622. /* Check the keys for this line. */
  623. for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  624. end_position.x = (E->value() == -1) ? E->key() : -1;
  625. if (E->key() > p_column) {
  626. break;
  627. }
  628. region = E->value();
  629. }
  630. /* Region was found on this line and is not a multiline continuation. */
  631. if (region != -1 && end_position.x != -1 && (delimiters[region].line_only || end_position.x != get_line(p_line).length() + 1)) {
  632. end_position.y = p_line;
  633. return end_position;
  634. }
  635. /* Not in a region */
  636. if (region == -1) {
  637. end_position.x = -1;
  638. return end_position;
  639. }
  640. /* Region ends on a later line */
  641. for (int i = p_line + 1; i < get_line_count(); i++) {
  642. if (delimiter_cache[i].size() < 1 || delimiter_cache[i].front()->value() != -1) {
  643. continue;
  644. }
  645. end_position.x = delimiter_cache[i].front()->key();
  646. /* Make sure it's not a multiline continuation. */
  647. if (get_line(i).length() > 0 && end_position.x != get_line(i).length() + 1) {
  648. end_position.y = i;
  649. break;
  650. }
  651. end_position.x = -1;
  652. }
  653. return end_position;
  654. }
  655. /* Code hint */
  656. void CodeEdit::set_code_hint(const String &p_hint) {
  657. code_hint = p_hint;
  658. code_hint_xpos = -0xFFFF;
  659. update();
  660. }
  661. void CodeEdit::set_code_hint_draw_below(bool p_below) {
  662. code_hint_draw_below = p_below;
  663. update();
  664. }
  665. /* Code Completion */
  666. void CodeEdit::set_code_completion_enabled(bool p_enable) {
  667. code_completion_enabled = p_enable;
  668. }
  669. bool CodeEdit::is_code_completion_enabled() const {
  670. return code_completion_enabled;
  671. }
  672. void CodeEdit::set_code_completion_prefixes(const TypedArray<String> &p_prefixes) {
  673. code_completion_prefixes.clear();
  674. for (int i = 0; i < p_prefixes.size(); i++) {
  675. code_completion_prefixes.insert(p_prefixes[i]);
  676. }
  677. }
  678. TypedArray<String> CodeEdit::get_code_completion_prefixes() const {
  679. TypedArray<String> prefixes;
  680. for (Set<String>::Element *E = code_completion_prefixes.front(); E; E = E->next()) {
  681. prefixes.push_back(E->get());
  682. }
  683. return prefixes;
  684. }
  685. String CodeEdit::get_text_for_code_completion() const {
  686. StringBuilder completion_text;
  687. const int text_size = get_line_count();
  688. for (int i = 0; i < text_size; i++) {
  689. String line = get_line(i);
  690. if (i == cursor_get_line()) {
  691. completion_text += line.substr(0, cursor_get_column());
  692. /* Not unicode, represents the caret. */
  693. completion_text += String::chr(0xFFFF);
  694. completion_text += line.substr(cursor_get_column(), line.size());
  695. } else {
  696. completion_text += line;
  697. }
  698. if (i != text_size - 1) {
  699. completion_text += "\n";
  700. }
  701. }
  702. return completion_text.as_string();
  703. }
  704. void CodeEdit::request_code_completion(bool p_force) {
  705. ScriptInstance *si = get_script_instance();
  706. if (si && si->has_method("_request_code_completion")) {
  707. si->call("_request_code_completion", p_force);
  708. return;
  709. }
  710. /* Don't re-query if all existing options are quoted types, eg path, signal. */
  711. bool ignored = code_completion_active && !code_completion_options.is_empty();
  712. if (ignored) {
  713. ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_PLAIN_TEXT;
  714. const ScriptCodeCompletionOption *previous_option = nullptr;
  715. for (int i = 0; i < code_completion_options.size(); i++) {
  716. const ScriptCodeCompletionOption &current_option = code_completion_options[i];
  717. if (!previous_option) {
  718. previous_option = &current_option;
  719. kind = current_option.kind;
  720. }
  721. if (previous_option->kind != current_option.kind) {
  722. ignored = false;
  723. break;
  724. }
  725. }
  726. ignored = ignored && (kind == ScriptCodeCompletionOption::KIND_FILE_PATH || kind == ScriptCodeCompletionOption::KIND_NODE_PATH || kind == ScriptCodeCompletionOption::KIND_SIGNAL);
  727. }
  728. if (ignored) {
  729. return;
  730. }
  731. if (p_force) {
  732. emit_signal("request_code_completion");
  733. return;
  734. }
  735. String line = get_line(cursor_get_line());
  736. int ofs = CLAMP(cursor_get_column(), 0, line.length());
  737. 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])))) {
  738. emit_signal("request_code_completion");
  739. } else if (ofs > 1 && line[ofs - 1] == ' ' && code_completion_prefixes.has(String::chr(line[ofs - 2]))) {
  740. emit_signal("request_code_completion");
  741. }
  742. }
  743. 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) {
  744. ScriptCodeCompletionOption completion_option;
  745. completion_option.kind = (ScriptCodeCompletionOption::Kind)p_type;
  746. completion_option.display = p_display_text;
  747. completion_option.insert_text = p_insert_text;
  748. completion_option.font_color = p_text_color;
  749. completion_option.icon = p_icon;
  750. completion_option.default_value = p_value;
  751. code_completion_option_submitted.push_back(completion_option);
  752. }
  753. void CodeEdit::update_code_completion_options(bool p_forced) {
  754. code_completion_forced = p_forced;
  755. code_completion_option_sources = code_completion_option_submitted;
  756. code_completion_option_submitted.clear();
  757. _filter_code_completion_candidates();
  758. }
  759. TypedArray<Dictionary> CodeEdit::get_code_completion_options() const {
  760. if (!code_completion_active) {
  761. return TypedArray<Dictionary>();
  762. }
  763. TypedArray<Dictionary> completion_options;
  764. completion_options.resize(code_completion_options.size());
  765. for (int i = 0; i < code_completion_options.size(); i++) {
  766. Dictionary option;
  767. option["kind"] = code_completion_options[i].kind;
  768. option["display_text"] = code_completion_options[i].display;
  769. option["insert_text"] = code_completion_options[i].insert_text;
  770. option["font_color"] = code_completion_options[i].font_color;
  771. option["icon"] = code_completion_options[i].icon;
  772. option["default_value"] = code_completion_options[i].default_value;
  773. completion_options[i] = option;
  774. }
  775. return completion_options;
  776. }
  777. Dictionary CodeEdit::get_code_completion_option(int p_index) const {
  778. if (!code_completion_active) {
  779. return Dictionary();
  780. }
  781. ERR_FAIL_INDEX_V(p_index, code_completion_options.size(), Dictionary());
  782. Dictionary option;
  783. option["kind"] = code_completion_options[p_index].kind;
  784. option["display_text"] = code_completion_options[p_index].display;
  785. option["insert_text"] = code_completion_options[p_index].insert_text;
  786. option["font_color"] = code_completion_options[p_index].font_color;
  787. option["icon"] = code_completion_options[p_index].icon;
  788. option["default_value"] = code_completion_options[p_index].default_value;
  789. return option;
  790. }
  791. int CodeEdit::get_code_completion_selected_index() const {
  792. return (code_completion_active) ? code_completion_current_selected : -1;
  793. }
  794. void CodeEdit::set_code_completion_selected_index(int p_index) {
  795. if (!code_completion_active) {
  796. return;
  797. }
  798. ERR_FAIL_INDEX(p_index, code_completion_options.size());
  799. code_completion_current_selected = p_index;
  800. update();
  801. }
  802. void CodeEdit::confirm_code_completion(bool p_replace) {
  803. if (is_readonly() || !code_completion_active) {
  804. return;
  805. }
  806. ScriptInstance *si = get_script_instance();
  807. if (si && si->has_method("_confirm_code_completion")) {
  808. si->call("_confirm_code_completion", p_replace);
  809. return;
  810. }
  811. begin_complex_operation();
  812. int caret_line = cursor_get_line();
  813. const String &insert_text = code_completion_options[code_completion_current_selected].insert_text;
  814. const String &display_text = code_completion_options[code_completion_current_selected].display;
  815. if (p_replace) {
  816. /* Find end of current section */
  817. const String line = get_line(caret_line);
  818. int caret_col = cursor_get_column();
  819. int caret_remove_line = caret_line;
  820. bool merge_text = true;
  821. int in_string = is_in_string(caret_line, caret_col);
  822. if (in_string != -1) {
  823. Point2 string_end = get_delimiter_end_position(caret_line, caret_col);
  824. if (string_end.x != -1) {
  825. merge_text = false;
  826. caret_remove_line = string_end.y;
  827. caret_col = string_end.x - 1;
  828. }
  829. }
  830. if (merge_text) {
  831. for (; caret_col < line.length(); caret_col++) {
  832. if (!_is_char(line[caret_col])) {
  833. break;
  834. }
  835. }
  836. }
  837. /* Replace. */
  838. _remove_text(caret_line, cursor_get_column() - code_completion_base.length(), caret_remove_line, caret_col);
  839. cursor_set_column(cursor_get_column() - code_completion_base.length(), false);
  840. insert_text_at_cursor(insert_text);
  841. } else {
  842. /* Get first non-matching char. */
  843. const String line = get_line(caret_line);
  844. int caret_col = cursor_get_column();
  845. int matching_chars = code_completion_base.length();
  846. for (; matching_chars <= insert_text.length(); matching_chars++) {
  847. if (caret_col >= line.length() || line[caret_col] != insert_text[matching_chars]) {
  848. break;
  849. }
  850. caret_col++;
  851. }
  852. /* Remove base completion text. */
  853. _remove_text(caret_line, cursor_get_column() - code_completion_base.length(), caret_line, cursor_get_column());
  854. cursor_set_column(cursor_get_column() - code_completion_base.length(), false);
  855. /* Merge with text. */
  856. insert_text_at_cursor(insert_text.substr(0, code_completion_base.length()));
  857. cursor_set_column(caret_col, false);
  858. insert_text_at_cursor(insert_text.substr(matching_chars));
  859. }
  860. /* TODO: merge with autobrace completion, when in CodeEdit. */
  861. /* Handle merging of symbols eg strings, brackets. */
  862. const String line = get_line(caret_line);
  863. char32_t next_char = line[cursor_get_column()];
  864. char32_t last_completion_char = insert_text[insert_text.length() - 1];
  865. char32_t last_completion_char_display = display_text[display_text.length() - 1];
  866. if ((last_completion_char == '"' || last_completion_char == '\'') && (last_completion_char == next_char || last_completion_char_display == next_char)) {
  867. _remove_text(caret_line, cursor_get_column(), caret_line, cursor_get_column() + 1);
  868. }
  869. if (last_completion_char == '(') {
  870. if (next_char == last_completion_char) {
  871. _remove_text(caret_line, cursor_get_column() - 1, caret_line, cursor_get_column());
  872. } else if (auto_brace_completion_enabled) {
  873. insert_text_at_cursor(")");
  874. cursor_set_column(cursor_get_column() - 1);
  875. }
  876. } else if (last_completion_char == ')' && next_char == '(') {
  877. _remove_text(caret_line, cursor_get_column() - 2, caret_line, cursor_get_column());
  878. if (line[cursor_get_column() + 1] != ')') {
  879. cursor_set_column(cursor_get_column() - 1);
  880. }
  881. }
  882. end_complex_operation();
  883. cancel_code_completion();
  884. if (last_completion_char == '(') {
  885. request_code_completion();
  886. }
  887. }
  888. void CodeEdit::cancel_code_completion() {
  889. if (!code_completion_active) {
  890. return;
  891. }
  892. code_completion_forced = false;
  893. code_completion_active = false;
  894. update();
  895. }
  896. void CodeEdit::_bind_methods() {
  897. /* Main Gutter */
  898. ClassDB::bind_method(D_METHOD("_main_gutter_draw_callback"), &CodeEdit::_main_gutter_draw_callback);
  899. ClassDB::bind_method(D_METHOD("set_draw_breakpoints_gutter", "enable"), &CodeEdit::set_draw_breakpoints_gutter);
  900. ClassDB::bind_method(D_METHOD("is_drawing_breakpoints_gutter"), &CodeEdit::is_drawing_breakpoints_gutter);
  901. ClassDB::bind_method(D_METHOD("set_draw_bookmarks_gutter", "enable"), &CodeEdit::set_draw_bookmarks_gutter);
  902. ClassDB::bind_method(D_METHOD("is_drawing_bookmarks_gutter"), &CodeEdit::is_drawing_bookmarks_gutter);
  903. ClassDB::bind_method(D_METHOD("set_draw_executing_lines_gutter", "enable"), &CodeEdit::set_draw_executing_lines_gutter);
  904. ClassDB::bind_method(D_METHOD("is_drawing_executing_lines_gutter"), &CodeEdit::is_drawing_executing_lines_gutter);
  905. // Breakpoints
  906. ClassDB::bind_method(D_METHOD("set_line_as_breakpoint", "line", "breakpointed"), &CodeEdit::set_line_as_breakpoint);
  907. ClassDB::bind_method(D_METHOD("is_line_breakpointed", "line"), &CodeEdit::is_line_breakpointed);
  908. ClassDB::bind_method(D_METHOD("clear_breakpointed_lines"), &CodeEdit::clear_breakpointed_lines);
  909. ClassDB::bind_method(D_METHOD("get_breakpointed_lines"), &CodeEdit::get_breakpointed_lines);
  910. // Bookmarks
  911. ClassDB::bind_method(D_METHOD("set_line_as_bookmarked", "line", "bookmarked"), &CodeEdit::set_line_as_bookmarked);
  912. ClassDB::bind_method(D_METHOD("is_line_bookmarked", "line"), &CodeEdit::is_line_bookmarked);
  913. ClassDB::bind_method(D_METHOD("clear_bookmarked_lines"), &CodeEdit::clear_bookmarked_lines);
  914. ClassDB::bind_method(D_METHOD("get_bookmarked_lines"), &CodeEdit::get_bookmarked_lines);
  915. // executing lines
  916. ClassDB::bind_method(D_METHOD("set_line_as_executing", "line", "executing"), &CodeEdit::set_line_as_executing);
  917. ClassDB::bind_method(D_METHOD("is_line_executing", "line"), &CodeEdit::is_line_executing);
  918. ClassDB::bind_method(D_METHOD("clear_executing_lines"), &CodeEdit::clear_executing_lines);
  919. ClassDB::bind_method(D_METHOD("get_executing_lines"), &CodeEdit::get_executing_lines);
  920. /* Line numbers */
  921. ClassDB::bind_method(D_METHOD("_line_number_draw_callback"), &CodeEdit::_line_number_draw_callback);
  922. ClassDB::bind_method(D_METHOD("set_draw_line_numbers", "enable"), &CodeEdit::set_draw_line_numbers);
  923. ClassDB::bind_method(D_METHOD("is_draw_line_numbers_enabled"), &CodeEdit::is_draw_line_numbers_enabled);
  924. ClassDB::bind_method(D_METHOD("set_line_numbers_zero_padded", "enable"), &CodeEdit::set_line_numbers_zero_padded);
  925. ClassDB::bind_method(D_METHOD("is_line_numbers_zero_padded"), &CodeEdit::is_line_numbers_zero_padded);
  926. /* Fold Gutter */
  927. ClassDB::bind_method(D_METHOD("_fold_gutter_draw_callback"), &CodeEdit::_fold_gutter_draw_callback);
  928. ClassDB::bind_method(D_METHOD("set_draw_fold_gutter", "enable"), &CodeEdit::set_draw_fold_gutter);
  929. ClassDB::bind_method(D_METHOD("is_drawing_fold_gutter"), &CodeEdit::is_drawing_fold_gutter);
  930. /* Delimiters */
  931. // Strings
  932. ClassDB::bind_method(D_METHOD("add_string_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_string_delimiter, DEFVAL(false));
  933. ClassDB::bind_method(D_METHOD("remove_string_delimiter", "start_key"), &CodeEdit::remove_string_delimiter);
  934. ClassDB::bind_method(D_METHOD("has_string_delimiter", "start_key"), &CodeEdit::has_string_delimiter);
  935. ClassDB::bind_method(D_METHOD("set_string_delimiters", "string_delimiters"), &CodeEdit::set_string_delimiters);
  936. ClassDB::bind_method(D_METHOD("clear_string_delimiters"), &CodeEdit::clear_string_delimiters);
  937. ClassDB::bind_method(D_METHOD("get_string_delimiters"), &CodeEdit::get_string_delimiters);
  938. ClassDB::bind_method(D_METHOD("is_in_string", "line", "column"), &CodeEdit::is_in_string, DEFVAL(-1));
  939. // Comments
  940. ClassDB::bind_method(D_METHOD("add_comment_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_comment_delimiter, DEFVAL(false));
  941. ClassDB::bind_method(D_METHOD("remove_comment_delimiter", "start_key"), &CodeEdit::remove_comment_delimiter);
  942. ClassDB::bind_method(D_METHOD("has_comment_delimiter", "start_key"), &CodeEdit::has_comment_delimiter);
  943. ClassDB::bind_method(D_METHOD("set_comment_delimiters", "comment_delimiters"), &CodeEdit::set_comment_delimiters);
  944. ClassDB::bind_method(D_METHOD("clear_comment_delimiters"), &CodeEdit::clear_comment_delimiters);
  945. ClassDB::bind_method(D_METHOD("get_comment_delimiters"), &CodeEdit::get_comment_delimiters);
  946. ClassDB::bind_method(D_METHOD("is_in_comment", "line", "column"), &CodeEdit::is_in_comment, DEFVAL(-1));
  947. // Util
  948. ClassDB::bind_method(D_METHOD("get_delimiter_start_key", "delimiter_index"), &CodeEdit::get_delimiter_start_key);
  949. ClassDB::bind_method(D_METHOD("get_delimiter_end_key", "delimiter_index"), &CodeEdit::get_delimiter_end_key);
  950. ClassDB::bind_method(D_METHOD("get_delimiter_start_position", "line", "column"), &CodeEdit::get_delimiter_start_position);
  951. ClassDB::bind_method(D_METHOD("get_delimiter_end_position", "line", "column"), &CodeEdit::get_delimiter_end_position);
  952. /* Code hint */
  953. ClassDB::bind_method(D_METHOD("set_code_hint", "code_hint"), &CodeEdit::set_code_hint);
  954. ClassDB::bind_method(D_METHOD("set_code_hint_draw_below", "draw_below"), &CodeEdit::set_code_hint_draw_below);
  955. /* Code Completion */
  956. BIND_ENUM_CONSTANT(KIND_CLASS);
  957. BIND_ENUM_CONSTANT(KIND_FUNCTION);
  958. BIND_ENUM_CONSTANT(KIND_SIGNAL);
  959. BIND_ENUM_CONSTANT(KIND_VARIABLE);
  960. BIND_ENUM_CONSTANT(KIND_MEMBER);
  961. BIND_ENUM_CONSTANT(KIND_ENUM);
  962. BIND_ENUM_CONSTANT(KIND_CONSTANT);
  963. BIND_ENUM_CONSTANT(KIND_NODE_PATH);
  964. BIND_ENUM_CONSTANT(KIND_FILE_PATH);
  965. BIND_ENUM_CONSTANT(KIND_PLAIN_TEXT);
  966. ClassDB::bind_method(D_METHOD("get_text_for_code_completion"), &CodeEdit::get_text_for_code_completion);
  967. ClassDB::bind_method(D_METHOD("request_code_completion", "force"), &CodeEdit::request_code_completion, DEFVAL(false));
  968. 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));
  969. ClassDB::bind_method(D_METHOD("update_code_completion_options", "force"), &CodeEdit::update_code_completion_options);
  970. ClassDB::bind_method(D_METHOD("get_code_completion_options"), &CodeEdit::get_code_completion_options);
  971. ClassDB::bind_method(D_METHOD("get_code_completion_option", "index"), &CodeEdit::get_code_completion_option);
  972. ClassDB::bind_method(D_METHOD("get_code_completion_selected_index"), &CodeEdit::get_code_completion_selected_index);
  973. ClassDB::bind_method(D_METHOD("set_code_completion_selected_index", "index"), &CodeEdit::set_code_completion_selected_index);
  974. ClassDB::bind_method(D_METHOD("confirm_code_completion", "replace"), &CodeEdit::confirm_code_completion, DEFVAL(false));
  975. ClassDB::bind_method(D_METHOD("cancel_code_completion"), &CodeEdit::cancel_code_completion);
  976. ClassDB::bind_method(D_METHOD("set_code_completion_enabled", "enable"), &CodeEdit::set_code_completion_enabled);
  977. ClassDB::bind_method(D_METHOD("is_code_completion_enabled"), &CodeEdit::is_code_completion_enabled);
  978. ClassDB::bind_method(D_METHOD("set_code_completion_prefixes", "prefixes"), &CodeEdit::set_code_completion_prefixes);
  979. ClassDB::bind_method(D_METHOD("get_code_comletion_prefixes"), &CodeEdit::get_code_completion_prefixes);
  980. // Overridable
  981. BIND_VMETHOD(MethodInfo("_confirm_code_completion", PropertyInfo(Variant::BOOL, "replace")));
  982. BIND_VMETHOD(MethodInfo("_request_code_completion", PropertyInfo(Variant::BOOL, "force")));
  983. BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_filter_code_completion_candidates", PropertyInfo(Variant::ARRAY, "candidates")));
  984. /* Inspector */
  985. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_breakpoints_gutter"), "set_draw_breakpoints_gutter", "is_drawing_breakpoints_gutter");
  986. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_bookmarks"), "set_draw_bookmarks_gutter", "is_drawing_bookmarks_gutter");
  987. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_executing_lines"), "set_draw_executing_lines_gutter", "is_drawing_executing_lines_gutter");
  988. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_line_numbers"), "set_draw_line_numbers", "is_draw_line_numbers_enabled");
  989. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "zero_pad_line_numbers"), "set_line_numbers_zero_padded", "is_line_numbers_zero_padded");
  990. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_fold_gutter"), "set_draw_fold_gutter", "is_drawing_fold_gutter");
  991. ADD_GROUP("Delimiters", "delimiter_");
  992. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_strings"), "set_string_delimiters", "get_string_delimiters");
  993. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_comments"), "set_comment_delimiters", "get_comment_delimiters");
  994. ADD_GROUP("Code Completion", "code_completion_");
  995. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "code_completion_enabled"), "set_code_completion_enabled", "is_code_completion_enabled");
  996. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "code_completion_prefixes"), "set_code_completion_prefixes", "get_code_comletion_prefixes");
  997. /* Signals */
  998. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "line")));
  999. ADD_SIGNAL(MethodInfo("request_code_completion"));
  1000. }
  1001. void CodeEdit::_gutter_clicked(int p_line, int p_gutter) {
  1002. if (p_gutter == main_gutter) {
  1003. if (draw_breakpoints) {
  1004. set_line_as_breakpoint(p_line, !is_line_breakpointed(p_line));
  1005. }
  1006. return;
  1007. }
  1008. if (p_gutter == line_number_gutter) {
  1009. set_selection_mode(TextEdit::SelectionMode::SELECTION_MODE_LINE, p_line, 0);
  1010. select(p_line, 0, p_line + 1, 0);
  1011. cursor_set_line(p_line + 1);
  1012. cursor_set_column(0);
  1013. return;
  1014. }
  1015. if (p_gutter == fold_gutter) {
  1016. if (is_folded(p_line)) {
  1017. unfold_line(p_line);
  1018. } else if (can_fold(p_line)) {
  1019. fold_line(p_line);
  1020. }
  1021. return;
  1022. }
  1023. }
  1024. void CodeEdit::_update_gutter_indexes() {
  1025. for (int i = 0; i < get_gutter_count(); i++) {
  1026. if (get_gutter_name(i) == "main_gutter") {
  1027. main_gutter = i;
  1028. continue;
  1029. }
  1030. if (get_gutter_name(i) == "line_numbers") {
  1031. line_number_gutter = i;
  1032. continue;
  1033. }
  1034. if (get_gutter_name(i) == "fold_gutter") {
  1035. fold_gutter = i;
  1036. continue;
  1037. }
  1038. }
  1039. }
  1040. /* Delimiters */
  1041. void CodeEdit::_update_delimiter_cache(int p_from_line, int p_to_line) {
  1042. if (delimiters.size() == 0) {
  1043. return;
  1044. }
  1045. int line_count = get_line_count();
  1046. if (p_to_line == -1) {
  1047. p_to_line = line_count;
  1048. }
  1049. int start_line = MIN(p_from_line, p_to_line);
  1050. int end_line = MAX(p_from_line, p_to_line);
  1051. /* Make sure delimiter_cache has all the lines. */
  1052. if (start_line != end_line) {
  1053. if (p_to_line < p_from_line) {
  1054. for (int i = end_line; i > start_line; i--) {
  1055. delimiter_cache.remove(i);
  1056. }
  1057. } else {
  1058. for (int i = start_line; i < end_line; i++) {
  1059. delimiter_cache.insert(i, Map<int, int>());
  1060. }
  1061. }
  1062. }
  1063. int in_region = -1;
  1064. for (int i = start_line; i < MIN(end_line + 1, line_count); i++) {
  1065. int current_end_region = (i <= 0 || delimiter_cache[i].size() < 1) ? -1 : delimiter_cache[i].back()->value();
  1066. in_region = (i <= 0 || delimiter_cache[i - 1].size() < 1) ? -1 : delimiter_cache[i - 1].back()->value();
  1067. const String &str = get_line(i);
  1068. const int line_length = str.length();
  1069. delimiter_cache.write[i].clear();
  1070. if (str.length() == 0) {
  1071. if (in_region != -1) {
  1072. delimiter_cache.write[i][0] = in_region;
  1073. }
  1074. if (i == end_line && current_end_region != in_region) {
  1075. end_line++;
  1076. end_line = MIN(end_line, line_count);
  1077. }
  1078. continue;
  1079. }
  1080. int end_region = -1;
  1081. for (int j = 0; j < line_length; j++) {
  1082. int from = j;
  1083. for (; from < line_length; from++) {
  1084. if (str[from] == '\\') {
  1085. from++;
  1086. continue;
  1087. }
  1088. break;
  1089. }
  1090. /* check if we are in entering a region */
  1091. bool same_line = false;
  1092. if (in_region == -1) {
  1093. for (int d = 0; d < delimiters.size(); d++) {
  1094. /* check there is enough room */
  1095. int chars_left = line_length - from;
  1096. int start_key_length = delimiters[d].start_key.length();
  1097. int end_key_length = delimiters[d].end_key.length();
  1098. if (chars_left < start_key_length) {
  1099. continue;
  1100. }
  1101. /* search the line */
  1102. bool match = true;
  1103. const char32_t *start_key = delimiters[d].start_key.get_data();
  1104. for (int k = 0; k < start_key_length; k++) {
  1105. if (start_key[k] != str[from + k]) {
  1106. match = false;
  1107. break;
  1108. }
  1109. }
  1110. if (!match) {
  1111. continue;
  1112. }
  1113. same_line = true;
  1114. in_region = d;
  1115. delimiter_cache.write[i][from + 1] = d;
  1116. from += start_key_length;
  1117. /* check if it's the whole line */
  1118. if (end_key_length == 0 || delimiters[d].line_only || from + end_key_length > line_length) {
  1119. j = line_length;
  1120. if (delimiters[d].line_only) {
  1121. delimiter_cache.write[i][line_length + 1] = -1;
  1122. } else {
  1123. end_region = in_region;
  1124. }
  1125. }
  1126. break;
  1127. }
  1128. if (j == line_length || in_region == -1) {
  1129. continue;
  1130. }
  1131. }
  1132. /* if we are in one find the end key */
  1133. /* search the line */
  1134. int region_end_index = -1;
  1135. int end_key_length = delimiters[in_region].end_key.length();
  1136. const char32_t *end_key = delimiters[in_region].end_key.get_data();
  1137. for (; from < line_length; from++) {
  1138. if (line_length - from < end_key_length) {
  1139. break;
  1140. }
  1141. if (!is_symbol(str[from])) {
  1142. continue;
  1143. }
  1144. if (str[from] == '\\') {
  1145. from++;
  1146. continue;
  1147. }
  1148. region_end_index = from;
  1149. for (int k = 0; k < end_key_length; k++) {
  1150. if (end_key[k] != str[from + k]) {
  1151. region_end_index = -1;
  1152. break;
  1153. }
  1154. }
  1155. if (region_end_index != -1) {
  1156. break;
  1157. }
  1158. }
  1159. j = from + (end_key_length - 1);
  1160. end_region = (region_end_index == -1) ? in_region : -1;
  1161. if (!same_line || region_end_index != -1) {
  1162. delimiter_cache.write[i][j + 1] = end_region;
  1163. }
  1164. in_region = -1;
  1165. }
  1166. if (i == end_line && current_end_region != end_region) {
  1167. end_line++;
  1168. end_line = MIN(end_line, line_count);
  1169. }
  1170. }
  1171. }
  1172. int CodeEdit::_is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const {
  1173. if (delimiters.size() == 0) {
  1174. return -1;
  1175. }
  1176. ERR_FAIL_INDEX_V(p_line, get_line_count(), 0);
  1177. int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
  1178. bool in_region = region != -1 && delimiters[region].type == p_type;
  1179. for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
  1180. /* If column is specified, loop untill the key is larger then the column. */
  1181. if (p_column != -1) {
  1182. if (E->key() > p_column) {
  1183. break;
  1184. }
  1185. in_region = E->value() != -1 && delimiters[E->value()].type == p_type;
  1186. region = in_region ? E->value() : -1;
  1187. continue;
  1188. }
  1189. /* If no column, calulate if the entire line is a region */
  1190. /* excluding whitespace. */
  1191. const String line = get_line(p_line);
  1192. if (!in_region) {
  1193. if (E->value() == -1 || delimiters[E->value()].type != p_type) {
  1194. break;
  1195. }
  1196. region = E->value();
  1197. in_region = true;
  1198. for (int i = E->key() - 2; i >= 0; i--) {
  1199. if (!_is_whitespace(line[i])) {
  1200. return -1;
  1201. }
  1202. }
  1203. }
  1204. if (delimiters[region].line_only) {
  1205. return region;
  1206. }
  1207. int end_col = E->key();
  1208. if (E->value() != -1) {
  1209. if (!E->next()) {
  1210. return region;
  1211. }
  1212. end_col = E->next()->key();
  1213. }
  1214. for (int i = end_col; i < line.length(); i++) {
  1215. if (!_is_whitespace(line[i])) {
  1216. return -1;
  1217. }
  1218. }
  1219. return region;
  1220. }
  1221. return in_region ? region : -1;
  1222. }
  1223. void CodeEdit::_add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type) {
  1224. if (p_start_key.length() > 0) {
  1225. for (int i = 0; i < p_start_key.length(); i++) {
  1226. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "delimiter must start with a symbol");
  1227. }
  1228. }
  1229. if (p_end_key.length() > 0) {
  1230. for (int i = 0; i < p_end_key.length(); i++) {
  1231. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "delimiter must end with a symbol");
  1232. }
  1233. }
  1234. int at = 0;
  1235. for (int i = 0; i < delimiters.size(); i++) {
  1236. ERR_FAIL_COND_MSG(delimiters[i].start_key == p_start_key, "delimiter with start key '" + p_start_key + "' already exists.");
  1237. if (p_start_key.length() < delimiters[i].start_key.length()) {
  1238. at++;
  1239. }
  1240. }
  1241. Delimiter delimiter;
  1242. delimiter.type = p_type;
  1243. delimiter.start_key = p_start_key;
  1244. delimiter.end_key = p_end_key;
  1245. delimiter.line_only = p_line_only || p_end_key == "";
  1246. delimiters.insert(at, delimiter);
  1247. if (!setting_delimiters) {
  1248. delimiter_cache.clear();
  1249. _update_delimiter_cache();
  1250. }
  1251. }
  1252. void CodeEdit::_remove_delimiter(const String &p_start_key, DelimiterType p_type) {
  1253. for (int i = 0; i < delimiters.size(); i++) {
  1254. if (delimiters[i].start_key != p_start_key) {
  1255. continue;
  1256. }
  1257. if (delimiters[i].type != p_type) {
  1258. break;
  1259. }
  1260. delimiters.remove(i);
  1261. if (!setting_delimiters) {
  1262. delimiter_cache.clear();
  1263. _update_delimiter_cache();
  1264. }
  1265. break;
  1266. }
  1267. }
  1268. bool CodeEdit::_has_delimiter(const String &p_start_key, DelimiterType p_type) const {
  1269. for (int i = 0; i < delimiters.size(); i++) {
  1270. if (delimiters[i].start_key == p_start_key) {
  1271. return delimiters[i].type == p_type;
  1272. }
  1273. }
  1274. return false;
  1275. }
  1276. void CodeEdit::_set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type) {
  1277. setting_delimiters = true;
  1278. _clear_delimiters(p_type);
  1279. for (int i = 0; i < p_delimiters.size(); i++) {
  1280. String key = p_delimiters[i].is_null() ? "" : p_delimiters[i];
  1281. const String start_key = key.get_slice(" ", 0);
  1282. const String end_key = key.get_slice_count(" ") > 1 ? key.get_slice(" ", 1) : String();
  1283. _add_delimiter(start_key, end_key, end_key == "", p_type);
  1284. }
  1285. setting_delimiters = false;
  1286. _update_delimiter_cache();
  1287. }
  1288. void CodeEdit::_clear_delimiters(DelimiterType p_type) {
  1289. for (int i = delimiters.size() - 1; i >= 0; i--) {
  1290. if (delimiters[i].type == p_type) {
  1291. delimiters.remove(i);
  1292. }
  1293. }
  1294. delimiter_cache.clear();
  1295. if (!setting_delimiters) {
  1296. _update_delimiter_cache();
  1297. }
  1298. }
  1299. TypedArray<String> CodeEdit::_get_delimiters(DelimiterType p_type) const {
  1300. TypedArray<String> r_delimiters;
  1301. for (int i = 0; i < delimiters.size(); i++) {
  1302. if (delimiters[i].type != p_type) {
  1303. continue;
  1304. }
  1305. r_delimiters.push_back(delimiters[i].start_key + (delimiters[i].end_key.is_empty() ? "" : " " + delimiters[i].end_key));
  1306. }
  1307. return r_delimiters;
  1308. }
  1309. /* Code Completion */
  1310. void CodeEdit::_filter_code_completion_candidates() {
  1311. ScriptInstance *si = get_script_instance();
  1312. if (si && si->has_method("_filter_code_completion_candidates")) {
  1313. code_completion_options.clear();
  1314. code_completion_base = "";
  1315. /* Build options argument. */
  1316. TypedArray<Dictionary> completion_options_sources;
  1317. completion_options_sources.resize(code_completion_option_sources.size());
  1318. int i = 0;
  1319. for (List<ScriptCodeCompletionOption>::Element *E = code_completion_option_sources.front(); E; E = E->next()) {
  1320. Dictionary option;
  1321. option["kind"] = E->get().kind;
  1322. option["display_text"] = E->get().display;
  1323. option["insert_text"] = E->get().insert_text;
  1324. option["font_color"] = E->get().font_color;
  1325. option["icon"] = E->get().icon;
  1326. option["default_value"] = E->get().default_value;
  1327. completion_options_sources[i] = option;
  1328. i++;
  1329. }
  1330. TypedArray<Dictionary> completion_options = si->call("_filter_code_completion_candidates", completion_options_sources);
  1331. /* No options to complete, cancel. */
  1332. if (completion_options.size() == 0) {
  1333. cancel_code_completion();
  1334. return;
  1335. }
  1336. /* Convert back into options. */
  1337. int max_width = 0;
  1338. for (i = 0; i < completion_options.size(); i++) {
  1339. ScriptCodeCompletionOption option;
  1340. option.kind = (ScriptCodeCompletionOption::Kind)(int)completion_options[i].get("kind");
  1341. option.display = completion_options[i].get("display_text");
  1342. option.insert_text = completion_options[i].get("insert_text");
  1343. option.font_color = completion_options[i].get("font_color");
  1344. option.icon = completion_options[i].get("icon");
  1345. option.default_value = completion_options[i].get("default_value");
  1346. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  1347. code_completion_options.push_back(option);
  1348. }
  1349. code_completion_longest_line = MIN(max_width, code_completion_max_width);
  1350. code_completion_current_selected = 0;
  1351. code_completion_active = true;
  1352. update();
  1353. return;
  1354. }
  1355. const int caret_line = cursor_get_line();
  1356. const int caret_column = cursor_get_column();
  1357. const String line = get_line(caret_line);
  1358. if (caret_column > 0 && line[caret_column - 1] == '(' && !code_completion_forced) {
  1359. cancel_code_completion();
  1360. return;
  1361. }
  1362. /* Get string status, are we in one or at the close. */
  1363. int in_string = is_in_string(caret_line, caret_column);
  1364. int first_quote_col = -1;
  1365. if (in_string != -1) {
  1366. Point2 string_start_pos = get_delimiter_start_position(caret_line, caret_column);
  1367. first_quote_col = (string_start_pos.y == caret_line) ? string_start_pos.x : -1;
  1368. } else if (caret_column > 0) {
  1369. if (is_in_string(caret_line, caret_column - 1) != -1) {
  1370. first_quote_col = caret_column - 1;
  1371. }
  1372. }
  1373. int cofs = caret_column;
  1374. String string_to_complete;
  1375. bool prev_is_word = false;
  1376. /* Cancel if we are at the close of a string. */
  1377. if (in_string == -1 && first_quote_col == cofs - 1) {
  1378. cancel_code_completion();
  1379. return;
  1380. /* In a string, therefore we are trying to complete the string text. */
  1381. } else if (in_string != -1 && first_quote_col != -1) {
  1382. int key_length = delimiters[in_string].start_key.length();
  1383. string_to_complete = line.substr(first_quote_col - key_length, (cofs - first_quote_col) + key_length);
  1384. /* If we have a space, previous word might be a keyword. eg "func |". */
  1385. } else if (cofs > 0 && line[cofs - 1] == ' ') {
  1386. int ofs = cofs - 1;
  1387. while (ofs >= 0 && line[ofs] == ' ') {
  1388. ofs--;
  1389. }
  1390. prev_is_word = _is_char(line[ofs]);
  1391. /* Otherwise get current word and set cofs to the start. */
  1392. } else {
  1393. int start_cofs = cofs;
  1394. while (cofs > 0 && line[cofs - 1] > 32 && (line[cofs - 1] == '/' || _is_char(line[cofs - 1]))) {
  1395. cofs--;
  1396. }
  1397. string_to_complete = line.substr(cofs, start_cofs - cofs);
  1398. }
  1399. /* If all else fails, check for a prefix. */
  1400. /* Single space between caret and prefix is okay. */
  1401. bool prev_is_prefix = false;
  1402. if (cofs > 0 && code_completion_prefixes.has(String::chr(line[cofs - 1]))) {
  1403. prev_is_prefix = true;
  1404. } else if (cofs > 1 && line[cofs - 1] == ' ' && code_completion_prefixes.has(String::chr(line[cofs - 2]))) {
  1405. prev_is_prefix = true;
  1406. }
  1407. if (!prev_is_word && string_to_complete.is_empty() && (cofs == 0 || !prev_is_prefix)) {
  1408. cancel_code_completion();
  1409. return;
  1410. }
  1411. /* Filter Options. */
  1412. /* For now handle only tradional quoted strings. */
  1413. bool single_quote = in_string != -1 && first_quote_col > 0 && delimiters[in_string].start_key == "'";
  1414. code_completion_options.clear();
  1415. code_completion_base = string_to_complete;
  1416. Vector<ScriptCodeCompletionOption> completion_options_casei;
  1417. Vector<ScriptCodeCompletionOption> completion_options_subseq;
  1418. Vector<ScriptCodeCompletionOption> completion_options_subseq_casei;
  1419. int max_width = 0;
  1420. String string_to_complete_lower = string_to_complete.to_lower();
  1421. for (List<ScriptCodeCompletionOption>::Element *E = code_completion_option_sources.front(); E; E = E->next()) {
  1422. ScriptCodeCompletionOption &option = E->get();
  1423. if (single_quote && option.display.is_quoted()) {
  1424. option.display = option.display.unquote().quote("'");
  1425. }
  1426. if (in_string != -1) {
  1427. String quote = single_quote ? "'" : "\"";
  1428. option.display = option.display.unquote().quote(quote);
  1429. option.insert_text = option.insert_text.unquote().quote(quote);
  1430. }
  1431. if (option.display.length() == 0) {
  1432. continue;
  1433. }
  1434. if (string_to_complete.length() == 0) {
  1435. code_completion_options.push_back(option);
  1436. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  1437. continue;
  1438. }
  1439. /* This code works the same as:
  1440. if (option.display.begins_with(s)) {
  1441. completion_options.push_back(option);
  1442. } else if (option.display.to_lower().begins_with(s.to_lower())) {
  1443. completion_options_casei.push_back(option);
  1444. } else if (s.is_subsequence_of(option.display)) {
  1445. completion_options_subseq.push_back(option);
  1446. } else if (s.is_subsequence_ofi(option.display)) {
  1447. completion_options_subseq_casei.push_back(option);
  1448. }
  1449. But is more performant due to being inlined and looping over the characters only once
  1450. */
  1451. String display_lower = option.display.to_lower();
  1452. const char32_t *ssq = &string_to_complete[0];
  1453. const char32_t *ssq_lower = &string_to_complete_lower[0];
  1454. const char32_t *tgt = &option.display[0];
  1455. const char32_t *tgt_lower = &display_lower[0];
  1456. const char32_t *ssq_last_tgt = nullptr;
  1457. const char32_t *ssq_lower_last_tgt = nullptr;
  1458. for (; *tgt; tgt++, tgt_lower++) {
  1459. if (*ssq == *tgt) {
  1460. ssq++;
  1461. ssq_last_tgt = tgt;
  1462. }
  1463. if (*ssq_lower == *tgt_lower) {
  1464. ssq_lower++;
  1465. ssq_lower_last_tgt = tgt;
  1466. }
  1467. }
  1468. /* Matched the whole subsequence in s. */
  1469. if (!*ssq) {
  1470. /* Finished matching in the first s.length() characters. */
  1471. if (ssq_last_tgt == &option.display[string_to_complete.length() - 1]) {
  1472. code_completion_options.push_back(option);
  1473. } else {
  1474. completion_options_subseq.push_back(option);
  1475. }
  1476. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  1477. /* Matched the whole subsequence in s_lower. */
  1478. } else if (!*ssq_lower) {
  1479. /* Finished matching in the first s.length() characters. */
  1480. if (ssq_lower_last_tgt == &option.display[string_to_complete.length() - 1]) {
  1481. completion_options_casei.push_back(option);
  1482. } else {
  1483. completion_options_subseq_casei.push_back(option);
  1484. }
  1485. max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
  1486. }
  1487. }
  1488. code_completion_options.append_array(completion_options_casei);
  1489. code_completion_options.append_array(completion_options_subseq);
  1490. code_completion_options.append_array(completion_options_subseq_casei);
  1491. /* No options to complete, cancel. */
  1492. if (code_completion_options.size() == 0) {
  1493. cancel_code_completion();
  1494. return;
  1495. }
  1496. /* A perfect match, stop completion. */
  1497. if (code_completion_options.size() == 1 && string_to_complete == code_completion_options[0].display) {
  1498. cancel_code_completion();
  1499. return;
  1500. }
  1501. code_completion_longest_line = MIN(max_width, code_completion_max_width);
  1502. code_completion_current_selected = 0;
  1503. code_completion_active = true;
  1504. update();
  1505. }
  1506. void CodeEdit::_lines_edited_from(int p_from_line, int p_to_line) {
  1507. _update_delimiter_cache(p_from_line, p_to_line);
  1508. if (p_from_line == p_to_line) {
  1509. return;
  1510. }
  1511. int lc = get_line_count();
  1512. line_number_digits = 1;
  1513. while (lc /= 10) {
  1514. line_number_digits++;
  1515. }
  1516. set_gutter_width(line_number_gutter, (line_number_digits + 1) * cache.font->get_char_size('0', 0, cache.font_size).width);
  1517. int from_line = MIN(p_from_line, p_to_line);
  1518. int line_count = (p_to_line - p_from_line);
  1519. List<int> breakpoints;
  1520. breakpointed_lines.get_key_list(&breakpoints);
  1521. for (const List<int>::Element *E = breakpoints.front(); E; E = E->next()) {
  1522. int line = E->get();
  1523. if (line <= from_line) {
  1524. continue;
  1525. }
  1526. breakpointed_lines.erase(line);
  1527. emit_signal("breakpoint_toggled", line);
  1528. if (line_count > 0 || line >= p_from_line) {
  1529. emit_signal("breakpoint_toggled", line + line_count);
  1530. breakpointed_lines[line + line_count] = true;
  1531. continue;
  1532. }
  1533. }
  1534. }
  1535. CodeEdit::CodeEdit() {
  1536. /* Text Direction */
  1537. set_layout_direction(LAYOUT_DIRECTION_LTR);
  1538. set_text_direction(TEXT_DIRECTION_LTR);
  1539. /* Gutters */
  1540. int gutter_idx = 0;
  1541. /* Main Gutter */
  1542. add_gutter();
  1543. set_gutter_name(gutter_idx, "main_gutter");
  1544. set_gutter_draw(gutter_idx, false);
  1545. set_gutter_overwritable(gutter_idx, true);
  1546. set_gutter_type(gutter_idx, GUTTER_TPYE_CUSTOM);
  1547. set_gutter_custom_draw(gutter_idx, this, "_main_gutter_draw_callback");
  1548. gutter_idx++;
  1549. /* Line numbers */
  1550. add_gutter();
  1551. set_gutter_name(gutter_idx, "line_numbers");
  1552. set_gutter_draw(gutter_idx, false);
  1553. set_gutter_type(gutter_idx, GUTTER_TPYE_CUSTOM);
  1554. set_gutter_custom_draw(gutter_idx, this, "_line_number_draw_callback");
  1555. gutter_idx++;
  1556. /* Fold Gutter */
  1557. add_gutter();
  1558. set_gutter_name(gutter_idx, "fold_gutter");
  1559. set_gutter_draw(gutter_idx, false);
  1560. set_gutter_type(gutter_idx, GUTTER_TPYE_CUSTOM);
  1561. set_gutter_custom_draw(gutter_idx, this, "_fold_gutter_draw_callback");
  1562. gutter_idx++;
  1563. connect("lines_edited_from", callable_mp(this, &CodeEdit::_lines_edited_from));
  1564. connect("gutter_clicked", callable_mp(this, &CodeEdit::_gutter_clicked));
  1565. connect("gutter_added", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  1566. connect("gutter_removed", callable_mp(this, &CodeEdit::_update_gutter_indexes));
  1567. _update_gutter_indexes();
  1568. }
  1569. CodeEdit::~CodeEdit() {
  1570. }