com.intellij.refactoring.PackageWrapper Java Examples

The following examples show how to use com.intellij.refactoring.PackageWrapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DestinationChooser.java    From CodeMaker with Apache License 2.0 6 votes vote down vote up
static Destination chooseDestination(final PackageWrapper targetPackage,
                                     final List<? extends VirtualFile> contentSourceRoots,
                                     final PsiDirectory initialDirectory) {
    Project project = targetPackage.getManager().getProject();
    //ensure that there would be no duplicates: e.g. when one content root is subfolder of another root (configured via excluded roots)
    LinkedHashSet<PsiDirectory> targetDirectories = new LinkedHashSet<>();
    Map<PsiDirectory, String> relativePathsToCreate = new HashMap<>();
    buildDirectoryList(targetPackage, contentSourceRoots, targetDirectories, relativePathsToCreate);

    return chooseDirectory(
            targetDirectories.toArray(PsiDirectory.EMPTY_ARRAY),
            initialDirectory,
            project,
            relativePathsToCreate
    );
}
 
Example #2
Source File: HaxeMoveTest.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
public void testMoveClass() throws Exception {
  final String testHx = "pack1/Moved.hx";
  final String targetDirName = "pack2";
  doTest((rootDir, rootAfter) -> {
    final VirtualFile src = VfsUtil.findRelativeFile(testHx, rootDir);
    assertNotNull("Class pack1.Moved not found", src);


    PsiElement file = myPsiManager.findFile(src);
    assertNotNull("Psi for " + testHx + " not found", file);
    PsiElement cls = file.getNode().getPsi(HaxeFile.class).findChildByClass(HaxeClassDeclaration.class);

    PackageWrapper pack = new PackageWrapper(myPsiManager, targetDirName);
    VirtualFile targetDir = VfsUtil.findRelativeFile(targetDirName, rootDir);
    PsiDirectoryImpl dir = new PsiDirectoryImpl(myPsiManager, targetDir);

    ArrayList<PsiElement> list = new ArrayList<>();
    list.add(cls);
    new MoveClassesOrPackagesProcessor(myProject, PsiUtilCore.toPsiElementArray(list),
                                       new SingleSourceRootMoveDestination(pack, dir),
                                       true, true, null).run();
    FileDocumentManager.getInstance().saveAllDocuments();
  });
}
 
Example #3
Source File: CodeMakerAction.java    From CodeMaker with Apache License 2.0 5 votes vote down vote up
/**
 * allow user to select the generated code source root
 */
private DestinationChooser.Destination chooseDestination(ClassEntry classEntry, Project project, PsiElement psiElement) {
    String packageName = classEntry.getPackageName();
    final PackageWrapper targetPackage = new PackageWrapper(PsiManager.getInstance(project), packageName);
    List<VirtualFile> suitableRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
    return DestinationChooser.chooseDestination(targetPackage, suitableRoots,
            psiElement.getContainingFile().getContainingDirectory());
}
 
Example #4
Source File: JavaExtractSuperBaseDialog.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Override
protected void preparePackage() throws OperationFailedException {
  final String targetPackageName = getTargetPackageName();
  final PsiFile containingFile = mySourceClass.getContainingFile();
  final boolean fromDefaultPackage = containingFile instanceof PsiClassOwner && ((PsiClassOwner)containingFile).getPackageName().isEmpty(); 
  if (!(fromDefaultPackage && StringUtil.isEmpty(targetPackageName)) && !PsiNameHelper.getInstance(myProject).isQualifiedName(targetPackageName)) {
    throw new OperationFailedException("Invalid package name: " + targetPackageName);
  }
  final PsiPackage aPackage = JavaPsiFacade.getInstance(myProject).findPackage(targetPackageName);
  if (aPackage != null) {
    final PsiDirectory[] directories = aPackage.getDirectories(mySourceClass.getResolveScope());
    if (directories.length >= 1) {
      myTargetDirectory = getDirUnderSameSourceRoot(directories);
    }
  }
  
  final MoveDestination moveDestination =
    myDestinationFolderComboBox.selectDirectory(new PackageWrapper(PsiManager.getInstance(myProject), targetPackageName), false);
  if (moveDestination == null) return;

  myTargetDirectory = myTargetDirectory != null ? ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
    @Override
    public PsiDirectory compute() {
      return moveDestination.getTargetDirectory(myTargetDirectory);
    }
  }) : null;

  if (myTargetDirectory == null) {
    throw new OperationFailedException(""); // message already reported by PackageUtil
  }
  String error = RefactoringMessageUtil.checkCanCreateClass(myTargetDirectory, getExtractedSuperName());
  if (error != null) {
    throw new OperationFailedException(error);
  }
}
 
Example #5
Source File: HaxeMoveTest.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
private void doTest(final String[] toMove, final String targetDirName) throws Exception {
  doTest(new PerformAction() {
    @Override
    public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception {
      Collection<PsiElement> files = new ArrayList<PsiElement>();
      for (String s : toMove) {
        final VirtualFile child = VfsUtil.findRelativeFile(s, rootDir);
        assertNotNull("Neither class nor file " + s + " not found", child);
        PsiElement file = myPsiManager.findFile(child);
        if (file == null) file = JavaPsiFacade.getInstance(myProject).findPackage(s);
        files.add(file);
      }
      final VirtualFile child1 = VfsUtil.findRelativeFile(targetDirName, rootDir);
      assertNotNull("Target dir " + targetDirName + " not found", child1);
      final PsiDirectory targetDirectory = myPsiManager.findDirectory(child1);
      assertNotNull(targetDirectory);

      if (files.iterator().next() instanceof PsiFile) {
        new MoveFilesOrDirectoriesProcessor(myProject, PsiUtilCore.toPsiElementArray(files), targetDirectory,
                                            false, false, null, null).run();
      }
      else if (files.iterator().next() instanceof PsiPackage) {
        PsiPackage newParentPackage = JavaPsiFacade.getInstance(myPsiManager.getProject()).findPackage(targetDirName);
        assertNotNull(newParentPackage);
        final PsiDirectory[] dirs = newParentPackage.getDirectories();
        assertEquals(dirs.length, 1);

        new MoveClassesOrPackagesProcessor(myProject, PsiUtilCore.toPsiElementArray(files),
                                           new SingleSourceRootMoveDestination(PackageWrapper.create(newParentPackage),
                                                                               newParentPackage.getDirectories()[0]),
                                           true, true, null).run();
      }
      FileDocumentManager.getInstance().saveAllDocuments();
    }
  });
}