private void initPubListPackageDirsMap(final @NotNull VirtualFile contextFile) {
    final Module module = ModuleUtilCore.findModuleForFile(contextFile, myProject);

    final List<OrderEntry> orderEntries =
        module != null
            ? Arrays.asList(ModuleRootManager.getInstance(module).getOrderEntries())
            : ProjectRootManager.getInstance(myProject)
                .getFileIndex()
                .getOrderEntriesForFile(contextFile);
    for (OrderEntry orderEntry : orderEntries) {
      if (orderEntry instanceof LibraryOrderEntry
          && LibraryTablesRegistrar.PROJECT_LEVEL.equals(
              ((LibraryOrderEntry) orderEntry).getLibraryLevel())
          && PubListPackageDirsAction.PUB_LIST_PACKAGE_DIRS_LIB_NAME.equals(
              ((LibraryOrderEntry) orderEntry).getLibraryName())) {
        final LibraryEx library = (LibraryEx) ((LibraryOrderEntry) orderEntry).getLibrary();
        final LibraryProperties properties = library == null ? null : library.getProperties();

        if (properties instanceof DartListPackageDirsLibraryProperties) {
          myPubListPackageDirsMap.putAll(
              ((DartListPackageDirsLibraryProperties) properties).getPackageNameToDirsMap());
          return;
        }
      }
    }
  }
Ejemplo n.º 2
0
  @NotNull
  private static DartLibInfo collectPackagesLibraryRoots(
      @NotNull final Project project, @NotNull final DartSdk sdk) {
    final DartLibInfo libInfo = new DartLibInfo(false);

    final Collection<VirtualFile> pubspecYamlFiles =
        FilenameIndex.getVirtualFilesByName(
            project, PUBSPEC_YAML, GlobalSearchScope.projectScope(project));
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();

    for (VirtualFile pubspecFile : pubspecYamlFiles) {
      final VirtualFile dotPackagesFile =
          pubspecFile.getParent().findChild(DotPackagesFileUtil.DOT_PACKAGES);
      final Module module =
          dotPackagesFile == null ? null : fileIndex.getModuleForFile(dotPackagesFile);

      if (dotPackagesFile != null
          && !dotPackagesFile.isDirectory()
          && module != null
          && DartSdkGlobalLibUtil.isDartSdkEnabled(module)) {
        final Map<String, String> packagesMap = DotPackagesFileUtil.getPackagesMap(dotPackagesFile);
        if (packagesMap != null) {
          for (Map.Entry<String, String> entry : packagesMap.entrySet()) {
            final String packageName = entry.getKey();
            final String packagePath = entry.getValue();
            if (isPathOutsideProjectContent(fileIndex, packagePath)) {
              libInfo.addPackage(packageName, packagePath);
            }
          }
        }
      }
    }

    return libInfo;
  }
  private void initLivePackageNameToDirMap() {
    final VirtualFile baseDir = myPubspecYamlFile == null ? null : myPubspecYamlFile.getParent();
    if (myPubspecYamlFile == null || baseDir == null) return;

    final String name = PubspecYamlUtil.getDartProjectName(myPubspecYamlFile);
    final VirtualFile libFolder = baseDir.findChild(PubspecYamlUtil.LIB_DIR_NAME);

    if (name != null && libFolder != null && libFolder.isDirectory()) {
      myLivePackageNameToDirMap.put(name, libFolder);
    }

    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();

    PubspecYamlUtil.processPathPackages(
        myPubspecYamlFile,
        new PairConsumer<String, VirtualFile>() {
          @Override
          public void consume(
              @NotNull final String packageName, @NotNull final VirtualFile packageDir) {
            if (fileIndex.isInContent(packageDir)) {
              myLivePackageNameToDirMap.put(packageName, packageDir);
            }
          }
        });
  }
  public static void removeDuplicatingClasses(
      final Module module,
      @NotNull final String packageName,
      @NotNull String className,
      @Nullable File classFile,
      String sourceRootPath) {
    if (sourceRootPath == null) {
      return;
    }
    VirtualFile sourceRoot = LocalFileSystem.getInstance().findFileByPath(sourceRootPath);
    if (sourceRoot == null) {
      return;
    }
    final Project project = module.getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final String interfaceQualifiedName = packageName + '.' + className;
    PsiClass[] classes =
        facade.findClasses(interfaceQualifiedName, GlobalSearchScope.moduleScope(module));
    final ProjectFileIndex projectFileIndex =
        ProjectRootManager.getInstance(project).getFileIndex();
    for (PsiClass c : classes) {
      PsiFile psiFile = c.getContainingFile();
      if (className.equals(FileUtil.getNameWithoutExtension(psiFile.getName()))) {
        VirtualFile virtualFile = psiFile.getVirtualFile();
        if (virtualFile != null
            && projectFileIndex.getSourceRootForFile(virtualFile) == sourceRoot) {
          final String path = virtualFile.getPath();
          File f = new File(path);

          try {
            f = f.getCanonicalFile();
            classFile = classFile != null ? classFile.getCanonicalFile() : null;
            if (f != null && !f.equals(classFile) && f.exists()) {
              if (f.delete()) {
                virtualFile.refresh(true, false);
              } else {
                ApplicationManager.getApplication()
                    .invokeLater(
                        new Runnable() {
                          public void run() {
                            Messages.showErrorDialog(
                                project, "Can't delete file " + path, CommonBundle.getErrorTitle());
                          }
                        },
                        project.getDisposed());
              }
            }
          } catch (IOException e) {
            LOG.info(e);
          }
        }
      }
    }
  }
 @NotNull
 @Override
 public Collection<String> getNonTrivialPackagePrefixes() {
   Set<String> names = myNontrivialPackagePrefixes;
   if (names == null) {
     names = new HashSet<>();
     final ProjectRootManager rootManager = ProjectRootManager.getInstance(myManager.getProject());
     final List<VirtualFile> sourceRoots =
         rootManager.getModuleSourceRoots(JavaModuleSourceRootTypes.SOURCES);
     final ProjectFileIndex fileIndex = rootManager.getFileIndex();
     for (final VirtualFile sourceRoot : sourceRoots) {
       if (sourceRoot.isDirectory()) {
         final String packageName = fileIndex.getPackageNameByDirectory(sourceRoot);
         if (packageName != null && !packageName.isEmpty()) {
           names.add(packageName);
         }
       }
     }
     myNontrivialPackagePrefixes = names;
   }
   return names;
 }
