From 5b15112b47dc47e70c529f18f3ea18a2548156e6 Mon Sep 17 00:00:00 2001 From: Vincent Dechenaux Date: Sun, 1 Oct 2017 23:44:34 +0200 Subject: [PATCH 1/2] Fix autocomplete on mock objects --- META-INF/plugin.xml | 6 ++- .../plugin/atoum/MockTypeProvider.java | 41 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/org/atoum/intellij/plugin/atoum/MockTypeProvider.java diff --git a/META-INF/plugin.xml b/META-INF/plugin.xml index 4fff986..444e457 100644 --- a/META-INF/plugin.xml +++ b/META-INF/plugin.xml @@ -38,6 +38,10 @@ + + + + @@ -79,4 +83,4 @@ com.jetbrains.php com.intellij.modules.platform - \ No newline at end of file + diff --git a/src/org/atoum/intellij/plugin/atoum/MockTypeProvider.java b/src/org/atoum/intellij/plugin/atoum/MockTypeProvider.java new file mode 100644 index 0000000..2efe685 --- /dev/null +++ b/src/org/atoum/intellij/plugin/atoum/MockTypeProvider.java @@ -0,0 +1,41 @@ +package org.atoum.intellij.plugin.atoum; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import com.jetbrains.php.lang.psi.elements.ClassReference; +import com.jetbrains.php.lang.psi.elements.NewExpression; +import com.jetbrains.php.lang.psi.elements.PhpNamedElement; +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'; + } + + @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())); + } + } + + return null; + } + + @Override + public Collection getBySignature(String s, Set set, int i, Project project) { + return null; + } +} From 291615dbfa8914ee5bb710142f9aba30f7a1f5fc Mon Sep 17 00:00:00 2001 From: Vincent Dechenaux Date: Tue, 17 Oct 2017 23:32:07 +0200 Subject: [PATCH 2/2] Mocks: add support of newMockInstance method --- .../plugin/atoum/MockTypeProvider.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/org/atoum/intellij/plugin/atoum/MockTypeProvider.java b/src/org/atoum/intellij/plugin/atoum/MockTypeProvider.java index 2efe685..b4f9623 100644 --- a/src/org/atoum/intellij/plugin/atoum/MockTypeProvider.java +++ b/src/org/atoum/intellij/plugin/atoum/MockTypeProvider.java @@ -2,9 +2,7 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; -import com.jetbrains.php.lang.psi.elements.ClassReference; -import com.jetbrains.php.lang.psi.elements.NewExpression; -import com.jetbrains.php.lang.psi.elements.PhpNamedElement; +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; @@ -29,6 +27,26 @@ public PhpType getType(PsiElement psiElement) { if (ref != null && ref.getFQN() != null && ref.getFQN().startsWith("\\mock\\")) { return new PhpType().add(ref.getFQN().substring("\\mock".length())); } + } 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;