| 1 | /**************************************************************************/ |
| 2 | /* gdscript_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 "gdscript.h" |
| 32 | |
| 33 | #include "gdscript_analyzer.h" |
| 34 | #include "gdscript_parser.h" |
| 35 | #include "gdscript_tokenizer.h" |
| 36 | #include "gdscript_utility_functions.h" |
| 37 | |
| 38 | #ifdef TOOLS_ENABLED |
| 39 | #include "editor/gdscript_docgen.h" |
| 40 | #include "editor/script_templates/templates.gen.h" |
| 41 | #endif |
| 42 | |
| 43 | #include "core/config/engine.h" |
| 44 | #include "core/core_constants.h" |
| 45 | #include "core/io/file_access.h" |
| 46 | #include "core/math/expression.h" |
| 47 | #include "core/variant/container_type_validate.h" |
| 48 | |
| 49 | #ifdef TOOLS_ENABLED |
| 50 | #include "core/config/project_settings.h" |
| 51 | #include "editor/editor_node.h" |
| 52 | #include "editor/editor_string_names.h" |
| 53 | #include "editor/file_system/editor_file_system.h" |
| 54 | #include "editor/settings/editor_settings.h" |
| 55 | #endif |
| 56 | |
| 57 | Vector<String> GDScriptLanguage::get_comment_delimiters() const { |
| 58 | static const Vector<String> delimiters = { "#" }; |
| 59 | return delimiters; |
| 60 | } |
| 61 | |
| 62 | Vector<String> GDScriptLanguage::get_doc_comment_delimiters() const { |
| 63 | static const Vector<String> delimiters = { "##" }; |
| 64 | return delimiters; |
| 65 | } |
| 66 | |
| 67 | Vector<String> GDScriptLanguage::get_string_delimiters() const { |
| 68 | static const Vector<String> delimiters = { |
| 69 | "\" \"", |
| 70 | "' '", |
| 71 | "\"\"\" \"\"\"", |
| 72 | "''' '''", |
| 73 | }; |
| 74 | // NOTE: StringName, NodePath and r-strings are not listed here. |
| 75 | return delimiters; |
| 76 | } |
| 77 | |
| 78 | bool GDScriptLanguage::is_using_templates() { |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | Ref<Script> GDScriptLanguage::make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const { |
| 83 | Ref<GDScript> scr; |
| 84 | scr.instantiate(); |
| 85 | |
| 86 | String processed_template = p_template; |
| 87 | |
| 88 | #ifdef TOOLS_ENABLED |
| 89 | const bool type_hints = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints"); |
| 90 | #else |
| 91 | const bool type_hints = true; |
| 92 | #endif |
| 93 | |
| 94 | if (!type_hints) { |
| 95 | processed_template = processed_template.replace(": int", "") |
| 96 | .replace(": Shader.Mode", "") |
| 97 | .replace(": VisualShader.Type", "") |
| 98 | .replace(": float", "") |
| 99 | .replace(": String", "") |
| 100 | .replace(": Array[String]", "") |
| 101 | .replace(": Node", "") |
| 102 | .replace(": CharFXTransform", "") |
| 103 | .replace(":=", "=") |
| 104 | .replace(" -> void", "") |
| 105 | .replace(" -> bool", "") |
| 106 | .replace(" -> int", "") |
| 107 | .replace(" -> PortType", "") |
| 108 | .replace(" -> String", "") |
| 109 | .replace(" -> Object", ""); |
| 110 | } |
| 111 | |
| 112 | processed_template = processed_template.replace("_BASE_", p_base_class_name) |
| 113 | .replace("_CLASS_SNAKE_CASE_", p_class_name.to_snake_case().validate_unicode_identifier()) |
| 114 | .replace("_CLASS_", p_class_name.to_pascal_case().validate_unicode_identifier()) |
| 115 | .replace("_TS_", _get_indentation()); |
| 116 | scr->set_source_code(processed_template); |
| 117 | |
| 118 | return scr; |
| 119 | } |
| 120 | |
| 121 | Vector<ScriptLanguage::ScriptTemplate> GDScriptLanguage::get_built_in_templates(const StringName &p_object) { |
| 122 | Vector<ScriptLanguage::ScriptTemplate> templates; |
| 123 | #ifdef TOOLS_ENABLED |
| 124 | for (int i = 0; i < TEMPLATES_ARRAY_SIZE; i++) { |
| 125 | if (TEMPLATES[i].inherit == p_object) { |
| 126 | templates.append(TEMPLATES[i]); |
| 127 | } |
| 128 | } |
| 129 | #endif |
| 130 | return templates; |
| 131 | } |
| 132 | |
| 133 | static void get_function_names_recursively(const GDScriptParser::ClassNode *p_class, const String &p_prefix, HashMap<int, String> &r_funcs) { |
| 134 | for (int i = 0; i < p_class->members.size(); i++) { |
| 135 | if (p_class->members[i].type == GDScriptParser::ClassNode::Member::FUNCTION) { |
| 136 | const GDScriptParser::FunctionNode *function = p_class->members[i].function; |
| 137 | r_funcs[function->start_line] = p_prefix.is_empty() ? String(function->identifier->name) : p_prefix + "." + String(function->identifier->name); |
| 138 | } else if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) { |
| 139 | String new_prefix = p_class->members[i].m_class->identifier->name; |
| 140 | get_function_names_recursively(p_class->members[i].m_class, p_prefix.is_empty() ? new_prefix : p_prefix + "." + new_prefix, r_funcs); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | bool GDScriptLanguage::validate(const String &p_script, const String &p_path, List<String> *r_functions, List<ScriptLanguage::ScriptError> *r_errors, List<ScriptLanguage::Warning> *r_warnings, HashSet<int> *r_safe_lines) const { |
| 146 | GDScriptParser parser; |
| 147 | GDScriptAnalyzer analyzer(&parser); |
| 148 | |
| 149 | Error err = parser.parse(p_script, p_path, false); |
| 150 | if (err == OK) { |
| 151 | err = analyzer.analyze(); |
| 152 | } |
| 153 | #ifdef DEBUG_ENABLED |
| 154 | if (r_warnings) { |
| 155 | for (const GDScriptWarning &E : parser.get_warnings()) { |
| 156 | const GDScriptWarning &warn = E; |
| 157 | ScriptLanguage::Warning w; |
| 158 | w.start_line = warn.start_line; |
| 159 | w.end_line = warn.end_line; |
| 160 | w.code = (int)warn.code; |
| 161 | w.string_code = GDScriptWarning::get_name_from_code(warn.code); |
| 162 | w.message = warn.get_message(); |
| 163 | r_warnings->push_back(w); |
| 164 | } |
| 165 | } |
| 166 | #endif |
| 167 | if (err) { |
| 168 | if (r_errors) { |
| 169 | for (const GDScriptParser::ParserError &pe : parser.get_errors()) { |
| 170 | ScriptLanguage::ScriptError e; |
| 171 | e.path = p_path; |
| 172 | e.line = pe.line; |
| 173 | e.column = pe.column; |
| 174 | e.message = pe.message; |
| 175 | r_errors->push_back(e); |
| 176 | } |
| 177 | |
| 178 | for (KeyValue<String, Ref<GDScriptParserRef>> E : parser.get_depended_parsers()) { |
| 179 | GDScriptParser *depended_parser = E.value->get_parser(); |
| 180 | for (const GDScriptParser::ParserError &pe : depended_parser->get_errors()) { |
| 181 | ScriptLanguage::ScriptError e; |
| 182 | e.path = E.key; |
| 183 | e.line = pe.line; |
| 184 | e.column = pe.column; |
| 185 | e.message = pe.message; |
| 186 | r_errors->push_back(e); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | return false; |
| 191 | } else if (r_functions) { |
| 192 | const GDScriptParser::ClassNode *cl = parser.get_tree(); |
| 193 | HashMap<int, String> funcs; |
| 194 | |
| 195 | get_function_names_recursively(cl, "", funcs); |
| 196 | |
| 197 | for (const KeyValue<int, String> &E : funcs) { |
| 198 | r_functions->push_back(E.value + ":" + itos(E.key)); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | #ifdef DEBUG_ENABLED |
| 203 | if (r_safe_lines) { |
| 204 | const HashSet<int> &unsafe_lines = parser.get_unsafe_lines(); |
| 205 | for (int i = 1; i <= parser.get_last_line_number(); i++) { |
| 206 | if (!unsafe_lines.has(i)) { |
| 207 | r_safe_lines->insert(i); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | #endif |
| 212 | |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | bool GDScriptLanguage::supports_builtin_mode() const { |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | bool GDScriptLanguage::supports_documentation() const { |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const { |
| 225 | GDScriptTokenizerText tokenizer; |
| 226 | tokenizer.set_source_code(p_code); |
| 227 | int indent = 0; |
| 228 | GDScriptTokenizer::Token current = tokenizer.scan(); |
| 229 | while (current.type != GDScriptTokenizer::Token::TK_EOF && current.type != GDScriptTokenizer::Token::ERROR) { |
| 230 | if (current.type == GDScriptTokenizer::Token::INDENT) { |
| 231 | indent++; |
| 232 | } else if (current.type == GDScriptTokenizer::Token::DEDENT) { |
| 233 | indent--; |
| 234 | } |
| 235 | if (indent == 0 && current.type == GDScriptTokenizer::Token::FUNC) { |
| 236 | current = tokenizer.scan(); |
| 237 | if (current.is_identifier()) { |
| 238 | String identifier = current.get_identifier(); |
| 239 | if (identifier == p_function) { |
| 240 | return current.start_line; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | current = tokenizer.scan(); |
| 245 | } |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | Script *GDScriptLanguage::create_script() const { |
| 250 | return memnew(GDScript); |
| 251 | } |
| 252 | |
| 253 | /* DEBUGGER FUNCTIONS */ |
| 254 | |
| 255 | thread_local int GDScriptLanguage::_debug_parse_err_line = -1; |
| 256 | thread_local String GDScriptLanguage::_debug_parse_err_file; |
| 257 | thread_local String GDScriptLanguage::_debug_error; |
| 258 | |
| 259 | bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) { |
| 260 | // break because of parse error |
| 261 | |
| 262 | if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) { |
| 263 | _debug_parse_err_line = p_line; |
| 264 | _debug_parse_err_file = p_file; |
| 265 | _debug_error = p_error; |
| 266 | EngineDebugger::get_script_debugger()->debug(this, false, true); |
| 267 | // Because this is thread local, clear the memory afterwards. |
| 268 | _debug_parse_err_file = String(); |
| 269 | _debug_error = String(); |
| 270 | return true; |
| 271 | } else { |
| 272 | return false; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) { |
| 277 | if (EngineDebugger::is_active()) { |
| 278 | _debug_parse_err_line = -1; |
| 279 | _debug_parse_err_file = ""; |
| 280 | _debug_error = p_error; |
| 281 | bool is_error_breakpoint = p_error != "Breakpoint"; |
| 282 | EngineDebugger::get_script_debugger()->debug(this, p_allow_continue, is_error_breakpoint); |
| 283 | // Because this is thread local, clear the memory afterwards. |
| 284 | _debug_parse_err_file = String(); |
| 285 | _debug_error = String(); |
| 286 | return true; |
| 287 | } else { |
| 288 | return false; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | String GDScriptLanguage::debug_get_error() const { |
| 293 | return _debug_error; |
| 294 | } |
| 295 | |
| 296 | int GDScriptLanguage::debug_get_stack_level_count() const { |
| 297 | if (_debug_parse_err_line >= 0) { |
| 298 | return 1; |
| 299 | } |
| 300 | |
| 301 | return _call_stack_size; |
| 302 | } |
| 303 | |
| 304 | int GDScriptLanguage::debug_get_stack_level_line(int p_level) const { |
| 305 | if (_debug_parse_err_line >= 0) { |
| 306 | return _debug_parse_err_line; |
| 307 | } |
| 308 | |
| 309 | ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, -1); |
| 310 | |
| 311 | return *(_get_stack_level(p_level)->line); |
| 312 | } |
| 313 | |
| 314 | String GDScriptLanguage::debug_get_stack_level_function(int p_level) const { |
| 315 | if (_debug_parse_err_line >= 0) { |
| 316 | return ""; |
| 317 | } |
| 318 | |
| 319 | ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, ""); |
| 320 | GDScriptFunction *func = _get_stack_level(p_level)->function; |
| 321 | return func ? func->get_name().operator String() : ""; |
| 322 | } |
| 323 | |
| 324 | String GDScriptLanguage::debug_get_stack_level_source(int p_level) const { |
| 325 | if (_debug_parse_err_line >= 0) { |
| 326 | return _debug_parse_err_file; |
| 327 | } |
| 328 | |
| 329 | ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, ""); |
| 330 | return _get_stack_level(p_level)->function->get_source(); |
| 331 | } |
| 332 | |
| 333 | void GDScriptLanguage::debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) { |
| 334 | if (_debug_parse_err_line >= 0) { |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | ERR_FAIL_INDEX(p_level, (int)_call_stack_size); |
| 339 | |
| 340 | CallLevel *cl = _get_stack_level(p_level); |
| 341 | GDScriptFunction *f = cl->function; |
| 342 | |
| 343 | List<Pair<StringName, int>> locals; |
| 344 | |
| 345 | f->debug_get_stack_member_state(*cl->line, &locals); |
| 346 | for (const Pair<StringName, int> &E : locals) { |
| 347 | p_locals->push_back(E.first); |
| 348 | p_values->push_back(cl->stack[E.second]); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void GDScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) { |
| 353 | if (_debug_parse_err_line >= 0) { |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | ERR_FAIL_INDEX(p_level, (int)_call_stack_size); |
| 358 | |
| 359 | CallLevel *cl = _get_stack_level(p_level); |
| 360 | GDScriptInstance *instance = cl->instance; |
| 361 | |
| 362 | if (!instance) { |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | Ref<GDScript> scr = instance->get_script(); |
| 367 | ERR_FAIL_COND(scr.is_null()); |
| 368 | |
| 369 | const HashMap<StringName, GDScript::MemberInfo> &mi = scr->debug_get_member_indices(); |
| 370 | |
| 371 | for (const KeyValue<StringName, GDScript::MemberInfo> &E : mi) { |
| 372 | p_members->push_back(E.key); |
| 373 | p_values->push_back(instance->debug_get_member_by_index(E.value.index)); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) { |
| 378 | if (_debug_parse_err_line >= 0) { |
| 379 | return nullptr; |
| 380 | } |
| 381 | |
| 382 | ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, nullptr); |
| 383 | |
| 384 | return _get_stack_level(p_level)->instance; |
| 385 | } |
| 386 | |
| 387 | void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) { |
| 388 | const HashMap<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map(); |
| 389 | const Variant *gl_array = GDScriptLanguage::get_singleton()->get_global_array(); |
| 390 | |
| 391 | List<Pair<String, Variant>> cinfo; |
| 392 | get_public_constants(&cinfo); |
| 393 | |
| 394 | for (const KeyValue<StringName, int> &E : name_idx) { |
| 395 | if (GDScriptAnalyzer::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) { |
| 396 | continue; |
| 397 | } |
| 398 | |
| 399 | bool is_script_constant = false; |
| 400 | for (List<Pair<String, Variant>>::Element *CE = cinfo.front(); CE; CE = CE->next()) { |
| 401 | if (CE->get().first == E.key) { |
| 402 | is_script_constant = true; |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | if (is_script_constant) { |
| 407 | continue; |
| 408 | } |
| 409 | |
| 410 | const Variant &var = gl_array[E.value]; |
| 411 | bool freed = false; |
| 412 | const Object *obj = var.get_validated_object_with_check(freed); |
| 413 | if (obj && !freed) { |
| 414 | if (Object::cast_to<GDScriptNativeClass>(obj)) { |
| 415 | continue; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | bool skip = false; |
| 420 | for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) { |
| 421 | if (E.key == CoreConstants::get_global_constant_name(i)) { |
| 422 | skip = true; |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | if (skip) { |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | p_globals->push_back(E.key); |
| 431 | p_values->push_back(var); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) { |
| 436 | List<String> names; |
| 437 | List<Variant> values; |
| 438 | debug_get_stack_level_locals(p_level, &names, &values, p_max_subitems, p_max_depth); |
| 439 | |
| 440 | Vector<String> name_vector; |
| 441 | for (const String &name : names) { |
| 442 | name_vector.push_back(name); |
| 443 | } |
| 444 | |
| 445 | Array value_array; |
| 446 | for (const Variant &value : values) { |
| 447 | value_array.push_back(value); |
| 448 | } |
| 449 | |
| 450 | Expression expression; |
| 451 | if (expression.parse(p_expression, name_vector) == OK) { |
| 452 | ScriptInstance *instance = debug_get_stack_level_instance(p_level); |
| 453 | if (instance) { |
| 454 | Variant return_val = expression.execute(value_array, instance->get_owner()); |
| 455 | return return_val.get_construct_string(); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return String(); |
| 460 | } |
| 461 | |
| 462 | void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const { |
| 463 | p_extensions->push_back("gd"); |
| 464 | } |
| 465 | |
| 466 | void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const { |
| 467 | List<StringName> functions; |
| 468 | GDScriptUtilityFunctions::get_function_list(&functions); |
| 469 | |
| 470 | for (const StringName &E : functions) { |
| 471 | p_functions->push_back(GDScriptUtilityFunctions::get_function_info(E)); |
| 472 | } |
| 473 | |
| 474 | // Not really "functions", but show in documentation. |
| 475 | { |
| 476 | MethodInfo mi; |
| 477 | mi.name = "preload"; |
| 478 | mi.arguments.push_back(PropertyInfo(Variant::STRING, "path")); |
| 479 | mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource"); |
| 480 | p_functions->push_back(mi); |
| 481 | } |
| 482 | { |
| 483 | MethodInfo mi; |
| 484 | mi.name = "assert"; |
| 485 | mi.return_val.type = Variant::NIL; |
| 486 | mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition")); |
| 487 | mi.arguments.push_back(PropertyInfo(Variant::STRING, "message")); |
| 488 | mi.default_arguments.push_back(String()); |
| 489 | p_functions->push_back(mi); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void GDScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const { |
| 494 | Pair<String, Variant> pi; |
| 495 | pi.first = "PI"; |
| 496 | pi.second = Math::PI; |
| 497 | p_constants->push_back(pi); |
| 498 | |
| 499 | Pair<String, Variant> tau; |
| 500 | tau.first = "TAU"; |
| 501 | tau.second = Math::TAU; |
| 502 | p_constants->push_back(tau); |
| 503 | |
| 504 | Pair<String, Variant> infinity; |
| 505 | infinity.first = "INF"; |
| 506 | infinity.second = Math::INF; |
| 507 | p_constants->push_back(infinity); |
| 508 | |
| 509 | Pair<String, Variant> nan; |
| 510 | nan.first = "NAN"; |
| 511 | nan.second = Math::NaN; |
| 512 | p_constants->push_back(nan); |
| 513 | } |
| 514 | |
| 515 | void GDScriptLanguage::get_public_annotations(List<MethodInfo> *p_annotations) const { |
| 516 | GDScriptParser parser; |
| 517 | List<MethodInfo> annotations; |
| 518 | parser.get_annotation_list(&annotations); |
| 519 | |
| 520 | for (const MethodInfo &E : annotations) { |
| 521 | p_annotations->push_back(E); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const { |
| 526 | #ifdef TOOLS_ENABLED |
| 527 | const bool type_hints = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints"); |
| 528 | #else |
| 529 | const bool type_hints = true; |
| 530 | #endif |
| 531 | |
| 532 | String result = "func " + p_name + "("; |
| 533 | if (p_args.size()) { |
| 534 | for (int i = 0; i < p_args.size(); i++) { |
| 535 | if (i > 0) { |
| 536 | result += ", "; |
| 537 | } |
| 538 | |
| 539 | const String name_unstripped = p_args[i].get_slicec(':', 0); |
| 540 | result += name_unstripped.strip_edges(); |
| 541 | |
| 542 | if (type_hints) { |
| 543 | const String type_stripped = p_args[i].substr(name_unstripped.length() + 1).strip_edges(); |
| 544 | if (!type_stripped.is_empty()) { |
| 545 | result += ": " + type_stripped; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | result += String(")") + (type_hints ? " -> void" : "") + ":\n" + |
| 551 | _get_indentation() + "pass # Replace with function body.\n"; |
| 552 | |
| 553 | return result; |
| 554 | } |
| 555 | |
| 556 | //////// COMPLETION ////////// |
| 557 | |
| 558 | #ifdef TOOLS_ENABLED |
| 559 | |
| 560 | #define COMPLETION_RECURSION_LIMIT 200 |
| 561 | |
| 562 | struct GDScriptCompletionIdentifier { |
| 563 | GDScriptParser::DataType type; |
| 564 | String enumeration; |
| 565 | Variant value; |
| 566 | const GDScriptParser::ExpressionNode *assigned_expression = nullptr; |
| 567 | }; |
| 568 | |
| 569 | // LOCATION METHODS |
| 570 | // These methods are used to populate the `CodeCompletionOption::location` integer. |
| 571 | // For these methods, the location is based on the depth in the inheritance chain that the property |
| 572 | // appears. For example, if you are completing code in a class that inherits Node2D, a property found on Node2D |
| 573 | // will have a "better" (lower) location "score" than a property that is found on CanvasItem. |
| 574 | |
| 575 | static int _get_property_location(const StringName &p_class, const StringName &p_property) { |
| 576 | if (!ClassDB::has_property(p_class, p_property)) { |
| 577 | return ScriptLanguage::LOCATION_OTHER; |
| 578 | } |
| 579 | |
| 580 | int depth = 0; |
| 581 | StringName class_test = p_class; |
| 582 | while (class_test && !ClassDB::has_property(class_test, p_property, true)) { |
| 583 | class_test = ClassDB::get_parent_class(class_test); |
| 584 | depth++; |
| 585 | } |
| 586 | |
| 587 | return depth | ScriptLanguage::LOCATION_PARENT_MASK; |
| 588 | } |
| 589 | |
| 590 | static int _get_property_location(Ref<Script> p_script, const StringName &p_property) { |
| 591 | int depth = 0; |
| 592 | Ref<Script> scr = p_script; |
| 593 | while (scr.is_valid()) { |
| 594 | if (scr->get_member_line(p_property) != -1) { |
| 595 | return depth | ScriptLanguage::LOCATION_PARENT_MASK; |
| 596 | } |
| 597 | depth++; |
| 598 | scr = scr->get_base_script(); |
| 599 | } |
| 600 | return depth + _get_property_location(p_script->get_instance_base_type(), p_property); |
| 601 | } |
| 602 | |
| 603 | static int _get_constant_location(const StringName &p_class, const StringName &p_constant) { |
| 604 | if (!ClassDB::has_integer_constant(p_class, p_constant)) { |
| 605 | return ScriptLanguage::LOCATION_OTHER; |
| 606 | } |
| 607 | |
| 608 | int depth = 0; |
| 609 | StringName class_test = p_class; |
| 610 | while (class_test && !ClassDB::has_integer_constant(class_test, p_constant, true)) { |
| 611 | class_test = ClassDB::get_parent_class(class_test); |
| 612 | depth++; |
| 613 | } |
| 614 | |
| 615 | return depth | ScriptLanguage::LOCATION_PARENT_MASK; |
| 616 | } |
| 617 | |
| 618 | static int _get_constant_location(Ref<Script> p_script, const StringName &p_constant) { |
| 619 | int depth = 0; |
| 620 | Ref<Script> scr = p_script; |
| 621 | while (scr.is_valid()) { |
| 622 | if (scr->get_member_line(p_constant) != -1) { |
| 623 | return depth | ScriptLanguage::LOCATION_PARENT_MASK; |
| 624 | } |
| 625 | depth++; |
| 626 | scr = scr->get_base_script(); |
| 627 | } |
| 628 | return depth + _get_constant_location(p_script->get_instance_base_type(), p_constant); |
| 629 | } |
| 630 | |
| 631 | static int _get_signal_location(const StringName &p_class, const StringName &p_signal) { |
| 632 | if (!ClassDB::has_signal(p_class, p_signal)) { |
| 633 | return ScriptLanguage::LOCATION_OTHER; |
| 634 | } |
| 635 | |
| 636 | int depth = 0; |
| 637 | StringName class_test = p_class; |
| 638 | while (class_test && !ClassDB::has_signal(class_test, p_signal, true)) { |
| 639 | class_test = ClassDB::get_parent_class(class_test); |
| 640 | depth++; |
| 641 | } |
| 642 | |
| 643 | return depth | ScriptLanguage::LOCATION_PARENT_MASK; |
| 644 | } |
| 645 | |
| 646 | static int _get_signal_location(Ref<Script> p_script, const StringName &p_signal) { |
| 647 | int depth = 0; |
| 648 | Ref<Script> scr = p_script; |
| 649 | while (scr.is_valid()) { |
| 650 | if (scr->get_member_line(p_signal) != -1) { |
| 651 | return depth | ScriptLanguage::LOCATION_PARENT_MASK; |
| 652 | } |
| 653 | depth++; |
| 654 | scr = scr->get_base_script(); |
| 655 | } |
| 656 | return depth + _get_signal_location(p_script->get_instance_base_type(), p_signal); |
| 657 | } |
| 658 | |
| 659 | static int _get_method_location(const StringName &p_class, const StringName &p_method) { |
| 660 | if (!ClassDB::has_method(p_class, p_method)) { |
| 661 | return ScriptLanguage::LOCATION_OTHER; |
| 662 | } |
| 663 | |
| 664 | int depth = 0; |
| 665 | StringName class_test = p_class; |
| 666 | while (class_test && !ClassDB::has_method(class_test, p_method, true)) { |
| 667 | class_test = ClassDB::get_parent_class(class_test); |
| 668 | depth++; |
| 669 | } |
| 670 | |
| 671 | return depth | ScriptLanguage::LOCATION_PARENT_MASK; |
| 672 | } |
| 673 | |
| 674 | static int _get_enum_constant_location(const StringName &p_class, const StringName &p_enum_constant) { |
| 675 | if (!ClassDB::get_integer_constant_enum(p_class, p_enum_constant)) { |
| 676 | return ScriptLanguage::LOCATION_OTHER; |
| 677 | } |
| 678 | |
| 679 | int depth = 0; |
| 680 | StringName class_test = p_class; |
| 681 | while (class_test && !ClassDB::get_integer_constant_enum(class_test, p_enum_constant, true)) { |
| 682 | class_test = ClassDB::get_parent_class(class_test); |
| 683 | depth++; |
| 684 | } |
| 685 | |
| 686 | return depth | ScriptLanguage::LOCATION_PARENT_MASK; |
| 687 | } |
| 688 | |
| 689 | static int _get_enum_location(const StringName &p_class, const StringName &p_enum) { |
| 690 | if (!ClassDB::has_enum(p_class, p_enum)) { |
| 691 | return ScriptLanguage::LOCATION_OTHER; |
| 692 | } |
| 693 | |
| 694 | int depth = 0; |
| 695 | StringName class_test = p_class; |
| 696 | while (class_test && !ClassDB::has_enum(class_test, p_enum, true)) { |
| 697 | class_test = ClassDB::get_parent_class(class_test); |
| 698 | depth++; |
| 699 | } |
| 700 | |
| 701 | return depth | ScriptLanguage::LOCATION_PARENT_MASK; |
| 702 | } |
| 703 | |
| 704 | // END LOCATION METHODS |
| 705 | |
| 706 | static String _trim_parent_class(const String &p_class, const String &p_base_class) { |
| 707 | if (p_base_class.is_empty()) { |
| 708 | return p_class; |
| 709 | } |
| 710 | Vector<String> names = p_class.split(".", false, 1); |
| 711 | if (names.size() == 2) { |
| 712 | const String &first = names[0]; |
| 713 | if (GDScriptAnalyzer::class_exists(p_base_class) && GDScriptAnalyzer::class_exists(first) && ClassDB::is_parent_class(p_base_class, first)) { |
| 714 | const String &rest = names[1]; |
| 715 | return rest; |
| 716 | } |
| 717 | } |
| 718 | return p_class; |
| 719 | } |
| 720 | |
| 721 | static String _get_visual_datatype(const PropertyInfo &p_info, bool p_is_arg, const String &p_base_class = "") { |
| 722 | String class_name = p_info.class_name; |
| 723 | bool is_enum = p_info.type == Variant::INT && p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM; |
| 724 | // PROPERTY_USAGE_CLASS_IS_BITFIELD: BitField[T] isn't supported (yet?), use plain int. |
| 725 | |
| 726 | if ((p_info.type == Variant::OBJECT || is_enum) && !class_name.is_empty()) { |
| 727 | if (is_enum && CoreConstants::is_global_enum(p_info.class_name)) { |
| 728 | return class_name; |
| 729 | } |
| 730 | return _trim_parent_class(class_name, p_base_class); |
| 731 | } else if (p_info.type == Variant::ARRAY && p_info.hint == PROPERTY_HINT_ARRAY_TYPE && !p_info.hint_string.is_empty()) { |
| 732 | return "Array[" + _trim_parent_class(p_info.hint_string, p_base_class) + "]"; |
| 733 | } else if (p_info.type == Variant::DICTIONARY && p_info.hint == PROPERTY_HINT_DICTIONARY_TYPE && !p_info.hint_string.is_empty()) { |
| 734 | const String key = p_info.hint_string.get_slicec(';', 0); |
| 735 | const String value = p_info.hint_string.get_slicec(';', 1); |
| 736 | return "Dictionary[" + _trim_parent_class(key, p_base_class) + ", " + _trim_parent_class(value, p_base_class) + "]"; |
| 737 | } else if (p_info.type == Variant::NIL) { |
| 738 | if (p_is_arg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) { |
| 739 | return "Variant"; |
| 740 | } else { |
| 741 | return "void"; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | return Variant::get_type_name(p_info.type); |
| 746 | } |
| 747 | |
| 748 | static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx, bool p_is_annotation = false) { |
| 749 | String arghint; |
| 750 | if (!p_is_annotation) { |
| 751 | arghint += _get_visual_datatype(p_info.return_val, false) + " "; |
| 752 | } |
| 753 | arghint += p_info.name + "("; |
| 754 | |
| 755 | int def_args = p_info.arguments.size() - p_info.default_arguments.size(); |
| 756 | int i = 0; |
| 757 | for (const PropertyInfo &E : p_info.arguments) { |
| 758 | if (i > 0) { |
| 759 | arghint += ", "; |
| 760 | } |
| 761 | |
| 762 | if (i == p_arg_idx) { |
| 763 | arghint += String::chr(0xFFFF); |
| 764 | } |
| 765 | arghint += E.name + ": " + _get_visual_datatype(E, true); |
| 766 | |
| 767 | if (i - def_args >= 0) { |
| 768 | arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string(); |
| 769 | } |
| 770 | |
| 771 | if (i == p_arg_idx) { |
| 772 | arghint += String::chr(0xFFFF); |
| 773 | } |
| 774 | |
| 775 | i++; |
| 776 | } |
| 777 | |
| 778 | if (p_info.flags & METHOD_FLAG_VARARG) { |
| 779 | if (p_info.arguments.size() > 0) { |
| 780 | arghint += ", "; |
| 781 | } |
| 782 | if (p_arg_idx >= p_info.arguments.size()) { |
| 783 | arghint += String::chr(0xFFFF); |
| 784 | } |
| 785 | arghint += "...args: Array"; // `MethodInfo` does not support the rest parameter name. |
| 786 | if (p_arg_idx >= p_info.arguments.size()) { |
| 787 | arghint += String::chr(0xFFFF); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | arghint += ")"; |
| 792 | |
| 793 | return arghint; |
| 794 | } |
| 795 | |
| 796 | static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx, bool p_just_args = false) { |
| 797 | String arghint; |
| 798 | |
| 799 | if (p_just_args) { |
| 800 | arghint = "("; |
| 801 | } else { |
| 802 | if (p_function->get_datatype().builtin_type == Variant::NIL) { |
| 803 | arghint = "void " + p_function->identifier->name + "("; |
| 804 | } else { |
| 805 | arghint = p_function->get_datatype().to_string() + " " + p_function->identifier->name + "("; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | for (int i = 0; i < p_function->parameters.size(); i++) { |
| 810 | if (i > 0) { |
| 811 | arghint += ", "; |
| 812 | } |
| 813 | |
| 814 | if (i == p_arg_idx) { |
| 815 | arghint += String::chr(0xFFFF); |
| 816 | } |
| 817 | const GDScriptParser::ParameterNode *par = p_function->parameters[i]; |
| 818 | if (!par->get_datatype().is_hard_type()) { |
| 819 | arghint += par->identifier->name.operator String() + ": Variant"; |
| 820 | } else { |
| 821 | arghint += par->identifier->name.operator String() + ": " + par->get_datatype().to_string(); |
| 822 | } |
| 823 | |
| 824 | if (par->initializer) { |
| 825 | String def_val = "<unknown>"; |
| 826 | switch (par->initializer->type) { |
| 827 | case GDScriptParser::Node::LITERAL: { |
| 828 | const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(par->initializer); |
| 829 | def_val = literal->value.get_construct_string(); |
| 830 | } break; |
| 831 | case GDScriptParser::Node::IDENTIFIER: { |
| 832 | const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(par->initializer); |
| 833 | def_val = id->name.operator String(); |
| 834 | } break; |
| 835 | case GDScriptParser::Node::CALL: { |
| 836 | const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(par->initializer); |
| 837 | if (call->is_constant && call->reduced) { |
| 838 | def_val = call->reduced_value.get_construct_string(); |
| 839 | } else if (call->get_callee_type() == GDScriptParser::Node::IDENTIFIER) { |
| 840 | def_val = call->function_name.operator String() + (call->arguments.is_empty() ? "()" : "(...)"); |
| 841 | } |
| 842 | } break; |
| 843 | case GDScriptParser::Node::ARRAY: { |
| 844 | const GDScriptParser::ArrayNode *arr = static_cast<const GDScriptParser::ArrayNode *>(par->initializer); |
| 845 | if (arr->is_constant && arr->reduced) { |
| 846 | def_val = arr->reduced_value.get_construct_string(); |
| 847 | } else { |
| 848 | def_val = arr->elements.is_empty() ? "[]" : "[...]"; |
| 849 | } |
| 850 | } break; |
| 851 | case GDScriptParser::Node::DICTIONARY: { |
| 852 | const GDScriptParser::DictionaryNode *dict = static_cast<const GDScriptParser::DictionaryNode *>(par->initializer); |
| 853 | if (dict->is_constant && dict->reduced) { |
| 854 | def_val = dict->reduced_value.get_construct_string(); |
| 855 | } else { |
| 856 | def_val = dict->elements.is_empty() ? "{}" : "{...}"; |
| 857 | } |
| 858 | } break; |
| 859 | case GDScriptParser::Node::SUBSCRIPT: { |
| 860 | const GDScriptParser::SubscriptNode *sub = static_cast<const GDScriptParser::SubscriptNode *>(par->initializer); |
| 861 | if (sub->is_attribute && sub->datatype.kind == GDScriptParser::DataType::ENUM && !sub->datatype.is_meta_type) { |
| 862 | def_val = sub->get_datatype().to_string() + "." + sub->attribute->name; |
| 863 | } else if (sub->is_constant && sub->reduced) { |
| 864 | def_val = sub->reduced_value.get_construct_string(); |
| 865 | } |
| 866 | } break; |
| 867 | default: |
| 868 | break; |
| 869 | } |
| 870 | arghint += " = " + def_val; |
| 871 | } |
| 872 | if (i == p_arg_idx) { |
| 873 | arghint += String::chr(0xFFFF); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | if (p_function->is_vararg()) { |
| 878 | if (!p_function->parameters.is_empty()) { |
| 879 | arghint += ", "; |
| 880 | } |
| 881 | if (p_arg_idx >= p_function->parameters.size()) { |
| 882 | arghint += String::chr(0xFFFF); |
| 883 | } |
| 884 | const GDScriptParser::ParameterNode *rest_param = p_function->rest_parameter; |
| 885 | arghint += "..." + rest_param->identifier->name + ": " + rest_param->get_datatype().to_string(); |
| 886 | if (p_arg_idx >= p_function->parameters.size()) { |
| 887 | arghint += String::chr(0xFFFF); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | arghint += ")"; |
| 892 | |
| 893 | return arghint; |
| 894 | } |
| 895 | |
| 896 | static void _get_directory_contents(EditorFileSystemDirectory *p_dir, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_list, const StringName &p_required_type = StringName()) { |
| 897 | const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\""; |
| 898 | const bool requires_type = !p_required_type.is_empty(); |
| 899 | |
| 900 | for (int i = 0; i < p_dir->get_file_count(); i++) { |
| 901 | if (requires_type && !ClassDB::is_parent_class(p_dir->get_file_type(i), p_required_type)) { |
| 902 | continue; |
| 903 | } |
| 904 | ScriptLanguage::CodeCompletionOption option(p_dir->get_file_path(i).quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH); |
| 905 | r_list.insert(option.display, option); |
| 906 | } |
| 907 | |
| 908 | for (int i = 0; i < p_dir->get_subdir_count(); i++) { |
| 909 | _get_directory_contents(p_dir->get_subdir(i), r_list, p_required_type); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | static void _find_annotation_arguments(const GDScriptParser::AnnotationNode *p_annotation, int p_argument, const String p_quote_style, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, String &r_arghint) { |
| 914 | ERR_FAIL_NULL(p_annotation); |
| 915 | |
| 916 | if (p_annotation->info != nullptr) { |
| 917 | r_arghint = _make_arguments_hint(p_annotation->info->info, p_argument, true); |
| 918 | } |
| 919 | if (p_annotation->name == SNAME("@export_range")) { |
| 920 | if (p_argument == 3 || p_argument == 4 || p_argument == 5) { |
| 921 | // Slider hint. |
| 922 | ScriptLanguage::CodeCompletionOption slider1("or_greater", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 923 | slider1.insert_text = slider1.display.quote(p_quote_style); |
| 924 | r_result.insert(slider1.display, slider1); |
| 925 | ScriptLanguage::CodeCompletionOption slider2("or_less", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 926 | slider2.insert_text = slider2.display.quote(p_quote_style); |
| 927 | r_result.insert(slider2.display, slider2); |
| 928 | ScriptLanguage::CodeCompletionOption slider3("prefer_slider", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 929 | slider3.insert_text = slider3.display.quote(p_quote_style); |
| 930 | r_result.insert(slider3.display, slider3); |
| 931 | ScriptLanguage::CodeCompletionOption slider4("hide_control", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 932 | slider4.insert_text = slider4.display.quote(p_quote_style); |
| 933 | r_result.insert(slider4.display, slider4); |
| 934 | } |
| 935 | } else if (p_annotation->name == SNAME("@export_exp_easing")) { |
| 936 | if (p_argument == 0 || p_argument == 1) { |
| 937 | // Easing hint. |
| 938 | ScriptLanguage::CodeCompletionOption hint1("attenuation", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 939 | hint1.insert_text = hint1.display.quote(p_quote_style); |
| 940 | r_result.insert(hint1.display, hint1); |
| 941 | ScriptLanguage::CodeCompletionOption hint2("inout", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 942 | hint2.insert_text = hint2.display.quote(p_quote_style); |
| 943 | r_result.insert(hint2.display, hint2); |
| 944 | } |
| 945 | } else if (p_annotation->name == SNAME("@export_node_path")) { |
| 946 | ScriptLanguage::CodeCompletionOption node("Node", ScriptLanguage::CODE_COMPLETION_KIND_CLASS); |
| 947 | node.insert_text = node.display.quote(p_quote_style); |
| 948 | r_result.insert(node.display, node); |
| 949 | |
| 950 | LocalVector<StringName> native_classes; |
| 951 | ClassDB::get_inheriters_from_class("Node", native_classes); |
| 952 | for (const StringName &E : native_classes) { |
| 953 | if (!ClassDB::is_class_exposed(E)) { |
| 954 | continue; |
| 955 | } |
| 956 | ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS); |
| 957 | option.insert_text = option.display.quote(p_quote_style); |
| 958 | r_result.insert(option.display, option); |
| 959 | } |
| 960 | |
| 961 | LocalVector<StringName> global_script_classes; |
| 962 | ScriptServer::get_global_class_list(global_script_classes); |
| 963 | for (const StringName &class_name : global_script_classes) { |
| 964 | if (!ClassDB::is_parent_class(ScriptServer::get_global_class_native_base(class_name), "Node")) { |
| 965 | continue; |
| 966 | } |
| 967 | ScriptLanguage::CodeCompletionOption option(class_name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS); |
| 968 | option.insert_text = option.display.quote(p_quote_style); |
| 969 | r_result.insert(option.display, option); |
| 970 | } |
| 971 | } else if (p_annotation->name == SNAME("@export_tool_button")) { |
| 972 | if (p_argument == 1) { |
| 973 | const Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme(); |
| 974 | if (theme.is_valid()) { |
| 975 | List<StringName> icon_list; |
| 976 | theme->get_icon_list(EditorStringName(EditorIcons), &icon_list); |
| 977 | for (const StringName &E : icon_list) { |
| 978 | ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS); |
| 979 | option.insert_text = option.display.quote(p_quote_style); |
| 980 | r_result.insert(option.display, option); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | } else if (p_annotation->name == SNAME("@export_custom")) { |
| 985 | switch (p_argument) { |
| 986 | case 0: { |
| 987 | static HashMap<StringName, int64_t> items; |
| 988 | if (unlikely(items.is_empty())) { |
| 989 | CoreConstants::get_enum_values(SNAME("PropertyHint"), &items); |
| 990 | } |
| 991 | for (const KeyValue<StringName, int64_t> &item : items) { |
| 992 | ScriptLanguage::CodeCompletionOption option(item.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT); |
| 993 | r_result.insert(option.display, option); |
| 994 | } |
| 995 | } break; |
| 996 | case 2: { |
| 997 | static HashMap<StringName, int64_t> items; |
| 998 | if (unlikely(items.is_empty())) { |
| 999 | CoreConstants::get_enum_values(SNAME("PropertyUsageFlags"), &items); |
| 1000 | } |
| 1001 | for (const KeyValue<StringName, int64_t> &item : items) { |
| 1002 | ScriptLanguage::CodeCompletionOption option(item.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT); |
| 1003 | r_result.insert(option.display, option); |
| 1004 | } |
| 1005 | } break; |
| 1006 | } |
| 1007 | } else if (p_annotation->name == SNAME("@warning_ignore") || p_annotation->name == SNAME("@warning_ignore_start") || p_annotation->name == SNAME("@warning_ignore_restore")) { |
| 1008 | for (int warning_code = 0; warning_code < GDScriptWarning::WARNING_MAX; warning_code++) { |
| 1009 | #ifndef DISABLE_DEPRECATED |
| 1010 | if (warning_code >= GDScriptWarning::FIRST_DEPRECATED_WARNING) { |
| 1011 | break; // Don't suggest deprecated warnings as they are never produced. |
| 1012 | } |
| 1013 | #endif // DISABLE_DEPRECATED |
| 1014 | ScriptLanguage::CodeCompletionOption warning(GDScriptWarning::get_name_from_code((GDScriptWarning::Code)warning_code).to_lower(), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 1015 | warning.insert_text = warning.display.quote(p_quote_style); |
| 1016 | r_result.insert(warning.display, warning); |
| 1017 | } |
| 1018 | } else if (p_annotation->name == SNAME("@rpc")) { |
| 1019 | if (p_argument == 0 || p_argument == 1 || p_argument == 2) { |
| 1020 | static const char *options[7] = { "call_local", "call_remote", "any_peer", "authority", "reliable", "unreliable", "unreliable_ordered" }; |
| 1021 | for (int i = 0; i < 7; i++) { |
| 1022 | ScriptLanguage::CodeCompletionOption option(options[i], ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 1023 | option.insert_text = option.display.quote(p_quote_style); |
| 1024 | r_result.insert(option.display, option); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | static void _find_built_in_variants(HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) { |
| 1031 | for (int i = 0; i < Variant::VARIANT_MAX; i++) { |
| 1032 | if (Variant::Type(i) == Variant::Type::NIL) { |
| 1033 | continue; |
| 1034 | } |
| 1035 | ScriptLanguage::CodeCompletionOption option(Variant::get_type_name(Variant::Type(i)), ScriptLanguage::CODE_COMPLETION_KIND_CLASS); |
| 1036 | r_result.insert(option.display, option); |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | static void _find_global_enums(HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) { |
| 1041 | List<StringName> global_enums; |
| 1042 | CoreConstants::get_global_enums(&global_enums); |
| 1043 | for (const StringName &enum_name : global_enums) { |
| 1044 | ScriptLanguage::CodeCompletionOption option(enum_name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, ScriptLanguage::LOCATION_OTHER); |
| 1045 | r_result.insert(option.display, option); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | static void _list_available_types(bool p_inherit_only, GDScriptParser::CompletionContext &p_context, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) { |
| 1050 | // Built-in Variant Types |
| 1051 | _find_built_in_variants(r_result); |
| 1052 | |
| 1053 | // Variant meta-type |
| 1054 | if (!p_inherit_only) { |
| 1055 | ScriptLanguage::CodeCompletionOption variant_option("Variant", ScriptLanguage::CODE_COMPLETION_KIND_CLASS); |
| 1056 | r_result.insert(variant_option.display, variant_option); |
| 1057 | } |
| 1058 | |
| 1059 | LocalVector<StringName> native_types; |
| 1060 | ClassDB::get_class_list(native_types); |
| 1061 | for (const StringName &type : native_types) { |
| 1062 | if (ClassDB::is_class_exposed(type) && !Engine::get_singleton()->has_singleton(type)) { |
| 1063 | ScriptLanguage::CodeCompletionOption option(type, ScriptLanguage::CODE_COMPLETION_KIND_CLASS); |
| 1064 | r_result.insert(option.display, option); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | // TODO: Unify with _find_identifiers_in_class. |
| 1069 | if (p_context.current_class) { |
| 1070 | if (!p_inherit_only && p_context.current_class->base_type.is_set()) { |
| 1071 | // Native enums from base class |
| 1072 | List<StringName> enums; |
| 1073 | ClassDB::get_enum_list(p_context.current_class->base_type.native_type, &enums); |
| 1074 | for (const StringName &E : enums) { |
| 1075 | ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM); |
| 1076 | r_result.insert(option.display, option); |
| 1077 | } |
| 1078 | } |
| 1079 | // Check current class for potential types. |
| 1080 | // TODO: Also check classes the current class inherits from. |
| 1081 | const GDScriptParser::ClassNode *current = p_context.current_class; |
| 1082 | int location_offset = 0; |
| 1083 | while (current) { |
| 1084 | for (int i = 0; i < current->members.size(); i++) { |
| 1085 | const GDScriptParser::ClassNode::Member &member = current->members[i]; |
| 1086 | switch (member.type) { |
| 1087 | case GDScriptParser::ClassNode::Member::CLASS: { |
| 1088 | ScriptLanguage::CodeCompletionOption option(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL + location_offset); |
| 1089 | r_result.insert(option.display, option); |
| 1090 | } break; |
| 1091 | case GDScriptParser::ClassNode::Member::ENUM: { |
| 1092 | if (!p_inherit_only) { |
| 1093 | ScriptLanguage::CodeCompletionOption option(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, ScriptLanguage::LOCATION_LOCAL + location_offset); |
| 1094 | r_result.insert(option.display, option); |
| 1095 | } |
| 1096 | } break; |
| 1097 | case GDScriptParser::ClassNode::Member::CONSTANT: { |
| 1098 | if (member.constant->get_datatype().is_meta_type) { |
| 1099 | ScriptLanguage::CodeCompletionOption option(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL + location_offset); |
| 1100 | r_result.insert(option.display, option); |
| 1101 | } |
| 1102 | } break; |
| 1103 | default: |
| 1104 | break; |
| 1105 | } |
| 1106 | } |
| 1107 | location_offset += 1; |
| 1108 | current = current->outer; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | // Global scripts |
| 1113 | LocalVector<StringName> global_classes; |
| 1114 | ScriptServer::get_global_class_list(global_classes); |
| 1115 | for (const StringName &class_name : global_classes) { |
| 1116 | ScriptLanguage::CodeCompletionOption option(class_name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE); |
| 1117 | r_result.insert(option.display, option); |
| 1118 | } |
| 1119 | |
| 1120 | // Global enums |
| 1121 | if (!p_inherit_only) { |
| 1122 | _find_global_enums(r_result); |
| 1123 | } |
| 1124 | |
| 1125 | // Autoload singletons |
| 1126 | HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list(); |
| 1127 | |
| 1128 | for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) { |
| 1129 | const ProjectSettings::AutoloadInfo &info = E.value; |
| 1130 | if (!info.is_singleton || !info.path.has_extension("gd")) { |
| 1131 | continue; |
| 1132 | } |
| 1133 | ScriptLanguage::CodeCompletionOption option(info.name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE); |
| 1134 | r_result.insert(option.display, option); |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | static void _find_identifiers_in_suite(const GDScriptParser::SuiteNode *p_suite, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth = 0) { |
| 1139 | for (int i = 0; i < p_suite->locals.size(); i++) { |
| 1140 | ScriptLanguage::CodeCompletionOption option; |
| 1141 | int location = p_recursion_depth == 0 ? ScriptLanguage::LOCATION_LOCAL : (p_recursion_depth | ScriptLanguage::LOCATION_PARENT_MASK); |
| 1142 | if (p_suite->locals[i].type == GDScriptParser::SuiteNode::Local::CONSTANT) { |
| 1143 | option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location); |
| 1144 | option.default_value = p_suite->locals[i].constant->initializer->reduced_value; |
| 1145 | } else { |
| 1146 | option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE, location); |
| 1147 | } |
| 1148 | r_result.insert(option.display, option); |
| 1149 | } |
| 1150 | if (p_suite->parent_block) { |
| 1151 | _find_identifiers_in_suite(p_suite->parent_block, r_result, p_recursion_depth + 1); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, bool p_types_only, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth); |
| 1156 | |
| 1157 | static void _find_identifiers_in_class(const GDScriptParser::ClassNode *p_class, bool p_only_functions, bool p_types_only, bool p_static, bool p_parent_only, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) { |
| 1158 | ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT); |
| 1159 | |
| 1160 | if (!p_parent_only) { |
| 1161 | bool outer = false; |
| 1162 | const GDScriptParser::ClassNode *clss = p_class; |
| 1163 | int classes_processed = 0; |
| 1164 | while (clss) { |
| 1165 | for (int i = 0; i < clss->members.size(); i++) { |
| 1166 | const int location = p_recursion_depth == 0 ? classes_processed : (p_recursion_depth | ScriptLanguage::LOCATION_PARENT_MASK); |
| 1167 | const GDScriptParser::ClassNode::Member &member = clss->members[i]; |
| 1168 | ScriptLanguage::CodeCompletionOption option; |
| 1169 | switch (member.type) { |
| 1170 | case GDScriptParser::ClassNode::Member::VARIABLE: |
| 1171 | if (p_types_only || p_only_functions || outer || (p_static && !member.variable->is_static)) { |
| 1172 | continue; |
| 1173 | } |
| 1174 | option = ScriptLanguage::CodeCompletionOption(member.variable->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location); |
| 1175 | break; |
| 1176 | case GDScriptParser::ClassNode::Member::CONSTANT: |
| 1177 | if ((p_types_only && !member.constant->datatype.is_meta_type) || p_only_functions) { |
| 1178 | continue; |
| 1179 | } |
| 1180 | if (r_result.has(member.constant->identifier->name)) { |
| 1181 | continue; |
| 1182 | } |
| 1183 | option = ScriptLanguage::CodeCompletionOption(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location); |
| 1184 | if (member.constant->initializer) { |
| 1185 | option.default_value = member.constant->initializer->reduced_value; |
| 1186 | } |
| 1187 | break; |
| 1188 | case GDScriptParser::ClassNode::Member::CLASS: |
| 1189 | if (p_only_functions) { |
| 1190 | continue; |
| 1191 | } |
| 1192 | option = ScriptLanguage::CodeCompletionOption(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, location); |
| 1193 | break; |
| 1194 | case GDScriptParser::ClassNode::Member::ENUM_VALUE: |
| 1195 | if (p_types_only || p_only_functions) { |
| 1196 | continue; |
| 1197 | } |
| 1198 | option = ScriptLanguage::CodeCompletionOption(member.enum_value.identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location); |
| 1199 | break; |
| 1200 | case GDScriptParser::ClassNode::Member::ENUM: |
| 1201 | if (p_only_functions) { |
| 1202 | continue; |
| 1203 | } |
| 1204 | option = ScriptLanguage::CodeCompletionOption(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location); |
| 1205 | break; |
| 1206 | case GDScriptParser::ClassNode::Member::FUNCTION: |
| 1207 | if (p_types_only || outer || (p_static && !member.function->is_static) || member.function->identifier->name.operator String().begins_with("@")) { |
| 1208 | continue; |
| 1209 | } |
| 1210 | option = ScriptLanguage::CodeCompletionOption(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location); |
| 1211 | if (p_add_braces) { |
| 1212 | if (member.function->parameters.size() > 0 || (member.function->info.flags & METHOD_FLAG_VARARG)) { |
| 1213 | option.insert_text += "("; |
| 1214 | option.display += U"(\u2026)"; |
| 1215 | } else { |
| 1216 | option.insert_text += "()"; |
| 1217 | option.display += "()"; |
| 1218 | } |
| 1219 | } |
| 1220 | break; |
| 1221 | case GDScriptParser::ClassNode::Member::SIGNAL: |
| 1222 | if (p_types_only || p_only_functions || outer || p_static) { |
| 1223 | continue; |
| 1224 | } |
| 1225 | option = ScriptLanguage::CodeCompletionOption(member.signal->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location); |
| 1226 | break; |
| 1227 | case GDScriptParser::ClassNode::Member::GROUP: |
| 1228 | break; // No-op, but silences warnings. |
| 1229 | case GDScriptParser::ClassNode::Member::UNDEFINED: |
| 1230 | break; |
| 1231 | } |
| 1232 | r_result.insert(option.display, option); |
| 1233 | } |
| 1234 | if (p_types_only) { |
| 1235 | break; // Otherwise, it will fill the results with types from the outer class (which is undesired for that case). |
| 1236 | } |
| 1237 | |
| 1238 | outer = true; |
| 1239 | clss = clss->outer; |
| 1240 | classes_processed++; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | // Parents. |
| 1245 | GDScriptCompletionIdentifier base_type; |
| 1246 | base_type.type = p_class->base_type; |
| 1247 | base_type.type.is_meta_type = p_static; |
| 1248 | |
| 1249 | _find_identifiers_in_base(base_type, p_only_functions, p_types_only, p_add_braces, r_result, p_recursion_depth + 1); |
| 1250 | } |
| 1251 | |
| 1252 | static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, bool p_types_only, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) { |
| 1253 | ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT); |
| 1254 | |
| 1255 | GDScriptParser::DataType base_type = p_base.type; |
| 1256 | |
| 1257 | if (!p_types_only && base_type.is_meta_type && base_type.kind != GDScriptParser::DataType::BUILTIN && base_type.kind != GDScriptParser::DataType::ENUM) { |
| 1258 | ScriptLanguage::CodeCompletionOption option("new", ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, ScriptLanguage::LOCATION_LOCAL); |
| 1259 | if (p_add_braces) { |
| 1260 | option.insert_text += "("; |
| 1261 | option.display += U"(\u2026)"; |
| 1262 | } |
| 1263 | r_result.insert(option.display, option); |
| 1264 | } |
| 1265 | |
| 1266 | while (!base_type.has_no_type()) { |
| 1267 | switch (base_type.kind) { |
| 1268 | case GDScriptParser::DataType::CLASS: { |
| 1269 | _find_identifiers_in_class(base_type.class_type, p_only_functions, p_types_only, base_type.is_meta_type, false, p_add_braces, r_result, p_recursion_depth); |
| 1270 | // This already finds all parent identifiers, so we are done. |
| 1271 | base_type = GDScriptParser::DataType(); |
| 1272 | } break; |
| 1273 | case GDScriptParser::DataType::SCRIPT: { |
| 1274 | Ref<Script> scr = base_type.script_type; |
| 1275 | if (scr.is_valid()) { |
| 1276 | if (p_types_only) { |
| 1277 | // TODO: Need to implement Script::get_script_enum_list and retrieve the enum list from a script. |
| 1278 | } else if (!p_only_functions) { |
| 1279 | if (!base_type.is_meta_type) { |
| 1280 | List<PropertyInfo> members; |
| 1281 | scr->get_script_property_list(&members); |
| 1282 | for (const PropertyInfo &E : members) { |
| 1283 | if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) { |
| 1284 | continue; |
| 1285 | } |
| 1286 | if (E.name.contains_char('/')) { |
| 1287 | continue; |
| 1288 | } |
| 1289 | int location = p_recursion_depth + _get_property_location(scr, E.name); |
| 1290 | ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location); |
| 1291 | r_result.insert(option.display, option); |
| 1292 | } |
| 1293 | |
| 1294 | List<MethodInfo> signals; |
| 1295 | scr->get_script_signal_list(&signals); |
| 1296 | for (const MethodInfo &E : signals) { |
| 1297 | if (E.name.begins_with("_")) { |
| 1298 | continue; |
| 1299 | } |
| 1300 | int location = p_recursion_depth + _get_signal_location(scr, E.name); |
| 1301 | ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location); |
| 1302 | r_result.insert(option.display, option); |
| 1303 | } |
| 1304 | } |
| 1305 | HashMap<StringName, Variant> constants; |
| 1306 | scr->get_constants(&constants); |
| 1307 | for (const KeyValue<StringName, Variant> &E : constants) { |
| 1308 | int location = p_recursion_depth + _get_constant_location(scr, E.key); |
| 1309 | ScriptLanguage::CodeCompletionOption option(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location); |
| 1310 | r_result.insert(option.display, option); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | if (!p_types_only) { |
| 1315 | List<MethodInfo> methods; |
| 1316 | scr->get_script_method_list(&methods); |
| 1317 | for (const MethodInfo &E : methods) { |
| 1318 | if (E.name.begins_with("@")) { |
| 1319 | continue; |
| 1320 | } |
| 1321 | int location = p_recursion_depth + _get_method_location(scr->get_class_name(), E.name); |
| 1322 | ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location); |
| 1323 | if (p_add_braces) { |
| 1324 | if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) { |
| 1325 | option.insert_text += "("; |
| 1326 | option.display += U"(\u2026)"; |
| 1327 | } else { |
| 1328 | option.insert_text += "()"; |
| 1329 | option.display += "()"; |
| 1330 | } |
| 1331 | } |
| 1332 | r_result.insert(option.display, option); |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | Ref<Script> base_script = scr->get_base_script(); |
| 1337 | if (base_script.is_valid()) { |
| 1338 | base_type.script_type = base_script; |
| 1339 | } else { |
| 1340 | base_type.kind = GDScriptParser::DataType::NATIVE; |
| 1341 | base_type.builtin_type = Variant::OBJECT; |
| 1342 | base_type.native_type = scr->get_instance_base_type(); |
| 1343 | } |
| 1344 | } else { |
| 1345 | return; |
| 1346 | } |
| 1347 | } break; |
| 1348 | case GDScriptParser::DataType::NATIVE: { |
| 1349 | StringName type = base_type.native_type; |
| 1350 | if (!GDScriptAnalyzer::class_exists(type)) { |
| 1351 | return; |
| 1352 | } |
| 1353 | |
| 1354 | List<StringName> enums; |
| 1355 | ClassDB::get_enum_list(type, &enums); |
| 1356 | for (const StringName &E : enums) { |
| 1357 | int location = p_recursion_depth + _get_enum_location(type, E); |
| 1358 | ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location); |
| 1359 | r_result.insert(option.display, option); |
| 1360 | } |
| 1361 | |
| 1362 | if (p_types_only) { |
| 1363 | return; |
| 1364 | } |
| 1365 | |
| 1366 | if (!p_only_functions) { |
| 1367 | List<String> constants; |
| 1368 | ClassDB::get_integer_constant_list(type, &constants); |
| 1369 | for (const String &E : constants) { |
| 1370 | int location = p_recursion_depth + _get_constant_location(type, StringName(E)); |
| 1371 | ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location); |
| 1372 | r_result.insert(option.display, option); |
| 1373 | } |
| 1374 | |
| 1375 | if (!base_type.is_meta_type || Engine::get_singleton()->has_singleton(type)) { |
| 1376 | List<PropertyInfo> pinfo; |
| 1377 | ClassDB::get_property_list(type, &pinfo); |
| 1378 | for (const PropertyInfo &E : pinfo) { |
| 1379 | if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) { |
| 1380 | continue; |
| 1381 | } |
| 1382 | if (E.name.contains_char('/')) { |
| 1383 | continue; |
| 1384 | } |
| 1385 | int location = p_recursion_depth + _get_property_location(type, E.name); |
| 1386 | ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location); |
| 1387 | r_result.insert(option.display, option); |
| 1388 | } |
| 1389 | |
| 1390 | List<MethodInfo> signals; |
| 1391 | ClassDB::get_signal_list(type, &signals); |
| 1392 | for (const MethodInfo &E : signals) { |
| 1393 | if (E.name.begins_with("_")) { |
| 1394 | continue; |
| 1395 | } |
| 1396 | int location = p_recursion_depth + _get_signal_location(type, StringName(E.name)); |
| 1397 | ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location); |
| 1398 | r_result.insert(option.display, option); |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | bool only_static = base_type.is_meta_type && !Engine::get_singleton()->has_singleton(type); |
| 1404 | |
| 1405 | List<MethodInfo> methods; |
| 1406 | ClassDB::get_method_list(type, &methods, false, true); |
| 1407 | for (const MethodInfo &E : methods) { |
| 1408 | if (only_static && (E.flags & METHOD_FLAG_STATIC) == 0) { |
| 1409 | continue; |
| 1410 | } |
| 1411 | if (E.name.begins_with("_")) { |
| 1412 | continue; |
| 1413 | } |
| 1414 | int location = p_recursion_depth + _get_method_location(type, E.name); |
| 1415 | ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location); |
| 1416 | if (p_add_braces) { |
| 1417 | if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) { |
| 1418 | option.insert_text += "("; |
| 1419 | option.display += U"(\u2026)"; |
| 1420 | } else { |
| 1421 | option.insert_text += "()"; |
| 1422 | option.display += "()"; |
| 1423 | } |
| 1424 | } |
| 1425 | r_result.insert(option.display, option); |
| 1426 | } |
| 1427 | return; |
| 1428 | } break; |
| 1429 | case GDScriptParser::DataType::ENUM: { |
| 1430 | if (p_types_only) { |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | String type_str = base_type.native_type; |
| 1435 | |
| 1436 | if (type_str.contains_char('.')) { |
| 1437 | StringName type = type_str.get_slicec('.', 0); |
| 1438 | StringName type_enum = base_type.enum_type; |
| 1439 | |
| 1440 | List<StringName> enum_values; |
| 1441 | |
| 1442 | ClassDB::get_enum_constants(type, type_enum, &enum_values); |
| 1443 | |
| 1444 | for (const StringName &E : enum_values) { |
| 1445 | int location = p_recursion_depth + _get_enum_constant_location(type, E); |
| 1446 | ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location); |
| 1447 | r_result.insert(option.display, option); |
| 1448 | } |
| 1449 | } else if (CoreConstants::is_global_enum(base_type.enum_type)) { |
| 1450 | HashMap<StringName, int64_t> enum_values; |
| 1451 | CoreConstants::get_enum_values(base_type.enum_type, &enum_values); |
| 1452 | |
| 1453 | for (const KeyValue<StringName, int64_t> &enum_value : enum_values) { |
| 1454 | int location = p_recursion_depth + ScriptLanguage::LOCATION_OTHER; |
| 1455 | ScriptLanguage::CodeCompletionOption option(enum_value.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location); |
| 1456 | r_result.insert(option.display, option); |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | [[fallthrough]]; |
| 1461 | case GDScriptParser::DataType::BUILTIN: { |
| 1462 | if (p_types_only) { |
| 1463 | return; |
| 1464 | } |
| 1465 | |
| 1466 | Callable::CallError err; |
| 1467 | Variant tmp; |
| 1468 | Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err); |
| 1469 | if (err.error != Callable::CallError::CALL_OK) { |
| 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | int location = ScriptLanguage::LOCATION_OTHER; |
| 1474 | |
| 1475 | if (!p_only_functions) { |
| 1476 | List<PropertyInfo> members; |
| 1477 | if (p_base.value.get_type() != Variant::NIL) { |
| 1478 | p_base.value.get_property_list(&members); |
| 1479 | } else { |
| 1480 | tmp.get_property_list(&members); |
| 1481 | } |
| 1482 | |
| 1483 | for (const PropertyInfo &E : members) { |
| 1484 | if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) { |
| 1485 | continue; |
| 1486 | } |
| 1487 | if (!String(E.name).contains_char('/')) { |
| 1488 | ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location); |
| 1489 | if (base_type.kind == GDScriptParser::DataType::ENUM) { |
| 1490 | // Sort enum members in their declaration order. |
| 1491 | location += 1; |
| 1492 | } |
| 1493 | if (GDScriptParser::theme_color_names.has(E.name)) { |
| 1494 | option.theme_color_name = GDScriptParser::theme_color_names[E.name]; |
| 1495 | } |
| 1496 | r_result.insert(option.display, option); |
| 1497 | } |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | List<MethodInfo> methods; |
| 1502 | tmp.get_method_list(&methods); |
| 1503 | for (const MethodInfo &E : methods) { |
| 1504 | if (base_type.kind == GDScriptParser::DataType::ENUM && base_type.is_meta_type && !(E.flags & METHOD_FLAG_CONST)) { |
| 1505 | // Enum types are static and cannot change, therefore we skip non-const dictionary methods. |
| 1506 | continue; |
| 1507 | } |
| 1508 | ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location); |
| 1509 | if (p_add_braces) { |
| 1510 | if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) { |
| 1511 | option.insert_text += "("; |
| 1512 | option.display += U"(\u2026)"; |
| 1513 | } else { |
| 1514 | option.insert_text += "()"; |
| 1515 | option.display += "()"; |
| 1516 | } |
| 1517 | } |
| 1518 | r_result.insert(option.display, option); |
| 1519 | } |
| 1520 | |
| 1521 | return; |
| 1522 | } break; |
| 1523 | default: { |
| 1524 | return; |
| 1525 | } break; |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | static void _find_identifiers(const GDScriptParser::CompletionContext &p_context, bool p_only_functions, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) { |
| 1531 | if (!p_only_functions && p_context.current_suite) { |
| 1532 | // This includes function parameters, since they are also locals. |
| 1533 | _find_identifiers_in_suite(p_context.current_suite, r_result); |
| 1534 | } |
| 1535 | |
| 1536 | if (p_context.current_class) { |
| 1537 | _find_identifiers_in_class(p_context.current_class, p_only_functions, false, (!p_context.current_function || p_context.current_function->is_static), false, p_add_braces, r_result, p_recursion_depth); |
| 1538 | } |
| 1539 | |
| 1540 | List<StringName> functions; |
| 1541 | GDScriptUtilityFunctions::get_function_list(&functions); |
| 1542 | |
| 1543 | for (const StringName &E : functions) { |
| 1544 | MethodInfo function = GDScriptUtilityFunctions::get_function_info(E); |
| 1545 | ScriptLanguage::CodeCompletionOption option(String(E), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION); |
| 1546 | if (p_add_braces) { |
| 1547 | if (function.arguments.size() || (function.flags & METHOD_FLAG_VARARG)) { |
| 1548 | option.insert_text += "("; |
| 1549 | option.display += U"(\u2026)"; |
| 1550 | } else { |
| 1551 | option.insert_text += "()"; |
| 1552 | option.display += "()"; |
| 1553 | } |
| 1554 | } |
| 1555 | r_result.insert(option.display, option); |
| 1556 | } |
| 1557 | |
| 1558 | if (p_only_functions) { |
| 1559 | return; |
| 1560 | } |
| 1561 | |
| 1562 | _find_built_in_variants(r_result); |
| 1563 | |
| 1564 | static const char *_keywords[] = { |
| 1565 | "true", "false", "PI", "TAU", "INF", "NAN", "null", "self", "super", |
| 1566 | "break", "breakpoint", "continue", "pass", "return", |
| 1567 | nullptr |
| 1568 | }; |
| 1569 | |
| 1570 | const char **kw = _keywords; |
| 1571 | while (*kw) { |
| 1572 | ScriptLanguage::CodeCompletionOption option(*kw, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 1573 | r_result.insert(option.display, option); |
| 1574 | kw++; |
| 1575 | } |
| 1576 | |
| 1577 | static const char *_keywords_with_space[] = { |
| 1578 | "and", "not", "or", "in", "as", "class", "class_name", "extends", "is", "func", "signal", "await", |
| 1579 | "const", "enum", "static", "var", "if", "elif", "else", "for", "match", "when", "while", |
| 1580 | nullptr |
| 1581 | }; |
| 1582 | |
| 1583 | const char **kws = _keywords_with_space; |
| 1584 | while (*kws) { |
| 1585 | ScriptLanguage::CodeCompletionOption option(*kws, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 1586 | option.insert_text += " "; |
| 1587 | r_result.insert(option.display, option); |
| 1588 | kws++; |
| 1589 | } |
| 1590 | |
| 1591 | static const char *_keywords_with_args[] = { |
| 1592 | "assert", "preload", |
| 1593 | nullptr |
| 1594 | }; |
| 1595 | |
| 1596 | const char **kwa = _keywords_with_args; |
| 1597 | while (*kwa) { |
| 1598 | ScriptLanguage::CodeCompletionOption option(*kwa, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION); |
| 1599 | if (p_add_braces) { |
| 1600 | option.insert_text += "("; |
| 1601 | option.display += U"(\u2026)"; |
| 1602 | } |
| 1603 | r_result.insert(option.display, option); |
| 1604 | kwa++; |
| 1605 | } |
| 1606 | |
| 1607 | List<StringName> utility_func_names; |
| 1608 | Variant::get_utility_function_list(&utility_func_names); |
| 1609 | |
| 1610 | for (const StringName &util_func_name : utility_func_names) { |
| 1611 | ScriptLanguage::CodeCompletionOption option(util_func_name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION); |
| 1612 | if (p_add_braces) { |
| 1613 | option.insert_text += "("; |
| 1614 | option.display += U"(\u2026)"; // As all utility functions contain an argument or more, this is hardcoded here. |
| 1615 | } |
| 1616 | r_result.insert(option.display, option); |
| 1617 | } |
| 1618 | |
| 1619 | for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) { |
| 1620 | if (!E.value.is_singleton) { |
| 1621 | continue; |
| 1622 | } |
| 1623 | ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT); |
| 1624 | r_result.insert(option.display, option); |
| 1625 | } |
| 1626 | |
| 1627 | // Native classes and global constants. |
| 1628 | for (const KeyValue<StringName, int> &E : GDScriptLanguage::get_singleton()->get_global_map()) { |
| 1629 | ScriptLanguage::CodeCompletionOption option; |
| 1630 | if (GDScriptAnalyzer::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) { |
| 1631 | option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CLASS); |
| 1632 | } else { |
| 1633 | option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT); |
| 1634 | } |
| 1635 | r_result.insert(option.display, option); |
| 1636 | } |
| 1637 | |
| 1638 | // Global enums |
| 1639 | _find_global_enums(r_result); |
| 1640 | |
| 1641 | // Global classes |
| 1642 | LocalVector<StringName> global_classes; |
| 1643 | ScriptServer::get_global_class_list(global_classes); |
| 1644 | for (const StringName &class_name : global_classes) { |
| 1645 | ScriptLanguage::CodeCompletionOption option(class_name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE); |
| 1646 | r_result.insert(option.display, option); |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value, GDScriptParser::CompletionContext &p_context) { |
| 1651 | GDScriptCompletionIdentifier ci; |
| 1652 | ci.value = p_value; |
| 1653 | ci.type.is_constant = true; |
| 1654 | ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 1655 | ci.type.kind = GDScriptParser::DataType::BUILTIN; |
| 1656 | ci.type.builtin_type = p_value.get_type(); |
| 1657 | |
| 1658 | if (ci.type.builtin_type == Variant::OBJECT) { |
| 1659 | Object *obj = p_value.operator Object *(); |
| 1660 | if (!obj) { |
| 1661 | return ci; |
| 1662 | } |
| 1663 | ci.type.native_type = obj->get_class_name(); |
| 1664 | Ref<Script> scr = p_value; |
| 1665 | if (scr.is_valid()) { |
| 1666 | ci.type.is_meta_type = true; |
| 1667 | } else { |
| 1668 | ci.type.is_meta_type = false; |
| 1669 | scr = obj->get_script(); |
| 1670 | } |
| 1671 | if (scr.is_valid()) { |
| 1672 | ci.type.script_path = scr->get_path(); |
| 1673 | ci.type.script_type = scr; |
| 1674 | ci.type.native_type = scr->get_instance_base_type(); |
| 1675 | ci.type.kind = GDScriptParser::DataType::SCRIPT; |
| 1676 | |
| 1677 | if (scr->get_path().ends_with(".gd")) { |
| 1678 | Ref<GDScriptParserRef> parser = p_context.parser->get_depended_parser_for(scr->get_path()); |
| 1679 | if (parser.is_valid() && parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED) == OK) { |
| 1680 | ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 1681 | ci.type.class_type = parser->get_parser()->get_tree(); |
| 1682 | ci.type.kind = GDScriptParser::DataType::CLASS; |
| 1683 | return ci; |
| 1684 | } |
| 1685 | } |
| 1686 | } else { |
| 1687 | ci.type.kind = GDScriptParser::DataType::NATIVE; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | return ci; |
| 1692 | } |
| 1693 | |
| 1694 | static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) { |
| 1695 | GDScriptCompletionIdentifier ci; |
| 1696 | |
| 1697 | if (p_property.type == Variant::NIL) { |
| 1698 | // Variant |
| 1699 | ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 1700 | ci.type.kind = GDScriptParser::DataType::VARIANT; |
| 1701 | return ci; |
| 1702 | } |
| 1703 | |
| 1704 | if (p_property.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) { |
| 1705 | ci.enumeration = p_property.class_name; |
| 1706 | } |
| 1707 | |
| 1708 | ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 1709 | ci.type.builtin_type = p_property.type; |
| 1710 | if (p_property.type == Variant::OBJECT) { |
| 1711 | if (ScriptServer::is_global_class(p_property.class_name)) { |
| 1712 | ci.type.kind = GDScriptParser::DataType::SCRIPT; |
| 1713 | ci.type.script_path = ScriptServer::get_global_class_path(p_property.class_name); |
| 1714 | ci.type.native_type = ScriptServer::get_global_class_native_base(p_property.class_name); |
| 1715 | |
| 1716 | Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_property.class_name)); |
| 1717 | if (scr.is_valid()) { |
| 1718 | ci.type.script_type = scr; |
| 1719 | } |
| 1720 | } else { |
| 1721 | ci.type.kind = GDScriptParser::DataType::NATIVE; |
| 1722 | ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name; |
| 1723 | } |
| 1724 | } else { |
| 1725 | ci.type.kind = GDScriptParser::DataType::BUILTIN; |
| 1726 | } |
| 1727 | return ci; |
| 1728 | } |
| 1729 | |
| 1730 | static GDScriptCompletionIdentifier _callable_type_from_method_info(const MethodInfo &p_method) { |
| 1731 | GDScriptCompletionIdentifier ci; |
| 1732 | ci.type.kind = GDScriptParser::DataType::BUILTIN; |
| 1733 | ci.type.builtin_type = Variant::CALLABLE; |
| 1734 | ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 1735 | ci.type.is_constant = true; |
| 1736 | ci.type.method_info = p_method; |
| 1737 | return ci; |
| 1738 | } |
| 1739 | |
| 1740 | #define MAX_COMPLETION_RECURSION 100 |
| 1741 | struct RecursionCheck { |
| 1742 | int *counter; |
| 1743 | _FORCE_INLINE_ bool check() { |
| 1744 | return (*counter) > MAX_COMPLETION_RECURSION; |
| 1745 | } |
| 1746 | RecursionCheck(int *p_counter) : |
| 1747 | counter(p_counter) { |
| 1748 | (*counter)++; |
| 1749 | } |
| 1750 | ~RecursionCheck() { |
| 1751 | (*counter)--; |
| 1752 | } |
| 1753 | }; |
| 1754 | |
| 1755 | static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::IdentifierNode *p_identifier, GDScriptCompletionIdentifier &r_type); |
| 1756 | static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type); |
| 1757 | static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type); |
| 1758 | |
| 1759 | static bool _is_expression_named_identifier(const GDScriptParser::ExpressionNode *p_expression, const StringName &p_name) { |
| 1760 | if (p_expression) { |
| 1761 | switch (p_expression->type) { |
| 1762 | case GDScriptParser::Node::IDENTIFIER: { |
| 1763 | const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression); |
| 1764 | if (id->name == p_name) { |
| 1765 | return true; |
| 1766 | } |
| 1767 | } break; |
| 1768 | case GDScriptParser::Node::CAST: { |
| 1769 | const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression); |
| 1770 | return _is_expression_named_identifier(cn->operand, p_name); |
| 1771 | } break; |
| 1772 | default: |
| 1773 | break; |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | return false; |
| 1778 | } |
| 1779 | |
| 1780 | // Creates a map of exemplary results for some functions that return a structured dictionary. |
| 1781 | // Setting this example as value allows autocompletion to suggest the specific keys in some cases. |
| 1782 | static HashMap<String, Dictionary> make_structure_samples() { |
| 1783 | HashMap<String, Dictionary> res; |
| 1784 | const Array arr; |
| 1785 | |
| 1786 | { |
| 1787 | Dictionary d; |
| 1788 | d.set("major", 0); |
| 1789 | d.set("minor", 0); |
| 1790 | d.set("patch", 0); |
| 1791 | d.set("hex", 0); |
| 1792 | d.set("status", String()); |
| 1793 | d.set("build", String()); |
| 1794 | d.set("hash", String()); |
| 1795 | d.set("timestamp", 0); |
| 1796 | d.set("string", String()); |
| 1797 | res["Engine::get_version_info"] = d; |
| 1798 | } |
| 1799 | |
| 1800 | { |
| 1801 | Dictionary d; |
| 1802 | d.set("lead_developers", arr); |
| 1803 | d.set("founders", arr); |
| 1804 | d.set("project_managers", arr); |
| 1805 | d.set("developers", arr); |
| 1806 | res["Engine::get_author_info"] = d; |
| 1807 | } |
| 1808 | |
| 1809 | { |
| 1810 | Dictionary d; |
| 1811 | d.set("platinum_sponsors", arr); |
| 1812 | d.set("gold_sponsors", arr); |
| 1813 | d.set("silver_sponsors", arr); |
| 1814 | d.set("bronze_sponsors", arr); |
| 1815 | d.set("mini_sponsors", arr); |
| 1816 | d.set("gold_donors", arr); |
| 1817 | d.set("silver_donors", arr); |
| 1818 | d.set("bronze_donors", arr); |
| 1819 | res["Engine::get_donor_info"] = d; |
| 1820 | } |
| 1821 | |
| 1822 | { |
| 1823 | Dictionary d; |
| 1824 | d.set("physical", -1); |
| 1825 | d.set("free", -1); |
| 1826 | d.set("available", -1); |
| 1827 | d.set("stack", -1); |
| 1828 | res["OS::get_memory_info"] = d; |
| 1829 | } |
| 1830 | |
| 1831 | { |
| 1832 | Dictionary d; |
| 1833 | d.set("year", 0); |
| 1834 | d.set("month", 0); |
| 1835 | d.set("day", 0); |
| 1836 | d.set("weekday", 0); |
| 1837 | d.set("hour", 0); |
| 1838 | d.set("minute", 0); |
| 1839 | d.set("second", 0); |
| 1840 | d.set("dst", 0); |
| 1841 | res["Time::get_datetime_dict_from_system"] = d; |
| 1842 | } |
| 1843 | |
| 1844 | { |
| 1845 | Dictionary d; |
| 1846 | d.set("year", 0); |
| 1847 | d.set("month", 0); |
| 1848 | d.set("day", 0); |
| 1849 | d.set("weekday", 0); |
| 1850 | d.set("hour", 0); |
| 1851 | d.set("minute", 0); |
| 1852 | d.set("second", 0); |
| 1853 | res["Time::get_datetime_dict_from_unix_time"] = d; |
| 1854 | } |
| 1855 | |
| 1856 | { |
| 1857 | Dictionary d; |
| 1858 | d.set("year", 0); |
| 1859 | d.set("month", 0); |
| 1860 | d.set("day", 0); |
| 1861 | d.set("weekday", 0); |
| 1862 | res["Time::get_date_dict_from_system"] = d; |
| 1863 | res["Time::get_date_dict_from_unix_time"] = d; |
| 1864 | } |
| 1865 | |
| 1866 | { |
| 1867 | Dictionary d; |
| 1868 | d.set("hour", 0); |
| 1869 | d.set("minute", 0); |
| 1870 | d.set("second", 0); |
| 1871 | res["Time::get_time_dict_from_system"] = d; |
| 1872 | res["Time::get_time_dict_from_unix_time"] = d; |
| 1873 | } |
| 1874 | |
| 1875 | { |
| 1876 | Dictionary d; |
| 1877 | d.set("bias", 0); |
| 1878 | d.set("name", String()); |
| 1879 | res["Time::get_time_zone_from_system"] = d; |
| 1880 | } |
| 1881 | |
| 1882 | return res; |
| 1883 | } |
| 1884 | |
| 1885 | static const HashMap<String, Dictionary> structure_examples = make_structure_samples(); |
| 1886 | |
| 1887 | static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::ExpressionNode *p_expression, GDScriptCompletionIdentifier &r_type) { |
| 1888 | bool found = false; |
| 1889 | |
| 1890 | if (p_expression == nullptr) { |
| 1891 | return false; |
| 1892 | } |
| 1893 | |
| 1894 | static int recursion_depth = 0; |
| 1895 | RecursionCheck recursion(&recursion_depth); |
| 1896 | if (unlikely(recursion.check())) { |
| 1897 | ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type."); |
| 1898 | } |
| 1899 | |
| 1900 | if (p_expression->is_constant) { |
| 1901 | // Already has a value, so just use that. |
| 1902 | r_type = _type_from_variant(p_expression->reduced_value, p_context); |
| 1903 | switch (p_expression->get_datatype().kind) { |
| 1904 | case GDScriptParser::DataType::ENUM: |
| 1905 | case GDScriptParser::DataType::CLASS: |
| 1906 | r_type.type = p_expression->get_datatype(); |
| 1907 | break; |
| 1908 | default: |
| 1909 | break; |
| 1910 | } |
| 1911 | found = true; |
| 1912 | } else { |
| 1913 | switch (p_expression->type) { |
| 1914 | case GDScriptParser::Node::IDENTIFIER: { |
| 1915 | const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression); |
| 1916 | found = _guess_identifier_type(p_context, id, r_type); |
| 1917 | } break; |
| 1918 | case GDScriptParser::Node::DICTIONARY: { |
| 1919 | // Try to recreate the dictionary. |
| 1920 | const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression); |
| 1921 | Dictionary d; |
| 1922 | bool full = true; |
| 1923 | for (int i = 0; i < dn->elements.size(); i++) { |
| 1924 | GDScriptCompletionIdentifier key; |
| 1925 | if (_guess_expression_type(p_context, dn->elements[i].key, key)) { |
| 1926 | if (!key.type.is_constant) { |
| 1927 | full = false; |
| 1928 | break; |
| 1929 | } |
| 1930 | GDScriptCompletionIdentifier value; |
| 1931 | if (_guess_expression_type(p_context, dn->elements[i].value, value)) { |
| 1932 | if (!value.type.is_constant) { |
| 1933 | full = false; |
| 1934 | break; |
| 1935 | } |
| 1936 | d[key.value] = value.value; |
| 1937 | } else { |
| 1938 | full = false; |
| 1939 | break; |
| 1940 | } |
| 1941 | } else { |
| 1942 | full = false; |
| 1943 | break; |
| 1944 | } |
| 1945 | } |
| 1946 | if (full) { |
| 1947 | r_type.value = d; |
| 1948 | r_type.type.is_constant = true; |
| 1949 | } |
| 1950 | r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 1951 | r_type.type.kind = GDScriptParser::DataType::BUILTIN; |
| 1952 | r_type.type.builtin_type = Variant::DICTIONARY; |
| 1953 | found = true; |
| 1954 | } break; |
| 1955 | case GDScriptParser::Node::ARRAY: { |
| 1956 | // Try to recreate the array |
| 1957 | const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression); |
| 1958 | Array a; |
| 1959 | bool full = true; |
| 1960 | a.resize(an->elements.size()); |
| 1961 | for (int i = 0; i < an->elements.size(); i++) { |
| 1962 | GDScriptCompletionIdentifier value; |
| 1963 | if (_guess_expression_type(p_context, an->elements[i], value)) { |
| 1964 | if (value.type.is_constant) { |
| 1965 | a[i] = value.value; |
| 1966 | } else { |
| 1967 | full = false; |
| 1968 | break; |
| 1969 | } |
| 1970 | } else { |
| 1971 | full = false; |
| 1972 | break; |
| 1973 | } |
| 1974 | } |
| 1975 | if (full) { |
| 1976 | // If not fully constant, setting this value is detrimental to the inference. |
| 1977 | r_type.value = a; |
| 1978 | r_type.type.is_constant = true; |
| 1979 | } |
| 1980 | r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 1981 | r_type.type.kind = GDScriptParser::DataType::BUILTIN; |
| 1982 | r_type.type.builtin_type = Variant::ARRAY; |
| 1983 | found = true; |
| 1984 | } break; |
| 1985 | case GDScriptParser::Node::CAST: { |
| 1986 | const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression); |
| 1987 | GDScriptCompletionIdentifier value; |
| 1988 | if (_guess_expression_type(p_context, cn->operand, r_type)) { |
| 1989 | r_type.type = cn->get_datatype(); |
| 1990 | found = true; |
| 1991 | } |
| 1992 | } break; |
| 1993 | case GDScriptParser::Node::CALL: { |
| 1994 | const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression); |
| 1995 | GDScriptParser::CompletionContext c = p_context; |
| 1996 | c.current_line = call->start_line; |
| 1997 | |
| 1998 | GDScriptParser::Node::Type callee_type = call->get_callee_type(); |
| 1999 | |
| 2000 | GDScriptCompletionIdentifier base; |
| 2001 | if (callee_type == GDScriptParser::Node::IDENTIFIER || call->is_super) { |
| 2002 | // Simple call, so base is 'self'. |
| 2003 | if (p_context.current_class) { |
| 2004 | if (call->is_super) { |
| 2005 | base.type = p_context.current_class->base_type; |
| 2006 | base.value = p_context.base; |
| 2007 | } else { |
| 2008 | base.type.kind = GDScriptParser::DataType::CLASS; |
| 2009 | base.type.type_source = GDScriptParser::DataType::INFERRED; |
| 2010 | base.type.is_constant = true; |
| 2011 | base.type.class_type = p_context.current_class; |
| 2012 | base.value = p_context.base; |
| 2013 | } |
| 2014 | } else { |
| 2015 | break; |
| 2016 | } |
| 2017 | } else if (callee_type == GDScriptParser::Node::SUBSCRIPT && static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->is_attribute) { |
| 2018 | if (!_guess_expression_type(c, static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->base, base)) { |
| 2019 | found = false; |
| 2020 | break; |
| 2021 | } |
| 2022 | } else { |
| 2023 | break; |
| 2024 | } |
| 2025 | |
| 2026 | // Apply additional behavior aware inference that the analyzer can't do. |
| 2027 | if (base.type.is_set()) { |
| 2028 | // Maintain type for duplicate methods. |
| 2029 | if (call->function_name == SNAME("duplicate")) { |
| 2030 | if (base.type.builtin_type == Variant::OBJECT && (ClassDB::is_parent_class(base.type.native_type, SNAME("Resource")) || ClassDB::is_parent_class(base.type.native_type, SNAME("Node")))) { |
| 2031 | r_type.type = base.type; |
| 2032 | found = true; |
| 2033 | break; |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | // Simulate generics for some typed array methods. |
| 2038 | if (base.type.builtin_type == Variant::ARRAY && base.type.has_container_element_types() && (call->function_name == SNAME("back") || call->function_name == SNAME("front") || call->function_name == SNAME("get") || call->function_name == SNAME("max") || call->function_name == SNAME("min") || call->function_name == SNAME("pick_random") || call->function_name == SNAME("pop_at") || call->function_name == SNAME("pop_back") || call->function_name == SNAME("pop_front"))) { |
| 2039 | r_type.type = base.type.get_container_element_type(0); |
| 2040 | found = true; |
| 2041 | break; |
| 2042 | } |
| 2043 | |
| 2044 | // Insert example values for functions which a structured dictionary response. |
| 2045 | if (!base.type.is_meta_type) { |
| 2046 | const Dictionary *example = structure_examples.getptr(base.type.native_type.operator String() + "::" + call->function_name); |
| 2047 | if (example != nullptr) { |
| 2048 | r_type = _type_from_variant(*example, p_context); |
| 2049 | found = true; |
| 2050 | break; |
| 2051 | } |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | if (!found) { |
| 2056 | found = _guess_method_return_type_from_base(c, base, call->function_name, r_type); |
| 2057 | } |
| 2058 | } break; |
| 2059 | case GDScriptParser::Node::SUBSCRIPT: { |
| 2060 | const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression); |
| 2061 | if (subscript->is_attribute) { |
| 2062 | GDScriptParser::CompletionContext c = p_context; |
| 2063 | c.current_line = subscript->start_line; |
| 2064 | |
| 2065 | GDScriptCompletionIdentifier base; |
| 2066 | if (!_guess_expression_type(c, subscript->base, base)) { |
| 2067 | found = false; |
| 2068 | break; |
| 2069 | } |
| 2070 | |
| 2071 | if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(subscript->attribute->name))) { |
| 2072 | Variant value = base.value.operator Dictionary()[String(subscript->attribute->name)]; |
| 2073 | r_type = _type_from_variant(value, p_context); |
| 2074 | found = true; |
| 2075 | break; |
| 2076 | } |
| 2077 | |
| 2078 | const GDScriptParser::DictionaryNode *dn = nullptr; |
| 2079 | if (subscript->base->type == GDScriptParser::Node::DICTIONARY) { |
| 2080 | dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base); |
| 2081 | } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) { |
| 2082 | dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression); |
| 2083 | } |
| 2084 | |
| 2085 | if (dn) { |
| 2086 | for (int i = 0; i < dn->elements.size(); i++) { |
| 2087 | GDScriptCompletionIdentifier key; |
| 2088 | if (!_guess_expression_type(c, dn->elements[i].key, key)) { |
| 2089 | continue; |
| 2090 | } |
| 2091 | if (key.value == String(subscript->attribute->name)) { |
| 2092 | r_type.assigned_expression = dn->elements[i].value; |
| 2093 | found = _guess_expression_type(c, dn->elements[i].value, r_type); |
| 2094 | break; |
| 2095 | } |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | if (!found) { |
| 2100 | found = _guess_identifier_type_from_base(c, base, subscript->attribute->name, r_type); |
| 2101 | } |
| 2102 | } else { |
| 2103 | if (subscript->index == nullptr) { |
| 2104 | found = false; |
| 2105 | break; |
| 2106 | } |
| 2107 | |
| 2108 | GDScriptParser::CompletionContext c = p_context; |
| 2109 | c.current_line = subscript->start_line; |
| 2110 | |
| 2111 | GDScriptCompletionIdentifier base; |
| 2112 | if (!_guess_expression_type(c, subscript->base, base)) { |
| 2113 | found = false; |
| 2114 | break; |
| 2115 | } |
| 2116 | |
| 2117 | GDScriptCompletionIdentifier index; |
| 2118 | if (!_guess_expression_type(c, subscript->index, index)) { |
| 2119 | found = false; |
| 2120 | break; |
| 2121 | } |
| 2122 | |
| 2123 | if (base.type.is_constant && index.type.is_constant) { |
| 2124 | if (base.value.get_type() == Variant::DICTIONARY) { |
| 2125 | Dictionary base_dict = base.value.operator Dictionary(); |
| 2126 | if (base_dict.get_key_validator().test_validate(index.value) && base_dict.has(index.value)) { |
| 2127 | r_type = _type_from_variant(base_dict[index.value], p_context); |
| 2128 | found = true; |
| 2129 | break; |
| 2130 | } |
| 2131 | } else { |
| 2132 | bool valid; |
| 2133 | Variant value = base.value.get(index.value, &valid); |
| 2134 | if (valid) { |
| 2135 | r_type = _type_from_variant(value, p_context); |
| 2136 | found = true; |
| 2137 | break; |
| 2138 | } |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | // Look if it is a dictionary node. |
| 2143 | const GDScriptParser::DictionaryNode *dn = nullptr; |
| 2144 | if (subscript->base->type == GDScriptParser::Node::DICTIONARY) { |
| 2145 | dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base); |
| 2146 | } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) { |
| 2147 | dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression); |
| 2148 | } |
| 2149 | |
| 2150 | if (dn) { |
| 2151 | for (int i = 0; i < dn->elements.size(); i++) { |
| 2152 | GDScriptCompletionIdentifier key; |
| 2153 | if (!_guess_expression_type(c, dn->elements[i].key, key)) { |
| 2154 | continue; |
| 2155 | } |
| 2156 | if (key.value == index.value) { |
| 2157 | r_type.assigned_expression = dn->elements[i].value; |
| 2158 | found = _guess_expression_type(p_context, dn->elements[i].value, r_type); |
| 2159 | break; |
| 2160 | } |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | // Look if it is an array node. |
| 2165 | if (!found && index.value.is_num()) { |
| 2166 | int idx = index.value; |
| 2167 | const GDScriptParser::ArrayNode *an = nullptr; |
| 2168 | if (subscript->base->type == GDScriptParser::Node::ARRAY) { |
| 2169 | an = static_cast<const GDScriptParser::ArrayNode *>(subscript->base); |
| 2170 | } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::ARRAY) { |
| 2171 | an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression); |
| 2172 | } |
| 2173 | |
| 2174 | if (an && idx >= 0 && an->elements.size() > idx) { |
| 2175 | r_type.assigned_expression = an->elements[idx]; |
| 2176 | found = _guess_expression_type(c, an->elements[idx], r_type); |
| 2177 | break; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | // Look for valid indexing in other types |
| 2182 | if (!found && (index.value.is_string() || index.value.get_type() == Variant::NODE_PATH)) { |
| 2183 | StringName id = index.value; |
| 2184 | found = _guess_identifier_type_from_base(c, base, id, r_type); |
| 2185 | } else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) { |
| 2186 | Callable::CallError err; |
| 2187 | Variant base_val; |
| 2188 | Variant::construct(base.type.builtin_type, base_val, nullptr, 0, err); |
| 2189 | bool valid = false; |
| 2190 | Variant res = base_val.get(index.value, &valid); |
| 2191 | if (valid) { |
| 2192 | r_type = _type_from_variant(res, p_context); |
| 2193 | r_type.value = Variant(); |
| 2194 | r_type.type.is_constant = false; |
| 2195 | found = true; |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | } break; |
| 2200 | case GDScriptParser::Node::BINARY_OPERATOR: { |
| 2201 | const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression); |
| 2202 | |
| 2203 | if (op->variant_op == Variant::OP_MAX) { |
| 2204 | break; |
| 2205 | } |
| 2206 | |
| 2207 | GDScriptParser::CompletionContext context = p_context; |
| 2208 | context.current_line = op->start_line; |
| 2209 | |
| 2210 | GDScriptCompletionIdentifier p1; |
| 2211 | GDScriptCompletionIdentifier p2; |
| 2212 | |
| 2213 | if (!_guess_expression_type(context, op->left_operand, p1)) { |
| 2214 | found = false; |
| 2215 | break; |
| 2216 | } |
| 2217 | |
| 2218 | if (!_guess_expression_type(context, op->right_operand, p2)) { |
| 2219 | found = false; |
| 2220 | break; |
| 2221 | } |
| 2222 | |
| 2223 | Callable::CallError ce; |
| 2224 | bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT; |
| 2225 | Variant d1; |
| 2226 | Variant::construct(p1.type.builtin_type, d1, nullptr, 0, ce); |
| 2227 | Variant d2; |
| 2228 | Variant::construct(p2.type.builtin_type, d2, nullptr, 0, ce); |
| 2229 | |
| 2230 | Variant v1 = (v1_use_value) ? p1.value : d1; |
| 2231 | bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT; |
| 2232 | Variant v2 = (v2_use_value) ? p2.value : d2; |
| 2233 | // avoid potential invalid ops |
| 2234 | if ((op->variant_op == Variant::OP_DIVIDE || op->variant_op == Variant::OP_MODULE) && v2.get_type() == Variant::INT) { |
| 2235 | v2 = 1; |
| 2236 | v2_use_value = false; |
| 2237 | } |
| 2238 | if (op->variant_op == Variant::OP_DIVIDE && v2.get_type() == Variant::FLOAT) { |
| 2239 | v2 = 1.0; |
| 2240 | v2_use_value = false; |
| 2241 | } |
| 2242 | |
| 2243 | Variant res; |
| 2244 | bool valid; |
| 2245 | Variant::evaluate(op->variant_op, v1, v2, res, valid); |
| 2246 | if (!valid) { |
| 2247 | found = false; |
| 2248 | break; |
| 2249 | } |
| 2250 | r_type = _type_from_variant(res, p_context); |
| 2251 | if (!v1_use_value || !v2_use_value) { |
| 2252 | r_type.value = Variant(); |
| 2253 | r_type.type.is_constant = false; |
| 2254 | } |
| 2255 | |
| 2256 | found = true; |
| 2257 | } break; |
| 2258 | default: |
| 2259 | break; |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | // It may have found a null, but that's never useful |
| 2264 | if (found && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) { |
| 2265 | found = false; |
| 2266 | } |
| 2267 | |
| 2268 | // If the found type was not fully analyzed we analyze it now. |
| 2269 | if (found && r_type.type.kind == GDScriptParser::DataType::CLASS && !r_type.type.class_type->resolved_body) { |
| 2270 | Error err; |
| 2271 | Ref<GDScriptParserRef> r = GDScriptCache::get_parser(r_type.type.script_path, GDScriptParserRef::FULLY_SOLVED, err); |
| 2272 | } |
| 2273 | |
| 2274 | // Check type hint last. For collections we want chance to get the actual value first |
| 2275 | // This way we can detect types from the content of dictionaries and arrays |
| 2276 | if (!found && p_expression->get_datatype().is_hard_type()) { |
| 2277 | r_type.type = p_expression->get_datatype(); |
| 2278 | if (!r_type.assigned_expression) { |
| 2279 | r_type.assigned_expression = p_expression; |
| 2280 | } |
| 2281 | found = true; |
| 2282 | } |
| 2283 | |
| 2284 | return found; |
| 2285 | } |
| 2286 | |
| 2287 | static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::IdentifierNode *p_identifier, GDScriptCompletionIdentifier &r_type) { |
| 2288 | static int recursion_depth = 0; |
| 2289 | RecursionCheck recursion(&recursion_depth); |
| 2290 | if (unlikely(recursion.check())) { |
| 2291 | ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type."); |
| 2292 | } |
| 2293 | |
| 2294 | // Look in blocks first. |
| 2295 | int last_assign_line = -1; |
| 2296 | const GDScriptParser::ExpressionNode *last_assigned_expression = nullptr; |
| 2297 | GDScriptCompletionIdentifier id_type; |
| 2298 | GDScriptParser::SuiteNode *suite = p_context.current_suite; |
| 2299 | bool is_function_parameter = false; |
| 2300 | |
| 2301 | bool can_be_local = true; |
| 2302 | switch (p_identifier->source) { |
| 2303 | case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: |
| 2304 | case GDScriptParser::IdentifierNode::MEMBER_CONSTANT: |
| 2305 | case GDScriptParser::IdentifierNode::MEMBER_FUNCTION: |
| 2306 | case GDScriptParser::IdentifierNode::MEMBER_SIGNAL: |
| 2307 | case GDScriptParser::IdentifierNode::MEMBER_CLASS: |
| 2308 | case GDScriptParser::IdentifierNode::INHERITED_VARIABLE: |
| 2309 | case GDScriptParser::IdentifierNode::STATIC_VARIABLE: |
| 2310 | case GDScriptParser::IdentifierNode::NATIVE_CLASS: |
| 2311 | can_be_local = false; |
| 2312 | break; |
| 2313 | default: |
| 2314 | break; |
| 2315 | } |
| 2316 | |
| 2317 | if (can_be_local && suite && suite->has_local(p_identifier->name)) { |
| 2318 | const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_identifier->name); |
| 2319 | |
| 2320 | id_type.type = local.get_datatype(); |
| 2321 | |
| 2322 | // Check initializer as the first assignment. |
| 2323 | switch (local.type) { |
| 2324 | case GDScriptParser::SuiteNode::Local::VARIABLE: |
| 2325 | if (local.variable->initializer) { |
| 2326 | last_assign_line = local.variable->initializer->end_line; |
| 2327 | last_assigned_expression = local.variable->initializer; |
| 2328 | } |
| 2329 | break; |
| 2330 | case GDScriptParser::SuiteNode::Local::CONSTANT: |
| 2331 | if (local.constant->initializer) { |
| 2332 | last_assign_line = local.constant->initializer->end_line; |
| 2333 | last_assigned_expression = local.constant->initializer; |
| 2334 | } |
| 2335 | break; |
| 2336 | case GDScriptParser::SuiteNode::Local::PARAMETER: |
| 2337 | if (local.parameter->initializer) { |
| 2338 | last_assign_line = local.parameter->initializer->end_line; |
| 2339 | last_assigned_expression = local.parameter->initializer; |
| 2340 | } |
| 2341 | is_function_parameter = true; |
| 2342 | break; |
| 2343 | default: |
| 2344 | break; |
| 2345 | } |
| 2346 | } else { |
| 2347 | if (p_context.current_class) { |
| 2348 | GDScriptCompletionIdentifier base_identifier; |
| 2349 | |
| 2350 | GDScriptCompletionIdentifier base; |
| 2351 | base.value = p_context.base; |
| 2352 | base.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 2353 | base.type.kind = GDScriptParser::DataType::CLASS; |
| 2354 | base.type.class_type = p_context.current_class; |
| 2355 | base.type.is_meta_type = p_context.current_function && p_context.current_function->is_static; |
| 2356 | |
| 2357 | if (_guess_identifier_type_from_base(p_context, base, p_identifier->name, base_identifier)) { |
| 2358 | id_type = base_identifier; |
| 2359 | } |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | while (suite) { |
| 2364 | for (int i = 0; i < suite->statements.size(); i++) { |
| 2365 | if (suite->statements[i]->end_line >= p_context.current_line) { |
| 2366 | break; |
| 2367 | } |
| 2368 | |
| 2369 | switch (suite->statements[i]->type) { |
| 2370 | case GDScriptParser::Node::ASSIGNMENT: { |
| 2371 | const GDScriptParser::AssignmentNode *assign = static_cast<const GDScriptParser::AssignmentNode *>(suite->statements[i]); |
| 2372 | if (assign->end_line > last_assign_line && assign->assignee && assign->assigned_value && assign->assignee->type == GDScriptParser::Node::IDENTIFIER) { |
| 2373 | const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->assignee); |
| 2374 | if (id->name == p_identifier->name && id->source == p_identifier->source) { |
| 2375 | last_assign_line = assign->assigned_value->end_line; |
| 2376 | last_assigned_expression = assign->assigned_value; |
| 2377 | } |
| 2378 | } |
| 2379 | } break; |
| 2380 | default: |
| 2381 | // TODO: Check sub blocks (control flow statements) as they might also reassign stuff. |
| 2382 | break; |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | if (suite->parent_if && suite->parent_if->condition && suite->parent_if->condition->type == GDScriptParser::Node::TYPE_TEST) { |
| 2387 | // Operator `is` used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common.. |
| 2388 | // Super dirty hack, but very useful. |
| 2389 | // Credit: Zylann. |
| 2390 | // TODO: this could be hacked to detect AND-ed conditions too... |
| 2391 | const GDScriptParser::TypeTestNode *type_test = static_cast<const GDScriptParser::TypeTestNode *>(suite->parent_if->condition); |
| 2392 | if (type_test->operand && type_test->test_type && type_test->operand->type == GDScriptParser::Node::IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(type_test->operand)->name == p_identifier->name && static_cast<const GDScriptParser::IdentifierNode *>(type_test->operand)->source == p_identifier->source) { |
| 2393 | // Bingo. |
| 2394 | GDScriptParser::CompletionContext c = p_context; |
| 2395 | c.current_line = type_test->operand->start_line; |
| 2396 | c.current_suite = suite; |
| 2397 | if (type_test->test_datatype.is_hard_type()) { |
| 2398 | id_type.type = type_test->test_datatype; |
| 2399 | if (last_assign_line < c.current_line) { |
| 2400 | // Override last assignment. |
| 2401 | last_assign_line = c.current_line; |
| 2402 | last_assigned_expression = nullptr; |
| 2403 | } |
| 2404 | } |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | suite = suite->parent_block; |
| 2409 | } |
| 2410 | |
| 2411 | if (last_assigned_expression && last_assign_line < p_context.current_line) { |
| 2412 | GDScriptParser::CompletionContext c = p_context; |
| 2413 | c.current_line = last_assign_line; |
| 2414 | GDScriptCompletionIdentifier assigned_type; |
| 2415 | if (_guess_expression_type(c, last_assigned_expression, assigned_type)) { |
| 2416 | if (id_type.type.is_set() && (assigned_type.type.kind == GDScriptParser::DataType::VARIANT || (assigned_type.type.is_set() && !GDScriptAnalyzer::check_type_compatibility(id_type.type, assigned_type.type)))) { |
| 2417 | // The assigned type is incompatible. The annotated type takes priority. |
| 2418 | r_type = id_type; |
| 2419 | r_type.assigned_expression = last_assigned_expression; |
| 2420 | } else { |
| 2421 | r_type = assigned_type; |
| 2422 | } |
| 2423 | return true; |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | if (is_function_parameter && p_context.current_function && p_context.current_function->source_lambda == nullptr && p_context.current_class) { |
| 2428 | // Check if it's override of native function, then we can assume the type from the signature. |
| 2429 | GDScriptParser::DataType base_type = p_context.current_class->base_type; |
| 2430 | while (base_type.is_set()) { |
| 2431 | switch (base_type.kind) { |
| 2432 | case GDScriptParser::DataType::CLASS: |
| 2433 | if (base_type.class_type->has_function(p_context.current_function->identifier->name)) { |
| 2434 | GDScriptParser::FunctionNode *parent_function = base_type.class_type->get_member(p_context.current_function->identifier->name).function; |
| 2435 | if (parent_function->parameters_indices.has(p_identifier->name)) { |
| 2436 | const GDScriptParser::ParameterNode *parameter = parent_function->parameters[parent_function->parameters_indices[p_identifier->name]]; |
| 2437 | if ((!id_type.type.is_set() || id_type.type.is_variant()) && parameter->get_datatype().is_hard_type()) { |
| 2438 | id_type.type = parameter->get_datatype(); |
| 2439 | } |
| 2440 | if (parameter->initializer) { |
| 2441 | GDScriptParser::CompletionContext c = p_context; |
| 2442 | c.current_function = parent_function; |
| 2443 | c.current_class = base_type.class_type; |
| 2444 | c.base = nullptr; |
| 2445 | if (_guess_expression_type(c, parameter->initializer, r_type)) { |
| 2446 | return true; |
| 2447 | } |
| 2448 | } |
| 2449 | } |
| 2450 | } |
| 2451 | base_type = base_type.class_type->base_type; |
| 2452 | break; |
| 2453 | case GDScriptParser::DataType::NATIVE: { |
| 2454 | if (id_type.type.is_set() && !id_type.type.is_variant()) { |
| 2455 | base_type = GDScriptParser::DataType(); |
| 2456 | break; |
| 2457 | } |
| 2458 | MethodInfo info; |
| 2459 | if (ClassDB::get_method_info(base_type.native_type, p_context.current_function->identifier->name, &info)) { |
| 2460 | for (const PropertyInfo &E : info.arguments) { |
| 2461 | if (E.name == p_identifier->name) { |
| 2462 | r_type = _type_from_property(E); |
| 2463 | return true; |
| 2464 | } |
| 2465 | } |
| 2466 | } |
| 2467 | base_type = GDScriptParser::DataType(); |
| 2468 | } break; |
| 2469 | default: |
| 2470 | break; |
| 2471 | } |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | if (id_type.type.is_set() && !id_type.type.is_variant()) { |
| 2476 | r_type = id_type; |
| 2477 | return true; |
| 2478 | } |
| 2479 | |
| 2480 | // Check global scripts. |
| 2481 | if (ScriptServer::is_global_class(p_identifier->name)) { |
| 2482 | String script = ScriptServer::get_global_class_path(p_identifier->name); |
| 2483 | if (script.to_lower().ends_with(".gd")) { |
| 2484 | Ref<GDScriptParserRef> parser = p_context.parser->get_depended_parser_for(script); |
| 2485 | if (parser.is_valid() && parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED) == OK) { |
| 2486 | r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 2487 | r_type.type.script_path = script; |
| 2488 | r_type.type.class_type = parser->get_parser()->get_tree(); |
| 2489 | r_type.type.is_meta_type = true; |
| 2490 | r_type.type.is_constant = false; |
| 2491 | r_type.type.kind = GDScriptParser::DataType::CLASS; |
| 2492 | r_type.value = Variant(); |
| 2493 | return true; |
| 2494 | } |
| 2495 | } else { |
| 2496 | Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier->name)); |
| 2497 | if (scr.is_valid()) { |
| 2498 | r_type = _type_from_variant(scr, p_context); |
| 2499 | r_type.type.is_meta_type = true; |
| 2500 | return true; |
| 2501 | } |
| 2502 | } |
| 2503 | return false; |
| 2504 | } |
| 2505 | |
| 2506 | // Check global variables (including autoloads). |
| 2507 | if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier->name)) { |
| 2508 | r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier->name], p_context); |
| 2509 | return true; |
| 2510 | } |
| 2511 | |
| 2512 | // Check ClassDB. |
| 2513 | if (GDScriptAnalyzer::class_exists(p_identifier->name)) { |
| 2514 | r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 2515 | r_type.type.kind = GDScriptParser::DataType::NATIVE; |
| 2516 | r_type.type.builtin_type = Variant::OBJECT; |
| 2517 | r_type.type.native_type = p_identifier->name; |
| 2518 | r_type.type.is_constant = true; |
| 2519 | if (Engine::get_singleton()->has_singleton(p_identifier->name)) { |
| 2520 | r_type.type.is_meta_type = false; |
| 2521 | r_type.value = Engine::get_singleton()->get_singleton_object(p_identifier->name); |
| 2522 | } else { |
| 2523 | r_type.type.is_meta_type = true; |
| 2524 | r_type.value = Variant(); |
| 2525 | } |
| 2526 | return true; |
| 2527 | } |
| 2528 | |
| 2529 | return false; |
| 2530 | } |
| 2531 | |
| 2532 | static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) { |
| 2533 | static int recursion_depth = 0; |
| 2534 | RecursionCheck recursion(&recursion_depth); |
| 2535 | if (unlikely(recursion.check())) { |
| 2536 | ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type."); |
| 2537 | } |
| 2538 | |
| 2539 | GDScriptParser::DataType base_type = p_base.type; |
| 2540 | bool is_static = base_type.is_meta_type; |
| 2541 | while (base_type.is_set()) { |
| 2542 | switch (base_type.kind) { |
| 2543 | case GDScriptParser::DataType::CLASS: |
| 2544 | if (base_type.class_type->has_member(p_identifier)) { |
| 2545 | const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_identifier); |
| 2546 | switch (member.type) { |
| 2547 | case GDScriptParser::ClassNode::Member::CONSTANT: |
| 2548 | r_type.type = member.constant->get_datatype(); |
| 2549 | if (member.constant->initializer && member.constant->initializer->is_constant) { |
| 2550 | r_type.value = member.constant->initializer->reduced_value; |
| 2551 | } |
| 2552 | return true; |
| 2553 | case GDScriptParser::ClassNode::Member::VARIABLE: |
| 2554 | if (!is_static || member.variable->is_static) { |
| 2555 | if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) { |
| 2556 | r_type.type = member.variable->get_datatype(); |
| 2557 | return true; |
| 2558 | } else if (member.variable->initializer) { |
| 2559 | const GDScriptParser::ExpressionNode *init = member.variable->initializer; |
| 2560 | if (init->is_constant) { |
| 2561 | r_type.value = init->reduced_value; |
| 2562 | r_type = _type_from_variant(init->reduced_value, p_context); |
| 2563 | return true; |
| 2564 | } else if (init->start_line == p_context.current_line) { |
| 2565 | return false; |
| 2566 | // Detects if variable is assigned to itself |
| 2567 | } else if (_is_expression_named_identifier(init, member.variable->identifier->name)) { |
| 2568 | if (member.variable->initializer->get_datatype().is_set()) { |
| 2569 | r_type.type = member.variable->initializer->get_datatype(); |
| 2570 | } else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) { |
| 2571 | r_type.type = member.variable->get_datatype(); |
| 2572 | } |
| 2573 | return true; |
| 2574 | } else if (_guess_expression_type(p_context, init, r_type)) { |
| 2575 | return true; |
| 2576 | } else if (init->get_datatype().is_set() && !init->get_datatype().is_variant()) { |
| 2577 | r_type.type = init->get_datatype(); |
| 2578 | return true; |
| 2579 | } |
| 2580 | } |
| 2581 | } |
| 2582 | // TODO: Check assignments in constructor. |
| 2583 | return false; |
| 2584 | case GDScriptParser::ClassNode::Member::ENUM: |
| 2585 | r_type.type = member.m_enum->get_datatype(); |
| 2586 | r_type.enumeration = member.m_enum->identifier->name; |
| 2587 | return true; |
| 2588 | case GDScriptParser::ClassNode::Member::ENUM_VALUE: |
| 2589 | r_type = _type_from_variant(member.enum_value.value, p_context); |
| 2590 | return true; |
| 2591 | case GDScriptParser::ClassNode::Member::SIGNAL: |
| 2592 | r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 2593 | r_type.type.kind = GDScriptParser::DataType::BUILTIN; |
| 2594 | r_type.type.builtin_type = Variant::SIGNAL; |
| 2595 | r_type.type.method_info = member.signal->method_info; |
| 2596 | return true; |
| 2597 | case GDScriptParser::ClassNode::Member::FUNCTION: |
| 2598 | if (is_static && !member.function->is_static) { |
| 2599 | return false; |
| 2600 | } |
| 2601 | r_type = _callable_type_from_method_info(member.function->info); |
| 2602 | return true; |
| 2603 | case GDScriptParser::ClassNode::Member::CLASS: |
| 2604 | r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 2605 | r_type.type.kind = GDScriptParser::DataType::CLASS; |
| 2606 | r_type.type.class_type = member.m_class; |
| 2607 | r_type.type.is_meta_type = true; |
| 2608 | return true; |
| 2609 | case GDScriptParser::ClassNode::Member::GROUP: |
| 2610 | return false; // No-op, but silences warnings. |
| 2611 | case GDScriptParser::ClassNode::Member::UNDEFINED: |
| 2612 | return false; // Unreachable. |
| 2613 | } |
| 2614 | return false; |
| 2615 | } |
| 2616 | base_type = base_type.class_type->base_type; |
| 2617 | break; |
| 2618 | case GDScriptParser::DataType::SCRIPT: { |
| 2619 | Ref<Script> scr = base_type.script_type; |
| 2620 | if (scr.is_valid()) { |
| 2621 | HashMap<StringName, Variant> constants; |
| 2622 | scr->get_constants(&constants); |
| 2623 | if (constants.has(p_identifier)) { |
| 2624 | r_type = _type_from_variant(constants[p_identifier], p_context); |
| 2625 | return true; |
| 2626 | } |
| 2627 | |
| 2628 | List<PropertyInfo> members; |
| 2629 | if (is_static) { |
| 2630 | scr->get_property_list(&members); |
| 2631 | } else { |
| 2632 | scr->get_script_property_list(&members); |
| 2633 | } |
| 2634 | for (const PropertyInfo &prop : members) { |
| 2635 | if (prop.name == p_identifier) { |
| 2636 | r_type = _type_from_property(prop); |
| 2637 | return true; |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | if (scr->has_method(p_identifier)) { |
| 2642 | MethodInfo mi = scr->get_method_info(p_identifier); |
| 2643 | r_type = _callable_type_from_method_info(mi); |
| 2644 | return true; |
| 2645 | } |
| 2646 | |
| 2647 | Ref<Script> parent = scr->get_base_script(); |
| 2648 | if (parent.is_valid()) { |
| 2649 | base_type.script_type = parent; |
| 2650 | } else { |
| 2651 | base_type.kind = GDScriptParser::DataType::NATIVE; |
| 2652 | base_type.builtin_type = Variant::OBJECT; |
| 2653 | base_type.native_type = scr->get_instance_base_type(); |
| 2654 | } |
| 2655 | } else { |
| 2656 | return false; |
| 2657 | } |
| 2658 | } break; |
| 2659 | case GDScriptParser::DataType::NATIVE: { |
| 2660 | StringName class_name = base_type.native_type; |
| 2661 | if (!GDScriptAnalyzer::class_exists(class_name)) { |
| 2662 | return false; |
| 2663 | } |
| 2664 | |
| 2665 | // Skip constants since they're all integers. Type does not matter because int has no members. |
| 2666 | |
| 2667 | PropertyInfo prop; |
| 2668 | if (ClassDB::get_property_info(class_name, p_identifier, &prop)) { |
| 2669 | StringName getter = ClassDB::get_property_getter(class_name, p_identifier); |
| 2670 | if (getter != StringName()) { |
| 2671 | MethodBind *g = ClassDB::get_method(class_name, getter); |
| 2672 | if (g) { |
| 2673 | r_type = _type_from_property(g->get_return_info()); |
| 2674 | return true; |
| 2675 | } |
| 2676 | } else { |
| 2677 | r_type = _type_from_property(prop); |
| 2678 | return true; |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | MethodInfo method; |
| 2683 | if (ClassDB::get_method_info(class_name, p_identifier, &method)) { |
| 2684 | r_type = _callable_type_from_method_info(method); |
| 2685 | return true; |
| 2686 | } |
| 2687 | |
| 2688 | if (ClassDB::has_enum(class_name, p_identifier)) { |
| 2689 | r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; |
| 2690 | r_type.type.kind = GDScriptParser::DataType::ENUM; |
| 2691 | r_type.type.enum_type = p_identifier; |
| 2692 | r_type.type.is_constant = true; |
| 2693 | r_type.type.is_meta_type = true; |
| 2694 | r_type.type.native_type = String(class_name) + "." + p_identifier; |
| 2695 | return true; |
| 2696 | } |
| 2697 | |
| 2698 | return false; |
| 2699 | } break; |
| 2700 | case GDScriptParser::DataType::BUILTIN: { |
| 2701 | if (Variant::has_builtin_method(base_type.builtin_type, p_identifier)) { |
| 2702 | r_type = _callable_type_from_method_info(Variant::get_builtin_method_info(base_type.builtin_type, p_identifier)); |
| 2703 | return true; |
| 2704 | } else { |
| 2705 | Callable::CallError err; |
| 2706 | Variant tmp; |
| 2707 | Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err); |
| 2708 | |
| 2709 | if (err.error != Callable::CallError::CALL_OK) { |
| 2710 | return false; |
| 2711 | } |
| 2712 | bool valid = false; |
| 2713 | Variant res = tmp.get(p_identifier, &valid); |
| 2714 | if (valid) { |
| 2715 | r_type = _type_from_variant(res, p_context); |
| 2716 | r_type.value = Variant(); |
| 2717 | r_type.type.is_constant = false; |
| 2718 | return true; |
| 2719 | } |
| 2720 | } |
| 2721 | return false; |
| 2722 | } break; |
| 2723 | default: { |
| 2724 | return false; |
| 2725 | } break; |
| 2726 | } |
| 2727 | } |
| 2728 | return false; |
| 2729 | } |
| 2730 | |
| 2731 | static void _find_last_return_in_block(GDScriptParser::CompletionContext &p_context, int &r_last_return_line, const GDScriptParser::ExpressionNode **r_last_returned_value) { |
| 2732 | if (!p_context.current_suite) { |
| 2733 | return; |
| 2734 | } |
| 2735 | |
| 2736 | for (int i = 0; i < p_context.current_suite->statements.size(); i++) { |
| 2737 | if (p_context.current_suite->statements[i]->start_line < r_last_return_line) { |
| 2738 | break; |
| 2739 | } |
| 2740 | |
| 2741 | GDScriptParser::CompletionContext c = p_context; |
| 2742 | switch (p_context.current_suite->statements[i]->type) { |
| 2743 | case GDScriptParser::Node::FOR: |
| 2744 | c.current_suite = static_cast<const GDScriptParser::ForNode *>(p_context.current_suite->statements[i])->loop; |
| 2745 | _find_last_return_in_block(c, r_last_return_line, r_last_returned_value); |
| 2746 | break; |
| 2747 | case GDScriptParser::Node::WHILE: |
| 2748 | c.current_suite = static_cast<const GDScriptParser::WhileNode *>(p_context.current_suite->statements[i])->loop; |
| 2749 | _find_last_return_in_block(c, r_last_return_line, r_last_returned_value); |
| 2750 | break; |
| 2751 | case GDScriptParser::Node::IF: { |
| 2752 | const GDScriptParser::IfNode *_if = static_cast<const GDScriptParser::IfNode *>(p_context.current_suite->statements[i]); |
| 2753 | c.current_suite = _if->true_block; |
| 2754 | _find_last_return_in_block(c, r_last_return_line, r_last_returned_value); |
| 2755 | if (_if->false_block) { |
| 2756 | c.current_suite = _if->false_block; |
| 2757 | _find_last_return_in_block(c, r_last_return_line, r_last_returned_value); |
| 2758 | } |
| 2759 | } break; |
| 2760 | case GDScriptParser::Node::MATCH: { |
| 2761 | const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(p_context.current_suite->statements[i]); |
| 2762 | for (int j = 0; j < match->branches.size(); j++) { |
| 2763 | c.current_suite = match->branches[j]->block; |
| 2764 | _find_last_return_in_block(c, r_last_return_line, r_last_returned_value); |
| 2765 | } |
| 2766 | } break; |
| 2767 | case GDScriptParser::Node::RETURN: { |
| 2768 | const GDScriptParser::ReturnNode *ret = static_cast<const GDScriptParser::ReturnNode *>(p_context.current_suite->statements[i]); |
| 2769 | if (ret->return_value) { |
| 2770 | if (ret->start_line > r_last_return_line) { |
| 2771 | r_last_return_line = ret->start_line; |
| 2772 | *r_last_returned_value = ret->return_value; |
| 2773 | } |
| 2774 | } |
| 2775 | } break; |
| 2776 | default: |
| 2777 | break; |
| 2778 | } |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) { |
| 2783 | static int recursion_depth = 0; |
| 2784 | RecursionCheck recursion(&recursion_depth); |
| 2785 | if (unlikely(recursion.check())) { |
| 2786 | ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type."); |
| 2787 | } |
| 2788 | |
| 2789 | GDScriptParser::DataType base_type = p_base.type; |
| 2790 | bool is_static = base_type.is_meta_type; |
| 2791 | |
| 2792 | if (is_static && p_method == SNAME("new")) { |
| 2793 | r_type.type = base_type; |
| 2794 | r_type.type.is_meta_type = false; |
| 2795 | r_type.type.is_constant = false; |
| 2796 | return true; |
| 2797 | } |
| 2798 | |
| 2799 | while (base_type.is_set() && !base_type.is_variant()) { |
| 2800 | switch (base_type.kind) { |
| 2801 | case GDScriptParser::DataType::CLASS: |
| 2802 | if (base_type.class_type->has_function(p_method)) { |
| 2803 | GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function; |
| 2804 | if (!is_static || method->is_static) { |
| 2805 | if (method->get_datatype().is_set() && !method->get_datatype().is_variant()) { |
| 2806 | r_type.type = method->get_datatype(); |
| 2807 | return true; |
| 2808 | } |
| 2809 | |
| 2810 | int last_return_line = -1; |
| 2811 | const GDScriptParser::ExpressionNode *last_returned_value = nullptr; |
| 2812 | GDScriptParser::CompletionContext c = p_context; |
| 2813 | c.current_class = base_type.class_type; |
| 2814 | c.current_function = method; |
| 2815 | c.current_suite = method->body; |
| 2816 | |
| 2817 | _find_last_return_in_block(c, last_return_line, &last_returned_value); |
| 2818 | if (last_returned_value) { |
| 2819 | c.current_line = c.current_suite->end_line; |
| 2820 | if (_guess_expression_type(c, last_returned_value, r_type)) { |
| 2821 | return true; |
| 2822 | } |
| 2823 | } |
| 2824 | } |
| 2825 | } |
| 2826 | base_type = base_type.class_type->base_type; |
| 2827 | break; |
| 2828 | case GDScriptParser::DataType::SCRIPT: { |
| 2829 | Ref<Script> scr = base_type.script_type; |
| 2830 | if (scr.is_valid()) { |
| 2831 | List<MethodInfo> methods; |
| 2832 | scr->get_script_method_list(&methods); |
| 2833 | for (const MethodInfo &mi : methods) { |
| 2834 | if (mi.name == p_method) { |
| 2835 | r_type = _type_from_property(mi.return_val); |
| 2836 | return true; |
| 2837 | } |
| 2838 | } |
| 2839 | Ref<Script> base_script = scr->get_base_script(); |
| 2840 | if (base_script.is_valid()) { |
| 2841 | base_type.script_type = base_script; |
| 2842 | } else { |
| 2843 | base_type.kind = GDScriptParser::DataType::NATIVE; |
| 2844 | base_type.builtin_type = Variant::OBJECT; |
| 2845 | base_type.native_type = scr->get_instance_base_type(); |
| 2846 | } |
| 2847 | } else { |
| 2848 | return false; |
| 2849 | } |
| 2850 | } break; |
| 2851 | case GDScriptParser::DataType::NATIVE: { |
| 2852 | if (!GDScriptAnalyzer::class_exists(base_type.native_type)) { |
| 2853 | return false; |
| 2854 | } |
| 2855 | MethodBind *mb = ClassDB::get_method(base_type.native_type, p_method); |
| 2856 | if (mb) { |
| 2857 | r_type = _type_from_property(mb->get_return_info()); |
| 2858 | return true; |
| 2859 | } |
| 2860 | return false; |
| 2861 | } break; |
| 2862 | case GDScriptParser::DataType::BUILTIN: { |
| 2863 | Callable::CallError err; |
| 2864 | Variant tmp; |
| 2865 | Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err); |
| 2866 | if (err.error != Callable::CallError::CALL_OK) { |
| 2867 | return false; |
| 2868 | } |
| 2869 | |
| 2870 | List<MethodInfo> methods; |
| 2871 | tmp.get_method_list(&methods); |
| 2872 | |
| 2873 | for (const MethodInfo &mi : methods) { |
| 2874 | if (mi.name == p_method) { |
| 2875 | r_type = _type_from_property(mi.return_val); |
| 2876 | return true; |
| 2877 | } |
| 2878 | } |
| 2879 | return false; |
| 2880 | } break; |
| 2881 | default: { |
| 2882 | return false; |
| 2883 | } |
| 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | return false; |
| 2888 | } |
| 2889 | |
| 2890 | static bool _guess_expecting_callable(GDScriptParser::CompletionContext &p_context) { |
| 2891 | if (p_context.call.call != nullptr && p_context.call.call->type == GDScriptParser::Node::CALL) { |
| 2892 | GDScriptParser::CallNode *call_node = static_cast<GDScriptParser::CallNode *>(p_context.call.call); |
| 2893 | GDScriptCompletionIdentifier ci; |
| 2894 | if (_guess_expression_type(p_context, call_node->callee, ci)) { |
| 2895 | if (ci.type.kind == GDScriptParser::DataType::BUILTIN && ci.type.builtin_type == Variant::CALLABLE) { |
| 2896 | if (p_context.call.argument >= 0 && p_context.call.argument < ci.type.method_info.arguments.size()) { |
| 2897 | return ci.type.method_info.arguments.get(p_context.call.argument).type == Variant::CALLABLE; |
| 2898 | } |
| 2899 | } |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | return false; |
| 2904 | } |
| 2905 | |
| 2906 | static void _find_enumeration_candidates(GDScriptParser::CompletionContext &p_context, const String &p_enum_hint, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) { |
| 2907 | if (!p_enum_hint.contains_char('.')) { |
| 2908 | // Global constant or in the current class. |
| 2909 | StringName current_enum = p_enum_hint; |
| 2910 | if (p_context.current_class && p_context.current_class->has_member(current_enum) && p_context.current_class->get_member(current_enum).type == GDScriptParser::ClassNode::Member::ENUM) { |
| 2911 | const GDScriptParser::EnumNode *_enum = p_context.current_class->get_member(current_enum).m_enum; |
| 2912 | for (int i = 0; i < _enum->values.size(); i++) { |
| 2913 | ScriptLanguage::CodeCompletionOption option(_enum->values[i].identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM); |
| 2914 | r_result.insert(option.display, option); |
| 2915 | } |
| 2916 | } else { |
| 2917 | for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) { |
| 2918 | if (CoreConstants::get_global_constant_enum(i) == current_enum) { |
| 2919 | ScriptLanguage::CodeCompletionOption option(CoreConstants::get_global_constant_name(i), ScriptLanguage::CODE_COMPLETION_KIND_ENUM); |
| 2920 | r_result.insert(option.display, option); |
| 2921 | } |
| 2922 | } |
| 2923 | } |
| 2924 | } else { |
| 2925 | String class_name = p_enum_hint.get_slicec('.', 0); |
| 2926 | String enum_name = p_enum_hint.get_slicec('.', 1); |
| 2927 | |
| 2928 | if (!GDScriptAnalyzer::class_exists(class_name)) { |
| 2929 | return; |
| 2930 | } |
| 2931 | |
| 2932 | List<StringName> enum_constants; |
| 2933 | ClassDB::get_enum_constants(class_name, enum_name, &enum_constants); |
| 2934 | for (const StringName &E : enum_constants) { |
| 2935 | String candidate = class_name + "." + E; |
| 2936 | int location = _get_enum_constant_location(class_name, E); |
| 2937 | ScriptLanguage::CodeCompletionOption option(candidate, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location); |
| 2938 | r_result.insert(option.display, option); |
| 2939 | } |
| 2940 | } |
| 2941 | } |
| 2942 | |
| 2943 | static void _list_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const GDScriptParser::CallNode *p_call, int p_argidx, bool p_static, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, String &r_arghint) { |
| 2944 | Variant base = p_base.value; |
| 2945 | GDScriptParser::DataType base_type = p_base.type; |
| 2946 | const StringName &method = p_call->function_name; |
| 2947 | |
| 2948 | const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\""; |
| 2949 | const bool use_string_names = EDITOR_GET("text_editor/completion/add_string_name_literals"); |
| 2950 | const bool use_node_paths = EDITOR_GET("text_editor/completion/add_node_path_literals"); |
| 2951 | |
| 2952 | while (base_type.is_set() && !base_type.is_variant()) { |
| 2953 | switch (base_type.kind) { |
| 2954 | case GDScriptParser::DataType::CLASS: { |
| 2955 | if (base_type.is_meta_type && method == SNAME("new")) { |
| 2956 | const GDScriptParser::ClassNode *current = base_type.class_type; |
| 2957 | |
| 2958 | do { |
| 2959 | if (current->has_member("_init")) { |
| 2960 | const GDScriptParser::ClassNode::Member &member = current->get_member("_init"); |
| 2961 | |
| 2962 | if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) { |
| 2963 | r_arghint = base_type.class_type->get_datatype().to_string() + " new" + _make_arguments_hint(member.function, p_argidx, true); |
| 2964 | return; |
| 2965 | } |
| 2966 | } |
| 2967 | current = current->base_type.class_type; |
| 2968 | } while (current != nullptr); |
| 2969 | |
| 2970 | r_arghint = base_type.class_type->get_datatype().to_string() + " new()"; |
| 2971 | return; |
| 2972 | } |
| 2973 | |
| 2974 | if (base_type.class_type->has_member(method)) { |
| 2975 | const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(method); |
| 2976 | |
| 2977 | if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) { |
| 2978 | r_arghint = _make_arguments_hint(member.function, p_argidx); |
| 2979 | return; |
| 2980 | } |
| 2981 | } |
| 2982 | |
| 2983 | base_type = base_type.class_type->base_type; |
| 2984 | } break; |
| 2985 | case GDScriptParser::DataType::SCRIPT: { |
| 2986 | if (base_type.script_type->is_valid() && base_type.script_type->has_method(method)) { |
| 2987 | r_arghint = _make_arguments_hint(base_type.script_type->get_method_info(method), p_argidx); |
| 2988 | return; |
| 2989 | } |
| 2990 | Ref<Script> base_script = base_type.script_type->get_base_script(); |
| 2991 | if (base_script.is_valid()) { |
| 2992 | base_type.script_type = base_script; |
| 2993 | } else { |
| 2994 | base_type.kind = GDScriptParser::DataType::NATIVE; |
| 2995 | base_type.builtin_type = Variant::OBJECT; |
| 2996 | base_type.native_type = base_type.script_type->get_instance_base_type(); |
| 2997 | } |
| 2998 | } break; |
| 2999 | case GDScriptParser::DataType::NATIVE: { |
| 3000 | StringName class_name = base_type.native_type; |
| 3001 | if (!GDScriptAnalyzer::class_exists(class_name)) { |
| 3002 | base_type.kind = GDScriptParser::DataType::UNRESOLVED; |
| 3003 | break; |
| 3004 | } |
| 3005 | |
| 3006 | MethodInfo info; |
| 3007 | int method_args = 0; |
| 3008 | |
| 3009 | if (ClassDB::get_method_info(class_name, method, &info)) { |
| 3010 | method_args = info.arguments.size(); |
| 3011 | if (base.get_type() == Variant::OBJECT) { |
| 3012 | Object *obj = base.operator Object *(); |
| 3013 | if (obj) { |
| 3014 | List<String> options; |
| 3015 | obj->get_argument_options(method, p_argidx, &options); |
| 3016 | for (String &opt : options) { |
| 3017 | // Handle user preference. |
| 3018 | if (opt.is_quoted()) { |
| 3019 | opt = opt.unquote().quote(quote_style); |
| 3020 | if (use_string_names && info.arguments[p_argidx].type == Variant::STRING_NAME) { |
| 3021 | if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) { |
| 3022 | GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]); |
| 3023 | if (literal->value.get_type() == Variant::STRING) { |
| 3024 | opt = "&" + opt; |
| 3025 | } |
| 3026 | } else { |
| 3027 | opt = "&" + opt; |
| 3028 | } |
| 3029 | } else if (use_node_paths && info.arguments[p_argidx].type == Variant::NODE_PATH) { |
| 3030 | if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) { |
| 3031 | GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]); |
| 3032 | if (literal->value.get_type() == Variant::STRING) { |
| 3033 | opt = "^" + opt; |
| 3034 | } |
| 3035 | } else { |
| 3036 | opt = "^" + opt; |
| 3037 | } |
| 3038 | } |
| 3039 | } |
| 3040 | ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 3041 | r_result.insert(option.display, option); |
| 3042 | } |
| 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | if (p_argidx < method_args) { |
| 3047 | const PropertyInfo &arg_info = info.arguments[p_argidx]; |
| 3048 | if (arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) { |
| 3049 | _find_enumeration_candidates(p_context, arg_info.class_name, r_result); |
| 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | r_arghint = _make_arguments_hint(info, p_argidx); |
| 3054 | } |
| 3055 | |
| 3056 | if (p_argidx == 1 && p_call && ClassDB::is_parent_class(class_name, SNAME("Tween")) && method == SNAME("tween_property")) { |
| 3057 | // Get tweened objects properties. |
| 3058 | if (p_call->arguments.is_empty()) { |
| 3059 | base_type.kind = GDScriptParser::DataType::UNRESOLVED; |
| 3060 | break; |
| 3061 | } |
| 3062 | GDScriptParser::ExpressionNode *tweened_object = p_call->arguments[0]; |
| 3063 | if (!tweened_object) { |
| 3064 | base_type.kind = GDScriptParser::DataType::UNRESOLVED; |
| 3065 | break; |
| 3066 | } |
| 3067 | StringName native_type = tweened_object->datatype.native_type; |
| 3068 | switch (tweened_object->datatype.kind) { |
| 3069 | case GDScriptParser::DataType::SCRIPT: { |
| 3070 | Ref<Script> script = tweened_object->datatype.script_type; |
| 3071 | native_type = script->get_instance_base_type(); |
| 3072 | int n = 0; |
| 3073 | while (script.is_valid()) { |
| 3074 | List<PropertyInfo> properties; |
| 3075 | script->get_script_property_list(&properties); |
| 3076 | for (const PropertyInfo &E : properties) { |
| 3077 | if (E.usage & (PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_INTERNAL)) { |
| 3078 | continue; |
| 3079 | } |
| 3080 | String name = E.name.quote(quote_style); |
| 3081 | if (use_node_paths) { |
| 3082 | if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) { |
| 3083 | GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]); |
| 3084 | if (literal->value.get_type() == Variant::STRING) { |
| 3085 | name = "^" + name; |
| 3086 | } |
| 3087 | } else { |
| 3088 | name = "^" + name; |
| 3089 | } |
| 3090 | } |
| 3091 | ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::CodeCompletionLocation::LOCATION_LOCAL + n); |
| 3092 | r_result.insert(option.display, option); |
| 3093 | } |
| 3094 | script = script->get_base_script(); |
| 3095 | n++; |
| 3096 | } |
| 3097 | } break; |
| 3098 | case GDScriptParser::DataType::CLASS: { |
| 3099 | GDScriptParser::ClassNode *clss = tweened_object->datatype.class_type; |
| 3100 | native_type = clss->base_type.native_type; |
| 3101 | int n = 0; |
| 3102 | while (clss) { |
| 3103 | for (GDScriptParser::ClassNode::Member member : clss->members) { |
| 3104 | if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) { |
| 3105 | String name = member.get_name().quote(quote_style); |
| 3106 | if (use_node_paths) { |
| 3107 | if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) { |
| 3108 | GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]); |
| 3109 | if (literal->value.get_type() == Variant::STRING) { |
| 3110 | name = "^" + name; |
| 3111 | } |
| 3112 | } else { |
| 3113 | name = "^" + name; |
| 3114 | } |
| 3115 | } |
| 3116 | ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::CodeCompletionLocation::LOCATION_LOCAL + n); |
| 3117 | r_result.insert(option.display, option); |
| 3118 | } |
| 3119 | } |
| 3120 | if (clss->base_type.kind == GDScriptParser::DataType::Kind::CLASS) { |
| 3121 | clss = clss->base_type.class_type; |
| 3122 | n++; |
| 3123 | } else { |
| 3124 | native_type = clss->base_type.native_type; |
| 3125 | clss = nullptr; |
| 3126 | } |
| 3127 | } |
| 3128 | } break; |
| 3129 | default: |
| 3130 | break; |
| 3131 | } |
| 3132 | |
| 3133 | List<PropertyInfo> properties; |
| 3134 | ClassDB::get_property_list(native_type, &properties); |
| 3135 | for (const PropertyInfo &E : properties) { |
| 3136 | if (E.usage & (PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_INTERNAL)) { |
| 3137 | continue; |
| 3138 | } |
| 3139 | String name = E.name.quote(quote_style); |
| 3140 | if (use_node_paths) { |
| 3141 | if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) { |
| 3142 | GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]); |
| 3143 | if (literal->value.get_type() == Variant::STRING) { |
| 3144 | name = "^" + name; |
| 3145 | } |
| 3146 | } else { |
| 3147 | name = "^" + name; |
| 3148 | } |
| 3149 | } |
| 3150 | ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER); |
| 3151 | r_result.insert(option.display, option); |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | if (p_argidx == 0 && ClassDB::is_parent_class(class_name, SNAME("Node")) && (method == SNAME("get_node") || method == SNAME("has_node"))) { |
| 3156 | // Get autoloads |
| 3157 | List<PropertyInfo> props; |
| 3158 | ProjectSettings::get_singleton()->get_property_list(&props); |
| 3159 | |
| 3160 | for (const PropertyInfo &E : props) { |
| 3161 | String s = E.name; |
| 3162 | if (!s.begins_with("autoload/")) { |
| 3163 | continue; |
| 3164 | } |
| 3165 | String name = s.get_slicec('/', 1); |
| 3166 | String path = ("/root/" + name).quote(quote_style); |
| 3167 | if (use_node_paths) { |
| 3168 | if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) { |
| 3169 | GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]); |
| 3170 | if (literal->value.get_type() == Variant::STRING) { |
| 3171 | path = "^" + path; |
| 3172 | } |
| 3173 | } else { |
| 3174 | path = "^" + path; |
| 3175 | } |
| 3176 | } |
| 3177 | ScriptLanguage::CodeCompletionOption option(path, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH); |
| 3178 | r_result.insert(option.display, option); |
| 3179 | } |
| 3180 | } |
| 3181 | |
| 3182 | if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, SNAME("InputEvent")) && method.operator String().contains("action")) { |
| 3183 | // Get input actions |
| 3184 | List<PropertyInfo> props; |
| 3185 | ProjectSettings::get_singleton()->get_property_list(&props); |
| 3186 | for (const PropertyInfo &E : props) { |
| 3187 | String s = E.name; |
| 3188 | if (!s.begins_with("input/")) { |
| 3189 | continue; |
| 3190 | } |
| 3191 | String name = s.get_slicec('/', 1).quote(quote_style); |
| 3192 | if (use_string_names) { |
| 3193 | if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) { |
| 3194 | GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]); |
| 3195 | if (literal->value.get_type() == Variant::STRING) { |
| 3196 | name = "&" + name; |
| 3197 | } |
| 3198 | } else { |
| 3199 | name = "&" + name; |
| 3200 | } |
| 3201 | } |
| 3202 | ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT); |
| 3203 | r_result.insert(option.display, option); |
| 3204 | } |
| 3205 | } |
| 3206 | if (EDITOR_GET("text_editor/completion/complete_file_paths")) { |
| 3207 | if (p_argidx == 0 && method == SNAME("change_scene_to_file") && ClassDB::is_parent_class(class_name, SNAME("SceneTree"))) { |
| 3208 | HashMap<String, ScriptLanguage::CodeCompletionOption> list; |
| 3209 | _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), list, SNAME("PackedScene")); |
| 3210 | for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &key_value_pair : list) { |
| 3211 | ScriptLanguage::CodeCompletionOption option = key_value_pair.value; |
| 3212 | r_result.insert(option.display, option); |
| 3213 | } |
| 3214 | } |
| 3215 | } |
| 3216 | |
| 3217 | base_type.kind = GDScriptParser::DataType::UNRESOLVED; |
| 3218 | } break; |
| 3219 | case GDScriptParser::DataType::BUILTIN: { |
| 3220 | if (base.get_type() == Variant::NIL) { |
| 3221 | Callable::CallError err; |
| 3222 | Variant::construct(base_type.builtin_type, base, nullptr, 0, err); |
| 3223 | if (err.error != Callable::CallError::CALL_OK) { |
| 3224 | return; |
| 3225 | } |
| 3226 | } |
| 3227 | |
| 3228 | List<MethodInfo> methods; |
| 3229 | base.get_method_list(&methods); |
| 3230 | for (const MethodInfo &E : methods) { |
| 3231 | if (E.name == method) { |
| 3232 | r_arghint = _make_arguments_hint(E, p_argidx); |
| 3233 | return; |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | base_type.kind = GDScriptParser::DataType::UNRESOLVED; |
| 3238 | } break; |
| 3239 | default: { |
| 3240 | base_type.kind = GDScriptParser::DataType::UNRESOLVED; |
| 3241 | } break; |
| 3242 | } |
| 3243 | } |
| 3244 | } |
| 3245 | |
| 3246 | static bool _get_subscript_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::SubscriptNode *p_subscript, GDScriptParser::DataType &r_base_type, Variant *r_base = nullptr) { |
| 3247 | if (p_context.base == nullptr) { |
| 3248 | return false; |
| 3249 | } |
| 3250 | |
| 3251 | const GDScriptParser::GetNodeNode *get_node = nullptr; |
| 3252 | |
| 3253 | switch (p_subscript->base->type) { |
| 3254 | case GDScriptParser::Node::GET_NODE: { |
| 3255 | get_node = static_cast<GDScriptParser::GetNodeNode *>(p_subscript->base); |
| 3256 | } break; |
| 3257 | |
| 3258 | case GDScriptParser::Node::IDENTIFIER: { |
| 3259 | const GDScriptParser::IdentifierNode *identifier_node = static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base); |
| 3260 | |
| 3261 | switch (identifier_node->source) { |
| 3262 | case GDScriptParser::IdentifierNode::Source::MEMBER_VARIABLE: { |
| 3263 | if (p_context.current_class != nullptr) { |
| 3264 | const StringName &member_name = identifier_node->name; |
| 3265 | const GDScriptParser::ClassNode *current_class = p_context.current_class; |
| 3266 | |
| 3267 | if (current_class->has_member(member_name)) { |
| 3268 | const GDScriptParser::ClassNode::Member &member = current_class->get_member(member_name); |
| 3269 | |
| 3270 | if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) { |
| 3271 | const GDScriptParser::VariableNode *variable = static_cast<GDScriptParser::VariableNode *>(member.variable); |
| 3272 | |
| 3273 | if (variable->initializer && variable->initializer->type == GDScriptParser::Node::GET_NODE) { |
| 3274 | get_node = static_cast<GDScriptParser::GetNodeNode *>(variable->initializer); |
| 3275 | } |
| 3276 | } |
| 3277 | } |
| 3278 | } |
| 3279 | } break; |
| 3280 | case GDScriptParser::IdentifierNode::Source::LOCAL_VARIABLE: { |
| 3281 | // TODO: Do basic assignment flow analysis like in `_guess_expression_type`. |
| 3282 | const GDScriptParser::SuiteNode::Local local = identifier_node->suite->get_local(identifier_node->name); |
| 3283 | switch (local.type) { |
| 3284 | case GDScriptParser::SuiteNode::Local::CONSTANT: { |
| 3285 | if (local.constant->initializer && local.constant->initializer->type == GDScriptParser::Node::GET_NODE) { |
| 3286 | get_node = static_cast<GDScriptParser::GetNodeNode *>(local.constant->initializer); |
| 3287 | } |
| 3288 | } break; |
| 3289 | case GDScriptParser::SuiteNode::Local::VARIABLE: { |
| 3290 | if (local.variable->initializer && local.variable->initializer->type == GDScriptParser::Node::GET_NODE) { |
| 3291 | get_node = static_cast<GDScriptParser::GetNodeNode *>(local.variable->initializer); |
| 3292 | } |
| 3293 | } break; |
| 3294 | default: { |
| 3295 | } break; |
| 3296 | } |
| 3297 | } break; |
| 3298 | default: { |
| 3299 | } break; |
| 3300 | } |
| 3301 | } break; |
| 3302 | default: { |
| 3303 | } break; |
| 3304 | } |
| 3305 | |
| 3306 | if (get_node != nullptr) { |
| 3307 | const Object *node = p_context.base->call("get_node_or_null", NodePath(get_node->full_path)); |
| 3308 | if (node != nullptr) { |
| 3309 | GDScriptParser::DataType assigned_type = _type_from_variant(node, p_context).type; |
| 3310 | GDScriptParser::DataType base_type = p_subscript->base->datatype; |
| 3311 | |
| 3312 | if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER && base_type.type_source == GDScriptParser::DataType::ANNOTATED_EXPLICIT && (assigned_type.kind != base_type.kind || assigned_type.script_path != base_type.script_path || assigned_type.native_type != base_type.native_type)) { |
| 3313 | // Annotated type takes precedence. |
| 3314 | return false; |
| 3315 | } |
| 3316 | |
| 3317 | if (r_base != nullptr) { |
| 3318 | *r_base = node; |
| 3319 | } |
| 3320 | |
| 3321 | r_base_type.type_source = GDScriptParser::DataType::INFERRED; |
| 3322 | r_base_type.builtin_type = Variant::OBJECT; |
| 3323 | r_base_type.native_type = node->get_class_name(); |
| 3324 | |
| 3325 | Ref<Script> scr = node->get_script(); |
| 3326 | if (scr.is_null()) { |
| 3327 | r_base_type.kind = GDScriptParser::DataType::NATIVE; |
| 3328 | } else { |
| 3329 | r_base_type.kind = GDScriptParser::DataType::SCRIPT; |
| 3330 | r_base_type.script_type = scr; |
| 3331 | } |
| 3332 | |
| 3333 | return true; |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | return false; |
| 3338 | } |
| 3339 | |
| 3340 | static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptParser::Node *p_call, int p_argidx, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, bool &r_forced, String &r_arghint) { |
| 3341 | if (p_call->type == GDScriptParser::Node::PRELOAD) { |
| 3342 | if (p_argidx == 0 && bool(EDITOR_GET("text_editor/completion/complete_file_paths"))) { |
| 3343 | _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result); |
| 3344 | } |
| 3345 | |
| 3346 | MethodInfo mi(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "preload", PropertyInfo(Variant::STRING, "path")); |
| 3347 | r_arghint = _make_arguments_hint(mi, p_argidx); |
| 3348 | return; |
| 3349 | } else if (p_call->type != GDScriptParser::Node::CALL) { |
| 3350 | return; |
| 3351 | } |
| 3352 | |
| 3353 | Variant base; |
| 3354 | GDScriptParser::DataType base_type; |
| 3355 | bool _static = false; |
| 3356 | const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_call); |
| 3357 | GDScriptParser::Node::Type callee_type = call->get_callee_type(); |
| 3358 | |
| 3359 | if (callee_type == GDScriptParser::Node::SUBSCRIPT) { |
| 3360 | const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee); |
| 3361 | |
| 3362 | if (subscript->base != nullptr && subscript->base->type == GDScriptParser::Node::IDENTIFIER) { |
| 3363 | const GDScriptParser::IdentifierNode *base_identifier = static_cast<const GDScriptParser::IdentifierNode *>(subscript->base); |
| 3364 | |
| 3365 | Variant::Type method_type = GDScriptParser::get_builtin_type(base_identifier->name); |
| 3366 | if (method_type < Variant::VARIANT_MAX) { |
| 3367 | Variant v; |
| 3368 | Callable::CallError err; |
| 3369 | Variant::construct(method_type, v, nullptr, 0, err); |
| 3370 | if (err.error != Callable::CallError::CALL_OK) { |
| 3371 | return; |
| 3372 | } |
| 3373 | List<MethodInfo> methods; |
| 3374 | v.get_method_list(&methods); |
| 3375 | |
| 3376 | for (MethodInfo &E : methods) { |
| 3377 | if (p_argidx >= E.arguments.size()) { |
| 3378 | continue; |
| 3379 | } |
| 3380 | if (E.name == call->function_name) { |
| 3381 | r_arghint += _make_arguments_hint(E, p_argidx); |
| 3382 | return; |
| 3383 | } |
| 3384 | } |
| 3385 | } |
| 3386 | } |
| 3387 | |
| 3388 | if (subscript->is_attribute) { |
| 3389 | bool found_type = _get_subscript_type(p_context, subscript, base_type, &base); |
| 3390 | |
| 3391 | if (!found_type) { |
| 3392 | GDScriptCompletionIdentifier ci; |
| 3393 | if (_guess_expression_type(p_context, subscript->base, ci)) { |
| 3394 | base_type = ci.type; |
| 3395 | base = ci.value; |
| 3396 | } else { |
| 3397 | return; |
| 3398 | } |
| 3399 | } |
| 3400 | |
| 3401 | _static = base_type.is_meta_type; |
| 3402 | } |
| 3403 | } else if (Variant::has_utility_function(call->function_name)) { |
| 3404 | MethodInfo info = Variant::get_utility_function_info(call->function_name); |
| 3405 | r_arghint = _make_arguments_hint(info, p_argidx); |
| 3406 | return; |
| 3407 | } else if (GDScriptUtilityFunctions::function_exists(call->function_name)) { |
| 3408 | MethodInfo info = GDScriptUtilityFunctions::get_function_info(call->function_name); |
| 3409 | r_arghint = _make_arguments_hint(info, p_argidx); |
| 3410 | return; |
| 3411 | } else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) { |
| 3412 | // Complete constructor. |
| 3413 | List<MethodInfo> constructors; |
| 3414 | Variant::get_constructor_list(GDScriptParser::get_builtin_type(call->function_name), &constructors); |
| 3415 | |
| 3416 | int i = 0; |
| 3417 | for (const MethodInfo &E : constructors) { |
| 3418 | if (p_argidx >= E.arguments.size()) { |
| 3419 | continue; |
| 3420 | } |
| 3421 | if (i > 0) { |
| 3422 | r_arghint += "\n"; |
| 3423 | } |
| 3424 | r_arghint += _make_arguments_hint(E, p_argidx); |
| 3425 | i++; |
| 3426 | } |
| 3427 | return; |
| 3428 | } else if (call->is_super || callee_type == GDScriptParser::Node::IDENTIFIER) { |
| 3429 | base = p_context.base; |
| 3430 | |
| 3431 | if (p_context.current_class) { |
| 3432 | base_type = p_context.current_class->get_datatype(); |
| 3433 | _static = !p_context.current_function || p_context.current_function->is_static; |
| 3434 | } |
| 3435 | } else { |
| 3436 | return; |
| 3437 | } |
| 3438 | |
| 3439 | GDScriptCompletionIdentifier ci; |
| 3440 | ci.type = base_type; |
| 3441 | ci.value = base; |
| 3442 | _list_call_arguments(p_context, ci, call, p_argidx, _static, r_result, r_arghint); |
| 3443 | |
| 3444 | r_forced = r_result.size() > 0; |
| 3445 | } |
| 3446 | |
| 3447 | ::Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) { |
| 3448 | const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\""; |
| 3449 | |
| 3450 | GDScriptParser parser; |
| 3451 | GDScriptAnalyzer analyzer(&parser); |
| 3452 | |
| 3453 | parser.parse(p_code, p_path, true); |
| 3454 | analyzer.analyze(); |
| 3455 | |
| 3456 | r_forced = false; |
| 3457 | HashMap<String, ScriptLanguage::CodeCompletionOption> options; |
| 3458 | |
| 3459 | GDScriptParser::CompletionContext completion_context = parser.get_completion_context(); |
| 3460 | if (completion_context.current_class != nullptr && completion_context.current_class->outer == nullptr) { |
| 3461 | completion_context.base = p_owner; |
| 3462 | } |
| 3463 | bool is_function = false; |
| 3464 | |
| 3465 | switch (completion_context.type) { |
| 3466 | case GDScriptParser::COMPLETION_NONE: |
| 3467 | break; |
| 3468 | case GDScriptParser::COMPLETION_ANNOTATION: { |
| 3469 | List<MethodInfo> annotations; |
| 3470 | parser.get_annotation_list(&annotations); |
| 3471 | for (const MethodInfo &E : annotations) { |
| 3472 | ScriptLanguage::CodeCompletionOption option(E.name.substr(1), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 3473 | if (E.arguments.size() > 0) { |
| 3474 | option.insert_text += "("; |
| 3475 | } |
| 3476 | options.insert(option.display, option); |
| 3477 | } |
| 3478 | r_forced = true; |
| 3479 | } break; |
| 3480 | case GDScriptParser::COMPLETION_ANNOTATION_ARGUMENTS: { |
| 3481 | if (completion_context.node == nullptr || completion_context.node->type != GDScriptParser::Node::ANNOTATION) { |
| 3482 | break; |
| 3483 | } |
| 3484 | const GDScriptParser::AnnotationNode *annotation = static_cast<const GDScriptParser::AnnotationNode *>(completion_context.node); |
| 3485 | _find_annotation_arguments(annotation, completion_context.current_argument, quote_style, options, r_call_hint); |
| 3486 | r_forced = true; |
| 3487 | } break; |
| 3488 | case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: { |
| 3489 | // Constants. |
| 3490 | { |
| 3491 | List<StringName> constants; |
| 3492 | Variant::get_constants_for_type(completion_context.builtin_type, &constants); |
| 3493 | for (const StringName &E : constants) { |
| 3494 | ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT); |
| 3495 | bool valid = false; |
| 3496 | Variant default_value = Variant::get_constant_value(completion_context.builtin_type, E, &valid); |
| 3497 | if (valid) { |
| 3498 | option.default_value = default_value; |
| 3499 | } |
| 3500 | options.insert(option.display, option); |
| 3501 | } |
| 3502 | } |
| 3503 | // Methods. |
| 3504 | { |
| 3505 | List<StringName> methods; |
| 3506 | Variant::get_builtin_method_list(completion_context.builtin_type, &methods); |
| 3507 | for (const StringName &E : methods) { |
| 3508 | if (Variant::is_builtin_method_static(completion_context.builtin_type, E)) { |
| 3509 | ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION); |
| 3510 | if (!_guess_expecting_callable(completion_context)) { |
| 3511 | if (Variant::get_builtin_method_argument_count(completion_context.builtin_type, E) > 0 || Variant::is_builtin_method_vararg(completion_context.builtin_type, E)) { |
| 3512 | option.insert_text += "("; |
| 3513 | } else { |
| 3514 | option.insert_text += "()"; |
| 3515 | } |
| 3516 | } |
| 3517 | options.insert(option.display, option); |
| 3518 | } |
| 3519 | } |
| 3520 | } |
| 3521 | } break; |
| 3522 | case GDScriptParser::COMPLETION_INHERIT_TYPE: { |
| 3523 | _list_available_types(true, completion_context, options); |
| 3524 | r_forced = true; |
| 3525 | } break; |
| 3526 | case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID: { |
| 3527 | ScriptLanguage::CodeCompletionOption option("void", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 3528 | options.insert(option.display, option); |
| 3529 | } |
| 3530 | [[fallthrough]]; |
| 3531 | case GDScriptParser::COMPLETION_TYPE_NAME: { |
| 3532 | _list_available_types(false, completion_context, options); |
| 3533 | r_forced = true; |
| 3534 | } break; |
| 3535 | case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE: { |
| 3536 | _list_available_types(false, completion_context, options); |
| 3537 | ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 3538 | options.insert(get.display, get); |
| 3539 | ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 3540 | options.insert(set.display, set); |
| 3541 | r_forced = true; |
| 3542 | } break; |
| 3543 | case GDScriptParser::COMPLETION_PROPERTY_DECLARATION: { |
| 3544 | ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 3545 | options.insert(get.display, get); |
| 3546 | ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT); |
| 3547 | options.insert(set.display, set); |
| 3548 | r_forced = true; |
| 3549 | } break; |
| 3550 | case GDScriptParser::COMPLETION_PROPERTY_METHOD: { |
| 3551 | if (!completion_context.current_class) { |
| 3552 | break; |
| 3553 | } |
| 3554 | for (int i = 0; i < completion_context.current_class->members.size(); i++) { |
| 3555 | const GDScriptParser::ClassNode::Member &member = completion_context.current_class->members[i]; |
| 3556 | if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) { |
| 3557 | continue; |
| 3558 | } |
| 3559 | if (member.function->is_static) { |
| 3560 | continue; |
| 3561 | } |
| 3562 | ScriptLanguage::CodeCompletionOption option(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION); |
| 3563 | options.insert(option.display, option); |
| 3564 | } |
| 3565 | r_forced = true; |
| 3566 | } break; |
| 3567 | case GDScriptParser::COMPLETION_ASSIGN: { |
| 3568 | GDScriptCompletionIdentifier type; |
| 3569 | if (!completion_context.node || completion_context.node->type != GDScriptParser::Node::ASSIGNMENT) { |
| 3570 | break; |
| 3571 | } |
| 3572 | if (!_guess_expression_type(completion_context, static_cast<const GDScriptParser::AssignmentNode *>(completion_context.node)->assignee, type)) { |
| 3573 | _find_identifiers(completion_context, false, true, options, 0); |
| 3574 | r_forced = true; |
| 3575 | break; |
| 3576 | } |
| 3577 | |
| 3578 | if (!type.enumeration.is_empty()) { |
| 3579 | _find_enumeration_candidates(completion_context, type.enumeration, options); |
| 3580 | r_forced = options.size() > 0; |
| 3581 | } else { |
| 3582 | _find_identifiers(completion_context, false, true, options, 0); |
| 3583 | r_forced = true; |
| 3584 | } |
| 3585 | } break; |
| 3586 | case GDScriptParser::COMPLETION_METHOD: |
| 3587 | is_function = true; |
| 3588 | [[fallthrough]]; |
| 3589 | case GDScriptParser::COMPLETION_IDENTIFIER: { |
| 3590 | _find_identifiers(completion_context, is_function, !_guess_expecting_callable(completion_context), options, 0); |
| 3591 | } break; |
| 3592 | case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD: |
| 3593 | is_function = true; |
| 3594 | [[fallthrough]]; |
| 3595 | case GDScriptParser::COMPLETION_ATTRIBUTE: { |
| 3596 | r_forced = true; |
| 3597 | const GDScriptParser::SubscriptNode *attr = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node); |
| 3598 | if (attr->base) { |
| 3599 | GDScriptCompletionIdentifier base; |
| 3600 | bool found_type = _get_subscript_type(completion_context, attr, base.type); |
| 3601 | if (!found_type && !_guess_expression_type(completion_context, attr->base, base)) { |
| 3602 | break; |
| 3603 | } |
| 3604 | |
| 3605 | _find_identifiers_in_base(base, is_function, false, !_guess_expecting_callable(completion_context), options, 0); |
| 3606 | } |
| 3607 | } break; |
| 3608 | case GDScriptParser::COMPLETION_SUBSCRIPT: { |
| 3609 | const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node); |
| 3610 | GDScriptCompletionIdentifier base; |
| 3611 | const bool res = _guess_expression_type(completion_context, subscript->base, base); |
| 3612 | |
| 3613 | // If the type is not known, we assume it is BUILTIN, since indices on arrays is the most common use case. |
| 3614 | if (!subscript->is_attribute && (!res || base.type.kind == GDScriptParser::DataType::BUILTIN || base.type.is_variant())) { |
| 3615 | if (base.value.get_type() == Variant::DICTIONARY) { |
| 3616 | List<PropertyInfo> members; |
| 3617 | base.value.get_property_list(&members); |
| 3618 | |
| 3619 | for (const PropertyInfo &E : members) { |
| 3620 | ScriptLanguage::CodeCompletionOption option(E.name.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::LOCATION_LOCAL); |
| 3621 | options.insert(option.display, option); |
| 3622 | } |
| 3623 | } |
| 3624 | if (!subscript->index || subscript->index->type != GDScriptParser::Node::LITERAL) { |
| 3625 | _find_identifiers(completion_context, false, !_guess_expecting_callable(completion_context), options, 0); |
| 3626 | } |
| 3627 | } else if (res) { |
| 3628 | if (!subscript->is_attribute) { |
| 3629 | // Quote the options if they are not accessed as attribute. |
| 3630 | |
| 3631 | HashMap<String, ScriptLanguage::CodeCompletionOption> opt; |
| 3632 | _find_identifiers_in_base(base, false, false, false, opt, 0); |
| 3633 | for (const KeyValue<String, CodeCompletionOption> &E : opt) { |
| 3634 | ScriptLanguage::CodeCompletionOption option(E.value.insert_text.quote(quote_style), E.value.kind, E.value.location); |
| 3635 | options.insert(option.display, option); |
| 3636 | } |
| 3637 | } else { |
| 3638 | _find_identifiers_in_base(base, false, false, !_guess_expecting_callable(completion_context), options, 0); |
| 3639 | } |
| 3640 | } |
| 3641 | } break; |
| 3642 | case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: { |
| 3643 | if (!completion_context.current_class) { |
| 3644 | break; |
| 3645 | } |
| 3646 | |
| 3647 | const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(completion_context.node); |
| 3648 | ERR_FAIL_INDEX_V_MSG(completion_context.type_chain_index - 1, type->type_chain.size(), Error::ERR_BUG, "Could not complete type argument with out of bounds type chain index."); |
| 3649 | |
| 3650 | GDScriptCompletionIdentifier base; |
| 3651 | |
| 3652 | if (_guess_identifier_type(completion_context, type->type_chain[0], base)) { |
| 3653 | bool found = true; |
| 3654 | for (int i = 1; i < completion_context.type_chain_index; i++) { |
| 3655 | GDScriptCompletionIdentifier ci; |
| 3656 | found = _guess_identifier_type_from_base(completion_context, base, type->type_chain[i]->name, ci); |
| 3657 | base = ci; |
| 3658 | if (!found) { |
| 3659 | break; |
| 3660 | } |
| 3661 | } |
| 3662 | if (found) { |
| 3663 | _find_identifiers_in_base(base, false, true, true, options, 0); |
| 3664 | } |
| 3665 | } |
| 3666 | |
| 3667 | r_forced = true; |
| 3668 | } break; |
| 3669 | case GDScriptParser::COMPLETION_RESOURCE_PATH: { |
| 3670 | if (EDITOR_GET("text_editor/completion/complete_file_paths")) { |
| 3671 | _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options); |
| 3672 | r_forced = true; |
| 3673 | } |
| 3674 | } break; |
| 3675 | case GDScriptParser::COMPLETION_CALL_ARGUMENTS: { |
| 3676 | if (!completion_context.node) { |
| 3677 | break; |
| 3678 | } |
| 3679 | _find_call_arguments(completion_context, completion_context.node, completion_context.current_argument, options, r_forced, r_call_hint); |
| 3680 | } break; |
| 3681 | case GDScriptParser::COMPLETION_OVERRIDE_METHOD: { |
| 3682 | GDScriptParser::DataType native_type = completion_context.current_class->base_type; |
| 3683 | GDScriptParser::FunctionNode *function_node = static_cast<GDScriptParser::FunctionNode *>(completion_context.node); |
| 3684 | bool is_static = function_node != nullptr && function_node->is_static; |
| 3685 | while (native_type.is_set() && native_type.kind != GDScriptParser::DataType::NATIVE) { |
| 3686 | switch (native_type.kind) { |
| 3687 | case GDScriptParser::DataType::CLASS: { |
| 3688 | for (const GDScriptParser::ClassNode::Member &member : native_type.class_type->members) { |
| 3689 | if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) { |
| 3690 | continue; |
| 3691 | } |
| 3692 | |
| 3693 | if (options.has(member.function->identifier->name)) { |
| 3694 | continue; |
| 3695 | } |
| 3696 | |
| 3697 | if (completion_context.current_class->has_function(member.get_name()) && completion_context.current_class->get_member(member.get_name()).function != function_node) { |
| 3698 | continue; |
| 3699 | } |
| 3700 | |
| 3701 | if (is_static != member.function->is_static) { |
| 3702 | continue; |
| 3703 | } |
| 3704 | |
| 3705 | String display_name = member.function->identifier->name; |
| 3706 | display_name += member.function->signature + ":"; |
| 3707 | ScriptLanguage::CodeCompletionOption option(display_name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION); |
| 3708 | options.insert(member.function->identifier->name, option); // Insert name instead of display to track duplicates. |
| 3709 | } |
| 3710 | native_type = native_type.class_type->base_type; |
| 3711 | } break; |
| 3712 | default: { |
| 3713 | native_type.kind = GDScriptParser::DataType::UNRESOLVED; |
| 3714 | } break; |
| 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | if (!native_type.is_set()) { |
| 3719 | break; |
| 3720 | } |
| 3721 | |
| 3722 | StringName class_name = native_type.native_type; |
| 3723 | if (!GDScriptAnalyzer::class_exists(class_name)) { |
| 3724 | break; |
| 3725 | } |
| 3726 | |
| 3727 | const bool type_hints = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints"); |
| 3728 | |
| 3729 | List<MethodInfo> virtual_methods; |
| 3730 | if (is_static) { |
| 3731 | // Not truly a virtual method, but can also be "overridden". |
| 3732 | MethodInfo static_init("_static_init"); |
| 3733 | static_init.return_val.type = Variant::NIL; |
| 3734 | static_init.flags |= METHOD_FLAG_STATIC | METHOD_FLAG_VIRTUAL; |
| 3735 | virtual_methods.push_back(static_init); |
| 3736 | } else { |
| 3737 | ClassDB::get_virtual_methods(class_name, &virtual_methods); |
| 3738 | } |
| 3739 | |
| 3740 | for (const MethodInfo &mi : virtual_methods) { |
| 3741 | if (options.has(mi.name)) { |
| 3742 | continue; |
| 3743 | } |
| 3744 | if (completion_context.current_class->has_function(mi.name) && completion_context.current_class->get_member(mi.name).function != function_node) { |
| 3745 | continue; |
| 3746 | } |
| 3747 | String method_hint = mi.name; |
| 3748 | if (method_hint.contains_char(':')) { |
| 3749 | method_hint = method_hint.get_slicec(':', 0); |
| 3750 | } |
| 3751 | method_hint += "("; |
| 3752 | |
| 3753 | for (int64_t i = 0; i < mi.arguments.size(); ++i) { |
| 3754 | if (i > 0) { |
| 3755 | method_hint += ", "; |
| 3756 | } |
| 3757 | String arg = mi.arguments[i].name; |
| 3758 | if (arg.contains_char(':')) { |
| 3759 | arg = arg.substr(0, arg.find_char(':')); |
| 3760 | } |
| 3761 | method_hint += arg; |
| 3762 | if (type_hints) { |
| 3763 | method_hint += ": " + _get_visual_datatype(mi.arguments[i], true, class_name); |
| 3764 | } |
| 3765 | } |
| 3766 | if (mi.flags & METHOD_FLAG_VARARG) { |
| 3767 | if (!mi.arguments.is_empty()) { |
| 3768 | method_hint += ", "; |
| 3769 | } |
| 3770 | method_hint += "...args"; // `MethodInfo` does not support the rest parameter name. |
| 3771 | if (type_hints) { |
| 3772 | method_hint += ": Array"; |
| 3773 | } |
| 3774 | } |
| 3775 | method_hint += ")"; |
| 3776 | if (type_hints) { |
| 3777 | method_hint += " -> " + _get_visual_datatype(mi.return_val, false, class_name); |
| 3778 | } |
| 3779 | method_hint += ":"; |
| 3780 | |
| 3781 | ScriptLanguage::CodeCompletionOption option(method_hint, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION); |
| 3782 | options.insert(option.display, option); |
| 3783 | } |
| 3784 | } break; |
| 3785 | case GDScriptParser::COMPLETION_GET_NODE: { |
| 3786 | // Handles the `$Node/Path` or `$"Some NodePath"` syntax specifically. |
| 3787 | if (p_owner) { |
| 3788 | List<String> opts; |
| 3789 | p_owner->get_argument_options("get_node", 0, &opts); |
| 3790 | |
| 3791 | bool for_unique_name = false; |
| 3792 | if (completion_context.node != nullptr && completion_context.node->type == GDScriptParser::Node::GET_NODE && !static_cast<GDScriptParser::GetNodeNode *>(completion_context.node)->use_dollar) { |
| 3793 | for_unique_name = true; |
| 3794 | } |
| 3795 | |
| 3796 | for (const String &E : opts) { |
| 3797 | r_forced = true; |
| 3798 | String opt = E.strip_edges(); |
| 3799 | if (opt.is_quoted()) { |
| 3800 | // Remove quotes so that we can handle user preferred quote style, |
| 3801 | // or handle NodePaths which are valid identifiers and don't need quotes. |
| 3802 | opt = opt.unquote(); |
| 3803 | } |
| 3804 | |
| 3805 | if (for_unique_name) { |
| 3806 | if (!opt.begins_with("%")) { |
| 3807 | continue; |
| 3808 | } |
| 3809 | opt = opt.substr(1); |
| 3810 | } |
| 3811 | |
| 3812 | // The path needs quotes if at least one of its components (excluding `%` prefix and `/` separations) |
| 3813 | // is not a valid identifier. |
| 3814 | bool path_needs_quote = false; |
| 3815 | for (const String &part : opt.trim_prefix("%").split("/")) { |
| 3816 | if (!part.is_valid_ascii_identifier()) { |
| 3817 | path_needs_quote = true; |
| 3818 | break; |
| 3819 | } |
| 3820 | } |
| 3821 | |
| 3822 | if (path_needs_quote) { |
| 3823 | // Ignore quote_style and just use double quotes for paths with apostrophes. |
| 3824 | // Double quotes don't need to be checked because they're not valid in node and property names. |
| 3825 | opt = opt.quote(opt.contains_char('\'') ? "\"" : quote_style); // Handle user preference. |
| 3826 | } |
| 3827 | ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH); |
| 3828 | options.insert(option.display, option); |
| 3829 | } |
| 3830 | |
| 3831 | if (!for_unique_name) { |
| 3832 | // Get autoloads. |
| 3833 | for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) { |
| 3834 | String path = "/root/" + E.key; |
| 3835 | ScriptLanguage::CodeCompletionOption option(path.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH); |
| 3836 | options.insert(option.display, option); |
| 3837 | } |
| 3838 | } |
| 3839 | } |
| 3840 | } break; |
| 3841 | case GDScriptParser::COMPLETION_SUPER: |
| 3842 | break; |
| 3843 | case GDScriptParser::COMPLETION_SUPER_METHOD: { |
| 3844 | if (!completion_context.current_class) { |
| 3845 | break; |
| 3846 | } |
| 3847 | _find_identifiers_in_class(completion_context.current_class, true, false, false, true, !_guess_expecting_callable(completion_context), options, 0); |
| 3848 | } break; |
| 3849 | } |
| 3850 | |
| 3851 | for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &E : options) { |
| 3852 | r_options->push_back(E.value); |
| 3853 | } |
| 3854 | |
| 3855 | return OK; |
| 3856 | } |
| 3857 | |
| 3858 | #else // !TOOLS_ENABLED |
| 3859 | |
| 3860 | Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) { |
| 3861 | return OK; |
| 3862 | } |
| 3863 | |
| 3864 | #endif // TOOLS_ENABLED |
| 3865 | |
| 3866 | //////// END COMPLETION ////////// |
| 3867 | |
| 3868 | String GDScriptLanguage::_get_indentation() const { |
| 3869 | #ifdef TOOLS_ENABLED |
| 3870 | if (Engine::get_singleton()->is_editor_hint()) { |
| 3871 | bool use_space_indentation = EDITOR_GET("text_editor/behavior/indent/type"); |
| 3872 | |
| 3873 | if (use_space_indentation) { |
| 3874 | int indent_size = EDITOR_GET("text_editor/behavior/indent/size"); |
| 3875 | return String(" ").repeat(indent_size); |
| 3876 | } |
| 3877 | } |
| 3878 | #endif |
| 3879 | return "\t"; |
| 3880 | } |
| 3881 | |
| 3882 | void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const { |
| 3883 | String indent = _get_indentation(); |
| 3884 | |
| 3885 | Vector<String> lines = p_code.split("\n"); |
| 3886 | List<int> indent_stack; |
| 3887 | |
| 3888 | for (int i = 0; i < lines.size(); i++) { |
| 3889 | String l = lines[i]; |
| 3890 | int tc = 0; |
| 3891 | for (int j = 0; j < l.length(); j++) { |
| 3892 | if (l[j] == ' ' || l[j] == '\t') { |
| 3893 | tc++; |
| 3894 | } else { |
| 3895 | break; |
| 3896 | } |
| 3897 | } |
| 3898 | |
| 3899 | String st = l.substr(tc).strip_edges(); |
| 3900 | if (st.is_empty() || st.begins_with("#")) { |
| 3901 | continue; //ignore! |
| 3902 | } |
| 3903 | |
| 3904 | int ilevel = 0; |
| 3905 | if (indent_stack.size()) { |
| 3906 | ilevel = indent_stack.back()->get(); |
| 3907 | } |
| 3908 | |
| 3909 | if (tc > ilevel) { |
| 3910 | indent_stack.push_back(tc); |
| 3911 | } else if (tc < ilevel) { |
| 3912 | while (indent_stack.size() && indent_stack.back()->get() > tc) { |
| 3913 | indent_stack.pop_back(); |
| 3914 | } |
| 3915 | |
| 3916 | if (indent_stack.size() && indent_stack.back()->get() != tc) { |
| 3917 | indent_stack.push_back(tc); // this is not right but gets the job done |
| 3918 | } |
| 3919 | } |
| 3920 | |
| 3921 | if (i >= p_from_line) { |
| 3922 | l = indent.repeat(indent_stack.size()) + st; |
| 3923 | } else if (i > p_to_line) { |
| 3924 | break; |
| 3925 | } |
| 3926 | |
| 3927 | lines.write[i] = l; |
| 3928 | } |
| 3929 | |
| 3930 | p_code = ""; |
| 3931 | for (int i = 0; i < lines.size(); i++) { |
| 3932 | if (i > 0) { |
| 3933 | p_code += "\n"; |
| 3934 | } |
| 3935 | p_code += lines[i]; |
| 3936 | } |
| 3937 | } |
| 3938 | |
| 3939 | #ifdef TOOLS_ENABLED |
| 3940 | |
| 3941 | static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, GDScriptLanguage::LookupResult &r_result) { |
| 3942 | GDScriptParser::DataType base_type = p_base; |
| 3943 | |
| 3944 | while (true) { |
| 3945 | switch (base_type.kind) { |
| 3946 | case GDScriptParser::DataType::CLASS: { |
| 3947 | ERR_FAIL_NULL_V(base_type.class_type, ERR_BUG); |
| 3948 | |
| 3949 | String name = p_symbol; |
| 3950 | if (name == "new") { |
| 3951 | name = "_init"; |
| 3952 | } |
| 3953 | |
| 3954 | if (!base_type.class_type->has_member(name)) { |
| 3955 | base_type = base_type.class_type->base_type; |
| 3956 | break; |
| 3957 | } |
| 3958 | |
| 3959 | const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(name); |
| 3960 | |
| 3961 | switch (member.type) { |
| 3962 | case GDScriptParser::ClassNode::Member::UNDEFINED: |
| 3963 | case GDScriptParser::ClassNode::Member::GROUP: |
| 3964 | return ERR_BUG; |
| 3965 | case GDScriptParser::ClassNode::Member::CLASS: { |
| 3966 | String doc_type_name; |
| 3967 | String doc_enum_name; |
| 3968 | GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(member.get_datatype()), doc_type_name, doc_enum_name); |
| 3969 | |
| 3970 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS; |
| 3971 | r_result.class_name = doc_type_name; |
| 3972 | } break; |
| 3973 | case GDScriptParser::ClassNode::Member::CONSTANT: |
| 3974 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; |
| 3975 | break; |
| 3976 | case GDScriptParser::ClassNode::Member::FUNCTION: |
| 3977 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD; |
| 3978 | break; |
| 3979 | case GDScriptParser::ClassNode::Member::SIGNAL: |
| 3980 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL; |
| 3981 | break; |
| 3982 | case GDScriptParser::ClassNode::Member::VARIABLE: |
| 3983 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY; |
| 3984 | break; |
| 3985 | case GDScriptParser::ClassNode::Member::ENUM: |
| 3986 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM; |
| 3987 | break; |
| 3988 | case GDScriptParser::ClassNode::Member::ENUM_VALUE: |
| 3989 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; |
| 3990 | break; |
| 3991 | } |
| 3992 | |
| 3993 | if (member.type != GDScriptParser::ClassNode::Member::CLASS) { |
| 3994 | String doc_type_name; |
| 3995 | String doc_enum_name; |
| 3996 | GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(base_type), doc_type_name, doc_enum_name); |
| 3997 | |
| 3998 | r_result.class_name = doc_type_name; |
| 3999 | r_result.class_member = name; |
| 4000 | } |
| 4001 | |
| 4002 | Error err = OK; |
| 4003 | r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err); |
| 4004 | r_result.script_path = base_type.script_path; |
| 4005 | r_result.location = member.get_line(); |
| 4006 | return err; |
| 4007 | } break; |
| 4008 | case GDScriptParser::DataType::SCRIPT: { |
| 4009 | const Ref<Script> scr = base_type.script_type; |
| 4010 | |
| 4011 | if (scr.is_null()) { |
| 4012 | return ERR_CANT_RESOLVE; |
| 4013 | } |
| 4014 | |
| 4015 | String name = p_symbol; |
| 4016 | if (name == "new") { |
| 4017 | name = "_init"; |
| 4018 | } |
| 4019 | |
| 4020 | const int line = scr->get_member_line(name); |
| 4021 | if (line >= 0) { |
| 4022 | bool found_type = false; |
| 4023 | r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION; |
| 4024 | { |
| 4025 | List<PropertyInfo> properties; |
| 4026 | scr->get_script_property_list(&properties); |
| 4027 | for (const PropertyInfo &property : properties) { |
| 4028 | if (property.name == name && (property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE)) { |
| 4029 | found_type = true; |
| 4030 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY; |
| 4031 | r_result.class_name = scr->get_doc_class_name(); |
| 4032 | r_result.class_member = name; |
| 4033 | break; |
| 4034 | } |
| 4035 | } |
| 4036 | } |
| 4037 | if (!found_type) { |
| 4038 | List<MethodInfo> methods; |
| 4039 | scr->get_script_method_list(&methods); |
| 4040 | for (const MethodInfo &method : methods) { |
| 4041 | if (method.name == name) { |
| 4042 | found_type = true; |
| 4043 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD; |
| 4044 | r_result.class_name = scr->get_doc_class_name(); |
| 4045 | r_result.class_member = name; |
| 4046 | break; |
| 4047 | } |
| 4048 | } |
| 4049 | } |
| 4050 | if (!found_type) { |
| 4051 | List<MethodInfo> signals; |
| 4052 | scr->get_script_method_list(&signals); |
| 4053 | for (const MethodInfo &signal : signals) { |
| 4054 | if (signal.name == name) { |
| 4055 | found_type = true; |
| 4056 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL; |
| 4057 | r_result.class_name = scr->get_doc_class_name(); |
| 4058 | r_result.class_member = name; |
| 4059 | break; |
| 4060 | } |
| 4061 | } |
| 4062 | } |
| 4063 | if (!found_type) { |
| 4064 | const Ref<GDScript> gds = scr; |
| 4065 | if (gds.is_valid()) { |
| 4066 | const Ref<GDScript> *subclass = gds->get_subclasses().getptr(name); |
| 4067 | if (subclass != nullptr) { |
| 4068 | found_type = true; |
| 4069 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS; |
| 4070 | r_result.class_name = subclass->ptr()->get_doc_class_name(); |
| 4071 | } |
| 4072 | // TODO: enums. |
| 4073 | } |
| 4074 | } |
| 4075 | if (!found_type) { |
| 4076 | HashMap<StringName, Variant> constants; |
| 4077 | scr->get_constants(&constants); |
| 4078 | if (constants.has(name)) { |
| 4079 | found_type = true; |
| 4080 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; |
| 4081 | r_result.class_name = scr->get_doc_class_name(); |
| 4082 | r_result.class_member = name; |
| 4083 | } |
| 4084 | } |
| 4085 | |
| 4086 | r_result.script = scr; |
| 4087 | r_result.script_path = base_type.script_path; |
| 4088 | r_result.location = line; |
| 4089 | return OK; |
| 4090 | } |
| 4091 | |
| 4092 | const Ref<Script> base_script = scr->get_base_script(); |
| 4093 | if (base_script.is_valid()) { |
| 4094 | base_type.script_type = base_script; |
| 4095 | } else { |
| 4096 | base_type.kind = GDScriptParser::DataType::NATIVE; |
| 4097 | base_type.builtin_type = Variant::OBJECT; |
| 4098 | base_type.native_type = scr->get_instance_base_type(); |
| 4099 | } |
| 4100 | } break; |
| 4101 | case GDScriptParser::DataType::NATIVE: { |
| 4102 | const StringName &class_name = base_type.native_type; |
| 4103 | |
| 4104 | ERR_FAIL_COND_V(!GDScriptAnalyzer::class_exists(class_name), ERR_BUG); |
| 4105 | |
| 4106 | if (ClassDB::has_method(class_name, p_symbol, true)) { |
| 4107 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD; |
| 4108 | r_result.class_name = class_name; |
| 4109 | r_result.class_member = p_symbol; |
| 4110 | return OK; |
| 4111 | } |
| 4112 | |
| 4113 | List<MethodInfo> virtual_methods; |
| 4114 | ClassDB::get_virtual_methods(class_name, &virtual_methods, true); |
| 4115 | for (const MethodInfo &E : virtual_methods) { |
| 4116 | if (E.name == p_symbol) { |
| 4117 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD; |
| 4118 | r_result.class_name = class_name; |
| 4119 | r_result.class_member = p_symbol; |
| 4120 | return OK; |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | if (ClassDB::has_signal(class_name, p_symbol, true)) { |
| 4125 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL; |
| 4126 | r_result.class_name = class_name; |
| 4127 | r_result.class_member = p_symbol; |
| 4128 | return OK; |
| 4129 | } |
| 4130 | |
| 4131 | List<StringName> enums; |
| 4132 | ClassDB::get_enum_list(class_name, &enums); |
| 4133 | for (const StringName &E : enums) { |
| 4134 | if (E == p_symbol) { |
| 4135 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM; |
| 4136 | r_result.class_name = class_name; |
| 4137 | r_result.class_member = p_symbol; |
| 4138 | return OK; |
| 4139 | } |
| 4140 | } |
| 4141 | |
| 4142 | if (!String(ClassDB::get_integer_constant_enum(class_name, p_symbol, true)).is_empty()) { |
| 4143 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; |
| 4144 | r_result.class_name = class_name; |
| 4145 | r_result.class_member = p_symbol; |
| 4146 | return OK; |
| 4147 | } |
| 4148 | |
| 4149 | List<String> constants; |
| 4150 | ClassDB::get_integer_constant_list(class_name, &constants, true); |
| 4151 | for (const String &E : constants) { |
| 4152 | if (E == p_symbol) { |
| 4153 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; |
| 4154 | r_result.class_name = class_name; |
| 4155 | r_result.class_member = p_symbol; |
| 4156 | return OK; |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | if (ClassDB::has_property(class_name, p_symbol, true)) { |
| 4161 | PropertyInfo prop_info; |
| 4162 | ClassDB::get_property_info(class_name, p_symbol, &prop_info, true); |
| 4163 | if (prop_info.usage & PROPERTY_USAGE_INTERNAL) { |
| 4164 | return ERR_CANT_RESOLVE; |
| 4165 | } |
| 4166 | |
| 4167 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY; |
| 4168 | r_result.class_name = class_name; |
| 4169 | r_result.class_member = p_symbol; |
| 4170 | return OK; |
| 4171 | } |
| 4172 | |
| 4173 | const StringName parent_class = ClassDB::get_parent_class(class_name); |
| 4174 | if (parent_class != StringName()) { |
| 4175 | base_type.native_type = parent_class; |
| 4176 | } else { |
| 4177 | return ERR_CANT_RESOLVE; |
| 4178 | } |
| 4179 | } break; |
| 4180 | case GDScriptParser::DataType::BUILTIN: { |
| 4181 | if (base_type.is_meta_type) { |
| 4182 | if (Variant::has_enum(base_type.builtin_type, p_symbol)) { |
| 4183 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM; |
| 4184 | r_result.class_name = Variant::get_type_name(base_type.builtin_type); |
| 4185 | r_result.class_member = p_symbol; |
| 4186 | return OK; |
| 4187 | } |
| 4188 | |
| 4189 | if (Variant::has_constant(base_type.builtin_type, p_symbol)) { |
| 4190 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; |
| 4191 | r_result.class_name = Variant::get_type_name(base_type.builtin_type); |
| 4192 | r_result.class_member = p_symbol; |
| 4193 | return OK; |
| 4194 | } |
| 4195 | } else { |
| 4196 | if (Variant::has_member(base_type.builtin_type, p_symbol)) { |
| 4197 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY; |
| 4198 | r_result.class_name = Variant::get_type_name(base_type.builtin_type); |
| 4199 | r_result.class_member = p_symbol; |
| 4200 | return OK; |
| 4201 | } |
| 4202 | } |
| 4203 | |
| 4204 | if (Variant::has_builtin_method(base_type.builtin_type, p_symbol)) { |
| 4205 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD; |
| 4206 | r_result.class_name = Variant::get_type_name(base_type.builtin_type); |
| 4207 | r_result.class_member = p_symbol; |
| 4208 | return OK; |
| 4209 | } |
| 4210 | |
| 4211 | return ERR_CANT_RESOLVE; |
| 4212 | } break; |
| 4213 | case GDScriptParser::DataType::ENUM: { |
| 4214 | if (base_type.is_meta_type) { |
| 4215 | if (base_type.enum_values.has(p_symbol)) { |
| 4216 | String doc_type_name; |
| 4217 | String doc_enum_name; |
| 4218 | GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(base_type), doc_type_name, doc_enum_name); |
| 4219 | |
| 4220 | if (CoreConstants::is_global_enum(doc_enum_name)) { |
| 4221 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; |
| 4222 | r_result.class_name = "@GlobalScope"; |
| 4223 | r_result.class_member = p_symbol; |
| 4224 | return OK; |
| 4225 | } else { |
| 4226 | const int dot_pos = doc_enum_name.rfind_char('.'); |
| 4227 | if (dot_pos >= 0) { |
| 4228 | Error err = OK; |
| 4229 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; |
| 4230 | if (base_type.class_type != nullptr) { |
| 4231 | // For script enums the value isn't accessible as class constant so we need the full enum name. |
| 4232 | r_result.class_name = doc_enum_name; |
| 4233 | r_result.class_member = p_symbol; |
| 4234 | r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err); |
| 4235 | r_result.script_path = base_type.script_path; |
| 4236 | const String enum_name = doc_enum_name.substr(dot_pos + 1); |
| 4237 | if (base_type.class_type->has_member(enum_name)) { |
| 4238 | const GDScriptParser::ClassNode::Member member = base_type.class_type->get_member(enum_name); |
| 4239 | if (member.type == GDScriptParser::ClassNode::Member::ENUM) { |
| 4240 | for (const GDScriptParser::EnumNode::Value &value : member.m_enum->values) { |
| 4241 | if (value.identifier->name == p_symbol) { |
| 4242 | r_result.location = value.line; |
| 4243 | break; |
| 4244 | } |
| 4245 | } |
| 4246 | } |
| 4247 | } |
| 4248 | } else if (base_type.script_type.is_valid()) { |
| 4249 | // For script enums the value isn't accessible as class constant so we need the full enum name. |
| 4250 | r_result.class_name = doc_enum_name; |
| 4251 | r_result.class_member = p_symbol; |
| 4252 | r_result.script = base_type.script_type; |
| 4253 | r_result.script_path = base_type.script_path; |
| 4254 | // TODO: Find a way to obtain enum value location for a script |
| 4255 | r_result.location = base_type.script_type->get_member_line(doc_enum_name.substr(dot_pos + 1)); |
| 4256 | } else { |
| 4257 | r_result.class_name = doc_enum_name.left(dot_pos); |
| 4258 | r_result.class_member = p_symbol; |
| 4259 | } |
| 4260 | return err; |
| 4261 | } |
| 4262 | } |
| 4263 | } else if (Variant::has_builtin_method(Variant::DICTIONARY, p_symbol)) { |
| 4264 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD; |
| 4265 | r_result.class_name = "Dictionary"; |
| 4266 | r_result.class_member = p_symbol; |
| 4267 | return OK; |
| 4268 | } |
| 4269 | } |
| 4270 | |
| 4271 | return ERR_CANT_RESOLVE; |
| 4272 | } break; |
| 4273 | case GDScriptParser::DataType::VARIANT: { |
| 4274 | if (base_type.is_meta_type) { |
| 4275 | const String enum_name = "Variant." + p_symbol; |
| 4276 | if (CoreConstants::is_global_enum(enum_name)) { |
| 4277 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM; |
| 4278 | r_result.class_name = "@GlobalScope"; |
| 4279 | r_result.class_member = enum_name; |
| 4280 | return OK; |
| 4281 | } |
| 4282 | } |
| 4283 | |
| 4284 | return ERR_CANT_RESOLVE; |
| 4285 | } break; |
| 4286 | case GDScriptParser::DataType::RESOLVING: |
| 4287 | case GDScriptParser::DataType::UNRESOLVED: { |
| 4288 | return ERR_CANT_RESOLVE; |
| 4289 | } break; |
| 4290 | } |
| 4291 | } |
| 4292 | |
| 4293 | return ERR_CANT_RESOLVE; |
| 4294 | } |
| 4295 | |
| 4296 | ::Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) { |
| 4297 | // Before parsing, try the usual stuff. |
| 4298 | if (GDScriptAnalyzer::class_exists(p_symbol)) { |
| 4299 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS; |
| 4300 | r_result.class_name = p_symbol; |
| 4301 | return OK; |
| 4302 | } |
| 4303 | |
| 4304 | if (Variant::get_type_by_name(p_symbol) < Variant::VARIANT_MAX) { |
| 4305 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS; |
| 4306 | r_result.class_name = p_symbol; |
| 4307 | return OK; |
| 4308 | } |
| 4309 | |
| 4310 | if (p_symbol == "Variant") { |
| 4311 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS; |
| 4312 | r_result.class_name = "Variant"; |
| 4313 | return OK; |
| 4314 | } |
| 4315 | |
| 4316 | if (p_symbol == "PI" || p_symbol == "TAU" || p_symbol == "INF" || p_symbol == "NAN") { |
| 4317 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; |
| 4318 | r_result.class_name = "@GDScript"; |
| 4319 | r_result.class_member = p_symbol; |
| 4320 | return OK; |
| 4321 | } |
| 4322 | |
| 4323 | GDScriptParser parser; |
| 4324 | parser.parse(p_code, p_path, true); |
| 4325 | |
| 4326 | GDScriptParser::CompletionContext context = parser.get_completion_context(); |
| 4327 | context.base = p_owner; |
| 4328 | |
| 4329 | // Allows class functions with the names like built-ins to be handled properly. |
| 4330 | if (context.type != GDScriptParser::COMPLETION_ATTRIBUTE) { |
| 4331 | // Need special checks for `assert` and `preload` as they are technically |
| 4332 | // keywords, so are not registered in `GDScriptUtilityFunctions`. |
| 4333 | if (GDScriptUtilityFunctions::function_exists(p_symbol) || p_symbol == "assert" || p_symbol == "preload") { |
| 4334 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD; |
| 4335 | r_result.class_name = "@GDScript"; |
| 4336 | r_result.class_member = p_symbol; |
| 4337 | return OK; |
| 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | GDScriptAnalyzer analyzer(&parser); |
| 4342 | analyzer.analyze(); |
| 4343 | |
| 4344 | if (context.current_class && context.current_class->extends.size() > 0) { |
| 4345 | StringName class_name = context.current_class->extends[0]->name; |
| 4346 | |
| 4347 | bool success = false; |
| 4348 | ClassDB::get_integer_constant(class_name, p_symbol, &success); |
| 4349 | if (success) { |
| 4350 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; |
| 4351 | r_result.class_name = class_name; |
| 4352 | r_result.class_member = p_symbol; |
| 4353 | return OK; |
| 4354 | } |
| 4355 | do { |
| 4356 | List<StringName> enums; |
| 4357 | ClassDB::get_enum_list(class_name, &enums, true); |
| 4358 | for (const StringName &enum_name : enums) { |
| 4359 | if (enum_name == p_symbol) { |
| 4360 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM; |
| 4361 | r_result.class_name = class_name; |
| 4362 | r_result.class_member = p_symbol; |
| 4363 | return OK; |
| 4364 | } |
| 4365 | } |
| 4366 | class_name = ClassDB::get_parent_class_nocheck(class_name); |
| 4367 | } while (class_name != StringName()); |
| 4368 | } |
| 4369 | |
| 4370 | const GDScriptParser::TypeNode *type_node = dynamic_cast<const GDScriptParser::TypeNode *>(context.node); |
| 4371 | if (type_node != nullptr && !type_node->type_chain.is_empty()) { |
| 4372 | StringName class_name = type_node->type_chain[0]->name; |
| 4373 | if (ScriptServer::is_global_class(class_name)) { |
| 4374 | class_name = ScriptServer::get_global_class_native_base(class_name); |
| 4375 | } |
| 4376 | do { |
| 4377 | List<StringName> enums; |
| 4378 | ClassDB::get_enum_list(class_name, &enums, true); |
| 4379 | for (const StringName &enum_name : enums) { |
| 4380 | if (enum_name == p_symbol) { |
| 4381 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM; |
| 4382 | r_result.class_name = class_name; |
| 4383 | r_result.class_member = p_symbol; |
| 4384 | return OK; |
| 4385 | } |
| 4386 | } |
| 4387 | class_name = ClassDB::get_parent_class_nocheck(class_name); |
| 4388 | } while (class_name != StringName()); |
| 4389 | } |
| 4390 | |
| 4391 | bool is_function = false; |
| 4392 | |
| 4393 | switch (context.type) { |
| 4394 | case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: { |
| 4395 | GDScriptParser::DataType base_type; |
| 4396 | base_type.kind = GDScriptParser::DataType::BUILTIN; |
| 4397 | base_type.builtin_type = context.builtin_type; |
| 4398 | base_type.is_meta_type = true; |
| 4399 | if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) { |
| 4400 | return OK; |
| 4401 | } |
| 4402 | } break; |
| 4403 | case GDScriptParser::COMPLETION_SUPER: { |
| 4404 | if (context.current_class && context.current_function) { |
| 4405 | if (_lookup_symbol_from_base(context.current_class->base_type, context.current_function->info.name, r_result) == OK) { |
| 4406 | return OK; |
| 4407 | } |
| 4408 | } |
| 4409 | } break; |
| 4410 | case GDScriptParser::COMPLETION_SUPER_METHOD: |
| 4411 | case GDScriptParser::COMPLETION_METHOD: |
| 4412 | case GDScriptParser::COMPLETION_ASSIGN: |
| 4413 | case GDScriptParser::COMPLETION_CALL_ARGUMENTS: |
| 4414 | case GDScriptParser::COMPLETION_IDENTIFIER: |
| 4415 | case GDScriptParser::COMPLETION_PROPERTY_METHOD: |
| 4416 | case GDScriptParser::COMPLETION_SUBSCRIPT: { |
| 4417 | GDScriptParser::DataType base_type; |
| 4418 | if (context.current_class) { |
| 4419 | if (context.type != GDScriptParser::COMPLETION_SUPER_METHOD) { |
| 4420 | base_type = context.current_class->get_datatype(); |
| 4421 | } else { |
| 4422 | base_type = context.current_class->base_type; |
| 4423 | } |
| 4424 | } else { |
| 4425 | break; |
| 4426 | } |
| 4427 | |
| 4428 | if (!is_function && context.current_suite) { |
| 4429 | // Lookup local variables. |
| 4430 | const GDScriptParser::SuiteNode *suite = context.current_suite; |
| 4431 | while (suite) { |
| 4432 | if (suite->has_local(p_symbol)) { |
| 4433 | const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_symbol); |
| 4434 | |
| 4435 | switch (local.type) { |
| 4436 | case GDScriptParser::SuiteNode::Local::UNDEFINED: |
| 4437 | return ERR_BUG; |
| 4438 | case GDScriptParser::SuiteNode::Local::CONSTANT: |
| 4439 | r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT; |
| 4440 | r_result.description = local.constant->doc_data.description; |
| 4441 | r_result.is_deprecated = local.constant->doc_data.is_deprecated; |
| 4442 | r_result.deprecated_message = local.constant->doc_data.deprecated_message; |
| 4443 | r_result.is_experimental = local.constant->doc_data.is_experimental; |
| 4444 | r_result.experimental_message = local.constant->doc_data.experimental_message; |
| 4445 | if (local.constant->initializer != nullptr) { |
| 4446 | r_result.value = GDScriptDocGen::docvalue_from_expression(local.constant->initializer); |
| 4447 | } |
| 4448 | break; |
| 4449 | case GDScriptParser::SuiteNode::Local::VARIABLE: |
| 4450 | r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE; |
| 4451 | r_result.description = local.variable->doc_data.description; |
| 4452 | r_result.is_deprecated = local.variable->doc_data.is_deprecated; |
| 4453 | r_result.deprecated_message = local.variable->doc_data.deprecated_message; |
| 4454 | r_result.is_experimental = local.variable->doc_data.is_experimental; |
| 4455 | r_result.experimental_message = local.variable->doc_data.experimental_message; |
| 4456 | if (local.variable->initializer != nullptr) { |
| 4457 | r_result.value = GDScriptDocGen::docvalue_from_expression(local.variable->initializer); |
| 4458 | } |
| 4459 | break; |
| 4460 | case GDScriptParser::SuiteNode::Local::PARAMETER: |
| 4461 | case GDScriptParser::SuiteNode::Local::FOR_VARIABLE: |
| 4462 | case GDScriptParser::SuiteNode::Local::PATTERN_BIND: |
| 4463 | r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE; |
| 4464 | break; |
| 4465 | } |
| 4466 | |
| 4467 | GDScriptDocGen::doctype_from_gdtype(local.get_datatype(), r_result.doc_type, r_result.enumeration); |
| 4468 | |
| 4469 | Error err = OK; |
| 4470 | r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err); |
| 4471 | r_result.script_path = base_type.script_path; |
| 4472 | r_result.location = local.start_line; |
| 4473 | return err; |
| 4474 | } |
| 4475 | suite = suite->parent_block; |
| 4476 | } |
| 4477 | } |
| 4478 | |
| 4479 | if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) { |
| 4480 | return OK; |
| 4481 | } |
| 4482 | |
| 4483 | if (!is_function) { |
| 4484 | if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) { |
| 4485 | const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(p_symbol); |
| 4486 | if (autoload.is_singleton) { |
| 4487 | String scr_path = autoload.path; |
| 4488 | if (!scr_path.ends_with(".gd")) { |
| 4489 | // Not a script, try find the script anyway, may have some success. |
| 4490 | scr_path = scr_path.get_basename() + ".gd"; |
| 4491 | } |
| 4492 | |
| 4493 | if (FileAccess::exists(scr_path)) { |
| 4494 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS; |
| 4495 | r_result.class_name = p_symbol; |
| 4496 | r_result.script = ResourceLoader::load(scr_path); |
| 4497 | r_result.script_path = scr_path; |
| 4498 | r_result.location = 0; |
| 4499 | return OK; |
| 4500 | } |
| 4501 | } |
| 4502 | } |
| 4503 | |
| 4504 | if (ScriptServer::is_global_class(p_symbol)) { |
| 4505 | const String scr_path = ScriptServer::get_global_class_path(p_symbol); |
| 4506 | const Ref<Script> scr = ResourceLoader::load(scr_path); |
| 4507 | if (scr.is_null()) { |
| 4508 | return ERR_BUG; |
| 4509 | } |
| 4510 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS; |
| 4511 | r_result.class_name = scr->get_doc_class_name(); |
| 4512 | r_result.script = scr; |
| 4513 | r_result.script_path = scr_path; |
| 4514 | r_result.location = 0; |
| 4515 | return OK; |
| 4516 | } |
| 4517 | |
| 4518 | const HashMap<StringName, int> &global_map = GDScriptLanguage::get_singleton()->get_global_map(); |
| 4519 | if (global_map.has(p_symbol)) { |
| 4520 | Variant value = GDScriptLanguage::get_singleton()->get_global_array()[global_map[p_symbol]]; |
| 4521 | if (value.get_type() == Variant::OBJECT) { |
| 4522 | const Object *obj = value; |
| 4523 | if (obj) { |
| 4524 | if (Object::cast_to<GDScriptNativeClass>(obj)) { |
| 4525 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS; |
| 4526 | r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name(); |
| 4527 | } else { |
| 4528 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS; |
| 4529 | r_result.class_name = obj->get_class(); |
| 4530 | } |
| 4531 | return OK; |
| 4532 | } |
| 4533 | } |
| 4534 | } |
| 4535 | |
| 4536 | if (CoreConstants::is_global_enum(p_symbol)) { |
| 4537 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM; |
| 4538 | r_result.class_name = "@GlobalScope"; |
| 4539 | r_result.class_member = p_symbol; |
| 4540 | return OK; |
| 4541 | } |
| 4542 | |
| 4543 | if (CoreConstants::is_global_constant(p_symbol)) { |
| 4544 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; |
| 4545 | r_result.class_name = "@GlobalScope"; |
| 4546 | r_result.class_member = p_symbol; |
| 4547 | return OK; |
| 4548 | } |
| 4549 | |
| 4550 | if (Variant::has_utility_function(p_symbol)) { |
| 4551 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD; |
| 4552 | r_result.class_name = "@GlobalScope"; |
| 4553 | r_result.class_member = p_symbol; |
| 4554 | return OK; |
| 4555 | } |
| 4556 | } |
| 4557 | } break; |
| 4558 | case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD: |
| 4559 | case GDScriptParser::COMPLETION_ATTRIBUTE: { |
| 4560 | if (context.node->type != GDScriptParser::Node::SUBSCRIPT) { |
| 4561 | break; |
| 4562 | } |
| 4563 | const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(context.node); |
| 4564 | if (!subscript->is_attribute) { |
| 4565 | break; |
| 4566 | } |
| 4567 | GDScriptCompletionIdentifier base; |
| 4568 | |
| 4569 | bool found_type = _get_subscript_type(context, subscript, base.type); |
| 4570 | if (!found_type && !_guess_expression_type(context, subscript->base, base)) { |
| 4571 | break; |
| 4572 | } |
| 4573 | |
| 4574 | if (_lookup_symbol_from_base(base.type, p_symbol, r_result) == OK) { |
| 4575 | return OK; |
| 4576 | } |
| 4577 | } break; |
| 4578 | case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: { |
| 4579 | if (context.node == nullptr || context.node->type != GDScriptParser::Node::TYPE) { |
| 4580 | break; |
| 4581 | } |
| 4582 | const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(context.node); |
| 4583 | |
| 4584 | GDScriptParser::DataType base_type; |
| 4585 | const GDScriptParser::IdentifierNode *prev = nullptr; |
| 4586 | for (const GDScriptParser::IdentifierNode *E : type->type_chain) { |
| 4587 | if (E->name == p_symbol && prev != nullptr) { |
| 4588 | base_type = prev->get_datatype(); |
| 4589 | break; |
| 4590 | } |
| 4591 | prev = E; |
| 4592 | } |
| 4593 | if (base_type.kind != GDScriptParser::DataType::CLASS) { |
| 4594 | GDScriptCompletionIdentifier base; |
| 4595 | if (!_guess_expression_type(context, prev, base)) { |
| 4596 | break; |
| 4597 | } |
| 4598 | base_type = base.type; |
| 4599 | } |
| 4600 | |
| 4601 | if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) { |
| 4602 | return OK; |
| 4603 | } |
| 4604 | } break; |
| 4605 | case GDScriptParser::COMPLETION_OVERRIDE_METHOD: { |
| 4606 | GDScriptParser::DataType base_type = context.current_class->base_type; |
| 4607 | |
| 4608 | if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) { |
| 4609 | return OK; |
| 4610 | } |
| 4611 | } break; |
| 4612 | case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE: |
| 4613 | case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID: |
| 4614 | case GDScriptParser::COMPLETION_TYPE_NAME: { |
| 4615 | GDScriptParser::DataType base_type = context.current_class->get_datatype(); |
| 4616 | |
| 4617 | if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) { |
| 4618 | return OK; |
| 4619 | } |
| 4620 | } break; |
| 4621 | case GDScriptParser::COMPLETION_ANNOTATION: { |
| 4622 | const String annotation_symbol = "@" + p_symbol; |
| 4623 | if (parser.annotation_exists(annotation_symbol)) { |
| 4624 | r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION; |
| 4625 | r_result.class_name = "@GDScript"; |
| 4626 | r_result.class_member = annotation_symbol; |
| 4627 | return OK; |
| 4628 | } |
| 4629 | } break; |
| 4630 | default: { |
| 4631 | } |
| 4632 | } |
| 4633 | |
| 4634 | return ERR_CANT_RESOLVE; |
| 4635 | } |
| 4636 | |
| 4637 | #endif // TOOLS_ENABLED |