Visual studio 2013 无法具体遍历语法树?

Visual studio 2013 无法具体遍历语法树?,visual-studio-2013,roslyn,vsix,Visual Studio 2013,Roslyn,Vsix,我正在尝试为集合初始值设定项及其相应的代码修复提供程序实现一个诊断分析器 错误代码: var sampleList= new List<string>(); sampleList.Add(""); sampleList.Add(""); var sampleList=newlist(); 示例列表。添加(“”); 示例列表。添加(“”); 代码修复后: var sampleList= new List<string>(){"", ""}; var-sampleLis

我正在尝试为集合初始值设定项及其相应的代码修复提供程序实现一个诊断分析器

错误代码:

var sampleList= new List<string>();
sampleList.Add("");
sampleList.Add("");
var sampleList=newlist();
示例列表。添加(“”);
示例列表。添加(“”);
代码修复后:

var sampleList= new List<string>(){"", ""};
var-sampleList=newlist(){“”“”};
但我陷入了这个问题,一旦我为LocalDeclarationStatement获取了一个节点,我就不知道是否存在从父节点获取下一个相邻节点的方法

在上图中,在分析LocalDeclarationStatement之后,我需要这两个表达式语句

对分析仪的要求

  • 识别
    LocalDeclarationStatement
    ,该集合已初始化,但不包含
    CollectionInitializerExpression
  • 查找下一行是否有在同一集合上使用
    Add
    方法的表达式语句
  • 代码修复的要求

  • 为在集合上使用
    Add
    方法的相邻表达式语句提供集合初始值设定项语法
  • 必须避免在集合上间歇性使用Add方法

  • 你不需要把混凝土块变成清单,然后检查一下吗

    var nodes = yourSyntaxTree.DescentNodes().ToList();
    
    for(var i = 0; i < nodes.Count; i++){
      var localDeclarationStatement = nodes[i] as LocalDeclarationStatement;
      if(localDeclarationStatement==null && i + 1 >= nodes.Length)
          continue;
    
      var expressionStatement = nodes[i+1] as ExpressionStatement;
      if(expressionStatement==null)
         continue;
    
      // there you have it.
    }
    
    var nodes=yourSyntaxTree.DescentNodes().ToList();
    对于(var i=0;i=nodes.Length)
    继续;
    var expressionStatement=节点[i+1]作为expressionStatement;
    if(expressionStatement==null)
    继续;
    //给你。
    }
    
    你不需要把混凝土块变成列表,然后检查一下吗

    var nodes = yourSyntaxTree.DescentNodes().ToList();
    
    for(var i = 0; i < nodes.Count; i++){
      var localDeclarationStatement = nodes[i] as LocalDeclarationStatement;
      if(localDeclarationStatement==null && i + 1 >= nodes.Length)
          continue;
    
      var expressionStatement = nodes[i+1] as ExpressionStatement;
      if(expressionStatement==null)
         continue;
    
      // there you have it.
    }
    
    var nodes=yourSyntaxTree.DescentNodes().ToList();
    对于(var i=0;i=nodes.Length)
    继续;
    var expressionStatement=节点[i+1]作为expressionStatement;
    if(expressionStatement==null)
    继续;
    //给你。
    }
    
    您可以执行以下操作:

    var declarationStatement = ...;
    var block = (BlockSyntax)declarationStatement.Parent;
    var index = block.Statements.IndexOf(declarationStatement);
    var nextStatement = block.Statements[index + 1];
    

    您可以执行以下操作:

    var declarationStatement = ...;
    var block = (BlockSyntax)declarationStatement.Parent;
    var index = block.Statements.IndexOf(declarationStatement);
    var nextStatement = block.Statements[index + 1];
    

    这个很好用!我真的很感激。我们如何删除额外的代码行(相邻的
    Add
    调用),这些代码被替换为使用相同CodeFix提供程序的集合初始值设定项语法。我能够为集合初始值设定项提供修复,但无法删除
    Add
    调用的其他连续行。@JerricLynsJohn是否尝试了
    RemoveNode
    扩展方法?@svick是的,我尝试了,但似乎没有影响根。我不确定我是否做对了
    root.RemoveNode(expression,SyntaxRemoveOptions.KeepNoTrivia)
    @JerricLynsJohn就像Roslyn中的所有东西一样,
    RemoveNode
    不会变异原始树,而是返回一个新树。所以您需要类似于
    root=root.RemoveNode(…)。这工作非常好!我真的很感激。我们如何删除额外的代码行(相邻的
    Add
    调用),这些代码被替换为使用相同CodeFix提供程序的集合初始值设定项语法。我能够为集合初始值设定项提供修复,但无法删除
    Add
    调用的其他连续行。@JerricLynsJohn是否尝试了
    RemoveNode
    扩展方法?@svick是的,我尝试了,但似乎没有影响根。我不确定我是否做对了
    root.RemoveNode(expression,SyntaxRemoveOptions.KeepNoTrivia)
    @JerricLynsJohn就像Roslyn中的所有东西一样,
    RemoveNode
    不会变异原始树,而是返回一个新树。所以您需要类似于
    root=root.RemoveNode(…)