Skip to content

Commit 7ce1aad

Browse files
authored
Merge pull request #337 from Haehnchen/feature/pattern-static
Refactor annotation patterns to static constants for performance
2 parents 9ad6b42 + 97cb1bc commit 7ce1aad

12 files changed

Lines changed: 291 additions & 159 deletions

src/main/java/de/espend/idea/php/annotation/completion/AnnotationCompletionContributor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.intellij.codeInsight.completion.*;
44
import com.intellij.codeInsight.lookup.LookupElementBuilder;
55
import com.intellij.openapi.project.Project;
6+
import com.intellij.patterns.ElementPattern;
67
import com.intellij.patterns.PlatformPatterns;
78
import com.intellij.psi.PsiElement;
89
import com.intellij.psi.PsiWhiteSpace;
@@ -46,6 +47,9 @@
4647
*/
4748
public class AnnotationCompletionContributor extends CompletionContributor {
4849

50+
private static final ElementPattern<PsiElement> DOC_IDENTIFIER_PATTERN =
51+
PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER);
52+
4953
public AnnotationCompletionContributor() {
5054

5155
// @<caret>
@@ -309,7 +313,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull
309313
return;
310314
}
311315

312-
PsiElement propertyName = PhpElementsUtil.getPrevSiblingOfPatternMatch(phpDocString, PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER));
316+
PsiElement propertyName = PhpElementsUtil.getPrevSiblingOfPatternMatch(phpDocString, DOC_IDENTIFIER_PATTERN);
313317
if(propertyName == null) {
314318
return;
315319
}

