コード例 #1
0
    public MkDirs(@NotNull String newName, @NotNull PsiDirectory directory) {
      if (SystemInfo.isWindows) {
        newName = newName.replace('\\', '/');
      }
      if (newName.contains("/")) {
        final List<String> subDirs = StringUtil.split(newName, "/");
        newName = subDirs.remove(subDirs.size() - 1);
        boolean firstToken = true;
        for (String dir : subDirs) {
          if (firstToken && "~".equals(dir)) {
            final VirtualFile userHomeDir = VfsUtil.getUserHomeDir();
            if (userHomeDir == null)
              throw new IncorrectOperationException("User home directory not found");
            final PsiDirectory directory1 = directory.getManager().findDirectory(userHomeDir);
            if (directory1 == null)
              throw new IncorrectOperationException("User home directory not found");
            directory = directory1;
          } else if ("..".equals(dir)) {
            final PsiDirectory parentDirectory = directory.getParentDirectory();
            if (parentDirectory == null)
              throw new IncorrectOperationException("Not a valid directory");
            directory = parentDirectory;
          } else if (!".".equals(dir)) {
            final PsiDirectory sub = directory.findSubdirectory(dir);
            directory = sub == null ? directory.createSubdirectory(dir) : sub;
          }
          firstToken = false;
        }
      }

      this.newName = newName;
      this.directory = directory;
    }
コード例 #2
0
 private void doCreate(final PsiDirectory baseDirectory, PsiElement myContext) {
   final PsiManager manager = baseDirectory.getManager();
   PsiDirectory directory = baseDirectory;
   String lastName;
   for (StringTokenizer st = new StringTokenizer(myRedPart, "."); ; ) {
     lastName = st.nextToken();
     if (st.hasMoreTokens()) {
       try {
         final PsiDirectory subdirectory = directory.findSubdirectory(lastName);
         directory = subdirectory != null ? subdirectory : directory.createSubdirectory(lastName);
       } catch (IncorrectOperationException e) {
         CreateFromUsageUtils.scheduleFileOrPackageCreationFailedMessageBox(
             e, lastName, directory, true);
         return;
       }
     } else {
       break;
     }
   }
   if (myClassKind != null) {
     PsiClass createdClass;
     if (myTemplateName != null) {
       createdClass =
           CreateClassUtil.createClassFromCustomTemplate(
               directory, null, lastName, myTemplateName);
     } else {
       createdClass =
           CreateFromUsageUtils.createClass(
               myClassKind == ClassKind.INTERFACE
                   ? CreateClassKind.INTERFACE
                   : CreateClassKind.CLASS,
               directory,
               lastName,
               manager,
               myContext,
               null,
               mySuperClass);
     }
     if (createdClass != null) {
       createdClass.navigate(true);
     }
   } else {
     try {
       directory.createSubdirectory(lastName);
     } catch (IncorrectOperationException e) {
       CreateFromUsageUtils.scheduleFileOrPackageCreationFailedMessageBox(
           e, lastName, directory, true);
     }
   }
 }
コード例 #3
0
  public static String getRootDirectoryForPackage(PsiDirectory directory) {
    PsiManager manager = directory.getManager();
    final VirtualFile virtualFile = directory.getVirtualFile();
    final ProjectFileIndex fileIndex =
        ProjectRootManager.getInstance(manager.getProject()).getFileIndex();
    VirtualFile root = fileIndex.getSourceRootForFile(virtualFile);

    if (root == null) {
      root = fileIndex.getClassRootForFile(virtualFile);
    }
    if (root != null) {
      return root.getPresentableUrl();
    }
    return null;
  }
コード例 #4
0
  public static void checkCreateClassOrInterface(@NotNull PsiDirectory directory, String name)
      throws IncorrectOperationException {
    PsiUtil.checkIsIdentifier(directory.getManager(), name);

    String fileName = name + "." + StdFileTypes.JAVA.getDefaultExtension();
    directory.checkCreateFile(fileName);

    PsiNameHelper helper = JavaPsiFacade.getInstance(directory.getProject()).getNameHelper();
    PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(directory);
    String qualifiedName = aPackage == null ? null : aPackage.getQualifiedName();
    if (!StringUtil.isEmpty(qualifiedName) && !helper.isQualifiedName(qualifiedName)) {
      throw new IncorrectOperationException(
          "Cannot create class in invalid package: '" + qualifiedName + "'");
    }
  }