简体   繁体   English

Intellij Idea插件:Psi类

[英]Intellij idea plugin: Psi Classes

I am developing a plugin in Intellij idea. 我正在开发Intellij想法的插件。 I am stuck at inserting a piece of code after a statement. 我被困在声明后插入一段代码。

For eg: 例如:

mHelper.launchPurchaseflow(str,str,str);

I need to find the element launchPurchaseflow and add a piece of code after that statement.I used PsiElement and string matcher to get to the corresponding string. 我需要找到元素launchPurchaseflow并在该语句后添加一段代码。我使用了PsiElement和字符串匹配器来获取相应的字符串。

Now my PsiElement = mhelper.launchPurchaseFlow . 现在我的PsiElement = mhelper.launchPurchaseFlow If I use 如果我用

psiClass.addAfter(newElement, PsiElement.getContext());

It is only trying to add after the closing paranthesis.So its pop out my new element as Incorrect Statement. 它只是试图在结束括号之后添加。因此它弹出了我的新元素作为Incorrect Statement。

For eg: 例如:

Its adding at position 其添加位置

mHelper.launchPurchaseflow(str,str,str)//newElement;

But I need to add at 但是我需要添加

mHelper.launchPurchaseflow(str,str,str); // new Element.

It's hard to give you a correct answer since you're not showing us how you are inserting the new text, but let's say you have a variable PsiElement myElement that contains the expression mHelper.launchPurchaseflow(str,str,str) (without the semicolon). 很难给您正确的答案,因为您没有向我们展示如何插入新文本,但是假设您有一个变量PsiElement myElement ,其中包含表达式 mHelper.launchPurchaseflow(str,str,str) (不带分号) )。 If you call getParent() on that element, you will likely have access to the statement representing the method call: 如果在该元素上调用getParent() ,则可能可以访问表示方法调用的语句

// Should represent "mHelper.launchPurchaseflow(str,str,str);" with the semicolon
PsiElement parent = myElement.getParent();

If you add your code after this node, it should produce a valid statement. 如果将代码添加到此节点之后,它将产生一个有效的语句。

Alternatively, if getParent() does not return a PsiStatement and you want to be certain to have the full statement, you can do this: 另外,如果getParent()没有返回PsiStatement并且您希望确定具有完整的语句,则可以执行以下操作:

PsiElement statement = PsiTreeUtil.getParentOfType(myElement, PsiStatement.class);

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

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