PsiBlock inserting linebreak
Answered
I'm creating a new PsiIfStatement where the Else-Branch is supposed to only contain a single comment:
final PsiBlockStatement elseBlockStatement = (PsiBlockStatement) factory.createStatementFromText("{}", null);
final PsiElement elseCodeBlock = elseBlockStatement.getCodeBlock();
PsiComment comment = factory.createCommentFromText("// my comment", null);
elseCodeBlock.add(comment);
newIfStatement.setElseBranch(elseBlockStatement);
This produces the following code:
if (...) {
...
} else {// my comment
}
However I want the comment to be in its own line, like so:
if (...) {
...
} else {
// my comment
}
I tried to use PsiWhiteSpaceImpl like this:
PsiWhiteSpaceImpl linebreak = new PsiWhiteSpaceImpl("\n");
elseCodeBlock.add(linebreak);
PsiComment comment = factory.createCommentFromText("// my comment", null);
elseCodeBlock.add(comment);
But this produces an exception when adding the linebreak to elseCodeBlock:
java.lang.IllegalArgumentException: Argument for @NotNull parameter 'manager' of com/intellij/psi/impl/source/DummyHolderFactory.createHolder must not be null
How can I correctly add a linebreak to the codeblock?
Please sign in to leave a comment.
I found out that this can basically be accomplished like this:
PsiComment comment = factory.createCommentFromText("// my comment", null);
PsiElement addedComment = elseCodeBlock.add(comment);
PsiComment comment2 = factory.createCommentFromText("// my comment2", null);
PsiElement addedComment2 = elseCodeBlock.add(comment2);
PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project);
PsiElement lineBreakTab = parserFacade.createWhiteSpaceFromText("\n\t");
elseCodeBlock.addBefore(lineBreakTab, addedComment);
elseCodeBlock.addBefore(lineBreakTab, addedComment2);
However in this case, only the first comment is correctly indented, so it looks like this:
if (...) {
...
} else {
// my comment
// my comment2
}
I could not yet find out how to indent the second comment correctly too, any help would be much appreciated.
Moribund,
Adding comments/code with proper handling of the leading white characters may be tricky i.e., when a user has defined different tab indent width (2 spaces/4 spaces).
I'd suggest you using the ReformatCodeProcessor after inserting your comments to achieve the expected code formatting.
It seems like the ReformatCodeProcessor is automatically used, but the indentation is only applied correctly if there is a space or a tab in front of the comment.
This is a minimum working example of my code:
This produces the if-statement in the following code:
public void method() {...
if (true) {
// my comment 1
// my comment 2
}
}
Here, the first comment is automatically indented correctly if the whitespace before it has a space or a tab after the newline ("\n ").
But the second comment is not indented at all and it seems like the space at the end of the added whitespace is automatically removed instead?
I still couldn't find a solution for this problem, is this the wrong approach for adding multiple single-line comments?