IDEA-57936 Not-compiling code after reformating in Java

Avoiding creating code block for replacing expressions
This commit is contained in:
Denis Zhdanov
2010-08-27 13:16:04 +04:00
parent e5161c0e28
commit 51d7fc3713
2 changed files with 3 additions and 6 deletions
@@ -113,8 +113,6 @@ public class BraceEnforcer extends JavaJspRecursiveElementVisitor {
final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
String oldText = blockCandidate.getText();
StringBuilder buf = new StringBuilder(oldText.length() + 5);
// There is a possible case that target block to wrap ends with single-line comment. Example:
// if (true) i = 1; // Cool assignement
// We can't just surround target block of code with curly braces because the closing one will be treated as comment as well.
@@ -122,17 +120,17 @@ public class BraceEnforcer extends JavaJspRecursiveElementVisitor {
int lastLineFeedIndex = oldText.lastIndexOf("\n");
lastLineFeedIndex = Math.max(0, lastLineFeedIndex);
int lastLineCommentIndex = oldText.indexOf("//", lastLineFeedIndex);
StringBuilder buf = new StringBuilder(oldText.length() + 5);
buf.append("{ ").append(oldText);
if (lastLineCommentIndex >= 0) {
buf.append("\n");
}
buf.append(" }");
final int oldTextLength = statement.getTextLength();
try {
CodeEditUtil.replaceChild(SourceTreeToPsiMap.psiElementToTree(statement),
SourceTreeToPsiMap.psiElementToTree(blockCandidate),
SourceTreeToPsiMap.psiElementToTree(factory.createCodeBlockFromText(buf.toString(), null)));
SourceTreeToPsiMap.psiElementToTree(factory.createStatementFromText(buf.toString(), null)));
CodeStyleManager.getInstance(statement.getProject()).reformat(statement, true);
}
catch (IncorrectOperationException e) {
@@ -84,8 +84,7 @@ public class JavaFormatterBracesTest extends AbstractJavaFormatterTest {
"if (true) {\n" +
" i = 1; // Cool if\n" +
"}\n" +
"else {\n" +
"} else {\n" +
" i = 2;\n" +
"}"
);