Browse Source

Merge pull request #88853 from dalexeev/gds-canonicalize-fqcn

GDScript: Canonicalize script path in FQCN
Rémi Verschelde 1 year ago
parent
commit
99addd6d9c

+ 4 - 9
modules/gdscript/gdscript.cpp

@@ -1405,16 +1405,11 @@ String GDScript::debug_get_script_name(const Ref<Script> &p_script) {
 }
 #endif
 
-bool GDScript::is_equal_gdscript_paths(const String &p_path_a, const String &p_path_b) {
-	String path_a = p_path_a;
-	if (path_a.get_extension() == "gdc") {
-		path_a = path_a.get_basename() + ".gd";
+String GDScript::canonicalize_path(const String &p_path) {
+	if (p_path.get_extension() == "gdc") {
+		return p_path.get_basename() + ".gd";
 	}
-	String path_b = p_path_b;
-	if (path_b.get_extension() == "gdc") {
-		path_b = path_b.get_basename() + ".gd";
-	}
-	return path_a == path_b;
+	return p_path;
 }
 
 GDScript::UpdatableFuncPtr::UpdatableFuncPtr(GDScriptFunction *p_function) {

+ 4 - 1
modules/gdscript/gdscript.h

@@ -230,7 +230,10 @@ public:
 	static String debug_get_script_name(const Ref<Script> &p_script);
 #endif
 
-	static bool is_equal_gdscript_paths(const String &p_path_a, const String &p_path_b);
+	static String canonicalize_path(const String &p_path);
+	_FORCE_INLINE_ static bool is_canonically_equal_paths(const String &p_path_a, const String &p_path_b) {
+		return canonicalize_path(p_path_a) == canonicalize_path(p_path_b);
+	}
 
 	_FORCE_INLINE_ StringName get_local_name() const { return local_name; }
 

+ 3 - 3
modules/gdscript/gdscript_analyzer.cpp

@@ -361,7 +361,7 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c
 			push_error(vformat(R"(Class "%s" hides a built-in type.)", class_name), p_class->identifier);
 		} else if (class_exists(class_name)) {
 			push_error(vformat(R"(Class "%s" hides a native class.)", class_name), p_class->identifier);
-		} else if (ScriptServer::is_global_class(class_name) && (!GDScript::is_equal_gdscript_paths(ScriptServer::get_global_class_path(class_name), parser->script_path) || p_class != parser->head)) {
+		} else if (ScriptServer::is_global_class(class_name) && (!GDScript::is_canonically_equal_paths(ScriptServer::get_global_class_path(class_name), parser->script_path) || p_class != parser->head)) {
 			push_error(vformat(R"(Class "%s" hides a global script class.)", class_name), p_class->identifier);
 		} else if (ProjectSettings::get_singleton()->has_autoload(class_name) && ProjectSettings::get_singleton()->get_autoload(class_name).is_singleton) {
 			push_error(vformat(R"(Class "%s" hides an autoload singleton.)", class_name), p_class->identifier);
@@ -425,7 +425,7 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c
 			if (ScriptServer::is_global_class(name)) {
 				String base_path = ScriptServer::get_global_class_path(name);
 
-				if (GDScript::is_equal_gdscript_paths(base_path, parser->script_path)) {
+				if (GDScript::is_canonically_equal_paths(base_path, parser->script_path)) {
 					base = parser->head->get_datatype();
 				} else {
 					Ref<GDScriptParserRef> base_parser = get_parser_for(base_path);
@@ -698,7 +698,7 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type
 			result.builtin_type = Variant::OBJECT;
 			result.native_type = first;
 		} else if (ScriptServer::is_global_class(first)) {
-			if (GDScript::is_equal_gdscript_paths(parser->script_path, ScriptServer::get_global_class_path(first))) {
+			if (GDScript::is_canonically_equal_paths(parser->script_path, ScriptServer::get_global_class_path(first))) {
 				result = parser->head->get_datatype();
 			} else {
 				String path = ScriptServer::get_global_class_path(first);

+ 2 - 2
modules/gdscript/gdscript_parser.cpp

@@ -551,7 +551,7 @@ void GDScriptParser::end_statement(const String &p_context) {
 
 void GDScriptParser::parse_program() {
 	head = alloc_node<ClassNode>();
-	head->fqcn = script_path;
+	head->fqcn = GDScript::canonicalize_path(script_path);
 	current_class = head;
 	bool can_have_class_or_extends = true;
 
@@ -709,7 +709,7 @@ GDScriptParser::ClassNode *GDScriptParser::parse_class(bool p_is_static) {
 		if (n_class->outer) {
 			String fqcn = n_class->outer->fqcn;
 			if (fqcn.is_empty()) {
-				fqcn = script_path;
+				fqcn = GDScript::canonicalize_path(script_path);
 			}
 			n_class->fqcn = fqcn + "::" + n_class->identifier->name;
 		} else {

+ 2 - 2
modules/gdscript/language_server/gdscript_workspace.cpp

@@ -641,7 +641,7 @@ void GDScriptWorkspace::completion(const lsp::CompletionParams &p_params, List<S
 			while (!stack.is_empty()) {
 				current = Object::cast_to<Node>(stack.pop_back());
 				Ref<GDScript> scr = current->get_script();
-				if (scr.is_valid() && GDScript::is_equal_gdscript_paths(scr->get_path(), path)) {
+				if (scr.is_valid() && GDScript::is_canonically_equal_paths(scr->get_path(), path)) {
 					break;
 				}
 				for (int i = 0; i < current->get_child_count(); ++i) {
@@ -650,7 +650,7 @@ void GDScriptWorkspace::completion(const lsp::CompletionParams &p_params, List<S
 			}
 
 			Ref<GDScript> scr = current->get_script();
-			if (!scr.is_valid() || !GDScript::is_equal_gdscript_paths(scr->get_path(), path)) {
+			if (!scr.is_valid() || !GDScript::is_canonically_equal_paths(scr->get_path(), path)) {
 				current = owner_scene_node;
 			}
 		}