Search in sources :

Example 1 with KotlinRefactoringSettings

use of org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringSettings in project kotlin by JetBrains.

the class MoveKotlinNestedClassesToUpperLevelDialog method init.

@Override
protected void init() {
    classNameField.setText(innerClass.getName());
    classNameField.selectAll();
    if (innerClass instanceof KtClass && ((KtClass) innerClass).isInner()) {
        passOuterClassCheckBox.setSelected(true);
        passOuterClassCheckBox.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                parameterField.setEnabled(passOuterClassCheckBox.isSelected());
            }
        });
    } else {
        passOuterClassCheckBox.setSelected(false);
        passOuterClassCheckBox.setEnabled(false);
        parameterField.setEnabled(false);
    }
    if (passOuterClassCheckBox.isEnabled()) {
        boolean thisNeeded = isThisNeeded();
        passOuterClassCheckBox.setSelected(thisNeeded);
        parameterField.setEnabled(thisNeeded);
    }
    passOuterClassCheckBox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            boolean selected = passOuterClassCheckBox.isSelected();
            parameterField.getComponent().setEnabled(selected);
        }
    });
    if (!(targetContainer instanceof PsiDirectory)) {
        packageNameField.setVisible(false);
        packageNameLabel.setVisible(false);
    }
    if (innerClass instanceof KtClass && ((KtClass) innerClass).isInner()) {
        KtClassBody innerClassBody = innerClass.getBody();
        Function1<String, Boolean> validator = innerClassBody != null ? new NewDeclarationNameValidator(innerClassBody, (PsiElement) null, NewDeclarationNameValidator.Target.VARIABLES, Collections.<KtDeclaration>emptyList()) : new CollectingNameValidator();
        List<String> suggestions = KotlinNameSuggester.INSTANCE.suggestNamesByType(getOuterInstanceType(), validator, "outer");
        parameterField.setSuggestions(ArrayUtil.toStringArray(suggestions));
    } else {
        parameterField.getComponent().setEnabled(false);
    }
    FqName packageFqName = getTargetPackageFqName();
    if (packageFqName != null) {
        packageNameField.prependItem(packageFqName.asString());
    }
    KotlinRefactoringSettings settings = KotlinRefactoringSettings.getInstance();
    searchForTextOccurrencesCheckBox.setSelected(settings.MOVE_TO_UPPER_LEVEL_SEARCH_FOR_TEXT);
    searchInCommentsCheckBox.setSelected(settings.MOVE_TO_UPPER_LEVEL_SEARCH_IN_COMMENTS);
    super.init();
}
Also used : ItemEvent(java.awt.event.ItemEvent) FqName(org.jetbrains.kotlin.name.FqName) CollectingNameValidator(org.jetbrains.kotlin.idea.core.CollectingNameValidator) NewDeclarationNameValidator(org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator) KotlinRefactoringSettings(org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringSettings) ItemListener(java.awt.event.ItemListener)

Example 2 with KotlinRefactoringSettings

use of org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringSettings in project kotlin by JetBrains.

the class MoveKotlinNestedClassesToUpperLevelDialog method doAction.

@Override
protected void doAction() {
    PsiElement target;
    try {
        target = getTargetContainerWithValidation();
        if (target == null)
            return;
    } catch (ConfigurationException e) {
        CommonRefactoringUtil.showErrorMessage(MoveInnerImpl.REFACTORING_NAME, e.getMessage(), HelpID.MOVE_INNER_UPPER, project);
        return;
    }
    KotlinRefactoringSettings settings = KotlinRefactoringSettings.getInstance();
    settings.MOVE_TO_UPPER_LEVEL_SEARCH_FOR_TEXT = searchForTextOccurrencesCheckBox.isSelected();
    settings.MOVE_TO_UPPER_LEVEL_SEARCH_IN_COMMENTS = searchInCommentsCheckBox.isSelected();
    KotlinMoveTarget moveTarget;
    if (target instanceof PsiDirectory) {
        final PsiDirectory targetDir = (PsiDirectory) target;
        final FqName targetPackageFqName = getTargetPackageFqName();
        if (targetPackageFqName == null)
            return;
        String innerClassName = innerClass.getName();
        if (innerClassName == null)
            return;
        final String targetFileName = KotlinNameSuggester.INSTANCE.suggestNameByName(innerClassName, new Function1<String, Boolean>() {

            @Override
            public Boolean invoke(String s) {
                return targetDir.findFile(s + "." + KotlinFileType.EXTENSION) == null;
            }
        }) + "." + KotlinFileType.EXTENSION;
        moveTarget = new KotlinMoveTargetForDeferredFile(targetPackageFqName, targetDir, null, new Function1<KtFile, KtFile>() {

            @Override
            public KtFile invoke(@NotNull KtFile originalFile) {
                return KotlinRefactoringUtilKt.createKotlinFile(targetFileName, targetDir, targetPackageFqName.asString());
            }
        });
    } else {
        //noinspection ConstantConditions
        moveTarget = new KotlinMoveTargetForExistingElement((KtElement) target);
    }
    String outerInstanceParameterName = passOuterClassCheckBox.isSelected() ? getParameterName() : null;
    String newClassName = getClassName();
    MoveDeclarationsDelegate delegate = new MoveDeclarationsDelegate.NestedClass(newClassName, outerInstanceParameterName);
    MoveDeclarationsDescriptor moveDescriptor = new MoveDeclarationsDescriptor(CollectionsKt.listOf(innerClass), moveTarget, delegate, isSearchInComments(), isSearchInNonJavaFiles(), true, false, null, isOpenInEditor());
    saveOpenInEditorOption();
    invokeRefactoring(new MoveKotlinDeclarationsProcessor(project, moveDescriptor, Mover.Default.INSTANCE));
}
Also used : FqName(org.jetbrains.kotlin.name.FqName) Function1(kotlin.jvm.functions.Function1) NotNull(org.jetbrains.annotations.NotNull) ConfigurationException(com.intellij.openapi.options.ConfigurationException) KotlinRefactoringSettings(org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringSettings)

Aggregations

KotlinRefactoringSettings (org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringSettings)2 FqName (org.jetbrains.kotlin.name.FqName)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 Function1 (kotlin.jvm.functions.Function1)1 NotNull (org.jetbrains.annotations.NotNull)1 CollectingNameValidator (org.jetbrains.kotlin.idea.core.CollectingNameValidator)1 NewDeclarationNameValidator (org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator)1