This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
InlineCommentPeekService.cs
175 lines (149 loc) · 6.65 KB
/
InlineCommentPeekService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GitHub.Api;
using GitHub.Extensions;
using GitHub.Factories;
using GitHub.InlineReviews.Peek;
using GitHub.InlineReviews.Tags;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Differencing;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Outlining;
using Microsoft.VisualStudio.Text.Projection;
namespace GitHub.InlineReviews.Services
{
/// <summary>
/// Shows inline comments in a peek view.
/// </summary>
[Export(typeof(IInlineCommentPeekService))]
class InlineCommentPeekService : IInlineCommentPeekService
{
readonly IOutliningManagerService outliningService;
readonly IPeekBroker peekBroker;
readonly IUsageTracker usageTracker;
[ImportingConstructor]
public InlineCommentPeekService(
IOutliningManagerService outliningManager,
IPeekBroker peekBroker,
IUsageTracker usageTracker)
{
this.outliningService = outliningManager;
this.peekBroker = peekBroker;
this.usageTracker = usageTracker;
}
/// <inheritdoc/>
public Tuple<int, bool> GetLineNumber(IPeekSession session, ITrackingPoint point)
{
var diffModel = (session.TextView as IWpfTextView)?.TextViewModel as IDifferenceTextViewModel;
var leftBuffer = false;
ITextSnapshotLine line = null;
if (diffModel != null)
{
if (diffModel.ViewType == DifferenceViewType.InlineView)
{
// If we're displaying a diff in inline mode, then we need to map the point down
// to the left or right buffer.
var snapshotPoint = point.GetPoint(point.TextBuffer.CurrentSnapshot);
var mappedPoint = session.TextView.BufferGraph.MapDownToFirstMatch(
snapshotPoint,
PointTrackingMode.Negative,
x => !(x is IProjectionSnapshot),
PositionAffinity.Successor);
if (mappedPoint != null)
{
leftBuffer = mappedPoint.Value.Snapshot == diffModel.Viewer.DifferenceBuffer.LeftBuffer.CurrentSnapshot;
line = mappedPoint.Value.GetContainingLine();
}
}
else
{
// If we're displaying a diff in any other mode than inline, then we're in the
// left buffer if the session's text view is the diff's left view.
leftBuffer = session.TextView == diffModel.Viewer.LeftView;
}
}
if (line == null)
{
line = point.GetPoint(point.TextBuffer.CurrentSnapshot).GetContainingLine();
}
return Tuple.Create(line.LineNumber, leftBuffer);
}
/// <inheritdoc/>
public void Hide(ITextView textView)
{
peekBroker.DismissPeekSession(textView);
}
/// <inheritdoc/>
public ITrackingPoint Show(ITextView textView, AddInlineCommentTag tag)
{
Guard.ArgumentNotNull(tag, nameof(tag));
var lineAndtrackingPoint = GetLineAndTrackingPoint(textView, tag);
var line = lineAndtrackingPoint.Item1;
var trackingPoint = lineAndtrackingPoint.Item2;
var options = new PeekSessionCreationOptions(
textView,
InlineCommentPeekRelationship.Instance.Name,
trackingPoint,
defaultHeight: 0);
ExpandCollapsedRegions(textView, line.Extent);
var session = peekBroker.TriggerPeekSession(options);
var item = session.PeekableItems.OfType<InlineCommentPeekableItem>().FirstOrDefault();
item?.ViewModel.Close.Take(1).Subscribe(_ => session.Dismiss());
return trackingPoint;
}
/// <inheritdoc/>
public ITrackingPoint Show(ITextView textView, ShowInlineCommentTag tag)
{
Guard.ArgumentNotNull(textView, nameof(textView));
Guard.ArgumentNotNull(tag, nameof(tag));
var lineAndtrackingPoint = GetLineAndTrackingPoint(textView, tag);
var line = lineAndtrackingPoint.Item1;
var trackingPoint = lineAndtrackingPoint.Item2;
var options = new PeekSessionCreationOptions(
textView,
InlineCommentPeekRelationship.Instance.Name,
trackingPoint,
defaultHeight: 0);
ExpandCollapsedRegions(textView, line.Extent);
var session = peekBroker.TriggerPeekSession(options);
var item = session.PeekableItems.OfType<InlineCommentPeekableItem>().FirstOrDefault();
item?.ViewModel.Close.Take(1).Subscribe(_ => session.Dismiss());
return trackingPoint;
}
Tuple<ITextSnapshotLine, ITrackingPoint> GetLineAndTrackingPoint(ITextView textView, InlineCommentTag tag)
{
var diffModel = (textView as IWpfTextView)?.TextViewModel as IDifferenceTextViewModel;
var snapshot = textView.TextSnapshot;
if (diffModel?.ViewType == DifferenceViewType.InlineView)
{
snapshot = tag.DiffChangeType == DiffChangeType.Delete ?
diffModel.Viewer.DifferenceBuffer.LeftBuffer.CurrentSnapshot :
diffModel.Viewer.DifferenceBuffer.RightBuffer.CurrentSnapshot;
}
var line = snapshot.GetLineFromLineNumber(tag.LineNumber);
var trackingPoint = snapshot.CreateTrackingPoint(line.Start.Position, PointTrackingMode.Positive);
ExpandCollapsedRegions(textView, line.Extent);
peekBroker.TriggerPeekSession(textView, trackingPoint, InlineCommentPeekRelationship.Instance.Name);
usageTracker.IncrementCounter(x => x.NumberOfPRReviewDiffViewInlineCommentOpen).Forget();
return Tuple.Create(line, trackingPoint);
}
void ExpandCollapsedRegions(ITextView textView, SnapshotSpan span)
{
var outlining = outliningService.GetOutliningManager(textView);
if (outlining != null)
{
foreach (var collapsed in outlining.GetCollapsedRegions(span))
{
outlining.Expand(collapsed);
}
}
}
}
}