code_editor.h 9.4 KB

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