com.jetbrains.php.lang.psi.elements.ArrayHashElement Java Examples

The following examples show how to use com.jetbrains.php.lang.psi.elements.ArrayHashElement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArrayReturnPsiRecursiveVisitor.java    From Thinkphp5-Plugin with MIT License 6 votes vote down vote up
@Override
    public void visitElement(PsiElement element) {

//        if (element instanceof PhpReturn) {
//            visitPhpReturn((PhpReturn) element);
//        }

        if (element instanceof ArrayCreationExpression && !(element.getParent().getParent() instanceof ArrayHashElement)) {   //如果是创建数组会进行收录, 如果是数组中的数组不能满足条件
//        if (element instanceof ArrayCreationExpression) {
            collectConfigKeys((ArrayCreationExpression) element, this.arrayKeyVisitor, fileNameWithoutExtension);
//            Tool.printPsiTree(element.getParent().getParent());
//            element = element.getNextSibling();
        }

        super.visitElement(element);
    }
 
Example #2
Source File: TCAPatterns.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@NotNull
public static PsiElementPattern.Capture<PsiElement> arrayAssignmentValueWithIndexPattern(@NotNull String targetIndex) {

    return PlatformPatterns.psiElement().withParent(
            PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(
                    PlatformPatterns.or(
                            // ['eval' => '<caret>']
                            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).withParent(
                                    PlatformPatterns.psiElement(ArrayHashElement.class).withChild(
                                            elementWithStringLiteral(PhpElementTypes.ARRAY_KEY, targetIndex)
                                    )
                            ),
                            // $GLOBALS['eval'] = '<caret>';
                            PlatformPatterns.psiElement(PhpElementTypes.ASSIGNMENT_EXPRESSION).withChild(
                                    PlatformPatterns.psiElement(PhpElementTypes.ARRAY_ACCESS_EXPRESSION).withChild(
                                            elementWithStringLiteral(PhpElementTypes.ARRAY_INDEX, targetIndex)
                                    )
                            )
                    )
            )
    );
}
 
Example #3
Source File: BxComponentIncludeReference.java    From bxfs with MIT License 6 votes vote down vote up
@Nullable
@Override
public PsiElement resolve() {
	BxCore bitrix = new BxCore(getElement().getProject());
	String inclusionType = null;

	for (ArrayHashElement arElement : ((ArrayCreationExpression) getElement().getParent().getParent().getParent()).getHashElements()) {
		if (arElement.getKey() instanceof StringLiteralExpression && ((StringLiteralExpression) arElement.getKey()).getContents().equals("AREA_FILE_SHOW") && arElement.getValue() instanceof StringLiteralExpression)
			inclusionType = ((StringLiteralExpression) arElement.getValue()).getContents();
	}

	if (inclusionType != null) {
		VirtualFile componentTemplateFile = bitrix.getPageIncludeFile(
			getElement(),
			inclusionType.equals("page") ? "index" : "sect"
		);

		if (componentTemplateFile != null)
			return getElement().getManager().findFile(componentTemplateFile);
	}

	return null;
}
 
Example #4
Source File: ArrayReturnPsiRecursiveVisitor.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
public static void collectConfigKeys(ArrayCreationExpression creationExpression, ArrayKeyVisitor arrayKeyVisitor, List<String> context) {

        for(ArrayHashElement hashElement: PsiTreeUtil.getChildrenOfTypeAsList(creationExpression, ArrayHashElement.class)) {

            PsiElement arrayKey = hashElement.getKey();
            PsiElement arrayValue = hashElement.getValue();

            if(arrayKey instanceof StringLiteralExpression) {

                List<String> myContext = new ArrayList<>(context);
                myContext.add(((StringLiteralExpression) arrayKey).getContents());
                String keyName = StringUtils.join(myContext, ".");

                if(arrayValue instanceof ArrayCreationExpression) {
                    arrayKeyVisitor.visit(keyName, arrayKey, true);
                    collectConfigKeys((ArrayCreationExpression) arrayValue, arrayKeyVisitor, myContext);
                } else {
                    arrayKeyVisitor.visit(keyName, arrayKey, false);
                }

            }
        }

    }
 
