Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit bb30e4d

Browse files
Adding functionality to create a tag object for a comment thread and annotations
1 parent db07173 commit bb30e4d

File tree

3 files changed

+93
-35
lines changed

3 files changed

+93
-35
lines changed

src/GitHub.InlineReviews/Tags/InlineTagger.cs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,40 +87,56 @@ public IEnumerable<ITagSpan<InlineTag>> GetTags(NormalizedSnapshotSpanCollection
8787
var linesWithTags = new BitArray((endLine - startLine) + 1);
8888
var spanThreads = file.InlineCommentThreads.Where(x =>
8989
x.LineNumber >= startLine &&
90-
x.LineNumber <= endLine);
90+
x.LineNumber <= endLine)
91+
.ToArray();
9192

92-
foreach (var thread in spanThreads)
93-
{
94-
var snapshot = span.Snapshot;
95-
var line = snapshot.GetLineFromLineNumber(thread.LineNumber);
93+
var spanThreadsByLine = spanThreads.ToDictionary(model => model.LineNumber);
9694

97-
if ((side == DiffSide.Left && thread.DiffLineType == DiffChangeType.Delete) ||
98-
(side == DiffSide.Right && thread.DiffLineType != DiffChangeType.Delete))
99-
{
100-
linesWithTags[thread.LineNumber - startLine] = true;
95+
Dictionary<int, IInlineAnnotationModel[]> spanAnnotationsByLine = null;
96+
if (side == DiffSide.Right)
97+
{
98+
var spanAnnotations = file.InlineAnnotations.Where(x =>
99+
x.EndLine - 1 >= startLine &&
100+
x.EndLine - 1 <= endLine);
101101

102-
result.Add(new TagSpan<ShowInlineTag>(
103-
new SnapshotSpan(line.Start, line.End),
104-
new ShowInlineTag(currentSession, thread, thread.LineNumber, thread.DiffLineType)));
105-
}
102+
spanAnnotationsByLine = spanAnnotations
103+
.GroupBy(model => model.EndLine)
104+
.ToDictionary(models => models.Key - 1, models => models.ToArray());
106105
}
107106

108-
var spanAnnotations = file.InlineAnnotations.Where(x =>
109-
x.EndLine - 1 >= startLine &&
110-
x.EndLine - 1 <= endLine);
111-
112-
foreach (var annotation in spanAnnotations)
107+
var lines = spanThreadsByLine.Keys.Union(spanAnnotationsByLine?.Keys ?? Enumerable.Empty<int>());
108+
foreach (var line in lines)
113109
{
114110
var snapshot = span.Snapshot;
115-
var line = snapshot.GetLineFromLineNumber(annotation.EndLine - 1);
111+
var snapshotLine = snapshot.GetLineFromLineNumber(line);
116112

117-
if (side == DiffSide.Right)
113+
IInlineCommentThreadModel thread;
114+
if (spanThreadsByLine.TryGetValue(line, out thread))
118115
{
119-
linesWithTags[annotation.EndLine - 1 - startLine] = true;
116+
var isThreadDeleteSide = thread.DiffLineType == DiffChangeType.Delete;
117+
var sidesMatch = side == DiffSide.Left && isThreadDeleteSide || side == DiffSide.Right && !isThreadDeleteSide;
118+
if (!sidesMatch)
119+
{
120+
thread = null;
121+
}
122+
}
123+
124+
IInlineAnnotationModel[] annotations = null;
125+
spanAnnotationsByLine?.TryGetValue(line, out annotations);
126+
127+
if (thread != null || annotations != null)
128+
{
129+
linesWithTags[line - startLine] = true;
130+
131+
var showInlineTag = new ShowInlineTag(currentSession, line, thread?.DiffLineType ?? DiffChangeType.Add)
132+
{
133+
Thread = thread,
134+
Annotations = annotations
135+
};
120136

121137
result.Add(new TagSpan<ShowInlineTag>(
122-
new SnapshotSpan(line.Start, line.End),
123-
new ShowInlineTag(currentSession, null, annotation.EndLine, DiffChangeType.Add)));
138+
new SnapshotSpan(snapshotLine.Start, snapshotLine.End),
139+
showInlineTag));
124140
}
125141
}
126142

src/GitHub.InlineReviews/Tags/ShowInlineTag.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@ public class ShowInlineTag : InlineTag
1414
/// Initializes a new instance of the <see cref="ShowInlineTag"/> class.
1515
/// </summary>
1616
/// <param name="session">The pull request session.</param>
17-
/// <param name="thread">A model holding the details of the thread.</param>
1817
/// <param name="lineNumber"></param>
1918
/// <param name="diffLineType"></param>
20-
public ShowInlineTag(IPullRequestSession session,
21-
IInlineCommentThreadModel thread, int lineNumber, DiffChangeType diffLineType)
19+
public ShowInlineTag(IPullRequestSession session, int lineNumber, DiffChangeType diffLineType)
2220
: base(session, lineNumber, diffLineType)
2321
{
24-
Thread = thread;
2522
}
2623

