com.intellij.openapi.roots.OrderRootType Java Examples

The following examples show how to use com.intellij.openapi.roots.OrderRootType. 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: RootsAsVirtualFilePointers.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * <roots>
 * <sourcePath>
 * <root type="composite">
 * <root type="simple" url="jar://I:/Java/jdk1.8/src.zip!/" />
 * <root type="simple" url="jar://I:/Java/jdk1.8/javafx-src.zip!/" />
 * </root>
 * </sourcePath>
 * </roots>
 */
private void read(@Nonnull Element roots, @Nonnull OrderRootType type) {
  String sdkRootName = type.getName();
  Element child = sdkRootName == null ? null : roots.getChild(sdkRootName);
  if (child == null) {
    return;
  }

  List<Element> composites = child.getChildren();
  if (composites.size() != 1) {
    LOG.error(composites);
  }
  Element composite = composites.get(0);
  if (!composite.getChildren("root").isEmpty()) {
    VirtualFilePointerContainer container = getOrCreateContainer(type);
    container.readExternal(composite, "root", false);
  }
}
 
Example #2
Source File: LibraryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<String> getInvalidRootUrls(@Nonnull OrderRootType type) {
  if (myDisposed) return Collections.emptyList();

  VirtualFilePointerContainer container = myRoots.get(type);
  final List<VirtualFilePointer> pointers = container == null ? Collections.emptyList() : container.getList();
  List<String> invalidPaths = null;
  for (VirtualFilePointer pointer : pointers) {
    if (!pointer.isValid()) {
      if (invalidPaths == null) {
        invalidPaths = new SmartList<>();
      }
      invalidPaths.add(pointer.getUrl());
    }
  }
  return ContainerUtil.notNullize(invalidPaths);
}
 
Example #3
Source File: ProjectRootManagerComponent.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void addRootsFromModules(boolean includeSourceRoots, Set<String> recursive, Set<String> flat) {
  final Module[] modules = ModuleManager.getInstance(myProject).getModules();
  for (Module module : modules) {
    final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);

    addRootsToTrack(moduleRootManager.getContentRootUrls(), recursive, flat);

    if (includeSourceRoots) {
      addRootsToTrack(moduleRootManager.getContentFolderUrls(ContentFolderScopes.all(false)), recursive, flat);
    }

    final OrderEntry[] orderEntries = moduleRootManager.getOrderEntries();
    for (OrderEntry entry : orderEntries) {
      if (entry instanceof OrderEntryWithTracking) {
        for (OrderRootType orderRootType : OrderRootType.getAllTypes()) {
          addRootsToTrack(entry.getUrls(orderRootType), recursive, flat);
        }
      }
    }
  }
}
 
Example #4
Source File: BlazeSourceJarNavigationPolicy.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
private VirtualFile getSourceJarRoot(
    Project project, BlazeProjectData blazeProjectData, PsiJavaFile clsFile) {

  Library library = findLibrary(project, clsFile);
  if (library == null || library.getFiles(OrderRootType.SOURCES).length != 0) {
    // If the library already has sources attached, no need to hunt for them.
    return null;
  }

  BlazeJarLibrary blazeLibrary =
      LibraryActionHelper.findLibraryFromIntellijLibrary(project, blazeProjectData, library);
  if (blazeLibrary == null) {
    return null;
  }

  // TODO: If there are multiple source jars, search for one containing this PsiJavaFile.
  for (ArtifactLocation jar : blazeLibrary.libraryArtifact.getSourceJars()) {
    VirtualFile root =
        getSourceJarRoot(project, blazeProjectData.getArtifactLocationDecoder(), jar);
    if (root != null) {
      return root;
    }
  }
  return null;
}
 
Example #5
Source File: AarLibrary.java    From intellij with Apache License 2.0 6 votes vote down vote up
/**
 * Create an IntelliJ library that matches Android Studio's expectation for an AAR. See {@link
 * org.jetbrains.android.facet.ResourceFolderManager#addAarsFromModuleLibraries}.
 */
