|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.dic.command; |
| 2 | + |
| 3 | +import com.intellij.execution.actions.BaseRunConfigurationAction; |
| 4 | +import com.intellij.execution.actions.RunContextAction; |
| 5 | +import com.intellij.execution.executors.DefaultRunExecutor; |
| 6 | +import com.intellij.execution.lineMarker.RunLineMarkerContributor; |
| 7 | +import com.intellij.icons.AllIcons; |
| 8 | +import com.intellij.openapi.actionSystem.AnAction; |
| 9 | +import com.intellij.psi.PsiElement; |
| 10 | +import com.intellij.util.ObjectUtils; |
| 11 | +import com.jetbrains.php.lang.lexer.PhpTokenTypes; |
| 12 | +import com.jetbrains.php.lang.psi.PhpPsiUtil; |
| 13 | +import com.jetbrains.php.lang.psi.elements.Field; |
| 14 | +import com.jetbrains.php.lang.psi.elements.PhpAttribute; |
| 15 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 16 | +import com.jetbrains.php.lang.psi.elements.PhpNamedElement; |
| 17 | +import com.jetbrains.php.lang.psi.stubs.indexes.expectedArguments.PhpExpectedFunctionArgument; |
| 18 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
| 19 | +import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils; |
| 20 | +import org.apache.commons.lang.StringUtils; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | +import org.jetbrains.annotations.Nullable; |
| 23 | + |
| 24 | +import java.util.Collection; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 28 | + */ |
| 29 | +public class SymfonyCommandTestRunLineMarkerProvider extends RunLineMarkerContributor { |
| 30 | + @Override |
| 31 | + public @Nullable Info getInfo(@NotNull PsiElement leaf) { |
| 32 | + PhpClass phpClass = getCommandContext(leaf); |
| 33 | + if (phpClass != null) { |
| 34 | + String commandNameFromClass = getCommandNameFromClass(phpClass); |
| 35 | + if (commandNameFromClass != null) { |
| 36 | + BaseRunConfigurationAction baseRunConfigurationAction = new RunContextAction(DefaultRunExecutor.getRunExecutorInstance()); |
| 37 | + return new Info(AllIcons.RunConfigurations.TestState.Run, new AnAction[]{baseRunConfigurationAction}, (psiElement) -> "Run Script"); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + @Nullable |
| 45 | + public static PhpClass getCommandContext(@NotNull PsiElement leaf) { |
| 46 | + if (PhpPsiUtil.isOfType(leaf, PhpTokenTypes.IDENTIFIER)) { |
| 47 | + PhpNamedElement element = ObjectUtils.tryCast(leaf.getParent(), PhpNamedElement.class); |
| 48 | + if (element != null && element.getNameIdentifier() == leaf) { |
| 49 | + if (element instanceof PhpClass) { |
| 50 | + return (PhpClass) element; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + @Nullable |
| 59 | + public static String getCommandNameFromClass(@NotNull PhpClass phpClass) { |
| 60 | + if (PhpElementsUtil.isInstanceOf(phpClass, "\\Symfony\\Component\\Console\\Command\\Command")) { |
| 61 | + // lazy naming: |
| 62 | + // protected static $defaultName = 'app:create-user' |
| 63 | + Field defaultName = phpClass.findFieldByName("defaultName", false); |
| 64 | + if (defaultName != null) { |
| 65 | + PsiElement defaultValue = defaultName.getDefaultValue(); |
| 66 | + if (defaultValue != null) { |
| 67 | + return PhpElementsUtil.getStringValue(defaultValue); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // php attributes: |
| 72 | + // #[AsCommand(name: 'app:create-user')] |
| 73 | + for (PhpAttribute attribute : phpClass.getAttributes("\\Symfony\\Component\\Console\\Attribute\\AsCommand")) { |
| 74 | + for (PhpAttribute.PhpAttributeArgument argument : attribute.getArguments()) { |
| 75 | + String name = argument.getName(); |
| 76 | + if ("name".equals(name)) { |
| 77 | + PhpExpectedFunctionArgument argument1 = argument.getArgument(); |
| 78 | + if (argument1 != null) { |
| 79 | + String value1 = PsiElementUtils.trimQuote(argument1.getValue()); |
| 80 | + if (StringUtils.isNotBlank(value1)) { |
| 81 | + return value1; |
| 82 | + } |
| 83 | + } |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // @TODO: provide tag resolving here |
| 90 | + // - { name: 'console.command', command: 'app:sunshine' } |
| 91 | + } |
| 92 | + |
| 93 | + return null; |
| 94 | + } |
| 95 | +} |
0 commit comments