script_editor_plugin.h 18 KB

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