Rewrite to Kotlin: JetCodeBlockModificationListener
This commit is contained in:
+75
-78
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2013 JetBrains s.r.o.
|
* Copyright 2010-2014 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -14,115 +14,112 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.jet.asJava;
|
package org.jetbrains.jet.asJava
|
||||||
|
|
||||||
import com.intellij.openapi.diagnostic.Logger;
|
import com.intellij.openapi.diagnostic.Logger
|
||||||
import com.intellij.psi.*;
|
import com.intellij.psi.*
|
||||||
import com.intellij.psi.impl.PsiModificationTrackerImpl;
|
import com.intellij.psi.impl.PsiModificationTrackerImpl
|
||||||
import com.intellij.psi.impl.PsiTreeChangeEventImpl;
|
import com.intellij.psi.impl.PsiTreeChangeEventImpl.PsiEventType.*
|
||||||
import com.intellij.psi.impl.PsiTreeChangePreprocessor;
|
import com.intellij.psi.impl.PsiTreeChangeEventImpl
|
||||||
import com.intellij.psi.util.PsiModificationTracker;
|
import com.intellij.psi.impl.PsiTreeChangePreprocessor
|
||||||
import org.jetbrains.annotations.NotNull;
|
import com.intellij.psi.util.PsiModificationTracker
|
||||||
import org.jetbrains.jet.lang.psi.JetBlockExpression;
|
import org.jetbrains.jet.lang.psi.JetBlockExpression
|
||||||
import org.jetbrains.jet.lang.psi.JetClass;
|
import org.jetbrains.jet.lang.psi.JetClass
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.JetFile
|
||||||
import org.jetbrains.jet.lang.psi.PsiPackage;
|
import org.jetbrains.jet.lang.psi.shouldChangeModificationCount
|
||||||
|
|
||||||
public class JetCodeBlockModificationListener implements PsiTreeChangePreprocessor {
|
public class JetCodeBlockModificationListener(modificationTracker: PsiModificationTracker) : PsiTreeChangePreprocessor {
|
||||||
private static final Logger LOG = Logger.getInstance("#org.jetbrains.jet.asJava.JetCodeBlockModificationListener");
|
private val myModificationTracker = modificationTracker as PsiModificationTrackerImpl
|
||||||
|
|
||||||
private final PsiModificationTrackerImpl myModificationTracker;
|
|
||||||
|
|
||||||
public JetCodeBlockModificationListener(PsiModificationTracker modificationTracker) {
|
override fun treeChanged(event: PsiTreeChangeEventImpl) {
|
||||||
myModificationTracker = (PsiModificationTrackerImpl) modificationTracker;
|
if (event.getFile() !is JetFile) return
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
when (event.getCode()) {
|
||||||
public void treeChanged(@NotNull PsiTreeChangeEventImpl event) {
|
BEFORE_CHILDREN_CHANGE,
|
||||||
if (!(event.getFile() instanceof JetFile)) return;
|
BEFORE_PROPERTY_CHANGE,
|
||||||
switch (event.getCode()) {
|
BEFORE_CHILD_MOVEMENT,
|
||||||
case BEFORE_CHILDREN_CHANGE:
|
BEFORE_CHILD_REPLACEMENT,
|
||||||
case BEFORE_PROPERTY_CHANGE:
|
BEFORE_CHILD_ADDITION,
|
||||||
case BEFORE_CHILD_MOVEMENT:
|
BEFORE_CHILD_REMOVAL -> {
|
||||||
case BEFORE_CHILD_REPLACEMENT:
|
// skip
|
||||||
case BEFORE_CHILD_ADDITION:
|
}
|
||||||
case BEFORE_CHILD_REMOVAL:
|
|
||||||
break;
|
|
||||||
|
|
||||||
case CHILD_ADDED:
|
CHILD_ADDED,
|
||||||
case CHILD_REMOVED:
|
CHILD_REMOVED,
|
||||||
case CHILD_REPLACED:
|
CHILD_REPLACED -> {
|
||||||
processChange(event.getParent(), event.getOldChild(), event.getChild());
|
processChange(event.getParent(), event.getOldChild(), event.getChild())
|
||||||
break;
|
}
|
||||||
|
|
||||||
case CHILDREN_CHANGED:
|
CHILDREN_CHANGED -> {
|
||||||
// general childrenChanged() event after each change
|
|
||||||
if (!event.isGenericChange()) {
|
if (!event.isGenericChange()) {
|
||||||
processChange(event.getParent(), event.getParent(), null);
|
processChange(event.getParent(), event.getParent(), null)
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
|
|
||||||
case CHILD_MOVED:
|
CHILD_MOVED,
|
||||||
case PROPERTY_CHANGED:
|
PROPERTY_CHANGED -> {
|
||||||
myModificationTracker.incCounter();
|
myModificationTracker.incCounter()
|
||||||
break;
|
}
|
||||||
|
|
||||||
default:
|
else -> LOG.error("Unknown code:" + event.getCode())
|
||||||
LOG.error("Unknown code:" + event.getCode());
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processChange(PsiElement parent, PsiElement child1, PsiElement child2) {
|
private fun processChange(parent: PsiElement?, child1: PsiElement, child2: PsiElement?) {
|
||||||
try {
|
try {
|
||||||
if (!isInsideCodeBlock(parent)) {
|
if (!isInsideCodeBlock(parent)) {
|
||||||
if (parent.getContainingFile() instanceof JetFile) {
|
if (parent != null && parent.getContainingFile() is JetFile) {
|
||||||
myModificationTracker.incCounter();
|
myModificationTracker.incCounter()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
myModificationTracker.incOutOfCodeBlockModificationCounter();
|
myModificationTracker.incOutOfCodeBlockModificationCounter()
|
||||||
}
|
}
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (containsClassesInside(child1) || (child2 != child1 && containsClassesInside(child2))) {
|
if (containsClassesInside(child1) || (child2 != child1 && containsClassesInside(child2))) {
|
||||||
myModificationTracker.incCounter();
|
myModificationTracker.incCounter()
|
||||||
}
|
}
|
||||||
} catch (PsiInvalidElementAccessException e) {
|
}
|
||||||
myModificationTracker.incCounter(); // Shall not happen actually, just a pre-release paranoia
|
catch (e: PsiInvalidElementAccessException) {
|
||||||
|
myModificationTracker.incCounter() // Shall not happen actually, just a pre-release paranoia
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean containsClassesInside(PsiElement element) {
|
class object {
|
||||||
if (element == null) return false;
|
private val LOG = Logger.getInstance("#org.jetbrains.jet.asJava.JetCodeBlockModificationListener")
|
||||||
if (element instanceof PsiClass) return true;
|
|
||||||
|
|
||||||
PsiElement child = element.getFirstChild();
|
private fun containsClassesInside(element: PsiElement?): Boolean {
|
||||||
while (child != null) {
|
if (element == null) return false
|
||||||
if (containsClassesInside(child)) return true;
|
if (element is PsiClass) return true
|
||||||
child = child.getNextSibling();
|
|
||||||
|
var child = element.getFirstChild()
|
||||||
|
while (child != null) {
|
||||||
|
if (containsClassesInside(child)) return true
|
||||||
|
child = child!!.getNextSibling()
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
private fun isInsideCodeBlock(element: PsiElement?): Boolean {
|
||||||
}
|
if (element is PsiFileSystemItem) return false
|
||||||
|
if (element == null || element.getParent() == null) return true
|
||||||
|
|
||||||
private static boolean isInsideCodeBlock(PsiElement element) {
|
var parent = element
|
||||||
if (element instanceof PsiFileSystemItem) return false;
|
while (parent !is PsiFile && parent !is PsiDirectory && parent != null) {
|
||||||
|
if (parent is JetClass) return false // anonymous or local class
|
||||||
|
|
||||||
if (element == null || element.getParent() == null) return true;
|
if (parent is JetBlockExpression) {
|
||||||
|
if (!shouldChangeModificationCount(element)) {
|
||||||
PsiElement parent = element;
|
return true;
|
||||||
while (true) {
|
}
|
||||||
if (parent instanceof PsiFile || parent instanceof PsiDirectory || parent == null) return false;
|
|
||||||
|
|
||||||
if (parent instanceof JetClass) return false; // anonymous or local class
|
|
||||||
|
|
||||||
if (parent instanceof JetBlockExpression) {
|
|
||||||
if (!PsiPackage.shouldChangeModificationCount(element)) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parent = parent!!.getParent()
|
||||||
}
|
}
|
||||||
parent = parent.getParent();
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user