Skip to content

Fix autocomplete on mock objects #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<!-- Add your extensions here -->
</extensions>

<extensions defaultExtensionNs="com.jetbrains.php">
<typeProvider3 implementation="org.atoum.intellij.plugin.atoum.MockTypeProvider" />
</extensions>

<application-components>
<!-- Add your application components here -->
</application-components>
Expand Down Expand Up @@ -79,4 +83,4 @@
<depends>com.jetbrains.php</depends>
<depends>com.intellij.modules.platform</depends>

</idea-plugin>
</idea-plugin>
59 changes: 59 additions & 0 deletions src/org/atoum/intellij/plugin/atoum/MockTypeProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.atoum.intellij.plugin.atoum;

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.lang.psi.elements.*;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider3;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Set;

public class MockTypeProvider implements PhpTypeProvider3 {

@Override
public char getKey() {
return '\u0102';
Copy link
Member Author

@vdechenaux vdechenaux Oct 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your custom signature key, i.e. "Я"

https://confluence.jetbrains.com/display/PhpStorm/PHP+Open+API

I checked other projects, like here https://github.com/Haehnchen/idea-php-symfony2-plugin/blob/a3fa7ef7c3d5a57be2a7130cf831d44d7760ea1e/src/fr/adrienbrault/idea/symfony2plugin/doctrine/ObjectManagerFindTypeProvider.java and they seems to use random unicode character.
I used Ă ( 'A' for atoum and it looks like the atoum logo with the horns 😛 )

}

@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
if (psiElement instanceof NewExpression) {
NewExpression expr = (NewExpression) psiElement;
ClassReference ref = expr.getClassReference();

if (ref != null && ref.getFQN() != null && ref.getFQN().startsWith("\\mock\\")) {
return new PhpType().add(ref.getFQN().substring("\\mock".length()));
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the element which need type resolution is a class with FQN like \mock\xxxxxx we return the class \xxxxxx

} else if (psiElement instanceof MethodReference) {
MethodReference expr = (MethodReference) psiElement;
if (expr.getName() == null || !expr.getName().equals("newMockInstance")) {
return null;
}

if (expr.getParameters().length == 0) {
return null;
}

PsiElement param = expr.getParameters()[0];

if (param instanceof ClassConstantReference) {
PhpExpression ref = ((ClassConstantReference) param).getClassReference();
if (ref instanceof ClassReference) {
return new PhpType().add(ref);
}
} else if (param instanceof StringLiteralExpression) {
return new PhpType().add(((StringLiteralExpression) param).getContents());
}
}

return null;
}

@Override
public Collection<? extends PhpNamedElement> getBySignature(String s, Set<String> set, int i, Project project) {
return null;
}
}