code_editor.cpp 69 KB

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