Roslyn C#递增变化

Mal*_*loz 5 c# diagnostics roslyn

尝试使用Diagnostic和CodeFix来创建一个代码转换为:

variable = variable + 1;
otherVariable = otherVariable -1;
Run Code Online (Sandbox Code Playgroud)

成:

variable++;
otherVariable--;
Run Code Online (Sandbox Code Playgroud)

已完成诊断(可行):

var incrementing = node as BinaryExpressionSyntax;
if (incrementing != null)
{
    string right = incrementing .Right.ToString();
    string left = incrementing .Left.ToString();

    if (right == left + " - 1" || right == left + " + 1")
    {
        addDiagnostic(Diagnostic.Create(Rule, incrementation.GetLocation(), "Use the shorter way"));
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑: 我做了一些改变.现在始终识别递增.该程序进入CodeFix,但我的ReplaceToken与SyntaxFactory不起作用.(它现在仅用于"++"而不是" - "):

 if (node.IsKind(SyntaxKind.SimpleAssignmentExpression)) //I use a node instead of a token
 {
     var IncrementationClause = (BinaryExpressionSyntax)node;

      string left = IncrementationClause.Left.ToString();
      left = left + "++";
      string rigt = IncrementationClause.Right.ToString();

      var newIncrementationClause = IncrementationClause.ReplaceToken(SyntaxFactory.Identifier(IncrementationClause.Left.ToString()), SyntaxFactory.Identifier(left));
      newIncrementationClause = IncrementationClause.ReplaceToken(SyntaxFactory.Identifier(IncrementationClause.Right.ToString()), SyntaxFactory.Identifier(String.Empty));
      newIncrementationClause = IncrementationClause.ReplaceToken(SyntaxFactory.Identifier(IncrementationClause.OperatorToken.ToString()), SyntaxFactory.Identifier(String.Empty));

      var newRoot = root.ReplaceNode(IncrementationClause, newIncrementationClause);

      return new[] { CodeAction.Create("Changer la mise en forme", document.WithSyntaxRoot(newRoot)) };
 }
Run Code Online (Sandbox Code Playgroud)

Mal*_*loz 0

好吧,我自己找路!这是代码修复:

 if (node.IsKind(SyntaxKind.SimpleAssignmentExpression)) 
 {
        var IncrementationClause = (BinaryExpressionSyntax)node;
        var IncrementionClauseExpressionStatement = IncrementationClause.Parent;

        string right = IncrementationClause.Right.ToString(); 
        string left = IncrementationClause.Left.ToString();

        var ExpressionNew = SyntaxFactory.ExpressionStatement(IncrementationClause); 

         if (right == left + " - 1")
         {
           var BonneIncrementation = SyntaxFactory.PostfixUnaryExpression(SyntaxKind.PostDecrementExpression, IncrementationClause.Left);
           ExpressionNew = SyntaxFactory.ExpressionStatement(BonneIncrementation).WithAdditionalAnnotations(Formatter.Annotation).WithLeadingTrivia(leading).WithTrailingTrivia(trailing); 
          }
          else 
          {
            var BonneIncrementation = SyntaxFactory.PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, IncrementationClause.Left); 
            ExpressionNew = SyntaxFactory.ExpressionStatement(BonneIncrementation).WithAdditionalAnnotations(Formatter.Annotation).WithLeadingTrivia(leading).WithTrailingTrivia(trailing);
          }

          var newRoot = root.ReplaceNode(IncrementionClauseExpressionStatement, ExpressionNew);

          return new[] { CodeAction.Create("Convert in the good way of incrementing", document.WithSyntaxRoot(newRoot)) };


 }
Run Code Online (Sandbox Code Playgroud)