Ejemplo n.º 6
0
  @Nullable
  public VirtualFile findAppDirectory(@Nullable PsiElement element) {
    if (element == null) return null;

    PsiFile containingFile = element.getContainingFile().getOriginalFile();
    VirtualFile file = containingFile.getVirtualFile();
    if (file == null) return null;

    ProjectFileIndex index =
        ProjectRootManager.getInstance(containingFile.getProject()).getFileIndex();

    VirtualFile root = index.getContentRootForFile(file);
    if (root == null) return null;

    return root.findChild(getApplicationDirectoryName());
  }
  private boolean hasAcceptablePackage(@NotNull VirtualFile vFile) {
    if (vFile.getFileType() == JavaClassFileType.INSTANCE) {
      // See IDEADEV-5626
      final VirtualFile root =
          ProjectRootManager.getInstance(myManager.getProject())
              .getFileIndex()
              .getClassRootForFile(vFile);
      VirtualFile parent = vFile.getParent();
      final PsiNameHelper nameHelper = PsiNameHelper.getInstance(myManager.getProject());
      while (parent != null && !Comparing.equal(parent, root)) {
        if (!nameHelper.isIdentifier(parent.getName())) return false;
        parent = parent.getParent();
      }
    }

    return true;
  }
  FindInProjectTask(
      @NotNull final FindModel findModel,
      @NotNull final Project project,
      @Nullable final PsiDirectory psiDirectory) {
    myFindModel = findModel;
    myProject = project;
    myPsiDirectory = psiDirectory;
    myPsiManager = PsiManager.getInstance(project);

    final String moduleName = findModel.getModuleName();
    myModule =
        moduleName == null
            ? null
            : ApplicationManager.getApplication()
                .runReadAction(
                    new Computable<Module>() {
                      @Override
                      public Module compute() {
                        return ModuleManager.getInstance(project).findModuleByName(moduleName);
                      }
                    });
    myFileIndex =
        myModule == null
            ? ProjectRootManager.getInstance(project).getFileIndex()
            : ModuleRootManager.getInstance(myModule).getFileIndex();

    final String filter = findModel.getFileFilter();
    final Pattern pattern = FindInProjectUtil.createFileMaskRegExp(filter);

    //noinspection unchecked
    myFileMask =
        pattern == null
            ? Condition.TRUE
            : new Condition<VirtualFile>() {
              @Override
              public boolean value(VirtualFile file) {
                return file != null && pattern.matcher(file.getName()).matches();
              }
            };

    final ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
    myProgress = progress != null ? progress : new EmptyProgressIndicator();
  }
 public LibraryRuntimeClasspathScope(final Project project, final List<Module> modules) {
   super(project);
   myIndex = ProjectRootManager.getInstance(project).getFileIndex();
   final Set<Sdk> processedSdk = new THashSet<Sdk>();
   final Set<Library> processedLibraries = new THashSet<Library>();
   final Set<Module> processedModules = new THashSet<Module>();
   final Condition<OrderEntry> condition =
       new Condition<OrderEntry>() {
         @Override
         public boolean value(OrderEntry orderEntry) {
           if (orderEntry instanceof ModuleOrderEntry) {
             final Module module = ((ModuleOrderEntry) orderEntry).getModule();
             return module != null && !processedModules.contains(module);
           }
           return true;
         }
       };
   for (Module module : modules) {
     buildEntries(module, processedModules, processedLibraries, processedSdk, condition);
   }
 }
