Skip to content

Commit

Permalink
Generate-Documentation Check if file is excluded (#77353)
Browse files Browse the repository at this point in the history
* check for excluded files

* remove unused usings

* fix indentation
  • Loading branch information
akhera99 authored Feb 26, 2025
1 parent 410c349 commit 9e6dca8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Copilot;
Expand Down Expand Up @@ -51,16 +48,16 @@ public void TriggerDocumentationCommentProposalGeneration(Document document,

private async Task GenerateDocumentationCommentProposalsAsync(Document document, DocumentationCommentSnippet snippet, ITextSnapshot snapshot, VirtualSnapshotPoint caret, ITextView textView, CancellationToken cancellationToken)
{
var generateDocumentationCommentProvider = await CreateProviderAsync(document, textView, cancellationToken).ConfigureAwait(false);
var generateDocumentationCommentProvider = await CreateProviderAsync(document, textView, snippet.MemberNode, cancellationToken).ConfigureAwait(false);
if (generateDocumentationCommentProvider is not null)
{
await generateDocumentationCommentProvider.GenerateDocumentationProposalAsync(snippet, snapshot, caret, cancellationToken).ConfigureAwait(false);
}
}

private async Task<CopilotGenerateDocumentationCommentProvider?> CreateProviderAsync(Document document, ITextView textView, CancellationToken cancellationToken)
private async Task<CopilotGenerateDocumentationCommentProvider?> CreateProviderAsync(Document document, ITextView textView, SyntaxNode? memberNode, CancellationToken cancellationToken)
{
var copilotService = await IsGenerateDocumentationAvailableAsync(document, cancellationToken).ConfigureAwait(false);
var copilotService = await IsGenerateDocumentationAvailableAsync(document, memberNode, cancellationToken).ConfigureAwait(false);

if (copilotService is null)
{
Expand All @@ -75,7 +72,7 @@ private async Task GenerateDocumentationCommentProposalsAsync(Document document,
return provider;
}

private static async Task<ICopilotCodeAnalysisService?> IsGenerateDocumentationAvailableAsync(Document document, CancellationToken cancellationToken)
private static async Task<ICopilotCodeAnalysisService?> IsGenerateDocumentationAvailableAsync(Document document, SyntaxNode? memberNode, CancellationToken cancellationToken)
{
// Bailing out if copilot is not available or the option is not enabled.
if (document.GetLanguageService<ICopilotOptionsService>() is not { } copilotOptionService ||
Expand All @@ -90,6 +87,17 @@ await copilotService.IsAvailableAsync(cancellationToken).ConfigureAwait(false) i
return null;
}

if (memberNode is null)
{
return null;
}

// Check to see if the file containing the member being documented has been excluded.
if (await copilotService.IsFileExcludedAsync(memberNode.SyntaxTree.FilePath, cancellationToken).ConfigureAwait(false))
{
return null;
}

return copilotService;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task InitializeAsync(ITextView textView, SuggestionServiceBase sugg
}

public async Task GenerateDocumentationProposalAsync(DocumentationCommentSnippet snippet,
ITextSnapshot oldSnapshot, VirtualSnapshotPoint oldCaret, CancellationToken cancellationToken)
ITextSnapshot oldSnapshot, VirtualSnapshotPoint oldCaret, CancellationToken cancellationToken)
{
await Task.Yield().ConfigureAwait(false);

Expand All @@ -52,7 +52,8 @@ public async Task GenerateDocumentationProposalAsync(DocumentationCommentSnippet
return;
}

var snippetProposal = GetSnippetProposal(snippet.SnippetText, snippet.MemberNode, snippet.Position, snippet.CaretOffset);
// MemberNode is not null at this point, checked when determining if the file is exluded.
var snippetProposal = GetSnippetProposal(snippet.SnippetText, snippet.MemberNode!, snippet.Position, snippet.CaretOffset);

if (snippetProposal is null)
{
Expand Down Expand Up @@ -80,13 +81,8 @@ public async Task GenerateDocumentationProposalAsync(DocumentationCommentSnippet
/// <summary>
/// Traverses the documentation comment shell and retrieves the pieces that are needed to generate the documentation comment.
/// </summary>
private static DocumentationCommentProposal? GetSnippetProposal(string comments, SyntaxNode? memberNode, int? position, int caret)
private static DocumentationCommentProposal? GetSnippetProposal(string comments, SyntaxNode memberNode, int? position, int caret)
{
if (memberNode is null)
{
return null;
}

if (position is null)
{
return null;
Expand Down

0 comments on commit 9e6dca8

Please sign in to comment.