Skip to content

Commit

Permalink
Minor finalizing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Baas-hub committed Aug 19, 2022
1 parent 8218958 commit 455f571
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 144 deletions.
8 changes: 4 additions & 4 deletions src/dotnet/APIView/APIViewWeb/Client/css/usageSample.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
font-size: 125%;
}

code {
font-size: 100%;
}

.code-inner {
height: 100%;
}

.usage-sample .internal {
display: inline-block;
}
288 changes: 173 additions & 115 deletions src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Samples.cshtml

Large diffs are not rendered by default.

50 changes: 30 additions & 20 deletions src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Samples.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class UsageSamplePageModel : PageModel
public string Endpoint { get; }
public ReviewModel Review { get; private set; }
public UsageSampleModel Sample { get; private set; }
public List<UsageSampleModel> SampleRevisions { get; private set; }
public IEnumerable<UsageSampleModel> SampleRevisions { get; private set; }
public CodeLineModel[] SampleContent { get; set; }
public ReviewCommentsModel Comments { get; set; }
public string SampleOriginal { get; set; }
Expand Down Expand Up @@ -59,33 +59,32 @@ public async Task<IActionResult> OnGetAsync(string id, string revisionId = null)
Review = await _reviewManager.GetReviewAsync(User, id);
Comments = await _commentsManager.GetUsageSampleCommentsAsync(id);
latestRevision = -1;

