1

我正在查看 PhpStorm 中的一些遗留代码,并且代码库中有一些实例,其中特定函数调用被 if 语句中的另一个函数调用包装,例如

if (thisIsTrue($param1, $param2)) {
    // possibly some function calls above
    callThisFunction($paramA, $paramB);
    // possibly some function calls below
}

我仅使用基本的 Linux find/grep 命令来突出显示感兴趣的文件就取得了一些成功,但我希望能够创建一个结构搜索检查,该检查可以找到callThisFunction未被特定 if 语句包装的实例。

有谁知道香草 IntelliJ 搜索模板功能是否可以做到这一点?

4

1 回答 1

1

简短的回答是(仍然)不。

如果您复制现有模板“Logging without if”(在 Operators 类别中)或导入此 XML,则可以定义“不在 if 语句内”模板约束:

<searchConfiguration name="Logging without if" text="LOG.debug($Argument$);" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default">
  <constraint name="__context__" negateWithin="true" within="statement in if" contains="" />
  <constraint name="Argument" minCount="0" maxCount="2147483647" within="" contains="" />
</searchConfiguration>

但是,当 if 正文有多个语句时它不匹配,并且当我将文本修改为$Statement1$; log.tracef($Argument$); $Statement2$;对语句使用与参数相同的计数约束时,我的 IntelliJ 挂起。

您可以改为运行 2 次搜索:一次只搜索callThisFunction($paramA, $paramB);,一次搜索整个模板,然后比较结果。

如果您将结构搜索转换为检查,然后从命令行运行这些检查(https://www.jetbrains.com/help/idea/command-line-code-inspector.html) ,比较结果应该会更容易

有关“内部”约束的更多信息,请参阅 https://ijnspector.wordpress.com/2020/06/11/contained-in-constraints/

于 2021-07-16T08:09:26.463 回答