IntelliJ plugin development - add whitespace manually

Hi,

I'm working on a plugin which generates code. I would like to add an annotation to a generated class. I use PsiElementFactory to create the annotation and then I call 

psiClass.addBefore(annotation, psiClass.firstChild)

The command succeed, however, the code looks like this:

@MyAnnotation public SomeClass{
}

So the annotation is on the same line as the class declaration. And calling

codeStyleManager.reformat(psiClass)

doesn't change this (code style is set properly).

So the question is how can I do that? How can I add a line break after the annotation or before the class?

I also generate methods where I also would like to add additional line break between lines of code. For example:

public void run() {
method1()

method2()
}

Thanks for help in advance.

Martin

1
2 comments

Okay, I found out the annotation part.

 

I've just needed to call

modifierList?.addAnnotation(...)

on the psiClass.

 

However, I'm still interested how I can add whitespace (especially line break) where I find it necessary.

0

I think you want to look at PsiParserFacade.

```

protected PsiElement afterAddHandler(PsiElement element, PsiElement anchor) {
final PsiElement newLineNode =
PsiParserFacade.SERVICE.getInstance(element.getProject()).createWhiteSpaceFromText("\n\n");
anchor.getParent().addBefore(newLineNode, anchor);
return anchor;
}
```
0

Please sign in to leave a comment.