Path: blob/master/editor/script/script_editor_plugin.h
9903 views
/**************************************************************************/1/* script_editor_plugin.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/object/script_language.h"33#include "editor/plugins/editor_plugin.h"34#include "scene/gui/dialogs.h"35#include "scene/gui/panel_container.h"36#include "scene/resources/syntax_highlighter.h"37#include "scene/resources/text_file.h"3839class CodeTextEditor;40class EditorFileDialog;41class EditorHelpSearch;42class FindReplaceBar;43class HSplitContainer;44class ItemList;45class MenuButton;46class TabContainer;47class TextureRect;48class Tree;49class VSplitContainer;50class WindowWrapper;5152class EditorSyntaxHighlighter : public SyntaxHighlighter {53GDCLASS(EditorSyntaxHighlighter, SyntaxHighlighter)5455private:56Ref<RefCounted> edited_resource;5758protected:59static void _bind_methods();6061GDVIRTUAL0RC(String, _get_name)62GDVIRTUAL0RC(PackedStringArray, _get_supported_languages)63GDVIRTUAL0RC(Ref<EditorSyntaxHighlighter>, _create)6465public:66virtual String _get_name() const;67virtual PackedStringArray _get_supported_languages() const;6869void _set_edited_resource(const Ref<Resource> &p_res) { edited_resource = p_res; }70Ref<RefCounted> _get_edited_resource() { return edited_resource; }7172virtual Ref<EditorSyntaxHighlighter> _create() const;73};7475class EditorStandardSyntaxHighlighter : public EditorSyntaxHighlighter {76GDCLASS(EditorStandardSyntaxHighlighter, EditorSyntaxHighlighter)7778private:79Ref<CodeHighlighter> highlighter;80ScriptLanguage *script_language = nullptr; // See GH-89610.8182public:83virtual void _update_cache() override;84virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }8586virtual String _get_name() const override { return TTR("Standard"); }8788virtual Ref<EditorSyntaxHighlighter> _create() const override;8990void _set_script_language(ScriptLanguage *p_script_language) { script_language = p_script_language; }9192EditorStandardSyntaxHighlighter() { highlighter.instantiate(); }93};9495class EditorPlainTextSyntaxHighlighter : public EditorSyntaxHighlighter {96GDCLASS(EditorPlainTextSyntaxHighlighter, EditorSyntaxHighlighter)9798public:99virtual String _get_name() const override { return TTR("Plain Text"); }100101virtual Ref<EditorSyntaxHighlighter> _create() const override;102};103104class EditorJSONSyntaxHighlighter : public EditorSyntaxHighlighter {105GDCLASS(EditorJSONSyntaxHighlighter, EditorSyntaxHighlighter)106107private:108Ref<CodeHighlighter> highlighter;109110public:111virtual void _update_cache() override;112virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }113114virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "json" }; }115virtual String _get_name() const override { return TTR("JSON"); }116117virtual Ref<EditorSyntaxHighlighter> _create() const override;118119EditorJSONSyntaxHighlighter() { highlighter.instantiate(); }120};121122class EditorMarkdownSyntaxHighlighter : public EditorSyntaxHighlighter {123GDCLASS(EditorMarkdownSyntaxHighlighter, EditorSyntaxHighlighter)124125private:126Ref<CodeHighlighter> highlighter;127128public:129virtual void _update_cache() override;130virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }131132virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "md", "markdown" }; }133virtual String _get_name() const override { return TTR("Markdown"); }134135virtual Ref<EditorSyntaxHighlighter> _create() const override;136137EditorMarkdownSyntaxHighlighter() { highlighter.instantiate(); }138};139140class EditorConfigFileSyntaxHighlighter : public EditorSyntaxHighlighter {141GDCLASS(EditorConfigFileSyntaxHighlighter, EditorSyntaxHighlighter)142143private:144Ref<CodeHighlighter> highlighter;145146public:147virtual void _update_cache() override;148virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }149150// While not explicitly designed for those formats, this highlighter happens151// to handle TSCN, TRES, `project.godot` well. We can expose it in case the152// user opens one of these using the script editor (which can be done using153// the All Files filter).154virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "ini", "cfg", "tscn", "tres", "godot" }; }155virtual String _get_name() const override { return TTR("ConfigFile"); }156157virtual Ref<EditorSyntaxHighlighter> _create() const override;158159EditorConfigFileSyntaxHighlighter() { highlighter.instantiate(); }160};161162///////////////////////////////////////////////////////////////////////////////163164class ScriptEditorQuickOpen : public ConfirmationDialog {165GDCLASS(ScriptEditorQuickOpen, ConfirmationDialog);166167LineEdit *search_box = nullptr;168Tree *search_options = nullptr;169String function;170171void _update_search();172173void _sbox_input(const Ref<InputEvent> &p_event);174Vector<String> functions;175176void _confirmed();177void _text_changed(const String &p_newtext);178179protected:180void _notification(int p_what);181static void _bind_methods();182183public:184void popup_dialog(const Vector<String> &p_functions, bool p_dontclear = false);185ScriptEditorQuickOpen();186};187188class EditorDebuggerNode;189190class ScriptEditorBase : public VBoxContainer {191GDCLASS(ScriptEditorBase, VBoxContainer);192193protected:194static void _bind_methods();195196public:197struct EditedFileData {198String path;199uint64_t last_modified_time = -1;200} edited_file_data;201202virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) = 0;203virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) = 0;204205virtual void apply_code() = 0;206virtual Ref<Resource> get_edited_resource() const = 0;207virtual Vector<String> get_functions() = 0;208virtual void set_edited_resource(const Ref<Resource> &p_res) = 0;209virtual void enable_editor(Control *p_shortcut_context = nullptr) = 0;210virtual void reload_text() = 0;211virtual String get_name() = 0;212virtual Ref<Texture2D> get_theme_icon() = 0;213virtual bool is_unsaved() = 0;214virtual Variant get_edit_state() = 0;215virtual void set_edit_state(const Variant &p_state) = 0;216virtual Variant get_navigation_state() = 0;217virtual void goto_line(int p_line, int p_column = 0) = 0;218virtual void set_executing_line(int p_line) = 0;219virtual void clear_executing_line() = 0;220virtual void trim_trailing_whitespace() = 0;221virtual void trim_final_newlines() = 0;222virtual void insert_final_newline() = 0;223virtual void convert_indent() = 0;224virtual void ensure_focus() = 0;225virtual void tag_saved_version() = 0;226virtual void reload(bool p_soft) {}227virtual PackedInt32Array get_breakpoints() = 0;228virtual void set_breakpoint(int p_line, bool p_enabled) = 0;229virtual void clear_breakpoints() = 0;230virtual void add_callback(const String &p_function, const PackedStringArray &p_args) = 0;231virtual void update_settings() = 0;232virtual void set_debugger_active(bool p_active) = 0;233virtual void update_toggle_files_button() {}234235virtual bool show_members_overview() = 0;236237virtual void set_tooltip_request_func(const Callable &p_toolip_callback) = 0;238virtual Control *get_edit_menu() = 0;239virtual void clear_edit_menu() = 0;240virtual void set_find_replace_bar(FindReplaceBar *p_bar) = 0;241242virtual Control *get_base_editor() const = 0;243virtual CodeTextEditor *get_code_editor() const = 0;244245virtual void validate() = 0;246};247248typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const Ref<Resource> &p_resource);249250class EditorScriptCodeCompletionCache;251class FindInFilesDialog;252class FindInFilesPanel;253254class ScriptEditor : public PanelContainer {255GDCLASS(ScriptEditor, PanelContainer);256257enum MenuOptions {258// File.259FILE_MENU_NEW,260FILE_MENU_NEW_TEXTFILE,261FILE_MENU_OPEN,262FILE_MENU_REOPEN_CLOSED,263FILE_MENU_OPEN_RECENT,264265FILE_MENU_SAVE,266FILE_MENU_SAVE_AS,267FILE_MENU_SAVE_ALL,268269FILE_MENU_SOFT_RELOAD_TOOL,270FILE_MENU_COPY_PATH,271FILE_MENU_COPY_UID,272FILE_MENU_SHOW_IN_FILE_SYSTEM,273274FILE_MENU_HISTORY_PREV,275FILE_MENU_HISTORY_NEXT,276277FILE_MENU_THEME_SUBMENU,278279FILE_MENU_CLOSE,280FILE_MENU_CLOSE_ALL,281FILE_MENU_CLOSE_OTHER_TABS,282FILE_MENU_CLOSE_TABS_BELOW,283FILE_MENU_CLOSE_DOCS,284285FILE_MENU_RUN,286287FILE_MENU_TOGGLE_FILES_PANEL,288289FILE_MENU_MOVE_UP,290FILE_MENU_MOVE_DOWN,291FILE_MENU_SORT,292293// Search.294HELP_SEARCH_FIND,295HELP_SEARCH_FIND_NEXT,296HELP_SEARCH_FIND_PREVIOUS,297298SEARCH_IN_FILES,299REPLACE_IN_FILES,300301SEARCH_HELP,302SEARCH_WEBSITE,303304// Theme.305THEME_IMPORT,306THEME_RELOAD,307THEME_SAVE_AS,308};309310enum ScriptSortBy {311SORT_BY_NAME,312SORT_BY_PATH,313SORT_BY_NONE,314};315316enum ScriptListName {317DISPLAY_NAME,318DISPLAY_DIR_AND_NAME,319DISPLAY_FULL_PATH,320};321322HBoxContainer *menu_hb = nullptr;323MenuButton *file_menu = nullptr;324MenuButton *edit_menu = nullptr;325MenuButton *script_search_menu = nullptr;326MenuButton *debug_menu = nullptr;327PopupMenu *context_menu = nullptr;328Timer *autosave_timer = nullptr;329uint64_t idle = 0;330331PopupMenu *recent_scripts = nullptr;332PopupMenu *theme_submenu = nullptr;333334Button *help_search = nullptr;335Button *site_search = nullptr;336Button *make_floating = nullptr;337bool is_floating = false;338EditorHelpSearch *help_search_dialog = nullptr;339340ItemList *script_list = nullptr;341HSplitContainer *script_split = nullptr;342ItemList *members_overview = nullptr;343LineEdit *filter_scripts = nullptr;344LineEdit *filter_methods = nullptr;345VBoxContainer *scripts_vbox = nullptr;346VBoxContainer *overview_vbox = nullptr;347HBoxContainer *buttons_hbox = nullptr;348Label *filename = nullptr;349Button *members_overview_alphabeta_sort_button = nullptr;350bool members_overview_enabled;351ItemList *help_overview = nullptr;352bool help_overview_enabled;353VSplitContainer *list_split = nullptr;354TabContainer *tab_container = nullptr;355EditorFileDialog *file_dialog = nullptr;356AcceptDialog *error_dialog = nullptr;357ConfirmationDialog *erase_tab_confirm = nullptr;358ScriptCreateDialog *script_create_dialog = nullptr;359Button *scripts_visible = nullptr;360FindReplaceBar *find_replace_bar = nullptr;361362float zoom_factor = 1.0f;363364TextureRect *script_icon = nullptr;365Label *script_name_label = nullptr;366367Button *script_back = nullptr;368Button *script_forward = nullptr;369370FindInFilesDialog *find_in_files_dialog = nullptr;371FindInFilesPanel *find_in_files = nullptr;372Button *find_in_files_button = nullptr;373374WindowWrapper *window_wrapper = nullptr;375376enum {377SCRIPT_EDITOR_FUNC_MAX = 32,378};379380static int script_editor_func_count;381static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX];382383Vector<Ref<EditorSyntaxHighlighter>> syntax_highlighters;384385struct ScriptHistory {386Control *control = nullptr;387Variant state;388};389390Vector<ScriptHistory> history;391int history_pos;392393List<String> previous_scripts;394List<int> script_close_queue;395396void _tab_changed(int p_which);397void _menu_option(int p_option);398void _theme_option(int p_option);399void _show_save_theme_as_dialog();400bool _has_docs_tab() const;401bool _has_script_tab() const;402void _prepare_file_menu();403void _file_menu_closed();404405Tree *disk_changed_list = nullptr;406ConfirmationDialog *disk_changed = nullptr;407408bool restoring_layout;409410void _resave_scripts(const String &p_str);411412bool _test_script_times_on_disk(Ref<Resource> p_for_script = Ref<Resource>());413414void _add_recent_script(const String &p_path);415void _update_recent_scripts();416void _open_recent_script(int p_idx);417418void _show_error_dialog(const String &p_path);419420void _close_tab(int p_idx, bool p_save = true, bool p_history_back = true);421void _update_find_replace_bar();422423void _close_current_tab(bool p_save = true, bool p_history_back = true);424void _close_discard_current_tab(const String &p_str);425void _close_docs_tab();426void _close_other_tabs();427void _close_tabs_below();428void _close_all_tabs();429void _queue_close_tabs();430431void _copy_script_path();432void _copy_script_uid();433434void _ask_close_current_unsaved_tab(ScriptEditorBase *current);435436bool grab_focus_block;437438bool pending_auto_reload;439bool auto_reload_running_scripts;440bool reload_all_scripts = false;441Vector<String> script_paths_to_reload;442void _live_auto_reload_running_scripts();443444void _update_selected_editor_menu();445446void _editor_stop();447448int edit_pass;449450void _add_callback(Object *p_obj, const String &p_function, const PackedStringArray &p_args);451void _res_saved_callback(const Ref<Resource> &p_res);452void _scene_saved_callback(const String &p_path);453void _mark_built_in_scripts_as_saved(const String &p_parent_path);454455bool open_textfile_after_create = true;456bool trim_trailing_whitespace_on_save;457bool trim_final_newlines_on_save;458bool convert_indent_on_save;459bool external_editor_active;460461void _goto_script_line2(int p_line);462void _goto_script_line(Ref<RefCounted> p_script, int p_line);463void _set_execution(Ref<RefCounted> p_script, int p_line);464void _clear_execution(Ref<RefCounted> p_script);465String _get_debug_tooltip(const String &p_text, Node *p_se);466void _breaked(bool p_breaked, bool p_can_debug);467void _script_created(Ref<Script> p_script);468void _set_breakpoint(Ref<RefCounted> p_script, int p_line, bool p_enabled);469void _clear_breakpoints();470Array _get_cached_breakpoints_for_script(const String &p_path) const;471472ScriptEditorBase *_get_current_editor() const;473TypedArray<ScriptEditorBase> _get_open_script_editors() const;474475Ref<ConfigFile> script_editor_cache;476void _save_editor_state(ScriptEditorBase *p_editor);477void _save_layout();478void _editor_settings_changed();479void _apply_editor_settings();480void _filesystem_changed();481void _files_moved(const String &p_old_file, const String &p_new_file);482void _file_removed(const String &p_file);483void _autosave_scripts();484void _update_autosave_timer();485void _reload_scripts(bool p_refresh_only = false);486487void _update_members_overview_visibility();488void _update_members_overview();489void _toggle_members_overview_alpha_sort(bool p_alphabetic_sort);490void _filter_scripts_text_changed(const String &p_newtext);491void _filter_methods_text_changed(const String &p_newtext);492void _update_script_names();493bool _sort_list_on_update;494495void _members_overview_selected(int p_idx);496void _script_selected(int p_idx);497498void _update_help_overview_visibility();499void _update_help_overview();500void _help_overview_selected(int p_idx);501502void _update_online_doc();503504void _find_scripts(Node *p_base, Node *p_current, HashSet<Ref<Script>> &used);505506void _tree_changed();507508void _split_dragged(float);509510Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);511bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;512void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);513514virtual void input(const Ref<InputEvent> &p_event) override;515virtual void shortcut_input(const Ref<InputEvent> &p_event) override;516517void _script_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index);518void _make_script_list_context_menu();519520void _help_search(const String &p_text);521522void _history_forward();523void _history_back();524525bool waiting_update_names;526bool lock_history = false;527528void _help_class_open(const String &p_class);529void _help_class_goto(const String &p_desc);530bool _help_tab_goto(const String &p_name, const String &p_desc);531void _update_history_arrows();532void _save_history();533void _save_previous_state(Dictionary p_state);534void _go_to_tab(int p_idx);535void _update_history_pos(int p_new_pos);536void _update_script_colors();537void _update_modified_scripts_for_external_editor(Ref<Script> p_for_script = Ref<Script>());538539void _script_changed();540int file_dialog_option;541void _file_dialog_action(const String &p_file);542543Ref<Script> _get_current_script();544TypedArray<Script> _get_open_scripts() const;545546HashSet<String> textfile_extensions;547Ref<TextFile> _load_text_file(const String &p_path, Error *r_error) const;548Error _save_text_file(Ref<TextFile> p_text_file, const String &p_path);549550void _on_replace_in_files_requested(const String &text);551void _on_find_in_files_result_selected(const String &fpath, int line_number, int begin, int end);552void _start_find_in_files(bool with_replace);553void _on_find_in_files_modified_files(const PackedStringArray &paths);554void _on_find_in_files_close_button_clicked();555556void _set_script_zoom_factor(float p_zoom_factor);557void _update_code_editor_zoom_factor(CodeTextEditor *p_code_text_editor);558559void _window_changed(bool p_visible);560561static void _open_script_request(const String &p_path);562void _close_builtin_scripts_from_scene(const String &p_scene);563564static ScriptEditor *script_editor;565566protected:567void _notification(int p_what);568static void _bind_methods();569570public:571static ScriptEditor *get_singleton() { return script_editor; }572573bool toggle_files_panel();574bool is_files_panel_toggled();575void apply_scripts() const;576void reload_scripts(bool p_refresh_only = false);577void open_find_in_files_dialog(const String &text);578void open_script_create_dialog(const String &p_base_name, const String &p_base_path);579void open_text_file_create_dialog(const String &p_base_path, const String &p_base_name = "");580Ref<Resource> open_file(const String &p_file);581582void ensure_select_current();583584bool is_editor_floating();585586_FORCE_INLINE_ bool edit(const Ref<Resource> &p_resource, bool p_grab_focus = true) { return edit(p_resource, -1, 0, p_grab_focus); }587bool edit(const Ref<Resource> &p_resource, int p_line, int p_col, bool p_grab_focus = true);588589Vector<String> _get_breakpoints();590void get_breakpoints(List<String> *p_breakpoints);591592PackedStringArray get_unsaved_scripts() const;593void save_current_script();594void save_all_scripts();595void update_script_times();596597void set_window_layout(Ref<ConfigFile> p_layout);598void get_window_layout(Ref<ConfigFile> p_layout);599600void set_scene_root_script(Ref<Script> p_script);601Vector<Ref<Script>> get_open_scripts() const;602603bool script_goto_method(Ref<Script> p_script, const String &p_method);604605virtual void edited_scene_changed();606607void notify_script_close(const Ref<Script> &p_script);608void notify_script_changed(const Ref<Script> &p_script);609610void goto_help(const String &p_desc) { _help_class_goto(p_desc); }611void update_doc(const String &p_name);612void clear_docs_from_script(const Ref<Script> &p_script);613void update_docs_from_script(const Ref<Script> &p_script);614615void trigger_live_script_reload(const String &p_script_path);616void trigger_live_script_reload_all();617618VSplitContainer *get_left_list_split() { return list_split; }619620void set_live_auto_reload_running_scripts(bool p_enabled);621622void register_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter);623void unregister_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter);624625static void register_create_script_editor_function(CreateScriptEditorFunc p_func);626627ScriptEditor(WindowWrapper *p_wrapper);628};629630class ScriptEditorPlugin : public EditorPlugin {631GDCLASS(ScriptEditorPlugin, EditorPlugin);632633ScriptEditor *script_editor = nullptr;634WindowWrapper *window_wrapper = nullptr;635636String last_editor;637638void _focus_another_editor();639640void _save_last_editor(const String &p_editor);641void _window_visibility_changed(bool p_visible);642643protected:644void _notification(int p_what);645646public:647virtual String get_plugin_name() const override { return TTRC("Script"); }648bool has_main_screen() const override { return true; }649virtual void edit(Object *p_object) override;650virtual bool handles(Object *p_object) const override;651virtual void make_visible(bool p_visible) override;652virtual void selected_notify() override;653654virtual String get_unsaved_status(const String &p_for_scene) const override;655virtual void save_external_data() override;656virtual void apply_changes() override;657658virtual void set_window_layout(Ref<ConfigFile> p_layout) override;659virtual void get_window_layout(Ref<ConfigFile> p_layout) override;660661virtual void get_breakpoints(List<String> *p_breakpoints) override;662663virtual void edited_scene_changed() override;664665ScriptEditorPlugin();666};667668669