code_editor.cpp 71 KB

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