script_editor_plugin.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*************************************************************************/
  2. /* script_editor_plugin.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_EDITOR_PLUGIN_H
  31. #define SCRIPT_EDITOR_PLUGIN_H
  32. #include "core/object/script_language.h"
  33. #include "editor/code_editor.h"
  34. #include "editor/editor_help.h"
  35. #include "editor/editor_help_search.h"
  36. #include "editor/editor_plugin.h"
  37. #include "editor/script_create_dialog.h"
  38. #include "scene/gui/item_list.h"
  39. #include "scene/gui/line_edit.h"
  40. #include "scene/gui/menu_button.h"
  41. #include "scene/gui/split_container.h"
  42. #include "scene/gui/tab_container.h"
  43. #include "scene/gui/text_edit.h"
  44. #include "scene/gui/tree.h"
  45. #include "scene/main/timer.h"
  46. #include "scene/resources/text_file.h"
  47. class EditorSyntaxHighlighter : public SyntaxHighlighter {
  48. GDCLASS(EditorSyntaxHighlighter, SyntaxHighlighter)
  49. private:
  50. REF edited_resourse;
  51. protected:
  52. static void _bind_methods();
  53. public:
  54. virtual String _get_name() const;
  55. virtual Array _get_supported_languages() const;
  56. void _set_edited_resource(const RES &p_res) { edited_resourse = p_res; }
  57. REF _get_edited_resource() { return edited_resourse; }
  58. virtual Ref<EditorSyntaxHighlighter> _create() const;
  59. };
  60. class EditorStandardSyntaxHighlighter : public EditorSyntaxHighlighter {
  61. GDCLASS(EditorStandardSyntaxHighlighter, EditorSyntaxHighlighter)
  62. private:
  63. Ref<CodeHighlighter> highlighter;
  64. public:
  65. virtual void _update_cache() override;
  66. virtual Dictionary _get_line_syntax_highlighting(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
  67. virtual String _get_name() const override { return TTR("Standard"); }
  68. virtual Ref<EditorSyntaxHighlighter> _create() const override;
  69. EditorStandardSyntaxHighlighter() { highlighter.instance(); }
  70. };
  71. class EditorPlainTextSyntaxHighlighter : public EditorSyntaxHighlighter {
  72. GDCLASS(EditorPlainTextSyntaxHighlighter, EditorSyntaxHighlighter)
  73. public:
  74. virtual String _get_name() const override { return TTR("Plain Text"); }
  75. virtual Ref<EditorSyntaxHighlighter> _create() const override;
  76. };
  77. ///////////////////////////////////////////////////////////////////////////////
  78. class ScriptEditorQuickOpen : public ConfirmationDialog {
  79. GDCLASS(ScriptEditorQuickOpen, ConfirmationDialog);
  80. LineEdit *search_box;
  81. Tree *search_options;
  82. String function;
  83. void _update_search();
  84. void _sbox_input(const Ref<InputEvent> &p_ie);
  85. Vector<String> functions;
  86. void _confirmed();
  87. void _text_changed(const String &p_newtext);
  88. protected:
  89. void _notification(int p_what);
  90. static void _bind_methods();
  91. public:
  92. void popup_dialog(const Vector<String> &p_functions, bool p_dontclear = false);
  93. ScriptEditorQuickOpen();
  94. };
  95. class EditorDebuggerNode;
  96. class ScriptEditorBase : public VBoxContainer {
  97. GDCLASS(ScriptEditorBase, VBoxContainer);
  98. protected:
  99. static void _bind_methods();
  100. public:
  101. virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) = 0;
  102. virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) = 0;
  103. virtual void apply_code() = 0;
  104. virtual RES get_edited_resource() const = 0;
  105. virtual Vector<String> get_functions() = 0;
  106. virtual void set_edited_resource(const RES &p_res) = 0;
  107. virtual void enable_editor() = 0;
  108. virtual void reload_text() = 0;
  109. virtual String get_name() = 0;
  110. virtual Ref<Texture2D> get_theme_icon() = 0;
  111. virtual bool is_unsaved() = 0;
  112. virtual Variant get_edit_state() = 0;
  113. virtual void set_edit_state(const Variant &p_state) = 0;
  114. virtual void goto_line(int p_line, bool p_with_error = false) = 0;
  115. virtual void set_executing_line(int p_line) = 0;
  116. virtual void clear_executing_line() = 0;
  117. virtual void trim_trailing_whitespace() = 0;
  118. virtual void insert_final_newline() = 0;
  119. virtual void convert_indent_to_spaces() = 0;
  120. virtual void convert_indent_to_tabs() = 0;
  121. virtual void ensure_focus() = 0;
  122. virtual void tag_saved_version() = 0;
  123. virtual void reload(bool p_soft) {}
  124. virtual Array get_breakpoints() = 0;
  125. virtual void add_callback(const String &p_function, PackedStringArray p_args) = 0;
  126. virtual void update_settings() = 0;
  127. virtual void set_debugger_active(bool p_active) = 0;
  128. virtual bool can_lose_focus_on_node_selection() { return true; }
  129. virtual bool show_members_overview() = 0;
  130. virtual void set_tooltip_request_func(String p_method, Object *p_obj) = 0;
  131. virtual Control *get_edit_menu() = 0;
  132. virtual void clear_edit_menu() = 0;
  133. virtual Control *get_base_editor() const = 0;
  134. virtual void validate() = 0;
  135. ScriptEditorBase() {}
  136. };
  137. typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const RES &p_resource);
  138. class EditorScriptCodeCompletionCache;
  139. class FindInFilesDialog;
  140. class FindInFilesPanel;
  141. class ScriptEditor : public PanelContainer {
  142. GDCLASS(ScriptEditor, PanelContainer);
  143. EditorNode *editor;
  144. enum {
  145. FILE_NEW,
  146. FILE_NEW_TEXTFILE,
  147. FILE_OPEN,
  148. FILE_REOPEN_CLOSED,
  149. FILE_OPEN_RECENT,
  150. FILE_SAVE,
  151. FILE_SAVE_AS,
  152. FILE_SAVE_ALL,
  153. FILE_THEME,
  154. FILE_RUN,
  155. FILE_CLOSE,
  156. CLOSE_DOCS,
  157. CLOSE_ALL,
  158. CLOSE_OTHER_TABS,
  159. TOGGLE_SCRIPTS_PANEL,
  160. SHOW_IN_FILE_SYSTEM,
  161. FILE_COPY_PATH,
  162. FILE_TOOL_RELOAD,
  163. FILE_TOOL_RELOAD_SOFT,
  164. SEARCH_IN_FILES,
  165. REPLACE_IN_FILES,
  166. SEARCH_HELP,
  167. SEARCH_WEBSITE,
  168. HELP_SEARCH_FIND,
  169. HELP_SEARCH_FIND_NEXT,
  170. HELP_SEARCH_FIND_PREVIOUS,
  171. WINDOW_MOVE_UP,
  172. WINDOW_MOVE_DOWN,
  173. WINDOW_NEXT,
  174. WINDOW_PREV,
  175. WINDOW_SORT,
  176. WINDOW_SELECT_BASE = 100
  177. };
  178. enum {
  179. THEME_IMPORT,
  180. THEME_RELOAD,
  181. THEME_SAVE,
  182. THEME_SAVE_AS
  183. };
  184. enum ScriptSortBy {
  185. SORT_BY_NAME,
  186. SORT_BY_PATH,
  187. SORT_BY_NONE
  188. };
  189. enum ScriptListName {
  190. DISPLAY_NAME,
  191. DISPLAY_DIR_AND_NAME,
  192. DISPLAY_FULL_PATH,
  193. };
  194. HBoxContainer *menu_hb;
  195. MenuButton *file_menu;
  196. MenuButton *edit_menu;
  197. MenuButton *script_search_menu;
  198. MenuButton *debug_menu;
  199. PopupMenu *context_menu;
  200. Timer *autosave_timer;
  201. uint64_t idle;
  202. PopupMenu *recent_scripts;
  203. PopupMenu *theme_submenu;
  204. Button *help_search;
  205. Button *site_search;
  206. EditorHelpSearch *help_search_dialog;
  207. ItemList *script_list;
  208. HSplitContainer *script_split;
  209. ItemList *members_overview;
  210. LineEdit *filter_scripts;
  211. LineEdit *filter_methods;
  212. VBoxContainer *scripts_vbox;
  213. VBoxContainer *overview_vbox;
  214. HBoxContainer *buttons_hbox;
  215. Label *filename;
  216. Button *members_overview_alphabeta_sort_button;
  217. bool members_overview_enabled;
  218. ItemList *help_overview;
  219. bool help_overview_enabled;
  220. VSplitContainer *list_split;
  221. TabContainer *tab_container;
  222. EditorFileDialog *file_dialog;
  223. AcceptDialog *error_dialog;
  224. ConfirmationDialog *erase_tab_confirm;
  225. ScriptCreateDialog *script_create_dialog;
  226. Button *scripts_visible;
  227. String current_theme;
  228. TextureRect *script_icon;
  229. Label *script_name_label;
  230. Button *script_back;
  231. Button *script_forward;
  232. FindInFilesDialog *find_in_files_dialog;
  233. FindInFilesPanel *find_in_files;
  234. Button *find_in_files_button;
  235. enum {
  236. SCRIPT_EDITOR_FUNC_MAX = 32,
  237. };
  238. static int script_editor_func_count;
  239. static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX];
  240. Vector<Ref<EditorSyntaxHighlighter>> syntax_highlighters;
  241. struct ScriptHistory {
  242. Control *control = nullptr;
  243. Variant state;
  244. };
  245. Vector<ScriptHistory> history;
  246. int history_pos;
  247. List<String> previous_scripts;
  248. void _tab_changed(int p_which);
  249. void _menu_option(int p_option);
  250. void _theme_option(int p_option);
  251. void _show_save_theme_as_dialog();
  252. Tree *disk_changed_list;
  253. ConfirmationDialog *disk_changed;
  254. bool restoring_layout;
  255. String _get_debug_tooltip(const String &p_text, Node *_se);
  256. void _resave_scripts(const String &p_str);
  257. void _reload_scripts();
  258. bool _test_script_times_on_disk(RES p_for_script = Ref<Resource>());
  259. void _add_recent_script(String p_path);
  260. void _update_recent_scripts();
  261. void _open_recent_script(int p_idx);
  262. void _show_error_dialog(String p_path);
  263. void _close_tab(int p_idx, bool p_save = true, bool p_history_back = true);
  264. void _close_current_tab(bool p_save = true);
  265. void _close_discard_current_tab(const String &p_str);
  266. void _close_docs_tab();
  267. void _close_other_tabs();
  268. void _close_all_tabs();
  269. void _copy_script_path();
  270. void _ask_close_current_unsaved_tab(ScriptEditorBase *current);
  271. bool grab_focus_block;
  272. bool pending_auto_reload;
  273. bool auto_reload_running_scripts;
  274. void _live_auto_reload_running_scripts();
  275. void _update_selected_editor_menu();
  276. EditorScriptCodeCompletionCache *completion_cache;
  277. void _editor_stop();
  278. int edit_pass;
  279. void _add_callback(Object *p_obj, const String &p_function, const PackedStringArray &p_args);
  280. void _res_saved_callback(const Ref<Resource> &p_res);
  281. bool trim_trailing_whitespace_on_save;
  282. bool use_space_indentation;
  283. bool convert_indent_on_save;
  284. void _trim_trailing_whitespace(TextEdit *tx);
  285. void _goto_script_line2(int p_line);
  286. void _goto_script_line(REF p_script, int p_line);
  287. void _set_execution(REF p_script, int p_line);
  288. void _clear_execution(REF p_script);
  289. void _breaked(bool p_breaked, bool p_can_debug);
  290. void _update_window_menu();
  291. void _script_created(Ref<Script> p_script);
  292. ScriptEditorBase *_get_current_editor() const;
  293. Array _get_open_script_editors() const;
  294. void _save_layout();
  295. void _editor_settings_changed();
  296. void _filesystem_changed();
  297. void _file_removed(const String &p_file);
  298. void _autosave_scripts();
  299. void _update_autosave_timer();
  300. void _update_members_overview_visibility();
  301. void _update_members_overview();
  302. void _toggle_members_overview_alpha_sort(bool p_alphabetic_sort);
  303. void _filter_scripts_text_changed(const String &p_newtext);
  304. void _filter_methods_text_changed(const String &p_newtext);
  305. void _update_script_names();
  306. void _update_script_connections();
  307. bool _sort_list_on_update;
  308. void _members_overview_selected(int p_idx);
  309. void _script_selected(int p_idx);
  310. void _update_help_overview_visibility();
  311. void _update_help_overview();
  312. void _help_overview_selected(int p_idx);
  313. void _find_scripts(Node *p_base, Node *p_current, Set<Ref<Script>> &used);
  314. void _tree_changed();
  315. void _script_split_dragged(float);
  316. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  317. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  318. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  319. void _unhandled_key_input(const Ref<InputEvent> &p_event);
  320. void _script_list_gui_input(const Ref<InputEvent> &ev);
  321. void _make_script_list_context_menu();
  322. void _help_search(String p_text);
  323. void _help_index(String p_text);
  324. void _history_forward();
  325. void _history_back();
  326. bool waiting_update_names;
  327. void _help_class_open(const String &p_class);
  328. void _help_class_goto(const String &p_desc);
  329. void _update_history_arrows();
  330. void _save_history();
  331. void _go_to_tab(int p_idx);
  332. void _update_history_pos(int p_new_pos);
  333. void _update_script_colors();
  334. void _update_modified_scripts_for_external_editor(Ref<Script> p_for_script = Ref<Script>());
  335. void _script_changed();
  336. int file_dialog_option;
  337. void _file_dialog_action(String p_file);
  338. Ref<Script> _get_current_script();
  339. Array _get_open_scripts() const;
  340. Ref<TextFile> _load_text_file(const String &p_path, Error *r_error);
  341. Error _save_text_file(Ref<TextFile> p_text_file, const String &p_path);
  342. void _on_find_in_files_requested(String text);
  343. void _on_replace_in_files_requested(String text);
  344. void _on_find_in_files_result_selected(String fpath, int line_number, int begin, int end);
  345. void _start_find_in_files(bool with_replace);
  346. void _on_find_in_files_modified_files(PackedStringArray paths);
  347. static void _open_script_request(const String &p_path);
  348. static ScriptEditor *script_editor;
  349. protected:
  350. void _notification(int p_what);
  351. static void _bind_methods();
  352. public:
  353. static ScriptEditor *get_singleton() { return script_editor; }
  354. bool toggle_scripts_panel();
  355. bool is_scripts_panel_toggled();
  356. void apply_scripts() const;
  357. void open_script_create_dialog(const String &p_base_name, const String &p_base_path);
  358. void ensure_select_current();
  359. _FORCE_INLINE_ bool edit(const RES &p_resource, bool p_grab_focus = true) { return edit(p_resource, -1, 0, p_grab_focus); }
  360. bool edit(const RES &p_resource, int p_line, int p_col, bool p_grab_focus = true);
  361. void get_breakpoints(List<String> *p_breakpoints);
  362. void save_current_script();
  363. void save_all_scripts();
  364. void set_window_layout(Ref<ConfigFile> p_layout);
  365. void get_window_layout(Ref<ConfigFile> p_layout);
  366. void set_scene_root_script(Ref<Script> p_script);
  367. Vector<Ref<Script>> get_open_scripts() const;
  368. bool script_goto_method(Ref<Script> p_script, const String &p_method);
  369. virtual void edited_scene_changed();
  370. void notify_script_close(const Ref<Script> &p_script);
  371. void notify_script_changed(const Ref<Script> &p_script);
  372. void close_builtin_scripts_from_scene(const String &p_scene);
  373. void goto_help(const String &p_desc) { _help_class_goto(p_desc); }
  374. void update_doc(const String &p_name);
  375. bool can_take_away_focus() const;
  376. VSplitContainer *get_left_list_split() { return list_split; }
  377. void set_live_auto_reload_running_scripts(bool p_enabled);
  378. void register_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter);
  379. void unregister_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter);
  380. static void register_create_script_editor_function(CreateScriptEditorFunc p_func);
  381. ScriptEditor(EditorNode *p_editor);
  382. ~ScriptEditor();
  383. };
  384. class ScriptEditorPlugin : public EditorPlugin {
  385. GDCLASS(ScriptEditorPlugin, EditorPlugin);
  386. ScriptEditor *script_editor;
  387. EditorNode *editor;
  388. public:
  389. virtual String get_name() const override { return "Script"; }
  390. bool has_main_screen() const override { return true; }
  391. virtual void edit(Object *p_object) override;
  392. virtual bool handles(Object *p_object) const override;
  393. virtual void make_visible(bool p_visible) override;
  394. virtual void selected_notify() override;
  395. virtual void save_external_data() override;
  396. virtual void apply_changes() override;
  397. virtual void restore_global_state() override;
  398. virtual void save_global_state() override;
  399. virtual void set_window_layout(Ref<ConfigFile> p_layout) override;
  400. virtual void get_window_layout(Ref<ConfigFile> p_layout) override;
  401. virtual void get_breakpoints(List<String> *p_breakpoints) override;
  402. virtual void edited_scene_changed() override;
  403. ScriptEditorPlugin(EditorNode *p_node);
  404. ~ScriptEditorPlugin();
  405. };
  406. #endif // SCRIPT_EDITOR_PLUGIN_H