Skip to content

Commit

Permalink
Merge pull request #11023 from CyrusNajmabadi/usingOptions
Browse files Browse the repository at this point in the history
Add options to enable/disable nuget search.
  • Loading branch information
CyrusNajmabadi committed May 4, 2016
2 parents 1a3b616 + c5de602 commit 3379fcb
Show file tree
Hide file tree
Showing 54 changed files with 1,310 additions and 1,063 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.CodeFixes.AddImport;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Packaging;
using Microsoft.CodeAnalysis.Shared.Options;
using Microsoft.CodeAnalysis.SymbolSearch;
using Moq;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.AddUsing
{
using FixProviderData = Tuple<IPackageInstallerService, IPackageSearchService>;
using FixProviderData = Tuple<IPackageInstallerService, ISymbolSearchService>;

public partial class AddUsingTests
{
Expand All @@ -27,6 +30,15 @@ public class NuGet : AddUsingTests
private static readonly ImmutableArray<PackageSource> NugetPackageSources =
ImmutableArray.Create(new PackageSource(NugetOrgSource, "http://nuget.org/"));

protected override async Task<TestWorkspace> CreateWorkspaceFromFileAsync(string definition, ParseOptions parseOptions, CompilationOptions compilationOptions)
{
var workspace = await base.CreateWorkspaceFromFileAsync(definition, parseOptions, compilationOptions);
workspace.Options = workspace.Options
.WithChangedOption(AddImportOptions.SuggestForTypesInNuGetPackages, LanguageNames.CSharp, true)
.WithChangedOption(AddImportOptions.SuggestForTypesInReferenceAssemblies, LanguageNames.CSharp, true);
return workspace;
}

internal override Tuple<DiagnosticAnalyzer, CodeFixProvider> CreateDiagnosticProviderAndFixer(
Workspace workspace, object fixProviderData)
{
Expand All @@ -50,7 +62,7 @@ public async Task TestSearchPackageSingleName()
installerServiceMock.SetupGet(i => i.IsEnabled).Returns(true);
installerServiceMock.SetupGet(i => i.PackageSources).Returns(NugetPackageSources);

var packageServiceMock = new Mock<IPackageSearchService>();
var packageServiceMock = new Mock<ISymbolSearchService>();
packageServiceMock.Setup(s => s.FindPackagesWithType(
NugetOrgSource, "NuGetType", 0, It.IsAny<CancellationToken>()))
.Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NuGetNamespace")));
Expand Down Expand Up @@ -79,7 +91,7 @@ public async Task TestSearchPackageMultipleNames()
installerServiceMock.SetupGet(i => i.IsEnabled).Returns(true);
installerServiceMock.SetupGet(i => i.PackageSources).Returns(NugetPackageSources);

var packageServiceMock = new Mock<IPackageSearchService>();
var packageServiceMock = new Mock<ISymbolSearchService>();
packageServiceMock.Setup(s => s.FindPackagesWithType(
NugetOrgSource, "NuGetType", 0, It.IsAny<CancellationToken>()))
.Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NS1", "NS2")));
Expand Down Expand Up @@ -110,7 +122,7 @@ public async Task TestMissingIfPackageAlreadyInstalled()
installerServiceMock.Setup(s => s.IsInstalled(It.IsAny<Workspace>(), It.IsAny<ProjectId>(), "NuGetPackage"))
.Returns(true);

var packageServiceMock = new Mock<IPackageSearchService>();
var packageServiceMock = new Mock<ISymbolSearchService>();
packageServiceMock.Setup(s => s.FindPackagesWithType(
NugetOrgSource, "NuGetType", 0, It.IsAny<CancellationToken>()))
.Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NS1", "NS2")));
Expand All @@ -134,7 +146,7 @@ public async Task TestOptionsOffered()
installerServiceMock.Setup(s => s.GetInstalledVersions("NuGetPackage"))
.Returns(new[] { "1.0", "2.0" });

var packageServiceMock = new Mock<IPackageSearchService>();
var packageServiceMock = new Mock<ISymbolSearchService>();
packageServiceMock.Setup(s => s.FindPackagesWithType(
NugetOrgSource, "NuGetType", 0, It.IsAny<CancellationToken>()))
.Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NS1", "NS2")));
Expand Down Expand Up @@ -180,7 +192,7 @@ public async Task TestInstallGetsCalledNoVersion()
installerServiceMock.Setup(s => s.TryInstallPackage(
It.IsAny<Workspace>(), It.IsAny<DocumentId>(), It.IsAny<string>(), "NuGetPackage", /*versionOpt*/ null, It.IsAny<CancellationToken>()));

var packageServiceMock = new Mock<IPackageSearchService>();
var packageServiceMock = new Mock<ISymbolSearchService>();
packageServiceMock.Setup(s => s.FindPackagesWithType(
NugetOrgSource, "NuGetType", 0, It.IsAny<CancellationToken>()))
.Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NuGetNamespace")));
Expand Down Expand Up @@ -212,7 +224,7 @@ public async Task TestInstallGetsCalledWithVersion()
installerServiceMock.Setup(s => s.TryInstallPackage(
It.IsAny<Workspace>(), It.IsAny<DocumentId>(), It.IsAny<string>(), "NuGetPackage", "1.0", It.IsAny<CancellationToken>()));

var packageServiceMock = new Mock<IPackageSearchService>();
var packageServiceMock = new Mock<ISymbolSearchService>();
packageServiceMock.Setup(s => s.FindPackagesWithType(NugetOrgSource, "NuGetType", 0, It.IsAny<CancellationToken>()))
.Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NuGetNamespace")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ Imports System.Threading
Imports Microsoft.CodeAnalysis.CodeActions
Imports Microsoft.CodeAnalysis.CodeFixes
Imports Microsoft.CodeAnalysis.Diagnostics
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Packaging
Imports Microsoft.CodeAnalysis.Shared.Options
Imports Microsoft.CodeAnalysis.SymbolSearch
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.CodeFixes.AddImport
Imports Moq
Imports ProviderData = System.Tuple(Of Microsoft.CodeAnalysis.Packaging.IPackageInstallerService, Microsoft.CodeAnalysis.Packaging.IPackageSearchService)
Imports ProviderData = System.Tuple(Of Microsoft.CodeAnalysis.Packaging.IPackageInstallerService, Microsoft.CodeAnalysis.SymbolSearch.ISymbolSearchService)

Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeActions.AddImport
Partial Public Class AddImportTests
Expand All @@ -21,6 +24,14 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeActions.AddImp
Private Shared ReadOnly NugetPackageSources As ImmutableArray(Of PackageSource) =
ImmutableArray.Create(New PackageSource(NugetOrgSource, "http://nuget.org"))

Protected Overrides Async Function CreateWorkspaceFromFileAsync(definition As String, parseOptions As ParseOptions, compilationOptions As CompilationOptions) As Task(Of TestWorkspace)
Dim workspace = Await MyBase.CreateWorkspaceFromFileAsync(definition, parseOptions, compilationOptions)
workspace.Options = workspace.Options.
WithChangedOption(AddImportOptions.SuggestForTypesInNuGetPackages, LanguageNames.VisualBasic, True).
WithChangedOption(AddImportOptions.SuggestForTypesInReferenceAssemblies, LanguageNames.VisualBasic, True)
Return workspace
End Function

Friend Overrides Function CreateDiagnosticProviderAndFixer(workspace As Workspace, fixProviderData As Object) As Tuple(Of DiagnosticAnalyzer, CodeFixProvider)
Dim data = DirectCast(fixProviderData, ProviderData)
Return Tuple.Create(Of DiagnosticAnalyzer, CodeFixProvider)(
Expand All @@ -40,7 +51,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeActions.AddImp
installerServiceMock.SetupGet(Function(i) i.IsEnabled).Returns(True)
installerServiceMock.SetupGet(Function(i) i.PackageSources).Returns(NugetPackageSources)

Dim packageServiceMock = New Mock(Of IPackageSearchService)()
Dim packageServiceMock = New Mock(Of ISymbolSearchService)()
packageServiceMock.Setup(Function(s) s.FindPackagesWithType(NugetOrgSource, "NuGetType", 0, It.IsAny(Of CancellationToken)())).
Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NuGetNamespace")))

Expand All @@ -65,7 +76,7 @@ End Class", fixProviderData:=New ProviderData(installerServiceMock.Object, packa
installerServiceMock.SetupGet(Function(i) i.IsEnabled).Returns(True)
installerServiceMock.SetupGet(Function(i) i.PackageSources).Returns(NugetPackageSources)

Dim packageServiceMock = New Mock(Of IPackageSearchService)()
Dim packageServiceMock = New Mock(Of ISymbolSearchService)()
packageServiceMock.Setup(Function(s) s.FindPackagesWithType(NugetOrgSource, "NuGetType", 0, It.IsAny(Of CancellationToken)())).
Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NS1", "NS2")))

Expand All @@ -92,7 +103,7 @@ End Class", fixProviderData:=New ProviderData(installerServiceMock.Object, packa
installerServiceMock.Setup(Function(s) s.IsInstalled(It.IsAny(Of Workspace)(), It.IsAny(Of ProjectId)(), "NuGetPackage")).
Returns(True)

Dim packageServiceMock = New Mock(Of IPackageSearchService)()
Dim packageServiceMock = New Mock(Of ISymbolSearchService)()
packageServiceMock.Setup(Function(s) s.FindPackagesWithType(NugetOrgSource, "NuGetType", 0, It.IsAny(Of CancellationToken)())).
Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NS1", "NS2")))

Expand All @@ -114,7 +125,7 @@ fixProviderData:=New ProviderData(installerServiceMock.Object, packageServiceMoc
installerServiceMock.Setup(Function(s) s.GetInstalledVersions("NuGetPackage")).
Returns({"1.0", "2.0"})

Dim packageServiceMock = New Mock(Of IPackageSearchService)()
Dim packageServiceMock = New Mock(Of ISymbolSearchService)()
packageServiceMock.Setup(Function(s) s.FindPackagesWithType(NugetOrgSource, "NuGetType", 0, It.IsAny(Of CancellationToken)())).
Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NS1", "NS2")))

Expand Down Expand Up @@ -155,7 +166,7 @@ fixProviderData:=data)
installerServiceMock.Setup(Function(s) s.TryInstallPackage(
It.IsAny(Of Workspace), It.IsAny(Of DocumentId), It.IsAny(Of String), "NuGetPackage", Nothing, It.IsAny(Of CancellationToken)))

Dim packageServiceMock = New Mock(Of IPackageSearchService)()
Dim packageServiceMock = New Mock(Of ISymbolSearchService)()
packageServiceMock.Setup(Function(s) s.FindPackagesWithType(NugetOrgSource, "NuGetType", 0, It.IsAny(Of CancellationToken)())).
Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NuGetNamespace")))

Expand Down Expand Up @@ -183,7 +194,7 @@ End Class", fixProviderData:=New ProviderData(installerServiceMock.Object, packa
installerServiceMock.Setup(Function(s) s.TryInstallPackage(
It.IsAny(Of Workspace), It.IsAny(Of DocumentId), It.IsAny(Of String), "NuGetPackage", "1.0", It.IsAny(Of CancellationToken)))

Dim packageServiceMock = New Mock(Of IPackageSearchService)()
Dim packageServiceMock = New Mock(Of ISymbolSearchService)()
packageServiceMock.Setup(Function(s) s.FindPackagesWithType(NugetOrgSource, "NuGetType", 0, It.IsAny(Of CancellationToken)())).
Returns(CreateSearchResult("NuGetPackage", "NuGetType", CreateNameParts("NuGetNamespace")))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.CodeAnalysis.Packaging;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.SymbolSearch;
using Roslyn.Utilities;
using static Microsoft.CodeAnalysis.CSharp.CodeFixes.AddImport.AddImportDiagnosticIds;

Expand Down Expand Up @@ -154,8 +155,8 @@ public CSharpAddImportCodeFixProvider()
/// <summary>For testing purposes only (so that tests can pass in mock values)</summary>
internal CSharpAddImportCodeFixProvider(
IPackageInstallerService installerService,
IPackageSearchService packageSearchService)
: base(installerService, packageSearchService)
ISymbolSearchService symbolSearchService)
: base(installerService, symbolSearchService)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Packaging;
using Microsoft.CodeAnalysis.SymbolSearch;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// 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;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,6 +8,8 @@
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Packaging;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Options;
using Microsoft.CodeAnalysis.SymbolSearch;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
Expand Down Expand Up @@ -232,32 +233,41 @@ private async Task FindNugetOrReferenceAssemblyTypeReferencesWorkerAsync(
{
var workspaceServices = _document.Project.Solution.Workspace.Services;

var packageSearchService = _owner._packageSearchService ?? workspaceServices.GetService<IPackageSearchService>();
var referenceAssemblySearchService = _owner._referenceAssemblySearchService ?? workspaceServices.GetService<IReferenceAssemblySearchService>();
var symbolSearchService = _owner._symbolSearchService ?? workspaceServices.GetService<ISymbolSearchService>();
var installerService = _owner._packageInstallerService ?? workspaceServices.GetService<IPackageInstallerService>();

if (referenceAssemblySearchService != null)
var language = _document.Project.Language;

var options = workspaceServices.Workspace.Options;
var searchReferenceAssemblies = options.GetOption(
AddImportOptions.SuggestForTypesInReferenceAssemblies, language);
var searchNugetPackages = options.GetOption(
AddImportOptions.SuggestForTypesInNuGetPackages, language);

if (symbolSearchService != null &&
searchReferenceAssemblies)
{
cancellationToken.ThrowIfCancellationRequested();
await FindReferenceAssemblyTypeReferencesAsync(
referenceAssemblySearchService, allReferences, nameNode, name, arity, isAttributeSearch, cancellationToken).ConfigureAwait(false);
symbolSearchService, allReferences, nameNode, name, arity, isAttributeSearch, cancellationToken).ConfigureAwait(false);
}

if (packageSearchService != null &&
if (symbolSearchService != null &&
searchNugetPackages &&
installerService.IsEnabled)
{
foreach (var packageSource in installerService.PackageSources)
{
cancellationToken.ThrowIfCancellationRequested();
await FindNugetTypeReferencesAsync(
packageSource, packageSearchService, installerService, allReferences,
packageSource, symbolSearchService, installerService, allReferences,
nameNode, name, arity, isAttributeSearch, cancellationToken).ConfigureAwait(false);
}
}
}

private async Task FindReferenceAssemblyTypeReferencesAsync(
IReferenceAssemblySearchService searchService,
ISymbolSearchService searchService,
List<Reference> allReferences,
TSimpleNameSyntax nameNode,
string name,
Expand All @@ -283,7 +293,7 @@ await HandleReferenceAssemblyReferenceAsync(

private async Task FindNugetTypeReferencesAsync(
PackageSource source,
IPackageSearchService searchService,
ISymbolSearchService searchService,
IPackageInstallerService installerService,
List<Reference> allReferences,
TSimpleNameSyntax nameNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.Packaging;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Options;
using Microsoft.CodeAnalysis.SymbolSearch;
using Roslyn.Utilities;
using static Roslyn.Utilities.PortableShim;

Expand All @@ -25,18 +26,15 @@ internal abstract partial class AbstractAddImportCodeFixProvider<TSimpleNameSynt
private const int MaxResults = 3;

private readonly IPackageInstallerService _packageInstallerService;
private readonly IPackageSearchService _packageSearchService;
private readonly IReferenceAssemblySearchService _referenceAssemblySearchService;
private readonly ISymbolSearchService _symbolSearchService;

/// <summary>Values for these parameters can be provided (during testing) for mocking purposes.</summary>
protected AbstractAddImportCodeFixProvider(
IPackageInstallerService packageInstallerService = null,
IPackageSearchService packageSearchService = null,
IReferenceAssemblySearchService referenceAssemblySearchService = null)
ISymbolSearchService symbolSearchService = null)
{
_packageInstallerService = packageInstallerService;
_packageSearchService = packageSearchService;
_referenceAssemblySearchService = referenceAssemblySearchService;
_symbolSearchService = symbolSearchService;
}

protected abstract bool CanAddImport(SyntaxNode node, CancellationToken cancellationToken);
Expand Down Expand Up @@ -81,7 +79,8 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;
}

var placeSystemNamespaceFirst = document.Project.Solution.Workspace.Options.GetOption(
var options = document.Project.Solution.Workspace.Options;
var placeSystemNamespaceFirst = options.GetOption(
OrganizerOptions.PlaceSystemNamespaceFirst, document.Project.Language);

using (Logger.LogBlock(FunctionId.Refactoring_AddImport, cancellationToken))
Expand Down
3 changes: 2 additions & 1 deletion src/Features/Core/Portable/Features.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,9 @@
<Compile Include="RemoveUnnecessaryImports\AbstractRemoveUnnecessaryImportsService.cs" />
<Compile Include="ReplaceMethodWithProperty\ReplaceMethodWithPropertyCodeRefactoringProvider.cs" />
<Compile Include="ReplaceMethodWithProperty\IReplaceMethodWithPropertyService.cs" />
<Compile Include="Shared\Options\AddImportOptionsProvider.cs" />
<Compile Include="Shared\Extensions\ProjectExtensions.cs" />
<Compile Include="Shared\Options\OrganizerOptionsProvider.cs" />
<Compile Include="Shared\Options\AddImportOptions.cs" />
<Compile Include="Shared\Options\RuntimeOptions.cs" />
<Compile Include="Shared\Options\RuntimeOptionsProvider.cs" />
<Compile Include="Shared\Options\ServiceComponentOnOffOptionsProvider.cs" />
Expand Down
Loading

0 comments on commit 3379fcb

Please sign in to comment.