Java Code Examples for com.intellij.psi.util.PsiTreeUtil#processElements()

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#processElements() . 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: MethodChainLookupElement.java    From litho with Apache License 2.0 6 votes vote down vote up
private static Template createTemplate(PsiElement from) {
  TemplateBuilderImpl templateBuilder = new TemplateBuilderImpl(from);
  PsiTreeUtil.processElements(
      from,
      psiElement -> {
        if (psiElement instanceof PsiIdentifier) {
          PsiIdentifier psiIdentifier = (PsiIdentifier) psiElement;
          String identifier = psiIdentifier.getText();
          if (TEMPLATE_INSERT_PLACEHOLDER.equals(identifier)) {
            templateBuilder.replaceElement(psiIdentifier, new TextExpression(""));
          } else if (TEMPLATE_INSERT_PLACEHOLDER_C.equals(identifier)) {
            templateBuilder.replaceElement(psiIdentifier, new TextExpression("c"));
          }
        }
        return true;
      });
  Template template = templateBuilder.buildTemplate();
  return template;
}
 
Example 2
Source File: DuneFoldingBuilder.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    List<FoldingDescriptor> descriptors = new ArrayList<>();

    PsiTreeUtil.processElements(root, element -> {
        if (isMultiline(element.getTextRange(), document)) {
            FoldingDescriptor fold = null;
            if (element instanceof PsiStanza) {
                fold = foldStanza((PsiStanza) element);
            } else if (DuneTypes.INSTANCE.C_SEXPR == element.getNode().getElementType()) {
                fold = fold(element);
            }
            if (fold != null) {
                descriptors.add(fold);
            }
        }

        return true;
    });

    return descriptors.toArray(new FoldingDescriptor[0]);
}
 
Example 3
Source File: FoldingBuilder.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    ORTypes types = root.getLanguage() == RmlLanguage.INSTANCE ? RmlTypes.INSTANCE : OclTypes.INSTANCE;

    PsiTreeUtil.processElements(root, element -> {
        if (element instanceof PsiLet) {
            foldLet(descriptors, (PsiLet) element);
        } else if (element instanceof PsiType) {
            foldType(descriptors, (PsiType) element);
        } else if (element instanceof PsiInnerModule) {
            foldModule(descriptors, (PsiInnerModule) element);
        } else if (element instanceof PsiFunction) {
            foldFunction(descriptors, (PsiFunction) element);
        } else if (element instanceof PsiFunctor) {
            foldFunctor(descriptors, (PsiFunctor) element);
        } else if (element instanceof PsiTag) {
            foldTag(descriptors, (PsiTag) element);
        } else if (element instanceof OclYaccHeader) {
            foldHeader(descriptors, (OclYaccHeader) element);
        } else if (element instanceof OclYaccRule) {
            foldRule(descriptors, (OclYaccRule) element);
        } else if (types.MULTI_COMMENT == element.getNode().getElementType()) {
            FoldingDescriptor fold = fold(element);
            if (fold != null) {
                descriptors.add(fold);
            }
        }

        return true;
    });

    return descriptors.toArray(new FoldingDescriptor[0]);
}
 
Example 4
Source File: IdeaUtilsIsCamelRouteStartTestIT.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
public void testRouteStartFinder() {
    // caret is at start of rout in the test java file
    PsiFile psiFile = myFixture.configureByText("DummyTestData.java", CODE);
    List<PsiElement> psiElements = new ArrayList<>();

    PsiTreeUtil.processElements(psiFile, element -> {
        if (getCamelIdeaUtils().isCamelRouteStart(element)) {
            psiElements.add(element);
        }
        return true;
    });

    assertTrue(psiElements.size() == 8);
}
 
