Skip to content

Commit

Permalink
feat(type-provider): PHPUnits TestCase autocompletion for $this (#11)
Browse files Browse the repository at this point in the history
Add basic auto completion when using `$this` inside closure in pest tests.

The autocompletion is only for phpunit testcase.
  • Loading branch information
adelf authored Jul 1, 2020
1 parent 5ec0262 commit 77d3126
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/main/java/com/pestphp/pest/types/ThisTypeProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.pestphp.pest.types;

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.psi.elements.*;
import com.jetbrains.php.lang.psi.elements.impl.FunctionReferenceImpl;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider4;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class ThisTypeProvider implements PhpTypeProvider4 {
private final Set<String> PEST_TEST_FUNCTION_NAMES = new HashSet<>(Arrays.asList("it", "test"));
private final PhpType TEST_CASE_TYPE = new PhpType().add("\\PHPUnit\\Framework\\TestCase");

@Override
public char getKey() {
return '\u0221';
}

@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
if (!(psiElement instanceof Variable)) return null;
if (!((Variable) psiElement).getName().equals("this")) return null;

Function closure = PsiTreeUtil.getParentOfType(psiElement, Function.class);

if (closure == null || !closure.isClosure()) return null;

if (!(closure.getParent() instanceof PhpExpression)) return null;

if (!(closure.getParent().getParent() instanceof ParameterList)) return null;

ParameterList parameterList = (ParameterList) closure.getParent().getParent();

if (!closure.getParent().equals(parameterList.getParameter(1))) return null;

if (!(parameterList.getParent() instanceof FunctionReferenceImpl)) return null;

if (!PEST_TEST_FUNCTION_NAMES.contains(((FunctionReferenceImpl) parameterList.getParent()).getName())) return null;

return TEST_CASE_TYPE;
}

@Nullable
@Override
public PhpType complete(String s, Project project) {
return null;
}

@Override
public Collection<? extends PhpNamedElement> getBySignature(String s, Set<String> set, int i, Project project) {
return null;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<extensions defaultExtensionNs="com.jetbrains.php">
<testFrameworkType implementation="com.pestphp.pest.PestFrameworkType"/>
<composerConfigClient implementation="com.pestphp.pest.PestComposerConfig"/>

<typeProvider4 implementation="com.pestphp.pest.types.ThisTypeProvider"/>
</extensions>

<actions>
Expand Down

0 comments on commit 77d3126

Please sign in to comment.