From d3517d19cc8fa1e469eed08f9cae811d42ebfb84 Mon Sep 17 00:00:00 2001 From: Dustin Campbell Date: Mon, 14 Sep 2015 15:41:25 -0700 Subject: [PATCH] Fix C# "speculative T" completion to appear properly in linked document scenarios, fixing failing unit test Additionally, this change audits calls to GetUnionItemsFromDocumentAndLinkedDocumentsAsync to avoid redundant awaits. --- .../SnippetCompletionProvider.cs | 4 +-- .../SpeculativeTCompletionProvider.cs | 8 ++++-- .../AbstractKeywordCompletionProvider.cs | 4 +-- .../Shared/Extensions/DocumentExtensions.cs | 25 ++++++++++++++++++- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Features/CSharp/Portable/Completion/CompletionProviders/SnippetCompletionProvider.cs b/src/Features/CSharp/Portable/Completion/CompletionProviders/SnippetCompletionProvider.cs index 75d5a67e1ac..0e4c2101037 100644 --- a/src/Features/CSharp/Portable/Completion/CompletionProviders/SnippetCompletionProvider.cs +++ b/src/Features/CSharp/Portable/Completion/CompletionProviders/SnippetCompletionProvider.cs @@ -62,9 +62,9 @@ public override async Task ProduceCompletionListAsync(CompletionListContext cont return; } - var snippetCompletionItems = await document.GetUnionResultsFromDocumentAndLinks( + var snippetCompletionItems = await document.GetUnionItemsFromDocumentAndLinkedDocumentsAsync( UnionCompletionItemComparer.Instance, - async (d, c) => await GetSnippetsForDocumentAsync(d, position, workspace, c).ConfigureAwait(false), + (d, c) => GetSnippetsForDocumentAsync(d, position, workspace, c), cancellationToken).ConfigureAwait(false); context.AddItems(snippetCompletionItems); diff --git a/src/Features/CSharp/Portable/Completion/CompletionProviders/SpeculativeTCompletionProvider.cs b/src/Features/CSharp/Portable/Completion/CompletionProviders/SpeculativeTCompletionProvider.cs index ea62c54c508..8e8da1b0b17 100644 --- a/src/Features/CSharp/Portable/Completion/CompletionProviders/SpeculativeTCompletionProvider.cs +++ b/src/Features/CSharp/Portable/Completion/CompletionProviders/SpeculativeTCompletionProvider.cs @@ -31,7 +31,11 @@ public override async Task ProduceCompletionListAsync(CompletionListContext cont var position = context.Position; var cancellationToken = context.CancellationToken; - if (await ShouldShowSpeculativeTCompletionItem(document, position, cancellationToken).ConfigureAwait(false)) + var showSpeculativeT = await document.IsValidContextForDocumentOrLinkedDocumentsAsync( + (doc, ct) => ShouldShowSpeculativeTCompletionItemAsync(doc, position, ct), + cancellationToken).ConfigureAwait(false); + + if (showSpeculativeT) { var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); var filterSpan = this.GetTextChangeSpan(text, position); @@ -41,7 +45,7 @@ public override async Task ProduceCompletionListAsync(CompletionListContext cont } } - private async Task ShouldShowSpeculativeTCompletionItem(Document document, int position, CancellationToken cancellationToken) + private async Task ShouldShowSpeculativeTCompletionItemAsync(Document document, int position, CancellationToken cancellationToken) { var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); if (syntaxTree.IsInNonUserCode(position, cancellationToken) || diff --git a/src/Features/Core/Portable/Completion/Providers/AbstractKeywordCompletionProvider.cs b/src/Features/Core/Portable/Completion/Providers/AbstractKeywordCompletionProvider.cs index e4e005b57ea..277ea3c771e 100644 --- a/src/Features/Core/Portable/Completion/Providers/AbstractKeywordCompletionProvider.cs +++ b/src/Features/Core/Portable/Completion/Providers/AbstractKeywordCompletionProvider.cs @@ -54,9 +54,9 @@ public override async Task ProduceCompletionListAsync(CompletionListContext cont using (Logger.LogBlock(FunctionId.Completion_KeywordCompletionProvider_GetItemsWorker, cancellationToken)) { - var keywords = await document.GetUnionResultsFromDocumentAndLinks( + var keywords = await document.GetUnionItemsFromDocumentAndLinkedDocumentsAsync( s_comparer, - async (doc, ct) => await RecommendKeywordsAsync(doc, position, ct).ConfigureAwait(false), + (doc, ct) => RecommendKeywordsAsync(doc, position, ct), cancellationToken).ConfigureAwait(false); var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); diff --git a/src/Features/Core/Portable/Shared/Extensions/DocumentExtensions.cs b/src/Features/Core/Portable/Shared/Extensions/DocumentExtensions.cs index 3051d1bfb75..10b17574d3d 100644 --- a/src/Features/Core/Portable/Shared/Extensions/DocumentExtensions.cs +++ b/src/Features/Core/Portable/Shared/Extensions/DocumentExtensions.cs @@ -35,7 +35,7 @@ public static bool ShouldHideAdvancedMembers(this Document document) return document.WithSyntaxRoot(newRoot); } - public static async Task> GetUnionResultsFromDocumentAndLinks( + public static async Task> GetUnionItemsFromDocumentAndLinkedDocumentsAsync( this Document document, IEqualityComparer comparer, Func>> getItemsWorker, @@ -61,5 +61,28 @@ public static bool ShouldHideAdvancedMembers(this Document document) return totalItems; } + + public static async Task IsValidContextForDocumentOrLinkedDocumentsAsync( + this Document document, + Func> contextChecker, + CancellationToken cancellationToken) + { + if (await contextChecker(document, cancellationToken).ConfigureAwait(false)) + { + return true; + } + + var solution = document.Project.Solution; + foreach (var linkedDocumentId in document.GetLinkedDocumentIds()) + { + var linkedDocument = solution.GetDocument(linkedDocumentId); + if (await contextChecker(linkedDocument, cancellationToken).ConfigureAwait(false)) + { + return true; + } + } + + return false; + } } } -- GitLab