From 76ce86c4173994c94cc031a0826d44555db4ea84 Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Wed, 19 Jun 2024 14:22:37 -0700 Subject: [PATCH] Convert Magic strings to static properties --- .../APIView/Model/StructuredTokenModel.cs | 65 ++++++++++++++++--- .../APIView/APIView/Model/TokenTreeModel.cs | 40 +++++++++++- .../parsers/csharp-api-parser/Program.cs | 2 +- .../TreeToken/CodeFileBuilder.cs | 28 ++++---- 4 files changed, 108 insertions(+), 27 deletions(-) diff --git a/src/dotnet/APIView/APIView/Model/StructuredTokenModel.cs b/src/dotnet/APIView/APIView/Model/StructuredTokenModel.cs index c3f74d781ca..451f9fc25f8 100644 --- a/src/dotnet/APIView/APIView/Model/StructuredTokenModel.cs +++ b/src/dotnet/APIView/APIView/Model/StructuredTokenModel.cs @@ -5,6 +5,7 @@ namespace APIView.TreeToken { + /// /// Represents the type of a structured token. /// All tokens should be content except for spacing tokens and url. @@ -23,7 +24,7 @@ public enum StructuredTokenKind /// /// Regular single space. /// - NoneBreakingSpace = 2, + NonBreakingSpace = 2, /// /// 4 NonBreakingSpaces. /// @@ -44,6 +45,52 @@ public enum StructuredTokenKind public class StructuredToken { + /// + /// Property key to indicate that a range of tokens is a group + /// + public static string GROUP_ID = "GroupId"; + /// + /// Property key to indicate id to be navigated to when a token is clicked. + /// + public static string NAVIGATE_TO_ID = "NavigateToId"; + /// + /// Property value that marks a token as documentation + /// + public static string DOCUMENTATION = "doc"; + /// + /// Style class for text + /// + public static string TEXT = "text"; + /// + /// Style class for keyword + /// + public static string KEYWORD = "keyword"; + /// + /// Style class for literal + /// + public static string LITERAL = "literal"; + /// + /// Style class for member-name + /// + public static string MEMBER_NAME = "mname"; + /// + /// Style class for punctuation + /// + public static string PUNCTUATION = "punc"; + /// + /// Style class for string-literal + /// + public static string STRING_LITERAL = "sliteral"; + /// + /// Style class for type-name + /// + public static string TYPE_NAME = "tname"; + /// + /// Style class for comment + /// + public static string COMMENT = "comment"; + + /// /// Token Id. /// Previously known as DefinitionId. @@ -169,7 +216,7 @@ public static StructuredToken CreateEmptyToken(string id = null) public static StructuredToken CreateSpaceToken() { var token = new StructuredToken(); - token.Kind = StructuredTokenKind.NoneBreakingSpace; + token.Kind = StructuredTokenKind.NonBreakingSpace; return token; } @@ -187,14 +234,14 @@ public static StructuredToken CreateTextToken(string value, string id = null) { token.Id = id; } - token.RenderClassesObj.Add("text"); + token.RenderClassesObj.Add(TEXT); return token; } public static StructuredToken CreateKeywordToken(string value) { var token = new StructuredToken(value); - token.RenderClassesObj.Add("keyword"); + token.RenderClassesObj.Add(KEYWORD); return token; } @@ -211,7 +258,7 @@ public static StructuredToken CreateKeywordToken(Accessibility accessibility) public static StructuredToken CreatePunctuationToken(string value) { var token = new StructuredToken(value); - token.RenderClassesObj.Add("punc"); + token.RenderClassesObj.Add(PUNCTUATION); return token; } @@ -223,28 +270,28 @@ public static StructuredToken CreatePunctuationToken(SyntaxKind syntaxKind) public static StructuredToken CreateTypeNameToken(string value) { var token = new StructuredToken(value); - token.RenderClassesObj.Add("tname"); + token.RenderClassesObj.Add(TYPE_NAME); return token; } public static StructuredToken CreateMemberNameToken(string value) { var token = new StructuredToken(value); - token.RenderClassesObj.Add("mname"); + token.RenderClassesObj.Add(MEMBER_NAME); return token; } public static StructuredToken CreateLiteralToken(string value) { var token = new StructuredToken(value); - token.RenderClassesObj.Add("literal"); + token.RenderClassesObj.Add(LITERAL); return token; } public static StructuredToken CreateStringLiteralToken(string value) { var token = new StructuredToken(value); - token.RenderClassesObj.Add("sliteral"); + token.RenderClassesObj.Add(STRING_LITERAL); return token; } } diff --git a/src/dotnet/APIView/APIView/Model/TokenTreeModel.cs b/src/dotnet/APIView/APIView/Model/TokenTreeModel.cs index a1ec2d15c6d..f6ff4043519 100644 --- a/src/dotnet/APIView/APIView/Model/TokenTreeModel.cs +++ b/src/dotnet/APIView/APIView/Model/TokenTreeModel.cs @@ -1,13 +1,47 @@ using System.Collections.Generic; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using System.Text.Json; using System.Text.Json.Serialization; namespace APIView.TreeToken { public class APITreeNode { + /// + /// Indicates a node is an assembly kind + /// + public static string ASSEMBLY = "Assembly"; + /// + /// Indicates a node is a InternalsVisibleTo kind + /// + public static string INTERNALS_VISIBLE_TO = "InternalsVisibleTo"; + /// + /// Indicates a node is a Dependencies kind + /// + public static string DEPENDENCIES = "Dependencies"; + /// + /// Indicates a node is a Namespace kind + /// + public static string NAMESPACE = "Namespace"; + /// + /// Indicates a node is a Type kind + /// + public static string TYPE = "Type"; + /// + /// Indicates a node is a Member kind + /// + public static string MEMBER = "Member"; + /// + /// Indicates a node is hidden + /// + public static string HIDDEN = "Hidden"; + /// + /// Indicates that a node should be hidden from Navigation + /// + public static string HIDE_FROM_NAV = "HideFromNav"; + /// + /// Property key to use to make a nodes kind more specific. + /// + public static string SUB_KIND = "SubKind"; + /// /// Id of the node, which should be unique at the node level. i.e. unique among its siblings. /// This was previously represented by the DefinitionId for the main Token of the node. diff --git a/tools/apiview/parsers/csharp-api-parser/Program.cs b/tools/apiview/parsers/csharp-api-parser/Program.cs index 4ab1598aaa3..375bd05fd4b 100644 --- a/tools/apiview/parsers/csharp-api-parser/Program.cs +++ b/tools/apiview/parsers/csharp-api-parser/Program.cs @@ -102,7 +102,7 @@ static void HandlePackageFileParsing(Stream stream, FileInfo packageFilePath, Di } var parsedFileName = string.IsNullOrEmpty(outputFileName) ? assemblySymbol.Name : outputFileName; var treeTokenCodeFile = new CSharpAPIParser.TreeToken.CodeFileBuilder().Build(assemblySymbol, runAnalysis, dependencies); - var gzipJsonTokenFilePath = Path.Combine(OutputDirectory.FullName, $"{parsedFileName}"); + var gzipJsonTokenFilePath = Path.Combine(OutputDirectory.FullName, $"{parsedFileName}.json.tgz"); var options = new JsonSerializerOptions() diff --git a/tools/apiview/parsers/csharp-api-parser/TreeToken/CodeFileBuilder.cs b/tools/apiview/parsers/csharp-api-parser/TreeToken/CodeFileBuilder.cs index 76d21b60e64..166d704513c 100644 --- a/tools/apiview/parsers/csharp-api-parser/TreeToken/CodeFileBuilder.cs +++ b/tools/apiview/parsers/csharp-api-parser/TreeToken/CodeFileBuilder.cs @@ -77,7 +77,7 @@ public CodeFile Build(IAssemblySymbol assemblySymbol, bool runAnalysis, List apiTree, if (assemblyAttributes != null && assemblyAttributes.Any()) { var apiTreeNode = new APITreeNode(); - apiTreeNode.Kind = apiTreeNode.Name = apiTreeNode.Id = "InternalsVisibleTo"; + apiTreeNode.Kind = apiTreeNode.Name = apiTreeNode.Id = APITreeNode.INTERNALS_VISIBLE_TO; apiTreeNode.TopTokensObj.Add(StructuredToken.CreateTextToken(value: "Exposes internals to:")); apiTreeNode.TopTokensObj.Add(StructuredToken.CreateLineBreakToken()); @@ -158,7 +158,7 @@ public static void BuildDependencies(List apiTree, List apiTree, INamespaceSymbol namespac var apiTreeNode = new APITreeNode(); apiTreeNode.Id = namespaceSymbol.GetId(); apiTreeNode.Name = namespaceSymbol.ToDisplayString(); - apiTreeNode.Kind = "Namespace"; + apiTreeNode.Kind = APITreeNode.NAMESPACE; if (isHidden) { - apiTreeNode.TagsObj.Add("Hidden"); + apiTreeNode.TagsObj.Add(APITreeNode.HIDDEN); } apiTreeNode.TopTokensObj.Add(StructuredToken.CreateKeywordToken(SyntaxKind.NamespaceKeyword)); @@ -238,14 +238,14 @@ private void BuildType(List apiTree, INamedTypeSymbol namedType, bo bool isHidden = IsHiddenFromIntellisense(namedType); var apiTreeNode = new APITreeNode(); - apiTreeNode.Kind = "Type"; - apiTreeNode.PropertiesObj.Add("SubKind", namedType.TypeKind.ToString().ToLowerInvariant()); + apiTreeNode.Kind = APITreeNode.TYPE; + apiTreeNode.PropertiesObj.Add(APITreeNode.SUB_KIND, namedType.TypeKind.ToString().ToLowerInvariant()); apiTreeNode.Id = namedType.GetId(); apiTreeNode.Name = namedType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat); if (isHidden && !inHiddenScope) { - apiTreeNode.TagsObj.Add("Hidden"); + apiTreeNode.TagsObj.Add(APITreeNode.HIDDEN); } BuildDocumentation(apiTreeNode.TopTokensObj, namedType); @@ -334,7 +334,7 @@ private void BuildDocumentation(List tokenList, ISymbol symbol) { var docToken = new StructuredToken("// " + line.Trim()); docToken.RenderClassesObj.Add("comment"); - docToken.PropertiesObj.Add("GroupId", "doc"); + docToken.PropertiesObj.Add(StructuredToken.GROUP_ID, StructuredToken.DOCUMENTATION); tokenList.Add(docToken); tokenList.Add(StructuredToken.CreateLineBreakToken()); } @@ -405,15 +405,15 @@ private void BuildMember(List apiTree, ISymbol member, bool inHidde { bool isHidden = IsHiddenFromIntellisense(member); var apiTreeNode = new APITreeNode(); - apiTreeNode.Kind = "Member"; - apiTreeNode.PropertiesObj.Add("SubKind", member.Kind.ToString()); + apiTreeNode.Kind = APITreeNode.MEMBER; + apiTreeNode.PropertiesObj.Add(APITreeNode.SUB_KIND, member.Kind.ToString()); apiTreeNode.Id = member.GetId(); apiTreeNode.Name = member.ToDisplayString(); - apiTreeNode.TagsObj.Add("HideFromNav"); + apiTreeNode.TagsObj.Add(APITreeNode.HIDE_FROM_NAV); if (isHidden && !inHiddenScope) { - apiTreeNode.TagsObj.Add("Hidden"); + apiTreeNode.TagsObj.Add(APITreeNode.HIDDEN); } BuildDocumentation(apiTreeNode.TopTokensObj, member); @@ -706,7 +706,7 @@ private StructuredToken MapToken(ISymbol definedSymbol, SymbolDisplayPart symbol if (!String.IsNullOrWhiteSpace(navigateToId)) { - token.PropertiesObj.Add("NavigateToId", navigateToId!); + token.PropertiesObj.Add(StructuredToken.NAVIGATE_TO_ID, navigateToId!); } return token;