src/main/java/de/espend/idea/php/annotation/doctrine/action/DoctrineAddRepositoryGenerateAction.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.intellij.codeInsight.actions.CodeInsightAction;
55
import com.intellij.openapi.editor.Editor;
66
import com.intellij.openapi.project.Project;
7+
import com.intellij.patterns.ElementPattern;
78
import com.intellij.patterns.PlatformPatterns;
89
import com.intellij.psi.PsiElement;
910
import com.intellij.psi.PsiFile;
@@ -19,6 +20,12 @@
1920
* @author Daniel Espendiller <daniel@espendiller.net>
2021
*/
2122
public class DoctrineAddRepositoryGenerateAction extends CodeInsightAction {
23+
24+
private static final ElementPattern<PsiElement> INSIDE_PHP_CLASS_PATTERN =
25+
PlatformPatterns.psiElement().inside(PhpClass.class);
26+
private static final ElementPattern<PsiElement> INSIDE_PHP_DOC_COMMENT_PATTERN =
27+
PlatformPatterns.psiElement().inside(PhpDocComment.class);
28+
2229
@NotNull
2330
@Override
2431
protected CodeInsightActionHandler getHandler() {
@@ -63,12 +70,12 @@ private static PhpClass getPhpClassOnValidScope(@NotNull Editor editor, @NotNull
6370
}
6471

6572
// attribute and direct hit
66-
if (PlatformPatterns.psiElement().inside(PhpClass.class).accepts(psiElement)) {
73+
if (INSIDE_PHP_CLASS_PATTERN.accepts(psiElement)) {
6774
return PsiTreeUtil.getParentOfType(psiElement, PhpClass.class);
6875
}
6976

7077
// docblock are outside the phpclass scope
71-
if (PlatformPatterns.psiElement().inside(PhpDocComment.class).accepts(psiElement)) {
78+
if (INSIDE_PHP_DOC_COMMENT_PATTERN.accepts(psiElement)) {
7279
PhpDocComment parentOfType = PsiTreeUtil.getParentOfType(psiElement, PhpDocComment.class);
7380
if (parentOfType != null) {
7481
PhpPsiElement nextPsiSibling = parentOfType.getNextPsiSibling();

src/main/java/de/espend/idea/php/annotation/doctrine/action/DoctrineClassGeneratorAction.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.intellij.codeInsight.actions.CodeInsightAction;
55
import com.intellij.openapi.editor.Editor;
66
import com.intellij.openapi.project.Project;
7+
import com.intellij.patterns.ElementPattern;
78
import com.intellij.patterns.PlatformPatterns;
89
import com.intellij.psi.PsiElement;
910
import com.intellij.psi.PsiFile;
@@ -21,6 +22,10 @@
2122
* @author Daniel Espendiller <daniel@espendiller.net>
2223
*/
2324
abstract public class DoctrineClassGeneratorAction extends CodeInsightAction {
25+
26+
private static final ElementPattern<PsiElement> INSIDE_PHP_CLASS_PATTERN =
27+
PlatformPatterns.psiElement().inside(PhpClass.class);
28+
2429
@Override
2530
protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
2631
if (!(file instanceof PhpFile) || !DoctrineUtil.isDoctrineOrmInVendor(project)) {
@@ -37,7 +42,7 @@ protected boolean isValidForFile(@NotNull Project project, @NotNull Editor edito
3742
return false;
3843
}
3944

40-
if (!PlatformPatterns.psiElement().inside(PhpClass.class).accepts(psiElement)) {
45+
if (!INSIDE_PHP_CLASS_PATTERN.accepts(psiElement)) {
4146
return false;
4247
}
4348

@@ -76,7 +81,7 @@ public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull Ps
7681
return;
7782
}
7883

79-
if(!PlatformPatterns.psiElement().inside(PhpClass.class).accepts(psiElement)) {
84+
if(!INSIDE_PHP_CLASS_PATTERN.accepts(psiElement)) {
8085
return;
8186
}
8287

src/main/java/de/espend/idea/php/annotation/doctrine/action/DoctrinePropertyOrmAnnotationGenerateAction.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.intellij.codeInsight.actions.CodeInsightAction;
55
import com.intellij.openapi.editor.Editor;
66
import com.intellij.openapi.project.Project;
7+
import com.intellij.patterns.ElementPattern;
78
import com.intellij.patterns.PlatformPatterns;
89
import com.intellij.psi.PsiElement;
910
import com.intellij.psi.PsiFile;
@@ -22,6 +23,9 @@
2223
*/
2324
public class DoctrinePropertyOrmAnnotationGenerateAction extends CodeInsightAction {
2425

26+
private static final ElementPattern<PsiElement> INSIDE_PHP_CLASS_PATTERN =
27+
PlatformPatterns.psiElement().inside(PhpClass.class);
28+
2529
private final PhpGenerateFieldAccessorHandlerBase myHandler = new PhpGenerateFieldAccessorHandlerBase()
2630
{
2731

@@ -78,7 +82,7 @@ protected boolean isValidForFile(@NotNull Project project, @NotNull Editor edito
7882
return false;
7983
}
8084

81-
if (!PlatformPatterns.psiElement().inside(PhpClass.class).accepts(psiElement)) {
85+
if (!INSIDE_PHP_CLASS_PATTERN.accepts(psiElement)) {
8286
return false;
8387
}
8488

src/main/java/de/espend/idea/php/annotation/doctrine/inspection/DoctrineTypeDeprecatedInspection.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.intellij.codeInspection.LocalInspectionTool;
44
import com.intellij.codeInspection.ProblemHighlightType;
55
import com.intellij.codeInspection.ProblemsHolder;
6+
import com.intellij.patterns.ElementPattern;
67
import com.intellij.patterns.PlatformPatterns;
78
import com.intellij.psi.PsiElement;
89
import com.intellij.psi.PsiElementVisitor;
@@ -34,6 +35,10 @@
3435
* @author Daniel Espendiller <daniel@espendiller.net>
3536
*/
3637
public class DoctrineTypeDeprecatedInspection extends LocalInspectionTool {
38+
39+
private static final ElementPattern<PsiElement> DOC_IDENTIFIER_PATTERN =
40+
PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER);
41+
3742
@NotNull
3843
@Override
3944
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
@@ -87,7 +92,7 @@ private static String getContentIfTypeValid(@NotNull StringLiteralExpression str
8792
}
8893
}
8994
} else if (stringLiteralExpression.getNode().getElementType() == PhpDocElementTypes.phpDocString) {
90-
PsiElement propertyName = PhpElementsUtil.getPrevSiblingOfPatternMatch(stringLiteralExpression, PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER));
95+
PsiElement propertyName = PhpElementsUtil.getPrevSiblingOfPatternMatch(stringLiteralExpression, DOC_IDENTIFIER_PATTERN);
9196
if (propertyName != null && property.equals(propertyName.getText())) {
9297
PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(stringLiteralExpression, PhpDocTag.class);
9398
if (phpDocTag != null) {

src/main/java/de/espend/idea/php/annotation/navigation/AnnotationGoToDeclarationHandler.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler;
44
import com.intellij.openapi.actionSystem.DataContext;
55
import com.intellij.openapi.editor.Editor;
6+
import com.intellij.patterns.ElementPattern;
67
import com.intellij.patterns.PlatformPatterns;
78
import com.intellij.patterns.StandardPatterns;
89
import com.intellij.psi.PsiElement;
@@ -31,6 +32,14 @@
3132
*/
3233
public class AnnotationGoToDeclarationHandler implements GotoDeclarationHandler {
3334

35+
// <@Test>, <@Test\Test>
36+
private static final ElementPattern<PsiElement> DOC_TAG_NAME_PATTERN =
37+
PlatformPatterns.psiElement(PhpDocElementTypes.DOC_TAG_NAME).withText(StandardPatterns.string().startsWith("@")).withLanguage(PhpLanguage.INSTANCE);
38+
39+
// @Route(name=<ClassName>::FOO)
40+
private static final ElementPattern<PsiElement> DOC_IDENTIFIER_BEFORE_STATIC_PATTERN =
41+
PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER).beforeLeaf(AnnotationPattern.getDocStaticPattern()).withLanguage(PhpLanguage.INSTANCE);
42+
3443
@Nullable
3544
@Override
3645
public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Editor editor) {
@@ -42,12 +51,12 @@ public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Edit
4251

4352
// <@Test>
4453
// <@Test\Test>
45-
if (PlatformPatterns.psiElement(PhpDocElementTypes.DOC_TAG_NAME).withText(StandardPatterns.string().startsWith("@")).withLanguage(PhpLanguage.INSTANCE).accepts(psiElement)) {
54+
if (DOC_TAG_NAME_PATTERN.accepts(psiElement)) {
4655
this.addDocTagNameGoto(psiElement, psiElements);
4756
}
4857

4958
// @Route(name=<ClassName>::FOO)
50-
if (PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER).beforeLeaf(AnnotationPattern.getDocStaticPattern()).withLanguage(PhpLanguage.INSTANCE).accepts(psiElement)) {
59+
if (DOC_IDENTIFIER_BEFORE_STATIC_PATTERN.accepts(psiElement)) {
5160
this.addStaticClassTargets(psiElement, psiElements);
5261
}
5362

src/main/java/de/espend/idea/php/annotation/navigation/AnnotationUsageLineMarkerProvider.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,17 @@ public void collectSlowLineMarkers(@NotNull List<? extends PsiElement> psiElemen
7979
/**
8080
* class "Foo" extends
8181
*/
82-
private static PsiElementPattern.Capture<PsiElement> getClassNamePattern() {
83-
return PlatformPatterns
82+
private static final PsiElementPattern.Capture<PsiElement> CLASS_NAME_PATTERN =
83+
PlatformPatterns
8484
.psiElement(PhpTokenTypes.IDENTIFIER)
8585
.afterLeafSkipping(
8686
PlatformPatterns.psiElement(PsiWhiteSpace.class),
8787
PlatformPatterns.psiElement(PhpTokenTypes.kwCLASS)
8888
)
8989
.withParent(PhpClass.class)
9090
.withLanguage(PhpLanguage.INSTANCE);
91+
92+
private static PsiElementPattern.Capture<PsiElement> getClassNamePattern() {
93+
return CLASS_NAME_PATTERN;
9194
}
9295
}

0 commit comments

Comments
 (0)