script_text_editor.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**************************************************************************/
  2. /* script_text_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 SCRIPT_TEXT_EDITOR_H
  31. #define SCRIPT_TEXT_EDITOR_H
  32. #include "script_editor_plugin.h"
  33. #include "editor/code_editor.h"
  34. #include "scene/gui/color_picker.h"
  35. #include "scene/gui/dialogs.h"
  36. #include "scene/gui/tree.h"
  37. class RichTextLabel;
  38. class ConnectionInfoDialog : public AcceptDialog {
  39. GDCLASS(ConnectionInfoDialog, AcceptDialog);
  40. Label *method = nullptr;
  41. Tree *tree = nullptr;
  42. virtual void ok_pressed() override;
  43. public:
  44. void popup_connections(const String &p_method, const Vector<Node *> &p_nodes);
  45. ConnectionInfoDialog();
  46. };
  47. class ScriptTextEditor : public ScriptEditorBase {
  48. GDCLASS(ScriptTextEditor, ScriptEditorBase);
  49. CodeTextEditor *code_editor = nullptr;
  50. RichTextLabel *warnings_panel = nullptr;
  51. RichTextLabel *errors_panel = nullptr;
  52. Ref<Script> script;
  53. bool script_is_valid = false;
  54. bool editor_enabled = false;
  55. Vector<String> functions;
  56. List<ScriptLanguage::Warning> warnings;
  57. List<ScriptLanguage::ScriptError> errors;
  58. HashMap<String, List<ScriptLanguage::ScriptError>> depended_errors;
  59. HashSet<int> safe_lines;
  60. List<Connection> missing_connections;
  61. Vector<String> member_keywords;
  62. HBoxContainer *edit_hb = nullptr;
  63. MenuButton *edit_menu = nullptr;
  64. MenuButton *search_menu = nullptr;
  65. MenuButton *goto_menu = nullptr;
  66. PopupMenu *bookmarks_menu = nullptr;
  67. PopupMenu *breakpoints_menu = nullptr;
  68. PopupMenu *highlighter_menu = nullptr;
  69. PopupMenu *context_menu = nullptr;
  70. GotoLineDialog *goto_line_dialog = nullptr;
  71. ScriptEditorQuickOpen *quick_open = nullptr;
  72. ConnectionInfoDialog *connection_info_dialog = nullptr;
  73. int connection_gutter = -1;
  74. void _gutter_clicked(int p_line, int p_gutter);
  75. void _update_gutter_indexes();
  76. int line_number_gutter = -1;
  77. Color default_line_number_color = Color(1, 1, 1);
  78. Color safe_line_number_color = Color(1, 1, 1);
  79. Color marked_line_color = Color(1, 1, 1);
  80. Color folded_code_region_color = Color(1, 1, 1);
  81. int previous_line = 0;
  82. PopupPanel *color_panel = nullptr;
  83. ColorPicker *color_picker = nullptr;
  84. Vector2 color_position;
  85. String color_args;
  86. bool theme_loaded = false;
  87. enum {
  88. EDIT_UNDO,
  89. EDIT_REDO,
  90. EDIT_CUT,
  91. EDIT_COPY,
  92. EDIT_PASTE,
  93. EDIT_SELECT_ALL,
  94. EDIT_COMPLETE,
  95. EDIT_AUTO_INDENT,
  96. EDIT_TRIM_TRAILING_WHITESAPCE,
  97. EDIT_TRIM_FINAL_NEWLINES,
  98. EDIT_CONVERT_INDENT_TO_SPACES,
  99. EDIT_CONVERT_INDENT_TO_TABS,
  100. EDIT_TOGGLE_COMMENT,
  101. EDIT_MOVE_LINE_UP,
  102. EDIT_MOVE_LINE_DOWN,
  103. EDIT_INDENT,
  104. EDIT_UNINDENT,
  105. EDIT_DELETE_LINE,
  106. EDIT_DUPLICATE_SELECTION,
  107. EDIT_DUPLICATE_LINES,
  108. EDIT_PICK_COLOR,
  109. EDIT_TO_UPPERCASE,
  110. EDIT_TO_LOWERCASE,
  111. EDIT_CAPITALIZE,
  112. EDIT_EVALUATE,
  113. EDIT_TOGGLE_WORD_WRAP,
  114. EDIT_TOGGLE_FOLD_LINE,
  115. EDIT_FOLD_ALL_LINES,
  116. EDIT_CREATE_CODE_REGION,
  117. EDIT_UNFOLD_ALL_LINES,
  118. SEARCH_FIND,
  119. SEARCH_FIND_NEXT,
  120. SEARCH_FIND_PREV,
  121. SEARCH_REPLACE,
  122. SEARCH_LOCATE_FUNCTION,
  123. SEARCH_GOTO_LINE,
  124. SEARCH_IN_FILES,
  125. REPLACE_IN_FILES,
  126. BOOKMARK_TOGGLE,
  127. BOOKMARK_GOTO_NEXT,
  128. BOOKMARK_GOTO_PREV,
  129. BOOKMARK_REMOVE_ALL,
  130. DEBUG_TOGGLE_BREAKPOINT,
  131. DEBUG_REMOVE_ALL_BREAKPOINTS,
  132. DEBUG_GOTO_NEXT_BREAKPOINT,
  133. DEBUG_GOTO_PREV_BREAKPOINT,
  134. HELP_CONTEXTUAL,
  135. LOOKUP_SYMBOL,
  136. };
  137. void _enable_code_editor();
  138. protected:
  139. void _update_breakpoint_list();
  140. void _breakpoint_item_pressed(int p_idx);
  141. void _breakpoint_toggled(int p_row);
  142. void _on_caret_moved();
  143. void _validate_script(); // No longer virtual.
  144. void _update_warnings();
  145. void _update_errors();
  146. void _update_bookmark_list();
  147. void _bookmark_item_pressed(int p_idx);
  148. static void _code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force);
  149. void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force);
  150. void _load_theme_settings();
  151. void _set_theme_for_script();
  152. void _show_errors_panel(bool p_show);
  153. void _show_warnings_panel(bool p_show);
  154. void _error_clicked(const Variant &p_line);
  155. void _warning_clicked(const Variant &p_line);
  156. void _notification(int p_what);
  157. HashMap<String, Ref<EditorSyntaxHighlighter>> highlighters;
  158. void _change_syntax_highlighter(int p_idx);
  159. void _edit_option(int p_op);
  160. void _edit_option_toggle_inline_comment();
  161. void _make_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_goto_definition, Vector2 p_pos);
  162. void _text_edit_gui_input(const Ref<InputEvent> &ev);
  163. void _color_changed(const Color &p_color);
  164. void _prepare_edit_menu();
  165. void _goto_line(int p_line) { goto_line(p_line); }
  166. void _lookup_symbol(const String &p_symbol, int p_row, int p_column);
  167. void _validate_symbol(const String &p_symbol);
  168. void _convert_case(CodeTextEditor::CaseStyle p_case);
  169. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  170. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  171. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  172. String _get_absolute_path(const String &rel_path);
  173. public:
  174. void _update_connected_methods();
  175. virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
  176. virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
  177. void update_toggle_scripts_button() override;
  178. virtual void apply_code() override;
  179. virtual Ref<Resource> get_edited_resource() const override;
  180. virtual void set_edited_resource(const Ref<Resource> &p_res) override;
  181. virtual void enable_editor(Control *p_shortcut_context = nullptr) override;
  182. virtual Vector<String> get_functions() override;
  183. virtual void reload_text() override;
  184. virtual String get_name() override;
  185. virtual Ref<Texture2D> get_theme_icon() override;
  186. virtual bool is_unsaved() override;
  187. virtual Variant get_edit_state() override;
  188. virtual void set_edit_state(const Variant &p_state) override;
  189. virtual Variant get_navigation_state() override;
  190. virtual void ensure_focus() override;
  191. virtual void trim_trailing_whitespace() override;
  192. virtual void trim_final_newlines() override;
  193. virtual void insert_final_newline() override;
  194. virtual void convert_indent() override;
  195. virtual void tag_saved_version() override;
  196. virtual void goto_line(int p_line, int p_column = 0) override;
  197. void goto_line_selection(int p_line, int p_begin, int p_end);
  198. void goto_line_centered(int p_line, int p_column = 0);
  199. virtual void set_executing_line(int p_line) override;
  200. virtual void clear_executing_line() override;
  201. virtual void reload(bool p_soft) override;
  202. virtual PackedInt32Array get_breakpoints() override;
  203. virtual void set_breakpoint(int p_line, bool p_enabled) override;
  204. virtual void clear_breakpoints() override;
  205. virtual void add_callback(const String &p_function, const PackedStringArray &p_args) override;
  206. virtual void update_settings() override;
  207. virtual bool show_members_overview() override;
  208. virtual void set_tooltip_request_func(const Callable &p_toolip_callback) override;
  209. virtual void set_debugger_active(bool p_active) override;
  210. Control *get_edit_menu() override;
  211. virtual void clear_edit_menu() override;
  212. virtual void set_find_replace_bar(FindReplaceBar *p_bar) override;
  213. static void register_editor();
  214. virtual Control *get_base_editor() const override;
  215. virtual CodeTextEditor *get_code_editor() const override;
  216. virtual void validate() override;
  217. Variant get_previous_state();
  218. void store_previous_state();
  219. ScriptTextEditor();
  220. ~ScriptTextEditor();
  221. };
  222. #endif // SCRIPT_TEXT_EDITOR_H