Skip to content

Commit

Permalink
On the fly docs exclusion (#73770)
Browse files Browse the repository at this point in the history
* remove usings

* wip

* remove usings

* move

* added comment
  • Loading branch information
akhera99 authored May 29, 2024
1 parent e5dc6c6 commit cdf451b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/EditorFeatures/Test2/CodeFixes/CodeFixServiceTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.CodeFixes.UnitTests
Public Function GetOnTheFlyDocsAsync(symbolSignature As String, declarationCode As ImmutableArray(Of String), language As String, cancellationToken As CancellationToken) As Task(Of String) Implements ICopilotCodeAnalysisService.GetOnTheFlyDocsAsync
Return Task.FromResult("")
End Function

Public Function IsAnyExclusionAsync(cancellationToken As CancellationToken) As Task(Of Boolean) Implements ICopilotCodeAnalysisService.IsAnyExclusionAsync
Return Task.FromResult(False)
End Function
End Class
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -145,6 +146,13 @@ protected override NullableFlowState GetNullabilityAnalysis(SemanticModel semant
return null;
}

// Checks to see if there have been any files excluded at the workspace level
// since the copilot service passes along symbol information.
if (await copilotService.IsAnyExclusionAsync(cancellationToken).ConfigureAwait(false))
{
return null;
}

if (document.GetLanguageService<ICopilotOptionsService>() is not { } service ||
!await service.IsOnTheFlyDocsOptionEnabledAsync().ConfigureAwait(false))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@ internal interface ICopilotCodeAnalysisService : ILanguageService
/// </para>
/// </summary>
Task<string> GetOnTheFlyDocsAsync(string symbolSignature, ImmutableArray<string> declarationCode, string language, CancellationToken cancellationToken);

/// <summary>
/// Determines if there are any exclusions in the workspace.
/// </summary>
Task<bool> IsAnyExclusionAsync(CancellationToken cancellationToken);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ internal interface IExternalCSharpCopilotCodeAnalysisService
Task<ImmutableArray<Diagnostic>> GetCachedDiagnosticsAsync(Document document, string promptTitle, CancellationToken cancellationToken);
Task StartRefinementSessionAsync(Document oldDocument, Document newDocument, Diagnostic? primaryDiagnostic, CancellationToken cancellationToken);
Task<string> GetOnTheFlyDocsAsync(string symbolSignature, ImmutableArray<string> declarationCode, string language, CancellationToken cancellationToken);
Task<bool> IsAnyExclusionAsync(CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ internal abstract class AbstractCopilotCodeAnalysisService(IDiagnosticsRefresher
protected abstract Task<ImmutableArray<Diagnostic>> GetCachedDiagnosticsCoreAsync(Document document, string promptTitle, CancellationToken cancellationToken);
protected abstract Task StartRefinementSessionCoreAsync(Document oldDocument, Document newDocument, Diagnostic? primaryDiagnostic, CancellationToken cancellationToken);
protected abstract Task<string> GetOnTheFlyDocsCoreAsync(string symbolSignature, ImmutableArray<string> declarationCode, string language, CancellationToken cancellationToken);
protected abstract Task<bool> IsAnyExclusionCoreAsync(CancellationToken cancellationToken);

public Task<bool> IsAvailableAsync(CancellationToken cancellationToken)
=> IsAvailableCoreAsync(cancellationToken);
Expand Down Expand Up @@ -178,4 +179,12 @@ public async Task<string> GetOnTheFlyDocsAsync(string symbolSignature, Immutable

return await GetOnTheFlyDocsCoreAsync(symbolSignature, declarationCode, language, cancellationToken).ConfigureAwait(false);
}

public async Task<bool> IsAnyExclusionAsync(CancellationToken cancellationToken)
{
if (!await IsAvailableAsync(cancellationToken).ConfigureAwait(false))
return false;

return await IsAnyExclusionCoreAsync(cancellationToken).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.Copilot.Internal.Analyzer.CSharp
using IsAvailableAsyncDelegateType = Func<CancellationToken, Task<bool>>;
using StartRefinementSessionAsyncDelegateType = Func<Document, Document, Diagnostic?, CancellationToken, Task>;
using GetOnTheFlyDocsAsyncDelegateType = Func<string, ImmutableArray<string>, string, CancellationToken, Task<string>>;
using IsAnyExclusionAsyncDelegateType = Func<CancellationToken, Task<bool>>;

internal sealed partial class CSharpCopilotCodeAnalysisService
{
Expand All @@ -35,6 +36,7 @@ private sealed class ReflectionWrapper : IExternalCSharpCopilotCodeAnalysisServi
private const string GetCachedDiagnosticsAsyncMethodName = "GetCachedDiagnosticsAsync";
private const string StartRefinementSessionAsyncMethodName = "StartRefinementSessionAsync";
private const string GetOnTheFlyDocsAsyncMethodName = "GetOnTheFlyDocsAsync";
private const string IsAnyExclusionAsyncMethodName = "IsAnyExclusionAsync";

// Create and cache closed delegate to ensure we use a singleton object and with better performance.
private readonly Type? _analyzerType;
Expand All @@ -45,6 +47,7 @@ private sealed class ReflectionWrapper : IExternalCSharpCopilotCodeAnalysisServi
private readonly Lazy<GetCachedDiagnosticsAsyncDelegateType?> _lazyGetCachedDiagnosticsAsyncDelegate;
private readonly Lazy<StartRefinementSessionAsyncDelegateType?> _lazyStartRefinementSessionAsyncDelegate;
private readonly Lazy<GetOnTheFlyDocsAsyncDelegateType?> _lazyGetOnTheFlyDocsAsyncDelegate;
private readonly Lazy<IsAnyExclusionAsyncDelegateType?> _lazyIsAnyExclusionAsyncDelegate;

public ReflectionWrapper(IServiceProvider serviceProvider, IVsService<SVsBrokeredServiceContainer, IBrokeredServiceContainer> brokeredServiceContainer)
{
Expand Down Expand Up @@ -73,6 +76,7 @@ public ReflectionWrapper(IServiceProvider serviceProvider, IVsService<SVsBrokere
_lazyGetCachedDiagnosticsAsyncDelegate = new(CreateGetCachedDiagnosticsAsyncDelegate, LazyThreadSafetyMode.PublicationOnly);
_lazyStartRefinementSessionAsyncDelegate = new(CreateStartRefinementSessionAsyncDelegate, LazyThreadSafetyMode.PublicationOnly);
_lazyGetOnTheFlyDocsAsyncDelegate = new(CreateGetOnTheFlyDocsAsyncDelegate, LazyThreadSafetyMode.PublicationOnly);
_lazyIsAnyExclusionAsyncDelegate = new(CreateIsAnyExclusionAsyncDelegate, LazyThreadSafetyMode.PublicationOnly);
}

private T? CreateDelegate<T>(string methodName, Type[] types) where T : Delegate
Expand Down Expand Up @@ -111,6 +115,9 @@ public ReflectionWrapper(IServiceProvider serviceProvider, IVsService<SVsBrokere
private GetOnTheFlyDocsAsyncDelegateType? CreateGetOnTheFlyDocsAsyncDelegate()
=> CreateDelegate<GetOnTheFlyDocsAsyncDelegateType>(GetOnTheFlyDocsAsyncMethodName, [typeof(string), typeof(ImmutableArray<string>), typeof(string), typeof(CancellationToken)]);

private IsAnyExclusionAsyncDelegateType? CreateIsAnyExclusionAsyncDelegate()
=> CreateDelegate<IsAnyExclusionAsyncDelegateType>(IsAnyExclusionAsyncMethodName, [typeof(CancellationToken)]);

public async Task<bool> IsAvailableAsync(CancellationToken cancellationToken)
{
if (_lazyIsAvailableAsyncDelegate.Value is null)
Expand Down Expand Up @@ -158,5 +165,13 @@ public async Task<string> GetOnTheFlyDocsAsync(string symbolSignature, Immutable

return await _lazyGetOnTheFlyDocsAsyncDelegate.Value(symbolSignature, declarationCode, language, cancellationToken).ConfigureAwait(false);
}

public async Task<bool> IsAnyExclusionAsync(CancellationToken cancellationToken)
{
if (_lazyIsAnyExclusionAsyncDelegate.Value is null)
return false;

return await _lazyIsAnyExclusionAsyncDelegate.Value(cancellationToken).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ protected override async Task<ImmutableArray<Diagnostic>> GetDiagnosticsIntersec

return filteredDiagnostics.ToImmutable();
}

protected override Task<bool> IsAnyExclusionCoreAsync(CancellationToken cancellationToken)
=> _lazyExternalCopilotService.Value.IsAnyExclusionAsync(cancellationToken);
}
1 change: 1 addition & 0 deletions src/Tools/ExternalAccess/Copilot/InternalAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Microsoft.CodeAnalysis.ExternalAccess.Copilot.IExternalCSharpCopilotCodeAnalysis
Microsoft.CodeAnalysis.ExternalAccess.Copilot.IExternalCSharpCopilotCodeAnalysisService.GetAvailablePromptTitlesAsync(Microsoft.CodeAnalysis.Document! document, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<System.Collections.Immutable.ImmutableArray<string!>>!
Microsoft.CodeAnalysis.ExternalAccess.Copilot.IExternalCSharpCopilotCodeAnalysisService.GetCachedDiagnosticsAsync(Microsoft.CodeAnalysis.Document! document, string! promptTitle, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostic!>>!
Microsoft.CodeAnalysis.ExternalAccess.Copilot.IExternalCSharpCopilotCodeAnalysisService.GetOnTheFlyDocsAsync(string! symbolSignature, System.Collections.Immutable.ImmutableArray<string!> declarationCode, string! language, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<string!>!
Microsoft.CodeAnalysis.ExternalAccess.Copilot.IExternalCSharpCopilotCodeAnalysisService.IsAnyExclusionAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<bool>!
Microsoft.CodeAnalysis.ExternalAccess.Copilot.IExternalCSharpCopilotCodeAnalysisService.IsAvailableAsync(System.Threading.CancellationToken cancellation) -> System.Threading.Tasks.Task<bool>!
Microsoft.CodeAnalysis.ExternalAccess.Copilot.IExternalCSharpCopilotCodeAnalysisService.StartRefinementSessionAsync(Microsoft.CodeAnalysis.Document! oldDocument, Microsoft.CodeAnalysis.Document! newDocument, Microsoft.CodeAnalysis.Diagnostic? primaryDiagnostic, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
override Microsoft.CodeAnalysis.ExternalAccess.Copilot.CopilotChecksumWrapper.Equals(object? obj) -> bool
Expand Down

0 comments on commit cdf451b

Please sign in to comment.