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

Fix razor completion #35225

Merged
merged 8 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -273,7 +273,7 @@ public void ChangeSignatureCommandDisabledInSubmission()
{
var exportProvider = ExportProviderCache
.GetOrCreateExportProviderFactory(
TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(typeof(InteractiveTextBufferSupportsFeatureService)))
TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(typeof(InteractiveSupportsFeatureService.InteractiveTextBufferSupportsFeatureService)))
.CreateExportProvider();

using (var workspace = TestWorkspace.Create(XElement.Parse(@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void EncapsulateFieldCommandDisabledInSubmission()
{
var exportProvider = ExportProviderCache
.GetOrCreateExportProviderFactory(
TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(typeof(InteractiveTextBufferSupportsFeatureService)))
TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(typeof(InteractiveSupportsFeatureService.InteractiveTextBufferSupportsFeatureService)))
.CreateExportProvider();

using (var workspace = TestWorkspace.Create(XElement.Parse(@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ public void M(T a, U b) { }
public void ExtractInterfaceCommandDisabledInSubmission()
{
var exportProvider = ExportProviderCache
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(typeof(InteractiveTextBufferSupportsFeatureService)))
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(typeof(InteractiveSupportsFeatureService.InteractiveTextBufferSupportsFeatureService)))
.CreateExportProvider();

using (var workspace = TestWorkspace.Create(XElement.Parse(@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10353,7 +10353,7 @@ public async Task ExtractMethod_Argument2()
public void ExtractMethodCommandDisabledInSubmission()
{
var exportProvider = ExportProviderCache
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(typeof(InteractiveTextBufferSupportsFeatureService)))
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(typeof(InteractiveSupportsFeatureService.InteractiveTextBufferSupportsFeatureService)))
.CreateExportProvider();

using (var workspace = TestWorkspace.Create(XElement.Parse(@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ void B()
public void OrganizingCommandsDisabledInSubmission()
{
var exportProvider = ExportProviderCache
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(typeof(InteractiveTextBufferSupportsFeatureService)))
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(typeof(InteractiveSupportsFeatureService.InteractiveTextBufferSupportsFeatureService)))
.CreateExportProvider();

using (var workspace = TestWorkspace.Create(XElement.Parse(@"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Composition;
using Microsoft.CodeAnalysis.Editor.Shared;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.InteractiveWindow;
using Microsoft.CodeAnalysis.Shared;

namespace Microsoft.CodeAnalysis.Editor.Implementation.Interactive
{
internal sealed class InteractiveSupportsFeatureService
{
[ExportWorkspaceService(typeof(ITextBufferSupportsFeatureService), WorkspaceKind.Interactive), Shared]
internal class InteractiveTextBufferSupportsFeatureService : ITextBufferSupportsFeatureService
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can have 1 type with 2 interfaces with 2 ExportWorkspaceServices?

you can even share instance by exporting ExportWorkspaceServiceFactory with same service instance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you can't. Each export is a nested type itself.

{
public bool SupportsCodeFixes(ITextBuffer textBuffer)
{
if (textBuffer != null)
{
var evaluator = (IInteractiveEvaluator)textBuffer.Properties[typeof(IInteractiveEvaluator)];
var window = evaluator?.CurrentWindow;
if (window?.CurrentLanguageBuffer == textBuffer)
{
// These are only correct if we're on the UI thread.
// Otherwise, they're guesses and they might change immediately even if they're correct.
// If we return true and the buffer later becomes readonly, it appears that the
// the code fix simply has no effect.
return !window.IsResetting && !window.IsRunning;
}
}

return false;
}

public bool SupportsRefactorings(ITextBuffer textBuffer)
{
return false;
}

public bool SupportsRename(ITextBuffer textBuffer)
{
return false;
}

public bool SupportsNavigationToAnyPosition(ITextBuffer textBuffer)
{
return true;
}
}

[ExportWorkspaceService(typeof(IDocumentSupportsFeatureService), WorkspaceKind.Interactive), Shared]
internal class InteractiveDocumentSupportsFeatureService : IDocumentSupportsFeatureService
{
public bool SupportsCodeFixes(Document document)
{
// TODO: Implement this.
return false;
}

public bool SupportsRefactorings(Document document)
{
return false;
}

public bool SupportsRename(Document document)
{
return false;
}

public bool SupportsNavigationToAnyPosition(Document document)
{
return true;
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Shared;
using Microsoft.VisualStudio.Text;

namespace Microsoft.CodeAnalysis.Editor.Shared
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Rename
<Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub RenameCommandDisabledInSubmission()
Dim exportProvider = ExportProviderCache _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveTextBufferSupportsFeatureService))) _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveSupportsFeatureService.InteractiveTextBufferSupportsFeatureService))) _
.CreateExportProvider()

Using workspace = TestWorkspace.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ End Module
<Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub TestChangeSignatureCommandDisabledInSubmission()
Dim exportProvider = ExportProviderCache _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveTextBufferSupportsFeatureService))) _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveSupportsFeatureService.InteractiveTextBufferSupportsFeatureService))) _
.CreateExportProvider()

Using workspace = TestWorkspace.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ End Class
<Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub EncapsulateFieldCommandDisabledInSubmission()
Dim exportProvider = ExportProviderCache _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveTextBufferSupportsFeatureService))) _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveSupportsFeatureService.InteractiveTextBufferSupportsFeatureService))) _
.CreateExportProvider()

