use of com.intellij.refactoring.MoveDestination in project intellij-community by JetBrains.
the class JavaIntroduceParameterObjectDelegate method collectConflicts.
@Override
public void collectConflicts(MultiMap<PsiElement, String> conflicts, UsageInfo[] infos, PsiMethod method, JavaIntroduceParameterObjectClassDescriptor classDescriptor) {
final MoveDestination moveDestination = classDescriptor.getMoveDestination();
if (moveDestination != null) {
if (!moveDestination.isTargetAccessible(method.getProject(), method.getContainingFile().getVirtualFile())) {
conflicts.putValue(method, "Created class won't be accessible");
}
if (!classDescriptor.isCreateInnerClass() && !classDescriptor.isUseExistingClass()) {
final PsiFile containingFile = method.getContainingFile();
final PsiDirectory containingDirectory = containingFile.getContainingDirectory();
PsiDirectory directory = moveDestination.getTargetDirectory(containingDirectory);
if (directory != null) {
PsiFile file = directory.findFile(classDescriptor.getClassName() + ".java");
if (file != null) {
VirtualFile virtualFile = PsiUtilCore.getVirtualFile(file);
if (virtualFile != null) {
conflicts.putValue(method, "File already exits: " + virtualFile.getPresentableUrl());
}
}
}
}
}
if (moveDestination != null) {
boolean constructorMiss = false;
for (UsageInfo info : infos) {
if (info instanceof IntroduceParameterObjectProcessor.ChangeSignatureUsageWrapper) {
final UsageInfo usageInfo = ((IntroduceParameterObjectProcessor.ChangeSignatureUsageWrapper) info).getInfo();
if (usageInfo instanceof OverriderMethodUsageInfo) {
final PsiElement overridingMethod = ((OverriderMethodUsageInfo) usageInfo).getOverridingMethod();
if (!moveDestination.isTargetAccessible(overridingMethod.getProject(), overridingMethod.getContainingFile().getVirtualFile())) {
conflicts.putValue(overridingMethod, "Created class won't be accessible");
}
}
if (!constructorMiss && classDescriptor.isUseExistingClass() && usageInfo instanceof MethodCallUsageInfo && classDescriptor.getExistingClassCompatibleConstructor() == null) {
conflicts.putValue(classDescriptor.getExistingClass(), "Existing class misses compatible constructor");
constructorMiss = true;
}
}
}
}
if (classDescriptor.isUseExistingClass()) {
for (ParameterInfoImpl info : classDescriptor.getParamsToMerge()) {
Object existingClassBean = classDescriptor.getBean(info);
if (existingClassBean == null) {
conflicts.putValue(classDescriptor.getExistingClass(), "No field associated with " + info.getName() + " found");
}
}
}
}
use of com.intellij.refactoring.MoveDestination in project kotlin by JetBrains.
the class MoveKotlinTopLevelDeclarationsDialog method selectMoveTarget.
@Nullable
private KotlinMoveTarget selectMoveTarget() {
String message = verifyBeforeRun();
if (message != null) {
setErrorText(message);
return null;
}
setErrorText(null);
List<KtFile> sourceFiles = getSourceFiles(getSelectedElementsToMove());
PsiDirectory sourceDirectory = getSourceDirectory(sourceFiles);
if (isMoveToPackage()) {
Pair<VirtualFile, ? extends MoveDestination> targetDirWithMoveDestination = selectPackageBasedTargetDirAndDestination(true);
if (targetDirWithMoveDestination == null)
return null;
VirtualFile targetDir = targetDirWithMoveDestination.getFirst();
final MoveDestination moveDestination = targetDirWithMoveDestination.getSecond();
final String targetFileName = sourceFiles.size() > 1 ? null : tfFileNameInPackage.getText();
if (targetFileName != null && !checkTargetFileName(targetFileName))
return null;
PsiDirectory targetDirectory = moveDestination.getTargetIfExists(sourceDirectory);
List<PsiFile> filesExistingInTargetDir = getFilesExistingInTargetDir(sourceFiles, targetFileName, targetDirectory);
if (!filesExistingInTargetDir.isEmpty()) {
if (!CollectionsKt.intersect(sourceFiles, filesExistingInTargetDir).isEmpty()) {
setErrorText("Can't move to the original file(s)");
return null;
}
if (filesExistingInTargetDir.size() > 1) {
String filePathsToReport = StringUtil.join(filesExistingInTargetDir, new Function<PsiFile, String>() {
@Override
public String fun(PsiFile file) {
return file.getVirtualFile().getPath();
}
}, "\n");
Messages.showErrorDialog(myProject, "Cannot perform refactoring since the following files already exist:\n\n" + filePathsToReport, RefactoringBundle.message("move.title"));
return null;
}
String question = String.format("File '%s' already exists. Do you want to move selected declarations to this file?", filesExistingInTargetDir.get(0).getVirtualFile().getPath());
int ret = Messages.showYesNoDialog(myProject, question, RefactoringBundle.message("move.title"), Messages.getQuestionIcon());
if (ret != Messages.YES)
return null;
}
// All source files must be in the same directory
return new KotlinMoveTargetForDeferredFile(new FqName(getTargetPackage()), moveDestination.getTargetIfExists(sourceFiles.get(0)), targetDir, new Function1<KtFile, KtFile>() {
@Override
public KtFile invoke(@NotNull KtFile originalFile) {
return KotlinRefactoringUtilKt.getOrCreateKotlinFile(targetFileName != null ? targetFileName : originalFile.getName(), moveDestination.getTargetDirectory(originalFile));
}
});
}
final File targetFile = new File(getTargetFilePath());
if (!checkTargetFileName(targetFile.getName()))
return null;
KtFile jetFile = (KtFile) KotlinRefactoringUtilKt.toPsiFile(targetFile, myProject);
if (jetFile != null) {
if (sourceFiles.size() == 1 && sourceFiles.contains(jetFile)) {
setErrorText("Can't move to the original file");
return null;
}
return new KotlinMoveTargetForExistingElement(jetFile);
}
File targetDir = targetFile.getParentFile();
final PsiDirectory psiDirectory = targetDir != null ? KotlinRefactoringUtilKt.toPsiDirectory(targetDir, myProject) : null;
if (psiDirectory == null) {
setErrorText("No directory found for file: " + targetFile.getPath());
return null;
}
Set<FqName> sourcePackageFqNames = CollectionsKt.mapTo(sourceFiles, new LinkedHashSet<FqName>(), new Function1<KtFile, FqName>() {
@Override
public FqName invoke(KtFile file) {
return file.getPackageFqName();
}
});
FqName targetPackageFqName = CollectionsKt.singleOrNull(sourcePackageFqNames);
if (targetPackageFqName == null) {
PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
if (psiPackage == null) {
setErrorText("Could not find package corresponding to " + targetDir.getPath());
return null;
}
targetPackageFqName = new FqName(psiPackage.getQualifiedName());
}
final String finalTargetPackageFqName = targetPackageFqName.asString();
return new KotlinMoveTargetForDeferredFile(targetPackageFqName, psiDirectory, null, new Function1<KtFile, KtFile>() {
@Override
public KtFile invoke(@NotNull KtFile originalFile) {
return KotlinRefactoringUtilKt.getOrCreateKotlinFile(targetFile.getName(), psiDirectory, finalTargetPackageFqName);
}
});
}
use of com.intellij.refactoring.MoveDestination in project intellij-plugins by JetBrains.
the class ActivatorRenameTest method renamePackage.
private void renamePackage() {
VirtualFile sourceRoot = ModuleRootManager.getInstance(myModule).getSourceRoots()[0];
JavaRefactoringFactory factory = JavaRefactoringFactory.getInstance(getProject());
MoveDestination moveDestination = factory.createSourceRootMoveDestination("tx", sourceRoot);
factory.createMoveClassesOrPackages(new PsiElement[] { myActivator }, moveDestination).run();
}
Aggregations