script_editor_plugin.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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.instantiate(); }
  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 void set_find_replace_bar(FindReplaceBar *p_bar) = 0;
  134. virtual Control *get_base_editor() const = 0;
  135. virtual void validate() = 0;
  136. ScriptEditorBase() {}
  137. };
  138. typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const RES &p_resource);
  139. class EditorScriptCodeCompletionCache;
  140. class FindInFilesDialog;
  141. class FindInFilesPanel;
  142. class ScriptEditor : public PanelContainer {
  143. GDCLASS(ScriptEditor, PanelContainer);
  144. EditorNode *editor;
  145. enum {
  146. FILE_NEW,
  147. FILE_NEW_TEXTFILE,
  148. FILE_OPEN,
  149. FILE_REOPEN_CLOSED,
  150. FILE_OPEN_RECENT,
  151. FILE_SAVE,
  152. FILE_SAVE_AS,
  153. FILE_SAVE_ALL,
  154. FILE_THEME,
  155. FILE_RUN,
  156. FILE_CLOSE,
  157. CLOSE_DOCS,
  158. CLOSE_ALL,
  159. CLOSE_OTHER_TABS,
  160. TOGGLE_SCRIPTS_PANEL,
  161. SHOW_IN_FILE_SYSTEM,
  162. FILE_COPY_PATH,
  163. FILE_TOOL_RELOAD,
  164. FILE_TOOL_RELOAD_SOFT,
  165. SEARCH_IN_FILES,
  166. REPLACE_IN_FILES,
  167. SEARCH_HELP,
  168. SEARCH_WEBSITE,
  169. HELP_SEARCH_FIND,
  170. HELP_SEARCH_FIND_NEXT,
  171. HELP_SEARCH_FIND_PREVIOUS,
  172. WINDOW_MOVE_UP,
  173. WINDOW_MOVE_DOWN,
  174. WINDOW_NEXT,
  175. WINDOW_PREV,
  176. WINDOW_SORT,
  177. WINDOW_SELECT_BASE = 100
  178. };
  179. enum {
  180. THEME_IMPORT,
  181. THEME_RELOAD,
  182. THEME_SAVE,
  183. THEME_SAVE_AS
  184. };
  185. enum ScriptSortBy {
  186. SORT_BY_NAME,
  187. SORT_BY_PATH,
  188. SORT_BY_NONE
  189. };
  190. enum ScriptListName {
  191. DISPLAY_NAME,
  192. DISPLAY_DIR_AND_NAME,
  193. DISPLAY_FULL_PATH,
  194. };
  195. HBoxContainer *menu_hb;
  196. MenuButton *file_menu;
  197. MenuButton *edit_menu;
  198. MenuButton *script_search_menu;
  199. MenuButton *debug_menu;
  200. PopupMenu *context_menu;
  201. Timer *autosave_timer;
  202. uint64_t idle;
  203. PopupMenu *recent_scripts;
  204. PopupMenu *theme_submenu;
  205. Button *help_search;
  206. Button *site_search;
  207. EditorHelpSearch *help_search_dialog;
  208. ItemList *script_list;
  209. HSplitContainer *script_split;
  210. ItemList *members_overview;
  211. LineEdit *filter_scripts;
  212. LineEdit *filter_methods;
  213. VBoxContainer *scripts_vbox;
  214. VBoxContainer *overview_vbox;
  215. HBoxContainer *buttons_hbox;
  216. Label *filename;
  217. Button *members_overview_alphabeta_sort_button;
  218. bool members_overview_enabled;
  219. ItemList *help_overview;
  220. bool help_overview_enabled;
  221. VSplitContainer *list_split;
  222. TabContainer *tab_container;
  223. EditorFileDialog *file_dialog;
  224. AcceptDialog *error_dialog;
  225. ConfirmationDialog *erase_tab_confirm;
  226. ScriptCreateDialog *script_create_dialog;
  227. Button *scripts_visible;
  228. FindReplaceBar *find_replace_bar;
  229. String current_theme;
  230. TextureRect *script_icon;
  231. Label *script_name_label;
  232. Button *script_back;
  233. Button *script_forward;
  234. FindInFilesDialog *find_in_files_dialog;
  235. FindInFilesPanel *find_in_files;
  236. Button *find_in_files_button;
  237. enum {
  238. SCRIPT_EDITOR_FUNC_MAX = 32,
  239. };
  240. static int script_editor_func_count;
  241. static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX];
  242. Vector<Ref<EditorSyntaxHighlighter>> syntax_highlighters;
  243. struct ScriptHistory {
  244. Control *control = nullptr;
  245. Variant state;
  246. };
  247. Vector<ScriptHistory> history;
  248. int history_pos;
  249. List<String> previous_scripts;
  250. void _tab_changed(int p_which);
  251. void _menu_option(int p_option);
  252. void _theme_option(int p_option);
  253. void _show_save_theme_as_dialog();
  254. Tree *disk_changed_list;
  255. ConfirmationDialog *disk_changed;
  256. bool restoring_layout;
  257. String _get_debug_tooltip(const String &p_text, Node *_se);
  258. void _resave_scripts(const String &p_str);
  259. void _reload_scripts();
  260. bool _test_script_times_on_disk(RES p_for_script = Ref<Resource>());
  261. void _add_recent_script(String p_path);
  262. void _update_recent_scripts();
  263. void _open_recent_script(int p_idx);
  264. void _show_error_dialog(String p_path);
  265. void _close_tab(int p_idx, bool p_save = true, bool p_history_back = true);
  266. void _update_find_replace_bar();
  267. void _close_current_tab(bool p_save = true);
  268. void _close_discard_current_tab(const String &p_str);
  269. void _close_docs_tab();
  270. void _close_other_tabs();
  271. void _close_all_tabs();
  272. void _copy_script_path();
  273. void _ask_close_current_unsaved_tab(ScriptEditorBase *current);
  274. bool grab_focus_block;
  275. bool pending_auto_reload;
  276. bool auto_reload_running_scripts;
  277. void _trigger_live_script_reload();
  278. void _live_auto_reload_running_scripts();
  279. void _update_selected_editor_menu();
  280. EditorScriptCodeCompletionCache *completion_cache;
  281. void _editor_stop();
  282. int edit_pass;
  283. void _add_callback(Object *p_obj, const String &p_function, const PackedStringArray &p_args);
  284. void _res_saved_callback(const Ref<Resource> &p_res);
  285. bool trim_trailing_whitespace_on_save;
  286. bool use_space_indentation;
  287. bool convert_indent_on_save;
  288. void _trim_trailing_whitespace(TextEdit *tx);
  289. void _goto_script_line2(int p_line);
  290. void _goto_script_line(REF p_script, int p_line);
  291. void _set_execution(REF p_script, int p_line);
  292. void _clear_execution(REF p_script);
  293. void _breaked(bool p_breaked, bool p_can_debug);
  294. void _update_window_menu();
  295. void _script_created(Ref<Script> p_script);
  296. ScriptEditorBase *_get_current_editor() const;
  297. Array _get_open_script_editors() const;
  298. void _save_layout();
  299. void _editor_settings_changed();
  300. void _filesystem_changed();
  301. void _file_removed(const String &p_file);
  302. void _autosave_scripts();
  303. void _update_autosave_timer();
  304. void _update_members_overview_visibility();
  305. void _update_members_overview();
  306. void _toggle_members_overview_alpha_sort(bool p_alphabetic_sort);
  307. void _filter_scripts_text_changed(const String &p_newtext);
  308. void _filter_methods_text_changed(const String &p_newtext);
  309. void _update_script_names();
  310. void _update_script_connections();
  311. bool _sort_list_on_update;
  312. void _members_overview_selected(int p_idx);
  313. void _script_selected(int p_idx);
  314. void _update_help_overview_visibility();
  315. void _update_help_overview();
  316. void _help_overview_selected(int p_idx);
  317. void _find_scripts(Node *p_base, Node *p_current, Set<Ref<Script>> &used);
  318. void _tree_changed();
  319. void _script_split_dragged(float);
  320. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  321. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  322. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  323. void _unhandled_key_input(const Ref<InputEvent> &p_event);
  324. void _script_list_gui_input(const Ref<InputEvent> &ev);
  325. void _make_script_list_context_menu();
  326. void _help_search(String p_text);
  327. void _help_index(String p_text);
  328. void _history_forward();
  329. void _history_back();
  330. bool waiting_update_names;
  331. void _help_class_open(const String &p_class);
  332. void _help_class_goto(const String &p_desc);
  333. void _update_history_arrows();
  334. void _save_history();
  335. void _go_to_tab(int p_idx);
  336. void _update_history_pos(int p_new_pos);
  337. void _update_script_colors();
  338. void _update_modified_scripts_for_external_editor(Ref<Script> p_for_script = Ref<Script>());
  339. void _script_changed();
  340. int file_dialog_option;
  341. void _file_dialog_action(String p_file);
  342. Ref<Script> _get_current_script();
  343. Array _get_open_scripts() const;
  344. Ref<TextFile> _load_text_file(const String &p_path, Error *r_error);
  345. Error _save_text_file(Ref<TextFile> p_text_file, const String &p_path);
  346. void _on_find_in_files_requested(String text);
  347. void _on_replace_in_files_requested(String text);
  348. void _on_find_in_files_result_selected(String fpath, int line_number, int begin, int end);
  349. void _start_find_in_files(bool with_replace);
  350. void _on_find_in_files_modified_files(PackedStringArray paths);
  351. static void _open_script_request(const String &p_path);
  352. static ScriptEditor *script_editor;
  353. protected:
  354. void _notification(int p_what);
  355. static void _bind_methods();
  356. public:
  357. static ScriptEditor *get_singleton() { return script_editor; }
  358. bool toggle_scripts_panel();
  359. bool is_scripts_panel_toggled();
  360. void apply_scripts() const;
  361. void open_script_create_dialog(const String &p_base_name, const String &p_base_path);
  362. void ensure_select_current();
  363. _FORCE_INLINE_ bool edit(const RES &p_resource, bool p_grab_focus = true) { return edit(p_resource, -1, 0, p_grab_focus); }
  364. bool edit(const RES &p_resource, int p_line, int p_col, bool p_grab_focus = true);
  365. void get_breakpoints(List<String> *p_breakpoints);
  366. void save_current_script();
  367. void save_all_scripts();
  368. void set_window_layout(Ref<ConfigFile> p_layout);
  369. void get_window_layout(Ref<ConfigFile> p_layout);
  370. void set_scene_root_script(Ref<Script> p_script);
  371. Vector<Ref<Script>> get_open_scripts() const;
  372. bool script_goto_method(Ref<Script> p_script, const String &p_method);
  373. virtual void edited_scene_changed();
  374. void notify_script_close(const Ref<Script> &p_script);
  375. void notify_script_changed(const Ref<Script> &p_script);
  376. void close_builtin_scripts_from_scene(const String &p_scene);
  377. void goto_help(const String &p_desc) { _help_class_goto(p_desc); }
  378. void update_doc(const String &p_name);
  379. bool can_take_away_focus() const;
  380. VSplitContainer *get_left_list_split() { return list_split; }
  381. void set_live_auto_reload_running_scripts(bool p_enabled);
  382. void register_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter);
  383. void unregister_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter);
  384. static void register_create_script_editor_function(CreateScriptEditorFunc p_func);
  385. ScriptEditor(EditorNode *p_editor);
  386. ~ScriptEditor();
  387. };
  388. class ScriptEditorPlugin : public EditorPlugin {
  389. GDCLASS(ScriptEditorPlugin, EditorPlugin);
  390. ScriptEditor *script_editor;
  391. EditorNode *editor;
  392. public:
  393. virtual String get_name() const override { return "Script"; }
  394. bool has_main_screen() const override { return true; }
  395. virtual void edit(Object *p_object) override;
  396. virtual bool handles(Object *p_object) const override;
  397. virtual void make_visible(bool p_visible) override;
  398. virtual void selected_notify() override;
  399. virtual void save_external_data() override;
  400. virtual void apply_changes() override;
  401. virtual void restore_global_state() override;
  402. virtual void save_global_state() override;
  403. virtual void set_window_layout(Ref<ConfigFile> p_layout) override;
  404. virtual void get_window_layout(Ref<ConfigFile> p_layout) override;
  405. virtual void get_breakpoints(List<String> *p_breakpoints) override;
  406. virtual void edited_scene_changed() override;
  407. ScriptEditorPlugin(EditorNode *p_node);
  408. ~ScriptEditorPlugin();
  409. };
  410. #endif // SCRIPT_EDITOR_PLUGIN_H