/*
 * Copyright 2000-2010 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.psi.impl;

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author yole
 */
public abstract class PsiTreeChangePreprocessorBase implements PsiTreeChangePreprocessor {
  @NotNull private final Project myProject;
  @Nullable private final PsiManagerImpl myPsiManager;

  public PsiTreeChangePreprocessorBase(@NotNull Project project) {
    myProject = project;
    myPsiManager = null;
  }
  
  public PsiTreeChangePreprocessorBase(@NotNull PsiManagerImpl psiManager) {
    myPsiManager = psiManager;
    myProject = psiManager.getProject();
    psiManager.addTreeChangePreprocessor(this);
  }

  @Override
  public void treeChanged(@NotNull PsiTreeChangeEventImpl event) {
    boolean changedInsideCodeBlock = false;

    switch (event.getCode()) {
      case BEFORE_CHILDREN_CHANGE:
        if (event.getParent() instanceof PsiFile) {
          changedInsideCodeBlock = true;
          break; // May be caused by fake PSI event from PomTransaction. A real event will anyway follow.
        }

      case CHILDREN_CHANGED :
        if (event.isGenericChange()) {
          return;
        }
        changedInsideCodeBlock = isInsideCodeBlock(event.getParent());
      break;

      case BEFORE_CHILD_ADDITION:
      case BEFORE_CHILD_REMOVAL:
      case CHILD_ADDED :
      case CHILD_REMOVED :
        changedInsideCodeBlock = isInsideCodeBlock(event.getParent());
      break;

      case BEFORE_PROPERTY_CHANGE:
      case PROPERTY_CHANGED :
        changedInsideCodeBlock = false;
      break;

      case BEFORE_CHILD_REPLACEMENT:
      case CHILD_REPLACED :
        changedInsideCodeBlock = isInsideCodeBlock(event.getParent());
      break;

      case BEFORE_CHILD_MOVEMENT:
      case CHILD_MOVED :
        changedInsideCodeBlock = isInsideCodeBlock(event.getOldParent()) && isInsideCodeBlock(event.getNewParent());
      break;
    }

    if (!changedInsideCodeBlock) {
      getModificationTracker().incOutOfCodeBlockModificationCounter();
    }
  }

  @NotNull
  private PsiModificationTrackerImpl getModificationTracker() {
    return (PsiModificationTrackerImpl)ObjectUtils.notNull(myPsiManager, PsiManager.getInstance(myProject)).getModificationTracker();
  }

  protected abstract boolean isInsideCodeBlock(PsiElement element);
}

@@ -905,32 +905,8 @@
 e;%0A%0A
-/**%0A * @author yole%0A */%0A
 publ
@@ -1213,18 +1213,383 @@
 ll;%0A  %7D%0A
+%0A
   
+/**%0A   * Note that this constructor can't be used for preprocessors that is defined in extensions%0A   * because it will be invoke during constructing PsiManager.%0A   *%0A   * @deprecated to delete in IDEA 15, define preprocessor as an extension of %7B@link PsiTreeChangePreprocessor#EP_NAME%7D and%0A   * use %7B@link this#PsiTreeChangePreprocessorBase(Project)%7D instead%0A   */
 %0A  publi
@@ -2202,33 +2202,32 @@
 CHILDREN_CHANGED
- 
 :%0A        if (ev
@@ -2342,32 +2342,34 @@
 t.getParent());%0A
+  
       break;%0A%0A  
@@ -2357,32 +2357,32 @@
 %0A        break;%0A
-
 %0A      case BEFO
@@ -2455,17 +2455,16 @@
 LD_ADDED
- 
 :%0A      
@@ -2477,25 +2477,24 @@
 HILD_REMOVED
- 
 :%0A        ch
@@ -2546,32 +2546,34 @@
 t.getParent());%0A
+  
       break;%0A%0A  
@@ -2561,32 +2561,32 @@
 %0A        break;%0A
-
 %0A      case BEFO
@@ -2632,17 +2632,16 @@
 _CHANGED
- 
 :%0A      
@@ -2674,16 +2674,18 @@
  false;%0A
+  
       br
@@ -2681,32 +2681,32 @@
 %0A        break;%0A
-
 %0A      case BEFO
@@ -2752,17 +2752,16 @@
 REPLACED
- 
 :%0A      
@@ -2817,32 +2817,34 @@
 t.getParent());%0A
+  
       break;%0A%0A  
@@ -2832,32 +2832,32 @@
 %0A        break;%0A
-
 %0A      case BEFO
@@ -2897,17 +2897,16 @@
 LD_MOVED
- 
 :%0A      
@@ -3012,24 +3012,26 @@
 wParent());%0A
+  
       break;

Add comment for PsiTreeChangeProcessorBase