// This try-catch is for the case that the deployment is set up incorrectly for usage samples
try
{
SampleRevisions = await _samplesManager.GetReviewUsageSampleAsync(id);
var SampleRevisionList = await _samplesManager.GetReviewUsageSampleAsync(id);
SampleRevisions = SampleRevisionList.OrderByDescending(e => e.RevisionNum);
if (SampleRevisions.Any())
{
if (SampleRevisions.Count > 1)
if (SampleRevisions.Count() > 1)
{
for (int i = 0; i < SampleRevisions.Count; i++)
{
if (SampleRevisions[i].RevisionNum > latestRevision)
{
latestRevision = SampleRevisions[i].RevisionNum;
}
}
// get latest revision num (useful for ordering)
latestRevision = SampleRevisions.First().RevisionNum;

// if a specific revision was selected, find it
if (revisionId != null)
{
Sample = SampleRevisions.Find(e => e.SampleId == revisionId);
Sample = SampleRevisions.Where(e => e.SampleId == revisionId).First();
}
else
{
Sample = SampleRevisions.Find(e => e.RevisionNum == latestRevision);
Sample = SampleRevisions.First();
}
}
else
{
Sample = SampleRevisions[0];
Sample = SampleRevisions.First();
latestRevision = 0;
}

Expand All @@ -94,17 +93,20 @@ public async Task<IActionResult> OnGetAsync(string id, string revisionId = null)
Upload.updateString = SampleOriginal;
if (SampleContent == null)
{
Sample.SampleId = "Bad Deployment";
// Potentially bad blob setup, potentially erroneous file fetch
Sample.SampleId = "File Content Missing";
}
}
else
{
// No samples.
Sample = new UsageSampleModel(null, Review.ReviewId, -1);
SampleContent = Array.Empty<CodeLineModel>();
}
}
catch (CosmosException e)
{
// Error gracefully
Sample = new UsageSampleModel(null, null);
Sample.SampleId = "Bad Deployment";
}
Expand Down Expand Up @@ -159,34 +161,42 @@ private async Task<CodeLineModel[]> ParseLines(string fileId, ReviewCommentsMode
string rawContent = (await _samplesManager.GetUsageSampleContentAsync(fileId));
if (rawContent == null)
{
return null;
return null; // should only occur if there is a blob error or the file is removed by other means
}
rawContent = rawContent.Trim();
string[] content = rawContent.Split("\n");

CodeLineModel[] lines = new CodeLineModel[content.Length];
CodeDiagnostic[] cd = Array.Empty<CodeDiagnostic>();
CodeDiagnostic[] cd = Array.Empty<CodeDiagnostic>(); // Avoids errors
int skipped = 0;
for (int i = 0; i < content.Length; i++)
{
string lineContent = content[i];

if (lineContent.StartsWith("<div class="))
// remove the newlines before codeblocks
if (lineContent.StartsWith("<div class=\"code"))
{
lineContent = "";
}
lineContent = lineContent.Replace("<pre>", "").Replace("</pre>", "").Replace("</div>", "");

// remove pre and closing div elements to clear more excess newlines
lineContent = lineContent.Replace("<pre>", "").Replace("</pre>", "").Replace("</div>", "").Replace("\n", "");
if (lineContent.Trim() == "")
{
// Count lines skipped to keep indexing correct
skipped++;
continue;
}

var line = new CodeLine(lineContent, Sample.SampleId + "-line-" + (i+ 1-skipped).ToString() , "");
comments.TryGetThreadForLine(Sample.SampleId + "-line-" + (i+ 1-skipped).ToString(), out var thread);
// Allows the indent to work correctly for spacing purposes
lineContent = "<div class=\"internal\">&nbsp;&nbsp;&nbsp;" + lineContent + "</div>";

var line = new CodeLine(lineContent, Sample.SampleId + "-line-" + (i+1-skipped).ToString() , "");
comments.TryGetThreadForLine(Sample.SampleId + "-line-" + (i+1-skipped).ToString(), out var thread);
lines[i] = new CodeLineModel(APIView.DIff.DiffLineKind.Unchanged, line, thread, cd, i+1-skipped);
}

// Removes excess lines added that cause errors
return Array.FindAll(lines, e => !(e.Diagnostics == null));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public async Task<string> GetUsageSampleContentAsync(string fileId)

public async Task<UsageSampleModel> UpsertReviewUsageSampleAsync(ClaimsPrincipal user, string reviewId, string sample, int revisionNum, string revisionTitle)
{
// markdig parser with syntax highlighting
MarkdownPipeline pipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.UseSyntaxHighlighting()
Expand All @@ -72,14 +73,14 @@ public async Task<UsageSampleModel> UpsertReviewUsageSampleAsync(ClaimsPrincipal
SampleModel.UsageSampleOriginalFileId = SampleOriginal.UsageSampleFileId;

await _samplesRepository.UpsertUsageSampleAsync(SampleModel);

await _sampleFilesRepository.UploadUsageSampleAsync(SampleModel.UsageSampleFileId, stream);
await _sampleFilesRepository.UploadUsageSampleAsync(SampleModel.UsageSampleOriginalFileId, originalStream);
return SampleModel;
}

public async Task<UsageSampleModel> UpsertReviewUsageSampleAsync(ClaimsPrincipal user, string reviewId, Stream fileStream, int revisionNum, string revisionTitle)
{
// For file upload. Read stream then continue.
StreamReader reader = new StreamReader(fileStream);
var sample = reader.ReadToEnd();
return await UpsertReviewUsageSampleAsync(user, reviewId, sample, revisionNum, revisionTitle);
Expand All @@ -95,8 +96,8 @@ public async Task DeleteUsageSampleAsync(ClaimsPrincipal user, string reviewId,
var comments = await _commentsRepository.GetCommentsAsync(reviewId);
foreach (var comment in comments)
{
string commentSampleId = comment.ElementId.Split("-")[0];
if (comment.IsSampleComment && commentSampleId == sampleId)
string commentSampleId = comment.ElementId.Split("-")[0]; // sample id is stored as first part of ElementId
if (comment.IsSampleComment && commentSampleId == sampleId) // remove all comments from server
{
await _commentsRepository.DeleteCommentAsync(comment);
}
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet/APIView/APIViewWeb/wwwroot/usagesample.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/dotnet/APIView/APIViewWeb/wwwroot/usagesample.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 455f571

Please sign in to comment.