Example #5
Source File: DrupalPattern.java    From idea-php-drupal-symfony2-bridge with MIT License 6 votes vote down vote up
public static boolean isAfterArrayKey(PsiElement psiElement, String arrayKeyName) {

        PsiElement literal = psiElement.getContext();
        if(!(literal instanceof StringLiteralExpression)) {
            return false;
        }

        PsiElement arrayValue = literal.getParent();
        if(arrayValue.getNode().getElementType() != PhpElementTypes.ARRAY_VALUE) {
            return false;
        }

        PsiElement arrayHashElement = arrayValue.getParent();
        if(!(arrayHashElement instanceof ArrayHashElement)) {
            return false;
        }

        PsiElement arrayKey = ((ArrayHashElement) arrayHashElement).getKey();
        String keyString = PhpElementsUtil.getStringValue(arrayKey);

        return arrayKeyName.equals(keyString);
    }
 
Example #6
Source File: ArrayReturnPsiRecursiveVisitor.java    From Thinkphp5-Plugin with MIT License 5 votes vote down vote up
public static void collectConfigKeys(ArrayCreationExpression creationExpression, ArrayKeyVisitor arrayKeyVisitor, List<String> context) {

        List<ArrayHashElement> childrenOfTypeAsList = PsiTreeUtil.getChildrenOfTypeAsList(creationExpression, ArrayHashElement.class);
        for (ArrayHashElement hashElement : childrenOfTypeAsList) {  //遍历文件所有元素

            PsiElement arrayKey = hashElement.getKey();
            PsiElement arrayValue = hashElement.getValue();

            if (arrayKey instanceof StringLiteralExpression) {  //键是数组键

                List<String> myContext = new ArrayList<>(context);

                //fwModify: 协助去掉文件前缀
                if (myContext.get(0).equals(""))
                    myContext.remove(0);

                myContext.add(((StringLiteralExpression) arrayKey).getContents());
                String keyName = StringUtils.join(myContext, ".");

                if (arrayValue instanceof ArrayCreationExpression) {
                    arrayKeyVisitor.visit(keyName, arrayKey, false); //数组键也收录
                    collectConfigKeys((ArrayCreationExpression) arrayValue, arrayKeyVisitor, myContext);
                } else {
                    arrayKeyVisitor.visit(keyName, arrayKey, false);
                }
            }
        }

    }
 
Example #7
Source File: ExtensionUtility.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@Override
public void visitElement(@NotNull PsiElement element) {

    if (PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(
            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withParent(
                    PlatformPatterns.psiElement(ArrayHashElement.class).withParent(
                            PlatformPatterns.psiElement(ArrayCreationExpression.class).withParent(
                                    PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).withParent(
                                            PlatformPatterns.psiElement(ArrayHashElement.class).withFirstChild(
                                                    PlatformPatterns.or(
                                                            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withText("'psr-4'"),
                                                            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withText("\"psr-4\"")
                                                    )
                                            )
                                    )
                            )
                    )
            )
    ).accepts(element)) {
        if (ns == null) {
            ns = new ArrayList<>();
        }

        String contents = ((StringLiteralExpression) element).getContents();
        if (contents.contains("\\")) {
            contents = contents.replace("\\\\", "\\");
        }

        if (contents.endsWith("\\")) {
            ns.add(contents);
        }

        ns.add((contents + "\\"));
    }

    super.visitElement(element);
}
 
