text_editor.cpp 29 KB

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