/*
 * 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.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

/**
 * @author yole
 */
public abstract class PsiTreeChangePreprocessorBase implements PsiTreeChangePreprocessor {
  private final PsiModificationTrackerImpl myModificationTracker;

  public PsiTreeChangePreprocessorBase(PsiManagerImpl psiManager) {
    myModificationTracker = (PsiModificationTrackerImpl) psiManager.getModificationTracker();
    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) {
      myModificationTracker.incOutOfCodeBlockModificationCounter();
    }
  }

  protected abstract boolean isInsideCodeBlock(PsiElement element);
}

@@ -627,16 +627,61 @@
 .impl;%0A%0A
+import com.intellij.openapi.project.Project;%0A
 import c
@@ -752,41 +752,158 @@
 ort 
-org.jetbrains.annotations.NotNull
+com.intellij.psi.PsiManager;%0Aimport com.intellij.util.ObjectUtils;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable
 ;%0A%0A/
@@ -1018,16 +1018,25 @@
 sor %7B%0A  
+@NotNull 
 private 
@@ -1046,57 +1046,205 @@
 al P
-siModificationTrackerImpl myModificationTracker;%0A
+roject myProject;%0A  @Nullable private final PsiManagerImpl myPsiManager;%0A%0A  public PsiTreeChangePreprocessorBase(@NotNull Project project) %7B%0A    myProject = project;%0A    myPsiManager = null;%0A  %7D%0A  
 %0A  p
@@ -1279,16 +1279,25 @@
 sorBase(
+@NotNull 
 PsiManag
@@ -1327,92 +1327,70 @@
   my
-ModificationTrack
+PsiManag
 er = 
-(PsiModificationTrackerImpl) psiManager.getModificationTracker
+psiManager;%0A    myProject = psiManager.getProject
 ();%0A
@@ -2734,18 +2734,19 @@
 %7B%0A      
-my
+get
 Modifica
@@ -2756,16 +2756,18 @@
 nTracker
+()
 .incOutO
@@ -2810,16 +2810,230 @@
  %7D%0A  %7D%0A%0A
+  @NotNull%0A  private PsiModificationTrackerImpl getModificationTracker() %7B%0A    return (PsiModificationTrackerImpl)ObjectUtils.notNull(myPsiManager, PsiManager.getInstance(myProject)).getModificationTracker();%0A  %7D%0A%0A
   protec

Allow not to register PsiTreeChangeProcessorBase on constructing