com.intellij.psi.util.PsiElementFilter Java Examples

The following examples show how to use com.intellij.psi.util.PsiElementFilter. 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 Thinkphp5-Plugin with MIT License 6 votes vote down vote up
@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());
        }
    });

}
 
Example #2
Source File: RTHtmlExtension.java    From react-templates-plugin with MIT License 6 votes vote down vote up
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;
    }
 
Example #3
Source File: RTHtmlExtension.java    From react-templates-plugin with MIT License 6 votes vote down vote up
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;
    }
 
Example #4
Source File: PhpElementsUtil.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
@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());
        }
    });

}
 
Example #5
Source File: MyPsiUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Finds the first {@link RuleSpecNode} or {@link ModeSpecNode} matching the {@code ruleName} defined in
 * the given {@code grammar}.
 *
 * Rule specs can be either children of the {@link RulesNode}, or under one of the {@code mode}s defined in
 * the grammar. This means we have to walk the whole grammar to find matching candidates.
 */
public static PsiElement findSpecNode(GrammarSpecNode grammar, final String ruleName) {
	PsiElementFilter definitionFilter = new PsiElementFilter() {
		@Override
		public boolean isAccepted(PsiElement element1) {
			if (!(element1 instanceof RuleSpecNode)) {
				return false;
			}

			GrammarElementRefNode id = ((RuleSpecNode) element1).getId();
			return id != null && id.getText().equals(ruleName);
		}
	};

	PsiElement[] ruleSpec = PsiTreeUtil.collectElements(grammar, definitionFilter);
	if (ruleSpec.length > 0) {
		return ruleSpec[0];
	}
	return null;
}
 
Example #6
Source File: XQueryPsiImplUtil.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
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;
        }
    });
}
 
Example #7
Source File: MyPsiUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Like PsiTreeUtil.findChildrenOfType, except no collection is created and it doesnt use recursion.
 * @param parent the element whose children will be searched
 * @param types the types to search for
 * @return an iterable that will traverse the psi tree depth-first, including only the elements
 * whose type is contained in the provided tokenset.
 */
public static Iterable<PsiElement> findChildrenOfType(final PsiElement parent, final TokenSet types) {
 PsiElement[] psiElements = PsiTreeUtil.collectElements(parent, new PsiElementFilter() {
  @Override
  public boolean isAccepted(PsiElement input) {
   if ( input==null ) return false;
   ASTNode node = input.getNode();
   if ( node==null ) return false;
   return types.contains(node.getElementType());
  }
 });
 return Arrays.asList(psiElements);
}
 
Example #8
Source File: MyPsiUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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);
		}
	});
}
 
Example #9
Source File: MyPsiUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** 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);
		}
	});
}
 
Example #10
Source File: MyPsiUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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);
		}
	});
}
 
Example #11
Source File: InflateLocalVariableAction.java    From idea-android-studio-plugin with GNU General Public License v2.0 4 votes vote down vote up
@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();

        }
    });

}
 
Example #12
Source File: PsiElementProcessor.java    From consulo with Apache License 2.0 4 votes vote down vote up
public CollectFilteredElements(@Nonnull PsiElementFilter filter, @Nonnull Collection<T> collection) {
  super(collection);
  myFilter = filter;
}
 
Example #13
Source File: PsiElementProcessor.java    From consulo with Apache License 2.0 4 votes vote down vote up
public CollectFilteredElements(@Nonnull PsiElementFilter filter) {
  myFilter = filter;
}
 
Example #14
Source File: PsiElementProcessor.java    From consulo with Apache License 2.0 4 votes vote down vote up
public FindFilteredElement(@Nonnull PsiElementFilter filter) {
  myFilter = filter;
}