script_text_editor.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*************************************************************************/
  2. /* script_text_editor.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "scene/gui/color_picker.h"
  33. #include "scene/gui/dialogs.h"
  34. #include "scene/gui/tree.h"
  35. #include "script_editor_plugin.h"
  36. class ConnectionInfoDialog : public AcceptDialog {
  37. GDCLASS(ConnectionInfoDialog, AcceptDialog);
  38. Label *method = nullptr;
  39. Tree *tree = nullptr;
  40. virtual void ok_pressed() override;
  41. public:
  42. void popup_connections(String p_method, Vector<Node *> p_nodes);
  43. ConnectionInfoDialog();
  44. };
  45. class ScriptTextEditor : public ScriptEditorBase {
  46. GDCLASS(ScriptTextEditor, ScriptEditorBase);
  47. CodeTextEditor *code_editor = nullptr;
  48. RichTextLabel *warnings_panel = nullptr;
  49. RichTextLabel *errors_panel = nullptr;
  50. Ref<Script> script;
  51. bool script_is_valid = false;
  52. bool editor_enabled = false;
  53. Vector<String> functions;
  54. List<ScriptLanguage::Warning> warnings;
  55. List<ScriptLanguage::ScriptError> errors;
  56. Set<int> safe_lines;
  57. List<Connection> missing_connections;
  58. Vector<String> member_keywords;
  59. HBoxContainer *edit_hb = nullptr;
  60. MenuButton *edit_menu = nullptr;
  61. MenuButton *search_menu = nullptr;
  62. MenuButton *goto_menu = nullptr;
  63. PopupMenu *bookmarks_menu = nullptr;
  64. PopupMenu *breakpoints_menu = nullptr;
  65. PopupMenu *highlighter_menu = nullptr;
  66. PopupMenu *context_menu = nullptr;
  67. PopupMenu *convert_case = nullptr;
  68. GotoLineDialog *goto_line_dialog = nullptr;
  69. ScriptEditorQuickOpen *quick_open = nullptr;
  70. ConnectionInfoDialog *connection_info_dialog = nullptr;
  71. int connection_gutter = -1;
  72. void _gutter_clicked(int p_line, int p_gutter);
  73. void _update_gutter_indexes();
  74. int line_number_gutter = -1;
  75. Color default_line_number_color = Color(1, 1, 1);
  76. Color safe_line_number_color = Color(1, 1, 1);
  77. Color marked_line_color = Color(1, 1, 1);
  78. PopupPanel *color_panel = nullptr;
  79. ColorPicker *color_picker = nullptr;
  80. Vector2 color_position;
  81. String color_args;
  82. bool theme_loaded = false;
  83. enum {
  84. EDIT_UNDO,
  85. EDIT_REDO,
  86. EDIT_CUT,
  87. EDIT_COPY,
  88. EDIT_PASTE,
  89. EDIT_SELECT_ALL,
  90. EDIT_COMPLETE,
  91. EDIT_AUTO_INDENT,
  92. EDIT_TRIM_TRAILING_WHITESAPCE,
  93. EDIT_CONVERT_INDENT_TO_SPACES,
  94. EDIT_CONVERT_INDENT_TO_TABS,
  95. EDIT_TOGGLE_COMMENT,
  96. EDIT_MOVE_LINE_UP,
  97. EDIT_MOVE_LINE_DOWN,
  98. EDIT_INDENT_RIGHT,
  99. EDIT_INDENT_LEFT,
  100. EDIT_DELETE_LINE,
  101. EDIT_DUPLICATE_SELECTION,
  102. EDIT_PICK_COLOR,
  103. EDIT_TO_UPPERCASE,
  104. EDIT_TO_LOWERCASE,
  105. EDIT_CAPITALIZE,
  106. EDIT_EVALUATE,
  107. EDIT_TOGGLE_FOLD_LINE,
  108. EDIT_FOLD_ALL_LINES,
  109. EDIT_UNFOLD_ALL_LINES,
  110. SEARCH_FIND,
  111. SEARCH_FIND_NEXT,
  112. SEARCH_FIND_PREV,
  113. SEARCH_REPLACE,
  114. SEARCH_LOCATE_FUNCTION,
  115. SEARCH_GOTO_LINE,
  116. SEARCH_IN_FILES,
  117. REPLACE_IN_FILES,
  118. BOOKMARK_TOGGLE,
  119. BOOKMARK_GOTO_NEXT,
  120. BOOKMARK_GOTO_PREV,
  121. BOOKMARK_REMOVE_ALL,
  122. DEBUG_TOGGLE_BREAKPOINT,
  123. DEBUG_REMOVE_ALL_BREAKPOINTS,
  124. DEBUG_GOTO_NEXT_BREAKPOINT,
  125. DEBUG_GOTO_PREV_BREAKPOINT,
  126. HELP_CONTEXTUAL,
  127. LOOKUP_SYMBOL,
  128. };
  129. void _enable_code_editor();
  130. protected:
  131. void _update_breakpoint_list();
  132. void _breakpoint_item_pressed(int p_idx);
  133. void _breakpoint_toggled(int p_row);
  134. void _validate_script(); // No longer virtual.
  135. void _update_warnings();
  136. void _update_errors();
  137. void _update_bookmark_list();
  138. void _bookmark_item_pressed(int p_idx);
  139. static void _code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force);
  140. void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force);
  141. void _load_theme_settings();
  142. void _set_theme_for_script();
  143. void _show_errors_panel(bool p_show);
  144. void _show_warnings_panel(bool p_show);
  145. void _error_clicked(Variant p_line);
  146. void _warning_clicked(Variant p_line);
  147. void _notification(int p_what);
  148. static void _bind_methods();
  149. Map<String, Ref<EditorSyntaxHighlighter>> highlighters;
  150. void _change_syntax_highlighter(int p_idx);
  151. void _edit_option(int p_op);
  152. void _edit_option_toggle_inline_comment();
  153. void _make_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_goto_definition, Vector2 p_pos);
  154. void _text_edit_gui_input(const Ref<InputEvent> &ev);
  155. void _color_changed(const Color &p_color);
  156. void _prepare_edit_menu();
  157. void _goto_line(int p_line) { goto_line(p_line); }
  158. void _lookup_symbol(const String &p_symbol, int p_row, int p_column);
  159. void _validate_symbol(const String &p_symbol);
  160. void _convert_case(CodeTextEditor::CaseStyle p_case);
  161. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  162. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  163. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  164. String _get_absolute_path(const String &rel_path);
  165. public:
  166. void _update_connected_methods();
  167. virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
  168. virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
  169. void update_toggle_scripts_button() override;
  170. virtual void apply_code() override;
  171. virtual Ref<Resource> get_edited_resource() const override;
  172. virtual void set_edited_resource(const Ref<Resource> &p_res) override;
  173. virtual void enable_editor() override;
  174. virtual Vector<String> get_functions() override;
  175. virtual void reload_text() override;
  176. virtual String get_name() override;
  177. virtual Ref<Texture2D> get_theme_icon() override;
  178. virtual bool is_unsaved() override;
  179. virtual Variant get_edit_state() override;
  180. virtual void set_edit_state(const Variant &p_state) override;
  181. virtual void ensure_focus() override;
  182. virtual void trim_trailing_whitespace() override;
  183. virtual void insert_final_newline() override;
  184. virtual void convert_indent_to_spaces() override;
  185. virtual void convert_indent_to_tabs() override;
  186. virtual void tag_saved_version() override;
  187. virtual void goto_line(int p_line, bool p_with_error = false) override;
  188. void goto_line_selection(int p_line, int p_begin, int p_end);
  189. void goto_line_centered(int p_line);
  190. virtual void set_executing_line(int p_line) override;
  191. virtual void clear_executing_line() override;
  192. virtual void reload(bool p_soft) override;
  193. virtual Array get_breakpoints() override;
  194. virtual void set_breakpoint(int p_line, bool p_enabled) override;
  195. virtual void clear_breakpoints() override;
  196. virtual void add_callback(const String &p_function, PackedStringArray p_args) override;
  197. virtual void update_settings() override;
  198. virtual bool show_members_overview() override;
  199. virtual void set_tooltip_request_func(const Callable &p_toolip_callback) override;
  200. virtual void set_debugger_active(bool p_active) override;
  201. Control *get_edit_menu() override;
  202. virtual void clear_edit_menu() override;
  203. virtual void set_find_replace_bar(FindReplaceBar *p_bar) override;
  204. static void register_editor();
  205. virtual Control *get_base_editor() const override;
  206. virtual void validate() override;
  207. ScriptTextEditor();
  208. ~ScriptTextEditor();
  209. };
  210. const int KIND_COUNT = 10;
  211. // The order in which to sort code completion options.
  212. const ScriptLanguage::CodeCompletionKind KIND_SORT_ORDER[KIND_COUNT] = {
  213. ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE,
  214. ScriptLanguage::CODE_COMPLETION_KIND_MEMBER,
  215. ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION,
  216. ScriptLanguage::CODE_COMPLETION_KIND_ENUM,
  217. ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL,
  218. ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT,
  219. ScriptLanguage::CODE_COMPLETION_KIND_CLASS,
  220. ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH,
  221. ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH,
  222. ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT,
  223. };
  224. // The custom comparer which will sort completion options.
  225. struct CodeCompletionOptionCompare {
  226. _FORCE_INLINE_ bool operator()(const ScriptLanguage::CodeCompletionOption &l, const ScriptLanguage::CodeCompletionOption &r) const {
  227. if (l.location == r.location) {
  228. // If locations are same, sort on kind
  229. if (l.kind == r.kind) {
  230. // If kinds are same, sort alphanumeric
  231. return l.display < r.display;
  232. }
  233. // Sort kinds based on the const sorting array defined above. Lower index = higher priority.
  234. int l_index = -1;
  235. int r_index = -1;
  236. for (int i = 0; i < KIND_COUNT; i++) {
  237. const ScriptLanguage::CodeCompletionKind kind = KIND_SORT_ORDER[i];
  238. l_index = kind == l.kind ? i : l_index;
  239. r_index = kind == r.kind ? i : r_index;
  240. if (l_index != -1 && r_index != -1) {
  241. return l_index < r_index;
  242. }
  243. }
  244. // This return should never be hit unless something goes wrong.
  245. // l and r should always have a Kind which is in the sort order array.
  246. return l.display < r.display;
  247. }
  248. return l.location < r.location;
  249. }
  250. };
  251. #endif // SCRIPT_TEXT_EDITOR_H