/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/MergeIfAndIntention.java

https://bitbucket.org/nbargnesi/idea · Java · 79 lines · 59 code · 5 blank · 15 comment · 12 complexity · 121376665210c28f4ca25c5c22c3b212 MD5 · raw file

  1. /*
  2. * Copyright 2003-2005 Dave Griffith
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.siyeh.ipp.trivialif;
  17. import com.intellij.psi.*;
  18. import com.intellij.util.IncorrectOperationException;
  19. import com.siyeh.ipp.base.Intention;
  20. import com.siyeh.ipp.base.PsiElementPredicate;
  21. import com.siyeh.ipp.psiutils.ConditionalUtils;
  22. import com.siyeh.ipp.psiutils.ParenthesesUtils;
  23. import org.jetbrains.annotations.NotNull;
  24. import org.jetbrains.annotations.NonNls;
  25. public class MergeIfAndIntention extends Intention {
  26. @NotNull
  27. public PsiElementPredicate getElementPredicate() {
  28. return new MergeIfAndPredicate();
  29. }
  30. public void processIntention(PsiElement element)
  31. throws IncorrectOperationException {
  32. final PsiJavaToken token =
  33. (PsiJavaToken)element;
  34. final PsiIfStatement parentStatement =
  35. (PsiIfStatement)token.getParent();
  36. if (parentStatement == null) {
  37. return;
  38. }
  39. final PsiStatement parentThenBranch = parentStatement.getThenBranch();
  40. final PsiIfStatement childStatement =
  41. (PsiIfStatement)ConditionalUtils.stripBraces(parentThenBranch);
  42. final PsiExpression childCondition = childStatement.getCondition();
  43. if (childCondition == null) {
  44. return;
  45. }
  46. final String childConditionText;
  47. if (ParenthesesUtils.getPrecedence(childCondition)
  48. > ParenthesesUtils.AND_PRECEDENCE) {
  49. childConditionText = '(' + childCondition.getText() + ')';
  50. }
  51. else {
  52. childConditionText = childCondition.getText();
  53. }
  54. final PsiExpression parentCondition = parentStatement.getCondition();
  55. if (parentCondition == null) {
  56. return;
  57. }
  58. final String parentConditionText;
  59. if (ParenthesesUtils.getPrecedence(parentCondition)
  60. > ParenthesesUtils.AND_PRECEDENCE) {
  61. parentConditionText = '(' + parentCondition.getText() + ')';
  62. }
  63. else {
  64. parentConditionText = parentCondition.getText();
  65. }
  66. final PsiStatement childThenBranch = childStatement.getThenBranch();
  67. if (childThenBranch == null) {
  68. return;
  69. }
  70. @NonNls final String statement = "if(" + parentConditionText + "&&" +
  71. childConditionText + ')' + childThenBranch.getText();
  72. replaceStatement(statement, parentStatement);
  73. }
  74. }