Skip to content

Commit

Permalink
Added arguments and annotations delegation to the mojo lite subs
Browse files Browse the repository at this point in the history
  • Loading branch information
hurricup committed Jun 24, 2019
1 parent 27e5ea9 commit 8804fe0
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2015-2019 Alexandr Evstigneev
*
* 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 com.perl5.lang.perl.extensions.mojo;

import com.intellij.openapi.util.AtomicNotNullLazyValue;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.PsiNamedElement;
import com.intellij.util.Processor;
import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlCallValue;
import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue;
import com.perl5.lang.perl.psi.PerlSub;
import com.perl5.lang.perl.psi.PerlSubDefinition;
import com.perl5.lang.perl.psi.impl.PerlUseStatementElement;
import com.perl5.lang.perl.psi.light.PerlLightSubDefinitionElement;
import com.perl5.lang.perl.psi.stubs.PerlStubElementTypes;
import com.perl5.lang.perl.psi.utils.PerlSubAnnotations;
import com.perl5.lang.perl.psi.utils.PerlSubArgument;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MojoLightDelegatingSubDefinition extends PerlLightSubDefinitionElement<PerlUseStatementElement> {
public MojoLightDelegatingSubDefinition(@NotNull PerlUseStatementElement useStatementElement,
@Nullable String namespaceName,
@NotNull String name,
@NotNull AtomicNotNullLazyValue<PerlValue> returnValueFromCodeProvider) {
super(useStatementElement, name, PerlStubElementTypes.LIGHT_METHOD_DEFINITION, null, namespaceName, null, Collections.emptyList(),
returnValueFromCodeProvider,
null);
setImplicit(true);
}

@NotNull
@Override
public List<PerlSubArgument> getSubArgumentsList() {
Ref<List<PerlSubArgument>> argumentsRef = Ref.create(super.getSubArgumentsList());
processTargets(it -> {
if (it instanceof PerlSubDefinition) {
List<PerlSubArgument> list = new ArrayList<>(((PerlSubDefinition)it).getSubArgumentsList());
if (!list.isEmpty()) {
list.remove(0);
}
argumentsRef.set(list);
return false;
}
return true;
});
return argumentsRef.get();
}

@Nullable
@Override
public PerlSubAnnotations getAnnotations() {
Ref<PerlSubAnnotations> annotationsRef = Ref.create(super.getAnnotations());
processTargets(it -> {
if (it instanceof PerlSub) {
PerlSubAnnotations annotations = ((PerlSub)it).getAnnotations();
if (annotations != null) {
annotationsRef.set(annotations);
return false;
}
}
return true;
});
return super.getAnnotations();
}

/**
* Processes this delegating sub targets if possible
*/
public boolean processTargets(Processor<PsiNamedElement> processor) {
PerlValue returnValue = getReturnValue();
return !(returnValue instanceof PerlCallValue) ||
((PerlCallValue)returnValue).processCallTargets(getDelegate(), it -> it.equals(this) || processor.process(it));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@
import com.perl5.lang.perl.internals.PerlFeaturesTable;
import com.perl5.lang.perl.psi.impl.PerlUseStatementElement;
import com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement;
import com.perl5.lang.perl.psi.light.PerlLightSubDefinitionElement;
import com.perl5.lang.perl.psi.stubs.PerlStubElementTypes;
import com.perl5.lang.perl.psi.stubs.imports.PerlUseStatementStub;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValues.UNKNOWN_VALUE;
Expand Down Expand Up @@ -93,23 +90,15 @@ public List<PerlDelegatingLightNamedElement> computeLightElementsFromPsi(@NotNul
}

@NotNull
public PerlLightSubDefinitionElement<PerlUseStatementElement> createLightMethod(@NotNull PerlUseStatementElement useStatementElement,
String contextNamespace,
String methodName,
PerlValue app) {
PerlLightSubDefinitionElement<PerlUseStatementElement> method = new PerlLightSubDefinitionElement<>(
public MojoLightDelegatingSubDefinition createLightMethod(@NotNull PerlUseStatementElement useStatementElement,
String contextNamespace,
String methodName,
PerlValue app) {
return new MojoLightDelegatingSubDefinition(
useStatementElement,
methodName,
PerlStubElementTypes.LIGHT_METHOD_DEFINITION,
null,
contextNamespace,
null,
Collections.emptyList(),
PerlValuesManager.lazy(app),
null
contextNamespace, methodName,
PerlValuesManager.lazy(app)
);
method.setImplicit(true);
return method;
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import com.intellij.util.IncorrectOperationException;
import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue;
import com.perl5.lang.perl.psi.PerlSubDefinitionElement;
import com.perl5.lang.perl.psi.light.PerlLightSubDefinitionElement;
import com.perl5.lang.perl.psi.utils.PerlSubAnnotations;
import com.perl5.lang.perl.psi.utils.PerlSubArgument;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

public class PerlLightSubDefinitionElementType extends PerlSubDefinitionElementType {
Expand All @@ -48,6 +50,20 @@ public final PerlSubDefinitionElement createPsi(@NotNull PerlSubDefinitionStub s
return PerlLightSubDefinitionsReverseIndex.KEY;
}

@NotNull
@Override
public PerlSubDefinitionStub createStub(@NotNull PerlSubDefinitionElement psi, StubElement parentStub) {
if (psi instanceof PerlLightSubDefinitionElement && ((PerlLightSubDefinitionElement)psi).isImplicit()) {
return createStubElement(parentStub,
psi.getNamespaceName(),
psi.getSubName(),
Collections.emptyList(),
psi.getReturnValueFromCode(),
null);
}
return super.createStub(psi, parentStub);
}

@NotNull
@Override
protected PerlSubDefinitionStub createStubElement(StubElement parentStub,
Expand Down

0 comments on commit 8804fe0

Please sign in to comment.