text_editor.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*************************************************************************/
  2. /* text_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "text_editor.h"
  31. #include "core/os/keyboard.h"
  32. #include "editor/editor_node.h"
  33. void TextEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  34. ERR_FAIL_COND(p_highlighter.is_null());
  35. highlighters[p_highlighter->_get_name()] = p_highlighter;
  36. highlighter_menu->add_radio_check_item(p_highlighter->_get_name());
  37. }
  38. void TextEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  39. ERR_FAIL_COND(p_highlighter.is_null());
  40. Map<String, Ref<EditorSyntaxHighlighter>>::Element *el = highlighters.front();
  41. while (el != nullptr) {
  42. int highlighter_index = highlighter_menu->get_item_idx_from_text(el->key());
  43. highlighter_menu->set_item_checked(highlighter_index, el->value() == p_highlighter);
  44. el = el->next();
  45. }
  46. CodeEdit *te = code_editor->get_text_editor();
  47. te->set_syntax_highlighter(p_highlighter);
  48. }
  49. void TextEditor::_change_syntax_highlighter(int p_idx) {
  50. set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]);
  51. }
  52. void TextEditor::_load_theme_settings() {
  53. code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
  54. }
  55. String TextEditor::get_name() {
  56. String name;
  57. if (text_file->get_path().find("local://") == -1 && text_file->get_path().find("::") == -1) {
  58. name = text_file->get_path().get_file();
  59. if (is_unsaved()) {
  60. if (text_file->get_path().is_empty()) {
  61. name = TTR("[unsaved]");
  62. }
  63. name += "(*)";
  64. }
  65. } else if (text_file->get_name() != "") {
  66. name = text_file->get_name();
  67. } else {
  68. name = text_file->get_class() + "(" + itos(text_file->get_instance_id()) + ")";
  69. }
  70. return name;
  71. }
  72. Ref<Texture2D> TextEditor::get_theme_icon() {
  73. return EditorNode::get_singleton()->get_object_icon(text_file.ptr(), "");
  74. }
  75. RES TextEditor::get_edited_resource() const {
  76. return text_file;
  77. }
  78. void TextEditor::set_edited_resource(const RES &p_res) {
  79. ERR_FAIL_COND(text_file.is_valid());
  80. ERR_FAIL_COND(p_res.is_null());
  81. text_file = p_res;
  82. code_editor->get_text_editor()->set_text(text_file->get_text());
  83. code_editor->get_text_editor()->clear_undo_history();
  84. code_editor->get_text_editor()->tag_saved_version();
  85. emit_signal("name_changed");
  86. code_editor->update_line_and_column();
  87. }
  88. void TextEditor::enable_editor() {
  89. if (editor_enabled) {
  90. return;
  91. }
  92. editor_enabled = true;
  93. _load_theme_settings();
  94. }
  95. void TextEditor::add_callback(const String &p_function, PackedStringArray p_args) {
  96. }
  97. void TextEditor::set_debugger_active(bool p_active) {
  98. }
  99. Control *TextEditor::get_base_editor() const {
  100. return code_editor->get_text_editor();
  101. }
  102. Array TextEditor::get_breakpoints() {
  103. return Array();
  104. }
  105. void TextEditor::reload_text() {
  106. ERR_FAIL_COND(text_file.is_null());
  107. CodeEdit *te = code_editor->get_text_editor();
  108. int column = te->cursor_get_column();
  109. int row = te->cursor_get_line();
  110. int h = te->get_h_scroll();
  111. int v = te->get_v_scroll();
  112. te->set_text(text_file->get_text());
  113. te->cursor_set_line(row);
  114. te->cursor_set_column(column);
  115. te->set_h_scroll(h);
  116. te->set_v_scroll(v);
  117. te->tag_saved_version();
  118. code_editor->update_line_and_column();
  119. }
  120. void TextEditor::_validate_script() {
  121. emit_signal("name_changed");
  122. emit_signal("edited_script_changed");
  123. }
  124. void TextEditor::_update_bookmark_list() {
  125. bookmarks_menu->clear();
  126. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  127. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
  128. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
  129. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
  130. Array bookmark_list = code_editor->get_text_editor()->get_bookmarked_lines();
  131. if (bookmark_list.size() == 0) {
  132. return;
  133. }
  134. bookmarks_menu->add_separator();
  135. for (int i = 0; i < bookmark_list.size(); i++) {
  136. String line = code_editor->get_text_editor()->get_line(bookmark_list[i]).strip_edges();
  137. // Limit the size of the line if too big.
  138. if (line.length() > 50) {
  139. line = line.substr(0, 50);
  140. }
  141. bookmarks_menu->add_item(String::num((int)bookmark_list[i] + 1) + " - \"" + line + "\"");
  142. bookmarks_menu->set_item_metadata(bookmarks_menu->get_item_count() - 1, bookmark_list[i]);
  143. }
  144. }
  145. void TextEditor::_bookmark_item_pressed(int p_idx) {
  146. if (p_idx < 4) { // Any item before the separator.
  147. _edit_option(bookmarks_menu->get_item_id(p_idx));
  148. } else {
  149. code_editor->goto_line(bookmarks_menu->get_item_metadata(p_idx));
  150. }
  151. }
  152. void TextEditor::apply_code() {
  153. text_file->set_text(code_editor->get_text_editor()->get_text());
  154. }
  155. bool TextEditor::is_unsaved() {
  156. const bool unsaved =
  157. code_editor->get_text_editor()->get_version() != code_editor->get_text_editor()->get_saved_version() ||
  158. text_file->get_path().is_empty(); // In memory.
  159. return unsaved;
  160. }
  161. Variant TextEditor::get_edit_state() {
  162. return code_editor->get_edit_state();
  163. }
  164. void TextEditor::set_edit_state(const Variant &p_state) {
  165. code_editor->set_edit_state(p_state);
  166. Dictionary state = p_state;
  167. if (state.has("syntax_highlighter")) {
  168. int idx = highlighter_menu->get_item_idx_from_text(state["syntax_highlighter"]);
  169. if (idx >= 0) {
  170. _change_syntax_highlighter(idx);
  171. }
  172. }
  173. ensure_focus();
  174. }
  175. void TextEditor::trim_trailing_whitespace() {
  176. code_editor->trim_trailing_whitespace();
  177. }
  178. void TextEditor::insert_final_newline() {
  179. code_editor->insert_final_newline();
  180. }
  181. void TextEditor::convert_indent_to_spaces() {
  182. code_editor->convert_indent_to_spaces();
  183. }
  184. void TextEditor::convert_indent_to_tabs() {
  185. code_editor->convert_indent_to_tabs();
  186. }
  187. void TextEditor::tag_saved_version() {
  188. code_editor->get_text_editor()->tag_saved_version();
  189. }
  190. void TextEditor::goto_line(int p_line, bool p_with_error) {
  191. code_editor->goto_line(p_line);
  192. }
  193. void TextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  194. code_editor->goto_line_selection(p_line, p_begin, p_end);
  195. }
  196. void TextEditor::set_executing_line(int p_line) {
  197. code_editor->set_executing_line(p_line);
  198. }
  199. void TextEditor::clear_executing_line() {
  200. code_editor->clear_executing_line();
  201. }
  202. void TextEditor::ensure_focus() {
  203. code_editor->get_text_editor()->grab_focus();
  204. }
  205. Vector<String> TextEditor::get_functions() {
  206. return Vector<String>();
  207. }
  208. bool TextEditor::show_members_overview() {
  209. return true;
  210. }
  211. void TextEditor::update_settings() {
  212. code_editor->update_editor_settings();
  213. }
  214. void TextEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  215. code_editor->get_text_editor()->set_tooltip_request_func(p_obj, p_method, this);
  216. }
  217. Control *TextEditor::get_edit_menu() {
  218. return edit_hb;
  219. }
  220. void TextEditor::clear_edit_menu() {
  221. memdelete(edit_hb);
  222. }
  223. void TextEditor::_edit_option(int p_op) {
  224. CodeEdit *tx = code_editor->get_text_editor();
  225. switch (p_op) {
  226. case EDIT_UNDO: {
  227. tx->undo();
  228. tx->call_deferred("grab_focus");
  229. } break;
  230. case EDIT_REDO: {
  231. tx->redo();
  232. tx->call_deferred("grab_focus");
  233. } break;
  234. case EDIT_CUT: {
  235. tx->cut();
  236. tx->call_deferred("grab_focus");
  237. } break;
  238. case EDIT_COPY: {
  239. tx->copy();
  240. tx->call_deferred("grab_focus");
  241. } break;
  242. case EDIT_PASTE: {
  243. tx->paste();
  244. tx->call_deferred("grab_focus");
  245. } break;
  246. case EDIT_SELECT_ALL: {
  247. tx->select_all();
  248. tx->call_deferred("grab_focus");
  249. } break;
  250. case EDIT_MOVE_LINE_UP: {
  251. code_editor->move_lines_up();
  252. } break;
  253. case EDIT_MOVE_LINE_DOWN: {
  254. code_editor->move_lines_down();
  255. } break;
  256. case EDIT_INDENT_LEFT: {
  257. tx->indent_selected_lines_left();
  258. } break;
  259. case EDIT_INDENT_RIGHT: {
  260. tx->indent_selected_lines_right();
  261. } break;
  262. case EDIT_DELETE_LINE: {
  263. code_editor->delete_lines();
  264. } break;
  265. case EDIT_CLONE_DOWN: {
  266. code_editor->clone_lines_down();
  267. } break;
  268. case EDIT_TOGGLE_FOLD_LINE: {
  269. tx->toggle_foldable_line(tx->cursor_get_line());
  270. tx->update();
  271. } break;
  272. case EDIT_FOLD_ALL_LINES: {
  273. tx->fold_all_lines();
  274. tx->update();
  275. } break;
  276. case EDIT_UNFOLD_ALL_LINES: {
  277. tx->unhide_all_lines();
  278. tx->update();
  279. } break;
  280. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  281. trim_trailing_whitespace();
  282. } break;
  283. case EDIT_CONVERT_INDENT_TO_SPACES: {
  284. convert_indent_to_spaces();
  285. } break;
  286. case EDIT_CONVERT_INDENT_TO_TABS: {
  287. convert_indent_to_tabs();
  288. } break;
  289. case EDIT_TO_UPPERCASE: {
  290. _convert_case(CodeTextEditor::UPPER);
  291. } break;
  292. case EDIT_TO_LOWERCASE: {
  293. _convert_case(CodeTextEditor::LOWER);
  294. } break;
  295. case EDIT_CAPITALIZE: {
  296. _convert_case(CodeTextEditor::CAPITALIZE);
  297. } break;
  298. case SEARCH_FIND: {
  299. code_editor->get_find_replace_bar()->popup_search();
  300. } break;
  301. case SEARCH_FIND_NEXT: {
  302. code_editor->get_find_replace_bar()->search_next();
  303. } break;
  304. case SEARCH_FIND_PREV: {
  305. code_editor->get_find_replace_bar()->search_prev();
  306. } break;
  307. case SEARCH_REPLACE: {
  308. code_editor->get_find_replace_bar()->popup_replace();
  309. } break;
  310. case SEARCH_IN_FILES: {
  311. String selected_text = code_editor->get_text_editor()->get_selection_text();
  312. // Yep, because it doesn't make sense to instance this dialog for every single script open...
  313. // So this will be delegated to the ScriptEditor.
  314. emit_signal("search_in_files_requested", selected_text);
  315. } break;
  316. case REPLACE_IN_FILES: {
  317. String selected_text = code_editor->get_text_editor()->get_selection_text();
  318. emit_signal("replace_in_files_requested", selected_text);
  319. } break;
  320. case SEARCH_GOTO_LINE: {
  321. goto_line_dialog->popup_find_line(tx);
  322. } break;
  323. case BOOKMARK_TOGGLE: {
  324. code_editor->toggle_bookmark();
  325. } break;
  326. case BOOKMARK_GOTO_NEXT: {
  327. code_editor->goto_next_bookmark();
  328. } break;
  329. case BOOKMARK_GOTO_PREV: {
  330. code_editor->goto_prev_bookmark();
  331. } break;
  332. case BOOKMARK_REMOVE_ALL: {
  333. code_editor->remove_all_bookmarks();
  334. } break;
  335. }
  336. }
  337. void TextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
  338. code_editor->convert_case(p_case);
  339. }
  340. void TextEditor::_bind_methods() {
  341. ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &TextEditor::add_syntax_highlighter);
  342. }
  343. static ScriptEditorBase *create_editor(const RES &p_resource) {
  344. if (Object::cast_to<TextFile>(*p_resource)) {
  345. return memnew(TextEditor);
  346. }
  347. return nullptr;
  348. }
  349. void TextEditor::register_editor() {
  350. ScriptEditor::register_create_script_editor_function(create_editor);
  351. }
  352. void TextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  353. Ref<InputEventMouseButton> mb = ev;
  354. if (mb.is_valid()) {
  355. if (mb->get_button_index() == MOUSE_BUTTON_RIGHT) {
  356. int col, row;
  357. CodeEdit *tx = code_editor->get_text_editor();
  358. tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col);
  359. tx->set_right_click_moves_caret(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret"));
  360. bool can_fold = tx->can_fold_line(row);
  361. bool is_folded = tx->is_line_folded(row);
  362. if (tx->is_right_click_moving_caret()) {
  363. if (tx->is_selection_active()) {
  364. int from_line = tx->get_selection_from_line();
  365. int to_line = tx->get_selection_to_line();
  366. int from_column = tx->get_selection_from_column();
  367. int to_column = tx->get_selection_to_column();
  368. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  369. // Right click is outside the selected text.
  370. tx->deselect();
  371. }
  372. }
  373. if (!tx->is_selection_active()) {
  374. tx->cursor_set_line(row, true, false);
  375. tx->cursor_set_column(col);
  376. }
  377. }
  378. if (!mb->is_pressed()) {
  379. _make_context_menu(tx->is_selection_active(), can_fold, is_folded, get_local_mouse_position());
  380. }
  381. }
  382. }
  383. Ref<InputEventKey> k = ev;
  384. if (k.is_valid() && k->is_pressed() && k->get_keycode() == KEY_MENU) {
  385. CodeEdit *tx = code_editor->get_text_editor();
  386. int line = tx->cursor_get_line();
  387. _make_context_menu(tx->is_selection_active(), tx->can_fold_line(line), tx->is_line_folded(line), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->_get_cursor_pixel_pos()));
  388. context_menu->grab_focus();
  389. }
  390. }
  391. void TextEditor::_make_context_menu(bool p_selection, bool p_can_fold, bool p_is_folded, Vector2 p_position) {
  392. context_menu->clear();
  393. if (p_selection) {
  394. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  395. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  396. }
  397. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  398. context_menu->add_separator();
  399. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  400. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  401. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  402. context_menu->add_separator();
  403. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  404. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  405. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  406. if (p_selection) {
  407. context_menu->add_separator();
  408. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
  409. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
  410. }
  411. if (p_can_fold || p_is_folded) {
  412. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  413. }
  414. context_menu->set_position(get_global_transform().xform(p_position));
  415. context_menu->set_size(Vector2(1, 1));
  416. context_menu->popup();
  417. }
  418. TextEditor::TextEditor() {
  419. code_editor = memnew(CodeTextEditor);
  420. add_child(code_editor);
  421. code_editor->add_theme_constant_override("separation", 0);
  422. code_editor->connect("load_theme_settings", callable_mp(this, &TextEditor::_load_theme_settings));
  423. code_editor->connect("validate_script", callable_mp(this, &TextEditor::_validate_script));
  424. code_editor->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  425. code_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  426. update_settings();
  427. code_editor->get_text_editor()->set_context_menu_enabled(false);
  428. code_editor->get_text_editor()->connect("gui_input", callable_mp(this, &TextEditor::_text_edit_gui_input));
  429. context_menu = memnew(PopupMenu);
  430. add_child(context_menu);
  431. context_menu->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  432. edit_hb = memnew(HBoxContainer);
  433. search_menu = memnew(MenuButton);
  434. search_menu->set_shortcut_context(this);
  435. edit_hb->add_child(search_menu);
  436. search_menu->set_text(TTR("Search"));
  437. search_menu->set_switch_on_hover(true);
  438. search_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  439. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  440. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  441. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  442. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  443. search_menu->get_popup()->add_separator();
  444. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_in_files"), SEARCH_IN_FILES);
  445. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES);
  446. edit_menu = memnew(MenuButton);
  447. edit_menu->set_shortcut_context(this);
  448. edit_hb->add_child(edit_menu);
  449. edit_menu->set_text(TTR("Edit"));
  450. edit_menu->set_switch_on_hover(true);
  451. edit_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  452. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  453. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("un_redo"), EDIT_REDO);
  454. edit_menu->get_popup()->add_separator();
  455. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  456. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  457. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  458. edit_menu->get_popup()->add_separator();
  459. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  460. edit_menu->get_popup()->add_separator();
  461. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  462. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  463. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  464. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  465. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  466. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  467. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
  468. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
  469. edit_menu->get_popup()->add_separator();
  470. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
  471. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  472. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
  473. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
  474. edit_menu->get_popup()->add_separator();
  475. PopupMenu *convert_case = memnew(PopupMenu);
  476. convert_case->set_name("convert_case");
  477. edit_menu->get_popup()->add_child(convert_case);
  478. edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
  479. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase")), EDIT_TO_UPPERCASE);
  480. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase")), EDIT_TO_LOWERCASE);
  481. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE);
  482. convert_case->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  483. highlighter_menu = memnew(PopupMenu);
  484. highlighter_menu->set_name("highlighter_menu");
  485. edit_menu->get_popup()->add_child(highlighter_menu);
  486. edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "highlighter_menu");
  487. highlighter_menu->connect("id_pressed", callable_mp(this, &TextEditor::_change_syntax_highlighter));
  488. Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
  489. plain_highlighter.instance();
  490. add_syntax_highlighter(plain_highlighter);
  491. Ref<EditorStandardSyntaxHighlighter> highlighter;
  492. highlighter.instance();
  493. add_syntax_highlighter(highlighter);
  494. set_syntax_highlighter(plain_highlighter);
  495. MenuButton *goto_menu = memnew(MenuButton);
  496. goto_menu->set_shortcut_context(this);
  497. edit_hb->add_child(goto_menu);
  498. goto_menu->set_text(TTR("Go To"));
  499. goto_menu->set_switch_on_hover(true);
  500. goto_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  501. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  502. goto_menu->get_popup()->add_separator();
  503. bookmarks_menu = memnew(PopupMenu);
  504. bookmarks_menu->set_name("Bookmarks");
  505. goto_menu->get_popup()->add_child(bookmarks_menu);
  506. goto_menu->get_popup()->add_submenu_item(TTR("Bookmarks"), "Bookmarks");
  507. _update_bookmark_list();
  508. bookmarks_menu->connect("about_to_popup", callable_mp(this, &TextEditor::_update_bookmark_list));
  509. bookmarks_menu->connect("index_pressed", callable_mp(this, &TextEditor::_bookmark_item_pressed));
  510. goto_line_dialog = memnew(GotoLineDialog);
  511. add_child(goto_line_dialog);
  512. code_editor->get_text_editor()->set_drag_forwarding(this);
  513. }
  514. TextEditor::~TextEditor() {
  515. highlighters.clear();
  516. }
  517. void TextEditor::validate() {
  518. }