-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1576 from Haehnchen/feature/routes-php8-attributes
#1567 support routes definition inside PHP8 attributes
- Loading branch information
Showing
5 changed files
with
262 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/fr/adrienbrault/idea/symfony2plugin/util/PhpPsiAttributesUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.util; | ||
|
||
import com.intellij.patterns.PlatformPatterns; | ||
import com.intellij.patterns.PsiElementPattern; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiWhiteSpace; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.jetbrains.php.lang.lexer.PhpTokenTypes; | ||
import com.jetbrains.php.lang.psi.PhpPsiUtil; | ||
import com.jetbrains.php.lang.psi.elements.ArrayCreationExpression; | ||
import com.jetbrains.php.lang.psi.elements.ParameterList; | ||
import com.jetbrains.php.lang.psi.elements.PhpAttribute; | ||
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
/** | ||
* Helpers for PHP 8 Attributes psi access | ||
* | ||
* @author Daniel Espendiller <daniel@espendiller.net> | ||
*/ | ||
public class PhpPsiAttributesUtil { | ||
@Nullable | ||
public static String getAttributeValueByNameAsString(@NotNull PhpAttribute attribute, @NotNull String attributeName) { | ||
PsiElement nextSibling = findAttributeByName(attribute, attributeName); | ||
|
||
if (nextSibling instanceof StringLiteralExpression) { | ||
String contents = ((StringLiteralExpression) nextSibling).getContents(); | ||
if (StringUtils.isNotBlank(contents)) { | ||
return contents; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@NotNull | ||
public static Collection<String> getAttributeValueByNameAsArray(@NotNull PhpAttribute attribute, @NotNull String attributeName) { | ||
PsiElement nextSibling = findAttributeByName(attribute, attributeName); | ||
|
||
if (nextSibling instanceof ArrayCreationExpression) { | ||
return PhpElementsUtil.getArrayValuesAsString((ArrayCreationExpression) nextSibling); | ||
} | ||
|
||
return Collections.emptyList(); | ||
} | ||
|
||
/** | ||
* Workaround to find given attribute: "#[Route('/attributesWithoutName', name: "")]" as attribute iteration given the index as "int" but not the key as name | ||
*/ | ||
@Nullable | ||
private static PsiElement findAttributeByName(@NotNull PhpAttribute attribute, @NotNull String attributeName) { | ||
ParameterList parameterList = PsiTreeUtil.findChildOfType(attribute, ParameterList.class); | ||
if (parameterList == null) { | ||
return null; | ||
} | ||
|
||
Collection<PsiElement> childrenOfTypeAsList = PsiElementUtils.getChildrenOfTypeAsList(parameterList, getAttributeColonPattern(attributeName)); | ||
|
||
if (childrenOfTypeAsList.isEmpty()) { | ||
return null; | ||
} | ||
|
||
PsiElement colon = childrenOfTypeAsList.iterator().next(); | ||
|
||
return PhpPsiUtil.getNextSibling(colon, psiElement -> psiElement instanceof PsiWhiteSpace); | ||
} | ||
|
||
/** | ||
* "#[Route('/path', name: 'attributes_action')]" | ||
*/ | ||
@NotNull | ||
private static PsiElementPattern.Capture<PsiElement> getAttributeColonPattern(String name) { | ||
return PlatformPatterns.psiElement().withElementType( | ||
PhpTokenTypes.opCOLON | ||
).afterLeaf(PlatformPatterns.psiElement().withElementType(PhpTokenTypes.IDENTIFIER).withText(name)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters