From 5106b835ed92f16dc88804d8e930e9d1d3d9f48e Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Sat, 21 Sep 2024 12:53:59 -0600 Subject: [PATCH 1/4] added symbol kind support --- lib/src/kind_generator.dart | 26 ++++++++++++++++++++++++++ lib/src/scip_visitor.dart | 3 +++ 2 files changed, 29 insertions(+) create mode 100644 lib/src/kind_generator.dart diff --git a/lib/src/kind_generator.dart b/lib/src/kind_generator.dart new file mode 100644 index 00000000..4eb4f36f --- /dev/null +++ b/lib/src/kind_generator.dart @@ -0,0 +1,26 @@ +import 'package:analyzer/dart/element/element.dart'; +import 'package:scip_dart/src/gen/scip.pbenum.dart'; + +SymbolInformation_Kind symbolKindFor(Element element) { + // These mappings are declared in the same order as their symbol parsing + // counterpart is declared within SymbolGenerator._getDescriptor. Ensure + // this order stays consistent to ensure the correct kinds are included. + final mappings = { + ClassElement: SymbolInformation_Kind.Class, + // MixinElement: SymbolInformation_Kind.Mixin, // Pending: https://github.com/sourcegraph/scip/pull/277 + EnumElement: SymbolInformation_Kind.Enum, + TypeAliasElement: SymbolInformation_Kind.TypeAlias, + // ExtensionDeclaration: SymbolInformation_Kind.Extension, // Pending: https://github.com/sourcegraph/scip/pull/277 + ConstructorElement: SymbolInformation_Kind.Constructor, + MethodElement: SymbolInformation_Kind.Method, + FunctionElement: SymbolInformation_Kind.Function, + TopLevelVariableElement: SymbolInformation_Kind.Variable, + PrefixElement: SymbolInformation_Kind.Namespace, // unsure if this is the right call for this + TypeParameterElement: SymbolInformation_Kind.TypeParameter, + ParameterElement: SymbolInformation_Kind.Parameter, + PropertyAccessorElement: SymbolInformation_Kind.Property, + FieldElement: SymbolInformation_Kind.Field, + }; + + return mappings[element.runtimeType] ?? SymbolInformation_Kind.UnspecifiedKind; +} \ No newline at end of file diff --git a/lib/src/scip_visitor.dart b/lib/src/scip_visitor.dart index 20da4d30..7bd9a5bc 100644 --- a/lib/src/scip_visitor.dart +++ b/lib/src/scip_visitor.dart @@ -5,6 +5,7 @@ import 'package:analyzer/error/error.dart'; import 'package:analyzer/source/line_info.dart'; import 'package:package_config/package_config.dart'; import 'package:pubspec_parse/pubspec_parse.dart'; +import 'package:scip_dart/src/kind_generator.dart'; import 'package:scip_dart/src/metadata.dart'; import 'package:scip_dart/src/gen/scip.pb.dart'; import 'package:scip_dart/src/relationship_generator.dart'; @@ -145,6 +146,7 @@ class ScipVisitor extends GeneralizingAstVisitor { symbol: symbol, documentation: meta.documentation, signatureDocumentation: meta.signatureDocumentation, + kind: symbolKindFor(element) )); } } @@ -170,6 +172,7 @@ class ScipVisitor extends GeneralizingAstVisitor { documentation: meta.documentation, relationships: relationships, signatureDocumentation: meta.signatureDocumentation, + kind: symbolKindFor(element), )); occurrences.add(Occurrence( From 9b53bc97ec4731f58ef0ba37c73f8301c77289ba Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Sat, 21 Sep 2024 12:55:48 -0600 Subject: [PATCH 2/4] fmt --- lib/src/kind_generator.dart | 8 +++++--- lib/src/scip_visitor.dart | 9 ++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/src/kind_generator.dart b/lib/src/kind_generator.dart index 4eb4f36f..67169eaa 100644 --- a/lib/src/kind_generator.dart +++ b/lib/src/kind_generator.dart @@ -15,12 +15,14 @@ SymbolInformation_Kind symbolKindFor(Element element) { MethodElement: SymbolInformation_Kind.Method, FunctionElement: SymbolInformation_Kind.Function, TopLevelVariableElement: SymbolInformation_Kind.Variable, - PrefixElement: SymbolInformation_Kind.Namespace, // unsure if this is the right call for this + PrefixElement: SymbolInformation_Kind + .Namespace, // unsure if this is the right call for this TypeParameterElement: SymbolInformation_Kind.TypeParameter, ParameterElement: SymbolInformation_Kind.Parameter, PropertyAccessorElement: SymbolInformation_Kind.Property, FieldElement: SymbolInformation_Kind.Field, }; - return mappings[element.runtimeType] ?? SymbolInformation_Kind.UnspecifiedKind; -} \ No newline at end of file + return mappings[element.runtimeType] ?? + SymbolInformation_Kind.UnspecifiedKind; +} diff --git a/lib/src/scip_visitor.dart b/lib/src/scip_visitor.dart index 7bd9a5bc..c1d1156c 100644 --- a/lib/src/scip_visitor.dart +++ b/lib/src/scip_visitor.dart @@ -143,11 +143,10 @@ class ScipVisitor extends GeneralizingAstVisitor { )) { final meta = getSymbolMetadata(element, offset, _analysisErrors); globalExternalSymbols.add(SymbolInformation( - symbol: symbol, - documentation: meta.documentation, - signatureDocumentation: meta.signatureDocumentation, - kind: symbolKindFor(element) - )); + symbol: symbol, + documentation: meta.documentation, + signatureDocumentation: meta.signatureDocumentation, + kind: symbolKindFor(element))); } } } From 224c335a227e00b10990b064aa3becb6e7eadd12 Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Mon, 23 Sep 2024 08:26:56 -0600 Subject: [PATCH 3/4] remoted doc --- lib/src/kind_generator.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/src/kind_generator.dart b/lib/src/kind_generator.dart index 67169eaa..45b5f84c 100644 --- a/lib/src/kind_generator.dart +++ b/lib/src/kind_generator.dart @@ -15,8 +15,7 @@ SymbolInformation_Kind symbolKindFor(Element element) { MethodElement: SymbolInformation_Kind.Method, FunctionElement: SymbolInformation_Kind.Function, TopLevelVariableElement: SymbolInformation_Kind.Variable, - PrefixElement: SymbolInformation_Kind - .Namespace, // unsure if this is the right call for this + PrefixElement: SymbolInformation_Kind.Namespace, TypeParameterElement: SymbolInformation_Kind.TypeParameter, ParameterElement: SymbolInformation_Kind.Parameter, PropertyAccessorElement: SymbolInformation_Kind.Property, From dd29c81b59a221c26117e25e0df78646ff67dffc Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Mon, 23 Sep 2024 10:25:13 -0600 Subject: [PATCH 4/4] fixed bug where symbolKind was always undefined --- lib/src/kind_generator.dart | 57 ++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/lib/src/kind_generator.dart b/lib/src/kind_generator.dart index 45b5f84c..6be74c10 100644 --- a/lib/src/kind_generator.dart +++ b/lib/src/kind_generator.dart @@ -1,27 +1,46 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:scip_dart/src/gen/scip.pbenum.dart'; -SymbolInformation_Kind symbolKindFor(Element element) { +SymbolInformation_Kind symbolKindFor(Element el) { // These mappings are declared in the same order as their symbol parsing // counterpart is declared within SymbolGenerator._getDescriptor. Ensure // this order stays consistent to ensure the correct kinds are included. - final mappings = { - ClassElement: SymbolInformation_Kind.Class, - // MixinElement: SymbolInformation_Kind.Mixin, // Pending: https://github.com/sourcegraph/scip/pull/277 - EnumElement: SymbolInformation_Kind.Enum, - TypeAliasElement: SymbolInformation_Kind.TypeAlias, - // ExtensionDeclaration: SymbolInformation_Kind.Extension, // Pending: https://github.com/sourcegraph/scip/pull/277 - ConstructorElement: SymbolInformation_Kind.Constructor, - MethodElement: SymbolInformation_Kind.Method, - FunctionElement: SymbolInformation_Kind.Function, - TopLevelVariableElement: SymbolInformation_Kind.Variable, - PrefixElement: SymbolInformation_Kind.Namespace, - TypeParameterElement: SymbolInformation_Kind.TypeParameter, - ParameterElement: SymbolInformation_Kind.Parameter, - PropertyAccessorElement: SymbolInformation_Kind.Property, - FieldElement: SymbolInformation_Kind.Field, - }; + // + // Note, we cannot declare this dynamically via a lookup map since the actual + // type of [el], is the Impl counterpart (`ClassElementImpl`). runtimeType + // type checking _does not_ take inheritance into account and `is` cannot + // be used with variables. Hence the large list of if statements. + if (el is ClassElement) { + return SymbolInformation_Kind.Class; + } else if (el is MixinElement) { + // Pending: https://github.com/sourcegraph/scip/pull/277 + // return SymbolInformation_Kind.Mixin; + } else if (el is EnumElement) { + return SymbolInformation_Kind.Enum; + } else if (el is TypeAliasElement) { + return SymbolInformation_Kind.TypeAlias; + } else if (el is ExtensionElement) { + // Pending: https://github.com/sourcegraph/scip/pull/277 + // return SymbolInformation_Kind.Extension; + } else if (el is ConstructorElement) { + return SymbolInformation_Kind.Constructor; + } else if (el is MethodElement) { + return SymbolInformation_Kind.Method; + } else if (el is FunctionElement) { + return SymbolInformation_Kind.Function; + } else if (el is TopLevelVariableElement) { + return SymbolInformation_Kind.Variable; + } else if (el is PrefixElement) { + return SymbolInformation_Kind.Namespace; + } else if (el is TypeParameterElement) { + return SymbolInformation_Kind.TypeParameter; + } else if (el is ParameterElement) { + return SymbolInformation_Kind.Parameter; + } else if (el is PropertyAccessorElement) { + return SymbolInformation_Kind.Property; + } else if (el is FieldElement) { + return SymbolInformation_Kind.Field; + } - return mappings[element.runtimeType] ?? - SymbolInformation_Kind.UnspecifiedKind; + return SymbolInformation_Kind.UnspecifiedKind; }