script_editor_plugin.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /**************************************************************************/
  2. /* script_editor_plugin.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_EDITOR_PLUGIN_H
  31. #define SCRIPT_EDITOR_PLUGIN_H
  32. #include "editor/editor_plugin.h"
  33. #include "scene/gui/dialogs.h"
  34. #include "scene/gui/panel_container.h"
  35. #include "scene/resources/syntax_highlighter.h"
  36. #include "scene/resources/text_file.h"
  37. class EditorFileDialog;
  38. class EditorHelpSearch;
  39. class FindReplaceBar;
  40. class HSplitContainer;
  41. class ItemList;
  42. class MenuButton;
  43. class TabContainer;
  44. class TextureRect;
  45. class Tree;
  46. class VSplitContainer;
  47. class EditorSyntaxHighlighter : public SyntaxHighlighter {
  48. GDCLASS(EditorSyntaxHighlighter, SyntaxHighlighter)
  49. private:
  50. Ref<RefCounted> edited_resourse;
  51. protected:
  52. static void _bind_methods();
  53. GDVIRTUAL0RC(String, _get_name)
  54. GDVIRTUAL0RC(PackedStringArray, _get_supported_languages)
  55. public:
  56. virtual String _get_name() const;
  57. virtual PackedStringArray _get_supported_languages() const;
  58. void _set_edited_resource(const Ref<Resource> &p_res) { edited_resourse = p_res; }
  59. Ref<RefCounted> _get_edited_resource() { return edited_resourse; }
  60. virtual Ref<EditorSyntaxHighlighter> _create() const;
  61. };
  62. class EditorStandardSyntaxHighlighter : public EditorSyntaxHighlighter {
  63. GDCLASS(EditorStandardSyntaxHighlighter, EditorSyntaxHighlighter)
  64. private:
  65. Ref<CodeHighlighter> highlighter;
  66. public:
  67. virtual void _update_cache() override;
  68. virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
  69. virtual String _get_name() const override { return TTR("Standard"); }
  70. virtual Ref<EditorSyntaxHighlighter> _create() const override;
  71. EditorStandardSyntaxHighlighter() { highlighter.instantiate(); }
  72. };
  73. class EditorPlainTextSyntaxHighlighter : public EditorSyntaxHighlighter {
  74. GDCLASS(EditorPlainTextSyntaxHighlighter, EditorSyntaxHighlighter)
  75. public:
  76. virtual String _get_name() const override { return TTR("Plain Text"); }
  77. virtual Ref<EditorSyntaxHighlighter> _create() const override;
  78. };
  79. ///////////////////////////////////////////////////////////////////////////////
  80. class ScriptEditorQuickOpen : public ConfirmationDialog {
  81. GDCLASS(ScriptEditorQuickOpen, ConfirmationDialog);
  82. LineEdit *search_box = nullptr;
  83. Tree *search_options = nullptr;
  84. String function;
  85. void _update_search();
  86. void _sbox_input(const Ref<InputEvent> &p_ie);
  87. Vector<String> functions;
  88. void _confirmed();
  89. void _text_changed(const String &p_newtext);
  90. protected:
  91. void _notification(int p_what);
  92. static void _bind_methods();
  93. public:
  94. void popup_dialog(const Vector<String> &p_functions, bool p_dontclear = false);
  95. ScriptEditorQuickOpen();
  96. };
  97. class EditorDebuggerNode;
  98. class ScriptEditorBase : public VBoxContainer {
  99. GDCLASS(ScriptEditorBase, VBoxContainer);
  100. protected:
  101. static void _bind_methods();
  102. public:
  103. virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) = 0;
  104. virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) = 0;
  105. virtual void apply_code() = 0;
  106. virtual Ref<Resource> get_edited_resource() const = 0;
  107. virtual Vector<String> get_functions() = 0;
  108. virtual void set_edited_resource(const Ref<Resource> &p_res) = 0;
  109. virtual void enable_editor(Control *p_shortcut_context = nullptr) = 0;
  110. virtual void reload_text() = 0;
  111. virtual String get_name() = 0;
  112. virtual Ref<Texture2D> get_theme_icon() = 0;
  113. virtual bool is_unsaved() = 0;
  114. virtual Variant get_edit_state() = 0;
  115. virtual void set_edit_state(const Variant &p_state) = 0;
  116. virtual Variant get_navigation_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 PackedInt32Array 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_SOFT,
  169. SEARCH_IN_FILES,
  170. REPLACE_IN_FILES,
  171. SEARCH_HELP,
  172. SEARCH_WEBSITE,
  173. HELP_SEARCH_FIND,
  174. HELP_SEARCH_FIND_NEXT,
  175. HELP_SEARCH_FIND_PREVIOUS,
  176. WINDOW_MOVE_UP,
  177. WINDOW_MOVE_DOWN,
  178. WINDOW_NEXT,
  179. WINDOW_PREV,
  180. WINDOW_SORT,
  181. WINDOW_SELECT_BASE = 100
  182. };
  183. enum {
  184. THEME_IMPORT,
  185. THEME_RELOAD,
  186. THEME_SAVE,
  187. THEME_SAVE_AS
  188. };
  189. enum ScriptSortBy {
  190. SORT_BY_NAME,
  191. SORT_BY_PATH,
  192. SORT_BY_NONE
  193. };
  194. enum ScriptListName {
  195. DISPLAY_NAME,
  196. DISPLAY_DIR_AND_NAME,
  197. DISPLAY_FULL_PATH,
  198. };
  199. HBoxContainer *menu_hb = nullptr;
  200. MenuButton *file_menu = nullptr;
  201. MenuButton *edit_menu = nullptr;
  202. MenuButton *script_search_menu = nullptr;
  203. MenuButton *debug_menu = nullptr;
  204. PopupMenu *context_menu = nullptr;
  205. Timer *autosave_timer = nullptr;
  206. uint64_t idle = 0;
  207. PopupMenu *recent_scripts = nullptr;
  208. PopupMenu *theme_submenu = nullptr;
  209. Button *help_search = nullptr;
  210. Button *site_search = nullptr;
  211. EditorHelpSearch *help_search_dialog = nullptr;
  212. ItemList *script_list = nullptr;
  213. HSplitContainer *script_split = nullptr;
  214. ItemList *members_overview = nullptr;
  215. LineEdit *filter_scripts = nullptr;
  216. LineEdit *filter_methods = nullptr;
  217. VBoxContainer *scripts_vbox = nullptr;
  218. VBoxContainer *overview_vbox = nullptr;
  219. HBoxContainer *buttons_hbox = nullptr;
  220. Label *filename = nullptr;
  221. Button *members_overview_alphabeta_sort_button = nullptr;
  222. bool members_overview_enabled;
  223. ItemList *help_overview = nullptr;
  224. bool help_overview_enabled;
  225. VSplitContainer *list_split = nullptr;
  226. TabContainer *tab_container = nullptr;
  227. EditorFileDialog *file_dialog = nullptr;
  228. AcceptDialog *error_dialog = nullptr;
  229. ConfirmationDialog *erase_tab_confirm = nullptr;
  230. ScriptCreateDialog *script_create_dialog = nullptr;
  231. Button *scripts_visible = nullptr;
  232. FindReplaceBar *find_replace_bar = nullptr;
  233. String current_theme;
  234. TextureRect *script_icon = nullptr;
  235. Label *script_name_label = nullptr;
  236. Button *script_back = nullptr;
  237. Button *script_forward = nullptr;
  238. FindInFilesDialog *find_in_files_dialog = nullptr;
  239. FindInFilesPanel *find_in_files = nullptr;
  240. Button *find_in_files_button = nullptr;
  241. enum {
  242. SCRIPT_EDITOR_FUNC_MAX = 32,
  243. };
  244. static int script_editor_func_count;
  245. static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX];
  246. Vector<Ref<EditorSyntaxHighlighter>> syntax_highlighters;
  247. struct ScriptHistory {
  248. Control *control = nullptr;
  249. Variant state;
  250. };
  251. Vector<ScriptHistory> history;
  252. int history_pos;
  253. List<String> previous_scripts;
  254. List<int> script_close_queue;
  255. void _tab_changed(int p_which);
  256. void _menu_option(int p_option);
  257. void _theme_option(int p_option);
  258. void _show_save_theme_as_dialog();
  259. bool _has_docs_tab() const;
  260. bool _has_script_tab() const;
  261. void _prepare_file_menu();
  262. Tree *disk_changed_list = nullptr;
  263. ConfirmationDialog *disk_changed = nullptr;
  264. bool restoring_layout;
  265. String _get_debug_tooltip(const String &p_text, Node *_se);
  266. void _resave_scripts(const String &p_str);
  267. bool _test_script_times_on_disk(Ref<Resource> p_for_script = Ref<Resource>());
  268. void _add_recent_script(String p_path);
  269. void _update_recent_scripts();
  270. void _open_recent_script(int p_idx);
  271. void _show_error_dialog(String p_path);
  272. void _close_tab(int p_idx, bool p_save = true, bool p_history_back = true);
  273. void _update_find_replace_bar();
  274. void _close_current_tab(bool p_save = true);
  275. void _close_discard_current_tab(const String &p_str);
  276. void _close_docs_tab();
  277. void _close_other_tabs();
  278. void _close_all_tabs();
  279. void _queue_close_tabs();
  280. void _copy_script_path();
  281. void _ask_close_current_unsaved_tab(ScriptEditorBase *current);
  282. bool grab_focus_block;
  283. bool pending_auto_reload;
  284. bool auto_reload_running_scripts;
  285. void _trigger_live_script_reload();
  286. void _live_auto_reload_running_scripts();
  287. void _update_selected_editor_menu();
  288. EditorScriptCodeCompletionCache *completion_cache = nullptr;
  289. void _editor_stop();
  290. int edit_pass;
  291. void _add_callback(Object *p_obj, const String &p_function, const PackedStringArray &p_args);
  292. void _res_saved_callback(const Ref<Resource> &p_res);
  293. void _scene_saved_callback(const String &p_path);
  294. bool open_textfile_after_create = true;
  295. bool trim_trailing_whitespace_on_save;
  296. bool use_space_indentation;
  297. bool convert_indent_on_save;
  298. void _goto_script_line2(int p_line);
  299. void _goto_script_line(Ref<RefCounted> p_script, int p_line);
  300. void _set_execution(Ref<RefCounted> p_script, int p_line);
  301. void _clear_execution(Ref<RefCounted> p_script);
  302. void _breaked(bool p_breaked, bool p_can_debug);
  303. void _script_created(Ref<Script> p_script);
  304. void _set_breakpoint(Ref<RefCounted> p_scrpt, int p_line, bool p_enabled);
  305. void _clear_breakpoints();
  306. Array _get_cached_breakpoints_for_script(const String &p_path) const;
  307. ScriptEditorBase *_get_current_editor() const;
  308. TypedArray<ScriptEditorBase> _get_open_script_editors() const;
  309. Ref<ConfigFile> script_editor_cache;
  310. void _save_editor_state(ScriptEditorBase *p_editor);
  311. void _save_layout();
  312. void _editor_settings_changed();
  313. void _filesystem_changed();
  314. void _files_moved(const String &p_old_file, const String &p_new_file);
  315. void _file_removed(const String &p_file);
  316. void _autosave_scripts();
  317. void _update_autosave_timer();
  318. void _update_members_overview_visibility();
  319. void _update_members_overview();
  320. void _toggle_members_overview_alpha_sort(bool p_alphabetic_sort);
  321. void _filter_scripts_text_changed(const String &p_newtext);
  322. void _filter_methods_text_changed(const String &p_newtext);
  323. void _update_script_names();
  324. bool _sort_list_on_update;
  325. void _members_overview_selected(int p_idx);
  326. void _script_selected(int p_idx);
  327. void _update_help_overview_visibility();
  328. void _update_help_overview();
  329. void _help_overview_selected(int p_idx);
  330. void _find_scripts(Node *p_base, Node *p_current, HashSet<Ref<Script>> &used);
  331. void _tree_changed();
  332. void _split_dragged(float);
  333. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  334. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  335. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  336. virtual void input(const Ref<InputEvent> &p_event) override;
  337. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  338. void _script_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index);
  339. void _make_script_list_context_menu();
  340. void _help_search(String p_text);
  341. void _history_forward();
  342. void _history_back();
  343. bool waiting_update_names;
  344. void _help_class_open(const String &p_class);
  345. void _help_class_goto(const String &p_desc);
  346. bool _help_tab_goto(const String &p_name, const String &p_desc);
  347. void _update_history_arrows();
  348. void _save_history();
  349. void _go_to_tab(int p_idx);
  350. void _update_history_pos(int p_new_pos);
  351. void _update_script_colors();
  352. void _update_modified_scripts_for_external_editor(Ref<Script> p_for_script = Ref<Script>());
  353. void _script_changed();
  354. int file_dialog_option;
  355. void _file_dialog_action(String p_file);
  356. Ref<Script> _get_current_script();
  357. TypedArray<Script> _get_open_scripts() const;
  358. HashSet<String> textfile_extensions;
  359. Ref<TextFile> _load_text_file(const String &p_path, Error *r_error) const;
  360. Error _save_text_file(Ref<TextFile> p_text_file, const String &p_path);
  361. void _on_find_in_files_requested(String text);
  362. void _on_replace_in_files_requested(String text);
  363. void _on_find_in_files_result_selected(String fpath, int line_number, int begin, int end);
  364. void _start_find_in_files(bool with_replace);
  365. void _on_find_in_files_modified_files(PackedStringArray paths);
  366. static void _open_script_request(const String &p_path);
  367. static ScriptEditor *script_editor;
  368. protected:
  369. void _notification(int p_what);
  370. static void _bind_methods();
  371. public:
  372. static ScriptEditor *get_singleton() { return script_editor; }
  373. bool toggle_scripts_panel();
  374. bool is_scripts_panel_toggled();
  375. void apply_scripts() const;
  376. void reload_scripts(bool p_refresh_only = false);
  377. void open_script_create_dialog(const String &p_base_name, const String &p_base_path);
  378. void open_text_file_create_dialog(const String &p_base_path, const String &p_base_name = "");
  379. Ref<Resource> open_file(const String &p_file);
  380. void ensure_select_current();
  381. _FORCE_INLINE_ bool edit(const Ref<Resource> &p_resource, bool p_grab_focus = true) { return edit(p_resource, -1, 0, p_grab_focus); }
  382. bool edit(const Ref<Resource> &p_resource, int p_line, int p_col, bool p_grab_focus = true);
  383. void get_breakpoints(List<String> *p_breakpoints);
  384. void save_current_script();
  385. void save_all_scripts();
  386. void set_window_layout(Ref<ConfigFile> p_layout);
  387. void get_window_layout(Ref<ConfigFile> p_layout);
  388. void set_scene_root_script(Ref<Script> p_script);
  389. Vector<Ref<Script>> get_open_scripts() const;
  390. bool script_goto_method(Ref<Script> p_script, const String &p_method);
  391. virtual void edited_scene_changed();
  392. void notify_script_close(const Ref<Script> &p_script);
  393. void notify_script_changed(const Ref<Script> &p_script);
  394. void close_builtin_scripts_from_scene(const String &p_scene);
  395. void goto_help(const String &p_desc) { _help_class_goto(p_desc); }
  396. void update_doc(const String &p_name);
  397. void clear_docs_from_script(const Ref<Script> &p_script);
  398. void update_docs_from_script(const Ref<Script> &p_script);
  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 set_window_layout(Ref<ConfigFile> p_layout) override;
  421. virtual void get_window_layout(Ref<ConfigFile> p_layout) override;
  422. virtual void get_breakpoints(List<String> *p_breakpoints) override;
  423. virtual void edited_scene_changed() override;
  424. ScriptEditorPlugin();
  425. ~ScriptEditorPlugin();
  426. };
  427. #endif // SCRIPT_EDITOR_PLUGIN_H