提交 414caf69 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #18442 from CyrusNajmabadi/addImportOOPWork1

Break types into their own files.
// 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.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.FindSymbols;
namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
{
internal abstract partial class AbstractAddImportCodeFixProvider<TSimpleNameSyntax>
{
/// <summary>
/// SearchScope used for searching *all* the symbols contained within a project/compilation.
/// i.e. the symbols created from source *and* symbols from references (both project and
/// metadata).
/// </summary>
private class AllSymbolsProjectSearchScope : ProjectSearchScope
{
public AllSymbolsProjectSearchScope(
AbstractAddImportCodeFixProvider<TSimpleNameSyntax> provider,
Project project,
bool exact,
CancellationToken cancellationToken)
: base(provider, project, exact, cancellationToken)
{
}
protected override Task<ImmutableArray<ISymbol>> FindDeclarationsAsync(
string name, SymbolFilter filter, SearchQuery searchQuery)
{
return SymbolFinder.FindDeclarationsAsync(_project, searchQuery, filter, CancellationToken);
}
}
}
}
\ No newline at end of file
// 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.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.FindSymbols.SymbolTree;
namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
{
internal abstract partial class AbstractAddImportCodeFixProvider<TSimpleNameSyntax>
{
private class MetadataSymbolsSearchScope : SearchScope
{
private readonly IAssemblySymbol _assembly;
private readonly PortableExecutableReference _metadataReference;
private readonly Solution _solution;
public MetadataSymbolsSearchScope(
AbstractAddImportCodeFixProvider<TSimpleNameSyntax> provider,
Solution solution,
IAssemblySymbol assembly,
PortableExecutableReference metadataReference,
bool exact,
CancellationToken cancellationToken)
: base(provider, exact, cancellationToken)
{
_solution = solution;
_assembly = assembly;
_metadataReference = metadataReference;
}
public override SymbolReference CreateReference<T>(SymbolResult<T> searchResult)
{
return new MetadataSymbolReference(
provider,
searchResult.WithSymbol<INamespaceOrTypeSymbol>(searchResult.Symbol),
_metadataReference);
}
protected override async Task<ImmutableArray<ISymbol>> FindDeclarationsAsync(
string name, SymbolFilter filter, SearchQuery searchQuery)
{
var service = _solution.Workspace.Services.GetService<ISymbolTreeInfoCacheService>();
var info = await service.TryGetMetadataSymbolTreeInfoAsync(_solution, _metadataReference, CancellationToken).ConfigureAwait(false);
if (info == null)
{
return ImmutableArray<ISymbol>.Empty;
}
return await info.FindAsync(searchQuery, _assembly, filter, CancellationToken).ConfigureAwait(false);
}
}
}
}
\ No newline at end of file
// 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.Threading;
namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
{
internal abstract partial class AbstractAddImportCodeFixProvider<TSimpleNameSyntax>
{
private abstract class ProjectSearchScope : SearchScope
{
protected readonly Project _project;
public ProjectSearchScope(
AbstractAddImportCodeFixProvider<TSimpleNameSyntax> provider,
Project project,
bool exact,
CancellationToken cancellationToken)
: base(provider, exact, cancellationToken)
{
_project = project;
}
public override SymbolReference CreateReference<T>(SymbolResult<T> symbol)
{
return new ProjectSymbolReference(
provider, symbol.WithSymbol<INamespaceOrTypeSymbol>(symbol.Symbol), _project);
}
}
}
}
\ No newline at end of file
// 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.Collections.Concurrent;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.FindSymbols.SymbolTree;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
......@@ -70,138 +68,5 @@ protected SearchScope(AbstractAddImportCodeFixProvider<TSimpleNameSyntax> provid
}
}
}
private abstract class ProjectSearchScope : SearchScope
{
protected readonly Project _project;
public ProjectSearchScope(
AbstractAddImportCodeFixProvider<TSimpleNameSyntax> provider,
Project project,
bool exact,
CancellationToken cancellationToken)
: base(provider, exact, cancellationToken)
{
_project = project;
}
public override SymbolReference CreateReference<T>(SymbolResult<T> symbol)
{
return new ProjectSymbolReference(
provider, symbol.WithSymbol<INamespaceOrTypeSymbol>(symbol.Symbol), _project);
}
}
/// <summary>
/// SearchScope used for searching *all* the symbols contained within a project/compilation.
/// i.e. the symbols created from source *and* symbols from references (both project and
/// metadata).
/// </summary>
private class AllSymbolsProjectSearchScope : ProjectSearchScope
{
public AllSymbolsProjectSearchScope(
AbstractAddImportCodeFixProvider<TSimpleNameSyntax> provider,
Project project,
bool exact,
CancellationToken cancellationToken)
: base(provider, project, exact, cancellationToken)
{
}
protected override Task<ImmutableArray<ISymbol>> FindDeclarationsAsync(
string name, SymbolFilter filter, SearchQuery searchQuery)
{
return SymbolFinder.FindDeclarationsAsync(_project, searchQuery, filter, CancellationToken);
}
}
/// <summary>
/// SearchScope used for searching *only* the source symbols contained within a project/compilation.
/// i.e. symbols from metadata will not be searched.
/// </summary>
private class SourceSymbolsProjectSearchScope : ProjectSearchScope
{
private readonly ConcurrentDictionary<Project, AsyncLazy<IAssemblySymbol>> _projectToAssembly;
public SourceSymbolsProjectSearchScope(
AbstractAddImportCodeFixProvider<TSimpleNameSyntax> provider,
ConcurrentDictionary<Project, AsyncLazy<IAssemblySymbol>> projectToAssembly,
Project project, bool ignoreCase, CancellationToken cancellationToken)
: base(provider, project, ignoreCase, cancellationToken)
{
_projectToAssembly = projectToAssembly;
}
protected override async Task<ImmutableArray<ISymbol>> FindDeclarationsAsync(
string name, SymbolFilter filter, SearchQuery searchQuery)
{
var service = _project.Solution.Workspace.Services.GetService<ISymbolTreeInfoCacheService>();
var info = await service.TryGetSourceSymbolTreeInfoAsync(_project, CancellationToken).ConfigureAwait(false);
if (info == null)
{
// Looks like there was nothing in the cache. Return no results for now.
return ImmutableArray<ISymbol>.Empty;
}
// Don't create the assembly until it is actually needed by the SymbolTreeInfo.FindAsync
// code. Creating the assembly can be costly and we want to avoid it until it is actually
// needed.
var lazyAssembly = _projectToAssembly.GetOrAdd(_project, CreateLazyAssembly);
return await info.FindAsync(searchQuery, lazyAssembly, filter, CancellationToken).ConfigureAwait(false);
}
private static AsyncLazy<IAssemblySymbol> CreateLazyAssembly(Project project)
{
return new AsyncLazy<IAssemblySymbol>(
async c =>
{
var compilation = await project.GetCompilationAsync(c).ConfigureAwait(false);
return compilation.Assembly;
}, cacheResult: true);
}
}
private class MetadataSymbolsSearchScope : SearchScope
{
private readonly IAssemblySymbol _assembly;
private readonly PortableExecutableReference _metadataReference;
private readonly Solution _solution;
public MetadataSymbolsSearchScope(
AbstractAddImportCodeFixProvider<TSimpleNameSyntax> provider,
Solution solution,
IAssemblySymbol assembly,
PortableExecutableReference metadataReference,
bool exact,
CancellationToken cancellationToken)
: base(provider, exact, cancellationToken)
{
_solution = solution;
_assembly = assembly;
_metadataReference = metadataReference;
}
public override SymbolReference CreateReference<T>(SymbolResult<T> searchResult)
{
return new MetadataSymbolReference(
provider,
searchResult.WithSymbol<INamespaceOrTypeSymbol>(searchResult.Symbol),
_metadataReference);
}
protected override async Task<ImmutableArray<ISymbol>> FindDeclarationsAsync(
string name, SymbolFilter filter, SearchQuery searchQuery)
{
var service = _solution.Workspace.Services.GetService<ISymbolTreeInfoCacheService>();
var info = await service.TryGetMetadataSymbolTreeInfoAsync(_solution, _metadataReference, CancellationToken).ConfigureAwait(false);
if (info == null)
{
return ImmutableArray<ISymbol>.Empty;
}
return await info.FindAsync(searchQuery, _assembly, filter, CancellationToken).ConfigureAwait(false);
}
}
}
}
\ No newline at end of file
// 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.Collections.Concurrent;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.FindSymbols.SymbolTree;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
{
internal abstract partial class AbstractAddImportCodeFixProvider<TSimpleNameSyntax>
{
/// <summary>
/// SearchScope used for searching *only* the source symbols contained within a project/compilation.
/// i.e. symbols from metadata will not be searched.
/// </summary>
private class SourceSymbolsProjectSearchScope : ProjectSearchScope
{
private readonly ConcurrentDictionary<Project, AsyncLazy<IAssemblySymbol>> _projectToAssembly;
public SourceSymbolsProjectSearchScope(
AbstractAddImportCodeFixProvider<TSimpleNameSyntax> provider,
ConcurrentDictionary<Project, AsyncLazy<IAssemblySymbol>> projectToAssembly,
Project project, bool ignoreCase, CancellationToken cancellationToken)
: base(provider, project, ignoreCase, cancellationToken)
{
_projectToAssembly = projectToAssembly;
}
protected override async Task<ImmutableArray<ISymbol>> FindDeclarationsAsync(
string name, SymbolFilter filter, SearchQuery searchQuery)
{
var service = _project.Solution.Workspace.Services.GetService<ISymbolTreeInfoCacheService>();
var info = await service.TryGetSourceSymbolTreeInfoAsync(_project, CancellationToken).ConfigureAwait(false);
if (info == null)
{
// Looks like there was nothing in the cache. Return no results for now.
return ImmutableArray<ISymbol>.Empty;
}
// Don't create the assembly until it is actually needed by the SymbolTreeInfo.FindAsync
// code. Creating the assembly can be costly and we want to avoid it until it is actually
// needed.
var lazyAssembly = _projectToAssembly.GetOrAdd(_project, CreateLazyAssembly);
return await info.FindAsync(searchQuery, lazyAssembly, filter, CancellationToken).ConfigureAwait(false);
}
private static AsyncLazy<IAssemblySymbol> CreateLazyAssembly(Project project)
{
return new AsyncLazy<IAssemblySymbol>(
async c =>
{
var compilation = await project.GetCompilationAsync(c).ConfigureAwait(false);
return compilation.Assembly;
}, cacheResult: true);
}
}
}
}
\ No newline at end of file
......@@ -100,6 +100,10 @@
<Compile Include="..\..\..\Compilers\Shared\DesktopShim.cs">
<Link>Shared\Utilities\DesktopShim.cs</Link>
</Compile>
<Compile Include="AddImport\SearchScopes\AllSymbolsProjectSearchScope.cs" />
<Compile Include="AddImport\SearchScopes\MetadataSymbolsSearchScope.cs" />
<Compile Include="AddImport\SearchScopes\ProjectSearchScope.cs" />
<Compile Include="AddImport\SearchScopes\SourceSymbolsProjectSearchScope.cs" />
<Compile Include="AddImport\SymbolReferenceFinder_PackageAssemblySearch.cs" />
<Compile Include="AddPackage\AbstractAddPackageCodeFixProvider.cs" />
<Compile Include="AddPackage\AbstractAddSpecificPackageCodeFixProvider.cs" />
......@@ -216,7 +220,7 @@
<Compile Include="AddImport\References\PackageReference.cs" />
<Compile Include="AddImport\References\ProjectSymbolReference.cs" />
<Compile Include="AddImport\References\Reference.cs" />
<Compile Include="AddImport\SearchScope.cs" />
<Compile Include="AddImport\SearchScopes\SearchScope.cs" />
<Compile Include="AddImport\References\SymbolReference.cs" />
<Compile Include="AddImport\SymbolReferenceFinder.cs" />
<Compile Include="AddImport\SymbolResult.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册