public RegExpGroup resolve() { final int index = getIndex(); final PsiElementProcessor.FindFilteredElement<RegExpElement> processor = new PsiElementProcessor.FindFilteredElement<RegExpElement>(new PsiElementFilter() { int groupCount; public boolean isAccepted(PsiElement element) { if (element instanceof RegExpGroup) { if (((RegExpGroup)element).isCapturing() && ++groupCount == index) { return true; } } return element == RegExpBackrefImpl.this; } }); PsiTreeUtil.processElements(getContainingFile(), processor); if (processor.getFoundElement() instanceof RegExpGroup) { return (RegExpGroup)processor.getFoundElement(); } return null; }
@Nullable public RegExpGroup resolve() { final PsiElementProcessor.FindFilteredElement<RegExpGroup> processor = new PsiElementProcessor.FindFilteredElement<RegExpGroup>( new PsiElementFilter() { public boolean isAccepted(PsiElement element) { if (!(element instanceof RegExpGroup)) { return false; } final RegExpGroup regExpGroup = (RegExpGroup)element; return (regExpGroup.isPythonNamedGroup() || regExpGroup.isRubyNamedGroup()) && Comparing.equal(getGroupName(), regExpGroup.getGroupName()); } } ); PsiTreeUtil.processElements(getContainingFile(), processor); return processor.getFoundElement(); }
public static List<String> loadImportedTags(@NotNull XmlFile file, @NotNull XmlTag context) { // PsiElement[] arr = file.getRootTag().getChildren(); // Collection<HtmlTag> tags = PsiTreeUtil.findChildrenOfType(file, HtmlTag.class); PsiElement[] reqTags = PsiTreeUtil.collectElements(file, new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { return element instanceof HtmlTag && (((HtmlTag) element).getName().equals(RTTagDescriptorsProvider.RT_REQUIRE) || ((HtmlTag) element).getName().equals(RTTagDescriptorsProvider.RT_IMPORT)); } }); List<String> importedTags = new ArrayList<String>(); for (PsiElement elem : reqTags) { String as = ((HtmlTag) elem).getAttributeValue("as"); if (!Strings.isNullOrEmpty(as)) { importedTags.add(as); } } return importedTags; }
@Nullable public static PsiElement[] getMethodParameterReferences(Method method, int parameterIndex) { // we dont have a parameter on resolved method Parameter[] parameters = method.getParameters(); if(parameters.length == 0 || parameterIndex >= parameters.length) { return null; } final String tempVariableName = parameters[parameterIndex].getName(); return PsiTreeUtil.collectElements(method.getLastChild(), new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { return element instanceof Variable && tempVariableName.equals(((Variable) element).getName()); } }); }
@Nullable public RegExpGroup resolve() { final PsiElementProcessor.FindFilteredElement<RegExpElement> processor = new PsiElementProcessor.FindFilteredElement<RegExpElement>(new PsiElementFilter() { public boolean isAccepted(PsiElement element) { if (element instanceof RegExpGroup) { if (((RegExpGroup)element).isPythonNamedGroup() && Comparing.equal(getGroupName(), ((RegExpGroup)element).getGroupName())) { return true; } } return element == RegExpPyNamedGroupRefImpl.this; } }); PsiTreeUtil.processElements(getContainingFile(), processor); if (processor.getFoundElement() instanceof RegExpGroup) { return (RegExpGroup) processor.getFoundElement(); } return null; }
private static PsiElement[] collectPackageLocalMembers(PsiElement classToMove) { return PsiTreeUtil.collectElements(classToMove, new PsiElementFilter() { public boolean isAccepted(final PsiElement element) { if (element instanceof PsiMember) { PsiMember member = (PsiMember) element; if (VisibilityUtil.getVisibilityModifier(member.getModifierList()) == PsiModifier.PACKAGE_LOCAL) { return true; } } return false; } }); }
private void checkReturnValueAllowed(PyCallable callable, PsiElement beingChecked, boolean allowed, String message) { // TODO: use a real flow analysis to check all exit points boolean hasReturns; if (callable instanceof PyFunction) { final PsiElement[] returnStatements = PsiTreeUtil.collectElements(callable, new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { return (element instanceof PyReturnStatement && ((PyReturnStatement)element).getExpression() != null) || (element instanceof PyYieldExpression); } }); hasReturns = returnStatements.length > 0; } else { final PyType type = myTypeEvalContext.getReturnType(callable); hasReturns = !(type instanceof PyNoneType); } if (allowed ^ hasReturns) { if (allowed && callable instanceof PyFunction) { if (PyUtil.isDecoratedAsAbstract(((PyFunction)callable))) { return; } // one last chance: maybe there's no return but a 'raise' statement, see PY-4043, PY-5048 PyStatementList statementList = ((PyFunction)callable).getStatementList(); for (PyStatement stmt : statementList.getStatements()) { if (stmt instanceof PyRaiseStatement) { return; } } } registerProblem(beingChecked, message); } }
@Nullable private String[] suggestVariableNameFromLiterals(PsiExpression expr, VariableKind variableKind, boolean correctKeywords) { final PsiElement[] literals = PsiTreeUtil.collectElements(expr, new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { if (isStringPsiLiteral(element) && StringUtil.isJavaIdentifier(StringUtil.unquoteString(element.getText()))) { final PsiElement exprList = element.getParent(); if (exprList instanceof PsiExpressionList) { final PsiElement call = exprList.getParent(); if (call instanceof PsiNewExpression) { return true; } else if (call instanceof PsiMethodCallExpression) { //TODO: exclude or not getA().getB("name").getC(); or getA(getB("name").getC()); It works fine for now in the most cases return true; } } } return false; } }); if (literals.length == 1) { final String text = StringUtil.unquoteString(literals[0].getText()); return getSuggestionsByName(text, variableKind, expr.getType() instanceof PsiArrayType, correctKeywords); } return null; }
public static PsiElement[] findChildrenOfType(PsiElement startingElement, final IElementType elementType) { return PsiTreeUtil.collectElements(startingElement, new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { return element.getNode() != null && element.getNode().getElementType() == elementType; } }); }
public static PsiElement findRuleSpecNode(final String ruleName, RulesNode rules) { PsiElementFilter defnode = new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { PsiElement nameNode = element.getFirstChild(); if ( nameNode==null ) return false; return (element instanceof ParserRuleSpecNode || element instanceof LexerRuleSpecNode) && nameNode.getText().equals(ruleName); } }; PsiElement[] ruleSpec = PsiTreeUtil.collectElements(rules, defnode); if ( ruleSpec.length>0 ) return ruleSpec[0]; return null; }
public static PsiElement[] collectAtActions(PsiElement root, final String tokenText) { return PsiTreeUtil.collectElements(root, new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { PsiElement p = element.getContext(); if (p != null) p = p.getContext(); return p instanceof AtAction && element instanceof ParserRuleRefNode && element.getText().equals(tokenText); } }); }
/** Search all internal and leaf nodes looking for token or internal node * with specific text. * This saves having to create lots of java classes just to identify psi nodes. */ public static PsiElement[] collectNodesWithName(PsiElement root, final String tokenText) { return PsiTreeUtil.collectElements(root, new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { String tokenTypeName = element.getNode().getElementType().toString(); return tokenTypeName.equals(tokenText); } }); }
public static PsiElement[] collectNodesWithText(PsiElement root, final String text) { return PsiTreeUtil.collectElements(root, new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { return element.getText().equals(text); } }); }
public static PsiElement[] getValues(LIDValues element) { return PsiTreeUtil.collectElements(element, new PsiElementFilter() { @Override public boolean isAccepted(PsiElement e) { return (e.getNode().getElementType() == LIDTypes.VALUE); } }); }
public static PsiElement[] getValues(DylanHeaderValues element) { return PsiTreeUtil.collectElements(element, new PsiElementFilter() { @Override public boolean isAccepted(PsiElement e) { return (e.getNode().getElementType() == DylanTypes.VALUE); } }); }
@Nullable private String[] suggestVariableNameFromLiterals(@NotNull PsiExpression expr, @NotNull VariableKind variableKind, boolean correctKeywords) { final PsiElement[] literals = PsiTreeUtil.collectElements(expr, new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { if(isStringPsiLiteral(element) && StringUtil.isJavaIdentifier(StringUtil.unquoteString(element.getText()))) { final PsiElement exprList = element.getParent(); if(exprList instanceof PsiExpressionList) { final PsiElement call = exprList.getParent(); if(call instanceof PsiNewExpression) { return true; } else if(call instanceof PsiMethodCallExpression) { //TODO: exclude or not getA().getB("name").getC(); or getA(getB("name").getC()); It works fine for now in the most cases return true; } } } return false; } }); if(literals.length == 1) { final String text = StringUtil.unquoteString(literals[0].getText()); return getSuggestionsByName(text, variableKind, expr.getType() instanceof PsiArrayType, correctKeywords); } return null; }
public static PsiElementFilter getPsiElementFilter() { return isUseSmartFolding() ? ServiceManager.getService(SmartFoldingBlocksFilter.class) : ServiceManager.getService(DefaultFoldingBlocksFilter.class); }
public CollectFilteredElements(@NotNull PsiElementFilter filter, @NotNull Collection<T> collection) { super(collection); myFilter = filter; }
public CollectFilteredElements(@NotNull PsiElementFilter filter) { myFilter = filter; }
public FindFilteredElement(@NotNull PsiElementFilter filter) { myFilter = filter; }
@Override public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile) throws IncorrectOperationException { DocumentUtil.writeInRunUndoTransparentAction(new Runnable() { @Override public void run() { List<AndroidView> androidViews = AndroidUtils.getIDsFromXML(xmlFile); PsiStatement psiStatement = PsiTreeUtil.getParentOfType(psiElement, PsiStatement.class); if (psiStatement == null) { return; } PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiStatement.getProject()); PsiElement[] localVariables = PsiTreeUtil.collectElements(psiStatement.getParent(), new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { return element instanceof PsiLocalVariable; } }); Set<String> variables = new HashSet<String>(); for (PsiElement localVariable : localVariables) { variables.add(((PsiLocalVariable) localVariable).getName()); } for (AndroidView v : androidViews) { if (!variables.contains(v.getFieldName())) { String sb1; if (variableName != null) { sb1 = String.format("%s %s = (%s) %s.findViewById(%s);", v.getName(), v.getFieldName(), v.getName(), variableName, v.getId()); } else { sb1 = String.format("%s %s = (%s) findViewById(%s);", v.getName(), v.getFieldName(), v.getName(), v.getId()); } PsiStatement statementFromText = elementFactory.createStatementFromText(sb1, null); psiStatement.getParent().addAfter(statementFromText, psiStatement); } } JavaCodeStyleManager.getInstance(psiStatement.getProject()).shortenClassReferences(psiStatement.getParent()); new ReformatAndOptimizeImportsProcessor(psiStatement.getProject(), psiStatement.getContainingFile(), true).run(); } }); }
public CollectFilteredElements(@Nonnull PsiElementFilter filter, @Nonnull Collection<T> collection) { super(collection); myFilter = filter; }
public CollectFilteredElements(@Nonnull PsiElementFilter filter) { myFilter = filter; }
public FindFilteredElement(@Nonnull PsiElementFilter filter) { myFilter = filter; }