Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't compute the information for the code definition window if not open #57307

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ private async Task UpdateForCaretPositionAsync(SnapshotPoint pointInRoslynSnapsh
{
await _asyncListener.Delay(TimeSpan.FromMilliseconds(250), cancellationToken).ConfigureAwait(false);

// If it's not open, don't do anything, since if we are going to show locations in metadata that might
// be expensive. This doesn't cause a functional issue, since opening the window clears whatever was previously there
// so the user won't notice we weren't doing anything when it was open.
if (!await _codeDefinitionWindowService.IsWindowOpenAsync(cancellationToken).ConfigureAwait(false))
{
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❔ How much penalty is there for calling SetContextAsync with an empty set of locations for this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the goal of doing that? Its entirely possible the service will throw away the call, I'm not certain.

}

var document = pointInRoslynSnapshot.Snapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.CodeDefinitionWindow
{
internal interface ICodeDefinitionWindowService
{
Task<bool> IsWindowOpenAsync(CancellationToken cancellationToken);
Task SetContextAsync(ImmutableArray<CodeDefinitionWindowLocation> locations, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Namespace Microsoft.CodeAnalysis.Editor.CodeDefinitionWindow.UnitTests
Public Sub New()
End Sub

Public Function IsWindowOpenAsync(cancellationToken As CancellationToken) As Task(Of Boolean) Implements ICodeDefinitionWindowService.IsWindowOpenAsync
Throw New NotImplementedException()
End Function

Public Function SetContextAsync(locations As ImmutableArray(Of CodeDefinitionWindowLocation), cancellationToken As CancellationToken) As Task Implements ICodeDefinitionWindowService.SetContextAsync
Throw New NotImplementedException()
End Function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
Expand All @@ -23,6 +24,9 @@ internal sealed class VisualStudioCodeDefinitionWindowService : ICodeDefinitionW
private readonly IAsyncServiceProvider _asyncServiceProvider;
private readonly IThreadingContext _threadingContext;

[DisallowNull]
private IVsCodeDefView? _lazyCodeDefView;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public VisualStudioCodeDefinitionWindowService(SVsServiceProvider asyncServiceProvider, IThreadingContext threadingContext)
Expand All @@ -31,6 +35,29 @@ public VisualStudioCodeDefinitionWindowService(SVsServiceProvider asyncServicePr
_threadingContext = threadingContext;
}

private async Task<IVsCodeDefView> GetVsCodeDefViewAsync()
{
if (_lazyCodeDefView is not null)
{
return _lazyCodeDefView;
}

_lazyCodeDefView = await _asyncServiceProvider.GetServiceAsync<SVsCodeDefView, IVsCodeDefView>().ConfigureAwait(true);

return _lazyCodeDefView;
}

public async Task<bool> IsWindowOpenAsync(CancellationToken cancellationToken)
{
var vsCodeDefView = await GetVsCodeDefViewAsync().ConfigureAwait(true);

// Switch to the UI thread before using the IVsCodeDefView service
await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
Copy link
Member

@sharwell sharwell Oct 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 This method will be more efficient if this call is moved to the start of the method (removes two thread context switches that would occur if the caller is on a background thread and the window is not already initialized, and no penalty for the other cases).


// IsVisible returns S_FALSE if it's not visible
return vsCodeDefView.IsVisible() == VSConstants.S_OK;
}

public async Task SetContextAsync(ImmutableArray<CodeDefinitionWindowLocation> locations, CancellationToken cancellationToken)
{
// If the new context has no location, then just don't update, instead of showing the
Expand All @@ -40,7 +67,7 @@ public async Task SetContextAsync(ImmutableArray<CodeDefinitionWindowLocation> l
return;
}

var vsCodeDefView = await _asyncServiceProvider.GetServiceAsync<SVsCodeDefView, IVsCodeDefView>().ConfigureAwait(false);
var vsCodeDefView = await GetVsCodeDefViewAsync().ConfigureAwait(true);

// Switch to the UI thread before using the IVsCodeDefView service
await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This should also be moved up to before the call to GetServiceAsync (same reason)

Expand Down