@SuppressWarnings("ConstantConditions")
  public void testIsSupported() throws Exception {
    VirtualFile layoutFile =
        myFixture.copyFileToProject("xmlpull/layout.xml", "res/layout-land-v14/foo.xml");
    VirtualFile menuFile = myFixture.copyFileToProject("menus/menu1.xml", "res/menu/menu1.xml");
    VirtualFile drawableFile = myFixture.copyFileToProject("menus/menu1.xml", "res/menu/menu1.xml");

    PsiManager psiManager = PsiManager.getInstance(getProject());

    assertTrue(LayoutPullParserFactory.isSupported(psiManager.findFile(layoutFile)));
    assertTrue(LayoutPullParserFactory.isSupported(psiManager.findFile(menuFile)));
    assertTrue(LayoutPullParserFactory.isSupported(psiManager.findFile(drawableFile)));
  }
Пример #2
0
 public static void addLibraryChildren(
     final LibraryOrSdkOrderEntry entry,
     final List<AbstractTreeNode> children,
     Project project,
     ProjectViewNode node) {
   final PsiManager psiManager = PsiManager.getInstance(project);
   VirtualFile[] files =
       entry instanceof LibraryOrderEntry
           ? getLibraryRoots((LibraryOrderEntry) entry)
           : entry.getRootFiles(OrderRootType.CLASSES);
   for (final VirtualFile file : files) {
     if (!file.isValid()) continue;
     if (file.isDirectory()) {
       final PsiDirectory psiDir = psiManager.findDirectory(file);
       if (psiDir == null) {
         continue;
       }
       children.add(new PsiDirectoryNode(project, psiDir, node.getSettings()));
     } else {
       final PsiFile psiFile = psiManager.findFile(file);
       if (psiFile == null) continue;
       children.add(new PsiFileNode(project, psiFile, node.getSettings()));
     }
   }
 }
 private boolean processFile(
     VirtualFile file, BundleNameEvaluator evaluator, PropertiesFileProcessor processor) {
   final PsiFile psiFile = myPsiManager.findFile(file);
   PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(psiFile);
   if (propertiesFile != null) {
     final String qName = evaluator.evaluateBundleName(psiFile);
     if (qName != null) {
       if (!processor.process(qName, propertiesFile)) return false;
     }
   }
   return true;
 }
  public static Collection<JSClass> searchClassInheritors(
      ID<String, Void> indexId, String name, Project project, final GlobalSearchScope scope) {
    final Collection<VirtualFile> files =
        FileBasedIndex.getInstance().getContainingFiles(indexId, name, scope);
    final PsiManager psiManager = PsiManager.getInstance(project);
    Collection<JSClass> classes = new ArrayList<JSClass>(files.size());
    for (VirtualFile file : files) {
      PsiFile psifile = psiManager.findFile(file);
      if (!(psifile instanceof XmlFile)) continue;
      classes.addAll(XmlBackedJSClassImpl.getClasses((XmlFile) psifile));
    }

    if (FlexNameAlias.CONTAINER_TYPE_NAME.equals(name)) {
      classes.addAll(
          searchClassInheritors(indexId, FlexNameAlias.COMPONENT_TYPE_NAME, project, scope));
    }
    return classes;
  }
  public void search() {
    myFiles.clear();
    myJars.clear();

    PsiManager psiManager = PsiManager.getInstance(myProject);
    for (PsiFile file : search(myModule, myProject)) {
      VirtualFile jar = JarFileSystem.getInstance().getVirtualFileForJar(file.getVirtualFile());
      if (jar != null) {
        myJars.putValue(jar, file);
      } else {
        Module module = ModuleUtilCore.findModuleForPsiElement(file);
        if (module != null) {
          myFiles.putValue(module, file);
        } else {
          VirtualFile virtualFile = file.getVirtualFile();
          myVirtualFiles.putValue(virtualFile.getParent(), psiManager.findFile(virtualFile));
        }
      }
    }
  }
 @Nullable
 private PsiFile getConfigFile(@NotNull Project project) {
   String directoryName = myConfigFileTextFieldWithBrowseButton.getText();
   if (directoryName.length() == 0) return null;
   directoryName = directoryName.replace(File.separatorChar, '/');
   VirtualFile path = LocalFileSystem.getInstance().findFileByPath(directoryName);
   while (path == null && directoryName.length() > 0) {
     int pos = directoryName.lastIndexOf('/');
     if (pos <= 0) break;
     directoryName = directoryName.substring(0, pos);
     path = LocalFileSystem.getInstance().findFileByPath(directoryName);
   }
   if (path != null) {
     PsiManager psiManager = PsiManager.getInstance(project);
     if (!path.isDirectory()) {
       return psiManager.findFile(path);
     }
   }
   return null;
 }
 public void processIncludingFiles(
     PsiFile context, Processor<Pair<VirtualFile, FileIncludeInfo>> processor) {
   context = context.getOriginalFile();
   VirtualFile contextFile = context.getVirtualFile();
   if (contextFile == null) return;
   MultiMap<VirtualFile, FileIncludeInfoImpl> infoList =
       FileIncludeIndex.getIncludingFileCandidates(
           context.getName(), GlobalSearchScope.allScope(myProject));
   for (VirtualFile candidate : infoList.keySet()) {
     PsiFile psiFile = myPsiManager.findFile(candidate);
     if (psiFile == null || context.equals(psiFile)) continue;
     for (FileIncludeInfo info : infoList.get(candidate)) {
       PsiFileSystemItem item = resolveFileInclude(info, psiFile);
       if (item != null && contextFile.equals(item.getVirtualFile())) {
         if (!processor.process(Pair.create(candidate, info))) {
           return;
         }
       }
     }
   }
 }
  public static Collection<ErlangFile> findFileTestElements(
      Project project, DataContext dataContext) {
    VirtualFile[] selectedFiles = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);

    if (selectedFiles == null) return Collections.emptyList();

    List<ErlangFile> testFiles = new ArrayList<ErlangFile>(selectedFiles.length);
    PsiManager psiManager = PsiManager.getInstance(project);
    for (VirtualFile file : selectedFiles) {
      if (!file.isDirectory()) {
        ContainerUtil.addIfNotNull(testFiles, getFileTestElement(psiManager.findFile(file)));
        continue;
      }

      PsiDirectory directory = psiManager.findDirectory(file);
      PsiFile[] children = directory == null ? new PsiFile[0] : directory.getFiles();
      for (PsiFile psiFile : children) {
        ContainerUtil.addIfNotNull(testFiles, getFileTestElement(psiFile));
      }
    }
    return testFiles;
  }
  @Override
  @NotNull
  public Collection<AbstractTreeNode> getChildren() {
    Module module = getValue();
    if (module == null || module.isDisposed()) { // module has been disposed
      return Collections.emptyList();
    }
    ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
    ModuleFileIndex moduleFileIndex = rootManager.getFileIndex();

    final VirtualFile[] contentRoots = rootManager.getContentRoots();
    final List<AbstractTreeNode> children =
        new ArrayList<AbstractTreeNode>(contentRoots.length + 1);
    final PsiManager psiManager = PsiManager.getInstance(module.getProject());
    for (final VirtualFile contentRoot : contentRoots) {
      if (!moduleFileIndex.isInContent(contentRoot)) continue;

      if (contentRoot.isDirectory()) {
        PsiDirectory directory = psiManager.findDirectory(contentRoot);
        if (directory != null) {
          children.add(new PsiDirectoryNode(getProject(), directory, getSettings()));
        }
      } else {
        PsiFile file = psiManager.findFile(contentRoot);
        if (file != null) {
          children.add(new PsiFileNode(getProject(), file, getSettings()));
        }
      }
    }

    /*
    if (getSettings().isShowLibraryContents()) {
      children.add(new LibraryGroupNode(getProject(), new LibraryGroupElement(getValue()), getSettings()));
    }
    */
    return children;
  }
 public static void cacheSingleFile(Project project, VirtualFile openFile) {
   if (openFile.exists()) {
     // cache attributes
     PsiManager psiManager = PsiManager.getInstance(project);
     if (psiManager == null) return;
     PsiFile psiFile = psiManager.findFile(openFile);
     if (psiFile == null) return;
     FileASTNode astNode = psiFile.getNode();
     if (astNode == null) return;
     HashSet<String> rs = findAllVariables(astNode.getChildren(null), PerlTypes.VARIABLE, false);
     if (rs == null) return;
     for (String str : rs) {
       addCachedVariables(null, str);
     }
     // cache subs
     ArrayList<Package> packages = ModulesContainer.getPackageListFromFile(openFile.getPath());
     for (int i = 0; i < packages.size(); i++) {
       ArrayList<Sub> subs = packages.get(i).getAllSubs();
       for (int j = 0; j < subs.size(); j++) {
         addCachedSub(null, subs.get(j));
       }
     }
   }
 }
 @Nullable
 public PsiFile getPsiFile() {
   return myPsiManager.findFile(myVirtualFile);
 }
  @Override
  public void setToEditor(@NotNull final Editor editor) {
    assertDispatchThread();
    final PsiManager psiManager = PsiManager.getInstance(myProject);
    if (psiManager.isDisposed()) return;

    if (!myFile.isValid()) return;
    final PsiFile psiFile = psiManager.findFile(myFile);
    if (psiFile == null) return;

    if (!mySerializedElements.isEmpty()) {
      // Restore postponed state
      assert myPsiElements.isEmpty() : "Sequential deserialization";
      for (SerializedPsiElement entry : mySerializedElements) {
        PsiElement restoredElement =
            FoldingPolicy.restoreBySignature(psiFile, entry.mySerializedElement);
        if (restoredElement != null && restoredElement.isValid()) {
          myPsiElements.add(
              SmartPointerManager.getInstance(myProject)
                  .createSmartPsiElementPointer(restoredElement));
          restoredElement.putUserData(FOLDING_INFO_KEY, entry.myFoldingInfo);
        }
      }
      mySerializedElements.clear();
    }

    Map<PsiElement, FoldingDescriptor> ranges = null;
    for (SmartPsiElementPointer<PsiElement> ptr : myPsiElements) {
      PsiElement element = ptr.getElement();
      if (element == null || !element.isValid()) {
        continue;
      }

      if (ranges == null) {
        ranges = buildRanges(editor, psiFile);
      }
      FoldingDescriptor descriptor = ranges.get(element);
      if (descriptor == null) {
        continue;
      }

      TextRange range = descriptor.getRange();
      FoldRegion region =
          FoldingUtil.findFoldRegion(editor, range.getStartOffset(), range.getEndOffset());
      if (region != null) {
        FoldingInfo fi = element.getUserData(FOLDING_INFO_KEY);
        boolean state = fi != null && fi.expanded;
        region.setExpanded(state);
      }
    }
    for (RangeMarker marker : myRangeMarkers) {
      if (!marker.isValid()) {
        continue;
      }
      FoldRegion region =
          FoldingUtil.findFoldRegion(editor, marker.getStartOffset(), marker.getEndOffset());
      FoldingInfo info = marker.getUserData(FOLDING_INFO_KEY);
      if (region == null) {
        if (info != null) {
          region =
              editor
                  .getFoldingModel()
                  .addFoldRegion(marker.getStartOffset(), marker.getEndOffset(), info.placeHolder);
        }
        if (region == null) {
          return;
        }
      }

      boolean state = info != null && info.expanded;
      region.setExpanded(state);
    }
  }
  @Override
  @Nullable
  public List<PsiFile> findExternalAnnotationsFiles(@NotNull PsiModifierListOwner listOwner) {
    final PsiFile containingFile = listOwner.getContainingFile();
    if (!(containingFile instanceof PsiJavaFile)) {
      return null;
    }
    final PsiJavaFile javaFile = (PsiJavaFile) containingFile;
    final String packageName = javaFile.getPackageName();
    final VirtualFile virtualFile = containingFile.getVirtualFile();
    if (virtualFile == null) return null;

    final List<PsiFile> files = myExternalAnnotations.get(virtualFile);
    if (files == NULL_LIST) return null;
    if (files != null) {
      boolean allValid = true;
      for (PsiFile file : files) {
        allValid &= file.isValid();
      }
      if (allValid) {
        return files;
      }
    }

    if (virtualFile == null) {
      return null;
    }

    Set<PsiFile> possibleAnnotationsXmls = new THashSet<PsiFile>();
    for (VirtualFile root : getExternalAnnotationsRoots(virtualFile)) {
      final VirtualFile ext =
          root.findFileByRelativePath(packageName.replace('.', '/') + "/" + ANNOTATIONS_XML);
      if (ext == null) continue;
      final PsiFile psiFile = myPsiManager.findFile(ext);
      if (psiFile == null) continue;
      possibleAnnotationsXmls.add(psiFile);
    }
    List<PsiFile> result;
    if (possibleAnnotationsXmls.isEmpty()) {
      myExternalAnnotations.put(virtualFile, NULL_LIST);
      result = null;
    } else {
      result = new SmartList<PsiFile>(possibleAnnotationsXmls);
      // sorting by writability: writable go first
      Collections.sort(
          result,
          new Comparator<PsiFile>() {
            @Override
            public int compare(PsiFile f1, PsiFile f2) {
              boolean w1 = f1.isWritable();
              boolean w2 = f2.isWritable();
              if (w1 == w2) {
                return 0;
              }
              return w1 ? -1 : 1;
            }
          });

      myExternalAnnotations.put(virtualFile, result);
    }
    return result;
  }
  public JComponent createComponent() {
    final JPanel panel = new JPanel(new GridBagLayout());
    panel.setBorder(IdeBorderFactory.createTitledBorder("File Context", false));
    myCombo = new ComboBox();
    myCombo.putClientProperty(CONTEXTS_COMBO_KEY, Boolean.TRUE);
    panel.add(
        new JLabel("Included into:"),
        new GridBagConstraints(
            0,
            0,
            1,
            1,
            0.0,
            0.0,
            GridBagConstraints.WEST,
            GridBagConstraints.NONE,
            new Insets(5, 0, 5, 0),
            0,
            0));
    panel.add(
        myCombo,
        new GridBagConstraints(
            1,
            0,
            1,
            1,
            1.0,
            0.0,
            GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL,
            new Insets(5, 5, 5, 0),
            0,
            0));

    final PsiManager psiManager = PsiManager.getInstance(myProject);
    final FileBasedIndex fbi = FileBasedIndex.getInstance();
    final Collection<VirtualFile> antFiles =
        fbi.getContainingFiles(
            AntImportsIndex.INDEX_NAME, AntImportsIndex.ANT_FILES_WITH_IMPORTS_KEY, myFileFilter);

    for (VirtualFile file : antFiles) {
      final PsiFile psiFile = psiManager.findFile(file);
      if (!(psiFile instanceof XmlFile)) {
        continue;
      }
      final XmlFile xmlFile = (XmlFile) psiFile;
      if (!xmlFile.equals(myFile) && AntDomFileDescription.isAntFile(xmlFile)) {
        final String path = PathUtil.getLocalPath(file);
        final XmlFile previous = myPathToFileMap.put(path, xmlFile);
        assert previous == null;
      }
    }

    final List<String> paths = new ArrayList<String>(myPathToFileMap.keySet());
    Collections.sort(
        paths,
        new Comparator<String>() {
          public int compare(final String o1, final String o2) {
            return o1.compareTo(o2);
          }
        });

    myCombo.addItem(NONE);
    for (String path : paths) {
      myCombo.addItem(path);
    }

    final AntConfigurationBase antConfig = AntConfigurationBase.getInstance(myProject);
    final XmlFile currentContext = antConfig.getContextFile(myFile);
    if (currentContext != null) {
      final VirtualFile vFile = currentContext.getVirtualFile();

      assert vFile != null;

      final String path = PathUtil.getLocalPath(vFile);
      if (!FileUtil.pathsEqual(path, myLocalPath)) {
        myOriginalContext = path;
      }
    }
    myCombo.setSelectedItem(myOriginalContext);

    return panel;
  }