Skip to content

Commit

Permalink
Render review file in server side to show/hide documentation (#1033)
Browse files Browse the repository at this point in the history
* Render review file in server side to show/hide documentation
  • Loading branch information
praveenkuttappan authored and benbp committed Dec 1, 2020
1 parent 7c3c848 commit d8ed8b9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 54 deletions.
35 changes: 10 additions & 25 deletions src/dotnet/APIView/APIView/CodeFileRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,41 @@ public class CodeFileRenderer
{
public static CodeFileRenderer Instance = new CodeFileRenderer();

public CodeLine[] Render(CodeFile file)
public CodeLine[] Render(CodeFile file, bool showDocumentation = false)
{
var list = new List<CodeLine>();
Render(list, file.Tokens);
Render(list, file.Tokens, showDocumentation);
return list.ToArray();
}

private void Render(List<CodeLine> list, IEnumerable<CodeFileToken> node)
private void Render(List<CodeLine> list, IEnumerable<CodeFileToken> node, bool showDocumentation)
{
var stringBuilder = new StringBuilder();
string currentId = null;
bool isDocumentation = false;
bool isDocumentationRange = false;
bool isDeprecatedToken = false;
bool isDocumentationLine = false;

foreach (var token in node)
{
if (!showDocumentation && isDocumentationRange && token.Kind != CodeFileTokenKind.DocumentRangeEnd)
continue;

switch(token.Kind)
{
case CodeFileTokenKind.Newline:
//Close documentation span if within doc range
if (isDocumentation)
{
CloseDocumentationRange(stringBuilder);
}
list.Add(new CodeLine(stringBuilder.ToString(), currentId, isDocumentationLine));
list.Add(new CodeLine(stringBuilder.ToString(), currentId));
currentId = null;
stringBuilder.Clear();
//Start documentation span if tokens still in documentation range
if (isDocumentation)
{
StartDocumentationRange(stringBuilder);
}
//Reset flag for line documentation. This will be set to false if atleast one token is not a doc
isDocumentationLine = isDocumentation;
break;

case CodeFileTokenKind.DocumentRangeStart:
isDocumentation = true;
isDocumentationLine = (stringBuilder.Length == 0);
StartDocumentationRange(stringBuilder);
isDocumentationRange = true;
break;

case CodeFileTokenKind.DocumentRangeEnd:
isDocumentation = false;
CloseDocumentationRange(stringBuilder);
isDocumentationRange = false;
break;

case CodeFileTokenKind.DeprecatedRangeStart:
Expand All @@ -73,10 +62,6 @@ private void Render(List<CodeLine> list, IEnumerable<CodeFileToken> node)
currentId = token.DefinitionId;
}
RenderToken(token, stringBuilder, isDeprecatedToken);
if (!isDocumentation)
{
isDocumentationLine = false;
}
break;
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/dotnet/APIView/APIView/CodeLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ namespace ApiView
{
public string DisplayString { get; }
public string ElementId { get; }
public bool IsDocumentationLine { get; }

public CodeLine(string html, string id, bool isDocumentation)
public CodeLine(string html, string id)
{
this.DisplayString = html;
this.ElementId = id;
this.IsDocumentationLine = isDocumentation;
}

public override string ToString()
Expand Down
3 changes: 0 additions & 3 deletions src/dotnet/APIView/APIViewWeb/Client/css/site.scss
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,6 @@ code-inner {
text-decoration: line-through;
}

.documentation {
display: none;
}
mark {
padding: 0;
}
11 changes: 6 additions & 5 deletions src/dotnet/APIView/APIViewWeb/Client/src/documentation.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
$(() => {
const SEL_DOC_CLASS = ".documentation";
const SHOW_DOC_CHECKBOX = "#show-documentation-checkbox";
const SHOW_DOC_CHECK_COMPONENT = "#show-documentation-component";
const SHOW_DOC_CHECKBOX = ".show-doc-checkbox";
const SHOW_DOC_HREF = ".show-document";

hideCheckboxIfNoDocs();

$(document).on("click", SHOW_DOC_CHECKBOX, e => {
$(SEL_DOC_CLASS).toggle(e.target.checked);
});

function hideCheckboxIfNoDocs() {
if ($(SEL_DOC_CLASS).length == 0) {
$(SHOW_DOC_CHECK_COMPONENT).hide();
}
}

$(SHOW_DOC_CHECKBOX).on("click", e => {
$(SHOW_DOC_HREF)[0].click();
});
});
22 changes: 19 additions & 3 deletions src/dotnet/APIView/APIViewWeb/Models/RenderedCodeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ public RenderedCodeFile(CodeFile codeFile)

public CodeFile CodeFile { get; }

public CodeLine[] Render()
public CodeLine[] Render(bool showDocumentation)
{
//Always render when documentation is requested to avoid cach thrashing
if (showDocumentation)
{
return CodeFileHtmlRenderer.Normal.Render(CodeFile, true);
}

if (_rendered == null)
{
_rendered = CodeFileHtmlRenderer.Normal.Render(CodeFile);
Expand All @@ -29,8 +35,13 @@ public CodeLine[] Render()
return _rendered;
}

public CodeLine[] RenderReadOnly()
public CodeLine[] RenderReadOnly(bool showDocumentation)
{
if (showDocumentation)
{
return CodeFileHtmlRenderer.ReadOnly.Render(CodeFile, true);
}

if (_renderedReadOnly == null)
{
_renderedReadOnly = CodeFileHtmlRenderer.ReadOnly.Render(CodeFile);
Expand All @@ -39,8 +50,13 @@ public CodeLine[] RenderReadOnly()
return _renderedReadOnly;
}

internal CodeLine[] RenderText()
internal CodeLine[] RenderText(bool showDocumentation)
{
if (showDocumentation)
{
return CodeFileHtmlRenderer.Instance.Render(CodeFile, true);
}

if (_renderedText == null)
{
_renderedText = CodeFileRenderer.Instance.Render(CodeFile);
Expand Down
22 changes: 13 additions & 9 deletions src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Review.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
<div class="dropdown-menu">
@foreach (var revision in Model.Review.Revisions.Reverse())
{
<a class="dropdown-item" asp-route-id="@Model.Review.ReviewId" asp-route-revisionId="@revision.RevisionId">@revision.DisplayName</a>
<a class="dropdown-item" asp-route-id="@Model.Review.ReviewId" asp-route-revisionId="@revision.RevisionId" asp-route-doc="@Model.ShowDocumentation">
@revision.DisplayName
</a>
}
</div>
</div>
Expand All @@ -34,18 +36,20 @@
<div class="btn-group float-left">
@if (Model.DiffRevisionId != null)
{
<a class="btn btn-sm btn-outline-secondary active" asp-route-diffRevisionId="">Diff with @Model.DiffRevision?.DisplayName</a>
<a class="btn btn-sm btn-outline-secondary active" asp-route-diffRevisionId="" asp-route-doc="@Model.ShowDocumentation">Diff with @Model.DiffRevision?.DisplayName</a>
}
else
{
<a class="btn btn-sm btn-outline-secondary" asp-route-diffRevisionId="@Model.PreviousRevisions.Last().RevisionId">Diff</a>
<a class="btn btn-sm btn-outline-secondary" asp-route-diffRevisionId="@Model.PreviousRevisions.Last().RevisionId" asp-route-doc="@Model.ShowDocumentation">Diff</a>
}
<button type="button" class="btn btn-sm btn-outline-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>

<div class="dropdown-menu">
@foreach (var revision in Model.PreviousRevisions.Reverse())
{
<a class="dropdown-item" asp-route-diffRevisionId="@revision.RevisionId" active-if="@(Model.DiffRevisionId == revision.RevisionId)">@revision.DisplayName</a>
<a class="dropdown-item" asp-route-diffRevisionId="@revision.RevisionId" active-if="@(Model.DiffRevisionId == revision.RevisionId)" asp-route-doc="@Model.ShowDocumentation">
@revision.DisplayName
</a>
}
</div>
</div>
Expand Down Expand Up @@ -88,11 +92,11 @@
Show comments
</label>
</span>
<span class="dropdown-item checkbox" id="show-documentation-component">
<label>
<input type="checkbox" id="show-documentation-checkbox">
Show documentation
</label>
<span class="dropdown-item" id="show-documentation-component">
<input asp-for="@Model.ShowDocumentation" class="show-doc-checkbox">
<a class="text-dark show-document" asp-route-diffRevisionId="@Model.DiffRevisionId" asp-route-doc="@(!Model.ShowDocumentation)">
<label>Show documentation</label>
</a>
</span>
</div>
</div>
Expand Down
12 changes: 8 additions & 4 deletions src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Review.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public ReviewPageModel(
[BindProperty(SupportsGet = true)]
public string DiffRevisionId { get; set; }

// Flag to decide whether to include documentation
[BindProperty(Name = "doc", SupportsGet = true)]
public bool ShowDocumentation { get; set; }

public async Task<IActionResult> OnGetAsync(string id, string revisionId = null)
{
TempData["Page"] = "api";
Expand All @@ -78,17 +82,17 @@ public async Task<IActionResult> OnGetAsync(string id, string revisionId = null)

var fileDiagnostics = CodeFile.Diagnostics ?? Array.Empty<CodeDiagnostic>();

var fileHtmlLines = renderedCodeFile.Render();
var fileHtmlLines = renderedCodeFile.Render(ShowDocumentation);

if (DiffRevisionId != null)
{
DiffRevision = PreviousRevisions.Single(r=>r.RevisionId == DiffRevisionId);

var previousRevisionFile = await _codeFileRepository.GetCodeFileAsync(DiffRevision);

var previousHtmlLines = previousRevisionFile.RenderReadOnly();
var previousRevisionTextLines = previousRevisionFile.RenderText();
var fileTextLines = renderedCodeFile.RenderText();
var previousHtmlLines = previousRevisionFile.RenderReadOnly(ShowDocumentation);
var previousRevisionTextLines = previousRevisionFile.RenderText(ShowDocumentation);
var fileTextLines = renderedCodeFile.RenderText(ShowDocumentation);

var diffLines = InlineDiff.Compute(
previousRevisionTextLines,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
DiffLineKind.Added => "code-added",
_ => ""
};
string documentationClass = Model.CodeLine.IsDocumentationLine ? "documentation" : "";
}

<tr class="code-line @documentationClass" data-line-id="@(isRemoved ? string.Empty : Model.CodeLine.ElementId)">
<tr class="code-line" data-line-id="@(isRemoved ? string.Empty : Model.CodeLine.ElementId)">
<td class="line-comment-button-cell">
@if (!isRemoved && Model.CodeLine.ElementId != null)
{
Expand Down

0 comments on commit d8ed8b9

Please sign in to comment.