text_editor.cpp 23 KB

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