text_editor.cpp 23 KB

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