/*
 * 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.editorActions.wordSelection;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;

import java.util.ArrayList;
import java.util.List;

public class CodeBlockOrInitializerSelectioner extends BasicSelectioner {
  @Override
  public boolean canSelect(PsiElement e) {
    return e instanceof PsiCodeBlock || e instanceof PsiArrayInitializerExpression;
  }

  @Override
  public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = new ArrayList<TextRange>();

    PsiElement[] children = e.getChildren();

    int start = findOpeningBrace(children);
    int end = findClosingBrace(children, start);

    result.add(e.getTextRange());
    result.addAll(expandToWholeLine(editorText, new TextRange(start, end)));

    return result;
  }

  public static int findOpeningBrace(PsiElement[] children) {
    int start = 0;
    for (int i = 0; i < children.length; i++) {
      PsiElement child = children[i];

      if (child instanceof PsiJavaToken) {
        PsiJavaToken token = (PsiJavaToken)child;

        if (token.getTokenType() == JavaTokenType.LBRACE) {
          int j = i + 1;

          while (children[j] instanceof PsiWhiteSpace) {
            j++;
          }

          start = children[j].getTextRange().getStartOffset();
        }
      }
    }
    return start;
  }

  public static int findClosingBrace(PsiElement[] children, int startOffset) {
    int end = children[children.length - 1].getTextRange().getEndOffset();
    for (int i = 0; i < children.length; i++) {
      PsiElement child = children[i];

      if (child instanceof PsiJavaToken) {
        PsiJavaToken token = (PsiJavaToken)child;

        if (token.getTokenType() == JavaTokenType.RBRACE) {
          int j = i - 1;

          while (children[j] instanceof PsiWhiteSpace && children[j].getTextRange().getStartOffset() > startOffset) {
            j--;
          }

          end = children[j].getTextRange().getEndOffset();
        }
      }
    }
    return end;
  }
}

@@ -20,10 +20,10 @@
 0-20
-09
+15
  Jet
@@ -1216,16 +1216,50 @@
 ange%3E();
+%0A    result.add(e.getTextRange());
 %0A%0A    Ps
@@ -1297,17 +1297,49 @@
 dren();%0A
-%0A
+    if (children.length %3E 0) %7B%0A  
     int 
@@ -1374,24 +1374,26 @@
 ldren);%0A    
+ 
+ 
 int end = fi
@@ -1429,43 +1429,10 @@
 t);%0A
-%0A
   
-  result.add(e.getTextRange());%0A
     
@@ -1503,16 +1503,22 @@
  end)));
+%0A    %7D
 %0A%0A    re

fix exception on select word when code block has no children (EA-64075 - AIOOBE: CodeBlockOrInitializerSelectioner.findClosingBrace)