text_editor.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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() {
  233. code_editor->get_text_editor()->convert_indent();
  234. }
  235. void TextEditor::tag_saved_version() {
  236. code_editor->get_text_editor()->tag_saved_version();
  237. }
  238. void TextEditor::goto_line(int p_line, bool p_with_error) {
  239. code_editor->goto_line(p_line);
  240. }
  241. void TextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  242. code_editor->goto_line_selection(p_line, p_begin, p_end);
  243. }
  244. void TextEditor::set_executing_line(int p_line) {
  245. code_editor->set_executing_line(p_line);
  246. }
  247. void TextEditor::clear_executing_line() {
  248. code_editor->clear_executing_line();
  249. }
  250. void TextEditor::ensure_focus() {
  251. code_editor->get_text_editor()->grab_focus();
  252. }
  253. Vector<String> TextEditor::get_functions() {
  254. return Vector<String>();
  255. }
  256. bool TextEditor::show_members_overview() {
  257. return true;
  258. }
  259. void TextEditor::update_settings() {
  260. code_editor->update_editor_settings();
  261. }
  262. void TextEditor::set_tooltip_request_func(const Callable &p_toolip_callback) {
  263. Variant args[1] = { this };
  264. const Variant *argp[] = { &args[0] };
  265. code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1));
  266. }
  267. Control *TextEditor::get_edit_menu() {
  268. return edit_hb;
  269. }
  270. void TextEditor::clear_edit_menu() {
  271. memdelete(edit_hb);
  272. }
  273. void TextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
  274. code_editor->set_find_replace_bar(p_bar);
  275. }
  276. void TextEditor::_edit_option(int p_op) {
  277. CodeEdit *tx = code_editor->get_text_editor();
  278. switch (p_op) {
  279. case EDIT_UNDO: {
  280. tx->undo();
  281. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  282. } break;
  283. case EDIT_REDO: {
  284. tx->redo();
  285. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  286. } break;
  287. case EDIT_CUT: {
  288. tx->cut();
  289. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  290. } break;
  291. case EDIT_COPY: {
  292. tx->copy();
  293. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  294. } break;
  295. case EDIT_PASTE: {
  296. tx->paste();
  297. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  298. } break;
  299. case EDIT_SELECT_ALL: {
  300. tx->select_all();
  301. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  302. } break;
  303. case EDIT_MOVE_LINE_UP: {
  304. code_editor->move_lines_up();
  305. } break;
  306. case EDIT_MOVE_LINE_DOWN: {
  307. code_editor->move_lines_down();
  308. } break;
  309. case EDIT_INDENT: {
  310. tx->indent_lines();
  311. } break;
  312. case EDIT_UNINDENT: {
  313. tx->unindent_lines();
  314. } break;
  315. case EDIT_DELETE_LINE: {
  316. code_editor->delete_lines();
  317. } break;
  318. case EDIT_DUPLICATE_SELECTION: {
  319. code_editor->duplicate_selection();
  320. } break;
  321. case EDIT_DUPLICATE_LINES: {
  322. code_editor->get_text_editor()->duplicate_lines();
  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. tx->set_indent_using_spaces(true);
  348. convert_indent();
  349. } break;
  350. case EDIT_CONVERT_INDENT_TO_TABS: {
  351. tx->set_indent_using_spaces(false);
  352. convert_indent();
  353. } break;
  354. case EDIT_TO_UPPERCASE: {
  355. _convert_case(CodeTextEditor::UPPER);
  356. } break;
  357. case EDIT_TO_LOWERCASE: {
  358. _convert_case(CodeTextEditor::LOWER);
  359. } break;
  360. case EDIT_CAPITALIZE: {
  361. _convert_case(CodeTextEditor::CAPITALIZE);
  362. } break;
  363. case EDIT_TOGGLE_WORD_WRAP: {
  364. TextEdit::LineWrappingMode wrap = code_editor->get_text_editor()->get_line_wrapping_mode();
  365. code_editor->get_text_editor()->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY);
  366. } break;
  367. case SEARCH_FIND: {
  368. code_editor->get_find_replace_bar()->popup_search();
  369. } break;
  370. case SEARCH_FIND_NEXT: {
  371. code_editor->get_find_replace_bar()->search_next();
  372. } break;
  373. case SEARCH_FIND_PREV: {
  374. code_editor->get_find_replace_bar()->search_prev();
  375. } break;
  376. case SEARCH_REPLACE: {
  377. code_editor->get_find_replace_bar()->popup_replace();
  378. } break;
  379. case SEARCH_IN_FILES: {
  380. String selected_text = code_editor->get_text_editor()->get_selected_text();
  381. // Yep, because it doesn't make sense to instance this dialog for every single script open...
  382. // So this will be delegated to the ScriptEditor.
  383. emit_signal(SNAME("search_in_files_requested"), selected_text);
  384. } break;
  385. case REPLACE_IN_FILES: {
  386. String selected_text = code_editor->get_text_editor()->get_selected_text();
  387. emit_signal(SNAME("replace_in_files_requested"), selected_text);
  388. } break;
  389. case SEARCH_GOTO_LINE: {
  390. goto_line_dialog->popup_find_line(tx);
  391. } break;
  392. case BOOKMARK_TOGGLE: {
  393. code_editor->toggle_bookmark();
  394. } break;
  395. case BOOKMARK_GOTO_NEXT: {
  396. code_editor->goto_next_bookmark();
  397. } break;
  398. case BOOKMARK_GOTO_PREV: {
  399. code_editor->goto_prev_bookmark();
  400. } break;
  401. case BOOKMARK_REMOVE_ALL: {
  402. code_editor->remove_all_bookmarks();
  403. } break;
  404. }
  405. }
  406. void TextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
  407. code_editor->convert_case(p_case);
  408. }
  409. ScriptEditorBase *TextEditor::create_editor(const Ref<Resource> &p_resource) {
  410. if (Object::cast_to<TextFile>(*p_resource) || Object::cast_to<JSON>(*p_resource)) {
  411. return memnew(TextEditor);
  412. }
  413. return nullptr;
  414. }
  415. void TextEditor::register_editor() {
  416. ScriptEditor::register_create_script_editor_function(create_editor);
  417. }
  418. void TextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  419. Ref<InputEventMouseButton> mb = ev;
  420. if (mb.is_valid()) {
  421. if (mb->get_button_index() == MouseButton::RIGHT) {
  422. CodeEdit *tx = code_editor->get_text_editor();
  423. Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());
  424. int row = pos.y;
  425. int col = pos.x;
  426. tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));
  427. bool can_fold = tx->can_fold_line(row);
  428. bool is_folded = tx->is_line_folded(row);
  429. if (tx->is_move_caret_on_right_click_enabled()) {
  430. tx->remove_secondary_carets();
  431. if (tx->has_selection()) {
  432. int from_line = tx->get_selection_from_line();
  433. int to_line = tx->get_selection_to_line();
  434. int from_column = tx->get_selection_from_column();
  435. int to_column = tx->get_selection_to_column();
  436. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  437. // Right click is outside the selected text.
  438. tx->deselect();
  439. }
  440. }
  441. if (!tx->has_selection()) {
  442. tx->set_caret_line(row, true, false);
  443. tx->set_caret_column(col);
  444. }
  445. }
  446. if (!mb->is_pressed()) {
  447. _make_context_menu(tx->has_selection(), can_fold, is_folded, get_local_mouse_position());
  448. }
  449. }
  450. }
  451. Ref<InputEventKey> k = ev;
  452. if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) {
  453. CodeEdit *tx = code_editor->get_text_editor();
  454. int line = tx->get_caret_line(0);
  455. tx->adjust_viewport_to_caret(0);
  456. _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)));
  457. context_menu->grab_focus();
  458. }
  459. }
  460. void TextEditor::_prepare_edit_menu() {
  461. const CodeEdit *tx = code_editor->get_text_editor();
  462. PopupMenu *popup = edit_menu->get_popup();
  463. popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
  464. popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
  465. }
  466. void TextEditor::_make_context_menu(bool p_selection, bool p_can_fold, bool p_is_folded, Vector2 p_position) {
  467. context_menu->clear();
  468. if (p_selection) {
  469. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  470. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  471. }
  472. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  473. context_menu->add_separator();
  474. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  475. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  476. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  477. context_menu->add_separator();
  478. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
  479. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
  480. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  481. if (p_selection) {
  482. context_menu->add_separator();
  483. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
  484. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
  485. }
  486. if (p_can_fold || p_is_folded) {
  487. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  488. }
  489. const CodeEdit *tx = code_editor->get_text_editor();
  490. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo());
  491. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo());
  492. context_menu->set_position(get_screen_position() + p_position);
  493. context_menu->reset_size();
  494. context_menu->popup();
  495. }
  496. void TextEditor::update_toggle_scripts_button() {
  497. code_editor->update_toggle_scripts_button();
  498. }
  499. TextEditor::TextEditor() {
  500. code_editor = memnew(CodeTextEditor);
  501. add_child(code_editor);
  502. code_editor->add_theme_constant_override("separation", 0);
  503. code_editor->connect("load_theme_settings", callable_mp(this, &TextEditor::_load_theme_settings));
  504. code_editor->connect("validate_script", callable_mp(this, &TextEditor::_validate_script));
  505. code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  506. code_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  507. code_editor->show_toggle_scripts_button();
  508. update_settings();
  509. code_editor->get_text_editor()->set_context_menu_enabled(false);
  510. code_editor->get_text_editor()->connect("gui_input", callable_mp(this, &TextEditor::_text_edit_gui_input));
  511. context_menu = memnew(PopupMenu);
  512. add_child(context_menu);
  513. context_menu->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  514. edit_hb = memnew(HBoxContainer);
  515. edit_menu = memnew(MenuButton);
  516. edit_menu->set_shortcut_context(this);
  517. edit_hb->add_child(edit_menu);
  518. edit_menu->set_text(TTR("Edit"));
  519. edit_menu->set_switch_on_hover(true);
  520. edit_menu->connect("about_to_popup", callable_mp(this, &TextEditor::_prepare_edit_menu));
  521. edit_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  522. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  523. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  524. edit_menu->get_popup()->add_separator();
  525. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  526. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  527. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  528. edit_menu->get_popup()->add_separator();
  529. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  530. edit_menu->get_popup()->add_separator();
  531. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  532. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  533. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
  534. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
  535. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  536. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  537. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
  538. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
  539. edit_menu->get_popup()->add_separator();
  540. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
  541. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);
  542. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP);
  543. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  544. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
  545. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
  546. edit_menu->get_popup()->add_separator();
  547. PopupMenu *convert_case = memnew(PopupMenu);
  548. convert_case->set_name("ConvertCase");
  549. edit_menu->get_popup()->add_child(convert_case);
  550. edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "ConvertCase");
  551. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase")), EDIT_TO_UPPERCASE);
  552. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase")), EDIT_TO_LOWERCASE);
  553. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE);
  554. convert_case->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  555. highlighter_menu = memnew(PopupMenu);
  556. highlighter_menu->set_name("HighlighterMenu");
  557. edit_menu->get_popup()->add_child(highlighter_menu);
  558. edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "HighlighterMenu");
  559. highlighter_menu->connect("id_pressed", callable_mp(this, &TextEditor::_change_syntax_highlighter));
  560. Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
  561. plain_highlighter.instantiate();
  562. add_syntax_highlighter(plain_highlighter);
  563. Ref<EditorStandardSyntaxHighlighter> highlighter;
  564. highlighter.instantiate();
  565. add_syntax_highlighter(highlighter);
  566. set_syntax_highlighter(plain_highlighter);
  567. search_menu = memnew(MenuButton);
  568. search_menu->set_shortcut_context(this);
  569. edit_hb->add_child(search_menu);
  570. search_menu->set_text(TTR("Search"));
  571. search_menu->set_switch_on_hover(true);
  572. search_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  573. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  574. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  575. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  576. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  577. search_menu->get_popup()->add_separator();
  578. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_in_files"), SEARCH_IN_FILES);
  579. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES);
  580. MenuButton *goto_menu = memnew(MenuButton);
  581. goto_menu->set_shortcut_context(this);
  582. edit_hb->add_child(goto_menu);
  583. goto_menu->set_text(TTR("Go To"));
  584. goto_menu->set_switch_on_hover(true);
  585. goto_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
  586. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  587. goto_menu->get_popup()->add_separator();
  588. bookmarks_menu = memnew(PopupMenu);
  589. bookmarks_menu->set_name("BookmarksMenu");
  590. goto_menu->get_popup()->add_child(bookmarks_menu);
  591. goto_menu->get_popup()->add_submenu_item(TTR("Bookmarks"), "BookmarksMenu");
  592. _update_bookmark_list();
  593. bookmarks_menu->connect("about_to_popup", callable_mp(this, &TextEditor::_update_bookmark_list));
  594. bookmarks_menu->connect("index_pressed", callable_mp(this, &TextEditor::_bookmark_item_pressed));
  595. goto_line_dialog = memnew(GotoLineDialog);
  596. add_child(goto_line_dialog);
  597. }
  598. TextEditor::~TextEditor() {
  599. highlighters.clear();
  600. }
  601. void TextEditor::validate() {
  602. code_editor->validate_script();
  603. }