private static THashSet<String> collectFoldersToExclude( final Module module, final VirtualFile pubspecYamlFile) { final THashSet<String> newExcludedPackagesUrls = new THashSet<String>(); final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(module.getProject()).getFileIndex(); final VirtualFile root = pubspecYamlFile.getParent(); final VirtualFile binFolder = root.findChild("bin"); if (binFolder != null && binFolder.isDirectory() && fileIndex.isInContent(binFolder)) { newExcludedPackagesUrls.add(binFolder.getUrl() + "/packages"); } appendPackagesFolders(newExcludedPackagesUrls, root.findChild("benchmark"), fileIndex); appendPackagesFolders(newExcludedPackagesUrls, root.findChild("example"), fileIndex); appendPackagesFolders(newExcludedPackagesUrls, root.findChild("test"), fileIndex); appendPackagesFolders(newExcludedPackagesUrls, root.findChild("tool"), fileIndex); appendPackagesFolders(newExcludedPackagesUrls, root.findChild("web"), fileIndex); // Folder packages/ThisProject (where ThisProject is the name specified in pubspec.yaml) is a // symlink to local 'lib' folder. Exclude it in order not to have duplicates. Resolve goes to // local 'lib' folder. // Empty 'ThisProject (link to 'lib' folder)' node is added to Project Structure by // DartTreeStructureProvider final VirtualFile libFolder = root.findChild("lib"); if (libFolder != null && libFolder.isDirectory()) { final String pubspecName = PubspecYamlUtil.getPubspecName(pubspecYamlFile); if (pubspecName != null) { newExcludedPackagesUrls.add(root.getUrl() + "/packages/" + pubspecName); } } return newExcludedPackagesUrls; }
@Nullable private MPSTreeNode getNode(MPSTreeNode rootTreeNode, VirtualFile file) { if (rootTreeNode instanceof AbstractFileTreeNode) { VirtualFile nodeFile = ((AbstractFileTreeNode) rootTreeNode).getFile(); if (nodeFile != null) { if (nodeFile.getUrl().equals(file.getUrl())) { return rootTreeNode; } if (!VfsUtil.isAncestor(nodeFile, file, false)) { return null; } } else { return null; } } for (MPSTreeNode node : rootTreeNode) { node.init(); MPSTreeNode result = getNode(node, file); if (result != null) { return result; } } return null; }
static String getJunitClsUrl(final boolean version4) { String url = version4 ? JavaSdkUtil.getJunit4JarPath() : JavaSdkUtil.getJunit3JarPath(); final VirtualFile localFile = VirtualFileManager.getInstance().findFileByUrl(pathToUrl(url)); if (localFile != null) { final VirtualFile jarFile = JarFileSystem.getInstance().getJarRootForLocalFile(localFile); url = jarFile != null ? jarFile.getUrl() : localFile.getUrl(); } return url; }
public void testContentWrite() throws Exception { File content = getTestRoot(); File source = new File(content, "source"); File testSource = new File(content, "testSource"); File exclude = new File(content, "exclude"); File classes = new File(content, "classes"); File testClasses = new File(content, "testClasses"); final VirtualFile contentFile = LocalFileSystem.getInstance().findFileByIoFile(content); assertNotNull(contentFile); final VirtualFile sourceFile = LocalFileSystem.getInstance().findFileByIoFile(source); assertNotNull(sourceFile); final VirtualFile testSourceFile = LocalFileSystem.getInstance().findFileByIoFile(testSource); assertNotNull(testSourceFile); final VirtualFile excludeFile = LocalFileSystem.getInstance().findFileByIoFile(exclude); assertNotNull(excludeFile); final VirtualFile classesFile = LocalFileSystem.getInstance().findFileByIoFile(classes); assertNotNull(classesFile); final VirtualFile testClassesFile = LocalFileSystem.getInstance().findFileByIoFile(testClasses); assertNotNull(testClassesFile); final File moduleFile = new File(content, "test.iml"); final Module module = createModule(moduleFile); final ModuleRootManagerImpl moduleRootManager = (ModuleRootManagerImpl) ModuleRootManager.getInstance(module); PsiTestUtil.addContentRoot(module, contentFile); PsiTestUtil.addSourceRoot(module, sourceFile); PsiTestUtil.addSourceRoot(module, testSourceFile, true); ModuleRootModificationUtil.setModuleSdk(module, JavaSdkImpl.getMockJdk17()); PsiTestUtil.addExcludedRoot(module, excludeFile); PsiTestUtil.setCompilerOutputPath(module, classesFile.getUrl(), false); PsiTestUtil.setCompilerOutputPath(module, testClassesFile.getUrl(), true); final Element element = new Element("root"); moduleRootManager.getState().writeExternal(element); assertElementEquals( element, "<root inherit-compiler-output=\"false\">" + "<output url=\"file://$MODULE_DIR$/classes\" />" + "<output-test url=\"file://$MODULE_DIR$/testClasses\" />" + "<exclude-output />" + "<content url=\"file://$MODULE_DIR$\">" + "<sourceFolder url=\"file://$MODULE_DIR$/source\" isTestSource=\"false\" />" + "<sourceFolder url=\"file://$MODULE_DIR$/testSource\" isTestSource=\"true\" />" + "<excludeFolder url=\"file://$MODULE_DIR$/exclude\" />" + "</content>" + "<orderEntry type=\"jdk\" jdkName=\"java 1.7\" jdkType=\"JavaSDK\" />" + "<orderEntry type=\"sourceFolder\" forTests=\"false\" />" + "</root>", module); }
private static String[] addOutputs(Module module, int index) { String[] outputs = new String[2]; String prefix = "outputs" + File.separatorChar; VirtualFile generalOutput = findFile(prefix + "general " + index); VirtualFile testOutput = findFile(prefix + "tests" + index); outputs[0] = generalOutput.getPresentableUrl(); outputs[1] = testOutput.getPresentableUrl(); PsiTestUtil.setCompilerOutputPath(module, generalOutput.getUrl(), false); PsiTestUtil.setCompilerOutputPath(module, testOutput.getUrl(), true); return outputs; }
@Override public Element export( @NotNull RefEntity refEntity, @NotNull final Element element, final int actualLine) { refEntity = getRefinedElement(refEntity); Element problem = new Element("problem"); if (refEntity instanceof RefElement) { final RefElement refElement = (RefElement) refEntity; final SmartPsiElementPointer pointer = refElement.getPointer(); PsiFile psiFile = pointer.getContainingFile(); if (psiFile == null) return null; Element fileElement = new Element("file"); Element lineElement = new Element("line"); final VirtualFile virtualFile = psiFile.getVirtualFile(); LOG.assertTrue(virtualFile != null); fileElement.addContent(virtualFile.getUrl()); if (actualLine == -1) { final Document document = PsiDocumentManager.getInstance(pointer.getProject()).getDocument(psiFile); LOG.assertTrue(document != null); final Segment range = pointer.getRange(); lineElement.addContent( String.valueOf( range != null ? document.getLineNumber(range.getStartOffset()) + 1 : -1)); } else { lineElement.addContent(String.valueOf(actualLine)); } problem.addContent(fileElement); problem.addContent(lineElement); appendModule(problem, refElement.getModule()); } else if (refEntity instanceof RefModule) { final RefModule refModule = (RefModule) refEntity; final VirtualFile moduleFile = refModule.getModule().getModuleFile(); final Element fileElement = new Element("file"); fileElement.addContent(moduleFile != null ? moduleFile.getUrl() : refEntity.getName()); problem.addContent(fileElement); appendModule(problem, refModule); } for (RefManagerExtension extension : myExtensions.values()) { extension.export(refEntity, problem); } new SmartRefElementPointerImpl(refEntity, true).writeExternal(problem); element.addContent(problem); return problem; }
@Test public void testProjectWithUnresolvedDependency() throws Exception { final VirtualFile depJar = createProjectJarSubFile("lib/dep/dep/1.0/dep-1.0.jar"); importProject( "apply plugin: 'java'\n" + "\n" + "repositories {\n" + " maven { url file('lib') }\n" + "}\n" + "dependencies {\n" + " compile 'dep:dep:1.0'\n" + " compile 'some:unresolvable-lib:0.1'\n" + "}\n"); assertModules("project", "project_main", "project_test"); final String depName = "Gradle: dep:dep:1.0"; assertModuleLibDep("project_main", depName, depJar.getUrl()); assertModuleLibDepScope("project_main", depName, DependencyScope.COMPILE); assertModuleLibDepScope( "project_main", "Gradle: some:unresolvable-lib:0.1", DependencyScope.COMPILE); List<LibraryOrderEntry> unresolvableDep = getModuleLibDeps("project_main", "Gradle: some:unresolvable-lib:0.1"); assertEquals(1, unresolvableDep.size()); LibraryOrderEntry unresolvableEntry = unresolvableDep.iterator().next(); assertFalse(unresolvableEntry.isModuleLevel()); assertEquals(DependencyScope.COMPILE, unresolvableEntry.getScope()); String[] unresolvableEntryUrls = unresolvableEntry.getUrls(OrderRootType.CLASSES); assertEquals(1, unresolvableEntryUrls.length); assertTrue(unresolvableEntryUrls[0].contains("Could not find some:unresolvable-lib:0.1")); assertModuleLibDep("project_test", depName, depJar.getUrl()); assertModuleLibDepScope("project_test", depName, DependencyScope.COMPILE); importProjectUsingSingeModulePerGradleProject(); assertModules("project"); assertModuleLibDep("project", depName, depJar.getUrl()); assertModuleLibDepScope("project", depName, DependencyScope.COMPILE); assertModuleLibDepScope("project", "Gradle: unresolvable-lib-0.1:1", DependencyScope.COMPILE); unresolvableDep = getModuleLibDeps("project", "Gradle: unresolvable-lib-0.1:1"); assertEquals(1, unresolvableDep.size()); unresolvableEntry = unresolvableDep.iterator().next(); assertTrue(unresolvableEntry.isModuleLevel()); assertEquals(DependencyScope.COMPILE, unresolvableEntry.getScope()); unresolvableEntryUrls = unresolvableEntry.getUrls(OrderRootType.CLASSES); assertEquals(0, unresolvableEntryUrls.length); }
@Override @NotNull public VirtualFile[] getFiles(@NotNull OrderRootType rootType) { assert !isDisposed(); final List<VirtualFile> expanded = new ArrayList<VirtualFile>(); for (VirtualFile file : myRoots.get(rootType).getFiles()) { if (file.isDirectory()) { if (myJarDirectories.contains(rootType, file.getUrl())) { collectJarFiles(file, expanded, myJarDirectories.isRecursive(rootType, file.getUrl())); continue; } } expanded.add(file); } return VfsUtilCore.toVirtualFileArray(expanded); }
@Override public int getTextLength() { VirtualFile file = getVirtualFile(); Document document = FileDocumentManager.getInstance().getDocument(file); assert document != null : file.getUrl(); return document.getTextLength(); }
private void showVFInfo(VirtualFile virtualFile) { logger.info("showVFInfo(): XML"); logger.info("virtualFile: isValid= " + virtualFile.isValid()); logger.info("virtualFile: isSymLink=" + virtualFile.isSymLink()); logger.info("virtualFile: url= " + virtualFile.getUrl()); logger.info("virtualFile: fileType= " + virtualFile.getFileType()); }
private static void fillLibrary( @NotNull Library library, @NotNull Collection<VirtualFile> libraryRoots, Set<VirtualFile> exclusions) { ApplicationManager.getApplication().assertWriteAccessAllowed(); Library.ModifiableModel libraryModel = library.getModifiableModel(); for (String root : libraryModel.getUrls(OrderRootType.CLASSES)) { libraryModel.removeRoot(root, OrderRootType.CLASSES); } for (String root : libraryModel.getUrls(OrderRootType.SOURCES)) { libraryModel.removeRoot(root, OrderRootType.SOURCES); } for (VirtualFile libraryRoot : libraryRoots) { libraryModel.addRoot( libraryRoot, OrderRootType .CLASSES); // in order to consider GOPATH as library and show it in Ext. Libraries libraryModel.addRoot( libraryRoot, OrderRootType.SOURCES); // in order to find usages inside GOPATH } for (VirtualFile root : exclusions) { ((LibraryEx.ModifiableModelEx) libraryModel).addExcludedRoot(root.getUrl()); } libraryModel.commit(); }
public List<String> getUrlFor(PsiElement element, PsiElement originalElement) { final VirtualFile helpFile = getHelpFile(originalElement); if (helpFile == null || !(helpFile.getFileSystem() instanceof LocalFileSystem)) { return null; } return Collections.singletonList(helpFile.getUrl()); }
private boolean cleanupChildrenRecursively( @NotNull final Object fromElement, final @Nullable CompileScope scope, @NotNull UUID currentSessionId) { final ErrorViewStructure structure = myPanel.getErrorViewStructure(); ErrorTreeElement[] elements = structure.getChildElements(fromElement); if (elements.length == 0) return true; boolean result = false; for (ErrorTreeElement element : elements) { if (element instanceof GroupingElement) { if (scope != null) { final VirtualFile file = ((GroupingElement) element).getFile(); if (file != null && !scope.belongs(file.getUrl())) { continue; } } if (!currentSessionId.equals(element.getData())) { structure.removeElement(element); result = true; } else { result |= cleanupChildrenRecursively(element, scope, currentSessionId); } } else { if (!currentSessionId.equals(element.getData())) { structure.removeElement(element); result = true; } } } return result; }
private void appendChosenAnnotationsRoot( @NotNull final OrderEntry entry, @NotNull final VirtualFile vFile) { if (entry instanceof LibraryOrderEntry) { Library library = ((LibraryOrderEntry) entry).getLibrary(); LOG.assertTrue(library != null); final ModifiableRootModel rootModel = ModuleRootManager.getInstance(entry.getOwnerModule()).getModifiableModel(); final Library.ModifiableModel model = library.getModifiableModel(); model.addRoot(vFile, AnnotationOrderRootType.getInstance()); model.commit(); rootModel.commit(); } else if (entry instanceof ModuleSourceOrderEntry) { final ModifiableRootModel model = ModuleRootManager.getInstance(entry.getOwnerModule()).getModifiableModel(); final JavaModuleExternalPaths extension = model.getModuleExtension(JavaModuleExternalPaths.class); extension.setExternalAnnotationUrls( ArrayUtil.mergeArrays(extension.getExternalAnnotationsUrls(), vFile.getUrl())); model.commit(); } else if (entry instanceof JdkOrderEntry) { final SdkModificator sdkModificator = ((JdkOrderEntry) entry).getJdk().getSdkModificator(); sdkModificator.addRoot(vFile, AnnotationOrderRootType.getInstance()); sdkModificator.commitChanges(); } myExternalAnnotations.clear(); }
protected XtextResource createExpectedResource() { XtextResource _xblockexpression = null; { XtextResourceSet resourceSet = this.createFreshResourceSet(); VirtualFile _virtualFile = this.myFile.getVirtualFile(); String _url = _virtualFile.getUrl(); final URI uri = URI.createURI(_url); Resource _createResource = resourceSet.createResource(uri); final Procedure1<XtextResource> _function = new Procedure1<XtextResource>() { @Override public void apply(final XtextResource it) { try { String _text = AbstractLanguageParsingTestCase.this.myFile.getText(); byte[] _bytes = _text.getBytes(); ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_bytes); it.load(_byteArrayInputStream, null); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } } }; _xblockexpression = ObjectExtensions.<XtextResource>operator_doubleArrow( ((XtextResource) _createResource), _function); } return _xblockexpression; }
private static String setCompilerOutput(Module module, String path, boolean testOutput) { VirtualFile output = ModuleRootManager.getInstance(module).getContentEntries()[0].getFile().findChild(path); assertNotNull(output); PsiTestUtil.setCompilerOutputPath(module, output.getUrl(), testOutput); return output.getPath().replace('/', File.separatorChar); }
@Override public PsiElement getMirror() { TreeElement mirrorTreeElement = myMirrorFileElement; if (mirrorTreeElement == null) { synchronized (myMirrorLock) { mirrorTreeElement = myMirrorFileElement; if (mirrorTreeElement == null) { VirtualFile file = getVirtualFile(); PsiClass[] classes = getClasses(); String fileName = (classes.length > 0 ? classes[0].getName() : file.getNameWithoutExtension()) + JavaFileType.DOT_DEFAULT_EXTENSION; final Document document = FileDocumentManager.getInstance().getDocument(file); assert document != null : file.getUrl(); CharSequence mirrorText = document.getImmutableCharSequence(); boolean internalDecompiler = StringUtil.startsWith(mirrorText, BANNER); PsiFileFactory factory = PsiFileFactory.getInstance(getManager().getProject()); PsiFile mirror = factory.createFileFromText(fileName, JavaLanguage.INSTANCE, mirrorText, false, false); mirror.putUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY, getLanguageLevel()); mirrorTreeElement = SourceTreeToPsiMap.psiToTreeNotNull(mirror); try { final TreeElement finalMirrorTreeElement = mirrorTreeElement; ProgressManager.getInstance() .executeNonCancelableSection( new Runnable() { @Override public void run() { setMirror(finalMirrorTreeElement); putUserData(CLS_DOCUMENT_LINK_KEY, document); } }); } catch (InvalidMirrorException e) { //noinspection ThrowableResultOfMethodCallIgnored LOG.error(file.getUrl(), internalDecompiler ? e : wrapException(e, file)); } ((PsiFileImpl) mirror).setOriginalFile(this); myMirrorFileElement = mirrorTreeElement; } } } return mirrorTreeElement.getPsi(); }
@Override @NotNull public synchronized VirtualFilePointer create( @NotNull VirtualFile file, @NotNull Disposable parent, @Nullable VirtualFilePointerListener listener) { return create(file, file.getUrl(), parent, listener); }
public static void removeExcludedRoot(Module module, VirtualFile root) { ModuleRootModificationUtil.updateModel( module, model -> { ContentEntry entry = findContentEntryWithAssertion(model, root); entry.removeExcludeFolder(root.getUrl()); }); }
public boolean isModified() { if (myProperties.isEnabled() != myRunTargetCheckBox.isSelected()) return true; if (myTarget == null) { return myProperties.getFileUrl() != null; } if (!Comparing.equal(myTarget.getName(), myProperties.getTargetName())) return true; final VirtualFile file = myTarget.getModel().getBuildFile().getVirtualFile(); return file != null && !Comparing.equal(file.getUrl(), myProperties.getFileUrl()); }
public AbstractUrl createUrlByElement(Object element) { if (element instanceof PsiFile) { VirtualFile file = ((PsiFile) element).getVirtualFile(); if (file != null) { return new PsiFileUrl(file.getUrl()); } } return null; }
@Override public void addJarDirectory( @NotNull final VirtualFile file, final boolean recursive, @NotNull OrderRootType rootType) { assert !isDisposed(); LOG.assertTrue(isWritable()); final VirtualFilePointerContainer container = myRoots.get(rootType); container.add(file); myJarDirectories.add(rootType, file.getUrl(), recursive); }
public void addDependency(final PsiElement element) { final PsiFile psiFile = element.getContainingFile(); if (psiFile != null) { VirtualFile file = psiFile.getVirtualFile(); if (file != null) { addDependency(file.getUrl(), file.getTimeStamp()); } } }
public boolean visitRoot(VirtualFile root, Module module, Sdk sdk, boolean isModuleSource) { String vpath = VfsUtilCore.urlToPath(root.getUrl()); if (myPath.startsWith(vpath)) { myResult = vpath; return false; } else { return true; } }
@Override @NotNull public String[] getUrls(@NotNull OrderRootType rootType) { Set<String> originalUrls = new LinkedHashSet<String>(Arrays.asList(LibraryImpl.this.getUrls(rootType))); for (VirtualFile file : getFiles(rootType)) { // Add those expanded with jar directories. originalUrls.add(file.getUrl()); } return ArrayUtil.toStringArray(originalUrls); }
protected String prepareValidUrlInsideJar(String url) { final VirtualFile localFile = VirtualFileManager.getInstance().findFileByUrl(url); if (localFile != null) { final VirtualFile jarFile = JarFileSystem.getInstance().getJarRootForLocalFile(localFile); if (jarFile != null) { return jarFile.getUrl(); } } return url; }
public void apply() { myProperties.setEnabled(myRunTargetCheckBox.isSelected()); if (myTarget != null) { final VirtualFile file = myTarget.getModel().getBuildFile().getVirtualFile(); if (file != null) { myProperties.setFileUrl(file.getUrl()); myProperties.setTargetName(myTarget.getName()); return; } } myProperties.setFileUrl(null); myProperties.setTargetName(null); }
@Nullable public static String getDefaultArtifactOutputPath( @NotNull String artifactName, final @NotNull Project project) { final CompilerProjectExtension extension = CompilerProjectExtension.getInstance(project); if (extension == null) return null; String outputUrl = extension.getCompilerOutputUrl(); if (outputUrl == null || outputUrl.length() == 0) { final VirtualFile baseDir = project.getBaseDir(); if (baseDir == null) return null; outputUrl = baseDir.getUrl() + "/out"; } return VfsUtil.urlToPath(outputUrl) + "/artifacts/" + FileUtil.sanitizeFileName(artifactName); }
@Nullable @Override public String getCompilerOutputUrl() { if (myOutputDirPointer == null) { VirtualFile baseDir = myProject.getBaseDir(); assert baseDir != null; VirtualFile outDir = baseDir.findFileByRelativePath(DEFAULT_OUTPUT_URL); return outDir == null ? myProject.getPresentableUrl() + "/" + DEFAULT_OUTPUT_URL : outDir.getUrl(); } return myOutputDirPointer.getUrl(); }
private boolean needTransformCopying(CompileScope compileScope) { final CompilerConfiguration configuration = CompilerConfiguration.getInstance(myProject); final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex(); for (VirtualFile file : FilenameIndex.getVirtualFilesByName( myProject, AST_TRANSFORM_FILE_NAME, GlobalSearchScope.projectScope(myProject))) { if (compileScope.belongs(file.getUrl()) && index.isInSource(file) && !configuration.isResourceFile(file)) { return true; } } return false; }