Skip to content

Commit

Permalink
Add new option to show only diff (#1316)
Browse files Browse the repository at this point in the history
* Add new option to show only diff
  • Loading branch information
praveenkuttappan authored Jan 14, 2021
1 parent f17aa84 commit a39c08e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/dotnet/APIView/APIViewWeb/Client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import "./comments.ts";
import "./revisions.ts";
import "./file-input.ts";
import "./navbar.ts";
import "./documentation.ts";
import "./review.ts";
import "./reviews.ts";
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const SHOW_DOC_CHECK_COMPONENT = "#show-documentation-component";
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";

hideCheckboxIfNoDocs();

Expand All @@ -15,4 +17,9 @@
$(SHOW_DOC_CHECKBOX).on("click", e => {
$(SHOW_DOC_HREF)[0].click();
});

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

});
15 changes: 12 additions & 3 deletions src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Review.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
}
else
{
<a class="btn btn-sm btn-outline-secondary" asp-route-diffRevisionId="@Model.PreviousRevisions.Last().RevisionId" asp-route-doc="@Model.ShowDocumentation">Diff</a>
<a class="btn btn-sm btn-outline-secondary" asp-all-route-data=@Model.GetRoutingData(diffRevisionId: Model.PreviousRevisions.Last().RevisionId, showDocumentation: Model.ShowDocumentation, showDiffOnly: Model.ShowDiffOnly)>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)" asp-route-doc="@Model.ShowDocumentation">
<a class="dropdown-item" active-if="@(Model.DiffRevisionId == revision.RevisionId)" asp-all-route-data=@Model.GetRoutingData(diffRevisionId: revision.RevisionId, showDocumentation: Model.ShowDocumentation, showDiffOnly: Model.ShowDiffOnly)>
@revision.DisplayName
</a>
}
Expand Down Expand Up @@ -93,10 +93,19 @@
</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-route-diffRevisionId="@Model.DiffRevisionId" asp-route-doc="@(!Model.ShowDocumentation)">
<a class="text-dark show-document" asp-all-route-data=@Model.GetRoutingData(diffRevisionId: Model.DiffRevisionId, showDocumentation: !Model.ShowDocumentation, showDiffOnly: Model.ShowDiffOnly)>
<label>Show documentation</label>
</a>
</span>
@if (!String.IsNullOrEmpty(Model.DiffRevisionId))
{
<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)>
<label>Show only diff</label>
</a>
</span>
}
</div>
</div>
<div class="float-right">
Expand Down
61 changes: 61 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Review.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -15,6 +16,9 @@ namespace APIViewWeb.Pages.Assemblies
{
public class ReviewPageModel : PageModel
{
private static int REVIEW_DIFF_CONTEXT_SIZE = 3;
private const string DIFF_CONTEXT_SEPERATOR = "<br><span>.....</span><br>";

private readonly ReviewManager _manager;

private readonly BlobCodeFileRepository _codeFileRepository;
Expand Down Expand Up @@ -60,6 +64,9 @@ public ReviewPageModel(
[BindProperty(Name = "doc", SupportsGet = true)]
public bool ShowDocumentation { get; set; }

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

public async Task<IActionResult> OnGetAsync(string id, string revisionId = null)
{
TempData["Page"] = "api";
Expand Down Expand Up @@ -113,8 +120,54 @@ public async Task<IActionResult> OnGetAsync(string id, string revisionId = null)
return Page();
}

private InlineDiffLine<CodeLine>[] CreateDiffOnlyLines(InlineDiffLine<CodeLine>[] lines)
{
var filteredLines = new List<InlineDiffLine<CodeLine>>();
int lastAddedLine = -1;
for (int i = 0; i < lines.Count(); i++)
{
if (lines[i].Kind != DiffLineKind.Unchanged)
{
// Find starting index for pre context
int preContextIndx = Math.Max(lastAddedLine + 1, i - REVIEW_DIFF_CONTEXT_SIZE);
if (preContextIndx < i)
{
// Add sepearator to show skipping lines. for e.g. .....
if (filteredLines.Count > 0)
{
filteredLines.Add(new InlineDiffLine<CodeLine>(new CodeLine(DIFF_CONTEXT_SEPERATOR, null), DiffLineKind.Unchanged));
}

while (preContextIndx < i)
{
filteredLines.Add(lines[preContextIndx]);
preContextIndx++;
}
}
//Add changed line
filteredLines.Add(lines[i]);
lastAddedLine = i;

// Add post context
int contextStart = i +1, contextEnd = i + REVIEW_DIFF_CONTEXT_SIZE;
while (contextStart <= contextEnd && contextStart < lines.Count() && lines[contextStart].Kind == DiffLineKind.Unchanged)
{
filteredLines.Add(lines[contextStart]);
lastAddedLine = contextStart;
contextStart++;
}
}
}
return filteredLines.ToArray();
}

private CodeLineModel[] CreateLines(CodeDiagnostic[] diagnostics, InlineDiffLine<CodeLine>[] lines, ReviewCommentsModel comments)
{
if (ShowDiffOnly)
{
lines = CreateDiffOnlyLines(lines);
}

return lines.Select(
diffLine => new CodeLineModel(
diffLine.Kind,
Expand Down Expand Up @@ -185,5 +238,13 @@ public async Task<IActionResult> OnPostToggleApprovalAsync(string id, string rev
await _manager.ToggleApprovalAsync(User, id, revisionId);
return RedirectToPage(new { id = id });
}
public Dictionary<string, string> GetRoutingData(string diffRevisionId = null, bool? showDocumentation = null, bool? showDiffOnly = null)
{
var routingData = new Dictionary<string, string>();
routingData["diffRevisionId"] = diffRevisionId;
routingData["doc"] = (showDocumentation ?? false).ToString();
routingData["diffOnly"] = (showDiffOnly ?? false).ToString();
return routingData;
}
}
}

0 comments on commit a39c08e

Please sign in to comment.