Example 5
Source File: HaskellFoldingBuilder.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
@Override
protected void
buildLanguageFoldRegions(@NotNull final List<FoldingDescriptor> descriptors,
                                        @NotNull PsiElement root,
                                        @NotNull Document document,
                                        boolean quick) {
    if (!(root instanceof HaskellFile)) return;
    HaskellFile file = (HaskellFile) root;
    final Set<PsiElement> seenComments = ContainerUtil.newHashSet();

    if (!quick) {
        PsiTreeUtil.processElements(file, new PsiElementProcessor() {
            @Override
            public boolean execute(@NotNull PsiElement element) {
                if (element.getNode().getElementType().equals(HaskellTypes.COMMENT)) {
                    addCommentFolds((PsiComment) element, seenComments, descriptors);
                } else if (HaskellParserDefinition.COMMENTS.contains(element.getNode().getElementType())) {
                    TextRange range = element.getTextRange();
                    String placeholderText = getPlaceholderText(element.getNode());
                    // Only fold if we actually save space to prevent
                    // assertions from kicking in. Means {- -} will not fold.
                    if (placeholderText != null && range.getLength() > 1 &&
                            range.getLength() > placeholderText.length()) {
                        descriptors.add(new FoldingDescriptor(element, range));
                    }
                }
                return true;
            }
        });
    }
}
 
Example 6
Source File: AndroidViewUtil.java    From idea-android-studio-plugin with GNU General Public License v2.0 5 votes vote down vote up
public static List<PsiFile> findLayoutFilesInsideMethod(PsiMethod psiMethod) {
    final List<PsiFile> xmlFiles = new ArrayList<PsiFile>();

    PsiTreeUtil.processElements(psiMethod, new PsiElementProcessor() {

        @Override
        public boolean execute(@NotNull PsiElement element) {

            if (element instanceof PsiMethodCallExpression) {
                PsiMethod psiMethodResolved = ((PsiMethodCallExpression) element).resolveMethod();
                if (psiMethodResolved != null) {
                    String methodName = psiMethodResolved.getName();
                    // @TODO: implement instance check
                    if ("setContentView".equals(methodName)) {
                        PsiExpression[] expressions = ((PsiMethodCallExpression) element).getArgumentList().getExpressions();
                        if (expressions.length > 0 && expressions[0] instanceof PsiReferenceExpression) {
                            PsiFile xmlFile = AndroidUtils.findXmlResource((PsiReferenceExpression) expressions[0]);
                            if (xmlFile != null) {
                                xmlFiles.add(xmlFile);
                            }
                        }

                    }

                }

            }

            return true;
        }
    });


    return xmlFiles;
}
 
Example 7
Source File: ProfilerUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * "_controller" and "_route"
 * "/_profiler/242e61?panel=request"
 *
 * <tr>
 *  <th>_route</th>
 *  <td>foo_route</td>
 * </tr>
 */
@NotNull
public static Map<String, String> getRequestAttributes(@NotNull Project project, @NotNull String html) {
    HtmlFileImpl htmlFile = (HtmlFileImpl) PsiFileFactory.getInstance(project).createFileFromText(HTMLLanguage.INSTANCE, html);

    String[] keys = new String[] {"_controller", "_route"};

    Map<String, String> map = new HashMap<>();
    PsiTreeUtil.processElements(htmlFile, psiElement -> {
        if(!(psiElement instanceof XmlTag) || !"th".equals(((XmlTag) psiElement).getName())) {
            return true;
        }

        XmlTagValue keyTag = ((XmlTag) psiElement).getValue();
        String key = StringUtils.trim(keyTag.getText());
        if(!ArrayUtils.contains(keys, key)) {
            return true;
        }

        XmlTag tdTag = PsiTreeUtil.getNextSiblingOfType(psiElement, XmlTag.class);
        if(tdTag == null || !"td".equals(tdTag.getName())) {
            return true;
        }

        XmlTagValue valueTag = tdTag.getValue();
        String value = valueTag.getText();
        if(StringUtils.isBlank(value)) {
            return true;
        }

        // Symfony 3.2 profiler debug? strip html
        map.put(key, stripHtmlTags(value));

        // exit if all item found
        return map.size() != keys.length;
    });

    return map;
}
 
