godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.6k stars 21.1k forks source link

GDScriptLanguageProtocol singleton not properly recognized as identifier #52162

Open Gallilus opened 3 years ago

Gallilus commented 3 years ago

Godot version

3.4.beta3

System information

Windows 10

Issue description

NavigationMeshGenerator singleton is not displayed green. GDScriptLanguageProtocol singleton Identifier not found: GDScriptLanguageProtocol image

Steps to reproduce

var engine_singletons := [
        Performance,
        ProjectSettings,
        IP,
        Geometry,
        ResourceFormatLoader,
        ResourceFormatSaver,
        OS,
        Engine,
        ClassDB,
        Marshalls,
        TranslationServer,
        Input,
        InputMap,
        JSON,
        JavaClassWrapper,
        JavaScript,
        NavigationMeshGenerator,
        VisualScriptEditor,
        VisualServer,
        PhysicsServer,
        Physics2DServer,
        ARVRServer,
        CameraServer,
        #GDScriptLanguageProtocol,
]
#   print(Engine.has_singleton("NavigationMeshGenerator")) # prints True
#   print(Engine.has_singleton("GDScriptLanguageProtocol")) # prints True

Minimal reproduction project

N-A

akien-mga commented 3 years ago

NavigationMeshGenerator singleton is not displayed green.

This is because NavigationMeshGenerator is not a class name, as the class of that singleton property is actually EditorNavigationMeshGenerator. That's the only case of a discrepancy between the singleton name and the class name, which is why it's the only one not displayed correctly.

The syntax highlighter should likely have custom code to handle singletons specifically, and not just classes.

GDScriptLanguageProtocol singleton Identifier not found: GDScriptLanguageProtocol

That one is weird as it seems to be properly registered as singleton. It's registered in the Core API when it should likely be in the Editor API only (only available in tool scripts), which can be fixed with this patch:

diff --git a/modules/gdscript/language_server/gdscript_language_server.cpp b/modules/gdscript/language_server/gdscript_language_server.cpp
index 12ed56a568..63b305ad82 100644
--- a/modules/gdscript/language_server/gdscript_language_server.cpp
+++ b/modules/gdscript/language_server/gdscript_language_server.cpp
@@ -106,7 +106,9 @@ void GDScriptLanguageServer::stop() {
 }

 void register_lsp_types() {
+       ClassDB::set_current_api(ClassDB::API_EDITOR);
        ClassDB::register_class<GDScriptLanguageProtocol>();
        ClassDB::register_class<GDScriptTextDocument>();
        ClassDB::register_class<GDScriptWorkspace>();
+       ClassDB::set_current_api(ClassDB::API_CORE);
 }

But that doesn't solve the GDScript error when trying to use the identifier in a tool script. Maybe the registration happens too late? CC @vnen

akien-mga commented 3 years ago

The GDScriptLanguageProtocol issue is valid in master too.

The NavigationMeshGenerator highlighting seems solved in master (the class was renamed) and should thus likely not be fixed in 3.4 which would break compat.

There's now an issue with VisualScriptEditor similar to NavigationMeshGenerator, because the class was renamed and not the singleton (see #51916 and follow-ups, CC @mhilbrunner).

akien-mga commented 3 years ago

There's now an issue with VisualScriptEditor similar to NavigationMeshGenerator, because the class was renamed and not the singleton (see #51916 and follow-ups, CC @mhilbrunner).

This is fixed by #52656, so I repurposed this issue to focus only on the GDScriptLanguageProtocol issue.

vnen commented 3 years ago

It seems the issue is that this class and its singleton are registered in the _editor_init() callback:

https://github.com/godotengine/godot/blob/8a211219c73c2456b0aba1fcd38777a664d88f11/modules/gdscript/register_types.cpp#L136-L147

But this is called quite late, which makes it doesn't appear on the editor help for instance. When running the project this is never called so the class don't get registered all, hence the error.

This registration code should probably be moved to the register_gdscript_types() function.