code_editor.h 9.3 KB

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