Composite folding builder
[fedora-idea.git] / lang-api / src / com / intellij / lang / folding / CompositeFoldingBuilder.java
blob0a3ea4ea1fb76677407a5f49b148830b8a234953
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.
17 package com.intellij.lang.folding;
19 import com.intellij.lang.ASTNode;
20 import com.intellij.openapi.editor.Document;
21 import com.intellij.psi.PsiDocumentManager;
22 import com.intellij.psi.PsiElement;
23 import com.intellij.psi.PsiFile;
24 import org.jetbrains.annotations.Nullable;
26 import java.util.*;
28 /**
29 * Used by LanguageFolding class if more than one FoldingBuilder were specified
30 * for a particular language.
32 * @author Konstantin Bulenkov
33 * @see com.intellij.lang.folding.LanguageFolding
34 * @since 9.0
36 class CompositeFoldingBuilder implements FoldingBuilder {
37 private final List<FoldingBuilder> myBuilders;
38 private final Map<Document, Map<ASTNode, FoldingBuilder>> foldings = new HashMap<Document, Map<ASTNode, FoldingBuilder>>();
39 //private final DocumentListener updater = new Updater();
40 //TODO: think about old links
42 CompositeFoldingBuilder(List<FoldingBuilder> builders) {
43 myBuilders = builders;
46 public FoldingDescriptor[] buildFoldRegions(ASTNode node, Document document) {
47 //document.addDocumentListener(updater);
48 List<FoldingDescriptor> descriptors = new ArrayList<FoldingDescriptor>();
49 Map<ASTNode, FoldingBuilder> builders = foldings.get(document);
50 if (builders == null) {
51 builders = new HashMap<ASTNode, FoldingBuilder>();
52 foldings.put(document, builders);
55 for (FoldingBuilder builder : myBuilders) {
56 final FoldingDescriptor[] foldingDescriptors = builder.buildFoldRegions(node, document);
57 if (foldingDescriptors.length > 0) {
58 descriptors.addAll(Arrays.asList(foldingDescriptors));
59 for (FoldingDescriptor descriptor : foldingDescriptors) {
60 builders.put(descriptor.getElement(), builder);
64 return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
67 public String getPlaceholderText(ASTNode node) {
68 final FoldingBuilder builder = findBuilderByASTNode(node);
69 return builder == null ? node.getText() : builder.getPlaceholderText(node);
72 @Nullable
73 private FoldingBuilder findBuilderByASTNode(ASTNode node) {
74 final PsiElement psi = node.getPsi();
75 if (psi == null) return null;
77 final PsiFile file = psi.getContainingFile();
78 final Document document = file == null ? null : PsiDocumentManager.getInstance(psi.getProject()).getDocument(file);
79 final Map<ASTNode, FoldingBuilder> builders;
81 if (document == null || (builders = foldings.get(document)) == null) return null;
83 return builders.get(node);
86 public boolean isCollapsedByDefault(ASTNode node) {
87 final FoldingBuilder builder = findBuilderByASTNode(node);
88 return builder == null ? false : builder.isCollapsedByDefault(node);
91 //class Updater implements DocumentListener {
92 // public void beforeDocumentChange(DocumentEvent event) {
93 // foldings.remove(event.getDocument());
94 // }
96 // public void documentChanged(DocumentEvent event) {
97 // }
98 //}