2
0

code_editor.cpp 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. /**************************************************************************/
  2. /* code_editor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_editor.h"
  31. #include "core/input/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/string/string_builder.h"
  34. #include "core/templates/pair.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/plugins/script_editor_plugin.h"
  38. #include "editor/themes/editor_scale.h"
  39. #include "editor/themes/editor_theme_manager.h"
  40. #include "scene/gui/menu_button.h"
  41. #include "scene/gui/separator.h"
  42. #include "scene/resources/font.h"
  43. void GotoLineDialog::popup_find_line(CodeEdit *p_edit) {
  44. text_editor = p_edit;
  45. // Add 1 because text_editor->get_caret_line() starts from 0, but the editor user interface starts from 1.
  46. line->set_text(itos(text_editor->get_caret_line() + 1));
  47. line->select_all();
  48. popup_centered(Size2(180, 80) * EDSCALE);
  49. line->grab_focus();
  50. }
  51. int GotoLineDialog::get_line() const {
  52. return line->get_text().to_int();
  53. }
  54. void GotoLineDialog::ok_pressed() {
  55. // Subtract 1 because the editor user interface starts from 1, but text_editor->set_caret_line(n) starts from 0.
  56. const int line_number = get_line() - 1;
  57. if (line_number < 0 || line_number >= text_editor->get_line_count()) {
  58. return;
  59. }
  60. text_editor->remove_secondary_carets();
  61. text_editor->unfold_line(line_number);
  62. text_editor->set_caret_line(line_number);
  63. hide();
  64. }
  65. GotoLineDialog::GotoLineDialog() {
  66. set_title(TTR("Go to Line"));
  67. VBoxContainer *vbc = memnew(VBoxContainer);
  68. vbc->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  69. vbc->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  70. vbc->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -8 * EDSCALE);
  71. vbc->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, -8 * EDSCALE);
  72. add_child(vbc);
  73. Label *l = memnew(Label);
  74. l->set_text(TTR("Line Number:"));
  75. vbc->add_child(l);
  76. line = memnew(LineEdit);
  77. vbc->add_child(line);
  78. register_text_enter(line);
  79. text_editor = nullptr;
  80. line_label = nullptr;
  81. set_hide_on_ok(false);
  82. }
  83. void FindReplaceBar::_notification(int p_what) {
  84. switch (p_what) {
  85. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  86. if (!EditorThemeManager::is_generated_theme_outdated()) {
  87. break;
  88. }
  89. [[fallthrough]];
  90. }
  91. case NOTIFICATION_READY: {
  92. find_prev->set_icon(get_editor_theme_icon(SNAME("MoveUp")));
  93. find_next->set_icon(get_editor_theme_icon(SNAME("MoveDown")));
  94. hide_button->set_texture_normal(get_editor_theme_icon(SNAME("Close")));
  95. hide_button->set_texture_hover(get_editor_theme_icon(SNAME("Close")));
  96. hide_button->set_texture_pressed(get_editor_theme_icon(SNAME("Close")));
  97. hide_button->set_custom_minimum_size(hide_button->get_texture_normal()->get_size());
  98. } break;
  99. case NOTIFICATION_VISIBILITY_CHANGED: {
  100. set_process_unhandled_input(is_visible_in_tree());
  101. } break;
  102. case NOTIFICATION_THEME_CHANGED: {
  103. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  104. } break;
  105. case NOTIFICATION_PREDELETE: {
  106. if (base_text_editor) {
  107. base_text_editor->remove_find_replace_bar();
  108. base_text_editor = nullptr;
  109. }
  110. } break;
  111. }
  112. }
  113. void FindReplaceBar::unhandled_input(const Ref<InputEvent> &p_event) {
  114. ERR_FAIL_COND(p_event.is_null());
  115. Ref<InputEventKey> k = p_event;
  116. if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true)) {
  117. Control *focus_owner = get_viewport()->gui_get_focus_owner();
  118. if (text_editor->has_focus() || (focus_owner && is_ancestor_of(focus_owner))) {
  119. _hide_bar();
  120. accept_event();
  121. }
  122. }
  123. }
  124. void FindReplaceBar::_focus_lost() {
  125. if (Input::get_singleton()->is_action_pressed(SNAME("ui_cancel"))) {
  126. // Unfocused after pressing Escape, so hide the bar.
  127. _hide_bar(true);
  128. }
  129. }
  130. void FindReplaceBar::_update_flags(bool p_direction_backwards) {
  131. flags = 0;
  132. if (is_whole_words()) {
  133. flags |= TextEdit::SEARCH_WHOLE_WORDS;
  134. }
  135. if (is_case_sensitive()) {
  136. flags |= TextEdit::SEARCH_MATCH_CASE;
  137. }
  138. if (p_direction_backwards) {
  139. flags |= TextEdit::SEARCH_BACKWARDS;
  140. }
  141. }
  142. bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
  143. if (!preserve_cursor) {
  144. text_editor->remove_secondary_carets();
  145. }
  146. String text = get_search_text();
  147. Point2i pos = text_editor->search(text, p_flags, p_from_line, p_from_col);
  148. if (pos.x != -1) {
  149. if (!preserve_cursor && !is_selection_only()) {
  150. text_editor->unfold_line(pos.y);
  151. text_editor->set_caret_line(pos.y, false);
  152. text_editor->set_caret_column(pos.x + text.length(), false);
  153. text_editor->center_viewport_to_caret(0);
  154. text_editor->select(pos.y, pos.x, pos.y, pos.x + text.length());
  155. line_col_changed_for_result = true;
  156. }
  157. text_editor->set_search_text(text);
  158. text_editor->set_search_flags(p_flags);
  159. result_line = pos.y;
  160. result_col = pos.x;
  161. _update_results_count();
  162. } else {
  163. results_count = 0;
  164. result_line = -1;
  165. result_col = -1;
  166. text_editor->set_search_text("");
  167. text_editor->set_search_flags(p_flags);
  168. }
  169. _update_matches_display();
  170. return pos.x != -1;
  171. }
  172. void FindReplaceBar::_replace() {
  173. text_editor->remove_secondary_carets();
  174. bool selection_enabled = text_editor->has_selection(0);
  175. Point2i selection_begin, selection_end;
  176. if (selection_enabled) {
  177. selection_begin = Point2i(text_editor->get_selection_from_line(0), text_editor->get_selection_from_column(0));
  178. selection_end = Point2i(text_editor->get_selection_to_line(0), text_editor->get_selection_to_column(0));
  179. }
  180. String repl_text = get_replace_text();
  181. int search_text_len = get_search_text().length();
  182. text_editor->begin_complex_operation();
  183. if (selection_enabled && is_selection_only()) {
  184. // Restrict search_current() to selected region.
  185. text_editor->set_caret_line(selection_begin.width, false, true, 0, 0);
  186. text_editor->set_caret_column(selection_begin.height, true, 0);
  187. }
  188. if (search_current()) {
  189. text_editor->unfold_line(result_line);
  190. text_editor->select(result_line, result_col, result_line, result_col + search_text_len, 0);
  191. if (selection_enabled && is_selection_only()) {
  192. Point2i match_from(result_line, result_col);
  193. Point2i match_to(result_line, result_col + search_text_len);
  194. if (!(match_from < selection_begin || match_to > selection_end)) {
  195. text_editor->insert_text_at_caret(repl_text, 0);
  196. if (match_to.x == selection_end.x) {
  197. // Adjust selection bounds if necessary.
  198. selection_end.y += repl_text.length() - search_text_len;
  199. }
  200. }
  201. } else {
  202. text_editor->insert_text_at_caret(repl_text, 0);
  203. }
  204. }
  205. text_editor->end_complex_operation();
  206. results_count = -1;
  207. results_count_to_current = -1;
  208. needs_to_count_results = true;
  209. if (selection_enabled && is_selection_only()) {
  210. // Reselect in order to keep 'Replace' restricted to selection.
  211. text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y, 0);
  212. } else {
  213. text_editor->deselect(0);
  214. }
  215. }
  216. void FindReplaceBar::_replace_all() {
  217. text_editor->remove_secondary_carets();
  218. text_editor->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  219. // Line as x so it gets priority in comparison, column as y.
  220. Point2i orig_cursor(text_editor->get_caret_line(0), text_editor->get_caret_column(0));
  221. Point2i prev_match = Point2(-1, -1);
  222. bool selection_enabled = text_editor->has_selection(0);
  223. if (!is_selection_only()) {
  224. text_editor->deselect();
  225. selection_enabled = false;
  226. } else {
  227. result_line = -1;
  228. result_col = -1;
  229. }
  230. Point2i selection_begin, selection_end;
  231. if (selection_enabled) {
  232. selection_begin = Point2i(text_editor->get_selection_from_line(0), text_editor->get_selection_from_column(0));
  233. selection_end = Point2i(text_editor->get_selection_to_line(0), text_editor->get_selection_to_column(0));
  234. }
  235. int vsval = text_editor->get_v_scroll();
  236. String repl_text = get_replace_text();
  237. int search_text_len = get_search_text().length();
  238. int rc = 0;
  239. replace_all_mode = true;
  240. text_editor->begin_complex_operation();
  241. if (selection_enabled && is_selection_only()) {
  242. text_editor->set_caret_line(selection_begin.width, false, true, 0, 0);
  243. text_editor->set_caret_column(selection_begin.height, true, 0);
  244. } else {
  245. text_editor->set_caret_line(0, false, true, 0, 0);
  246. text_editor->set_caret_column(0, true, 0);
  247. }
  248. if (search_current()) {
  249. do {
  250. // Replace area.
  251. Point2i match_from(result_line, result_col);
  252. Point2i match_to(result_line, result_col + search_text_len);
  253. if (match_from < prev_match) {
  254. break; // Done.
  255. }
  256. prev_match = Point2i(result_line, result_col + repl_text.length());
  257. text_editor->unfold_line(result_line);
  258. text_editor->select(result_line, result_col, result_line, match_to.y, 0);
  259. if (selection_enabled) {
  260. if (match_from < selection_begin || match_to > selection_end) {
  261. break; // Done.
  262. }
  263. // Replace but adjust selection bounds.
  264. text_editor->insert_text_at_caret(repl_text, 0);
  265. if (match_to.x == selection_end.x) {
  266. selection_end.y += repl_text.length() - search_text_len;
  267. }
  268. } else {
  269. // Just replace.
  270. text_editor->insert_text_at_caret(repl_text, 0);
  271. }
  272. rc++;
  273. } while (search_next());
  274. }
  275. text_editor->end_complex_operation();
  276. replace_all_mode = false;
  277. // Restore editor state (selection, cursor, scroll).
  278. text_editor->set_caret_line(orig_cursor.x, false, true, 0, 0);
  279. text_editor->set_caret_column(orig_cursor.y, true, 0);
  280. if (selection_enabled) {
  281. // Reselect.
  282. text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y, 0);
  283. }
  284. text_editor->set_v_scroll(vsval);
  285. matches_label->add_theme_color_override("font_color", rc > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  286. matches_label->set_text(vformat(TTR("%d replaced."), rc));
  287. callable_mp((Object *)text_editor, &Object::connect).call_deferred("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed), 0U);
  288. results_count = -1;
  289. results_count_to_current = -1;
  290. needs_to_count_results = true;
  291. }
  292. void FindReplaceBar::_get_search_from(int &r_line, int &r_col, bool p_is_searching_next) {
  293. if (!text_editor->has_selection(0) || is_selection_only()) {
  294. r_line = text_editor->get_caret_line(0);
  295. r_col = text_editor->get_caret_column(0);
  296. if (!p_is_searching_next && r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
  297. r_col = result_col;
  298. }
  299. return;
  300. }
  301. if (p_is_searching_next) {
  302. r_line = text_editor->get_selection_to_line();
  303. r_col = text_editor->get_selection_to_column();
  304. } else {
  305. r_line = text_editor->get_selection_from_line();
  306. r_col = text_editor->get_selection_from_column();
  307. }
  308. }
  309. void FindReplaceBar::_update_results_count() {
  310. if (!needs_to_count_results && (result_line != -1) && results_count_to_current > 0) {
  311. results_count_to_current += (flags & TextEdit::SEARCH_BACKWARDS) ? -1 : 1;
  312. if (results_count_to_current > results_count) {
  313. results_count_to_current = results_count_to_current - results_count;
  314. } else if (results_count_to_current <= 0) {
  315. results_count_to_current = results_count;
  316. }
  317. return;
  318. }
  319. String searched = get_search_text();
  320. if (searched.is_empty()) {
  321. return;
  322. }
  323. needs_to_count_results = false;
  324. results_count = 0;
  325. for (int i = 0; i < text_editor->get_line_count(); i++) {
  326. String line_text = text_editor->get_line(i);
  327. int col_pos = 0;
  328. bool searched_start_is_symbol = is_symbol(searched[0]);
  329. bool searched_end_is_symbol = is_symbol(searched[searched.length() - 1]);
  330. while (true) {
  331. col_pos = is_case_sensitive() ? line_text.find(searched, col_pos) : line_text.findn(searched, col_pos);
  332. if (col_pos == -1) {
  333. break;
  334. }
  335. if (is_whole_words()) {
  336. if (!searched_start_is_symbol && col_pos > 0 && !is_symbol(line_text[col_pos - 1])) {
  337. col_pos += searched.length();
  338. continue;
  339. }
  340. if (!searched_end_is_symbol && col_pos + searched.length() < line_text.length() && !is_symbol(line_text[col_pos + searched.length()])) {
  341. col_pos += searched.length();
  342. continue;
  343. }
  344. }
  345. results_count++;
  346. if (i == result_line) {
  347. if (col_pos == result_col) {
  348. results_count_to_current = results_count;
  349. } else if (col_pos < result_col && col_pos + searched.length() > result_col) {
  350. col_pos = result_col;
  351. results_count_to_current = results_count;
  352. }
  353. }
  354. col_pos += searched.length();
  355. }
  356. }
  357. }
  358. void FindReplaceBar::_update_matches_display() {
  359. if (search_text->get_text().is_empty() || results_count == -1) {
  360. matches_label->hide();
  361. } else {
  362. matches_label->show();
  363. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  364. if (results_count == 0) {
  365. matches_label->set_text(TTR("No match"));
  366. } else if (results_count_to_current == -1) {
  367. matches_label->set_text(vformat(TTRN("%d match", "%d matches", results_count), results_count));
  368. } else {
  369. matches_label->set_text(vformat(TTRN("%d of %d match", "%d of %d matches", results_count), results_count_to_current, results_count));
  370. }
  371. }
  372. find_prev->set_disabled(results_count < 1);
  373. find_next->set_disabled(results_count < 1);
  374. replace->set_disabled(search_text->get_text().is_empty());
  375. replace_all->set_disabled(search_text->get_text().is_empty());
  376. }
  377. bool FindReplaceBar::search_current() {
  378. _update_flags(false);
  379. int line, col;
  380. _get_search_from(line, col);
  381. return _search(flags, line, col);
  382. }
  383. bool FindReplaceBar::search_prev() {
  384. if (is_selection_only() && !replace_all_mode) {
  385. return false;
  386. }
  387. if (!is_visible()) {
  388. popup_search(true);
  389. }
  390. String text = get_search_text();
  391. _update_flags(true);
  392. int line, col;
  393. _get_search_from(line, col);
  394. col -= text.length();
  395. if (col < 0) {
  396. line -= 1;
  397. if (line < 0) {
  398. line = text_editor->get_line_count() - 1;
  399. }
  400. col = text_editor->get_line(line).length();
  401. }
  402. return _search(flags, line, col);
  403. }
  404. bool FindReplaceBar::search_next() {
  405. if (is_selection_only() && !replace_all_mode) {
  406. return false;
  407. }
  408. if (!is_visible()) {
  409. popup_search(true);
  410. }
  411. _update_flags(false);
  412. int line, col;
  413. _get_search_from(line, col, true);
  414. return _search(flags, line, col);
  415. }
  416. void FindReplaceBar::_hide_bar(bool p_force_focus) {
  417. if (replace_text->has_focus() || search_text->has_focus() || p_force_focus) {
  418. text_editor->grab_focus();
  419. }
  420. text_editor->set_search_text("");
  421. result_line = -1;
  422. result_col = -1;
  423. hide();
  424. }
  425. void FindReplaceBar::_show_search(bool p_with_replace, bool p_show_only) {
  426. show();
  427. if (p_show_only) {
  428. return;
  429. }
  430. const bool on_one_line = text_editor->has_selection(0) && text_editor->get_selection_from_line(0) == text_editor->get_selection_to_line(0);
  431. const bool focus_replace = p_with_replace && on_one_line;
  432. if (focus_replace) {
  433. search_text->deselect();
  434. callable_mp((Control *)replace_text, &Control::grab_focus).call_deferred();
  435. } else {
  436. replace_text->deselect();
  437. callable_mp((Control *)search_text, &Control::grab_focus).call_deferred();
  438. }
  439. if (on_one_line) {
  440. search_text->set_text(text_editor->get_selected_text(0));
  441. result_line = text_editor->get_selection_from_line();
  442. result_col = text_editor->get_selection_from_column();
  443. }
  444. if (!get_search_text().is_empty()) {
  445. if (focus_replace) {
  446. replace_text->select_all();
  447. replace_text->set_caret_column(replace_text->get_text().length());
  448. } else {
  449. search_text->select_all();
  450. search_text->set_caret_column(search_text->get_text().length());
  451. }
  452. preserve_cursor = true;
  453. _search_text_changed(get_search_text());
  454. preserve_cursor = false;
  455. }
  456. }
  457. void FindReplaceBar::popup_search(bool p_show_only) {
  458. replace_text->hide();
  459. hbc_button_replace->hide();
  460. hbc_option_replace->hide();
  461. selection_only->set_pressed(false);
  462. _show_search(false, p_show_only);
  463. }
  464. void FindReplaceBar::popup_replace() {
  465. if (!replace_text->is_visible_in_tree()) {
  466. replace_text->show();
  467. hbc_button_replace->show();
  468. hbc_option_replace->show();
  469. }
  470. selection_only->set_pressed(text_editor->has_selection(0) && text_editor->get_selection_from_line(0) < text_editor->get_selection_to_line(0));
  471. _show_search(true, false);
  472. }
  473. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  474. results_count = -1;
  475. results_count_to_current = -1;
  476. needs_to_count_results = true;
  477. search_current();
  478. }
  479. void FindReplaceBar::_editor_text_changed() {
  480. results_count = -1;
  481. results_count_to_current = -1;
  482. needs_to_count_results = true;
  483. if (is_visible_in_tree()) {
  484. preserve_cursor = true;
  485. search_current();
  486. preserve_cursor = false;
  487. }
  488. }
  489. void FindReplaceBar::_search_text_changed(const String &p_text) {
  490. results_count = -1;
  491. results_count_to_current = -1;
  492. needs_to_count_results = true;
  493. search_current();
  494. }
  495. void FindReplaceBar::_search_text_submitted(const String &p_text) {
  496. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  497. search_prev();
  498. } else {
  499. search_next();
  500. }
  501. }
  502. void FindReplaceBar::_replace_text_submitted(const String &p_text) {
  503. if (selection_only->is_pressed() && text_editor->has_selection(0)) {
  504. _replace_all();
  505. _hide_bar();
  506. } else if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  507. _replace();
  508. search_prev();
  509. } else {
  510. _replace();
  511. }
  512. }
  513. String FindReplaceBar::get_search_text() const {
  514. return search_text->get_text();
  515. }
  516. String FindReplaceBar::get_replace_text() const {
  517. return replace_text->get_text();
  518. }
  519. bool FindReplaceBar::is_case_sensitive() const {
  520. return case_sensitive->is_pressed();
  521. }
  522. bool FindReplaceBar::is_whole_words() const {
  523. return whole_words->is_pressed();
  524. }
  525. bool FindReplaceBar::is_selection_only() const {
  526. return selection_only->is_pressed();
  527. }
  528. void FindReplaceBar::set_error(const String &p_label) {
  529. emit_signal(SNAME("error"), p_label);
  530. }
  531. void FindReplaceBar::set_text_edit(CodeTextEditor *p_text_editor) {
  532. if (p_text_editor == base_text_editor) {
  533. return;
  534. }
  535. if (base_text_editor) {
  536. base_text_editor->remove_find_replace_bar();
  537. base_text_editor = nullptr;
  538. text_editor->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  539. text_editor = nullptr;
  540. }
  541. if (!p_text_editor) {
  542. return;
  543. }
  544. results_count = -1;
  545. results_count_to_current = -1;
  546. needs_to_count_results = true;
  547. base_text_editor = p_text_editor;
  548. text_editor = base_text_editor->get_text_editor();
  549. text_editor->connect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed));
  550. _update_results_count();
  551. _update_matches_display();
  552. }
  553. void FindReplaceBar::_bind_methods() {
  554. ClassDB::bind_method("_search_current", &FindReplaceBar::search_current);
  555. ADD_SIGNAL(MethodInfo("error"));
  556. }
  557. FindReplaceBar::FindReplaceBar() {
  558. vbc_lineedit = memnew(VBoxContainer);
  559. add_child(vbc_lineedit);
  560. vbc_lineedit->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  561. vbc_lineedit->set_h_size_flags(SIZE_EXPAND_FILL);
  562. VBoxContainer *vbc_button = memnew(VBoxContainer);
  563. add_child(vbc_button);
  564. VBoxContainer *vbc_option = memnew(VBoxContainer);
  565. add_child(vbc_option);
  566. HBoxContainer *hbc_button_search = memnew(HBoxContainer);
  567. vbc_button->add_child(hbc_button_search);
  568. hbc_button_search->set_alignment(BoxContainer::ALIGNMENT_END);
  569. hbc_button_replace = memnew(HBoxContainer);
  570. vbc_button->add_child(hbc_button_replace);
  571. hbc_button_replace->set_alignment(BoxContainer::ALIGNMENT_END);
  572. HBoxContainer *hbc_option_search = memnew(HBoxContainer);
  573. vbc_option->add_child(hbc_option_search);
  574. hbc_option_replace = memnew(HBoxContainer);
  575. vbc_option->add_child(hbc_option_replace);
  576. // Search toolbar
  577. search_text = memnew(LineEdit);
  578. vbc_lineedit->add_child(search_text);
  579. search_text->set_placeholder(TTR("Find"));
  580. search_text->set_tooltip_text(TTR("Find"));
  581. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  582. search_text->connect("text_changed", callable_mp(this, &FindReplaceBar::_search_text_changed));
  583. search_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_search_text_submitted));
  584. search_text->connect("focus_exited", callable_mp(this, &FindReplaceBar::_focus_lost));
  585. matches_label = memnew(Label);
  586. hbc_button_search->add_child(matches_label);
  587. matches_label->hide();
  588. find_prev = memnew(Button);
  589. find_prev->set_flat(true);
  590. find_prev->set_tooltip_text(TTR("Previous Match"));
  591. hbc_button_search->add_child(find_prev);
  592. find_prev->set_focus_mode(FOCUS_NONE);
  593. find_prev->connect("pressed", callable_mp(this, &FindReplaceBar::search_prev));
  594. find_next = memnew(Button);
  595. find_next->set_flat(true);
  596. find_next->set_tooltip_text(TTR("Next Match"));
  597. hbc_button_search->add_child(find_next);
  598. find_next->set_focus_mode(FOCUS_NONE);
  599. find_next->connect("pressed", callable_mp(this, &FindReplaceBar::search_next));
  600. case_sensitive = memnew(CheckBox);
  601. hbc_option_search->add_child(case_sensitive);
  602. case_sensitive->set_text(TTR("Match Case"));
  603. case_sensitive->set_focus_mode(FOCUS_NONE);
  604. case_sensitive->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  605. whole_words = memnew(CheckBox);
  606. hbc_option_search->add_child(whole_words);
  607. whole_words->set_text(TTR("Whole Words"));
  608. whole_words->set_focus_mode(FOCUS_NONE);
  609. whole_words->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  610. // Replace toolbar
  611. replace_text = memnew(LineEdit);
  612. vbc_lineedit->add_child(replace_text);
  613. replace_text->set_placeholder(TTR("Replace"));
  614. replace_text->set_tooltip_text(TTR("Replace"));
  615. replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  616. replace_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_replace_text_submitted));
  617. replace_text->connect("focus_exited", callable_mp(this, &FindReplaceBar::_focus_lost));
  618. replace = memnew(Button);
  619. hbc_button_replace->add_child(replace);
  620. replace->set_text(TTR("Replace"));
  621. replace->connect("pressed", callable_mp(this, &FindReplaceBar::_replace));
  622. replace_all = memnew(Button);
  623. hbc_button_replace->add_child(replace_all);
  624. replace_all->set_text(TTR("Replace All"));
  625. replace_all->connect("pressed", callable_mp(this, &FindReplaceBar::_replace_all));
  626. selection_only = memnew(CheckBox);
  627. hbc_option_replace->add_child(selection_only);
  628. selection_only->set_text(TTR("Selection Only"));
  629. selection_only->set_focus_mode(FOCUS_NONE);
  630. selection_only->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed));
  631. hide_button = memnew(TextureButton);
  632. add_child(hide_button);
  633. hide_button->set_tooltip_text(TTR("Hide"));
  634. hide_button->set_focus_mode(FOCUS_NONE);
  635. hide_button->connect("pressed", callable_mp(this, &FindReplaceBar::_hide_bar).bind(false));
  636. hide_button->set_v_size_flags(SIZE_SHRINK_CENTER);
  637. }
  638. /*** CODE EDITOR ****/
  639. static constexpr float ZOOM_FACTOR_PRESETS[7] = { 0.25f, 0.5f, 0.75f, 1.0f, 1.5f, 2.0f, 3.0f };
  640. // This function should be used to handle shortcuts that could otherwise
  641. // be handled too late if they weren't handled here.
  642. void CodeTextEditor::input(const Ref<InputEvent> &event) {
  643. ERR_FAIL_COND(event.is_null());
  644. const Ref<InputEventKey> key_event = event;
  645. if (!key_event.is_valid()) {
  646. return;
  647. }
  648. if (!key_event->is_pressed()) {
  649. return;
  650. }
  651. if (!text_editor->has_focus()) {
  652. if ((find_replace_bar != nullptr && find_replace_bar->is_visible()) && (find_replace_bar->has_focus() || (get_viewport()->gui_get_focus_owner() && find_replace_bar->is_ancestor_of(get_viewport()->gui_get_focus_owner())))) {
  653. if (ED_IS_SHORTCUT("script_text_editor/find_next", key_event)) {
  654. find_replace_bar->search_next();
  655. accept_event();
  656. return;
  657. }
  658. if (ED_IS_SHORTCUT("script_text_editor/find_previous", key_event)) {
  659. find_replace_bar->search_prev();
  660. accept_event();
  661. return;
  662. }
  663. }
  664. return;
  665. }
  666. if (ED_IS_SHORTCUT("script_text_editor/move_up", key_event)) {
  667. move_lines_up();
  668. accept_event();
  669. return;
  670. }
  671. if (ED_IS_SHORTCUT("script_text_editor/move_down", key_event)) {
  672. move_lines_down();
  673. accept_event();
  674. return;
  675. }
  676. if (ED_IS_SHORTCUT("script_text_editor/delete_line", key_event)) {
  677. delete_lines();
  678. accept_event();
  679. return;
  680. }
  681. if (ED_IS_SHORTCUT("script_text_editor/duplicate_selection", key_event)) {
  682. duplicate_selection();
  683. accept_event();
  684. return;
  685. }
  686. if (ED_IS_SHORTCUT("script_text_editor/duplicate_lines", key_event)) {
  687. text_editor->duplicate_lines();
  688. accept_event();
  689. return;
  690. }
  691. }
  692. void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  693. Ref<InputEventMouseButton> mb = p_event;
  694. if (mb.is_valid()) {
  695. if (mb->is_pressed() && mb->is_command_or_control_pressed()) {
  696. if (mb->get_button_index() == MouseButton::WHEEL_UP) {
  697. _zoom_in();
  698. accept_event();
  699. return;
  700. }
  701. if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  702. _zoom_out();
  703. accept_event();
  704. return;
  705. }
  706. }
  707. }
  708. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  709. if (magnify_gesture.is_valid()) {
  710. _zoom_to(zoom_factor * powf(magnify_gesture->get_factor(), 0.25f));
  711. accept_event();
  712. return;
  713. }
  714. Ref<InputEventKey> k = p_event;
  715. if (k.is_valid()) {
  716. if (k->is_pressed()) {
  717. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  718. _zoom_in();
  719. accept_event();
  720. return;
  721. }
  722. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  723. _zoom_out();
  724. accept_event();
  725. return;
  726. }
  727. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  728. _zoom_to(1);
  729. accept_event();
  730. return;
  731. }
  732. }
  733. }
  734. }
  735. void CodeTextEditor::_line_col_changed() {
  736. if (!code_complete_timer->is_stopped() && code_complete_timer_line != text_editor->get_caret_line()) {
  737. code_complete_timer->stop();
  738. }
  739. String line = text_editor->get_line(text_editor->get_caret_line());
  740. int positional_column = 0;
  741. for (int i = 0; i < text_editor->get_caret_column(); i++) {
  742. if (line[i] == '\t') {
  743. positional_column += text_editor->get_indent_size(); // Tab size
  744. } else {
  745. positional_column += 1;
  746. }
  747. }
  748. StringBuilder sb;
  749. sb.append(itos(text_editor->get_caret_line() + 1).lpad(4));
  750. sb.append(" : ");
  751. sb.append(itos(positional_column + 1).lpad(3));
  752. line_and_col_txt->set_text(sb.as_string());
  753. if (find_replace_bar) {
  754. if (!find_replace_bar->line_col_changed_for_result) {
  755. find_replace_bar->needs_to_count_results = true;
  756. }
  757. find_replace_bar->line_col_changed_for_result = false;
  758. }
  759. }
  760. void CodeTextEditor::_text_changed() {
  761. if (code_complete_enabled && text_editor->is_insert_text_operation()) {
  762. code_complete_timer_line = text_editor->get_caret_line();
  763. code_complete_timer->start();
  764. }
  765. idle->start();
  766. if (find_replace_bar) {
  767. find_replace_bar->needs_to_count_results = true;
  768. }
  769. }
  770. void CodeTextEditor::_code_complete_timer_timeout() {
  771. if (!is_visible_in_tree()) {
  772. return;
  773. }
  774. text_editor->request_code_completion();
  775. }
  776. void CodeTextEditor::_complete_request() {
  777. List<ScriptLanguage::CodeCompletionOption> entries;
  778. String ctext = text_editor->get_text_for_code_completion();
  779. _code_complete_script(ctext, &entries);
  780. bool forced = false;
  781. if (code_complete_func) {
  782. code_complete_func(code_complete_ud, ctext, &entries, forced);
  783. }
  784. for (const ScriptLanguage::CodeCompletionOption &e : entries) {
  785. Color font_color = completion_font_color;
  786. if (!e.theme_color_name.is_empty() && EDITOR_GET("text_editor/completion/colorize_suggestions")) {
  787. font_color = get_theme_color(e.theme_color_name, SNAME("Editor"));
  788. } else if (e.insert_text.begins_with("\"") || e.insert_text.begins_with("\'")) {
  789. font_color = completion_string_color;
  790. } else if (e.insert_text.begins_with("##") || e.insert_text.begins_with("///")) {
  791. font_color = completion_doc_comment_color;
  792. } else if (e.insert_text.begins_with("&")) {
  793. font_color = completion_string_name_color;
  794. } else if (e.insert_text.begins_with("^")) {
  795. font_color = completion_node_path_color;
  796. } else if (e.insert_text.begins_with("#") || e.insert_text.begins_with("//")) {
  797. font_color = completion_comment_color;
  798. }
  799. text_editor->add_code_completion_option((CodeEdit::CodeCompletionKind)e.kind, e.display, e.insert_text, font_color, _get_completion_icon(e), e.default_value, e.location);
  800. }
  801. text_editor->update_code_completion_options(forced);
  802. }
  803. Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptLanguage::CodeCompletionOption &p_option) {
  804. Ref<Texture2D> tex;
  805. switch (p_option.kind) {
  806. case ScriptLanguage::CODE_COMPLETION_KIND_CLASS: {
  807. if (has_theme_icon(p_option.display, EditorStringName(EditorIcons))) {
  808. tex = get_editor_theme_icon(p_option.display);
  809. } else {
  810. tex = get_editor_theme_icon(SNAME("Object"));
  811. }
  812. } break;
  813. case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:
  814. tex = get_editor_theme_icon(SNAME("Enum"));
  815. break;
  816. case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:
  817. tex = get_editor_theme_icon(SNAME("File"));
  818. break;
  819. case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:
  820. tex = get_editor_theme_icon(SNAME("NodePath"));
  821. break;
  822. case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:
  823. tex = get_editor_theme_icon(SNAME("Variant"));
  824. break;
  825. case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:
  826. tex = get_editor_theme_icon(SNAME("MemberConstant"));
  827. break;
  828. case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:
  829. tex = get_editor_theme_icon(SNAME("MemberProperty"));
  830. break;
  831. case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:
  832. tex = get_editor_theme_icon(SNAME("MemberSignal"));
  833. break;
  834. case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:
  835. tex = get_editor_theme_icon(SNAME("MemberMethod"));
  836. break;
  837. case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:
  838. tex = get_editor_theme_icon(SNAME("BoxMesh"));
  839. break;
  840. default:
  841. tex = get_editor_theme_icon(SNAME("String"));
  842. break;
  843. }
  844. return tex;
  845. }
  846. void CodeTextEditor::update_editor_settings() {
  847. // Theme: Highlighting
  848. completion_font_color = EDITOR_GET("text_editor/theme/highlighting/completion_font_color");
  849. completion_string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  850. completion_string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");
  851. completion_node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
  852. completion_comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  853. completion_doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
  854. // Appearance: Caret
  855. text_editor->set_caret_type((TextEdit::CaretType)EDITOR_GET("text_editor/appearance/caret/type").operator int());
  856. text_editor->set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
  857. text_editor->set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
  858. text_editor->set_highlight_current_line(EDITOR_GET("text_editor/appearance/caret/highlight_current_line"));
  859. text_editor->set_highlight_all_occurrences(EDITOR_GET("text_editor/appearance/caret/highlight_all_occurrences"));
  860. // Appearance: Gutters
  861. text_editor->set_draw_line_numbers(EDITOR_GET("text_editor/appearance/gutters/show_line_numbers"));
  862. text_editor->set_line_numbers_zero_padded(EDITOR_GET("text_editor/appearance/gutters/line_numbers_zero_padded"));
  863. // Appearance: Minimap
  864. text_editor->set_draw_minimap(EDITOR_GET("text_editor/appearance/minimap/show_minimap"));
  865. text_editor->set_minimap_width((int)EDITOR_GET("text_editor/appearance/minimap/minimap_width") * EDSCALE);
  866. // Appearance: Lines
  867. text_editor->set_line_folding_enabled(EDITOR_GET("text_editor/appearance/lines/code_folding"));
  868. text_editor->set_draw_fold_gutter(EDITOR_GET("text_editor/appearance/lines/code_folding"));
  869. text_editor->set_line_wrapping_mode((TextEdit::LineWrappingMode)EDITOR_GET("text_editor/appearance/lines/word_wrap").operator int());
  870. text_editor->set_autowrap_mode((TextServer::AutowrapMode)EDITOR_GET("text_editor/appearance/lines/autowrap_mode").operator int());
  871. // Appearance: Whitespace
  872. text_editor->set_draw_tabs(EDITOR_GET("text_editor/appearance/whitespace/draw_tabs"));
  873. text_editor->set_draw_spaces(EDITOR_GET("text_editor/appearance/whitespace/draw_spaces"));
  874. text_editor->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));
  875. // Behavior: Navigation
  876. text_editor->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file"));
  877. text_editor->set_smooth_scroll_enabled(EDITOR_GET("text_editor/behavior/navigation/smooth_scrolling"));
  878. text_editor->set_v_scroll_speed(EDITOR_GET("text_editor/behavior/navigation/v_scroll_speed"));
  879. text_editor->set_drag_and_drop_selection_enabled(EDITOR_GET("text_editor/behavior/navigation/drag_and_drop_selection"));
  880. // Behavior: Indent
  881. set_indent_using_spaces(EDITOR_GET("text_editor/behavior/indent/type"));
  882. text_editor->set_indent_size(EDITOR_GET("text_editor/behavior/indent/size"));
  883. text_editor->set_auto_indent_enabled(EDITOR_GET("text_editor/behavior/indent/auto_indent"));
  884. text_editor->set_indent_wrapped_lines(EDITOR_GET("text_editor/behavior/indent/indent_wrapped_lines"));
  885. // Completion
  886. text_editor->set_auto_brace_completion_enabled(EDITOR_GET("text_editor/completion/auto_brace_complete"));
  887. text_editor->set_code_hint_draw_below(EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"));
  888. code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled");
  889. code_complete_timer->set_wait_time(EDITOR_GET("text_editor/completion/code_complete_delay"));
  890. idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay"));
  891. // Appearance: Guidelines
  892. if (EDITOR_GET("text_editor/appearance/guidelines/show_line_length_guidelines")) {
  893. TypedArray<int> guideline_cols;
  894. guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_hard_column"));
  895. if (EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column") != guideline_cols[0]) {
  896. guideline_cols.append(EDITOR_GET("text_editor/appearance/guidelines/line_length_guideline_soft_column"));
  897. }
  898. text_editor->set_line_length_guidelines(guideline_cols);
  899. } else {
  900. text_editor->set_line_length_guidelines(TypedArray<int>());
  901. }
  902. set_zoom_factor(zoom_factor);
  903. }
  904. void CodeTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
  905. if (find_replace_bar) {
  906. return;
  907. }
  908. find_replace_bar = p_bar;
  909. find_replace_bar->set_text_edit(this);
  910. find_replace_bar->connect("error", callable_mp(error, &Label::set_text));
  911. }
  912. void CodeTextEditor::remove_find_replace_bar() {
  913. if (!find_replace_bar) {
  914. return;
  915. }
  916. find_replace_bar->disconnect("error", callable_mp(error, &Label::set_text));
  917. find_replace_bar = nullptr;
  918. }
  919. void CodeTextEditor::trim_trailing_whitespace() {
  920. bool trimmed_whitespace = false;
  921. for (int i = 0; i < text_editor->get_line_count(); i++) {
  922. String line = text_editor->get_line(i);
  923. if (line.ends_with(" ") || line.ends_with("\t")) {
  924. if (!trimmed_whitespace) {
  925. text_editor->begin_complex_operation();
  926. trimmed_whitespace = true;
  927. }
  928. int end = 0;
  929. for (int j = line.length() - 1; j > -1; j--) {
  930. if (line[j] != ' ' && line[j] != '\t') {
  931. end = j + 1;
  932. break;
  933. }
  934. }
  935. text_editor->set_line(i, line.substr(0, end));
  936. }
  937. }
  938. if (trimmed_whitespace) {
  939. text_editor->merge_overlapping_carets();
  940. text_editor->end_complex_operation();
  941. text_editor->queue_redraw();
  942. }
  943. }
  944. void CodeTextEditor::insert_final_newline() {
  945. int final_line = text_editor->get_line_count() - 1;
  946. String line = text_editor->get_line(final_line);
  947. // Length 0 means it's already an empty line, no need to add a newline.
  948. if (line.length() > 0 && !line.ends_with("\n")) {
  949. text_editor->begin_complex_operation();
  950. line += "\n";
  951. text_editor->set_line(final_line, line);
  952. text_editor->end_complex_operation();
  953. text_editor->queue_redraw();
  954. }
  955. }
  956. void CodeTextEditor::convert_case(CaseStyle p_case) {
  957. if (!text_editor->has_selection()) {
  958. return;
  959. }
  960. text_editor->begin_complex_operation();
  961. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  962. for (const int &c : caret_edit_order) {
  963. if (!text_editor->has_selection(c)) {
  964. continue;
  965. }
  966. int begin = text_editor->get_selection_from_line(c);
  967. int end = text_editor->get_selection_to_line(c);
  968. int begin_col = text_editor->get_selection_from_column(c);
  969. int end_col = text_editor->get_selection_to_column(c);
  970. for (int i = begin; i <= end; i++) {
  971. int len = text_editor->get_line(i).length();
  972. if (i == end) {
  973. len = end_col;
  974. }
  975. if (i == begin) {
  976. len -= begin_col;
  977. }
  978. String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len);
  979. switch (p_case) {
  980. case UPPER: {
  981. new_line = new_line.to_upper();
  982. } break;
  983. case LOWER: {
  984. new_line = new_line.to_lower();
  985. } break;
  986. case CAPITALIZE: {
  987. new_line = new_line.capitalize();
  988. } break;
  989. }
  990. if (i == begin) {
  991. new_line = text_editor->get_line(i).left(begin_col) + new_line;
  992. }
  993. if (i == end) {
  994. new_line = new_line + text_editor->get_line(i).substr(end_col);
  995. }
  996. text_editor->set_line(i, new_line);
  997. }
  998. }
  999. text_editor->end_complex_operation();
  1000. }
  1001. void CodeTextEditor::set_indent_using_spaces(bool p_use_spaces) {
  1002. text_editor->set_indent_using_spaces(p_use_spaces);
  1003. indentation_txt->set_text(p_use_spaces ? TTR("Spaces", "Indentation") : TTR("Tabs", "Indentation"));
  1004. }
  1005. void CodeTextEditor::move_lines_up() {
  1006. text_editor->begin_complex_operation();
  1007. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1008. // Lists of carets representing each group.
  1009. Vector<Vector<int>> caret_groups;
  1010. Vector<Pair<int, int>> group_borders;
  1011. // Search for groups of carets and their selections residing on the same lines.
  1012. for (int i = 0; i < caret_edit_order.size(); i++) {
  1013. int c = caret_edit_order[i];
  1014. Vector<int> new_group{ c };
  1015. Pair<int, int> group_border;
  1016. group_border.first = _get_affected_lines_from(c);
  1017. group_border.second = _get_affected_lines_to(c);
  1018. for (int j = i; j < caret_edit_order.size() - 1; j++) {
  1019. int c_current = caret_edit_order[j];
  1020. int c_next = caret_edit_order[j + 1];
  1021. int next_start_pos = _get_affected_lines_from(c_next);
  1022. int next_end_pos = _get_affected_lines_to(c_next);
  1023. int current_start_pos = text_editor->has_selection(c_current) ? text_editor->get_selection_from_line(c_current) : text_editor->get_caret_line(c_current);
  1024. i = j;
  1025. if (next_end_pos != current_start_pos && next_end_pos + 1 != current_start_pos) {
  1026. break;
  1027. }
  1028. group_border.first = next_start_pos;
  1029. new_group.push_back(c_next);
  1030. // If the last caret is added to the current group there is no need to process it again.
  1031. if (j + 1 == caret_edit_order.size() - 1) {
  1032. i++;
  1033. }
  1034. }
  1035. group_borders.push_back(group_border);
  1036. caret_groups.push_back(new_group);
  1037. }
  1038. for (int i = group_borders.size() - 1; i >= 0; i--) {
  1039. if (group_borders[i].first - 1 < 0) {
  1040. continue;
  1041. }
  1042. // If the group starts overlapping with the upper group don't move it.
  1043. if (i < group_borders.size() - 1 && group_borders[i].first - 1 <= group_borders[i + 1].second) {
  1044. continue;
  1045. }
  1046. // We have to remember caret positions and selections prior to line swapping.
  1047. Vector<Vector<int>> caret_group_parameters;
  1048. for (int j = 0; j < caret_groups[i].size(); j++) {
  1049. int c = caret_groups[i][j];
  1050. int cursor_line = text_editor->get_caret_line(c);
  1051. int cursor_column = text_editor->get_caret_column(c);
  1052. if (!text_editor->has_selection(c)) {
  1053. caret_group_parameters.push_back(Vector<int>{ -1, -1, -1, -1, cursor_line, cursor_column });
  1054. continue;
  1055. }
  1056. int from_line = text_editor->get_selection_from_line(c);
  1057. int from_col = text_editor->get_selection_from_column(c);
  1058. int to_line = text_editor->get_selection_to_line(c);
  1059. int to_column = text_editor->get_selection_to_column(c);
  1060. caret_group_parameters.push_back(Vector<int>{ from_line, from_col, to_line, to_column, cursor_line, cursor_column });
  1061. }
  1062. for (int line_id = group_borders[i].first; line_id <= group_borders[i].second; line_id++) {
  1063. text_editor->unfold_line(line_id);
  1064. text_editor->unfold_line(line_id - 1);
  1065. text_editor->swap_lines(line_id - 1, line_id);
  1066. }
  1067. for (int j = 0; j < caret_groups[i].size(); j++) {
  1068. int c = caret_groups[i][j];
  1069. const Vector<int> &caret_parameters = caret_group_parameters[j];
  1070. text_editor->set_caret_line(caret_parameters[4] - 1, c == 0, true, 0, c);
  1071. text_editor->set_caret_column(caret_parameters[5], c == 0, c);
  1072. if (caret_parameters[0] >= 0) {
  1073. text_editor->select(caret_parameters[0] - 1, caret_parameters[1], caret_parameters[2] - 1, caret_parameters[3], c);
  1074. }
  1075. }
  1076. }
  1077. text_editor->end_complex_operation();
  1078. text_editor->merge_overlapping_carets();
  1079. text_editor->queue_redraw();
  1080. }
  1081. void CodeTextEditor::move_lines_down() {
  1082. text_editor->begin_complex_operation();
  1083. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1084. // Lists of carets representing each group.
  1085. Vector<Vector<int>> caret_groups;
  1086. Vector<Pair<int, int>> group_borders;
  1087. Vector<int> group_border_ends;
  1088. // Search for groups of carets and their selections residing on the same lines.
  1089. for (int i = 0; i < caret_edit_order.size(); i++) {
  1090. int c = caret_edit_order[i];
  1091. Vector<int> new_group{ c };
  1092. Pair<int, int> group_border;
  1093. group_border.first = _get_affected_lines_from(c);
  1094. group_border.second = _get_affected_lines_to(c);
  1095. for (int j = i; j < caret_edit_order.size() - 1; j++) {
  1096. int c_current = caret_edit_order[j];
  1097. int c_next = caret_edit_order[j + 1];
  1098. int next_start_pos = _get_affected_lines_from(c_next);
  1099. int next_end_pos = _get_affected_lines_to(c_next);
  1100. int current_start_pos = text_editor->has_selection(c_current) ? text_editor->get_selection_from_line(c_current) : text_editor->get_caret_line(c_current);
  1101. i = j;
  1102. if (next_end_pos == current_start_pos || next_end_pos + 1 == current_start_pos) {
  1103. group_border.first = next_start_pos;
  1104. new_group.push_back(c_next);
  1105. // If the last caret is added to the current group there is no need to process it again.
  1106. if (j + 1 == caret_edit_order.size() - 1) {
  1107. i++;
  1108. }
  1109. } else {
  1110. break;
  1111. }
  1112. }
  1113. group_borders.push_back(group_border);
  1114. group_border_ends.push_back(text_editor->has_selection(c) ? text_editor->get_selection_to_line(c) : text_editor->get_caret_line(c));
  1115. caret_groups.push_back(new_group);
  1116. }
  1117. for (int i = 0; i < group_borders.size(); i++) {
  1118. if (group_border_ends[i] + 1 > text_editor->get_line_count() - 1) {
  1119. continue;
  1120. }
  1121. // If the group starts overlapping with the upper group don't move it.
  1122. if (i > 0 && group_border_ends[i] + 1 >= group_borders[i - 1].first) {
  1123. continue;
  1124. }
  1125. // We have to remember caret positions and selections prior to line swapping.
  1126. Vector<Vector<int>> caret_group_parameters;
  1127. for (int j = 0; j < caret_groups[i].size(); j++) {
  1128. int c = caret_groups[i][j];
  1129. int cursor_line = text_editor->get_caret_line(c);
  1130. int cursor_column = text_editor->get_caret_column(c);
  1131. if (!text_editor->has_selection(c)) {
  1132. caret_group_parameters.push_back(Vector<int>{ -1, -1, -1, -1, cursor_line, cursor_column });
  1133. continue;
  1134. }
  1135. int from_line = text_editor->get_selection_from_line(c);
  1136. int from_col = text_editor->get_selection_from_column(c);
  1137. int to_line = text_editor->get_selection_to_line(c);
  1138. int to_column = text_editor->get_selection_to_column(c);
  1139. caret_group_parameters.push_back(Vector<int>{ from_line, from_col, to_line, to_column, cursor_line, cursor_column });
  1140. }
  1141. for (int line_id = group_borders[i].second; line_id >= group_borders[i].first; line_id--) {
  1142. text_editor->unfold_line(line_id);
  1143. text_editor->unfold_line(line_id + 1);
  1144. text_editor->swap_lines(line_id + 1, line_id);
  1145. }
  1146. for (int j = 0; j < caret_groups[i].size(); j++) {
  1147. int c = caret_groups[i][j];
  1148. const Vector<int> &caret_parameters = caret_group_parameters[j];
  1149. text_editor->set_caret_line(caret_parameters[4] + 1, c == 0, true, 0, c);
  1150. text_editor->set_caret_column(caret_parameters[5], c == 0, c);
  1151. if (caret_parameters[0] >= 0) {
  1152. text_editor->select(caret_parameters[0] + 1, caret_parameters[1], caret_parameters[2] + 1, caret_parameters[3], c);
  1153. }
  1154. }
  1155. }
  1156. text_editor->merge_overlapping_carets();
  1157. text_editor->end_complex_operation();
  1158. text_editor->queue_redraw();
  1159. }
  1160. void CodeTextEditor::delete_lines() {
  1161. text_editor->begin_complex_operation();
  1162. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1163. Vector<int> lines;
  1164. int last_line = INT_MAX;
  1165. for (const int &c : caret_edit_order) {
  1166. for (int line = _get_affected_lines_to(c); line >= _get_affected_lines_from(c); line--) {
  1167. if (line >= last_line) {
  1168. continue;
  1169. }
  1170. last_line = line;
  1171. lines.append(line);
  1172. }
  1173. }
  1174. for (const int &line : lines) {
  1175. if (line != text_editor->get_line_count() - 1) {
  1176. text_editor->remove_text(line, 0, line + 1, 0);
  1177. } else {
  1178. text_editor->remove_text(line - 1, text_editor->get_line(line - 1).length(), line, text_editor->get_line(line).length());
  1179. }
  1180. // Readjust carets.
  1181. int new_line = MIN(line, text_editor->get_line_count() - 1);
  1182. text_editor->unfold_line(new_line);
  1183. for (const int &c : caret_edit_order) {
  1184. if (text_editor->get_caret_line(c) == line || (text_editor->get_caret_line(c) == line + 1 && text_editor->get_caret_column(c) == 0)) {
  1185. text_editor->deselect(c);
  1186. text_editor->set_caret_line(new_line, c == 0, true, 0, c);
  1187. continue;
  1188. }
  1189. if (text_editor->get_caret_line(c) > line) {
  1190. text_editor->set_caret_line(text_editor->get_caret_line(c) - 1, c == 0, true, 0, c);
  1191. continue;
  1192. }
  1193. break;
  1194. }
  1195. }
  1196. text_editor->merge_overlapping_carets();
  1197. text_editor->end_complex_operation();
  1198. }
  1199. void CodeTextEditor::duplicate_selection() {
  1200. text_editor->begin_complex_operation();
  1201. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1202. for (const int &c : caret_edit_order) {
  1203. const int cursor_column = text_editor->get_caret_column(c);
  1204. int from_line = text_editor->get_caret_line(c);
  1205. int to_line = text_editor->get_caret_line(c);
  1206. int from_column = 0;
  1207. int to_column = 0;
  1208. int cursor_new_line = to_line + 1;
  1209. int cursor_new_column = text_editor->get_caret_column(c);
  1210. String new_text = "\n" + text_editor->get_line(from_line);
  1211. bool selection_active = false;
  1212. text_editor->set_caret_column(text_editor->get_line(from_line).length(), c == 0, c);
  1213. if (text_editor->has_selection(c)) {
  1214. from_column = text_editor->get_selection_from_column(c);
  1215. to_column = text_editor->get_selection_to_column(c);
  1216. from_line = text_editor->get_selection_from_line(c);
  1217. to_line = text_editor->get_selection_to_line(c);
  1218. cursor_new_line = to_line + text_editor->get_caret_line(c) - from_line;
  1219. cursor_new_column = to_column == cursor_column ? 2 * to_column - from_column : to_column;
  1220. new_text = text_editor->get_selected_text(c);
  1221. selection_active = true;
  1222. text_editor->set_caret_line(to_line, c == 0, true, 0, c);
  1223. text_editor->set_caret_column(to_column, c == 0, c);
  1224. }
  1225. for (int i = from_line; i <= to_line; i++) {
  1226. text_editor->unfold_line(i);
  1227. }
  1228. text_editor->deselect(c);
  1229. text_editor->insert_text_at_caret(new_text, c);
  1230. text_editor->set_caret_line(cursor_new_line, c == 0, true, 0, c);
  1231. text_editor->set_caret_column(cursor_new_column, c == 0, c);
  1232. if (selection_active) {
  1233. text_editor->select(to_line, to_column, 2 * to_line - from_line, to_line == from_line ? 2 * to_column - from_column : to_column, c);
  1234. }
  1235. }
  1236. text_editor->merge_overlapping_carets();
  1237. text_editor->end_complex_operation();
  1238. text_editor->queue_redraw();
  1239. }
  1240. void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
  1241. text_editor->begin_complex_operation();
  1242. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1243. caret_edit_order.reverse();
  1244. int last_line = -1;
  1245. int folded_to = 0;
  1246. for (const int &c1 : caret_edit_order) {
  1247. int from = _get_affected_lines_from(c1);
  1248. from += from == last_line ? 1 + folded_to : 0;
  1249. int to = _get_affected_lines_to(c1);
  1250. last_line = to;
  1251. // If last line is folded, extends to the end of the folded section
  1252. if (text_editor->is_line_folded(to)) {
  1253. folded_to = text_editor->get_next_visible_line_offset_from(to + 1, 1) - 1;
  1254. to += folded_to;
  1255. }
  1256. // Check first if there's any uncommented lines in selection.
  1257. bool is_commented = true;
  1258. bool is_all_empty = true;
  1259. for (int line = from; line <= to; line++) {
  1260. // `+ delimiter.length()` here because comment delimiter is not actually `in comment` so we check first character after it
  1261. int delimiter_idx = text_editor->is_in_comment(line, text_editor->get_first_non_whitespace_column(line) + delimiter.length());
  1262. // Empty lines should not be counted.
  1263. bool is_empty = text_editor->get_line(line).strip_edges().is_empty();
  1264. is_all_empty = is_all_empty && is_empty;
  1265. // get_delimiter_start_key will return `##` instead of `#` when there is multiple comment delimiter in a line.
  1266. if (!is_empty && (delimiter_idx == -1 || !text_editor->get_delimiter_start_key(delimiter_idx).begins_with(delimiter))) {
  1267. is_commented = false;
  1268. break;
  1269. }
  1270. }
  1271. // Special case for commenting empty lines, treat it/them as uncommented lines.
  1272. is_commented = is_commented && !is_all_empty;
  1273. // Caret positions need to be saved since they could be moved at the eol.
  1274. Vector<int> caret_cols;
  1275. Vector<int> selection_to_cols;
  1276. for (const int &c2 : caret_edit_order) {
  1277. if (text_editor->get_caret_line(c2) >= from && text_editor->get_caret_line(c2) <= to) {
  1278. caret_cols.append(text_editor->get_caret_column(c2));
  1279. }
  1280. if (text_editor->has_selection(c2) && text_editor->get_selection_to_line(c2) >= from && text_editor->get_selection_to_line(c2) <= to) {
  1281. selection_to_cols.append(text_editor->get_selection_to_column(c2));
  1282. }
  1283. }
  1284. // Comment/uncomment.
  1285. for (int line = from; line <= to; line++) {
  1286. String line_text = text_editor->get_line(line);
  1287. if (is_all_empty) {
  1288. text_editor->set_line(line, delimiter);
  1289. continue;
  1290. }
  1291. if (is_commented) {
  1292. text_editor->set_line(line, line_text.replace_first(delimiter, ""));
  1293. } else {
  1294. text_editor->set_line(line, line_text.insert(text_editor->get_first_non_whitespace_column(line), delimiter));
  1295. }
  1296. }
  1297. // Readjust carets and selections.
  1298. int caret_i = 0;
  1299. int selection_i = 0;
  1300. int offset = (is_commented ? -1 : 1) * delimiter.length();
  1301. for (const int &c2 : caret_edit_order) {
  1302. bool is_line_selection = text_editor->has_selection(c2) && text_editor->get_selection_from_line(c2) < text_editor->get_selection_to_line(c2);
  1303. if (text_editor->get_caret_line(c2) >= from && text_editor->get_caret_line(c2) <= to) {
  1304. int caret_col = caret_cols[caret_i++];
  1305. caret_col += (is_line_selection && caret_col == 0) ? 0 : offset;
  1306. text_editor->set_caret_column(caret_col, c2 == 0, c2);
  1307. }
  1308. if (text_editor->has_selection(c2) && text_editor->get_selection_to_line(c2) >= from && text_editor->get_selection_to_line(c2) <= to) {
  1309. int from_col = text_editor->get_selection_from_column(c2);
  1310. from_col += (is_line_selection && from_col == 0) ? 0 : offset;
  1311. int to_col = selection_to_cols[selection_i++];
  1312. to_col += (to_col == 0) ? 0 : offset;
  1313. text_editor->select(
  1314. text_editor->get_selection_from_line(c2), from_col,
  1315. text_editor->get_selection_to_line(c2), to_col, c2);
  1316. }
  1317. }
  1318. }
  1319. text_editor->merge_overlapping_carets();
  1320. text_editor->end_complex_operation();
  1321. text_editor->queue_redraw();
  1322. }
  1323. void CodeTextEditor::goto_line(int p_line) {
  1324. text_editor->remove_secondary_carets();
  1325. text_editor->deselect();
  1326. text_editor->unfold_line(p_line);
  1327. callable_mp((TextEdit *)text_editor, &TextEdit::set_caret_line).call_deferred(p_line, true, true, 0, 0);
  1328. }
  1329. void CodeTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  1330. text_editor->remove_secondary_carets();
  1331. text_editor->unfold_line(p_line);
  1332. callable_mp((TextEdit *)text_editor, &TextEdit::set_caret_line).call_deferred(p_line, true, true, 0, 0);
  1333. callable_mp((TextEdit *)text_editor, &TextEdit::set_caret_column).call_deferred(p_begin, true, 0);
  1334. text_editor->select(p_line, p_begin, p_line, p_end);
  1335. }
  1336. void CodeTextEditor::goto_line_centered(int p_line) {
  1337. goto_line(p_line);
  1338. callable_mp((TextEdit *)text_editor, &TextEdit::center_viewport_to_caret).call_deferred(0);
  1339. }
  1340. void CodeTextEditor::set_executing_line(int p_line) {
  1341. text_editor->set_line_as_executing(p_line, true);
  1342. }
  1343. void CodeTextEditor::clear_executing_line() {
  1344. text_editor->clear_executing_lines();
  1345. }
  1346. Variant CodeTextEditor::get_edit_state() {
  1347. Dictionary state;
  1348. state.merge(get_navigation_state());
  1349. state["folded_lines"] = text_editor->get_folded_lines();
  1350. state["breakpoints"] = text_editor->get_breakpointed_lines();
  1351. state["bookmarks"] = text_editor->get_bookmarked_lines();
  1352. Ref<EditorSyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighter();
  1353. state["syntax_highlighter"] = syntax_highlighter->_get_name();
  1354. return state;
  1355. }
  1356. void CodeTextEditor::set_edit_state(const Variant &p_state) {
  1357. Dictionary state = p_state;
  1358. /* update the row first as it sets the column to 0 */
  1359. text_editor->set_caret_line(state["row"]);
  1360. text_editor->set_caret_column(state["column"]);
  1361. text_editor->set_v_scroll(state["scroll_position"]);
  1362. text_editor->set_h_scroll(state["h_scroll_position"]);
  1363. if (state.get("selection", false)) {
  1364. text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]);
  1365. } else {
  1366. text_editor->deselect();
  1367. }
  1368. if (state.has("folded_lines")) {
  1369. Vector<int> folded_lines = state["folded_lines"];
  1370. for (int i = 0; i < folded_lines.size(); i++) {
  1371. text_editor->fold_line(folded_lines[i]);
  1372. }
  1373. }
  1374. if (state.has("breakpoints")) {
  1375. Array breakpoints = state["breakpoints"];
  1376. for (int i = 0; i < breakpoints.size(); i++) {
  1377. text_editor->set_line_as_breakpoint(breakpoints[i], true);
  1378. }
  1379. }
  1380. if (state.has("bookmarks")) {
  1381. Array bookmarks = state["bookmarks"];
  1382. for (int i = 0; i < bookmarks.size(); i++) {
  1383. text_editor->set_line_as_bookmarked(bookmarks[i], true);
  1384. }
  1385. }
  1386. }
  1387. Variant CodeTextEditor::get_navigation_state() {
  1388. Dictionary state;
  1389. state["scroll_position"] = text_editor->get_v_scroll();
  1390. state["h_scroll_position"] = text_editor->get_h_scroll();
  1391. state["column"] = text_editor->get_caret_column();
  1392. state["row"] = text_editor->get_caret_line();
  1393. state["selection"] = get_text_editor()->has_selection();
  1394. if (get_text_editor()->has_selection()) {
  1395. state["selection_from_line"] = text_editor->get_selection_from_line();
  1396. state["selection_from_column"] = text_editor->get_selection_from_column();
  1397. state["selection_to_line"] = text_editor->get_selection_to_line();
  1398. state["selection_to_column"] = text_editor->get_selection_to_column();
  1399. }
  1400. return state;
  1401. }
  1402. void CodeTextEditor::set_error(const String &p_error) {
  1403. error->set_text(p_error);
  1404. if (!p_error.is_empty()) {
  1405. error->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1406. } else {
  1407. error->set_default_cursor_shape(CURSOR_ARROW);
  1408. }
  1409. }
  1410. void CodeTextEditor::set_error_pos(int p_line, int p_column) {
  1411. error_line = p_line;
  1412. error_column = p_column;
  1413. }
  1414. Point2i CodeTextEditor::get_error_pos() const {
  1415. return Point2i(error_line, error_column);
  1416. }
  1417. void CodeTextEditor::goto_error() {
  1418. if (!error->get_text().is_empty()) {
  1419. if (text_editor->get_line_count() != error_line) {
  1420. text_editor->unfold_line(error_line);
  1421. }
  1422. text_editor->remove_secondary_carets();
  1423. text_editor->set_caret_line(error_line);
  1424. text_editor->set_caret_column(error_column);
  1425. text_editor->center_viewport_to_caret();
  1426. }
  1427. }
  1428. void CodeTextEditor::_update_text_editor_theme() {
  1429. emit_signal(SNAME("load_theme_settings"));
  1430. error_button->set_icon(get_editor_theme_icon(SNAME("StatusError")));
  1431. warning_button->set_icon(get_editor_theme_icon(SNAME("NodeWarning")));
  1432. Ref<Font> status_bar_font = get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts));
  1433. int status_bar_font_size = get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts));
  1434. int count = status_bar->get_child_count();
  1435. for (int i = 0; i < count; i++) {
  1436. Control *n = Object::cast_to<Control>(status_bar->get_child(i));
  1437. if (n) {
  1438. n->add_theme_font_override(SNAME("font"), status_bar_font);
  1439. n->add_theme_font_size_override(SNAME("font_size"), status_bar_font_size);
  1440. }
  1441. }
  1442. const Color &error_color = get_theme_color(SNAME("error_color"), EditorStringName(Editor));
  1443. const Color &warning_color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
  1444. error->add_theme_color_override(SNAME("font_color"), error_color);
  1445. error_button->add_theme_color_override(SNAME("font_color"), error_color);
  1446. warning_button->add_theme_color_override(SNAME("font_color"), warning_color);
  1447. _update_font_ligatures();
  1448. }
  1449. void CodeTextEditor::_update_font_ligatures() {
  1450. int ot_mode = EDITOR_GET("interface/editor/code_font_contextual_ligatures");
  1451. Ref<FontVariation> fc = text_editor->get_theme_font(SNAME("font"));
  1452. if (fc.is_valid()) {
  1453. switch (ot_mode) {
  1454. case 1: { // Disable ligatures.
  1455. Dictionary ftrs;
  1456. ftrs[TS->name_to_tag("calt")] = 0;
  1457. fc->set_opentype_features(ftrs);
  1458. } break;
  1459. case 2: { // Custom.
  1460. Vector<String> subtag = String(EDITOR_GET("interface/editor/code_font_custom_opentype_features")).split(",");
  1461. Dictionary ftrs;
  1462. for (int i = 0; i < subtag.size(); i++) {
  1463. Vector<String> subtag_a = subtag[i].split("=");
  1464. if (subtag_a.size() == 2) {
  1465. ftrs[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  1466. } else if (subtag_a.size() == 1) {
  1467. ftrs[TS->name_to_tag(subtag_a[0])] = 1;
  1468. }
  1469. }
  1470. fc->set_opentype_features(ftrs);
  1471. } break;
  1472. default: { // Enabled.
  1473. Dictionary ftrs;
  1474. ftrs[TS->name_to_tag("calt")] = 1;
  1475. fc->set_opentype_features(ftrs);
  1476. } break;
  1477. }
  1478. }
  1479. }
  1480. void CodeTextEditor::_text_changed_idle_timeout() {
  1481. _validate_script();
  1482. emit_signal(SNAME("validate_script"));
  1483. }
  1484. void CodeTextEditor::validate_script() {
  1485. idle->start();
  1486. }
  1487. void CodeTextEditor::_error_button_pressed() {
  1488. _set_show_errors_panel(!is_errors_panel_opened);
  1489. _set_show_warnings_panel(false);
  1490. }
  1491. void CodeTextEditor::_warning_button_pressed() {
  1492. _set_show_warnings_panel(!is_warnings_panel_opened);
  1493. _set_show_errors_panel(false);
  1494. }
  1495. void CodeTextEditor::_zoom_popup_id_pressed(int p_idx) {
  1496. _zoom_to(zoom_button->get_popup()->get_item_metadata(p_idx));
  1497. }
  1498. void CodeTextEditor::_set_show_errors_panel(bool p_show) {
  1499. is_errors_panel_opened = p_show;
  1500. emit_signal(SNAME("show_errors_panel"), p_show);
  1501. }
  1502. void CodeTextEditor::_set_show_warnings_panel(bool p_show) {
  1503. is_warnings_panel_opened = p_show;
  1504. emit_signal(SNAME("show_warnings_panel"), p_show);
  1505. }
  1506. void CodeTextEditor::_toggle_scripts_pressed() {
  1507. ScriptEditor::get_singleton()->toggle_scripts_panel();
  1508. update_toggle_scripts_button();
  1509. }
  1510. int CodeTextEditor::_get_affected_lines_from(int p_caret) {
  1511. return text_editor->has_selection(p_caret) ? text_editor->get_selection_from_line(p_caret) : text_editor->get_caret_line(p_caret);
  1512. }
  1513. int CodeTextEditor::_get_affected_lines_to(int p_caret) {
  1514. if (!text_editor->has_selection(p_caret)) {
  1515. return text_editor->get_caret_line(p_caret);
  1516. }
  1517. int line = text_editor->get_selection_to_line(p_caret);
  1518. // Don't affect a line with no selected characters.
  1519. if (text_editor->get_selection_to_column(p_caret) == 0) {
  1520. line--;
  1521. }
  1522. return line;
  1523. }
  1524. void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
  1525. Ref<InputEventMouseButton> mb = p_event;
  1526. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1527. goto_error();
  1528. }
  1529. }
  1530. void CodeTextEditor::_notification(int p_what) {
  1531. switch (p_what) {
  1532. case NOTIFICATION_THEME_CHANGED: {
  1533. if (toggle_scripts_button->is_visible()) {
  1534. update_toggle_scripts_button();
  1535. }
  1536. _update_text_editor_theme();
  1537. } break;
  1538. case NOTIFICATION_VISIBILITY_CHANGED: {
  1539. if (toggle_scripts_button->is_visible()) {
  1540. update_toggle_scripts_button();
  1541. }
  1542. set_process_input(is_visible_in_tree());
  1543. } break;
  1544. case NOTIFICATION_PREDELETE: {
  1545. if (find_replace_bar) {
  1546. find_replace_bar->set_text_edit(nullptr);
  1547. }
  1548. } break;
  1549. }
  1550. }
  1551. void CodeTextEditor::set_error_count(int p_error_count) {
  1552. error_button->set_text(itos(p_error_count));
  1553. error_button->set_visible(p_error_count > 0);
  1554. if (!p_error_count) {
  1555. _set_show_errors_panel(false);
  1556. }
  1557. }
  1558. void CodeTextEditor::set_warning_count(int p_warning_count) {
  1559. warning_button->set_text(itos(p_warning_count));
  1560. warning_button->set_visible(p_warning_count > 0);
  1561. if (!p_warning_count) {
  1562. _set_show_warnings_panel(false);
  1563. }
  1564. }
  1565. void CodeTextEditor::toggle_bookmark() {
  1566. Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order();
  1567. caret_edit_order.reverse();
  1568. int last_line = -1;
  1569. for (const int &c : caret_edit_order) {
  1570. int from = text_editor->has_selection(c) ? text_editor->get_selection_from_line(c) : text_editor->get_caret_line(c);
  1571. from += from == last_line ? 1 : 0;
  1572. int to = text_editor->has_selection(c) ? text_editor->get_selection_to_line(c) : text_editor->get_caret_line(c);
  1573. if (to < from) {
  1574. continue;
  1575. }
  1576. // Check first if there's any bookmarked lines in the selection.
  1577. bool selection_has_bookmarks = false;
  1578. for (int line = from; line <= to; line++) {
  1579. if (text_editor->is_line_bookmarked(line)) {
  1580. selection_has_bookmarks = true;
  1581. break;
  1582. }
  1583. }
  1584. // Set bookmark on caret or remove all bookmarks from the selection.
  1585. if (!selection_has_bookmarks) {
  1586. if (text_editor->get_caret_line(c) != last_line) {
  1587. text_editor->set_line_as_bookmarked(text_editor->get_caret_line(c), true);
  1588. }
  1589. } else {
  1590. for (int line = from; line <= to; line++) {
  1591. text_editor->set_line_as_bookmarked(line, false);
  1592. }
  1593. }
  1594. last_line = to;
  1595. }
  1596. }
  1597. void CodeTextEditor::goto_next_bookmark() {
  1598. PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
  1599. if (bmarks.size() <= 0) {
  1600. return;
  1601. }
  1602. int current_line = text_editor->get_caret_line();
  1603. int bmark_idx = 0;
  1604. if (current_line < (int)bmarks[bmarks.size() - 1]) {
  1605. while (bmark_idx < bmarks.size() && bmarks[bmark_idx] <= current_line) {
  1606. bmark_idx++;
  1607. }
  1608. }
  1609. goto_line_centered(bmarks[bmark_idx]);
  1610. }
  1611. void CodeTextEditor::goto_prev_bookmark() {
  1612. PackedInt32Array bmarks = text_editor->get_bookmarked_lines();
  1613. if (bmarks.size() <= 0) {
  1614. return;
  1615. }
  1616. int current_line = text_editor->get_caret_line();
  1617. int bmark_idx = bmarks.size() - 1;
  1618. if (current_line > (int)bmarks[0]) {
  1619. while (bmark_idx >= 0 && bmarks[bmark_idx] >= current_line) {
  1620. bmark_idx--;
  1621. }
  1622. }
  1623. goto_line_centered(bmarks[bmark_idx]);
  1624. }
  1625. void CodeTextEditor::remove_all_bookmarks() {
  1626. text_editor->clear_bookmarked_lines();
  1627. }
  1628. void CodeTextEditor::_zoom_in() {
  1629. int s = text_editor->get_theme_font_size("font_size");
  1630. _zoom_to(zoom_factor * (s + MAX(1.0f, EDSCALE)) / s);
  1631. }
  1632. void CodeTextEditor::_zoom_out() {
  1633. int s = text_editor->get_theme_font_size("font_size");
  1634. _zoom_to(zoom_factor * (s - MAX(1.0f, EDSCALE)) / s);
  1635. }
  1636. void CodeTextEditor::_zoom_to(float p_zoom_factor) {
  1637. if (zoom_factor == p_zoom_factor) {
  1638. return;
  1639. }
  1640. float old_zoom_factor = zoom_factor;
  1641. set_zoom_factor(p_zoom_factor);
  1642. if (old_zoom_factor != zoom_factor) {
  1643. emit_signal(SNAME("zoomed"), zoom_factor);
  1644. }
  1645. }
  1646. void CodeTextEditor::set_zoom_factor(float p_zoom_factor) {
  1647. int preset_count = sizeof(ZOOM_FACTOR_PRESETS) / sizeof(float);
  1648. zoom_factor = CLAMP(p_zoom_factor, ZOOM_FACTOR_PRESETS[0], ZOOM_FACTOR_PRESETS[preset_count - 1]);
  1649. int neutral_font_size = int(EDITOR_GET("interface/editor/code_font_size")) * EDSCALE;
  1650. int new_font_size = Math::round(zoom_factor * neutral_font_size);
  1651. zoom_button->set_text(itos(Math::round(zoom_factor * 100)) + " %");
  1652. if (text_editor->has_theme_font_size_override("font_size")) {
  1653. text_editor->remove_theme_font_size_override("font_size");
  1654. }
  1655. text_editor->add_theme_font_size_override("font_size", new_font_size);
  1656. }
  1657. float CodeTextEditor::get_zoom_factor() {
  1658. return zoom_factor;
  1659. }
  1660. void CodeTextEditor::_bind_methods() {
  1661. ADD_SIGNAL(MethodInfo("validate_script"));
  1662. ADD_SIGNAL(MethodInfo("load_theme_settings"));
  1663. ADD_SIGNAL(MethodInfo("show_errors_panel"));
  1664. ADD_SIGNAL(MethodInfo("show_warnings_panel"));
  1665. ADD_SIGNAL(MethodInfo("zoomed", PropertyInfo(Variant::FLOAT, "p_zoom_factor")));
  1666. }
  1667. void CodeTextEditor::set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud) {
  1668. code_complete_func = p_code_complete_func;
  1669. code_complete_ud = p_ud;
  1670. }
  1671. void CodeTextEditor::show_toggle_scripts_button() {
  1672. toggle_scripts_button->show();
  1673. }
  1674. void CodeTextEditor::update_toggle_scripts_button() {
  1675. if (is_layout_rtl()) {
  1676. toggle_scripts_button->set_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward") : SNAME("Back")));
  1677. } else {
  1678. toggle_scripts_button->set_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back") : SNAME("Forward")));
  1679. }
  1680. toggle_scripts_button->set_tooltip_text(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
  1681. }
  1682. CodeTextEditor::CodeTextEditor() {
  1683. code_complete_func = nullptr;
  1684. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KeyModifierMask::CMD_OR_CTRL | Key::EQUAL);
  1685. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KeyModifierMask::CMD_OR_CTRL | Key::MINUS);
  1686. ED_SHORTCUT_ARRAY("script_editor/reset_zoom", TTR("Reset Zoom"),
  1687. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KEY_0), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_0) });
  1688. text_editor = memnew(CodeEdit);
  1689. add_child(text_editor);
  1690. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1691. text_editor->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_GDSCRIPT);
  1692. text_editor->set_draw_bookmarks_gutter(true);
  1693. text_editor->set_draw_line_numbers(true);
  1694. text_editor->set_highlight_matching_braces_enabled(true);
  1695. text_editor->set_auto_indent_enabled(true);
  1696. text_editor->set_deselect_on_focus_loss_enabled(false);
  1697. status_bar = memnew(HBoxContainer);
  1698. add_child(status_bar);
  1699. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1700. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the warning icon.
  1701. idle = memnew(Timer);
  1702. add_child(idle);
  1703. idle->set_one_shot(true);
  1704. code_complete_enabled = EDITOR_GET("text_editor/completion/code_complete_enabled");
  1705. code_complete_timer = memnew(Timer);
  1706. add_child(code_complete_timer);
  1707. code_complete_timer->set_one_shot(true);
  1708. error_line = 0;
  1709. error_column = 0;
  1710. toggle_scripts_button = memnew(Button);
  1711. toggle_scripts_button->set_flat(true);
  1712. toggle_scripts_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1713. toggle_scripts_button->connect("pressed", callable_mp(this, &CodeTextEditor::_toggle_scripts_pressed));
  1714. status_bar->add_child(toggle_scripts_button);
  1715. toggle_scripts_button->hide();
  1716. // Error
  1717. ScrollContainer *scroll = memnew(ScrollContainer);
  1718. scroll->set_h_size_flags(SIZE_EXPAND_FILL);
  1719. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1720. scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1721. status_bar->add_child(scroll);
  1722. error = memnew(Label);
  1723. scroll->add_child(error);
  1724. error->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1725. error->set_mouse_filter(MOUSE_FILTER_STOP);
  1726. error->connect("gui_input", callable_mp(this, &CodeTextEditor::_error_pressed));
  1727. // Errors
  1728. error_button = memnew(Button);
  1729. error_button->set_flat(true);
  1730. status_bar->add_child(error_button);
  1731. error_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1732. error_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1733. error_button->connect("pressed", callable_mp(this, &CodeTextEditor::_error_button_pressed));
  1734. error_button->set_tooltip_text(TTR("Errors"));
  1735. set_error_count(0);
  1736. // Warnings
  1737. warning_button = memnew(Button);
  1738. warning_button->set_flat(true);
  1739. status_bar->add_child(warning_button);
  1740. warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1741. warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1742. warning_button->connect("pressed", callable_mp(this, &CodeTextEditor::_warning_button_pressed));
  1743. warning_button->set_tooltip_text(TTR("Warnings"));
  1744. set_warning_count(0);
  1745. status_bar->add_child(memnew(VSeparator));
  1746. // Zoom
  1747. zoom_button = memnew(MenuButton);
  1748. status_bar->add_child(zoom_button);
  1749. zoom_button->set_flat(true);
  1750. zoom_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1751. zoom_button->set_tooltip_text(TTR("Zoom factor"));
  1752. zoom_button->set_text("100 %");
  1753. PopupMenu *zoom_menu = zoom_button->get_popup();
  1754. int preset_count = sizeof(ZOOM_FACTOR_PRESETS) / sizeof(float);
  1755. for (int i = 0; i < preset_count; i++) {
  1756. float z = ZOOM_FACTOR_PRESETS[i];
  1757. zoom_menu->add_item(itos(Math::round(z * 100)) + " %");
  1758. zoom_menu->set_item_metadata(i, z);
  1759. }
  1760. zoom_menu->connect("id_pressed", callable_mp(this, &CodeTextEditor::_zoom_popup_id_pressed));
  1761. status_bar->add_child(memnew(VSeparator));
  1762. // Line and column
  1763. line_and_col_txt = memnew(Label);
  1764. status_bar->add_child(line_and_col_txt);
  1765. line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1766. line_and_col_txt->set_tooltip_text(TTR("Line and column numbers."));
  1767. line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1768. status_bar->add_child(memnew(VSeparator));
  1769. // Indentation
  1770. indentation_txt = memnew(Label);
  1771. status_bar->add_child(indentation_txt);
  1772. indentation_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  1773. indentation_txt->set_tooltip_text(TTR("Indentation"));
  1774. indentation_txt->set_mouse_filter(MOUSE_FILTER_STOP);
  1775. text_editor->connect("gui_input", callable_mp(this, &CodeTextEditor::_text_editor_gui_input));
  1776. text_editor->connect("caret_changed", callable_mp(this, &CodeTextEditor::_line_col_changed));
  1777. text_editor->connect("text_changed", callable_mp(this, &CodeTextEditor::_text_changed));
  1778. text_editor->connect("code_completion_requested", callable_mp(this, &CodeTextEditor::_complete_request));
  1779. TypedArray<String> cs;
  1780. cs.push_back(".");
  1781. cs.push_back(",");
  1782. cs.push_back("(");
  1783. cs.push_back("=");
  1784. cs.push_back("$");
  1785. cs.push_back("@");
  1786. cs.push_back("\"");
  1787. cs.push_back("\'");
  1788. text_editor->set_code_completion_prefixes(cs);
  1789. idle->connect("timeout", callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout));
  1790. code_complete_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_code_complete_timer_timeout));
  1791. add_theme_constant_override("separation", 4 * EDSCALE);
  1792. }