1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:47:34 +00:00

CppLanguageServer: Make properties_of_type return any property

Previously, CppComprehensionEngine::properties_of_type only returned
variables.
This commit is contained in:
Itamar 2021-06-26 17:17:46 +03:00 committed by Ali Mohammad Pur
parent d7aa831a43
commit ccb52b005e
2 changed files with 33 additions and 16 deletions

View file

@ -206,8 +206,8 @@ Vector<GUI::AutocompleteProvider::Entry> CppComprehensionEngine::autocomplete_pr
Vector<GUI::AutocompleteProvider::Entry> suggestions; Vector<GUI::AutocompleteProvider::Entry> suggestions;
for (auto& prop : properties_of_type(document, type)) { for (auto& prop : properties_of_type(document, type)) {
if (prop.name.starts_with(partial_text)) { if (prop.name.name.starts_with(partial_text)) {
suggestions.append({ prop.name, partial_text.length(), GUI::AutocompleteProvider::CompletionKind::Identifier }); suggestions.append({ prop.name.name, partial_text.length(), GUI::AutocompleteProvider::CompletionKind::Identifier });
} }
} }
return suggestions; return suggestions;
@ -227,8 +227,18 @@ String CppComprehensionEngine::type_of_property(const DocumentData& document, co
auto& parent = (const MemberExpression&)(*identifier.parent()); auto& parent = (const MemberExpression&)(*identifier.parent());
auto properties = properties_of_type(document, type_of(document, *parent.m_object)); auto properties = properties_of_type(document, type_of(document, *parent.m_object));
for (auto& prop : properties) { for (auto& prop : properties) {
if (prop.name == identifier.m_name && prop.type->is_named_type()) if (prop.name.name != identifier.m_name)
return ((NamedType&)prop.type).m_name->full_name(); continue;
Type* type { nullptr };
if (prop.declaration->is_variable_declaration()) {
type = ((VariableDeclaration&)*prop.declaration).m_type.ptr();
}
if (!type)
continue;
if (!type->is_named_type())
continue;
return ((NamedType&)*type).m_name->full_name();
} }
return {}; return {};
} }
@ -275,9 +285,10 @@ String CppComprehensionEngine::type_of(const DocumentData& document, const Expre
return type_of_variable(*identifier); return type_of_variable(*identifier);
} }
Vector<CppComprehensionEngine::PropertyInfo> CppComprehensionEngine::properties_of_type(const DocumentData& document, const String& type) const Vector<CppComprehensionEngine::Symbol> CppComprehensionEngine::properties_of_type(const DocumentData& document, const String& type) const
{ {
auto decl = find_declaration_of(document, SymbolName::create(type, {})); auto type_symbol = SymbolName::create(type);
auto decl = find_declaration_of(document, type_symbol);
if (!decl) { if (!decl) {
dbgln("Couldn't find declaration of type: {}", type); dbgln("Couldn't find declaration of type: {}", type);
return {}; return {};
@ -289,13 +300,14 @@ Vector<CppComprehensionEngine::PropertyInfo> CppComprehensionEngine::properties_
} }
auto& struct_or_class = (StructOrClassDeclaration&)*decl; auto& struct_or_class = (StructOrClassDeclaration&)*decl;
VERIFY(struct_or_class.m_name == type); // FIXME: this won't work with scoped types VERIFY(struct_or_class.name() == type_symbol.name);
Vector<PropertyInfo> properties; Vector<Symbol> properties;
for (auto& member : struct_or_class.m_members) { for (auto& member : struct_or_class.m_members) {
if (!member.is_variable_declaration()) Vector<StringView> scope(type_symbol.scope);
continue; scope.append(type_symbol.name);
properties.append({ member.m_name, ((VariableDeclaration&)member).m_type }); // FIXME: We don't have to create the Symbol here, it should already exist in the 'm_symbol' table of some DocumentData we already parsed.
properties.append(Symbol::create(member.m_name, scope, member, Symbol::IsLocal::No));
} }
return properties; return properties;
} }
@ -681,6 +693,14 @@ CppComprehensionEngine::SymbolName CppComprehensionEngine::SymbolName::create(St
return { name, move(scope) }; return { name, move(scope) };
} }
CppComprehensionEngine::SymbolName CppComprehensionEngine::SymbolName::create(StringView qualified_name)
{
auto parts = qualified_name.split_view("::");
VERIFY(!parts.is_empty());
auto name = parts.take_last();
return SymbolName::create(name, move(parts));
}
String CppComprehensionEngine::SymbolName::to_string() const String CppComprehensionEngine::SymbolName::to_string() const
{ {
if (scope.is_empty()) if (scope.is_empty())

View file

@ -36,6 +36,7 @@ private:
Vector<StringView> scope; Vector<StringView> scope;
static SymbolName create(StringView, Vector<StringView>&&); static SymbolName create(StringView, Vector<StringView>&&);
static SymbolName create(StringView);
String scope_as_string() const; String scope_as_string() const;
String to_string() const; String to_string() const;
@ -106,11 +107,7 @@ private:
Yes Yes
}; };
struct PropertyInfo { Vector<Symbol> properties_of_type(const DocumentData& document, const String& type) const;
StringView name;
RefPtr<Type> type;
};
Vector<PropertyInfo> properties_of_type(const DocumentData& document, const String& type) const;
Vector<Symbol> get_child_symbols(const ASTNode&) const; Vector<Symbol> get_child_symbols(const ASTNode&) const;
Vector<Symbol> get_child_symbols(const ASTNode&, const Vector<StringView>& scope, Symbol::IsLocal) const; Vector<Symbol> get_child_symbols(const ASTNode&, const Vector<StringView>& scope, Symbol::IsLocal) const;