Add light parameters for functions
This commit is contained in:
committed by
Nikolay Krasko
parent
f8357aecc1
commit
4348678cea
+26
@@ -23,14 +23,38 @@ import com.intellij.psi.PsiManager
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiNamedElement
|
||||
import org.jetbrains.jet.lang.psi.JetDeclaration
|
||||
import com.intellij.psi.PsiParameterList
|
||||
import org.jetbrains.jet.plugin.JetLanguage
|
||||
import kotlin.properties.Delegates
|
||||
import org.jetbrains.jet.asJava.light.LightParameter
|
||||
import org.jetbrains.jet.asJava.light.LightParameterListBuilder
|
||||
import org.jetbrains.jet.lang.resolve.java.jetAsJava.KotlinLightMethod
|
||||
import com.intellij.psi.PsiParameterList
|
||||
import org.jetbrains.jet.plugin.JetLanguage
|
||||
import kotlin.properties.Delegates
|
||||
import com.intellij.psi.util.CachedValuesManager
|
||||
import com.intellij.psi.util.PsiModificationTracker
|
||||
import com.intellij.psi.util.CachedValueProvider
|
||||
import com.intellij.openapi.application.Result
|
||||
import com.intellij.psi.util.CachedValue
|
||||
|
||||
public class KotlinLightMethodForDeclaration(manager: PsiManager, val method: PsiMethod, val jetDeclaration: JetDeclaration, containingClass: PsiClass):
|
||||
LightMethod(manager, method, containingClass), KotlinLightMethod {
|
||||
|
||||
private val paramsList: CachedValue<PsiParameterList> by Delegates.blockingLazy {
|
||||
val cacheManager = CachedValuesManager.getManager(method.getProject())
|
||||
cacheManager.createCachedValue<PsiParameterList>({
|
||||
val parameterBuilder = LightParameterListBuilder(getManager(), JetLanguage.INSTANCE)
|
||||
|
||||
for ((index, parameter) in method.getParameterList().getParameters().withIndices()) {
|
||||
val lightParameter = LightParameter(parameter.getName() ?: "p$index", parameter.getType(), this, JetLanguage.INSTANCE)
|
||||
parameterBuilder.addParameter(lightParameter)
|
||||
}
|
||||
|
||||
CachedValueProvider.Result.create(parameterBuilder, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT)
|
||||
}, false)
|
||||
}
|
||||
|
||||
override fun getNavigationElement() : PsiElement = jetDeclaration
|
||||
override fun getOriginalElement() : PsiElement = jetDeclaration
|
||||
override fun getOrigin(): JetDeclaration? = jetDeclaration
|
||||
@@ -56,6 +80,8 @@ public class KotlinLightMethodForDeclaration(manager: PsiManager, val method: Ps
|
||||
return super<LightMethod>.isEquivalentTo(another)
|
||||
}
|
||||
|
||||
override fun getParameterList(): PsiParameterList = paramsList.getValue()!!
|
||||
|
||||
override fun copy(): PsiElement? {
|
||||
return KotlinLightMethodForDeclaration(getManager()!!, method, jetDeclaration.copy() as JetDeclaration, getContainingClass()!!)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.asJava.light;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
// Based on com.intellij.psi.impl.light.LightParameter
|
||||
public class LightParameter extends LightVariableBuilder<LightVariableBuilder> implements PsiParameter {
|
||||
public static final LightParameter[] EMPTY_ARRAY = new LightParameter[0];
|
||||
|
||||
private final String myName;
|
||||
private final PsiElement myDeclarationScope;
|
||||
private final boolean myVarArgs;
|
||||
|
||||
public LightParameter(@NotNull String name, @NotNull PsiType type, PsiElement declarationScope, Language language) {
|
||||
this(name, type, declarationScope, language, type instanceof PsiEllipsisType);
|
||||
}
|
||||
|
||||
public LightParameter(@NotNull String name, @NotNull PsiType type, PsiElement declarationScope, Language language, boolean isVarArgs) {
|
||||
super(declarationScope.getManager(), name, type, language);
|
||||
myName = name;
|
||||
myDeclarationScope = declarationScope;
|
||||
myVarArgs = isVarArgs;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElement getDeclarationScope() {
|
||||
return myDeclarationScope;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(@NotNull PsiElementVisitor visitor) {
|
||||
if (visitor instanceof JavaElementVisitor) {
|
||||
((JavaElementVisitor)visitor).visitParameter(this);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Light Parameter";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVarArgs() {
|
||||
return myVarArgs;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return myName;
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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.asJava.light;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.light.LightElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
// Copy of com.intellij.psi.impl.light.LightParameterListBuilder
|
||||
public class LightParameterListBuilder extends LightElement implements PsiParameterList {
|
||||
private final List<PsiParameter> myParameters = new ArrayList<PsiParameter>();
|
||||
private PsiParameter[] myCachedParameters;
|
||||
|
||||
public LightParameterListBuilder(PsiManager manager, Language language) {
|
||||
super(manager, language);
|
||||
}
|
||||
|
||||
public void addParameter(PsiParameter parameter) {
|
||||
myParameters.add(parameter);
|
||||
myCachedParameters = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Light parameter lsit";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiParameter[] getParameters() {
|
||||
if (myCachedParameters == null) {
|
||||
if (myParameters.isEmpty()) {
|
||||
myCachedParameters = PsiParameter.EMPTY_ARRAY;
|
||||
}
|
||||
else {
|
||||
myCachedParameters = myParameters.toArray(new PsiParameter[myParameters.size()]);
|
||||
}
|
||||
}
|
||||
|
||||
return myCachedParameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getParameterIndex(PsiParameter parameter) {
|
||||
return myParameters.indexOf(parameter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getParametersCount() {
|
||||
return myParameters.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(@NotNull PsiElementVisitor visitor) {
|
||||
if (visitor instanceof JavaElementVisitor) {
|
||||
((JavaElementVisitor) visitor).visitParameterList(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* 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.asJava.light;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.navigation.NavigationItem;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.ElementPresentationUtil;
|
||||
import com.intellij.psi.impl.light.LightElement;
|
||||
import com.intellij.psi.impl.light.LightModifierList;
|
||||
import com.intellij.ui.RowIcon;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
// Based on com.intellij.psi.impl.light.LightVariableBuilder
|
||||
public class LightVariableBuilder<T extends LightVariableBuilder> extends LightElement implements PsiVariable, NavigationItem {
|
||||
private final String myName;
|
||||
private final PsiType myType;
|
||||
private volatile LightModifierList myModifierList;
|
||||
private volatile Icon myBaseIcon = PlatformIcons.VARIABLE_ICON;
|
||||
private String myOriginInfo;
|
||||
|
||||
public LightVariableBuilder(@NotNull String name, @NotNull String type, @NotNull PsiElement navigationElement) {
|
||||
this(name, JavaPsiFacade.getElementFactory(navigationElement.getProject()).createTypeFromText(type, navigationElement), navigationElement);
|
||||
}
|
||||
|
||||
public LightVariableBuilder(@NotNull String name, @NotNull PsiType type, @NotNull PsiElement navigationElement) {
|
||||
this(navigationElement.getManager(), name, type, JavaLanguage.INSTANCE);
|
||||
setNavigationElement(navigationElement);
|
||||
}
|
||||
|
||||
public LightVariableBuilder(PsiManager manager, @NotNull String name, @NotNull PsiType type, Language language) {
|
||||
super(manager, language);
|
||||
myName = name;
|
||||
myType = type;
|
||||
myModifierList = new LightModifierList(manager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LightVariableBuilder:" + getName();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiType getType() {
|
||||
return myType;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public PsiModifierList getModifierList() {
|
||||
return myModifierList;
|
||||
}
|
||||
|
||||
public T setModifiers(String... modifiers) {
|
||||
myModifierList = new LightModifierList(getManager(), getLanguage(), modifiers);
|
||||
return (T)this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasModifierProperty(@NonNls @NotNull String name) {
|
||||
return myModifierList.hasModifierProperty(name);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return myName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiTypeElement getTypeElement() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiExpression getInitializer() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasInitializer() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void normalizeDeclaration() throws IncorrectOperationException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object computeConstantValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiIdentifier getNameIdentifier() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
|
||||
throw new UnsupportedOperationException("setName is not implemented yet in org.jetbrains.jet.asJava.light.LightVariableBuilder");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isVisibilitySupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Icon getElementIcon(final int flags) {
|
||||
final RowIcon baseIcon = ElementPresentationUtil.createLayeredIcon(myBaseIcon, this, false);
|
||||
return ElementPresentationUtil.addVisibilityIcon(this, flags, baseIcon);
|
||||
}
|
||||
|
||||
public T setBaseIcon(Icon baseIcon) {
|
||||
myBaseIcon = baseIcon;
|
||||
return (T)this;
|
||||
}
|
||||
|
||||
//@Nullable
|
||||
//@Override
|
||||
//public String getOriginInfo() {
|
||||
// return myOriginInfo;
|
||||
//}
|
||||
|
||||
public void setOriginInfo(@Nullable String originInfo) {
|
||||
myOriginInfo = originInfo;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user