com.jetbrains.php.lang.psi.PhpPsiUtil Java Examples
The following examples show how to use
com.jetbrains.php.lang.psi.PhpPsiUtil.
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: PhpElementsUtil.java From idea-php-laravel-plugin with MIT License | 6 votes |
/** * Get array string values mapped with their PsiElements * * ["value", "value2"] */ @NotNull static public Map<String, PsiElement> getArrayValuesAsMap(@NotNull ArrayCreationExpression arrayCreationExpression) { List<PsiElement> arrayValues = PhpPsiUtil.getChildren(arrayCreationExpression, new Condition<PsiElement>() { @Override public boolean value(PsiElement psiElement) { return psiElement.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE; } }); Map<String, PsiElement> keys = new HashMap<String, PsiElement>(); for (PsiElement child : arrayValues) { String stringValue = PhpElementsUtil.getStringValue(child.getFirstChild()); if(stringValue != null && StringUtils.isNotBlank(stringValue)) { keys.put(stringValue, child); } } return keys; }
Example #2
Source File: PhpHighlightPackParametersUsagesHandlerFactory.java From idea-php-advanced-autocomplete with MIT License | 6 votes |
@Override public @Nullable HighlightUsagesHandlerBase createHighlightUsagesHandler(@NotNull Editor editor, @NotNull PsiFile file, @NotNull PsiElement target) { ParameterList parameterList = PhpPsiUtil.getParentByCondition(target, true, ParameterList.INSTANCEOF, Statement.INSTANCEOF); if (parameterList == null) { return null; } FunctionReference functionCall = ObjectUtils.tryCast(parameterList.getParent(), FunctionReference.class); String fqn = resolveFqn(functionCall); if (!"\\pack".equals(fqn)) { return null; } PsiElement[] parameters = parameterList.getParameters(); PsiElement selectedParameter = StreamEx.of(parameters).findFirst((p) -> p.getTextRange().containsOffset(editor.getCaretModel().getOffset())).orElse(null); if (selectedParameter == null) { return null; } int selectedIndex = PhpCodeInsightUtil.getParameterIndex(selectedParameter); if (selectedIndex < 0 || selectedIndex >= parameters.length) { return null; } return new PhpHighlightPackParametersUsagesHandler(editor, file, 0, selectedIndex, parameters); }
Example #3
Source File: PhpElementsUtil.java From idea-php-advanced-autocomplete with MIT License | 6 votes |
@Nullable public static Function getFunction(PsiElement position) { FunctionReference functionReference = PhpPsiUtil.getParentByCondition(position, true, FunctionReference.INSTANCEOF, Statement.INSTANCEOF); if (functionReference != null) { return getFunction(functionReference); } else { NewExpression newExpression = PhpPsiUtil.getParentByCondition(position, true, NewExpression.INSTANCEOF, Statement.INSTANCEOF); if (newExpression != null) { ClassReference classReference = newExpression.getClassReference(); if (classReference != null) { return getFunction(classReference); } } return null; } }
Example #4
Source File: PhpHeaderDocumentationProvider.java From idea-php-advanced-autocomplete with MIT License | 6 votes |
@Override public @Nullable PsiElement getCustomDocumentationElement(@NotNull Editor editor, @NotNull PsiFile file, @Nullable PsiElement contextElement) { if (!isCallToHeaderFunc(contextElement)) { return null; } if (!(contextElement instanceof StringLiteralExpression)) { contextElement = PhpPsiUtil.getParentByCondition(contextElement, true, StringLiteralExpression.INSTANCEOF); } if (contextElement instanceof StringLiteralExpression) { String contents = ((StringLiteralExpression)contextElement).getContents(); if (!contents.contains(":")) { return null; } String headerName = StringUtils.substringBefore(contents, ":"); if (headerName.isEmpty() || !isHeaderName(headerName)) { return null; } return new HeaderDocElement(contextElement.getManager(), contextElement.getLanguage(), headerName); } return null; }
Example #5
Source File: PhpDocUtil.java From idea-php-annotation-plugin with MIT License | 6 votes |
/** * Get class path on "use" path statement */ @Nullable public static String getQualifiedName(@NotNull PsiElement psiElement, @NotNull String fqn) { PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(psiElement); if (scopeForUseOperator == null) { return null; } PhpReference reference = PhpPsiUtil.getParentByCondition(psiElement, false, PhpReference.INSTANCEOF); String qualifiedName = PhpCodeInsightUtil.createQualifiedName(scopeForUseOperator, fqn, reference, false); if (!PhpLangUtil.isFqn(qualifiedName)) { return qualifiedName; } // @TODO: remove full fqn fallback if(qualifiedName.startsWith("\\")) { qualifiedName = qualifiedName.substring(1); } return qualifiedName; }
Example #6
Source File: BundleClassGeneratorUtil.java From idea-php-symfony2-plugin with MIT License | 6 votes |
@Nullable public static PhpClass getBundleClassInDirectory(@NotNull PsiDirectory bundleDirContext) { for (PsiFile psiFile : bundleDirContext.getFiles()) { if(!(psiFile instanceof PhpFile)) { continue; } PhpClass aClass = PhpPsiUtil.findClass((PhpFile) psiFile, phpClass -> PhpElementsUtil.isInstanceOf(phpClass, "\\Symfony\\Component\\HttpKernel\\Bundle\\BundleInterface") ); if(aClass != null) { return aClass; } } return null; }
Example #7
Source File: PhpServiceIntention.java From idea-php-symfony2-plugin with MIT License | 6 votes |
@Override public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement psiElement) { if(!Symfony2ProjectComponent.isEnabled(project)) { return false; } PsiElement parentByCondition = PhpPsiUtil.getParentByCondition(psiElement, Method.INSTANCEOF); if(parentByCondition == null) { return false; } PhpClass phpClass = PhpPsiUtil.getParentByCondition(psiElement, PhpClass.INSTANCEOF); if(phpClass == null) { return false; } return true; }
Example #8
Source File: PhpElementsUtil.java From idea-php-symfony2-plugin with MIT License | 6 votes |
/** * Get array string values mapped with their PsiElements * * ["value", "value2"] */ @NotNull static public Map<String, PsiElement> getArrayValuesAsMap(@NotNull ArrayCreationExpression arrayCreationExpression) { Collection<PsiElement> arrayValues = PhpPsiUtil.getChildren(arrayCreationExpression, psiElement -> psiElement.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE ); Map<String, PsiElement> keys = new HashMap<>(); for (PsiElement child : arrayValues) { String stringValue = PhpElementsUtil.getStringValue(child.getFirstChild()); if(StringUtils.isNotBlank(stringValue)) { keys.put(stringValue, child); } } return keys; }
Example #9
Source File: AnnotationBackportUtil.java From idea-php-symfony2-plugin with MIT License | 6 votes |
/** * Get class path on "use" path statement */ @Nullable public static String getQualifiedName(@NotNull PsiElement psiElement, @NotNull String fqn) { PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(psiElement); if (scopeForUseOperator == null) { return null; } PhpReference reference = PhpPsiUtil.getParentByCondition(psiElement, false, PhpReference.INSTANCEOF); String qualifiedName = PhpCodeInsightUtil.createQualifiedName(scopeForUseOperator, fqn, reference, false); if (!PhpLangUtil.isFqn(qualifiedName)) { return qualifiedName; } // @TODO: remove full fqn fallback if(qualifiedName.startsWith("\\")) { qualifiedName = qualifiedName.substring(1); } return qualifiedName; }
Example #10
Source File: ContainerBuilderStubIndex.java From idea-php-symfony2-plugin with MIT License | 6 votes |
@NotNull @Override public DataIndexer<String, ContainerBuilderCall, FileContent> getIndexer() { return inputData -> { Map<String, ContainerBuilderCall> map = new THashMap<>(); PsiFile psiFile = inputData.getPsiFile(); if(!(psiFile instanceof PhpFile) || !Symfony2ProjectComponent.isEnabledForIndex(psiFile.getProject()) || !isValidForIndex(inputData, psiFile) ){ return map; } StreamEx.of(PhpPsiUtil.findAllClasses((PhpFile) psiFile)) .flatMap(clazz -> StreamEx.of(clazz.getOwnMethods())) .forEach(method -> processMethod(method, map)); return map; }; }
Example #11
Source File: PhpElementsUtil.java From Thinkphp5-Plugin with MIT License | 6 votes |
/** * Get array string values mapped with their PsiElements * * ["value", "value2"] */ @NotNull static public Map<String, PsiElement> getArrayValuesAsMap(@NotNull ArrayCreationExpression arrayCreationExpression) { List<PsiElement> arrayValues = PhpPsiUtil.getChildren(arrayCreationExpression, new Condition<PsiElement>() { @Override public boolean value(PsiElement psiElement) { return psiElement.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE; } }); Map<String, PsiElement> keys = new HashMap<String, PsiElement>(); for (PsiElement child : arrayValues) { String stringValue = PhpElementsUtil.getStringValue(child.getFirstChild()); if(stringValue != null && StringUtils.isNotBlank(stringValue)) { keys.put(stringValue, child); } } return keys; }
Example #12
Source File: PhpServiceIntention.java From idea-php-symfony2-plugin with MIT License | 5 votes |
@Override public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement psiElement) throws IncorrectOperationException { PsiElement parentByCondition = PhpPsiUtil.getParentByCondition(psiElement, Method.INSTANCEOF); if(parentByCondition == null) { return; } PhpClass phpClass = PhpPsiUtil.getParentByCondition(psiElement, PhpClass.INSTANCEOF); if(phpClass == null) { return; } ServiceGenerateAction.invokeServiceGenerator(project, phpClass.getContainingFile(), phpClass, editor); }
Example #13
Source File: AnnotationIconProvider.java From idea-php-annotation-plugin with MIT License | 5 votes |
public Icon getIcon(@NotNull PsiElement element, @Iconable.IconFlags int flags) { if (element instanceof PhpFile && PhpPsiUtil.findClasses((PhpFile)element, AnnotationUtil::isAnnotationClass).size() == 1) { return PlatformIcons.ANNOTATION_TYPE_ICON; } return null; }
Example #14
Source File: PhpHeaderDocumentationProvider.java From idea-php-advanced-autocomplete with MIT License | 5 votes |
private boolean isCallToHeaderFunc(PsiElement psiElement) { FunctionReference function = PhpPsiUtil.getParentByCondition(psiElement, true, FunctionReference.INSTANCEOF, Statement.INSTANCEOF); if (function == null) { return false; } return "header".equals(function.getName()); }
Example #15
Source File: PhpVariableInsertHandler.java From intellij-latte with MIT License | 5 votes |
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) { PsiElement element = context.getFile().findElementAt(context.getStartOffset()); if (element != null && element.getLanguage() == LatteLanguage.INSTANCE) { PsiElement parent = element.getParent(); if (!(parent instanceof Variable) && !element.getText().startsWith("$")) { Editor editor = context.getEditor(); CaretModel caretModel = editor.getCaretModel(); int offset = caretModel.getOffset(); caretModel.moveToOffset(element.getTextOffset() + (PhpPsiUtil.isOfType(parent, PhpElementTypes.CAST_EXPRESSION) ? 1 : 0)); EditorModificationUtil.insertStringAtCaret(editor, "$"); caretModel.moveToOffset(offset + 1); PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument()); } } }
Example #16
Source File: PhpAutoPopupTypedHandler.java From idea-php-advanced-autocomplete with MIT License | 5 votes |
@NotNull @Override public Result checkAutoPopup(char charTyped, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { if (!(file instanceof PhpFile)) { return Result.CONTINUE; } if (charTyped != '%') { return Result.CONTINUE; } int offset = editor.getCaretModel().getOffset(); PsiElement psiElement = file.findElementAt(offset); ParameterList parameterList = PhpPsiUtil.getParentByCondition(psiElement, true, ParameterList.INSTANCEOF, Statement.INSTANCEOF); if (parameterList != null) { FunctionReference functionCall = ObjectUtils.tryCast(parameterList.getParent(), FunctionReference.class); String fqn = PhpElementsUtil.resolveFqn(functionCall); if (/*charTyped == '%' &&*/ PhpElementsUtil.isFormatFunction(fqn)) { if (StringUtil.getPrecedingCharNum(editor.getDocument().getCharsSequence(), offset, '%') % 2 == 0) { PhpCompletionUtil.showCompletion(editor); } } } return Result.CONTINUE; }
Example #17
Source File: TemplateUtil.java From idea-php-shopware-plugin with MIT License | 5 votes |
/** * {tag attribute="foobar"}{/s} */ @Nullable public static String getTagAttributeValueByName(@NotNull SmartyTag tag, @NotNull String attribute) { PsiElement psiAttribute = getTagAttributeByName(tag, attribute); if(psiAttribute == null) { return null; } PsiElement nextSibling = PhpPsiUtil.getNextSibling(psiAttribute, (Condition<PsiElement>) psiElement -> { IElementType elementType = psiElement.getNode().getElementType(); return psiElement instanceof PsiWhiteSpace || elementType == SmartyTokenTypes.EQ || elementType == SmartyTokenTypes.DOUBLE_QUOTE || elementType == SmartyTokenTypes.SINGLE_QUOTE; }); if(nextSibling == null) { return null; } String text = nextSibling.getText(); if(StringUtils.isNotBlank(text)) { return text; } return null; }
Example #18
Source File: PhpFormatDocumentationProvider.java From idea-php-advanced-autocomplete with MIT License | 5 votes |
private String getCallToFormatFunc(PsiElement psiElement) { FunctionReference function = PhpPsiUtil.getParentByCondition(psiElement, true, FunctionReference.INSTANCEOF, Statement.INSTANCEOF); if (function == null) { return null; } return PhpElementsUtil.resolveFqn(function); }
Example #19
Source File: PhpHighlightPackParametersUsagesHandler.java From idea-php-advanced-autocomplete with MIT License | 5 votes |
private int resolveSpecificationIndexFromCaret(@NotNull HashMap<Integer, RelativeRange> specificationRelativeRanges) { int caretOffset = this.myEditor.getCaretModel().getOffset(); StringLiteralExpression selectedLiteralExpression = PhpPsiUtil.getParentByCondition(this.myFile.findElementAt(caretOffset), false, StringLiteralExpression.INSTANCEOF); return StreamEx.of(specificationRelativeRanges.entrySet()) .findFirst((e) -> specificationAtCaretOffsetExists(caretOffset, selectedLiteralExpression, e.getValue())) .map(Map.Entry::getKey).orElse(-1); }
Example #20
Source File: WebTestCaseGeneratorAction.java From idea-php-symfony2-plugin with MIT License | 4 votes |
@Override public void actionPerformed(AnActionEvent event) { Project project = getEventProject(event); if(project == null) { return; } PsiDirectory bundleDirContext = BundleClassGeneratorUtil.getBundleDirContext(event); if(bundleDirContext == null) { return; } PsiFile data = CommonDataKeys.PSI_FILE.getData(event.getDataContext()); if(!(data instanceof PhpFile)) { return; } String relativePath = VfsUtil.getRelativePath(data.getVirtualFile(), bundleDirContext.getVirtualFile()); if(relativePath == null) { return; } PhpClass aClass = PhpPsiUtil.findClass((PhpFile) data, Conditions.alwaysTrue()); if(aClass == null) { return; } int i = relativePath.lastIndexOf("."); final String className = "Tests\\" + relativePath.substring(0, i).replace("/", "\\") + "Test"; final PhpClass phpClass = BundleClassGeneratorUtil.getBundleClassInDirectory(bundleDirContext); if(phpClass == null) { return; } new WriteCommandAction(project) { @Override protected void run(@NotNull Result result) throws Throwable { PsiElement file = null; try { file = PhpBundleFileFactory.createBundleFile(phpClass, "web_test_case", className, new HashMap<>()); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error:" + e.getMessage()); } if(file != null) { new OpenFileDescriptor(getProject(), file.getContainingFile().getVirtualFile(), 0).navigate(true); } } @Override public String getGroupID() { return "Create Symfony WebTestFile"; } }.execute(); }
Example #21
Source File: RouteHelper.java From idea-php-symfony2-plugin with MIT License | 4 votes |
/** * Find every possible route name declaration inside yaml, xml or @Route annotation */ @Nullable public static PsiElement getRouteNameTarget(@NotNull Project project, @NotNull String routeName) { for(VirtualFile virtualFile: RouteHelper.getRouteDefinitionInsideFile(project, routeName)) { PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile); if(psiFile instanceof YAMLFile) { return YAMLUtil.getQualifiedKeyInFile((YAMLFile) psiFile, routeName); } else if(psiFile instanceof XmlFile) { PsiElement target = RouteHelper.getXmlRouteNameTarget((XmlFile) psiFile, routeName); if(target != null) { return target; } } else if(psiFile instanceof PhpFile) { // find on @Route annotation for (PhpClass phpClass : PhpPsiUtil.findAllClasses((PhpFile) psiFile)) { // get prefix by PhpClass String prefix = getRouteNamePrefix(phpClass); for (Method method : phpClass.getOwnMethods()) { PhpDocComment docComment = method.getDocComment(); if(docComment == null) { continue; } PhpDocCommentAnnotation container = AnnotationUtil.getPhpDocCommentAnnotationContainer(docComment); if(container == null) { continue; } // multiple @Route annotation in bundles are allowed for (String routeClass : ROUTE_CLASSES) { PhpDocTagAnnotation phpDocTagAnnotation = container.getPhpDocBlock(routeClass); if(phpDocTagAnnotation != null) { String annotationRouteName = phpDocTagAnnotation.getPropertyValue("name"); if(annotationRouteName != null) { // name provided @Route(name="foobar") if(routeName.equals(prefix + annotationRouteName)) { return phpDocTagAnnotation.getPropertyValuePsi("name"); } } else { // just @Route() without name provided String routeByMethod = AnnotationBackportUtil.getRouteByMethod(phpDocTagAnnotation.getPhpDocTag()); if(routeName.equals(prefix + routeByMethod)) { return phpDocTagAnnotation.getPhpDocTag(); } } } } } } } } return null; }
Example #22
Source File: PhpDocUtil.java From idea-php-annotation-plugin with MIT License | 4 votes |
/** * Workaround for STATIC node element: @Foo(foo::bar) and @Foo(name={foo::bar}); * see: WI-32801 */ public static boolean isDocStaticElement(@NotNull PsiElement psiElement) { return PhpPsiUtil.isOfType(psiElement, PhpDocTokenTypes.DOC_STATIC) || (PhpPsiUtil.isOfType(psiElement, PhpDocTokenTypes.DOC_TEXT) && "::".equals(psiElement.getText())); }