mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
ConvertSwitchToIfIntention: better comments handling
This commit is contained in:
@@ -182,10 +182,11 @@ public class ConvertSwitchToIfIntention implements IntentionAction {
|
||||
dumpBody(defaultBranch, sb, commentTracker);
|
||||
PsiBlockStatement defaultBody = (PsiBlockStatement)factory.createStatementFromText(sb.toString(), switchStatement);
|
||||
if (!BlockUtils.containsConflictingDeclarations(Objects.requireNonNull(switchStatement.getBody()), parent)) {
|
||||
commentTracker.grabComments(switchStatement);
|
||||
BlockUtils.inlineCodeBlock(switchStatement, defaultBody.getCodeBlock());
|
||||
}
|
||||
else {
|
||||
switchStatement.replace(defaultBody);
|
||||
commentTracker.replace(switchStatement, defaultBody);
|
||||
}
|
||||
commentTracker.insertCommentsBefore(addedIf);
|
||||
if (ifStatementText.equals(";")) {
|
||||
@@ -334,6 +335,16 @@ public class ConvertSwitchToIfIntention implements IntentionAction {
|
||||
private static void dumpBody(SwitchStatementBranch branch, @NonNls StringBuilder out, CommentTracker commentTracker) {
|
||||
final List<PsiElement> bodyStatements = branch.getBodyElements();
|
||||
out.append('{');
|
||||
if (!bodyStatements.isEmpty()) {
|
||||
PsiElement firstBodyElement = bodyStatements.get(0);
|
||||
PsiElement prev = PsiTreeUtil.skipWhitespacesAndCommentsBackward(firstBodyElement);
|
||||
if (prev instanceof PsiSwitchLabelStatement) {
|
||||
PsiExpression value = ((PsiSwitchLabelStatement)prev).getCaseValue();
|
||||
if (value != null) {
|
||||
out.append(CommentTracker.commentsBetween(value, firstBodyElement));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (PsiElement element : branch.getPendingDeclarations()) {
|
||||
if (ReferencesSearch.search(element, new LocalSearchScope(bodyStatements.toArray(PsiElement.EMPTY_ARRAY))).findFirst() != null) {
|
||||
if (element instanceof PsiVariable) {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Replace 'switch' with 'if'" "true"
|
||||
class Foo {
|
||||
Object foo(int x) {
|
||||
if (x == 1) { // not needed
|
||||
return null;
|
||||
} else if (x == 2) {// not needed
|
||||
return null;
|
||||
} else if (x == 4) {
|
||||
return "foo";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
// "Replace 'switch' with 'if'" "true"
|
||||
class X {
|
||||
public void doSomething( String value) {
|
||||
//comment1
|
||||
//comment4
|
||||
//comment6
|
||||
//comment7
|
||||
if ("case1".equals(value)) {//comment2
|
||||
if ("case1".equals(value)) {//comment1
|
||||
//comment2
|
||||
//comment3
|
||||
} else if ("case2".equals(value)) {//comment5
|
||||
} else if ("case2".equals(value)) {//comment4
|
||||
//comment5
|
||||
}//comment8
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Replace 'switch' with 'if'" "true"
|
||||
class Foo {
|
||||
Object foo(int x) {
|
||||
sw<caret>itch (x) {
|
||||
case 1: // not needed
|
||||
return null;
|
||||
case 2:// not needed
|
||||
return null;
|
||||
case 4:
|
||||
return "foo";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,10 @@
|
||||
class Main {
|
||||
static void fff(int x) {
|
||||
if (x == 5) {
|
||||
//1
|
||||
//2
|
||||
//3
|
||||
//4
|
||||
System.out.println("five-ten-fifteen"); //5
|
||||
System.out.println("six"); //6
|
||||
System.out.println("seven"); //7
|
||||
|
||||
Reference in New Issue
Block a user