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

Updates content exclusion for on-the-fly-docs #75172

Merged
merged 10 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -137,6 +137,15 @@ private async Task SetResultTextAsync(ICopilotCodeAnalysisService copilotService

try
{
if (_onTheFlyDocsElement.IsContentExcluded)
{
SetResultText(EditorFeaturesResources.Your_organization_excluded_a_referenced_file_from_Copilot);
CurrentState = OnTheFlyDocsState.Finished;
Logger.Log(FunctionId.Copilot_On_The_Fly_Docs_Content_Excluded, logLevel: LogLevel.Information);
return;

}

var response = await copilotService.GetOnTheFlyDocsAsync(_onTheFlyDocsElement.SymbolSignature, _onTheFlyDocsElement.DeclarationCode, _onTheFlyDocsElement.Language, cancellationToken).ConfigureAwait(false);
var copilotRequestTime = stopwatch.Elapsed;

Expand Down Expand Up @@ -202,7 +211,6 @@ public void RequestResults()
CurrentState = OnTheFlyDocsState.Loading;
Logger.Log(FunctionId.Copilot_On_The_Fly_Docs_Loading_State_Entered, KeyValueLogMessage.Create(m =>
{
m["SymbolHeaderText"] = _onTheFlyDocsElement.SymbolSignature;
m["HasDocumentationComments"] = _onTheFlyDocsElement.HasComments;
}, LogLevel.Information));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public OnTheFlyDocsViewFactory(IViewElementFactoryService factoryService, IAsync

var editorFeaturesOnTheFlyDocsElement = (EditorFeaturesOnTheFlyDocsElement)model;

Logger.Log(FunctionId.Copilot_On_The_Fly_Docs_Showed_Link, KeyValueLogMessage.Create(m =>
{
m["SymbolHeaderText"] = editorFeaturesOnTheFlyDocsElement.OnTheFlyDocsElement.SymbolSignature;
}, LogLevel.Information));
Logger.Log(FunctionId.Copilot_On_The_Fly_Docs_Showed_Link, logLevel: LogLevel.Information);

var quickInfoSession = _asyncQuickInfoBroker.GetSession(textView);

Expand Down
3 changes: 3 additions & 0 deletions src/EditorFeatures/Core/EditorFeaturesResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -953,4 +953,7 @@ Do you want to proceed?</value>
<data name="An_error_occurred_while_generating_documentation_for_this_code" xml:space="preserve">
<value>An error occurred while generating documentation for this code.</value>
</data>
<data name="Your_organization_excluded_a_referenced_file_from_Copilot" xml:space="preserve">
<value>Your organization excluded a referenced file from Copilot.</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/EditorFeatures/Core/xlf/EditorFeaturesResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ internal interface IExternalCSharpCopilotCodeAnalysisService
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);
Task<bool> IsFileExcludedAsync(string filePath, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ internal abstract class AbstractCopilotCodeAnalysisService(IDiagnosticsRefresher
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);
protected abstract Task<bool> IsFileExcludedCoreAsync(string filePath, CancellationToken cancellationToken);

public Task<bool> IsAvailableAsync(CancellationToken cancellationToken)
=> IsAvailableCoreAsync(cancellationToken);
Expand Down Expand Up @@ -187,4 +188,12 @@ public async Task<bool> IsAnyExclusionAsync(CancellationToken cancellationToken)

return await IsAnyExclusionCoreAsync(cancellationToken).ConfigureAwait(false);
}

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

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

internal sealed partial class CSharpCopilotCodeAnalysisService
{
Expand All @@ -37,6 +38,7 @@ private sealed class ReflectionWrapper : IExternalCSharpCopilotCodeAnalysisServi
private const string StartRefinementSessionAsyncMethodName = "StartRefinementSessionAsync";
private const string GetOnTheFlyDocsAsyncMethodName = "GetOnTheFlyDocsAsync";
private const string IsAnyExclusionAsyncMethodName = "IsAnyExclusionAsync";
private const string IsFileExcludedAsyncMethodName = "IsFileExcludedAsync";

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

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

private T? CreateDelegate<T>(string methodName, Type[] types) where T : Delegate
Expand Down Expand Up @@ -118,6 +122,9 @@ public ReflectionWrapper(IServiceProvider serviceProvider, IVsService<SVsBrokere
private IsAnyExclusionAsyncDelegateType? CreateIsAnyExclusionAsyncDelegate()
=> CreateDelegate<IsAnyExclusionAsyncDelegateType>(IsAnyExclusionAsyncMethodName, [typeof(CancellationToken)]);

private IsFileExcludedAsyncDelegateType? CreateIsFileExcludedAsyncDelegate()
=> CreateDelegate<IsFileExcludedAsyncDelegateType>(IsFileExcludedAsyncMethodName, [typeof(string), typeof(CancellationToken)]);

public async Task<bool> IsAvailableAsync(CancellationToken cancellationToken)
{
if (_lazyIsAvailableAsyncDelegate.Value is null)
Expand Down Expand Up @@ -173,5 +180,13 @@ public async Task<bool> IsAnyExclusionAsync(CancellationToken cancellationToken)

return await _lazyIsAnyExclusionAsyncDelegate.Value(cancellationToken).ConfigureAwait(false);
}

public async Task<bool> IsFileExcludedAsync(string filePath, CancellationToken cancellationToken)
{
if (_lazyIsFileExcludedAsyncDelegate.Value is null)
return false;

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

protected override Task<bool> IsAnyExclusionCoreAsync(CancellationToken cancellationToken)
=> _lazyExternalCopilotService.Value.IsAnyExclusionAsync(cancellationToken);

protected override Task<bool> IsFileExcludedCoreAsync(string filePath, CancellationToken cancellationToken)
=> _lazyExternalCopilotService.Value.IsFileExcludedAsync(filePath, cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Microsoft.CodeAnalysis.ExternalAccess.Copilot.IExternalCSharpCopilotCodeAnalysis
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.IsFileExcludedAsync(string! filePath, System.Threading.CancellationToken cancellationToken) -> 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!
Microsoft.CodeAnalysis.ExternalAccess.Copilot.RelatedDocuments.ICopilotRelatedDocumentsService
Microsoft.CodeAnalysis.ExternalAccess.Copilot.RelatedDocuments.ICopilotRelatedDocumentsService.GetRelatedDocumentIdsAsync(Microsoft.CodeAnalysis.Document! document, int position, System.Func<System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.DocumentId!>, System.Threading.CancellationToken, System.Threading.Tasks.ValueTask>! callbackAsync, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
Expand Down
4 changes: 4 additions & 0 deletions src/EditorFeatures/Test2/CodeFixes/CodeFixServiceTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.CodeFixes.UnitTests
Public Function IsAnyExclusionAsync(cancellationToken As CancellationToken) As Task(Of Boolean) Implements ICopilotCodeAnalysisService.IsAnyExclusionAsync
Return Task.FromResult(False)
End Function

Public Function IsFileExcludedAsync(filePath As String, cancellationToken As CancellationToken) As Task(Of Boolean) Implements ICopilotCodeAnalysisService.IsFileExcludedAsync
Return Task.FromResult(False)
End Function
End Class
End Class
End Namespace
Loading
Loading