code_editor.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*************************************************************************/
  2. /* code_editor.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifndef CODE_EDITOR_H
  31. #define CODE_EDITOR_H
  32. #include "editor/editor_plugin.h"
  33. #include "scene/gui/check_box.h"
  34. #include "scene/gui/check_button.h"
  35. #include "scene/gui/dialogs.h"
  36. #include "scene/gui/line_edit.h"
  37. #include "scene/gui/text_edit.h"
  38. #include "scene/gui/tool_button.h"
  39. #include "scene/main/timer.h"
  40. class GotoLineDialog : public ConfirmationDialog {
  41. GDCLASS(GotoLineDialog, ConfirmationDialog);
  42. Label *line_label;
  43. LineEdit *line;
  44. TextEdit *text_editor;
  45. virtual void ok_pressed();
  46. public:
  47. void popup_find_line(TextEdit *p_edit);
  48. int get_line() const;
  49. void set_text_editor(TextEdit *p_text_editor);
  50. GotoLineDialog();
  51. };
  52. class FindReplaceBar : public HBoxContainer {
  53. GDCLASS(FindReplaceBar, HBoxContainer);
  54. LineEdit *search_text;
  55. Label *matches_label;
  56. ToolButton *find_prev;
  57. ToolButton *find_next;
  58. CheckBox *case_sensitive;
  59. CheckBox *whole_words;
  60. TextureButton *hide_button;
  61. LineEdit *replace_text;
  62. Button *replace;
  63. Button *replace_all;
  64. CheckBox *selection_only;
  65. VBoxContainer *vbc_lineedit;
  66. HBoxContainer *hbc_button_replace;
  67. HBoxContainer *hbc_option_replace;
  68. TextEdit *text_edit;
  69. int result_line;
  70. int result_col;
  71. int results_count;
  72. bool replace_all_mode;
  73. bool preserve_cursor;
  74. void _get_search_from(int &r_line, int &r_col);
  75. void _update_results_count();
  76. void _update_matches_label();
  77. void _show_search(bool p_focus_replace = false, bool p_show_only = false);
  78. void _hide_bar();
  79. void _editor_text_changed();
  80. void _search_options_changed(bool p_pressed);
  81. void _search_text_changed(const String &p_text);
  82. void _search_text_entered(const String &p_text);
  83. void _replace_text_entered(const String &p_text);
  84. void _update_size();
  85. protected:
  86. void _notification(int p_what);
  87. void _unhandled_input(const Ref<InputEvent> &p_event);
  88. bool _search(uint32_t p_flags, int p_from_line, int p_from_col);
  89. void _replace();
  90. void _replace_all();
  91. static void _bind_methods();
  92. public:
  93. String get_search_text() const;
  94. String get_replace_text() const;
  95. bool is_case_sensitive() const;
  96. bool is_whole_words() const;
  97. bool is_selection_only() const;
  98. void set_error(const String &p_label);
  99. void set_text_edit(TextEdit *p_text_edit);
  100. void popup_search(bool p_show_only = false);
  101. void popup_replace();
  102. bool search_current();
  103. bool search_prev();
  104. bool search_next();
  105. FindReplaceBar();
  106. };
  107. typedef void (*CodeTextEditorCodeCompleteFunc)(void *p_ud, const String &p_code, List<ScriptCodeCompletionOption> *r_options, bool &r_forced);
  108. class CodeTextEditor : public VBoxContainer {
  109. GDCLASS(CodeTextEditor, VBoxContainer);
  110. TextEdit *text_editor;
  111. FindReplaceBar *find_replace_bar;
  112. HBoxContainer *status_bar;
  113. ToolButton *toggle_scripts_button;
  114. ToolButton *warning_button;
  115. Label *warning_count_label;
  116. Label *line_and_col_txt;
  117. Label *info;
  118. Timer *idle;
  119. Timer *code_complete_timer;
  120. Timer *font_resize_timer;
  121. int font_resize_val;
  122. real_t font_size;
  123. Label *error;
  124. int error_line;
  125. int error_column;
  126. void _on_settings_change();
  127. void _update_font();
  128. void _complete_request();
  129. Ref<Texture2D> _get_completion_icon(const ScriptCodeCompletionOption &p_option);
  130. void _font_resize_timeout();
  131. bool _add_font_size(int p_delta);
  132. void _input(const Ref<InputEvent> &event);
  133. void _text_editor_gui_input(const Ref<InputEvent> &p_event);
  134. void _zoom_in();
  135. void _zoom_out();
  136. void _zoom_changed();
  137. void _reset_zoom();
  138. CodeTextEditorCodeCompleteFunc code_complete_func;
  139. void *code_complete_ud;
  140. void _warning_label_gui_input(const Ref<InputEvent> &p_event);
  141. void _warning_button_pressed();
  142. void _set_show_warnings_panel(bool p_show);
  143. void _error_pressed(const Ref<InputEvent> &p_event);
  144. void _delete_line(int p_line);
  145. void _toggle_scripts_pressed();
  146. protected:
  147. virtual void _load_theme_settings() {}
  148. virtual void _validate_script() {}
  149. virtual void _code_complete_script(const String &p_code, List<ScriptCodeCompletionOption> *r_options) {}
  150. void _text_changed_idle_timeout();
  151. void _code_complete_timer_timeout();
  152. void _text_changed();
  153. void _line_col_changed();
  154. void _notification(int);
  155. static void _bind_methods();
  156. bool is_warnings_panel_opened;
  157. public:
  158. void trim_trailing_whitespace();
  159. void insert_final_newline();
  160. void convert_indent_to_spaces();
  161. void convert_indent_to_tabs();
  162. enum CaseStyle {
  163. UPPER,
  164. LOWER,
  165. CAPITALIZE,
  166. };
  167. void convert_case(CaseStyle p_case);
  168. void move_lines_up();
  169. void move_lines_down();
  170. void delete_lines();
  171. void clone_lines_down();
  172. /// Toggle inline comment on currently selected lines, or on current line if nothing is selected,
  173. /// by adding or removing comment delimiter
  174. void toggle_inline_comment(const String &delimiter);
  175. void goto_line(int p_line);
  176. void goto_line_selection(int p_line, int p_begin, int p_end);
  177. void goto_line_centered(int p_line);
  178. void set_executing_line(int p_line);
  179. void clear_executing_line();
  180. Variant get_edit_state();
  181. void set_edit_state(const Variant &p_state);
  182. void set_warning_nb(int p_warning_nb);
  183. void update_editor_settings();
  184. void set_error(const String &p_error);
  185. void set_error_pos(int p_line, int p_column);
  186. void update_line_and_column() { _line_col_changed(); }
  187. TextEdit *get_text_edit() { return text_editor; }
  188. FindReplaceBar *get_find_replace_bar() { return find_replace_bar; }
  189. virtual void apply_code() {}
  190. void goto_error();
  191. void toggle_bookmark();
  192. void goto_next_bookmark();
  193. void goto_prev_bookmark();
  194. void remove_all_bookmarks();
  195. void set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud);
  196. void validate_script();
  197. void show_toggle_scripts_button();
  198. void update_toggle_scripts_button();
  199. CodeTextEditor();
  200. };
  201. #endif // CODE_EDITOR_H