summaryrefslogtreecommitdiff
path: root/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithUtil.java
blob: 046a9b7cea0a080e0141f767f536b91aeed65c10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

/*
 * Copyright 2000-2009 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.intellij.codeInsight.generation.surroundWith;

import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.impl.source.tree.JavaJspElementType;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;

public class SurroundWithUtil {
  
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.generation.surroundWith.SurroundWithUtil");

  private SurroundWithUtil() {
  }

  static PsiElement[] moveDeclarationsOut(PsiElement block, PsiElement[] statements, boolean generateInitializers) {
    try{
      PsiManager psiManager = block.getManager();
      PsiElementFactory factory = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory();
      ArrayList<PsiElement> array = new ArrayList<PsiElement>();
      for (PsiElement statement : statements) {
        if (statement instanceof PsiDeclarationStatement) {
          PsiDeclarationStatement declaration = (PsiDeclarationStatement)statement;
          if (needToDeclareOut(statements, declaration)) {
            PsiElement[] elements = declaration.getDeclaredElements();
            for (PsiElement element : elements) {
              PsiVariable var = (PsiVariable)element;
              PsiExpression initializer = var.getInitializer();
              if (initializer != null) {
                String name = var.getName();
                PsiExpressionStatement assignment = (PsiExpressionStatement)factory.createStatementFromText(name + "=x;", null);
                assignment = (PsiExpressionStatement)CodeStyleManager.getInstance(psiManager.getProject()).reformat(assignment);
                PsiAssignmentExpression expr = (PsiAssignmentExpression)assignment.getExpression();
                expr.getRExpression().replace(initializer);
                assignment = (PsiExpressionStatement)block.addAfter(assignment, declaration);
                array.add(assignment);
              }
            }
            PsiDeclarationStatement newDeclaration;
            if (!array.isEmpty()) {
              PsiElement firstStatement = array.get(0);
              newDeclaration = (PsiDeclarationStatement)block.addBefore(declaration, firstStatement);
              declaration.delete();
            }
            else {
              newDeclaration = declaration;
            }
            elements = newDeclaration.getDeclaredElements();
            for (PsiElement element1 : elements) {
              PsiVariable var = (PsiVariable)element1;
              PsiExpression initializer = var.getInitializer();
              if (initializer != null) {
                if (!generateInitializers || var.hasModifierProperty(PsiModifier.FINAL)) {
                  initializer.delete();
                }
                else {
                  String defaultValue = PsiTypesUtil.getDefaultValueOfType(var.getType());
                  PsiExpression expr = factory.createExpressionFromText(defaultValue, null);
                  initializer.replace(expr);
                }
              }
            }
            continue;
          }
        }
        array.add(statement);
      }
      return PsiUtilCore.toPsiElementArray(array);
    }
    catch(IncorrectOperationException e){
      LOG.error(e);
      return statements;
    }
  }

  private static boolean needToDeclareOut(PsiElement[] statements, PsiDeclarationStatement statement) {
    PsiElement[] elements = statement.getDeclaredElements();
    PsiElement lastStatement = statements[statements.length - 1];
    int endOffset = lastStatement.getTextRange().getEndOffset();
    for (PsiElement element : elements) {
      if (element instanceof PsiVariable) {
        GlobalSearchScope projectScope = GlobalSearchScope.projectScope(element.getProject());
        PsiReference[] refs = ReferencesSearch.search(element, projectScope, false).toArray(PsiReference.EMPTY_ARRAY);
        if (refs.length > 0) {
          PsiReference lastRef = refs[refs.length - 1];
          if (lastRef.getElement().getTextOffset() > endOffset) {
            return true;
          }
        }
      }
    }
    return false;
  }

  public static TextRange getRangeToSelect (@NotNull PsiCodeBlock block) {
    PsiElement first = block.getFirstBodyElement();
    if (first instanceof PsiWhiteSpace) {
      first = first.getNextSibling();
    }
    if (first == null) {
      int offset = block.getTextRange().getStartOffset() + 1;
      return new TextRange(offset, offset);
    }
    PsiJavaToken rBrace = block.getRBrace();
    PsiElement last = rBrace.getPrevSibling();
    if (last instanceof PsiWhiteSpace) {
      last = last.getPrevSibling();
    }
    final int startOffset = first.getTextRange().getStartOffset();
    final int endOffset = last.getTextRange().getEndOffset();
    return startOffset <= endOffset ? new TextRange(startOffset, endOffset) 
                                    : new TextRange(startOffset, startOffset);
  }

  /**
   * Performs indentation (if necessary) of the given code block that surrounds target statements.
   * <p/>
   * The main trick here is to handle situations like the one below:
   * <pre>
   *   void test() {
   *     // This is comment
   *         int i = 1;
   *   }
   * </pre>
   * The problem is that surround block doesn't contain any indent spaces, hence, the first statement is inserted to the
   * zero column. But we have a dedicated code style setting <code>'keep comment at first column'</code>, i.e. the comment
   * will not be moved if that setting is checked.
   * <p/>
   * Current method handles that situation.
   * 
   * @param container     code block that surrounds target statements
   * @param statements    target statements being surrounded
   */
  public static void indentCommentIfNecessary(@NotNull PsiCodeBlock container, @Nullable PsiElement[] statements) {
    if (statements == null || statements.length <= 0) {
      return;
    }

    PsiElement first = statements[0];
    ASTNode node = first.getNode();
    if (node == null || !ElementType.JAVA_COMMENT_BIT_SET.contains(node.getElementType())) {
      return;
    }

    ASTNode commentWsText = node.getTreePrev();
    if (commentWsText == null || !JavaJspElementType.WHITE_SPACE_BIT_SET.contains(commentWsText.getElementType())) {
      return;
    }

    int indent = 0;
    CharSequence text = commentWsText.getChars();
    for (int i = text.length() - 1; i >= 0; i--, indent++) {
      if (text.charAt(i) == '\n') {
        break;
      }
    }

    if (indent <= 0) {
      return;
    }
    
    PsiElement codeBlockWsElement = null;
    ASTNode codeBlockWsNode = null;
    boolean lbraceFound = false;
    final PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(container.getProject());
    for (PsiElement codeBlockChild = container.getFirstChild(); codeBlockChild != null; codeBlockChild = codeBlockChild.getNextSibling()) {
      ASTNode childNode = codeBlockChild.getNode();
      if (childNode == null) {
        continue;
      }
      
      if (!lbraceFound) {
        if (JavaTokenType.LBRACE == childNode.getElementType()) {
          lbraceFound = true;
        }
        continue;
      }

      if (JavaJspElementType.WHITE_SPACE_BIT_SET.contains(childNode.getElementType())) {
        codeBlockWsElement = codeBlockChild;
        codeBlockWsNode = childNode;
        break;
      }
      else if (JavaTokenType.RBRACE == childNode.getElementType()) {
        break;
      }
    }

    if (codeBlockWsElement != null) {
      CharSequence existingWhiteSpaceText = codeBlockWsNode.getChars();
      int existingWhiteSpaceEndOffset = existingWhiteSpaceText.length();
      for (int i = existingWhiteSpaceEndOffset - 1; i >= 0; i--) {
        if (existingWhiteSpaceText.charAt(i) == '\n') {
          existingWhiteSpaceEndOffset = i;
          break;
        }
      }
      String newWsText = text.subSequence(text.length() - indent, text.length()).toString();
      
      // Add white spaces from all lines except the last one.
      if (existingWhiteSpaceEndOffset < existingWhiteSpaceText.length()) {
        newWsText = existingWhiteSpaceText.subSequence(0, existingWhiteSpaceEndOffset + 1).toString() + newWsText;
      }
      PsiElement indentElement = parserFacade.createWhiteSpaceFromText(newWsText);
      codeBlockWsElement.replace(indentElement);
    }
    else {
      PsiElement indentElement = parserFacade.createWhiteSpaceFromText(text.subSequence(text.length() - indent, text.length()).toString());
      container.add(indentElement);
    }
  }
}