Skip to content

Commit

Permalink
Merge pull request #88853 from dalexeev/gds-canonicalize-fqcn
Browse files Browse the repository at this point in the history
GDScript: Canonicalize script path in FQCN
  • Loading branch information
akien-mga committed Feb 26, 2024
2 parents 924e579 + 8ffc50e commit 99addd6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
13 changes: 4 additions & 9 deletions modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ class GDScript : public Script {
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; }

Expand Down
6 changes: 3 additions & 3 deletions modules/gdscript/gdscript_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions modules/gdscript/language_server/gdscript_workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 99addd6

Please sign in to comment.