简体   繁体   English

有条件地在Roslyn Code Fix中添加“using”语句

[英]Conditionally add “using” statements in Roslyn Code Fix

I'm using the .NET compiler API to write some code analyzers / code fixers in Roslyn. 我正在使用.NET编译器API在Roslyn中编写一些代码分析器/代码修复程序。 I want the code fix to transform the following code: 我希望代码修复转换以下代码:

string.Format("{0} {1}", A, B)

To

StringExtensions.SafeJoin(" ", A, B)

So far, I have this code: 到目前为止,我有这个代码:

private async Task<Document> UseJoinAsync(Document document, InvocationExpressionSyntax invocationExpr, CancellationToken cancellationToken)
{
    var argumentList = invocationExpr.ArgumentList;
    var firstArgument = argumentList.Arguments[1];
    var secondArgument = argumentList.Arguments[2];

    var statement =
        InvocationExpression(
                MemberAccessExpression(
                    SyntaxKind.SimpleMemberAccessExpression,
                    IdentifierName("StringExtensions"), // requires using Trilogy.Miscellaneous
                    IdentifierName("SafeJoin")))
            .WithArgumentList(
                ArgumentList(
                    SeparatedList<ArgumentSyntax>(
                        new SyntaxNodeOrToken[]
                        {
                            Argument(
                                LiteralExpression(
                                    SyntaxKind.StringLiteralExpression,
                                    Literal(" "))),
                            Token(SyntaxKind.CommaToken),
                            firstArgument,
                            Token(SyntaxKind.CommaToken),
                            secondArgument
                        }))).WithLeadingTrivia(invocationExpr.GetLeadingTrivia()).WithTrailingTrivia(invocationExpr.GetTrailingTrivia())
            .WithAdditionalAnnotations(Formatter.Annotation);

    var root = await document.GetSyntaxRootAsync(cancellationToken);

    var newRoot = root.ReplaceNode(invocationExpr, statement);

    var newDocument = document.WithSyntaxRoot(newRoot);

    return newDocument;
}

However, I have two outstanding issues: 但是,我有两个悬而未决的问题:

1) How can I add the required using Trilogy.Miscellaneous to the top of the file. 1)如何using Trilogy.Miscellaneous将所需内容添加到文件顶部。

and

2) How can I detect if the required assembly is referenced by my Project. 2)如何检测我的项目是否引用了所需的程序集。 In this case, if my assembly Trilogy.Common is not referenced I would either not offer the code fix, or I would propose string.Join(" ", A, B) instead of my own SafeJoin implementation. 在这种情况下,如果我的程序集Trilogy.Common未被引用,我将不提供代码修复,或者我会建议使用string.Join(" ", A, B)而不是我自己的SafeJoin实现。

UPDATE UPDATE

I've solved #1 by updating my code as follows... 我通过更新我的代码解决了#1,如下所示......

var newRoot = root.ReplaceNode(invocationExpr, statement);

// Iterate through our usings to see if we've got what we need...
if (root?.Usings.Any(u => u.Name.ToString() == "Trilogy.Miscellaneous") == false)
{
    // Create and add the using statement...
    var usingStatement = UsingDirective(QualifiedName(IdentifierName("Trilogy"), IdentifierName("Miscellaneous")));
     newRoot = newRoot.AddUsings(usingStatement);
}

var newDocument = document.WithSyntaxRoot(newRoot);

return newDocument;

Still hoping for some help with item #2. 仍然希望对第2项有所帮助。

I ended up adding some code to the RegisterCodeFixesAsync method. 我最终在RegisterCodeFixesAsync方法中添加了一些代码。 I feel like the test for my required assembly is a bit of a hack, so I'll accept a better answer to this question if someone posts one. 我觉得我所需组装的测试有点像黑客,所以如果有人发帖,我会接受这个问题的更好答案。

   public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
    {
        var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

        var diagnostic = context.Diagnostics.First();
        var diagnosticSpan = diagnostic.Location.SourceSpan;

        // Find the type invocation expression identified by the diagnostic.
        var invocationExpr = root.FindToken(
                diagnosticSpan.Start).Parent.AncestorsAndSelf()
            .OfType<InvocationExpressionSyntax>().First();

        // Get the symantec model from the document
        var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);

        // Check for the assembly we need.  I suspect there is a better way...
        var hasAssembly = semanticModel.Compilation.ExternalReferences.Any(er => er.Properties.Kind == MetadataImageKind.Assembly && er.Display.EndsWith("Trilogy.Common.dll"));

        // Register a code action that will invoke the fix, but only
        // if we have the assembly that we need
        if (hasAssembly)
            context.RegisterCodeFix(
                CodeAction.Create(title, c => UseJoinAsync(
                    context.Document, invocationExpr, c), equivalenceKey: title), diagnostic);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM