diff --git a/META-INF/plugin.xml b/META-INF/plugin.xml index 9931f70..51b6324 100755 --- a/META-INF/plugin.xml +++ b/META-INF/plugin.xml @@ -1,7 +1,7 @@ com.phpuaca PHPUnit Autocomplete Assistant - 1.2.1 + 1.3.0-unstable Max Filatov Preferences... -> Plugins -> Browse repositories ...` and search for PHPUnit Autocomplete Assistant plugin +* Restart PhpStorm + +Latest version: * Go to `PhpStorm -> Preferences... -> Plugins -> Install plugin from disk...` and choose jar file * Restart PhpStorm diff --git a/src/com/phpuaca/completion/StringLiteralContributor.java b/src/com/phpuaca/completion/StringLiteralContributor.java index fbc7160..216558f 100755 --- a/src/com/phpuaca/completion/StringLiteralContributor.java +++ b/src/com/phpuaca/completion/StringLiteralContributor.java @@ -26,8 +26,7 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters if (originalPosition != null) { Filter filter = FilterFactory.getInstance().getFilter(originalPosition.getParent()); if (filter != null) { - Project project = originalPosition.getProject(); - List elements = (new LookupElementProvider()).find(project, filter); + List elements = (new LookupElementProvider()).find(filter); completionResultSet.addAllElements(elements); } } diff --git a/src/com/phpuaca/completion/filter/Filter.java b/src/com/phpuaca/completion/filter/Filter.java index 9cd4856..32bf3e8 100755 --- a/src/com/phpuaca/completion/filter/Filter.java +++ b/src/com/phpuaca/completion/filter/Filter.java @@ -3,7 +3,6 @@ import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocMethod; import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocProperty; import com.jetbrains.php.lang.psi.elements.*; -import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; @@ -18,8 +17,7 @@ abstract public class Filter { private List allowedModifiers; private List disallowedMethods; - private ClassConstantReference classConstantReference; - private String className; + private PhpClass phpClass; public Filter(FilterContext context) { @@ -113,22 +111,13 @@ protected boolean isModifierAllowed(PhpModifier modifier) return isModifierAllowed(modifier.toString()); } - public void setClassConstantReference(@Nullable ClassConstantReference value) + public void setPhpClass(PhpClass phpClass) { - classConstantReference = value; + this.phpClass = phpClass; } - @Nullable - public ClassConstantReference getClassConstantReference() + public PhpClass getPhpClass() { - return classConstantReference; - } - - protected void setClassName(String className) { - this.className = className; - } - - public String getClassName() { - return className; + return phpClass; } } diff --git a/src/com/phpuaca/completion/filter/InvocationMockerFilter.java b/src/com/phpuaca/completion/filter/InvocationMockerFilter.java index 04cefbc..95672ca 100755 --- a/src/com/phpuaca/completion/filter/InvocationMockerFilter.java +++ b/src/com/phpuaca/completion/filter/InvocationMockerFilter.java @@ -23,12 +23,7 @@ public InvocationMockerFilter(FilterContext context) { allowModifier(PhpModifier.PROTECTED_ABSTRACT_DYNAMIC); allowModifier(PhpModifier.PROTECTED_IMPLEMENTED_DYNAMIC); - if (classFinderResult.getClassConstantReference() != null) { - setClassConstantReference(classFinderResult.getClassConstantReference()); - } - if (classFinderResult.getClassName() != null) { - setClassName(classFinderResult.getClassName()); - } + setPhpClass(classFinderResult.getPhpClass()); MethodReference definitionMethodReference = (new PhpMethodChain(methodReference)).findMethodReference("setMethods"); if (definitionMethodReference == null) { diff --git a/src/com/phpuaca/completion/filter/MethodMockFilter.java b/src/com/phpuaca/completion/filter/MethodMockFilter.java index 0544e73..5242f9e 100644 --- a/src/com/phpuaca/completion/filter/MethodMockFilter.java +++ b/src/com/phpuaca/completion/filter/MethodMockFilter.java @@ -3,25 +3,34 @@ import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.php.lang.psi.elements.*; import com.phpuaca.completion.filter.util.ClassFinder; +import com.phpuaca.completion.util.PhpClassResolver; public class MethodMockFilter extends Filter { public MethodMockFilter(FilterContext context) { super(context); + PhpClass phpClass = null; ParameterList parameterList = context.getMethodReference().getParameterList(); ClassConstantReference classConstantReference = PsiTreeUtil.getChildOfType(parameterList, ClassConstantReference.class); if (classConstantReference == null) { + // MethodMock::callProtectedMethod($Variable, 'doSomething'); Variable variable = PsiTreeUtil.getChildOfType(parameterList, Variable.class); if (variable != null) { ClassFinder.Result classFinderResult = (new ClassFinder()).find(variable); if (classFinderResult != null) { - classConstantReference = classFinderResult.getClassConstantReference(); + phpClass = classFinderResult.getPhpClass(); } } + } else { + phpClass = (new PhpClassResolver()).resolveByClassConstantReference(classConstantReference); + } + + if (phpClass == null) { + return; } - setClassConstantReference(classConstantReference); + setPhpClass(phpClass); String methodName = context.getFilterConfigItem().getMethodName(); if (methodName.equals("callProtectedMethod")) { diff --git a/src/com/phpuaca/completion/filter/MockBuilderFilter.java b/src/com/phpuaca/completion/filter/MockBuilderFilter.java index 580daa9..8519a5f 100755 --- a/src/com/phpuaca/completion/filter/MockBuilderFilter.java +++ b/src/com/phpuaca/completion/filter/MockBuilderFilter.java @@ -21,12 +21,7 @@ public MockBuilderFilter(FilterContext context) { ClassFinder.Result classFinderResult = (new ClassFinder()).find(methodReference); if (classFinderResult != null) { - if (classFinderResult.getClassConstantReference() != null) { - setClassConstantReference(classFinderResult.getClassConstantReference()); - } - if (classFinderResult.getClassName() != null) { - setClassName(classFinderResult.getClassName()); - } + setPhpClass(classFinderResult.getPhpClass()); } disallowMethod("__construct"); diff --git a/src/com/phpuaca/completion/filter/util/ClassFinder.java b/src/com/phpuaca/completion/filter/util/ClassFinder.java index 876245d..afd79d8 100644 --- a/src/com/phpuaca/completion/filter/util/ClassFinder.java +++ b/src/com/phpuaca/completion/filter/util/ClassFinder.java @@ -4,6 +4,7 @@ import com.jetbrains.php.lang.psi.elements.*; import com.phpuaca.completion.filter.FilterConfigItem; import com.phpuaca.completion.filter.FilterFactory; +import com.phpuaca.completion.util.PhpClassResolver; import com.phpuaca.completion.util.PhpMethodChain; import com.phpuaca.completion.util.PhpVariable; import org.jetbrains.annotations.NotNull; @@ -33,26 +34,19 @@ public Result find(@NotNull MethodReference methodReference) return null; } + PhpClass phpClass = null; ParameterList parameterList = mockBuilderMethodReference.getParameterList(); ClassConstantReference classConstantReference = PsiTreeUtil.getChildOfType(parameterList, ClassConstantReference.class); if (classConstantReference != null) { - return new Result(classConstantReference, filterConfigItem.getParameterNumber()); - } - - StringLiteralExpression stringLiteral = PsiTreeUtil.getChildOfType(parameterList, StringLiteralExpression.class); - if (stringLiteral != null) { - String className = stringLiteral.getContents(); - - if (!className.isEmpty()) { - // \\ -> \ - className = className.replace("\\\\", "\\"); - if (!className.isEmpty()) { - return new Result(className, filterConfigItem.getParameterNumber()); - } + phpClass = (new PhpClassResolver()).resolveByClassConstantReference(classConstantReference); + } else { + StringLiteralExpression stringLiteralExpression = PsiTreeUtil.getChildOfType(parameterList, StringLiteralExpression.class); + if (stringLiteralExpression != null) { + phpClass = (new PhpClassResolver()).resolveByClassStringLiteralExpression(stringLiteralExpression); } } - return null; + return phpClass == null ? null : new Result(phpClass, filterConfigItem.getParameterNumber()); } @Nullable @@ -63,34 +57,23 @@ public Result find(@NotNull Variable variable) } public class Result { - private ClassConstantReference classConstantReference; - private String className; + private PhpClass phpClass; private int parameterNumber; - public Result(@NotNull ClassConstantReference classConstantReference, int parameterNumber) + public Result(@NotNull PhpClass phpClass, int parameterNumber) { - this.classConstantReference = classConstantReference; + this.phpClass = phpClass; this.parameterNumber = parameterNumber; } - public Result(@NotNull String className, int parameterNumber) + public PhpClass getPhpClass() { - this.className = className; - this.parameterNumber = parameterNumber; - } - - public ClassConstantReference getClassConstantReference() - { - return classConstantReference; + return phpClass; } public int getParameterNumber() { return parameterNumber; } - - public String getClassName() { - return className; - } } } diff --git a/src/com/phpuaca/completion/provider/LookupElementProvider.java b/src/com/phpuaca/completion/provider/LookupElementProvider.java index 5fb8110..0ac7a2f 100644 --- a/src/com/phpuaca/completion/provider/LookupElementProvider.java +++ b/src/com/phpuaca/completion/provider/LookupElementProvider.java @@ -1,14 +1,11 @@ package com.phpuaca.completion.provider; import com.intellij.codeInsight.lookup.LookupElement; -import com.intellij.openapi.project.Project; import com.jetbrains.php.completion.PhpLookupElement; -import com.jetbrains.php.lang.psi.elements.ClassConstantReference; import com.jetbrains.php.lang.psi.elements.Field; import com.jetbrains.php.lang.psi.elements.Method; import com.jetbrains.php.lang.psi.elements.PhpClass; import com.phpuaca.completion.filter.Filter; -import com.phpuaca.completion.util.PhpClassResolver; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -17,31 +14,18 @@ public class LookupElementProvider { @NotNull - public List find(@NotNull Project project, @NotNull Filter filter) + public List find(@NotNull Filter filter) { List list = new ArrayList(); - ClassConstantReference classConstantReference = filter.getClassConstantReference(); - PhpClass resolvedClass = null; + PhpClass phpClass = filter.getPhpClass(); - if (classConstantReference != null) { - PhpClassResolver resolver = new PhpClassResolver(classConstantReference); - if (resolver.resolve()) { - resolvedClass = resolver.getResolvedClass(); - } - } - - String className = filter.getClassName(); - if (className != null) { - resolvedClass = PhpClassResolver.getClass(project, className); - } - - if (resolvedClass != null) { - for (Method method : resolvedClass.getMethods()) { + if (phpClass != null) { + for (Method method : phpClass.getMethods()) { if (filter.isMethodAllowed(method)) { list.add(new PhpLookupElement(method)); } } - for (Field field : resolvedClass.getFields()) { + for (Field field : phpClass.getFields()) { if (!field.isConstant() && filter.isFieldAllowed(field)) { list.add(new PhpLookupElement(field)); } diff --git a/src/com/phpuaca/completion/util/IResolver.java b/src/com/phpuaca/completion/util/IResolver.java deleted file mode 100644 index a9bfb85..0000000 --- a/src/com/phpuaca/completion/util/IResolver.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.phpuaca.completion.util; - -public interface IResolver { - public boolean resolve(); -} diff --git a/src/com/phpuaca/completion/util/PhpClassResolver.java b/src/com/phpuaca/completion/util/PhpClassResolver.java index 3b853f4..fe43b6b 100644 --- a/src/com/phpuaca/completion/util/PhpClassResolver.java +++ b/src/com/phpuaca/completion/util/PhpClassResolver.java @@ -3,59 +3,44 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.php.PhpIndex; -import com.jetbrains.php.lang.psi.elements.ClassConstantReference; -import com.jetbrains.php.lang.psi.elements.ClassReference; -import com.jetbrains.php.lang.psi.elements.PhpClass; -import com.jetbrains.php.lang.psi.elements.PhpNamedElement; +import com.jetbrains.php.lang.psi.elements.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Collection; -final public class PhpClassResolver implements IResolver { +final public class PhpClassResolver { - private ClassReference classReference; - private PhpClass resolvedClass; - - public PhpClassResolver(@NotNull ClassReference classReference) - { - this.classReference = classReference; - } - - public PhpClassResolver(@NotNull ClassConstantReference classConstantReference) - { - this.classReference = PsiTreeUtil.getChildOfType(classConstantReference, ClassReference.class); - } - - public boolean resolve() + @Nullable + public PhpClass resolveByClassConstantReference(@NotNull ClassConstantReference classConstantReference) { + ClassReference classReference = PsiTreeUtil.getChildOfType(classConstantReference, ClassReference.class); if (classReference != null) { - Collection resolvedCollection = classReference.resolveGlobal(false); + Collection resolvedCollection = classReference.resolveGlobal(false); if (!resolvedCollection.isEmpty()) { PhpNamedElement resolvedElement = resolvedCollection.iterator().next(); if (resolvedElement instanceof PhpClass) { - resolvedClass = (PhpClass) resolvedElement; - return true; + return (PhpClass) resolvedElement; } } } - return false; - } - - public PhpClass getResolvedClass() - { - return resolvedClass; + return null; } @Nullable - static public PhpClass getClass(Project project, String className) { - return getClass(PhpIndex.getInstance(project), className); - } + public PhpClass resolveByClassStringLiteralExpression(@NotNull StringLiteralExpression stringLiteralExpression) + { + String className = stringLiteralExpression.getContents(); + if (!className.isEmpty()) { + className = className.replace("\\\\", "\\"); + Project project = stringLiteralExpression.getProject(); + Collection phpClasses = PhpIndex.getInstance(project).getClassesByFQN(className); + if (!phpClasses.isEmpty()) { + return phpClasses.iterator().next(); + } + } - @Nullable - static public PhpClass getClass(PhpIndex phpIndex, String className) { - Collection classes = phpIndex.getClassesByFQN(className); - return classes.isEmpty() ? null : classes.iterator().next(); + return null; } } diff --git a/src/com/phpuaca/completion/util/PhpMethodResolver.java b/src/com/phpuaca/completion/util/PhpMethodResolver.java index 902afb0..5ef4783 100644 --- a/src/com/phpuaca/completion/util/PhpMethodResolver.java +++ b/src/com/phpuaca/completion/util/PhpMethodResolver.java @@ -9,13 +9,12 @@ import java.util.Collection; -final public class PhpMethodResolver implements IResolver { +final public class PhpMethodResolver { private MethodReference methodReference; private Method resolvedMethod; private PhpClass resolvedClass; - public PhpMethodResolver(@NotNull MethodReference methodReference) { this.methodReference = methodReference;