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

Cleanup the code in FullyQualify #65654

Merged
merged 6 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -18,7 +18,7 @@ namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.FullyQualify
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.FullyQualify), Shared]
[ExtensionOrder(After = PredefinedCodeFixProviderNames.AddImport)]
internal class CSharpFullyQualifyCodeFixProvider : AbstractFullyQualifyCodeFixProvider
internal class CSharpFullyQualifyCodeFixProvider : AbstractFullyQualifyCodeFixProvider<SimpleNameSyntax>
{
/// <summary>
/// name does not exist in context
Expand Down Expand Up @@ -51,45 +51,32 @@ public CSharpFullyQualifyCodeFixProvider()
{
}

public override ImmutableArray<string> FixableDiagnosticIds
{
get { return ImmutableArray.Create(CS0103, CS0104, CS0246, CS0305, CS0308, IDEDiagnosticIds.UnboundIdentifierId); }
}

protected override bool IgnoreCase => false;
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
ImmutableArray.Create(CS0103, CS0104, CS0246, CS0305, CS0308, IDEDiagnosticIds.UnboundIdentifierId);

protected override bool CanFullyQualify(Diagnostic diagnostic, ref SyntaxNode node)
protected override bool CanFullyQualify(Diagnostic diagnostic, SyntaxNode node, [NotNullWhen(true)] out SimpleNameSyntax? simpleName)
{
if (node is not SimpleNameSyntax simpleName)
{
simpleName = node as SimpleNameSyntax;
if (simpleName is null)
return false;
}

if (!simpleName.LooksLikeStandaloneTypeName())
{
return false;
}

if (!simpleName.CanBeReplacedWithAnyName())
{
return false;
}

return true;
}

protected override async Task<SyntaxNode> ReplaceNodeAsync(SyntaxNode node, string containerName, bool resultingSymbolIsType, CancellationToken cancellationToken)
protected override async Task<SyntaxNode> ReplaceNodeAsync(SimpleNameSyntax simpleName, string containerName, bool resultingSymbolIsType, CancellationToken cancellationToken)
{
var simpleName = (SimpleNameSyntax)node;

var leadingTrivia = simpleName.GetLeadingTrivia();
var newName = simpleName.WithLeadingTrivia(SyntaxTriviaList.Empty);

var qualifiedName = SyntaxFactory.QualifiedName(
SyntaxFactory.ParseName(containerName), newName);

qualifiedName = qualifiedName.WithLeadingTrivia(leadingTrivia);
qualifiedName = qualifiedName.WithAdditionalAnnotations(Formatter.Annotation);
var qualifiedName = SyntaxFactory.QualifiedName(SyntaxFactory.ParseName(containerName), newName)
.WithLeadingTrivia(leadingTrivia)
.WithAdditionalAnnotations(Formatter.Annotation);

var syntaxTree = simpleName.SyntaxTree;
var root = await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -99,7 +86,7 @@ protected override async Task<SyntaxNode> ReplaceNodeAsync(SyntaxNode node, stri
// CS0138 that would result from the former. Don't do this for using aliases though as `static` and using
// aliases cannot be combined.
if (resultingSymbolIsType &&
node.Parent is UsingDirectiveSyntax { Alias: null, StaticKeyword.RawKind: 0 } usingDirective)
simpleName.Parent is UsingDirectiveSyntax { Alias: null, StaticKeyword.RawKind: 0 } usingDirective)
{
var newUsingDirective = usingDirective
.WithStaticKeyword(SyntaxFactory.Token(SyntaxKind.StaticKeyword))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ protected override async Task<ImmutableArray<ISymbol>> FindDeclarationsAsync(
searchQuery, lazyAssembly, filter, CancellationToken).ConfigureAwait(false);

return declarations;
}

private static AsyncLazy<IAssemblySymbol> CreateLazyAssembly(Project project)
{
return new AsyncLazy<IAssemblySymbol>(
async c =>
{
var compilation = await project.GetRequiredCompilationAsync(c).ConfigureAwait(false);
return compilation.Assembly;
}, cacheResult: true);
static AsyncLazy<IAssemblySymbol> CreateLazyAssembly(Project project)
{
return new AsyncLazy<IAssemblySymbol>(
async c =>
{
var compilation = await project.GetRequiredCompilationAsync(c).ConfigureAwait(false);
return compilation.Assembly;
}, cacheResult: true);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Microsoft.CodeAnalysis.CodeFixes.FullyQualify
{
internal abstract partial class AbstractFullyQualifyCodeFixProvider : CodeFixProvider
internal abstract partial class AbstractFullyQualifyCodeFixProvider<TSimpleNameSyntax>
{
private readonly struct SymbolResult : IEquatable<SymbolResult>, IComparable<SymbolResult>
{
Expand Down
Loading