使用IntelliJ结构搜索将字段及其表达式替换为新字段

arb*_*erg 5 java intellij-idea structural-search android-studio

我想使用ButterKnife for Android.我需要根据代码中其他地方的一些表达式来注释一些字段.我有这样的代码

private String myField;
...
public myClassConstructor() {
    ...
    myField = res.getString(R.string.my_string_id);
    ...
}
Run Code Online (Sandbox Code Playgroud)

我要这个

@BindString(R.string.my_string_id);
String myField;
...
public myClassConstructor() {
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

在结果中,表达式消失了,并且该字段基于旧表达式进行注释.

是否有可能在IntelliJ的结构替换中进行此类搜索和替换?当感兴趣的线不相邻并且在结构上处于不同的位置时,它似乎没有优雅地处理这种情况.我尝试将它基于类模板,并使用$ Statement $(0-unbounded occurrence),但它对我不起作用.

我意识到它实际上相对简单的使用正则表达式,当然比使用IntelliJ结构搜索来玩球更简单,但我喜欢学习我的工具,所以我仍然想知道这是否可行.

Igo*_*nko 3

虽然Search structurally它以某种方式工作并且可以进行搜索查询,但它replace structuraly是一种由“外星人”为“掠夺者”制作的工具。我希望 intelliJ 团队能够重新审视这个功能,并使其更加可用/易于理解。

我试图自己进行Replace structurally查询,但只能做Search structurally子句。

例如,我已经用 Mockito lib 完成了它。

结构搜索对话框

这里的关键点之一是将最小和最大计数设置$SomeStatement1$$SomeStatement2${0, Unlimited}

在此输入图像描述

从这一点开始,我尝试使用替换子句,但它完全搞乱了方法/字段声明/类的范围。

希望这可以帮助。