Skip to content

Commit

Permalink
Merge pull request #35225 from genlu/FixRazorCompletion
Browse files Browse the repository at this point in the history
Fix razor completion
  • Loading branch information
genlu authored May 2, 2019
2 parents 76e66c4 + ae12b5e commit ee7c923
Show file tree
Hide file tree
Showing 21 changed files with 247 additions and 125 deletions.
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
{
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 @@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.RemoveUnnecessaryImports;
Expand Down Expand Up @@ -588,6 +589,13 @@ private async Task<Document> FixReferencingDocumentAsync(
string newNamespace,
CancellationToken cancellationToken)
{
// Can't apply change to certain document, simply return unchanged.
// e.g. Razor document (*.g.cs file, not *.cshtml)
if (!document.CanApplyChange())
{
return document;
}

// 1. Fully qualify all simple references (i.e. not via an alias) with new namespace.
// 2. Add using of new namespace (for each reference's container).
// 3. Try to simplify qualified names introduced from step(1).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var textSpan = context.Span;
var cancellationToken = context.CancellationToken;

var workspace = document.Project.Solution.Workspace;
if (workspace.Kind == WorkspaceKind.MiscellaneousFiles || document.IsGeneratedCode(cancellationToken))
if (document.Project.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles ||
document.IsGeneratedCode(cancellationToken))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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 @@ -249,6 +250,13 @@ static bool ShouldCompleteWithFullyQualifyTypeName(Document document)
return true;
}

// Certain documents, e.g. Razor document, don't support adding imports
var documentSupportsFeatureService = workspace.Services.GetService<IDocumentSupportsFeatureService>();
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)
=> true;

public bool SupportsNavigationToAnyPosition(Document document)
=> true;

public bool SupportsRefactorings(Document document)
=> true;

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

0 comments on commit ee7c923

Please sign in to comment.