Skip to content

Commit

Permalink
Code-window working
Browse files Browse the repository at this point in the history
  • Loading branch information
chidozieononiwu committed Oct 9, 2023
1 parent 06e25e5 commit aefd20e
Show file tree
Hide file tree
Showing 19 changed files with 327 additions and 232 deletions.
172 changes: 171 additions & 1 deletion src/dotnet/APIView/APIViewWeb/Helpers/PageModelHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using System.Security.Claims;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using APIView.DIff;
using ApiView;
using APIView;
using APIViewWeb.Models;
using APIViewWeb.Repositories;
using System;

namespace APIViewWeb.Helpers
{
Expand All @@ -20,5 +26,169 @@ public static string GetHiddenApiClass(UserPreferenceModel userPreference)
}
return hiddenApiClass;
}

public static CodeLineModel[] CreateLines(CodeDiagnostic[] diagnostics, InlineDiffLine<CodeLine>[] lines,
ReviewCommentsModel comments, bool showDiffOnly, int reviewDiffContextSize, string diffContextSeparator,
HashSet<int> headingsOfSectionsWithDiff, bool hideCommentRows = false)
{
if (showDiffOnly)
{
lines = CreateDiffOnlyLines(lines, reviewDiffContextSize, diffContextSeparator);
if (lines.Length == 0)
{
return Array.Empty<CodeLineModel>();
}
}
List<int> documentedByLines = new List<int>();
int lineNumberExcludingDocumentation = 0;
int diffSectionId = 0;

return lines.Select(
(diffLine, index) =>
{
if (diffLine.Line.IsDocumentation)
{
// documentedByLines must include the index of a line, assuming that documentation lines are counted
documentedByLines.Add(++index);
return new CodeLineModel(
kind: diffLine.Kind,
codeLine: diffLine.Line,
commentThread: comments.TryGetThreadForLine(diffLine.Line.ElementId, out var thread, hideCommentRows) ?
thread :
null,
diagnostics: diffLine.Kind != DiffLineKind.Removed ?
diagnostics.Where(d => d.TargetId == diffLine.Line.ElementId).ToArray() :
Array.Empty<CodeDiagnostic>(),
lineNumber: lineNumberExcludingDocumentation,
documentedByLines: new int[] { },
isDiffView: true,
diffSectionId: diffLine.Line.SectionKey != null ? ++diffSectionId : null,
otherLineSectionKey: diffLine.Kind == DiffLineKind.Unchanged ? diffLine.OtherLine.SectionKey : null,
headingsOfSectionsWithDiff: headingsOfSectionsWithDiff,
isSubHeadingWithDiffInSection: diffLine.IsHeadingWithDiffInSection
);
}
else
{
CodeLineModel c = new CodeLineModel(
kind: diffLine.Kind,
codeLine: diffLine.Line,
commentThread: diffLine.Kind != DiffLineKind.Removed &&
comments.TryGetThreadForLine(diffLine.Line.ElementId, out var thread, hideCommentRows) ?
thread :
null,
diagnostics: diffLine.Kind != DiffLineKind.Removed ?
diagnostics.Where(d => d.TargetId == diffLine.Line.ElementId).ToArray() :
Array.Empty<CodeDiagnostic>(),
lineNumber: diffLine.Line.LineNumber ?? ++lineNumberExcludingDocumentation,
documentedByLines: documentedByLines.ToArray(),
isDiffView: true,
diffSectionId: diffLine.Line.SectionKey != null ? ++diffSectionId : null,
otherLineSectionKey: diffLine.Kind == DiffLineKind.Unchanged ? diffLine.OtherLine.SectionKey : null,
headingsOfSectionsWithDiff: headingsOfSectionsWithDiff,
isSubHeadingWithDiffInSection: diffLine.IsHeadingWithDiffInSection
);
documentedByLines.Clear();
return c;
}
}).ToArray();
}

public static CodeLineModel[] CreateLines(CodeDiagnostic[] diagnostics, CodeLine[] lines, ReviewCommentsModel comments, bool hideCommentRows = false)
{
List<int> documentedByLines = new List<int>();
int lineNumberExcludingDocumentation = 0;
return lines.Select(
(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, hideCommentRows) ? 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, hideCommentRows) ? thread : null,
diagnostics.Where(d => d.TargetId == line.ElementId).ToArray(),
line.LineNumber ?? ++lineNumberExcludingDocumentation,
documentedByLines.ToArray()
);
documentedByLines.Clear();
return c;
}
}).ToArray();
}

public static int ComputeActiveConversations(CodeLine[] lines, ReviewCommentsModel comments)
{
int activeThreads = 0;
foreach (CodeLine line in lines)
{
if (string.IsNullOrEmpty(line.ElementId))
{
continue;
}

// if we have comments for this line and the thread has not been resolved.
// Add "&& !thread.Comments.First().IsUsageSampleComment()" to exclude sample comments from being counted (This also prevents the popup before approval)
if (comments.TryGetThreadForLine(line.ElementId, out CommentThreadModel thread) && !thread.IsResolved)
{
activeThreads++;
}
}
return activeThreads;
}

