@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;
  }
  @Nullable
  private static VirtualFile getHelpFile(final PsiElement element) {
    final XmlTag xmlTag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
    if (xmlTag == null) {
      return null;
    }
    final AntDomElement antElement = AntSupport.getAntDomElement(xmlTag);
    if (antElement == null) {
      return null;
    }
    final AntDomProject antProject = antElement.getAntProject();
    if (antProject == null) {
      return null;
    }
    final AntInstallation installation = antProject.getAntInstallation();
    if (installation == null) {
      return null; // not configured properly and bundled installation missing
    }
    final String antHomeDir = AntInstallation.HOME_DIR.get(installation.getProperties());

    if (antHomeDir == null) {
      return null;
    }

    @NonNls String path = antHomeDir + "/docs/manual";
    String url;
    if (new File(path).exists()) {
      url =
          VirtualFileManager.constructUrl(
              LocalFileSystem.PROTOCOL, FileUtil.toSystemIndependentName(path));
    } else {
      path = antHomeDir + "/docs.zip";
      if (new File(path).exists()) {
        url =
            VirtualFileManager.constructUrl(
                JarFileSystem.PROTOCOL,
                FileUtil.toSystemIndependentName(path)
                    + JarFileSystem.JAR_SEPARATOR
                    + "docs/manual");
      } else {
        return null;
      }
    }

    final VirtualFile documentationRoot = VirtualFileManager.getInstance().findFileByUrl(url);
    if (documentationRoot == null) {
      return null;
    }

    return getHelpFile(antElement, documentationRoot);
  }
예제 #3
0
    private void updateTree() {
      if (myFileSystemTree != null) {
        Disposer.dispose(myFileSystemTree);
        myFileSystemTree = null;
      }

      myFileSystemTree =
          new FileSystemTreeImpl(
              null, new FileChooserDescriptor(true, true, true, true, true, false));
      AbstractTreeUi ui = myFileSystemTree.getTreeBuilder().getUi();

      String path = myModelRoot.getPath() == null ? "" : myModelRoot.getPath();
      VirtualFile virtualFile =
          VirtualFileManager.getInstance()
              .findFileByUrl(VirtualFileManager.constructUrl("file", path));
      if (myModelRoot.getModule() != null && (virtualFile == null || path.isEmpty())) {
        if (myModelRoot.getModule() instanceof AbstractModule) {
          virtualFile =
              VirtualFileManager.getInstance()
                  .findFileByUrl(
                      VirtualFileManager.constructUrl(
                          "file",
                          ((AbstractModule) myModelRoot.getModule())
                              .getModuleSourceDir()
                              .getPath()));
        }
      }

      if (virtualFile != null) myFileSystemTree.select(virtualFile, null);

      myFileSystemTree.addListener(
          new Listener() {
            @Override
            public void selectionChanged(List<VirtualFile> selection) {
              if (selection.size() > 0) {
                myModelRoot.setPath(FileUtil.getCanonicalPath(selection.get(0).getPath()));
                myEventDispatcher.getMulticaster().fireDataChanged();
              }
            }
          },
          SModelRootEntry.this);

      Disposer.register(SModelRootEntry.this, myFileSystemTree);

      myTreePanel.removeAll();
      myTreePanel.add(ui.getTree(), BorderLayout.CENTER);
      ui.scrollSelectionToVisible(null, true);
    }
예제 #4
0
 private static String correctRepositoryRule(String input) {
   String protocol = VirtualFileManager.extractProtocol(input);
   if (protocol == null) {
     input = VirtualFileManager.constructUrl(URLUtil.HTTP_PROTOCOL, input);
   }
   return input;
 }
  private static void updateUrl(
      Library.ModifiableModel library,
      OrderRootType type,
      MavenArtifact artifact,
      MavenExtraArtifactType artifactType,
      MavenProject project,
      boolean clearAll) {
    String classifier = null;
    String extension = null;

    if (artifactType != null) {
      Pair<String, String> result = project.getClassifierAndExtension(artifact, artifactType);
      classifier = result.first;
      extension = result.second;
    }

    String newPath = artifact.getPathForExtraArtifact(classifier, extension);
    String newUrl =
        VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, newPath)
            + JarFileSystem.JAR_SEPARATOR;
    for (String url : library.getUrls(type)) {
      if (newUrl.equals(url)) return;
      if (clearAll || isRepositoryUrl(artifact, url, classifier, extension)) {
        library.removeRoot(url, type);
      }
    }
    library.addRoot(newUrl, type);
  }
 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);
 }
