Rewrite to kotlin: JetModifiableBlockHelper

This commit is contained in:
Nikolay Krasko
2014-10-21 17:24:53 +04:00
parent 565612bc19
commit 6b605ec4f9
3 changed files with 89 additions and 92 deletions
@@ -1,84 +0,0 @@
/*
* Copyright 2010-2013 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 org.jetbrains.jet.lang.psi;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
public final class JetModifiableBlockHelper {
private JetModifiableBlockHelper() {
}
/**
* Tested in OutOfBlockModificationTestGenerated
*/
public static boolean shouldChangeModificationCount(PsiElement place) {
JetDeclaration declaration = PsiTreeUtil.getParentOfType(place, JetDeclaration.class, true);
if (declaration != null) {
if (declaration instanceof JetNamedFunction) {
JetNamedFunction function = (JetNamedFunction) declaration;
if (function.hasDeclaredReturnType() || function.hasBlockBody()) {
return takePartInDeclarationTypeInference(function);
}
return shouldChangeModificationCount(function);
}
else if (declaration instanceof JetPropertyAccessor) {
return takePartInDeclarationTypeInference(declaration);
}
else if (declaration instanceof JetProperty) {
JetProperty property = (JetProperty) declaration;
if (property.getTypeReference() != null) {
return takePartInDeclarationTypeInference(property);
}
return shouldChangeModificationCount(property);
}
else if (declaration instanceof JetMultiDeclaration || declaration instanceof JetMultiDeclarationEntry) {
return shouldChangeModificationCount(declaration);
}
else if (declaration instanceof JetFunctionLiteral) {
// TODO: Check return type
return shouldChangeModificationCount(declaration);
}
}
return true;
}
private static boolean takePartInDeclarationTypeInference(PsiElement place) {
JetDeclaration declaration = PsiTreeUtil.getParentOfType(place, JetDeclaration.class, true);
if (declaration != null) {
if (declaration instanceof JetNamedFunction) {
JetNamedFunction function = (JetNamedFunction) declaration;
if (!function.hasDeclaredReturnType() && !function.hasBlockBody()) {
return true;
}
}
else if (declaration instanceof JetProperty) {
JetProperty property = (JetProperty) declaration;
if (property.getTypeReference() == null) {
return true;
}
}
return takePartInDeclarationTypeInference(declaration);
}
return false;
}
}
@@ -0,0 +1,83 @@
/*
* Copyright 2010-2014 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 org.jetbrains.jet.lang.psi
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
/**
* Tested in OutOfBlockModificationTestGenerated
*/
public fun shouldChangeModificationCount(place: PsiElement): Boolean {
// false -> inside code block
// true -> means nothing, parent will be checked
val declaration = PsiTreeUtil.getParentOfType<JetDeclaration>(place, javaClass<JetDeclaration>(), true)
if (declaration == null) return true
return when (declaration) {
is JetNamedFunction -> {
val function: JetNamedFunction = declaration
if (function.hasDeclaredReturnType() || function.hasBlockBody()) {
takePartInDeclarationTypeInference(function)
}
else {
shouldChangeModificationCount(function)
}
}
is JetPropertyAccessor -> {
takePartInDeclarationTypeInference(declaration)
}
is JetProperty -> {
val property = declaration as JetProperty
if (property.getTypeReference() != null) {
takePartInDeclarationTypeInference(property)
}
else {
shouldChangeModificationCount(property)
}
}
is JetMultiDeclaration, is JetMultiDeclarationEntry, is JetFunctionLiteral -> {
shouldChangeModificationCount(declaration)
}
else -> {
true
}
}
}
private fun takePartInDeclarationTypeInference(place: PsiElement): Boolean {
val declaration = PsiTreeUtil.getParentOfType<JetDeclaration>(place, javaClass<JetDeclaration>(), true)
if (declaration != null) {
if (declaration is JetNamedFunction) {
val function = declaration as JetNamedFunction
if (!function.hasDeclaredReturnType() && !function.hasBlockBody()) {
return true
}
}
else if (declaration is JetProperty) {
val property = declaration as JetProperty
if (property.getTypeReference() == null) {
return true
}
}
return takePartInDeclarationTypeInference(declaration)
}
return false
}
@@ -26,7 +26,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetBlockExpression;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetModifiableBlockHelper;
import org.jetbrains.jet.lang.psi.PsiPackage;
public class JetCodeBlockModificationListener implements PsiTreeChangePreprocessor {
private static final Logger LOG = Logger.getInstance("#org.jetbrains.jet.asJava.JetCodeBlockModificationListener");
@@ -107,20 +107,18 @@ public class JetCodeBlockModificationListener implements PsiTreeChangePreprocess
}
private static boolean isInsideCodeBlock(PsiElement element) {
if (element instanceof PsiFileSystemItem) {
return false;
}
if (element instanceof PsiFileSystemItem) return false;
if (element == null || element.getParent() == null) return true;
PsiElement parent = element;
while (true) {
if (parent instanceof PsiFile || parent instanceof PsiDirectory || parent == null) {
return false;
}
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 (!JetModifiableBlockHelper.shouldChangeModificationCount(element)) {
if (!PsiPackage.shouldChangeModificationCount(element)) {
return true;
}
}