IDEA-179557 Merge nested if's removes comment

This commit is contained in:
Tagir Valeev
2017-09-26 16:57:02 +07:00
parent 24ffaeffb0
commit 4e24c72077
6 changed files with 107 additions and 31 deletions

View File

@@ -0,0 +1,20 @@
// "Merge nested 'if's" "true"
class Test {
// IDEA-179557
public static void main(String[] args) {
long abc = 0;
do {
// comment
if (abc++ == 71 && abc++ >= 999) {
System.out.println(88);
if (abc++ < 23) {
System.err.println("Log nonsense");
}
}
} while( abc++ < 7 );
if ( abc++ < 47 ) {
System.out.println(abc);
}
}
}

View File

@@ -0,0 +1,11 @@
// "Merge nested 'if's" "true"
class Test {
public static void main(String[] args) {
/*comment1*/
// comment3
if (args.length > 0 && args[/*comment2*/0].equals("foo")) {
System.out.println("oops");
}
}
}

View File

@@ -0,0 +1,22 @@
// "Merge nested 'if's" "true"
class Test {
// IDEA-179557
public static void main(String[] args) {
long abc = 0;
do {
i<caret>f ( abc++ == 71 ) {
// comment
if ( abc++ >= 999 ) {
System.out.println(88);
if ( abc++ < 23 ) {
System.err.println( "Log nonsense" );
}
}
}
} while( abc++ < 7 );
if ( abc++ < 47 ) {
System.out.println(abc);
}
}
}

View File

@@ -0,0 +1,11 @@
// "Merge nested 'if's" "true"
class Test {
public static void main(String[] args) {
i<caret>f(args.length > 0/*comment1*/)
// comment3
if(args[/*comment2*/0].equals("foo")) {
System.out.println("oops");
}
}
}

View File

@@ -17,7 +17,7 @@ package com.siyeh.ipp.trivialif;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ipp.base.Intention;
@@ -35,39 +35,23 @@ public class MergeIfAndIntention extends Intention {
public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
final PsiJavaToken token = (PsiJavaToken)element;
final PsiIfStatement parentStatement = (PsiIfStatement)token.getParent();
if (parentStatement == null) {
return;
}
if (parentStatement == null) return;
final PsiStatement parentThenBranch = parentStatement.getThenBranch();
final PsiIfStatement childStatement = (PsiIfStatement)ControlFlowUtils.stripBraces(parentThenBranch);
if (childStatement == null) return;
final PsiExpression childCondition = childStatement.getCondition();
if (childCondition == null) {
return;
}
final String childConditionText;
if (ParenthesesUtils.getPrecedence(childCondition) > ParenthesesUtils.AND_PRECEDENCE) {
childConditionText = '(' + childCondition.getText() + ')';
}
else {
childConditionText = childCondition.getText();
}
final PsiExpression parentCondition = parentStatement.getCondition();
if (parentCondition == null) {
return;
}
final String parentConditionText;
if (ParenthesesUtils.getPrecedence(parentCondition) > ParenthesesUtils.AND_PRECEDENCE) {
parentConditionText = '(' + parentCondition.getText() + ')';
}
else {
parentConditionText = parentCondition.getText();
}
if (childCondition == null) return;
final PsiStatement childThenBranch = childStatement.getThenBranch();
if (childThenBranch == null) {
return;
}
@NonNls final String statement = "if(" + parentConditionText + "&&" + childConditionText + ')' + childThenBranch.getText();
PsiReplacementUtil.replaceStatement(parentStatement, statement);
if (childThenBranch == null) return;
final PsiExpression parentCondition = parentStatement.getCondition();
if (parentCondition == null) return;
CommentTracker ct = new CommentTracker();
final String childConditionText = ParenthesesUtils.getText(ct.markUnchanged(childCondition), ParenthesesUtils.OR_PRECEDENCE);
final String parentConditionText = ParenthesesUtils.getText(ct.markUnchanged(parentCondition), ParenthesesUtils.OR_PRECEDENCE);
@NonNls final String statement = "if(" + parentConditionText + "&&" + childConditionText + ')' + ct.text(childThenBranch);
ct.replaceAndRestoreComments(parentStatement, statement);
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ipp.trivialif;
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
public class MergeIfAndIntentionTest extends LightIntentionActionTestCase {
public void test() { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/mergeIfAnd";
}
}