Example 8
Source File: SmartyBlockUtil.java    From idea-php-shopware-plugin with MIT License 4 votes vote down vote up
public static List<SmartyBlock> collectFileBlocks(PsiFile psiFile, final Map<VirtualFile, String> map, List<SmartyBlock> blockNameSet, List<VirtualFile> virtualFilesCatch, int depth) {

        final List<VirtualFile> virtualFiles = new ArrayList<>();

        PsiTreeUtil.processElements(psiFile, element -> {

            if (SmartyPattern.getExtendPattern().accepts(element)) {
                String extendsName = element.getText();
                if(extendsName.startsWith("parent:")) {
                    extendsName = extendsName.substring(7);
                }
                for (Map.Entry<VirtualFile, String> entry : map.entrySet()) {
                    if (entry.getValue().equals(extendsName)) {
                        virtualFiles.add(entry.getKey());
                    }

                }
            }

            return true;
        });

        if(virtualFiles.size() == 0) {
            return blockNameSet;
        }

        // for recursive calls
        List<PsiFile> parentFiles = new ArrayList<>();

        for(VirtualFile virtualFile: virtualFiles) {
            if(!virtualFilesCatch.contains(virtualFile)) {
                virtualFilesCatch.add(virtualFile);
                PsiFile parentPsiFile = PsiManager.getInstance(psiFile.getProject()).findFile(virtualFile);
                if(parentPsiFile != null) {
                    parentFiles.add(parentPsiFile);
                    blockNameSet.addAll(getFileBlocks(parentPsiFile));
                }
            }
        }

        if(depth-- < 0) {
            return blockNameSet;
        }

        for(PsiFile parentFile: parentFiles) {
            collectFileBlocks(parentFile, map, blockNameSet,virtualFilesCatch, depth);
        }

        return blockNameSet;
    }
 
Example 9
Source File: ProfilerUtil.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
/**
 * ["foo/foo.html.twig": 1]
 *
 * <tr>
 *  <td>@Twig/Exception/traces_text.html.twig</td>
 *  <td class="font-normal">1</td>
 * </tr>
 */
public static Map<String, Integer> getRenderedElementTwigTemplates(@NotNull Project project, @NotNull String html) {
    HtmlFileImpl htmlFile = (HtmlFileImpl) PsiFileFactory.getInstance(project).createFileFromText(HTMLLanguage.INSTANCE, html);

    final XmlTag[] xmlTag = new XmlTag[1];
    PsiTreeUtil.processElements(htmlFile, psiElement -> {
        if(!(psiElement instanceof XmlTag) || !"h2".equals(((XmlTag) psiElement).getName())) {
            return true;
        }

        XmlTagValue keyTag = ((XmlTag) psiElement).getValue();
        String contents = StringUtils.trim(keyTag.getText());
        if(!"Rendered Templates".equalsIgnoreCase(contents)) {
            return true;
        }

        xmlTag[0] = (XmlTag) psiElement;

        return true;
    });

    if(xmlTag[0] == null) {
        return Collections.emptyMap();
    }

    XmlTag tableTag = PsiTreeUtil.getNextSiblingOfType(xmlTag[0], XmlTag.class);
    if(tableTag == null || !"table".equals(tableTag.getName())) {
        return Collections.emptyMap();
    }

    XmlTag tbody = tableTag.findFirstSubTag("tbody");
    if(tbody == null) {
        return Collections.emptyMap();
    }

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

    for (XmlTag tag : PsiTreeUtil.getChildrenOfTypeAsList(tbody, XmlTag.class)) {
        if(!"tr".equals(tag.getName())) {
            continue;
        }

        XmlTag[] tds = tag.findSubTags("td");
        if(tds.length < 2) {
            continue;
        }

        String template = stripHtmlTags(StringUtils.trim(tds[0].getValue().getText()));
        if(StringUtils.isBlank(template)) {
            continue;
        }

        Integer count;
        try {
            count = Integer.valueOf(stripHtmlTags(StringUtils.trim(tds[1].getValue().getText())));
        } catch (NumberFormatException e) {
            count = 0;
        }

        templates.put(template, count);
    }

    return templates;
}
 
Example 10
Source File: SmartyBlockUtil.java    From idea-php-shopware-plugin with MIT License 3 votes vote down vote up
public static Set<SmartyBlock> getFileBlocks(PsiFile psiFile) {

        final Set<SmartyBlock> blockNameSet = new HashSet<>();

        PsiTreeUtil.processElements(psiFile, element -> {

            if (SmartyPattern.getBlockPattern().accepts(element)) {
                blockNameSet.add(new SmartyBlock(element, element.getText()));
            }

            return true;
        });

        return blockNameSet;
    }