/*
 * 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;

import com.intellij.codeInsight.highlighting.BraceMatcher;
import com.intellij.codeInsight.highlighting.BraceMatchingUtil;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;

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

/**
 * @author Gregory.Shrago
 */
public abstract class BraceMatcherBasedSelectioner extends ExtendWordSelectionHandlerBase {

  public List<TextRange> select(final PsiElement e, final CharSequence editorText, final int cursorOffset, final Editor editor) {
    final VirtualFile file = e.getContainingFile().getVirtualFile();
    final FileType fileType = file == null? null : file.getFileType();
    if (fileType == null) return super.select(e, editorText, cursorOffset, editor);
    final TextRange totalRange = e.getTextRange();
    final HighlighterIterator iterator = ((EditorEx)editor).getHighlighter().createIterator(totalRange.getStartOffset());
    final BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);

    final ArrayList<TextRange> result = new ArrayList<TextRange>();
    final LinkedList<Pair<Integer, IElementType>> stack = new LinkedList<Pair<Integer, IElementType>>();
    while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
      final Pair<Integer, IElementType> last;
      if (braceMatcher.isLBraceToken(iterator, editorText, fileType)) {
        stack.addLast(Pair.create(iterator.getStart(), iterator.getTokenType()));
      }
      else if (braceMatcher.isRBraceToken(iterator, editorText, fileType)
          && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).second, iterator.getTokenType())) {
        stack.removeLast();
        final int start = last.first;
        result.addAll(expandToWholeLine(editorText, new TextRange(start, iterator.getEnd())));
        int bodyStart = start + 1;
        int bodyEnd = iterator.getEnd() - 1;
        while (Character.isWhitespace(editorText.charAt(bodyStart))) bodyStart ++;
        while (Character.isWhitespace(editorText.charAt(bodyEnd - 1))) bodyEnd --;
        result.addAll(expandToWholeLine(editorText, new TextRange(bodyStart, bodyEnd)));
      }
      iterator.advance();
    }
    result.add(e.getTextRange());
    return result;
  }
}
@@ -1057,16 +1057,58 @@
 tRange;%0A
+import com.intellij.openapi.util.Trinity;%0A
 import c
@@ -2134,37 +2134,49 @@
 inal LinkedList%3C
-Pair%3C
+Trinity%3CInteger, 
 Integer, IElemen
@@ -2206,21 +2206,33 @@
 kedList%3C
-Pair%3C
+Trinity%3CInteger, 
 Integer,
@@ -2349,13 +2349,25 @@
 nal 
-Pair%3C
+Trinity%3CInteger, 
 Inte
@@ -2485,20 +2485,23 @@
 addLast(
-Pair
+Trinity
 .create(
@@ -2520,16 +2520,35 @@
 Start(),
+ iterator.getEnd(),
  iterato
@@ -2737,13 +2737,12 @@
 ()).
-secon
+thir
 d, i
@@ -2800,46 +2800,8 @@
 ();%0A
-        final int start = last.first;%0A
     
@@ -2862,20 +2862,25 @@
 xtRange(
-star
+last.firs
 t, itera
@@ -2924,13 +2924,19 @@
 t = 
-start
+last.second
  + 1
@@ -2975,17 +2975,15 @@
 .get
-End() - 1
+Start()
 ;%0A  

fix long braces case (begin..end)