@Override
public void modifyLibraryModel(
    Project project,
    ArtifactLocationDecoder artifactLocationDecoder,
    ModifiableModel libraryModel) {
  UnpackedAars unpackedAars = UnpackedAars.getInstance(project);
  File resourceDirectory = unpackedAars.getResourceDirectory(artifactLocationDecoder, this);
  if (resourceDirectory == null) {
    logger.warn("Failed to update AAR library model for: " + aarArtifact);
    return;
  }
  if (libraryArtifact != null) {
    File jar = unpackedAars.getClassJar(artifactLocationDecoder, this);
    libraryModel.addRoot(pathToUrl(jar), OrderRootType.CLASSES);
  }
  libraryModel.addRoot(pathToUrl(resourceDirectory), OrderRootType.CLASSES);
}
 
Example #6
Source File: RoboVmSdkType.java    From robovm-idea with GNU General Public License v2.0 6 votes vote down vote up
public void setupSdkRoots(Sdk sdk, Sdk jdk) {
    SdkModificator sdkModificator = sdk.getSdkModificator();
    sdkModificator.removeAllRoots();

    // add all class and source jars from the SDK lib/ folder
    for(File file: RoboVmPlugin.getSdkLibraries()) {
        VirtualFile virtualFile = JarFileSystem.getInstance().findLocalVirtualFileByPath(file.getAbsolutePath());
        sdkModificator.addRoot(virtualFile, file.getName().endsWith("-sources.jar")?  OrderRootType.SOURCES: OrderRootType.CLASSES);
    }

    // set the JDK version as the version string, otherwise
    // IDEA gets angry
    sdkModificator.setVersionString(jdk.getVersionString());

    // set the home path, we check this in createSdkIfNotExists
    sdkModificator.setHomePath(RoboVmPlugin.getSdkHome().getAbsolutePath());

    // commit changes and let IDEA handle the rest
    sdkModificator.commitChanges();
}
 
Example #7
Source File: DirectoryPathMatcher.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static List<Pair<VirtualFile, String>> getProjectRoots(GotoFileModel model) {
  Set<VirtualFile> roots = new HashSet<>();
  for (Module module : ModuleManager.getInstance(model.getProject()).getModules()) {
    Collections.addAll(roots, ModuleRootManager.getInstance(module).getContentRoots());
    for (OrderEntry entry : ModuleRootManager.getInstance(module).getOrderEntries()) {
      if (entry instanceof OrderEntryWithTracking) {
        Collections.addAll(roots, entry.getFiles(OrderRootType.CLASSES));
        Collections.addAll(roots, entry.getFiles(OrderRootType.SOURCES));
      }
    }
  }
  return roots.stream().map(root -> {
    VirtualFile top = model.getTopLevelRoot(root);
    return top != null ? top : root;
  }).distinct().map(r -> Pair.create(r, StringUtil.notNullize(model.getFullName(r)))).collect(Collectors.toList());
}
 
Example #8
Source File: LibraryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void readRoots(@Nonnull Element element) throws InvalidDataException {
  for (OrderRootType rootType : getAllRootTypes()) {
    final Element rootChild = element.getChild(rootType.name());
    if (rootChild == null) {
      continue;
    }
    if (!rootChild.getChildren(ROOT_PATH_ELEMENT).isEmpty()) {
      VirtualFilePointerContainer roots = getOrCreateContainer(rootType);
      roots.readExternal(rootChild, ROOT_PATH_ELEMENT, false);
    }
  }
  Element excludedRoot = element.getChild(EXCLUDED_ROOTS_TAG);
  if (excludedRoot != null && !excludedRoot.getChildren(ROOT_PATH_ELEMENT).isEmpty()) {
    getOrCreateExcludedRoots().readExternal(excludedRoot, ROOT_PATH_ELEMENT, false);
  }
}
 
Example #9
Source File: LibraryDataService.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void importLibrary(@Nonnull final String libraryName, @Nonnull final Map<OrderRootType, Collection<File>> libraryFiles, @Nonnull final Project project, boolean synchronous) {
  ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(project) {
    @RequiredUIAccess
    @Override
    public void execute() {
      // Is assumed to be called from the EDT.
      final LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
      final LibraryTable.ModifiableModel projectLibraryModel = libraryTable.getModifiableModel();
      final Library intellijLibrary;
      try {
        intellijLibrary = projectLibraryModel.createLibrary(libraryName);
      }
      finally {
        projectLibraryModel.commit();
      }
      final Library.ModifiableModel libraryModel = intellijLibrary.getModifiableModel();
      try {
        registerPaths(libraryFiles, libraryModel, libraryName);
      }
      finally {
        libraryModel.commit();
      }
    }
  });
}
 
