如何创建通过添加和删除从老根检索节点一个新的根? [英] How do I create a new root by adding and removing nodes retrieved from the old root?

查看:163
本文介绍了如何创建通过添加和删除从老根检索节点一个新的根?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个code修正,改变这样的:

 如果(obj是MyClass的)
{
    VAR castedObj的obj = MyClass的作为;
}

这个:

  VAR castedObj的obj = MyClass的作为;
如果(castedObj!= NULL)
{
}

这意味着我必须做3件事:


  • 更改如果语句中的条件。

  • 将铸造权的如果上述声明。

  • 删除在体内的声明。

到目前为止,我所有的尝试都被困在我得到这些东西顶多2工作。

我相信会出现这个问题,因为你基本上在同一水平上2语法节点。这样,使得改变到其中的一个无效的另一个的位置。或类似的东西。长话短说:我无论是管理,​​如果语句复制外变量赋值,或者我设法改变条件+删除变量赋值。千万不要所有3。

我将如何解决这个问题?

有关良好的措施,这是我的code这改变的条件,并删除分配:

  VAR newIfStatement = ifStatement.RemoveNode(
                                   variableDeclaration,
                                   SyntaxRemoveOptions.KeepExteriorTrivia);
newIfStatement = newIfStatement.ReplaceNode(newIfStatement.Condition,newCondition);VAR ifParent = ifStatement.Parent;
VAR newParent = ifParent.ReplaceNode(ifStatement,newIfStatement);
newParent = newParent.InsertNodesBefore(
                           newIfStatement,
                           新的[] {} variableDeclaration)
                           .WithAdditionalAnnotations(Formatter.Annotation);VAR newRoot = root.ReplaceNode(ifParent,newParent);


解决方案

您是否看了<一个href=\"http://source.roslyn.$c$cplex.com/#Microsoft.$c$cAnalysis.Workspaces/Editing/DocumentEditor.cs,324ac2311809b8f7\"><$c$c>DocumentEditor上课吗?与修改的语法处理时,特别是当应用到树中的变化可能会导致失效的问题是非常有用的。该操作是pretty一样一样的,你已经中定义的,只要用 DocumentEditor 方法来代替,看看是否有帮助。我无法验证如果ATM解决您的问题,但我认为它在过去解决了类似的问题,我一次。我会,如果我以后可以测试它。

这样的事会做到这一点:

  VAR编辑=等待DocumentEditor.CreateAsync(文件);
editor.RemoveNode(variableDeclaration);
editor.ReplaceNode(ifStatement.Condition,newCondition);
editor.InsertBefore(ifStatement,
     新的[] {variableDeclaration.WithAdditionalAnnotations(Formatter.Annotation)});变种新建文档= editor.GetChangedDocument();

I am creating a Code Fix that changes this:

if(obj is MyClass)
{
    var castedObj = obj as MyClass;
}

into this:

var castedObj = obj as MyClass;
if(castedObj != null)
{
}

This means I have to do 3 things:

  • Change the condition in the if statement.
  • Move the casting right above the if statement.
  • Remove the statement in the body.

So far, all my attempts have stranded me at getting at most 2 of these things to work.

I believe this problem occurs because you basically have 2 syntax nodes on the same level. As such, making a change to one of them invalidates the location of the other one. Or something like that. Long story short: I either manage to copy the variable assignment outside the if statement, or I manage to change the condition + remove the variable assignment. Never all 3.

How would I solve this?

For good measure, here is my code which changes the condition and removes the assignment:

var newIfStatement = ifStatement.RemoveNode(
                                   variableDeclaration,
                                   SyntaxRemoveOptions.KeepExteriorTrivia);
newIfStatement = newIfStatement.ReplaceNode(newIfStatement.Condition, newCondition);

var ifParent = ifStatement.Parent;
var newParent = ifParent.ReplaceNode(ifStatement, newIfStatement);
newParent = newParent.InsertNodesBefore(
                           newIfStatement, 
                           new[] { variableDeclaration })
                           .WithAdditionalAnnotations(Formatter.Annotation);

var newRoot = root.ReplaceNode(ifParent, newParent);

解决方案

Have you looked at the DocumentEditor class ? It is very useful when dealing with modifying syntax, especially when the changes that are applied to the tree might cause invalidation problems. The operations are pretty much the same as the ones you already have defined, just use the DocumentEditor methods instead and see if that helps. I can't verify if that solves your problem ATM, but I think it solved the a similar problem for me once in the past. I'll test it out later if I can.

Something like this will do it:

var editor = await DocumentEditor.CreateAsync(document);
editor.RemoveNode(variableDeclaration);
editor.ReplaceNode(ifStatement.Condition, newCondition);
editor.InsertBefore(ifStatement, 
     new[] { variableDeclaration.WithAdditionalAnnotations(Formatter.Annotation) });

var newDocument = editor.GetChangedDocument();

这篇关于如何创建通过添加和删除从老根检索节点一个新的根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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