/*
 * Copyright 2013-2014 must-be.org
 *
 * 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 org.mustbe.consulo.csharp.lang.psi.impl;

import org.jetbrains.annotations.NotNull;
import org.mustbe.consulo.csharp.lang.CSharpFileType;
import org.mustbe.consulo.dotnet.psi.DotNetStatement;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.PsiModificationTrackerImpl;
import com.intellij.psi.impl.PsiTreeChangeEventImpl;
import com.intellij.psi.impl.PsiTreeChangePreprocessor;
import com.intellij.psi.util.PsiModificationTracker;
import com.intellij.psi.util.PsiTreeUtil;

/**
 * @author VISTALL
 * @since 19.12.13.
 */
public class CSharpPsiTreeChangePreprocessor implements PsiTreeChangePreprocessor
{
	private final PsiModificationTrackerImpl myTracker;

	public CSharpPsiTreeChangePreprocessor(PsiModificationTracker tracker)
	{
		myTracker = (PsiModificationTrackerImpl) tracker;
	}

	@Override
	public void treeChanged(@NotNull PsiTreeChangeEventImpl psiTreeChangeEvent)
	{
		switch(psiTreeChangeEvent.getCode())
		{
			case BEFORE_CHILD_ADDITION:
			case BEFORE_CHILD_REMOVAL:
			case BEFORE_CHILD_REPLACEMENT:
			case BEFORE_CHILD_MOVEMENT:
			case BEFORE_CHILDREN_CHANGE:
			case BEFORE_PROPERTY_CHANGE:
				break;
			case CHILD_ADDED:
			case CHILD_REMOVED:
			case CHILD_REPLACED:
			case CHILD_MOVED:
			case CHILDREN_CHANGED:
			case PROPERTY_CHANGED:
				PsiFile file = psiTreeChangeEvent.getFile();
				if(file == null || file.getFileType() != CSharpFileType.INSTANCE)
				{
					return;
				}
				PsiElement element = psiTreeChangeEvent.getElement();
				if(element == null)
				{
					return;
				}
				DotNetStatement statement = PsiTreeUtil.getParentOfType(element, DotNetStatement.class);
				if(statement != null)
				{
					return;
				}
				myTracker.incOutOfCodeBlockModificationCounter();
				break;
		}
	}
}

@@ -682,16 +682,59 @@
 otNull;%0A
+import org.jetbrains.annotations.Nullable;%0A
 import o
@@ -763,16 +763,20 @@
 rp.lang.
+psi.
 CSharpFi
@@ -777,20 +777,16 @@
 harpFile
-Type
 ;%0Aimport
@@ -829,24 +829,69 @@
 tStatement;%0A
+import com.intellij.openapi.project.Project;%0A
 import com.i
@@ -983,193 +983,34 @@
 .Psi
-ModificationTrackerImpl;%0Aimport com.intellij.psi.impl.PsiTreeChangeEventImpl;%0Aimport com.intellij.psi.impl.PsiTreeChangePreprocessor;%0Aimport com.intellij.psi.util.PsiModificationTracker
+TreeChangePreprocessorBase
 ;%0Aim
@@ -1146,17 +1146,14 @@
 sor 
-implement
+extend
 s Ps
@@ -1179,1135 +1179,377 @@
 ssor
-%0A%7B%0A%09private final PsiModificationTrackerImpl myTracker;%0A%0A%09public CSharpPsiTreeChangePreprocessor(PsiModificationTracker tracker)%0A%09%7B%0A%09%09myTracker = (PsiModificationTrackerImpl) tracker;%0A%09%7D%0A%0A%09@Override%0A%09public void treeChanged(@NotNull PsiTreeChangeEventImpl psiTreeChangeEvent)%0A%09%7B%0A%09%09switch(psiTreeChangeEvent.getCode())%0A%09%09%7B%0A%09%09%09case BEFORE_CHILD_ADDITION:%0A%09%09%09case BEFORE_CHILD_REMOVAL:%0A%09%09%09case BEFORE_CHILD_REPLACEMENT:%0A%09%09%09case BEFORE_CHILD_MOVEMENT:%0A%09%09%09case BEFORE_CHILDREN_CHANGE:%0A%09%09%09case BEFORE_PROPERTY_CHANGE:%0A%09%09%09%09break;%0A%09%09%09case CHILD_ADDED:%0A%09%09%09case CHILD_REMOVED:%0A%09%09%09case CHILD_REPLACED:%0A%09%09%09case CHILD_MOVED:%0A%09%09%09case CHILDREN_CHANGED:%0A%09%09%09case PROPERTY_CHANGED:%0A%09%09%09%09PsiFile file = psiTreeChangeEvent.getFile();%0A%09%09%09%09if(file == null %7C%7C file.getFileType() != CSharpFileType.INSTANCE)%0A%09%09%09%09%7B%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%09%09%09%09PsiElement element = psiTreeChangeEvent.getElement();%0A%09%09%09%09if(element == null)%0A%09%09%09%09%7B%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%09%09%09%09DotNetStatement statement = PsiTreeUtil.getParentOfType(element, DotNetStatement.class);%0A%09%09%09%09if(statement != null)%0A%09%09%09%09%7B%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%09%09%09%09myTracker.incOutOfCodeBlockModificationCounter();%0A%09%09%09%09break;%0A%09%09%7D
+Base%0A%7B%0A%09public CSharpPsiTreeChangePreprocessor(@NotNull Project project)%0A%09%7B%0A%09%09super(project);%0A%09%7D%0A%0A%09@Override%0A%09protected boolean isMyFile(@NotNull PsiFile file)%0A%09%7B%0A%09%09return file instanceof CSharpFile;%0A%09%7D%0A%0A%09@Override%0A%09protected boolean isInsideCodeBlock(@Nullable PsiElement element)%0A%09%7B%0A%09%09return PsiTreeUtil.getParentOfType(element, DotNetStatement.class, false) != null;
 %0A%09%7D%0A

use class from platform