From 935ea10f3aba865a50d6ea3304ae3bde62eeaaaf Mon Sep 17 00:00:00 2001 From: Diogo Costa Date: Fri, 15 Mar 2024 20:20:36 +0000 Subject: [PATCH] Fixed enum autocompletion for core classes (Issue #88858) Minor fix consisted in the use of [[fallthrough]] macro --- modules/gdscript/gdscript_editor.cpp | 44 ++++++++++++++++--- .../builtin_enum_autocomplete.cfg | 9 ++++ .../builtin_enum/builtin_enum_autocomplete.gd | 4 ++ .../builtin_enum_values_autocompletion.cfg | 5 +++ .../builtin_enum_values_autocompletion.gd | 4 ++ 5 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_autocomplete.cfg create mode 100644 modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_autocomplete.gd create mode 100644 modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_values_autocompletion.cfg create mode 100644 modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_values_autocompletion.gd diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index babd2c17721..56dafb4c4a0 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -651,6 +651,21 @@ static int _get_enum_constant_location(const StringName &p_class, const StringNa return depth | ScriptLanguage::LOCATION_PARENT_MASK; } +static int _get_enum_location(const StringName &p_class, const StringName &p_enum) { + if (!ClassDB::has_enum(p_class, p_enum)) { + return ScriptLanguage::LOCATION_OTHER; + } + + int depth = 0; + StringName class_test = p_class; + while (class_test && !ClassDB::has_enum(class_test, p_enum, true)) { + class_test = ClassDB::get_parent_class(class_test); + depth++; + } + + return depth | ScriptLanguage::LOCATION_PARENT_MASK; +} + // END LOCATION METHODS static String _trim_parent_class(const String &p_class, const String &p_base_class) { @@ -1198,13 +1213,15 @@ static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base return; } + List enums; + ClassDB::get_enum_list(type, &enums); + for (const StringName &E : enums) { + int location = p_recursion_depth + _get_enum_location(type, E); + ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location); + r_result.insert(option.display, option); + } + if (p_types_only) { - List enums; - ClassDB::get_enum_list(type, &enums); - for (const StringName &E : enums) { - ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM); - r_result.insert(option.display, option); - } return; } @@ -1264,7 +1281,20 @@ static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base } return; } break; - case GDScriptParser::DataType::ENUM: + case GDScriptParser::DataType::ENUM: { + String type_str = base_type.native_type; + StringName type = type_str.get_slicec('.', 0); + StringName type_enum = base_type.enum_type; + + List enum_values; + ClassDB::get_enum_constants(type, type_enum, &enum_values); + for (const StringName &E : enum_values) { + int location = p_recursion_depth + _get_enum_constant_location(type, E); + ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location); + r_result.insert(option.display, option); + } + } + [[fallthrough]]; case GDScriptParser::DataType::BUILTIN: { if (p_types_only) { return; diff --git a/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_autocomplete.cfg b/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_autocomplete.cfg new file mode 100644 index 00000000000..7c7b465f267 --- /dev/null +++ b/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_autocomplete.cfg @@ -0,0 +1,9 @@ +[output] +include=[ + {"display": "DrawMode", + "location": 256}, + {"display": "Anchor", + "location": 257}, + {"display": "TextureRepeat", + "location": 258}, +] diff --git a/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_autocomplete.gd b/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_autocomplete.gd new file mode 100644 index 00000000000..83f4b17a867 --- /dev/null +++ b/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_autocomplete.gd @@ -0,0 +1,4 @@ +extends Control + +func _ready(): + var t = BaseButton.➡ diff --git a/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_values_autocompletion.cfg b/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_values_autocompletion.cfg new file mode 100644 index 00000000000..7ccfa550e25 --- /dev/null +++ b/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_values_autocompletion.cfg @@ -0,0 +1,5 @@ +[output] +include=[ + {"display": "HEURISTIC_MAX"}, + {"display": "HEURISTIC_OCTILE"}, +] diff --git a/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_values_autocompletion.gd b/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_values_autocompletion.gd new file mode 100644 index 00000000000..99e38be6b9e --- /dev/null +++ b/modules/gdscript/tests/scripts/completion/builtin_enum/builtin_enum_values_autocompletion.gd @@ -0,0 +1,4 @@ +extends Control + +func _ready(): + AStarGrid2D.Heuristic.➡