mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-57936 Not-compiling code after reformating in Java
Avoiding creating code block for replacing expressions
This commit is contained in:
@@ -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) {
|
||||
|
||||
+1
-2
@@ -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" +
|
||||
"}"
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user