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 JetFunction) { 082 List<JetParameter> paramList = ((JetFunction) declaration).getValueParameters(); 083 return jetIndex < paramList.size() ? paramList.get(jetIndex) : null; 084 } 085 086 if (jetIndex != 0) return null; 087 088 JetPropertyAccessor setter = null; 089 if (declaration instanceof JetPropertyAccessor) { 090 JetPropertyAccessor accessor = (JetPropertyAccessor) declaration; 091 setter = accessor.isSetter() ? accessor : null; 092 } 093 else if (declaration instanceof JetProperty) { 094 setter = ((JetProperty) declaration).getSetter(); 095 } 096 097 return setter != null ? setter.getParameter() : null; 098 } 099 100 @NotNull 101 @Override 102 public PsiElement getNavigationElement() { 103 JetParameter origin = getOrigin(); 104 return origin != null ? origin : super.getNavigationElement(); 105 } 106 107 @Override 108 public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException { 109 JetParameter origin = getOrigin(); 110 if (origin != null) { 111 origin.setName(name); 112 } 113 return this; 114 } 115 116 @Override 117 public PsiFile getContainingFile() { 118 JetDeclaration declaration = method.getOrigin(); 119 return declaration != null ? declaration.getContainingFile() : super.getContainingFile(); 120 } 121 122 @NotNull 123 @Override 124 public Language getLanguage() { 125 return JetLanguage.INSTANCE; 126 } 127 128 @NotNull 129 @Override 130 public SearchScope getUseScope() { 131 JetParameter origin = getOrigin(); 132 return origin != null ? origin.getUseScope() : GlobalSearchScope.EMPTY_SCOPE; 133 } 134 135 public KotlinLightMethod getMethod() { 136 return method; 137 } 138 }