罗斯林重命名Majusucle变量常量 [英] Roslyn Rename variable const in Majusucle

查看:361
本文介绍了罗斯林重命名Majusucle变量常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图将其转换:

const string maj = "variable";

const string MAJ = "variable";



我使用的是一个CodeFix诊断。

I'm using a Diagnostic with CodeFix.

我已经做了诊断:

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

}

但我不能找到该CodeFix的一种方式。以下是我已经写:

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

编辑:

于是,我开始明白它是如何作品。但是,我不知道它是如何工作的一个点。让我来解释一下:

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


 }



这就是我所做的。要存取权限变量的名称(ConstClause.Identifier.Text)我用VariableDeclaratorSyntax而不是LocalDeclarationStatementSyntax。

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.

推荐答案

欧凯,我会找到一个方式,现在它的作品!
这里是诊断:

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

}

和这里是CodeFix:

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!

这篇关于罗斯林重命名Majusucle变量常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