From 89255b25b0211edfb44fae78dff7f9296e2eba8c Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Thu, 23 Jan 2014 18:12:33 +0400 Subject: [PATCH] Make light parameters compatible with KotlinLightElement --- .../asJava/KotlinLightMethodForDeclaration.kt | 3 +- .../jet/asJava/KotlinLightParameter.java | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightParameter.java diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightMethodForDeclaration.kt b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightMethodForDeclaration.kt index 6f62a0611d0..b58493c24e3 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightMethodForDeclaration.kt +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightMethodForDeclaration.kt @@ -49,8 +49,7 @@ public class KotlinLightMethodForDeclaration( val parameterBuilder = LightParameterListBuilder(getManager(), JetLanguage.INSTANCE) for ((index, parameter) in delegate.getParameterList().getParameters().withIndices()) { - val lightParameter = LightParameter(parameter.getName() ?: "p$index", parameter.getType(), this, JetLanguage.INSTANCE) - parameterBuilder.addParameter(lightParameter) + parameterBuilder.addParameter(KotlinLightParameter(parameter, index, this)) } CachedValueProvider.Result.create(parameterBuilder, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT) diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightParameter.java b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightParameter.java new file mode 100644 index 00000000000..ecec9743f10 --- /dev/null +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightParameter.java @@ -0,0 +1,81 @@ +/* + * 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.asJava; + +import com.intellij.psi.PsiParameter; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.asJava.light.LightParameter; +import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage; +import org.jetbrains.jet.lang.resolve.java.jetAsJava.KotlinLightElement; +import org.jetbrains.jet.lang.resolve.java.jetAsJava.KotlinLightMethod; +import org.jetbrains.jet.plugin.JetLanguage; + +import java.util.List; + +public class KotlinLightParameter extends LightParameter implements KotlinLightElement { + private static String getName(PsiParameter delegate, int index) { + String name = delegate.getName(); + return name != null ? name : "p" + index; + } + + private final PsiParameter delegate; + private final int index; + private final KotlinLightMethod method; + + public KotlinLightParameter(PsiParameter delegate, int index, KotlinLightMethod method) { + super(getName(delegate, index), delegate.getType(), method, JetLanguage.INSTANCE); + this.delegate = delegate; + this.index = index; + this.method = method; + } + + @NotNull + @Override + public PsiParameter getDelegate() { + return delegate; + } + + @Nullable + @Override + public JetParameter getOrigin() { + JetDeclaration declaration = method.getOrigin(); + if (declaration == null) return null; + + int jetIndex = PsiUtilPackage.isExtensionDeclaration(declaration) ? index - 1 : index; + if (jetIndex < 0) return null; + + if (declaration instanceof JetNamedFunction) { + List paramList = ((JetNamedFunction) declaration).getValueParameters(); + return jetIndex < paramList.size() ? paramList.get(jetIndex) : null; + } + + if (jetIndex != 0) return null; + + JetPropertyAccessor setter = null; + if (declaration instanceof JetPropertyAccessor) { + JetPropertyAccessor accessor = (JetPropertyAccessor) declaration; + setter = accessor.isSetter() ? accessor : null; + } + else if (declaration instanceof JetProperty) { + setter = ((JetProperty) declaration).getSetter(); + } + + return setter != null ? setter.getParameter() : null; + } +} \ No newline at end of file