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-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #ifndef SCRIPT_EDITOR_PLUGIN_H
32 #define SCRIPT_EDITOR_PLUGIN_H
33 
34 #include "core/script_language.h"
35 #include "editor/code_editor.h"
36 #include "editor/editor_help.h"
37 #include "editor/editor_help_search.h"
38 #include "editor/editor_plugin.h"
39 #include "editor/script_create_dialog.h"
40 #include "scene/gui/item_list.h"
41 #include "scene/gui/line_edit.h"
42 #include "scene/gui/menu_button.h"
43 #include "scene/gui/split_container.h"
44 #include "scene/gui/tab_container.h"
45 #include "scene/gui/text_edit.h"
46 #include "scene/gui/tool_button.h"
47 #include "scene/gui/tree.h"
48 #include "scene/main/timer.h"
49 #include "scene/resources/text_file.h"
50 
51 class ScriptEditorQuickOpen : public ConfirmationDialog {
52 
53 	GDCLASS(ScriptEditorQuickOpen, ConfirmationDialog);
54 
55 	LineEdit *search_box;
56 	Tree *search_options;
57 	String function;
58 
59 	void _update_search();
60 
61 	void _sbox_input(const Ref<InputEvent> &p_ie);
62 	Vector<String> functions;
63 
64 	void _confirmed();
65 	void _text_changed(const String &p_newtext);
66 
67 protected:
68 	void _notification(int p_what);
69 	static void _bind_methods();
70 
71 public:
72 	void popup_dialog(const Vector<String> &p_functions, bool p_dontclear = false);
73 	ScriptEditorQuickOpen();
74 };
75 
76 class ScriptEditorDebugger;
77 
78 class ScriptEditorBase : public VBoxContainer {
79 
80 	GDCLASS(ScriptEditorBase, VBoxContainer);
81 
82 protected:
83 	static void _bind_methods();
84 
85 public:
86 	virtual void add_syntax_highlighter(SyntaxHighlighter *p_highlighter) = 0;
87 	virtual void set_syntax_highlighter(SyntaxHighlighter *p_highlighter) = 0;
88 
89 	virtual void apply_code() = 0;
90 	virtual RES get_edited_resource() const = 0;
91 	virtual Vector<String> get_functions() = 0;
92 	virtual void set_edited_resource(const RES &p_res) = 0;
93 	virtual void reload_text() = 0;
94 	virtual String get_name() = 0;
95 	virtual Ref<Texture> get_icon() = 0;
96 	virtual bool is_unsaved() = 0;
97 	virtual Variant get_edit_state() = 0;
98 	virtual void set_edit_state(const Variant &p_state) = 0;
99 	virtual void goto_line(int p_line, bool p_with_error = false) = 0;
100 	virtual void set_executing_line(int p_line) = 0;
101 	virtual void clear_executing_line() = 0;
102 	virtual void trim_trailing_whitespace() = 0;
103 	virtual void insert_final_newline() = 0;
104 	virtual void convert_indent_to_spaces() = 0;
105 	virtual void convert_indent_to_tabs() = 0;
106 	virtual void ensure_focus() = 0;
107 	virtual void tag_saved_version() = 0;
reload(bool p_soft)108 	virtual void reload(bool p_soft) {}
109 	virtual void get_breakpoints(List<int> *p_breakpoints) = 0;
110 	virtual void add_callback(const String &p_function, PoolStringArray p_args) = 0;
111 	virtual void update_settings() = 0;
112 	virtual void set_debugger_active(bool p_active) = 0;
can_lose_focus_on_node_selection()113 	virtual bool can_lose_focus_on_node_selection() { return true; }
114 
115 	virtual bool show_members_overview() = 0;
116 
117 	virtual void set_tooltip_request_func(String p_method, Object *p_obj) = 0;
118 	virtual Control *get_edit_menu() = 0;
119 	virtual void clear_edit_menu() = 0;
120 
121 	virtual void validate() = 0;
122 
ScriptEditorBase()123 	ScriptEditorBase() {}
124 };
125 
126 typedef SyntaxHighlighter *(*CreateSyntaxHighlighterFunc)();
127 typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const RES &p_resource);
128 
129 class EditorScriptCodeCompletionCache;
130 class FindInFilesDialog;
131 class FindInFilesPanel;
132 
133 class ScriptEditor : public PanelContainer {
134 
135 	GDCLASS(ScriptEditor, PanelContainer);
136 
137 	EditorNode *editor;
138 	enum {
139 		FILE_NEW,
140 		FILE_NEW_TEXTFILE,
141 		FILE_OPEN,
142 		FILE_REOPEN_CLOSED,
143 		FILE_OPEN_RECENT,
144 		FILE_SAVE,
145 		FILE_SAVE_AS,
146 		FILE_SAVE_ALL,
147 		FILE_THEME,
148 		FILE_RUN,
149 		FILE_CLOSE,
150 		CLOSE_DOCS,
151 		CLOSE_ALL,
152 		CLOSE_OTHER_TABS,
153 		TOGGLE_SCRIPTS_PANEL,
154 		SHOW_IN_FILE_SYSTEM,
155 		FILE_COPY_PATH,
156 		FILE_TOOL_RELOAD,
157 		FILE_TOOL_RELOAD_SOFT,
158 		DEBUG_NEXT,
159 		DEBUG_STEP,
160 		DEBUG_BREAK,
161 		DEBUG_CONTINUE,
162 		DEBUG_SHOW,
163 		DEBUG_SHOW_KEEP_OPEN,
164 		DEBUG_WITH_EXTERNAL_EDITOR,
165 		SEARCH_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 
179 	enum {
180 		THEME_IMPORT,
181 		THEME_RELOAD,
182 		THEME_SAVE,
183 		THEME_SAVE_AS
184 	};
185 
186 	enum ScriptSortBy {
187 		SORT_BY_NAME,
188 		SORT_BY_PATH,
189 		SORT_BY_NONE
190 	};
191 
192 	enum ScriptListName {
193 		DISPLAY_NAME,
194 		DISPLAY_DIR_AND_NAME,
195 		DISPLAY_FULL_PATH,
196 	};
197 
198 	HBoxContainer *menu_hb;
199 	MenuButton *file_menu;
200 	MenuButton *edit_menu;
201 	MenuButton *script_search_menu;
202 	MenuButton *debug_menu;
203 	PopupMenu *context_menu;
204 	Timer *autosave_timer;
205 	uint64_t idle;
206 
207 	PopupMenu *recent_scripts;
208 	PopupMenu *theme_submenu;
209 
210 	Button *help_search;
211 	Button *site_search;
212 	EditorHelpSearch *help_search_dialog;
213 
214 	ItemList *script_list;
215 	HSplitContainer *script_split;
216 	ItemList *members_overview;
217 	LineEdit *filter_scripts;
218 	LineEdit *filter_methods;
219 	VBoxContainer *scripts_vbox;
220 	VBoxContainer *overview_vbox;
221 	HBoxContainer *buttons_hbox;
222 	Label *filename;
223 	ToolButton *members_overview_alphabeta_sort_button;
224 	bool members_overview_enabled;
225 	ItemList *help_overview;
226 	bool help_overview_enabled;
227 	VSplitContainer *list_split;
228 	TabContainer *tab_container;
229 	EditorFileDialog *file_dialog;
230 	AcceptDialog *error_dialog;
231 	ConfirmationDialog *erase_tab_confirm;
232 	ScriptCreateDialog *script_create_dialog;
233 	ScriptEditorDebugger *debugger;
234 	ToolButton *scripts_visible;
235 
236 	String current_theme;
237 
238 	TextureRect *script_icon;
239 	Label *script_name_label;
240 
241 	ToolButton *script_back;
242 	ToolButton *script_forward;
243 
244 	FindInFilesDialog *find_in_files_dialog;
245 	FindInFilesPanel *find_in_files;
246 	Button *find_in_files_button;
247 
248 	enum {
249 		SCRIPT_EDITOR_FUNC_MAX = 32,
250 		SYNTAX_HIGHLIGHTER_FUNC_MAX = 32
251 	};
252 
253 	static int script_editor_func_count;
254 	static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX];
255 
256 	static int syntax_highlighters_func_count;
257 	static CreateSyntaxHighlighterFunc syntax_highlighters_funcs[SYNTAX_HIGHLIGHTER_FUNC_MAX];
258 
259 	struct ScriptHistory {
260 
261 		Control *control;
262 		Variant state;
263 	};
264 
265 	Vector<ScriptHistory> history;
266 	int history_pos;
267 
268 	List<String> previous_scripts;
269 
270 	void _tab_changed(int p_which);
271 	void _menu_option(int p_option);
272 	void _theme_option(int p_option);
273 	void _show_save_theme_as_dialog();
274 
275 	Tree *disk_changed_list;
276 	ConfirmationDialog *disk_changed;
277 
278 	bool restoring_layout;
279 
280 	String _get_debug_tooltip(const String &p_text, Node *_se);
281 
282 	void _resave_scripts(const String &p_str);
283 	void _reload_scripts();
284 
285 	bool _test_script_times_on_disk(RES p_for_script = Ref<Resource>());
286 
287 	void _add_recent_script(String p_path);
288 	void _update_recent_scripts();
289 	void _open_recent_script(int p_idx);
290 
291 	void _show_error_dialog(String p_path);
292 
293 	void _close_tab(int p_idx, bool p_save = true, bool p_history_back = true);
294 
295 	void _close_current_tab();
296 	void _close_discard_current_tab(const String &p_str);
297 	void _close_docs_tab();
298 	void _close_other_tabs();
299 	void _close_all_tabs();
300 
301 	void _copy_script_path();
302 
303 	void _ask_close_current_unsaved_tab(ScriptEditorBase *current);
304 
305 	bool grab_focus_block;
306 
307 	bool pending_auto_reload;
308 	bool auto_reload_running_scripts;
309 	void _live_auto_reload_running_scripts();
310 
311 	void _update_selected_editor_menu();
312 
313 	EditorScriptCodeCompletionCache *completion_cache;
314 
315 	void _editor_play();
316 	void _editor_pause();
317 	void _editor_stop();
318 
319 	int edit_pass;
320 
321 	void _add_callback(Object *p_obj, const String &p_function, const PoolStringArray &p_args);
322 	void _res_saved_callback(const Ref<Resource> &p_res);
323 
324 	bool trim_trailing_whitespace_on_save;
325 	bool use_space_indentation;
326 	bool convert_indent_on_save;
327 
328 	void _trim_trailing_whitespace(TextEdit *tx);
329 
330 	void _goto_script_line2(int p_line);
331 	void _goto_script_line(REF p_script, int p_line);
332 	void _set_execution(REF p_script, int p_line);
333 	void _clear_execution(REF p_script);
334 	void _breaked(bool p_breaked, bool p_can_debug);
335 	void _show_debugger(bool p_show);
336 	void _update_window_menu();
337 	void _script_created(Ref<Script> p_script);
338 
339 	ScriptEditorBase *_get_current_editor() const;
340 
341 	void _save_layout();
342 	void _editor_settings_changed();
343 	void _autosave_scripts();
344 	void _update_autosave_timer();
345 
346 	void _update_members_overview_visibility();
347 	void _update_members_overview();
348 	void _toggle_members_overview_alpha_sort(bool p_alphabetic_sort);
349 	void _filter_scripts_text_changed(const String &p_newtext);
350 	void _filter_methods_text_changed(const String &p_newtext);
351 	void _update_script_names();
352 	void _update_script_connections();
353 	bool _sort_list_on_update;
354 
355 	void _members_overview_selected(int p_idx);
356 	void _script_selected(int p_idx);
357 
358 	void _update_help_overview_visibility();
359 	void _update_help_overview();
360 	void _help_overview_selected(int p_idx);
361 
362 	void _find_scripts(Node *p_base, Node *p_current, Set<Ref<Script> > &used);
363 
364 	void _tree_changed();
365 
366 	void _script_split_dragged(float);
367 
368 	Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
369 	bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
370 	void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
371 
372 	void _unhandled_input(const Ref<InputEvent> &p_event);
373 
374 	void _script_list_gui_input(const Ref<InputEvent> &ev);
375 	void _make_script_list_context_menu();
376 
377 	void _help_search(String p_text);
378 	void _help_index(String p_text);
379 
380 	void _history_forward();
381 	void _history_back();
382 
383 	bool waiting_update_names;
384 
385 	void _help_class_open(const String &p_class);
386 	void _help_class_goto(const String &p_desc);
387 	void _update_history_arrows();
388 	void _save_history();
389 	void _go_to_tab(int p_idx);
390 	void _update_history_pos(int p_new_pos);
391 	void _update_script_colors();
392 	void _update_modified_scripts_for_external_editor(Ref<Script> p_for_script = Ref<Script>());
393 
394 	void _script_changed();
395 	int file_dialog_option;
396 	void _file_dialog_action(String p_file);
397 
398 	Ref<Script> _get_current_script();
399 	Array _get_open_scripts() const;
400 
401 	Ref<TextFile> _load_text_file(const String &p_path, Error *r_error);
402 	Error _save_text_file(Ref<TextFile> p_text_file, const String &p_path);
403 
404 	void _on_find_in_files_requested(String text);
405 	void _on_find_in_files_result_selected(String fpath, int line_number, int begin, int end);
406 	void _start_find_in_files(bool with_replace);
407 	void _on_find_in_files_modified_files(PoolStringArray paths);
408 
409 	static void _open_script_request(const String &p_path);
410 
411 	static ScriptEditor *script_editor;
412 
413 protected:
414 	void _notification(int p_what);
415 	static void _bind_methods();
416 
417 public:
get_singleton()418 	static ScriptEditor *get_singleton() { return script_editor; }
419 
420 	bool toggle_scripts_panel();
421 	bool is_scripts_panel_toggled();
422 	void ensure_focus_current();
423 	void apply_scripts() const;
424 	void open_script_create_dialog(const String &p_base_name, const String &p_base_path);
425 
426 	void ensure_select_current();
427 
428 	_FORCE_INLINE_ bool edit(const RES &p_resource, bool p_grab_focus = true) { return edit(p_resource, -1, 0, p_grab_focus); }
429 	bool edit(const RES &p_resource, int p_line, int p_col, bool p_grab_focus = true);
430 
431 	void get_breakpoints(List<String> *p_breakpoints);
432 
433 	void save_all_scripts();
434 
435 	void set_window_layout(Ref<ConfigFile> p_layout);
436 	void get_window_layout(Ref<ConfigFile> p_layout);
437 
438 	void set_scene_root_script(Ref<Script> p_script);
439 	Vector<Ref<Script> > get_open_scripts() const;
440 
441 	bool script_goto_method(Ref<Script> p_script, const String &p_method);
442 
443 	virtual void edited_scene_changed();
444 
445 	void notify_script_close(const Ref<Script> &p_script);
446 	void notify_script_changed(const Ref<Script> &p_script);
447 
448 	void close_builtin_scripts_from_scene(const String &p_scene);
449 
goto_help(const String & p_desc)450 	void goto_help(const String &p_desc) { _help_class_goto(p_desc); }
451 
452 	bool can_take_away_focus() const;
453 
get_left_list_split()454 	VSplitContainer *get_left_list_split() { return list_split; }
455 
get_debugger()456 	ScriptEditorDebugger *get_debugger() { return debugger; }
457 	void set_live_auto_reload_running_scripts(bool p_enabled);
458 
459 	static void register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func);
460 	static void register_create_script_editor_function(CreateScriptEditorFunc p_func);
461 
462 	ScriptEditor(EditorNode *p_editor);
463 	~ScriptEditor();
464 };
465 
466 class ScriptEditorPlugin : public EditorPlugin {
467 
468 	GDCLASS(ScriptEditorPlugin, EditorPlugin);
469 
470 	ScriptEditor *script_editor;
471 	EditorNode *editor;
472 
473 public:
get_name()474 	virtual String get_name() const { return "Script"; }
has_main_screen()475 	bool has_main_screen() const { return true; }
476 	virtual void edit(Object *p_object);
477 	virtual bool handles(Object *p_object) const;
478 	virtual void make_visible(bool p_visible);
479 	virtual void selected_notify();
480 
481 	virtual void save_external_data();
482 	virtual void apply_changes();
483 
484 	virtual void restore_global_state();
485 	virtual void save_global_state();
486 
487 	virtual void set_window_layout(Ref<ConfigFile> p_layout);
488 	virtual void get_window_layout(Ref<ConfigFile> p_layout);
489 
490 	virtual void get_breakpoints(List<String> *p_breakpoints);
491 
492 	virtual void edited_scene_changed();
493 
494 	ScriptEditorPlugin(EditorNode *p_node);
495 	~ScriptEditorPlugin();
496 };
497 
498 #endif // SCRIPT_EDITOR_PLUGIN_H
499