-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.NET API review parser changes to use new token schema (#8850)
* .NET API review parser changes to use new token schema
- Loading branch information
1 parent
7aea8e6
commit f5b732c
Showing
11 changed files
with
1,059 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using System.Xml; | ||
using APIView.TreeToken; | ||
|
||
namespace APIView.Model.V2 | ||
{ | ||
/// <summary> | ||
/// 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. | ||
/// </summary> | ||
public class ReviewLine | ||
{ | ||
/// <summary> | ||
/// 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 | ||
/// </summary> | ||
public string LineId { get; set; } | ||
public string CrossLanguageId { get; set; } | ||
/// <summary> | ||
/// List of tokens that constructs a line in API review | ||
/// </summary> | ||
public List<ReviewToken> Tokens { get; set; } = []; | ||
/// <summary> | ||
/// Add any child lines as children. For e.g. all classes and namespace level methods are added as a children of namespace(module) level code line. | ||
/// Similarly all method level code lines are added as children of it's class code line. | ||
/// </summary> | ||
public List<ReviewLine> Children { get; set; } = []; | ||
/// <summary> | ||
/// This is set if API is marked as hidden | ||
/// </summary> | ||
public bool? IsHidden { get; set; } | ||
/// <summary> | ||
/// This is set if a line is end of context. For e.g. end of a class or name space line "}" | ||
/// </summary> | ||
public bool? IsContextEndLine { 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] | ||
public bool IsDocumentation => Tokens.Count > 0 && Tokens[0].IsDocumentation == true; | ||
[JsonIgnore] | ||
public bool IsEmpty => Tokens.Count == 0 || !Tokens.Any( t => t.SkipDiff != true); | ||
[JsonIgnore] | ||
public bool Processed { get; set; } = false; | ||
|
||
public void AddToken(ReviewToken token) | ||
{ | ||
Tokens.Add(token); | ||
} | ||
|
||
public void AppendApiTextToBuilder(StringBuilder sb, int indent = 0, bool skipDocs = true) | ||
{ | ||
if (skipDocs && Tokens.Count > 0 && Tokens[0].IsDocumentation == true) | ||
{ | ||
return; | ||
} | ||
|
||
//Add empty line in case of review line without tokens | ||
if (Tokens.Count == 0) | ||
{ | ||
sb.Append(Environment.NewLine); | ||
return; | ||
} | ||
//Add spaces for indentation | ||
for (int i = 0; i < indent; i++) | ||
{ | ||
sb.Append(" "); | ||
} | ||
//Process all tokens | ||
sb.Append(ToString(true)); | ||
|
||
sb.Append(Environment.NewLine); | ||
foreach (var child in Children) | ||
{ | ||
child.AppendApiTextToBuilder(sb, indent + 1, skipDocs); | ||
} | ||
} | ||
|
||
private string ToString(bool includeAllTokens) | ||
{ | ||
var filterdTokens = includeAllTokens ? Tokens: Tokens.Where(x => x.SkipDiff != true); | ||
if (!filterdTokens.Any()) | ||
{ | ||
return string.Empty; | ||
} | ||
StringBuilder sb = new(); | ||
foreach (var token in filterdTokens) | ||
{ | ||
sb.Append(token.Value); | ||
sb.Append(token.HasSuffixSpace == true ? " " : string.Empty); | ||
} | ||
return sb.ToString(); | ||
} | ||
|
||
|
||
public override string ToString() | ||
{ | ||
return ToString(false); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if(obj is ReviewLine other) | ||
{ | ||
return ToString() == other.ToString(); | ||
} | ||
return false; | ||
} | ||
|
||
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 static string CreateHashFromString(string inputString) | ||
{ | ||
int hash = inputString.GetHashCode(); | ||
return "nId" + hash.ToString(); | ||
} | ||
} | ||
} |
Oops, something went wrong.