Ejemplo n.º 10
0
 public BackendCompilerWrapper(
     TranslatingCompiler translatingCompiler,
     Chunk<Module> chunk,
     @NotNull final Project project,
     @NotNull List<VirtualFile> filesToCompile,
     @NotNull CompileContextEx compileContext,
     @NotNull BackendCompiler compiler,
     TranslatingCompiler.OutputSink sink) {
   myTranslatingCompiler = translatingCompiler;
   myChunk = chunk;
   myProject = project;
   myCompiler = compiler;
   myCompileContext = compileContext;
   myFilesToCompile = filesToCompile;
   mySink = sink;
   myProjectFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
   CompileStatistics stat = compileContext.getUserData(CompileStatistics.KEY);
   if (stat == null) {
     stat = new CompileStatistics();
     compileContext.putUserData(CompileStatistics.KEY, stat);
   }
   myStatistics = stat;
 }
 public LibraryRuntimeClasspathScope(Project project, LibraryOrderEntry entry) {
   super(project);
   myIndex = ProjectRootManager.getInstance(project).getFileIndex();
   Collections.addAll(myEntries, entry.getFiles(BinariesOrderRootType.getInstance()));
 }
  // currently only one level here..
  public boolean contains(@NotNull String name, @NotNull final VirtualFile vFile) {
    final ProjectFileIndex projectFileIndex =
        ProjectRootManager.getInstance(myProject).getFileIndex();
    final Set<Boolean> find = new HashSet<Boolean>();
    final ContentIterator contentIterator =
        new ContentIterator() {
          public boolean processFile(VirtualFile fileOrDir) {
            if (fileOrDir != null && fileOrDir.getPath().equals(vFile.getPath())) {
              find.add(Boolean.TRUE);
            }
            return true;
          }
        };

    Collection<TreeItem<Pair<AbstractUrl, String>>> urls = getFavoritesListRootUrls(name);
    for (TreeItem<Pair<AbstractUrl, String>> pair : urls) {
      AbstractUrl abstractUrl = pair.getData().getFirst();
      if (abstractUrl == null) {
        continue;
      }
      final Object[] path = abstractUrl.createPath(myProject);
      if (path == null || path.length < 1 || path[0] == null) {
        continue;
      }
      Object element = path[path.length - 1];
      if (element instanceof SmartPsiElementPointer) {
        final VirtualFile virtualFile =
            PsiUtilBase.getVirtualFile(((SmartPsiElementPointer) element).getElement());
        if (virtualFile == null) continue;
        if (vFile.getPath().equals(virtualFile.getPath())) {
          return true;
        }
        if (!virtualFile.isDirectory()) {
          continue;
        }
        projectFileIndex.iterateContentUnderDirectory(virtualFile, contentIterator);
      }

      if (element instanceof PsiElement) {
        final VirtualFile virtualFile = PsiUtilBase.getVirtualFile((PsiElement) element);
        if (virtualFile == null) continue;
        if (vFile.getPath().equals(virtualFile.getPath())) {
          return true;
        }
        if (!virtualFile.isDirectory()) {
          continue;
        }
        projectFileIndex.iterateContentUnderDirectory(virtualFile, contentIterator);
      }
      if (element instanceof Module) {
        ModuleRootManager.getInstance((Module) element)
            .getFileIndex()
            .iterateContent(contentIterator);
      }
      if (element instanceof LibraryGroupElement) {
        final boolean inLibrary =
            ModuleRootManager.getInstance(((LibraryGroupElement) element).getModule())
                    .getFileIndex()
                    .isInContent(vFile)
                && projectFileIndex.isInLibraryClasses(vFile);
        if (inLibrary) {
          return true;
        }
      }
      if (element instanceof NamedLibraryElement) {
        NamedLibraryElement namedLibraryElement = (NamedLibraryElement) element;
        final VirtualFile[] files =
            namedLibraryElement.getOrderEntry().getRootFiles(OrderRootType.CLASSES);
        if (files != null && ArrayUtil.find(files, vFile) > -1) {
          return true;
        }
      }
      if (element instanceof ModuleGroup) {
        ModuleGroup group = (ModuleGroup) element;
        final Collection<Module> modules = group.modulesInGroup(myProject, true);
        for (Module module : modules) {
          ModuleRootManager.getInstance(module).getFileIndex().iterateContent(contentIterator);
        }
      }

      for (FavoriteNodeProvider provider :
          Extensions.getExtensions(FavoriteNodeProvider.EP_NAME, myProject)) {
        if (provider.elementContainsFile(element, vFile)) {
          return true;
        }
      }

      if (!find.isEmpty()) {
        return true;
      }
    }
    return false;
  }