Example #8
Source File: TCAPatterns.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@NotNull
public static PsiElementPattern.Capture<PsiElement> arrayAssignmentIndexWithIndexPattern(@NotNull String targetIndex) {

    return PlatformPatterns.psiElement().withParent(
            PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(
                    PlatformPatterns.or(
                            // ['config' => ['<caret>']]
                            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).withParent(
                                    PlatformPatterns.psiElement(PhpElementTypes.ARRAY_CREATION_EXPRESSION).withParent(
                                            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).withParent(
                                                    PlatformPatterns.psiElement(ArrayHashElement.class).withChild(
                                                            elementWithStringLiteral(PhpElementTypes.ARRAY_KEY, targetIndex)
                                                    )
                                            )
                                    )
                            ),
                            // ['config' => ['<caret>' => '']]
                            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withParent(
                                    PlatformPatterns.psiElement(ArrayHashElement.class).withParent(
                                            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_CREATION_EXPRESSION).withParent(
                                                    PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).withParent(
                                                            PlatformPatterns.psiElement(ArrayHashElement.class).withChild(
                                                                    elementWithStringLiteral(PhpElementTypes.ARRAY_KEY, targetIndex)
                                                            )
                                                    )
                                            )
                                    )
                            ),
                            // $GLOBALS['<caret>'] = '';
                            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_INDEX).withParent(
                                    PlatformPatterns.psiElement(PhpElementTypes.ARRAY_ACCESS_EXPRESSION).withChild(
                                            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_ACCESS_EXPRESSION).withChild(
                                                    elementWithStringLiteral(PhpElementTypes.ARRAY_INDEX, targetIndex)
                                            )
                                    )
                            )
                    )
            )
    );
}
 
Example #9
Source File: TCAPatterns.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public static ElementPattern<? extends PsiElement> tableNameAsArrayValue() {
    return PlatformPatterns
        .psiElement(StringLiteralExpression.class).withParent(
            PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).withSuperParent(
                1,
                PlatformPatterns.or(
                    PlatformPatterns.psiElement(ArrayHashElement.class).withChild(
                        PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withChild(
                            PlatformPatterns.psiElement(StringLiteralExpression.class)
                        )
                    )
                )
            )
        );
}
 
Example #10
Source File: RouteGroupUtil.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
/**
 * Analyzes Route::group elements and returns string values for specified property.
 * Route::group(['namespace' => 'Foo'], function() {
 *      Route::group(['namespace' => 'Bar'], function() {
 *          Route::get(...
 *      });
 * });
 *
 * getRouteGroupPropertiesCollection(Route::get element, "namespace") will return list with 'Foo', 'Bar'
 */
@NotNull
public static List<String> getRouteGroupPropertiesCollection(PsiElement element, String propertyName)
{
    List<String> values = new ArrayList<>();

    RouteGroupCondition routeGroupCondition = new RouteGroupCondition();

    PsiElement routeGroup = PsiTreeUtil.findFirstParent(element, true, routeGroupCondition);

    while (routeGroup != null) {
        ArrayCreationExpression arrayCreation = PsiTreeUtil.getChildOfType(((MethodReference)routeGroup).getParameterList(), ArrayCreationExpression.class);

        if (arrayCreation != null) {
            for (ArrayHashElement hashElement : arrayCreation.getHashElements()) {
                if (hashElement.getKey() instanceof StringLiteralExpression) {
                    if (propertyName.equals(((StringLiteralExpression) hashElement.getKey()).getContents())) {
                        if (hashElement.getValue() instanceof StringLiteralExpression) {
                            values.add(((StringLiteralExpression) hashElement.getValue()).getContents());
                        }
                        break;
                    }
                }
            }
        }

        routeGroup = PsiTreeUtil.findFirstParent(routeGroup, true, routeGroupCondition);
    }

    Collections.reverse(values);
    return values;
}
 
Example #11
Source File: CommonHelper.java    From yiistorm with MIT License 5 votes vote down vote up
public static HashMap<String, String> parsePhpArrayConfig(Project project, String filePath) {
    PsiFile pf = CommonHelper.getPsiFile(project, filePath);
    HashMap<String, String> hashx = new HashMap<String, String>();
    if (pf != null) {
        PsiElement groupStatement = pf.getFirstChild();
        if (groupStatement != null) {
            for (PsiElement pl : groupStatement.getChildren()) {
                if (pl.toString().equals("Return")) {
                    PsiElement[] pl2 = pl.getChildren();
                    if (pl2.length > 0 && pl2[0].toString().equals("Array creation expression")) {
                        ArrayCreationExpressionImpl ar = (ArrayCreationExpressionImpl) pl2[0];
                        if (ar.getChildren().length > 0) {
                            for (ArrayHashElement he : ar.getHashElements()) {
                                String val = he.getValue().getText();
                                String key = he.getKey().getText();
                                hashx.put(CommonHelper.rmQuotes(
                                        key.substring(0, key.length() > 40 ? 40 : key.length())
                                ),
                                        CommonHelper.rmQuotes(
                                                val.substring(0, val.length() > 40 ? 40 : val.length()))
                                );
                            }
                        }
                    }
                }
            }
        }
    }
    return hashx;
}
 
