Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roslyn CodeFixProvider gives VS 2015 error

I have created a roslyn CodeAnalyzer and a CodeFixProvider.

The Analyzer runs fine, and creates the rule, but when I'm trying to open the popup showing the fix, I get a "One or more errors occurred" VS popup.

First time I ran it, it worked fine, but then I stopped debugging and after that it gave me that error, so I tried on another computer and again it worked fine the first time I debugged.

My Analyzer:

private static void Analyze(SyntaxNodeAnalysisContext context)
{
    var localDeclaration = (LocalDeclarationStatementSyntax)context.Node;

    foreach (var variable in localDeclaration.Declaration.Variables)
    {
        var initializer = variable.Initializer;
        if (initializer == null) return;
    }

    var node = context.Node;

    while (node.Kind() != SyntaxKind.MethodDeclaration)
    {
        node = node.Parent;
    }

    var method = (MethodDeclarationSyntax)node;

    if (method.AttributeLists.Any(x => x.Attributes.Any(y => y.Name is IdentifierNameSyntax && ((IdentifierNameSyntax)y.Name).Identifier.Text.ToLower().Contains("test"))))
    {
        context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation()));
    }
}

My CodeFixProvider

private async Task<Document> AddAssertionsAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken)
{
    var editor = await DocumentEditor.CreateAsync(document, cancellationToken);

    var assert = SyntaxFactory.IdentifierName("Assert");
    var areEqual = SyntaxFactory.IdentifierName("AreEqual");

    var memberAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, assert,
            areEqual);

    var firstArgument = SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(@"")));
    var secondArgument = SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(@"")));

    var argumentList = SyntaxFactory.SeparatedList<ArgumentSyntax>(
        new SyntaxNodeOrToken[] {
            firstArgument,
            SyntaxFactory.Token(SyntaxKind.CommaToken),
            secondArgument
        });

    var assertToken =
        SyntaxFactory.ExpressionStatement(
        SyntaxFactory.InvocationExpression(memberAccess,
        SyntaxFactory.ArgumentList(argumentList)));

    editor.InsertAfter(localDeclaration, assertToken);
    var newDocument = editor.GetChangedDocument();

    return newDocument;
}

What I'm trying to achive is

[Test]
public void blah()
{
    var stat = string.Empty;
}

Becomes

[Test]
public void blah()
{
    var stat = string.Empty;
    Assert.AreEqual("", "");
}

When you press ctrl+. on "stat"... And it is here that VS2015 gives the error, just not first time...

like image 391
Joshlo Avatar asked Oct 29 '22 15:10

Joshlo


1 Answers

Don't know why, but I've found that deleting the %localappdata%\Microsoft\VisualStudio\14.0Exp folder helps.

like image 188
Maxence Avatar answered Nov 15 '22 05:11

Maxence