Roslyn C#递增变化

本文关键字:变化 Roslyn | 更新日期: 2023-09-27 18:00:35

尝试使用Diagnostic和CodeFix来生成转换代码:

variable = variable + 1;
otherVariable = otherVariable -1;

进入:

variable++;
otherVariable--;

已经完成诊断(有效):

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"));
    }
}

编辑:我做了一些改变。现在,增长总是被认可的。程序进入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)) };
 }

Roslyn C#递增变化

尝试使用诊断的源span,而不是span参数。此外,跨度中第一个标记的父级不一定是您要查找的二进制表达式。您必须使用.AcestorsAndSelf()搜索父链,或者使用FindNode()查找与跨度最匹配的节点。

此外,您还要检查一个令牌,看看它是否具有节点的类型。通常,所有令牌类型都以token或Keyword结尾。您需要找到具有SyntaxKind的节点。

好的,我自己找到路了!这是CodeFix:

 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)) };

 }