1 /*************************************************************************/
2 /* script_text_editor.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30
31 #include "script_text_editor.h"
32
33 #include "core/math/expression.h"
34 #include "core/os/keyboard.h"
35 #include "editor/editor_node.h"
36 #include "editor/editor_scale.h"
37 #include "editor/editor_settings.h"
38 #include "editor/script_editor_debugger.h"
39
ok_pressed()40 void ConnectionInfoDialog::ok_pressed() {
41 }
42
popup_connections(String p_method,Vector<Node * > p_nodes)43 void ConnectionInfoDialog::popup_connections(String p_method, Vector<Node *> p_nodes) {
44 method->set_text(p_method);
45
46 tree->clear();
47 TreeItem *root = tree->create_item();
48
49 for (int i = 0; i < p_nodes.size(); i++) {
50 List<Connection> all_connections;
51 p_nodes[i]->get_signals_connected_to_this(&all_connections);
52
53 for (List<Connection>::Element *E = all_connections.front(); E; E = E->next()) {
54 Connection connection = E->get();
55
56 if (connection.method != p_method) {
57 continue;
58 }
59
60 TreeItem *node_item = tree->create_item(root);
61
62 node_item->set_text(0, Object::cast_to<Node>(connection.source)->get_name());
63 node_item->set_icon(0, EditorNode::get_singleton()->get_object_icon(connection.source, "Node"));
64 node_item->set_selectable(0, false);
65 node_item->set_editable(0, false);
66
67 node_item->set_text(1, connection.signal);
68 node_item->set_icon(1, get_parent_control()->get_icon("Slot", "EditorIcons"));
69 node_item->set_selectable(1, false);
70 node_item->set_editable(1, false);
71
72 node_item->set_text(2, Object::cast_to<Node>(connection.target)->get_name());
73 node_item->set_icon(2, EditorNode::get_singleton()->get_object_icon(connection.target, "Node"));
74 node_item->set_selectable(2, false);
75 node_item->set_editable(2, false);
76 }
77 }
78
79 popup_centered(Size2(600, 300) * EDSCALE);
80 }
81
ConnectionInfoDialog()82 ConnectionInfoDialog::ConnectionInfoDialog() {
83 set_title(TTR("Connections to method:"));
84
85 VBoxContainer *vbc = memnew(VBoxContainer);
86 vbc->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 8 * EDSCALE);
87 vbc->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 8 * EDSCALE);
88 vbc->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -8 * EDSCALE);
89 vbc->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -8 * EDSCALE);
90 add_child(vbc);
91
92 method = memnew(Label);
93 method->set_align(Label::ALIGN_CENTER);
94 vbc->add_child(method);
95
96 tree = memnew(Tree);
97 tree->set_columns(3);
98 tree->set_hide_root(true);
99 tree->set_column_titles_visible(true);
100 tree->set_column_title(0, TTR("Source"));
101 tree->set_column_title(1, TTR("Signal"));
102 tree->set_column_title(2, TTR("Target"));
103 vbc->add_child(tree);
104 tree->set_v_size_flags(SIZE_EXPAND_FILL);
105 tree->set_allow_rmb_select(true);
106 }
107
108 ////////////////////////////////////////////////////////////////////////////////
109
get_functions()110 Vector<String> ScriptTextEditor::get_functions() {
111
112 String errortxt;
113 int line = -1, col;
114 TextEdit *te = code_editor->get_text_edit();
115 String text = te->get_text();
116 List<String> fnc;
117
118 if (script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
119
120 //if valid rewrite functions to latest
121 functions.clear();
122 for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
123
124 functions.push_back(E->get());
125 }
126 }
127
128 return functions;
129 }
130
apply_code()131 void ScriptTextEditor::apply_code() {
132
133 if (script.is_null())
134 return;
135 script->set_source_code(code_editor->get_text_edit()->get_text());
136 script->update_exports();
137 _update_member_keywords();
138 }
139
get_edited_resource() const140 RES ScriptTextEditor::get_edited_resource() const {
141 return script;
142 }
143
set_edited_resource(const RES & p_res)144 void ScriptTextEditor::set_edited_resource(const RES &p_res) {
145 ERR_FAIL_COND(!script.is_null());
146
147 script = p_res;
148 _set_theme_for_script();
149
150 code_editor->get_text_edit()->set_text(script->get_source_code());
151 code_editor->get_text_edit()->clear_undo_history();
152 code_editor->get_text_edit()->tag_saved_version();
153
154 emit_signal("name_changed");
155 code_editor->update_line_and_column();
156
157 _validate_script();
158 }
159
_update_member_keywords()160 void ScriptTextEditor::_update_member_keywords() {
161 member_keywords.clear();
162 code_editor->get_text_edit()->clear_member_keywords();
163 Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
164
165 StringName instance_base = script->get_instance_base_type();
166
167 if (instance_base == StringName())
168 return;
169 List<PropertyInfo> plist;
170 ClassDB::get_property_list(instance_base, &plist);
171
172 for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
173 String name = E->get().name;
174 if (E->get().usage & PROPERTY_USAGE_CATEGORY || E->get().usage & PROPERTY_USAGE_GROUP)
175 continue;
176 if (name.find("/") != -1)
177 continue;
178
179 code_editor->get_text_edit()->add_member_keyword(name, member_variable_color);
180 }
181
182 List<String> clist;
183 ClassDB::get_integer_constant_list(instance_base, &clist);
184
185 for (List<String>::Element *E = clist.front(); E; E = E->next()) {
186
187 code_editor->get_text_edit()->add_member_keyword(E->get(), member_variable_color);
188 }
189 }
190
_load_theme_settings()191 void ScriptTextEditor::_load_theme_settings() {
192
193 TextEdit *text_edit = code_editor->get_text_edit();
194
195 text_edit->clear_colors();
196
197 Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
198 Color completion_background_color = EDITOR_GET("text_editor/highlighting/completion_background_color");
199 Color completion_selected_color = EDITOR_GET("text_editor/highlighting/completion_selected_color");
200 Color completion_existing_color = EDITOR_GET("text_editor/highlighting/completion_existing_color");
201 Color completion_scroll_color = EDITOR_GET("text_editor/highlighting/completion_scroll_color");
202 Color completion_font_color = EDITOR_GET("text_editor/highlighting/completion_font_color");
203 Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
204 Color line_number_color = EDITOR_GET("text_editor/highlighting/line_number_color");
205 Color safe_line_number_color = EDITOR_GET("text_editor/highlighting/safe_line_number_color");
206 Color caret_color = EDITOR_GET("text_editor/highlighting/caret_color");
207 Color caret_background_color = EDITOR_GET("text_editor/highlighting/caret_background_color");
208 Color text_selected_color = EDITOR_GET("text_editor/highlighting/text_selected_color");
209 Color selection_color = EDITOR_GET("text_editor/highlighting/selection_color");
210 Color brace_mismatch_color = EDITOR_GET("text_editor/highlighting/brace_mismatch_color");
211 Color current_line_color = EDITOR_GET("text_editor/highlighting/current_line_color");
212 Color line_length_guideline_color = EDITOR_GET("text_editor/highlighting/line_length_guideline_color");
213 Color word_highlighted_color = EDITOR_GET("text_editor/highlighting/word_highlighted_color");
214 Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
215 Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
216 Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
217 Color mark_color = EDITOR_GET("text_editor/highlighting/mark_color");
218 Color bookmark_color = EDITOR_GET("text_editor/highlighting/bookmark_color");
219 Color breakpoint_color = EDITOR_GET("text_editor/highlighting/breakpoint_color");
220 Color executing_line_color = EDITOR_GET("text_editor/highlighting/executing_line_color");
221 Color code_folding_color = EDITOR_GET("text_editor/highlighting/code_folding_color");
222 Color search_result_color = EDITOR_GET("text_editor/highlighting/search_result_color");
223 Color search_result_border_color = EDITOR_GET("text_editor/highlighting/search_result_border_color");
224 Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
225 Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
226 Color basetype_color = EDITOR_GET("text_editor/highlighting/base_type_color");
227 Color type_color = EDITOR_GET("text_editor/highlighting/engine_type_color");
228 Color usertype_color = EDITOR_GET("text_editor/highlighting/user_type_color");
229 Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
230 Color string_color = EDITOR_GET("text_editor/highlighting/string_color");
231
232 text_edit->add_color_override("background_color", background_color);
233 text_edit->add_color_override("completion_background_color", completion_background_color);
234 text_edit->add_color_override("completion_selected_color", completion_selected_color);
235 text_edit->add_color_override("completion_existing_color", completion_existing_color);
236 text_edit->add_color_override("completion_scroll_color", completion_scroll_color);
237 text_edit->add_color_override("completion_font_color", completion_font_color);
238 text_edit->add_color_override("font_color", text_color);
239 text_edit->add_color_override("line_number_color", line_number_color);
240 text_edit->add_color_override("safe_line_number_color", safe_line_number_color);
241 text_edit->add_color_override("caret_color", caret_color);
242 text_edit->add_color_override("caret_background_color", caret_background_color);
243 text_edit->add_color_override("font_color_selected", text_selected_color);
244 text_edit->add_color_override("selection_color", selection_color);
245 text_edit->add_color_override("brace_mismatch_color", brace_mismatch_color);
246 text_edit->add_color_override("current_line_color", current_line_color);
247 text_edit->add_color_override("line_length_guideline_color", line_length_guideline_color);
248 text_edit->add_color_override("word_highlighted_color", word_highlighted_color);
249 text_edit->add_color_override("number_color", number_color);
250 text_edit->add_color_override("function_color", function_color);
251 text_edit->add_color_override("member_variable_color", member_variable_color);
252 text_edit->add_color_override("bookmark_color", bookmark_color);
253 text_edit->add_color_override("breakpoint_color", breakpoint_color);
254 text_edit->add_color_override("executing_line_color", executing_line_color);
255 text_edit->add_color_override("mark_color", mark_color);
256 text_edit->add_color_override("code_folding_color", code_folding_color);
257 text_edit->add_color_override("search_result_color", search_result_color);
258 text_edit->add_color_override("search_result_border_color", search_result_border_color);
259 text_edit->add_color_override("symbol_color", symbol_color);
260
261 text_edit->add_constant_override("line_spacing", EDITOR_DEF("text_editor/theme/line_spacing", 6));
262
263 colors_cache.symbol_color = symbol_color;
264 colors_cache.keyword_color = keyword_color;
265 colors_cache.basetype_color = basetype_color;
266 colors_cache.type_color = type_color;
267 colors_cache.usertype_color = usertype_color;
268 colors_cache.comment_color = comment_color;
269 colors_cache.string_color = string_color;
270
271 theme_loaded = true;
272 if (!script.is_null())
273 _set_theme_for_script();
274 }
275
_set_theme_for_script()276 void ScriptTextEditor::_set_theme_for_script() {
277
278 if (!theme_loaded)
279 return;
280
281 TextEdit *text_edit = code_editor->get_text_edit();
282
283 List<String> keywords;
284 script->get_language()->get_reserved_words(&keywords);
285
286 for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
287
288 text_edit->add_keyword_color(E->get(), colors_cache.keyword_color);
289 }
290
291 //colorize core types
292 const Color basetype_color = colors_cache.basetype_color;
293 text_edit->add_keyword_color("String", basetype_color);
294 text_edit->add_keyword_color("Vector2", basetype_color);
295 text_edit->add_keyword_color("Rect2", basetype_color);
296 text_edit->add_keyword_color("Transform2D", basetype_color);
297 text_edit->add_keyword_color("Vector3", basetype_color);
298 text_edit->add_keyword_color("AABB", basetype_color);
299 text_edit->add_keyword_color("Basis", basetype_color);
300 text_edit->add_keyword_color("Plane", basetype_color);
301 text_edit->add_keyword_color("Transform", basetype_color);
302 text_edit->add_keyword_color("Quat", basetype_color);
303 text_edit->add_keyword_color("Color", basetype_color);
304 text_edit->add_keyword_color("Object", basetype_color);
305 text_edit->add_keyword_color("NodePath", basetype_color);
306 text_edit->add_keyword_color("RID", basetype_color);
307 text_edit->add_keyword_color("Dictionary", basetype_color);
308 text_edit->add_keyword_color("Array", basetype_color);
309 text_edit->add_keyword_color("PoolByteArray", basetype_color);
310 text_edit->add_keyword_color("PoolIntArray", basetype_color);
311 text_edit->add_keyword_color("PoolRealArray", basetype_color);
312 text_edit->add_keyword_color("PoolStringArray", basetype_color);
313 text_edit->add_keyword_color("PoolVector2Array", basetype_color);
314 text_edit->add_keyword_color("PoolVector3Array", basetype_color);
315 text_edit->add_keyword_color("PoolColorArray", basetype_color);
316
317 //colorize engine types
318 List<StringName> types;
319 ClassDB::get_class_list(&types);
320
321 for (List<StringName>::Element *E = types.front(); E; E = E->next()) {
322
323 String n = E->get();
324 if (n.begins_with("_"))
325 n = n.substr(1, n.length());
326
327 text_edit->add_keyword_color(n, colors_cache.type_color);
328 }
329 _update_member_keywords();
330
331 //colorize user types
332 List<StringName> global_classes;
333 ScriptServer::get_global_class_list(&global_classes);
334
335 for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
336
337 text_edit->add_keyword_color(E->get(), colors_cache.usertype_color);
338 }
339
340 //colorize singleton autoloads (as types, just as engine singletons are)
341 List<PropertyInfo> props;
342 ProjectSettings::get_singleton()->get_property_list(&props);
343 for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
344 String s = E->get().name;
345 if (!s.begins_with("autoload/")) {
346 continue;
347 }
348 String path = ProjectSettings::get_singleton()->get(s);
349 if (path.begins_with("*")) {
350 text_edit->add_keyword_color(s.get_slice("/", 1), colors_cache.usertype_color);
351 }
352 }
353
354 //colorize comments
355 List<String> comments;
356 script->get_language()->get_comment_delimiters(&comments);
357
358 for (List<String>::Element *E = comments.front(); E; E = E->next()) {
359
360 String comment = E->get();
361 String beg = comment.get_slice(" ", 0);
362 String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
363
364 text_edit->add_color_region(beg, end, colors_cache.comment_color, end == "");
365 }
366
367 //colorize strings
368 List<String> strings;
369 script->get_language()->get_string_delimiters(&strings);
370 for (List<String>::Element *E = strings.front(); E; E = E->next()) {
371
372 String string = E->get();
373 String beg = string.get_slice(" ", 0);
374 String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
375 text_edit->add_color_region(beg, end, colors_cache.string_color, end == "");
376 }
377 }
378
_show_warnings_panel(bool p_show)379 void ScriptTextEditor::_show_warnings_panel(bool p_show) {
380 warnings_panel->set_visible(p_show);
381 }
382
_warning_clicked(Variant p_line)383 void ScriptTextEditor::_warning_clicked(Variant p_line) {
384 if (p_line.get_type() == Variant::INT) {
385 code_editor->get_text_edit()->cursor_set_line(p_line.operator int64_t());
386 } else if (p_line.get_type() == Variant::DICTIONARY) {
387 Dictionary meta = p_line.operator Dictionary();
388 code_editor->get_text_edit()->insert_at("# warning-ignore:" + meta["code"].operator String(), meta["line"].operator int64_t() - 1);
389 _validate_script();
390 }
391 }
392
reload_text()393 void ScriptTextEditor::reload_text() {
394
395 ERR_FAIL_COND(script.is_null());
396
397 TextEdit *te = code_editor->get_text_edit();
398 int column = te->cursor_get_column();
399 int row = te->cursor_get_line();
400 int h = te->get_h_scroll();
401 int v = te->get_v_scroll();
402
403 te->set_text(script->get_source_code());
404 te->cursor_set_line(row);
405 te->cursor_set_column(column);
406 te->set_h_scroll(h);
407 te->set_v_scroll(v);
408
409 te->tag_saved_version();
410
411 code_editor->update_line_and_column();
412 }
413
_notification(int p_what)414 void ScriptTextEditor::_notification(int p_what) {
415
416 switch (p_what) {
417 case NOTIFICATION_READY:
418 _load_theme_settings();
419 break;
420 }
421 }
422
add_callback(const String & p_function,PoolStringArray p_args)423 void ScriptTextEditor::add_callback(const String &p_function, PoolStringArray p_args) {
424
425 String code = code_editor->get_text_edit()->get_text();
426 int pos = script->get_language()->find_function(p_function, code);
427 if (pos == -1) {
428 //does not exist
429 code_editor->get_text_edit()->deselect();
430 pos = code_editor->get_text_edit()->get_line_count() + 2;
431 String func = script->get_language()->make_function("", p_function, p_args);
432 //code=code+func;
433 code_editor->get_text_edit()->cursor_set_line(pos + 1);
434 code_editor->get_text_edit()->cursor_set_column(1000000); //none shall be that big
435 code_editor->get_text_edit()->insert_text_at_cursor("\n\n" + func);
436 }
437 code_editor->get_text_edit()->cursor_set_line(pos);
438 code_editor->get_text_edit()->cursor_set_column(1);
439 }
440
show_members_overview()441 bool ScriptTextEditor::show_members_overview() {
442 return true;
443 }
444
update_settings()445 void ScriptTextEditor::update_settings() {
446
447 code_editor->update_editor_settings();
448 }
449
is_unsaved()450 bool ScriptTextEditor::is_unsaved() {
451
452 return code_editor->get_text_edit()->get_version() != code_editor->get_text_edit()->get_saved_version();
453 }
454
get_edit_state()455 Variant ScriptTextEditor::get_edit_state() {
456
457 return code_editor->get_edit_state();
458 }
459
set_edit_state(const Variant & p_state)460 void ScriptTextEditor::set_edit_state(const Variant &p_state) {
461
462 code_editor->set_edit_state(p_state);
463
464 Dictionary state = p_state;
465 if (state.has("syntax_highlighter")) {
466 int idx = highlighter_menu->get_item_idx_from_text(state["syntax_highlighter"]);
467 if (idx >= 0) {
468 _change_syntax_highlighter(idx);
469 }
470 }
471 }
472
_convert_case(CodeTextEditor::CaseStyle p_case)473 void ScriptTextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
474
475 code_editor->convert_case(p_case);
476 }
477
trim_trailing_whitespace()478 void ScriptTextEditor::trim_trailing_whitespace() {
479
480 code_editor->trim_trailing_whitespace();
481 }
482
insert_final_newline()483 void ScriptTextEditor::insert_final_newline() {
484
485 code_editor->insert_final_newline();
486 }
487
convert_indent_to_spaces()488 void ScriptTextEditor::convert_indent_to_spaces() {
489
490 code_editor->convert_indent_to_spaces();
491 }
492
convert_indent_to_tabs()493 void ScriptTextEditor::convert_indent_to_tabs() {
494
495 code_editor->convert_indent_to_tabs();
496 }
497
tag_saved_version()498 void ScriptTextEditor::tag_saved_version() {
499
500 code_editor->get_text_edit()->tag_saved_version();
501 }
502
goto_line(int p_line,bool p_with_error)503 void ScriptTextEditor::goto_line(int p_line, bool p_with_error) {
504
505 code_editor->goto_line(p_line);
506 }
507
goto_line_selection(int p_line,int p_begin,int p_end)508 void ScriptTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
509
510 code_editor->goto_line_selection(p_line, p_begin, p_end);
511 }
512
goto_line_centered(int p_line)513 void ScriptTextEditor::goto_line_centered(int p_line) {
514
515 code_editor->goto_line_centered(p_line);
516 }
517
set_executing_line(int p_line)518 void ScriptTextEditor::set_executing_line(int p_line) {
519 code_editor->set_executing_line(p_line);
520 }
521
clear_executing_line()522 void ScriptTextEditor::clear_executing_line() {
523 code_editor->clear_executing_line();
524 }
525
ensure_focus()526 void ScriptTextEditor::ensure_focus() {
527
528 code_editor->get_text_edit()->grab_focus();
529 }
530
get_name()531 String ScriptTextEditor::get_name() {
532 String name;
533
534 if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
535 name = script->get_path().get_file();
536 if (is_unsaved()) {
537 name += "(*)";
538 }
539 } else if (script->get_name() != "")
540 name = script->get_name();
541 else
542 name = script->get_class() + "(" + itos(script->get_instance_id()) + ")";
543
544 return name;
545 }
546
get_icon()547 Ref<Texture> ScriptTextEditor::get_icon() {
548
549 if (get_parent_control() && get_parent_control()->has_icon(script->get_class(), "EditorIcons")) {
550 return get_parent_control()->get_icon(script->get_class(), "EditorIcons");
551 }
552
553 return Ref<Texture>();
554 }
555
_validate_script()556 void ScriptTextEditor::_validate_script() {
557
558 String errortxt;
559 int line = -1, col;
560 TextEdit *te = code_editor->get_text_edit();
561
562 String text = te->get_text();
563 List<String> fnc;
564 Set<int> safe_lines;
565 List<ScriptLanguage::Warning> warnings;
566
567 if (!script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc, &warnings, &safe_lines)) {
568 String error_text = "error(" + itos(line) + "," + itos(col) + "): " + errortxt;
569 code_editor->set_error(error_text);
570 code_editor->set_error_pos(line - 1, col - 1);
571 script_is_valid = false;
572 } else {
573 code_editor->set_error("");
574 line = -1;
575 if (!script->is_tool()) {
576 script->set_source_code(text);
577 script->update_exports();
578 _update_member_keywords();
579 }
580
581 functions.clear();
582 for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
583
584 functions.push_back(E->get());
585 }
586 script_is_valid = true;
587 }
588 _update_connected_methods();
589
590 int warning_nb = warnings.size();
591 warnings_panel->clear();
592
593 // Add missing connections.
594 if (GLOBAL_GET("debug/gdscript/warnings/enable").booleanize()) {
595 Node *base = get_tree()->get_edited_scene_root();
596 if (base && missing_connections.size() > 0) {
597 warnings_panel->push_table(1);
598 for (List<Connection>::Element *E = missing_connections.front(); E; E = E->next()) {
599 Connection connection = E->get();
600
601 String base_path = base->get_name();
602 String source_path = base == connection.source ? base_path : base_path + "/" + base->get_path_to(Object::cast_to<Node>(connection.source));
603 String target_path = base == connection.target ? base_path : base_path + "/" + base->get_path_to(Object::cast_to<Node>(connection.target));
604
605 warnings_panel->push_cell();
606 warnings_panel->push_color(warnings_panel->get_color("warning_color", "Editor"));
607 warnings_panel->add_text(vformat(TTR("Missing connected method '%s' for signal '%s' from node '%s' to node '%s'."), connection.method, connection.signal, source_path, target_path));
608 warnings_panel->pop(); // Color.
609 warnings_panel->pop(); // Cell.
610 }
611 warnings_panel->pop(); // Table.
612
613 warning_nb += missing_connections.size();
614 }
615 }
616
617 code_editor->set_warning_nb(warning_nb);
618
619 // Add script warnings.
620 warnings_panel->push_table(3);
621 for (List<ScriptLanguage::Warning>::Element *E = warnings.front(); E; E = E->next()) {
622 ScriptLanguage::Warning w = E->get();
623
624 Dictionary ignore_meta;
625 ignore_meta["line"] = w.line;
626 ignore_meta["code"] = w.string_code.to_lower();
627 warnings_panel->push_cell();
628 warnings_panel->push_meta(ignore_meta);
629 warnings_panel->push_color(
630 warnings_panel->get_color("accent_color", "Editor").linear_interpolate(warnings_panel->get_color("mono_color", "Editor"), 0.5));
631 warnings_panel->add_text(TTR("[Ignore]"));
632 warnings_panel->pop(); // Color.
633 warnings_panel->pop(); // Meta ignore.
634 warnings_panel->pop(); // Cell.
635
636 warnings_panel->push_cell();
637 warnings_panel->push_meta(w.line - 1);
638 warnings_panel->push_color(warnings_panel->get_color("warning_color", "Editor"));
639 warnings_panel->add_text(TTR("Line") + " " + itos(w.line));
640 warnings_panel->add_text(" (" + w.string_code + "):");
641 warnings_panel->pop(); // Color.
642 warnings_panel->pop(); // Meta goto.
643 warnings_panel->pop(); // Cell.
644
645 warnings_panel->push_cell();
646 warnings_panel->add_text(w.message);
647 warnings_panel->pop(); // Cell.
648 }
649 warnings_panel->pop(); // Table.
650
651 line--;
652 bool highlight_safe = EDITOR_DEF("text_editor/highlighting/highlight_type_safe_lines", true);
653 bool last_is_safe = false;
654 for (int i = 0; i < te->get_line_count(); i++) {
655 te->set_line_as_marked(i, line == i);
656 if (highlight_safe) {
657 if (safe_lines.has(i + 1)) {
658 te->set_line_as_safe(i, true);
659 last_is_safe = true;
660 } else if (last_is_safe && (te->is_line_comment(i) || te->get_line(i).strip_edges().empty())) {
661 te->set_line_as_safe(i, true);
662 } else {
663 te->set_line_as_safe(i, false);
664 last_is_safe = false;
665 }
666 } else {
667 te->set_line_as_safe(i, false);
668 }
669 }
670
671 emit_signal("name_changed");
672 emit_signal("edited_script_changed");
673 }
674
_update_bookmark_list()675 void ScriptTextEditor::_update_bookmark_list() {
676
677 bookmarks_menu->clear();
678 bookmarks_menu->set_size(Size2(1, 1));
679
680 bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
681 bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
682 bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
683 bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
684
685 Array bookmark_list = code_editor->get_text_edit()->get_bookmarks_array();
686 if (bookmark_list.size() == 0) {
687 return;
688 }
689
690 bookmarks_menu->add_separator();
691
692 for (int i = 0; i < bookmark_list.size(); i++) {
693 // Strip edges to remove spaces or tabs.
694 // Also replace any tabs by spaces, since we can't print tabs in the menu.
695 String line = code_editor->get_text_edit()->get_line(bookmark_list[i]).replace("\t", " ").strip_edges();
696
697 // Limit the size of the line if too big.
698 if (line.length() > 50) {
699 line = line.substr(0, 50);
700 }
701
702 bookmarks_menu->add_item(String::num((int)bookmark_list[i] + 1) + " - `" + line + "`");
703 bookmarks_menu->set_item_metadata(bookmarks_menu->get_item_count() - 1, bookmark_list[i]);
704 }
705 }
706
_bookmark_item_pressed(int p_idx)707 void ScriptTextEditor::_bookmark_item_pressed(int p_idx) {
708
709 if (p_idx < 4) { // Any item before the separator.
710 _edit_option(bookmarks_menu->get_item_id(p_idx));
711 } else {
712 code_editor->goto_line(bookmarks_menu->get_item_metadata(p_idx));
713 code_editor->get_text_edit()->call_deferred("center_viewport_to_cursor"); //Need to be deferred, because goto uses call_deferred().
714 }
715 }
716
_find_all_node_for_script(Node * p_base,Node * p_current,const Ref<Script> & p_script)717 static Vector<Node *> _find_all_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
718
719 Vector<Node *> nodes;
720
721 if (p_current->get_owner() != p_base && p_base != p_current) {
722 return nodes;
723 }
724
725 Ref<Script> c = p_current->get_script();
726 if (c == p_script) {
727 nodes.push_back(p_current);
728 }
729
730 for (int i = 0; i < p_current->get_child_count(); i++) {
731 Vector<Node *> found = _find_all_node_for_script(p_base, p_current->get_child(i), p_script);
732 nodes.append_array(found);
733 }
734
735 return nodes;
736 }
737
_find_node_for_script(Node * p_base,Node * p_current,const Ref<Script> & p_script)738 static Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
739
740 if (p_current->get_owner() != p_base && p_base != p_current)
741 return NULL;
742 Ref<Script> c = p_current->get_script();
743 if (c == p_script)
744 return p_current;
745 for (int i = 0; i < p_current->get_child_count(); i++) {
746 Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
747 if (found)
748 return found;
749 }
750
751 return NULL;
752 }
753
_find_changed_scripts_for_external_editor(Node * p_base,Node * p_current,Set<Ref<Script>> & r_scripts)754 static void _find_changed_scripts_for_external_editor(Node *p_base, Node *p_current, Set<Ref<Script> > &r_scripts) {
755
756 if (p_current->get_owner() != p_base && p_base != p_current)
757 return;
758 Ref<Script> c = p_current->get_script();
759
760 if (c.is_valid())
761 r_scripts.insert(c);
762
763 for (int i = 0; i < p_current->get_child_count(); i++) {
764 _find_changed_scripts_for_external_editor(p_base, p_current->get_child(i), r_scripts);
765 }
766 }
767
_update_modified_scripts_for_external_editor(Ref<Script> p_for_script)768 void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
769
770 if (!bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor")))
771 return;
772
773 ERR_FAIL_COND(!get_tree());
774
775 Set<Ref<Script> > scripts;
776
777 Node *base = get_tree()->get_edited_scene_root();
778 if (base) {
779 _find_changed_scripts_for_external_editor(base, base, scripts);
780 }
781
782 for (Set<Ref<Script> >::Element *E = scripts.front(); E; E = E->next()) {
783
784 Ref<Script> script = E->get();
785
786 if (p_for_script.is_valid() && p_for_script != script)
787 continue;
788
789 if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
790
791 continue; //internal script, who cares, though weird
792 }
793
794 uint64_t last_date = script->get_last_modified_time();
795 uint64_t date = FileAccess::get_modified_time(script->get_path());
796
797 if (last_date != date) {
798
799 Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_class(), true);
800 ERR_CONTINUE(!rel_script.is_valid());
801 script->set_source_code(rel_script->get_source_code());
802 script->set_last_modified_time(rel_script->get_last_modified_time());
803 script->update_exports();
804 }
805 }
806 }
807
_code_complete_scripts(void * p_ud,const String & p_code,List<ScriptCodeCompletionOption> * r_options,bool & r_force)808 void ScriptTextEditor::_code_complete_scripts(void *p_ud, const String &p_code, List<ScriptCodeCompletionOption> *r_options, bool &r_force) {
809
810 ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
811 ste->_code_complete_script(p_code, r_options, r_force);
812 }
813
_code_complete_script(const String & p_code,List<ScriptCodeCompletionOption> * r_options,bool & r_force)814 void ScriptTextEditor::_code_complete_script(const String &p_code, List<ScriptCodeCompletionOption> *r_options, bool &r_force) {
815
816 if (color_panel->is_visible_in_tree()) return;
817 Node *base = get_tree()->get_edited_scene_root();
818 if (base) {
819 base = _find_node_for_script(base, base, script);
820 }
821 String hint;
822 Error err = script->get_language()->complete_code(p_code, script->get_path(), base, r_options, r_force, hint);
823 if (err == OK) {
824 code_editor->get_text_edit()->set_code_hint(hint);
825 }
826 }
827
_update_breakpoint_list()828 void ScriptTextEditor::_update_breakpoint_list() {
829
830 breakpoints_menu->clear();
831 breakpoints_menu->set_size(Size2(1, 1));
832
833 breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
834 breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
835 breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
836 breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
837
838 Array breakpoint_list = code_editor->get_text_edit()->get_breakpoints_array();
839 if (breakpoint_list.size() == 0) {
840 return;
841 }
842
843 breakpoints_menu->add_separator();
844
845 for (int i = 0; i < breakpoint_list.size(); i++) {
846 // Strip edges to remove spaces or tabs.
847 // Also replace any tabs by spaces, since we can't print tabs in the menu.
848 String line = code_editor->get_text_edit()->get_line(breakpoint_list[i]).replace("\t", " ").strip_edges();
849
850 // Limit the size of the line if too big.
851 if (line.length() > 50) {
852 line = line.substr(0, 50);
853 }
854
855 breakpoints_menu->add_item(String::num((int)breakpoint_list[i] + 1) + " - `" + line + "`");
856 breakpoints_menu->set_item_metadata(breakpoints_menu->get_item_count() - 1, breakpoint_list[i]);
857 }
858 }
859
_breakpoint_item_pressed(int p_idx)860 void ScriptTextEditor::_breakpoint_item_pressed(int p_idx) {
861
862 if (p_idx < 4) { // Any item before the separator.
863 _edit_option(breakpoints_menu->get_item_id(p_idx));
864 } else {
865 code_editor->goto_line(breakpoints_menu->get_item_metadata(p_idx));
866 code_editor->get_text_edit()->call_deferred("center_viewport_to_cursor"); //Need to be deferred, because goto uses call_deferred().
867 }
868 }
869
_breakpoint_toggled(int p_row)870 void ScriptTextEditor::_breakpoint_toggled(int p_row) {
871
872 ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(script->get_path(), p_row + 1, code_editor->get_text_edit()->is_line_set_as_breakpoint(p_row));
873 }
874
_lookup_symbol(const String & p_symbol,int p_row,int p_column)875 void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_column) {
876
877 Node *base = get_tree()->get_edited_scene_root();
878 if (base) {
879 base = _find_node_for_script(base, base, script);
880 }
881
882 ScriptLanguage::LookupResult result;
883 if (ScriptServer::is_global_class(p_symbol)) {
884 EditorNode::get_singleton()->load_resource(ScriptServer::get_global_class_path(p_symbol));
885 } else if (p_symbol.is_resource_file()) {
886 List<String> scene_extensions;
887 ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
888
889 if (scene_extensions.find(p_symbol.get_extension())) {
890 EditorNode::get_singleton()->load_scene(p_symbol);
891 } else {
892 EditorNode::get_singleton()->load_resource(p_symbol);
893 }
894
895 } else if (script->get_language()->lookup_code(code_editor->get_text_edit()->get_text_for_lookup_completion(), p_symbol, script->get_path(), base, result) == OK) {
896
897 _goto_line(p_row);
898
899 result.class_name = result.class_name.trim_prefix("_");
900
901 switch (result.type) {
902 case ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION: {
903
904 if (result.script.is_valid()) {
905 emit_signal("request_open_script_at_line", result.script, result.location - 1);
906 } else {
907 emit_signal("request_save_history");
908 _goto_line(result.location - 1);
909 }
910 } break;
911 case ScriptLanguage::LookupResult::RESULT_CLASS: {
912 emit_signal("go_to_help", "class_name:" + result.class_name);
913 } break;
914 case ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT: {
915
916 StringName cname = result.class_name;
917 bool success;
918 while (true) {
919 ClassDB::get_integer_constant(cname, result.class_member, &success);
920 if (success) {
921 result.class_name = cname;
922 cname = ClassDB::get_parent_class(cname);
923 } else {
924 break;
925 }
926 }
927
928 emit_signal("go_to_help", "class_constant:" + result.class_name + ":" + result.class_member);
929
930 } break;
931 case ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY: {
932 emit_signal("go_to_help", "class_property:" + result.class_name + ":" + result.class_member);
933
934 } break;
935 case ScriptLanguage::LookupResult::RESULT_CLASS_METHOD: {
936
937 StringName cname = result.class_name;
938
939 while (true) {
940 if (ClassDB::has_method(cname, result.class_member)) {
941 result.class_name = cname;
942 cname = ClassDB::get_parent_class(cname);
943 } else {
944 break;
945 }
946 }
947
948 emit_signal("go_to_help", "class_method:" + result.class_name + ":" + result.class_member);
949
950 } break;
951 case ScriptLanguage::LookupResult::RESULT_CLASS_ENUM: {
952
953 StringName cname = result.class_name;
954 StringName success;
955 while (true) {
956 success = ClassDB::get_integer_constant_enum(cname, result.class_member, true);
957 if (success != StringName()) {
958 result.class_name = cname;
959 cname = ClassDB::get_parent_class(cname);
960 } else {
961 break;
962 }
963 }
964
965 emit_signal("go_to_help", "class_enum:" + result.class_name + ":" + result.class_member);
966
967 } break;
968 case ScriptLanguage::LookupResult::RESULT_CLASS_TBD_GLOBALSCOPE: {
969 emit_signal("go_to_help", "class_global:" + result.class_name + ":" + result.class_member);
970 } break;
971 }
972 } else if (ProjectSettings::get_singleton()->has_setting("autoload/" + p_symbol)) {
973 //check for Autoload scenes
974 String path = ProjectSettings::get_singleton()->get("autoload/" + p_symbol);
975 if (path.begins_with("*")) {
976 path = path.substr(1, path.length());
977 EditorNode::get_singleton()->load_scene(path);
978 }
979 } else if (p_symbol.is_rel_path()) {
980 // Every symbol other than absolute path is relative path so keep this condition at last.
981 String path = _get_absolute_path(p_symbol);
982 if (FileAccess::exists(path)) {
983 List<String> scene_extensions;
984 ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
985
986 if (scene_extensions.find(path.get_extension())) {
987 EditorNode::get_singleton()->load_scene(path);
988 } else {
989 EditorNode::get_singleton()->load_resource(path);
990 }
991 }
992 }
993 }
994
_get_absolute_path(const String & rel_path)995 String ScriptTextEditor::_get_absolute_path(const String &rel_path) {
996 String base_path = script->get_path().get_base_dir();
997 String path = base_path.plus_file(rel_path);
998 return path.replace("///", "//").simplify_path();
999 }
1000
update_toggle_scripts_button()1001 void ScriptTextEditor::update_toggle_scripts_button() {
1002 if (code_editor != NULL) {
1003 code_editor->update_toggle_scripts_button();
1004 }
1005 }
1006
_update_connected_methods()1007 void ScriptTextEditor::_update_connected_methods() {
1008 TextEdit *text_edit = code_editor->get_text_edit();
1009 text_edit->clear_info_icons();
1010 missing_connections.clear();
1011
1012 if (!script_is_valid) {
1013 return;
1014 }
1015
1016 Node *base = get_tree()->get_edited_scene_root();
1017 if (!base) {
1018 return;
1019 }
1020
1021 Vector<Node *> nodes = _find_all_node_for_script(base, base, script);
1022 Set<StringName> methods_found;
1023 for (int i = 0; i < nodes.size(); i++) {
1024 List<Connection> connections;
1025 nodes[i]->get_signals_connected_to_this(&connections);
1026
1027 for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
1028 Connection connection = E->get();
1029 if (!(connection.flags & CONNECT_PERSIST)) {
1030 continue;
1031 }
1032
1033 // As deleted nodes are still accessible via the undo/redo system, check if they're still on the tree.
1034 Node *source = Object::cast_to<Node>(connection.source);
1035 if (source && !source->is_inside_tree()) {
1036 continue;
1037 }
1038
1039 if (methods_found.has(connection.method)) {
1040 continue;
1041 }
1042
1043 if (!ClassDB::has_method(script->get_instance_base_type(), connection.method)) {
1044 int line = -1;
1045
1046 for (int j = 0; j < functions.size(); j++) {
1047 String name = functions[j].get_slice(":", 0);
1048 if (name == connection.method) {
1049 line = functions[j].get_slice(":", 1).to_int();
1050 text_edit->set_line_info_icon(line - 1, get_parent_control()->get_icon("Slot", "EditorIcons"), connection.method);
1051 methods_found.insert(connection.method);
1052 break;
1053 }
1054 }
1055
1056 if (line >= 0) {
1057 continue;
1058 }
1059
1060 // There is a chance that the method is inherited from another script.
1061 bool found_inherited_function = false;
1062 Ref<Script> inherited_script = script->get_base_script();
1063 while (!inherited_script.is_null()) {
1064 if (inherited_script->has_method(connection.method)) {
1065 found_inherited_function = true;
1066 break;
1067 }
1068
1069 inherited_script = inherited_script->get_base_script();
1070 }
1071
1072 if (!found_inherited_function) {
1073 missing_connections.push_back(connection);
1074 }
1075 }
1076 }
1077 }
1078 }
1079
_lookup_connections(int p_row,String p_method)1080 void ScriptTextEditor::_lookup_connections(int p_row, String p_method) {
1081 Node *base = get_tree()->get_edited_scene_root();
1082 if (!base) {
1083 return;
1084 }
1085
1086 Vector<Node *> nodes = _find_all_node_for_script(base, base, script);
1087 connection_info_dialog->popup_connections(p_method, nodes);
1088 }
1089
_edit_option(int p_op)1090 void ScriptTextEditor::_edit_option(int p_op) {
1091
1092 TextEdit *tx = code_editor->get_text_edit();
1093
1094 switch (p_op) {
1095 case EDIT_UNDO: {
1096
1097 tx->undo();
1098 tx->call_deferred("grab_focus");
1099 } break;
1100 case EDIT_REDO: {
1101
1102 tx->redo();
1103 tx->call_deferred("grab_focus");
1104 } break;
1105 case EDIT_CUT: {
1106
1107 tx->cut();
1108 tx->call_deferred("grab_focus");
1109 } break;
1110 case EDIT_COPY: {
1111
1112 tx->copy();
1113 tx->call_deferred("grab_focus");
1114 } break;
1115 case EDIT_PASTE: {
1116
1117 tx->paste();
1118 tx->call_deferred("grab_focus");
1119 } break;
1120 case EDIT_SELECT_ALL: {
1121
1122 tx->select_all();
1123 tx->call_deferred("grab_focus");
1124 } break;
1125 case EDIT_MOVE_LINE_UP: {
1126
1127 code_editor->move_lines_up();
1128 } break;
1129 case EDIT_MOVE_LINE_DOWN: {
1130
1131 code_editor->move_lines_down();
1132 } break;
1133 case EDIT_INDENT_LEFT: {
1134
1135 Ref<Script> scr = script;
1136 if (scr.is_null())
1137 return;
1138
1139 tx->indent_left();
1140 } break;
1141 case EDIT_INDENT_RIGHT: {
1142
1143 Ref<Script> scr = script;
1144 if (scr.is_null())
1145 return;
1146
1147 tx->indent_right();
1148 } break;
1149 case EDIT_DELETE_LINE: {
1150
1151 code_editor->delete_lines();
1152 } break;
1153 case EDIT_CLONE_DOWN: {
1154
1155 code_editor->clone_lines_down();
1156 } break;
1157 case EDIT_TOGGLE_FOLD_LINE: {
1158
1159 tx->toggle_fold_line(tx->cursor_get_line());
1160 tx->update();
1161 } break;
1162 case EDIT_FOLD_ALL_LINES: {
1163
1164 tx->fold_all_lines();
1165 tx->update();
1166 } break;
1167 case EDIT_UNFOLD_ALL_LINES: {
1168
1169 tx->unhide_all_lines();
1170 tx->update();
1171 } break;
1172 case EDIT_TOGGLE_COMMENT: {
1173
1174 _edit_option_toggle_inline_comment();
1175 } break;
1176 case EDIT_COMPLETE: {
1177
1178 tx->query_code_comple();
1179 } break;
1180 case EDIT_AUTO_INDENT: {
1181
1182 String text = tx->get_text();
1183 Ref<Script> scr = script;
1184 if (scr.is_null())
1185 return;
1186
1187 tx->begin_complex_operation();
1188 int begin, end;
1189 if (tx->is_selection_active()) {
1190 begin = tx->get_selection_from_line();
1191 end = tx->get_selection_to_line();
1192 // ignore if the cursor is not past the first column
1193 if (tx->get_selection_to_column() == 0) {
1194 end--;
1195 }
1196 } else {
1197 begin = 0;
1198 end = tx->get_line_count() - 1;
1199 }
1200 scr->get_language()->auto_indent_code(text, begin, end);
1201 Vector<String> lines = text.split("\n");
1202 for (int i = begin; i <= end; ++i) {
1203 tx->set_line(i, lines[i]);
1204 }
1205
1206 tx->end_complex_operation();
1207 } break;
1208 case EDIT_TRIM_TRAILING_WHITESAPCE: {
1209
1210 trim_trailing_whitespace();
1211 } break;
1212 case EDIT_CONVERT_INDENT_TO_SPACES: {
1213
1214 convert_indent_to_spaces();
1215 } break;
1216 case EDIT_CONVERT_INDENT_TO_TABS: {
1217
1218 convert_indent_to_tabs();
1219 } break;
1220 case EDIT_PICK_COLOR: {
1221
1222 color_panel->popup();
1223 } break;
1224 case EDIT_TO_UPPERCASE: {
1225
1226 _convert_case(CodeTextEditor::UPPER);
1227 } break;
1228 case EDIT_TO_LOWERCASE: {
1229
1230 _convert_case(CodeTextEditor::LOWER);
1231 } break;
1232 case EDIT_CAPITALIZE: {
1233
1234 _convert_case(CodeTextEditor::CAPITALIZE);
1235 } break;
1236 case EDIT_EVALUATE: {
1237
1238 Expression expression;
1239 Vector<String> lines = code_editor->get_text_edit()->get_selection_text().split("\n");
1240 PoolStringArray results;
1241
1242 for (int i = 0; i < lines.size(); i++) {
1243 String line = lines[i];
1244 String whitespace = line.substr(0, line.size() - line.strip_edges(true, false).size()); //extract the whitespace at the beginning
1245
1246 if (expression.parse(line) == OK) {
1247 Variant result = expression.execute(Array(), Variant(), false);
1248 if (expression.get_error_text() == "") {
1249 results.append(whitespace + result.get_construct_string());
1250 } else {
1251 results.append(line);
1252 }
1253 } else {
1254 results.append(line);
1255 }
1256 }
1257
1258 code_editor->get_text_edit()->begin_complex_operation(); //prevents creating a two-step undo
1259 code_editor->get_text_edit()->insert_text_at_cursor(results.join("\n"));
1260 code_editor->get_text_edit()->end_complex_operation();
1261 } break;
1262 case SEARCH_FIND: {
1263
1264 code_editor->get_find_replace_bar()->popup_search();
1265 } break;
1266 case SEARCH_FIND_NEXT: {
1267
1268 code_editor->get_find_replace_bar()->search_next();
1269 } break;
1270 case SEARCH_FIND_PREV: {
1271
1272 code_editor->get_find_replace_bar()->search_prev();
1273 } break;
1274 case SEARCH_REPLACE: {
1275
1276 code_editor->get_find_replace_bar()->popup_replace();
1277 } break;
1278 case SEARCH_IN_FILES: {
1279
1280 String selected_text = code_editor->get_text_edit()->get_selection_text();
1281
1282 // Yep, because it doesn't make sense to instance this dialog for every single script open...
1283 // So this will be delegated to the ScriptEditor.
1284 emit_signal("search_in_files_requested", selected_text);
1285 } break;
1286 case SEARCH_LOCATE_FUNCTION: {
1287
1288 quick_open->popup_dialog(get_functions());
1289 quick_open->set_title(TTR("Go to Function"));
1290 } break;
1291 case SEARCH_GOTO_LINE: {
1292
1293 goto_line_dialog->popup_find_line(tx);
1294 } break;
1295 case BOOKMARK_TOGGLE: {
1296
1297 code_editor->toggle_bookmark();
1298 } break;
1299 case BOOKMARK_GOTO_NEXT: {
1300
1301 code_editor->goto_next_bookmark();
1302 } break;
1303 case BOOKMARK_GOTO_PREV: {
1304
1305 code_editor->goto_prev_bookmark();
1306 } break;
1307 case BOOKMARK_REMOVE_ALL: {
1308
1309 code_editor->remove_all_bookmarks();
1310 } break;
1311 case DEBUG_TOGGLE_BREAKPOINT: {
1312
1313 int line = tx->cursor_get_line();
1314 bool dobreak = !tx->is_line_set_as_breakpoint(line);
1315 tx->set_line_as_breakpoint(line, dobreak);
1316 ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(script->get_path(), line + 1, dobreak);
1317 } break;
1318 case DEBUG_REMOVE_ALL_BREAKPOINTS: {
1319
1320 List<int> bpoints;
1321 tx->get_breakpoints(&bpoints);
1322
1323 for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
1324 int line = E->get();
1325 bool dobreak = !tx->is_line_set_as_breakpoint(line);
1326 tx->set_line_as_breakpoint(line, dobreak);
1327 ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(script->get_path(), line + 1, dobreak);
1328 }
1329 } break;
1330 case DEBUG_GOTO_NEXT_BREAKPOINT: {
1331
1332 List<int> bpoints;
1333 tx->get_breakpoints(&bpoints);
1334 if (bpoints.size() <= 0) {
1335 return;
1336 }
1337
1338 int line = tx->cursor_get_line();
1339
1340 // wrap around
1341 if (line >= bpoints[bpoints.size() - 1]) {
1342 tx->unfold_line(bpoints[0]);
1343 tx->cursor_set_line(bpoints[0]);
1344 tx->center_viewport_to_cursor();
1345 } else {
1346 for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
1347 int bline = E->get();
1348 if (bline > line) {
1349 tx->unfold_line(bline);
1350 tx->cursor_set_line(bline);
1351 tx->center_viewport_to_cursor();
1352 return;
1353 }
1354 }
1355 }
1356
1357 } break;
1358 case DEBUG_GOTO_PREV_BREAKPOINT: {
1359
1360 List<int> bpoints;
1361 tx->get_breakpoints(&bpoints);
1362 if (bpoints.size() <= 0) {
1363 return;
1364 }
1365
1366 int line = tx->cursor_get_line();
1367 // wrap around
1368 if (line <= bpoints[0]) {
1369 tx->unfold_line(bpoints[bpoints.size() - 1]);
1370 tx->cursor_set_line(bpoints[bpoints.size() - 1]);
1371 tx->center_viewport_to_cursor();
1372 } else {
1373 for (List<int>::Element *E = bpoints.back(); E; E = E->prev()) {
1374 int bline = E->get();
1375 if (bline < line) {
1376 tx->unfold_line(bline);
1377 tx->cursor_set_line(bline);
1378 tx->center_viewport_to_cursor();
1379 return;
1380 }
1381 }
1382 }
1383
1384 } break;
1385 case HELP_CONTEXTUAL: {
1386
1387 String text = tx->get_selection_text();
1388 if (text == "")
1389 text = tx->get_word_under_cursor();
1390 if (text != "") {
1391 emit_signal("request_help", text);
1392 }
1393 } break;
1394 case LOOKUP_SYMBOL: {
1395
1396 String text = tx->get_word_under_cursor();
1397 if (text == "")
1398 text = tx->get_selection_text();
1399 if (text != "") {
1400 _lookup_symbol(text, tx->cursor_get_line(), tx->cursor_get_column());
1401 }
1402 } break;
1403 }
1404 }
1405
_edit_option_toggle_inline_comment()1406 void ScriptTextEditor::_edit_option_toggle_inline_comment() {
1407 if (script.is_null())
1408 return;
1409
1410 String delimiter = "#";
1411 List<String> comment_delimiters;
1412 script->get_language()->get_comment_delimiters(&comment_delimiters);
1413
1414 for (List<String>::Element *E = comment_delimiters.front(); E; E = E->next()) {
1415 String script_delimiter = E->get();
1416 if (script_delimiter.find(" ") == -1) {
1417 delimiter = script_delimiter;
1418 break;
1419 }
1420 }
1421
1422 code_editor->toggle_inline_comment(delimiter);
1423 }
1424
add_syntax_highlighter(SyntaxHighlighter * p_highlighter)1425 void ScriptTextEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
1426 highlighters[p_highlighter->get_name()] = p_highlighter;
1427 highlighter_menu->add_radio_check_item(p_highlighter->get_name());
1428 }
1429
set_syntax_highlighter(SyntaxHighlighter * p_highlighter)1430 void ScriptTextEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
1431 TextEdit *te = code_editor->get_text_edit();
1432 te->_set_syntax_highlighting(p_highlighter);
1433 if (p_highlighter != NULL)
1434 highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(p_highlighter->get_name()), true);
1435 else
1436 highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(TTR("Standard")), true);
1437 }
1438
_change_syntax_highlighter(int p_idx)1439 void ScriptTextEditor::_change_syntax_highlighter(int p_idx) {
1440 Map<String, SyntaxHighlighter *>::Element *el = highlighters.front();
1441 while (el != NULL) {
1442 highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(el->key()), false);
1443 el = el->next();
1444 }
1445 // highlighter_menu->set_item_checked(p_idx, true);
1446 set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]);
1447 }
1448
_bind_methods()1449 void ScriptTextEditor::_bind_methods() {
1450
1451 ClassDB::bind_method("_validate_script", &ScriptTextEditor::_validate_script);
1452 ClassDB::bind_method("_update_bookmark_list", &ScriptTextEditor::_update_bookmark_list);
1453 ClassDB::bind_method("_bookmark_item_pressed", &ScriptTextEditor::_bookmark_item_pressed);
1454 ClassDB::bind_method("_load_theme_settings", &ScriptTextEditor::_load_theme_settings);
1455 ClassDB::bind_method("_update_breakpoint_list", &ScriptTextEditor::_update_breakpoint_list);
1456 ClassDB::bind_method("_breakpoint_item_pressed", &ScriptTextEditor::_breakpoint_item_pressed);
1457 ClassDB::bind_method("_breakpoint_toggled", &ScriptTextEditor::_breakpoint_toggled);
1458 ClassDB::bind_method("_lookup_connections", &ScriptTextEditor::_lookup_connections);
1459 ClassDB::bind_method("_update_connected_methods", &ScriptTextEditor::_update_connected_methods);
1460 ClassDB::bind_method("_change_syntax_highlighter", &ScriptTextEditor::_change_syntax_highlighter);
1461 ClassDB::bind_method("_edit_option", &ScriptTextEditor::_edit_option);
1462 ClassDB::bind_method("_goto_line", &ScriptTextEditor::_goto_line);
1463 ClassDB::bind_method("_lookup_symbol", &ScriptTextEditor::_lookup_symbol);
1464 ClassDB::bind_method("_text_edit_gui_input", &ScriptTextEditor::_text_edit_gui_input);
1465 ClassDB::bind_method("_show_warnings_panel", &ScriptTextEditor::_show_warnings_panel);
1466 ClassDB::bind_method("_warning_clicked", &ScriptTextEditor::_warning_clicked);
1467 ClassDB::bind_method("_color_changed", &ScriptTextEditor::_color_changed);
1468
1469 ClassDB::bind_method("get_drag_data_fw", &ScriptTextEditor::get_drag_data_fw);
1470 ClassDB::bind_method("can_drop_data_fw", &ScriptTextEditor::can_drop_data_fw);
1471 ClassDB::bind_method("drop_data_fw", &ScriptTextEditor::drop_data_fw);
1472 }
1473
get_edit_menu()1474 Control *ScriptTextEditor::get_edit_menu() {
1475
1476 return edit_hb;
1477 }
1478
clear_edit_menu()1479 void ScriptTextEditor::clear_edit_menu() {
1480 memdelete(edit_hb);
1481 }
1482
reload(bool p_soft)1483 void ScriptTextEditor::reload(bool p_soft) {
1484
1485 TextEdit *te = code_editor->get_text_edit();
1486 Ref<Script> scr = script;
1487 if (scr.is_null())
1488 return;
1489 scr->set_source_code(te->get_text());
1490 bool soft = p_soft || scr->get_instance_base_type() == "EditorPlugin"; //always soft-reload editor plugins
1491
1492 scr->get_language()->reload_tool_script(scr, soft);
1493 }
1494
get_breakpoints(List<int> * p_breakpoints)1495 void ScriptTextEditor::get_breakpoints(List<int> *p_breakpoints) {
1496
1497 code_editor->get_text_edit()->get_breakpoints(p_breakpoints);
1498 }
1499
set_tooltip_request_func(String p_method,Object * p_obj)1500 void ScriptTextEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
1501
1502 code_editor->get_text_edit()->set_tooltip_request_func(p_obj, p_method, this);
1503 }
1504
set_debugger_active(bool p_active)1505 void ScriptTextEditor::set_debugger_active(bool p_active) {
1506 }
1507
get_drag_data_fw(const Point2 & p_point,Control * p_from)1508 Variant ScriptTextEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
1509
1510 return Variant();
1511 }
1512
can_drop_data_fw(const Point2 & p_point,const Variant & p_data,Control * p_from) const1513 bool ScriptTextEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
1514
1515 Dictionary d = p_data;
1516 if (d.has("type") && (String(d["type"]) == "resource" ||
1517 String(d["type"]) == "files" ||
1518 String(d["type"]) == "nodes" ||
1519 String(d["type"]) == "files_and_dirs")) {
1520
1521 return true;
1522 }
1523
1524 return false;
1525 }
1526
_find_script_node(Node * p_edited_scene,Node * p_current_node,const Ref<Script> & script)1527 static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
1528
1529 if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene)
1530 return NULL;
1531
1532 Ref<Script> scr = p_current_node->get_script();
1533
1534 if (scr.is_valid() && scr == script)
1535 return p_current_node;
1536
1537 for (int i = 0; i < p_current_node->get_child_count(); i++) {
1538 Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
1539 if (n)
1540 return n;
1541 }
1542
1543 return NULL;
1544 }
1545
drop_data_fw(const Point2 & p_point,const Variant & p_data,Control * p_from)1546 void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
1547
1548 Dictionary d = p_data;
1549
1550 TextEdit *te = code_editor->get_text_edit();
1551 int row, col;
1552 te->_get_mouse_pos(p_point, row, col);
1553
1554 if (d.has("type") && String(d["type"]) == "resource") {
1555
1556 Ref<Resource> res = d["resource"];
1557 if (!res.is_valid()) {
1558 return;
1559 }
1560
1561 if (res->get_path().is_resource_file()) {
1562 EditorNode::get_singleton()->show_warning(TTR("Only resources from filesystem can be dropped."));
1563 return;
1564 }
1565
1566 te->cursor_set_line(row);
1567 te->cursor_set_column(col);
1568 te->insert_text_at_cursor(res->get_path());
1569 }
1570
1571 if (d.has("type") && (String(d["type"]) == "files" || String(d["type"]) == "files_and_dirs")) {
1572
1573 Array files = d["files"];
1574
1575 String text_to_drop;
1576 for (int i = 0; i < files.size(); i++) {
1577
1578 if (i > 0)
1579 text_to_drop += ",";
1580 text_to_drop += "\"" + String(files[i]).c_escape() + "\"";
1581 }
1582
1583 te->cursor_set_line(row);
1584 te->cursor_set_column(col);
1585 te->insert_text_at_cursor(text_to_drop);
1586 }
1587
1588 if (d.has("type") && String(d["type"]) == "nodes") {
1589
1590 Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
1591
1592 if (!sn) {
1593 EditorNode::get_singleton()->show_warning(vformat(TTR("Can't drop nodes because script '%s' is not used in this scene."), get_name()));
1594 return;
1595 }
1596
1597 Array nodes = d["nodes"];
1598 String text_to_drop;
1599 for (int i = 0; i < nodes.size(); i++) {
1600
1601 if (i > 0)
1602 text_to_drop += ",";
1603
1604 NodePath np = nodes[i];
1605 Node *node = get_node(np);
1606 if (!node) {
1607 continue;
1608 }
1609
1610 String path = sn->get_path_to(node);
1611 text_to_drop += "\"" + path.c_escape() + "\"";
1612 }
1613
1614 te->cursor_set_line(row);
1615 te->cursor_set_column(col);
1616 te->insert_text_at_cursor(text_to_drop);
1617 }
1618 }
1619
_text_edit_gui_input(const Ref<InputEvent> & ev)1620 void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
1621
1622 Ref<InputEventMouseButton> mb = ev;
1623 Ref<InputEventKey> k = ev;
1624 Point2 local_pos;
1625 bool create_menu = false;
1626
1627 TextEdit *tx = code_editor->get_text_edit();
1628 if (mb.is_valid() && mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
1629 local_pos = mb->get_global_position() - tx->get_global_position();
1630 create_menu = true;
1631 } else if (k.is_valid() && k->get_scancode() == KEY_MENU) {
1632 local_pos = tx->_get_cursor_pixel_pos();
1633 create_menu = true;
1634 }
1635
1636 if (create_menu) {
1637 int col, row;
1638 tx->_get_mouse_pos(local_pos, row, col);
1639
1640 tx->set_right_click_moves_caret(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret"));
1641 if (tx->is_right_click_moving_caret()) {
1642 if (tx->is_selection_active()) {
1643 int from_line = tx->get_selection_from_line();
1644 int to_line = tx->get_selection_to_line();
1645 int from_column = tx->get_selection_from_column();
1646 int to_column = tx->get_selection_to_column();
1647
1648 if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
1649 // Right click is outside the selected text
1650 tx->deselect();
1651 }
1652 }
1653 if (!tx->is_selection_active()) {
1654 tx->cursor_set_line(row, true, false);
1655 tx->cursor_set_column(col);
1656 }
1657 }
1658
1659 String word_at_pos = tx->get_word_at_pos(local_pos);
1660 if (word_at_pos == "")
1661 word_at_pos = tx->get_word_under_cursor();
1662 if (word_at_pos == "")
1663 word_at_pos = tx->get_selection_text();
1664
1665 bool has_color = (word_at_pos == "Color");
1666 bool foldable = tx->can_fold(row) || tx->is_folded(row);
1667 bool open_docs = false;
1668 bool goto_definition = false;
1669
1670 if (word_at_pos.is_resource_file()) {
1671 open_docs = true;
1672 } else {
1673 Node *base = get_tree()->get_edited_scene_root();
1674 if (base) {
1675 base = _find_node_for_script(base, base, script);
1676 }
1677 ScriptLanguage::LookupResult result;
1678 if (script->get_language()->lookup_code(code_editor->get_text_edit()->get_text_for_lookup_completion(), word_at_pos, script->get_path(), base, result) == OK) {
1679 open_docs = true;
1680 }
1681 }
1682
1683 if (has_color) {
1684 String line = tx->get_line(row);
1685 color_position.x = row;
1686 color_position.y = col;
1687
1688 int begin = 0;
1689 int end = 0;
1690 bool valid = false;
1691 for (int i = col; i < line.length(); i++) {
1692 if (line[i] == '(') {
1693 begin = i;
1694 continue;
1695 } else if (line[i] == ')') {
1696 end = i + 1;
1697 valid = true;
1698 break;
1699 }
1700 }
1701 if (valid) {
1702 color_args = line.substr(begin, end - begin);
1703 String stripped = color_args.replace(" ", "").replace("(", "").replace(")", "");
1704 Vector<float> color = stripped.split_floats(",");
1705 if (color.size() > 2) {
1706 float alpha = color.size() > 3 ? color[3] : 1.0f;
1707 color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));
1708 }
1709 color_panel->set_position(get_global_transform().xform(local_pos));
1710 } else {
1711 has_color = false;
1712 }
1713 }
1714 _make_context_menu(tx->is_selection_active(), has_color, foldable, open_docs, goto_definition, local_pos);
1715 }
1716 }
1717
_color_changed(const Color & p_color)1718 void ScriptTextEditor::_color_changed(const Color &p_color) {
1719 String new_args;
1720 if (p_color.a == 1.0f) {
1721 new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ")");
1722 } else {
1723 new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ", " + rtos(p_color.a) + ")");
1724 }
1725
1726 String line = code_editor->get_text_edit()->get_line(color_position.x);
1727 int color_args_pos = line.find(color_args, color_position.y);
1728 String line_with_replaced_args = line;
1729 line_with_replaced_args.erase(color_args_pos, color_args.length());
1730 line_with_replaced_args = line_with_replaced_args.insert(color_args_pos, new_args);
1731
1732 color_args = new_args;
1733 code_editor->get_text_edit()->begin_complex_operation();
1734 code_editor->get_text_edit()->set_line(color_position.x, line_with_replaced_args);
1735 code_editor->get_text_edit()->end_complex_operation();
1736 code_editor->get_text_edit()->update();
1737 }
1738
_make_context_menu(bool p_selection,bool p_color,bool p_foldable,bool p_open_docs,bool p_goto_definition,Vector2 p_pos)1739 void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_goto_definition, Vector2 p_pos) {
1740
1741 context_menu->clear();
1742 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
1743 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
1744
1745 context_menu->add_separator();
1746 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
1747 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
1748 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
1749
1750 context_menu->add_separator();
1751 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
1752
1753 context_menu->add_separator();
1754 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
1755 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
1756 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
1757 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
1758
1759 if (p_selection) {
1760 context_menu->add_separator();
1761 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
1762 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
1763 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
1764 }
1765 if (p_foldable)
1766 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
1767
1768 if (p_color || p_open_docs || p_goto_definition) {
1769 context_menu->add_separator();
1770 if (p_open_docs)
1771 context_menu->add_item(TTR("Lookup Symbol"), LOOKUP_SYMBOL);
1772 if (p_color)
1773 context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR);
1774 }
1775
1776 context_menu->set_position(get_global_transform().xform(p_pos));
1777 context_menu->set_size(Vector2(1, 1));
1778 context_menu->popup();
1779 }
1780
ScriptTextEditor()1781 ScriptTextEditor::ScriptTextEditor() {
1782
1783 theme_loaded = false;
1784 script_is_valid = false;
1785
1786 VSplitContainer *editor_box = memnew(VSplitContainer);
1787 add_child(editor_box);
1788 editor_box->set_anchors_and_margins_preset(Control::PRESET_WIDE);
1789 editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
1790
1791 code_editor = memnew(CodeTextEditor);
1792 editor_box->add_child(code_editor);
1793 code_editor->add_constant_override("separation", 2);
1794 code_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
1795 code_editor->connect("validate_script", this, "_validate_script");
1796 code_editor->connect("load_theme_settings", this, "_load_theme_settings");
1797 code_editor->set_code_complete_func(_code_complete_scripts, this);
1798 code_editor->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled");
1799 code_editor->get_text_edit()->connect("symbol_lookup", this, "_lookup_symbol");
1800 code_editor->get_text_edit()->connect("info_clicked", this, "_lookup_connections");
1801 code_editor->set_v_size_flags(SIZE_EXPAND_FILL);
1802 code_editor->show_toggle_scripts_button();
1803
1804 warnings_panel = memnew(RichTextLabel);
1805 editor_box->add_child(warnings_panel);
1806 warnings_panel->add_font_override(
1807 "normal_font", EditorNode::get_singleton()->get_gui_base()->get_font("main", "EditorFonts"));
1808 warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
1809 warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);
1810 warnings_panel->set_meta_underline(true);
1811 warnings_panel->set_selection_enabled(true);
1812 warnings_panel->set_focus_mode(FOCUS_CLICK);
1813 warnings_panel->hide();
1814
1815 code_editor->connect("show_warnings_panel", this, "_show_warnings_panel");
1816 warnings_panel->connect("meta_clicked", this, "_warning_clicked");
1817
1818 update_settings();
1819
1820 code_editor->get_text_edit()->set_callhint_settings(
1821 EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"),
1822 EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset"));
1823
1824 code_editor->get_text_edit()->set_select_identifiers_on_hover(true);
1825 code_editor->get_text_edit()->set_context_menu_enabled(false);
1826 code_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input");
1827
1828 context_menu = memnew(PopupMenu);
1829 add_child(context_menu);
1830 context_menu->connect("id_pressed", this, "_edit_option");
1831 context_menu->set_hide_on_window_lose_focus(true);
1832
1833 color_panel = memnew(PopupPanel);
1834 add_child(color_panel);
1835 color_picker = memnew(ColorPicker);
1836 color_picker->set_deferred_mode(true);
1837 color_panel->add_child(color_picker);
1838 color_picker->connect("color_changed", this, "_color_changed");
1839
1840 // get default color picker mode from editor settings
1841 int default_color_mode = EDITOR_GET("interface/inspector/default_color_picker_mode");
1842 if (default_color_mode == 1)
1843 color_picker->set_hsv_mode(true);
1844 else if (default_color_mode == 2)
1845 color_picker->set_raw_mode(true);
1846
1847 edit_hb = memnew(HBoxContainer);
1848
1849 edit_menu = memnew(MenuButton);
1850 edit_menu->set_text(TTR("Edit"));
1851 edit_menu->set_switch_on_hover(true);
1852 edit_menu->get_popup()->set_hide_on_window_lose_focus(true);
1853 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
1854 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
1855 edit_menu->get_popup()->add_separator();
1856 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
1857 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
1858 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
1859 edit_menu->get_popup()->add_separator();
1860 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
1861 edit_menu->get_popup()->add_separator();
1862 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
1863 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
1864 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
1865 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
1866 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
1867 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
1868 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
1869 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
1870 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
1871 edit_menu->get_popup()->add_separator();
1872 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
1873 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
1874 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
1875 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
1876 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
1877 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
1878 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
1879 edit_menu->get_popup()->connect("id_pressed", this, "_edit_option");
1880 edit_menu->get_popup()->add_separator();
1881
1882 PopupMenu *convert_case = memnew(PopupMenu);
1883 convert_case->set_name("convert_case");
1884 edit_menu->get_popup()->add_child(convert_case);
1885 edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
1886 convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase"), KEY_MASK_SHIFT | KEY_F4), EDIT_TO_UPPERCASE);
1887 convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase"), KEY_MASK_SHIFT | KEY_F5), EDIT_TO_LOWERCASE);
1888 convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize"), KEY_MASK_SHIFT | KEY_F6), EDIT_CAPITALIZE);
1889 convert_case->connect("id_pressed", this, "_edit_option");
1890
1891 highlighters[TTR("Standard")] = NULL;
1892 highlighter_menu = memnew(PopupMenu);
1893 highlighter_menu->set_name("highlighter_menu");
1894 edit_menu->get_popup()->add_child(highlighter_menu);
1895 edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "highlighter_menu");
1896 highlighter_menu->add_radio_check_item(TTR("Standard"));
1897 highlighter_menu->connect("id_pressed", this, "_change_syntax_highlighter");
1898
1899 search_menu = memnew(MenuButton);
1900 edit_hb->add_child(search_menu);
1901 search_menu->set_text(TTR("Search"));
1902 search_menu->set_switch_on_hover(true);
1903 search_menu->get_popup()->set_hide_on_window_lose_focus(true);
1904 search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
1905 search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
1906 search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
1907 search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
1908 search_menu->get_popup()->add_separator();
1909 search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_in_files"), SEARCH_IN_FILES);
1910 search_menu->get_popup()->add_separator();
1911 search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
1912 search_menu->get_popup()->connect("id_pressed", this, "_edit_option");
1913
1914 edit_hb->add_child(edit_menu);
1915
1916 MenuButton *goto_menu = memnew(MenuButton);
1917 edit_hb->add_child(goto_menu);
1918 goto_menu->set_text(TTR("Go To"));
1919 goto_menu->set_switch_on_hover(true);
1920 goto_menu->get_popup()->connect("id_pressed", this, "_edit_option");
1921
1922 goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
1923 goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
1924 goto_menu->get_popup()->add_separator();
1925
1926 bookmarks_menu = memnew(PopupMenu);
1927 bookmarks_menu->set_name("Bookmarks");
1928 goto_menu->get_popup()->add_child(bookmarks_menu);
1929 goto_menu->get_popup()->add_submenu_item(TTR("Bookmarks"), "Bookmarks");
1930 _update_bookmark_list();
1931 bookmarks_menu->connect("about_to_show", this, "_update_bookmark_list");
1932 bookmarks_menu->connect("index_pressed", this, "_bookmark_item_pressed");
1933
1934 breakpoints_menu = memnew(PopupMenu);
1935 breakpoints_menu->set_name("Breakpoints");
1936 goto_menu->get_popup()->add_child(breakpoints_menu);
1937 goto_menu->get_popup()->add_submenu_item(TTR("Breakpoints"), "Breakpoints");
1938 _update_breakpoint_list();
1939 breakpoints_menu->connect("about_to_show", this, "_update_breakpoint_list");
1940 breakpoints_menu->connect("index_pressed", this, "_breakpoint_item_pressed");
1941
1942 quick_open = memnew(ScriptEditorQuickOpen);
1943 add_child(quick_open);
1944 quick_open->connect("goto_line", this, "_goto_line");
1945
1946 goto_line_dialog = memnew(GotoLineDialog);
1947 add_child(goto_line_dialog);
1948
1949 connection_info_dialog = memnew(ConnectionInfoDialog);
1950 add_child(connection_info_dialog);
1951
1952 code_editor->get_text_edit()->set_drag_forwarding(this);
1953 }
1954
~ScriptTextEditor()1955 ScriptTextEditor::~ScriptTextEditor() {
1956 for (const Map<String, SyntaxHighlighter *>::Element *E = highlighters.front(); E; E = E->next()) {
1957 if (E->get() != NULL) {
1958 memdelete(E->get());
1959 }
1960 }
1961 highlighters.clear();
1962 }
1963
create_editor(const RES & p_resource)1964 static ScriptEditorBase *create_editor(const RES &p_resource) {
1965
1966 if (Object::cast_to<Script>(*p_resource)) {
1967 return memnew(ScriptTextEditor);
1968 }
1969 return NULL;
1970 }
1971
register_editor()1972 void ScriptTextEditor::register_editor() {
1973
1974 ED_SHORTCUT("script_text_editor/undo", TTR("Undo"), KEY_MASK_CMD | KEY_Z);
1975 ED_SHORTCUT("script_text_editor/redo", TTR("Redo"), KEY_MASK_CMD | KEY_Y);
1976 ED_SHORTCUT("script_text_editor/cut", TTR("Cut"), KEY_MASK_CMD | KEY_X);
1977 ED_SHORTCUT("script_text_editor/copy", TTR("Copy"), KEY_MASK_CMD | KEY_C);
1978 ED_SHORTCUT("script_text_editor/paste", TTR("Paste"), KEY_MASK_CMD | KEY_V);
1979 ED_SHORTCUT("script_text_editor/select_all", TTR("Select All"), KEY_MASK_CMD | KEY_A);
1980 ED_SHORTCUT("script_text_editor/move_up", TTR("Move Up"), KEY_MASK_ALT | KEY_UP);
1981 ED_SHORTCUT("script_text_editor/move_down", TTR("Move Down"), KEY_MASK_ALT | KEY_DOWN);
1982 ED_SHORTCUT("script_text_editor/delete_line", TTR("Delete Line"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_K);
1983
1984 // Leave these at zero, same can be accomplished with tab/shift-tab, including selection.
1985 // The next/previous in history shortcut in this case makes a lot more sense.
1986
1987 ED_SHORTCUT("script_text_editor/indent_left", TTR("Indent Left"), 0);
1988 ED_SHORTCUT("script_text_editor/indent_right", TTR("Indent Right"), 0);
1989 ED_SHORTCUT("script_text_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD | KEY_K);
1990 ED_SHORTCUT("script_text_editor/toggle_fold_line", TTR("Fold/Unfold Line"), KEY_MASK_ALT | KEY_F);
1991 ED_SHORTCUT("script_text_editor/fold_all_lines", TTR("Fold All Lines"), 0);
1992 ED_SHORTCUT("script_text_editor/unfold_all_lines", TTR("Unfold All Lines"), 0);
1993 #ifdef OSX_ENABLED
1994 ED_SHORTCUT("script_text_editor/clone_down", TTR("Clone Down"), KEY_MASK_SHIFT | KEY_MASK_CMD | KEY_C);
1995 ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL | KEY_SPACE);
1996 #else
1997 ED_SHORTCUT("script_text_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD | KEY_D);
1998 ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD | KEY_SPACE);
1999 #endif
2000 ED_SHORTCUT("script_text_editor/evaluate_selection", TTR("Evaluate Selection"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_E);
2001 ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CMD | KEY_MASK_ALT | KEY_T);
2002 ED_SHORTCUT("script_text_editor/convert_indent_to_spaces", TTR("Convert Indent to Spaces"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Y);
2003 ED_SHORTCUT("script_text_editor/convert_indent_to_tabs", TTR("Convert Indent to Tabs"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_I);
2004 ED_SHORTCUT("script_text_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD | KEY_I);
2005
2006 ED_SHORTCUT("script_text_editor/find", TTR("Find..."), KEY_MASK_CMD | KEY_F);
2007 #ifdef OSX_ENABLED
2008 ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_MASK_CMD | KEY_G);
2009 ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_G);
2010 ED_SHORTCUT("script_text_editor/replace", TTR("Replace..."), KEY_MASK_ALT | KEY_MASK_CMD | KEY_F);
2011 #else
2012 ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_F3);
2013 ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT | KEY_F3);
2014 ED_SHORTCUT("script_text_editor/replace", TTR("Replace..."), KEY_MASK_CMD | KEY_R);
2015 #endif
2016
2017 ED_SHORTCUT("script_text_editor/find_in_files", TTR("Find in Files..."), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_F);
2018
2019 #ifdef OSX_ENABLED
2020 ED_SHORTCUT("script_text_editor/contextual_help", TTR("Contextual Help"), KEY_MASK_ALT | KEY_MASK_SHIFT | KEY_SPACE);
2021 #else
2022 ED_SHORTCUT("script_text_editor/contextual_help", TTR("Contextual Help"), KEY_MASK_ALT | KEY_F1);
2023 #endif
2024
2025 ED_SHORTCUT("script_text_editor/toggle_bookmark", TTR("Toggle Bookmark"), KEY_MASK_CMD | KEY_MASK_ALT | KEY_B);
2026 ED_SHORTCUT("script_text_editor/goto_next_bookmark", TTR("Go to Next Bookmark"), KEY_MASK_CMD | KEY_B);
2027 ED_SHORTCUT("script_text_editor/goto_previous_bookmark", TTR("Go to Previous Bookmark"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_B);
2028 ED_SHORTCUT("script_text_editor/remove_all_bookmarks", TTR("Remove All Bookmarks"), 0);
2029
2030 #ifdef OSX_ENABLED
2031 ED_SHORTCUT("script_text_editor/goto_function", TTR("Go to Function..."), KEY_MASK_CTRL | KEY_MASK_CMD | KEY_J);
2032 #else
2033 ED_SHORTCUT("script_text_editor/goto_function", TTR("Go to Function..."), KEY_MASK_ALT | KEY_MASK_CMD | KEY_F);
2034 #endif
2035 ED_SHORTCUT("script_text_editor/goto_line", TTR("Go to Line..."), KEY_MASK_CMD | KEY_L);
2036
2037 #ifdef OSX_ENABLED
2038 ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_B);
2039 #else
2040 ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
2041 #endif
2042 ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_F9);
2043 ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTR("Go to Next Breakpoint"), KEY_MASK_CMD | KEY_PERIOD);
2044 ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTR("Go to Previous Breakpoint"), KEY_MASK_CMD | KEY_COMMA);
2045
2046 ScriptEditor::register_create_script_editor_function(create_editor);
2047 }
2048
validate()2049 void ScriptTextEditor::validate() {
2050 this->code_editor->validate_script();
2051 }
2052