001    /*
002     * Copyright 2010-2015 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    
017    package org.jetbrains.kotlin.asJava;
018    
019    import com.intellij.lang.Language;
020    import com.intellij.psi.*;
021    import com.intellij.psi.search.GlobalSearchScope;
022    import com.intellij.psi.search.SearchScope;
023    import com.intellij.util.ArrayUtil;
024    import com.intellij.util.IncorrectOperationException;
025    import org.jetbrains.annotations.NonNls;
026    import org.jetbrains.annotations.NotNull;
027    import org.jetbrains.annotations.Nullable;
028    import org.jetbrains.kotlin.idea.JetLanguage;
029    import org.jetbrains.kotlin.psi.*;
030    import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage;
031    
032    import java.util.List;
033    
034    public class KotlinLightParameter extends LightParameter implements KotlinLightElement<JetParameter, PsiParameter> {
035        private static String getName(PsiParameter delegate, int index) {
036            String name = delegate.getName();
037            return name != null ? name : "p" + index;
038        }
039    
040        private final PsiModifierList modifierList;
041        private final PsiParameter delegate;
042        private final int index;
043        private final KotlinLightMethod method;
044    
045        public KotlinLightParameter(final PsiParameter delegate, int index, KotlinLightMethod method) {
046            super(getName(delegate, index), delegate.getType(), method, JetLanguage.INSTANCE);
047    
048            this.delegate = delegate;
049            this.index = index;
050            this.method = method;
051    
052            this.modifierList = new KotlinLightModifierList(method.getManager(), ArrayUtil.EMPTY_STRING_ARRAY) {
053                @Override
054                public PsiAnnotationOwner getDelegate() {
055                    return delegate.getModifierList();
056                }
057            };
058        }
059    
060        @NotNull
061        @Override
062        public PsiModifierList getModifierList() {
063            return modifierList;
064        }
065    
066        @NotNull
067        @Override
068        public PsiParameter getDelegate() {
069            return delegate;
070        }
071    
072        @Nullable
073        @Override
074        public JetParameter getOrigin() {
075            JetDeclaration declaration = method.getOrigin();
076            if (declaration == null) return null;
077    
078            int jetIndex = PsiUtilPackage.isExtensionDeclaration(declaration) ? index - 1 : index;
079            if (jetIndex < 0) return null;
080    
081            if (declaration instanceof JetNamedFunction) {
082                List<JetParameter> paramList = ((JetNamedFunction) declaration).getValueParameters();
083                return jetIndex < paramList.size() ? paramList.get(jetIndex) : null;
084            }
085    
086            if (declaration instanceof JetClass) {
087                List<JetParameter> paramList = ((JetClass) declaration).getPrimaryConstructorParameters();
088                return jetIndex < paramList.size() ? paramList.get(jetIndex) : null;
089            }
090    
091            if (jetIndex != 0) return null;
092    
093            JetPropertyAccessor setter = null;
094            if (declaration instanceof JetPropertyAccessor) {
095                JetPropertyAccessor accessor = (JetPropertyAccessor) declaration;
096                setter = accessor.isSetter() ? accessor : null;
097            }
098            else if (declaration instanceof JetProperty) {
099                setter = ((JetProperty) declaration).getSetter();
100            }
101    
102            return setter != null ? setter.getParameter() : null;
103        }
104    
105        @NotNull
106        @Override
107        public PsiElement getNavigationElement() {
108            JetParameter origin = getOrigin();
109            return origin != null ? origin : super.getNavigationElement();
110        }
111    
112        @Override
113        public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
114            JetParameter origin = getOrigin();
115            if (origin != null) {
116                origin.setName(name);
117            }
118            return this;
119        }
120    
121        @Override
122        public PsiFile getContainingFile() {
123            JetDeclaration declaration = method.getOrigin();
124            return declaration != null ? declaration.getContainingFile() : super.getContainingFile();
125        }
126    
127        @NotNull
128        @Override
129        public Language getLanguage() {
130            return JetLanguage.INSTANCE;
131        }
132    
133        @NotNull
134        @Override
135        public SearchScope getUseScope() {
136            JetParameter origin = getOrigin();
137            return origin != null ? origin.getUseScope() : GlobalSearchScope.EMPTY_SCOPE;
138        }
139    }