text_editor.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /**************************************************************************/
  2. /* text_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 "text_editor.h"
  31. #include "core/io/json.h"
  32. #include "core/os/keyboard.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "scene/gui/menu_button.h"
  36. void TextEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  37. ERR_FAIL_COND(p_highlighter.is_null());
  38. highlighters[p_highlighter->_get_name()] = p_highlighter;
  39. highlighter_menu->add_radio_check_item(p_highlighter->_get_name());
  40. }
  41. void TextEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  42. ERR_FAIL_COND(p_highlighter.is_null());
  43. HashMap<String, Ref<EditorSyntaxHighlighter>>::Iterator el = highlighters.begin();
  44. while (el) {
  45. int highlighter_index = highlighter_menu->get_item_idx_from_text(el->key);
  46. highlighter_menu->set_item_checked(highlighter_index, el->value == p_highlighter);
  47. ++el;
  48. }
  49. CodeEdit *te = code_editor->get_text_editor();
  50. te->set_syntax_highlighter(p_highlighter);
  51. }
  52. void TextEditor::_change_syntax_highlighter(int p_idx) {
  53. set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]);
  54. }
  55. void TextEditor::_load_theme_settings() {
  56. code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
  57. }
  58. String TextEditor::get_name() {
  59. String name;
  60. name = edited_res->get_path().get_file();
  61. if (name.is_empty()) {
  62. // This appears for newly created built-in text_files before saving the scene.
  63. name = TTR("[unsaved]");
  64. } else if (edited_res->is_built_in()) {
  65. const String &text_file_name = edited_res->get_name();
  66. if (!text_file_name.is_empty()) {
  67. // If the built-in text_file has a custom resource name defined,
  68. // display the built-in text_file name as follows: `ResourceName (scene_file.tscn)`
  69. name = vformat("%s (%s)", text_file_name, name.get_slice("::", 0));
  70. }
  71. }
  72. if (is_unsaved()) {
  73. name += "(*)";
  74. }
  75. return name;
  76. }
  77. Ref<Texture2D> TextEditor::get_theme_icon() {
  78. return EditorNode::get_singleton()->get_object_icon(edited_res.ptr(), "TextFile");
  79. }
  80. Ref<Resource> TextEditor::get_edited_resource() const {
  81. return edited_res;
  82. }
  83. void TextEditor::set_edited_resource(const Ref<Resource> &p_res) {
  84. ERR_FAIL_COND(edited_res.is_valid());
  85. ERR_FAIL_COND(p_res.is_null());
  86. edited_res = p_res;
  87. Ref<TextFile> text_file = edited_res;
  88. if (text_file != nullptr) {
  89. code_editor->get_text_editor()->set_text(text_file->get_text());
  90. }
  91. Ref<JSON> json_file = edited_res;
  92. if (json_file != nullptr) {
  93. code_editor->get_text_editor()->set_text(json_file->get_parsed_text());
  94. }
  95. code_editor->get_text_editor()->clear_undo_history();
  96. code_editor->get_text_editor()->tag_saved_version();
  97. emit_signal(SNAME("name_changed"));
  98. code_editor->update_line_and_column();
  99. }
  100. void TextEditor::enable_editor(Control *p_shortcut_context) {
  101. if (editor_enabled) {
  102. return;
  103. }
  104. editor_enabled = true;
  105. _load_theme_settings();
  106. _validate_script();
  107. if (p_shortcut_context) {
  108. for (int i = 0; i < edit_hb->get_child_count(); ++i) {
  109. Control *c = cast_to<Control>(edit_hb->get_child(i));
  110. if (c) {
  111. c->set_shortcut_context(p_shortcut_context);
  112. }
  113. }
  114. }
  115. }
  116. void TextEditor::add_callback(const String &p_function, PackedStringArray p_args) {
  117. }
  118. void TextEditor::set_debugger_active(bool p_active) {
  119. }
  120. Control *TextEditor::get_base_editor() const {
  121. return code_editor->get_text_editor();
  122. }
  123. PackedInt32Array TextEditor::get_breakpoints() {
  124. return PackedInt32Array();
  125. }
  126. void TextEditor::reload_text() {
  127. ERR_FAIL_COND(edited_res.is_null());
  128. CodeEdit *te = code_editor->get_text_editor();
  129. int column = te->get_caret_column();
  130. int row = te->get_caret_line();
  131. int h = te->get_h_scroll();
  132. int v = te->get_v_scroll();
  133. Ref<TextFile> text_file = edited_res;
  134. if (text_file != nullptr) {
  135. te->set_text(text_file->get_text());
  136. }
  137. Ref<JSON> json_file = edited_res;
  138. if (json_file != nullptr) {
  139. te->set_text(json_file->get_parsed_text());
  140. }
  141. te->set_caret_line(row);
  142. te->set_caret_column(column);
  143. te->set_h_scroll(h);
  144. te->set_v_scroll(v);
  145. te->tag_saved_version();
  146. code_editor->update_line_and_column();
  147. _validate_script();
  148. }
  149. void TextEditor::_validate_script() {
  150. emit_signal(SNAME("name_changed"));
  151. emit_signal(SNAME("edited_script_changed"));
  152. Ref<JSON> json_file = edited_res;
  153. if (json_file != nullptr) {
  154. CodeEdit *te = code_editor->get_text_editor();
  155. te->set_line_background_color(code_editor->get_error_pos().x, Color(0, 0, 0, 0));
  156. code_editor->set_error("");
  157. if (json_file->parse(te->get_text(), true) != OK) {
  158. code_editor->set_error(json_file->get_error_message());
  159. code_editor->set_error_pos(json_file->get_error_line(), 0);
  160. te->set_line_background_color(code_editor->get_error_pos().x, EDITOR_GET("text_editor/theme/highlighting/mark_color"));
  161. }
  162. }
  163. }
  164. void TextEditor::_update_bookmark_list() {
  165. bookmarks_menu->clear();
  166. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  167. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
  168. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
  169. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
  170. PackedInt32Array bookmark_list = code_editor->get_text_editor()->get_bookmarked_lines();
  171. if (bookmark_list.size() == 0) {
  172. return;
  173. }
  174. bookmarks_menu->add_separator();
  175. for (int i = 0; i < bookmark_list.size(); i++) {
  176. String line = code_editor->get_text_editor()->get_line(bookmark_list[i]).strip_edges();
  177. // Limit the size of the line if too big.
  178. if (line.length() > 50) {
  179. line = line.substr(0, 50);
  180. }
  181. bookmarks_menu->add_item(String::num((int)bookmark_list[i] + 1) + " - \"" + line + "\"");
  182. bookmarks_menu->set_item_metadata(-1, bookmark_list[i]);
  183. }
  184. }
  185. void TextEditor::_bookmark_item_pressed(int p_idx) {
  186. if (p_idx < 4) { // Any item before the separator.
  187. _edit_option(bookmarks_menu->get_item_id(p_idx));
  188. } else {
  189. code_editor->goto_line(bookmarks_menu->get_item_metadata(p_idx));
  190. }
  191. }
  192. void TextEditor::apply_code() {
  193. Ref<TextFile> text_file = edited_res;
  194. if (text_file != nullptr) {
  195. text_file->set_text(code_editor->get_text_editor()->get_text());
  196. }
  197. Ref<JSON> json_file = edited_res;
  198. if (json_file != nullptr) {
  199. json_file->parse(code_editor->get_text_editor()->get_text(), true);
  200. }
  201. code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
  202. }
  203. bool TextEditor::is_unsaved() {
  204. const bool unsaved =
  205. code_editor->get_text_editor()->get_version() != code_editor->get_text_editor()->get_saved_version() ||
  206. edited_res->get_path().is_empty(); // In memory.
  207. return unsaved;
  208. }
  209. Variant TextEditor::get_edit_state() {
  210. return code_editor->get_edit_state();
  211. }
  212. void TextEditor::set_edit_state(const Variant &p_state) {
  213. code_editor->set_edit_state(p_state);
  214. Dictionary state = p_state;
  215. if (state.has("syntax_highlighter")) {
  216. int idx = highlighter_menu->get_item_idx_from_text(state["syntax_highlighter"]);
  217. if (idx >= 0) {
  218. _change_syntax_highlighter(idx);
  219. }
  220. }
  221. ensure_focus();
  222. }
  223. Variant TextEditor::get_navigation_state() {
  224. return code_editor->get_navigation_state();
  225. }
  226. void TextEditor::trim_trailing_whitespace() {
  227. code_editor->trim_trailing_whitespace();
  228. }
  229. void TextEditor::insert_final_newline() {
  230. code_editor->insert_final_newline();
  231. }
  232. void TextEditor::convert_indent_to_spaces() {
  233. code_editor->convert_indent_to_spaces();
  234. }
  235. void TextEditor::convert_indent_to_tabs() {
  236. code_editor->convert_indent_to_tabs();
  237. }
  238. void TextEditor::tag_saved_version() {
  239. code_editor->get_text_editor()->tag_saved_version();
  240. }
  241. void TextEditor::goto_line(int p_line, bool p_with_error) {
  242. code_editor->goto_line(p_line);
  243. }
  244. void TextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  245. code_editor->goto_line_selection(p_line, p_begin, p_end);
  246. }
  247. void TextEditor::set_executing_line(int p_line) {
  248. code_editor->set_executing_line(p_line);
  249. }
  250. void TextEditor::clear_executing_line() {
  251. code_editor->clear_executing_line();
  252. }
  253. void TextEditor::ensure_focus() {
  254. code_editor->get_text_editor()->grab_focus();
  255. }
  256. Vector<String> TextEditor::get_functions() {
  257. return Vector<String>();
  258. }
  259. bool TextEditor::show_members_overview() {
  260. return true;
  261. }
  262. void TextEditor::update_settings() {
  263. code_editor->update_editor_settings();
  264. }
  265. void TextEditor::set_tooltip_request_func(const Callable &p_toolip_callback) {
  266. Variant args[1] = { this };
  267. const Variant *argp[] = { &args[0] };
  268. code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1));
  269. }
  270. Control *TextEditor::get_edit_menu() {
  271. return edit_hb;
  272. }
  273. void TextEditor::clear_edit_menu() {
  274. memdelete(edit_hb);
  275. }
  276. void TextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
  277. code_editor->set_find_replace_bar(p_bar);
  278. }
  279. void TextEditor::_edit_option(int p_op) {
  280. CodeEdit *tx = code_editor->get_text_editor();
  281. switch (p_op) {
  282. case EDIT_UNDO: {
  283. tx->undo();
  284. tx->call_deferred(SNAME("grab_focus"));
  285. } break;
  286. case EDIT_REDO: {
  287. tx->redo();
  288. tx->call_deferred(SNAME("grab_focus"));
  289. } break;
  290. case EDIT_CUT: {
  291. tx->cut();
  292. tx->call_deferred(SNAME("grab_focus"));
  293. } break;
  294. case EDIT_COPY: {
  295. tx->copy();
  296. tx->call_deferred(SNAME("grab_focus"));
  297. } break;
  298. case EDIT_PASTE: {
  299. tx->paste();
  300. tx->call_deferred(SNAME("grab_focus"));
  301. } break;
  302. case EDIT_SELECT_ALL: {
  303. tx->select_all();
  304. tx->call_deferred(SNAME("grab_focus"));
  305. } break;
  306. case EDIT_MOVE_LINE_UP: {
  307. code_editor->move_lines_up();
  308. } break;
  309. case EDIT_MOVE_LINE_DOWN: {
  310. code_editor->move_lines_down();
  311. } break;
  312. case EDIT_INDENT: {
  313. tx->indent_lines();
  314. } break;
  315. case EDIT_UNINDENT: {
  316. tx->unindent_lines();
  317. } break;
  318. case EDIT_DELETE_LINE: {
  319. code_editor->delete_lines();
  320. } break;
  321. case EDIT_DUPLICATE_SELECTION: {
  322. code_editor->duplicate_selection();
  323. } break;
  324. case EDIT_TOGGLE_FOLD_LINE: {
  325. int previous_line = -1;
  326. for (int caret_idx : tx->get_caret_index_edit_order()) {
  327. int line_idx = tx->get_caret_line(caret_idx);
  328. if (line_idx != previous_line) {
  329. tx->toggle_foldable_line(line_idx);
  330. previous_line = line_idx;
  331. }
  332. }
  333. tx->queue_redraw();
  334. } break;
  335. case EDIT_FOLD_ALL_LINES: {
  336. tx->fold_all_lines();
  337. tx->queue_redraw();
  338. } break;
  339. case EDIT_UNFOLD_ALL_LINES: {
  340. tx->unfold_all_lines();
  341. tx->queue_redraw();
  342. } break;
  343. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  344. trim_trailing_whitespace();
  345. } break;
  346. case EDIT_CONVERT_INDENT_TO_SPACES: {
  347. convert_indent_to_spaces();
  348. } break;
  349. case EDIT_CONVERT_INDENT_TO_TABS: {
  350. convert_indent_to_tabs();
  351. } break;
  352. case EDIT_TO_UPPERCASE: {
  353. _convert_case(CodeTextEditor::UPPER);
  354. } break;
  355. case EDIT_TO_LOWERCASE: {
  356. _convert_case(CodeTextEditor::LOWER);
  357. } break;
  358. case EDIT_CAPITALIZE: {
  359. _convert_case(CodeTextEditor::CAPITALIZE);
  360. } break;
  361. case SEARCH_FIND: {
  362. code_editor->get_find_replace_bar()->popup_search();
  363. } break;
  364. case SEARCH_FIND_NEXT: {
  365. code_editor->get_find_replace_bar()->search_next();
  366. } break;
  367. case SEARCH_FIND_PREV: {
  368. code_editor->get_find_replace_bar()->search_prev();
  369. } break;
  370. case SEARCH_REPLACE: {
  371. code_editor->get_find_replace_bar()->popup_replace();
  372. } break;
  373. case SEARCH_IN_FILES: {
  374. String selected_text = code_editor->get_text_editor()->get_selected_text();
  375. // Yep, because it doesn't make sense to instance this dialog for every single script open...
  376. // So this will be delegated to the ScriptEditor.
  377. emit_signal(SNAME("search_in_files_requested"), selected_text);
  378. } break;
  379. case REPLACE_IN_FILES: {
  380. String selected_text = code_editor->get_text_editor()->get_selected_text();
  381. emit_signal(SNAME("replace_in_files_requested"), selected_text);
  382. } break;
  383. case SEARCH_GOTO_LINE: {
  384. goto_line_dialog->popup_find_line(tx);
  385. } break;
  386. case BOOKMARK_TOGGLE: {
  387. code_editor->toggle_bookmark();
  388. } break;
  389. case BOOKMARK_GOTO_NEXT: {
  390. code_editor->goto_next_bookmark();
  391. } break;
  392. case BOOKMARK_GOTO_PREV: {
  393. code_editor->goto_prev_bookmark();
  394. } break;
  395. case BOOKMARK_REMOVE_ALL: {
  396. code_editor->remove_all_bookmarks();
  397. } break;
  398. }
  399. }
  400. void TextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
  401. code_editor->convert_case(p_case);
  402. }
  403. static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource) {
  404. if (Object::cast_to<TextFile>(*p_resource) || Object::cast_to<JSON>(*p_resource)) {
  405. return memnew(TextEditor);
  406. }
  407. return nullptr;
  408. }
  409. void TextEditor::register_editor() {
  410. ScriptEditor::register_create_script_editor_function(create_editor);
  411. }
  412. void TextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  413. Ref<InputEventMouseButton> mb = ev;
  414. if (mb.is_valid()) {
  415. if (mb->get_button_index() == MouseButton::RIGHT) {
  416. CodeEdit *tx = code_editor->get_text_editor();
  417. Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());
  418. int row = pos.y;
  419. int col = pos.x;
  420. tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));
  421. bool can_fold = tx->can_fold_line(row);
  422. bool is_folded = tx->is_line_folded(row);
  423. if (tx->is_move_caret_on_right_click_enabled()) {
  424. tx->remove_secondary_carets();
  425. if (tx->has_selection()) {
  426. int from_line = tx->get_selection_from_line();
  427. int to_line = tx->get_selection_to_line();
  428. int from_column = tx->get_selection_from_column();
  429. int to_column = tx->get_selection_to_column();
  430. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  431. // Right click is outside the selected text.
  432. tx->deselect();
  433. }
  434. }
  435. if (!tx->has_selection()) {
  436. tx->set_caret_line(row, true, false);
  437. tx->set_caret_column(col);
  438. }
  439. }
  440. if (!mb->is_pressed()) {
  441. _make_context_menu(tx->has_selection(), can_fold, is_folded, get_local_mouse_position());
  442. }
  443. }
  444. }
  445. Ref<InputEventKey> k = ev;
  446. if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) {
  447. CodeEdit *tx = code_editor->get_text_editor();
  448. int line = tx->get_caret_line(0);
  449. tx->adjust_viewport_to_caret(0);
  450. _make_context_menu(tx->has_selection(0), tx->can_fold_line(line), tx->is_line_folded(line), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->get_caret_draw_pos(0)));
  451. context_menu->grab_focus();
  452. }
  453. }
  454. void TextEditor::_prepare_edit_menu() {
  455. const CodeEdit *tx = code_editor->get_text_editor();
  456. PopupMenu *popup = edit_menu->get_popup();
  457. popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
  458. popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
  459. }
  460. void TextEditor::_make_context_menu(bool p_selection, bool p_can_fold, bool p_is_folded, Vector2 p_position) {
  461. context_menu->clear();
  462. if (p_selection) {
  463. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  464. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  465. }
  466. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  467. context_menu->add_separator();
  468. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  469. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  470. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  471. context_menu->add_separator();
  472. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
  473. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
  474. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  475. if (p_selection) {
  476. context_menu->add_separator();
  477. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
  478. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
  479. }
  480. if (p_can_fold || p_is_folded) {
  481. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  482. }
  483. const CodeEdit *tx = code_editor->get_text_editor();
  484. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo());
  485. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo());
  486. context_menu->set_position(get_screen_position() + p_position);
  487. context_menu->reset_size();
  488. context_menu->popup();
  489. }
  490. void TextEditor::update_toggle_scripts_button() {
  491. code_editor->update_toggle_scripts_button();
  492. }
  493. TextEditor::TextEditor() {
  494. code_editor = memnew(CodeTextEditor);
  495. add_child(code_editor);
  496. code_editor->add_theme_constant_override("separation", 0);
  497. code_editor->connect("load_theme_settings", callable_mp(this, &TextEditor::_load_theme_settings));
  498. code_editor->connect("validate_script", callable_mp(this, &TextEditor::_validate_script));
  499. code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  500. code_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  501. code_editor->show_toggle_scripts_button();
  502. update_settings();
  503. code_editor->get_text_editor()->set_context_menu_enabled(false);
  504. code_editor->get_text_editor()->connect("gui_input", callable_mp(this, &TextEditor::_text_edit_gui_input));
  505. context_menu = memnew(PopupMenu);
  506. add_child(context_menu);
  507. context_menu->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  508. edit_hb = memnew(HBoxContainer);
  509. search_menu = memnew(MenuButton);
  510. search_menu->set_shortcut_context(this);
  511. edit_hb->add_child(search_menu);
  512. search_menu->set_text(TTR("Search"));
  513. search_menu->set_switch_on_hover(true);
  514. search_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  515. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  516. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  517. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  518. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  519. search_menu->get_popup()->add_separator();
  520. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_in_files"), SEARCH_IN_FILES);
  521. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES);
  522. edit_menu = memnew(MenuButton);
  523. edit_menu->set_shortcut_context(this);
  524. edit_hb->add_child(edit_menu);
  525. edit_menu->set_text(TTR("Edit"));
  526. edit_menu->set_switch_on_hover(true);
  527. edit_menu->connect("about_to_popup", callable_mp(this, &TextEditor::_prepare_edit_menu));
  528. edit_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  529. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  530. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  531. edit_menu->get_popup()->add_separator();
  532. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  533. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  534. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  535. edit_menu->get_popup()->add_separator();
  536. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  537. edit_menu->get_popup()->add_separator();
  538. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  539. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  540. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
  541. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
  542. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  543. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  544. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
  545. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
  546. edit_menu->get_popup()->add_separator();
  547. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
  548. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  549. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
  550. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
  551. edit_menu->get_popup()->add_separator();
  552. PopupMenu *convert_case = memnew(PopupMenu);
  553. convert_case->set_name("convert_case");
  554. edit_menu->get_popup()->add_child(convert_case);
  555. edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
  556. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase")), EDIT_TO_UPPERCASE);
  557. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase")), EDIT_TO_LOWERCASE);
  558. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE);
  559. convert_case->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  560. highlighter_menu = memnew(PopupMenu);
  561. highlighter_menu->set_name("highlighter_menu");
  562. edit_menu->get_popup()->add_child(highlighter_menu);
  563. edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "highlighter_menu");
  564. highlighter_menu->connect("id_pressed", callable_mp(this, &TextEditor::_change_syntax_highlighter));
  565. Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
  566. plain_highlighter.instantiate();
  567. add_syntax_highlighter(plain_highlighter);
  568. Ref<EditorStandardSyntaxHighlighter> highlighter;
  569. highlighter.instantiate();
  570. add_syntax_highlighter(highlighter);
  571. set_syntax_highlighter(plain_highlighter);
  572. MenuButton *goto_menu = memnew(MenuButton);
  573. goto_menu->set_shortcut_context(this);
  574. edit_hb->add_child(goto_menu);
  575. goto_menu->set_text(TTR("Go To"));
  576. goto_menu->set_switch_on_hover(true);
  577. goto_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  578. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  579. goto_menu->get_popup()->add_separator();
  580. bookmarks_menu = memnew(PopupMenu);
  581. bookmarks_menu->set_name("Bookmarks");
  582. goto_menu->get_popup()->add_child(bookmarks_menu);
  583. goto_menu->get_popup()->add_submenu_item(TTR("Bookmarks"), "Bookmarks");
  584. _update_bookmark_list();
  585. bookmarks_menu->connect("about_to_popup", callable_mp(this, &TextEditor::_update_bookmark_list));
  586. bookmarks_menu->connect("index_pressed", callable_mp(this, &TextEditor::_bookmark_item_pressed));
  587. goto_line_dialog = memnew(GotoLineDialog);
  588. add_child(goto_line_dialog);
  589. }
  590. TextEditor::~TextEditor() {
  591. highlighters.clear();
  592. }
  593. void TextEditor::validate() {
  594. this->code_editor->validate_script();
  595. }