code_editor.cpp 72 KB

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