简体   繁体   English

AST eclipse,尝试创建一个InfixExpression

[英]AST eclipse, trying to create an InfixExpression

I am working on an Eclipse code formatting plugin using the AST. 我正在使用AST开发Eclipse代码格式插件。

I am trying to create an InfixExpression using this code: 我正在尝试使用以下代码创建InfixExpression:

public static InfixExpression makeInfixExpression(final AST t, final ASTRewrite r, final InfixExpression.Operator o,
        final Expression left, final Expression right) {
    final InfixExpression $ = t.newInfixExpression();
    $.setOperator(o);
    $.setRightOperand(right.getParent() == null ? right : (Expression) r.createMoveTarget(right));
    $.setLeftOperand(left.getParent() == null ? left : (Expression) r.createMoveTarget(left));
    return $;
}

with this command: 使用此命令:

makeInfixExpression(t, r, InfixExpression.Operator.PLUS, asgnThen.getRightHandSide(), otherAsgn)

Declarations: 声明:

final Assignment asgnThen, final Expression otherAsgn;

But the InfixExpression I am getting back from this function is 0+0 instead of 3 + 4 according to the parameters I sent. 但是根据我发送的参数,我从该函数返回的InfixExpression是0 + 0而不是3 + 4。 after some debugging I saw that it gets messed up in the createMoveTarget part but if i replace it with just right and left it throw an exception.... I also noticed that if I use t.newNumberLiteral to create 2 new lietrals and define the to be the value of what asgnThen.getRightHandSide and otherAsgn is the function returns a proper InfixExpression but the problem is it doesnt have to be a NumberLiteral so I cant count on that. 经过一些调试后,我发现它在createMoveTarget部分中被弄乱了,但是如果我用左右替换它,则会抛出异常。...我还注意到,如果我使用t.newNumberLiteral创建2个新的笔尖并定义作为asgnThen.getRightHandSide和otherAsgn的值,该函数将返回适当的InfixExpression,但问题是它不一定是NumberLiteral,所以我不能指望它。 when the plugin lets me see the preview is shows "3 + " and no 0+0.... wierd stuff going on. 当插件让我看到预览时,显示的是“ 3 +”,没有0 + 0 ....发生了一些奇怪的事情。

Any suggestions? 有什么建议么?

Thanks! 谢谢!

Found the problem, needed to change the MoveTarget to CopyTarget. 找到了问题,需要将MoveTarget更改为CopyTarget。 I trie to "Move" the expression to 2 places in the conditional but "Move" here means actually moving it and not copying it which means I tried to move the same expression to 2 places but you only have 1 of him so you have to copy it with CopyTarget 我尝试在条件中将表达式“移动”到2个位置,但是“移动”在这里意味着实际上将其移动而不是复制它,这意味着我试图将相同的表达式移动到2个位置,但是您只有一个他,所以您必须用CopyTarget复制它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM