Skip to content

Commit

Permalink
Added resolve test for the PodWeaver
Browse files Browse the repository at this point in the history
Additionally, fixed resolve from pod to the light definitions

#1839
  • Loading branch information
hurricup committed Feb 14, 2019
1 parent 3b584f3 commit 9def4b9
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 14 deletions.
Expand Up @@ -25,9 +25,11 @@
import com.intellij.psi.ResolveResult;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.perl5.lang.perl.psi.PerlPolyNamedElement;
import com.perl5.lang.perl.psi.PerlSubDeclarationElement;
import com.perl5.lang.perl.psi.PerlSubDefinitionElement;
import com.perl5.lang.perl.psi.PerlSubElement;
import com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement;
import com.perl5.lang.perl.psi.references.PerlCachingReference;
import com.perl5.lang.perl.util.PerlPackageUtil;
import com.perl5.lang.perl.util.PerlSubUtil;
Expand All @@ -37,6 +39,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
* Created by hurricup on 05.04.2016.
Expand Down Expand Up @@ -64,6 +67,7 @@ protected ResolveResult[] resolveInner(boolean incompleteCode) {
String subName = element.getText();
if (StringUtil.isNotEmpty(subName)) {
final PsiFile containingFile = element.getContainingFile();
// this is a bit lame. we could try to detect from perl context. But unsure
String packageName = PodFileUtil.getPackageName(containingFile);

List<ResolveResult> results = new ArrayList<>();
Expand All @@ -79,17 +83,29 @@ protected ResolveResult[] resolveInner(boolean incompleteCode) {
}

if (results.isEmpty()) {
final PsiFile perlFile = containingFile.getViewProvider().getStubBindingRoot();

for (PerlSubElement subBase : PsiTreeUtil.findChildrenOfType(perlFile, PerlSubElement.class)) {
String subBaseName = subBase.getName();
Consumer<? super PerlSubElement> subConsumer = it -> {
String subBaseName = it.getName();
if (subBaseName != null && StringUtil.equals(subBaseName, subName)) {
results.add(new PsiElementResolveResult(subBase));
results.add(new PsiElementResolveResult(it));
}
}
};

PsiTreeUtil.processElements(containingFile.getViewProvider().getStubBindingRoot(), it -> {
if (it instanceof PerlSubElement) {
subConsumer.accept((PerlSubElement)it);
}
else if (it instanceof PerlPolyNamedElement) {
for (PerlDelegatingLightNamedElement lightElement : ((PerlPolyNamedElement)it).getLightElements()) {
if (lightElement instanceof PerlSubElement) {
subConsumer.accept((PerlSubElement)lightElement);
}
}
}
return true;
});
}

return results.toArray(new ResolveResult[results.size()]);
return results.toArray(ResolveResult.EMPTY_ARRAY);
}

return ResolveResult.EMPTY_ARRAY;
Expand Down
19 changes: 12 additions & 7 deletions core/test/base/PerlLightTestCaseBase.java
Expand Up @@ -99,6 +99,7 @@
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.perl5.lang.perl.PerlLanguage;
import com.perl5.lang.perl.extensions.PerlImplicitVariablesProvider;
import com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor;
import com.perl5.lang.perl.fileTypes.PerlFileTypeScript;
Expand Down Expand Up @@ -127,6 +128,7 @@
import com.perl5.lang.perl.psi.mixins.PerlStringMixin;
import com.perl5.lang.perl.util.PerlPackageUtil;
import com.perl5.lang.perl.util.PerlRunUtil;
import com.perl5.lang.pod.PodLanguage;
import gnu.trove.THashSet;
import junit.framework.AssertionFailedError;
import org.intellij.plugins.intelliLang.inject.InjectLanguageAction;
Expand Down Expand Up @@ -505,14 +507,17 @@ private String serializeReference(PsiReference reference) {
private List<PsiReference> collectFileReferences() {
final List<PsiReference> references = new ArrayList<>();

PsiFile file = getFile();

file.accept(new PsiElementVisitor() {
@Override
public void visitElement(PsiElement element) {
Collections.addAll(references, element.getReferences());
element.acceptChildren(this);
getFile().getViewProvider().getAllFiles().forEach(file -> {
if (!file.getLanguage().isKindOf(PerlLanguage.INSTANCE) && !file.getLanguage().isKindOf(PodLanguage.INSTANCE)) {
return;
}
file.accept(new PsiElementVisitor() {
@Override
public void visitElement(PsiElement element) {
Collections.addAll(references, element.getReferences());
element.acceptChildren(this);
}
});
});

references.sort((o1, o2) -> o1.getElement().getTextRange().getStartOffset() + o1.getRangeInElement().getStartOffset() -
Expand Down
2 changes: 2 additions & 0 deletions core/test/resolve/perl/PerlResolveTest.java
Expand Up @@ -39,6 +39,8 @@ public void testCpanfile() {
checkSerializedReferencesWithFile();
}

public void testPodWeaverTags() {doTest();}

public void testReadonly() {doTest();}

public void testTypesStandard() {doTest();}
Expand Down
69 changes: 69 additions & 0 deletions core/testData/resolve/perl/podWeaverTags.code
@@ -0,0 +1,69 @@
=pod

=head1 METHODS

=method somemethod

Method description

=cut

method somemethod{

}

=head1 ATTRIBUTES

=attr someattr

Attribute description

=cut

has someattr => (is => 'rw');

=head1 FUNCTIONS

=func somefunc

Function dscription

=cut

func somefunc{
}

=head1 OVERVIEW

Some overview text


=method othermethod

Other method description

=cut

method othermethod{

}

=func otherfunc

Other func description

=cut

func otherfunc{

}

=attr otherattr

Other attr description

=cut

has otherattr => sub{

};
30 changes: 30 additions & 0 deletions core/testData/resolve/perl/podWeaverTags.pl.txt
@@ -0,0 +1,30 @@
PodSubReference at (13,20); text in range: 'METHODS' => 0 results:

PodSubReference at (30,40); text in range: 'somemethod' => 1 results:
PsiPerlMethodDefinitionImpl(METHOD_DEFINITION)@main::somemethod at 68 in podWeaverTags.pl

PodSubReference at (98,108); text in range: 'ATTRIBUTES' => 0 results:

PodSubReference at (116,124); text in range: 'someattr' => 1 results:
PerlAttributeDefinition(LIGHT_ATTRIBUTE)@main::someattr($self, [$new_value]); navigated to: PsiPerlStringBareImpl(Perl5: STRING_BARE) at 159 in podWeaverTags.pl

PerlSubReference at (155,158); text in range: 'has' => 0 results:

PodSubReference at (193,202); text in range: 'FUNCTIONS' => 0 results:

PodSubReference at (210,218); text in range: 'somefunc' => 1 results:
PsiPerlFuncDefinitionImpl(FUNC_DEFINITION)@main::somefunc at 247 in podWeaverTags.pl

PodSubReference at (272,280); text in range: 'OVERVIEW' => 0 results:

PodSubReference at (311,322); text in range: 'othermethod' => 1 results:
PsiPerlMethodDefinitionImpl(METHOD_DEFINITION)@main::othermethod at 356 in podWeaverTags.pl

PodSubReference at (386,395); text in range: 'otherfunc' => 1 results:
PsiPerlFuncDefinitionImpl(FUNC_DEFINITION)@main::otherfunc at 427 in podWeaverTags.pl

PodSubReference at (453,462); text in range: 'otherattr' => 1 results:
PerlLightMethodDefinitionElement(LIGHT_METHOD_DEFINITION)@main::otherattr($self, [$new_value]); navigated to: PsiPerlStringBareImpl(Perl5: STRING_BARE) at 498 in podWeaverTags.pl

PerlSubReference at (494,497); text in range: 'has' => 0 results:

0 comments on commit 9def4b9

Please sign in to comment.