@NotNull private VirtualFilePointer create( @Nullable("null means the pointer will be created from the (not null) url") VirtualFile file, @Nullable("null means url has to be computed from the (not-null) file path") String url, @NotNull Disposable parentDisposable, @Nullable VirtualFilePointerListener listener) { VirtualFileSystem fileSystem; String protocol; String path; if (file == null) { int protocolEnd = url.indexOf(URLUtil.SCHEME_SEPARATOR); if (protocolEnd == -1) { protocol = null; fileSystem = null; } else { protocol = url.substring(0, protocolEnd); fileSystem = myVirtualFileManager.getFileSystem(protocol); } path = url.substring(protocolEnd + URLUtil.SCHEME_SEPARATOR.length()); } else { fileSystem = file.getFileSystem(); protocol = fileSystem.getProtocol(); path = file.getPath(); url = VirtualFileManager.constructUrl(protocol, path); } if (fileSystem == TEMP_FILE_SYSTEM) { // for tests, recreate always VirtualFile found = file == null ? VirtualFileManager.getInstance().findFileByUrl(url) : file; return new IdentityVirtualFilePointer(found, url); } boolean isJar = fileSystem == JAR_FILE_SYSTEM; if (fileSystem != LOCAL_FILE_SYSTEM && !isJar) { // we are unable to track alien file systems for now VirtualFile found = fileSystem == null ? null : file != null ? file : VirtualFileManager.getInstance().findFileByUrl(url); // if file is null, this pointer will never be alive return getOrCreateIdentity(url, found); } if (file == null) { String cleanPath = cleanupPath(path, isJar); // if newly created path is the same as substringed from url one then the url did not change, // we can reuse it //noinspection StringEquality if (cleanPath != path) { url = VirtualFileManager.constructUrl(protocol, cleanPath); path = cleanPath; } } // else url has come from VirtualFile.getPath() and is good enough VirtualFilePointerImpl pointer = getOrCreate(parentDisposable, listener, path, Pair.create(file, url)); DelegatingDisposable.registerDisposable(parentDisposable, pointer); return pointer; }
@NotNull private VirtualFilePointer create( @Nullable VirtualFile file, @NotNull String url, @NotNull final Disposable parentDisposable, @Nullable VirtualFilePointerListener listener) { String protocol; VirtualFileSystem fileSystem; if (file == null) { protocol = VirtualFileManager.extractProtocol(url); fileSystem = myVirtualFileManager.getFileSystem(protocol); } else { protocol = null; fileSystem = file.getFileSystem(); } if (fileSystem == TempFileSystem.getInstance()) { // for tests, recreate always since VirtualFile found = fileSystem == null ? null : file != null ? file : VirtualFileManager.getInstance().findFileByUrl(url); return new IdentityVirtualFilePointer(found, url); } if (fileSystem != LocalFileSystem.getInstance() && fileSystem != JarFileSystem.getInstance()) { // we are unable to track alien file systems for now VirtualFile found = fileSystem == null ? null : file != null ? file : VirtualFileManager.getInstance().findFileByUrl(url); // if file is null, this pointer will never be alive return getOrCreateIdentity(url, found); } String path; if (file == null) { path = VirtualFileManager.extractPath(url); path = cleanupPath(path, protocol); url = VirtualFileManager.constructUrl(protocol, path); } else { path = file.getPath(); // url has come from VirtualFile.getUrl() and is good enough } VirtualFilePointerImpl pointer = getOrCreate(file, url, parentDisposable, listener, path); int newCount = pointer.incrementUsageCount(); if (newCount == 1) { Disposer.register(parentDisposable, pointer); } else { // already registered register(parentDisposable, pointer); } return pointer; }
@SuppressWarnings({"HardCodedStringLiteral"}) @Nullable public static VirtualFile findRelativeFile(@NotNull String uri, @Nullable VirtualFile base) { if (base != null) { if (!base.isValid()) { LOG.error("Invalid file name: " + base.getName() + ", url: " + uri); } } uri = uri.replace('\\', '/'); if (uri.startsWith("file:///")) { uri = uri.substring("file:///".length()); if (!SystemInfo.isWindows) uri = "/" + uri; } else if (uri.startsWith("file:/")) { uri = uri.substring("file:/".length()); if (!SystemInfo.isWindows) uri = "/" + uri; } else if (uri.startsWith("file:")) { uri = uri.substring("file:".length()); } VirtualFile file = null; if (uri.startsWith("jar:file:/")) { uri = uri.substring("jar:file:/".length()); if (!SystemInfo.isWindows) uri = "/" + uri; file = VirtualFileManager.getInstance().findFileByUrl(JarFileSystem.PROTOCOL_PREFIX + uri); } else { if (!SystemInfo.isWindows && StringUtil.startsWithChar(uri, '/')) { file = LocalFileSystem.getInstance().findFileByPath(uri); } else if (SystemInfo.isWindows && uri.length() >= 2 && Character.isLetter(uri.charAt(0)) && uri.charAt(1) == ':') { file = LocalFileSystem.getInstance().findFileByPath(uri); } } if (file == null && uri.contains(JarFileSystem.JAR_SEPARATOR)) { file = JarFileSystem.getInstance().findFileByPath(uri); if (file == null && base == null) { file = VirtualFileManager.getInstance().findFileByUrl(uri); } } if (file == null) { if (base == null) return LocalFileSystem.getInstance().findFileByPath(uri); if (!base.isDirectory()) base = base.getParent(); if (base == null) return LocalFileSystem.getInstance().findFileByPath(uri); file = VirtualFileManager.getInstance().findFileByUrl(base.getUrl() + "/" + uri); if (file == null) return null; } return file; }
@Nullable public static VirtualFile getDirectory(@NotNull final FindModel findModel) { String directoryName = findModel.getDirectoryName(); if (findModel.isProjectScope() || StringUtil.isEmpty(directoryName)) { return null; } String path = directoryName.replace(File.separatorChar, '/'); VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path); if (virtualFile == null || !virtualFile.isDirectory()) { virtualFile = null; for (LocalFileProvider provider : ((VirtualFileManagerEx) VirtualFileManager.getInstance()).getLocalFileProviders()) { VirtualFile file = provider.findLocalVirtualFileByPath(path); if (file != null && file.isDirectory()) { if (file.getChildren().length > 0) { virtualFile = file; break; } if (virtualFile == null) { virtualFile = file; } } } } return virtualFile; }
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 private static List<VirtualFile> findClasses(File file, boolean isJre) { List<VirtualFile> result = ContainerUtil.newArrayList(); VirtualFileManager fileManager = VirtualFileManager.getInstance(); String path = file.getPath(); if (JrtFileSystem.isModularJdk(path)) { String url = VirtualFileManager.constructUrl( JrtFileSystem.PROTOCOL, FileUtil.toSystemIndependentName(path) + JrtFileSystem.SEPARATOR); for (String module : JrtFileSystem.listModules(path)) { ContainerUtil.addIfNotNull(result, fileManager.findFileByUrl(url + module)); } } for (File root : JavaSdkUtil.getJdkClassesRoots(file, isJre)) { String url = VfsUtil.getUrlForLibraryRoot(root); ContainerUtil.addIfNotNull(result, fileManager.findFileByUrl(url)); } Collections.sort(result, (o1, o2) -> o1.getPath().compareTo(o2.getPath())); return result; }
@NotNull private VirtualFilePointer create( @Nullable VirtualFile file, @NotNull String url, @NotNull final Disposable parentDisposable, @Nullable VirtualFilePointerListener listener) { String protocol; IVirtualFileSystem fileSystem; if (file == null) { protocol = VirtualFileManager.extractProtocol(url); fileSystem = myVirtualFileManager.getFileSystem(protocol); } else { protocol = null; fileSystem = file.getFileSystem(); } if (fileSystem == myTempFileSystem) { // for tests, recreate always VirtualFile found = file == null ? VirtualFileManager.getInstance().findFileByUrl(url) : file; return new IdentityVirtualFilePointer(found, url); } if (fileSystem != myLocalFileSystem && !(fileSystem instanceof ArchiveFileSystem)) { // we are unable to track alien file systems for now VirtualFile found = fileSystem == null ? null : file != null ? file : VirtualFileManager.getInstance().findFileByUrl(url); // if file is null, this pointer will never be alive return getOrCreateIdentity(url, found); } String path; if (file == null) { path = VirtualFileManager.extractPath(url); path = cleanupPath(path, protocol, fileSystem); url = VirtualFileManager.constructUrl(protocol, path); } else { path = file.getPath(); // url has come from VirtualFile.getUrl() and is good enough } VirtualFilePointerImpl pointer = getOrCreate(parentDisposable, listener, path, Pair.create(file, url)); DelegatingDisposable.registerDisposable(parentDisposable, pointer); return pointer; }
@Nullable private static VirtualFile findInJar(File jarFile, String relativePath) { if (!jarFile.exists()) return null; String url = JarFileSystem.PROTOCOL_PREFIX + jarFile.getAbsolutePath().replace(File.separatorChar, '/') + JarFileSystem.JAR_SEPARATOR + relativePath; return VirtualFileManager.getInstance().findFileByUrl(url); }
@Override public void dispose() { if (mySelectedEditor != null) { for (final Map.Entry<PropertiesFile, Editor> entry : myEditors.entrySet()) { if (mySelectedEditor.equals(entry.getValue())) { writeEditorPropertyValue(mySelectedEditor, entry.getKey(), null); } } } VirtualFileManager.getInstance().removeVirtualFileListener(myVfsListener); myDisposed = true; Disposer.dispose(myStructureViewComponent); releaseAllEditors(); }
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); }
@NotNull private static String getPresentableFile( @NotNull String url, @Nullable Map<VirtualFile, VirtualFile> presentableFilesMap) { final VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(url); if (file == null) { return url; } if (presentableFilesMap == null) { return url; } for (Map.Entry<VirtualFile, VirtualFile> entry : presentableFilesMap.entrySet()) { if (file == entry.getValue()) { return entry.getKey().getUrl(); } } return url; }
private void installPropertiesChangeListeners() { final VirtualFileManager virtualFileManager = VirtualFileManager.getInstance(); if (myVfsListener != null) { assert false; virtualFileManager.removeVirtualFileListener(myVfsListener); } myVfsListener = new VirtualFileAdapter() { @Override public void fileCreated(@NotNull VirtualFileEvent event) { if (PropertiesImplUtil.isPropertiesFile(event.getFile(), myProject)) { recreateEditorsPanel(); } } @Override public void fileDeleted(@NotNull VirtualFileEvent event) { for (PropertiesFile file : myEditors.keySet()) { if (Comparing.equal(file.getVirtualFile(), event.getFile())) { recreateEditorsPanel(); return; } } } @Override public void propertyChanged(@NotNull VirtualFilePropertyEvent event) { if (PropertiesImplUtil.isPropertiesFile(event.getFile(), myProject)) { if (VirtualFile.PROP_NAME.equals(event.getPropertyName())) { recreateEditorsPanel(); } else { updateEditorsFromProperties(); } } } }; virtualFileManager.addVirtualFileListener(myVfsListener, this); PsiTreeChangeAdapter psiTreeChangeAdapter = new PsiTreeChangeAdapter() { @Override public void childAdded(@NotNull PsiTreeChangeEvent event) { childrenChanged(event); } @Override public void childRemoved(@NotNull PsiTreeChangeEvent event) { childrenChanged(event); } @Override public void childReplaced(@NotNull PsiTreeChangeEvent event) { childrenChanged(event); } @Override public void childMoved(@NotNull PsiTreeChangeEvent event) { childrenChanged(event); } @Override public void childrenChanged(@NotNull PsiTreeChangeEvent event) { final PsiFile file = event.getFile(); PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(file); if (propertiesFile == null) return; if (!propertiesFile.getResourceBundle().equals(myResourceBundle)) return; updateEditorsFromProperties(); } }; PsiManager.getInstance(myProject).addPsiTreeChangeListener(psiTreeChangeAdapter, this); }
private void initListeners(@NotNull MessageBus messageBus, @NotNull PsiManager psiManager) { messageBus .connect() .subscribe( VirtualFileManager.VFS_CHANGES, new BulkFileListener.Adapter() { @Override public void after(@NotNull List<? extends VFileEvent> events) { fileCount.set(0); List<VirtualFile> files = ContainerUtil.mapNotNull( events, new Function<VFileEvent, VirtualFile>() { @Override public VirtualFile fun(VFileEvent event) { return event.getFile(); } }); queue(files, "VFS events " + events.size()); } }); psiManager.addPsiTreeChangeListener( new PsiTreeChangeAdapter() { @Override public void childrenChanged(@NotNull PsiTreeChangeEvent event) { PsiFile file = event.getFile(); VirtualFile virtualFile = PsiUtilCore.getVirtualFile(file); if (virtualFile != null) { queue(Collections.singletonList(virtualFile), event); } } @Override public void propertyChanged(@NotNull PsiTreeChangeEvent event) { childrenChanged(event); } }); messageBus .connect() .subscribe( DumbService.DUMB_MODE, new DumbService.DumbModeListener() { @Override public void enteredDumbMode() { disable(); } @Override public void exitDumbMode() { enable(); } }); messageBus .connect() .subscribe( PowerSaveMode.TOPIC, new PowerSaveMode.Listener() { @Override public void powerSaveStateChanged() { if (PowerSaveMode.isEnabled()) { enable(); } else { disable(); } } }); myApplication.addApplicationListener( new ApplicationAdapter() { @Override public void beforeWriteActionStart(@NotNull Object action) { disable(); } @Override public void writeActionFinished(@NotNull Object action) { enable(); } @Override public void applicationExiting() { disable(); } }, this); VirtualFileManager.getInstance() .addVirtualFileManagerListener( new VirtualFileManagerListener() { @Override public void beforeRefreshStart(boolean asynchronous) { disable(); } @Override public void afterRefreshFinish(boolean asynchronous) { enable(); } }, this); HeavyProcessLatch.INSTANCE.addListener( new HeavyProcessLatch.HeavyProcessListener() { @Override public void processStarted() {} @Override public void processFinished() { wakeUp(); } }, this); }
@Override @SuppressWarnings("HardCodedStringLiteral") public void setupSdkPaths(@NotNull Sdk sdk) { String homePath = sdk.getHomePath(); assert homePath != null : sdk; File jdkHome = new File(homePath); List<VirtualFile> classes = findClasses(jdkHome, false); VirtualFile sources = findSources(jdkHome); VirtualFile docs = findDocs(jdkHome, "docs/api"); SdkModificator sdkModificator = sdk.getSdkModificator(); Set<VirtualFile> previousRoots = new LinkedHashSet<>(Arrays.asList(sdkModificator.getRoots(OrderRootType.CLASSES))); sdkModificator.removeRoots(OrderRootType.CLASSES); previousRoots.removeAll(new HashSet<>(classes)); for (VirtualFile aClass : classes) { sdkModificator.addRoot(aClass, OrderRootType.CLASSES); } for (VirtualFile root : previousRoots) { sdkModificator.addRoot(root, OrderRootType.CLASSES); } if (sources != null) { sdkModificator.addRoot(sources, OrderRootType.SOURCES); } VirtualFile javaFxSources = findSources(jdkHome, "javafx-src"); if (javaFxSources != null) { sdkModificator.addRoot(javaFxSources, OrderRootType.SOURCES); } if (docs != null) { sdkModificator.addRoot(docs, JavadocOrderRootType.getInstance()); } else if (SystemInfo.isMac) { VirtualFile commonDocs = findDocs(jdkHome, "docs"); if (commonDocs == null) { commonDocs = findInJar(new File(jdkHome, "docs.jar"), "doc/api"); if (commonDocs == null) { commonDocs = findInJar(new File(jdkHome, "docs.jar"), "docs/api"); } } if (commonDocs != null) { sdkModificator.addRoot(commonDocs, JavadocOrderRootType.getInstance()); } VirtualFile appleDocs = findDocs(jdkHome, "appledocs"); if (appleDocs == null) { appleDocs = findInJar(new File(jdkHome, "appledocs.jar"), "appledoc/api"); } if (appleDocs != null) { sdkModificator.addRoot(appleDocs, JavadocOrderRootType.getInstance()); } if (commonDocs == null && appleDocs == null && sources == null) { String url = getDefaultDocumentationUrl(sdk); if (url != null) { sdkModificator.addRoot( VirtualFileManager.getInstance().findFileByUrl(url), JavadocOrderRootType.getInstance()); } } } else if (getVersion(sdk) == JavaSdkVersion.JDK_1_7) { VirtualFile url = VirtualFileManager.getInstance().findFileByUrl("http://docs.oracle.com/javafx/2/api/"); sdkModificator.addRoot(url, JavadocOrderRootType.getInstance()); } attachJdkAnnotations(sdkModificator); sdkModificator.commitChanges(); }
/** * Searches for the file specified by given java,net.URL. Note that this method currently tested * only for "file" and "jar" protocols under Unix and Windows * * @param url the URL to find file by * @return <code>{@link VirtualFile}</code> if the file was found, <code>null</code> otherwise */ public static VirtualFile findFileByURL(@NotNull URL url) { VirtualFileManager virtualFileManager = VirtualFileManager.getInstance(); return findFileByURL(url, virtualFileManager); }