update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / folding / impl / CollapseBlockHandler.java
blob82fca64c97e5f1c39bdeb75ba76f13e29b5cb55b
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.codeInsight.folding.impl;
18 import com.intellij.codeInsight.CodeInsightActionHandler;
19 import com.intellij.openapi.diagnostic.Logger;
20 import com.intellij.openapi.editor.Editor;
21 import com.intellij.openapi.editor.FoldRegion;
22 import com.intellij.openapi.editor.ex.FoldingModelEx;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.psi.*;
25 import com.intellij.psi.util.PsiTreeUtil;
26 import org.jetbrains.annotations.NotNull;
28 /**
29 * @author ven
31 public class CollapseBlockHandler implements CodeInsightActionHandler {
32 public static final String ourPlaceHolderText = "{...}";
33 private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.folding.impl.CollapseBlockHandler");
35 public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) {
36 editor.getFoldingModel().runBatchFoldingOperation(new Runnable() {
37 public void run() {
38 final EditorFoldingInfo info = EditorFoldingInfo.get(editor);
39 FoldingModelEx model = (FoldingModelEx) editor.getFoldingModel();
40 PsiElement element = file.findElementAt(editor.getCaretModel().getOffset() - 1);
41 if (!(element instanceof PsiJavaToken) || ((PsiJavaToken) element).getTokenType() != JavaTokenType.RBRACE) {
42 element = file.findElementAt(editor.getCaretModel().getOffset());
44 if (element == null) return;
45 PsiCodeBlock block = PsiTreeUtil.getParentOfType(element, PsiCodeBlock.class);
46 FoldRegion previous = null;
47 FoldRegion myPrevious = null;
48 while (block != null) {
49 int start = block.getTextRange().getStartOffset();
50 int end = block.getTextRange().getEndOffset();
51 FoldRegion existing = FoldingUtil.findFoldRegion(editor, start, end);
52 if (existing != null) {
53 previous = existing;
54 if (info.getPsiElement(existing) == null) myPrevious = existing;
55 block = PsiTreeUtil.getParentOfType(block, PsiCodeBlock.class);
56 continue;
58 if (!model.intersectsRegion(start, end)) {
59 FoldRegion region = model.addFoldRegion(start, end, ourPlaceHolderText);
60 LOG.assertTrue(region != null);
61 region.setExpanded(false);
62 if (myPrevious != null && info.getPsiElement(region) == null) {
63 info.removeRegion(myPrevious);
64 model.removeFoldRegion(myPrevious);
66 int offset = block.getTextRange().getEndOffset() < editor.getCaretModel().getOffset() ?
67 start : end;
68 editor.getCaretModel().moveToOffset(offset);
69 return;
70 } else break;
72 if (previous != null) {
73 previous.setExpanded(false);
74 if (myPrevious != null) {
75 info.removeRegion(myPrevious);
76 model.removeFoldRegion(myPrevious);
78 editor.getCaretModel().moveToOffset(previous.getEndOffset());
81 });
84 public boolean startInWriteAction() {
85 return true;