/** * @return true, if the element contains a reference to a different class than fullyQualifiedName * but which has the same class name */ public static boolean containsConflictingReference(PsiFile element, String fullyQualifiedName) { final Map<String, Boolean> cachedValue = CachedValuesManager.getManager(element.getProject()) .getCachedValue( element, new CachedValueProvider<Map<String, Boolean>>() { @Nullable @Override public Result<Map<String, Boolean>> compute() { return new Result<Map<String, Boolean>>( Collections.synchronizedMap(new HashMap<String, Boolean>()), PsiModificationTracker.MODIFICATION_COUNT); } }); Boolean conflictingRef = cachedValue.get(fullyQualifiedName); if (conflictingRef != null) { return conflictingRef.booleanValue(); } final ConflictingClassReferenceVisitor visitor = new ConflictingClassReferenceVisitor(fullyQualifiedName); element.accept(visitor); conflictingRef = visitor.isConflictingReferenceFound(); cachedValue.put(fullyQualifiedName, conflictingRef); return conflictingRef.booleanValue(); }
@Override protected void checkFile(PsiFile file, final ProblemsHolder problemsHolder) { if (!(file instanceof ErlangFile)) return; file.accept( new ErlangRecursiveVisitor() { @Override public void visitQVar(@NotNull ErlangQVar o) { PsiReference reference = o.getReference(); PsiElement resolve = reference != null ? reference.resolve() : null; if (resolve == null && !isForceSkipped(o) && !isMacros(o) && ((inDefinition(o) && !inArgumentList(o)) || inAssignment(o))) { ErlangFunctionClause functionClause = PsiTreeUtil.getTopmostParentOfType(o, ErlangFunctionClause.class); if (functionClause == null) return; Query<PsiReference> search = ReferencesSearch.search(o, new LocalSearchScope(functionClause)); if (search.findFirst() == null) { problemsHolder.registerProblem( o, "Unused variable " + "'" + o.getText() + "'", ProblemHighlightType.LIKE_UNUSED_SYMBOL, new ErlangRenameVariableFix()); } } } }); }
public static String getUnescapedText( PsiFile file, @Nullable final PsiElement startElement, @Nullable final PsiElement endElement) { final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(file.getProject()); if (manager.getInjectionHost(file) == null) { return file.getText() .substring( startElement == null ? 0 : startElement.getTextRange().getStartOffset(), endElement == null ? file.getTextLength() : endElement.getTextRange().getStartOffset()); } final StringBuilder sb = new StringBuilder(); file.accept( new PsiRecursiveElementWalkingVisitor() { Boolean myState = startElement == null ? Boolean.TRUE : null; @Override public void visitElement(PsiElement element) { if (element == startElement) myState = Boolean.TRUE; if (element == endElement) myState = Boolean.FALSE; if (Boolean.FALSE == myState) return; if (Boolean.TRUE == myState && element.getFirstChild() == null) { sb.append(getUnescapedLeafText(element, false)); } else { super.visitElement(element); } } }); return sb.toString(); }
@Nullable public ProblemDescriptor[] checkFile( @NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { final Language language = file.getLanguage(); if (!acceptsLanguage(language)) return null; final Visitor visitor = createVisitor(manager, isOnTheFly); file.accept(visitor); return visitor.getProblems(); }
private static void checkFile(PsiFile file, final ProblemsHolder problemsHolder) { file.accept( new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element instanceof BnfChoice) { checkChoice((BnfChoice) element, problemsHolder); } super.visitElement(element); } }); }
private static void mapJsp(FileContent inputData, final List<LinkInfo> result) { final FileViewProvider viewProvider = inputData.getPsiFile().getViewProvider(); PsiFile psiFile = null; if (viewProvider instanceof TemplateLanguageFileViewProvider) { final Language dataLanguage = ((TemplateLanguageFileViewProvider) viewProvider).getTemplateDataLanguage(); if (dataLanguage == HTMLLanguage.INSTANCE || dataLanguage == XHTMLLanguage.INSTANCE) { psiFile = viewProvider.getPsi(dataLanguage); } } else { psiFile = viewProvider.getPsi(viewProvider.getBaseLanguage()); } if (psiFile != null) { final XmlRecursiveElementVisitor visitor = new XmlRecursiveElementVisitor() { @Override public void visitXmlTag(XmlTag tag) { if (LINK.equalsIgnoreCase(tag.getLocalName())) { final String href = getAttributeValue(tag, HREF_ATTR); final String media = getAttributeValue(tag, MEDIA_ATTR); final String type = getAttributeValue(tag, TYPE_ATTR); final String rel = getAttributeValue(tag, REL_ATTR); final String title = getAttributeValue(tag, TITLE_ATTR); addResult( result, tag.getTextOffset(), href, media, type, rel, title, isHrefScripted(tag)); } super.visitXmlTag(tag); } }; psiFile.accept(visitor); } }
@Override protected void checkFile(PsiFile file, final ProblemsHolder problemsHolder) { if (!(file instanceof ErlangFile)) return; file.accept( new ErlangRecursiveVisitor() { @Override public void visitTypeDefinition(@NotNull ErlangTypeDefinition o) { Query<PsiReference> search = ReferencesSearch.search(o, new LocalSearchScope(o.getContainingFile())); if (search.findFirst() == null) { problemsHolder.registerProblem( o.getNameIdentifier(), "Unused function " + "'" + o.getName() + "'", ProblemHighlightType.LIKE_UNUSED_SYMBOL, new ErlangRemoveTypeFix(), new ErlangExportTypeFix()); } } }); }
@Override protected void checkFile(PsiFile file, final ProblemsHolder problemsHolder) { if (!(file instanceof ErlangFile)) return; file.accept( new ErlangRecursiveVisitor() { @Override public void visitExportFunction(@NotNull ErlangExportFunction o) { PsiReference reference = o.getReference(); if (reference.resolve() == null) { String name = o.getQAtom().getText(); int arity = ErlangPsiImplUtil.getArity(o.getInteger()); if (arity < 0) return; problemsHolder.registerProblem( o, "Unresolved function " + "'" + o.getText() + "'", new ErlangCreateFunctionQuickFix(name, arity)); } } }); }
@NotNull public static PsiElement createYamlPsiFromText( Project p, final IElementType type, @NotNull String text) { final Ref<PsiElement> ret = new Ref<PsiElement>(); PsiFile dummyFile = createDummyFile(p, text); dummyFile.accept( new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element.getNode() == type) { ret.set(element); } super.visitElement(element); } }); assert !ret.isNull() : "cannot create element from text:\n" + dummyFile.getText(); return ret.get(); }
private void checkAllElementsValid(@NotNull Document document, @NotNull final Object reason) { final PsiFile psiFile = getCachedPsiFile(document); if (psiFile != null) { psiFile.accept( new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (!element.isValid()) { throw new AssertionError( "Commit to '" + psiFile.getVirtualFile() + "' has led to invalid element: " + element + "; Reason: '" + reason + "'"); } } }); } }
public static TodoItem[] findAllTodos(final PsiFile psiFile, final PsiTodoSearchHelper helper) { final List<TodoItem> todoItems = new ArrayList<TodoItem>(Arrays.asList(helper.findTodoItems(psiFile))); psiFile.accept( new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element instanceof PsiLanguageInjectionHost) { InjectedLanguageUtil.enumerate( element, new PsiLanguageInjectionHost.InjectedPsiVisitor() { @Override public void visit( @NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) { if (places.size() == 1) { Document document = PsiDocumentManager.getInstance(injectedPsi.getProject()) .getCachedDocument(injectedPsi); if (!(document instanceof DocumentWindow)) return; for (TodoItem item : helper.findTodoItems(injectedPsi)) { TextRange rangeInHost = ((DocumentWindow) document).injectedToHost(item.getTextRange()); todoItems.add( new TodoItemImpl( psiFile, rangeInHost.getStartOffset(), rangeInHost.getEndOffset(), item.getPattern())); } } } }); } super.visitElement(element); } }); return todoItems.toArray(new TodoItem[todoItems.size()]); }
private List<String> collectAssemblyReferences(PsiFile file) { if (!(PythonSdkFlavor.getFlavor(mySdk) instanceof IronPythonSdkFlavor)) { return Collections.emptyList(); } final List<String> result = new ArrayList<String>(); file.accept( new PyRecursiveElementVisitor() { @Override public void visitPyCallExpression(PyCallExpression node) { super.visitPyCallExpression(node); // TODO: What if user loads it not by literal? We need to ask user for list of DLLs if (node.isCalleeText( "AddReference", "AddReferenceByPartialName", "AddReferenceByName")) { final PyExpression[] args = node.getArguments(); if (args.length == 1 && args[0] instanceof PyStringLiteralExpression) { result.add(((PyStringLiteralExpression) args[0]).getStringValue()); } } } }); return result; }
public PsiFile[] getRelatedFiles(final XPathFile file) { final XmlAttribute attribute = PsiTreeUtil.getContextOfType(file, XmlAttribute.class, false); assert attribute != null; final PsiFile psiFile = attribute.getContainingFile(); assert psiFile != null; final List<PsiFile> files = new ArrayList<PsiFile>(); psiFile.accept( new XmlRecursiveElementVisitor() { @Override public void visitXmlAttribute(XmlAttribute attribute) { final PsiFile[] _files = XsltSupport.getFiles(attribute); for (PsiFile _file : _files) { if (_file != file) files.add(_file); } } }); return PsiUtilCore.toPsiFileArray(files); }
@Override public ProblemDescriptor[] checkFile( @NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { if (!(file instanceof XmlFile)) { return ProblemDescriptor.EMPTY_ARRAY; } final AndroidFacet facet = AndroidFacet.getInstance(file); if (facet == null) { return ProblemDescriptor.EMPTY_ARRAY; } final DomFileDescription<?> description = DomManager.getDomManager(file.getProject()).getDomFileDescription((XmlFile) file); if (!(description instanceof LayoutDomFileDescription) && !(description instanceof MenuDomFileDescription)) { return ProblemDescriptor.EMPTY_ARRAY; } final Collection<PsiClass> activities = findRelatedActivities((XmlFile) file, facet, description); final MyVisitor visitor = new MyVisitor(manager, isOnTheFly, activities); file.accept(visitor); return visitor.myResult.toArray(new ProblemDescriptor[visitor.myResult.size()]); }
public static List<Pair<PsiFile, HighlightInfo>> runGeneralHighlighting( PsiFile file, final boolean highlightErrorElements, final boolean runAnnotators) { MyPsiElementVisitor visitor = new MyPsiElementVisitor(highlightErrorElements, runAnnotators); file.accept(visitor); return new ArrayList<Pair<PsiFile, HighlightInfo>>(visitor.result); }
public static MultiMap<String, GoImportSpec> filterUnusedImports( @NotNull PsiFile file, @NotNull MultiMap<String, GoImportSpec> importMap) { MultiMap<String, GoImportSpec> result = MultiMap.create(); result.putAllValues(importMap); result.remove("_"); // imports for side effects are always used Collection<GoImportSpec> implicitImports = ContainerUtil.newArrayList(result.get(".")); for (GoImportSpec importEntry : implicitImports) { GoImportSpec spec = getImportSpec(importEntry); if (spec != null && spec.isDot() && hasImportUsers(spec)) { result.remove(".", importEntry); } } file.accept( new GoRecursiveVisitor() { @Override public void visitTypeReferenceExpression(@NotNull GoTypeReferenceExpression o) { GoTypeReferenceExpression lastQualifier = o.getQualifier(); if (lastQualifier != null) { GoTypeReferenceExpression previousQualifier; while ((previousQualifier = lastQualifier.getQualifier()) != null) { lastQualifier = previousQualifier; } markAsUsed(lastQualifier.getIdentifier(), lastQualifier.getReference()); } } @Override public void visitReferenceExpression(@NotNull GoReferenceExpression o) { GoReferenceExpression lastQualifier = o.getQualifier(); if (lastQualifier != null) { GoReferenceExpression previousQualifier; while ((previousQualifier = lastQualifier.getQualifier()) != null) { lastQualifier = previousQualifier; } markAsUsed(lastQualifier.getIdentifier(), lastQualifier.getReference()); } } private void markAsUsed(@NotNull PsiElement qualifier, @NotNull PsiReference reference) { String qualifierText = qualifier.getText(); if (!result.containsKey(qualifierText)) { // already marked return; } PsiElement resolve = reference.resolve(); if (!(resolve instanceof PsiDirectory || resolve instanceof GoImportSpec || resolve instanceof PsiDirectoryContainer)) { return; } Collection<String> qualifiersToDelete = ContainerUtil.newHashSet(); for (GoImportSpec spec : result.get(qualifierText)) { for (Map.Entry<String, Collection<GoImportSpec>> entry : result.entrySet()) { for (GoImportSpec importSpec : entry.getValue()) { if (importSpec == spec) { qualifiersToDelete.add(entry.getKey()); } } } } for (String qualifierToDelete : qualifiersToDelete) { result.remove(qualifierToDelete); } } }); return result; }
public static MultiMap<String, GoImportSpec> filterUnusedImports( @NotNull PsiFile file, @NotNull final MultiMap<String, GoImportSpec> importMap) { final MultiMap<String, GoImportSpec> result = MultiMap.create(); result.putAllValues(importMap); result.remove("_"); // imports for side effects are always used Collection<GoImportSpec> implicitImports = ContainerUtil.newArrayList(result.get(".")); for (GoImportSpec importEntry : implicitImports) { GoImportSpec spec = getImportSpec(importEntry); if (spec != null && spec.isDot()) { List<? extends PsiElement> list = spec.getUserData(GoReference.IMPORT_USERS); if (list != null) { for (PsiElement e : list) { if (e.isValid()) { result.remove(".", importEntry); } } } } } file.accept( new GoRecursiveVisitor() { @Override public void visitTypeReferenceExpression(@NotNull GoTypeReferenceExpression o) { GoTypeReferenceExpression lastQualifier = o.getQualifier(); if (lastQualifier != null) { GoTypeReferenceExpression previousQualifier; while ((previousQualifier = lastQualifier.getQualifier()) != null) { lastQualifier = previousQualifier; } markAsUsed(lastQualifier.getIdentifier()); } } @Override public void visitReferenceExpression(@NotNull GoReferenceExpression o) { GoReferenceExpression lastQualifier = o.getQualifier(); if (lastQualifier != null) { GoReferenceExpression previousQualifier; while ((previousQualifier = lastQualifier.getQualifier()) != null) { lastQualifier = previousQualifier; } markAsUsed(lastQualifier.getIdentifier()); } } private void markAsUsed(@NotNull PsiElement qualifier) { Collection<String> qualifiersToDelete = ContainerUtil.newHashSet(); for (GoImportSpec spec : result.get(qualifier.getText())) { for (Map.Entry<String, Collection<GoImportSpec>> entry : result.entrySet()) { for (GoImportSpec importSpec : entry.getValue()) { if (importSpec == spec) { qualifiersToDelete.add(entry.getKey()); } } } } for (String qualifierToDelete : qualifiersToDelete) { result.remove(qualifierToDelete); } } }); return result; }