diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetModifiableBlockHelper.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetModifiableBlockHelper.java deleted file mode 100644 index 511e200bb8d..00000000000 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetModifiableBlockHelper.java +++ /dev/null @@ -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; - } -} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetModifiableBlockHelper.kt b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetModifiableBlockHelper.kt new file mode 100644 index 00000000000..f37613c026b --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetModifiableBlockHelper.kt @@ -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(place, javaClass(), 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(place, javaClass(), 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 +} \ No newline at end of file diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JetCodeBlockModificationListener.java b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JetCodeBlockModificationListener.java index b9381ac9450..7582d820a94 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JetCodeBlockModificationListener.java +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JetCodeBlockModificationListener.java @@ -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; } }