001/*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.jetbrains.jet.lang.psi;
018
019import com.intellij.psi.PsiElement;
020import com.intellij.psi.util.PsiTreeUtil;
021
022public final class JetModifiableBlockHelper {
023    private JetModifiableBlockHelper() {
024    }
025
026    /**
027     * Tested in OutOfBlockModificationTest
028     */
029    public static boolean shouldChangeModificationCount(PsiElement place) {
030        JetDeclaration declaration = PsiTreeUtil.getParentOfType(place, JetDeclaration.class, true);
031        if (declaration != null) {
032            if (declaration instanceof JetNamedFunction) {
033                JetNamedFunction function = (JetNamedFunction) declaration;
034                if (function.hasDeclaredReturnType() || function.hasBlockBody()) {
035                    return takePartInDeclarationTypeInference(function);
036                }
037
038                return shouldChangeModificationCount(function);
039            }
040            else if (declaration instanceof JetPropertyAccessor) {
041                return takePartInDeclarationTypeInference(declaration);
042            }
043            else if (declaration instanceof JetProperty) {
044                JetProperty property = (JetProperty) declaration;
045                if (property.getTypeRef() != null) {
046                    return takePartInDeclarationTypeInference(property);
047                }
048
049                return shouldChangeModificationCount(property);
050            }
051            else if (declaration instanceof JetMultiDeclaration || declaration instanceof JetMultiDeclarationEntry) {
052                return shouldChangeModificationCount(declaration);
053            }
054            else if (declaration instanceof JetFunctionLiteral) {
055                // TODO: Check return type
056                return shouldChangeModificationCount(declaration);
057            }
058        }
059
060        return true;
061    }
062
063    private static boolean takePartInDeclarationTypeInference(PsiElement place) {
064        JetDeclaration declaration = PsiTreeUtil.getParentOfType(place, JetDeclaration.class, true);
065        if (declaration != null) {
066            if (declaration instanceof JetNamedFunction) {
067                JetNamedFunction function = (JetNamedFunction) declaration;
068                if (!function.hasDeclaredReturnType() && !function.hasBlockBody()) {
069                    return true;
070                }
071            }
072            else if (declaration instanceof JetProperty) {
073                JetProperty property = (JetProperty) declaration;
074                if (property.getTypeRef() == null) {
075                    return true;
076                }
077            }
078
079            return takePartInDeclarationTypeInference(declaration);
080        }
081
082        return false;
083    }
084}