001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.asJava;
018    
019    import com.intellij.openapi.diagnostic.Logger;
020    import com.intellij.psi.*;
021    import com.intellij.psi.impl.PsiModificationTrackerImpl;
022    import com.intellij.psi.impl.PsiTreeChangeEventImpl;
023    import com.intellij.psi.impl.PsiTreeChangePreprocessor;
024    import com.intellij.psi.util.PsiModificationTracker;
025    import org.jetbrains.annotations.NotNull;
026    import org.jetbrains.jet.lang.psi.JetBlockExpression;
027    import org.jetbrains.jet.lang.psi.JetClass;
028    import org.jetbrains.jet.lang.psi.JetFile;
029    
030    public class JetCodeBlockModificationListener implements PsiTreeChangePreprocessor {
031        private static final Logger LOG = Logger.getInstance("#org.jetbrains.jet.asJava.JetCodeBlockModificationListener");
032        
033        private final PsiModificationTrackerImpl myModificationTracker;
034    
035        public JetCodeBlockModificationListener(PsiModificationTracker modificationTracker) {
036            myModificationTracker = (PsiModificationTrackerImpl) modificationTracker;
037        }
038    
039        @Override
040        public void treeChanged(@NotNull PsiTreeChangeEventImpl event) {
041            if (!(event.getFile() instanceof JetFile)) return;
042            switch (event.getCode()) {
043                case BEFORE_CHILDREN_CHANGE:
044                case BEFORE_PROPERTY_CHANGE:
045                case BEFORE_CHILD_MOVEMENT:
046                case BEFORE_CHILD_REPLACEMENT:
047                case BEFORE_CHILD_ADDITION:
048                case BEFORE_CHILD_REMOVAL:
049                    break;
050    
051                case CHILD_ADDED:
052                case CHILD_REMOVED:
053                case CHILD_REPLACED:
054                    processChange(event.getParent(), event.getOldChild(), event.getChild());
055                    break;
056    
057                case CHILDREN_CHANGED:
058                    // general childrenChanged() event after each change
059                    if (!event.isGenericChange()) {
060                        processChange(event.getParent(), event.getParent(), null);
061                    }
062                    break;
063    
064                case CHILD_MOVED:
065                case PROPERTY_CHANGED:
066                    myModificationTracker.incCounter();
067                    break;
068    
069                default:
070                    LOG.error("Unknown code:" + event.getCode());
071                    break;
072            }
073        }
074    
075        private void processChange(PsiElement parent, PsiElement child1, PsiElement child2) {
076            try {
077                if (!isInsideCodeBlock(parent)) {
078                    if (parent != null && parent.getContainingFile() instanceof JetFile) {
079                        myModificationTracker.incCounter();
080                    }
081                    else {
082                        myModificationTracker.incOutOfCodeBlockModificationCounter();
083                    }
084                    return;
085                }
086    
087                if (containsClassesInside(child1) || child2 != child1 && containsClassesInside(child2)) {
088                    myModificationTracker.incCounter();
089                }
090            } catch (PsiInvalidElementAccessException e) {
091                myModificationTracker.incCounter(); // Shall not happen actually, just a pre-release paranoia
092            }
093        }
094    
095        private static boolean containsClassesInside(PsiElement element) {
096            if (element == null) return false;
097            if (element instanceof PsiClass) return true;
098    
099            PsiElement child = element.getFirstChild();
100            while (child != null) {
101                if (containsClassesInside(child)) return true;
102                child = child.getNextSibling();
103            }
104    
105            return false;
106        }
107    
108        private static boolean isInsideCodeBlock(PsiElement element) {
109            if (element instanceof PsiFileSystemItem) {
110                return false;
111            }
112    
113            if (element == null || element.getParent() == null) return true;
114    
115            PsiElement parent = element;
116            while (true) {
117                if (parent instanceof PsiFile || parent instanceof PsiDirectory || parent == null) {
118                    return false;
119                }
120                if (parent instanceof JetClass) return false; // anonymous or local class
121                if (parent instanceof JetBlockExpression) {
122                    return true;
123                }
124                parent = parent.getParent();
125            }
126        }
127    }