Skip to content

Commit

Permalink
Feature/2389 show reference docs for specific method class (#3941)
Browse files Browse the repository at this point in the history
Reference doc code folding feature - fully implemented
  • Loading branch information
Vailorium authored Aug 30, 2022
1 parent 6c01549 commit 0bf2dda
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 61 deletions.
11 changes: 4 additions & 7 deletions src/dotnet/APIView/APIView/CodeFileRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public class CodeFileRenderer
{
public static CodeFileRenderer Instance = new CodeFileRenderer();

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

private void Render(List<CodeLine> list, IEnumerable<CodeFileToken> node, bool showDocumentation, bool enableSkipDiff)
private void Render(List<CodeLine> list, IEnumerable<CodeFileToken> node, bool enableSkipDiff)
{
var stringBuilder = new StringBuilder();
string currentId = null;
Expand All @@ -39,9 +39,6 @@ private void Render(List<CodeLine> list, IEnumerable<CodeFileToken> node, bool s
if (enableSkipDiff && isSkipDiffRange && token.Kind != CodeFileTokenKind.SkipDiffRangeEnd)
continue;

if (!showDocumentation && isDocumentationRange && token.Kind != CodeFileTokenKind.DocumentRangeEnd)
continue;

switch(token.Kind)
{
case CodeFileTokenKind.Newline:
Expand All @@ -68,7 +65,7 @@ private void Render(List<CodeLine> list, IEnumerable<CodeFileToken> node, bool s
lineClass = lineClass.Trim();
}

list.Add(new CodeLine(stringBuilder.ToString(), currentId, lineClass, indentSize));
list.Add(new CodeLine(stringBuilder.ToString(), currentId, lineClass, indentSize, isDocumentationRange));
currentId = null;
stringBuilder.Clear();
break;
Expand Down
4 changes: 3 additions & 1 deletion src/dotnet/APIView/APIView/CodeLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ namespace ApiView
public string ElementId { get; }
public string LineClass { get; }
public int IndentSize { get; }
public bool IsDocumentation { get; }

public CodeLine(string html, string id, string lineClass, int indentSize = 0)
public CodeLine(string html, string id, string lineClass, int indentSize = 0, bool isDocumentation = false)
{
this.DisplayString = html;
this.ElementId = id;
this.LineClass = lineClass;
this.IndentSize = indentSize;
this.IsDocumentation = isDocumentation;
}

public override string ToString()
Expand Down
30 changes: 29 additions & 1 deletion src/dotnet/APIView/APIViewWeb/Client/css/site.scss
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,14 @@ code {
z-index: 1;
}

.icon-chevron-right {
background: url(/icons/chevron-right.svg) center center no-repeat;
}

.icon-chevron-up {
background: url(/icons/chevron-up.svg) center center no-repeat;
}

.icon-language {
width: 34px;
height: 34px;
Expand Down Expand Up @@ -864,7 +872,6 @@ code {
.icon-swagger {
background: url(/icons/swagger-original.svg) center center no-repeat;
}

/*---------------------------------------------------------------------*/

.info-text {
Expand Down Expand Up @@ -911,6 +918,27 @@ code {
left: 0px;
}

.line-toggle-documentation-button:hover {
transform: scale(1);
}

.line-toggle-documentation-button {
display: inline-block;
width: 21px;
height: 21px;
line-height: 21px;
color: #fff !important;
text-align: center;
text-indent: 0;
cursor: pointer;
background-color: #0366d6;
border-radius: 3px;
box-shadow: 0 1px 4px rgba(27, 31, 35, .15);
transition: transform .1s ease-in-out;
transform: scale(.8);
user-select: none;
}

.line-details-button-cell {
width: 22px;
height: 22px;
Expand Down
36 changes: 34 additions & 2 deletions src/dotnet/APIView/APIViewWeb/Client/src/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { updatePageSettings } from "./helpers";
$(() => {
const SEL_DOC_CLASS = ".documentation";
const SHOW_DOC_CHECK_COMPONENT = "#show-documentation-component";
const SHOW_DOC_CHECKBOX = ".show-doc-checkbox";
const SHOW_DOC_CHECKBOX = "#show-doc-checkbox";
const SHOW_DOC_HREF = ".show-document";
const SHOW_DIFFONLY_CHECKBOX = ".show-diffonly-checkbox";
const SHOW_DIFFONLY_HREF = ".show-diffonly";
const TOGGLE_DOCUMENTATION = ".line-toggle-documentation-button";

hideCheckboxIfNoDocs();

Expand Down Expand Up @@ -62,7 +63,21 @@ $(() => {
/* TOGGLE PAGE OPTIONS
--------------------------------------------------------------------------------------------------------------------------------------------------------*/
$(SHOW_DOC_CHECKBOX).on("click", e => {
$(SHOW_DOC_HREF)[0].click();
if((e.target as HTMLInputElement).checked) {
// show all documentation
$(".code-line-documentation").removeClass('hidden-row');
$(TOGGLE_DOCUMENTATION).removeClass("icon-chevron-right");
$(TOGGLE_DOCUMENTATION).addClass("icon-chevron-up");
} else {
// hide all documentation
$(".code-line-documentation").addClass("hidden-row");
$(TOGGLE_DOCUMENTATION).removeClass("icon-chevron-up");
$(TOGGLE_DOCUMENTATION).addClass("icon-chevron-right");
}
});

$(SHOW_DOC_HREF).on("click", e => {
$(SHOW_DOC_CHECKBOX)[0].click();
});

$(SHOW_DIFFONLY_CHECKBOX).on("click", e => {
Expand Down Expand Up @@ -97,6 +112,23 @@ $(() => {
});
});

/* TOGGLE DOCUMENTATION DROPDOWN
--------------------------------------------------------------------------------------------------------------------------------------------------------*/
$(TOGGLE_DOCUMENTATION).on("click", function(e){
const documentedBy = $(this).data('documented-by');
const codeLines = $(".code-window > tbody > .code-line");

for(var i = 0; i < documentedBy.length; i++) {
$(codeLines[documentedBy[i] - 1]).toggleClass("hidden-row");
}

$(this).toggleClass('icon-chevron-right');
$(this).toggleClass('icon-chevron-up');

// scroll button to center of screen, so that the line is visible after toggling folding
$(this).get(0).scrollIntoView({ block: "center"});
});

/* DROPDOWN FILTER FOR REVIEW, REVISIONS AND DIFF (UPDATES REVIEW PAGE ON CHANGE)
--------------------------------------------------------------------------------------------------------------------------------------------------------*/
$('#revisions-bootstraps-select, #review-bootstraps-select, #diff-bootstraps-select').each(function(index, value) {
Expand Down
4 changes: 3 additions & 1 deletion src/dotnet/APIView/APIViewWeb/Models/CodeLineModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ namespace APIViewWeb.Models
{
public readonly struct CodeLineModel
{
public CodeLineModel(DiffLineKind kind, CodeLine codeLine, CommentThreadModel commentThread, CodeDiagnostic[] diagnostics, int lineNumber)
public CodeLineModel(DiffLineKind kind, CodeLine codeLine, CommentThreadModel commentThread, CodeDiagnostic[] diagnostics, int lineNumber, int[] documentedByLines)
{
CodeLine = codeLine;
CommentThread = commentThread;
Diagnostics = diagnostics;
Kind = kind;
LineNumber = lineNumber;
DocumentedByLines = documentedByLines;
}

public CodeLine CodeLine { get; }
public CodeDiagnostic[] Diagnostics { get; }
public CommentThreadModel CommentThread { get; }
public DiffLineKind Kind { get; }
public int LineNumber { get; }
public int[] DocumentedByLines { get; }
}
}
23 changes: 6 additions & 17 deletions src/dotnet/APIView/APIViewWeb/Models/RenderedCodeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@ public RenderedCodeFile(CodeFile codeFile)

public CodeFile CodeFile { get; }

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

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

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

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

internal CodeLine[] RenderText(bool showDocumentation, bool skipDiff = false)
internal CodeLine[] RenderText(bool skipDiff = false)
{
if (showDocumentation || skipDiff)
if (skipDiff)
{
return CodeFileRenderer.Instance.Render(CodeFile, showDocumentation: showDocumentation, enableSkipDiff: skipDiff);
return CodeFileRenderer.Instance.Render(CodeFile, enableSkipDiff: skipDiff);
}

if (_renderedText == null)
Expand All @@ -65,4 +54,4 @@ internal CodeLine[] RenderText(bool showDocumentation, bool skipDiff = false)
return _renderedText;
}
}
}
}
12 changes: 4 additions & 8 deletions src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Review.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
</label>
</span>
<span class="dropdown-item" id="show-documentation-component">
<input asp-for="@Model.ShowDocumentation" class="show-doc-checkbox">
<a class="text-dark show-document" asp-all-route-data=@Model.GetRoutingData(diffRevisionId: Model.DiffRevisionId, showDocumentation: !Model.ShowDocumentation, showDiffOnly: Model.ShowDiffOnly, revisionId: Model.Revision.RevisionId)>
<input type="checkbox" id="show-doc-checkbox">
<a class="text-dark show-document">
<label>&nbsp;Show Documentation</label>
</a>
</span>
Expand Down Expand Up @@ -146,7 +146,7 @@
{
<span class="dropdown-item">
<input asp-for="@Model.ShowDiffOnly" class="show-diffonly-checkbox">
<a class="text-dark show-diffonly" asp-all-route-data=@Model.GetRoutingData(diffRevisionId: Model.DiffRevisionId, showDocumentation: Model.ShowDocumentation, showDiffOnly: !Model.ShowDiffOnly, revisionId: Model.Revision.RevisionId)>
<a class="text-dark show-diffonly" asp-all-route-data=@Model.GetRoutingData(diffRevisionId: Model.DiffRevisionId, showDiffOnly: !Model.ShowDiffOnly, revisionId: Model.Revision.RevisionId)>
<label>&nbsp;Show Only Diff</label>
</a>
</span>
Expand Down Expand Up @@ -232,8 +232,7 @@
var optionName = revision.IsApproved ? $"{@revision.DisplayName} {@approvedBadge}" : @revision.DisplayName;
var urlValue = @Url.ActionLink("Review", "Assemblies", new {
id = @Model.Review.ReviewId,
revisionId = @revision.RevisionId,
doc = @Model.ShowDocumentation
revisionId = @revision.RevisionId
});
if (@revision.DisplayName == @Model.Revision.DisplayName)
{
Expand All @@ -256,7 +255,6 @@
id = @Model.Review.ReviewId,
revisionId = @Model.Revision.RevisionId,
diffRevisionId = @Model.DiffRevisionId,
doc = @Model.ShowDocumentation,
diffOnly = @Model.ShowDiffOnly
});
<label class="mb-0 ml-1"><a href="@urlValue"><small>Diff With:</small></a></label>
Expand All @@ -267,7 +265,6 @@
id = @Model.Review.ReviewId,
revisionId = @Model.Revision.RevisionId,
diffRevisionId = @Model.PreviousRevisions.Last().RevisionId,
doc = @Model.ShowDocumentation,
diffOnly = @Model.ShowDiffOnly
});
<label class="mb-0 ml-1"><a href="@urlValue"><small>Diff:</small></a></label>
Expand All @@ -284,7 +281,6 @@
var urlValue = @Url.ActionLink("Review", "Assemblies", new {
id = @Model.Review.ReviewId,
diffRevisionId = @revision.RevisionId,
doc = @Model.ShowDocumentation,
diffOnly = @Model.ShowDiffOnly,
revisionId = @Model.Revision.RevisionId
});
Expand Down
56 changes: 38 additions & 18 deletions src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Review.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ 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; }

[BindProperty(Name = "diffOnly", SupportsGet = true)]
public bool ShowDiffOnly { get; set; }

Expand All @@ -94,17 +90,17 @@ public async Task<IActionResult> OnGetAsync(string id, string revisionId = null)
CodeFile = renderedCodeFile.CodeFile;

var fileDiagnostics = CodeFile.Diagnostics ?? Array.Empty<CodeDiagnostic>();
var fileHtmlLines = renderedCodeFile.Render(ShowDocumentation);
var fileHtmlLines = renderedCodeFile.Render();

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

var previousRevisionFile = await _codeFileRepository.GetCodeFileAsync(DiffRevision);

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

var diffLines = InlineDiff.Compute(
previousRevisionTextLines,
Expand Down Expand Up @@ -186,20 +182,45 @@ private CodeLineModel[] CreateLines(CodeDiagnostic[] diagnostics, InlineDiffLine
diffLine.Kind != DiffLineKind.Removed ?
diagnostics.Where(d => d.TargetId == diffLine.Line.ElementId).ToArray() :
Array.Empty<CodeDiagnostic>(),
++index
++index,
new int[] { }
)).ToArray();
}

private CodeLineModel[] CreateLines(CodeDiagnostic[] diagnostics, CodeLine[] lines, ReviewCommentsModel comments)
{
List<int> documentedByLines = new List<int>();
int lineNumberExcludingDocumentation = 0;
return lines.Select(
(line, index) => new CodeLineModel(
DiffLineKind.Unchanged,
line,
comments.TryGetThreadForLine(line.ElementId, out var thread) ? thread : null,
diagnostics.Where(d => d.TargetId == line.ElementId).ToArray(),
++index
)).ToArray();
(line, index) =>
{
if (line.IsDocumentation)
{
// documentedByLines must include the index of a line, assuming that documentation lines are counted
documentedByLines.Add(++index);
return new CodeLineModel(
DiffLineKind.Unchanged,
line,
comments.TryGetThreadForLine(line.ElementId, out var thread) ? thread : null,
diagnostics.Where(d => d.TargetId == line.ElementId).ToArray(),
lineNumberExcludingDocumentation,
new int[] {}
);
}
else
{
CodeLineModel c = new CodeLineModel(
DiffLineKind.Unchanged,
line,
comments.TryGetThreadForLine(line.ElementId, out var thread) ? thread : null,
diagnostics.Where(d => d.TargetId == line.ElementId).ToArray(),
++lineNumberExcludingDocumentation,
documentedByLines.ToArray()
);
documentedByLines.Clear();
return c;
}
}).ToArray();
}

private int ComputeActiveConversations(CodeLine[] lines, ReviewCommentsModel comments)
Expand Down Expand Up @@ -240,12 +261,11 @@ public async Task<IActionResult> OnPostToggleApprovalAsync(string id, string rev
return RedirectToPage(new { id = id });
}

public Dictionary<string, string> GetRoutingData(string diffRevisionId = null, bool? showDocumentation = null, bool? showDiffOnly = null, string revisionId = null)
public Dictionary<string, string> GetRoutingData(string diffRevisionId = null, bool? showDiffOnly = null, string revisionId = null)
{
var routingData = new Dictionary<string, string>();
routingData["revisionId"] = revisionId;
routingData["diffRevisionId"] = diffRevisionId;
routingData["doc"] = (showDocumentation ?? false).ToString();
routingData["diffOnly"] = (showDiffOnly ?? false).ToString();
return routingData;
}
Expand Down
Loading

0 comments on commit 0bf2dda

Please sign in to comment.