diff options
Diffstat (limited to 'modules/gdscript')
24 files changed, 483 insertions, 128 deletions
diff --git a/modules/gdscript/doc_classes/@GDScript.xml b/modules/gdscript/doc_classes/@GDScript.xml index c4b7e4887e..258e94f909 100644 --- a/modules/gdscript/doc_classes/@GDScript.xml +++ b/modules/gdscript/doc_classes/@GDScript.xml @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="UTF-8" ?> -<class name="@GDScript" category="Core" version="3.2"> +<class name="@GDScript" version="3.2"> <brief_description> Built-in GDScript functions. </brief_description> @@ -39,12 +39,11 @@ <argument index="1" name="alpha" type="float" default="1.0"> </argument> <description> - Returns a color according to the standardised [code]name[/code] with [code]alpha[/code] ranging from 0 to 1. + Returns a color according to the standardized [code]name[/code] with [code]alpha[/code] ranging from 0 to 1. [codeblock] red = ColorN("red", 1) [/codeblock] - Supported color names: - "aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige", "bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown", "burlywood", "cadetblue", "chartreuse", "chocolate", "coral", "cornflower", "cornsilk", "crimson", "cyan", "darkblue", "darkcyan", "darkgoldenrod", "darkgray", "darkgreen", "darkkhaki", "darkmagenta", "darkolivegreen", "darkorange", "darkorchid", "darkred", "darksalmon", "darkseagreen", "darkslateblue", "darkslategray", "darkturquoise", "darkviolet", "deeppink", "deepskyblue", "dimgray", "dodgerblue", "firebrick", "floralwhite", "forestgreen", "fuchsia", "gainsboro", "ghostwhite", "gold", "goldenrod", "gray", "webgray", "green", "webgreen", "greenyellow", "honeydew", "hotpink", "indianred", "indigo", "ivory", "khaki", "lavender", "lavenderblush", "lawngreen", "lemonchiffon", "lightblue", "lightcoral", "lightcyan", "lightgoldenrod", "lightgray", "lightgreen", "lightpink", "lightsalmon", "lightseagreen", "lightskyblue", "lightslategray", "lightsteelblue", "lightyellow", "lime", "limegreen", "linen", "magenta", "maroon", "webmaroon", "mediumaquamarine", "mediumblue", "mediumorchid", "mediumpurple", "mediumseagreen", "mediumslateblue", "mediumspringgreen", "mediumturquoise", "mediumvioletred", "midnightblue", "mintcream", "mistyrose", "moccasin", "navajowhite", "navyblue", "oldlace", "olive", "olivedrab", "orange", "orangered", "orchid", "palegoldenrod", "palegreen", "paleturquoise", "palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum", "powderblue", "purple", "webpurple", "rebeccapurple", "red", "rosybrown", "royalblue", "saddlebrown", "salmon", "sandybrown", "seagreen", "seashell", "sienna", "silver", "skyblue", "slateblue", "slategray", "snow", "springgreen", "steelblue", "tan", "teal", "thistle", "tomato", "turquoise", "violet", "wheat", "white", "whitesmoke", "yellow", "yellowgreen". + Supported color names are the same as the constants defined in [Color]. </description> </method> <method name="abs"> diff --git a/modules/gdscript/doc_classes/GDScript.xml b/modules/gdscript/doc_classes/GDScript.xml index 8e175a7ab8..acd153f0cc 100644 --- a/modules/gdscript/doc_classes/GDScript.xml +++ b/modules/gdscript/doc_classes/GDScript.xml @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="UTF-8" ?> -<class name="GDScript" inherits="Script" category="Core" version="3.2"> +<class name="GDScript" inherits="Script" version="3.2"> <brief_description> A script implemented in the GDScript programming language. </brief_description> diff --git a/modules/gdscript/doc_classes/GDScriptFunctionState.xml b/modules/gdscript/doc_classes/GDScriptFunctionState.xml index 690953108f..be8267c243 100644 --- a/modules/gdscript/doc_classes/GDScriptFunctionState.xml +++ b/modules/gdscript/doc_classes/GDScriptFunctionState.xml @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="UTF-8" ?> -<class name="GDScriptFunctionState" inherits="Reference" category="Core" version="3.2"> +<class name="GDScriptFunctionState" inherits="Reference" version="3.2"> <brief_description> State of a function call after yielding. </brief_description> @@ -33,7 +33,7 @@ </methods> <signals> <signal name="completed"> - <argument index="0" name="result" type="Nil"> + <argument index="0" name="result" type="Variant"> </argument> <description> </description> diff --git a/modules/gdscript/doc_classes/GDScriptNativeClass.xml b/modules/gdscript/doc_classes/GDScriptNativeClass.xml index 70583d47a7..d4a0ce33aa 100644 --- a/modules/gdscript/doc_classes/GDScriptNativeClass.xml +++ b/modules/gdscript/doc_classes/GDScriptNativeClass.xml @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="UTF-8" ?> -<class name="GDScriptNativeClass" inherits="Reference" category="Core" version="3.2"> +<class name="GDScriptNativeClass" inherits="Reference" version="3.2"> <brief_description> </brief_description> <description> diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 5b0210b16d..a255b92257 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -320,7 +320,7 @@ ScriptInstance *GDScript::instance_create(Object *p_this) { if (!ClassDB::is_parent_class(p_this->get_class_name(), top->native->get_name())) { if (ScriptDebugger::get_singleton()) { - GDScriptLanguage::get_singleton()->debug_break_parse(get_path(), 0, "Script inherits from native type '" + String(top->native->get_name()) + "', so it can't be instanced in object of type: '" + p_this->get_class() + "'"); + GDScriptLanguage::get_singleton()->debug_break_parse(get_path(), 1, "Script inherits from native type '" + String(top->native->get_name()) + "', so it can't be instanced in object of type: '" + p_this->get_class() + "'"); } ERR_FAIL_V_MSG(NULL, "Script inherits from native type '" + String(top->native->get_name()) + "', so it can't be instanced in object of type '" + p_this->get_class() + "'" + "."); } @@ -915,14 +915,43 @@ GDScript::GDScript() : #endif } +void GDScript::_save_orphaned_subclasses() { + struct ClassRefWithName { + ObjectID id; + String fully_qualified_name; + }; + Vector<ClassRefWithName> weak_subclasses; + // collect subclasses ObjectID and name + for (Map<StringName, Ref<GDScript> >::Element *E = subclasses.front(); E; E = E->next()) { + E->get()->_owner = NULL; //bye, you are no longer owned cause I died + ClassRefWithName subclass; + subclass.id = E->get()->get_instance_id(); + subclass.fully_qualified_name = E->get()->fully_qualified_name; + weak_subclasses.push_back(subclass); + } + + // clear subclasses to allow unused subclasses to be deleted + subclasses.clear(); + // subclasses are also held by constants, clear those as well + constants.clear(); + + // keep orphan subclass only for subclasses that are still in use + for (int i = 0; i < weak_subclasses.size(); i++) { + ClassRefWithName subclass = weak_subclasses[i]; + Object *obj = ObjectDB::get_instance(subclass.id); + if (!obj) + continue; + // subclass is not released + GDScriptLanguage::get_singleton()->add_orphan_subclass(subclass.fully_qualified_name, subclass.id); + } +} + GDScript::~GDScript() { for (Map<StringName, GDScriptFunction *>::Element *E = member_functions.front(); E; E = E->next()) { memdelete(E->get()); } - for (Map<StringName, Ref<GDScript> >::Element *E = subclasses.front(); E; E = E->next()) { - E->get()->_owner = NULL; //bye, you are no longer owned cause I died - } + _save_orphaned_subclasses(); #ifdef DEBUG_ENABLED if (GDScriptLanguage::get_singleton()->lock) { @@ -2051,6 +2080,9 @@ String GDScriptWarning::get_message() const { CHECK_SYMBOLS(2); return "The '" + symbols[0] + "' keyword is deprecated and will be removed in a future release, please replace its uses by '" + symbols[1] + "'."; } break; + case STANDALONE_TERNARY: { + return "Standalone ternary conditional operator: the return value is being discarded."; + } case WARNING_MAX: break; // Can't happen, but silences warning } ERR_FAIL_V_MSG(String(), "Invalid GDScript warning code: " + get_name_from_code(code) + "."); @@ -2092,6 +2124,7 @@ String GDScriptWarning::get_name_from_code(Code p_code) { "UNSAFE_CAST", "UNSAFE_CALL_ARGUMENT", "DEPRECATED_KEYWORD", + "STANDALONE_TERNARY", NULL }; @@ -2172,6 +2205,22 @@ GDScriptLanguage::~GDScriptLanguage() { singleton = NULL; } +void GDScriptLanguage::add_orphan_subclass(const String &p_qualified_name, const ObjectID &p_subclass) { + orphan_subclasses[p_qualified_name] = p_subclass; +} + +Ref<GDScript> GDScriptLanguage::get_orphan_subclass(const String &p_qualified_name) { + Map<String, ObjectID>::Element *orphan_subclass_element = orphan_subclasses.find(p_qualified_name); + if (!orphan_subclass_element) + return Ref<GDScript>(); + ObjectID orphan_subclass = orphan_subclass_element->get(); + Object *obj = ObjectDB::get_instance(orphan_subclass); + orphan_subclasses.erase(orphan_subclass_element); + if (!obj) + return Ref<GDScript>(); + return Ref<GDScript>(Object::cast_to<GDScript>(obj)); +} + /*************** RESOURCE ***************/ RES ResourceFormatLoaderGDScript::load(const String &p_path, const String &p_original_path, Error *r_error) { diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h index cc25c1c313..4ae52238ce 100644 --- a/modules/gdscript/gdscript.h +++ b/modules/gdscript/gdscript.h @@ -111,6 +111,7 @@ class GDScript : public Script { String source; String path; String name; + String fully_qualified_name; SelfList<GDScript> script_list; GDScriptInstance *_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, bool p_isref, Variant::CallError &r_error); @@ -131,6 +132,8 @@ class GDScript : public Script { bool _update_exports(); + void _save_orphaned_subclasses(); + protected: bool _get(const StringName &p_name, Variant &r_ret) const; bool _set(const StringName &p_name, const Variant &p_value); @@ -297,6 +300,7 @@ struct GDScriptWarning { UNSAFE_CAST, // Cast used in an unknown type UNSAFE_CALL_ARGUMENT, // Function call argument is of a supertype of the require argument DEPRECATED_KEYWORD, // The keyword is deprecated and should be replaced + STANDALONE_TERNARY, // Return value of ternary expression is discarded WARNING_MAX, } code; Vector<String> symbols; @@ -353,6 +357,8 @@ class GDScriptLanguage : public ScriptLanguage { bool profiling; uint64_t script_frame_time; + Map<String, ObjectID> orphan_subclasses; + public: int calls; @@ -504,6 +510,9 @@ public: virtual bool handles_global_class_type(const String &p_type) const; virtual String get_global_class_name(const String &p_path, String *r_base_type = NULL, String *r_icon_path = NULL) const; + void add_orphan_subclass(const String &p_qualified_name, const ObjectID &p_subclass); + Ref<GDScript> get_orphan_subclass(const String &p_qualified_name); + GDScriptLanguage(); ~GDScriptLanguage(); }; diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp index 11e015b473..fba1b992ec 100644 --- a/modules/gdscript/gdscript_compiler.cpp +++ b/modules/gdscript/gdscript_compiler.cpp @@ -90,11 +90,11 @@ bool GDScriptCompiler::_create_unary_operator(CodeGen &codegen, const GDScriptPa return true; } -bool GDScriptCompiler::_create_binary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer) { +bool GDScriptCompiler::_create_binary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer, int p_index_addr) { ERR_FAIL_COND_V(on->arguments.size() != 2, false); - int src_address_a = _parse_expression(codegen, on->arguments[0], p_stack_level, false, p_initializer); + int src_address_a = _parse_expression(codegen, on->arguments[0], p_stack_level, false, p_initializer, p_index_addr); if (src_address_a < 0) return false; if (src_address_a & GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS) @@ -171,7 +171,7 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D return result; } -int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level) { +int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level, int p_index_addr) { Variant::Operator var_op = Variant::OP_MAX; @@ -205,7 +205,7 @@ int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDS return _parse_expression(codegen, p_expression->arguments[1], p_stack_level, false, initializer); } - if (!_create_binary_operator(codegen, p_expression, var_op, p_stack_level, initializer)) + if (!_create_binary_operator(codegen, p_expression, var_op, p_stack_level, initializer, p_index_addr)) return -1; int dst_addr = (p_stack_level) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); @@ -214,7 +214,7 @@ int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDS return dst_addr; } -int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root, bool p_initializer) { +int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root, bool p_initializer, int p_index_addr) { switch (p_expression->type) { //should parse variable declaration and adjust stack accordingly... @@ -704,7 +704,9 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: return from; int index; - if (named) { + if (p_index_addr != 0) { + index = p_index_addr; + } else if (named) { if (on->arguments[0]->type == GDScriptParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) { GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(on->arguments[1]); @@ -1091,7 +1093,7 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser:: codegen.alloc_stack(slevel); } - int set_value = _parse_assign_right_expression(codegen, on, slevel + 1); + int set_value = _parse_assign_right_expression(codegen, on, slevel + 1, named ? 0 : set_index); if (set_value < 0) //error return set_value; @@ -2121,14 +2123,21 @@ void GDScriptCompiler::_make_scripts(GDScript *p_script, const GDScriptParser::C StringName name = p_class->subclasses[i]->name; Ref<GDScript> subclass; + String fully_qualified_name = p_script->fully_qualified_name + "::" + name; if (old_subclasses.has(name)) { subclass = old_subclasses[name]; } else { - subclass.instance(); + Ref<GDScript> orphan_subclass = GDScriptLanguage::get_singleton()->get_orphan_subclass(fully_qualified_name); + if (orphan_subclass.is_valid()) { + subclass = orphan_subclass; + } else { + subclass.instance(); + } } subclass->_owner = p_script; + subclass->fully_qualified_name = fully_qualified_name; p_script->subclasses.insert(name, subclass); _make_scripts(subclass.ptr(), p_class->subclasses[i], false); @@ -2147,6 +2156,9 @@ Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_scri source = p_script->get_path(); + // The best fully qualified name for a base level script is its file path + p_script->fully_qualified_name = p_script->path; + // Create scripts for subclasses beforehand so they can be referenced _make_scripts(p_script, static_cast<const GDScriptParser::ClassNode *>(root), p_keep_state); diff --git a/modules/gdscript/gdscript_compiler.h b/modules/gdscript/gdscript_compiler.h index bb3ee881f8..7d5234a023 100644 --- a/modules/gdscript/gdscript_compiler.h +++ b/modules/gdscript/gdscript_compiler.h @@ -140,12 +140,12 @@ class GDScriptCompiler { void _set_error(const String &p_error, const GDScriptParser::Node *p_node); bool _create_unary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level); - bool _create_binary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer = false); + bool _create_binary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer = false, int p_index_addr = 0); GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype) const; - int _parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level); - int _parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root = false, bool p_initializer = false); + int _parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level, int p_index_addr = 0); + int _parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root = false, bool p_initializer = false, int p_index_addr = 0); Error _parse_block(CodeGen &codegen, const GDScriptParser::BlockNode *p_block, int p_stack_level = 0, int p_break_addr = -1, int p_continue_addr = -1); Error _parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false); Error _parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state); diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index e2f2b7fc3b..65c61cb57c 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -335,7 +335,9 @@ void GDScriptLanguage::debug_get_stack_level_members(int p_level, List<String> * ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) { - ERR_FAIL_COND_V(_debug_parse_err_line >= 0, NULL); + if (_debug_parse_err_line >= 0) + return NULL; + ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, NULL); int l = _debug_call_stack_pos - p_level - 1; @@ -555,7 +557,7 @@ static String _get_visual_datatype(const PropertyInfo &p_info, bool p_isarg = tr } if (p_info.type == Variant::NIL) { if (p_isarg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) { - return "var"; + return "Variant"; } else { return "void"; } @@ -1736,14 +1738,12 @@ static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx) { for (const List<PropertyInfo>::Element *E = p_info.arguments.front(); E; E = E->next()) { if (i > 0) { arghint += ", "; - } else { - arghint += " "; } if (i == p_arg_idx) { arghint += String::chr(0xFFFF); } - arghint += _get_visual_datatype(E->get(), true) + " " + E->get().name; + arghint += E->get().name + ": " + _get_visual_datatype(E->get(), true); if (i - def_args >= 0) { arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string(); @@ -1759,8 +1759,6 @@ static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx) { if (p_info.flags & METHOD_FLAG_VARARG) { if (p_info.arguments.size() > 0) { arghint += ", "; - } else { - arghint += " "; } if (p_arg_idx >= p_info.arguments.size()) { arghint += String::chr(0xFFFF); @@ -1770,9 +1768,6 @@ static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx) { arghint += String::chr(0xFFFF); } } - if (p_info.arguments.size() > 0 || (p_info.flags & METHOD_FLAG_VARARG)) { - arghint += " "; - } arghint += ")"; @@ -1787,14 +1782,12 @@ static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_functio for (int i = 0; i < p_function->arguments.size(); i++) { if (i > 0) { arghint += ", "; - } else { - arghint += " "; } if (i == p_arg_idx) { arghint += String::chr(0xFFFF); } - arghint += p_function->argument_types[i].to_string() + " " + p_function->arguments[i].operator String(); + arghint += p_function->arguments[i].operator String() + ": " + p_function->argument_types[i].to_string(); if (i - def_args >= 0) { String def_val = "<unknown>"; @@ -1818,9 +1811,6 @@ static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_functio } } - if (p_function->arguments.size() > 0) { - arghint += " "; - } arghint += ")"; return arghint; diff --git a/modules/gdscript/gdscript_function.cpp b/modules/gdscript/gdscript_function.cpp index a01a7397fe..452b1933eb 100644 --- a/modules/gdscript/gdscript_function.cpp +++ b/modules/gdscript/gdscript_function.cpp @@ -67,23 +67,23 @@ Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_insta case ADDR_TYPE_CLASS_CONSTANT: { //todo change to index! - GDScript *o = p_script; + GDScript *s = p_script; #ifdef DEBUG_ENABLED ERR_FAIL_INDEX_V(address, _global_names_count, NULL); #endif const StringName *sn = &_global_names_ptr[address]; - while (o) { - GDScript *s = o; - while (s) { + while (s) { + GDScript *o = s; + while (o) { - Map<StringName, Variant>::Element *E = s->constants.find(*sn); + Map<StringName, Variant>::Element *E = o->constants.find(*sn); if (E) { return &E->get(); } - s = s->_base; + o = o->_owner; } - o = o->_owner; + s = s->_base; } ERR_FAIL_V_MSG(NULL, "GDScriptCompiler bug."); @@ -500,6 +500,13 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Object *obj_A = *a; Object *obj_B = *b; +#ifdef DEBUG_ENABLED + if (!ObjectDB::instance_validate(obj_A)) { + err_text = "Left operand of 'is' was already freed."; + OPCODE_BREAK; + } +#endif // DEBUG_ENABLED + GDScript *scr_B = Object::cast_to<GDScript>(obj_B); if (scr_B) { @@ -1543,10 +1550,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE_BREAK; } -// Enable for debugging -#if 0 +#if 0 // Enable for debugging. default: { - err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip); OPCODE_BREAK; } diff --git a/modules/gdscript/gdscript_function.h b/modules/gdscript/gdscript_function.h index 236ca720a2..ad95ebc543 100644 --- a/modules/gdscript/gdscript_function.h +++ b/modules/gdscript/gdscript_function.h @@ -76,14 +76,17 @@ struct GDScriptDataType { if (p_variant.get_type() != Variant::OBJECT) { return false; } + Object *obj = p_variant.operator Object *(); - if (obj) { - if (!ClassDB::is_parent_class(obj->get_class_name(), native_type)) { - // Try with underscore prefix - StringName underscore_native_type = "_" + native_type; - if (!ClassDB::is_parent_class(obj->get_class_name(), underscore_native_type)) { - return false; - } + if (!obj || !ObjectDB::instance_validate(obj)) { + return false; + } + + if (!ClassDB::is_parent_class(obj->get_class_name(), native_type)) { + // Try with underscore prefix + StringName underscore_native_type = "_" + native_type; + if (!ClassDB::is_parent_class(obj->get_class_name(), underscore_native_type)) { + return false; } } return true; @@ -96,7 +99,12 @@ struct GDScriptDataType { if (p_variant.get_type() != Variant::OBJECT) { return false; } + Object *obj = p_variant.operator Object *(); + if (!obj || !ObjectDB::instance_validate(obj)) { + return false; + } + Ref<Script> base = obj && obj->get_script_instance() ? obj->get_script_instance()->get_script() : NULL; bool valid = false; while (base.is_valid()) { diff --git a/modules/gdscript/gdscript_functions.cpp b/modules/gdscript/gdscript_functions.cpp index e0e95f259e..01d62a1c62 100644 --- a/modules/gdscript/gdscript_functions.cpp +++ b/modules/gdscript/gdscript_functions.cpp @@ -2057,12 +2057,13 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) { mi.return_val.type = Variant::BOOL; return mi; } break; - case FUNC_MAX: { + default: { ERR_FAIL_V(MethodInfo()); } break; } #endif - - return MethodInfo(); + MethodInfo mi; + mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; + return mi; } diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 112569a8d6..d125da5b79 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -1868,6 +1868,10 @@ GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to _set_error("Can't assign to constant", tokenizer->get_token_line() - 1); error_line = op->line; return op; + } else if (op->arguments[0]->type == Node::TYPE_SELF) { + _set_error("Can't assign to self.", op->line); + error_line = op->line; + return op; } if (op->arguments[0]->type == Node::TYPE_OPERATOR) { @@ -2689,6 +2693,7 @@ void GDScriptParser::_transform_match_statment(MatchNode *p_match_statement) { op->op = OperatorNode::OP_ASSIGN; op->arguments.push_back(id2); op->arguments.push_back(local_var->assign); + local_var->assign_op = op; branch->body->statements.push_front(op); branch->body->statements.push_front(local_var); @@ -2866,7 +2871,6 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { assigned = _get_default_value_for_type(lv->datatype, var_line); } - lv->assign = assigned; //must be added later, to avoid self-referencing. p_block->variables.insert(n, lv); @@ -4741,10 +4745,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { member.line = tokenizer->get_token_line(); member.usages = 0; member.rpc_mode = rpc_mode; -#ifdef TOOLS_ENABLED - Variant::CallError ce; - member.default_value = Variant::construct(member._export.type, NULL, 0, ce); -#endif if (current_class->constant_expressions.has(member.identifier)) { _set_error("A constant named \"" + String(member.identifier) + "\" already exists in this class (at line: " + @@ -4797,6 +4797,32 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { } } + if (autoexport && member.data_type.has_type) { + if (member.data_type.kind == DataType::BUILTIN) { + member._export.type = member.data_type.builtin_type; + } else if (member.data_type.kind == DataType::NATIVE) { + if (ClassDB::is_parent_class(member.data_type.native_type, "Resource")) { + member._export.type = Variant::OBJECT; + member._export.hint = PROPERTY_HINT_RESOURCE_TYPE; + member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE; + member._export.hint_string = member.data_type.native_type; + member._export.class_name = member.data_type.native_type; + } else { + _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line); + return; + } + + } else { + _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line); + return; + } + } + +#ifdef TOOLS_ENABLED + Variant::CallError ce; + member.default_value = Variant::construct(member._export.type, NULL, 0, ce); +#endif + if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) { #ifdef DEBUG_ENABLED @@ -4930,27 +4956,6 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { member.initial_assignment = op; } - if (autoexport && member.data_type.has_type) { - if (member.data_type.kind == DataType::BUILTIN) { - member._export.type = member.data_type.builtin_type; - } else if (member.data_type.kind == DataType::NATIVE) { - if (ClassDB::is_parent_class(member.data_type.native_type, "Resource")) { - member._export.type = Variant::OBJECT; - member._export.hint = PROPERTY_HINT_RESOURCE_TYPE; - member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE; - member._export.hint_string = member.data_type.native_type; - member._export.class_name = member.data_type.native_type; - } else { - _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line); - return; - } - - } else { - _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line); - return; - } - } - if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_SETGET) { tokenizer->advance(); @@ -5403,12 +5408,12 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive ident += ("." + subclass); - if (base_script->get_subclasses().has(subclass)) { + if (find_subclass->get_subclasses().has(subclass)) { - find_subclass = base_script->get_subclasses()[subclass]; - } else if (base_script->get_constants().has(subclass)) { + find_subclass = find_subclass->get_subclasses()[subclass]; + } else if (find_subclass->get_constants().has(subclass)) { - Ref<GDScript> new_base_class = base_script->get_constants()[subclass]; + Ref<GDScript> new_base_class = find_subclass->get_constants()[subclass]; if (new_base_class.is_null()) { _set_error("Constant isn't a class: " + ident, p_class->line); return; @@ -6289,6 +6294,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { node_type.has_type = true; node_type.kind = DataType::CLASS; node_type.class_type = current_class; + node_type.is_constant = true; } break; case Node::TYPE_IDENTIFIER: { IdentifierNode *id = static_cast<IdentifierNode *>(p_node); @@ -6965,6 +6971,17 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat if (error_set) return DataType(); + // Special case: check copy constructor. Those are defined implicitly in Variant. + if (par_types.size() == 1) { + if (!par_types[0].has_type || (par_types[0].kind == DataType::BUILTIN && par_types[0].builtin_type == tn->vtype)) { + DataType result; + result.has_type = true; + result.kind = DataType::BUILTIN; + result.builtin_type = tn->vtype; + return result; + } + } + bool match = false; List<MethodInfo> constructors; Variant::get_constructor_list(tn->vtype, &constructors); @@ -7035,12 +7052,10 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat return_type = _type_from_property(mi.return_val, false); -#ifdef DEBUG_ENABLED // Check all arguments beforehand to solve warnings for (int i = 1; i < p_call->arguments.size(); i++) { _reduce_node_type(p_call->arguments[i]); } -#endif // DEBUG_ENABLED // Check arguments @@ -7068,12 +7083,10 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat ERR_FAIL_V(DataType()); } -#ifdef DEBUG_ENABLED // Check all arguments beforehand to solve warnings for (int i = arg_id + 1; i < p_call->arguments.size(); i++) { _reduce_node_type(p_call->arguments[i]); } -#endif // DEBUG_ENABLED IdentifierNode *func_id = static_cast<IdentifierNode *>(p_call->arguments[arg_id]); callee_name = func_id->name; @@ -7637,6 +7650,11 @@ GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType void GDScriptParser::_check_class_level_types(ClassNode *p_class) { + // Names of internal object properties that we check to avoid overriding them. + // "__meta__" could also be in here, but since it doesn't really affect object metadata, + // it is okay to override it on script. + StringName script_name = CoreStringNames::get_singleton()->_script; + _mark_line_as_safe(p_class->line); // Constants @@ -7657,8 +7675,9 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) { c.expression->set_datatype(expr); DataType tmp; - if (_get_member_type(p_class->base_type, E->key(), tmp)) { - _set_error("The member \"" + String(E->key()) + "\" already exists in a parent class.", c.expression->line); + const StringName &constant_name = E->key(); + if (constant_name == script_name || _get_member_type(p_class->base_type, constant_name, tmp)) { + _set_error("The member \"" + String(constant_name) + "\" already exists in a parent class.", c.expression->line); return; } } @@ -7679,7 +7698,7 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) { ClassNode::Member &v = p_class->variables.write[i]; DataType tmp; - if (_get_member_type(p_class->base_type, v.identifier, tmp)) { + if (v.identifier == script_name || _get_member_type(p_class->base_type, v.identifier, tmp)) { _set_error("The member \"" + String(v.identifier) + "\" already exists in a parent class.", v.line); return; } @@ -7856,12 +7875,12 @@ void GDScriptParser::_check_function_types(FunctionNode *p_function) { def_type.is_constant = false; p_function->argument_types.write[i] = def_type; } else { - p_function->return_type = _resolve_type(p_function->return_type, p_function->line); + p_function->argument_types.write[i] = _resolve_type(p_function->argument_types[i], p_function->line); if (!_is_type_compatible(p_function->argument_types[i], def_type, true)) { String arg_name = p_function->arguments[i]; _set_error("Value type (" + def_type.to_string() + ") doesn't match the type of argument '" + - arg_name + "' (" + p_function->arguments[i] + ").", + arg_name + "' (" + p_function->argument_types[i].to_string() + ").", p_function->line); } } @@ -8047,6 +8066,7 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) { last_var_assign = lv->assign; if (lv->assign) { + lv->assign_op->arguments[0]->set_datatype(lv->datatype); DataType assign_type = _reduce_node_type(lv->assign); #ifdef DEBUG_ENABLED if (assign_type.has_type && assign_type.kind == DataType::BUILTIN && assign_type.builtin_type == Variant::NIL) { @@ -8191,11 +8211,12 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) { if (lh_type.has_type && rh_type.may_yield && op->arguments[1]->type == Node::TYPE_OPERATOR) { _add_warning(GDScriptWarning::FUNCTION_MAY_YIELD, op->line, _find_function_name(static_cast<OperatorNode *>(op->arguments[1]))); } -#endif // DEBUG_ENABLED - bool type_match = check_types; +#endif // DEBUG_ENABLED + bool type_match = lh_type.has_type && rh_type.has_type; if (check_types && !_is_type_compatible(lh_type, rh_type)) { type_match = false; + // Try supertype test if (_is_type_compatible(rh_type, lh_type)) { _mark_line_as_unsafe(op->line); @@ -8266,7 +8287,11 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) { _mark_line_as_safe(op->line); _reduce_node_type(op); // Test for safety anyway #ifdef DEBUG_ENABLED - _add_warning(GDScriptWarning::STANDALONE_EXPRESSION, statement->line); + if (op->op == OperatorNode::OP_TERNARY_IF) { + _add_warning(GDScriptWarning::STANDALONE_TERNARY, statement->line); + } else { + _add_warning(GDScriptWarning::STANDALONE_EXPRESSION, statement->line); + } #endif // DEBUG_ENABLED } } @@ -8483,11 +8508,8 @@ Error GDScriptParser::_parse(const String &p_base_path) { current_class = main_class; current_function = NULL; current_block = NULL; -#ifdef DEBUG_ENABLED + if (for_completion) check_types = false; -#else - check_types = false; -#endif // Resolve all class-level stuff before getting into function blocks _check_class_level_types(main_class); diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp index 8287c9c084..0f6f13944b 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.cpp +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -112,9 +112,10 @@ void ExtendGDScriptParser::update_document_links(const String &p_code) { FileAccessRef fs = FileAccess::create(FileAccess::ACCESS_RESOURCES); tokenizer.set_code(p_code); while (true) { - if (tokenizer.get_token() == GDScriptTokenizer::TK_EOF) { + GDScriptTokenizerText::Token token = tokenizer.get_token(); + if (token == GDScriptTokenizer::TK_EOF || token == GDScriptTokenizer::TK_ERROR) { break; - } else if (tokenizer.get_token() == GDScriptTokenizer::TK_CONSTANT) { + } else if (token == GDScriptTokenizer::TK_CONSTANT) { const Variant &const_val = tokenizer.get_token_constant(); if (const_val.get_type() == Variant::STRING) { String path = const_val; @@ -522,6 +523,51 @@ const lsp::DocumentSymbol *ExtendGDScriptParser::search_symbol_defined_at_line(i return ret; } +Error ExtendGDScriptParser::get_left_function_call(const lsp::Position &p_position, lsp::Position &r_func_pos, int &r_arg_index) const { + + ERR_FAIL_INDEX_V(p_position.line, lines.size(), ERR_INVALID_PARAMETER); + + int bracket_stack = 0; + int index = 0; + + bool found = false; + for (int l = p_position.line; l >= 0; --l) { + String line = lines[l]; + int c = line.length() - 1; + if (l == p_position.line) { + c = MIN(c, p_position.character - 1); + } + + while (c >= 0) { + const CharType &character = line[c]; + if (character == ')') { + ++bracket_stack; + } else if (character == '(') { + --bracket_stack; + if (bracket_stack < 0) { + found = true; + } + } + if (bracket_stack <= 0 && character == ',') { + ++index; + } + --c; + if (found) { + r_func_pos.character = c; + break; + } + } + + if (found) { + r_func_pos.line = l; + r_arg_index = index; + return OK; + } + } + + return ERR_METHOD_NOT_FOUND; +} + const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const { if (p_line <= 0) { return &class_symbol; @@ -711,7 +757,7 @@ Dictionary ExtendGDScriptParser::dump_class_api(const GDScriptParser::ClassNode Array static_functions; for (int i = 0; i < p_class->static_functions.size(); ++i) { - static_functions.append(dump_function_api(p_class->functions[i])); + static_functions.append(dump_function_api(p_class->static_functions[i])); } class_api["static_functions"] = static_functions; diff --git a/modules/gdscript/language_server/gdscript_extend_parser.h b/modules/gdscript/language_server/gdscript_extend_parser.h index 5afe991d24..43dfce994b 100644 --- a/modules/gdscript/language_server/gdscript_extend_parser.h +++ b/modules/gdscript/language_server/gdscript_extend_parser.h @@ -83,6 +83,8 @@ public: _FORCE_INLINE_ const ClassMembers &get_members() const { return members; } _FORCE_INLINE_ const HashMap<String, ClassMembers> &get_inner_classes() const { return inner_classes; } + Error get_left_function_call(const lsp::Position &p_position, lsp::Position &r_func_pos, int &r_arg_index) const; + String get_text_for_completion(const lsp::Position &p_cursor) const; String get_text_for_lookup_symbol(const lsp::Position &p_cursor, const String &p_symbol = "", bool p_func_requred = false) const; String get_identifier_under_position(const lsp::Position &p_position, Vector2i &p_offset) const; diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp index cff7653d3a..7133c6b4be 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.cpp +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -156,7 +156,7 @@ void GDScriptLanguageProtocol::poll() { server->poll(); } -Error GDScriptLanguageProtocol::start(int p_port) { +Error GDScriptLanguageProtocol::start(int p_port, const IP_Address &p_bind_ip) { if (server == NULL) { server = dynamic_cast<WebSocketServer *>(ClassDB::instance("WebSocketServer")); ERR_FAIL_COND_V(!server, FAILED); @@ -165,6 +165,7 @@ Error GDScriptLanguageProtocol::start(int p_port) { server->connect("client_connected", this, "on_client_connected"); server->connect("client_disconnected", this, "on_client_disconnected"); } + server->set_bind_ip(p_bind_ip); return server->listen(p_port); } diff --git a/modules/gdscript/language_server/gdscript_language_protocol.h b/modules/gdscript/language_server/gdscript_language_protocol.h index e45db274e9..52c680ab19 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.h +++ b/modules/gdscript/language_server/gdscript_language_protocol.h @@ -77,7 +77,7 @@ public: _FORCE_INLINE_ bool is_initialized() const { return _initialized; } void poll(); - Error start(int p_port); + Error start(int p_port, const IP_Address &p_bind_ip); void stop(); void notify_all_clients(const String &p_method, const Variant &p_params = Variant()); diff --git a/modules/gdscript/language_server/gdscript_language_server.cpp b/modules/gdscript/language_server/gdscript_language_server.cpp index 159f2ba2d4..7170c63058 100644 --- a/modules/gdscript/language_server/gdscript_language_server.cpp +++ b/modules/gdscript/language_server/gdscript_language_server.cpp @@ -36,10 +36,15 @@ GDScriptLanguageServer::GDScriptLanguageServer() { thread = NULL; - thread_exit = false; - _EDITOR_DEF("network/language_server/remote_port", 6008); - _EDITOR_DEF("network/language_server/enable_smart_resolve", false); + thread_running = false; + started = false; + + use_thread = false; + port = 6008; + _EDITOR_DEF("network/language_server/remote_port", port); + _EDITOR_DEF("network/language_server/enable_smart_resolve", true); _EDITOR_DEF("network/language_server/show_native_symbols_in_editor", false); + _EDITOR_DEF("network/language_server/use_thread", use_thread); } void GDScriptLanguageServer::_notification(int p_what) { @@ -51,12 +56,25 @@ void GDScriptLanguageServer::_notification(int p_what) { case NOTIFICATION_EXIT_TREE: stop(); break; + case NOTIFICATION_INTERNAL_PROCESS: { + if (started && !use_thread) { + protocol.poll(); + } + } break; + case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { + int port = (int)_EDITOR_GET("network/language_server/remote_port"); + bool use_thread = (bool)_EDITOR_GET("network/language_server/use_thread"); + if (port != this->port || use_thread != this->use_thread) { + this->stop(); + this->start(); + } + } break; } } void GDScriptLanguageServer::thread_main(void *p_userdata) { GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata); - while (!self->thread_exit) { + while (self->thread_running) { // Poll 20 times per second self->protocol.poll(); OS::get_singleton()->delay_usec(50000); @@ -64,22 +82,30 @@ void GDScriptLanguageServer::thread_main(void *p_userdata) { } void GDScriptLanguageServer::start() { - int port = (int)_EDITOR_GET("network/language_server/remote_port"); - if (protocol.start(port) == OK) { + port = (int)_EDITOR_GET("network/language_server/remote_port"); + use_thread = (bool)_EDITOR_GET("network/language_server/use_thread"); + if (protocol.start(port, IP_Address("127.0.0.1")) == OK) { EditorNode::get_log()->add_message("--- GDScript language server started ---", EditorLog::MSG_TYPE_EDITOR); - ERR_FAIL_COND(thread != NULL || thread_exit); - thread_exit = false; - thread = Thread::create(GDScriptLanguageServer::thread_main, this); + if (use_thread) { + ERR_FAIL_COND(thread != NULL); + thread_running = true; + thread = Thread::create(GDScriptLanguageServer::thread_main, this); + } + set_process_internal(!use_thread); + started = true; } } void GDScriptLanguageServer::stop() { - ERR_FAIL_COND(NULL == thread || thread_exit); - thread_exit = true; - Thread::wait_to_finish(thread); - memdelete(thread); - thread = NULL; + if (use_thread) { + ERR_FAIL_COND(NULL == thread); + thread_running = false; + Thread::wait_to_finish(thread); + memdelete(thread); + thread = NULL; + } protocol.stop(); + started = false; EditorNode::get_log()->add_message("--- GDScript language server stopped ---", EditorLog::MSG_TYPE_EDITOR); } diff --git a/modules/gdscript/language_server/gdscript_language_server.h b/modules/gdscript/language_server/gdscript_language_server.h index 191b8bfa85..228d29bf42 100644 --- a/modules/gdscript/language_server/gdscript_language_server.h +++ b/modules/gdscript/language_server/gdscript_language_server.h @@ -41,7 +41,10 @@ class GDScriptLanguageServer : public EditorPlugin { GDScriptLanguageProtocol protocol; Thread *thread; - bool thread_exit; + bool thread_running; + bool started; + bool use_thread; + int port; static void thread_main(void *p_userdata); private: diff --git a/modules/gdscript/language_server/gdscript_text_document.cpp b/modules/gdscript/language_server/gdscript_text_document.cpp index b3d1b67af5..0572c5f746 100644 --- a/modules/gdscript/language_server/gdscript_text_document.cpp +++ b/modules/gdscript/language_server/gdscript_text_document.cpp @@ -50,6 +50,7 @@ void GDScriptTextDocument::_bind_methods() { ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover); ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition); ClassDB::bind_method(D_METHOD("declaration"), &GDScriptTextDocument::declaration); + ClassDB::bind_method(D_METHOD("signatureHelp"), &GDScriptTextDocument::signatureHelp); ClassDB::bind_method(D_METHOD("show_native_symbol_in_editor"), &GDScriptTextDocument::show_native_symbol_in_editor); } @@ -268,7 +269,7 @@ Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) { if ((item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function) && !item.label.ends_with("):")) { item.insertText = item.label + "("; - if (symbol && symbol->detail.find(",") == -1) { + if (symbol && symbol->children.empty()) { item.insertText += ")"; } } else if (item.kind == lsp::CompletionItemKind::Event) { @@ -387,6 +388,20 @@ Variant GDScriptTextDocument::declaration(const Dictionary &p_params) { return arr; } +Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) { + Variant ret; + + lsp::TextDocumentPositionParams params; + params.load(p_params); + + lsp::SignatureHelp s; + if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) { + ret = s.to_json(); + } + + return ret; +} + GDScriptTextDocument::GDScriptTextDocument() { file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES); } diff --git a/modules/gdscript/language_server/gdscript_text_document.h b/modules/gdscript/language_server/gdscript_text_document.h index d93d828003..b2fd0c31f9 100644 --- a/modules/gdscript/language_server/gdscript_text_document.h +++ b/modules/gdscript/language_server/gdscript_text_document.h @@ -67,6 +67,7 @@ public: Variant hover(const Dictionary &p_params); Array definition(const Dictionary &p_params); Variant declaration(const Dictionary &p_params); + Variant signatureHelp(const Dictionary &p_params); void initialize(); diff --git a/modules/gdscript/language_server/gdscript_workspace.cpp b/modules/gdscript/language_server/gdscript_workspace.cpp index 0661c4af26..1c0590cff1 100644 --- a/modules/gdscript/language_server/gdscript_workspace.cpp +++ b/modules/gdscript/language_server/gdscript_workspace.cpp @@ -252,6 +252,12 @@ Error GDScriptWorkspace::initialize() { bool arg_default_value_started = false; for (int j = 0; j < data.arguments.size(); j++) { const DocData::ArgumentDoc &arg = data.arguments[j]; + + lsp::DocumentSymbol symbol_arg; + symbol_arg.name = arg.name; + symbol_arg.kind = lsp::SymbolKind::Variable; + symbol_arg.detail = arg.type; + if (!arg_default_value_started && !arg.default_value.empty()) { arg_default_value_started = true; } @@ -263,6 +269,8 @@ Error GDScriptWorkspace::initialize() { arg_str += ", "; } params += arg_str; + + symbol.children.push_back(symbol_arg); } if (data.qualifiers.find("vararg") != -1) { params += params.empty() ? "..." : ", ..."; @@ -513,6 +521,49 @@ Dictionary GDScriptWorkspace::generate_script_api(const String &p_path) { return api; } +Error GDScriptWorkspace::resolve_signature(const lsp::TextDocumentPositionParams &p_doc_pos, lsp::SignatureHelp &r_signature) { + if (const ExtendGDScriptParser *parser = get_parse_result(get_file_path(p_doc_pos.textDocument.uri))) { + + lsp::TextDocumentPositionParams text_pos; + text_pos.textDocument = p_doc_pos.textDocument; + + if (parser->get_left_function_call(p_doc_pos.position, text_pos.position, r_signature.activeParameter) == OK) { + + List<const lsp::DocumentSymbol *> symbols; + + if (const lsp::DocumentSymbol *symbol = resolve_symbol(text_pos)) { + symbols.push_back(symbol); + } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { + GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(text_pos, symbols); + } + + for (List<const lsp::DocumentSymbol *>::Element *E = symbols.front(); E; E = E->next()) { + const lsp::DocumentSymbol *symbol = E->get(); + if (symbol->kind == lsp::SymbolKind::Method || symbol->kind == lsp::SymbolKind::Function) { + + lsp::SignatureInformation signature_info; + signature_info.label = symbol->detail; + signature_info.documentation = symbol->render(); + + for (int i = 0; i < symbol->children.size(); i++) { + const lsp::DocumentSymbol &arg = symbol->children[i]; + lsp::ParameterInformation arg_info; + arg_info.label = arg.name; + signature_info.parameters.push_back(arg_info); + } + r_signature.signatures.push_back(signature_info); + break; + } + } + + if (r_signature.signatures.size()) { + return OK; + } + } + } + return ERR_METHOD_NOT_FOUND; +} + GDScriptWorkspace::GDScriptWorkspace() { ProjectSettings::get_singleton()->get_resource_path(); } diff --git a/modules/gdscript/language_server/gdscript_workspace.h b/modules/gdscript/language_server/gdscript_workspace.h index f44153dcd0..146a5cb7c9 100644 --- a/modules/gdscript/language_server/gdscript_workspace.h +++ b/modules/gdscript/language_server/gdscript_workspace.h @@ -83,6 +83,7 @@ public: const lsp::DocumentSymbol *resolve_native_symbol(const lsp::NativeSymbolInspectParams &p_params); void resolve_document_links(const String &p_uri, List<lsp::DocumentLink> &r_list); Dictionary generate_script_api(const String &p_path); + Error resolve_signature(const lsp::TextDocumentPositionParams &p_doc_pos, lsp::SignatureHelp &r_signature); GDScriptWorkspace(); ~GDScriptWorkspace(); diff --git a/modules/gdscript/language_server/lsp.hpp b/modules/gdscript/language_server/lsp.hpp index ffeea70008..a2dcc48820 100644 --- a/modules/gdscript/language_server/lsp.hpp +++ b/modules/gdscript/language_server/lsp.hpp @@ -330,8 +330,6 @@ struct CompletionOptions { triggerCharacters.push_back("$"); triggerCharacters.push_back("'"); triggerCharacters.push_back("\""); - triggerCharacters.push_back("("); - triggerCharacters.push_back(","); } Dictionary to_json() const { @@ -1402,6 +1400,120 @@ struct Hover { } }; +/** + * Represents a parameter of a callable-signature. A parameter can + * have a label and a doc-comment. + */ +struct ParameterInformation { + + /** + * The label of this parameter information. + * + * Either a string or an inclusive start and exclusive end offsets within its containing + * signature label. (see SignatureInformation.label). The offsets are based on a UTF-16 + * string representation as `Position` and `Range` does. + * + * *Note*: a label of type string should be a substring of its containing signature label. + * Its intended use case is to highlight the parameter label part in the `SignatureInformation.label`. + */ + String label; + + /** + * The human-readable doc-comment of this parameter. Will be shown + * in the UI but can be omitted. + */ + MarkupContent documentation; + + Dictionary to_json() const { + Dictionary dict; + dict["label"] = label; + dict["documentation"] = documentation.to_json(); + return dict; + } +}; + +/** + * Represents the signature of something callable. A signature + * can have a label, like a function-name, a doc-comment, and + * a set of parameters. + */ +struct SignatureInformation { + /** + * The label of this signature. Will be shown in + * the UI. + */ + String label; + + /** + * The human-readable doc-comment of this signature. Will be shown + * in the UI but can be omitted. + */ + MarkupContent documentation; + + /** + * The parameters of this signature. + */ + Vector<ParameterInformation> parameters; + + Dictionary to_json() const { + Dictionary dict; + dict["label"] = label; + dict["documentation"] = documentation.to_json(); + Array args; + for (int i = 0; i < parameters.size(); i++) { + args.push_back(parameters[i].to_json()); + } + dict["parameters"] = args; + return dict; + } +}; + +/** + * Signature help represents the signature of something + * callable. There can be multiple signature but only one + * active and only one active parameter. + */ +struct SignatureHelp { + /** + * One or more signatures. + */ + Vector<SignatureInformation> signatures; + + /** + * The active signature. If omitted or the value lies outside the + * range of `signatures` the value defaults to zero or is ignored if + * `signatures.length === 0`. Whenever possible implementors should + * make an active decision about the active signature and shouldn't + * rely on a default value. + * In future version of the protocol this property might become + * mandatory to better express this. + */ + int activeSignature = 0; + + /** + * The active parameter of the active signature. If omitted or the value + * lies outside the range of `signatures[activeSignature].parameters` + * defaults to 0 if the active signature has parameters. If + * the active signature has no parameters it is ignored. + * In future version of the protocol this property might become + * mandatory to better express the active parameter if the + * active signature does have any. + */ + int activeParameter = 0; + + Dictionary to_json() const { + Dictionary dict; + Array sigs; + for (int i = 0; i < signatures.size(); i++) { + sigs.push_back(signatures[i].to_json()); + } + dict["signatures"] = sigs; + dict["activeSignature"] = activeSignature; + dict["activeParameter"] = activeParameter; + return dict; + } +}; + struct ServerCapabilities { /** * Defines how text documents are synced. Is either a detailed structure defining each notification or @@ -1532,6 +1644,8 @@ struct ServerCapabilities { Dictionary dict; dict["textDocumentSync"] = (int)textDocumentSync.change; dict["completionProvider"] = completionProvider.to_json(); + signatureHelpProvider.triggerCharacters.push_back(","); + signatureHelpProvider.triggerCharacters.push_back("("); dict["signatureHelpProvider"] = signatureHelpProvider.to_json(); dict["codeLensProvider"] = false; // codeLensProvider.to_json(); dict["documentOnTypeFormattingProvider"] = documentOnTypeFormattingProvider.to_json(); |