Skip to content

Commit

Permalink
Convert Magic strings to static properties
Browse files Browse the repository at this point in the history
  • Loading branch information
chidozieononiwu committed Jun 19, 2024
1 parent 513a046 commit 76ce86c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 27 deletions.
65 changes: 56 additions & 9 deletions src/dotnet/APIView/APIView/Model/StructuredTokenModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace APIView.TreeToken
{

/// <summary>
/// Represents the type of a structured token.
/// All tokens should be content except for spacing tokens and url.
Expand All @@ -23,7 +24,7 @@ public enum StructuredTokenKind
/// <summary>
/// Regular single space.
/// </summary>
NoneBreakingSpace = 2,
NonBreakingSpace = 2,
/// <summary>
/// 4 NonBreakingSpaces.
/// </summary>
Expand All @@ -44,6 +45,52 @@ public enum StructuredTokenKind

public class StructuredToken
{
/// <summary>
/// Property key to indicate that a range of tokens is a group
/// </summary>
public static string GROUP_ID = "GroupId";
/// <summary>
/// Property key to indicate id to be navigated to when a token is clicked.
/// </summary>
public static string NAVIGATE_TO_ID = "NavigateToId";
/// <summary>
/// Property value that marks a token as documentation
/// </summary>
public static string DOCUMENTATION = "doc";
/// <summary>
/// Style class for text
/// </summary>
public static string TEXT = "text";
/// <summary>
/// Style class for keyword
/// </summary>
public static string KEYWORD = "keyword";
/// <summary>
/// Style class for literal
/// </summary>
public static string LITERAL = "literal";
/// <summary>
/// Style class for member-name
/// </summary>
public static string MEMBER_NAME = "mname";
/// <summary>
/// Style class for punctuation
/// </summary>
public static string PUNCTUATION = "punc";
/// <summary>
/// Style class for string-literal
/// </summary>
public static string STRING_LITERAL = "sliteral";
/// <summary>
/// Style class for type-name
/// </summary>
public static string TYPE_NAME = "tname";
/// <summary>
/// Style class for comment
/// </summary>
public static string COMMENT = "comment";


/// <summary>
/// Token Id.
/// Previously known as DefinitionId.
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
}
Expand Down
40 changes: 37 additions & 3 deletions src/dotnet/APIView/APIView/Model/TokenTreeModel.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Indicates a node is an assembly kind
/// </summary>
public static string ASSEMBLY = "Assembly";
/// <summary>
/// Indicates a node is a InternalsVisibleTo kind
/// </summary>
public static string INTERNALS_VISIBLE_TO = "InternalsVisibleTo";
/// <summary>
/// Indicates a node is a Dependencies kind
/// </summary>
public static string DEPENDENCIES = "Dependencies";
/// <summary>
/// Indicates a node is a Namespace kind
/// </summary>
public static string NAMESPACE = "Namespace";
/// <summary>
/// Indicates a node is a Type kind
/// </summary>
public static string TYPE = "Type";
/// <summary>
/// Indicates a node is a Member kind
/// </summary>
public static string MEMBER = "Member";
/// <summary>
/// Indicates a node is hidden
/// </summary>
public static string HIDDEN = "Hidden";
/// <summary>
/// Indicates that a node should be hidden from Navigation
/// </summary>
public static string HIDE_FROM_NAV = "HideFromNav";
/// <summary>
/// Property key to use to make a nodes kind more specific.
/// </summary>
public static string SUB_KIND = "SubKind";

/// <summary>
/// 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.
Expand Down
2 changes: 1 addition & 1 deletion tools/apiview/parsers/csharp-api-parser/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public CodeFile Build(IAssemblySymbol assemblySymbol, bool runAnalysis, List<Dep
}

var apiTreeNode = new APITreeNode();
apiTreeNode.Kind = "Assembly";
apiTreeNode.Kind = APITreeNode.ASSEMBLY;
apiTreeNode.Id = assemblySymbol.Name;
apiTreeNode.Name = assemblySymbol.Name + ".dll";

Expand Down Expand Up @@ -130,7 +130,7 @@ public static void BuildInternalsVisibleToAttributes(List<APITreeNode> 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());

Expand Down Expand Up @@ -158,7 +158,7 @@ public static void BuildDependencies(List<APITreeNode> apiTree, List<DependencyI
if (dependencies != null && dependencies.Any())
{
var apiTreeNode = new APITreeNode();
apiTreeNode.Kind = apiTreeNode.Name = apiTreeNode.Id = "Dependencies";
apiTreeNode.Kind = apiTreeNode.Name = apiTreeNode.Id = APITreeNode.DEPENDENCIES;

apiTreeNode.TopTokensObj.Add(StructuredToken.CreateLineBreakToken());
apiTreeNode.TopTokensObj.Add(StructuredToken.CreateTextToken(value: "Dependencies:"));
Expand All @@ -183,11 +183,11 @@ private void BuildNamespace(List<APITreeNode> 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));
Expand Down Expand Up @@ -238,14 +238,14 @@ private void BuildType(List<APITreeNode> 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);
Expand Down Expand Up @@ -334,7 +334,7 @@ private void BuildDocumentation(List<StructuredToken> 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());
}
Expand Down Expand Up @@ -405,15 +405,15 @@ private void BuildMember(List<APITreeNode> 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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 76ce86c

Please sign in to comment.