Roslyn Rename variable const in Majusucle

我的梦境 提交于 2019-12-08 12:41:21

问题


Trying to convert that:

const string maj = "variable";

in

const string MAJ = "variable";

I'm using a Diagnostic with CodeFix.

I've already done the Diagnostic:

var localDeclarationConst = node as LocalDeclarationStatementSyntax;
if (localDeclarationConst != null &&
    localDeclarationConst.Modifiers.Any(SyntaxKind.ConstKeyword)
    )
{
    foreach (VariableDeclaratorSyntax variable in localDeclarationConst.Declaration.Variables)
    {
        var symbol = model.GetDeclaredSymbol(variable);
        if (symbol != null)
        {
            string varName = symbol.Name;
            if (!varName.Equals(varName.ToUpper()))
            {
                addDiagnostic(Diagnostic.Create(Rule, localDeclarationConst.GetLocation(), "Les constantes doivent être en majusucle"));
            }
        }
    }

}

But I cannot find a way for the CodeFix. Here is what I already wrote:

if (token.IsKind(SyntaxKind.ConstKeyword))
{
    var ConstClause = (LocalDeclarationStatementSyntax)token.Parent;
    var test = ConstClause.GetText();
    var newConstClause = ConstClause.With //What with this With ??


    var newRoot = root.ReplaceNode(ConstClause, newConstClause);

    return new[] { CodeAction.Create("Mettre en maj", document.WithSyntaxRoot(newRoot)) };
}

As you can see, I'm looking for something that I can use with the .With

Edit:

So, I begin to understand how it works. But there is a point that I cannot know how it works. Let me explain:

if (token.IsKind(SyntaxKind.ConstKeyword))
{
   var ConstClause = (VariableDeclaratorSyntax)token.Parent;
   var test = ConstClause.Identifier.Text;
   var newConstClause = ConstClause.ReplaceToken(SyntaxFactory.Identifier(test), SyntaxFactory.Identifier(test.ToUpperInvariant()));
   var newRoot = root.ReplaceNode(ConstClause, newConstClause);

   return new[] { CodeAction.Create("Make upper", document.WithSyntaxRoot(newRoot)) };


 }

Here it's what I've done. To acces to the name of the variable (ConstClause.Identifier.Text) I use a VariableDeclaratorSyntax instead of the LocalDeclarationStatementSyntax.

But it doesn't work. What does I have to use?? It will be very helpful, because I will know how to change the name of my variables. And I need that.


回答1:


Try ReplaceToken() instead of a With method.

Also, in your diagnostic, you could just use VariableDeclarator.Identifier instead of forcing the symbol to be created with GetDeclaredSymbol.




回答2:


Okey, I'll find a way a now it works! Here is the Diagnostic:

var localDeclarationConst = node as LocalDeclarationStatementSyntax;
if (localDeclarationConst != null &&
    localDeclarationConst.Modifiers.Any(SyntaxKind.ConstKeyword)
    )
{
foreach (VariableDeclaratorSyntax variable in localDeclarationConst.Declaration.Variables)
{
     string varName = variable.Identifier.Text;
     if (!varName.Equals(varName.ToUpper()))
     {
        addDiagnostic(Diagnostic.Create(Rule, variable.GetLocation(), "Les constantes doivent être en majusucle"));
     }

}

And here is the CodeFix:

 var root = await document.GetSyntaxRootAsync(cancellationToken); (root)
 var token = root.FindToken(span.Start); 
 var node = root.FindNode(span);

 if (node.IsKind(SyntaxKind.VariableDeclarator))
 {
   if (token.IsKind(SyntaxKind.IdentifierToken))
   {
        var variable = (VariableDeclaratorSyntax)node;
        string newName = variable.Identifier.ValueText;
        string NameDone = String.Empty;
        for (int i = 0; i < newName.Length; i++)
        {
             NameDone = NameDone.ToString() + char.ToUpper(newName[i]);
        }

        var leading = variable.Identifier.LeadingTrivia;
        var trailing = variable.Identifier.TrailingTrivia;

        VariableDeclaratorSyntax newVariable = variable.WithIdentifier(SyntaxFactory.Identifier(leading, NameDone, trailing));

        var newRoot = root.ReplaceNode(variable, newVariable);
        return new[] { CodeAction.Create("Make upper", document.WithSyntaxRoot(newRoot)) };
   }
}

If something looks wrong tell me, but I tried it and it works!



来源:https://stackoverflow.com/questions/23312525/roslyn-rename-variable-const-in-majusucle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!