2724
/// <summary>
2825
/// Gets a model holding details of the thread at the tagged line.
2926
/// </summary>
30-
public IInlineCommentThreadModel Thread { get; }
27+
public IInlineCommentThreadModel Thread { get; set; }
28+
29+
/// <summary>
30+
/// Gets an array of models holding details of the annotations at the tagged line.
31+
/// </summary>
32+
public IInlineAnnotationModel[] Annotations { get; set; }
3133
}
3234
}

test/GitHub.InlineReviews.UnitTests/Tags/InlineTaggerTests.cs

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ public void ShouldReturnShowInlineTagForComment()
252252
var result = target.GetTags(span).ToList();
253253

254254
Assert.That(result, Has.One.Items);
255-
Assert.That(result[0].Tag, Is.InstanceOf<ShowInlineTag>());
255+
256+
var showInlineTag = result[0].Tag as ShowInlineTag;
257+
Assert.That(showInlineTag, Is.Not.Null);
258+
Assert.That(showInlineTag.Thread, Is.Not.Null);
259+
Assert.That(showInlineTag.Annotations, Is.Null);
256260
}
257261

258262
[Test]
@@ -264,13 +268,41 @@ public void ShouldReturnShowInlineTagForAnnotation()
264268
Substitute.For<ITextBuffer>(),
265269
CreateSessionManager(file));
266270

267-
// Line 10 has an existing RHS comment.
271+
// Line 10 has an existing RHS annotation.
268272
var span = CreateSpan(10);
269273
var firstPass = target.GetTags(span);
270274
var result = target.GetTags(span).ToList();
271275

272276
Assert.That(result, Has.One.Items);
273-
Assert.That(result[0].Tag, Is.InstanceOf<ShowInlineTag>());
277+
278+
var showInlineTag = result[0].Tag as ShowInlineTag;
279+
Assert.That(showInlineTag, Is.Not.Null);
280+
Assert.That(showInlineTag.Thread, Is.Null);
281+
Assert.That(showInlineTag.Annotations, Is.Not.Null);
282+
Assert.That(showInlineTag.Annotations.Length, Is.EqualTo(1));
283+
}
284+
285+
[Test]
286+
public void ShouldReturnShowInlineTagForTwoAnnotations()
287+
{
288+
var file = CreateSessionFile(false, true);
289+
var target = new InlineTagger(
290+
Substitute.For<ITextView>(),
291+
Substitute.For<ITextBuffer>(),
292+
CreateSessionManager(file));
293+
294+
// Line 20 has an existing RHS annotation.
295+
var span = CreateSpan(20);
296+
var firstPass = target.GetTags(span);
297+
var result = target.GetTags(span).ToList();
298+
299+
Assert.That(result, Has.One.Items);
300+
301+
var showInlineTag = result[0].Tag as ShowInlineTag;
302+
Assert.That(showInlineTag, Is.Not.Null);
303+
Assert.That(showInlineTag.Thread, Is.Null);
304+
Assert.That(showInlineTag.Annotations, Is.Not.Null);
305+
Assert.That(showInlineTag.Annotations.Length, Is.EqualTo(2));
274306
}
275307

276308
[Test]
@@ -287,8 +319,13 @@ public void ShouldReturnShowOneInlineTagForCommentAndAnnotation()
287319
var firstPass = target.GetTags(span);
288320
var result = target.GetTags(span).ToList();
289321

290-
Assert.That(result.Count, Is.EqualTo(2));
291-
Assert.That(result[0].Tag, Is.InstanceOf<ShowInlineTag>());
322+
Assert.That(result, Has.One.Items);
323+
324+
var showInlineTag = result[0].Tag as ShowInlineTag;
325+
Assert.That(showInlineTag, Is.Not.Null);
326+
Assert.That(showInlineTag.Thread, Is.Not.Null);
327+
Assert.That(showInlineTag.Annotations, Is.Not.Null);
328+
Assert.That(showInlineTag.Annotations.Length, Is.EqualTo(1));
292329
}
293330

294331
[Test]
@@ -395,7 +432,7 @@ static ITextSnapshot CreateSnapshot()
395432
{
396433
// We pretend that each line has 10 chars and there are 20 lines.
397434
var result = Substitute.For<ITextSnapshot>();
398-
result.Length.Returns(200);
435+
result.Length.Returns(300);
399436
result.GetLineFromPosition(0).ReturnsForAnyArgs(x => CreateLine(result, x.Arg<int>() / 10));
400437
result.GetLineFromLineNumber(0).ReturnsForAnyArgs(x => CreateLine(result, x.Arg<int>()));
401438
return result;
@@ -470,7 +507,10 @@ static IPullRequestSessionFile CreateSessionFile(bool withComments = true, bool
470507
var annotation2 = Substitute.For<IInlineAnnotationModel>();
471508
annotation2.EndLine.Returns(21);
472509

473-
var annotations = new List<IInlineAnnotationModel> { annotation1, annotation2 };
510+
var annotation3 = Substitute.For<IInlineAnnotationModel>();
511+
annotation3.EndLine.Returns(21);
512+
513+
var annotations = new List<IInlineAnnotationModel> { annotation1, annotation2, annotation3 };
474514

475515
file.InlineAnnotations.Returns(annotations);
476516
}

0 commit comments

Comments
 (0)