Roslyn VSIX将局部变量转换为全局const

时间:2017-05-19 14:08:11

标签: c# roslyn vsix

我正在制作一个简单的Roslyn Extensions来寻找潜在的功能并使它们成为全球性的。我编写的代码首先转换为本地const,工作正常:

我有一个codeFixProvider注册我的修复程序,如下所示:

 context.RegisterCodeFix(CodeAction.Create(title: title,
     createChangedDocument: c => this.MakeConstAsync(context.Document, declaration, c),
     equivalenceKey: title),  diagnostic);

我的Make Const功能如下所示:

private async Task<Document> MakeConstAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken)
{
    // Remove the leading trivia from the local declaration.
    var firstToken = localDeclaration.GetFirstToken();
    var leadingTrivia = firstToken.LeadingTrivia;
    var trimmedLocal = localDeclaration.ReplaceToken(
        firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty));

    localDeclaration.GetNearestParent<ClassDeclarationSyntax>();

    // Create a const token with the leading trivia.
    var constToken = SyntaxFactory.Token(leadingTrivia, SyntaxKind.ConstKeyword, SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker));

    // Insert the const token into the modifiers list, creating a new modifiers list.
    var newModifiers = trimmedLocal.Modifiers.Insert(0, constToken);

    // If the type of the declaration is 'var', create a new type name
    // for the inferred type.
    var variableDeclaration = localDeclaration.Declaration;
    var variableTypeName = variableDeclaration.Type;
    if (variableTypeName.IsVar)
    {
        var semanticModel = await document.GetSemanticModelAsync(cancellationToken);

        // Special case: Ensure that 'var' isn't actually an alias to another type
        // (e.g. using var = System.String).
        var aliasInfo = semanticModel.GetAliasInfo(variableTypeName);
        if (aliasInfo == null)
        {
            // Retrieve the type inferred for var.
            var type = semanticModel.GetTypeInfo(variableTypeName).ConvertedType;

            // Special case: Ensure that 'var' isn't actually a type named 'var'.
            if (type.Name != "var")
            {
                // Create a new TypeSyntax for the inferred type. Be careful
                // to keep any leading and trailing trivia from the var keyword.
                var typeName = SyntaxFactory.ParseTypeName(type.ToDisplayString())
                    .WithLeadingTrivia(variableTypeName.GetLeadingTrivia())
                    .WithTrailingTrivia(variableTypeName.GetTrailingTrivia());

                // Add an annotation to simplify the type name.
                var simplifiedTypeName = typeName.WithAdditionalAnnotations(Simplifier.Annotation);

                // Replace the type in the variable declaration.
                variableDeclaration = variableDeclaration.WithType(simplifiedTypeName);
            }
        }
    }

    var root = await document.GetSyntaxRootAsync(cancellationToken);

    // Produce the new local declaration.
    var newLocal = trimmedLocal.WithModifiers(newModifiers)
                                .WithDeclaration(variableDeclaration);

    // Add an annotation to format the new local declaration.
    var formattedLocal = newLocal.WithAdditionalAnnotations(Formatter.Annotation);

    // Replace the old local declaration with the new local declaration.
    var newRoot = root.ReplaceNode(localDeclaration, formattedLocal);

    // Return document with transformed tree.
    return document.WithSyntaxRoot(newRoot);
}

所有这些工作都很好,但是当我介绍一个创建FieldDeclaration的函数时,下次进入hive时,要检查语法分析,我按Ctrl +。在弹出任何东西之前,我会遇到一个有问题的一个或多个错误的模态。

public FieldDeclarationSyntax ConvertToFieldDeclarationSyntax(SyntaxTokenList modifiers, VariableDeclarationSyntax declaration, SyntaxKind syntaxKind = SyntaxKind.PublicKeyword)
{
    return null;
    //return SyntaxFactory.FieldDeclaration(new SyntaxList<AttributeListSyntax>(), modifiers, declaration);
}

我不确定为什么但是上面抛出的函数会导致错误,我甚至从未引用过,出于某种原因我猜加载该程序集会发生冲突。有谁知道如何在Roslyn中制作FieldDeclarationSyntax?

修改

所以我打开所有异常,现在我看到我收到错误

  

无法将类型为“System.Reflection.RuntimeMethodInfo”的对象强制转换为“System.Reflection.ConstructorInfo”。

现在检查这个时,我在这里看到了堆栈跟踪的附加错误:

enter image description here

按下ctrl +时会发生这种情况。在我突出显示的语法错误规则上,从配置单元弹出一个模式:enter image description here

0 个答案:

没有答案