如何验证是否在接口文件上调用了 IntentionAction? IntelliJ 平台 SDK

How to verify that IntentionAction was called on an interface file? IntelliJ Platform SDK

本人是IntelliJ Platform SDK新手,插件开发

我想实现 IntentionAction 只有当光标设置在接口声明上时在编辑器中按 Alt+Enter 才可用。 换句话说,预期行为与出现 "Implement interface" 意图操作时的情况一样。

在编辑器中放置:

public interface SomeInterfaceNameDeclaration {

在您的 IntentionAction.isAvailable() 方法中,您收到一个 Editor 和一个 PsiFile。您可以使用 file.findElementAt(editor.getCaretModel().getOffset()) 在插入符号处查找 PSI 元素,然后使用 PsiTreeUtil.getParentOfType() 查找所需类型的元素。有关详细信息,请参阅 the documentation

另外,可以参考"Implement interface"动作的source code作为例子

有关使用 ConditionalOperatorConvertor.isAvailable() 中的 Psi 树确定是否启用或禁用意图操作功能的示例,请参阅代码示例 conditional_operator_intention