【问题标题】:How to get inspections or static code analysis errors in intellij Idea from Intellij Idea Plugin programmatically?如何以编程方式从 Intellij Idea 插件获取 intellij Idea 中的检查或静态代码分析错误?
【发布时间】:2016-11-09 05:08:10
【问题描述】:
我想为 intellij idea 创建一个插件,并且我想获得检查或代码分析错误。默认情况下,intellij 将显示这些代码检查,例如错误、死代码或未使用的组件。所以我想获得那些以编程方式对我的插件进行检查。我能够从我的插件中创建一个工具按钮并获取代码。过程是,
- 使用一些操作和工具为 intellij Idea 创建插件
菜单。
- 运行插件,在
intellijIdea
- 在编辑器的新 intellij 实例中编写了一些 java 代码
-->因为它会显示
检查或错误
所以我想对我的插件进行这些检查。我该怎么做?
【问题讨论】:
标签:
java
intellij-idea
intellij-plugin
static-code-analysis
【解决方案1】:
获取给定文件集中检查错误的最简单 API 是 CodeSmellDetector:
CodeSmellDetector.getInstance(project).findCodeSmells(files);
【解决方案2】:
如果你知道你需要什么确切的检查,你可以这样得到:
PhpUnusedAliasInspection inspection = new PhpUnusedAliasInspection();
InspectionManager manager = InspectionManager.getInstance(psiFile.getProject());
List<ProblemDescriptor> checked = inspection.processFile(psiFile, manager);
System.out.println("checked " + checked);
输出:
checked [Import 'Illuminate\Database\Eloquent\Model' is never used]
其中PhpUnusedAliasInspection 可以是任何扩展LocalInspectionTool 的类(您可以通过打开反编译的LocalInspectionTool 类并单击名称旁边的子类 圆圈将它们全部列出)。 p>