private String dubImportPath(String rootPath) { String pathUrl = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, rootPath); VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(pathUrl); if (file == null) { return null; } final List<String> sourcesDir = new ArrayList<>(); VfsUtilCore.visitChildrenRecursively( file, new VirtualFileVisitor() { @Override public boolean visitFile(@NotNull VirtualFile file) { if (file.isDirectory()) { if (file.getName().equals("source")) { sourcesDir.add("source"); } if (file.getName().equals("src")) { sourcesDir.add("src"); } } return true; } }); return sourcesDir.isEmpty() ? null : rootPath + File.separator + sourcesDir.get(0); }
@NotNull public static String buildStringToFindForIndicesFromRegExp( @NotNull String stringToFind, @NotNull Project project) { if (!Registry.is("idea.regexp.search.uses.indices")) return ""; final AccessToken accessToken = ReadAction.start(); try { final List<PsiElement> topLevelRegExpChars = getTopLevelRegExpChars("a", project); if (topLevelRegExpChars.size() != 1) return ""; // leave only top level regExpChars return StringUtil.join( getTopLevelRegExpChars(stringToFind, project), new Function<PsiElement, String>() { final Class regExpCharPsiClass = topLevelRegExpChars.get(0).getClass(); @Override public String fun(PsiElement element) { return regExpCharPsiClass.isInstance(element) ? element.getText() : " "; } }, ""); } finally { accessToken.finish(); } }
private void applyChildrenChangeEvents(VirtualFile parent, List<VFileEvent> events) { final NewVirtualFileSystem delegate = getDelegate(parent); TIntArrayList childrenIdsUpdated = new TIntArrayList(); List<VirtualFile> childrenToBeUpdated = new SmartList<VirtualFile>(); assert parent != null && parent != mySuperRoot; final int parentId = getFileId(parent); assert parentId != 0; TIntHashSet parentChildrenIds = new TIntHashSet(FSRecords.list(parentId)); boolean hasRemovedChildren = false; for (VFileEvent event : events) { if (event instanceof VFileCreateEvent) { String name = ((VFileCreateEvent) event).getChildName(); final VirtualFile fake = new FakeVirtualFile(parent, name); final FileAttributes attributes = delegate.getAttributes(fake); if (attributes != null) { final int childId = createAndFillRecord(delegate, fake, parentId, attributes); assert parent instanceof VirtualDirectoryImpl : parent; final VirtualDirectoryImpl dir = (VirtualDirectoryImpl) parent; VirtualFileSystemEntry child = dir.createChild(name, childId, dir.getFileSystem()); childrenToBeUpdated.add(child); childrenIdsUpdated.add(childId); parentChildrenIds.add(childId); } } else if (event instanceof VFileDeleteEvent) { VirtualFile file = ((VFileDeleteEvent) event).getFile(); if (!file.exists()) { LOG.error("Deleting a file, which does not exist: " + file.getPath()); continue; } hasRemovedChildren = true; int id = getFileId(file); childrenToBeUpdated.add(file); childrenIdsUpdated.add(-id); parentChildrenIds.remove(id); } } FSRecords.updateList(parentId, parentChildrenIds.toArray()); if (hasRemovedChildren) clearIdCache(); VirtualDirectoryImpl parentImpl = (VirtualDirectoryImpl) parent; for (int i = 0, len = childrenIdsUpdated.size(); i < len; ++i) { final int childId = childrenIdsUpdated.get(i); final VirtualFile childFile = childrenToBeUpdated.get(i); if (childId > 0) { parentImpl.addChild((VirtualFileSystemEntry) childFile); } else { FSRecords.deleteRecordRecursively(-childId); parentImpl.removeChild(childFile); invalidateSubtree(childFile); } } }
@NotNull public static List<VcsDirectoryMapping> addMapping( @NotNull List<VcsDirectoryMapping> existingMappings, @NotNull String path, @NotNull String vcs) { List<VcsDirectoryMapping> mappings = new ArrayList<VcsDirectoryMapping>(existingMappings); for (Iterator<VcsDirectoryMapping> iterator = mappings.iterator(); iterator.hasNext(); ) { VcsDirectoryMapping mapping = iterator.next(); if (mapping.isDefaultMapping() && StringUtil.isEmptyOrSpaces(mapping.getVcs())) { LOG.debug("Removing <Project> -> <None> mapping"); iterator.remove(); } else if (FileUtil.pathsEqual(mapping.getDirectory(), path)) { if (!StringUtil.isEmptyOrSpaces(mapping.getVcs())) { LOG.warn( "Substituting existing mapping [" + path + "] -> [" + mapping.getVcs() + "] with [" + vcs + "]"); } else { LOG.debug("Removing [" + path + "] -> <None> mapping"); } iterator.remove(); } } mappings.add(new VcsDirectoryMapping(path, vcs)); return mappings; }
@Override @NotNull public Project[] getOpenProjects() { synchronized (myOpenProjects) { if (myOpenProjectsArrayCache.length != myOpenProjects.size()) { LOG.error( "Open projects: " + myOpenProjects + "; cache: " + Arrays.asList(myOpenProjectsArrayCache)); } if (myOpenProjectsArrayCache.length > 0 && myOpenProjectsArrayCache[0] != myOpenProjects.get(0)) { LOG.error( "Open projects cache corrupted. Open projects: " + myOpenProjects + "; cache: " + Arrays.asList(myOpenProjectsArrayCache)); } if (ApplicationManager.getApplication().isUnitTestMode()) { Project[] testProjects = myTestProjects.toArray(new Project[myTestProjects.size()]); for (Project testProject : testProjects) { assert !testProject.isDisposed() : testProject; } return ArrayUtil.mergeArrays(myOpenProjectsArrayCache, testProjects); } return myOpenProjectsArrayCache; } }
private static void doCollectResourceDirs( AndroidFacet facet, boolean collectResCacheDirs, List<String> result, CompileContext context) { final Module module = facet.getModule(); if (collectResCacheDirs) { final AndroidPlatform platform = facet.getConfiguration().getAndroidPlatform(); final int platformToolsRevision = platform != null ? platform.getSdk().getPlatformToolsRevision() : -1; if (platformToolsRevision < 0 || platformToolsRevision > 7) { // png cache is supported since platform-tools-r8 final String resCacheDirOsPath = findResourcesCacheDirectory(module, false, context); if (resCacheDirOsPath != null) { result.add(resCacheDirOsPath); } else { LOG.info("PNG cache not found for module " + module.getName()); } } } final VirtualFile resourcesDir = AndroidAptCompiler.getResourceDirForApkCompiler(module, facet); if (resourcesDir != null) { result.add(resourcesDir.getPath()); } }
@Override public void removeProjectManagerListener( @NotNull Project project, @NotNull ProjectManagerListener listener) { List<ProjectManagerListener> listeners = project.getUserData(LISTENERS_IN_PROJECT_KEY); LOG.assertTrue(listeners != null); boolean removed = listeners.remove(listener); LOG.assertTrue(removed); }
@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() ? "" : "..."); }
private boolean processFilesConcurrently( @NotNull Set<VirtualFile> files, @NotNull final ProgressIndicator indicator, @NotNull final Processor<VirtualFile> processor) { final List<VirtualFile> fileList = new ArrayList<VirtualFile>(files); // fine but grabs all CPUs // return JobLauncher.getInstance().invokeConcurrentlyUnderProgress(fileList, indicator, false, // false, processor); int parallelism = CacheUpdateRunner.indexingThreadCount(); final Callable<Boolean> processFileFromSet = () -> { final boolean[] result = {true}; ProgressManager.getInstance() .executeProcessUnderProgress( () -> { while (true) { ProgressManager.checkCanceled(); VirtualFile file; synchronized (fileList) { file = fileList.isEmpty() ? null : fileList.remove(fileList.size() - 1); } if (file == null) { break; } if (!processor.process(file)) { result[0] = false; break; } } }, indicator); return result[0]; }; List<Future<Boolean>> futures = ContainerUtil.map( Collections.nCopies(parallelism, ""), s -> myApplication.executeOnPooledThread(processFileFromSet)); List<Boolean> results = ContainerUtil.map( futures, future -> { try { return future.get(); } catch (Exception e) { LOG.error(e); } return false; }); return !ContainerUtil.exists( results, result -> { return result != null && !result; // null means PCE }); }
@NotNull private static List<VirtualFile> toVf(@NotNull int[] ids) { List<VirtualFile> res = new ArrayList<VirtualFile>(); for (int id : ids) { VirtualFile file = PersistentFS.getInstance().findFileById(id); if (file != null) { res.add(file); } } return res; }
private static List<String> convertToLocalPaths(VirtualFile[] files) { final List<String> paths = new ArrayList<String>(); for (VirtualFile file : files) { if (file.isValid()) { paths.add( StringUtil.trimEnd( FileUtil.toSystemIndependentName(file.getPath()), JarFileSystem.JAR_SEPARATOR)); } } return paths; }
public static void addMessages( @NotNull Map<CompilerMessageCategory, List<String>> messages, @NotNull Map<CompilerMessageCategory, List<String>> toAdd) { for (Map.Entry<CompilerMessageCategory, List<String>> entry : toAdd.entrySet()) { List<String> list = messages.get(entry.getKey()); if (list == null) { list = new ArrayList<String>(); messages.put(entry.getKey(), list); } list.addAll(entry.getValue()); } }
@Override public void addProjectManagerListener( @NotNull Project project, @NotNull ProjectManagerListener listener) { List<ProjectManagerListener> listeners = project.getUserData(LISTENERS_IN_PROJECT_KEY); if (listeners == null) { listeners = ((UserDataHolderEx) project) .putUserDataIfAbsent( LISTENERS_IN_PROJECT_KEY, ContainerUtil.<ProjectManagerListener>createLockFreeCopyOnWriteList()); } listeners.add(listener); }
private void addPackage(@NotNull final String packageName, @NotNull final String packagePath) { myLibRootUrls.add(VfsUtilCore.pathToUrl(packagePath)); List<String> paths = myPackagesMap.get((packageName)); if (paths == null) { paths = new SmartList<String>(); myPackagesMap.put(packageName, paths); } if (!paths.contains(packagePath)) { paths.add(packagePath); } }
public void doPrepare(final List<Module> pluginModules, final Project project) { final List<String> errorMessages = new ArrayList<String>(); final List<String> successMessages = new ArrayList<String>(); final CompilerManager compilerManager = CompilerManager.getInstance(project); compilerManager.make( compilerManager.createModulesCompileScope( pluginModules.toArray(new Module[pluginModules.size()]), true), new CompileStatusNotification() { public void finished( final boolean aborted, final int errors, final int warnings, final CompileContext compileContext) { if (aborted || errors != 0) return; ApplicationManager.getApplication() .invokeLater( new Runnable() { public void run() { for (Module aModule : pluginModules) { if (!doPrepare(aModule, errorMessages, successMessages)) { return; } } if (!errorMessages.isEmpty()) { Messages.showErrorDialog( errorMessages.iterator().next(), DevKitBundle.message("error.occurred")); } else if (!successMessages.isEmpty()) { StringBuilder messageBuf = new StringBuilder(); for (String message : successMessages) { if (messageBuf.length() != 0) { messageBuf.append('\n'); } messageBuf.append(message); } Messages.showInfoMessage( messageBuf.toString(), pluginModules.size() == 1 ? DevKitBundle.message( "success.deployment.message", pluginModules.get(0).getName()) : DevKitBundle.message("success.deployment.message.all")); } } }, project.getDisposed()); } }); }
/** @deprecated to be removed in IDEA 16 */ @Nullable public static Pattern createFileMaskRegExp(@Nullable String filter) { if (filter == null) { return null; } String pattern; final List<String> strings = StringUtil.split(filter, ","); if (strings.size() == 1) { pattern = PatternUtil.convertToRegex(filter.trim()); } else { pattern = StringUtil.join(strings, s -> "(" + PatternUtil.convertToRegex(s.trim()) + ")", "|"); } return Pattern.compile(pattern, Pattern.CASE_INSENSITIVE); }
@NotNull private static VirtualFilePointer[] toPointers(@NotNull List<FilePointerPartNode> pointers) { if (pointers.isEmpty()) return VirtualFilePointer.EMPTY_ARRAY; List<VirtualFilePointer> list = ContainerUtil.mapNotNull( pointers, new Function<FilePointerPartNode, VirtualFilePointer>() { @Override public VirtualFilePointer fun(FilePointerPartNode pair) { return pair.leaf; } }); return list.toArray(new VirtualFilePointer[list.size()]); }
@RequiredWriteAction @Override public void processEvents(@NotNull List<VFileEvent> events) { ApplicationManager.getApplication().assertWriteAccessAllowed(); List<VFileEvent> validated = validateEvents(events); BulkFileListener publisher = myEventBus.syncPublisher(VirtualFileManager.VFS_CHANGES); publisher.before(validated); THashMap<VirtualFile, List<VFileEvent>> parentToChildrenEventsChanges = null; for (VFileEvent event : validated) { VirtualFile changedParent = null; if (event instanceof VFileCreateEvent) { changedParent = ((VFileCreateEvent) event).getParent(); ((VFileCreateEvent) event).resetCache(); } else if (event instanceof VFileDeleteEvent) { changedParent = ((VFileDeleteEvent) event).getFile().getParent(); } if (changedParent != null) { if (parentToChildrenEventsChanges == null) parentToChildrenEventsChanges = new THashMap<VirtualFile, List<VFileEvent>>(); List<VFileEvent> parentChildrenChanges = parentToChildrenEventsChanges.get(changedParent); if (parentChildrenChanges == null) { parentToChildrenEventsChanges.put( changedParent, parentChildrenChanges = new SmartList<VFileEvent>()); } parentChildrenChanges.add(event); } else { applyEvent(event); } } if (parentToChildrenEventsChanges != null) { parentToChildrenEventsChanges.forEachEntry( new TObjectObjectProcedure<VirtualFile, List<VFileEvent>>() { @Override public boolean execute(VirtualFile parent, List<VFileEvent> childrenEvents) { applyChildrenChangeEvents(parent, childrenEvents); return true; } }); parentToChildrenEventsChanges.clear(); } publisher.after(validated); }
private static void removeModuleOutput(Module module, List<VirtualFile> from) { CompilerPathsManager compilerPathsManager = CompilerPathsManager.getInstance(module.getProject()); for (ContentFolderType contentFolderType : ContentFolderType.ALL_SOURCE_ROOTS) { from.remove(compilerPathsManager.getCompilerOutput(module, contentFolderType)); } }
private static void retainOnlyJarsAndDirectories(List<VirtualFile> woSdk) { for (Iterator<VirtualFile> iterator = woSdk.iterator(); iterator.hasNext(); ) { VirtualFile file = iterator.next(); final VirtualFile local = ArchiveVfsUtil.getVirtualFileForJar(file); final boolean dir = file.isDirectory(); final String name = file.getName(); if (LOG.isDebugEnabled()) { LOG.debug( "Considering: " + file.getPath() + "; local=" + local + "; dir=" + dir + "; name=" + name); } if (dir || local != null) { continue; } if (name.endsWith(".jar")) { continue; } LOG.debug("Removing"); iterator.remove(); } }
private static void fillSdks(List<GlobalLibrary> globals) { for (Sdk sdk : ProjectJdkTable.getInstance().getAllJdks()) { final String name = sdk.getName(); final String homePath = sdk.getHomePath(); if (homePath == null) { continue; } final SdkAdditionalData data = sdk.getSdkAdditionalData(); final String additionalDataXml; final SdkType sdkType = (SdkType) sdk.getSdkType(); if (data == null) { additionalDataXml = null; } else { final Element element = new Element("additional"); sdkType.saveAdditionalData(data, element); additionalDataXml = JDOMUtil.writeElement(element, "\n"); } final List<String> paths = convertToLocalPaths(sdk.getRootProvider().getFiles(OrderRootType.CLASSES)); String versionString = sdk.getVersionString(); if (versionString != null && sdkType instanceof JavaSdk) { final JavaSdkVersion version = ((JavaSdk) sdkType).getVersion(versionString); if (version != null) { versionString = version.getDescription(); } } globals.add( new SdkLibrary( name, sdkType.getName(), versionString, homePath, paths, additionalDataXml)); } }
private List<VFileEvent> getEvents(String msg, @Nullable Runnable action) { LOG.debug("** waiting for " + msg); myAccept = true; if (action != null) { action.run(); } int timeout = myTimeout; try { synchronized (myWaiter) { //noinspection WaitNotInLoop myWaiter.wait(timeout); } } catch (InterruptedException e) { LOG.warn(e); } LOG.debug("** waited for " + timeout); myFileSystem.refresh(false); ArrayList<VFileEvent> result; synchronized (myEvents) { result = new ArrayList<VFileEvent>(myEvents); myEvents.clear(); } LOG.debug("** events: " + result.size()); return result; }
public static void addLibrary( Module module, String libName, String libDir, String[] classRoots, String[] sourceRoots) { String proto = (classRoots.length > 0 ? classRoots[0] : sourceRoots[0]).endsWith(".jar!/") ? JarFileSystem.PROTOCOL : LocalFileSystem.PROTOCOL; String parentUrl = VirtualFileManager.constructUrl(proto, libDir); List<String> classesUrls = new ArrayList<>(); List<String> sourceUrls = new ArrayList<>(); for (String classRoot : classRoots) { classesUrls.add(parentUrl + classRoot); } for (String sourceRoot : sourceRoots) { sourceUrls.add(parentUrl + sourceRoot); } ModuleRootModificationUtil.addModuleLibrary(module, libName, classesUrls, sourceUrls); }
@Override public boolean isProjectOpened(Project project) { synchronized (myOpenProjects) { return ApplicationManager.getApplication().isUnitTestMode() && myTestProjects.contains(project) || myOpenProjects.contains(project); } }
public static void attachJdkAnnotations(@NotNull SdkModificator modificator) { LocalFileSystem lfs = LocalFileSystem.getInstance(); List<String> pathsChecked = new ArrayList<>(); // community idea under idea String path = FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/java/jdkAnnotations"; VirtualFile root = lfs.findFileByPath(path); pathsChecked.add(path); if (root == null) { // idea under idea path = FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/community/java/jdkAnnotations"; root = lfs.findFileByPath(path); pathsChecked.add(path); } if (root == null) { // build String url = "jar://" + FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/lib/jdkAnnotations.jar!/"; root = VirtualFileManager.getInstance().findFileByUrl(url); pathsChecked.add( FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/lib/jdkAnnotations.jar"); } if (root == null) { String msg = "Paths checked:\n"; for (String p : pathsChecked) { File file = new File(p); msg += "Path: '" + p + "' " + (file.exists() ? "Found" : "Not found") + "; directory children: " + Arrays.toString(file.getParentFile().listFiles()) + "\n"; } LOG.error("JDK annotations not found", msg); return; } OrderRootType annoType = AnnotationOrderRootType.getInstance(); modificator.removeRoot(root, annoType); modificator.addRoot(root, annoType); }
private void runAutoMake() { if (ApplicationManager.getApplication().isUnitTestMode()) { return; } final Project[] openProjects = myProjectManager.getOpenProjects(); if (openProjects.length > 0) { final List<RequestFuture> futures = new ArrayList<RequestFuture>(); for (final Project project : openProjects) { if (project.isDefault() || project.isDisposed()) { continue; } final CompilerWorkspaceConfiguration config = CompilerWorkspaceConfiguration.getInstance(project); if (!config.useOutOfProcessBuild() || !config.MAKE_PROJECT_ON_SAVE) { continue; } final List<String> emptyList = Collections.emptyList(); final RequestFuture future = scheduleBuild( project, false, true, emptyList, emptyList, emptyList, Collections.<String, String>emptyMap(), new AutoMakeMessageHandler(project)); if (future != null) { futures.add(future); synchronized (myAutomakeFutures) { myAutomakeFutures.put(future, project); } } } try { for (RequestFuture future : futures) { future.waitFor(); } } finally { synchronized (myAutomakeFutures) { myAutomakeFutures.keySet().removeAll(futures); } } } }
@Override @NotNull public VirtualFile[] getLocalRoots() { List<VirtualFile> roots = ContainerUtil.newSmartList(); myRootsLock.readLock().lock(); try { for (NewVirtualFile root : myRoots.values()) { if (root.isInLocalFileSystem() && !(root.getFileSystem() instanceof TempFileSystem)) { roots.add(root); } } } finally { myRootsLock.readLock().unlock(); } return VfsUtilCore.toVirtualFileArray(roots); }
@Override @NotNull public VirtualFile[] getRoots(@NotNull final NewVirtualFileSystem fs) { final List<VirtualFile> roots = new ArrayList<VirtualFile>(); myRootsLock.readLock().lock(); try { for (NewVirtualFile root : myRoots.values()) { if (root.getFileSystem() == fs) { roots.add(root); } } } finally { myRootsLock.readLock().unlock(); } return VfsUtilCore.toVirtualFileArray(roots); }
private static void scanFolder(File javasFolder, List<String> result) { @SuppressWarnings("RedundantCast") File[] candidates = javasFolder.listFiles((FileFilter) JdkUtil::checkForJdk); if (candidates != null) { for (File file : candidates) { result.add(file.getAbsolutePath()); } } }
private static List<PsiElement> getTopLevelRegExpChars(String regExpText, Project project) { @SuppressWarnings("deprecation") PsiFile file = PsiFileFactory.getInstance(project).createFileFromText("A.regexp", regExpText); List<PsiElement> result = null; final PsiElement[] children = file.getChildren(); for (PsiElement child : children) { PsiElement[] grandChildren = child.getChildren(); if (grandChildren.length != 1) return Collections .emptyList(); // a | b, more than one branch, can not predict in current way for (PsiElement grandGrandChild : grandChildren[0].getChildren()) { if (result == null) result = new ArrayList<>(); result.add(grandGrandChild); } } return result != null ? result : Collections.<PsiElement>emptyList(); }