Modify a java-class with live-templates
Hi,
my plugin should create some java-code in the background. Therfore it opens the class with
JavaPsiFacade.getInstance(project).findClass(...)
Then, I have some Live-Templates which creates one or more methods inside a class. This is useful, because the editor can change this template on his own.
How can I use these templates inside my code? I've seen the TemplateManager, but this uses an editor. I only have a PsiClass and I don't want to open the file.
Please sign in to leave a comment.
The PsiClass is a PsiElement, each of which is the root of a sub-tree containing its internal elements (e.g. PsiClasses contain PsiElements -- a couple of child levels down, usually). In the tree represented by your PsiClass find the insertion point (by traversing the class' children). The insert new code using something like this:
public void addTextAfterElement(final PsiElement element, final String text) {if (element == null) return;
TextRange range = element.getTextRange();
Document document = PsiDocumentManager.getInstance(element.getProject()).getDocument(element.getContainingFile());
if (null != document)
document.replaceString(range.getEndOffset(), range.getEndOffset(), text);
}
Sorry, I don't know about using the Template Manager to retrieve and/or generate the new text.
Sorry, but this is not what I mean. In the settings, you can define Live Templates, which can be also defined in a plugin. These templates can be used during editing. But I haven't found a way to use these templates in the IntelliJ-SDK-Code. A similar code for File-Templates exists, but this is only for creating a new file.
https://github.com/JetBrains/intellij-sdk-docs
Then code_samples/live_templates
Michael, I don't understand what you need.
> Therfore it opens the class with JavaPsiFacade.getInstance(project).findClass(...)
findClass doesn't open anything, it just gives you a PsiClass object.
This:
> Then, I have some Live-Templates which creates one or more methods inside a class. This is useful, because the editor can change this template on his own.
and this:
> I don't want to open the file.
seem to be contradictory. Live templates require editor by design, I don't understand how you intend to use them without editor, it makes no sense.
Peter,
sorry, it seems, I have written it unclear. Then again:
I'm writing a plugin, which creates some code inside my java-classes. These new code-fragments (normally one or more methods) should be configured via some livetemplates. The default code for these livetemplates will be created by the plugin itself, so when adding the plugin, a standard behaviour for the livetemplates exists, but the developer is able to change this template.
Adding new livetemplates by the plugin is no problem. I can see them in the settings, can change them and can insert the code manually. The only problem is, that I don't want to add the code manually, but by the plugin itself.
For example:
My plugin should create a method in my currently edited class. Additionally, it should create some methods inside the coresponding test-class. There should be a livetemplate for the new method in the class and a second livetemplate for the new test methods.
The question is:
How can I use livetemplates inside the plugin code?
I need something like:
This kind of code generation is usually done with file templates (e.g. see templates for catch clause body, or for singleton).
If you still think you're better off with live templates, you can use com.intellij.codeInsight.template.impl.TemplateSettings#getTemplate to retrieve the template by group and key, then process the variables in some custom way (because the normal one requires editor), and then add the code either directly to the document, or create PSI from it (JavaPsiFacade.getElementFactory.create*FromText) and use PsiClass#add on the result.