Skip to content

Commit

Permalink
More changes
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenkuttappan committed Aug 18, 2024
1 parent fd9baeb commit 2fa9b75
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 533 deletions.
1 change: 0 additions & 1 deletion src/dotnet/APIView/APIView/Model/CodeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public string VersionString
public string PackageVersion { get; set; }
public string CrossLanguagePackageId { get; set; }
public CodeFileToken[] Tokens { get; set; } = [];
//public List<APITreeNode> APIForest { get; set; } = [];
public List<CodeFileToken[]> LeafSections { get; set; }
public NavigationItem[] Navigation { get; set; }
public CodeDiagnostic[] Diagnostics { get; set; }
Expand Down
88 changes: 51 additions & 37 deletions src/dotnet/APIView/APIView/Model/StructuredTokenModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text.Json.Serialization;
using System.Text.Json;
using System;
using APIView.Model.V2;

namespace APIView.TreeToken
{
Expand Down Expand Up @@ -45,43 +46,6 @@ public enum DiffKind
Removed = 3
}

public class StructuredTokenConverter : JsonConverter<StructuredToken>
{
private readonly string _parameterSeparator;

public StructuredTokenConverter(string parameterSeparator = "\u00A0")
{
_parameterSeparator = parameterSeparator;
}

public override StructuredToken Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jObject = JsonDocument.ParseValue(ref reader).RootElement;
var myObject = JsonSerializer.Deserialize<StructuredToken>(jObject.GetRawText());

switch (myObject.Kind)
{
case StructuredTokenKind.LineBreak:
myObject.Value = "\u000A";
break;
case StructuredTokenKind.NonBreakingSpace:
myObject.Value = "\u00A0";
break;
case StructuredTokenKind.TabSpace:
myObject.Value = "\u00A0\u00A0\u00A0\u00A0";
break;
case StructuredTokenKind.ParameterSeparator:
myObject.Value = _parameterSeparator;
break;
}
return myObject;
}

public override void Write(Utf8JsonWriter writer, StructuredToken value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, options);
}
}

/// <summary>
/// Represents an APIView token its properties and tags for APIView parsers.
Expand Down Expand Up @@ -260,6 +224,56 @@ public StructuredToken(StructuredToken token)
}
}

public StructuredToken(ReviewToken token)
{
Value = token.Value;
RenderClassesObj = new HashSet<string>(token.RenderClasses);

if (token.IsDeprecated == true)
{
TagsObj.Add("Deprecated");
}

if (!string.IsNullOrEmpty(token.NavigateToId))
{
PropertiesObj.Add("NavigateToId", token.NavigateToId);
}

if (token.IsDocumentation == true)
{
TagsObj.Add(StructuredToken.DOCUMENTATION);
}
string className = StructuredToken.TEXT;
switch (token.Kind)
{
case TokenKind.Text:
className = StructuredToken.TEXT;
break;
case TokenKind.Punctuation:
className = StructuredToken.PUNCTUATION;
break;
case TokenKind.Keyword:
className = StructuredToken.KEYWORD;
break;
case TokenKind.TypeName:
className = StructuredToken.TYPE_NAME;
break;
case TokenKind.MemberName:
className = StructuredToken.MEMBER_NAME;
break;
case TokenKind.Comment:
className = StructuredToken.COMMENT;
break;
case TokenKind.StringLiteral:
className = StructuredToken.STRING_LITERAL;
break;
case TokenKind.Literal:
className = StructuredToken.LITERAL;
break;
}
RenderClassesObj.Add(className);
}


public static StructuredToken CreateLineBreakToken()
{
Expand Down
28 changes: 24 additions & 4 deletions src/dotnet/APIView/APIView/Model/V2/ReviewLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

namespace APIView.Model.V2
{
/*** Code line object corresponds to each line displayed on API review. If an empty line is required then
* add a code line object without any token. */
/*** Review line object corresponds to each line displayed on API review. If an empty line is required then
* add a review line object without any token. */
public class ReviewLine
{
/*** lineId is only required if we need to support commenting on a line that contains this token.
/*** LineId is only required if we need to support commenting on a line that contains this token.
* Usually code line for documentation or just punctuation is not required to have lineId. lineId should be a unique value within
* the review token file to use it assign to review comments as well as navigation Id within the review page.
* for e.g Azure.Core.HttpHeader.Common, azure.template.template_main
Expand All @@ -28,9 +28,10 @@ public class ReviewLine
public List<ReviewLine> Children { get; set; } = [];
/*** This is set if API is marked as hidden */
public bool? IsHidden { get; set; }

// Following properties are helper methods that's used to render review lines to UI required format.
[JsonIgnore]
public DiffKind DiffKind { get; set; } = DiffKind.NoneDiff;

[JsonIgnore]
public bool IsActiveRevisionLine = true;
[JsonIgnore]
Expand Down Expand Up @@ -124,5 +125,24 @@ public override int GetHashCode()
{
return ToString().GetHashCode();
}

public string GetTokenNodeIdHash(string parentNodeIdHash, int lineIndex)
{
var idPart = LineId;
var token = Tokens.FirstOrDefault(t => t.RenderClasses.Count > 0);
if (token != null)
{
idPart = $"{idPart}-{token.RenderClasses.First()}";
}
idPart = $"{idPart}-{lineIndex}-{DiffKind}";
var hash = CreateHashFromString(idPart);
return hash + parentNodeIdHash.Replace("nId", "").Replace("root", ""); // Append the parent node Id to ensure uniqueness
}

private string CreateHashFromString(string inputString)
{
int hash = HashCode.Combine(inputString);
return "nId" + hash.ToString();
}
}
}
Loading

0 comments on commit 2fa9b75

Please sign in to comment.