code_editor.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "scene/gui/box_container.h"
  33. #include "scene/gui/button.h"
  34. #include "scene/gui/check_box.h"
  35. #include "scene/gui/code_edit.h"
  36. #include "scene/gui/dialogs.h"
  37. #include "scene/gui/label.h"
  38. #include "scene/gui/line_edit.h"
  39. #include "scene/main/timer.h"
  40. class GotoLineDialog : public ConfirmationDialog {
  41. GDCLASS(GotoLineDialog, ConfirmationDialog);
  42. Label *line_label = nullptr;
  43. LineEdit *line = nullptr;
  44. CodeEdit *text_editor = nullptr;
  45. virtual void ok_pressed() override;
  46. public:
  47. void popup_find_line(CodeEdit *p_edit);
  48. int get_line() const;
  49. GotoLineDialog();
  50. };
  51. class CodeTextEditor;
  52. class FindReplaceBar : public HBoxContainer {
  53. GDCLASS(FindReplaceBar, HBoxContainer);
  54. LineEdit *search_text = nullptr;
  55. Label *matches_label = nullptr;
  56. Button *find_prev = nullptr;
  57. Button *find_next = nullptr;
  58. CheckBox *case_sensitive = nullptr;
  59. CheckBox *whole_words = nullptr;
  60. TextureButton *hide_button = nullptr;
  61. LineEdit *replace_text = nullptr;
  62. Button *replace = nullptr;
  63. Button *replace_all = nullptr;
  64. CheckBox *selection_only = nullptr;
  65. VBoxContainer *vbc_lineedit = nullptr;
  66. HBoxContainer *hbc_button_replace = nullptr;
  67. HBoxContainer *hbc_option_replace = nullptr;
  68. CodeTextEditor *base_text_editor = nullptr;
  69. CodeEdit *text_editor = nullptr;
  70. uint32_t flags = 0;
  71. int result_line = 0;
  72. int result_col = 0;
  73. int results_count = -1;
  74. int results_count_to_current = -1;
  75. bool replace_all_mode = false;
  76. bool preserve_cursor = false;
  77. void _get_search_from(int &r_line, int &r_col, bool p_is_searching_next = false);
  78. void _update_results_count();
  79. void _update_matches_label();
  80. void _show_search(bool p_focus_replace = false, bool p_show_only = false);
  81. void _hide_bar(bool p_force_focus = false);
  82. void _editor_text_changed();
  83. void _search_options_changed(bool p_pressed);
  84. void _search_text_changed(const String &p_text);
  85. void _search_text_submitted(const String &p_text);
  86. void _replace_text_submitted(const String &p_text);
  87. protected:
  88. void _notification(int p_what);
  89. virtual void unhandled_input(const Ref<InputEvent> &p_event) override;
  90. void _focus_lost();
  91. void _update_flags(bool p_direction_backwards);
  92. bool _search(uint32_t p_flags, int p_from_line, int p_from_col);
  93. void _replace();
  94. void _replace_all();
  95. static void _bind_methods();
  96. public:
  97. String get_search_text() const;
  98. String get_replace_text() const;
  99. bool is_case_sensitive() const;
  100. bool is_whole_words() const;
  101. bool is_selection_only() const;
  102. void set_error(const String &p_label);
  103. void set_text_edit(CodeTextEditor *p_text_editor);
  104. void popup_search(bool p_show_only = false);
  105. void popup_replace();
  106. bool search_current();
  107. bool search_prev();
  108. bool search_next();
  109. bool needs_to_count_results = true;
  110. bool line_col_changed_for_result = false;
  111. FindReplaceBar();
  112. };
  113. typedef void (*CodeTextEditorCodeCompleteFunc)(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced);
  114. class CodeTextEditor : public VBoxContainer {
  115. GDCLASS(CodeTextEditor, VBoxContainer);
  116. CodeEdit *text_editor = nullptr;
  117. FindReplaceBar *find_replace_bar = nullptr;
  118. HBoxContainer *status_bar = nullptr;
  119. Button *toggle_scripts_button = nullptr;
  120. Button *error_button = nullptr;
  121. Button *warning_button = nullptr;
  122. Label *line_and_col_txt = nullptr;
  123. Label *info = nullptr;
  124. Timer *idle = nullptr;
  125. bool code_complete_enabled = true;
  126. Timer *code_complete_timer = nullptr;
  127. int code_complete_timer_line = 0;
  128. Timer *font_resize_timer = nullptr;
  129. int font_resize_val;
  130. real_t font_size;
  131. Label *error = nullptr;
  132. int error_line;
  133. int error_column;
  134. void _on_settings_change();
  135. void _apply_settings_change();
  136. void _update_text_editor_theme();
  137. void _complete_request();
  138. Ref<Texture2D> _get_completion_icon(const ScriptLanguage::CodeCompletionOption &p_option);
  139. void _font_resize_timeout();
  140. bool _add_font_size(int p_delta);
  141. virtual void input(const Ref<InputEvent> &event) override;
  142. void _text_editor_gui_input(const Ref<InputEvent> &p_event);
  143. void _zoom_in();
  144. void _zoom_out();
  145. void _zoom_changed();
  146. void _reset_zoom();
  147. Color completion_font_color;
  148. Color completion_string_color;
  149. Color completion_comment_color;
  150. Color completion_doc_comment_color;
  151. CodeTextEditorCodeCompleteFunc code_complete_func;
  152. void *code_complete_ud = nullptr;
  153. void _error_button_pressed();
  154. void _warning_button_pressed();
  155. void _set_show_errors_panel(bool p_show);
  156. void _set_show_warnings_panel(bool p_show);
  157. void _error_pressed(const Ref<InputEvent> &p_event);
  158. void _update_status_bar_theme();
  159. void _toggle_scripts_pressed();
  160. int _get_affected_lines_from(int p_caret);
  161. int _get_affected_lines_to(int p_caret);
  162. protected:
  163. virtual void _load_theme_settings() {}
  164. virtual void _validate_script() {}
  165. virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) {}
  166. void _text_changed_idle_timeout();
  167. void _code_complete_timer_timeout();
  168. void _text_changed();
  169. void _line_col_changed();
  170. void _notification(int);
  171. static void _bind_methods();
  172. bool is_warnings_panel_opened = false;
  173. bool is_errors_panel_opened = false;
  174. public:
  175. void trim_trailing_whitespace();
  176. void insert_final_newline();
  177. enum CaseStyle {
  178. UPPER,
  179. LOWER,
  180. CAPITALIZE,
  181. };
  182. void convert_case(CaseStyle p_case);
  183. void move_lines_up();
  184. void move_lines_down();
  185. void delete_lines();
  186. void duplicate_selection();
  187. /// Toggle inline comment on currently selected lines, or on current line if nothing is selected,
  188. /// by adding or removing comment delimiter
  189. void toggle_inline_comment(const String &delimiter);
  190. void goto_line(int p_line);
  191. void goto_line_selection(int p_line, int p_begin, int p_end);
  192. void goto_line_centered(int p_line);
  193. void set_executing_line(int p_line);
  194. void clear_executing_line();
  195. Variant get_edit_state();
  196. void set_edit_state(const Variant &p_state);
  197. Variant get_navigation_state();
  198. void set_error_count(int p_error_count);
  199. void set_warning_count(int p_warning_count);
  200. void update_editor_settings();
  201. void set_error(const String &p_error);
  202. void set_error_pos(int p_line, int p_column);
  203. Point2i get_error_pos() const;
  204. void update_line_and_column() { _line_col_changed(); }
  205. CodeEdit *get_text_editor() { return text_editor; }
  206. FindReplaceBar *get_find_replace_bar() { return find_replace_bar; }
  207. void set_find_replace_bar(FindReplaceBar *p_bar);
  208. void remove_find_replace_bar();
  209. virtual void apply_code() {}
  210. virtual void goto_error();
  211. void toggle_bookmark();
  212. void goto_next_bookmark();
  213. void goto_prev_bookmark();
  214. void remove_all_bookmarks();
  215. void set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud);
  216. void validate_script();
  217. void show_toggle_scripts_button();
  218. void update_toggle_scripts_button();
  219. CodeTextEditor();
  220. };
  221. #endif // CODE_EDITOR_H