Example #12
Source File: NewArrayValueLookupElement.java    From yiistorm with MIT License 5 votes vote down vote up
public void insertIntoArrayConfig(String string, String openFilePath) {

        String relpath = openFilePath.replace(project.getBasePath(), "").substring(1).replace("\\", "/");
        VirtualFile vf = project.getBaseDir().findFileByRelativePath(relpath);

        if (vf == null) {
            return;
        }
        PsiFile pf = PsiManager.getInstance(project).findFile(vf);

        String lineSeparator = " " + ProjectCodeStyleSettingsManager.getSettings(project).getLineSeparator();
        if (vf != null) {

            PsiElement groupStatement = pf.getFirstChild();
            if (groupStatement != null) {
                PsiDocumentManager.getInstance(project).commitDocument(pf.getViewProvider().getDocument());

                pf.getManager().reloadFromDisk(pf);
                for (PsiElement pl : groupStatement.getChildren()) {
                    if (pl.toString().equals("Return")) {
                        PsiElement[] pl2 = pl.getChildren();
                        if (pl2.length > 0 && pl2[0].toString().equals("Array creation expression")) {
                            ArrayCreationExpressionImpl ar = (ArrayCreationExpressionImpl) pl2[0];

                            ArrayHashElement p = (ArrayHashElement) PhpPsiElementFactory.createFromText(project,
                                    PhpElementTypes.HASH_ARRAY_ELEMENT,
                                    "array('" + string + "'=>'')");
                            PsiElement closingBrace = ar.getLastChild();
                            String preLast = closingBrace.getPrevSibling().toString();
                            if (!preLast.equals("Comma")) {
                                pf.getViewProvider().getDocument().insertString(
                                        closingBrace.getTextOffset(), "," + lineSeparator + p.getText());
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
 
Example #13
Source File: MethodArgumentDroppedIndex.java    From idea-php-typo3-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, Integer, FileContent> getIndexer() {
    return inputData -> {
        Set<PsiElement> elements = new HashSet<>();

        Collections.addAll(
                elements,
                PsiTreeUtil.collectElements(inputData.getPsiFile(), el -> PlatformPatterns
                        .psiElement(StringLiteralExpression.class)
                        .withParent(
                                PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY)
                                        .withAncestor(
                                                4,
                                                PlatformPatterns.psiElement(PhpElementTypes.RETURN)
                                        )
                        )
                        .accepts(el)
                )
        );

        Map<String, Integer> methodMap = new HashMap<>();

        for (PsiElement gatheredElement : elements) {
            PsiElement parent = gatheredElement.getParent().getParent();
            if (parent instanceof ArrayHashElement) {
                ArrayHashElement arr = (ArrayHashElement) parent;
                PhpPsiElement value = arr.getValue();
                if (value instanceof ArrayCreationExpression) {
                    ArrayCreationExpression creationExpression = (ArrayCreationExpression) value;
                    creationExpression.getHashElements().forEach(x -> {
                        PhpPsiElement key = x.getKey();
                        if (key != null && key.getText().contains("maximumNumberOfArguments")) {
                            methodMap.put("\\" + ((StringLiteralExpression) gatheredElement).getContents(), Integer.parseInt(x.getValue().getText()));
                        }
                    });
                }
            }
        }

        return methodMap;
    };
}
 
Example #14
Source File: TCAPatterns.java    From idea-php-typo3-plugin with MIT License 4 votes vote down vote up
private static PsiElementPattern.Capture<ArrayHashElement> arrayKeyWithString(@NotNull String arrayKey, IElementType arrayKey2) {
    return PlatformPatterns.psiElement(ArrayHashElement.class).withChild(
            elementWithStringLiteral(arrayKey2, arrayKey)
    );
}