@NotNull public static Library updatePackagesLibraryRoots( @NotNull final Project project, @NotNull final DartLibInfo libInfo) { final LibraryTable projectLibraryTable = ProjectLibraryTable.getInstance(project); final Library existingLibrary = projectLibraryTable.getLibraryByName(DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME); final Library library = existingLibrary != null ? existingLibrary : ApplicationManager.getApplication() .runWriteAction( new Computable<Library>() { @Override public Library compute() { final LibraryTableBase.ModifiableModel libTableModel = ProjectLibraryTable.getInstance(project).getModifiableModel(); final Library lib = libTableModel.createLibrary( DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME, DartPackagesLibraryType.LIBRARY_KIND); libTableModel.commit(); return lib; } }); final String[] existingUrls = library.getUrls(OrderRootType.CLASSES); final Collection<String> libRootUrls = libInfo.getLibRootUrls(); if ((!libInfo.isProjectWithoutPubspec() && isBrokenPackageMap(((LibraryEx) library).getProperties())) || existingUrls.length != libRootUrls.size() || !libRootUrls.containsAll(Arrays.asList(existingUrls))) { ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { final LibraryEx.ModifiableModelEx model = (LibraryEx.ModifiableModelEx) library.getModifiableModel(); for (String url : existingUrls) { model.removeRoot(url, OrderRootType.CLASSES); } for (String url : libRootUrls) { model.addRoot(url, OrderRootType.CLASSES); } final DartPackagesLibraryProperties libraryProperties = new DartPackagesLibraryProperties(); libraryProperties.setPackageNameToDirsMap(libInfo.getPackagesMap()); model.setProperties(libraryProperties); model.commit(); } }); } return library; }
@NotNull private static String toVfString(@NotNull Collection<VirtualFile> list) { List<VirtualFile> sub = new ArrayList<VirtualFile>(list).subList(0, Math.min(list.size(), 100)); return list.size() + " files: " + StringUtil.join(sub, file -> file.getName(), ", ") + (list.size() == sub.size() ? "" : "..."); }
@Nullable private IProperty getSelectedProperty() { final Collection<DefaultMutableTreeNode> selectedNode = getSelectedNodes(); if (selectedNode.isEmpty()) { return null; } final ResourceBundleEditorViewElement element = getSelectedElement(ContainerUtil.getFirstItem(selectedNode)); return element instanceof ResourceBundlePropertyStructureViewElement ? ((ResourceBundlePropertyStructureViewElement) element).getProperty() : null; }
public Collection<RequestFuture> cancelAutoMakeTasks(Project project) { final Collection<RequestFuture> futures = new ArrayList<RequestFuture>(); synchronized (myAutomakeFutures) { for (Map.Entry<RequestFuture, Project> entry : myAutomakeFutures.entrySet()) { if (entry.getValue().equals(project)) { final RequestFuture future = entry.getKey(); future.cancel(false); futures.add(future); } } } return futures; }
public static boolean areTooManyDocumentsInTheQueue(Collection<Document> documents) { if (documents.size() > 100) return true; int totalSize = 0; for (Document document : documents) { totalSize += document.getTextLength(); if (totalSize > 10 * FileUtilRt.MEGABYTE) return true; } return false; }
private void addAllPointers(@NotNull Collection<VirtualFilePointerImpl> pointers) { List<FilePointerPartNode> out = new ArrayList<FilePointerPartNode>(); for (FilePointerPartNode root : myPointers.values()) { root.getPointersUnder(null, false, "", out); } for (FilePointerPartNode node : out) { pointers.add(node.leaf); } }
@Override public boolean queue(@NotNull Collection<VirtualFile> files, @NotNull Object reason) { if (files.isEmpty()) { return false; } boolean queued = false; List<VirtualFile> added = new ArrayList<VirtualFile>(files.size()); for (VirtualFile file : files) { boolean wasAdded = queueIfNeeded(file, myProject); if (wasAdded) { added.add(file); } queued |= wasAdded; } if (queued) { log("Queued to resolve (from " + reason + "): " + toVfString(added)); flushLog(); } return queued; }
@NotNull public static Collection<VcsDirectoryMapping> findRoots( @NotNull VirtualFile rootDir, @NotNull Project project) throws IllegalArgumentException { if (!rootDir.isDirectory()) { throw new IllegalArgumentException( "Can't find VCS at the target file system path. Reason: expected to find a directory there but it's not. The path: " + rootDir.getParent()); } Collection<VcsRoot> roots = ServiceManager.getService(project, VcsRootDetector.class).detect(rootDir); Collection<VcsDirectoryMapping> result = ContainerUtilRt.newArrayList(); for (VcsRoot vcsRoot : roots) { VirtualFile vFile = vcsRoot.getPath(); AbstractVcs rootVcs = vcsRoot.getVcs(); if (rootVcs != null && vFile != null) { result.add(new VcsDirectoryMapping(vFile.getPath(), rootVcs.getName())); } } return result; }
private static void addSourceDirectoriesFromLibraries( @NotNull Project project, @NotNull VirtualFile directory, @NotNull Collection<VirtualFile> outSourceRoots) { ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(project); VirtualFile classRoot = index.getClassRootForFile(directory); if (classRoot == null) return; String relativePath = VfsUtilCore.getRelativePath(directory, classRoot); if (relativePath == null) return; for (OrderEntry orderEntry : index.getOrderEntriesForFile(directory)) { for (VirtualFile sourceRoot : orderEntry.getFiles(OrderRootType.SOURCES)) { VirtualFile sourceFile = sourceRoot.findFileByRelativePath(relativePath); if (sourceFile != null) { outSourceRoots.add(sourceFile); } } } }
public static void addAvailableScripts( final Collection<String> result, @Nullable final VirtualFile root) { if (root == null || !root.isDirectory()) { return; } final VirtualFile scripts = root.findChild("scripts"); if (scripts == null || !scripts.isDirectory()) { return; } for (VirtualFile child : scripts.getChildren()) { if (isScriptFile(child)) { result.add(GroovyNamesUtil.camelToSnake(child.getNameWithoutExtension())); } } }
private static void collectChildrenRecursively( @NotNull VirtualFile root, @NotNull VirtualFile anchor, @NotNull Collection<VirtualFile> result) { if (root == anchor) { return; } VirtualFile parent = anchor.getParent(); if (parent == null) { return; } for (VirtualFile child : parent.getChildren()) { if (child != anchor) { result.add(child); } } if (parent != root) { collectChildrenRecursively(root, parent, result); } }
public static VirtualFile createTestProjectStructure( String tempName, Module module, String rootPath, Collection<File> filesToDelete, boolean addProjectRoots) throws IOException { File dir = FileUtil.createTempDirectory(tempName, null, false); filesToDelete.add(dir); VirtualFile vDir = LocalFileSystem.getInstance() .refreshAndFindFileByPath(dir.getCanonicalPath().replace(File.separatorChar, '/')); assert vDir != null && vDir.isDirectory() : dir; PlatformTestCase.synchronizeTempDirVfs(vDir); EdtTestUtil.runInEdtAndWait( () -> { AccessToken token = WriteAction.start(); try { if (rootPath != null) { VirtualFile vDir1 = LocalFileSystem.getInstance() .findFileByPath(rootPath.replace(File.separatorChar, '/')); if (vDir1 == null) { throw new Exception(rootPath + " not found"); } VfsUtil.copyDirectory(null, vDir1, vDir, null); } if (addProjectRoots) { addSourceContentToRoots(module, vDir); } } finally { token.finish(); } }); return vDir; }
public static void addAvailableSystemScripts( final Collection<String> result, @NotNull Module module) { VirtualFile scriptRoot = null; GlobalSearchScope searchScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module, false); for (PsiClass aClass : JavaPsiFacade.getInstance(module.getProject()).findClasses("CreateApp_", searchScope)) { PsiClass superClass = aClass.getSuperClass(); if (superClass != null && GroovyCommonClassNames.GROOVY_LANG_SCRIPT.equals(superClass.getQualifiedName())) { PsiFile psiFile = aClass.getContainingFile(); if (psiFile != null) { VirtualFile file = psiFile.getVirtualFile(); if (file != null && file.getFileSystem() instanceof ArchiveFileSystem) { VirtualFile parent = file.getParent(); if (parent != null && parent.findChild("Console.class") != null) { scriptRoot = parent; break; } } } } } if (scriptRoot == null) return; Pattern scriptPattern = Pattern.compile("([A-Za-z0-9]+)_?\\.class"); for (VirtualFile file : scriptRoot.getChildren()) { Matcher matcher = scriptPattern.matcher(file.getName()); if (matcher.matches()) { result.add(GroovyNamesUtil.camelToSnake(matcher.group(1))); } } }
@Nullable public ResourceBundleEditorViewElement getSelectedElementIfOnlyOne() { final Collection<ResourceBundleEditorViewElement> selectedElements = getSelectedElements(); return selectedElements.size() == 1 ? ContainerUtil.getFirstItem(selectedElements) : null; }