예제 #7
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;
    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;
  }
  @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;
  }
예제 #10
0
 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);
 }
예제 #11
0
 private static String pathToUrl(DefaultModelRoot root) {
   return VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, root.getPath());
 }
  @NotNull
  protected String[] getSystemLibraryUrlsImpl(
      @NotNull Sdk sdk, String name, OrderRootType orderRootType) {
    if (orderRootType == BinariesOrderRootType.getInstance()) {
      File libraryByAssemblyName = getLibraryByAssemblyName(name, null);
      if (libraryByAssemblyName == null) {
        return ArrayUtil.EMPTY_STRING_ARRAY;
      }

      return new String[] {
        VirtualFileManager.constructUrl(
                DotNetModuleFileType.PROTOCOL, libraryByAssemblyName.getPath())
            + ArchiveFileSystem.ARCHIVE_SEPARATOR
      };
    } else if (orderRootType == DocumentationOrderRootType.getInstance()) {
      String[] systemLibraryUrls = getSystemLibraryUrls(name, BinariesOrderRootType.getInstance());
      if (systemLibraryUrls.length != 1) {
        return ArrayUtil.EMPTY_STRING_ARRAY;
      }
      VirtualFile libraryFile =
          VirtualFileManager.getInstance().findFileByUrl(systemLibraryUrls[0]);
      if (libraryFile == null) {
        return ArrayUtil.EMPTY_STRING_ARRAY;
      }
      VirtualFile localFile = ArchiveVfsUtil.getVirtualFileForArchive(libraryFile);
      if (localFile == null) {
        return ArrayUtil.EMPTY_STRING_ARRAY;
      }
      VirtualFile docFile =
          localFile.getParent().findChild(localFile.getNameWithoutExtension() + ".xml");
      if (docFile != null) {
        return new String[] {docFile.getUrl()};
      }
      return ArrayUtil.EMPTY_STRING_ARRAY;
    } else if (orderRootType == ExternalAttributesRootOrderType.getInstance()) {
      try {
        final Ref<Couple<String>> ref = Ref.create();
        File libraryFile = getLibraryByAssemblyName(name, ref);
        if (libraryFile == null) {
          return ArrayUtil.EMPTY_STRING_ARRAY;
        }

        assert ref.get() != null;

        PluginClassLoader classLoader = (PluginClassLoader) getClass().getClassLoader();
        IdeaPluginDescriptor plugin = PluginManager.getPlugin(classLoader.getPluginId());
        assert plugin != null;
        File dir = new File(plugin.getPath(), "externalAttributes");

        val urls = new SmartList<String>();
        val requiredFileName = name + ".xml";
        FileUtil.visitFiles(
            dir,
            new Processor<File>() {
              @Override
              public boolean process(File file) {
                if (file.isDirectory()) {
                  return true;
                }
                if (Comparing.equal(requiredFileName, file.getName(), false)
                    && isValidExternalFile(ref.get().getSecond(), file)) {
                  urls.add(VfsUtil.pathToUrl(file.getPath()));
                }
                return true;
              }
            });

        return ArrayUtil.toStringArray(urls);
      } catch (Exception e) {
        LOGGER.error(e);
      }
    }
    return ArrayUtil.EMPTY_STRING_ARRAY;
  }
  public boolean processMessageLine(Callback callback) {
    if (super.processMessageLine(callback)) {
      return true;
    }
    final String line = callback.getCurrentLine();
    if (line == null) {
      return false;
    }
    if (JavacResourcesReader.MSG_PATTERNS_START.equals(line)) {
      myParserActions.clear();
      while (true) {
        final String patternLine = callback.getNextLine();
        if (JavacResourcesReader.MSG_PATTERNS_END.equals(patternLine)) {
          break;
        }
        addJavacPattern(patternLine);
      }
      return true;
    }

    int colonIndex1 = line.indexOf(':');
    if (colonIndex1 == 1) { // drive letter
      colonIndex1 = line.indexOf(':', colonIndex1 + 1);
    }

    if (colonIndex1 >= 0) { // looks like found something like file path
      @NonNls String part1 = line.substring(0, colonIndex1).trim();
      if (part1.equalsIgnoreCase("error") /*jikes*/ || part1.equalsIgnoreCase("Caused by")) {
        addMessage(callback, CompilerMessageCategory.ERROR, line.substring(colonIndex1));
        return true;
      }
      if (part1.equalsIgnoreCase("warning")) {
        addMessage(callback, CompilerMessageCategory.WARNING, line.substring(colonIndex1));
        return true;
      }
      if (part1.equals("javac")) {
        addMessage(callback, CompilerMessageCategory.ERROR, line);
        return true;
      }

      final int colonIndex2 = line.indexOf(':', colonIndex1 + 1);
      if (colonIndex2 >= 0) {
        final String filePath = part1.replace(File.separatorChar, '/');
        final Boolean fileExists =
            ApplicationManager.getApplication()
                .runReadAction(
                    new Computable<Boolean>() {
                      public Boolean compute() {
                        return LocalFileSystem.getInstance().findFileByPath(filePath) != null;
                      }
                    });
        if (!fileExists.booleanValue()) {
          // the part one turned out to be something else than a file path
          return true;
        }
        try {
          final int lineNum = Integer.parseInt(line.substring(colonIndex1 + 1, colonIndex2).trim());
          String message = line.substring(colonIndex2 + 1).trim();
          CompilerMessageCategory category = CompilerMessageCategory.ERROR;
          if (message.startsWith(WARNING_PREFIX)) {
            message = message.substring(WARNING_PREFIX.length()).trim();
            category = CompilerMessageCategory.WARNING;
          }

          List<String> messages = new ArrayList<String>();
          messages.add(message);
          int colNum;
          String prevLine = null;
          do {
            final String nextLine = callback.getNextLine();
            if (nextLine == null) {
              return false;
            }
            if (nextLine.trim().equals("^")) {
              final CharSequence chars = prevLine == null ? line : prevLine;
              final int offset = Math.max(0, Math.min(chars.length(), nextLine.indexOf('^')));
              colNum = EditorUtil.calcColumnNumber(null, chars, 0, offset, myTabSize);
              String messageEnd = callback.getNextLine();
              while (isMessageEnd(messageEnd)) {
                messages.add(messageEnd.trim());
                messageEnd = callback.getNextLine();
              }
              if (messageEnd != null) {
                callback.pushBack(messageEnd);
              }
              break;
            }
            if (prevLine != null) {
              messages.add(prevLine);
            }
            prevLine = nextLine;
          } while (true);

          if (colNum >= 0) {
            messages = convertMessages(messages);
            final StringBuilder buf = StringBuilderSpinAllocator.alloc();
            try {
              for (final String m : messages) {
                if (buf.length() > 0) {
                  buf.append("\n");
                }
                buf.append(m);
              }
              addMessage(
                  callback,
                  category,
                  buf.toString(),
                  VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, filePath),
                  lineNum,
                  colNum + 1);
            } finally {
              StringBuilderSpinAllocator.dispose(buf);
            }
            return true;
          }
        } catch (NumberFormatException ignored) {
        }
      }
    }

    if (line.endsWith("java.lang.OutOfMemoryError")) {
      addMessage(
          callback,
          CompilerMessageCategory.ERROR,
          CompilerBundle.message("error.javac.out.of.memory"));
      return true;
    }

    addMessage(callback, CompilerMessageCategory.INFORMATION, line);
    return true;
  }