Example #10
Source File: LibraryRootsComponent.java    From consulo with Apache License 2.0 6 votes vote down vote up
private Set<VirtualFile> getNotExcludedRoots() {
  Set<VirtualFile> roots = new LinkedHashSet<VirtualFile>();
  String[] excludedRootUrls = getLibraryEditor().getExcludedRootUrls();
  Set<VirtualFile> excludedRoots = new HashSet<VirtualFile>();
  for (String url : excludedRootUrls) {
    ContainerUtil.addIfNotNull(excludedRoots, VirtualFileManager.getInstance().findFileByUrl(url));
  }
  for (OrderRootType type : OrderRootType.getAllTypes()) {
    VirtualFile[] files = getLibraryEditor().getFiles(type);
    for (VirtualFile file : files) {
      if (!VfsUtilCore.isUnder(file, excludedRoots)) {
        roots.add(PathUtil.getLocalFile(file));
      }
    }
  }
  return roots;
}
 
Example #11
Source File: RootsAsVirtualFilePointers.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private VirtualFilePointerContainer getOrCreateContainer(@Nonnull OrderRootType rootType) {
  VirtualFilePointerContainer roots = myRoots.get(rootType);
  if (roots == null) {
    roots = VirtualFilePointerManager.getInstance().createContainer(myParent, myListener);
    myRoots.put(rootType, roots);
  }
  return roots;
}
 
Example #12
Source File: BaseSdkEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public VirtualFile[] getRoots(OrderRootType rootType) {
  final PathEditor editor = myPathEditors.get(rootType);
  if (editor == null) {
    throw new IllegalStateException("no editor for root type " + rootType);
  }
  return editor.getRoots();
}
 
Example #13
Source File: LibraryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isJarDirectory(@Nonnull final String url, @Nonnull final OrderRootType rootType) {
  VirtualFilePointerContainer container = myRoots.get(rootType);
  if (container == null) return false;
  List<Pair<String, Boolean>> jarDirectories = container.getJarDirectories();
  return jarDirectories.contains(Pair.create(url, false)) || jarDirectories.contains(Pair.create(url, true));
}
 
Example #14
Source File: QuarkusModuleUtil.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
private static Integer computeHash(Module module) {
    ModuleRootManager manager = ModuleRootManager.getInstance(module);
    Set<String> files = manager.processOrder(new RootPolicy<Set<String>>() {
        @Override
        public Set<String> visitLibraryOrderEntry(@NotNull LibraryOrderEntry libraryOrderEntry, Set<String> value) {
            if (!libraryOrderEntry.getLibraryName().equalsIgnoreCase(QuarkusConstants.QUARKUS_DEPLOYMENT_LIBRARY_NAME) && isQuarkusExtensionWithDeploymentArtifact(libraryOrderEntry.getLibrary())) {
                for(VirtualFile file : libraryOrderEntry.getFiles(OrderRootType.CLASSES)) {
                    value.add(file.getPath());
                }
            }
            return value;
        }
    }, new HashSet<>());
    return files.isEmpty()?null:files.hashCode();
}
 
Example #15
Source File: NewLibraryEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isValid(@Nonnull String url, @Nonnull OrderRootType orderRootType) {
  final Collection<LightFilePointer> pointers = myRoots.get(orderRootType);
  for (LightFilePointer pointer : pointers) {
    if (pointer.getUrl().equals(url)) {
      return pointer.isValid();
    }
  }
  return false;
}
 
Example #16
Source File: ExistingLibraryEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public VirtualFile[] getFiles(OrderRootType rootType) {
  if (myModel != null) {
    return myModel.getFiles(rootType);
  }
  return myLibrary.getFiles(rootType);
}
 
Example #17
Source File: StructureConfigurableContext.java    From consulo with Apache License 2.0 5 votes vote down vote up
public VirtualFile[] getLibraryFiles(Library library, final OrderRootType type) {
  final LibraryTable table = library.getTable();
  if (table != null) {
    final LibraryTable.ModifiableModel modifiableModel = getModifiableLibraryTable(table);
    if (modifiableModel instanceof LibrariesModifiableModel) {
      final LibrariesModifiableModel librariesModel = (LibrariesModifiableModel)modifiableModel;
      if (librariesModel.hasLibraryEditor(library)) {
        return librariesModel.getLibraryEditor(library).getFiles(type);
      }
    }
  }
  return library.getFiles(type);
}
 
