code_editor.h 7.7 KB

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