From 612ae1efc1a04a2f0e8bf97c8cfa6333f606df35 Mon Sep 17 00:00:00 2001 From: Gen Lu Date: Mon, 22 Apr 2019 17:40:51 -0700 Subject: [PATCH] Using FQN instead of adding import during ENC session --- .../AbstractTypeImportCompletionProvider.cs | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Features/Core/Portable/Completion/Providers/AbstractTypeImportCompletionProvider.cs b/src/Features/Core/Portable/Completion/Providers/AbstractTypeImportCompletionProvider.cs index fce94d6b2956d..8727c0ce67c0e 100644 --- a/src/Features/Core/Portable/Completion/Providers/AbstractTypeImportCompletionProvider.cs +++ b/src/Features/Core/Portable/Completion/Providers/AbstractTypeImportCompletionProvider.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.AddImports; +using Microsoft.CodeAnalysis.Debugging; using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Experiments; using Microsoft.CodeAnalysis.Formatting; @@ -174,7 +175,14 @@ internal override async Task GetChangeAsync(Document document, var containingNamespace = TypeImportCompletionItem.GetContainingNamespace(completionItem); Debug.Assert(containingNamespace != null); - if (document.Project.Solution.Workspace.CanApplyChange(ApplyChangesKind.ChangeDocument)) + if (ShouldCompleteWithFullyQualifyTypeName(document)) + { + var fullyQualifiedName = $"{containingNamespace}.{completionItem.DisplayText}"; + var change = new TextChange(completionListSpan, fullyQualifiedName); + + return CompletionChange.Create(change); + } + else { // Find context node so we can use it to decide where to insert using/imports. var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); @@ -218,14 +226,25 @@ internal override async Task GetChangeAsync(Document document, return CompletionChange.Create(Utilities.Collapse(newText, builder.ToImmutableAndFree())); } - else + + static bool ShouldCompleteWithFullyQualifyTypeName(Document document) { - // For workspace that doesn't support document change, e.g. DebuggerIntellisense - // we complete the type name in its fully qualified form instead. - var fullyQualifiedName = $"{containingNamespace}.{completionItem.DisplayText}"; - var change = new TextChange(completionListSpan, fullyQualifiedName); + var workspace = document.Project.Solution.Workspace; - return CompletionChange.Create(change); + // Certain types of workspace don't support document change, e.g. DebuggerIntellisense + if (!workspace.CanApplyChange(ApplyChangesKind.ChangeDocument)) + { + return true; + } + + // During an EnC session, adding import is not supported. + var encService = workspace.Services.GetService()?.EditAndContinueServiceOpt; + if (encService?.EditSession != null) + { + return true; + } + + return false; } }