Example #18
Source File: LibraryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void addRoot(@Nonnull String url, @Nonnull OrderRootType rootType) {
  checkDisposed();
  LOG.assertTrue(isWritable());

  final VirtualFilePointerContainer container = getOrCreateContainer(rootType);
  container.add(url);
}
 
Example #19
Source File: NamedLibraryElementNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean containsFileInOrderType(final OrderEntry orderEntry, final OrderRootType orderType, final VirtualFile file) {
  if (!orderEntry.isValid()) return false;
  VirtualFile[] files = orderEntry.getFiles(orderType);
  for (VirtualFile virtualFile : files) {
    boolean ancestor = VfsUtilCore.isAncestor(virtualFile, file, false);
    if (ancestor) return true;
  }
  return false;
}
 
Example #20
Source File: LibraryProjectStructureElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void reportInvalidRoots(ProjectStructureProblemsHolder problemsHolder, LibraryEx library,
                                final OrderRootType type, String rootName, final ProjectStructureProblemType problemType) {
  final List<String> invalidUrls = library.getInvalidRootUrls(type);
  if (!invalidUrls.isEmpty()) {
    final String description = createInvalidRootsDescription(invalidUrls, rootName, library.getName());
    final PlaceInProjectStructure place = createPlace();
    final String message = ProjectBundle.message("project.roots.error.message.invalid.roots", rootName, invalidUrls.size());
    ProjectStructureProblemDescription.ProblemLevel level = library.getTable().getTableLevel().equals(LibraryTablesRegistrar.PROJECT_LEVEL)
                                                            ? ProjectStructureProblemDescription.ProblemLevel.PROJECT : ProjectStructureProblemDescription.ProblemLevel.GLOBAL;
    problemsHolder.registerProblem(new ProjectStructureProblemDescription(message, description, place,
                                                                          problemType, level,
                                                                          Collections.singletonList(new RemoveInvalidRootsQuickFix(library, type, invalidUrls)),
                                                                          true));
  }
}
 
Example #21
Source File: LibraryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void copyRootsFrom(@Nonnull LibraryImpl fromModel) {
  Map<OrderRootType, VirtualFilePointerContainer> clonedRoots = new HashMap<>();
  for (Map.Entry<OrderRootType, VirtualFilePointerContainer> entry : fromModel.myRoots.entrySet()) {
    OrderRootType rootType = entry.getKey();
    VirtualFilePointerContainer container = entry.getValue();
    VirtualFilePointerContainer clone = container.clone(myPointersDisposable, getListener());
    clonedRoots.put(rootType, clone);
  }
  myRoots.clear();
  myRoots.putAll(clonedRoots);

  VirtualFilePointerContainer excludedRoots = fromModel.myExcludedRoots;
  myExcludedRoots = excludedRoots != null ? excludedRoots.clone(myPointersDisposable) : null;
}
 
Example #22
Source File: LibraryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void addRoot(@Nonnull VirtualFile file, @Nonnull OrderRootType rootType) {
  checkDisposed();
  LOG.assertTrue(isWritable());

  final VirtualFilePointerContainer container = getOrCreateContainer(rootType);
  container.add(file);
}
 
Example #23
Source File: LibraryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void addJarDirectory(@Nonnull final String url, final boolean recursive, @Nonnull OrderRootType rootType) {
  checkDisposed();
  LOG.assertTrue(isWritable());

  final VirtualFilePointerContainer container = getOrCreateContainer(rootType);
  container.addJarDirectory(url, recursive);
}
 
