text_editor.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*************************************************************************/
  2. /* text_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "editor_node.h"
  32. void TextEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
  33. highlighters[p_highlighter->get_name()] = p_highlighter;
  34. highlighter_menu->add_radio_check_item(p_highlighter->get_name());
  35. }
  36. void TextEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
  37. TextEdit *te = code_editor->get_text_edit();
  38. te->_set_syntax_highlighting(p_highlighter);
  39. if (p_highlighter != NULL) {
  40. highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(p_highlighter->get_name()), true);
  41. } else {
  42. highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text("Standard"), true);
  43. }
  44. // little work around. GDScript highlighter goes through text_edit for colours,
  45. // so to remove all colours we need to set and unset them here.
  46. if (p_highlighter == NULL) { // standard
  47. TextEdit *text_edit = code_editor->get_text_edit();
  48. text_edit->add_color_override("number_color", colors_cache.font_color);
  49. text_edit->add_color_override("function_color", colors_cache.font_color);
  50. text_edit->add_color_override("number_color", colors_cache.font_color);
  51. text_edit->add_color_override("member_variable_color", colors_cache.font_color);
  52. } else {
  53. _load_theme_settings();
  54. }
  55. }
  56. void TextEditor::_change_syntax_highlighter(int p_idx) {
  57. Map<String, SyntaxHighlighter *>::Element *el = highlighters.front();
  58. while (el != NULL) {
  59. highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(el->key()), false);
  60. el = el->next();
  61. }
  62. set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]);
  63. }
  64. void TextEditor::_load_theme_settings() {
  65. TextEdit *text_edit = code_editor->get_text_edit();
  66. text_edit->clear_colors();
  67. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  68. Color completion_background_color = EDITOR_GET("text_editor/highlighting/completion_background_color");
  69. Color completion_selected_color = EDITOR_GET("text_editor/highlighting/completion_selected_color");
  70. Color completion_existing_color = EDITOR_GET("text_editor/highlighting/completion_existing_color");
  71. Color completion_scroll_color = EDITOR_GET("text_editor/highlighting/completion_scroll_color");
  72. Color completion_font_color = EDITOR_GET("text_editor/highlighting/completion_font_color");
  73. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  74. Color line_number_color = EDITOR_GET("text_editor/highlighting/line_number_color");
  75. Color caret_color = EDITOR_GET("text_editor/highlighting/caret_color");
  76. Color caret_background_color = EDITOR_GET("text_editor/highlighting/caret_background_color");
  77. Color text_selected_color = EDITOR_GET("text_editor/highlighting/text_selected_color");
  78. Color selection_color = EDITOR_GET("text_editor/highlighting/selection_color");
  79. Color brace_mismatch_color = EDITOR_GET("text_editor/highlighting/brace_mismatch_color");
  80. Color current_line_color = EDITOR_GET("text_editor/highlighting/current_line_color");
  81. Color line_length_guideline_color = EDITOR_GET("text_editor/highlighting/line_length_guideline_color");
  82. Color word_highlighted_color = EDITOR_GET("text_editor/highlighting/word_highlighted_color");
  83. Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
  84. Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
  85. Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
  86. Color mark_color = EDITOR_GET("text_editor/highlighting/mark_color");
  87. Color bookmark_color = EDITOR_GET("text_editor/highlighting/bookmark_color");
  88. Color breakpoint_color = EDITOR_GET("text_editor/highlighting/breakpoint_color");
  89. Color executing_line_color = EDITOR_GET("text_editor/highlighting/executing_line_color");
  90. Color code_folding_color = EDITOR_GET("text_editor/highlighting/code_folding_color");
  91. Color search_result_color = EDITOR_GET("text_editor/highlighting/search_result_color");
  92. Color search_result_border_color = EDITOR_GET("text_editor/highlighting/search_result_border_color");
  93. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  94. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  95. Color basetype_color = EDITOR_GET("text_editor/highlighting/base_type_color");
  96. Color type_color = EDITOR_GET("text_editor/highlighting/engine_type_color");
  97. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  98. Color string_color = EDITOR_GET("text_editor/highlighting/string_color");
  99. text_edit->add_color_override("background_color", background_color);
  100. text_edit->add_color_override("completion_background_color", completion_background_color);
  101. text_edit->add_color_override("completion_selected_color", completion_selected_color);
  102. text_edit->add_color_override("completion_existing_color", completion_existing_color);
  103. text_edit->add_color_override("completion_scroll_color", completion_scroll_color);
  104. text_edit->add_color_override("completion_font_color", completion_font_color);
  105. text_edit->add_color_override("font_color", text_color);
  106. text_edit->add_color_override("line_number_color", line_number_color);
  107. text_edit->add_color_override("caret_color", caret_color);
  108. text_edit->add_color_override("caret_background_color", caret_background_color);
  109. text_edit->add_color_override("font_selected_color", text_selected_color);
  110. text_edit->add_color_override("selection_color", selection_color);
  111. text_edit->add_color_override("brace_mismatch_color", brace_mismatch_color);
  112. text_edit->add_color_override("current_line_color", current_line_color);
  113. text_edit->add_color_override("line_length_guideline_color", line_length_guideline_color);
  114. text_edit->add_color_override("word_highlighted_color", word_highlighted_color);
  115. text_edit->add_color_override("number_color", number_color);
  116. text_edit->add_color_override("function_color", function_color);
  117. text_edit->add_color_override("member_variable_color", member_variable_color);
  118. text_edit->add_color_override("breakpoint_color", breakpoint_color);
  119. text_edit->add_color_override("executing_line_color", executing_line_color);
  120. text_edit->add_color_override("mark_color", mark_color);
  121. text_edit->add_color_override("bookmark_color", bookmark_color);
  122. text_edit->add_color_override("code_folding_color", code_folding_color);
  123. text_edit->add_color_override("search_result_color", search_result_color);
  124. text_edit->add_color_override("search_result_border_color", search_result_border_color);
  125. text_edit->add_color_override("symbol_color", symbol_color);
  126. text_edit->add_constant_override("line_spacing", EDITOR_DEF("text_editor/theme/line_spacing", 6));
  127. colors_cache.font_color = text_color;
  128. colors_cache.symbol_color = symbol_color;
  129. colors_cache.keyword_color = keyword_color;
  130. colors_cache.basetype_color = basetype_color;
  131. colors_cache.type_color = type_color;
  132. colors_cache.comment_color = comment_color;
  133. colors_cache.string_color = string_color;
  134. }
  135. String TextEditor::get_name() {
  136. String name;
  137. if (text_file->get_path().find("local://") == -1 && text_file->get_path().find("::") == -1) {
  138. name = text_file->get_path().get_file();
  139. if (is_unsaved()) {
  140. name += "(*)";
  141. }
  142. } else if (text_file->get_name() != "") {
  143. name = text_file->get_name();
  144. } else {
  145. name = text_file->get_class() + "(" + itos(text_file->get_instance_id()) + ")";
  146. }
  147. return name;
  148. }
  149. Ref<Texture> TextEditor::get_icon() {
  150. return EditorNode::get_singleton()->get_object_icon(text_file.operator->(), "");
  151. }
  152. RES TextEditor::get_edited_resource() const {
  153. return text_file;
  154. }
  155. void TextEditor::set_edited_resource(const RES &p_res) {
  156. ERR_FAIL_COND(!text_file.is_null());
  157. text_file = p_res;
  158. code_editor->get_text_edit()->set_text(text_file->get_text());
  159. code_editor->get_text_edit()->clear_undo_history();
  160. code_editor->get_text_edit()->tag_saved_version();
  161. emit_signal("name_changed");
  162. code_editor->update_line_and_column();
  163. }
  164. void TextEditor::add_callback(const String &p_function, PoolStringArray p_args) {
  165. }
  166. void TextEditor::set_debugger_active(bool p_active) {
  167. }
  168. void TextEditor::get_breakpoints(List<int> *p_breakpoints) {
  169. }
  170. void TextEditor::reload_text() {
  171. ERR_FAIL_COND(text_file.is_null());
  172. TextEdit *te = code_editor->get_text_edit();
  173. int column = te->cursor_get_column();
  174. int row = te->cursor_get_line();
  175. int h = te->get_h_scroll();
  176. int v = te->get_v_scroll();
  177. te->set_text(text_file->get_text());
  178. te->cursor_set_line(row);
  179. te->cursor_set_column(column);
  180. te->set_h_scroll(h);
  181. te->set_v_scroll(v);
  182. te->tag_saved_version();
  183. code_editor->update_line_and_column();
  184. }
  185. void TextEditor::_validate_script() {
  186. emit_signal("name_changed");
  187. emit_signal("edited_script_changed");
  188. }
  189. void TextEditor::_update_bookmark_list() {
  190. bookmarks_menu->get_popup()->clear();
  191. bookmarks_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  192. bookmarks_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
  193. bookmarks_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
  194. bookmarks_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
  195. Array bookmark_list = code_editor->get_text_edit()->get_bookmarks_array();
  196. if (bookmark_list.size() == 0) {
  197. return;
  198. }
  199. bookmarks_menu->get_popup()->add_separator();
  200. for (int i = 0; i < bookmark_list.size(); i++) {
  201. String line = code_editor->get_text_edit()->get_line(bookmark_list[i]).strip_edges();
  202. // Limit the size of the line if too big.
  203. if (line.length() > 50) {
  204. line = line.substr(0, 50);
  205. }
  206. bookmarks_menu->get_popup()->add_item(String::num((int)bookmark_list[i] + 1) + " - \"" + line + "\"");
  207. bookmarks_menu->get_popup()->set_item_metadata(bookmarks_menu->get_popup()->get_item_count() - 1, bookmark_list[i]);
  208. }
  209. }
  210. void TextEditor::_bookmark_item_pressed(int p_idx) {
  211. if (p_idx < 4) { // Any item before the separator.
  212. _edit_option(bookmarks_menu->get_popup()->get_item_id(p_idx));
  213. } else {
  214. code_editor->goto_line(bookmarks_menu->get_popup()->get_item_metadata(p_idx));
  215. }
  216. }
  217. void TextEditor::apply_code() {
  218. text_file->set_text(code_editor->get_text_edit()->get_text());
  219. }
  220. bool TextEditor::is_unsaved() {
  221. return code_editor->get_text_edit()->get_version() != code_editor->get_text_edit()->get_saved_version();
  222. }
  223. Variant TextEditor::get_edit_state() {
  224. return code_editor->get_edit_state();
  225. }
  226. void TextEditor::set_edit_state(const Variant &p_state) {
  227. code_editor->set_edit_state(p_state);
  228. Dictionary state = p_state;
  229. if (state.has("syntax_highlighter")) {
  230. int idx = highlighter_menu->get_item_idx_from_text(state["syntax_highlighter"]);
  231. if (idx >= 0) {
  232. _change_syntax_highlighter(idx);
  233. }
  234. }
  235. }
  236. void TextEditor::trim_trailing_whitespace() {
  237. code_editor->trim_trailing_whitespace();
  238. }
  239. void TextEditor::convert_indent_to_spaces() {
  240. code_editor->convert_indent_to_spaces();
  241. }
  242. void TextEditor::convert_indent_to_tabs() {
  243. code_editor->convert_indent_to_tabs();
  244. }
  245. void TextEditor::tag_saved_version() {
  246. code_editor->get_text_edit()->tag_saved_version();
  247. }
  248. void TextEditor::goto_line(int p_line, bool p_with_error) {
  249. code_editor->goto_line(p_line);
  250. }
  251. void TextEditor::set_executing_line(int p_line) {
  252. code_editor->set_executing_line(p_line);
  253. }
  254. void TextEditor::clear_executing_line() {
  255. code_editor->clear_executing_line();
  256. }
  257. void TextEditor::ensure_focus() {
  258. code_editor->get_text_edit()->grab_focus();
  259. }
  260. Vector<String> TextEditor::get_functions() {
  261. return Vector<String>();
  262. }
  263. bool TextEditor::show_members_overview() {
  264. return true;
  265. }
  266. void TextEditor::update_settings() {
  267. code_editor->update_editor_settings();
  268. }
  269. void TextEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  270. code_editor->get_text_edit()->set_tooltip_request_func(p_obj, p_method, this);
  271. }
  272. Control *TextEditor::get_edit_menu() {
  273. return edit_hb;
  274. }
  275. void TextEditor::clear_edit_menu() {
  276. memdelete(edit_hb);
  277. }
  278. void TextEditor::_notification(int p_what) {
  279. switch (p_what) {
  280. case NOTIFICATION_READY:
  281. _load_theme_settings();
  282. break;
  283. }
  284. }
  285. void TextEditor::_edit_option(int p_op) {
  286. TextEdit *tx = code_editor->get_text_edit();
  287. switch (p_op) {
  288. case EDIT_UNDO: {
  289. tx->undo();
  290. tx->call_deferred("grab_focus");
  291. } break;
  292. case EDIT_REDO: {
  293. tx->redo();
  294. tx->call_deferred("grab_focus");
  295. } break;
  296. case EDIT_CUT: {
  297. tx->cut();
  298. tx->call_deferred("grab_focus");
  299. } break;
  300. case EDIT_COPY: {
  301. tx->copy();
  302. tx->call_deferred("grab_focus");
  303. } break;
  304. case EDIT_PASTE: {
  305. tx->paste();
  306. tx->call_deferred("grab_focus");
  307. } break;
  308. case EDIT_SELECT_ALL: {
  309. tx->select_all();
  310. tx->call_deferred("grab_focus");
  311. } break;
  312. case EDIT_MOVE_LINE_UP: {
  313. code_editor->move_lines_up();
  314. } break;
  315. case EDIT_MOVE_LINE_DOWN: {
  316. code_editor->move_lines_down();
  317. } break;
  318. case EDIT_INDENT_LEFT: {
  319. tx->indent_left();
  320. } break;
  321. case EDIT_INDENT_RIGHT: {
  322. tx->indent_right();
  323. } break;
  324. case EDIT_DELETE_LINE: {
  325. code_editor->delete_lines();
  326. } break;
  327. case EDIT_CLONE_DOWN: {
  328. code_editor->clone_lines_down();
  329. } break;
  330. case EDIT_TOGGLE_FOLD_LINE: {
  331. tx->toggle_fold_line(tx->cursor_get_line());
  332. tx->update();
  333. } break;
  334. case EDIT_FOLD_ALL_LINES: {
  335. tx->fold_all_lines();
  336. tx->update();
  337. } break;
  338. case EDIT_UNFOLD_ALL_LINES: {
  339. tx->unhide_all_lines();
  340. tx->update();
  341. } break;
  342. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  343. trim_trailing_whitespace();
  344. } break;
  345. case EDIT_CONVERT_INDENT_TO_SPACES: {
  346. convert_indent_to_spaces();
  347. } break;
  348. case EDIT_CONVERT_INDENT_TO_TABS: {
  349. convert_indent_to_tabs();
  350. } break;
  351. case EDIT_TO_UPPERCASE: {
  352. _convert_case(CodeTextEditor::UPPER);
  353. } break;
  354. case EDIT_TO_LOWERCASE: {
  355. _convert_case(CodeTextEditor::LOWER);
  356. } break;
  357. case EDIT_CAPITALIZE: {
  358. _convert_case(CodeTextEditor::CAPITALIZE);
  359. } break;
  360. case SEARCH_FIND: {
  361. code_editor->get_find_replace_bar()->popup_search();
  362. } break;
  363. case SEARCH_FIND_NEXT: {
  364. code_editor->get_find_replace_bar()->search_next();
  365. } break;
  366. case SEARCH_FIND_PREV: {
  367. code_editor->get_find_replace_bar()->search_prev();
  368. } break;
  369. case SEARCH_REPLACE: {
  370. code_editor->get_find_replace_bar()->popup_replace();
  371. } break;
  372. case SEARCH_GOTO_LINE: {
  373. goto_line_dialog->popup_find_line(tx);
  374. } break;
  375. case BOOKMARK_TOGGLE: {
  376. code_editor->toggle_bookmark();
  377. } break;
  378. case BOOKMARK_GOTO_NEXT: {
  379. code_editor->goto_next_bookmark();
  380. } break;
  381. case BOOKMARK_GOTO_PREV: {
  382. code_editor->goto_prev_bookmark();
  383. } break;
  384. case BOOKMARK_REMOVE_ALL: {
  385. code_editor->remove_all_bookmarks();
  386. } break;
  387. }
  388. }
  389. void TextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
  390. code_editor->convert_case(p_case);
  391. }
  392. void TextEditor::_bind_methods() {
  393. ClassDB::bind_method("_validate_script", &TextEditor::_validate_script);
  394. ClassDB::bind_method("_update_bookmark_list", &TextEditor::_update_bookmark_list);
  395. ClassDB::bind_method("_bookmark_item_pressed", &TextEditor::_bookmark_item_pressed);
  396. ClassDB::bind_method("_load_theme_settings", &TextEditor::_load_theme_settings);
  397. ClassDB::bind_method("_edit_option", &TextEditor::_edit_option);
  398. ClassDB::bind_method("_change_syntax_highlighter", &TextEditor::_change_syntax_highlighter);
  399. ClassDB::bind_method("_text_edit_gui_input", &TextEditor::_text_edit_gui_input);
  400. }
  401. static ScriptEditorBase *create_editor(const RES &p_resource) {
  402. if (Object::cast_to<TextFile>(*p_resource)) {
  403. return memnew(TextEditor);
  404. }
  405. return NULL;
  406. }
  407. void TextEditor::register_editor() {
  408. ScriptEditor::register_create_script_editor_function(create_editor);
  409. }
  410. void TextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  411. Ref<InputEventMouseButton> mb = ev;
  412. if (mb.is_valid()) {
  413. if (mb->get_button_index() == BUTTON_RIGHT) {
  414. int col, row;
  415. TextEdit *tx = code_editor->get_text_edit();
  416. tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col);
  417. tx->set_right_click_moves_caret(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret"));
  418. bool can_fold = tx->can_fold(row);
  419. bool is_folded = tx->is_folded(row);
  420. if (tx->is_right_click_moving_caret()) {
  421. if (tx->is_selection_active()) {
  422. int from_line = tx->get_selection_from_line();
  423. int to_line = tx->get_selection_to_line();
  424. int from_column = tx->get_selection_from_column();
  425. int to_column = tx->get_selection_to_column();
  426. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  427. // Right click is outside the selected text
  428. tx->deselect();
  429. }
  430. }
  431. if (!tx->is_selection_active()) {
  432. tx->cursor_set_line(row, true, false);
  433. tx->cursor_set_column(col);
  434. }
  435. }
  436. if (!mb->is_pressed()) {
  437. _make_context_menu(tx->is_selection_active(), can_fold, is_folded);
  438. }
  439. }
  440. }
  441. }
  442. void TextEditor::_make_context_menu(bool p_selection, bool p_can_fold, bool p_is_folded) {
  443. context_menu->clear();
  444. if (p_selection) {
  445. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  446. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  447. }
  448. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  449. context_menu->add_separator();
  450. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  451. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  452. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  453. context_menu->add_separator();
  454. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  455. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  456. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  457. if (p_selection) {
  458. context_menu->add_separator();
  459. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
  460. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
  461. }
  462. if (p_can_fold || p_is_folded)
  463. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  464. context_menu->set_position(get_global_transform().xform(get_local_mouse_position()));
  465. context_menu->set_size(Vector2(1, 1));
  466. context_menu->popup();
  467. }
  468. TextEditor::TextEditor() {
  469. code_editor = memnew(CodeTextEditor);
  470. add_child(code_editor);
  471. code_editor->add_constant_override("separation", 0);
  472. code_editor->connect("load_theme_settings", this, "_load_theme_settings");
  473. code_editor->connect("validate_script", this, "_validate_script");
  474. code_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  475. code_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  476. update_settings();
  477. code_editor->get_text_edit()->set_context_menu_enabled(false);
  478. code_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input");
  479. context_menu = memnew(PopupMenu);
  480. add_child(context_menu);
  481. context_menu->connect("id_pressed", this, "_edit_option");
  482. edit_hb = memnew(HBoxContainer);
  483. search_menu = memnew(MenuButton);
  484. edit_hb->add_child(search_menu);
  485. search_menu->set_text(TTR("Search"));
  486. search_menu->set_switch_on_hover(true);
  487. search_menu->get_popup()->connect("id_pressed", this, "_edit_option");
  488. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  489. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  490. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  491. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  492. search_menu->get_popup()->add_separator();
  493. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  494. goto_line_dialog = memnew(GotoLineDialog);
  495. add_child(goto_line_dialog);
  496. edit_menu = memnew(MenuButton);
  497. edit_menu->set_text(TTR("Edit"));
  498. edit_menu->set_switch_on_hover(true);
  499. edit_menu->get_popup()->connect("id_pressed", this, "_edit_option");
  500. edit_hb->add_child(edit_menu);
  501. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  502. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  503. edit_menu->get_popup()->add_separator();
  504. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  505. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  506. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  507. edit_menu->get_popup()->add_separator();
  508. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  509. edit_menu->get_popup()->add_separator();
  510. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  511. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  512. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  513. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  514. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  515. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  516. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
  517. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
  518. edit_menu->get_popup()->add_separator();
  519. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
  520. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  521. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
  522. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
  523. edit_menu->get_popup()->add_separator();
  524. PopupMenu *convert_case = memnew(PopupMenu);
  525. convert_case->set_name("convert_case");
  526. edit_menu->get_popup()->add_child(convert_case);
  527. edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
  528. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase")), EDIT_TO_UPPERCASE);
  529. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase")), EDIT_TO_LOWERCASE);
  530. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE);
  531. convert_case->connect("id_pressed", this, "_edit_option");
  532. highlighters["Standard"] = NULL;
  533. highlighter_menu = memnew(PopupMenu);
  534. highlighter_menu->set_name("highlighter_menu");
  535. edit_menu->get_popup()->add_child(highlighter_menu);
  536. edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "highlighter_menu");
  537. highlighter_menu->add_radio_check_item(TTR("Standard"));
  538. highlighter_menu->connect("id_pressed", this, "_change_syntax_highlighter");
  539. bookmarks_menu = memnew(MenuButton);
  540. edit_hb->add_child(bookmarks_menu);
  541. bookmarks_menu->set_text(TTR("Bookmarks"));
  542. bookmarks_menu->set_switch_on_hover(true);
  543. _update_bookmark_list();
  544. bookmarks_menu->connect("about_to_show", this, "_update_bookmark_list");
  545. bookmarks_menu->get_popup()->connect("index_pressed", this, "_bookmark_item_pressed");
  546. code_editor->get_text_edit()->set_drag_forwarding(this);
  547. }