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

Add options to enable/disable nuget search. #11023

Merged
merged 26 commits into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8d5c08d
Remove option we aren't using.
CyrusNajmabadi May 2, 2016
a7a631f
Add UI options for configuring 'add using'
CyrusNajmabadi May 2, 2016
f39fd52
Update strings.
CyrusNajmabadi May 2, 2016
fd19f45
The option is not per language.
CyrusNajmabadi May 2, 2016
5de5408
Merge the two symbol search APIs into one.
CyrusNajmabadi May 2, 2016
968a975
Option serialization work.
CyrusNajmabadi May 2, 2016
f14aa61
Properly persist options.
CyrusNajmabadi May 2, 2016
6f66b48
Rename group.
CyrusNajmabadi May 2, 2016
9993a3d
Expose optoins for VS as well.
CyrusNajmabadi May 2, 2016
87d43bd
Make the nuget options into per-language options.
CyrusNajmabadi May 3, 2016
1132292
Update tests.
CyrusNajmabadi May 3, 2016
41a326f
Rename type.
CyrusNajmabadi May 3, 2016
70429d6
Rename files.
CyrusNajmabadi May 3, 2016
f58bc96
Move files to new folder.
CyrusNajmabadi May 3, 2016
2d4291f
Move file.
CyrusNajmabadi May 3, 2016
c10a2d7
Move SymbolSearch into its own namespace.
CyrusNajmabadi May 3, 2016
0ce9b17
Move files.
CyrusNajmabadi May 3, 2016
19e6663
Rename types.
CyrusNajmabadi May 3, 2016
3607ad9
Move types to SymbolSearch.
CyrusNajmabadi May 3, 2016
b8c6ae3
Move test code.
CyrusNajmabadi May 3, 2016
d03df21
Update type name.
CyrusNajmabadi May 3, 2016
f99d4e3
Update option name.
CyrusNajmabadi May 3, 2016
0b8fe75
Fix accelerators.
CyrusNajmabadi May 3, 2016
110bc76
Update accelerators.
CyrusNajmabadi May 3, 2016
2f00943
Sort usings.
CyrusNajmabadi May 3, 2016
c5de602
Merge branch 'master' into usingOptions
CyrusNajmabadi May 4, 2016
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 @@ -10,13 +10,16 @@
using Microsoft.CodeAnalysis.CSharp.CodeFixes.AddImport;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Packaging;
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 CodeAnalysis.Shared.Options;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Why are these inside the namespace?

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 fixed.

using Editor.UnitTests.Workspaces;
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;
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Types moved out of the packaging namespace into this new namespace.

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 @@ -10,6 +9,8 @@
using Microsoft.CodeAnalysis.Packaging;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.Shared.Options;
using Microsoft.CodeAnalysis.SymbolSearch;
Copy link
Member

Choose a reason for hiding this comment

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

Sort usings

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.


namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
{
Expand Down Expand Up @@ -232,32 +233,41 @@ private async Task<IList<SymbolReference>> GetMatchingTypesAsync(SearchScope sea
{
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 @@ private async Task<IList<SymbolReference>> GetMatchingTypesAsync(SearchScope sea

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 @@ -281,8 +281,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