| 1 | /**************************************************************************/ |
| 2 | /* script_text_editor.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "script_text_editor.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/io/dir_access.h" |
| 35 | #include "core/io/json.h" |
| 36 | #include "core/math/expression.h" |
| 37 | #include "core/os/keyboard.h" |
| 38 | #include "editor/debugger/editor_debugger_node.h" |
| 39 | #include "editor/doc/editor_help.h" |
| 40 | #include "editor/docks/filesystem_dock.h" |
| 41 | #include "editor/editor_interface.h" |
| 42 | #include "editor/editor_node.h" |
| 43 | #include "editor/editor_string_names.h" |
| 44 | #include "editor/gui/editor_toaster.h" |
| 45 | #include "editor/inspector/editor_context_menu_plugin.h" |
| 46 | #include "editor/inspector/editor_inspector.h" |
| 47 | #include "editor/inspector/multi_node_edit.h" |
| 48 | #include "editor/settings/editor_command_palette.h" |
| 49 | #include "editor/settings/editor_settings.h" |
| 50 | #include "editor/themes/editor_scale.h" |
| 51 | #include "scene/gui/grid_container.h" |
| 52 | #include "scene/gui/menu_button.h" |
| 53 | #include "scene/gui/rich_text_label.h" |
| 54 | #include "scene/gui/split_container.h" |
| 55 | |
| 56 | void ConnectionInfoDialog::ok_pressed() { |
| 57 | } |
| 58 | |
| 59 | void ConnectionInfoDialog::popup_connections(const String &p_method, const Vector<Node *> &p_nodes) { |
| 60 | method->set_text(p_method); |
| 61 | |
| 62 | tree->clear(); |
| 63 | TreeItem *root = tree->create_item(); |
| 64 | |
| 65 | for (int i = 0; i < p_nodes.size(); i++) { |
| 66 | List<Connection> all_connections; |
| 67 | p_nodes[i]->get_signals_connected_to_this(&all_connections); |
| 68 | |
| 69 | for (const Connection &connection : all_connections) { |
| 70 | if (connection.callable.get_method() != p_method) { |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | TreeItem *node_item = tree->create_item(root); |
| 75 | |
| 76 | node_item->set_text(0, Object::cast_to<Node>(connection.signal.get_object())->get_name()); |
| 77 | node_item->set_icon(0, EditorNode::get_singleton()->get_object_icon(connection.signal.get_object())); |
| 78 | node_item->set_selectable(0, false); |
| 79 | node_item->set_editable(0, false); |
| 80 | |
| 81 | node_item->set_text(1, connection.signal.get_name()); |
| 82 | Control *p = Object::cast_to<Control>(get_parent()); |
| 83 | node_item->set_icon(1, p->get_editor_theme_icon(SNAME("Slot"))); |
| 84 | node_item->set_selectable(1, false); |
| 85 | node_item->set_editable(1, false); |
| 86 | |
| 87 | node_item->set_text(2, Object::cast_to<Node>(connection.callable.get_object())->get_name()); |
| 88 | node_item->set_icon(2, EditorNode::get_singleton()->get_object_icon(connection.callable.get_object())); |
| 89 | node_item->set_selectable(2, false); |
| 90 | node_item->set_editable(2, false); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | popup_centered(Size2(600, 300) * EDSCALE); |
| 95 | } |
| 96 | |
| 97 | ConnectionInfoDialog::ConnectionInfoDialog() { |
| 98 | set_title(TTRC("Connections to method:")); |
| 99 | |
| 100 | VBoxContainer *vbc = memnew(VBoxContainer); |
| 101 | vbc->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 8 * EDSCALE); |
| 102 | vbc->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_BEGIN, 8 * EDSCALE); |
| 103 | vbc->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -8 * EDSCALE); |
| 104 | vbc->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, -8 * EDSCALE); |
| 105 | add_child(vbc); |
| 106 | |
| 107 | method = memnew(Label); |
| 108 | method->set_focus_mode(Control::FOCUS_ACCESSIBILITY); |
| 109 | method->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 110 | method->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 111 | vbc->add_child(method); |
| 112 | |
| 113 | tree = memnew(Tree); |
| 114 | tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 115 | tree->set_theme_type_variation("TreeTable"); |
| 116 | tree->set_hide_folding(true); |
| 117 | tree->set_columns(3); |
| 118 | tree->set_hide_root(true); |
| 119 | tree->set_column_titles_visible(true); |
| 120 | tree->set_column_title(0, TTRC("Source")); |
| 121 | tree->set_column_title(1, TTRC("Signal")); |
| 122 | tree->set_column_title(2, TTRC("Target")); |
| 123 | vbc->add_child(tree); |
| 124 | tree->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 125 | tree->set_allow_rmb_select(true); |
| 126 | } |
| 127 | |
| 128 | //////////////////////////////////////////////////////////////////////////////// |
| 129 | |
| 130 | Vector<String> ScriptTextEditor::get_functions() { |
| 131 | CodeEdit *te = code_editor->get_text_editor(); |
| 132 | String text = te->get_text(); |
| 133 | List<String> fnc; |
| 134 | |
| 135 | if (script->get_language()->validate(text, script->get_path(), &fnc)) { |
| 136 | //if valid rewrite functions to latest |
| 137 | functions.clear(); |
| 138 | for (const String &E : fnc) { |
| 139 | functions.push_back(E); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return functions; |
| 144 | } |
| 145 | |
| 146 | void ScriptTextEditor::apply_code() { |
| 147 | if (script.is_null()) { |
| 148 | return; |
| 149 | } |
| 150 | script->set_source_code(code_editor->get_text_editor()->get_text()); |
| 151 | script->update_exports(); |
| 152 | if (!pending_dragged_exports.is_empty()) { |
| 153 | _assign_dragged_export_variables(); |
| 154 | } |
| 155 | |
| 156 | code_editor->get_text_editor()->get_syntax_highlighter()->update_cache(); |
| 157 | } |
| 158 | |
| 159 | Ref<Resource> ScriptTextEditor::get_edited_resource() const { |
| 160 | return script; |
| 161 | } |
| 162 | |
| 163 | void ScriptTextEditor::set_edited_resource(const Ref<Resource> &p_res) { |
| 164 | ERR_FAIL_COND(script.is_valid()); |
| 165 | ERR_FAIL_COND(p_res.is_null()); |
| 166 | |
| 167 | script = p_res; |
| 168 | |
| 169 | code_editor->get_text_editor()->set_text(script->get_source_code()); |
| 170 | code_editor->get_text_editor()->clear_undo_history(); |
| 171 | code_editor->get_text_editor()->tag_saved_version(); |
| 172 | |
| 173 | emit_signal(SNAME("name_changed")); |
| 174 | code_editor->update_line_and_column(); |
| 175 | } |
| 176 | |
| 177 | void ScriptTextEditor::enable_editor(Control *p_shortcut_context) { |
| 178 | if (editor_enabled) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | editor_enabled = true; |
| 183 | |
| 184 | _enable_code_editor(); |
| 185 | |
| 186 | _validate_script(); |
| 187 | |
| 188 | if (pending_state != Variant()) { |
| 189 | code_editor->set_edit_state(pending_state); |
| 190 | pending_state = Variant(); |
| 191 | } |
| 192 | |
| 193 | if (p_shortcut_context) { |
| 194 | for (int i = 0; i < edit_hb->get_child_count(); ++i) { |
| 195 | Control *c = cast_to<Control>(edit_hb->get_child(i)); |
| 196 | if (c) { |
| 197 | c->set_shortcut_context(p_shortcut_context); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void ScriptTextEditor::_load_theme_settings() { |
| 204 | CodeEdit *text_edit = code_editor->get_text_editor(); |
| 205 | |
| 206 | Color updated_warning_line_color = EDITOR_GET("text_editor/theme/highlighting/warning_color"); |
| 207 | Color updated_marked_line_color = EDITOR_GET("text_editor/theme/highlighting/mark_color"); |
| 208 | Color updated_safe_line_number_color = EDITOR_GET("text_editor/theme/highlighting/safe_line_number_color"); |
| 209 | Color updated_folded_code_region_color = EDITOR_GET("text_editor/theme/highlighting/folded_code_region_color"); |
| 210 | |
| 211 | bool warning_line_color_updated = updated_warning_line_color != warning_line_color; |
| 212 | bool marked_line_color_updated = updated_marked_line_color != marked_line_color; |
| 213 | bool safe_line_number_color_updated = updated_safe_line_number_color != safe_line_number_color; |
| 214 | bool folded_code_region_color_updated = updated_folded_code_region_color != folded_code_region_color; |
| 215 | if (safe_line_number_color_updated || warning_line_color_updated || marked_line_color_updated || folded_code_region_color_updated) { |
| 216 | safe_line_number_color = updated_safe_line_number_color; |
| 217 | for (int i = 0; i < text_edit->get_line_count(); i++) { |
| 218 | if (warning_line_color_updated && text_edit->get_line_background_color(i) == warning_line_color) { |
| 219 | text_edit->set_line_background_color(i, updated_warning_line_color); |
| 220 | } |
| 221 | |
| 222 | if (marked_line_color_updated && text_edit->get_line_background_color(i) == marked_line_color) { |
| 223 | text_edit->set_line_background_color(i, updated_marked_line_color); |
| 224 | } |
| 225 | |
| 226 | if (safe_line_number_color_updated && text_edit->get_line_gutter_item_color(i, line_number_gutter) != default_line_number_color) { |
| 227 | text_edit->set_line_gutter_item_color(i, line_number_gutter, safe_line_number_color); |
| 228 | } |
| 229 | |
| 230 | if (folded_code_region_color_updated && text_edit->get_line_background_color(i) == folded_code_region_color) { |
| 231 | text_edit->set_line_background_color(i, updated_folded_code_region_color); |
| 232 | } |
| 233 | } |
| 234 | warning_line_color = updated_warning_line_color; |
| 235 | marked_line_color = updated_marked_line_color; |
| 236 | folded_code_region_color = updated_folded_code_region_color; |
| 237 | } |
| 238 | |
| 239 | theme_loaded = true; |
| 240 | if (script.is_valid()) { |
| 241 | _set_theme_for_script(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | void ScriptTextEditor::_set_theme_for_script() { |
| 246 | if (!theme_loaded) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | CodeEdit *text_edit = code_editor->get_text_editor(); |
| 251 | text_edit->get_syntax_highlighter()->update_cache(); |
| 252 | |
| 253 | Vector<String> strings = script->get_language()->get_string_delimiters(); |
| 254 | text_edit->clear_string_delimiters(); |
| 255 | for (const String &string : strings) { |
| 256 | String beg = string.get_slicec(' ', 0); |
| 257 | String end = string.get_slice_count(" ") > 1 ? string.get_slicec(' ', 1) : String(); |
| 258 | if (!text_edit->has_string_delimiter(beg)) { |
| 259 | text_edit->add_string_delimiter(beg, end, end.is_empty()); |
| 260 | } |
| 261 | |
| 262 | if (!end.is_empty() && !text_edit->has_auto_brace_completion_open_key(beg)) { |
| 263 | text_edit->add_auto_brace_completion_pair(beg, end); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | text_edit->clear_comment_delimiters(); |
| 268 | |
| 269 | for (const String &comment : script->get_language()->get_comment_delimiters()) { |
| 270 | String beg = comment.get_slicec(' ', 0); |
| 271 | String end = comment.get_slice_count(" ") > 1 ? comment.get_slicec(' ', 1) : String(); |
| 272 | text_edit->add_comment_delimiter(beg, end, end.is_empty()); |
| 273 | |
| 274 | if (!end.is_empty() && !text_edit->has_auto_brace_completion_open_key(beg)) { |
| 275 | text_edit->add_auto_brace_completion_pair(beg, end); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | for (const String &doc_comment : script->get_language()->get_doc_comment_delimiters()) { |
| 280 | String beg = doc_comment.get_slicec(' ', 0); |
| 281 | String end = doc_comment.get_slice_count(" ") > 1 ? doc_comment.get_slicec(' ', 1) : String(); |
| 282 | text_edit->add_comment_delimiter(beg, end, end.is_empty()); |
| 283 | |
| 284 | if (!end.is_empty() && !text_edit->has_auto_brace_completion_open_key(beg)) { |
| 285 | text_edit->add_auto_brace_completion_pair(beg, end); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void ScriptTextEditor::_show_errors_panel(bool p_show) { |
| 291 | errors_panel->set_visible(p_show); |
| 292 | } |
| 293 | |
| 294 | void ScriptTextEditor::_show_warnings_panel(bool p_show) { |
| 295 | warnings_panel->set_visible(p_show); |
| 296 | } |
| 297 | |
| 298 | void ScriptTextEditor::_warning_clicked(const Variant &p_line) { |
| 299 | if (p_line.get_type() == Variant::INT) { |
| 300 | goto_line_centered(p_line.operator int64_t()); |
| 301 | } else if (p_line.get_type() == Variant::DICTIONARY) { |
| 302 | Dictionary meta = p_line.operator Dictionary(); |
| 303 | const int line = meta["line"].operator int64_t() - 1; |
| 304 | const String code = meta["code"].operator String(); |
| 305 | const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\""; |
| 306 | |
| 307 | CodeEdit *text_editor = code_editor->get_text_editor(); |
| 308 | String prev_line = line > 0 ? text_editor->get_line(line - 1) : ""; |
| 309 | if (prev_line.contains("@warning_ignore")) { |
| 310 | const int closing_bracket_idx = prev_line.find_char(')'); |
| 311 | const String text_to_insert = ", " + code.quote(quote_style); |
| 312 | text_editor->insert_text(text_to_insert, line - 1, closing_bracket_idx); |
| 313 | } else { |
| 314 | const int indent = text_editor->get_indent_level(line) / text_editor->get_indent_size(); |
| 315 | String annotation_indent; |
| 316 | if (!text_editor->is_indent_using_spaces()) { |
| 317 | annotation_indent = String("\t").repeat(indent); |
| 318 | } else { |
| 319 | annotation_indent = String(" ").repeat(text_editor->get_indent_size() * indent); |
| 320 | } |
| 321 | text_editor->insert_line_at(line, annotation_indent + "@warning_ignore(" + code.quote(quote_style) + ")"); |
| 322 | } |
| 323 | |
| 324 | _validate_script(); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | void ScriptTextEditor::_error_clicked(const Variant &p_line) { |
| 329 | if (p_line.get_type() == Variant::INT) { |
| 330 | goto_line_centered(p_line.operator int64_t()); |
| 331 | } else if (p_line.get_type() == Variant::DICTIONARY) { |
| 332 | Dictionary meta = p_line.operator Dictionary(); |
| 333 | const String path = meta["path"].operator String(); |
| 334 | const int line = meta["line"].operator int64_t(); |
| 335 | const int column = meta["column"].operator int64_t(); |
| 336 | if (path.is_empty()) { |
| 337 | goto_line_centered(line, column); |
| 338 | } else { |
| 339 | Ref<Resource> scr = ResourceLoader::load(path); |
| 340 | if (scr.is_null()) { |
| 341 | EditorNode::get_singleton()->show_warning(TTR("Could not load file at:") + "\n\n" + path, TTR("Error!")); |
| 342 | } else { |
| 343 | int corrected_column = column; |
| 344 | |
| 345 | const String line_text = code_editor->get_text_editor()->get_line(line); |
| 346 | const int indent_size = code_editor->get_text_editor()->get_indent_size(); |
| 347 | if (indent_size > 1) { |
| 348 | const int tab_count = line_text.length() - line_text.lstrip("\t").length(); |
| 349 | corrected_column -= tab_count * (indent_size - 1); |
| 350 | } |
| 351 | |
| 352 | ScriptEditor::get_singleton()->edit(scr, line, corrected_column); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void ScriptTextEditor::reload_text() { |
| 359 | ERR_FAIL_COND(script.is_null()); |
| 360 | |
| 361 | CodeEdit *te = code_editor->get_text_editor(); |
| 362 | int column = te->get_caret_column(); |
| 363 | int row = te->get_caret_line(); |
| 364 | int h = te->get_h_scroll(); |
| 365 | int v = te->get_v_scroll(); |
| 366 | |
| 367 | te->set_text(script->get_source_code()); |
| 368 | te->set_caret_line(row); |
| 369 | te->set_caret_column(column); |
| 370 | te->set_h_scroll(h); |
| 371 | te->set_v_scroll(v); |
| 372 | |
| 373 | te->tag_saved_version(); |
| 374 | |
| 375 | code_editor->update_line_and_column(); |
| 376 | if (editor_enabled) { |
| 377 | _validate_script(); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void ScriptTextEditor::add_callback(const String &p_function, const PackedStringArray &p_args) { |
| 382 | ScriptLanguage *language = script->get_language(); |
| 383 | if (!language->can_make_function()) { |
| 384 | return; |
| 385 | } |
| 386 | code_editor->get_text_editor()->begin_complex_operation(); |
| 387 | code_editor->get_text_editor()->remove_secondary_carets(); |
| 388 | code_editor->get_text_editor()->deselect(); |
| 389 | String code = code_editor->get_text_editor()->get_text(); |
| 390 | int pos = language->find_function(p_function, code); |
| 391 | if (pos == -1) { |
| 392 | // Function does not exist, create it at the end of the file. |
| 393 | int last_line = code_editor->get_text_editor()->get_line_count() - 1; |
| 394 | String func = language->make_function("", p_function, p_args); |
| 395 | code_editor->get_text_editor()->insert_text("\n\n" + func, last_line, code_editor->get_text_editor()->get_line(last_line).length()); |
| 396 | pos = last_line + 3; |
| 397 | } |
| 398 | // Put caret on the line after the function, after the indent. |
| 399 | int indent_column = 1; |
| 400 | if (EDITOR_GET("text_editor/behavior/indent/type")) { |
| 401 | indent_column = EDITOR_GET("text_editor/behavior/indent/size"); |
| 402 | } |
| 403 | code_editor->get_text_editor()->set_caret_line(pos, true, true, -1); |
| 404 | code_editor->get_text_editor()->set_caret_column(indent_column); |
| 405 | code_editor->get_text_editor()->end_complex_operation(); |
| 406 | } |
| 407 | |
| 408 | bool ScriptTextEditor::show_members_overview() { |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | bool ScriptTextEditor::_is_valid_color_info(const Dictionary &p_info) { |
| 413 | if (p_info.get_valid("color").get_type() != Variant::COLOR) { |
| 414 | return false; |
| 415 | } |
| 416 | if (!p_info.get_valid("color_end").is_num() || !p_info.get_valid("color_mode").is_num()) { |
| 417 | return false; |
| 418 | } |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | Array ScriptTextEditor::_inline_object_parse(const String &p_text) { |
| 423 | Array result; |
| 424 | int i_end_previous = 0; |
| 425 | int i_start = p_text.find("Color"); |
| 426 | |
| 427 | while (i_start != -1) { |
| 428 | // Ignore words that just have "Color" in them. |
| 429 | if (i_start != 0 && ('_' + p_text.substr(i_start - 1, 1)).is_valid_ascii_identifier()) { |
| 430 | i_end_previous = MAX(i_end_previous, i_start); |
| 431 | i_start = p_text.find("Color", i_start + 1); |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | const int i_par_start = p_text.find_char('(', i_start + 5); |
| 436 | const int i_par_end = p_text.find_char(')', i_start + 5); |
| 437 | if (i_par_start == -1 || i_par_end == -1) { |
| 438 | i_end_previous = MAX(i_end_previous, i_start); |
| 439 | i_start = p_text.find("Color", i_start + 1); |
| 440 | continue; |
| 441 | } |
| 442 | |
| 443 | Dictionary color_info; |
| 444 | color_info["column"] = i_start; |
| 445 | color_info["width_ratio"] = 1.0; |
| 446 | color_info["color_end"] = i_par_end; |
| 447 | |
| 448 | const String fn_name = p_text.substr(i_start + 5, i_par_start - i_start - 5); |
| 449 | const String s_params = p_text.substr(i_par_start + 1, i_par_end - i_par_start - 1); |
| 450 | bool has_added_color = false; |
| 451 | |
| 452 | if (fn_name.is_empty()) { |
| 453 | String stripped = s_params.strip_edges(true, true); |
| 454 | if (stripped.length() > 1 && (stripped[0] == '"' || stripped[0] == '\'')) { |
| 455 | // String constructor. |
| 456 | const char32_t string_delimiter = stripped[0]; |
| 457 | if (stripped[stripped.length() - 1] == string_delimiter) { |
| 458 | const String color_string = stripped.substr(1, stripped.length() - 2); |
| 459 | if (!color_string.contains_char(string_delimiter)) { |
| 460 | color_info["color"] = Color::from_string(color_string, Color()); |
| 461 | color_info["color_mode"] = MODE_STRING; |
| 462 | has_added_color = true; |
| 463 | } |
| 464 | } |
| 465 | } else if (stripped.length() == 10 && stripped.begins_with("0x")) { |
| 466 | // Hex constructor. |
| 467 | const String color_string = stripped.substr(2); |
| 468 | if (color_string.is_valid_hex_number(false)) { |
| 469 | color_info["color"] = Color::from_string(color_string, Color()); |
| 470 | color_info["color_mode"] = MODE_HEX; |
| 471 | has_added_color = true; |
| 472 | } |
| 473 | } else if (stripped.is_empty()) { |
| 474 | // Empty Color() constructor. |
| 475 | color_info["color"] = Color(); |
| 476 | color_info["color_mode"] = MODE_RGB; |
| 477 | has_added_color = true; |
| 478 | } |
| 479 | } |
| 480 | // Float & int parameters. |
| 481 | if (!has_added_color && s_params.size() > 0) { |
| 482 | const PackedStringArray s_params_split = s_params.split(",", false, 4); |
| 483 | PackedFloat64Array params; |
| 484 | bool valid_floats = true; |
| 485 | for (const String &s_param : s_params_split) { |
| 486 | // Only allow float literals, expressions won't be evaluated and could get replaced. |
| 487 | if (!s_param.strip_edges().is_valid_float()) { |
| 488 | valid_floats = false; |
| 489 | break; |
| 490 | } |
| 491 | params.push_back(s_param.to_float()); |
| 492 | } |
| 493 | if (valid_floats && params.size() == 3) { |
| 494 | if (fn_name == ".from_rgba8") { |
| 495 | params.push_back(255); |
| 496 | } else { |
| 497 | params.push_back(1.0); |
| 498 | } |
| 499 | } |
| 500 | if (valid_floats && params.size() == 4) { |
| 501 | has_added_color = true; |
| 502 | if (fn_name == ".from_ok_hsl") { |
| 503 | color_info["color"] = Color::from_ok_hsl(params[0], params[1], params[2], params[3]); |
| 504 | color_info["color_mode"] = MODE_OKHSL; |
| 505 | } else if (fn_name == ".from_hsv") { |
| 506 | color_info["color"] = Color::from_hsv(params[0], params[1], params[2], params[3]); |
| 507 | color_info["color_mode"] = MODE_HSV; |
| 508 | } else if (fn_name == ".from_rgba8") { |
| 509 | color_info["color"] = Color::from_rgba8(int(params[0]), int(params[1]), int(params[2]), int(params[3])); |
| 510 | color_info["color_mode"] = MODE_RGB8; |
| 511 | } else if (fn_name.is_empty()) { |
| 512 | color_info["color"] = Color(params[0], params[1], params[2], params[3]); |
| 513 | color_info["color_mode"] = MODE_RGB; |
| 514 | } else { |
| 515 | has_added_color = false; |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | if (has_added_color) { |
| 521 | result.push_back(color_info); |
| 522 | i_end_previous = i_par_end + 1; |
| 523 | } |
| 524 | i_end_previous = MAX(i_end_previous, i_start); |
| 525 | i_start = p_text.find("Color", i_start + 1); |
| 526 | } |
| 527 | return result; |
| 528 | } |
| 529 | |
| 530 | void ScriptTextEditor::_inline_object_draw(const Dictionary &p_info, const Rect2 &p_rect) { |
| 531 | if (_is_valid_color_info(p_info)) { |
| 532 | Rect2 col_rect = p_rect.grow(-4); |
| 533 | if (color_alpha_texture.is_null()) { |
| 534 | color_alpha_texture = inline_color_picker->get_theme_icon("sample_bg", "ColorPicker"); |
| 535 | } |
| 536 | RID text_ci = code_editor->get_text_editor()->get_text_canvas_item(); |
| 537 | RS::get_singleton()->canvas_item_add_rect(text_ci, p_rect.grow(-3), Color(1, 1, 1)); |
| 538 | color_alpha_texture->draw_rect(text_ci, col_rect); |
| 539 | RS::get_singleton()->canvas_item_add_rect(text_ci, col_rect, Color(p_info["color"])); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | void ScriptTextEditor::_inline_object_handle_click(const Dictionary &p_info, const Rect2 &p_rect) { |
| 544 | if (_is_valid_color_info(p_info)) { |
| 545 | inline_color_picker->set_pick_color(p_info["color"]); |
| 546 | inline_color_line = p_info["line"]; |
| 547 | inline_color_start = p_info["column"]; |
| 548 | inline_color_end = p_info["color_end"]; |
| 549 | |
| 550 | // Reset tooltip hover timer. |
| 551 | code_editor->get_text_editor()->set_symbol_tooltip_on_hover_enabled(false); |
| 552 | code_editor->get_text_editor()->set_symbol_tooltip_on_hover_enabled(true); |
| 553 | |
| 554 | _update_color_constructor_options(); |
| 555 | inline_color_options->select(p_info["color_mode"]); |
| 556 | EditorNode::get_singleton()->setup_color_picker(inline_color_picker); |
| 557 | |
| 558 | // Move popup above the line if it's too low. |
| 559 | float_t view_h = get_viewport_rect().size.y; |
| 560 | float_t pop_h = inline_color_popup->get_contents_minimum_size().y; |
| 561 | float_t pop_y = p_rect.get_end().y; |
| 562 | float_t pop_x = p_rect.position.x; |
| 563 | if (pop_y + pop_h > view_h) { |
| 564 | pop_y = p_rect.position.y - pop_h; |
| 565 | } |
| 566 | // Move popup to the right if it's too high. |
| 567 | if (pop_y < 0) { |
| 568 | pop_x = p_rect.get_end().x; |
| 569 | } |
| 570 | |
| 571 | inline_color_popup->popup(Rect2(pop_x, pop_y, 0, 0)); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | String ScriptTextEditor::_picker_color_stringify(const Color &p_color, COLOR_MODE p_mode) { |
| 576 | String result; |
| 577 | String fname; |
| 578 | Vector<String> str_params; |
| 579 | switch (p_mode) { |
| 580 | case ScriptTextEditor::MODE_STRING: { |
| 581 | str_params.push_back("\"" + p_color.to_html() + "\""); |
| 582 | } break; |
| 583 | case ScriptTextEditor::MODE_HEX: { |
| 584 | str_params.push_back("0x" + p_color.to_html()); |
| 585 | } break; |
| 586 | case ScriptTextEditor::MODE_RGB: { |
| 587 | str_params = { |
| 588 | String::num(p_color.r, 3), |
| 589 | String::num(p_color.g, 3), |
| 590 | String::num(p_color.b, 3), |
| 591 | String::num(p_color.a, 3) |
| 592 | }; |
| 593 | } break; |
| 594 | case ScriptTextEditor::MODE_HSV: { |
| 595 | str_params = { |
| 596 | String::num(p_color.get_h(), 3), |
| 597 | String::num(p_color.get_s(), 3), |
| 598 | String::num(p_color.get_v(), 3), |
| 599 | String::num(p_color.a, 3) |
| 600 | }; |
| 601 | fname = ".from_hsv"; |
| 602 | } break; |
| 603 | case ScriptTextEditor::MODE_OKHSL: { |
| 604 | str_params = { |
| 605 | String::num(p_color.get_ok_hsl_h(), 3), |
| 606 | String::num(p_color.get_ok_hsl_s(), 3), |
| 607 | String::num(p_color.get_ok_hsl_l(), 3), |
| 608 | String::num(p_color.a, 3) |
| 609 | }; |
| 610 | fname = ".from_ok_hsl"; |
| 611 | } break; |
| 612 | case ScriptTextEditor::MODE_RGB8: { |
| 613 | str_params = { |
| 614 | itos(p_color.get_r8()), |
| 615 | itos(p_color.get_g8()), |
| 616 | itos(p_color.get_b8()), |
| 617 | itos(p_color.get_a8()) |
| 618 | }; |
| 619 | fname = ".from_rgba8"; |
| 620 | } break; |
| 621 | default: { |
| 622 | } break; |
| 623 | } |
| 624 | result = "Color" + fname + "(" + String(", ").join(str_params) + ")"; |
| 625 | return result; |
| 626 | } |
| 627 | |
| 628 | void ScriptTextEditor::_picker_color_changed(const Color &p_color) { |
| 629 | _update_color_constructor_options(); |
| 630 | _update_color_text(); |
| 631 | } |
| 632 | |
| 633 | void ScriptTextEditor::_update_color_constructor_options() { |
| 634 | int item_count = inline_color_options->get_item_count(); |
| 635 | // Update or add each constructor as an option. |
| 636 | for (int i = 0; i < MODE_MAX; i++) { |
| 637 | String option_text = _picker_color_stringify(inline_color_picker->get_pick_color(), (COLOR_MODE)i); |
| 638 | if (i >= item_count) { |
| 639 | inline_color_options->add_item(option_text); |
| 640 | } else { |
| 641 | inline_color_options->set_item_text(i, option_text); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | void ScriptTextEditor::_update_background_color() { |
| 647 | // Clear background lines. |
| 648 | CodeEdit *te = code_editor->get_text_editor(); |
| 649 | for (int i = 0; i < te->get_line_count(); i++) { |
| 650 | bool is_folded_code_region = te->is_line_code_region_start(i) && te->is_line_folded(i); |
| 651 | te->set_line_background_color(i, is_folded_code_region ? folded_code_region_color : Color(0, 0, 0, 0)); |
| 652 | } |
| 653 | |
| 654 | // Set the warning background. |
| 655 | if (warning_line_color.a != 0.0) { |
| 656 | for (const ScriptLanguage::Warning &warning : warnings) { |
| 657 | int warning_start_line = CLAMP(warning.start_line - 1, 0, te->get_line_count() - 1); |
| 658 | int warning_end_line = CLAMP(warning.end_line - 1, 0, te->get_line_count() - 1); |
| 659 | int folded_line_header = te->get_folded_line_header(warning_start_line); |
| 660 | |
| 661 | // If the warning highlight is too long, only highlight the start line. |
| 662 | const int warning_max_lines = 20; |
| 663 | |
| 664 | te->set_line_background_color(folded_line_header, warning_line_color); |
| 665 | if (warning_end_line - warning_start_line < warning_max_lines) { |
| 666 | for (int i = warning_start_line + 1; i <= warning_end_line; i++) { |
| 667 | te->set_line_background_color(i, warning_line_color); |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | // Set the error background. |
| 674 | if (marked_line_color.a != 0.0) { |
| 675 | for (const ScriptLanguage::ScriptError &error : errors) { |
| 676 | int error_line = CLAMP(error.line - 1, 0, te->get_line_count() - 1); |
| 677 | int folded_line_header = te->get_folded_line_header(error_line); |
| 678 | |
| 679 | te->set_line_background_color(folded_line_header, marked_line_color); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | void ScriptTextEditor::_update_color_text() { |
| 685 | if (inline_color_line < 0) { |
| 686 | return; |
| 687 | } |
| 688 | String result = inline_color_options->get_item_text(inline_color_options->get_selected_id()); |
| 689 | code_editor->get_text_editor()->begin_complex_operation(); |
| 690 | code_editor->get_text_editor()->remove_text(inline_color_line, inline_color_start, inline_color_line, inline_color_end + 1); |
| 691 | inline_color_end = inline_color_start + result.size() - 2; |
| 692 | code_editor->get_text_editor()->insert_text(result, inline_color_line, inline_color_start); |
| 693 | code_editor->get_text_editor()->end_complex_operation(); |
| 694 | } |
| 695 | |
| 696 | void ScriptTextEditor::update_settings() { |
| 697 | code_editor->get_text_editor()->set_gutter_draw(connection_gutter, EDITOR_GET("text_editor/appearance/gutters/show_info_gutter")); |
| 698 | if (EDITOR_GET("text_editor/appearance/enable_inline_color_picker")) { |
| 699 | code_editor->get_text_editor()->set_inline_object_handlers( |
| 700 | callable_mp(this, &ScriptTextEditor::_inline_object_parse), |
| 701 | callable_mp(this, &ScriptTextEditor::_inline_object_draw), |
| 702 | callable_mp(this, &ScriptTextEditor::_inline_object_handle_click)); |
| 703 | } else { |
| 704 | code_editor->get_text_editor()->set_inline_object_handlers(Callable(), Callable(), Callable()); |
| 705 | } |
| 706 | code_editor->update_editor_settings(); |
| 707 | } |
| 708 | |
| 709 | bool ScriptTextEditor::is_unsaved() { |
| 710 | const bool unsaved = |
| 711 | code_editor->get_text_editor()->get_version() != code_editor->get_text_editor()->get_saved_version() || |
| 712 | script->get_path().is_empty(); // In memory. |
| 713 | return unsaved; |
| 714 | } |
| 715 | |
| 716 | Variant ScriptTextEditor::get_edit_state() { |
| 717 | if (pending_state != Variant()) { |
| 718 | return pending_state; |
| 719 | } |
| 720 | return code_editor->get_edit_state(); |
| 721 | } |
| 722 | |
| 723 | void ScriptTextEditor::set_edit_state(const Variant &p_state) { |
| 724 | if (editor_enabled) { |
| 725 | code_editor->set_edit_state(p_state); |
| 726 | } else { |
| 727 | // The editor is not fully initialized, so the state can't be loaded properly. |
| 728 | pending_state = p_state; |
| 729 | } |
| 730 | |
| 731 | Dictionary state = p_state; |
| 732 | if (state.has("syntax_highlighter")) { |
| 733 | int idx = highlighter_menu->get_item_idx_from_text(state["syntax_highlighter"]); |
| 734 | if (idx >= 0) { |
| 735 | _change_syntax_highlighter(idx); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | if (editor_enabled) { |
| 740 | #ifndef ANDROID_ENABLED |
| 741 | ensure_focus(); |
| 742 | #endif |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | Variant ScriptTextEditor::get_navigation_state() { |
| 747 | return code_editor->get_navigation_state(); |
| 748 | } |
| 749 | |
| 750 | Variant ScriptTextEditor::get_previous_state() { |
| 751 | return code_editor->get_previous_state(); |
| 752 | } |
| 753 | |
| 754 | void ScriptTextEditor::store_previous_state() { |
| 755 | return code_editor->store_previous_state(); |
| 756 | } |
| 757 | |
| 758 | void ScriptTextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) { |
| 759 | code_editor->convert_case(p_case); |
| 760 | } |
| 761 | |
| 762 | void ScriptTextEditor::trim_trailing_whitespace() { |
| 763 | code_editor->trim_trailing_whitespace(); |
| 764 | } |
| 765 | |
| 766 | void ScriptTextEditor::trim_final_newlines() { |
| 767 | code_editor->trim_final_newlines(); |
| 768 | } |
| 769 | |
| 770 | void ScriptTextEditor::insert_final_newline() { |
| 771 | code_editor->insert_final_newline(); |
| 772 | } |
| 773 | |
| 774 | void ScriptTextEditor::convert_indent() { |
| 775 | code_editor->get_text_editor()->convert_indent(); |
| 776 | } |
| 777 | |
| 778 | void ScriptTextEditor::tag_saved_version() { |
| 779 | code_editor->get_text_editor()->tag_saved_version(); |
| 780 | edited_file_data.last_modified_time = FileAccess::get_modified_time(edited_file_data.path); |
| 781 | } |
| 782 | |
| 783 | void ScriptTextEditor::goto_line(int p_line, int p_column) { |
| 784 | code_editor->goto_line(p_line, p_column); |
| 785 | } |
| 786 | |
| 787 | void ScriptTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) { |
| 788 | code_editor->goto_line_selection(p_line, p_begin, p_end); |
| 789 | } |
| 790 | |
| 791 | void ScriptTextEditor::goto_line_centered(int p_line, int p_column) { |
| 792 | code_editor->goto_line_centered(p_line, p_column); |
| 793 | } |
| 794 | |
| 795 | void ScriptTextEditor::set_executing_line(int p_line) { |
| 796 | code_editor->set_executing_line(p_line); |
| 797 | } |
| 798 | |
| 799 | void ScriptTextEditor::clear_executing_line() { |
| 800 | code_editor->clear_executing_line(); |
| 801 | } |
| 802 | |
| 803 | void ScriptTextEditor::ensure_focus() { |
| 804 | code_editor->get_text_editor()->grab_focus(); |
| 805 | } |
| 806 | |
| 807 | String ScriptTextEditor::get_name() { |
| 808 | String name; |
| 809 | |
| 810 | name = script->get_path().get_file(); |
| 811 | if (name.is_empty()) { |
| 812 | // This appears for newly created built-in scripts before saving the scene. |
| 813 | name = TTR("[unsaved]"); |
| 814 | } else if (script->is_built_in()) { |
| 815 | const String &script_name = script->get_name(); |
| 816 | if (!script_name.is_empty()) { |
| 817 | // If the built-in script has a custom resource name defined, |
| 818 | // display the built-in script name as follows: `ResourceName (scene_file.tscn)` |
| 819 | name = vformat("%s (%s)", script_name, name.get_slice("::", 0)); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | if (is_unsaved()) { |
| 824 | name += "(*)"; |
| 825 | } |
| 826 | |
| 827 | return name; |
| 828 | } |
| 829 | |
| 830 | Ref<Texture2D> ScriptTextEditor::get_theme_icon() { |
| 831 | if (get_parent_control()) { |
| 832 | String icon_name = script->get_class(); |
| 833 | if (script->is_built_in()) { |
| 834 | icon_name += "Internal"; |
| 835 | } |
| 836 | |
| 837 | if (get_parent_control()->has_theme_icon(icon_name, EditorStringName(EditorIcons))) { |
| 838 | return get_parent_control()->get_editor_theme_icon(icon_name); |
| 839 | } else if (get_parent_control()->has_theme_icon(script->get_class(), EditorStringName(EditorIcons))) { |
| 840 | return get_parent_control()->get_editor_theme_icon(script->get_class()); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | Ref<Texture2D> extension_language_icon = EditorNode::get_editor_data().extension_class_get_icon(script->get_class()); |
| 845 | Ref<Texture2D> extension_language_alt_icon; |
| 846 | if (script->is_built_in()) { |
| 847 | extension_language_alt_icon = EditorNode::get_editor_data().extension_class_get_icon(script->get_class() + "Internal"); |
| 848 | } |
| 849 | |
| 850 | if (extension_language_alt_icon.is_valid()) { |
| 851 | return extension_language_alt_icon; |
| 852 | } else if (extension_language_icon.is_valid()) { |
| 853 | return extension_language_icon; |
| 854 | } |
| 855 | |
| 856 | return Ref<Texture2D>(); |
| 857 | } |
| 858 | |
| 859 | void ScriptTextEditor::_validate_script() { |
| 860 | CodeEdit *te = code_editor->get_text_editor(); |
| 861 | |
| 862 | String text = te->get_text(); |
| 863 | List<String> fnc; |
| 864 | |
| 865 | warnings.clear(); |
| 866 | errors.clear(); |
| 867 | depended_errors.clear(); |
| 868 | safe_lines.clear(); |
| 869 | |
| 870 | if (!script->get_language()->validate(text, script->get_path(), &fnc, &errors, &warnings, &safe_lines)) { |
| 871 | List<ScriptLanguage::ScriptError>::Element *E = errors.front(); |
| 872 | while (E) { |
| 873 | List<ScriptLanguage::ScriptError>::Element *next_E = E->next(); |
| 874 | if ((E->get().path.is_empty() && !script->get_path().is_empty()) || E->get().path != script->get_path()) { |
| 875 | depended_errors[E->get().path].push_back(E->get()); |
| 876 | E->erase(); |
| 877 | } |
| 878 | E = next_E; |
| 879 | } |
| 880 | |
| 881 | if (errors.size() > 0) { |
| 882 | const int line = errors.front()->get().line; |
| 883 | const int column = errors.front()->get().column; |
| 884 | const String message = errors.front()->get().message.replace("[", "[lb]"); |
| 885 | const String error_text = vformat(TTR("Error at ([hint=Line %d, column %d]%d, %d[/hint]):"), line, column, line, column) + " " + message; |
| 886 | code_editor->set_error(error_text); |
| 887 | code_editor->set_error_pos(line - 1, column - 1); |
| 888 | } |
| 889 | script_is_valid = false; |
| 890 | } else { |
| 891 | code_editor->set_error(""); |
| 892 | if (!script->is_tool()) { |
| 893 | script->set_source_code(text); |
| 894 | script->update_exports(); |
| 895 | te->get_syntax_highlighter()->update_cache(); |
| 896 | } |
| 897 | |
| 898 | functions.clear(); |
| 899 | for (const String &E : fnc) { |
| 900 | functions.push_back(E); |
| 901 | } |
| 902 | script_is_valid = true; |
| 903 | } |
| 904 | _update_connected_methods(); |
| 905 | _update_warnings(); |
| 906 | _update_errors(); |
| 907 | _update_background_color(); |
| 908 | |
| 909 | if (!pending_dragged_exports.is_empty()) { |
| 910 | _assign_dragged_export_variables(); |
| 911 | } |
| 912 | |
| 913 | emit_signal(SNAME("name_changed")); |
| 914 | emit_signal(SNAME("edited_script_changed")); |
| 915 | } |
| 916 | |
| 917 | void ScriptTextEditor::_update_warnings() { |
| 918 | int warning_nb = warnings.size(); |
| 919 | warnings_panel->clear(); |
| 920 | |
| 921 | bool has_connections_table = false; |
| 922 | // Add missing connections. |
| 923 | if (GLOBAL_GET("debug/gdscript/warnings/enable")) { |
| 924 | Node *base = get_tree()->get_edited_scene_root(); |
| 925 | if (base && missing_connections.size() > 0) { |
| 926 | has_connections_table = true; |
| 927 | warnings_panel->push_table(1); |
| 928 | for (const Connection &connection : missing_connections) { |
| 929 | String base_path = base->get_name(); |
| 930 | String source_path = base == connection.signal.get_object() ? base_path : base_path + "/" + String(base->get_path_to(Object::cast_to<Node>(connection.signal.get_object()))); |
| 931 | String target_path = base == connection.callable.get_object() ? base_path : base_path + "/" + String(base->get_path_to(Object::cast_to<Node>(connection.callable.get_object()))); |
| 932 | |
| 933 | warnings_panel->push_cell(); |
| 934 | warnings_panel->push_color(warnings_panel->get_theme_color(SNAME("warning_color"), EditorStringName(Editor))); |
| 935 | warnings_panel->add_text(vformat(TTR("Missing connected method '%s' for signal '%s' from node '%s' to node '%s'."), connection.callable.get_method(), connection.signal.get_name(), source_path, target_path)); |
| 936 | warnings_panel->pop(); // Color. |
| 937 | warnings_panel->pop(); // Cell. |
| 938 | } |
| 939 | warnings_panel->pop(); // Table. |
| 940 | |
| 941 | warning_nb += missing_connections.size(); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | code_editor->set_warning_count(warning_nb); |
| 946 | |
| 947 | if (has_connections_table) { |
| 948 | warnings_panel->add_newline(); |
| 949 | } |
| 950 | |
| 951 | // Add script warnings. |
| 952 | warnings_panel->push_table(3); |
| 953 | for (const ScriptLanguage::Warning &w : warnings) { |
| 954 | Dictionary ignore_meta; |
| 955 | ignore_meta["line"] = w.start_line; |
| 956 | ignore_meta["code"] = w.string_code.to_lower(); |
| 957 | warnings_panel->push_cell(); |
| 958 | warnings_panel->push_meta(ignore_meta); |
| 959 | warnings_panel->push_color( |
| 960 | warnings_panel->get_theme_color(SNAME("accent_color"), EditorStringName(Editor)).lerp(warnings_panel->get_theme_color(SNAME("mono_color"), EditorStringName(Editor)), 0.5f)); |
| 961 | warnings_panel->add_text(TTR("[Ignore]")); |
| 962 | warnings_panel->pop(); // Color. |
| 963 | warnings_panel->pop(); // Meta ignore. |
| 964 | warnings_panel->pop(); // Cell. |
| 965 | |
| 966 | warnings_panel->push_cell(); |
| 967 | warnings_panel->push_meta(w.start_line - 1); |
| 968 | warnings_panel->push_color(warnings_panel->get_theme_color(SNAME("warning_color"), EditorStringName(Editor))); |
| 969 | warnings_panel->add_text(vformat(TTR("Line %d (%s):"), w.start_line, w.string_code)); |
| 970 | warnings_panel->pop(); // Color. |
| 971 | warnings_panel->pop(); // Meta goto. |
| 972 | warnings_panel->pop(); // Cell. |
| 973 | |
| 974 | warnings_panel->push_cell(); |
| 975 | warnings_panel->add_text(w.message); |
| 976 | warnings_panel->add_newline(); |
| 977 | warnings_panel->pop(); // Cell. |
| 978 | } |
| 979 | warnings_panel->pop(); // Table. |
| 980 | } |
| 981 | |
| 982 | void ScriptTextEditor::_update_errors() { |
| 983 | code_editor->set_error_count(errors.size()); |
| 984 | |
| 985 | errors_panel->clear(); |
| 986 | errors_panel->push_table(2); |
| 987 | for (const ScriptLanguage::ScriptError &err : errors) { |
| 988 | Dictionary click_meta; |
| 989 | click_meta["line"] = err.line; |
| 990 | click_meta["column"] = err.column; |
| 991 | |
| 992 | errors_panel->push_cell(); |
| 993 | errors_panel->push_meta(err.line - 1); |
| 994 | errors_panel->push_color(warnings_panel->get_theme_color(SNAME("error_color"), EditorStringName(Editor))); |
| 995 | errors_panel->add_text(vformat(TTR("Line %d:"), err.line)); |
| 996 | errors_panel->pop(); // Color. |
| 997 | errors_panel->pop(); // Meta goto. |
| 998 | errors_panel->pop(); // Cell. |
| 999 | |
| 1000 | errors_panel->push_cell(); |
| 1001 | errors_panel->add_text(err.message); |
| 1002 | errors_panel->add_newline(); |
| 1003 | errors_panel->pop(); // Cell. |
| 1004 | } |
| 1005 | errors_panel->pop(); // Table |
| 1006 | |
| 1007 | for (const KeyValue<String, List<ScriptLanguage::ScriptError>> &KV : depended_errors) { |
| 1008 | Dictionary click_meta; |
| 1009 | click_meta["path"] = KV.key; |
| 1010 | click_meta["line"] = 1; |
| 1011 | |
| 1012 | errors_panel->add_newline(); |
| 1013 | errors_panel->add_newline(); |
| 1014 | errors_panel->push_meta(click_meta); |
| 1015 | errors_panel->add_text(vformat(R"(%s:)", KV.key)); |
| 1016 | errors_panel->pop(); // Meta goto. |
| 1017 | errors_panel->add_newline(); |
| 1018 | |
| 1019 | errors_panel->push_indent(1); |
| 1020 | errors_panel->push_table(2); |
| 1021 | String filename = KV.key.get_file(); |
| 1022 | for (const ScriptLanguage::ScriptError &err : KV.value) { |
| 1023 | click_meta["line"] = err.line; |
| 1024 | click_meta["column"] = err.column; |
| 1025 | |
| 1026 | errors_panel->push_cell(); |
| 1027 | errors_panel->push_meta(click_meta); |
| 1028 | errors_panel->push_color(errors_panel->get_theme_color(SNAME("error_color"), EditorStringName(Editor))); |
| 1029 | errors_panel->add_text(vformat(TTR("Line %d:"), err.line)); |
| 1030 | errors_panel->pop(); // Color. |
| 1031 | errors_panel->pop(); // Meta goto. |
| 1032 | errors_panel->pop(); // Cell. |
| 1033 | |
| 1034 | errors_panel->push_cell(); |
| 1035 | errors_panel->add_text(err.message); |
| 1036 | errors_panel->pop(); // Cell. |
| 1037 | } |
| 1038 | errors_panel->pop(); // Table |
| 1039 | errors_panel->pop(); // Indent. |
| 1040 | } |
| 1041 | |
| 1042 | bool highlight_safe = EDITOR_GET("text_editor/appearance/gutters/highlight_type_safe_lines"); |
| 1043 | bool last_is_safe = false; |
| 1044 | CodeEdit *te = code_editor->get_text_editor(); |
| 1045 | |
| 1046 | for (int i = 0; i < te->get_line_count(); i++) { |
| 1047 | if (highlight_safe) { |
| 1048 | if (safe_lines.has(i + 1)) { |
| 1049 | te->set_line_gutter_item_color(i, line_number_gutter, safe_line_number_color); |
| 1050 | last_is_safe = true; |
| 1051 | } else if (last_is_safe && (te->is_in_comment(i) != -1 || te->get_line(i).strip_edges().is_empty())) { |
| 1052 | te->set_line_gutter_item_color(i, line_number_gutter, safe_line_number_color); |
| 1053 | } else { |
| 1054 | te->set_line_gutter_item_color(i, line_number_gutter, default_line_number_color); |
| 1055 | last_is_safe = false; |
| 1056 | } |
| 1057 | } else { |
| 1058 | te->set_line_gutter_item_color(i, 1, default_line_number_color); |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | void ScriptTextEditor::_update_bookmark_list() { |
| 1064 | bookmarks_menu->clear(); |
| 1065 | bookmarks_menu->reset_size(); |
| 1066 | |
| 1067 | bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE); |
| 1068 | bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL); |
| 1069 | bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT); |
| 1070 | bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV); |
| 1071 | |
| 1072 | PackedInt32Array bookmark_list = code_editor->get_text_editor()->get_bookmarked_lines(); |
| 1073 | if (bookmark_list.is_empty()) { |
| 1074 | return; |
| 1075 | } |
| 1076 | |
| 1077 | bookmarks_menu->add_separator(); |
| 1078 | |
| 1079 | for (int i = 0; i < bookmark_list.size(); i++) { |
| 1080 | // Strip edges to remove spaces or tabs. |
| 1081 | // Also replace any tabs by spaces, since we can't print tabs in the menu. |
| 1082 | String line = code_editor->get_text_editor()->get_line(bookmark_list[i]).replace("\t", " ").strip_edges(); |
| 1083 | |
| 1084 | // Limit the size of the line if too big. |
| 1085 | if (line.length() > 50) { |
| 1086 | line = line.substr(0, 50); |
| 1087 | } |
| 1088 | |
| 1089 | bookmarks_menu->add_item(String::num_int64(bookmark_list[i] + 1) + " - `" + line + "`"); |
| 1090 | bookmarks_menu->set_item_metadata(-1, bookmark_list[i]); |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | void ScriptTextEditor::_bookmark_item_pressed(int p_idx) { |
| 1095 | if (p_idx < 4) { // Any item before the separator. |
| 1096 | _edit_option(bookmarks_menu->get_item_id(p_idx)); |
| 1097 | } else { |
| 1098 | code_editor->goto_line_centered(bookmarks_menu->get_item_metadata(p_idx)); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | static Vector<Node *> _find_all_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) { |
| 1103 | Vector<Node *> nodes; |
| 1104 | |
| 1105 | if (p_current->get_owner() != p_base && p_base != p_current) { |
| 1106 | return nodes; |
| 1107 | } |
| 1108 | |
| 1109 | Ref<Script> c = p_current->get_script(); |
| 1110 | if (c == p_script) { |
| 1111 | nodes.push_back(p_current); |
| 1112 | } |
| 1113 | |
| 1114 | for (int i = 0; i < p_current->get_child_count(); i++) { |
| 1115 | Vector<Node *> found = _find_all_node_for_script(p_base, p_current->get_child(i), p_script); |
| 1116 | nodes.append_array(found); |
| 1117 | } |
| 1118 | |
| 1119 | return nodes; |
| 1120 | } |
| 1121 | |
| 1122 | static Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) { |
| 1123 | if (p_current->get_owner() != p_base && p_base != p_current) { |
| 1124 | return nullptr; |
| 1125 | } |
| 1126 | Ref<Script> c = p_current->get_script(); |
| 1127 | if (c == p_script) { |
| 1128 | return p_current; |
| 1129 | } |
| 1130 | for (int i = 0; i < p_current->get_child_count(); i++) { |
| 1131 | Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script); |
| 1132 | if (found) { |
| 1133 | return found; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | return nullptr; |
| 1138 | } |
| 1139 | |
| 1140 | static void _find_changed_scripts_for_external_editor(Node *p_base, Node *p_current, HashSet<Ref<Script>> &r_scripts) { |
| 1141 | if (p_current->get_owner() != p_base && p_base != p_current) { |
| 1142 | return; |
| 1143 | } |
| 1144 | Ref<Script> c = p_current->get_script(); |
| 1145 | |
| 1146 | if (c.is_valid()) { |
| 1147 | r_scripts.insert(c); |
| 1148 | } |
| 1149 | |
| 1150 | for (int i = 0; i < p_current->get_child_count(); i++) { |
| 1151 | _find_changed_scripts_for_external_editor(p_base, p_current->get_child(i), r_scripts); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) { |
| 1156 | bool use_external_editor = bool(EDITOR_GET("text_editor/external/use_external_editor")); |
| 1157 | |
| 1158 | ERR_FAIL_NULL(get_tree()); |
| 1159 | |
| 1160 | HashSet<Ref<Script>> scripts; |
| 1161 | |
| 1162 | Node *base = get_tree()->get_edited_scene_root(); |
| 1163 | if (base) { |
| 1164 | _find_changed_scripts_for_external_editor(base, base, scripts); |
| 1165 | } |
| 1166 | |
| 1167 | for (const Ref<Script> &E : scripts) { |
| 1168 | Ref<Script> scr = E; |
| 1169 | |
| 1170 | if (!use_external_editor && !scr->get_language()->overrides_external_editor()) { |
| 1171 | continue; // We're not using an external editor for this script. |
| 1172 | } |
| 1173 | |
| 1174 | if (p_for_script.is_valid() && p_for_script != scr) { |
| 1175 | continue; |
| 1176 | } |
| 1177 | |
| 1178 | if (scr->is_built_in()) { |
| 1179 | continue; //internal script, who cares, though weird |
| 1180 | } |
| 1181 | |
| 1182 | uint64_t last_date = scr->get_last_modified_time(); |
| 1183 | uint64_t date = FileAccess::get_modified_time(scr->get_path()); |
| 1184 | |
| 1185 | if (last_date != date) { |
| 1186 | Ref<Script> rel_scr = ResourceLoader::load(scr->get_path(), scr->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE); |
| 1187 | ERR_CONTINUE(rel_scr.is_null()); |
| 1188 | scr->set_source_code(rel_scr->get_source_code()); |
| 1189 | scr->set_last_modified_time(rel_scr->get_last_modified_time()); |
| 1190 | scr->update_exports(); |
| 1191 | |
| 1192 | trigger_live_script_reload(scr->get_path()); |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | void ScriptTextEditor::_code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) { |
| 1198 | ScriptTextEditor *ste = (ScriptTextEditor *)p_ud; |
| 1199 | ste->_code_complete_script(p_code, r_options, r_force); |
| 1200 | } |
| 1201 | |
| 1202 | void ScriptTextEditor::_code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) { |
| 1203 | if (color_panel->is_visible()) { |
| 1204 | return; |
| 1205 | } |
| 1206 | Node *base = get_tree()->get_edited_scene_root(); |
| 1207 | if (base) { |
| 1208 | base = _find_node_for_script(base, base, script); |
| 1209 | } |
| 1210 | String hint; |
| 1211 | Error err = script->get_language()->complete_code(p_code, script->get_path(), base, r_options, r_force, hint); |
| 1212 | |
| 1213 | if (err == OK) { |
| 1214 | code_editor->get_text_editor()->set_code_hint(hint); |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | void ScriptTextEditor::_update_breakpoint_list() { |
| 1219 | breakpoints_menu->clear(); |
| 1220 | breakpoints_menu->reset_size(); |
| 1221 | |
| 1222 | breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT); |
| 1223 | breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS); |
| 1224 | breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT); |
| 1225 | breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT); |
| 1226 | |
| 1227 | PackedInt32Array breakpoint_list = code_editor->get_text_editor()->get_breakpointed_lines(); |
| 1228 | if (breakpoint_list.is_empty()) { |
| 1229 | return; |
| 1230 | } |
| 1231 | |
| 1232 | breakpoints_menu->add_separator(); |
| 1233 | |
| 1234 | for (int i = 0; i < breakpoint_list.size(); i++) { |
| 1235 | // Strip edges to remove spaces or tabs. |
| 1236 | // Also replace any tabs by spaces, since we can't print tabs in the menu. |
| 1237 | String line = code_editor->get_text_editor()->get_line(breakpoint_list[i]).replace("\t", " ").strip_edges(); |
| 1238 | |
| 1239 | // Limit the size of the line if too big. |
| 1240 | if (line.length() > 50) { |
| 1241 | line = line.substr(0, 50); |
| 1242 | } |
| 1243 | |
| 1244 | breakpoints_menu->add_item(String::num_int64(breakpoint_list[i] + 1) + " - `" + line + "`"); |
| 1245 | breakpoints_menu->set_item_metadata(-1, breakpoint_list[i]); |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | void ScriptTextEditor::_breakpoint_item_pressed(int p_idx) { |
| 1250 | if (p_idx < 4) { // Any item before the separator. |
| 1251 | _edit_option(breakpoints_menu->get_item_id(p_idx)); |
| 1252 | } else { |
| 1253 | code_editor->goto_line_centered(breakpoints_menu->get_item_metadata(p_idx)); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | void ScriptTextEditor::_breakpoint_toggled(int p_row) { |
| 1258 | const CodeEdit *ce = code_editor->get_text_editor(); |
| 1259 | bool enabled = p_row < ce->get_line_count() && ce->is_line_breakpointed(p_row); |
| 1260 | EditorDebuggerNode::get_singleton()->set_breakpoint(script->get_path(), p_row + 1, enabled); |
| 1261 | } |
| 1262 | |
| 1263 | void ScriptTextEditor::_on_caret_moved() { |
| 1264 | if (code_editor->is_previewing_navigation_change()) { |
| 1265 | return; |
| 1266 | } |
| 1267 | int current_line = code_editor->get_text_editor()->get_caret_line(); |
| 1268 | if (Math::abs(current_line - previous_line) >= 10) { |
| 1269 | Dictionary nav_state = get_navigation_state(); |
| 1270 | nav_state["row"] = previous_line; |
| 1271 | nav_state["scroll_position"] = -1; |
| 1272 | emit_signal(SNAME("request_save_previous_state"), nav_state); |
| 1273 | store_previous_state(); |
| 1274 | } |
| 1275 | previous_line = current_line; |
| 1276 | } |
| 1277 | |
| 1278 | void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_column) { |
| 1279 | Node *base = get_tree()->get_edited_scene_root(); |
| 1280 | if (base) { |
| 1281 | base = _find_node_for_script(base, base, script); |
| 1282 | } |
| 1283 | |
| 1284 | ScriptLanguage::LookupResult result; |
| 1285 | String code_text = code_editor->get_text_editor()->get_text_with_cursor_char(p_row, p_column); |
| 1286 | Error lc_error = script->get_language()->lookup_code(code_text, p_symbol, script->get_path(), base, result); |
| 1287 | if (ScriptServer::is_global_class(p_symbol)) { |
| 1288 | EditorNode::get_singleton()->load_resource(ScriptServer::get_global_class_path(p_symbol)); |
| 1289 | } else if (p_symbol.is_resource_file() || p_symbol.begins_with("uid://")) { |
| 1290 | if (DirAccess::dir_exists_absolute(p_symbol)) { |
| 1291 | FileSystemDock::get_singleton()->navigate_to_path(p_symbol); |
| 1292 | } else { |
| 1293 | EditorNode::get_singleton()->load_scene_or_resource(p_symbol); |
| 1294 | } |
| 1295 | } else if (lc_error == OK) { |
| 1296 | _goto_line(p_row); |
| 1297 | |
| 1298 | if (!result.class_name.is_empty() && EditorHelp::get_doc_data()->class_list.has(result.class_name) && !EditorHelp::get_doc_data()->class_list[result.class_name].is_script_doc) { |
| 1299 | switch (result.type) { |
| 1300 | case ScriptLanguage::LOOKUP_RESULT_CLASS: { |
| 1301 | emit_signal(SNAME("go_to_help"), "class_name:" + result.class_name); |
| 1302 | } break; |
| 1303 | case ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT: { |
| 1304 | StringName cname = result.class_name; |
| 1305 | while (ClassDB::class_exists(cname)) { |
| 1306 | if (ClassDB::has_integer_constant(cname, result.class_member, true)) { |
| 1307 | result.class_name = cname; |
| 1308 | break; |
| 1309 | } |
| 1310 | cname = ClassDB::get_parent_class(cname); |
| 1311 | } |
| 1312 | emit_signal(SNAME("go_to_help"), "class_constant:" + result.class_name + ":" + result.class_member); |
| 1313 | } break; |
| 1314 | case ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY: { |
| 1315 | StringName cname = result.class_name; |
| 1316 | while (ClassDB::class_exists(cname)) { |
| 1317 | if (ClassDB::has_property(cname, result.class_member, true)) { |
| 1318 | result.class_name = cname; |
| 1319 | break; |
| 1320 | } |
| 1321 | cname = ClassDB::get_parent_class(cname); |
| 1322 | } |
| 1323 | emit_signal(SNAME("go_to_help"), "class_property:" + result.class_name + ":" + result.class_member); |
| 1324 | } break; |
| 1325 | case ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD: { |
| 1326 | StringName cname = result.class_name; |
| 1327 | while (ClassDB::class_exists(cname)) { |
| 1328 | if (ClassDB::has_method(cname, result.class_member, true)) { |
| 1329 | result.class_name = cname; |
| 1330 | break; |
| 1331 | } |
| 1332 | cname = ClassDB::get_parent_class(cname); |
| 1333 | } |
| 1334 | emit_signal(SNAME("go_to_help"), "class_method:" + result.class_name + ":" + result.class_member); |
| 1335 | } break; |
| 1336 | case ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL: { |
| 1337 | StringName cname = result.class_name; |
| 1338 | while (ClassDB::class_exists(cname)) { |
| 1339 | if (ClassDB::has_signal(cname, result.class_member, true)) { |
| 1340 | result.class_name = cname; |
| 1341 | break; |
| 1342 | } |
| 1343 | cname = ClassDB::get_parent_class(cname); |
| 1344 | } |
| 1345 | emit_signal(SNAME("go_to_help"), "class_signal:" + result.class_name + ":" + result.class_member); |
| 1346 | } break; |
| 1347 | case ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM: { |
| 1348 | StringName cname = result.class_name; |
| 1349 | while (ClassDB::class_exists(cname)) { |
| 1350 | if (ClassDB::has_enum(cname, result.class_member, true)) { |
| 1351 | result.class_name = cname; |
| 1352 | break; |
| 1353 | } |
| 1354 | cname = ClassDB::get_parent_class(cname); |
| 1355 | } |
| 1356 | emit_signal(SNAME("go_to_help"), "class_enum:" + result.class_name + ":" + result.class_member); |
| 1357 | } break; |
| 1358 | case ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION: { |
| 1359 | emit_signal(SNAME("go_to_help"), "class_annotation:" + result.class_name + ":" + result.class_member); |
| 1360 | } break; |
| 1361 | case ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE: { // Deprecated. |
| 1362 | emit_signal(SNAME("go_to_help"), "class_global:" + result.class_name + ":" + result.class_member); |
| 1363 | } break; |
| 1364 | case ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION: |
| 1365 | case ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT: |
| 1366 | case ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE: |
| 1367 | case ScriptLanguage::LOOKUP_RESULT_MAX: { |
| 1368 | // Nothing to do. |
| 1369 | } break; |
| 1370 | } |
| 1371 | } else if (result.location >= 0) { |
| 1372 | if (result.script.is_valid()) { |
| 1373 | emit_signal(SNAME("request_open_script_at_line"), result.script, result.location - 1); |
| 1374 | } else { |
| 1375 | emit_signal(SNAME("request_save_history")); |
| 1376 | goto_line_centered(result.location - 1); |
| 1377 | } |
| 1378 | } |
| 1379 | } else if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) { |
| 1380 | // Check for Autoload scenes. |
| 1381 | const ProjectSettings::AutoloadInfo &info = ProjectSettings::get_singleton()->get_autoload(p_symbol); |
| 1382 | if (info.is_singleton) { |
| 1383 | EditorNode::get_singleton()->load_scene(info.path); |
| 1384 | } |
| 1385 | } else if (p_symbol.is_relative_path()) { |
| 1386 | // Every symbol other than absolute path is relative path so keep this condition at last. |
| 1387 | String path = _get_absolute_path(p_symbol); |
| 1388 | if (FileAccess::exists(path)) { |
| 1389 | EditorNode::get_singleton()->load_scene_or_resource(path); |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | void ScriptTextEditor::_validate_symbol(const String &p_symbol) { |
| 1395 | CodeEdit *text_edit = code_editor->get_text_editor(); |
| 1396 | |
| 1397 | Node *base = get_tree()->get_edited_scene_root(); |
| 1398 | if (base) { |
| 1399 | base = _find_node_for_script(base, base, script); |
| 1400 | } |
| 1401 | |
| 1402 | ScriptLanguage::LookupResult result; |
| 1403 | String lc_text = code_editor->get_text_editor()->get_text_for_symbol_lookup(); |
| 1404 | Error lc_error = script->get_language()->lookup_code(lc_text, p_symbol, script->get_path(), base, result); |
| 1405 | bool is_singleton = ProjectSettings::get_singleton()->has_autoload(p_symbol) && ProjectSettings::get_singleton()->get_autoload(p_symbol).is_singleton; |
| 1406 | if (lc_error == OK || is_singleton || ScriptServer::is_global_class(p_symbol) || p_symbol.is_resource_file() || p_symbol.begins_with("uid://")) { |
| 1407 | text_edit->set_symbol_lookup_word_as_valid(true); |
| 1408 | } else if (p_symbol.is_relative_path()) { |
| 1409 | String path = _get_absolute_path(p_symbol); |
| 1410 | if (FileAccess::exists(path)) { |
| 1411 | text_edit->set_symbol_lookup_word_as_valid(true); |
| 1412 | } else { |
| 1413 | text_edit->set_symbol_lookup_word_as_valid(false); |
| 1414 | } |
| 1415 | } else { |
| 1416 | text_edit->set_symbol_lookup_word_as_valid(false); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | void ScriptTextEditor::_show_symbol_tooltip(const String &p_symbol, int p_row, int p_column) { |
| 1421 | if (!EDITOR_GET("text_editor/behavior/documentation/enable_tooltips").booleanize()) { |
| 1422 | return; |
| 1423 | } |
| 1424 | |
| 1425 | if (p_symbol.begins_with("res://") || p_symbol.begins_with("uid://")) { |
| 1426 | Control *tmp = EditorHelpBitTooltip::make_tooltip(code_editor->get_text_editor(), "resource||" + p_symbol); |
| 1427 | memdelete(tmp); |
| 1428 | return; |
| 1429 | } |
| 1430 | |
| 1431 | Node *base = get_tree()->get_edited_scene_root(); |
| 1432 | if (base) { |
| 1433 | base = _find_node_for_script(base, base, script); |
| 1434 | } |
| 1435 | |
| 1436 | ScriptLanguage::LookupResult result; |
| 1437 | String doc_symbol; |
| 1438 | const String code_text = code_editor->get_text_editor()->get_text_with_cursor_char(p_row, p_column); |
| 1439 | const Error lc_error = script->get_language()->lookup_code(code_text, p_symbol, script->get_path(), base, result); |
| 1440 | if (lc_error == OK) { |
| 1441 | switch (result.type) { |
| 1442 | case ScriptLanguage::LOOKUP_RESULT_CLASS: { |
| 1443 | doc_symbol = "class|" + result.class_name + "|"; |
| 1444 | } break; |
| 1445 | case ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT: { |
| 1446 | StringName cname = result.class_name; |
| 1447 | while (ClassDB::class_exists(cname)) { |
| 1448 | if (ClassDB::has_integer_constant(cname, result.class_member, true)) { |
| 1449 | result.class_name = cname; |
| 1450 | break; |
| 1451 | } |
| 1452 | cname = ClassDB::get_parent_class(cname); |
| 1453 | } |
| 1454 | doc_symbol = "constant|" + result.class_name + "|" + result.class_member; |
| 1455 | } break; |
| 1456 | case ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY: { |
| 1457 | StringName cname = result.class_name; |
| 1458 | while (ClassDB::class_exists(cname)) { |
| 1459 | if (ClassDB::has_property(cname, result.class_member, true)) { |
| 1460 | result.class_name = cname; |
| 1461 | break; |
| 1462 | } |
| 1463 | cname = ClassDB::get_parent_class(cname); |
| 1464 | } |
| 1465 | doc_symbol = "property|" + result.class_name + "|" + result.class_member; |
| 1466 | } break; |
| 1467 | case ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD: { |
| 1468 | StringName cname = result.class_name; |
| 1469 | while (ClassDB::class_exists(cname)) { |
| 1470 | if (ClassDB::has_method(cname, result.class_member, true)) { |
| 1471 | result.class_name = cname; |
| 1472 | break; |
| 1473 | } |
| 1474 | cname = ClassDB::get_parent_class(cname); |
| 1475 | } |
| 1476 | doc_symbol = "method|" + result.class_name + "|" + result.class_member; |
| 1477 | } break; |
| 1478 | case ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL: { |
| 1479 | StringName cname = result.class_name; |
| 1480 | while (ClassDB::class_exists(cname)) { |
| 1481 | if (ClassDB::has_signal(cname, result.class_member, true)) { |
| 1482 | result.class_name = cname; |
| 1483 | break; |
| 1484 | } |
| 1485 | cname = ClassDB::get_parent_class(cname); |
| 1486 | } |
| 1487 | doc_symbol = "signal|" + result.class_name + "|" + result.class_member; |
| 1488 | } break; |
| 1489 | case ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM: { |
| 1490 | StringName cname = result.class_name; |
| 1491 | while (ClassDB::class_exists(cname)) { |
| 1492 | if (ClassDB::has_enum(cname, result.class_member, true)) { |
| 1493 | result.class_name = cname; |
| 1494 | break; |
| 1495 | } |
| 1496 | cname = ClassDB::get_parent_class(cname); |
| 1497 | } |
| 1498 | doc_symbol = "enum|" + result.class_name + "|" + result.class_member; |
| 1499 | } break; |
| 1500 | case ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION: { |
| 1501 | doc_symbol = "annotation|" + result.class_name + "|" + result.class_member; |
| 1502 | } break; |
| 1503 | case ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT: |
| 1504 | case ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE: { |
| 1505 | const String item_type = (result.type == ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT) ? "local_constant" : "local_variable"; |
| 1506 | Dictionary item_data; |
| 1507 | item_data["description"] = result.description; |
| 1508 | item_data["is_deprecated"] = result.is_deprecated; |
| 1509 | item_data["deprecated_message"] = result.deprecated_message; |
| 1510 | item_data["is_experimental"] = result.is_experimental; |
| 1511 | item_data["experimental_message"] = result.experimental_message; |
| 1512 | item_data["doc_type"] = result.doc_type; |
| 1513 | item_data["enumeration"] = result.enumeration; |
| 1514 | item_data["is_bitfield"] = result.is_bitfield; |
| 1515 | item_data["value"] = result.value; |
| 1516 | doc_symbol = item_type + "||" + p_symbol + "|" + JSON::stringify(item_data); |
| 1517 | } break; |
| 1518 | case ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION: |
| 1519 | case ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE: // Deprecated. |
| 1520 | case ScriptLanguage::LOOKUP_RESULT_MAX: { |
| 1521 | // Nothing to do. |
| 1522 | } break; |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | // NOTE: See also `ScriptEditor::_get_debug_tooltip()` for documentation tooltips disabled. |
| 1527 | String debug_value = EditorDebuggerNode::get_singleton()->get_var_value(p_symbol); |
| 1528 | if (!debug_value.is_empty()) { |
| 1529 | constexpr int DISPLAY_LIMIT = 1024; |
| 1530 | if (debug_value.size() > DISPLAY_LIMIT) { |
| 1531 | debug_value = debug_value.left(DISPLAY_LIMIT) + "... " + TTR("(truncated)"); |
| 1532 | } |
| 1533 | debug_value = TTR("Current value: ") + debug_value.replace("[", "[lb]"); |
| 1534 | } |
| 1535 | |
| 1536 | if (!doc_symbol.is_empty() || !debug_value.is_empty()) { |
| 1537 | Control *tmp = EditorHelpBitTooltip::make_tooltip(code_editor->get_text_editor(), doc_symbol, debug_value, true); |
| 1538 | memdelete(tmp); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | String ScriptTextEditor::_get_absolute_path(const String &rel_path) { |
| 1543 | String base_path = script->get_path().get_base_dir(); |
| 1544 | String path = base_path.path_join(rel_path); |
| 1545 | return path.replace("///", "//").simplify_path(); |
| 1546 | } |
| 1547 | |
| 1548 | void ScriptTextEditor::update_toggle_files_button() { |
| 1549 | code_editor->update_toggle_files_button(); |
| 1550 | } |
| 1551 | |
| 1552 | void ScriptTextEditor::_update_connected_methods() { |
| 1553 | CodeEdit *text_edit = code_editor->get_text_editor(); |
| 1554 | text_edit->set_gutter_width(connection_gutter, text_edit->get_line_height()); |
| 1555 | for (int i = 0; i < text_edit->get_line_count(); i++) { |
| 1556 | text_edit->set_line_gutter_metadata(i, connection_gutter, Dictionary()); |
| 1557 | text_edit->set_line_gutter_icon(i, connection_gutter, nullptr); |
| 1558 | text_edit->set_line_gutter_clickable(i, connection_gutter, false); |
| 1559 | } |
| 1560 | missing_connections.clear(); |
| 1561 | |
| 1562 | if (!script_is_valid) { |
| 1563 | return; |
| 1564 | } |
| 1565 | |
| 1566 | Node *base = get_tree()->get_edited_scene_root(); |
| 1567 | if (!base) { |
| 1568 | return; |
| 1569 | } |
| 1570 | |
| 1571 | // Add connection icons to methods. |
| 1572 | Vector<Node *> nodes = _find_all_node_for_script(base, base, script); |
| 1573 | HashSet<StringName> methods_found; |
| 1574 | for (int i = 0; i < nodes.size(); i++) { |
| 1575 | List<Connection> signal_connections; |
| 1576 | nodes[i]->get_signals_connected_to_this(&signal_connections); |
| 1577 | |
| 1578 | for (const Connection &connection : signal_connections) { |
| 1579 | if (!(connection.flags & CONNECT_PERSIST)) { |
| 1580 | continue; |
| 1581 | } |
| 1582 | |
| 1583 | // As deleted nodes are still accessible via the undo/redo system, check if they're still on the tree. |
| 1584 | Node *source = Object::cast_to<Node>(connection.signal.get_object()); |
| 1585 | if (source && !source->is_inside_tree()) { |
| 1586 | continue; |
| 1587 | } |
| 1588 | |
| 1589 | const StringName method = connection.callable.get_method(); |
| 1590 | if (methods_found.has(method)) { |
| 1591 | continue; |
| 1592 | } |
| 1593 | |
| 1594 | if (!ClassDB::has_method(script->get_instance_base_type(), method)) { |
| 1595 | int line = -1; |
| 1596 | |
| 1597 | for (int j = 0; j < functions.size(); j++) { |
| 1598 | String name = functions[j].get_slicec(':', 0); |
| 1599 | if (name == method) { |
| 1600 | Dictionary line_meta; |
| 1601 | line_meta["type"] = "connection"; |
| 1602 | line_meta["method"] = method; |
| 1603 | line = functions[j].get_slicec(':', 1).to_int() - 1; |
| 1604 | text_edit->set_line_gutter_metadata(line, connection_gutter, line_meta); |
| 1605 | text_edit->set_line_gutter_icon(line, connection_gutter, get_parent_control()->get_editor_theme_icon(SNAME("Slot"))); |
| 1606 | text_edit->set_line_gutter_clickable(line, connection_gutter, true); |
| 1607 | methods_found.insert(method); |
| 1608 | break; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | if (line >= 0) { |
| 1613 | continue; |
| 1614 | } |
| 1615 | |
| 1616 | // There is a chance that the method is inherited from another script. |
| 1617 | bool found_inherited_function = false; |
| 1618 | Ref<Script> inherited_script = script->get_base_script(); |
| 1619 | while (inherited_script.is_valid()) { |
| 1620 | if (inherited_script->has_method(method)) { |
| 1621 | found_inherited_function = true; |
| 1622 | break; |
| 1623 | } |
| 1624 | |
| 1625 | inherited_script = inherited_script->get_base_script(); |
| 1626 | } |
| 1627 | |
| 1628 | if (!found_inherited_function) { |
| 1629 | missing_connections.push_back(connection); |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | // Add override icons to methods. |
| 1636 | methods_found.clear(); |
| 1637 | for (int i = 0; i < functions.size(); i++) { |
| 1638 | String raw_name = functions[i].get_slicec(':', 0); |
| 1639 | StringName name = StringName(raw_name); |
| 1640 | if (methods_found.has(name)) { |
| 1641 | continue; |
| 1642 | } |
| 1643 | |
| 1644 | // Account for inner classes by stripping the class names from the method, |
| 1645 | // starting from the right since our inner class might be inside of another inner class. |
| 1646 | int pos = raw_name.rfind_char('.'); |
| 1647 | if (pos != -1) { |
| 1648 | name = raw_name.substr(pos + 1); |
| 1649 | } |
| 1650 | |
| 1651 | String found_base_class; |
| 1652 | StringName base_class = script->get_instance_base_type(); |
| 1653 | Ref<Script> inherited_script = script->get_base_script(); |
| 1654 | while (inherited_script.is_valid()) { |
| 1655 | if (inherited_script->has_method(name)) { |
| 1656 | found_base_class = "script:" + inherited_script->get_path(); |
| 1657 | break; |
| 1658 | } |
| 1659 | |
| 1660 | base_class = inherited_script->get_instance_base_type(); |
| 1661 | inherited_script = inherited_script->get_base_script(); |
| 1662 | } |
| 1663 | |
| 1664 | if (found_base_class.is_empty()) { |
| 1665 | while (base_class) { |
| 1666 | List<MethodInfo> methods; |
| 1667 | ClassDB::get_method_list(base_class, &methods, true); |
| 1668 | for (const MethodInfo &mi : methods) { |
| 1669 | if (mi.name == name) { |
| 1670 | found_base_class = "builtin:" + base_class; |
| 1671 | break; |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | ClassDB::ClassInfo *base_class_ptr = ClassDB::classes.getptr(base_class)->inherits_ptr; |
| 1676 | if (base_class_ptr == nullptr) { |
| 1677 | break; |
| 1678 | } |
| 1679 | base_class = base_class_ptr->name; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | if (!found_base_class.is_empty()) { |
| 1684 | int line = functions[i].get_slicec(':', 1).to_int() - 1; |
| 1685 | |
| 1686 | Dictionary line_meta = text_edit->get_line_gutter_metadata(line, connection_gutter); |
| 1687 | if (line_meta.is_empty()) { |
| 1688 | // Add override icon to gutter. |
| 1689 | line_meta["type"] = "inherits"; |
| 1690 | line_meta["method"] = name; |
| 1691 | line_meta["base_class"] = found_base_class; |
| 1692 | text_edit->set_line_gutter_icon(line, connection_gutter, get_parent_control()->get_editor_theme_icon(SNAME("MethodOverride"))); |
| 1693 | text_edit->set_line_gutter_clickable(line, connection_gutter, true); |
| 1694 | } else { |
| 1695 | // If method is also connected to signal, then merge icons and keep the click behavior of the slot. |
| 1696 | text_edit->set_line_gutter_icon(line, connection_gutter, get_parent_control()->get_editor_theme_icon(SNAME("MethodOverrideAndSlot"))); |
| 1697 | } |
| 1698 | |
| 1699 | methods_found.insert(StringName(raw_name)); |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | void ScriptTextEditor::_update_gutter_indexes() { |
| 1705 | for (int i = 0; i < code_editor->get_text_editor()->get_gutter_count(); i++) { |
| 1706 | if (code_editor->get_text_editor()->get_gutter_name(i) == "connection_gutter") { |
| 1707 | connection_gutter = i; |
| 1708 | continue; |
| 1709 | } |
| 1710 | |
| 1711 | if (code_editor->get_text_editor()->get_gutter_name(i) == "line_numbers") { |
| 1712 | line_number_gutter = i; |
| 1713 | continue; |
| 1714 | } |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | void ScriptTextEditor::_gutter_clicked(int p_line, int p_gutter) { |
| 1719 | if (p_gutter != connection_gutter) { |
| 1720 | return; |
| 1721 | } |
| 1722 | |
| 1723 | Dictionary meta = code_editor->get_text_editor()->get_line_gutter_metadata(p_line, p_gutter); |
| 1724 | String type = meta.get("type", ""); |
| 1725 | if (type.is_empty()) { |
| 1726 | return; |
| 1727 | } |
| 1728 | |
| 1729 | // All types currently need a method name. |
| 1730 | String method = meta.get("method", ""); |
| 1731 | if (method.is_empty()) { |
| 1732 | return; |
| 1733 | } |
| 1734 | |
| 1735 | if (type == "connection") { |
| 1736 | Node *base = get_tree()->get_edited_scene_root(); |
| 1737 | if (!base) { |
| 1738 | return; |
| 1739 | } |
| 1740 | |
| 1741 | Vector<Node *> nodes = _find_all_node_for_script(base, base, script); |
| 1742 | connection_info_dialog->popup_connections(method, nodes); |
| 1743 | } else if (type == "inherits") { |
| 1744 | String base_class_raw = meta["base_class"]; |
| 1745 | PackedStringArray base_class_split = base_class_raw.split(":", true, 1); |
| 1746 | |
| 1747 | if (base_class_split[0] == "script") { |
| 1748 | // Go to function declaration. |
| 1749 | Ref<Script> base_script = ResourceLoader::load(base_class_split[1]); |
| 1750 | ERR_FAIL_COND(base_script.is_null()); |
| 1751 | emit_signal(SNAME("go_to_method"), base_script, method); |
| 1752 | } else if (base_class_split[0] == "builtin") { |
| 1753 | // Open method documentation. |
| 1754 | emit_signal(SNAME("go_to_help"), "class_method:" + base_class_split[1] + ":" + method); |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | void ScriptTextEditor::_edit_option(int p_op) { |
| 1760 | CodeEdit *tx = code_editor->get_text_editor(); |
| 1761 | tx->apply_ime(); |
| 1762 | |
| 1763 | switch (p_op) { |
| 1764 | case EDIT_UNDO: { |
| 1765 | tx->undo(); |
| 1766 | callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false); |
| 1767 | } break; |
| 1768 | case EDIT_REDO: { |
| 1769 | tx->redo(); |
| 1770 | callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false); |
| 1771 | } break; |
| 1772 | case EDIT_CUT: { |
| 1773 | tx->cut(); |
| 1774 | callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false); |
| 1775 | } break; |
| 1776 | case EDIT_COPY: { |
| 1777 | tx->copy(); |
| 1778 | callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false); |
| 1779 | } break; |
| 1780 | case EDIT_PASTE: { |
| 1781 | tx->paste(); |
| 1782 | callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false); |
| 1783 | } break; |
| 1784 | case EDIT_SELECT_ALL: { |
| 1785 | tx->select_all(); |
| 1786 | callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false); |
| 1787 | } break; |
| 1788 | case EDIT_MOVE_LINE_UP: { |
| 1789 | code_editor->get_text_editor()->move_lines_up(); |
| 1790 | } break; |
| 1791 | case EDIT_MOVE_LINE_DOWN: { |
| 1792 | code_editor->get_text_editor()->move_lines_down(); |
| 1793 | } break; |
| 1794 | case EDIT_INDENT: { |
| 1795 | Ref<Script> scr = script; |
| 1796 | if (scr.is_null()) { |
| 1797 | return; |
| 1798 | } |
| 1799 | tx->indent_lines(); |
| 1800 | } break; |
| 1801 | case EDIT_UNINDENT: { |
| 1802 | Ref<Script> scr = script; |
| 1803 | if (scr.is_null()) { |
| 1804 | return; |
| 1805 | } |
| 1806 | tx->unindent_lines(); |
| 1807 | } break; |
| 1808 | case EDIT_DELETE_LINE: { |
| 1809 | code_editor->get_text_editor()->delete_lines(); |
| 1810 | } break; |
| 1811 | case EDIT_DUPLICATE_SELECTION: { |
| 1812 | code_editor->get_text_editor()->duplicate_selection(); |
| 1813 | } break; |
| 1814 | case EDIT_DUPLICATE_LINES: { |
| 1815 | code_editor->get_text_editor()->duplicate_lines(); |
| 1816 | } break; |
| 1817 | case EDIT_TOGGLE_FOLD_LINE: { |
| 1818 | tx->toggle_foldable_lines_at_carets(); |
| 1819 | } break; |
| 1820 | case EDIT_FOLD_ALL_LINES: { |
| 1821 | tx->fold_all_lines(); |
| 1822 | } break; |
| 1823 | case EDIT_UNFOLD_ALL_LINES: { |
| 1824 | tx->unfold_all_lines(); |
| 1825 | } break; |
| 1826 | case EDIT_CREATE_CODE_REGION: { |
| 1827 | tx->create_code_region(); |
| 1828 | } break; |
| 1829 | case EDIT_TOGGLE_COMMENT: { |
| 1830 | _edit_option_toggle_inline_comment(); |
| 1831 | } break; |
| 1832 | case EDIT_COMPLETE: { |
| 1833 | tx->request_code_completion(true); |
| 1834 | } break; |
| 1835 | case EDIT_AUTO_INDENT: { |
| 1836 | String text = tx->get_text(); |
| 1837 | Ref<Script> scr = script; |
| 1838 | if (scr.is_null()) { |
| 1839 | return; |
| 1840 | } |
| 1841 | |
| 1842 | tx->begin_complex_operation(); |
| 1843 | tx->begin_multicaret_edit(); |
| 1844 | int begin = tx->get_line_count() - 1, end = 0; |
| 1845 | if (tx->has_selection()) { |
| 1846 | // Auto indent all lines that have a caret or selection on it. |
| 1847 | Vector<Point2i> line_ranges = tx->get_line_ranges_from_carets(); |
| 1848 | for (Point2i line_range : line_ranges) { |
| 1849 | scr->get_language()->auto_indent_code(text, line_range.x, line_range.y); |
| 1850 | if (line_range.x < begin) { |
| 1851 | begin = line_range.x; |
| 1852 | } |
| 1853 | if (line_range.y > end) { |
| 1854 | end = line_range.y; |
| 1855 | } |
| 1856 | } |
| 1857 | } else { |
| 1858 | // Auto indent entire text. |
| 1859 | begin = 0; |
| 1860 | end = tx->get_line_count() - 1; |
| 1861 | scr->get_language()->auto_indent_code(text, begin, end); |
| 1862 | } |
| 1863 | |
| 1864 | // Apply auto indented code. |
| 1865 | Vector<String> lines = text.split("\n"); |
| 1866 | for (int i = begin; i <= end; ++i) { |
| 1867 | tx->set_line(i, lines[i]); |
| 1868 | } |
| 1869 | |
| 1870 | tx->end_multicaret_edit(); |
| 1871 | tx->end_complex_operation(); |
| 1872 | } break; |
| 1873 | case EDIT_TRIM_TRAILING_WHITESAPCE: { |
| 1874 | trim_trailing_whitespace(); |
| 1875 | } break; |
| 1876 | case EDIT_TRIM_FINAL_NEWLINES: { |
| 1877 | trim_final_newlines(); |
| 1878 | } break; |
| 1879 | case EDIT_CONVERT_INDENT_TO_SPACES: { |
| 1880 | code_editor->set_indent_using_spaces(true); |
| 1881 | convert_indent(); |
| 1882 | } break; |
| 1883 | case EDIT_CONVERT_INDENT_TO_TABS: { |
| 1884 | code_editor->set_indent_using_spaces(false); |
| 1885 | convert_indent(); |
| 1886 | } break; |
| 1887 | case EDIT_PICK_COLOR: { |
| 1888 | color_panel->popup(); |
| 1889 | } break; |
| 1890 | case EDIT_TO_UPPERCASE: { |
| 1891 | _convert_case(CodeTextEditor::UPPER); |
| 1892 | } break; |
| 1893 | case EDIT_TO_LOWERCASE: { |
| 1894 | _convert_case(CodeTextEditor::LOWER); |
| 1895 | } break; |
| 1896 | case EDIT_CAPITALIZE: { |
| 1897 | _convert_case(CodeTextEditor::CAPITALIZE); |
| 1898 | } break; |
| 1899 | case EDIT_EVALUATE: { |
| 1900 | Expression expression; |
| 1901 | tx->begin_complex_operation(); |
| 1902 | for (int caret_idx = 0; caret_idx < tx->get_caret_count(); caret_idx++) { |
| 1903 | Vector<String> lines = tx->get_selected_text(caret_idx).split("\n"); |
| 1904 | PackedStringArray results; |
| 1905 | |
| 1906 | for (int i = 0; i < lines.size(); i++) { |
| 1907 | const String &line = lines[i]; |
| 1908 | String whitespace = line.substr(0, line.size() - line.strip_edges(true, false).size()); // Extract the whitespace at the beginning. |
| 1909 | if (expression.parse(line) == OK) { |
| 1910 | Variant result = expression.execute(Array(), Variant(), false, true); |
| 1911 | if (expression.get_error_text().is_empty()) { |
| 1912 | results.push_back(whitespace + result.get_construct_string()); |
| 1913 | } else { |
| 1914 | results.push_back(line); |
| 1915 | } |
| 1916 | } else { |
| 1917 | results.push_back(line); |
| 1918 | } |
| 1919 | } |
| 1920 | tx->insert_text_at_caret(String("\n").join(results), caret_idx); |
| 1921 | } |
| 1922 | tx->end_complex_operation(); |
| 1923 | } break; |
| 1924 | case EDIT_TOGGLE_WORD_WRAP: { |
| 1925 | TextEdit::LineWrappingMode wrap = code_editor->get_text_editor()->get_line_wrapping_mode(); |
| 1926 | code_editor->get_text_editor()->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY); |
| 1927 | } break; |
| 1928 | case SEARCH_FIND: { |
| 1929 | code_editor->get_find_replace_bar()->popup_search(); |
| 1930 | } break; |
| 1931 | case SEARCH_FIND_NEXT: { |
| 1932 | code_editor->get_find_replace_bar()->search_next(); |
| 1933 | } break; |
| 1934 | case SEARCH_FIND_PREV: { |
| 1935 | code_editor->get_find_replace_bar()->search_prev(); |
| 1936 | } break; |
| 1937 | case SEARCH_REPLACE: { |
| 1938 | code_editor->get_find_replace_bar()->popup_replace(); |
| 1939 | } break; |
| 1940 | case SEARCH_IN_FILES: { |
| 1941 | String selected_text = tx->get_selected_text(); |
| 1942 | |
| 1943 | // Yep, because it doesn't make sense to instance this dialog for every single script open... |
| 1944 | // So this will be delegated to the ScriptEditor. |
| 1945 | emit_signal(SNAME("search_in_files_requested"), selected_text); |
| 1946 | } break; |
| 1947 | case REPLACE_IN_FILES: { |
| 1948 | String selected_text = tx->get_selected_text(); |
| 1949 | |
| 1950 | emit_signal(SNAME("replace_in_files_requested"), selected_text); |
| 1951 | } break; |
| 1952 | case SEARCH_LOCATE_FUNCTION: { |
| 1953 | quick_open->popup_dialog(get_functions()); |
| 1954 | } break; |
| 1955 | case SEARCH_GOTO_LINE: { |
| 1956 | goto_line_popup->popup_find_line(code_editor); |
| 1957 | } break; |
| 1958 | case BOOKMARK_TOGGLE: { |
| 1959 | code_editor->toggle_bookmark(); |
| 1960 | } break; |
| 1961 | case BOOKMARK_GOTO_NEXT: { |
| 1962 | code_editor->goto_next_bookmark(); |
| 1963 | } break; |
| 1964 | case BOOKMARK_GOTO_PREV: { |
| 1965 | code_editor->goto_prev_bookmark(); |
| 1966 | } break; |
| 1967 | case BOOKMARK_REMOVE_ALL: { |
| 1968 | code_editor->remove_all_bookmarks(); |
| 1969 | } break; |
| 1970 | case DEBUG_TOGGLE_BREAKPOINT: { |
| 1971 | Vector<int> sorted_carets = tx->get_sorted_carets(); |
| 1972 | int last_line = -1; |
| 1973 | for (const int &c : sorted_carets) { |
| 1974 | int from = tx->get_selection_from_line(c); |
| 1975 | from += from == last_line ? 1 : 0; |
| 1976 | int to = tx->get_selection_to_line(c); |
| 1977 | if (to < from) { |
| 1978 | continue; |
| 1979 | } |
| 1980 | // Check first if there's any lines with breakpoints in the selection. |
| 1981 | bool selection_has_breakpoints = false; |
| 1982 | for (int line = from; line <= to; line++) { |
| 1983 | if (tx->is_line_breakpointed(line)) { |
| 1984 | selection_has_breakpoints = true; |
| 1985 | break; |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | // Set breakpoint on caret or remove all bookmarks from the selection. |
| 1990 | if (!selection_has_breakpoints) { |
| 1991 | if (tx->get_caret_line(c) != last_line) { |
| 1992 | tx->set_line_as_breakpoint(tx->get_caret_line(c), true); |
| 1993 | } |
| 1994 | } else { |
| 1995 | for (int line = from; line <= to; line++) { |
| 1996 | tx->set_line_as_breakpoint(line, false); |
| 1997 | } |
| 1998 | } |
| 1999 | last_line = to; |
| 2000 | } |
| 2001 | } break; |
| 2002 | case DEBUG_REMOVE_ALL_BREAKPOINTS: { |
| 2003 | PackedInt32Array bpoints = tx->get_breakpointed_lines(); |
| 2004 | |
| 2005 | for (int i = 0; i < bpoints.size(); i++) { |
| 2006 | int line = bpoints[i]; |
| 2007 | bool dobreak = !tx->is_line_breakpointed(line); |
| 2008 | tx->set_line_as_breakpoint(line, dobreak); |
| 2009 | EditorDebuggerNode::get_singleton()->set_breakpoint(script->get_path(), line + 1, dobreak); |
| 2010 | } |
| 2011 | } break; |
| 2012 | case DEBUG_GOTO_NEXT_BREAKPOINT: { |
| 2013 | PackedInt32Array bpoints = tx->get_breakpointed_lines(); |
| 2014 | if (bpoints.is_empty()) { |
| 2015 | return; |
| 2016 | } |
| 2017 | |
| 2018 | int current_line = tx->get_caret_line(); |
| 2019 | int bpoint_idx = 0; |
| 2020 | if (current_line < (int)bpoints[bpoints.size() - 1]) { |
| 2021 | while (bpoint_idx < bpoints.size() && bpoints[bpoint_idx] <= current_line) { |
| 2022 | bpoint_idx++; |
| 2023 | } |
| 2024 | } |
| 2025 | code_editor->goto_line_centered(bpoints[bpoint_idx]); |
| 2026 | } break; |
| 2027 | case DEBUG_GOTO_PREV_BREAKPOINT: { |
| 2028 | PackedInt32Array bpoints = tx->get_breakpointed_lines(); |
| 2029 | if (bpoints.is_empty()) { |
| 2030 | return; |
| 2031 | } |
| 2032 | |
| 2033 | int current_line = tx->get_caret_line(); |
| 2034 | int bpoint_idx = bpoints.size() - 1; |
| 2035 | if (current_line > (int)bpoints[0]) { |
| 2036 | while (bpoint_idx >= 0 && bpoints[bpoint_idx] >= current_line) { |
| 2037 | bpoint_idx--; |
| 2038 | } |
| 2039 | } |
| 2040 | code_editor->goto_line_centered(bpoints[bpoint_idx]); |
| 2041 | } break; |
| 2042 | case HELP_CONTEXTUAL: { |
| 2043 | String text = tx->get_selected_text(0); |
| 2044 | if (text.is_empty()) { |
| 2045 | text = tx->get_word_under_caret(0); |
| 2046 | } |
| 2047 | if (!text.is_empty()) { |
| 2048 | emit_signal(SNAME("request_help"), text); |
| 2049 | } |
| 2050 | } break; |
| 2051 | case LOOKUP_SYMBOL: { |
| 2052 | String text = tx->get_word_under_caret(0); |
| 2053 | if (text.is_empty()) { |
| 2054 | text = tx->get_selected_text(0); |
| 2055 | } |
| 2056 | if (!text.is_empty()) { |
| 2057 | _lookup_symbol(text, tx->get_caret_line(0), tx->get_caret_column(0)); |
| 2058 | } |
| 2059 | } break; |
| 2060 | case EDIT_EMOJI_AND_SYMBOL: { |
| 2061 | code_editor->get_text_editor()->show_emoji_and_symbol_picker(); |
| 2062 | } break; |
| 2063 | default: { |
| 2064 | if (p_op >= EditorContextMenuPlugin::BASE_ID) { |
| 2065 | EditorContextMenuPluginManager::get_singleton()->activate_custom_option(EditorContextMenuPlugin::CONTEXT_SLOT_SCRIPT_EDITOR_CODE, p_op, tx); |
| 2066 | } |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | void ScriptTextEditor::_edit_option_toggle_inline_comment() { |
| 2072 | if (script.is_null()) { |
| 2073 | return; |
| 2074 | } |
| 2075 | |
| 2076 | String delimiter = "#"; |
| 2077 | |
| 2078 | for (const String &script_delimiter : script->get_language()->get_comment_delimiters()) { |
| 2079 | if (!script_delimiter.contains_char(' ')) { |
| 2080 | delimiter = script_delimiter; |
| 2081 | break; |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | code_editor->toggle_inline_comment(delimiter); |
| 2086 | } |
| 2087 | |
| 2088 | void ScriptTextEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) { |
| 2089 | ERR_FAIL_COND(p_highlighter.is_null()); |
| 2090 | |
| 2091 | highlighters[p_highlighter->_get_name()] = p_highlighter; |
| 2092 | highlighter_menu->add_radio_check_item(p_highlighter->_get_name()); |
| 2093 | } |
| 2094 | |
| 2095 | void ScriptTextEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) { |
| 2096 | ERR_FAIL_COND(p_highlighter.is_null()); |
| 2097 | |
| 2098 | HashMap<String, Ref<EditorSyntaxHighlighter>>::Iterator el = highlighters.begin(); |
| 2099 | while (el) { |
| 2100 | int highlighter_index = highlighter_menu->get_item_idx_from_text(el->key); |
| 2101 | highlighter_menu->set_item_checked(highlighter_index, el->value == p_highlighter); |
| 2102 | ++el; |
| 2103 | } |
| 2104 | |
| 2105 | CodeEdit *te = code_editor->get_text_editor(); |
| 2106 | p_highlighter->_set_edited_resource(script); |
| 2107 | te->set_syntax_highlighter(p_highlighter); |
| 2108 | } |
| 2109 | |
| 2110 | void ScriptTextEditor::_change_syntax_highlighter(int p_idx) { |
| 2111 | set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]); |
| 2112 | } |
| 2113 | |
| 2114 | void ScriptTextEditor::_notification(int p_what) { |
| 2115 | switch (p_what) { |
| 2116 | case NOTIFICATION_TRANSLATION_CHANGED: { |
| 2117 | if (is_ready() && is_visible_in_tree()) { |
| 2118 | _update_errors(); |
| 2119 | _update_warnings(); |
| 2120 | } |
| 2121 | } break; |
| 2122 | |
| 2123 | case NOTIFICATION_THEME_CHANGED: |
| 2124 | if (!editor_enabled) { |
| 2125 | break; |
| 2126 | } |
| 2127 | if (is_visible_in_tree()) { |
| 2128 | _update_warnings(); |
| 2129 | _update_errors(); |
| 2130 | _update_background_color(); |
| 2131 | } |
| 2132 | [[fallthrough]]; |
| 2133 | case NOTIFICATION_ENTER_TREE: { |
| 2134 | code_editor->get_text_editor()->set_gutter_width(connection_gutter, code_editor->get_text_editor()->get_line_height()); |
| 2135 | Ref<Font> code_font = get_theme_font("font", "CodeEdit"); |
| 2136 | inline_color_options->add_theme_font_override("font", code_font); |
| 2137 | inline_color_options->get_popup()->add_theme_font_override("font", code_font); |
| 2138 | } break; |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | Control *ScriptTextEditor::get_edit_menu() { |
| 2143 | return edit_hb; |
| 2144 | } |
| 2145 | |
| 2146 | void ScriptTextEditor::clear_edit_menu() { |
| 2147 | if (editor_enabled) { |
| 2148 | memdelete(edit_hb); |
| 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | void ScriptTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) { |
| 2153 | code_editor->set_find_replace_bar(p_bar); |
| 2154 | } |
| 2155 | |
| 2156 | void ScriptTextEditor::reload(bool p_soft) { |
| 2157 | CodeEdit *te = code_editor->get_text_editor(); |
| 2158 | Ref<Script> scr = script; |
| 2159 | if (scr.is_null()) { |
| 2160 | return; |
| 2161 | } |
| 2162 | scr->set_source_code(te->get_text()); |
| 2163 | bool soft = p_soft || ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorPlugin"); // Always soft-reload editor plugins. |
| 2164 | |
| 2165 | scr->get_language()->reload_tool_script(scr, soft); |
| 2166 | } |
| 2167 | |
| 2168 | PackedInt32Array ScriptTextEditor::get_breakpoints() { |
| 2169 | return code_editor->get_text_editor()->get_breakpointed_lines(); |
| 2170 | } |
| 2171 | |
| 2172 | void ScriptTextEditor::set_breakpoint(int p_line, bool p_enabled) { |
| 2173 | code_editor->get_text_editor()->set_line_as_breakpoint(p_line, p_enabled); |
| 2174 | } |
| 2175 | |
| 2176 | void ScriptTextEditor::clear_breakpoints() { |
| 2177 | code_editor->get_text_editor()->clear_breakpointed_lines(); |
| 2178 | } |
| 2179 | |
| 2180 | void ScriptTextEditor::set_tooltip_request_func(const Callable &p_toolip_callback) { |
| 2181 | Variant args[1] = { this }; |
| 2182 | const Variant *argp[] = { &args[0] }; |
| 2183 | code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1)); |
| 2184 | } |
| 2185 | |
| 2186 | void ScriptTextEditor::set_debugger_active(bool p_active) { |
| 2187 | } |
| 2188 | |
| 2189 | Control *ScriptTextEditor::get_base_editor() const { |
| 2190 | return code_editor->get_text_editor(); |
| 2191 | } |
| 2192 | |
| 2193 | CodeTextEditor *ScriptTextEditor::get_code_editor() const { |
| 2194 | return code_editor; |
| 2195 | } |
| 2196 | |
| 2197 | Variant ScriptTextEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) { |
| 2198 | return Variant(); |
| 2199 | } |
| 2200 | |
| 2201 | bool ScriptTextEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { |
| 2202 | Dictionary d = p_data; |
| 2203 | if (d.has("type") && |
| 2204 | (String(d["type"]) == "resource" || |
| 2205 | String(d["type"]) == "files" || |
| 2206 | String(d["type"]) == "nodes" || |
| 2207 | String(d["type"]) == "obj_property" || |
| 2208 | String(d["type"]) == "files_and_dirs")) { |
| 2209 | return true; |
| 2210 | } |
| 2211 | |
| 2212 | return false; |
| 2213 | } |
| 2214 | |
| 2215 | static Node *_find_script_node(Node *p_current_node, const Ref<Script> &script) { |
| 2216 | if (p_current_node->get_script() == script) { |
| 2217 | return p_current_node; |
| 2218 | } |
| 2219 | |
| 2220 | for (int i = 0; i < p_current_node->get_child_count(); i++) { |
| 2221 | Node *n = _find_script_node(p_current_node->get_child(i), script); |
| 2222 | if (n) { |
| 2223 | return n; |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | return nullptr; |
| 2228 | } |
| 2229 | |
| 2230 | static String _quote_drop_data(const String &str) { |
| 2231 | // This function prepares a string for being "dropped" into the script editor. |
| 2232 | // The string can be a resource path, node path or property name. |
| 2233 | |
| 2234 | const bool using_single_quotes = EDITOR_GET("text_editor/completion/use_single_quotes"); |
| 2235 | |
| 2236 | String escaped = str.c_escape(); |
| 2237 | |
| 2238 | // If string is double quoted, there is no need to escape single quotes. |
| 2239 | // We can revert the extra escaping added in c_escape(). |
| 2240 | if (!using_single_quotes) { |
| 2241 | escaped = escaped.replace("\\'", "\'"); |
| 2242 | } |
| 2243 | |
| 2244 | return escaped.quote(using_single_quotes ? "'" : "\""); |
| 2245 | } |
| 2246 | |
| 2247 | static String _get_dropped_resource_as_member(const Ref<Resource> &p_resource, bool p_create_field, bool p_allow_uid) { |
| 2248 | String path = p_resource->get_path(); |
| 2249 | if (p_allow_uid) { |
| 2250 | ResourceUID::ID id = ResourceLoader::get_resource_uid(path); |
| 2251 | if (id != ResourceUID::INVALID_ID) { |
| 2252 | path = ResourceUID::get_singleton()->id_to_text(id); |
| 2253 | } |
| 2254 | } |
| 2255 | const bool is_script = ClassDB::is_parent_class(p_resource->get_class(), "Script"); |
| 2256 | |
| 2257 | if (!p_create_field) { |
| 2258 | return vformat("preload(%s)", _quote_drop_data(path)); |
| 2259 | } |
| 2260 | |
| 2261 | String variable_name = p_resource->get_name(); |
| 2262 | if (variable_name.is_empty()) { |
| 2263 | variable_name = p_resource->get_path().get_file().get_basename(); |
| 2264 | } |
| 2265 | |
| 2266 | if (is_script) { |
| 2267 | variable_name = variable_name.to_pascal_case().validate_unicode_identifier(); |
| 2268 | } else { |
| 2269 | variable_name = variable_name.to_snake_case().to_upper().validate_unicode_identifier(); |
| 2270 | } |
| 2271 | return vformat("const %s = preload(%s)", variable_name, _quote_drop_data(path)); |
| 2272 | } |
| 2273 | |
| 2274 | String ScriptTextEditor::_get_dropped_resource_as_exported_member(const Ref<Resource> &p_resource, const Vector<ObjectID> &p_script_instance_obj_ids) { |
| 2275 | String variable_name = p_resource->get_name(); |
| 2276 | if (variable_name.is_empty()) { |
| 2277 | variable_name = p_resource->get_path().get_file().get_basename(); |
| 2278 | } |
| 2279 | |
| 2280 | variable_name = variable_name.to_snake_case().validate_unicode_identifier(); |
| 2281 | |
| 2282 | StringName class_name = p_resource->get_class(); |
| 2283 | Ref<Script> resource_script = p_resource->get_script(); |
| 2284 | |
| 2285 | if (resource_script.is_valid()) { |
| 2286 | StringName global_resource_script_name = resource_script->get_global_name(); |
| 2287 | if (!global_resource_script_name.is_empty()) { |
| 2288 | class_name = global_resource_script_name; |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | for (ObjectID obj_id : p_script_instance_obj_ids) { |
| 2293 | pending_dragged_exports.push_back(DraggedExport{ obj_id, variable_name, p_resource, class_name }); |
| 2294 | } |
| 2295 | |
| 2296 | return vformat("@export var %s: %s", variable_name, class_name); |
| 2297 | } |
| 2298 | |
| 2299 | void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { |
| 2300 | Dictionary d = p_data; |
| 2301 | |
| 2302 | CodeEdit *te = code_editor->get_text_editor(); |
| 2303 | Point2i pos = (p_point == Vector2(Math::INF, Math::INF)) ? Point2i(te->get_caret_line(0), te->get_caret_column(0)) : te->get_line_column_at_pos(p_point); |
| 2304 | int drop_at_line = pos.y; |
| 2305 | int drop_at_column = pos.x; |
| 2306 | int selection_index = te->get_selection_at_line_column(drop_at_line, drop_at_column); |
| 2307 | |
| 2308 | bool is_empty_line = false; |
| 2309 | if (selection_index >= 0) { |
| 2310 | // Dropped on a selection, it will be replaced. |
| 2311 | drop_at_line = te->get_selection_from_line(selection_index); |
| 2312 | drop_at_column = te->get_selection_from_column(selection_index); |
| 2313 | is_empty_line = drop_at_column <= te->get_first_non_whitespace_column(drop_at_line) && te->get_selection_to_column(selection_index) == te->get_line(te->get_selection_to_line(selection_index)).length(); |
| 2314 | } |
| 2315 | |
| 2316 | Node *scene_root = get_tree()->get_edited_scene_root(); |
| 2317 | |
| 2318 | const bool member_drop_modifier_pressed = Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL); |
| 2319 | const bool export_drop_modifier_pressed = Input::get_singleton()->is_key_pressed(Key::ALT); |
| 2320 | |
| 2321 | const bool allow_uid = Input::get_singleton()->is_key_pressed(Key::SHIFT) != bool(EDITOR_GET("text_editor/behavior/files/drop_preload_resources_as_uid")); |
| 2322 | const String &line = te->get_line(drop_at_line); |
| 2323 | |
| 2324 | if (selection_index < 0) { |
| 2325 | is_empty_line = line.is_empty() || te->get_first_non_whitespace_column(drop_at_line) == line.length(); |
| 2326 | } |
| 2327 | |
| 2328 | String text_to_drop; |
| 2329 | bool add_new_line = false; |
| 2330 | |
| 2331 | const String type = d.get("type", ""); |
| 2332 | if (type == "resource") { |
| 2333 | Ref<Resource> resource = d["resource"]; |
| 2334 | if (resource.is_null()) { |
| 2335 | return; |
| 2336 | } |
| 2337 | |
| 2338 | const String &path = resource->get_path(); |
| 2339 | if (path.is_empty() || path.ends_with("::")) { |
| 2340 | String warning = TTR("The resource does not have a valid path because it has not been saved.\nPlease save the scene or resource that contains this resource and try again."); |
| 2341 | EditorToaster::get_singleton()->popup_str(warning, EditorToaster::SEVERITY_ERROR); |
| 2342 | return; |
| 2343 | } |
| 2344 | |
| 2345 | if (member_drop_modifier_pressed) { |
| 2346 | if (resource->is_built_in()) { |
| 2347 | String warning = TTR("Preloading internal resources is not supported."); |
| 2348 | EditorToaster::get_singleton()->popup_str(warning, EditorToaster::SEVERITY_ERROR); |
| 2349 | } else { |
| 2350 | text_to_drop = _get_dropped_resource_as_member(resource, is_empty_line, allow_uid); |
| 2351 | } |
| 2352 | } else if (export_drop_modifier_pressed) { |
| 2353 | Vector<ObjectID> obj_ids = _get_objects_for_export_assignment(); |
| 2354 | text_to_drop = _get_dropped_resource_as_exported_member(resource, obj_ids); |
| 2355 | |
| 2356 | } else { |
| 2357 | text_to_drop = _quote_drop_data(path); |
| 2358 | } |
| 2359 | |
| 2360 | if (is_empty_line) { |
| 2361 | text_to_drop += "\n"; |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | if (type == "files" || type == "files_and_dirs") { |
| 2366 | const PackedStringArray files = d["files"]; |
| 2367 | PackedStringArray parts; |
| 2368 | |
| 2369 | for (const String &path : files) { |
| 2370 | if ((member_drop_modifier_pressed || export_drop_modifier_pressed) && ResourceLoader::exists(path)) { |
| 2371 | Ref<Resource> resource = ResourceLoader::load(path); |
| 2372 | if (resource.is_null()) { |
| 2373 | // Resource exists, but failed to load. We need only path and name, so we can use a dummy Resource instead. |
| 2374 | resource.instantiate(); |
| 2375 | resource->set_path_cache(path); |
| 2376 | } |
| 2377 | |
| 2378 | if (member_drop_modifier_pressed) { |
| 2379 | parts.append(_get_dropped_resource_as_member(resource, is_empty_line, allow_uid)); |
| 2380 | } else if (export_drop_modifier_pressed) { |
| 2381 | Vector<ObjectID> obj_ids = _get_objects_for_export_assignment(); |
| 2382 | parts.append(_get_dropped_resource_as_exported_member(resource, obj_ids)); |
| 2383 | } |
| 2384 | } else { |
| 2385 | parts.append(_quote_drop_data(path)); |
| 2386 | } |
| 2387 | } |
| 2388 | String join_string; |
| 2389 | if (is_empty_line) { |
| 2390 | int indent_level = te->get_indent_level(drop_at_line); |
| 2391 | if (te->is_indent_using_spaces()) { |
| 2392 | join_string = "\n" + String(" ").repeat(indent_level); |
| 2393 | } else { |
| 2394 | join_string = "\n" + String("\t").repeat(indent_level / te->get_tab_size()); |
| 2395 | } |
| 2396 | } else { |
| 2397 | join_string = ", "; |
| 2398 | } |
| 2399 | text_to_drop = join_string.join(parts); |
| 2400 | if (is_empty_line) { |
| 2401 | text_to_drop += join_string; |
| 2402 | } |
| 2403 | } |
| 2404 | |
| 2405 | if (type == "nodes") { |
| 2406 | if (!scene_root) { |
| 2407 | EditorNode::get_singleton()->show_warning(TTR("Can't drop nodes without an open scene.")); |
| 2408 | return; |
| 2409 | } |
| 2410 | |
| 2411 | if (!ClassDB::is_parent_class(script->get_instance_base_type(), "Node")) { |
| 2412 | EditorToaster::get_singleton()->popup_str(vformat(TTR("Can't drop nodes because script '%s' does not inherit Node."), get_name()), EditorToaster::SEVERITY_WARNING); |
| 2413 | return; |
| 2414 | } |
| 2415 | |
| 2416 | Node *sn = _find_script_node(scene_root, script); |
| 2417 | if (!sn) { |
| 2418 | sn = scene_root; |
| 2419 | } |
| 2420 | |
| 2421 | Array nodes = d["nodes"]; |
| 2422 | |
| 2423 | if (member_drop_modifier_pressed) { |
| 2424 | const bool use_type = EDITOR_GET("text_editor/completion/add_type_hints"); |
| 2425 | add_new_line = !is_empty_line && drop_at_column != 0; |
| 2426 | |
| 2427 | for (int i = 0; i < nodes.size(); i++) { |
| 2428 | NodePath np = nodes[i]; |
| 2429 | Node *node = get_node(np); |
| 2430 | if (!node) { |
| 2431 | continue; |
| 2432 | } |
| 2433 | |
| 2434 | bool is_unique = node->is_unique_name_in_owner() && (node->get_owner() == sn || node->get_owner() == sn->get_owner()); |
| 2435 | String path = is_unique ? String(node->get_name()) : String(sn->get_path_to(node)); |
| 2436 | for (const String &segment : path.split("/")) { |
| 2437 | if (!segment.is_valid_unicode_identifier()) { |
| 2438 | path = _quote_drop_data(path); |
| 2439 | break; |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | String variable_name = String(node->get_name()).to_snake_case().validate_unicode_identifier(); |
| 2444 | if (use_type) { |
| 2445 | StringName class_name = node->get_class_name(); |
| 2446 | Ref<Script> node_script = node->get_script(); |
| 2447 | if (node_script.is_valid()) { |
| 2448 | StringName global_node_script_name = node_script->get_global_name(); |
| 2449 | if (!global_node_script_name.is_empty()) { |
| 2450 | class_name = global_node_script_name; |
| 2451 | } |
| 2452 | } |
| 2453 | text_to_drop += vformat("@onready var %s: %s = %c%s", variable_name, class_name, is_unique ? '%' : '$', path); |
| 2454 | } else { |
| 2455 | text_to_drop += vformat("@onready var %s = %c%s", variable_name, is_unique ? '%' : '$', path); |
| 2456 | } |
| 2457 | if (i < nodes.size() - 1) { |
| 2458 | text_to_drop += "\n"; |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | if (is_empty_line || drop_at_column == 0) { |
| 2463 | text_to_drop += "\n"; |
| 2464 | } |
| 2465 | } else if (export_drop_modifier_pressed) { |
| 2466 | Vector<ObjectID> obj_ids = _get_objects_for_export_assignment(); |
| 2467 | |
| 2468 | for (int i = 0; i < nodes.size(); i++) { |
| 2469 | NodePath np = nodes[i]; |
| 2470 | Node *node = get_node(np); |
| 2471 | if (!node) { |
| 2472 | continue; |
| 2473 | } |
| 2474 | |
| 2475 | String variable_name = String(node->get_name()).to_snake_case().validate_unicode_identifier(); |
| 2476 | StringName class_name = node->get_class_name(); |
| 2477 | Ref<Script> node_script = node->get_script(); |
| 2478 | if (node_script.is_valid()) { |
| 2479 | StringName global_node_script_name = node_script->get_global_name(); |
| 2480 | if (!global_node_script_name.is_empty()) { |
| 2481 | class_name = global_node_script_name; |
| 2482 | } |
| 2483 | } |
| 2484 | |
| 2485 | text_to_drop += vformat("@export var %s: %s\n", variable_name, class_name); |
| 2486 | for (ObjectID obj_id : obj_ids) { |
| 2487 | pending_dragged_exports.push_back(DraggedExport{ obj_id, variable_name, node, class_name }); |
| 2488 | } |
| 2489 | } |
| 2490 | } else { |
| 2491 | for (int i = 0; i < nodes.size(); i++) { |
| 2492 | if (i > 0) { |
| 2493 | text_to_drop += ", "; |
| 2494 | } |
| 2495 | |
| 2496 | NodePath np = nodes[i]; |
| 2497 | Node *node = get_node(np); |
| 2498 | if (!node) { |
| 2499 | continue; |
| 2500 | } |
| 2501 | |
| 2502 | bool is_unique = node->is_unique_name_in_owner() && (node->get_owner() == sn || node->get_owner() == sn->get_owner()); |
| 2503 | String path = is_unique ? String(node->get_name()) : String(sn->get_path_to(node)); |
| 2504 | for (const String &segment : path.split("/")) { |
| 2505 | if (!segment.is_valid_ascii_identifier()) { |
| 2506 | path = _quote_drop_data(path); |
| 2507 | break; |
| 2508 | } |
| 2509 | } |
| 2510 | text_to_drop += (is_unique ? "%" : "$") + path; |
| 2511 | } |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | if (type == "obj_property") { |
| 2516 | bool add_literal = EDITOR_GET("text_editor/completion/add_node_path_literals"); |
| 2517 | text_to_drop = add_literal ? "^" : ""; |
| 2518 | // It is unclear whether properties may contain single or double quotes. |
| 2519 | // Assume here that double-quotes may not exist. We are escaping single-quotes if necessary. |
| 2520 | text_to_drop += _quote_drop_data(String(d["property"])); |
| 2521 | } |
| 2522 | |
| 2523 | if (text_to_drop.is_empty()) { |
| 2524 | return; |
| 2525 | } |
| 2526 | |
| 2527 | // Remove drag caret before any actions so it is not included in undo. |
| 2528 | te->remove_drag_caret(); |
| 2529 | te->begin_complex_operation(); |
| 2530 | if (selection_index >= 0) { |
| 2531 | te->delete_selection(selection_index); |
| 2532 | } |
| 2533 | te->remove_secondary_carets(); |
| 2534 | te->deselect(); |
| 2535 | te->set_caret_line(drop_at_line); |
| 2536 | if (add_new_line) { |
| 2537 | te->set_caret_column(te->get_line(drop_at_line).length()); |
| 2538 | text_to_drop = "\n" + text_to_drop; |
| 2539 | } else { |
| 2540 | te->set_caret_column(drop_at_column); |
| 2541 | } |
| 2542 | te->insert_text_at_caret(text_to_drop); |
| 2543 | te->end_complex_operation(); |
| 2544 | te->grab_focus(); |
| 2545 | } |
| 2546 | |
| 2547 | Vector<ObjectID> ScriptTextEditor::_get_objects_for_export_assignment() const { |
| 2548 | Vector<ObjectID> objects; |
| 2549 | Node *scene_root = get_tree()->get_edited_scene_root(); |
| 2550 | bool assign_export_variables = scene_root && ClassDB::is_parent_class(script->get_instance_base_type(), "Node"); |
| 2551 | |
| 2552 | if (!assign_export_variables) { |
| 2553 | return objects; |
| 2554 | } |
| 2555 | |
| 2556 | EditorInspector *inspector = EditorInterface::get_singleton()->get_inspector(); |
| 2557 | if (inspector) { |
| 2558 | Object *edited_object = inspector->get_edited_object(); |
| 2559 | Node *node_edit = Object::cast_to<Node>(edited_object); |
| 2560 | MultiNodeEdit *multi_node_edit = Object::cast_to<MultiNodeEdit>(edited_object); |
| 2561 | |
| 2562 | if (node_edit != nullptr) { |
| 2563 | if (node_edit->get_script() == script) { |
| 2564 | objects.push_back(node_edit->get_instance_id()); |
| 2565 | } |
| 2566 | } else if (multi_node_edit != nullptr) { |
| 2567 | Node *es = EditorNode::get_singleton()->get_edited_scene(); |
| 2568 | for (int i = 0; i < multi_node_edit->get_node_count(); i++) { |
| 2569 | NodePath np = multi_node_edit->get_node(i); |
| 2570 | Node *node = es->get_node(np); |
| 2571 | if (node->get_script() == script) { |
| 2572 | objects.push_back(node->get_instance_id()); |
| 2573 | } |
| 2574 | } |
| 2575 | } |
| 2576 | } |
| 2577 | |
| 2578 | // In case there is no current editor selection/editor selection does not contain this script, |
| 2579 | // it often still makes sense to try to assign the export variable, |
| 2580 | // so we default to the first node with the script we find in the scene. |
| 2581 | if (objects.is_empty()) { |
| 2582 | Node *sn = _find_script_node(scene_root, script); |
| 2583 | if (sn) { |
| 2584 | objects.push_back(sn->get_instance_id()); |
| 2585 | } |
| 2586 | } |
| 2587 | |
| 2588 | return objects; |
| 2589 | } |
| 2590 | |
| 2591 | void ScriptTextEditor::_assign_dragged_export_variables() { |
| 2592 | ERR_FAIL_COND(pending_dragged_exports.is_empty()); |
| 2593 | |
| 2594 | bool export_variable_set = false; |
| 2595 | |
| 2596 | for (int i = pending_dragged_exports.size() - 1; i >= 0; i--) { |
| 2597 | const DraggedExport &dragged_export = pending_dragged_exports[i]; |
| 2598 | Object *obj = ObjectDB::get_instance(dragged_export.obj_id); |
| 2599 | if (!obj) { |
| 2600 | WARN_PRINT("Object not found, can't assign export variable."); |
| 2601 | pending_dragged_exports.remove_at(i); |
| 2602 | continue; |
| 2603 | } |
| 2604 | |
| 2605 | ScriptInstance *si = obj->get_script_instance(); |
| 2606 | if (!si) { |
| 2607 | WARN_PRINT("Script on " + obj->to_string() + " does not exist anymore, can't assign export variable."); |
| 2608 | pending_dragged_exports.remove_at(i); |
| 2609 | continue; |
| 2610 | } |
| 2611 | |
| 2612 | bool script_has_errors = false; |
| 2613 | String scr_path = si->get_script()->get_path(); |
| 2614 | |
| 2615 | for (const ScriptLanguage::ScriptError &error : errors) { |
| 2616 | if (error.path == scr_path) { |
| 2617 | script_has_errors = true; |
| 2618 | break; |
| 2619 | } |
| 2620 | } |
| 2621 | |
| 2622 | if (!script_has_errors) { |
| 2623 | bool success = false; |
| 2624 | List<PropertyInfo> properties; |
| 2625 | si->get_property_list(&properties); |
| 2626 | for (const PropertyInfo &pi : properties) { |
| 2627 | if (pi.name == dragged_export.variable_name && pi.hint_string == dragged_export.class_name) { |
| 2628 | success = si->set(dragged_export.variable_name, dragged_export.value); |
| 2629 | break; |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | if (success) { |
| 2634 | export_variable_set = true; |
| 2635 | } |
| 2636 | pending_dragged_exports.remove_at(i); |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | if (export_variable_set) { |
| 2641 | EditorInterface::get_singleton()->mark_scene_as_unsaved(); |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) { |
| 2646 | Ref<InputEventMouseButton> mb = ev; |
| 2647 | Ref<InputEventKey> k = ev; |
| 2648 | Point2 local_pos; |
| 2649 | bool create_menu = false; |
| 2650 | |
| 2651 | CodeEdit *tx = code_editor->get_text_editor(); |
| 2652 | if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) { |
| 2653 | local_pos = mb->get_global_position() - tx->get_global_position(); |
| 2654 | create_menu = true; |
| 2655 | } else if (k.is_valid() && k->is_action("ui_menu", true)) { |
| 2656 | tx->adjust_viewport_to_caret(0); |
| 2657 | local_pos = tx->get_caret_draw_pos(0); |
| 2658 | create_menu = true; |
| 2659 | } |
| 2660 | |
| 2661 | if (create_menu) { |
| 2662 | tx->apply_ime(); |
| 2663 | |
| 2664 | Point2i pos = tx->get_line_column_at_pos(local_pos); |
| 2665 | int mouse_line = pos.y; |
| 2666 | int mouse_column = pos.x; |
| 2667 | |
| 2668 | tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click")); |
| 2669 | int selection_clicked = -1; |
| 2670 | if (tx->is_move_caret_on_right_click_enabled()) { |
| 2671 | selection_clicked = tx->get_selection_at_line_column(mouse_line, mouse_column, true); |
| 2672 | if (selection_clicked < 0) { |
| 2673 | tx->deselect(); |
| 2674 | tx->remove_secondary_carets(); |
| 2675 | selection_clicked = 0; |
| 2676 | tx->set_caret_line(mouse_line, false, false, -1); |
| 2677 | tx->set_caret_column(mouse_column); |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | String word_at_pos = tx->get_lookup_word(mouse_line, mouse_column); |
| 2682 | if (word_at_pos.is_empty()) { |
| 2683 | word_at_pos = tx->get_word_under_caret(selection_clicked); |
| 2684 | } |
| 2685 | if (word_at_pos.is_empty()) { |
| 2686 | word_at_pos = tx->get_selected_text(selection_clicked); |
| 2687 | } |
| 2688 | |
| 2689 | bool has_color = (word_at_pos == "Color"); |
| 2690 | bool foldable = tx->can_fold_line(mouse_line) || tx->is_line_folded(mouse_line); |
| 2691 | bool open_docs = false; |
| 2692 | bool goto_definition = false; |
| 2693 | |
| 2694 | if (ScriptServer::is_global_class(word_at_pos) || word_at_pos.is_resource_file()) { |
| 2695 | open_docs = true; |
| 2696 | } else { |
| 2697 | Node *base = get_tree()->get_edited_scene_root(); |
| 2698 | if (base) { |
| 2699 | base = _find_node_for_script(base, base, script); |
| 2700 | } |
| 2701 | ScriptLanguage::LookupResult result; |
| 2702 | if (script->get_language()->lookup_code(tx->get_text_for_symbol_lookup(), word_at_pos, script->get_path(), base, result) == OK) { |
| 2703 | open_docs = true; |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | if (has_color) { |
| 2708 | String line = tx->get_line(mouse_line); |
| 2709 | color_position.x = mouse_line; |
| 2710 | |
| 2711 | int begin = -1; |
| 2712 | int end = -1; |
| 2713 | enum EXPRESSION_PATTERNS { |
| 2714 | NOT_PARSED, |
| 2715 | RGBA_PARAMETER, // Color(float,float,float) or Color(float,float,float,float) |
| 2716 | COLOR_NAME, // Color.COLOR_NAME |
| 2717 | } expression_pattern = NOT_PARSED; |
| 2718 | |
| 2719 | for (int i = mouse_column; i < line.length(); i++) { |
| 2720 | if (line[i] == '(') { |
| 2721 | if (expression_pattern == NOT_PARSED) { |
| 2722 | begin = i; |
| 2723 | expression_pattern = RGBA_PARAMETER; |
| 2724 | } else { |
| 2725 | // Method call or '(' appearing twice. |
| 2726 | expression_pattern = NOT_PARSED; |
| 2727 | |
| 2728 | break; |
| 2729 | } |
| 2730 | } else if (expression_pattern == RGBA_PARAMETER && line[i] == ')' && end < 0) { |
| 2731 | end = i + 1; |
| 2732 | |
| 2733 | break; |
| 2734 | } else if (expression_pattern == NOT_PARSED && line[i] == '.') { |
| 2735 | begin = i; |
| 2736 | expression_pattern = COLOR_NAME; |
| 2737 | } else if (expression_pattern == COLOR_NAME && end < 0 && (line[i] == ' ' || line[i] == '\t')) { |
| 2738 | // Including '.' and spaces. |
| 2739 | continue; |
| 2740 | } else if (expression_pattern == COLOR_NAME && !(line[i] == '_' || ('A' <= line[i] && line[i] <= 'Z'))) { |
| 2741 | end = i; |
| 2742 | |
| 2743 | break; |
| 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | switch (expression_pattern) { |
| 2748 | case RGBA_PARAMETER: { |
| 2749 | color_args = line.substr(begin, end - begin); |
| 2750 | String stripped = color_args.remove_chars(" \t()"); |
| 2751 | PackedFloat64Array color = stripped.split_floats(","); |
| 2752 | if (color.size() > 2) { |
| 2753 | float alpha = color.size() > 3 ? color[3] : 1.0f; |
| 2754 | color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha)); |
| 2755 | } |
| 2756 | } break; |
| 2757 | case COLOR_NAME: { |
| 2758 | if (end < 0) { |
| 2759 | end = line.length(); |
| 2760 | } |
| 2761 | color_args = line.substr(begin, end - begin); |
| 2762 | const String color_name = color_args.remove_chars(" \t."); |
| 2763 | const int color_index = Color::find_named_color(color_name); |
| 2764 | if (0 <= color_index) { |
| 2765 | const Color color_constant = Color::get_named_color(color_index); |
| 2766 | color_picker->set_pick_color(color_constant); |
| 2767 | } else { |
| 2768 | has_color = false; |
| 2769 | } |
| 2770 | } break; |
| 2771 | default: |
| 2772 | has_color = false; |
| 2773 | break; |
| 2774 | } |
| 2775 | if (has_color) { |
| 2776 | color_panel->set_position(get_screen_position() + local_pos); |
| 2777 | color_position.y = begin; |
| 2778 | color_position.z = end; |
| 2779 | } |
| 2780 | } |
| 2781 | _make_context_menu(tx->has_selection(), has_color, foldable, open_docs, goto_definition, local_pos); |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | void ScriptTextEditor::_color_changed(const Color &p_color) { |
| 2786 | String new_args; |
| 2787 | const int decimals = 3; |
| 2788 | if (p_color.a == 1.0f) { |
| 2789 | new_args = String("(" + String::num(p_color.r, decimals) + ", " + String::num(p_color.g, decimals) + ", " + String::num(p_color.b, decimals) + ")"); |
| 2790 | } else { |
| 2791 | new_args = String("(" + String::num(p_color.r, decimals) + ", " + String::num(p_color.g, decimals) + ", " + String::num(p_color.b, decimals) + ", " + String::num(p_color.a, decimals) + ")"); |
| 2792 | } |
| 2793 | |
| 2794 | String line = code_editor->get_text_editor()->get_line(color_position.x); |
| 2795 | String line_with_replaced_args = line.substr(0, color_position.y) + line.substr(color_position.y, color_position.z - color_position.y).replace(color_args, new_args) + line.substr(color_position.z); |
| 2796 | |
| 2797 | color_args = new_args; |
| 2798 | code_editor->get_text_editor()->begin_complex_operation(); |
| 2799 | code_editor->get_text_editor()->set_line(color_position.x, line_with_replaced_args); |
| 2800 | code_editor->get_text_editor()->end_complex_operation(); |
| 2801 | } |
| 2802 | |
| 2803 | void ScriptTextEditor::_prepare_edit_menu() { |
| 2804 | const CodeEdit *tx = code_editor->get_text_editor(); |
| 2805 | PopupMenu *popup = edit_menu->get_popup(); |
| 2806 | popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo()); |
| 2807 | popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo()); |
| 2808 | } |
| 2809 | |
| 2810 | 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) { |
| 2811 | context_menu->clear(); |
| 2812 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) { |
| 2813 | context_menu->add_item(TTRC("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL); |
| 2814 | context_menu->add_separator(); |
| 2815 | } |
| 2816 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO); |
| 2817 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO); |
| 2818 | |
| 2819 | context_menu->add_separator(); |
| 2820 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT); |
| 2821 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY); |
| 2822 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE); |
| 2823 | |
| 2824 | context_menu->add_separator(); |
| 2825 | context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL); |
| 2826 | |
| 2827 | context_menu->add_separator(); |
| 2828 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT); |
| 2829 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT); |
| 2830 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT); |
| 2831 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE); |
| 2832 | |
| 2833 | if (p_selection) { |
| 2834 | context_menu->add_separator(); |
| 2835 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE); |
| 2836 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE); |
| 2837 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE); |
| 2838 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/create_code_region"), EDIT_CREATE_CODE_REGION); |
| 2839 | } |
| 2840 | if (p_foldable) { |
| 2841 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE); |
| 2842 | } |
| 2843 | |
| 2844 | if (p_color || p_open_docs || p_goto_definition) { |
| 2845 | context_menu->add_separator(); |
| 2846 | if (p_open_docs) { |
| 2847 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_symbol"), LOOKUP_SYMBOL); |
| 2848 | } |
| 2849 | if (p_color) { |
| 2850 | context_menu->add_item(TTRC("Pick Color"), EDIT_PICK_COLOR); |
| 2851 | } |
| 2852 | } |
| 2853 | |
| 2854 | const PackedStringArray paths = { String(code_editor->get_text_editor()->get_path()) }; |
| 2855 | EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(context_menu, EditorContextMenuPlugin::CONTEXT_SLOT_SCRIPT_EDITOR_CODE, paths); |
| 2856 | |
| 2857 | const CodeEdit *tx = code_editor->get_text_editor(); |
| 2858 | context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo()); |
| 2859 | context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo()); |
| 2860 | |
| 2861 | context_menu->set_position(get_screen_position() + p_pos); |
| 2862 | context_menu->reset_size(); |
| 2863 | context_menu->popup(); |
| 2864 | } |
| 2865 | |
| 2866 | void ScriptTextEditor::_enable_code_editor() { |
| 2867 | ERR_FAIL_COND(code_editor->get_parent()); |
| 2868 | |
| 2869 | VSplitContainer *editor_box = memnew(VSplitContainer); |
| 2870 | add_child(editor_box); |
| 2871 | editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 2872 | editor_box->set_v_size_flags(SIZE_EXPAND_FILL); |
| 2873 | |
| 2874 | editor_box->add_child(code_editor); |
| 2875 | code_editor->connect("show_errors_panel", callable_mp(this, &ScriptTextEditor::_show_errors_panel)); |
| 2876 | code_editor->connect("show_warnings_panel", callable_mp(this, &ScriptTextEditor::_show_warnings_panel)); |
| 2877 | code_editor->connect("validate_script", callable_mp(this, &ScriptTextEditor::_validate_script)); |
| 2878 | code_editor->connect("load_theme_settings", callable_mp(this, &ScriptTextEditor::_load_theme_settings)); |
| 2879 | code_editor->get_text_editor()->connect("symbol_lookup", callable_mp(this, &ScriptTextEditor::_lookup_symbol)); |
| 2880 | code_editor->get_text_editor()->connect("symbol_hovered", callable_mp(this, &ScriptTextEditor::_show_symbol_tooltip)); |
| 2881 | code_editor->get_text_editor()->connect("symbol_validate", callable_mp(this, &ScriptTextEditor::_validate_symbol)); |
| 2882 | code_editor->get_text_editor()->connect("gutter_added", callable_mp(this, &ScriptTextEditor::_update_gutter_indexes)); |
| 2883 | code_editor->get_text_editor()->connect("gutter_removed", callable_mp(this, &ScriptTextEditor::_update_gutter_indexes)); |
| 2884 | code_editor->get_text_editor()->connect("gutter_clicked", callable_mp(this, &ScriptTextEditor::_gutter_clicked)); |
| 2885 | code_editor->get_text_editor()->connect("_fold_line_updated", callable_mp(this, &ScriptTextEditor::_update_background_color)); |
| 2886 | code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &ScriptTextEditor::_text_edit_gui_input)); |
| 2887 | code_editor->show_toggle_files_button(); |
| 2888 | _update_gutter_indexes(); |
| 2889 | |
| 2890 | editor_box->add_child(warnings_panel); |
| 2891 | warnings_panel->connect("meta_clicked", callable_mp(this, &ScriptTextEditor::_warning_clicked)); |
| 2892 | |
| 2893 | editor_box->add_child(errors_panel); |
| 2894 | errors_panel->connect("meta_clicked", callable_mp(this, &ScriptTextEditor::_error_clicked)); |
| 2895 | |
| 2896 | add_child(context_menu); |
| 2897 | context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option)); |
| 2898 | |
| 2899 | add_child(color_panel); |
| 2900 | |
| 2901 | color_picker = memnew(ColorPicker); |
| 2902 | color_picker->set_deferred_mode(true); |
| 2903 | color_picker->connect("color_changed", callable_mp(this, &ScriptTextEditor::_color_changed)); |
| 2904 | color_panel->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(color_picker)); |
| 2905 | |
| 2906 | color_panel->add_child(color_picker); |
| 2907 | |
| 2908 | quick_open = memnew(ScriptEditorQuickOpen); |
| 2909 | quick_open->set_title(TTRC("Go to Function")); |
| 2910 | quick_open->connect("goto_line", callable_mp(this, &ScriptTextEditor::_goto_line)); |
| 2911 | add_child(quick_open); |
| 2912 | |
| 2913 | goto_line_popup = memnew(GotoLinePopup); |
| 2914 | add_child(goto_line_popup); |
| 2915 | |
| 2916 | add_child(connection_info_dialog); |
| 2917 | |
| 2918 | edit_hb->add_child(edit_menu); |
| 2919 | edit_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_prepare_edit_menu)); |
| 2920 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO); |
| 2921 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO); |
| 2922 | edit_menu->get_popup()->add_separator(); |
| 2923 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT); |
| 2924 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY); |
| 2925 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE); |
| 2926 | edit_menu->get_popup()->add_separator(); |
| 2927 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL); |
| 2928 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION); |
| 2929 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES); |
| 2930 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE); |
| 2931 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP); |
| 2932 | edit_menu->get_popup()->add_separator(); |
| 2933 | { |
| 2934 | PopupMenu *sub_menu = memnew(PopupMenu); |
| 2935 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP); |
| 2936 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN); |
| 2937 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT); |
| 2938 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT); |
| 2939 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE); |
| 2940 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT); |
| 2941 | sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option)); |
| 2942 | edit_menu->get_popup()->add_submenu_node_item(TTRC("Line"), sub_menu); |
| 2943 | } |
| 2944 | { |
| 2945 | PopupMenu *sub_menu = memnew(PopupMenu); |
| 2946 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE); |
| 2947 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES); |
| 2948 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES); |
| 2949 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/create_code_region"), EDIT_CREATE_CODE_REGION); |
| 2950 | sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option)); |
| 2951 | edit_menu->get_popup()->add_submenu_node_item(TTRC("Folding"), sub_menu); |
| 2952 | } |
| 2953 | edit_menu->get_popup()->add_separator(); |
| 2954 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE); |
| 2955 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE); |
| 2956 | edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_final_newlines"), EDIT_TRIM_FINAL_NEWLINES); |
| 2957 | { |
| 2958 | PopupMenu *sub_menu = memnew(PopupMenu); |
| 2959 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES); |
| 2960 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS); |
| 2961 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT); |
| 2962 | sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option)); |
| 2963 | edit_menu->get_popup()->add_submenu_node_item(TTRC("Indentation"), sub_menu); |
| 2964 | } |
| 2965 | edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option)); |
| 2966 | edit_menu->get_popup()->add_separator(); |
| 2967 | { |
| 2968 | PopupMenu *sub_menu = memnew(PopupMenu); |
| 2969 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE); |
| 2970 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE); |
| 2971 | sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/capitalize"), EDIT_CAPITALIZE); |
| 2972 | sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option)); |
| 2973 | edit_menu->get_popup()->add_submenu_node_item(TTRC("Convert Case"), sub_menu); |
| 2974 | } |
| 2975 | edit_menu->get_popup()->add_submenu_node_item(TTRC("Syntax Highlighter"), highlighter_menu); |
| 2976 | highlighter_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_change_syntax_highlighter)); |
| 2977 | |
| 2978 | edit_hb->add_child(search_menu); |
| 2979 | search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND); |
| 2980 | search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT); |
| 2981 | search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV); |
| 2982 | search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE); |
| 2983 | search_menu->get_popup()->add_separator(); |
| 2984 | search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("editor/find_in_files"), SEARCH_IN_FILES); |
| 2985 | search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES); |
| 2986 | search_menu->get_popup()->add_separator(); |
| 2987 | search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL); |
| 2988 | search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option)); |
| 2989 | |
| 2990 | _load_theme_settings(); |
| 2991 | |
| 2992 | edit_hb->add_child(goto_menu); |
| 2993 | goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION); |
| 2994 | goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE); |
| 2995 | goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_symbol"), LOOKUP_SYMBOL); |
| 2996 | goto_menu->get_popup()->add_separator(); |
| 2997 | |
| 2998 | goto_menu->get_popup()->add_submenu_node_item(TTRC("Bookmarks"), bookmarks_menu); |
| 2999 | _update_bookmark_list(); |
| 3000 | bookmarks_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_update_bookmark_list)); |
| 3001 | bookmarks_menu->connect("index_pressed", callable_mp(this, &ScriptTextEditor::_bookmark_item_pressed)); |
| 3002 | |
| 3003 | goto_menu->get_popup()->add_submenu_node_item(TTRC("Breakpoints"), breakpoints_menu); |
| 3004 | _update_breakpoint_list(); |
| 3005 | breakpoints_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_update_breakpoint_list)); |
| 3006 | breakpoints_menu->connect("index_pressed", callable_mp(this, &ScriptTextEditor::_breakpoint_item_pressed)); |
| 3007 | |
| 3008 | goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option)); |
| 3009 | } |
| 3010 | |
| 3011 | ScriptTextEditor::ScriptTextEditor() { |
| 3012 | code_editor = memnew(CodeTextEditor); |
| 3013 | code_editor->set_toggle_list_control(ScriptEditor::get_singleton()->get_left_list_split()); |
| 3014 | code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 3015 | code_editor->set_code_complete_func(_code_complete_scripts, this); |
| 3016 | code_editor->set_v_size_flags(SIZE_EXPAND_FILL); |
| 3017 | |
| 3018 | code_editor->get_text_editor()->set_draw_breakpoints_gutter(true); |
| 3019 | code_editor->get_text_editor()->set_draw_executing_lines_gutter(true); |
| 3020 | code_editor->get_text_editor()->connect("breakpoint_toggled", callable_mp(this, &ScriptTextEditor::_breakpoint_toggled)); |
| 3021 | code_editor->get_text_editor()->connect("caret_changed", callable_mp(this, &ScriptTextEditor::_on_caret_moved)); |
| 3022 | code_editor->connect("navigation_preview_ended", callable_mp(this, &ScriptTextEditor::_on_caret_moved)); |
| 3023 | |
| 3024 | connection_gutter = 1; |
| 3025 | code_editor->get_text_editor()->add_gutter(connection_gutter); |
| 3026 | code_editor->get_text_editor()->set_gutter_name(connection_gutter, "connection_gutter"); |
| 3027 | code_editor->get_text_editor()->set_gutter_draw(connection_gutter, false); |
| 3028 | code_editor->get_text_editor()->set_gutter_overwritable(connection_gutter, true); |
| 3029 | code_editor->get_text_editor()->set_gutter_type(connection_gutter, TextEdit::GUTTER_TYPE_ICON); |
| 3030 | |
| 3031 | warnings_panel = memnew(RichTextLabel); |
| 3032 | warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE)); |
| 3033 | warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL); |
| 3034 | warnings_panel->set_meta_underline(true); |
| 3035 | warnings_panel->set_selection_enabled(true); |
| 3036 | warnings_panel->set_context_menu_enabled(true); |
| 3037 | warnings_panel->set_focus_mode(FOCUS_CLICK); |
| 3038 | warnings_panel->hide(); |
| 3039 | |
| 3040 | errors_panel = memnew(RichTextLabel); |
| 3041 | errors_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE)); |
| 3042 | errors_panel->set_h_size_flags(SIZE_EXPAND_FILL); |
| 3043 | errors_panel->set_meta_underline(true); |
| 3044 | errors_panel->set_selection_enabled(true); |
| 3045 | errors_panel->set_context_menu_enabled(true); |
| 3046 | errors_panel->set_focus_mode(FOCUS_CLICK); |
| 3047 | errors_panel->hide(); |
| 3048 | |
| 3049 | update_settings(); |
| 3050 | |
| 3051 | code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true); |
| 3052 | code_editor->get_text_editor()->set_symbol_tooltip_on_hover_enabled(true); |
| 3053 | code_editor->get_text_editor()->set_context_menu_enabled(false); |
| 3054 | |
| 3055 | context_menu = memnew(PopupMenu); |
| 3056 | |
| 3057 | color_panel = memnew(PopupPanel); |
| 3058 | |
| 3059 | edit_hb = memnew(HBoxContainer); |
| 3060 | |
| 3061 | edit_menu = memnew(MenuButton); |
| 3062 | edit_menu->set_flat(false); |
| 3063 | edit_menu->set_theme_type_variation("FlatMenuButton"); |
| 3064 | edit_menu->set_text(TTRC("Edit")); |
| 3065 | edit_menu->set_switch_on_hover(true); |
| 3066 | edit_menu->set_shortcut_context(this); |
| 3067 | |
| 3068 | highlighter_menu = memnew(PopupMenu); |
| 3069 | |
| 3070 | Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter; |
| 3071 | plain_highlighter.instantiate(); |
| 3072 | add_syntax_highlighter(plain_highlighter); |
| 3073 | |
| 3074 | Ref<EditorStandardSyntaxHighlighter> highlighter; |
| 3075 | highlighter.instantiate(); |
| 3076 | add_syntax_highlighter(highlighter); |
| 3077 | set_syntax_highlighter(highlighter); |
| 3078 | |
| 3079 | search_menu = memnew(MenuButton); |
| 3080 | search_menu->set_flat(false); |
| 3081 | search_menu->set_theme_type_variation("FlatMenuButton"); |
| 3082 | search_menu->set_text(TTRC("Search")); |
| 3083 | search_menu->set_switch_on_hover(true); |
| 3084 | search_menu->set_shortcut_context(this); |
| 3085 | |
| 3086 | goto_menu = memnew(MenuButton); |
| 3087 | goto_menu->set_flat(false); |
| 3088 | goto_menu->set_theme_type_variation("FlatMenuButton"); |
| 3089 | goto_menu->set_text(TTRC("Go To")); |
| 3090 | goto_menu->set_switch_on_hover(true); |
| 3091 | goto_menu->set_shortcut_context(this); |
| 3092 | |
| 3093 | bookmarks_menu = memnew(PopupMenu); |
| 3094 | breakpoints_menu = memnew(PopupMenu); |
| 3095 | |
| 3096 | inline_color_popup = memnew(PopupPanel); |
| 3097 | add_child(inline_color_popup); |
| 3098 | |
| 3099 | inline_color_picker = memnew(ColorPicker); |
| 3100 | inline_color_picker->set_mouse_filter(MOUSE_FILTER_STOP); |
| 3101 | inline_color_picker->set_deferred_mode(true); |
| 3102 | inline_color_picker->set_hex_visible(false); |
| 3103 | inline_color_picker->connect("color_changed", callable_mp(this, &ScriptTextEditor::_picker_color_changed)); |
| 3104 | inline_color_popup->add_child(inline_color_picker); |
| 3105 | |
| 3106 | inline_color_options = memnew(OptionButton); |
| 3107 | inline_color_options->set_h_size_flags(SIZE_FILL); |
| 3108 | inline_color_options->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); |
| 3109 | inline_color_options->set_fit_to_longest_item(false); |
| 3110 | inline_color_options->connect("item_selected", callable_mp(this, &ScriptTextEditor::_update_color_text).unbind(1)); |
| 3111 | inline_color_picker->get_slider_container()->add_sibling(inline_color_options); |
| 3112 | |
| 3113 | connection_info_dialog = memnew(ConnectionInfoDialog); |
| 3114 | |
| 3115 | SET_DRAG_FORWARDING_GCD(code_editor->get_text_editor(), ScriptTextEditor); |
| 3116 | } |
| 3117 | |
| 3118 | ScriptTextEditor::~ScriptTextEditor() { |
| 3119 | highlighters.clear(); |
| 3120 | |
| 3121 | if (!editor_enabled) { |
| 3122 | memdelete(code_editor); |
| 3123 | memdelete(warnings_panel); |
| 3124 | memdelete(errors_panel); |
| 3125 | memdelete(context_menu); |
| 3126 | memdelete(color_panel); |
| 3127 | memdelete(edit_hb); |
| 3128 | memdelete(edit_menu); |
| 3129 | memdelete(highlighter_menu); |
| 3130 | memdelete(search_menu); |
| 3131 | memdelete(goto_menu); |
| 3132 | memdelete(bookmarks_menu); |
| 3133 | memdelete(breakpoints_menu); |
| 3134 | memdelete(connection_info_dialog); |
| 3135 | } |
| 3136 | } |
| 3137 | |
| 3138 | static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource) { |
| 3139 | if (Object::cast_to<Script>(*p_resource)) { |
| 3140 | return memnew(ScriptTextEditor); |
| 3141 | } |
| 3142 | return nullptr; |
| 3143 | } |
| 3144 | |
| 3145 | void ScriptTextEditor::register_editor() { |
| 3146 | ED_SHORTCUT("script_text_editor/move_up", TTRC("Move Up"), KeyModifierMask::ALT | Key::UP); |
| 3147 | ED_SHORTCUT("script_text_editor/move_down", TTRC("Move Down"), KeyModifierMask::ALT | Key::DOWN); |
| 3148 | ED_SHORTCUT("script_text_editor/delete_line", TTRC("Delete Line"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::K); |
| 3149 | |
| 3150 | // Leave these at zero, same can be accomplished with tab/shift-tab, including selection. |
| 3151 | // The next/previous in history shortcut in this case makes a lot more sense. |
| 3152 | |
| 3153 | ED_SHORTCUT("script_text_editor/indent", TTRC("Indent"), Key::NONE); |
| 3154 | ED_SHORTCUT("script_text_editor/unindent", TTRC("Unindent"), KeyModifierMask::SHIFT | Key::TAB); |
| 3155 | ED_SHORTCUT_ARRAY("script_text_editor/toggle_comment", TTRC("Toggle Comment"), { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::K), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::SLASH), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_DIVIDE), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::NUMBERSIGN) }); |
| 3156 | ED_SHORTCUT("script_text_editor/toggle_fold_line", TTRC("Fold/Unfold Line"), KeyModifierMask::ALT | Key::F); |
| 3157 | ED_SHORTCUT_OVERRIDE("script_text_editor/toggle_fold_line", "macos", KeyModifierMask::CTRL | KeyModifierMask::META | Key::F); |
| 3158 | ED_SHORTCUT("script_text_editor/fold_all_lines", TTRC("Fold All Lines"), Key::NONE); |
| 3159 | ED_SHORTCUT("script_text_editor/create_code_region", TTRC("Create Code Region"), KeyModifierMask::ALT | Key::R); |
| 3160 | ED_SHORTCUT("script_text_editor/unfold_all_lines", TTRC("Unfold All Lines"), Key::NONE); |
| 3161 | ED_SHORTCUT("script_text_editor/duplicate_selection", TTRC("Duplicate Selection"), KeyModifierMask::SHIFT | KeyModifierMask::CTRL | Key::D); |
| 3162 | ED_SHORTCUT_OVERRIDE("script_text_editor/duplicate_selection", "macos", KeyModifierMask::SHIFT | KeyModifierMask::META | Key::C); |
| 3163 | ED_SHORTCUT("script_text_editor/duplicate_lines", TTRC("Duplicate Lines"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::DOWN); |
| 3164 | ED_SHORTCUT_OVERRIDE("script_text_editor/duplicate_lines", "macos", KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::DOWN); |
| 3165 | ED_SHORTCUT("script_text_editor/evaluate_selection", TTRC("Evaluate Selection"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::E); |
| 3166 | ED_SHORTCUT("script_text_editor/toggle_word_wrap", TTRC("Toggle Word Wrap"), KeyModifierMask::ALT | Key::Z); |
| 3167 | ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTRC("Trim Trailing Whitespace"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::T); |
| 3168 | ED_SHORTCUT("script_text_editor/trim_final_newlines", TTRC("Trim Final Newlines"), Key::NONE); |
| 3169 | ED_SHORTCUT("script_text_editor/convert_indent_to_spaces", TTRC("Convert Indent to Spaces"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::Y); |
| 3170 | ED_SHORTCUT("script_text_editor/convert_indent_to_tabs", TTRC("Convert Indent to Tabs"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::I); |
| 3171 | ED_SHORTCUT("script_text_editor/auto_indent", TTRC("Auto Indent"), KeyModifierMask::CMD_OR_CTRL | Key::I); |
| 3172 | |
| 3173 | ED_SHORTCUT_AND_COMMAND("script_text_editor/find", TTRC("Find..."), KeyModifierMask::CMD_OR_CTRL | Key::F); |
| 3174 | |
| 3175 | ED_SHORTCUT("script_text_editor/find_next", TTRC("Find Next"), Key::F3); |
| 3176 | ED_SHORTCUT_OVERRIDE("script_text_editor/find_next", "macos", KeyModifierMask::META | Key::G); |
| 3177 | |
| 3178 | ED_SHORTCUT("script_text_editor/find_previous", TTRC("Find Previous"), KeyModifierMask::SHIFT | Key::F3); |
| 3179 | ED_SHORTCUT_OVERRIDE("script_text_editor/find_previous", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::G); |
| 3180 | |
| 3181 | ED_SHORTCUT_AND_COMMAND("script_text_editor/replace", TTRC("Replace..."), KeyModifierMask::CTRL | Key::R); |
| 3182 | ED_SHORTCUT_OVERRIDE("script_text_editor/replace", "macos", KeyModifierMask::ALT | KeyModifierMask::META | Key::F); |
| 3183 | |
| 3184 | ED_SHORTCUT("script_text_editor/replace_in_files", TTRC("Replace in Files..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::R); |
| 3185 | |
| 3186 | ED_SHORTCUT("script_text_editor/contextual_help", TTRC("Contextual Help"), KeyModifierMask::ALT | Key::F1); |
| 3187 | ED_SHORTCUT_OVERRIDE("script_text_editor/contextual_help", "macos", KeyModifierMask::ALT | KeyModifierMask::SHIFT | Key::SPACE); |
| 3188 | |
| 3189 | ED_SHORTCUT("script_text_editor/toggle_bookmark", TTRC("Toggle Bookmark"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::B); |
| 3190 | |
| 3191 | ED_SHORTCUT("script_text_editor/goto_next_bookmark", TTRC("Go to Next Bookmark"), KeyModifierMask::CMD_OR_CTRL | Key::B); |
| 3192 | ED_SHORTCUT_OVERRIDE("script_text_editor/goto_next_bookmark", "macos", KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::B); |
| 3193 | |
| 3194 | ED_SHORTCUT("script_text_editor/goto_previous_bookmark", TTRC("Go to Previous Bookmark"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::B); |
| 3195 | ED_SHORTCUT("script_text_editor/remove_all_bookmarks", TTRC("Remove All Bookmarks"), Key::NONE); |
| 3196 | |
| 3197 | ED_SHORTCUT("script_text_editor/goto_function", TTRC("Go to Function..."), KeyModifierMask::ALT | KeyModifierMask::CTRL | Key::F); |
| 3198 | ED_SHORTCUT_OVERRIDE("script_text_editor/goto_function", "macos", KeyModifierMask::CTRL | KeyModifierMask::META | Key::J); |
| 3199 | |
| 3200 | ED_SHORTCUT("script_text_editor/goto_line", TTRC("Go to Line..."), KeyModifierMask::CMD_OR_CTRL | Key::G); |
| 3201 | ED_SHORTCUT_OVERRIDE("script_text_editor/goto_line", "macos", KeyModifierMask::CMD_OR_CTRL | Key::L); |
| 3202 | ED_SHORTCUT("script_text_editor/goto_symbol", TTRC("Lookup Symbol")); |
| 3203 | |
| 3204 | ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTRC("Toggle Breakpoint"), Key::F9); |
| 3205 | ED_SHORTCUT_OVERRIDE("script_text_editor/toggle_breakpoint", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::B); |
| 3206 | |
| 3207 | ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTRC("Remove All Breakpoints"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::F9); |
| 3208 | // Using Control for these shortcuts even on macOS because Command+Comma is taken for opening Editor Settings. |
| 3209 | ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTRC("Go to Next Breakpoint"), KeyModifierMask::CTRL | Key::PERIOD); |
| 3210 | ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTRC("Go to Previous Breakpoint"), KeyModifierMask::CTRL | Key::COMMA); |
| 3211 | |
| 3212 | ScriptEditor::register_create_script_editor_function(create_editor); |
| 3213 | } |
| 3214 | |
| 3215 | void ScriptTextEditor::validate() { |
| 3216 | code_editor->validate_script(); |
| 3217 | } |