Move: Suggest target file name based on selected declarations

#KT-8227 Fixed
This commit is contained in:
Alexey Sedunov
2015-06-23 16:50:24 +03:00
parent 6055933258
commit c4442c59d4
2 changed files with 10 additions and 12 deletions
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.idea.refactoring.move.*
import org.jetbrains.kotlin.idea.refactoring.move.moveTopLevelDeclarations.ui.MoveKotlinTopLevelDeclarationsDialog
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.idea.core.getPackage
import org.jetbrains.kotlin.idea.refactoring.move.guessNewFileName
import java.util.HashSet
import java.util.LinkedHashSet
@@ -64,13 +65,7 @@ public class MoveKotlinTopLevelDeclarationsHandler : MoveHandlerDelegate() {
val targetDirectory = MoveClassesOrPackagesImpl.getInitialTargetDirectory(targetContainer, elements)
val searchInComments = JavaRefactoringSettings.getInstance()!!.MOVE_SEARCH_IN_COMMENTS
val searchInText = JavaRefactoringSettings.getInstance()!!.MOVE_SEARCH_FOR_TEXT
val targetFile: JetFile? = when (targetContainer) {
is JetFile -> targetContainer
else -> {
val files = elements.mapTo(HashSet<JetFile>()) { e -> e.getContainingFile() as JetFile }
if (files.size() == 1) files.first() else null
}
}
val targetFile = targetContainer as? JetFile
val moveToPackage = targetContainer !is JetFile
MoveKotlinTopLevelDeclarationsDialog(
@@ -142,7 +142,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
initPackageChooser(targetPackageName, targetDirectory);
initFileChooser(targetFile);
initFileChooser(targetFile, elementsToMove);
initMoveToButtons(moveToPackage);
@@ -278,16 +278,19 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
);
}
private void initFileChooser(JetFile targetFile) {
private void initFileChooser(@Nullable JetFile targetFile, @NotNull Set<JetNamedDeclaration> elementsToMove) {
FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor()
.withRoots(ProjectRootManager.getInstance(myProject).getContentRoots())
.withTreeRootVisible(true);
String title = JetRefactoringBundle.message("refactoring.move.top.level.declaration.file.title");
fileChooser.addBrowseFolderListener(title, null, myProject, descriptor, TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT);
if (targetFile != null) {
fileChooser.setText(targetFile.getVirtualFile().getPath());
}
String initialTargetPath =
targetFile != null
? targetFile.getVirtualFile().getPath()
: sourceFile.getVirtualFile().getParent().getPath() + "/" + MovePackage.guessNewFileName(sourceFile, elementsToMove);
fileChooser.setText(initialTargetPath);
}
private void createUIComponents() {