|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.ComponentModel.Composition; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Microsoft.CodeAnalysis.Copilot; |
| 13 | +using Microsoft.CodeAnalysis.Editor.Shared.Utilities; |
| 14 | +using Microsoft.CodeAnalysis.Host.Mef; |
| 15 | +using Microsoft.CodeAnalysis.Shared.Extensions; |
| 16 | +using Microsoft.CodeAnalysis.Shared.TestHooks; |
| 17 | +using Microsoft.VisualStudio.Language.Suggestions; |
| 18 | +using Microsoft.VisualStudio.Text; |
| 19 | +using Microsoft.VisualStudio.Text.Editor; |
| 20 | + |
| 21 | +namespace Microsoft.CodeAnalysis.DocumentationComments |
| 22 | +{ |
| 23 | + [Export(typeof(CopilotGenerateDocumentationCommentManager))] |
| 24 | + internal class CopilotGenerateDocumentationCommentManager |
| 25 | + { |
| 26 | + private readonly SuggestionServiceBase? _suggestionServiceBase; |
| 27 | + private readonly IThreadingContext _threadingContext; |
| 28 | + private readonly IAsynchronousOperationListener _asyncListener; |
| 29 | + |
| 30 | + [ImportingConstructor] |
| 31 | + [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] |
| 32 | + public CopilotGenerateDocumentationCommentManager([Import(AllowDefault = true)] SuggestionServiceBase? suggestionServiceBase, IThreadingContext threadingContext, |
| 33 | + IAsynchronousOperationListenerProvider listenerProvider) |
| 34 | + { |
| 35 | + _suggestionServiceBase = suggestionServiceBase; |
| 36 | + _threadingContext = threadingContext; |
| 37 | + _asyncListener = listenerProvider.GetListener(FeatureAttribute.GenerateDocumentation); |
| 38 | + } |
| 39 | + |
| 40 | + public void TriggerDocumentationCommentProposalGeneration(Document document, |
| 41 | + DocumentationCommentSnippet snippet, ITextSnapshot snapshot, VirtualSnapshotPoint caret, ITextView textView, CancellationToken cancellationToken) |
| 42 | + { |
| 43 | + if (_suggestionServiceBase is null) |
| 44 | + { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + var token = _asyncListener.BeginAsyncOperation(nameof(GenerateDocumentationCommentProposalsAsync)); |
| 49 | + _ = GenerateDocumentationCommentProposalsAsync(document, snippet, snapshot, caret, textView, cancellationToken).CompletesAsyncOperation(token); |
| 50 | + } |
| 51 | + |
| 52 | + private async Task GenerateDocumentationCommentProposalsAsync(Document document, DocumentationCommentSnippet snippet, ITextSnapshot snapshot, VirtualSnapshotPoint caret, ITextView textView, CancellationToken cancellationToken) |
| 53 | + { |
| 54 | + var generateDocumentationCommentProvider = await CreateProviderAsync(document, textView, cancellationToken).ConfigureAwait(false); |
| 55 | + if (generateDocumentationCommentProvider is not null) |
| 56 | + { |
| 57 | + await generateDocumentationCommentProvider.GenerateDocumentationProposalAsync(snippet, snapshot, caret, cancellationToken).ConfigureAwait(false); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private async Task<CopilotGenerateDocumentationCommentProvider?> CreateProviderAsync(Document document, ITextView textView, CancellationToken cancellationToken) |
| 62 | + { |
| 63 | + var copilotService = await IsGenerateDocumentationAvailableAsync(document, cancellationToken).ConfigureAwait(false); |
| 64 | + |
| 65 | + if (copilotService is null) |
| 66 | + { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + var provider = textView.Properties.GetOrCreateSingletonProperty(typeof(CopilotGenerateDocumentationCommentProvider), |
| 71 | + () => new CopilotGenerateDocumentationCommentProvider(_threadingContext, copilotService)); |
| 72 | + |
| 73 | + await provider!.InitializeAsync(textView, _suggestionServiceBase!, cancellationToken).ConfigureAwait(false); |
| 74 | + |
| 75 | + return provider; |
| 76 | + } |
| 77 | + |
| 78 | + private static async Task<ICopilotCodeAnalysisService?> IsGenerateDocumentationAvailableAsync(Document document, CancellationToken cancellationToken) |
| 79 | + { |
| 80 | + // Bailing out if copilot is not available or the option is not enabled. |
| 81 | + if (document.GetLanguageService<ICopilotOptionsService>() is not { } copilotOptionService || |
| 82 | + !await copilotOptionService.IsGenerateDocumentationCommentOptionEnabledAsync().ConfigureAwait(false)) |
| 83 | + { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + if (document.GetLanguageService<ICopilotCodeAnalysisService>() is not { } copilotService || |
| 88 | + await copilotService.IsAvailableAsync(cancellationToken).ConfigureAwait(false) is false) |
| 89 | + { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + return copilotService; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments