1 /*************************************************************************/
2 /*  script_editor_plugin.cpp                                             */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 #include "script_editor_plugin.h"
31 #include "editor/editor_node.h"
32 #include "editor/editor_settings.h"
33 #include "editor/script_editor_debugger.h"
34 #include "globals.h"
35 #include "io/resource_loader.h"
36 #include "io/resource_saver.h"
37 #include "os/file_access.h"
38 #include "os/input.h"
39 #include "os/keyboard.h"
40 #include "os/os.h"
41 #include "scene/main/viewport.h"
42 
43 /*** SCRIPT EDITOR ****/
44 
_can_open_in_editor(Script * p_script)45 static bool _can_open_in_editor(Script *p_script) {
46 
47 	String path = p_script->get_path();
48 
49 	if (path.find("::") != -1) {
50 		//refuse handling this if it can't be edited
51 
52 		bool valid = false;
53 		for (int i = 0; i < EditorNode::get_singleton()->get_editor_data().get_edited_scene_count(); i++) {
54 			if (path.begins_with(EditorNode::get_singleton()->get_editor_data().get_scene_path(i))) {
55 				valid = true;
56 				break;
57 			}
58 		}
59 
60 		return valid;
61 	}
62 
63 	return true;
64 }
65 
66 class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache {
67 
68 	struct Cache {
69 		uint64_t time_loaded;
70 		RES cache;
71 	};
72 
73 	Map<String, Cache> cached;
74 
75 public:
76 	uint64_t max_time_cache;
77 	int max_cache_size;
78 
cleanup()79 	void cleanup() {
80 
81 		List<Map<String, Cache>::Element *> to_clean;
82 
83 		Map<String, Cache>::Element *I = cached.front();
84 		while (I) {
85 			if ((OS::get_singleton()->get_ticks_msec() - I->get().time_loaded) > max_time_cache) {
86 				to_clean.push_back(I);
87 			}
88 			I = I->next();
89 		}
90 
91 		while (to_clean.front()) {
92 			cached.erase(to_clean.front()->get());
93 			to_clean.pop_front();
94 		}
95 	}
96 
get_cached_resource(const String & p_path)97 	RES get_cached_resource(const String &p_path) {
98 
99 		Map<String, Cache>::Element *E = cached.find(p_path);
100 		if (!E) {
101 
102 			Cache c;
103 			c.cache = ResourceLoader::load(p_path);
104 			E = cached.insert(p_path, c);
105 		}
106 
107 		E->get().time_loaded = OS::get_singleton()->get_ticks_msec();
108 
109 		if (cached.size() > max_cache_size) {
110 			uint64_t older;
111 			Map<String, Cache>::Element *O = cached.front();
112 			older = O->get().time_loaded;
113 			Map<String, Cache>::Element *I = O;
114 			while (I) {
115 				if (I->get().time_loaded < older) {
116 					older = I->get().time_loaded;
117 					O = I;
118 				}
119 				I = I->next();
120 			}
121 
122 			if (O != E) { //should never heppane..
123 				cached.erase(O);
124 			}
125 		}
126 
127 		return E->get().cache;
128 	}
129 
EditorScriptCodeCompletionCache()130 	EditorScriptCodeCompletionCache() {
131 
132 		max_cache_size = 128;
133 		max_time_cache = 5 * 60 * 1000; //minutes, five
134 	}
135 };
136 
137 #define SORT_SCRIPT_LIST
138 
popup(const Vector<String> & p_functions,bool p_dontclear)139 void ScriptEditorQuickOpen::popup(const Vector<String> &p_functions, bool p_dontclear) {
140 
141 	popup_centered_ratio(0.6);
142 	if (p_dontclear)
143 		search_box->select_all();
144 	else
145 		search_box->clear();
146 	search_box->grab_focus();
147 	functions = p_functions;
148 	_update_search();
149 }
150 
_text_changed(const String & p_newtext)151 void ScriptEditorQuickOpen::_text_changed(const String &p_newtext) {
152 
153 	_update_search();
154 }
155 
_sbox_input(const InputEvent & p_ie)156 void ScriptEditorQuickOpen::_sbox_input(const InputEvent &p_ie) {
157 
158 	if (p_ie.type == InputEvent::KEY && (p_ie.key.scancode == KEY_UP ||
159 												p_ie.key.scancode == KEY_DOWN ||
160 												p_ie.key.scancode == KEY_PAGEUP ||
161 												p_ie.key.scancode == KEY_PAGEDOWN)) {
162 
163 		search_options->call("_input_event", p_ie);
164 		search_box->accept_event();
165 	}
166 }
167 
_update_search()168 void ScriptEditorQuickOpen::_update_search() {
169 
170 	search_options->clear();
171 	TreeItem *root = search_options->create_item();
172 
173 	for (int i = 0; i < functions.size(); i++) {
174 
175 		String file = functions[i];
176 		if ((search_box->get_text() == "" || file.findn(search_box->get_text()) != -1)) {
177 
178 			TreeItem *ti = search_options->create_item(root);
179 			ti->set_text(0, file);
180 			if (root->get_children() == ti)
181 				ti->select(0);
182 		}
183 	}
184 
185 	get_ok()->set_disabled(root->get_children() == NULL);
186 }
187 
_confirmed()188 void ScriptEditorQuickOpen::_confirmed() {
189 
190 	TreeItem *ti = search_options->get_selected();
191 	if (!ti)
192 		return;
193 	int line = ti->get_text(0).get_slice(":", 1).to_int();
194 
195 	emit_signal("goto_line", line - 1);
196 	hide();
197 }
198 
_notification(int p_what)199 void ScriptEditorQuickOpen::_notification(int p_what) {
200 
201 	if (p_what == NOTIFICATION_ENTER_TREE) {
202 
203 		connect("confirmed", this, "_confirmed");
204 	}
205 }
206 
_bind_methods()207 void ScriptEditorQuickOpen::_bind_methods() {
208 
209 	ObjectTypeDB::bind_method(_MD("_text_changed"), &ScriptEditorQuickOpen::_text_changed);
210 	ObjectTypeDB::bind_method(_MD("_confirmed"), &ScriptEditorQuickOpen::_confirmed);
211 	ObjectTypeDB::bind_method(_MD("_sbox_input"), &ScriptEditorQuickOpen::_sbox_input);
212 
213 	ADD_SIGNAL(MethodInfo("goto_line", PropertyInfo(Variant::INT, "line")));
214 }
215 
ScriptEditorQuickOpen()216 ScriptEditorQuickOpen::ScriptEditorQuickOpen() {
217 
218 	VBoxContainer *vbc = memnew(VBoxContainer);
219 	add_child(vbc);
220 	set_child_rect(vbc);
221 	search_box = memnew(LineEdit);
222 	vbc->add_margin_child(TTR("Search:"), search_box);
223 	search_box->connect("text_changed", this, "_text_changed");
224 	search_box->connect("input_event", this, "_sbox_input");
225 	search_options = memnew(Tree);
226 	vbc->add_margin_child(TTR("Matches:"), search_options, true);
227 	get_ok()->set_text(TTR("Open"));
228 	get_ok()->set_disabled(true);
229 	register_text_enter(search_box);
230 	set_hide_on_ok(false);
231 	search_options->connect("item_activated", this, "_confirmed");
232 	search_options->set_hide_root(true);
233 }
234 
235 /////////////////////////////////
236 
237 ScriptEditor *ScriptEditor::script_editor = NULL;
238 
get_functions()239 Vector<String> ScriptTextEditor::get_functions() {
240 
241 	String errortxt;
242 	int line = -1, col;
243 	TextEdit *te = get_text_edit();
244 	String text = te->get_text();
245 	List<String> fnc;
246 
247 	if (script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
248 
249 		//if valid rewrite functions to latest
250 		functions.clear();
251 		for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
252 
253 			functions.push_back(E->get());
254 		}
255 	}
256 
257 	return functions;
258 }
259 
apply_code()260 void ScriptTextEditor::apply_code() {
261 
262 	if (script.is_null())
263 		return;
264 	//	print_line("applying code");
265 	script->set_source_code(get_text_edit()->get_text());
266 	script->update_exports();
267 }
268 
get_edited_script() const269 Ref<Script> ScriptTextEditor::get_edited_script() const {
270 
271 	return script;
272 }
273 
_load_theme_settings()274 void ScriptTextEditor::_load_theme_settings() {
275 
276 	get_text_edit()->clear_colors();
277 
278 	/* keyword color */
279 
280 	get_text_edit()->set_custom_bg_color(EDITOR_DEF("text_editor/background_color", Color(0, 0, 0, 0)));
281 	get_text_edit()->add_color_override("completion_background_color", EDITOR_DEF("text_editor/completion_background_color", Color(0, 0, 0, 0)));
282 	get_text_edit()->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/completion_selected_color", Color::html("434244")));
283 	get_text_edit()->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/completion_existing_color", Color::html("21dfdfdf")));
284 	get_text_edit()->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/completion_scroll_color", Color::html("ffffff")));
285 	get_text_edit()->add_color_override("completion_font_color", EDITOR_DEF("text_editor/completion_font_color", Color::html("aaaaaa")));
286 	get_text_edit()->add_color_override("font_color", EDITOR_DEF("text_editor/text_color", Color(0, 0, 0)));
287 	get_text_edit()->add_color_override("line_number_color", EDITOR_DEF("text_editor/line_number_color", Color(0, 0, 0)));
288 	get_text_edit()->add_color_override("caret_color", EDITOR_DEF("text_editor/caret_color", Color(0, 0, 0)));
289 	get_text_edit()->add_color_override("caret_background_color", EDITOR_DEF("text_editor/caret_background_color", Color(0, 0, 0)));
290 	get_text_edit()->add_color_override("font_selected_color", EDITOR_DEF("text_editor/text_selected_color", Color(1, 1, 1)));
291 	get_text_edit()->add_color_override("selection_color", EDITOR_DEF("text_editor/selection_color", Color(0.2, 0.2, 1)));
292 	get_text_edit()->add_color_override("brace_mismatch_color", EDITOR_DEF("text_editor/brace_mismatch_color", Color(1, 0.2, 0.2)));
293 	get_text_edit()->add_color_override("current_line_color", EDITOR_DEF("text_editor/current_line_color", Color(0.3, 0.5, 0.8, 0.15)));
294 	get_text_edit()->add_color_override("word_highlighted_color", EDITOR_DEF("text_editor/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15)));
295 	get_text_edit()->add_color_override("number_color", EDITOR_DEF("text_editor/number_color", Color(0.9, 0.6, 0.0, 2)));
296 	get_text_edit()->add_color_override("function_color", EDITOR_DEF("text_editor/function_color", Color(0.4, 0.6, 0.8)));
297 	get_text_edit()->add_color_override("member_variable_color", EDITOR_DEF("text_editor/member_variable_color", Color(0.9, 0.3, 0.3)));
298 	get_text_edit()->add_color_override("mark_color", EDITOR_DEF("text_editor/mark_color", Color(1.0, 0.4, 0.4, 0.4)));
299 	get_text_edit()->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2)));
300 	get_text_edit()->add_color_override("search_result_color", EDITOR_DEF("text_editor/search_result_color", Color(0.05, 0.25, 0.05, 1)));
301 	get_text_edit()->add_color_override("search_result_border_color", EDITOR_DEF("text_editor/search_result_border_color", Color(0.1, 0.45, 0.1, 1)));
302 	get_text_edit()->add_constant_override("line_spacing", EDITOR_DEF("text_editor/line_spacing", 4));
303 
304 	Color keyword_color = EDITOR_DEF("text_editor/keyword_color", Color(0.5, 0.0, 0.2));
305 
306 	List<String> keywords;
307 	script->get_language()->get_reserved_words(&keywords);
308 	for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
309 
310 		get_text_edit()->add_keyword_color(E->get(), keyword_color);
311 	}
312 
313 	//colorize core types
314 	Color basetype_color = EDITOR_DEF("text_editor/base_type_color", Color(0.3, 0.3, 0.0));
315 
316 	get_text_edit()->add_keyword_color("Vector2", basetype_color);
317 	get_text_edit()->add_keyword_color("Vector3", basetype_color);
318 	get_text_edit()->add_keyword_color("Plane", basetype_color);
319 	get_text_edit()->add_keyword_color("Quat", basetype_color);
320 	get_text_edit()->add_keyword_color("AABB", basetype_color);
321 	get_text_edit()->add_keyword_color("Matrix3", basetype_color);
322 	get_text_edit()->add_keyword_color("Transform", basetype_color);
323 	get_text_edit()->add_keyword_color("Color", basetype_color);
324 	get_text_edit()->add_keyword_color("Image", basetype_color);
325 	get_text_edit()->add_keyword_color("InputEvent", basetype_color);
326 	get_text_edit()->add_keyword_color("Rect2", basetype_color);
327 	get_text_edit()->add_keyword_color("NodePath", basetype_color);
328 
329 	//colorize engine types
330 	Color type_color = EDITOR_DEF("text_editor/engine_type_color", Color(0.0, 0.2, 0.4));
331 
332 	List<StringName> types;
333 	ObjectTypeDB::get_type_list(&types);
334 
335 	for (List<StringName>::Element *E = types.front(); E; E = E->next()) {
336 
337 		String n = E->get();
338 		if (n.begins_with("_"))
339 			n = n.substr(1, n.length());
340 
341 		get_text_edit()->add_keyword_color(n, type_color);
342 	}
343 
344 	//colorize comments
345 	Color comment_color = EDITOR_DEF("text_editor/comment_color", Color::hex(0x797e7eff));
346 	List<String> comments;
347 	script->get_language()->get_comment_delimiters(&comments);
348 
349 	for (List<String>::Element *E = comments.front(); E; E = E->next()) {
350 
351 		String comment = E->get();
352 		String beg = comment.get_slice(" ", 0);
353 		String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
354 
355 		get_text_edit()->add_color_region(beg, end, comment_color, end == "");
356 	}
357 
358 	//colorize strings
359 	Color string_color = EDITOR_DEF("text_editor/string_color", Color::hex(0x6b6f00ff));
360 	List<String> strings;
361 	script->get_language()->get_string_delimiters(&strings);
362 
363 	for (List<String>::Element *E = strings.front(); E; E = E->next()) {
364 
365 		String string = E->get();
366 		String beg = string.get_slice(" ", 0);
367 		String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
368 		get_text_edit()->add_color_region(beg, end, string_color, end == "");
369 	}
370 
371 	//colorize symbols
372 	Color symbol_color = EDITOR_DEF("text_editor/symbol_color", Color::hex(0x005291ff));
373 	get_text_edit()->set_symbol_color(symbol_color);
374 }
375 
reload_text()376 void ScriptTextEditor::reload_text() {
377 
378 	ERR_FAIL_COND(script.is_null());
379 
380 	TextEdit *te = get_text_edit();
381 	int column = te->cursor_get_column();
382 	int row = te->cursor_get_line();
383 	int h = te->get_h_scroll();
384 	int v = te->get_v_scroll();
385 
386 	te->set_text(script->get_source_code());
387 	te->clear_undo_history();
388 	te->cursor_set_line(row);
389 	te->cursor_set_column(column);
390 	te->set_h_scroll(h);
391 	te->set_v_scroll(v);
392 
393 	te->tag_saved_version();
394 
395 	_line_col_changed();
396 }
397 
_notification(int p_what)398 void ScriptTextEditor::_notification(int p_what) {
399 
400 	if (p_what == NOTIFICATION_READY) {
401 
402 		//emit_signal("name_changed");
403 	}
404 }
405 
is_unsaved()406 bool ScriptTextEditor::is_unsaved() {
407 
408 	return get_text_edit()->get_version() != get_text_edit()->get_saved_version();
409 }
410 
get_name()411 String ScriptTextEditor::get_name() {
412 	String name;
413 
414 	if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
415 		name = script->get_path().get_file();
416 		if (get_text_edit()->get_version() != get_text_edit()->get_saved_version()) {
417 			name += "(*)";
418 		}
419 	} else if (script->get_name() != "")
420 		name = script->get_name();
421 	else
422 		name = script->get_type() + "(" + itos(script->get_instance_ID()) + ")";
423 
424 	return name;
425 }
426 
get_icon()427 Ref<Texture> ScriptTextEditor::get_icon() {
428 
429 	if (get_parent_control() && get_parent_control()->has_icon(script->get_type(), "EditorIcons")) {
430 		return get_parent_control()->get_icon(script->get_type(), "EditorIcons");
431 	}
432 
433 	return Ref<Texture>();
434 }
435 
set_edited_script(const Ref<Script> & p_script)436 void ScriptTextEditor::set_edited_script(const Ref<Script> &p_script) {
437 
438 	ERR_FAIL_COND(!script.is_null());
439 
440 	script = p_script;
441 
442 	_load_theme_settings();
443 
444 	get_text_edit()->set_text(script->get_source_code());
445 	get_text_edit()->clear_undo_history();
446 	get_text_edit()->tag_saved_version();
447 
448 	emit_signal("name_changed");
449 	_line_col_changed();
450 }
451 
_validate_script()452 void ScriptTextEditor::_validate_script() {
453 
454 	String errortxt;
455 	int line = -1, col;
456 	TextEdit *te = get_text_edit();
457 
458 	String text = te->get_text();
459 	List<String> fnc;
460 
461 	if (!script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
462 		String error_text = "error(" + itos(line) + "," + itos(col) + "): " + errortxt;
463 		set_error(error_text);
464 	} else {
465 		set_error("");
466 		line = -1;
467 		if (!script->is_tool()) {
468 			script->set_source_code(text);
469 			script->update_exports();
470 			//script->reload(); //will update all the variables in property editors
471 		}
472 
473 		functions.clear();
474 		for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
475 
476 			functions.push_back(E->get());
477 		}
478 	}
479 
480 	line--;
481 	for (int i = 0; i < te->get_line_count(); i++) {
482 		te->set_line_as_marked(i, line == i);
483 	}
484 
485 	emit_signal("name_changed");
486 }
487 
_find_node_for_script(Node * p_base,Node * p_current,const Ref<Script> & p_script)488 static Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
489 
490 	if (p_current->get_owner() != p_base && p_base != p_current)
491 		return NULL;
492 	Ref<Script> c = p_current->get_script();
493 	if (c == p_script)
494 		return p_current;
495 	for (int i = 0; i < p_current->get_child_count(); i++) {
496 		Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
497 		if (found)
498 			return found;
499 	}
500 
501 	return NULL;
502 }
503 
_find_changed_scripts_for_external_editor(Node * p_base,Node * p_current,Set<Ref<Script>> & r_scripts)504 static void _find_changed_scripts_for_external_editor(Node *p_base, Node *p_current, Set<Ref<Script> > &r_scripts) {
505 
506 	if (p_current->get_owner() != p_base && p_base != p_current)
507 		return;
508 	Ref<Script> c = p_current->get_script();
509 
510 	if (c.is_valid())
511 		r_scripts.insert(c);
512 
513 	for (int i = 0; i < p_current->get_child_count(); i++) {
514 		_find_changed_scripts_for_external_editor(p_base, p_current->get_child(i), r_scripts);
515 	}
516 }
517 
_update_modified_scripts_for_external_editor(Ref<Script> p_for_script)518 void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
519 
520 	if (!bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
521 		return;
522 
523 	ERR_FAIL_COND(!get_tree());
524 
525 	Set<Ref<Script> > scripts;
526 
527 	Node *base = get_tree()->get_edited_scene_root();
528 	if (base) {
529 		_find_changed_scripts_for_external_editor(base, base, scripts);
530 	}
531 
532 	for (Set<Ref<Script> >::Element *E = scripts.front(); E; E = E->next()) {
533 
534 		Ref<Script> script = E->get();
535 
536 		if (p_for_script.is_valid() && p_for_script != script)
537 			continue;
538 
539 		if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
540 
541 			continue; //internal script, who cares, though weird
542 		}
543 
544 		uint64_t last_date = script->get_last_modified_time();
545 		uint64_t date = FileAccess::get_modified_time(script->get_path());
546 
547 		if (last_date != date) {
548 
549 			Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_type(), true);
550 			ERR_CONTINUE(!rel_script.is_valid());
551 			script->set_source_code(rel_script->get_source_code());
552 			script->set_last_modified_time(rel_script->get_last_modified_time());
553 			script->update_exports();
554 		}
555 	}
556 }
557 
_code_complete_script(const String & p_code,List<String> * r_options)558 void ScriptTextEditor::_code_complete_script(const String &p_code, List<String> *r_options) {
559 
560 	Node *base = get_tree()->get_edited_scene_root();
561 	if (base) {
562 		base = _find_node_for_script(base, base, script);
563 	}
564 	String hint;
565 	Error err = script->get_language()->complete_code(p_code, script->get_path().get_base_dir(), base, r_options, hint);
566 	if (hint != "") {
567 		get_text_edit()->set_code_hint(hint);
568 	}
569 }
_bind_methods()570 void ScriptTextEditor::_bind_methods() {
571 
572 	ADD_SIGNAL(MethodInfo("name_changed"));
573 }
574 
ScriptTextEditor()575 ScriptTextEditor::ScriptTextEditor() {
576 }
577 
578 /*** SCRIPT EDITOR ******/
579 
_get_debug_tooltip(const String & p_text,Node * _ste)580 String ScriptEditor::_get_debug_tooltip(const String &p_text, Node *_ste) {
581 
582 	ScriptTextEditor *ste = _ste->cast_to<ScriptTextEditor>();
583 
584 	String val = debugger->get_var_value(p_text);
585 	if (val != String()) {
586 		return p_text + ": " + val;
587 	} else {
588 
589 		return String();
590 	}
591 }
592 
_breaked(bool p_breaked,bool p_can_debug)593 void ScriptEditor::_breaked(bool p_breaked, bool p_can_debug) {
594 
595 	if (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor"))) {
596 		return;
597 	}
598 
599 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug));
600 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug));
601 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked);
602 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked);
603 }
604 
_show_debugger(bool p_show)605 void ScriptEditor::_show_debugger(bool p_show) {
606 
607 	//	debug_menu->get_popup()->set_item_checked( debug_menu->get_popup()->get_item_index(DEBUG_SHOW), p_show);
608 }
609 
_script_created(Ref<Script> p_script)610 void ScriptEditor::_script_created(Ref<Script> p_script) {
611 	editor->push_item(p_script.operator->());
612 }
613 
_trim_trailing_whitespace(TextEdit * tx)614 void ScriptEditor::_trim_trailing_whitespace(TextEdit *tx) {
615 
616 	bool trimed_whitespace = false;
617 	for (int i = 0; i < tx->get_line_count(); i++) {
618 		String line = tx->get_line(i);
619 		if (line.ends_with(" ") || line.ends_with("\t")) {
620 
621 			if (!trimed_whitespace) {
622 				tx->begin_complex_operation();
623 				trimed_whitespace = true;
624 			}
625 
626 			int end = 0;
627 			for (int j = line.length() - 1; j > -1; j--) {
628 				if (line[j] != ' ' && line[j] != '\t') {
629 					end = j + 1;
630 					break;
631 				}
632 			}
633 			tx->set_line(i, line.substr(0, end));
634 		}
635 	}
636 	if (trimed_whitespace) {
637 		tx->end_complex_operation();
638 		tx->update();
639 	}
640 }
641 
_goto_script_line2(int p_line)642 void ScriptEditor::_goto_script_line2(int p_line) {
643 
644 	int selected = tab_container->get_current_tab();
645 	if (selected < 0 || selected >= tab_container->get_child_count())
646 		return;
647 
648 	ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
649 	if (!current)
650 		return;
651 
652 	current->get_text_edit()->cursor_set_line(p_line);
653 }
654 
_goto_script_line(REF p_script,int p_line)655 void ScriptEditor::_goto_script_line(REF p_script, int p_line) {
656 
657 	editor->push_item(p_script.ptr());
658 	_goto_script_line2(p_line);
659 }
660 
_update_history_arrows()661 void ScriptEditor::_update_history_arrows() {
662 
663 	script_back->set_disabled(history_pos <= 0);
664 	script_forward->set_disabled(history_pos >= history.size() - 1);
665 }
666 
_go_to_tab(int p_idx)667 void ScriptEditor::_go_to_tab(int p_idx) {
668 
669 	Node *cn = tab_container->get_child(p_idx);
670 	if (!cn)
671 		return;
672 	Control *c = cn->cast_to<Control>();
673 	if (!c)
674 		return;
675 
676 	if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) {
677 
678 		Node *n = tab_container->get_current_tab_control();
679 
680 		if (n->cast_to<ScriptTextEditor>()) {
681 
682 			history[history_pos].scroll_pos = n->cast_to<ScriptTextEditor>()->get_text_edit()->get_v_scroll();
683 			history[history_pos].cursor_column = n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_column();
684 			history[history_pos].cursor_row = n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_line();
685 		}
686 		if (n->cast_to<EditorHelp>()) {
687 
688 			history[history_pos].scroll_pos = n->cast_to<EditorHelp>()->get_scroll();
689 		}
690 	}
691 
692 	history.resize(history_pos + 1);
693 	ScriptHistory sh;
694 	sh.control = c;
695 	sh.scroll_pos = 0;
696 
697 	history.push_back(sh);
698 	history_pos++;
699 
700 	tab_container->set_current_tab(p_idx);
701 
702 	c = tab_container->get_current_tab_control();
703 
704 	if (c->cast_to<ScriptTextEditor>()) {
705 
706 		script_name_label->set_text(c->cast_to<ScriptTextEditor>()->get_name());
707 		script_icon->set_texture(c->cast_to<ScriptTextEditor>()->get_icon());
708 		if (is_visible())
709 			c->cast_to<ScriptTextEditor>()->get_text_edit()->grab_focus();
710 	}
711 	if (c->cast_to<EditorHelp>()) {
712 
713 		script_name_label->set_text(c->cast_to<EditorHelp>()->get_class_name());
714 		script_icon->set_texture(get_icon("Help", "EditorIcons"));
715 		if (is_visible())
716 			c->cast_to<EditorHelp>()->set_focused();
717 	}
718 
719 	c->set_meta("__editor_pass", ++edit_pass);
720 	_update_history_arrows();
721 	_update_script_colors();
722 	_update_members_overview();
723 	_update_members_overview_visibility();
724 }
725 
_close_tab(int p_idx)726 void ScriptEditor::_close_tab(int p_idx) {
727 
728 	int selected = p_idx;
729 	if (selected < 0 || selected >= tab_container->get_child_count())
730 		return;
731 
732 	Node *tselected = tab_container->get_child(selected);
733 	ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
734 	if (current) {
735 		apply_scripts();
736 	}
737 
738 	// roll back to previous tab
739 	_history_back();
740 
741 	//remove from history
742 	history.resize(history_pos + 1);
743 
744 	for (int i = 0; i < history.size(); i++) {
745 		if (history[i].control == tselected) {
746 			history.remove(i);
747 			i--;
748 			history_pos--;
749 		}
750 	}
751 
752 	if (history_pos >= history.size()) {
753 		history_pos = history.size() - 1;
754 	}
755 
756 	int idx = tab_container->get_current_tab();
757 	memdelete(tselected);
758 	if (idx >= tab_container->get_child_count())
759 		idx = tab_container->get_child_count() - 1;
760 	if (idx >= 0) {
761 
762 		if (history_pos >= 0) {
763 			idx = history[history_pos].control->get_index();
764 		}
765 		tab_container->set_current_tab(idx);
766 
767 		//script_list->select(idx);
768 	}
769 
770 	_update_history_arrows();
771 
772 	_update_script_names();
773 	_update_members_overview_visibility();
774 	_save_layout();
775 }
776 
_close_current_tab()777 void ScriptEditor::_close_current_tab() {
778 
779 	_close_tab(tab_container->get_current_tab());
780 }
781 
_close_other_tabs(int idx)782 void ScriptEditor::_close_other_tabs(int idx) {
783 
784 	_close_all_tab(idx);
785 }
786 
_close_all_tab(int except)787 void ScriptEditor::_close_all_tab(int except) {
788 
789 	int child_count = tab_container->get_tab_count();
790 	for (int i = child_count - 1; i >= 0; i--) {
791 		if (i == except && except != -1) {
792 			continue;
793 		}
794 		ScriptTextEditor *current = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
795 		if (current) {
796 			if (current->get_text_edit()->get_version() != current->get_text_edit()->get_saved_version()) {
797 				erase_tab_confirm->set_text("Close and save changes?\n\"" + current->get_name() + "\"");
798 				erase_tab_confirm->popup_centered_minsize();
799 			} else {
800 				_close_tab(i);
801 			}
802 		} else {
803 			EditorHelp *help = tab_container->get_child(i)->cast_to<EditorHelp>();
804 			if (help) {
805 				_close_tab(i);
806 			}
807 		}
808 	}
809 }
810 
_copy_script_path()811 void ScriptEditor::_copy_script_path() {
812 	ScriptTextEditor *ste = tab_container->get_child(tab_container->get_current_tab())->cast_to<ScriptTextEditor>();
813 	Ref<Script> script = ste->get_edited_script();
814 	OS::get_singleton()->set_clipboard(script->get_path());
815 }
816 
_close_docs_tab()817 void ScriptEditor::_close_docs_tab() {
818 
819 	int child_count = tab_container->get_child_count();
820 	for (int i = child_count - 1; i >= 0; i--) {
821 
822 		EditorHelp *ste = tab_container->get_child(i)->cast_to<EditorHelp>();
823 
824 		if (ste) {
825 			_close_tab(i);
826 		}
827 	}
828 }
829 
_resave_scripts(const String & p_str)830 void ScriptEditor::_resave_scripts(const String &p_str) {
831 
832 	apply_scripts();
833 
834 	for (int i = 0; i < tab_container->get_child_count(); i++) {
835 
836 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
837 		if (!ste)
838 			continue;
839 
840 		Ref<Script> script = ste->get_edited_script();
841 
842 		if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1)
843 			continue; //internal script, who cares
844 
845 		if (trim_trailing_whitespace_on_save) {
846 			_trim_trailing_whitespace(ste->get_text_edit());
847 		}
848 		editor->save_resource(script);
849 		ste->get_text_edit()->tag_saved_version();
850 	}
851 
852 	disk_changed->hide();
853 }
854 
_reload_scripts()855 void ScriptEditor::_reload_scripts() {
856 
857 	for (int i = 0; i < tab_container->get_child_count(); i++) {
858 
859 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
860 		if (!ste) {
861 
862 			continue;
863 		}
864 
865 		Ref<Script> script = ste->get_edited_script();
866 
867 		if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
868 
869 			continue; //internal script, who cares
870 		}
871 
872 		uint64_t last_date = script->get_last_modified_time();
873 		uint64_t date = FileAccess::get_modified_time(script->get_path());
874 
875 		//printf("last date: %lli vs date: %lli\n",last_date,date);
876 		if (last_date == date) {
877 			continue;
878 		}
879 
880 		Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_type(), true);
881 		ERR_CONTINUE(!rel_script.is_valid());
882 		script->set_source_code(rel_script->get_source_code());
883 		script->set_last_modified_time(rel_script->get_last_modified_time());
884 		script->reload();
885 		ste->reload_text();
886 	}
887 
888 	disk_changed->hide();
889 	_update_script_names();
890 }
891 
_res_saved_callback(const Ref<Resource> & p_res)892 void ScriptEditor::_res_saved_callback(const Ref<Resource> &p_res) {
893 
894 	for (int i = 0; i < tab_container->get_child_count(); i++) {
895 
896 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
897 		if (!ste) {
898 
899 			continue;
900 		}
901 
902 		Ref<Script> script = ste->get_edited_script();
903 
904 		if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
905 			continue; //internal script, who cares
906 		}
907 
908 		if (script == p_res) {
909 
910 			ste->get_text_edit()->tag_saved_version();
911 		}
912 	}
913 
914 	_update_script_names();
915 
916 	if (!pending_auto_reload && auto_reload_running_scripts) {
917 		call_deferred("_live_auto_reload_running_scripts");
918 		pending_auto_reload = true;
919 	}
920 }
921 
_live_auto_reload_running_scripts()922 void ScriptEditor::_live_auto_reload_running_scripts() {
923 	pending_auto_reload = false;
924 	debugger->reload_scripts();
925 }
926 
_test_script_times_on_disk(Ref<Script> p_for_script)927 bool ScriptEditor::_test_script_times_on_disk(Ref<Script> p_for_script) {
928 
929 	disk_changed_list->clear();
930 	TreeItem *r = disk_changed_list->create_item();
931 	disk_changed_list->set_hide_root(true);
932 
933 	bool need_ask = false;
934 	bool need_reload = false;
935 	bool use_autoreload = bool(EDITOR_DEF("text_editor/auto_reload_scripts_on_external_change", false));
936 
937 	for (int i = 0; i < tab_container->get_child_count(); i++) {
938 
939 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
940 		if (ste) {
941 
942 			Ref<Script> script = ste->get_edited_script();
943 
944 			if (p_for_script.is_valid() && p_for_script != script)
945 				continue;
946 
947 			if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1)
948 				continue; //internal script, who cares
949 
950 			uint64_t last_date = script->get_last_modified_time();
951 			uint64_t date = FileAccess::get_modified_time(script->get_path());
952 
953 			//printf("last date: %lli vs date: %lli\n",last_date,date);
954 			if (last_date != date) {
955 
956 				TreeItem *ti = disk_changed_list->create_item(r);
957 				ti->set_text(0, script->get_path().get_file());
958 
959 				if (!use_autoreload || ste->is_unsaved()) {
960 					need_ask = true;
961 				}
962 				need_reload = true;
963 				//r->set_metadata(0,);
964 			}
965 		}
966 	}
967 
968 	if (need_reload) {
969 		if (!need_ask) {
970 			script_editor->_reload_scripts();
971 			need_reload = false;
972 		} else {
973 			disk_changed->call_deferred("popup_centered_ratio", 0.5);
974 		}
975 	}
976 
977 	return need_reload;
978 }
979 
swap_lines(TextEdit * tx,int line1,int line2)980 void ScriptEditor::swap_lines(TextEdit *tx, int line1, int line2) {
981 	String tmp = tx->get_line(line1);
982 	String tmp2 = tx->get_line(line2);
983 	tx->set_line(line2, tmp);
984 	tx->set_line(line1, tmp2);
985 
986 	tx->cursor_set_line(line2);
987 }
988 
_breakpoint_toggled(const int p_row)989 void ScriptEditor::_breakpoint_toggled(const int p_row) {
990 	int selected = tab_container->get_current_tab();
991 	if (selected < 0 || selected >= tab_container->get_child_count()) {
992 		return;
993 	}
994 
995 	ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
996 	if (current) {
997 		get_debugger()->set_breakpoint(current->get_edited_script()->get_path(), p_row + 1, current->get_text_edit()->is_line_set_as_breakpoint(p_row));
998 	}
999 }
1000 
_file_dialog_action(String p_file)1001 void ScriptEditor::_file_dialog_action(String p_file) {
1002 
1003 	switch (file_dialog_option) {
1004 		case FILE_SAVE_THEME_AS: {
1005 			if (!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) {
1006 				editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
1007 			}
1008 		} break;
1009 		case FILE_IMPORT_THEME: {
1010 			if (!EditorSettings::get_singleton()->import_text_editor_theme(p_file)) {
1011 				editor->show_warning(TTR("Error importing theme"), TTR("Error importing"));
1012 			}
1013 		} break;
1014 	}
1015 	file_dialog_option = -1;
1016 }
1017 
_menu_option(int p_option)1018 void ScriptEditor::_menu_option(int p_option) {
1019 
1020 	switch (p_option) {
1021 		case FILE_NEW: {
1022 			script_create_dialog->config("Node", ".gd");
1023 			script_create_dialog->popup_centered(Size2(300, 300) * EDSCALE);
1024 		} break;
1025 		case FILE_OPEN: {
1026 
1027 			editor->open_resource("Script");
1028 			return;
1029 		} break;
1030 		case FILE_SAVE_ALL: {
1031 
1032 			if (!_test_script_times_on_disk())
1033 				return;
1034 
1035 			save_all_scripts();
1036 
1037 #if 0
1038 			for(int i=0;i<tab_container->get_child_count();i++) {
1039 
1040 				ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
1041 				if (!ste)
1042 					continue;
1043 
1044 
1045 				Ref<Script> script = ste->get_edited_script();
1046 
1047 				if (script->get_path()=="" || script->get_path().find("local://")!=-1 || script->get_path().find("::")!=-1)
1048 					continue; //internal script, who cares
1049 
1050 
1051 				editor->save_resource( script );
1052 			}
1053 
1054 #endif
1055 		} break;
1056 		case FILE_IMPORT_THEME: {
1057 			file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
1058 			file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
1059 			file_dialog_option = FILE_IMPORT_THEME;
1060 			file_dialog->clear_filters();
1061 			file_dialog->add_filter("*.tet");
1062 			file_dialog->popup_centered_ratio();
1063 			file_dialog->set_title(TTR("Import Theme"));
1064 		} break;
1065 		case FILE_RELOAD_THEME: {
1066 			EditorSettings::get_singleton()->load_text_editor_theme();
1067 		} break;
1068 		case FILE_SAVE_THEME: {
1069 			if (!EditorSettings::get_singleton()->save_text_editor_theme()) {
1070 				editor->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
1071 			}
1072 		} break;
1073 		case FILE_SAVE_THEME_AS: {
1074 			file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
1075 			file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
1076 			file_dialog_option = FILE_SAVE_THEME_AS;
1077 			file_dialog->clear_filters();
1078 			file_dialog->add_filter("*.tet");
1079 			file_dialog->set_current_path(EditorSettings::get_singleton()->get_settings_path() + "/text_editor_themes/" + EditorSettings::get_singleton()->get("text_editor/color_theme"));
1080 			file_dialog->popup_centered_ratio();
1081 			file_dialog->set_title(TTR("Save Theme As.."));
1082 		} break;
1083 		case SEARCH_HELP: {
1084 
1085 			help_search_dialog->popup();
1086 		} break;
1087 		case SEARCH_CLASSES: {
1088 
1089 			String current;
1090 
1091 			if (tab_container->get_tab_count() > 0) {
1092 				EditorHelp *eh = tab_container->get_child(tab_container->get_current_tab())->cast_to<EditorHelp>();
1093 				if (eh) {
1094 					current = eh->get_class_name();
1095 				}
1096 			}
1097 
1098 			help_index->popup();
1099 
1100 			if (current != "") {
1101 				help_index->call_deferred("select_class", current);
1102 			}
1103 		} break;
1104 		case SEARCH_WEBSITE: {
1105 
1106 			OS::get_singleton()->shell_open("http://docs.godotengine.org/");
1107 		} break;
1108 
1109 		case WINDOW_NEXT: {
1110 
1111 			_history_forward();
1112 		} break;
1113 		case WINDOW_PREV: {
1114 			_history_back();
1115 		} break;
1116 		case DEBUG_SHOW: {
1117 			if (debugger) {
1118 				bool visible = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW));
1119 				debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW), !visible);
1120 				if (visible)
1121 					debugger->hide();
1122 				else
1123 					debugger->show();
1124 			}
1125 		} break;
1126 		case DEBUG_SHOW_KEEP_OPEN: {
1127 			bool visible = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN));
1128 			if (debugger)
1129 				debugger->set_hide_on_stop(visible);
1130 			debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(DEBUG_SHOW_KEEP_OPEN), !visible);
1131 		} break;
1132 	}
1133 
1134 	int selected = tab_container->get_current_tab();
1135 	if (selected < 0 || selected >= tab_container->get_child_count())
1136 		return;
1137 
1138 	ScriptTextEditor *current = tab_container->get_child(selected)->cast_to<ScriptTextEditor>();
1139 	if (current) {
1140 
1141 		switch (p_option) {
1142 			case FILE_NEW: {
1143 				script_create_dialog->config("Node", ".gd");
1144 				script_create_dialog->popup_centered(Size2(300, 300) * EDSCALE);
1145 			} break;
1146 			case FILE_SAVE: {
1147 
1148 				if (_test_script_times_on_disk())
1149 					return;
1150 
1151 				if (trim_trailing_whitespace_on_save) {
1152 					_trim_trailing_whitespace(current->get_text_edit());
1153 				}
1154 				editor->save_resource(current->get_edited_script());
1155 
1156 			} break;
1157 			case FILE_SAVE_AS: {
1158 
1159 				if (trim_trailing_whitespace_on_save) {
1160 					_trim_trailing_whitespace(current->get_text_edit());
1161 				}
1162 				editor->push_item(current->get_edited_script()->cast_to<Object>());
1163 				editor->save_resource_as(current->get_edited_script());
1164 
1165 			} break;
1166 			case EDIT_UNDO: {
1167 				current->get_text_edit()->undo();
1168 				current->get_text_edit()->call_deferred("grab_focus");
1169 			} break;
1170 			case EDIT_REDO: {
1171 				current->get_text_edit()->redo();
1172 				current->get_text_edit()->call_deferred("grab_focus");
1173 			} break;
1174 			case EDIT_CUT: {
1175 
1176 				current->get_text_edit()->cut();
1177 				current->get_text_edit()->call_deferred("grab_focus");
1178 			} break;
1179 			case EDIT_UPPERCASE: {
1180 
1181 				current->get_text_edit()->convert_case(current->get_text_edit()->UPPERCASE);
1182 				current->get_text_edit()->call_deferred("grab_focus");
1183 			} break;
1184 			case EDIT_LOWERCASE: {
1185 
1186 				current->get_text_edit()->convert_case(current->get_text_edit()->LOWERCASE);
1187 				current->get_text_edit()->call_deferred("grab_focus");
1188 			} break;
1189 			case EDIT_COPY: {
1190 				current->get_text_edit()->copy();
1191 				current->get_text_edit()->call_deferred("grab_focus");
1192 
1193 			} break;
1194 			case EDIT_PASTE: {
1195 				current->get_text_edit()->paste();
1196 				current->get_text_edit()->call_deferred("grab_focus");
1197 
1198 			} break;
1199 			case EDIT_SELECT_ALL: {
1200 
1201 				current->get_text_edit()->select_all();
1202 				current->get_text_edit()->call_deferred("grab_focus");
1203 
1204 			} break;
1205 			case EDIT_MOVE_LINE_UP: {
1206 
1207 				TextEdit *tx = current->get_text_edit();
1208 				Ref<Script> scr = current->get_edited_script();
1209 				if (scr.is_null())
1210 					return;
1211 
1212 				tx->begin_complex_operation();
1213 				if (tx->is_selection_active()) {
1214 					int from_line = tx->get_selection_from_line();
1215 					int from_col = tx->get_selection_from_column();
1216 					int to_line = tx->get_selection_to_line();
1217 					int to_column = tx->get_selection_to_column();
1218 
1219 					for (int i = from_line; i <= to_line; i++) {
1220 						int line_id = i;
1221 						int next_id = i - 1;
1222 
1223 						if (line_id == 0 || next_id < 0)
1224 							return;
1225 
1226 						swap_lines(tx, line_id, next_id);
1227 					}
1228 					int from_line_up = from_line > 0 ? from_line - 1 : from_line;
1229 					int to_line_up = to_line > 0 ? to_line - 1 : to_line;
1230 					tx->select(from_line_up, from_col, to_line_up, to_column);
1231 				} else {
1232 					int line_id = tx->cursor_get_line();
1233 					int next_id = line_id - 1;
1234 
1235 					if (line_id == 0 || next_id < 0)
1236 						return;
1237 
1238 					swap_lines(tx, line_id, next_id);
1239 				}
1240 				tx->end_complex_operation();
1241 				tx->update();
1242 
1243 			} break;
1244 			case EDIT_MOVE_LINE_DOWN: {
1245 
1246 				TextEdit *tx = current->get_text_edit();
1247 				Ref<Script> scr = current->get_edited_script();
1248 				if (scr.is_null())
1249 					return;
1250 
1251 				tx->begin_complex_operation();
1252 				if (tx->is_selection_active()) {
1253 					int from_line = tx->get_selection_from_line();
1254 					int from_col = tx->get_selection_from_column();
1255 					int to_line = tx->get_selection_to_line();
1256 					int to_column = tx->get_selection_to_column();
1257 
1258 					for (int i = to_line; i >= from_line; i--) {
1259 						int line_id = i;
1260 						int next_id = i + 1;
1261 
1262 						if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
1263 							return;
1264 
1265 						swap_lines(tx, line_id, next_id);
1266 					}
1267 					int from_line_down = from_line < tx->get_line_count() ? from_line + 1 : from_line;
1268 					int to_line_down = to_line < tx->get_line_count() ? to_line + 1 : to_line;
1269 					tx->select(from_line_down, from_col, to_line_down, to_column);
1270 				} else {
1271 					int line_id = tx->cursor_get_line();
1272 					int next_id = line_id + 1;
1273 
1274 					if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
1275 						return;
1276 
1277 					swap_lines(tx, line_id, next_id);
1278 				}
1279 				tx->end_complex_operation();
1280 				tx->update();
1281 
1282 			} break;
1283 			case EDIT_INDENT_LEFT: {
1284 
1285 				TextEdit *tx = current->get_text_edit();
1286 				Ref<Script> scr = current->get_edited_script();
1287 				if (scr.is_null())
1288 					return;
1289 
1290 				tx->begin_complex_operation();
1291 				if (tx->is_selection_active()) {
1292 					tx->indent_selection_left();
1293 				} else {
1294 					int begin = tx->cursor_get_line();
1295 					String line_text = tx->get_line(begin);
1296 					// begins with tab
1297 					if (line_text.begins_with("\t")) {
1298 						line_text = line_text.substr(1, line_text.length());
1299 						tx->set_line(begin, line_text);
1300 					}
1301 					// begins with 4 spaces
1302 					else if (line_text.begins_with("    ")) {
1303 						line_text = line_text.substr(4, line_text.length());
1304 						tx->set_line(begin, line_text);
1305 					}
1306 				}
1307 				tx->end_complex_operation();
1308 				tx->update();
1309 				//tx->deselect();
1310 
1311 			} break;
1312 			case EDIT_INDENT_RIGHT: {
1313 
1314 				TextEdit *tx = current->get_text_edit();
1315 				Ref<Script> scr = current->get_edited_script();
1316 				if (scr.is_null())
1317 					return;
1318 
1319 				tx->begin_complex_operation();
1320 				if (tx->is_selection_active()) {
1321 					tx->indent_selection_right();
1322 				} else {
1323 					int begin = tx->cursor_get_line();
1324 					String line_text = tx->get_line(begin);
1325 					line_text = '\t' + line_text;
1326 					tx->set_line(begin, line_text);
1327 				}
1328 				tx->end_complex_operation();
1329 				tx->update();
1330 				//tx->deselect();
1331 
1332 			} break;
1333 			case EDIT_CLONE_DOWN: {
1334 
1335 				TextEdit *tx = current->get_text_edit();
1336 				Ref<Script> scr = current->get_edited_script();
1337 				if (scr.is_null())
1338 					return;
1339 
1340 				int from_line = tx->cursor_get_line();
1341 				int to_line = tx->cursor_get_line();
1342 				int column = tx->cursor_get_column();
1343 
1344 				if (tx->is_selection_active()) {
1345 					from_line = tx->get_selection_from_line();
1346 					to_line = tx->get_selection_to_line();
1347 					column = tx->cursor_get_column();
1348 				}
1349 				int next_line = to_line + 1;
1350 
1351 				tx->begin_complex_operation();
1352 				for (int i = from_line; i <= to_line; i++) {
1353 
1354 					if (i >= tx->get_line_count() - 1) {
1355 						tx->set_line(i, tx->get_line(i) + "\n");
1356 					}
1357 					String line_clone = tx->get_line(i);
1358 					tx->insert_at(line_clone, next_line);
1359 					next_line++;
1360 				}
1361 
1362 				tx->cursor_set_column(column);
1363 				if (tx->is_selection_active()) {
1364 					tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
1365 				}
1366 
1367 				tx->end_complex_operation();
1368 				tx->update();
1369 
1370 			} break;
1371 			case EDIT_TOGGLE_COMMENT: {
1372 
1373 				TextEdit *tx = current->get_text_edit();
1374 				Ref<Script> scr = current->get_edited_script();
1375 				if (scr.is_null())
1376 					return;
1377 
1378 				tx->begin_complex_operation();
1379 				if (tx->is_selection_active()) {
1380 					int begin = tx->get_selection_from_line();
1381 					int end = tx->get_selection_to_line();
1382 
1383 					// End of selection ends on the first column of the last line, ignore it.
1384 					if (tx->get_selection_to_column() == 0)
1385 						end -= 1;
1386 
1387 					for (int i = begin; i <= end; i++) {
1388 						String line_text = tx->get_line(i);
1389 
1390 						if (line_text.begins_with("#"))
1391 							line_text = line_text.substr(1, line_text.length());
1392 						else
1393 							line_text = "#" + line_text;
1394 						tx->set_line(i, line_text);
1395 					}
1396 				} else {
1397 					int begin = tx->cursor_get_line();
1398 					String line_text = tx->get_line(begin);
1399 
1400 					if (line_text.begins_with("#"))
1401 						line_text = line_text.substr(1, line_text.length());
1402 					else
1403 						line_text = "#" + line_text;
1404 					tx->set_line(begin, line_text);
1405 				}
1406 				tx->end_complex_operation();
1407 				tx->update();
1408 				//tx->deselect();
1409 
1410 			} break;
1411 			case EDIT_COMPLETE: {
1412 
1413 				current->get_text_edit()->query_code_comple();
1414 
1415 			} break;
1416 			case EDIT_AUTO_INDENT: {
1417 
1418 				TextEdit *te = current->get_text_edit();
1419 				String text = te->get_text();
1420 				Ref<Script> scr = current->get_edited_script();
1421 				if (scr.is_null())
1422 					return;
1423 				int begin, end;
1424 				if (te->is_selection_active()) {
1425 					begin = te->get_selection_from_line();
1426 					end = te->get_selection_to_line();
1427 				} else {
1428 					begin = 0;
1429 					end = te->get_line_count() - 1;
1430 				}
1431 				scr->get_language()->auto_indent_code(text, begin, end);
1432 				te->set_text(text);
1433 
1434 			} break;
1435 			case FILE_TOOL_RELOAD:
1436 			case FILE_TOOL_RELOAD_SOFT: {
1437 
1438 				TextEdit *te = current->get_text_edit();
1439 				Ref<Script> scr = current->get_edited_script();
1440 				if (scr.is_null())
1441 					return;
1442 				scr->set_source_code(te->get_text());
1443 				bool soft = p_option == FILE_TOOL_RELOAD_SOFT || scr->get_instance_base_type() == "EditorPlugin"; //always soft-reload editor plugins
1444 
1445 				scr->get_language()->reload_tool_script(scr, soft);
1446 			} break;
1447 			case EDIT_TRIM_TRAILING_WHITESAPCE: {
1448 				_trim_trailing_whitespace(current->get_text_edit());
1449 			} break;
1450 			case SEARCH_FIND: {
1451 
1452 				current->get_find_replace_bar()->popup_search();
1453 			} break;
1454 			case SEARCH_FIND_NEXT: {
1455 
1456 				current->get_find_replace_bar()->search_next();
1457 			} break;
1458 			case SEARCH_FIND_PREV: {
1459 
1460 				current->get_find_replace_bar()->search_prev();
1461 			} break;
1462 			case SEARCH_REPLACE: {
1463 
1464 				current->get_find_replace_bar()->popup_replace();
1465 			} break;
1466 			case SEARCH_LOCATE_FUNCTION: {
1467 
1468 				if (!current)
1469 					return;
1470 				quick_open->popup(current->get_functions());
1471 			} break;
1472 			case SEARCH_GOTO_LINE: {
1473 
1474 				goto_line_dialog->popup_find_line(current->get_text_edit());
1475 			} break;
1476 			case DEBUG_TOGGLE_BREAKPOINT: {
1477 				int line = current->get_text_edit()->cursor_get_line();
1478 				bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
1479 				current->get_text_edit()->set_line_as_breakpoint(line, dobreak);
1480 				get_debugger()->set_breakpoint(current->get_edited_script()->get_path(), line + 1, dobreak);
1481 			} break;
1482 			case DEBUG_REMOVE_ALL_BREAKPOINTS: {
1483 				List<int> bpoints;
1484 				current->get_text_edit()->get_breakpoints(&bpoints);
1485 
1486 				for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
1487 					int line = E->get();
1488 					bool dobreak = !current->get_text_edit()->is_line_set_as_breakpoint(line);
1489 					current->get_text_edit()->set_line_as_breakpoint(line, dobreak);
1490 					get_debugger()->set_breakpoint(current->get_edited_script()->get_path(), line + 1, dobreak);
1491 				}
1492 			}
1493 			case DEBUG_GOTO_NEXT_BREAKPOINT: {
1494 				List<int> bpoints;
1495 				current->get_text_edit()->get_breakpoints(&bpoints);
1496 				if (bpoints.size() <= 0) {
1497 					return;
1498 				}
1499 
1500 				int line = current->get_text_edit()->cursor_get_line();
1501 				// wrap around
1502 				if (line >= bpoints[bpoints.size() - 1]) {
1503 					current->get_text_edit()->cursor_set_line(bpoints[0]);
1504 				} else {
1505 					for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
1506 						int bline = E->get();
1507 						if (bline > line) {
1508 							current->get_text_edit()->cursor_set_line(bline);
1509 							return;
1510 						}
1511 					}
1512 				}
1513 
1514 			} break;
1515 			case DEBUG_GOTO_PREV_BREAKPOINT: {
1516 				List<int> bpoints;
1517 				current->get_text_edit()->get_breakpoints(&bpoints);
1518 				if (bpoints.size() <= 0) {
1519 					return;
1520 				}
1521 
1522 				int line = current->get_text_edit()->cursor_get_line();
1523 				// wrap around
1524 				if (line <= bpoints[0]) {
1525 					current->get_text_edit()->cursor_set_line(bpoints[bpoints.size() - 1]);
1526 				} else {
1527 					for (List<int>::Element *E = bpoints.back(); E; E = E->prev()) {
1528 						int bline = E->get();
1529 						if (bline < line) {
1530 							current->get_text_edit()->cursor_set_line(bline);
1531 							return;
1532 						}
1533 					}
1534 				}
1535 
1536 			} break;
1537 			case DEBUG_NEXT: {
1538 
1539 				if (debugger)
1540 					debugger->debug_next();
1541 			} break;
1542 			case DEBUG_STEP: {
1543 
1544 				if (debugger)
1545 					debugger->debug_step();
1546 
1547 			} break;
1548 			case DEBUG_BREAK: {
1549 
1550 				if (debugger)
1551 					debugger->debug_break();
1552 
1553 			} break;
1554 			case DEBUG_CONTINUE: {
1555 
1556 				if (debugger)
1557 					debugger->debug_continue();
1558 
1559 			} break;
1560 			case HELP_CONTEXTUAL: {
1561 				String text = current->get_text_edit()->get_selection_text();
1562 				if (text == "")
1563 					text = current->get_text_edit()->get_word_under_cursor();
1564 				if (text != "")
1565 					help_search_dialog->popup(text);
1566 			} break;
1567 			case FILE_CLOSE: {
1568 				if (current->get_text_edit()->get_version() != current->get_text_edit()->get_saved_version()) {
1569 					erase_tab_confirm->set_text("Close and save changes?\n\"" + current->get_name() + "\"");
1570 					erase_tab_confirm->popup_centered_minsize();
1571 				} else {
1572 					_close_current_tab();
1573 				}
1574 			} break;
1575 			case FILE_CLOSE_OTHERS: {
1576 				_close_other_tabs(selected);
1577 			} break;
1578 			case FILE_CLOSE_ALL: {
1579 				_close_all_tab(-1);
1580 			} break;
1581 			case FILE_COPY_SCRIPT_PATH: {
1582 				_copy_script_path();
1583 			} break;
1584 			case CLOSE_DOCS: {
1585 				_close_docs_tab();
1586 			} break;
1587 			case WINDOW_MOVE_LEFT: {
1588 
1589 				if (tab_container->get_current_tab() > 0) {
1590 					tab_container->call_deferred("set_current_tab", tab_container->get_current_tab() - 1);
1591 					script_list->call_deferred("select", tab_container->get_current_tab() - 1);
1592 					tab_container->move_child(current, tab_container->get_current_tab() - 1);
1593 					_update_script_names();
1594 				}
1595 			} break;
1596 			case WINDOW_MOVE_RIGHT: {
1597 
1598 				if (tab_container->get_current_tab() < tab_container->get_child_count() - 1) {
1599 					tab_container->call_deferred("set_current_tab", tab_container->get_current_tab() + 1);
1600 					script_list->call_deferred("select", tab_container->get_current_tab() + 1);
1601 					tab_container->move_child(current, tab_container->get_current_tab() + 1);
1602 					_update_script_names();
1603 				}
1604 
1605 			} break;
1606 
1607 			default: {
1608 
1609 				if (p_option >= WINDOW_SELECT_BASE) {
1610 
1611 					tab_container->set_current_tab(p_option - WINDOW_SELECT_BASE);
1612 					script_list->select(p_option - WINDOW_SELECT_BASE);
1613 				}
1614 			}
1615 		}
1616 	} else {
1617 
1618 		EditorHelp *help = tab_container->get_current_tab_control()->cast_to<EditorHelp>();
1619 		if (help) {
1620 
1621 			switch (p_option) {
1622 
1623 				case SEARCH_FIND: {
1624 					help->popup_search();
1625 				} break;
1626 				case SEARCH_FIND_NEXT: {
1627 					help->search_again();
1628 				} break;
1629 				case FILE_CLOSE_OTHERS: {
1630 					_close_other_tabs(selected);
1631 				} break;
1632 				case FILE_CLOSE_ALL: {
1633 					_close_all_tab(-1);
1634 				} break;
1635 				case FILE_COPY_SCRIPT_PATH: {
1636 					_copy_script_path();
1637 				} break;
1638 				case FILE_CLOSE: {
1639 					_close_current_tab();
1640 				} break;
1641 				case CLOSE_DOCS: {
1642 					_close_docs_tab();
1643 				} break;
1644 			}
1645 		}
1646 	}
1647 }
1648 
_tab_changed(int p_which)1649 void ScriptEditor::_tab_changed(int p_which) {
1650 
1651 	ensure_select_current();
1652 }
1653 
_notification(int p_what)1654 void ScriptEditor::_notification(int p_what) {
1655 
1656 	if (p_what == NOTIFICATION_ENTER_TREE) {
1657 
1658 		editor->connect("play_pressed", this, "_editor_play");
1659 		editor->connect("pause_pressed", this, "_editor_pause");
1660 		editor->connect("stop_pressed", this, "_editor_stop");
1661 		editor->connect("script_add_function_request", this, "_add_callback");
1662 		editor->connect("resource_saved", this, "_res_saved_callback");
1663 		script_list->connect("item_selected", this, "_script_selected");
1664 		script_list->connect("item_rmb_selected", this, "_script_rmb_selected");
1665 		script_list_menu->connect("item_pressed", this, "_menu_option");
1666 		members_overview->connect("item_selected", this, "_members_overview_selected");
1667 		script_split->connect("dragged", this, "_script_split_dragged");
1668 		autosave_timer->connect("timeout", this, "_autosave_scripts");
1669 		{
1670 			float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
1671 			if (autosave_time > 0) {
1672 				autosave_timer->set_wait_time(autosave_time);
1673 				autosave_timer->start();
1674 			} else {
1675 				autosave_timer->stop();
1676 			}
1677 		}
1678 
1679 		EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
1680 		help_search->set_icon(get_icon("Help", "EditorIcons"));
1681 		site_search->set_icon(get_icon("Godot", "EditorIcons"));
1682 		class_search->set_icon(get_icon("ClassList", "EditorIcons"));
1683 
1684 		script_forward->set_icon(get_icon("Forward", "EditorIcons"));
1685 		script_back->set_icon(get_icon("Back", "EditorIcons"));
1686 	}
1687 
1688 	if (p_what == NOTIFICATION_READY) {
1689 
1690 		get_tree()->connect("tree_changed", this, "_tree_changed");
1691 		editor->connect("request_help", this, "_request_help");
1692 		editor->connect("request_help_search", this, "_help_search");
1693 	}
1694 
1695 	if (p_what == NOTIFICATION_EXIT_TREE) {
1696 
1697 		editor->disconnect("play_pressed", this, "_editor_play");
1698 		editor->disconnect("pause_pressed", this, "_editor_pause");
1699 		editor->disconnect("stop_pressed", this, "_editor_stop");
1700 	}
1701 
1702 	if (p_what == MainLoop::NOTIFICATION_WM_FOCUS_IN) {
1703 
1704 		_test_script_times_on_disk();
1705 		_update_modified_scripts_for_external_editor();
1706 	}
1707 
1708 	if (p_what == NOTIFICATION_PROCESS) {
1709 	}
1710 }
1711 
close_builtin_scripts_from_scene(const String & p_scene)1712 void ScriptEditor::close_builtin_scripts_from_scene(const String &p_scene) {
1713 
1714 	for (int i = 0; i < tab_container->get_child_count(); i++) {
1715 
1716 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
1717 
1718 		if (ste) {
1719 
1720 			Ref<Script> script = ste->get_edited_script();
1721 			if (!script.is_valid())
1722 				continue;
1723 
1724 			if (script->get_path().find("::") != -1 && script->get_path().begins_with(p_scene)) { //is an internal script and belongs to scene being closed
1725 				_close_tab(i);
1726 				i--;
1727 			}
1728 		}
1729 	}
1730 }
1731 
edited_scene_changed()1732 void ScriptEditor::edited_scene_changed() {
1733 
1734 	_update_modified_scripts_for_external_editor();
1735 }
1736 
_find_node_with_script(const Node * p_node,const RefPtr & p_script)1737 static const Node *_find_node_with_script(const Node *p_node, const RefPtr &p_script) {
1738 
1739 	if (p_node->get_script() == p_script)
1740 		return p_node;
1741 
1742 	for (int i = 0; i < p_node->get_child_count(); i++) {
1743 
1744 		const Node *result = _find_node_with_script(p_node->get_child(i), p_script);
1745 		if (result)
1746 			return result;
1747 	}
1748 
1749 	return NULL;
1750 }
1751 
get_state() const1752 Dictionary ScriptEditor::get_state() const {
1753 
1754 	//	apply_scripts();
1755 
1756 	Dictionary state;
1757 #if 0
1758 	Array paths;
1759 	int open=-1;
1760 
1761 	for(int i=0;i<tab_container->get_child_count();i++) {
1762 
1763 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
1764 		if (!ste)
1765 			continue;
1766 
1767 
1768 		Ref<Script> script = ste->get_edited_script();
1769 		if (script->get_path()!="" && script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
1770 
1771 			paths.push_back(script->get_path());
1772 		} else {
1773 
1774 
1775 			const Node *owner = _find_node_with_script(get_tree()->get_root(),script.get_ref_ptr());
1776 			if (owner)
1777 				paths.push_back(owner->get_path());
1778 
1779 		}
1780 
1781 		if (i==tab_container->get_current_tab())
1782 			open=i;
1783 	}
1784 
1785 	if (paths.size())
1786 		state["sources"]=paths;
1787 	if (open!=-1)
1788 		state["current"]=open;
1789 
1790 #endif
1791 	return state;
1792 }
set_state(const Dictionary & p_state)1793 void ScriptEditor::set_state(const Dictionary &p_state) {
1794 
1795 #if 0
1796 	print_line("attempt set state: "+String(Variant(p_state)));
1797 
1798 	if (!p_state.has("sources"))
1799 		return; //bleh
1800 
1801 	Array sources = p_state["sources"];
1802 	for(int i=0;i<sources.size();i++) {
1803 
1804 		Variant source=sources[i];
1805 
1806 		Ref<Script> script;
1807 
1808 		if (source.get_type()==Variant::NODE_PATH) {
1809 
1810 
1811 			Node *owner=get_tree()->get_root()->get_node(source);
1812 			if (!owner)
1813 				continue;
1814 
1815 			script = owner->get_script();
1816 		} else if (source.get_type()==Variant::STRING) {
1817 
1818 
1819 			script = ResourceLoader::load(source,"Script");
1820 		}
1821 
1822 
1823 		if (script.is_null()) //ah well..
1824 			continue;
1825 
1826 		editor->call("_resource_selected",script);
1827 	}
1828 
1829 	if (p_state.has("current")) {
1830 		tab_container->set_current_tab(p_state["current"]);
1831 	}
1832 #endif
1833 }
clear()1834 void ScriptEditor::clear() {
1835 #if 0
1836 	List<ScriptTextEditor*> stes;
1837 	for(int i=0;i<tab_container->get_child_count();i++) {
1838 
1839 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
1840 		if (!ste)
1841 			continue;
1842 		stes.push_back(ste);
1843 
1844 	}
1845 
1846 	while(stes.size()) {
1847 
1848 		memdelete(stes.front()->get());
1849 		stes.pop_front();
1850 	}
1851 
1852 	int idx = tab_container->get_current_tab();
1853 	if (idx>=tab_container->get_child_count())
1854 		idx=tab_container->get_child_count()-1;
1855 	if (idx>=0) {
1856 		tab_container->set_current_tab(idx);
1857 		script_list->select( script_list->find_metadata(idx) );
1858 	}
1859 
1860 #endif
1861 }
1862 
get_breakpoints(List<String> * p_breakpoints)1863 void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
1864 
1865 	for (int i = 0; i < tab_container->get_child_count(); i++) {
1866 
1867 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
1868 		if (!ste)
1869 			continue;
1870 
1871 		List<int> bpoints;
1872 		ste->get_text_edit()->get_breakpoints(&bpoints);
1873 
1874 		Ref<Script> script = ste->get_edited_script();
1875 		String base = script->get_path();
1876 		ERR_CONTINUE(base.begins_with("local://") || base == "");
1877 
1878 		for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
1879 
1880 			p_breakpoints->push_back(base + ":" + itos(E->get() + 1));
1881 		}
1882 	}
1883 }
1884 
ensure_focus_current()1885 void ScriptEditor::ensure_focus_current() {
1886 
1887 	if (!is_inside_tree())
1888 		return;
1889 
1890 	int cidx = tab_container->get_current_tab();
1891 	if (cidx < 0 || cidx >= tab_container->get_tab_count())
1892 		;
1893 	Control *c = tab_container->get_child(cidx)->cast_to<Control>();
1894 	if (!c)
1895 		return;
1896 	ScriptTextEditor *ste = c->cast_to<ScriptTextEditor>();
1897 	if (!ste)
1898 		return;
1899 	ste->get_text_edit()->grab_focus();
1900 }
1901 
_members_overview_selected(int p_idx)1902 void ScriptEditor::_members_overview_selected(int p_idx) {
1903 	int line = members_overview->get_item_metadata(p_idx);
1904 
1905 	_goto_script_line2(line);
1906 
1907 	Node *current = tab_container->get_child(tab_container->get_current_tab());
1908 	ScriptTextEditor *ste = current->cast_to<ScriptTextEditor>();
1909 	if (ste) {
1910 		ste->get_text_edit()->set_v_scroll(line);
1911 	}
1912 	ensure_focus_current();
1913 }
1914 
_script_selected(int p_idx)1915 void ScriptEditor::_script_selected(int p_idx) {
1916 
1917 	grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(1); //amazing hack, simply amazing
1918 
1919 	_go_to_tab(script_list->get_item_metadata(p_idx));
1920 	grab_focus_block = false;
1921 }
1922 
_script_rmb_selected(int p_idx,const Vector2 & p_pos)1923 void ScriptEditor::_script_rmb_selected(int p_idx, const Vector2 &p_pos) {
1924 
1925 	script_list_menu->clear();
1926 	script_list_menu->set_size(Size2(1, 1));
1927 	if (p_idx >= 0) {
1928 		script_list_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save"), FILE_SAVE);
1929 		script_list_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save_as"), FILE_SAVE_AS);
1930 		script_list_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save_all"), FILE_SAVE_ALL);
1931 		script_list_menu->add_separator();
1932 		script_list_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_file"), FILE_CLOSE);
1933 		script_list_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_other_tabs"), FILE_CLOSE_OTHERS);
1934 		script_list_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_all"), FILE_CLOSE_ALL);
1935 		script_list_menu->add_separator();
1936 		script_list_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/copy_script_path"), FILE_COPY_SCRIPT_PATH);
1937 	}
1938 
1939 	script_list_menu->set_pos(script_list->get_global_pos() + p_pos);
1940 	script_list_menu->popup();
1941 }
1942 
ensure_select_current()1943 void ScriptEditor::ensure_select_current() {
1944 
1945 	if (tab_container->get_child_count() && tab_container->get_current_tab() >= 0) {
1946 
1947 		Node *current = tab_container->get_child(tab_container->get_current_tab());
1948 
1949 		ScriptTextEditor *ste = current->cast_to<ScriptTextEditor>();
1950 		if (ste) {
1951 
1952 			Ref<Script> script = ste->get_edited_script();
1953 
1954 			if (!grab_focus_block && is_visible())
1955 				ste->get_text_edit()->grab_focus();
1956 
1957 			edit_menu->show();
1958 			search_menu->show();
1959 			script_search_menu->hide();
1960 		}
1961 
1962 		EditorHelp *eh = current->cast_to<EditorHelp>();
1963 
1964 		if (eh) {
1965 			edit_menu->hide();
1966 			search_menu->hide();
1967 			script_search_menu->show();
1968 		}
1969 	}
1970 }
1971 
_find_scripts(Node * p_base,Node * p_current,Set<Ref<Script>> & used)1972 void ScriptEditor::_find_scripts(Node *p_base, Node *p_current, Set<Ref<Script> > &used) {
1973 	if (p_current != p_base && p_current->get_owner() != p_base)
1974 		return;
1975 
1976 	if (p_current->get_script_instance()) {
1977 		Ref<Script> scr = p_current->get_script();
1978 		if (scr.is_valid())
1979 			used.insert(scr);
1980 	}
1981 
1982 	for (int i = 0; i < p_current->get_child_count(); i++) {
1983 		_find_scripts(p_base, p_current->get_child(i), used);
1984 	}
1985 }
1986 
1987 struct _ScriptEditorItemData {
1988 
1989 	String name;
1990 	String sort_key;
1991 	Ref<Texture> icon;
1992 	int index;
1993 	String tooltip;
1994 	bool used;
1995 	int category;
1996 
operator <_ScriptEditorItemData1997 	bool operator<(const _ScriptEditorItemData &id) const {
1998 
1999 		return category == id.category ? sort_key < id.sort_key : category < id.category;
2000 	}
2001 };
2002 
_update_members_overview_visibility()2003 void ScriptEditor::_update_members_overview_visibility() {
2004 	int selected = tab_container->get_current_tab();
2005 	if (selected < 0 || selected >= tab_container->get_child_count()) {
2006 		return;
2007 	}
2008 
2009 	if (members_overview_enabled) {
2010 		members_overview->set_hidden(false);
2011 	} else {
2012 		members_overview->set_hidden(true);
2013 	}
2014 }
2015 
_update_members_overview()2016 void ScriptEditor::_update_members_overview() {
2017 	members_overview->clear();
2018 
2019 	int selected = tab_container->get_current_tab();
2020 	if (selected < 0 || selected >= tab_container->get_child_count()) {
2021 		return;
2022 	}
2023 
2024 	if (tab_container->get_child_count() <= 0)
2025 		return;
2026 
2027 	Node *current = tab_container->get_child(tab_container->get_current_tab());
2028 	ScriptTextEditor *ste = tab_container->get_child(tab_container->get_current_tab())->cast_to<ScriptTextEditor>();
2029 	if (!ste) {
2030 		return;
2031 	}
2032 
2033 	Vector<String> functions = ste->get_functions();
2034 	for (int i = 0; i < functions.size(); i++) {
2035 		members_overview->add_item(functions[i].get_slice(":", 0));
2036 		members_overview->set_item_metadata(i, functions[i].get_slice(":", 1).to_int() - 1);
2037 	}
2038 }
2039 
_update_script_colors()2040 void ScriptEditor::_update_script_colors() {
2041 
2042 	bool script_temperature_enabled = EditorSettings::get_singleton()->get("text_editor/script_temperature_enabled");
2043 	bool highlight_current = EditorSettings::get_singleton()->get("text_editor/highlight_current_script");
2044 
2045 	int hist_size = EditorSettings::get_singleton()->get("text_editor/script_temperature_history_size");
2046 	Color hot_color = EditorSettings::get_singleton()->get("text_editor/script_temperature_hot_color");
2047 	Color cold_color = EditorSettings::get_singleton()->get("text_editor/script_temperature_cold_color");
2048 
2049 	for (int i = 0; i < script_list->get_item_count(); i++) {
2050 
2051 		int c = script_list->get_item_metadata(i);
2052 		Node *n = tab_container->get_child(c);
2053 		if (!n)
2054 			continue;
2055 
2056 		script_list->set_item_custom_bg_color(i, Color(0, 0, 0, 0));
2057 
2058 		bool current = tab_container->get_current_tab() == c;
2059 		if (current && highlight_current) {
2060 			script_list->set_item_custom_bg_color(i, EditorSettings::get_singleton()->get("text_editor/current_script_background_color"));
2061 
2062 		} else if (script_temperature_enabled) {
2063 
2064 			if (!n->has_meta("__editor_pass")) {
2065 				continue;
2066 			}
2067 
2068 			int pass = n->get_meta("__editor_pass");
2069 			int h = edit_pass - pass;
2070 			if (h > hist_size) {
2071 				continue;
2072 			}
2073 			int non_zero_hist_size = (hist_size == 0) ? 1 : hist_size;
2074 			float v = Math::ease((edit_pass - pass) / float(non_zero_hist_size), 0.4);
2075 
2076 			script_list->set_item_custom_bg_color(i, hot_color.linear_interpolate(cold_color, v));
2077 		}
2078 	}
2079 }
2080 
_update_script_names()2081 void ScriptEditor::_update_script_names() {
2082 
2083 	if (restoring_layout)
2084 		return;
2085 
2086 	waiting_update_names = false;
2087 	Set<Ref<Script> > used;
2088 	Node *edited = EditorNode::get_singleton()->get_edited_scene();
2089 	if (edited) {
2090 		_find_scripts(edited, edited, used);
2091 	}
2092 
2093 	script_list->clear();
2094 	bool split_script_help = EditorSettings::get_singleton()->get("text_editor/group_help_pages");
2095 	ScriptSortBy sort_by = (ScriptSortBy)(int)EditorSettings::get_singleton()->get("text_editor/sort_scripts_by");
2096 	ScriptListName display_as = (ScriptListName)(int)EditorSettings::get_singleton()->get("text_editor/list_script_names_as");
2097 
2098 	Vector<_ScriptEditorItemData> sedata;
2099 
2100 	for (int i = 0; i < tab_container->get_child_count(); i++) {
2101 
2102 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
2103 		if (ste) {
2104 
2105 			String name = ste->get_name();
2106 			Ref<Texture> icon = ste->get_icon();
2107 			String path = ste->get_edited_script()->get_path();
2108 
2109 			_ScriptEditorItemData sd;
2110 			sd.icon = icon;
2111 			sd.name = name;
2112 			sd.tooltip = path;
2113 			sd.index = i;
2114 			sd.used = used.has(ste->get_edited_script());
2115 			sd.category = 0;
2116 
2117 			switch (sort_by) {
2118 				case SORT_BY_NAME: {
2119 					sd.sort_key = name.to_lower();
2120 				} break;
2121 				case SORT_BY_PATH: {
2122 					sd.sort_key = path;
2123 				} break;
2124 			}
2125 
2126 			switch (display_as) {
2127 				case DISPLAY_NAME: {
2128 					sd.name = name;
2129 				} break;
2130 				case DISPLAY_DIR_AND_NAME: {
2131 					if (!path.get_base_dir().get_file().empty()) {
2132 						sd.name = path.get_base_dir().get_file() + "/" + name;
2133 					} else {
2134 						sd.name = name;
2135 					}
2136 				} break;
2137 				case DISPLAY_FULL_PATH: {
2138 					sd.name = path;
2139 				} break;
2140 			}
2141 
2142 			sedata.push_back(sd);
2143 		}
2144 
2145 		EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
2146 		if (eh) {
2147 
2148 			String name = eh->get_class_name();
2149 			Ref<Texture> icon = get_icon("Help", "EditorIcons");
2150 			String tooltip = name + " Class Reference";
2151 
2152 			_ScriptEditorItemData sd;
2153 			sd.icon = icon;
2154 			sd.name = name;
2155 			sd.sort_key = name;
2156 			sd.tooltip = tooltip;
2157 			sd.index = i;
2158 			sd.used = false;
2159 			sd.category = split_script_help ? 1 : 0;
2160 			sedata.push_back(sd);
2161 		}
2162 	}
2163 
2164 	sedata.sort();
2165 
2166 	for (int i = 0; i < sedata.size(); i++) {
2167 
2168 		script_list->add_item(sedata[i].name, sedata[i].icon);
2169 		int index = script_list->get_item_count() - 1;
2170 		script_list->set_item_tooltip(index, sedata[i].tooltip);
2171 		script_list->set_item_metadata(index, sedata[i].index);
2172 		if (sedata[i].used) {
2173 
2174 			script_list->set_item_custom_bg_color(index, Color(88 / 255.0, 88 / 255.0, 60 / 255.0));
2175 		}
2176 		if (tab_container->get_current_tab() == sedata[i].index) {
2177 			script_list->select(index);
2178 			script_name_label->set_text(sedata[i].name);
2179 			script_icon->set_texture(sedata[i].icon);
2180 		}
2181 	}
2182 
2183 	_update_members_overview();
2184 	_update_script_colors();
2185 }
2186 
edit(const Ref<Script> & p_script)2187 void ScriptEditor::edit(const Ref<Script> &p_script) {
2188 
2189 	if (p_script.is_null())
2190 		return;
2191 
2192 	// refuse to open built-in if scene is not loaded
2193 
2194 	// see if already has it
2195 
2196 	bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
2197 
2198 	if (p_script->get_path().is_resource_file() && bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor"))) {
2199 
2200 		String path = EditorSettings::get_singleton()->get("external_editor/exec_path");
2201 		String flags = EditorSettings::get_singleton()->get("external_editor/exec_flags");
2202 		List<String> args;
2203 		flags = flags.strip_edges();
2204 		if (flags != String()) {
2205 			Vector<String> flagss = flags.split(" ", false);
2206 			for (int i = 0; i < flagss.size(); i++)
2207 				args.push_back(flagss[i]);
2208 		}
2209 
2210 		args.push_back(Globals::get_singleton()->globalize_path(p_script->get_path()));
2211 		Error err = OS::get_singleton()->execute(path, args, false);
2212 		if (err == OK)
2213 			return;
2214 		WARN_PRINT("Couldn't open external text editor, using internal");
2215 	}
2216 
2217 	for (int i = 0; i < tab_container->get_child_count(); i++) {
2218 
2219 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
2220 		if (!ste)
2221 			continue;
2222 
2223 		if (ste->get_edited_script() == p_script) {
2224 
2225 			if (open_dominant || !EditorNode::get_singleton()->is_changing_scene()) {
2226 				if (tab_container->get_current_tab() != i) {
2227 					_go_to_tab(i);
2228 					script_list->select(script_list->find_metadata(i));
2229 				}
2230 				if (is_visible())
2231 					ste->get_text_edit()->grab_focus();
2232 			}
2233 			return;
2234 		}
2235 	}
2236 
2237 	// doesn't have it, make a new one
2238 
2239 	ScriptTextEditor *ste = memnew(ScriptTextEditor);
2240 	ste->update_editor_settings();
2241 	ste->set_edited_script(p_script);
2242 	ste->get_text_edit()->set_tooltip_request_func(this, "_get_debug_tooltip", ste);
2243 	ste->get_text_edit()->set_callhint_settings(
2244 			EditorSettings::get_singleton()->get("text_editor/put_callhint_tooltip_below_current_line"),
2245 			EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset"));
2246 	ste->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled");
2247 	tab_container->add_child(ste);
2248 	_go_to_tab(tab_container->get_tab_count() - 1);
2249 
2250 	_update_script_names();
2251 	_save_layout();
2252 	ste->connect("name_changed", this, "_update_script_names");
2253 
2254 	//test for modification, maybe the script was not edited but was loaded
2255 
2256 	_test_script_times_on_disk(p_script);
2257 	_update_modified_scripts_for_external_editor(p_script);
2258 }
2259 
save_all_scripts()2260 void ScriptEditor::save_all_scripts() {
2261 
2262 	for (int i = 0; i < tab_container->get_child_count(); i++) {
2263 
2264 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
2265 		if (!ste)
2266 			continue;
2267 
2268 		if (trim_trailing_whitespace_on_save) {
2269 			_trim_trailing_whitespace(ste->get_text_edit());
2270 		}
2271 		if (ste->get_text_edit()->get_version() == ste->get_text_edit()->get_saved_version())
2272 			continue;
2273 
2274 		Ref<Script> script = ste->get_edited_script();
2275 		if (script.is_valid())
2276 			ste->apply_code();
2277 
2278 		if (script->get_path() != "" && script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
2279 			//external script, save it
2280 
2281 			editor->save_resource(script);
2282 			//ResourceSaver::save(script->get_path(),script);
2283 		}
2284 	}
2285 }
2286 
apply_scripts() const2287 void ScriptEditor::apply_scripts() const {
2288 
2289 	for (int i = 0; i < tab_container->get_child_count(); i++) {
2290 
2291 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
2292 		if (!ste)
2293 			continue;
2294 		ste->apply_code();
2295 	}
2296 }
2297 
_editor_play()2298 void ScriptEditor::_editor_play() {
2299 
2300 	debugger->start();
2301 	debug_menu->get_popup()->grab_focus();
2302 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
2303 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
2304 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), false);
2305 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
2306 }
2307 
_editor_pause()2308 void ScriptEditor::_editor_pause() {
2309 }
_editor_stop()2310 void ScriptEditor::_editor_stop() {
2311 
2312 	debugger->stop();
2313 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
2314 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
2315 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true);
2316 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
2317 }
2318 
_add_callback(Object * p_obj,const String & p_function,const StringArray & p_args)2319 void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const StringArray &p_args) {
2320 
2321 	//print_line("add callback! hohoho"); kinda sad to remove this
2322 	ERR_FAIL_COND(!p_obj);
2323 	Ref<Script> script = p_obj->get_script();
2324 	ERR_FAIL_COND(!script.is_valid());
2325 
2326 	editor->push_item(script.ptr());
2327 
2328 	for (int i = 0; i < tab_container->get_child_count(); i++) {
2329 
2330 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
2331 		if (!ste)
2332 			continue;
2333 		if (ste->get_edited_script() != script)
2334 			continue;
2335 
2336 		String code = ste->get_text_edit()->get_text();
2337 		int pos = script->get_language()->find_function(p_function, code);
2338 		if (pos == -1) {
2339 			//does not exist
2340 			ste->get_text_edit()->deselect();
2341 			pos = ste->get_text_edit()->get_line_count() + 2;
2342 			String func = script->get_language()->make_function("", p_function, p_args);
2343 			//code=code+func;
2344 			ste->get_text_edit()->cursor_set_line(pos + 1);
2345 			ste->get_text_edit()->cursor_set_column(1000000); //none shall be that big
2346 			ste->get_text_edit()->insert_text_at_cursor("\n\n" + func);
2347 		}
2348 
2349 		_go_to_tab(i);
2350 		ste->get_text_edit()->cursor_set_line(pos);
2351 		ste->get_text_edit()->cursor_set_column(1);
2352 
2353 		script_list->select(script_list->find_metadata(i));
2354 
2355 		break;
2356 	}
2357 }
2358 
_save_layout()2359 void ScriptEditor::_save_layout() {
2360 
2361 	if (restoring_layout) {
2362 		return;
2363 	}
2364 
2365 	editor->save_layout();
2366 }
2367 
_editor_settings_changed()2368 void ScriptEditor::_editor_settings_changed() {
2369 
2370 	trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/trim_trailing_whitespace_on_save");
2371 
2372 	members_overview_enabled = EditorSettings::get_singleton()->get("text_editor/show_members_overview");
2373 	_update_members_overview_visibility();
2374 
2375 	float autosave_time = EditorSettings::get_singleton()->get("text_editor/autosave_interval_secs");
2376 	if (autosave_time > 0) {
2377 		autosave_timer->set_wait_time(autosave_time);
2378 		autosave_timer->start();
2379 	} else {
2380 		autosave_timer->stop();
2381 	}
2382 
2383 	if (current_theme == "") {
2384 		current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
2385 	} else if (current_theme != EditorSettings::get_singleton()->get("text_editor/color_theme")) {
2386 		current_theme = EditorSettings::get_singleton()->get("text_editor/color_theme");
2387 		EditorSettings::get_singleton()->load_text_editor_theme();
2388 	}
2389 
2390 	for (int i = 0; i < tab_container->get_child_count(); i++) {
2391 
2392 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
2393 		if (!ste)
2394 			continue;
2395 
2396 		ste->update_editor_settings();
2397 	}
2398 	_update_script_colors();
2399 	_update_script_names();
2400 
2401 	ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save", true));
2402 }
2403 
_autosave_scripts()2404 void ScriptEditor::_autosave_scripts() {
2405 
2406 	save_all_scripts();
2407 }
2408 
_tree_changed()2409 void ScriptEditor::_tree_changed() {
2410 
2411 	if (waiting_update_names)
2412 		return;
2413 
2414 	waiting_update_names = true;
2415 	call_deferred("_update_script_names");
2416 }
2417 
_script_split_dragged(float)2418 void ScriptEditor::_script_split_dragged(float) {
2419 
2420 	_save_layout();
2421 }
2422 
_unhandled_input(const InputEvent & p_event)2423 void ScriptEditor::_unhandled_input(const InputEvent &p_event) {
2424 	if (p_event.key.pressed || !is_visible()) return;
2425 	if (ED_IS_SHORTCUT("script_editor/next_script", p_event)) {
2426 		int next_tab = script_list->get_current() + 1;
2427 		next_tab %= script_list->get_item_count();
2428 		_go_to_tab(script_list->get_item_metadata(next_tab));
2429 		_update_script_names();
2430 	}
2431 	if (ED_IS_SHORTCUT("script_editor/prev_script", p_event)) {
2432 		int next_tab = script_list->get_current() - 1;
2433 		next_tab = next_tab >= 0 ? next_tab : script_list->get_item_count() - 1;
2434 		_go_to_tab(script_list->get_item_metadata(next_tab));
2435 		_update_script_names();
2436 	}
2437 }
2438 
set_window_layout(Ref<ConfigFile> p_layout)2439 void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
2440 
2441 	if (!bool(EDITOR_DEF("text_editor/restore_scripts_on_load", true))) {
2442 		return;
2443 	}
2444 
2445 	if (!p_layout->has_section_key("ScriptEditor", "open_scripts") && !p_layout->has_section_key("ScriptEditor", "open_help"))
2446 		return;
2447 
2448 	Array scripts = p_layout->get_value("ScriptEditor", "open_scripts");
2449 	Array helps;
2450 	if (p_layout->has_section_key("ScriptEditor", "open_help"))
2451 		helps = p_layout->get_value("ScriptEditor", "open_help");
2452 
2453 	restoring_layout = true;
2454 
2455 	for (int i = 0; i < scripts.size(); i++) {
2456 
2457 		String path = scripts[i];
2458 		if (!FileAccess::exists(path))
2459 			continue;
2460 		Ref<Script> scr = ResourceLoader::load(path);
2461 		if (scr.is_valid()) {
2462 			edit(scr);
2463 		}
2464 	}
2465 
2466 	for (int i = 0; i < helps.size(); i++) {
2467 
2468 		String path = helps[i];
2469 		if (path == "") { // invalid, skip
2470 			continue;
2471 		}
2472 		_help_class_open(path);
2473 	}
2474 
2475 	for (int i = 0; i < tab_container->get_child_count(); i++) {
2476 		tab_container->get_child(i)->set_meta("__editor_pass", Variant());
2477 	}
2478 
2479 	if (p_layout->has_section_key("ScriptEditor", "split_offset")) {
2480 		script_split->set_split_offset(p_layout->get_value("ScriptEditor", "split_offset"));
2481 	}
2482 
2483 	restoring_layout = false;
2484 
2485 	_update_script_names();
2486 }
2487 
get_window_layout(Ref<ConfigFile> p_layout)2488 void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
2489 
2490 	Array scripts;
2491 	Array helps;
2492 
2493 	for (int i = 0; i < tab_container->get_child_count(); i++) {
2494 
2495 		ScriptTextEditor *ste = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
2496 		if (ste) {
2497 
2498 			String path = ste->get_edited_script()->get_path();
2499 			if (!path.is_resource_file())
2500 				continue;
2501 
2502 			scripts.push_back(path);
2503 		}
2504 
2505 		EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
2506 
2507 		if (eh) {
2508 
2509 			helps.push_back(eh->get_class_name());
2510 		}
2511 	}
2512 
2513 	p_layout->set_value("ScriptEditor", "open_scripts", scripts);
2514 	p_layout->set_value("ScriptEditor", "open_help", helps);
2515 	p_layout->set_value("ScriptEditor", "split_offset", script_split->get_split_offset());
2516 }
2517 
_help_class_open(const String & p_class)2518 void ScriptEditor::_help_class_open(const String &p_class) {
2519 
2520 	for (int i = 0; i < tab_container->get_child_count(); i++) {
2521 
2522 		EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
2523 
2524 		if (eh && eh->get_class_name() == p_class) {
2525 
2526 			_go_to_tab(i);
2527 			_update_script_names();
2528 			return;
2529 		}
2530 	}
2531 
2532 	EditorHelp *eh = memnew(EditorHelp);
2533 
2534 	eh->set_name(p_class);
2535 	tab_container->add_child(eh);
2536 	_go_to_tab(tab_container->get_tab_count() - 1);
2537 	eh->go_to_class(p_class, 0);
2538 	eh->connect("go_to_help", this, "_help_class_goto");
2539 	_update_script_names();
2540 	_save_layout();
2541 }
2542 
_help_class_goto(const String & p_desc)2543 void ScriptEditor::_help_class_goto(const String &p_desc) {
2544 
2545 	String cname = p_desc.get_slice(":", 1);
2546 
2547 	for (int i = 0; i < tab_container->get_child_count(); i++) {
2548 
2549 		EditorHelp *eh = tab_container->get_child(i)->cast_to<EditorHelp>();
2550 
2551 		if (eh && eh->get_class_name() == cname) {
2552 
2553 			_go_to_tab(i);
2554 			eh->go_to_help(p_desc);
2555 			_update_script_names();
2556 			return;
2557 		}
2558 	}
2559 
2560 	EditorHelp *eh = memnew(EditorHelp);
2561 
2562 	eh->set_name(cname);
2563 	tab_container->add_child(eh);
2564 	_go_to_tab(tab_container->get_tab_count() - 1);
2565 	eh->go_to_help(p_desc);
2566 	eh->connect("go_to_help", this, "_help_class_goto");
2567 	_update_script_names();
2568 	_save_layout();
2569 }
2570 
_update_history_pos(int p_new_pos)2571 void ScriptEditor::_update_history_pos(int p_new_pos) {
2572 
2573 	Node *n = tab_container->get_current_tab_control();
2574 
2575 	if (n->cast_to<ScriptTextEditor>()) {
2576 
2577 		history[history_pos].scroll_pos = n->cast_to<ScriptTextEditor>()->get_text_edit()->get_v_scroll();
2578 		history[history_pos].cursor_column = n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_column();
2579 		history[history_pos].cursor_row = n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_get_line();
2580 	}
2581 	if (n->cast_to<EditorHelp>()) {
2582 
2583 		history[history_pos].scroll_pos = n->cast_to<EditorHelp>()->get_scroll();
2584 	}
2585 
2586 	history_pos = p_new_pos;
2587 	tab_container->set_current_tab(history[history_pos].control->get_index());
2588 
2589 	n = history[history_pos].control;
2590 
2591 	if (n->cast_to<ScriptTextEditor>()) {
2592 
2593 		n->cast_to<ScriptTextEditor>()->get_text_edit()->set_v_scroll(history[history_pos].scroll_pos);
2594 		n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_set_column(history[history_pos].cursor_column);
2595 		n->cast_to<ScriptTextEditor>()->get_text_edit()->cursor_set_line(history[history_pos].cursor_row);
2596 		n->cast_to<ScriptTextEditor>()->get_text_edit()->grab_focus();
2597 	}
2598 
2599 	if (n->cast_to<EditorHelp>()) {
2600 
2601 		n->cast_to<EditorHelp>()->set_scroll(history[history_pos].scroll_pos);
2602 		n->cast_to<EditorHelp>()->set_focused();
2603 	}
2604 
2605 	n->set_meta("__editor_pass", ++edit_pass);
2606 	_update_script_names();
2607 	_update_history_arrows();
2608 }
2609 
_history_forward()2610 void ScriptEditor::_history_forward() {
2611 
2612 	if (history_pos < history.size() - 1) {
2613 		_update_history_pos(history_pos + 1);
2614 	}
2615 }
2616 
_history_back()2617 void ScriptEditor::_history_back() {
2618 
2619 	if (history_pos > 0) {
2620 		_update_history_pos(history_pos - 1);
2621 	}
2622 }
set_scene_root_script(Ref<Script> p_script)2623 void ScriptEditor::set_scene_root_script(Ref<Script> p_script) {
2624 
2625 	bool open_dominant = EditorSettings::get_singleton()->get("text_editor/open_dominant_script_on_scene_change");
2626 
2627 	if (bool(EditorSettings::get_singleton()->get("external_editor/use_external_editor")))
2628 		return;
2629 
2630 	if (open_dominant && p_script.is_valid() && _can_open_in_editor(p_script.ptr())) {
2631 		edit(p_script);
2632 	}
2633 }
2634 
script_go_to_method(Ref<Script> p_script,const String & p_method)2635 bool ScriptEditor::script_go_to_method(Ref<Script> p_script, const String &p_method) {
2636 
2637 	Vector<String> functions;
2638 	bool found = false;
2639 
2640 	for (int i = 0; i < tab_container->get_child_count(); i++) {
2641 		ScriptTextEditor *current = tab_container->get_child(i)->cast_to<ScriptTextEditor>();
2642 
2643 		if (current && current->get_edited_script() == p_script) {
2644 			functions = current->get_functions();
2645 			found = true;
2646 			break;
2647 		}
2648 	}
2649 
2650 	if (!found) {
2651 		String errortxt;
2652 		int line = -1, col;
2653 		String text = p_script->get_source_code();
2654 		List<String> fnc;
2655 
2656 		if (p_script->get_language()->validate(text, line, col, errortxt, p_script->get_path(), &fnc)) {
2657 
2658 			for (List<String>::Element *E = fnc.front(); E; E = E->next())
2659 				functions.push_back(E->get());
2660 		}
2661 	}
2662 
2663 	String method_search = p_method + ":";
2664 
2665 	for (int i = 0; i < functions.size(); i++) {
2666 		String function = functions[i];
2667 
2668 		if (function.begins_with(method_search)) {
2669 
2670 			edit(p_script);
2671 			int line = function.get_slice(":", 1).to_int();
2672 			_goto_script_line2(line - 1);
2673 			return true;
2674 		}
2675 	}
2676 
2677 	return false;
2678 }
2679 
set_live_auto_reload_running_scripts(bool p_enabled)2680 void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) {
2681 
2682 	auto_reload_running_scripts = p_enabled;
2683 }
2684 
_bind_methods()2685 void ScriptEditor::_bind_methods() {
2686 
2687 	ObjectTypeDB::bind_method("_file_dialog_action", &ScriptEditor::_file_dialog_action);
2688 	ObjectTypeDB::bind_method("_tab_changed", &ScriptEditor::_tab_changed);
2689 	ObjectTypeDB::bind_method("_menu_option", &ScriptEditor::_menu_option);
2690 	ObjectTypeDB::bind_method("_copy_script_path", &ScriptEditor::_copy_script_path);
2691 	ObjectTypeDB::bind_method("_close_current_tab", &ScriptEditor::_close_current_tab);
2692 	ObjectTypeDB::bind_method("_close_other_tabs", &ScriptEditor::_close_other_tabs);
2693 	ObjectTypeDB::bind_method("_close_all_tab", &ScriptEditor::_close_all_tab);
2694 	ObjectTypeDB::bind_method("_close_docs_tab", &ScriptEditor::_close_docs_tab);
2695 	ObjectTypeDB::bind_method("_editor_play", &ScriptEditor::_editor_play);
2696 	ObjectTypeDB::bind_method("_editor_pause", &ScriptEditor::_editor_pause);
2697 	ObjectTypeDB::bind_method("_editor_stop", &ScriptEditor::_editor_stop);
2698 	ObjectTypeDB::bind_method("_add_callback", &ScriptEditor::_add_callback);
2699 	ObjectTypeDB::bind_method("_reload_scripts", &ScriptEditor::_reload_scripts);
2700 	ObjectTypeDB::bind_method("_resave_scripts", &ScriptEditor::_resave_scripts);
2701 	ObjectTypeDB::bind_method("_res_saved_callback", &ScriptEditor::_res_saved_callback);
2702 	ObjectTypeDB::bind_method("_goto_script_line", &ScriptEditor::_goto_script_line);
2703 	ObjectTypeDB::bind_method("_goto_script_line2", &ScriptEditor::_goto_script_line2);
2704 	ObjectTypeDB::bind_method("_breakpoint_toggled", &ScriptEditor::_breakpoint_toggled);
2705 	ObjectTypeDB::bind_method("_breaked", &ScriptEditor::_breaked);
2706 	ObjectTypeDB::bind_method("_show_debugger", &ScriptEditor::_show_debugger);
2707 	ObjectTypeDB::bind_method("_get_debug_tooltip", &ScriptEditor::_get_debug_tooltip);
2708 	ObjectTypeDB::bind_method("_autosave_scripts", &ScriptEditor::_autosave_scripts);
2709 	ObjectTypeDB::bind_method("_editor_settings_changed", &ScriptEditor::_editor_settings_changed);
2710 	ObjectTypeDB::bind_method("_update_script_names", &ScriptEditor::_update_script_names);
2711 	ObjectTypeDB::bind_method("_tree_changed", &ScriptEditor::_tree_changed);
2712 	ObjectTypeDB::bind_method("_members_overview_selected", &ScriptEditor::_members_overview_selected);
2713 	ObjectTypeDB::bind_method("_script_selected", &ScriptEditor::_script_selected);
2714 	ObjectTypeDB::bind_method("_script_rmb_selected", &ScriptEditor::_script_rmb_selected);
2715 	ObjectTypeDB::bind_method("_script_created", &ScriptEditor::_script_created);
2716 	ObjectTypeDB::bind_method("_script_split_dragged", &ScriptEditor::_script_split_dragged);
2717 	ObjectTypeDB::bind_method("_help_class_open", &ScriptEditor::_help_class_open);
2718 	ObjectTypeDB::bind_method("_help_class_goto", &ScriptEditor::_help_class_goto);
2719 	ObjectTypeDB::bind_method("_request_help", &ScriptEditor::_help_class_open);
2720 	ObjectTypeDB::bind_method("_history_forward", &ScriptEditor::_history_forward);
2721 	ObjectTypeDB::bind_method("_history_back", &ScriptEditor::_history_back);
2722 	ObjectTypeDB::bind_method("_live_auto_reload_running_scripts", &ScriptEditor::_live_auto_reload_running_scripts);
2723 	ObjectTypeDB::bind_method("_unhandled_input", &ScriptEditor::_unhandled_input);
2724 }
2725 
ScriptEditor(EditorNode * p_editor)2726 ScriptEditor::ScriptEditor(EditorNode *p_editor) {
2727 
2728 	current_theme = "";
2729 
2730 	completion_cache = memnew(EditorScriptCodeCompletionCache);
2731 	restoring_layout = false;
2732 	waiting_update_names = false;
2733 	pending_auto_reload = false;
2734 	auto_reload_running_scripts = false;
2735 	members_overview_enabled = true;
2736 	editor = p_editor;
2737 
2738 	menu_hb = memnew(HBoxContainer);
2739 	add_child(menu_hb);
2740 
2741 	script_split = memnew(HSplitContainer);
2742 	add_child(script_split);
2743 	script_split->set_v_size_flags(SIZE_EXPAND_FILL);
2744 
2745 	list_split = memnew(VSplitContainer);
2746 	script_split->add_child(list_split);
2747 	list_split->set_v_size_flags(SIZE_EXPAND_FILL);
2748 
2749 	script_list = memnew(ItemList);
2750 	list_split->add_child(script_list);
2751 	script_list->set_custom_minimum_size(Size2(0, 0));
2752 	script_list->set_allow_rmb_select(true);
2753 	script_split->set_split_offset(140);
2754 	list_split->set_split_offset(500);
2755 
2756 	script_list_menu = memnew(PopupMenu);
2757 	script_list->add_child(script_list_menu);
2758 
2759 	members_overview = memnew(ItemList);
2760 	list_split->add_child(members_overview);
2761 	members_overview->set_custom_minimum_size(Size2(0, 0));
2762 
2763 	tab_container = memnew(TabContainer);
2764 	tab_container->set_tabs_visible(false);
2765 	script_split->add_child(tab_container);
2766 
2767 	tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
2768 
2769 	ED_SHORTCUT("script_editor/next_script", TTR("Next script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_GREATER);
2770 	ED_SHORTCUT("script_editor/prev_script", TTR("Previous script"), KEY_MASK_CMD | KEY_LESS);
2771 	set_process_unhandled_input(true);
2772 
2773 	file_menu = memnew(MenuButton);
2774 	menu_hb->add_child(file_menu);
2775 	file_menu->set_text(TTR("File"));
2776 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTR("New")), FILE_NEW);
2777 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open", TTR("Open")), FILE_OPEN);
2778 	file_menu->get_popup()->add_separator();
2779 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save", TTR("Save"), KEY_MASK_ALT | KEY_MASK_CMD | KEY_S), FILE_SAVE);
2780 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as", TTR("Save As..")), FILE_SAVE_AS);
2781 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_all", TTR("Save All"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_MASK_ALT | KEY_S), FILE_SAVE_ALL);
2782 	file_menu->get_popup()->add_separator();
2783 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_previous", TTR("History Prev"), KEY_MASK_CTRL | KEY_MASK_ALT | KEY_LEFT), WINDOW_PREV);
2784 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_next", TTR("History Next"), KEY_MASK_CTRL | KEY_MASK_ALT | KEY_RIGHT), WINDOW_NEXT);
2785 	file_menu->get_popup()->add_separator();
2786 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/import_theme", TTR("Import Theme")), FILE_IMPORT_THEME);
2787 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_theme", TTR("Reload Theme")), FILE_RELOAD_THEME);
2788 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme", TTR("Save Theme")), FILE_SAVE_THEME);
2789 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as", TTR("Save Theme As")), FILE_SAVE_THEME_AS);
2790 	file_menu->get_popup()->add_separator();
2791 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy_script_path", TTR("Copy Script Path")), FILE_COPY_SCRIPT_PATH);
2792 	file_menu->get_popup()->add_separator();
2793 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_docs", TTR("Close Docs")), CLOSE_DOCS);
2794 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file", TTR("Close"), KEY_MASK_CMD | KEY_W), FILE_CLOSE);
2795 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_other_tabs", TTR("Close Other Tabs")), FILE_CLOSE_OTHERS);
2796 	file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_all", TTR("Close All")), FILE_CLOSE_ALL);
2797 	file_menu->get_popup()->connect("item_pressed", this, "_menu_option");
2798 
2799 	edit_menu = memnew(MenuButton);
2800 	menu_hb->add_child(edit_menu);
2801 	edit_menu->set_text(TTR("Edit"));
2802 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/undo", TTR("Undo"), KEY_MASK_CMD | KEY_Z), EDIT_UNDO);
2803 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/redo", TTR("Redo"), KEY_MASK_CMD | KEY_Y), EDIT_REDO);
2804 	edit_menu->get_popup()->add_separator();
2805 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/cut", TTR("Cut"), KEY_MASK_CMD | KEY_X), EDIT_CUT);
2806 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy", TTR("Copy"), KEY_MASK_CMD | KEY_C), EDIT_COPY);
2807 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/paste", TTR("Paste"), KEY_MASK_CMD | KEY_V), EDIT_PASTE);
2808 	edit_menu->get_popup()->add_separator();
2809 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/select_all", TTR("Select All"), KEY_MASK_CMD | KEY_A), EDIT_SELECT_ALL);
2810 	edit_menu->get_popup()->add_separator();
2811 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/convert_to_uppercase", TTR("Convert to UpperCase")), EDIT_UPPERCASE);
2812 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/convert_to_lowercase", TTR("Convert to LowerCase")), EDIT_LOWERCASE);
2813 	edit_menu->get_popup()->add_separator();
2814 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/move_up", TTR("Move Up"), KEY_MASK_ALT | KEY_UP), EDIT_MOVE_LINE_UP);
2815 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/move_down", TTR("Move Down"), KEY_MASK_ALT | KEY_DOWN), EDIT_MOVE_LINE_DOWN);
2816 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/indent_left", TTR("Indent Left"), KEY_MASK_ALT | KEY_LEFT), EDIT_INDENT_LEFT);
2817 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/indent_right", TTR("Indent Right"), KEY_MASK_ALT | KEY_RIGHT), EDIT_INDENT_RIGHT);
2818 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD | KEY_K), EDIT_TOGGLE_COMMENT);
2819 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD | KEY_B), EDIT_CLONE_DOWN);
2820 	edit_menu->get_popup()->add_separator();
2821 #ifdef OSX_ENABLED
2822 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL | KEY_SPACE), EDIT_COMPLETE);
2823 #else
2824 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD | KEY_SPACE), EDIT_COMPLETE);
2825 #endif
2826 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CTRL | KEY_MASK_ALT | KEY_T), EDIT_TRIM_TRAILING_WHITESAPCE);
2827 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD | KEY_I), EDIT_AUTO_INDENT);
2828 	edit_menu->get_popup()->connect("item_pressed", this, "_menu_option");
2829 	edit_menu->get_popup()->add_separator();
2830 	edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_script_soft", TTR("Soft Reload Script"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_R), FILE_TOOL_RELOAD_SOFT);
2831 
2832 	search_menu = memnew(MenuButton);
2833 	menu_hb->add_child(search_menu);
2834 	search_menu->set_text(TTR("Search"));
2835 	search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F), SEARCH_FIND);
2836 	search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
2837 	search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT | KEY_F3), SEARCH_FIND_PREV);
2838 	search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace", TTR("Replace.."), KEY_MASK_CMD | KEY_R), SEARCH_REPLACE);
2839 	search_menu->get_popup()->add_separator();
2840 	search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_function", TTR("Goto Function.."), KEY_MASK_SHIFT | KEY_MASK_CMD | KEY_F), SEARCH_LOCATE_FUNCTION);
2841 	search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD | KEY_L), SEARCH_GOTO_LINE);
2842 	search_menu->get_popup()->connect("item_pressed", this, "_menu_option");
2843 
2844 	script_search_menu = memnew(MenuButton);
2845 	menu_hb->add_child(script_search_menu);
2846 	script_search_menu->set_text(TTR("Search"));
2847 	script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F), SEARCH_FIND);
2848 	script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
2849 	script_search_menu->get_popup()->connect("item_pressed", this, "_menu_option");
2850 	script_search_menu->hide();
2851 
2852 	debug_menu = memnew(MenuButton);
2853 	menu_hb->add_child(debug_menu);
2854 	debug_menu->set_text(TTR("Debug"));
2855 	debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9), DEBUG_TOGGLE_BREAKPOINT);
2856 	debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_F9), DEBUG_REMOVE_ALL_BREAKPOINTS);
2857 	debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_next_breakpoint", TTR("Goto Next Breakpoint"), KEY_MASK_CTRL | KEY_PERIOD), DEBUG_GOTO_NEXT_BREAKPOINT);
2858 	debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_previous_breakpoint", TTR("Goto Previous Breakpoint"), KEY_MASK_CTRL | KEY_COMMA), DEBUG_GOTO_PREV_BREAKPOINT);
2859 	debug_menu->get_popup()->add_separator();
2860 	debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_over", TTR("Step Over"), KEY_F10), DEBUG_NEXT);
2861 	debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_into", TTR("Step Into"), KEY_F11), DEBUG_STEP);
2862 	debug_menu->get_popup()->add_separator();
2863 	debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/break", TTR("Break")), DEBUG_BREAK);
2864 	debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/continue", TTR("Continue")), DEBUG_CONTINUE);
2865 	debug_menu->get_popup()->add_separator();
2866 	//debug_menu->get_popup()->add_check_item("Show Debugger",DEBUG_SHOW);
2867 	debug_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("debugger/keep_debugger_open", TTR("Keep Debugger Open")), DEBUG_SHOW_KEEP_OPEN);
2868 	debug_menu->get_popup()->connect("item_pressed", this, "_menu_option");
2869 
2870 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), true);
2871 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), true);
2872 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), true);
2873 	debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), true);
2874 
2875 #if 0
2876 	window_menu = memnew( MenuButton );
2877 	menu_hb->add_child(window_menu);
2878 	window_menu->set_text(TTR("Window"));
2879 	window_menu->get_popup()->add_item(TTR("Close"),WINDOW_CLOSE,KEY_MASK_CMD|KEY_W);
2880 	window_menu->get_popup()->add_separator();
2881 	window_menu->get_popup()->add_item(TTR("Move Left"),WINDOW_MOVE_LEFT,KEY_MASK_CMD|KEY_LEFT);
2882 	window_menu->get_popup()->add_item(TTR("Move Right"),WINDOW_MOVE_RIGHT,KEY_MASK_CMD|KEY_RIGHT);
2883 	window_menu->get_popup()->add_separator();
2884 	window_menu->get_popup()->connect("item_pressed", this,"_menu_option");
2885 
2886 #endif
2887 
2888 	help_menu = memnew(MenuButton);
2889 	menu_hb->add_child(help_menu);
2890 	help_menu->set_text(TTR("Help"));
2891 	help_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/Contextual", TTR("Contextual Help"), KEY_MASK_SHIFT | KEY_F1), HELP_CONTEXTUAL);
2892 	help_menu->get_popup()->connect("item_pressed", this, "_menu_option");
2893 
2894 	menu_hb->add_spacer();
2895 
2896 	script_icon = memnew(TextureFrame);
2897 	menu_hb->add_child(script_icon);
2898 	script_name_label = memnew(Label);
2899 	menu_hb->add_child(script_name_label);
2900 
2901 	script_icon->hide();
2902 	script_name_label->hide();
2903 
2904 	menu_hb->add_spacer();
2905 
2906 	site_search = memnew(ToolButton);
2907 	site_search->set_text(TTR("Tutorials"));
2908 	site_search->connect("pressed", this, "_menu_option", varray(SEARCH_WEBSITE));
2909 	menu_hb->add_child(site_search);
2910 	site_search->set_tooltip(TTR("Open https://godotengine.org at tutorials section."));
2911 
2912 	class_search = memnew(ToolButton);
2913 	class_search->set_text(TTR("Classes"));
2914 	class_search->connect("pressed", this, "_menu_option", varray(SEARCH_CLASSES));
2915 	menu_hb->add_child(class_search);
2916 	class_search->set_tooltip(TTR("Search the class hierarchy."));
2917 
2918 	help_search = memnew(ToolButton);
2919 	help_search->set_text(TTR("Search Help"));
2920 	help_search->connect("pressed", this, "_menu_option", varray(SEARCH_HELP));
2921 	menu_hb->add_child(help_search);
2922 	help_search->set_tooltip(TTR("Search the reference documentation."));
2923 
2924 	menu_hb->add_child(memnew(VSeparator));
2925 
2926 	script_back = memnew(ToolButton);
2927 	script_back->connect("pressed", this, "_history_back");
2928 	menu_hb->add_child(script_back);
2929 	script_back->set_disabled(true);
2930 	script_back->set_tooltip(TTR("Go to previous edited document."));
2931 
2932 	script_forward = memnew(ToolButton);
2933 	script_forward->connect("pressed", this, "_history_forward");
2934 	menu_hb->add_child(script_forward);
2935 	script_forward->set_disabled(true);
2936 	script_forward->set_tooltip(TTR("Go to next edited document."));
2937 
2938 	tab_container->connect("tab_changed", this, "_tab_changed");
2939 
2940 	erase_tab_confirm = memnew(ConfirmationDialog);
2941 	add_child(erase_tab_confirm);
2942 	erase_tab_confirm->connect("confirmed", this, "_close_current_tab");
2943 
2944 	script_create_dialog = memnew(ScriptCreateDialog);
2945 	script_create_dialog->set_title(TTR("Create Script"));
2946 	add_child(script_create_dialog);
2947 	script_create_dialog->connect("script_created", this, "_script_created");
2948 
2949 	file_dialog_option = -1;
2950 	file_dialog = memnew(EditorFileDialog);
2951 	add_child(file_dialog);
2952 	file_dialog->connect("file_selected", this, "_file_dialog_action");
2953 
2954 	goto_line_dialog = memnew(GotoLineDialog);
2955 	add_child(goto_line_dialog);
2956 
2957 	debugger = memnew(ScriptEditorDebugger(editor));
2958 	debugger->connect("goto_script_line", this, "_goto_script_line");
2959 	debugger->connect("show_debugger", this, "_show_debugger");
2960 
2961 	disk_changed = memnew(ConfirmationDialog);
2962 	{
2963 		VBoxContainer *vbc = memnew(VBoxContainer);
2964 		disk_changed->add_child(vbc);
2965 		disk_changed->set_child_rect(vbc);
2966 
2967 		Label *dl = memnew(Label);
2968 		dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?:"));
2969 		vbc->add_child(dl);
2970 
2971 		disk_changed_list = memnew(Tree);
2972 		vbc->add_child(disk_changed_list);
2973 		disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
2974 
2975 		disk_changed->connect("confirmed", this, "_reload_scripts");
2976 		disk_changed->get_ok()->set_text(TTR("Reload"));
2977 
2978 		disk_changed->add_button(TTR("Resave"), !OS::get_singleton()->get_swap_ok_cancel(), "resave");
2979 		disk_changed->connect("custom_action", this, "_resave_scripts");
2980 	}
2981 
2982 	add_child(disk_changed);
2983 
2984 	script_editor = this;
2985 
2986 	quick_open = memnew(ScriptEditorQuickOpen);
2987 	add_child(quick_open);
2988 
2989 	quick_open->connect("goto_line", this, "_goto_script_line2");
2990 
2991 	Button *db = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Debugger"), debugger);
2992 	debugger->set_tool_button(db);
2993 
2994 	debugger->connect("breaked", this, "_breaked");
2995 
2996 	autosave_timer = memnew(Timer);
2997 	autosave_timer->set_one_shot(false);
2998 	add_child(autosave_timer);
2999 
3000 	grab_focus_block = false;
3001 
3002 	help_search_dialog = memnew(EditorHelpSearch);
3003 	add_child(help_search_dialog);
3004 	help_search_dialog->connect("go_to_help", this, "_help_class_goto");
3005 
3006 	help_index = memnew(EditorHelpIndex);
3007 	add_child(help_index);
3008 	help_index->connect("open_class", this, "_help_class_open");
3009 
3010 	history_pos = -1;
3011 	//	debugger_gui->hide();
3012 
3013 	edit_pass = 0;
3014 	trim_trailing_whitespace_on_save = false;
3015 }
3016 
~ScriptEditor()3017 ScriptEditor::~ScriptEditor() {
3018 
3019 	memdelete(completion_cache);
3020 }
3021 
edit(Object * p_object)3022 void ScriptEditorPlugin::edit(Object *p_object) {
3023 
3024 	if (!p_object->cast_to<Script>())
3025 		return;
3026 
3027 	script_editor->edit(p_object->cast_to<Script>());
3028 }
3029 
handles(Object * p_object) const3030 bool ScriptEditorPlugin::handles(Object *p_object) const {
3031 
3032 	if (p_object->cast_to<Script>()) {
3033 
3034 		bool valid = _can_open_in_editor(p_object->cast_to<Script>());
3035 
3036 		if (!valid) { //user tried to open it by clicking
3037 			EditorNode::get_singleton()->show_warning(TTR("Built-in scripts can only be edited when the scene they belong to is loaded"));
3038 		}
3039 		return valid;
3040 	}
3041 
3042 	return p_object->is_type("Script");
3043 }
3044 
make_visible(bool p_visible)3045 void ScriptEditorPlugin::make_visible(bool p_visible) {
3046 
3047 	if (p_visible) {
3048 		script_editor->show();
3049 		script_editor->set_process(true);
3050 		script_editor->ensure_select_current();
3051 	} else {
3052 
3053 		script_editor->hide();
3054 		script_editor->set_process(false);
3055 	}
3056 }
3057 
selected_notify()3058 void ScriptEditorPlugin::selected_notify() {
3059 
3060 	script_editor->ensure_select_current();
3061 }
3062 
get_state() const3063 Dictionary ScriptEditorPlugin::get_state() const {
3064 
3065 	return script_editor->get_state();
3066 }
3067 
set_state(const Dictionary & p_state)3068 void ScriptEditorPlugin::set_state(const Dictionary &p_state) {
3069 
3070 	script_editor->set_state(p_state);
3071 }
clear()3072 void ScriptEditorPlugin::clear() {
3073 
3074 	script_editor->clear();
3075 }
3076 
save_external_data()3077 void ScriptEditorPlugin::save_external_data() {
3078 
3079 	script_editor->save_all_scripts();
3080 }
3081 
apply_changes()3082 void ScriptEditorPlugin::apply_changes() {
3083 
3084 	script_editor->apply_scripts();
3085 }
3086 
restore_global_state()3087 void ScriptEditorPlugin::restore_global_state() {
3088 }
3089 
save_global_state()3090 void ScriptEditorPlugin::save_global_state() {
3091 }
3092 
set_window_layout(Ref<ConfigFile> p_layout)3093 void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
3094 
3095 	script_editor->set_window_layout(p_layout);
3096 }
3097 
get_window_layout(Ref<ConfigFile> p_layout)3098 void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
3099 
3100 	script_editor->get_window_layout(p_layout);
3101 }
3102 
get_breakpoints(List<String> * p_breakpoints)3103 void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
3104 
3105 	return script_editor->get_breakpoints(p_breakpoints);
3106 }
3107 
edited_scene_changed()3108 void ScriptEditorPlugin::edited_scene_changed() {
3109 
3110 	script_editor->edited_scene_changed();
3111 }
3112 
ScriptEditorPlugin(EditorNode * p_node)3113 ScriptEditorPlugin::ScriptEditorPlugin(EditorNode *p_node) {
3114 
3115 	editor = p_node;
3116 	script_editor = memnew(ScriptEditor(p_node));
3117 	editor->get_viewport()->add_child(script_editor);
3118 	script_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
3119 
3120 	script_editor->hide();
3121 
3122 	EDITOR_DEF("text_editor/auto_reload_scripts_on_external_change", true);
3123 	ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save", true));
3124 	EDITOR_DEF("text_editor/open_dominant_script_on_scene_change", true);
3125 	EDITOR_DEF("external_editor/use_external_editor", false);
3126 	EDITOR_DEF("external_editor/exec_path", "");
3127 	EDITOR_DEF("text_editor/script_temperature_enabled", true);
3128 	EDITOR_DEF("text_editor/highlight_current_script", true);
3129 	EDITOR_DEF("text_editor/script_temperature_history_size", 15);
3130 	EDITOR_DEF("text_editor/script_temperature_hot_color", Color(1, 0, 0, 0.3));
3131 	EDITOR_DEF("text_editor/script_temperature_cold_color", Color(0, 0, 1, 0.3));
3132 	EDITOR_DEF("text_editor/current_script_background_color", Color(0.81, 0.81, 0.14, 0.63));
3133 	EDITOR_DEF("text_editor/group_help_pages", true);
3134 	EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/sort_scripts_by", PROPERTY_HINT_ENUM, "Name,Path"));
3135 	EDITOR_DEF("text_editor/sort_scripts_by", 0);
3136 	EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/list_script_names_as", PROPERTY_HINT_ENUM, "Name,Parent Directory And Name,Full Path"));
3137 	EDITOR_DEF("text_editor/list_script_names_as", 0);
3138 	EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "external_editor/exec_path", PROPERTY_HINT_GLOBAL_FILE));
3139 	EDITOR_DEF("external_editor/exec_flags", "");
3140 }
3141 
~ScriptEditorPlugin()3142 ScriptEditorPlugin::~ScriptEditorPlugin() {
3143 }
3144