提交 06d68885 编写于 作者: C CyrusNajmabadi

Break type into its own file.

上级 ea7f125b
// 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.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Tags;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
{
internal abstract partial class AbstractAddImportCodeFixProvider<TSimpleNameSyntax>
{
private partial class AssemblyReference
{
private class AssemblyReferenceCodeAction : CodeAction
{
private readonly AssemblyReference _reference;
private readonly string _title;
private readonly Document _document;
private readonly SyntaxNode _node;
private readonly bool _placeSystemNamespaceFirst;
public override string Title => _title;
public override ImmutableArray<string> Tags => WellKnownTagArrays.AddReference;
private readonly Lazy<string> _lazyResolvedPath;
public AssemblyReferenceCodeAction(
AssemblyReference reference,
Document document,
SyntaxNode node,
bool placeSystemNamespaceFirst)
{
_reference = reference;
_document = document;
_node = node;
_placeSystemNamespaceFirst = placeSystemNamespaceFirst;
_title = $"{reference.provider.GetDescription(reference.SearchResult.NameParts)} ({string.Format(FeaturesResources.from_0, reference._referenceAssemblyWithType.AssemblyName)})";
_lazyResolvedPath = new Lazy<string>(ResolvePath);
}
// Adding a reference is always low priority.
internal override CodeActionPriority Priority => CodeActionPriority.Low;
private string ResolvePath()
{
var assemblyResolverService = _document.Project.Solution.Workspace.Services.GetService<IFrameworkAssemblyPathResolver>();
var packageWithType = _reference._referenceAssemblyWithType;
var fullyQualifiedName = string.Join(".", packageWithType.ContainingNamespaceNames.Concat(packageWithType.TypeName));
var assemblyPath = assemblyResolverService?.ResolveAssemblyPath(
_document.Project.Id, packageWithType.AssemblyName, fullyQualifiedName);
return assemblyPath;
}
internal override bool PerformFinalApplicabilityCheck => true;
internal override bool IsApplicable(Workspace workspace)
{
return !string.IsNullOrWhiteSpace(_lazyResolvedPath.Value);
}
protected override async Task<IEnumerable<CodeActionOperation>> ComputeOperationsAsync(CancellationToken cancellationToken)
{
var service = _document.Project.Solution.Workspace.Services.GetService<IMetadataService>();
var resolvedPath = _lazyResolvedPath.Value;
var reference = service.GetReference(resolvedPath, MetadataReferenceProperties.Assembly);
// First add the "using/import" directive in the code.
(SyntaxNode node, Document document) = await _reference.ReplaceNameNodeAsync(
_node, _document, cancellationToken).ConfigureAwait(false);
var newDocument = await _reference.provider.AddImportAsync(
node, _reference.SearchResult.NameParts, document, _placeSystemNamespaceFirst, cancellationToken).ConfigureAwait(false);
// Now add the actual assembly reference.
var newProject = newDocument.Project;
newProject = newProject.WithMetadataReferences(
newProject.MetadataReferences.Concat(reference));
var operation = new ApplyChangesOperation(newProject.Solution);
return SpecializedCollections.SingletonEnumerable<CodeActionOperation>(operation);
}
}
}
}
}
\ 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;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.SymbolSearch;
using Microsoft.CodeAnalysis.Tags;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CodeFixes.AddImport
{
internal abstract partial class AbstractAddImportCodeFixProvider<TSimpleNameSyntax>
{
private class AssemblyReference : Reference
private partial class AssemblyReference : Reference
{
private readonly ReferenceAssemblyWithTypeResult _referenceAssemblyWithType;
......@@ -46,80 +41,6 @@ public override int GetHashCode()
{
return Hash.Combine(_referenceAssemblyWithType.AssemblyName, base.GetHashCode());
}
private class AssemblyReferenceCodeAction : CodeAction
{
private readonly AssemblyReference _reference;
private readonly string _title;
private readonly Document _document;
private readonly SyntaxNode _node;
private readonly bool _placeSystemNamespaceFirst;
public override string Title => _title;
public override ImmutableArray<string> Tags => WellKnownTagArrays.AddReference;
private readonly Lazy<string> _lazyResolvedPath;
public AssemblyReferenceCodeAction(
AssemblyReference reference,
Document document,
SyntaxNode node,
bool placeSystemNamespaceFirst)
{
_reference = reference;
_document = document;
_node = node;
_placeSystemNamespaceFirst = placeSystemNamespaceFirst;
_title = $"{reference.provider.GetDescription(reference.SearchResult.NameParts)} ({string.Format(FeaturesResources.from_0, reference._referenceAssemblyWithType.AssemblyName)})";
_lazyResolvedPath = new Lazy<string>(ResolvePath);
}
// Adding a reference is always low priority.
internal override CodeActionPriority Priority => CodeActionPriority.Low;
private string ResolvePath()
{
var assemblyResolverService = _document.Project.Solution.Workspace.Services.GetService<IFrameworkAssemblyPathResolver>();
var packageWithType = _reference._referenceAssemblyWithType;
var fullyQualifiedName = string.Join(".", packageWithType.ContainingNamespaceNames.Concat(packageWithType.TypeName));
var assemblyPath = assemblyResolverService?.ResolveAssemblyPath(
_document.Project.Id, packageWithType.AssemblyName, fullyQualifiedName);
return assemblyPath;
}
internal override bool PerformFinalApplicabilityCheck => true;
internal override bool IsApplicable(Workspace workspace)
{
return !string.IsNullOrWhiteSpace(_lazyResolvedPath.Value);
}
protected override async Task<IEnumerable<CodeActionOperation>> ComputeOperationsAsync(CancellationToken cancellationToken)
{
var service = _document.Project.Solution.Workspace.Services.GetService<IMetadataService>();
var resolvedPath = _lazyResolvedPath.Value;
var reference = service.GetReference(resolvedPath, MetadataReferenceProperties.Assembly);
// First add the "using/import" directive in the code.
(SyntaxNode node, Document document) = await _reference.ReplaceNameNodeAsync(
_node, _document, cancellationToken).ConfigureAwait(false);
var newDocument = await _reference.provider.AddImportAsync(
node, _reference.SearchResult.NameParts, document, _placeSystemNamespaceFirst, cancellationToken).ConfigureAwait(false);
// Now add the actual assembly reference.
var newProject = newDocument.Project;
newProject = newProject.WithMetadataReferences(
newProject.MetadataReferences.Concat(reference));
var operation = new ApplyChangesOperation(newProject.Solution);
return SpecializedCollections.SingletonEnumerable<CodeActionOperation>(operation);
}
}
}
}
}
\ No newline at end of file
......@@ -100,6 +100,7 @@
<Compile Include="..\..\..\Compilers\Shared\DesktopShim.cs">
<Link>Shared\Utilities\DesktopShim.cs</Link>
</Compile>
<Compile Include="AddImport\CodeActions\AssemblyReferenceCodeAction.cs" />
<Compile Include="AddImport\SearchScopes\AllSymbolsProjectSearchScope.cs" />
<Compile Include="AddImport\SearchScopes\MetadataSymbolsSearchScope.cs" />
<Compile Include="AddImport\SearchScopes\ProjectSearchScope.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册