Using workspace = TestWorkspace.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ End Namespace
<Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub TestExtractInterfaceCommandDisabledInSubmission()
Dim exportProvider = ExportProviderCache _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveTextBufferSupportsFeatureService))) _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveSupportsFeatureService.InteractiveTextBufferSupportsFeatureService))) _
.CreateExportProvider()

Using workspace = TestWorkspace.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3367,7 +3367,7 @@ End Namespace"
<Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub TestExtractMethodCommandDisabledInSubmission()
Dim exportProvider = ExportProviderCache _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveTextBufferSupportsFeatureService))) _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveSupportsFeatureService.InteractiveTextBufferSupportsFeatureService))) _
.CreateExportProvider()

Using workspace = TestWorkspace.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ End Namespace</element>
<Trait(Traits.Feature, Traits.Features.Interactive)>
Public Sub TestOrganizingCommandsDisabledInSubmission()
Dim exportProvider = ExportProviderCache _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveTextBufferSupportsFeatureService))) _
.GetOrCreateExportProviderFactory(TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithParts(GetType(InteractiveSupportsFeatureService.InteractiveTextBufferSupportsFeatureService))) _
.CreateExportProvider()

Using workspace = TestWorkspace.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
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;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Extensions.ContextQuery;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -179,7 +181,14 @@ internal override async Task<CompletionChange> 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);
Expand Down Expand Up @@ -223,14 +232,32 @@ internal override async Task<CompletionChange> 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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use feature service from debugger workspace to disable it rather than using CanApplyChange just for debugger workspace?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, doesn't work. workspace.Services.GetService<IDocumentSupportsFeatureService>().SupportsRefactorings(document) returns true for DebuggerIntellisenseWorkspace

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, you need to export one for DebuggerIntellisenseWorkspace that return false? IDocumentSupportsFeatureService is per workpsace service which supposed to return different value based on workspace? like we have one for interactive and etc?

{
return true;
}

// During an EnC session, adding import is not supported.
var encService = workspace.Services.GetService<IDebuggingWorkspaceService>()?.EditAndContinueServiceOpt;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should move to FeatureService ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tmat What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return "CodeFix and Refactorings not available" in the ITextBufferSupportsFeatureService during EnC?

if (encService?.EditSession != null)
{
return true;
}

// Certain documents, e.g. Razor document, don't support adding imports
var documentSupportsFeatureService = workspace.Services.GetService<IDocumentSupportsFeatureService>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm.. why not add one specific for type import?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the existing SupportRefactoring already does what I need, so I just used it. Do you prefer adding one specific to adding import? That feels too narrow to me.

if (!documentSupportsFeatureService.SupportsRefactorings(document))
{
return true;
}

return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class MoveToNamespaceResult

public MoveToNamespaceResult(
Solution originalSolution,
Solution updatedSolution,
Solution updatedSolution,
DocumentId updatedDocumentId,
ImmutableDictionary<string, ISymbol> newNameOriginalSymbolMapping)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Composition;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;

namespace Microsoft.CodeAnalysis.Shared
{
internal interface IDocumentSupportsFeatureService : IWorkspaceService
{
bool SupportsCodeFixes(Document document);
bool SupportsRefactorings(Document document);
bool SupportsRename(Document document);
bool SupportsNavigationToAnyPosition(Document document);
}


[ExportWorkspaceService(typeof(IDocumentSupportsFeatureService), ServiceLayer.Default), Shared]
internal class DefaultDocumentSupportsFeatureService : IDocumentSupportsFeatureService
{
public bool SupportsCodeFixes(Document document)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we actually need one that accept Document for these? I think @dibarbet actually removed one that accept document and explicitly changed to text buffer for these APIs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't need those for my purpose, they were included just because I wanted to expose the same APIs in Feature layer. @dibarbet Any particular reason that those shouldn't be available for Document?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any reason for them not to be available - it was changed from document -> text buffer because we had the text buffer first in almost all the cases the interface was being used, so we were often just getting the document for no reason.

=> true;

public bool SupportsNavigationToAnyPosition(Document document)
=> true;

public bool SupportsRefactorings(Document document)
=> true;

public bool SupportsRename(Document document)
=> true;
}
}
Loading