From 76b4a2df467157524ff4ec07244b5f94e002d332 Mon Sep 17 00:00:00 2001 From: maxfilatov Date: Sun, 5 Jun 2016 16:13:52 +0300 Subject: [PATCH] #18 prevent access to index while service is dumb - 2 --- .../phpuaca/completion/BaseTypeProvider.java | 7 ------- .../phpuaca/completion/PHPUnitTypeProvider.java | 12 +++++++++++- .../completion/ProphecyTypeProvider.java | 15 ++++++--------- .../completion/StringCompletionProvider.java | 3 ++- src/com/phpuaca/helper/AvailabilityHelper.java | 5 +++++ src/com/phpuaca/reference/StringReference.java | 5 +++++ src/com/phpuaca/util/PhpClassResolver.java | 17 +++++++---------- 7 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/com/phpuaca/completion/BaseTypeProvider.java b/src/com/phpuaca/completion/BaseTypeProvider.java index 71ba425..186c39f 100644 --- a/src/com/phpuaca/completion/BaseTypeProvider.java +++ b/src/com/phpuaca/completion/BaseTypeProvider.java @@ -1,9 +1,6 @@ package com.phpuaca.completion; -import com.intellij.openapi.project.DumbService; -import com.intellij.psi.PsiElement; import com.jetbrains.php.lang.psi.elements.Method; -import com.jetbrains.php.lang.psi.elements.MethodReference; import com.jetbrains.php.lang.psi.elements.PhpClass; import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider2; import com.phpuaca.util.PhpClassAdapter; @@ -12,10 +9,6 @@ abstract public class BaseTypeProvider implements PhpTypeProvider2 { - protected boolean isAvailable(@NotNull PsiElement psiElement) { - return (psiElement instanceof MethodReference) && !DumbService.getInstance(psiElement.getProject()).isDumb(); - } - @Nullable protected PhpClassAdapter getPhpClassAdapterForMethod(@NotNull Method method) { PhpClass phpClass = method.getContainingClass(); diff --git a/src/com/phpuaca/completion/PHPUnitTypeProvider.java b/src/com/phpuaca/completion/PHPUnitTypeProvider.java index 777cb05..2881718 100644 --- a/src/com/phpuaca/completion/PHPUnitTypeProvider.java +++ b/src/com/phpuaca/completion/PHPUnitTypeProvider.java @@ -1,5 +1,6 @@ package com.phpuaca.completion; +import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; import com.jetbrains.php.PhpIndex; @@ -40,7 +41,12 @@ public char getKey() { @Nullable @Override public String getType(PsiElement psiElement) { - if (!isAvailable(psiElement)) { + Project project = psiElement.getProject(); + if (DumbService.isDumb(project)) { + return null; + } + + if (!(psiElement instanceof MethodReference)) { return null; } @@ -68,6 +74,10 @@ public String getType(PsiElement psiElement) { @Override public Collection getBySignature(String s, Project project) { + if (DumbService.isDumb(project)) { + return null; + } + PhpIndex phpIndex = PhpIndex.getInstance(project); Collection collection = new ArrayList(); collection.addAll(phpIndex.getInterfacesByFQN(CLASS_PHP_UNIT_MOCK_OBJECT)); diff --git a/src/com/phpuaca/completion/ProphecyTypeProvider.java b/src/com/phpuaca/completion/ProphecyTypeProvider.java index aececa1..d528731 100644 --- a/src/com/phpuaca/completion/ProphecyTypeProvider.java +++ b/src/com/phpuaca/completion/ProphecyTypeProvider.java @@ -4,13 +4,7 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; import com.jetbrains.php.PhpIndex; -import com.jetbrains.php.lang.psi.elements.FieldReference; -import com.jetbrains.php.lang.psi.elements.Method; -import com.jetbrains.php.lang.psi.elements.MethodReference; -import com.jetbrains.php.lang.psi.elements.Parameter; -import com.jetbrains.php.lang.psi.elements.PhpClass; -import com.jetbrains.php.lang.psi.elements.PhpNamedElement; -import com.jetbrains.php.lang.psi.elements.Variable; +import com.jetbrains.php.lang.psi.elements.*; import com.jetbrains.php.lang.psi.resolve.types.PhpType; import com.phpuaca.util.PhpClassAdapter; import com.phpuaca.util.PhpClassResolver; @@ -46,8 +40,7 @@ public char getKey() { @Override public String getType(PsiElement psiElement) { Project project = psiElement.getProject(); - - if (DumbService.getInstance(project).isDumb()) { + if (DumbService.isDumb(project)) { return null; } @@ -96,6 +89,10 @@ public String getType(PsiElement psiElement) { @Override public Collection getBySignature(String s, Project project) { + if (DumbService.isDumb(project)) { + return null; + } + PhpIndex phpIndex = PhpIndex.getInstance(project); Collection collection = new ArrayList(); for (String FQN : s.split(TYPE_SEPARATOR)) { diff --git a/src/com/phpuaca/completion/StringCompletionProvider.java b/src/com/phpuaca/completion/StringCompletionProvider.java index 45e9d8c..0fe44b9 100644 --- a/src/com/phpuaca/completion/StringCompletionProvider.java +++ b/src/com/phpuaca/completion/StringCompletionProvider.java @@ -4,6 +4,7 @@ import com.intellij.codeInsight.completion.CompletionProvider; import com.intellij.codeInsight.completion.CompletionResultSet; import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.openapi.project.DumbService; import com.intellij.psi.PsiElement; import com.intellij.util.ProcessingContext; import com.jetbrains.php.completion.PhpLookupElement; @@ -22,7 +23,7 @@ public class StringCompletionProvider extends CompletionProvidermethod('cursor'); PsiElement stringLiteralExpressionContext = psiElement.getContext(); if (stringLiteralExpressionContext instanceof ParameterList) { diff --git a/src/com/phpuaca/reference/StringReference.java b/src/com/phpuaca/reference/StringReference.java index c6fdb5c..bdc1d11 100644 --- a/src/com/phpuaca/reference/StringReference.java +++ b/src/com/phpuaca/reference/StringReference.java @@ -1,6 +1,7 @@ package com.phpuaca.reference; import com.intellij.lang.ASTNode; +import com.intellij.openapi.project.DumbService; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiReference; @@ -35,6 +36,10 @@ public TextRange getRangeInElement() { @Nullable @Override public PsiElement resolve() { + if (DumbService.isDumb(getElement().getProject())) { + return null; + } + PsiElement resolvedElement = null; Filter filter = FilterFactory.getInstance().getFilter(getElement()); if (filter != null) { diff --git a/src/com/phpuaca/util/PhpClassResolver.java b/src/com/phpuaca/util/PhpClassResolver.java index e1af13f..967ad36 100644 --- a/src/com/phpuaca/util/PhpClassResolver.java +++ b/src/com/phpuaca/util/PhpClassResolver.java @@ -1,6 +1,5 @@ package com.phpuaca.util; -import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiTreeUtil; @@ -32,15 +31,13 @@ public PhpClass resolveByClassConstantReference(@NotNull ClassConstantReference @Nullable public PhpClass resolveByClassStringLiteralExpression(@NotNull StringLiteralExpression stringLiteralExpression) { - Project project = stringLiteralExpression.getProject(); - if (!DumbService.getInstance(project).isDumb()) { - String className = stringLiteralExpression.getContents(); - if (!className.isEmpty()) { - className = className.replace("\\\\", "\\"); - Collection phpClasses = PhpIndex.getInstance(project).getAnyByFQN(className); - if (!phpClasses.isEmpty()) { - return phpClasses.iterator().next(); - } + String className = stringLiteralExpression.getContents(); + if (!className.isEmpty()) { + className = className.replace("\\\\", "\\"); + Project project = stringLiteralExpression.getProject(); + Collection phpClasses = PhpIndex.getInstance(project).getAnyByFQN(className); + if (!phpClasses.isEmpty()) { + return phpClasses.iterator().next(); } }