private static InlineDiffLine<CodeLine>[] CreateDiffOnlyLines(InlineDiffLine<CodeLine>[] lines, int reviewDiffContextSize, string diffContextSeparator)
{
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 - reviewDiffContextSize);
if (preContextIndx < i)
{
// Add sepearator to show skipping lines. for e.g. .....
if (filteredLines.Count > 0)
{
filteredLines.Add(new InlineDiffLine<CodeLine>(new CodeLine(diffContextSeparator, null, 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 + reviewDiffContextSize;
while (contextStart <= contextEnd && contextStart < lines.Count() && lines[contextStart].Kind == DiffLineKind.Unchanged)
{
filteredLines.Add(lines[contextStart]);
lastAddedLine = contextStart;
contextStart++;
}
}
}
return filteredLines.ToArray();
}
}
}
31 changes: 20 additions & 11 deletions src/dotnet/APIView/APIViewWeb/LeanControllers/ReviewsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using System.Threading.Tasks;
using APIViewWeb.Managers.Interfaces;
using System.Linq;
using System;
using APIView;
using Microsoft.AspNetCore.Authorization;

namespace APIViewWeb.LeanControllers
{
Expand All @@ -17,14 +20,17 @@ public class ReviewsController : BaseApiController
private readonly ILogger<ReviewsController> _logger;
private readonly IReviewManager _reviewManager;
private readonly IReviewRevisionsManager _reviewRevisionsManager;
private readonly ICommentsManager _commentManager;
private readonly IBlobCodeFileRepository _codeFileRepository;

public ReviewsController(ILogger<ReviewsController> logger,
IReviewRevisionsManager reviewRevisionsManager, IReviewManager reviewManager, IBlobCodeFileRepository codeFileRepository)
IReviewRevisionsManager reviewRevisionsManager, IReviewManager reviewManager,
ICommentsManager commentManager, IBlobCodeFileRepository codeFileRepository)
{
_logger = logger;
_reviewRevisionsManager = reviewRevisionsManager;
_reviewManager = reviewManager;
_commentManager = commentManager;
_codeFileRepository = codeFileRepository;
}

Expand All @@ -50,22 +56,25 @@ public async Task<ActionResult<PagedList<ReviewListItemModel>>> GetReviewsAsync(
///<returns></returns>
[HttpGet]
[Route("{reviewId}/content")]
public async Task<ActionResult<ReviewContentModel>> GetReviewContentAsync(string reviewId, string revisionId= null)
public async Task<ActionResult<ReviewContentModel>> GetReviewContentAsync(string reviewId, [FromQuery]string revisionId=null)
{
var review = await _reviewManager.GetReviewAsync(reviewId);
var revisions = await _reviewRevisionsManager.GetReviewRevisionsAsync(reviewId);
var activeRevision = (string.IsNullOrEmpty(revisionId)) ?
await _reviewRevisionsManager.GetLatestReviewRevisionsAsync(reviewId, revisions) : await _reviewRevisionsManager.GetReviewRevisionAsync(revisionId);
var review = await _reviewManager.GetReviewAsync(reviewId);
var revisions = await _reviewRevisionsManager.GetReviewRevisionsAsync(reviewId);
var activeRevision = (string.IsNullOrEmpty(revisionId)) ?
await _reviewRevisionsManager.GetLatestReviewRevisionsAsync(reviewId, revisions) : await _reviewRevisionsManager.GetReviewRevisionAsync(revisionId);
var comments = await _commentManager.GetReviewCommentsAsync(reviewId);


var reviewCodeFie = await _codeFileRepository.GetCodeFileAsync(activeRevision.Id, activeRevision.Files[0].ReviewFileId);
reviewCodeFie.Render(showDocumentation: true);
var renderableCodeFile = await _codeFileRepository.GetCodeFileAsync(activeRevision.Id, activeRevision.Files[0].ReviewFileId);
var reviewCodeFile = renderableCodeFile.CodeFile;
var fileDiagnostics = reviewCodeFile.Diagnostics ?? Array.Empty<CodeDiagnostic>();
var htmlLines = renderableCodeFile.Render(showDocumentation: false);
var codeLines = PageModelHelpers.CreateLines(diagnostics: fileDiagnostics, lines: htmlLines, comments: comments);

var pageModel = new ReviewContentModel
{
Review = review,
Navigation = reviewCodeFie.CodeFile.Navigation,
codeLines = reviewCodeFie.RenderResult.CodeLines,
Navigation = renderableCodeFile.CodeFile.Navigation,
codeLines = codeLines,
ReviewRevisions = revisions.GroupBy(r => r.ReviewRevisionType).ToDictionary(r => r.Key.ToString(), r => r.ToList()),
ActiveRevision = activeRevision
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Collections.Generic;
using ApiView;
using APIView;
using APIViewWeb.Models;

namespace APIViewWeb.LeanModels
{
public class ReviewContentModel
{
public ReviewListItemModel Review { get; set; }
public NavigationItem[] Navigation { get; set; }
public CodeLine[] codeLines { get; set; }
public CodeLineModel[] codeLines { get; set; }
public Dictionary<string, List<ReviewRevisionListItemModel>> ReviewRevisions { get; set; }
public ReviewRevisionListItemModel ActiveRevision { get; set; }
}
Expand Down
Loading

0 comments on commit aefd20e

Please sign in to comment.