Example #24
Source File: BlazePyOutsideModuleImportResolver.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public PsiElement resolveImportReference(
    QualifiedName name, PyQualifiedNameResolveContext context, boolean withRoots) {
  Project project = context.getProject();
  if (!Blaze.isBlazeProject(project)) {
    return null;
  }
  if (context.getModule() != null) {
    // the file is associated with a module, so this import resolver is not necessary.
    return null;
  }
  if (context.getFoothold() == null) {
    // we're not resolving in the context of a specific py file, so this hack is unnecessary.
    return null;
  }
  Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk();
  if (projectSdk != null && projectSdk.getSdkType() instanceof PythonSdkType) {
    // if this is a python workspace type, imports in external files are already resolved by the
    // python plugin.
    return null;
  }
  Sdk pythonSdk = PySdkUtils.getPythonSdk(context.getProject());
  if (pythonSdk == null) {
    return null;
  }
  for (VirtualFile root : pythonSdk.getRootProvider().getFiles(OrderRootType.CLASSES)) {
    if (!root.isValid() || !root.isDirectory()) {
      continue;
    }
    PsiElement element =
        resolveModuleAt(context.getPsiManager().findDirectory(root), name, context);
    if (element != null) {
      return element;
    }
  }
  return null;
}
 
Example #25
Source File: SdkModificator.java    From consulo with Apache License 2.0 5 votes vote down vote up
default void removeRoot(@Nonnull String url, @Nonnull OrderRootType rootType) {
  for (VirtualFile file : getRoots(rootType)) {
    if (file.getUrl().equals(url)) {
      removeRoot(file, rootType);
      break;
    }
  }
}
 
Example #26
Source File: Unity3dChildModuleExtension.java    From consulo-unity3d with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
@RequiredReadAction
public String[] getSystemLibraryUrls(@Nonnull String name, @Nonnull OrderRootType orderRootType)
{
	Unity3dRootModuleExtension rootModuleExtension = Unity3dModuleExtensionUtil.getRootModuleExtension(getProject());
	if(rootModuleExtension != null)
	{
		return rootModuleExtension.getSystemLibraryUrls(name, orderRootType);
	}
	return ArrayUtil.EMPTY_STRING_ARRAY;
}
 
Example #27
Source File: ExistingLibraryEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isValid(final String url, final OrderRootType orderRootType) {
  if (myModel != null) {
    return myModel.isValid(url, orderRootType);
  }
  return myLibrary.isValid(url, orderRootType);
}
 
Example #28
Source File: HaxeLibrary.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
/**
 * Test whether this library is effectively the same as a Library appearing
 * in IDEA's library tables.
 *
 * @param lib - Library to test.
 * @return true if this library uses the same sources as the IDEA library; false otherwise.
 */
public boolean matchesIdeaLib(Library lib) {
  if (null == lib) {
    return false;
  }

  HaxeClasspath cp = getClasspathEntries();
  VirtualFile[] sources = lib.getFiles(OrderRootType.SOURCES);
  for (VirtualFile file : sources) {
    if (!cp.containsUrl(file.getUrl())) {
      return false;
    }
  }
  return cp.size() == sources.length;
}
 
Example #29
Source File: OrderRootsCache.java    From consulo with Apache License 2.0 5 votes vote down vote up
public VirtualFilePointerContainer setCachedRoots(OrderRootType rootType, int flags, Collection<String> urls) {
  final VirtualFilePointerContainer container = VirtualFilePointerManager.getInstance().createContainer(myParentDisposable);
  for (String url : urls) {
    container.add(url);
  }
  myRoots.put(new CacheKey(rootType, flags), container);
  return container;
}
 
Example #30
Source File: HaxeSdkUtil.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
public static void setupSdkPaths(@Nullable VirtualFile sdkRoot, SdkModificator modificator) {
  if (sdkRoot == null) {
    return;
  }
  VirtualFile stdRoot;
  final String stdPath = System.getenv("HAXE_STD_PATH");
  if (stdPath != null) {
    stdRoot = VirtualFileManager.getInstance().findFileByUrl("file://" + stdPath);
  }
  else {
    stdRoot = sdkRoot.findChild("std");
  }
  if (stdRoot != null) {
    modificator.addRoot(stdRoot, OrderRootType.SOURCES);
    modificator.addRoot(stdRoot, OrderRootType.CLASSES);
  }
  VirtualFile docRoot;
  final String docPath = System.getenv("HAXE_DOC_PATH");
  if (docPath != null) {
    docRoot = VirtualFileManager.getInstance().findFileByUrl(docPath);
  }
  else {
    docRoot = sdkRoot.findChild("doc");
  }
  if (docRoot != null) {
    modificator.addRoot(docRoot, JavadocOrderRootType.getInstance());
  }
}