@Nullable @Override protected List<DfaVariableValue> create(PsiElement closure) { final Set<DfaVariableValue> result = ContainerUtil.newLinkedHashSet(); closure.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element instanceof PsiReferenceExpression) { DfaValue value = myFactory.createValue((PsiReferenceExpression)element); if (value instanceof DfaVariableValue) { result.add((DfaVariableValue)value); } } super.visitElement(element); } }); return ContainerUtil.newArrayList(result); }
private void scanCommentsInFile(final Project project, final VirtualFile vFile) { if (!vFile.isDirectory() && vFile.getFileType() instanceof LanguageFileType) { ApplicationManager.getApplication().runReadAction(new Runnable() { @Override public void run() { PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile); if (psiFile == null) return; for (PsiFile root : psiFile.getViewProvider().getAllFiles()) { root.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitComment(PsiComment comment) { commentFound(vFile, comment.getText()); } }); } } }); } }
@NotNull private List<Pair<Instruction, String>> findAllInstructionsInside(@NotNull PsiElement scope) { final List<Pair<Instruction, String>> result = ContainerUtil.newArrayList(); scope.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element instanceof GrReferenceExpression && !((GrReferenceExpression)element).isQualified()) { String varName = ((GrReferenceExpression)element).getReferenceName(); if (varName != null) { for (Instruction dependency : ControlFlowUtils.findAllInstructions(element, flow)) { result.add(Pair.create(dependency, varName)); } } } super.visitElement(element); } }); return result; }
private static Map<String, NamedArgumentDescriptor> findOtherNamedArgumentsInFile(PsiElement mapOrArgumentList) { final Map<String, NamedArgumentDescriptor> map = new HashMap<String, NamedArgumentDescriptor>(); mapOrArgumentList.getContainingFile().accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element instanceof GrArgumentLabel) { final String name = ((GrArgumentLabel)element).getName(); if (GroovyNamesUtil.isIdentifier(name)) { map.put(name, NamedArgumentDescriptor.SIMPLE_UNLIKELY); } } super.visitElement(element); } }); return map; }
@Nullable public static <T extends PsiElement> T createFromText(@NotNull Project p, final Class<T> aClass, String text) { final PsiElement[] ret = new PsiElement[]{null}; createDummyFile(p, text).accept(new PsiRecursiveElementWalkingVisitor() { public void visitElement(PsiElement element) { if(ret[0] == null && aClass.isInstance(element)) { ret[0] = element; } super.visitElement(element); } }); return (T) ret[0]; }
@Nullable public static <T extends PsiElement> T createFromText(@NotNull Project p, @NotNull final IElementType elementType, @NotNull String text) { final PsiElement[] ret = new PsiElement[]{null}; createDummyFile(p, text).accept(new PsiRecursiveElementWalkingVisitor() { public void visitElement(PsiElement element) { if(ret[0] == null && element.getNode().getElementType() == elementType) { ret[0] = element; } super.visitElement(element); } }); return (T) ret[0]; }
public static List<PsiElement> getDefinedSymbols(Project project) { final List<PsiElement> result = new ArrayList<PsiElement>(); Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, CMakeFileType.INSTANCE, GlobalSearchScope.allScope(project)); // Scan each file for named entities for (VirtualFile virtualFile : virtualFiles) { CMakeFile cmakeFile = (CMakeFile) PsiManager.getInstance(project).findFile(virtualFile); if (cmakeFile != null) { cmakeFile.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { super.visitElement(element); if(element instanceof CMakeCommandExpr && (element.getFirstChild().getNode().getElementType() == CMakeTypes.FUNCTION || element.getFirstChild().getNode().getElementType() == CMakeTypes.MACRO || element.getFirstChild().toString().equalsIgnoreCase("set"))) { result.add(element); } } }); } } return result; }
public static List<PsiElement> getDefinedSymbols(Project project, final String name) { final List<PsiElement> result = new ArrayList<PsiElement>(); Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, CMakeFileType.INSTANCE, GlobalSearchScope.allScope(project)); // Scan each file for named entities for (VirtualFile virtualFile : virtualFiles) { CMakeFile cmakeFile = (CMakeFile) PsiManager.getInstance(project).findFile(virtualFile); if (cmakeFile != null) { cmakeFile.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { super.visitElement(element); if(element instanceof CMakeCommandExpr && (element.getFirstChild().getNode().getElementType() == CMakeTypes.FUNCTION || element.getFirstChild().getNode().getElementType() == CMakeTypes.MACRO)) { result.add(element); } } }); } } return result; }
public static List<PsiElement> getDefinedSymbols(PsiElement root) { final List<PsiElement> result = new ArrayList<PsiElement>(); if(root instanceof CMakeFile){ root.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { super.visitElement(element); if(element instanceof CMakeCommandExpr && (element.getFirstChild().getNode().getElementType() == CMakeTypes.FUNCTION || element.getFirstChild().getNode().getElementType() == CMakeTypes.MACRO)) { result.add(element); } } }); } return result; }
public static List<PsiElement> getDefinedVars(Project project, String name) { final List<PsiElement> result = new ArrayList<PsiElement>(); Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, CMakeFileType.INSTANCE, GlobalSearchScope.allScope(project)); // Scan each file for named entities for (VirtualFile virtualFile : virtualFiles) { CMakeFile cmakeFile = (CMakeFile) PsiManager.getInstance(project).findFile(virtualFile); if (cmakeFile != null) { cmakeFile.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { super.visitElement(element); if(element instanceof CMakeCommandExpr && (element.getFirstChild().getText().contains("set") || element.getFirstChild().getText().contains("list") || element.getFirstChild().getText().contains("option"))) { result.add(element); } } }); } } return result; }
public static List<PsiElement> getDefinedVars(PsiElement root) { final List<PsiElement> result = new ArrayList<PsiElement>(); if(root instanceof CMakeFile){ root.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { super.visitElement(element); if(element instanceof CMakeCommandExpr && (element.getFirstChild().getText().contains("set") || element.getFirstChild().getText().contains("list") || element.getFirstChild().getText().contains("option"))) { result.add(element); } } }); } return result; }
public static List<PsiElement> getBlockPsiElement(PsiFile psiFile, final String blockName) { final List<PsiElement> psiElements = new ArrayList<>(); psiFile.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if(SmartyPattern.getBlockPattern().accepts(element)) { String text = element.getText(); if(blockName.equalsIgnoreCase(text)) { psiElements.add(element); } } super.visitElement(element); } }); return psiElements; }
private static List<PsiElement> getIncludePsiElement(PsiFile psiFile, final String templateName) { final List<PsiElement> psiElements = new ArrayList<>(); psiFile.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if(SmartyPattern.getFileIncludePattern().accepts(element)) { String text = element.getText(); if(templateName.equalsIgnoreCase(text)) { psiElements.add(element); } } super.visitElement(element); } }); return psiElements; }
@Nullable @RequiredReadAction public static CSharpTypeDeclaration findPrimaryType(@NotNull PsiFile file) { Ref<CSharpTypeDeclaration> typeRef = Ref.create(); file.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if(element instanceof CSharpTypeDeclaration) { typeRef.set((CSharpTypeDeclaration) element); stopWalking(); } super.visitElement(element); } }); return typeRef.get(); }
@Nullable @Override public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) { if (!(file instanceof LatteFile)) { return null; } final List<ProblemDescriptor> problems = new ArrayList<ProblemDescriptor>(); file.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element instanceof LatteMacroTag) { boolean result = checkClassicMacro((LatteMacroTag) element); if (result) { ProblemDescriptor problem = manager.createProblemDescriptor(element, "Modifiers are not allowed here", true, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly); problems.add(problem); } } else { super.visitElement(element); } } }); return problems.toArray(new ProblemDescriptor[problems.size()]); }
@NotNull public static List<WebReference> collectWebReferences() { final List<WebReference> refs = new ArrayList<>(); myFile.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { for (PsiReference ref : element.getReferences()) { if (ref instanceof WebReference) refs.add((WebReference)ref); } super.visitElement(element); } }); assertTrue(refs.stream().allMatch(PsiReferenceBase::isSoft)); return refs; }
private void deannotatePattern() { for (final PsiElement patternComponent : myPattern) { patternComponent.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element.getUserData(PARAMETER) != null) { element.putUserData(PARAMETER, null); } } }); } }
private void annotatePattern() { for (final PsiElement patternComponent : myPattern) { patternComponent.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { super.visitElement(element); if (myParameters.contains(element.getText())) { element.putUserData(PARAMETER, element); } } }); } }
public static Set<PsiElement> getContentIdentsTargets(final @NotNull Project project, final @NotNull VirtualFile currentFile, final @NotNull String ident) { final Set<PsiElement> psiElements = new HashSet<PsiElement>(); FileBasedIndexImpl.getInstance().getFilesWithKey(OxidContentIdentIndexer.KEY, new HashSet<String>(Arrays.asList(ident)), new Processor<VirtualFile>() { @Override public boolean process(VirtualFile virtualFile) { if (currentFile.equals(virtualFile)) { return true; } PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile); if (psiFile == null) { return true; } psiFile.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if(SmartyPattern.getAttributeInsideTagPattern("ident", "oxcontent").accepts(element)) { String content = element.getText(); if(StringUtils.isNotBlank(content) && content.equalsIgnoreCase(ident)) { psiElements.add(element); } } super.visitElement(element); } }); return true; } }, GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.allScope(project), SmartyFileType.INSTANCE)); return psiElements; }
/** * Find a string return value of a method context "function() { return 'foo'}" * First match wins */ @Nullable static public String getMethodReturnAsString(@NotNull Method method) { final Set<String> values = new HashSet<>(); method.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if(PhpElementsUtil.getMethodReturnPattern().accepts(element)) { String value = PhpElementsUtil.getStringValue(element); if(value != null && StringUtils.isNotBlank(value)) { values.add(value); } } super.visitElement(element); } }); if(values.size() == 0) { return null; } // we support only first item return values.iterator().next(); }
private void buildEventVariables(@NotNull final Project project, final StringBuilder stringBuilder, Method classMethod) { classMethod.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if ((element instanceof StringLiteralExpression) && ((StringLiteralExpression) element).getContents().equals(generatorContainer.getHookName())) { PsiElement parent = element.getParent(); if(parent instanceof ParameterList) { PsiElement[] parameterList = ((ParameterList) parent).getParameters(); if(parameterList.length > 1) { if(parameterList[1] instanceof ArrayCreationExpression) { Map<String, PsiElement> eventParameters = PhpElementsUtil.getArrayCreationKeyMap((ArrayCreationExpression) parameterList[1]); for(Map.Entry<String, PsiElement> entrySet : eventParameters.entrySet()) { stringBuilder.append("\n"); PhpPsiElement psiElement = PhpElementsUtil.getArrayValue((ArrayCreationExpression) parameterList[1], entrySet.getKey()); if(psiElement instanceof PhpTypedElement) { Set<String> classes = new HashSet<>(); PhpType type = ((PhpTypedElement) psiElement).getType(); for (PhpClass aClass : PhpElementsUtil.getClassFromPhpTypeSet(project, type.getTypes())) { // force absolute namespace classes.add("\\" + StringUtils.stripStart(aClass.getPresentableFQN(), "\\")); } if(classes.size() > 0) { stringBuilder.append("/** @var ").append(StringUtils.join(classes, "|")).append("$").append(entrySet.getKey()).append(" */\n"); } } stringBuilder.append("$").append(entrySet.getKey()).append(" = ").append("$args->get('").append(entrySet.getKey()).append("');\n"); } } } } } super.visitElement(element); } }); }
private void scanCommentsInFile(Project project, final VirtualFile vFile) { if (!vFile.isDirectory() && vFile.getFileType() instanceof LanguageFileType) { PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile); if (psiFile == null) return; for (PsiFile root : psiFile.getViewProvider().getAllFiles()) { root.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitComment(PsiComment comment) { commentFound(vFile, comment.getText()); } }); } } }
static Map<String, String> prepareQueries(PsiFile file) { final Set<GrAnnotation> grabs = new LinkedHashSet<GrAnnotation>(); final Set<GrAnnotation> excludes = new THashSet<GrAnnotation>(); final Set<GrAnnotation> resolvers = new THashSet<GrAnnotation>(); file.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element instanceof GrAnnotation) { GrAnnotation anno = (GrAnnotation)element; String qname = anno.getQualifiedName(); if (GRAB_ANNO.equals(qname)) grabs.add(anno); else if (GRAB_EXCLUDE_ANNO.equals(qname)) excludes.add(anno); else if (GRAB_RESOLVER_ANNO.equals(qname)) resolvers.add(anno); } super.visitElement(element); } }); Function<GrAnnotation, String> mapper = new Function<GrAnnotation, String>() { @Override public String fun(GrAnnotation grAnnotation) { return grAnnotation.getText(); } }; String common = StringUtil.join(excludes, mapper, " ") + " " + StringUtil.join(resolvers, mapper, " "); LinkedHashMap<String, String> result = new LinkedHashMap<String, String>(); for (GrAnnotation grab : grabs) { String grabText = grab.getText(); result.put(grabText, (grabText + " " + common).trim()); } return result; }
@Nullable @Override public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) { if (!(file instanceof LatteFile)) { return null; } final List<ProblemDescriptor> problems = new ArrayList<ProblemDescriptor>(); file.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element instanceof LatteMacroTag) { String macroName = ((LatteMacroTag) element).getMacroName(); LatteMacro macro = LatteConfiguration.INSTANCE.getMacro(element.getProject(), macroName); if (macro != null && macro.deprecated) { String description = macro.deprecatedMessage != null ? macro.deprecatedMessage : "Macro " + macroName + " is deprecated"; ProblemDescriptor problem = manager.createProblemDescriptor(element, description, true, ProblemHighlightType.LIKE_DEPRECATED, isOnTheFly); problems.add(problem); } } else { super.visitElement(element); } } }); return problems.toArray(new ProblemDescriptor[problems.size()]); }
private void visitYaml(final ProblemsHolder holder, PsiFile psiFile) { psiFile.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if(YamlElementPatternHelper.getSingleLineScalarKey("_controller", "controller").accepts(element)) { String text = PsiElementUtils.trimQuote(element.getText()); if(StringUtils.isNotBlank(text)) { InspectionUtil.inspectController(element, text, holder, new YamlLazyRouteName(element)); } } super.visitElement(element); } }); }
private void visitXml(final ProblemsHolder holder, PsiFile psiFile) { psiFile.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if(XmlHelper.getRouteControllerPattern().accepts(element)) { String text = PsiElementUtils.trimQuote(element.getText()); if(StringUtils.isNotBlank(text)) { InspectionUtil.inspectController(element, text, holder, new XmlLazyRouteName(element)); } } super.visitElement(element); } }); }
private void visitYamlFile(PsiFile psiFile, final ProblemsHolder holder, @NotNull final ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector) { psiFile.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { annotateCallMethod(element, holder, lazyServiceCollector); super.visitElement(element); } }); }
private void visitXmlFile(@NotNull PsiFile psiFile, @NotNull final ProblemsHolder holder, @NotNull final ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector) { psiFile.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if(XmlHelper.getTagAttributePattern("tag", "method").inside(XmlHelper.getInsideTagPattern("services")).inFile(XmlHelper.getXmlFilePattern()).accepts(element) || XmlHelper.getTagAttributePattern("call", "method").inside(XmlHelper.getInsideTagPattern("services")).inFile(XmlHelper.getXmlFilePattern()).accepts(element) ) { // attach to text child only PsiElement[] psiElements = element.getChildren(); if(psiElements.length < 2) { return; } String serviceClassValue = XmlHelper.getServiceDefinitionClass(element); if(serviceClassValue != null && StringUtils.isNotBlank(serviceClassValue)) { registerMethodProblem(psiElements[1], holder, serviceClassValue, lazyServiceCollector); } } super.visitElement(element); } }); }
/** * Find a string return value of a method context "function() { return 'foo'}" * First match wins */ @Nullable static public String getMethodReturnAsString(@NotNull PhpClass phpClass, @NotNull String methodName) { Method method = phpClass.findMethodByName(methodName); if(method == null) { return null; } final Set<String> values = new HashSet<>(); method.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if(PhpElementsUtil.getMethodReturnPattern().accepts(element)) { String value = PhpElementsUtil.getStringValue(element); if(value != null && StringUtils.isNotBlank(value)) { values.add(value); } } super.visitElement(element); } }); if(values.size() == 0) { return null; } // we support only first item return values.iterator().next(); }
private void removeRedundantAnnotationsInPackage(Project project, PsiFile[] files, PsiAnnotation annotation) { if (!removeRedundantAnnotations) { return; } List<String> redundantAnnotations = nullable ? NullableNotNullManager.getInstance(project).getNullables() : NullableNotNullManager.getInstance(project).getNotNulls(); Set<PsiAnnotation.TargetType> targetsForDefaultAnnotation = targetTypesForDefault(annotation); if (targetsForDefaultAnnotation == null) { return; } for (PsiFile file : files) { if (file instanceof PsiJavaFile) { JavaElementVisitor visitor = new JavaElementVisitor() { @Override public void visitMethod(@Nonnull PsiMethod method) { removeRedundantAnnotations(method, redundantAnnotations, targetsForDefaultAnnotation); } @Override public void visitParameter(@Nonnull PsiParameter parameter) { removeRedundantAnnotations(parameter, redundantAnnotations, targetsForDefaultAnnotation); } @Override public void visitField(@Nonnull PsiField field) { removeRedundantAnnotations(field, redundantAnnotations, targetsForDefaultAnnotation); } @Override public void visitLocalVariable(@Nonnull PsiLocalVariable variable) { super.visitLocalVariable(variable); } }; PsiRecursiveElementWalkingVisitor recursiveVisitor = new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(@Nonnull PsiElement element) { element.accept(visitor); super.visitElement(element); } }; file.accept(recursiveVisitor); JavaCodeStyleManager.getInstance(project).optimizeImports(file); } } }
private void visitReturnElements(@NotNull Project project, @NotNull String className, @NotNull String methodName, @NotNull final ReturnVisitor visitor) { for (PhpClass phpClass : PhpIndex.getInstance(project).getAllSubclasses(className)) { final Method method = phpClass.findOwnMethodByName(methodName); if(method == null) { continue; } method.acceptChildren(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if(!(element instanceof PhpReturn)) { super.visitElement(element); return; } PsiElement firstChild = ((PhpReturn) element).getFirstPsiChild(); if(!(firstChild instanceof ArrayCreationExpression)) { return; } for (PsiElement arrayValue : firstChild.getChildren()) { if(arrayValue.getNode().getElementType() != PhpElementTypes.ARRAY_VALUE) { continue; } PsiElement stringLiteral = arrayValue.getFirstChild(); if(!(stringLiteral instanceof StringLiteralExpression)) { continue; } String contents = ((StringLiteralExpression) stringLiteral).getContents(); if(StringUtils.isNotBlank(contents)) { visitor.visit(method, (StringLiteralExpression) stringLiteral, contents); } } super.visitElement(element); } }); } }