Reimplement json tree change preprocessor with PsiTreeChangePreprocessorBase

This commit is contained in:
Alexander Zolotov
2016-06-02 15:51:38 +03:00
parent d74245e436
commit 23b575b87b
@@ -1,85 +1,50 @@
/*
* Copyright 2000-2016 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.json.psi.impl;
import com.intellij.json.JsonLanguage;
import com.intellij.json.psi.JsonFile;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileSystemItem;
import com.intellij.psi.impl.PsiManagerImpl;
import com.intellij.psi.impl.PsiModificationTrackerImpl;
import com.intellij.psi.impl.PsiTreeChangeEventImpl;
import com.intellij.psi.impl.PsiTreeChangePreprocessor;
import com.intellij.psi.impl.PsiTreeChangePreprocessorBase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Mikhail Golubev
*/
public class JsonTreeChangePreprocessor implements PsiTreeChangePreprocessor {
/**
* @see com.intellij.psi.impl.PsiTreeChangePreprocessorBase#treeChanged(com.intellij.psi.impl.PsiTreeChangeEventImpl)
*/
public class JsonTreeChangePreprocessor extends PsiTreeChangePreprocessorBase {
public JsonTreeChangePreprocessor(@NotNull Project project) {
super(project);
}
@Override
public void treeChanged(@NotNull PsiTreeChangeEventImpl event) {
if (!(event.getFile() instanceof JsonFile)) return;
final PsiElement element = event.getParent();
if (element == null || !(element.getManager() instanceof PsiManagerImpl)) {
return;
}
final PsiModificationTrackerImpl modificationTracker = (PsiModificationTrackerImpl)element.getManager().getModificationTracker();
boolean changedInsideCodeBlock = false;
switch (event.getCode()) {
case BEFORE_CHILDREN_CHANGE:
if (event.getParent() instanceof PsiFile) {
changedInsideCodeBlock = true;
break;
}
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) {
modificationTracker.incOutOfCodeBlockModificationCounter();
if (event.getFile() instanceof JsonFile) {
super.treeChanged(event);
}
}
private boolean isInsideCodeBlock(@Nullable PsiElement element) {
@Override
protected boolean isInsideCodeBlock(@Nullable PsiElement element) {
if (element instanceof PsiFileSystemItem) {
return false;
}
if (element == null || element.getParent() == null) {
return true;
}
// Consider all elements of JSON PSI as externally visible, i.e. they are out of code block
return !(element.getLanguage() instanceof JsonLanguage);
}
}
}