Cercando con un diagnostico e un CodeFix per fare un codice che trasformazione che:Roslyn C cambiamento # incrementazione
variable = variable + 1;
otherVariable = otherVariable -1;
Into:
variable++;
otherVariable--;
già fatto la diagnostica (funziona):
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"));
}
}
Edit: ho fatto qualche cambiamento. Ora l'incremento viene sempre riconosciuto. Il programma va nel CodeFix, ma il mio ReplaceToken con SyntaxFactory non funziona. (E 'ora solo per "++" e non "-"):
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)) };
}
Si dovrebbe analizzare l'albero di sintassi, non controllare la rappresentazione di stringa. 'ToString()' probabilmente non restituisce ciò che ti aspetti. – SLaks
Ho trovato la strada e ora va nel CodeFix. Ma non cambia nulla ... Ho un CodeAction, ma non viene eseguita alcuna azione. Quando eseguo il debug passo dopo passo, posso vedere che il nuovoIncrementationClause è esattamente lo stesso di IncrementationClause. Forse puoi aiutarmi :) – Maloz
Stai sostituendo un nuovo nodo appena creato, che per definizione non è nell'albero. Non dovresti mai lavorare con 'node.ToString()'; utilizzare sempre le API del nodo direttamente. – SLaks