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

Commit 024a552

Browse files
committed
Only create margins when in context of a Git repo
Use all `Lazy<>` imports for ImportingConstructor.
1 parent 4af9d1c commit 024a552

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed
Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
22
using System.ComponentModel.Composition;
3+
using Microsoft.VisualStudio.Shell;
34
using Microsoft.VisualStudio.Utilities;
45
using Microsoft.VisualStudio.Text.Editor;
56
using Microsoft.VisualStudio.Text.Tagging;
67
using Microsoft.VisualStudio.Text.Classification;
7-
using GitHub.InlineReviews.Services;
88
using GitHub.Services;
9+
using GitHub.VisualStudio;
10+
using GitHub.InlineReviews.Services;
911

1012
namespace GitHub.InlineReviews.Margins
1113
{
@@ -17,28 +19,41 @@ namespace GitHub.InlineReviews.Margins
1719
[TextViewRole(PredefinedTextViewRoles.Interactive)]
1820
internal sealed class InlineCommentMarginProvider : IWpfTextViewMarginProvider
1921
{
20-
readonly IEditorFormatMapService editorFormatMapService;
21-
readonly IViewTagAggregatorFactoryService tagAggregatorFactory;
22-
readonly IInlineCommentPeekService peekService;
22+
readonly Lazy<IEditorFormatMapService> editorFormatMapService;
23+
readonly Lazy<IViewTagAggregatorFactoryService> tagAggregatorFactory;
24+
readonly Lazy<IInlineCommentPeekService> peekService;
2325
readonly Lazy<IPullRequestSessionManager> sessionManager;
26+
readonly UIContext uiContext;
2427

2528
[ImportingConstructor]
2629
public InlineCommentMarginProvider(
27-
IGitHubServiceProvider serviceProvider,
28-
IEditorFormatMapService editorFormatMapService,
29-
IViewTagAggregatorFactoryService tagAggregatorFactory,
30-
IInlineCommentPeekService peekService)
30+
Lazy<IGitHubServiceProvider> serviceProvider,
31+
Lazy<IEditorFormatMapService> editorFormatMapService,
32+
Lazy<IViewTagAggregatorFactoryService> tagAggregatorFactory,
33+
Lazy<IInlineCommentPeekService> peekService)
3134
{
3235
this.editorFormatMapService = editorFormatMapService;
3336
this.tagAggregatorFactory = tagAggregatorFactory;
3437
this.peekService = peekService;
35-
sessionManager = new Lazy<IPullRequestSessionManager>(() => serviceProvider.GetService<IPullRequestSessionManager>());
38+
sessionManager = new Lazy<IPullRequestSessionManager>(() => serviceProvider.Value.GetService<IPullRequestSessionManager>());
39+
40+
uiContext = UIContext.FromUIContextGuid(new Guid(Guids.UIContext_Git));
3641
}
3742

3843
public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin parent)
3944
{
45+
if (!uiContext.IsActive)
46+
{
47+
// Only create margin when in the context of a Git repository
48+
return null;
49+
}
50+
4051
return new InlineCommentMargin(
41-
wpfTextViewHost, peekService, editorFormatMapService, tagAggregatorFactory, sessionManager);
52+
wpfTextViewHost,
53+
peekService.Value,
54+
editorFormatMapService.Value,
55+
tagAggregatorFactory.Value,
56+
sessionManager);
4257
}
4358
}
4459
}

src/GitHub.InlineReviews/Margins/PullRequestFileMarginProvider.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using GitHub.Commands;
44
using GitHub.Services;
55
using GitHub.Settings;
6+
using GitHub.VisualStudio;
7+
using Microsoft.VisualStudio.Shell;
68
using Microsoft.VisualStudio.Utilities;
79
using Microsoft.VisualStudio.Text.Editor;
810

@@ -19,25 +21,28 @@ namespace GitHub.InlineReviews.Margins
1921
[TextViewRole(PredefinedTextViewRoles.Editable)]
2022
internal sealed class PullRequestFileMarginProvider : IWpfTextViewMarginProvider
2123
{
22-
readonly IPullRequestSessionManager sessionManager;
23-
readonly IToggleInlineCommentMarginCommand enableInlineCommentsCommand;
24-
readonly IGoToSolutionOrPullRequestFileCommand goToSolutionOrPullRequestFileCommand;
25-
readonly IPackageSettings packageSettings;
24+
readonly Lazy<IPullRequestSessionManager> sessionManager;
25+
readonly Lazy<IToggleInlineCommentMarginCommand> enableInlineCommentsCommand;
26+
readonly Lazy<IGoToSolutionOrPullRequestFileCommand> goToSolutionOrPullRequestFileCommand;
27+
readonly Lazy<IPackageSettings> packageSettings;
2628
readonly Lazy<IUsageTracker> usageTracker;
29+
readonly UIContext uiContext;
2730

2831
[ImportingConstructor]
2932
public PullRequestFileMarginProvider(
30-
IToggleInlineCommentMarginCommand enableInlineCommentsCommand,
31-
IGoToSolutionOrPullRequestFileCommand goToSolutionOrPullRequestFileCommand,
32-
IPullRequestSessionManager sessionManager,
33-
IPackageSettings packageSettings,
33+
Lazy<IToggleInlineCommentMarginCommand> enableInlineCommentsCommand,
34+
Lazy<IGoToSolutionOrPullRequestFileCommand> goToSolutionOrPullRequestFileCommand,
35+
Lazy<IPullRequestSessionManager> sessionManager,
36+
Lazy<IPackageSettings> packageSettings,
3437
Lazy<IUsageTracker> usageTracker)
3538
{
3639
this.enableInlineCommentsCommand = enableInlineCommentsCommand;
3740
this.goToSolutionOrPullRequestFileCommand = goToSolutionOrPullRequestFileCommand;
3841
this.sessionManager = sessionManager;
3942
this.packageSettings = packageSettings;
4043
this.usageTracker = usageTracker;
44+
45+
uiContext = UIContext.FromUIContextGuid(new Guid(Guids.UIContext_Git));
4146
}
4247

4348
/// <summary>
@@ -50,8 +55,14 @@ public PullRequestFileMarginProvider(
5055
/// </returns>
5156
public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin marginContainer)
5257
{
58+
if (!uiContext.IsActive)
59+
{
60+
// Only create margin when in the context of a Git repository
61+
return null;
62+
}
63+
5364
// Comments in the editor feature flag
54-
if (!packageSettings.EditorComments)
65+
if (!packageSettings.Value.EditorComments)
5566
{
5667
return null;
5768
}
@@ -63,7 +74,11 @@ public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTex
6374
}
6475

6576
return new PullRequestFileMargin(
66-
wpfTextViewHost.TextView, enableInlineCommentsCommand, goToSolutionOrPullRequestFileCommand, sessionManager, usageTracker);
77+
wpfTextViewHost.TextView,
78+
enableInlineCommentsCommand.Value,
79+
goToSolutionOrPullRequestFileCommand.Value,
80+
sessionManager.Value,
81+
usageTracker);
6782
}
6883

6984
bool IsDiffView(ITextView textView) => textView.Roles.Contains("DIFF");

0 commit comments

Comments
 (0)