Search in sources :

Example 1 with KtNamedDeclaration

use of org.jetbrains.kotlin.psi.KtNamedDeclaration in project kotlin by JetBrains.

the class MoveKotlinTopLevelDeclarationsDialog method doAction.

@Override
protected void doAction() {
    KotlinMoveTarget target = selectMoveTarget();
    if (target == null)
        return;
    saveRefactoringSettings();
    List<KtNamedDeclaration> elementsToMove = getSelectedElementsToMove();
    List<KtFile> sourceFiles = getSourceFiles(elementsToMove);
    final PsiDirectory sourceDirectory = getSourceDirectory(sourceFiles);
    for (PsiElement element : elementsToMove) {
        String message = target.verify(element.getContainingFile());
        if (message != null) {
            CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), message, null, myProject);
            return;
        }
    }
    try {
        boolean deleteSourceFile = false;
        if (isFullFileMove()) {
            if (isMoveToPackage()) {
                Pair<VirtualFile, ? extends MoveDestination> sourceRootWithMoveDestination = selectPackageBasedTargetDirAndDestination(false);
                //noinspection ConstantConditions
                final MoveDestination moveDestination = sourceRootWithMoveDestination.getSecond();
                PsiDirectory targetDir = moveDestination.getTargetIfExists(sourceDirectory);
                String targetFileName = sourceFiles.size() > 1 ? null : tfFileNameInPackage.getText();
                List<PsiFile> filesExistingInTargetDir = getFilesExistingInTargetDir(sourceFiles, targetFileName, targetDir);
                if (filesExistingInTargetDir.isEmpty()) {
                    PsiDirectory targetDirectory = ApplicationUtilsKt.runWriteAction(new Function0<PsiDirectory>() {

                        @Override
                        public PsiDirectory invoke() {
                            return moveDestination.getTargetDirectory(sourceDirectory);
                        }
                    });
                    for (KtFile sourceFile : sourceFiles) {
                        MoveUtilsKt.setUpdatePackageDirective(sourceFile, cbUpdatePackageDirective.isSelected());
                    }
                    invokeRefactoring(new MoveFilesWithDeclarationsProcessor(myProject, sourceFiles, targetDirectory, targetFileName, isSearchInComments(), isSearchInNonJavaFiles(), moveCallback));
                    return;
                }
            }
            int ret = Messages.showYesNoCancelDialog(myProject, "You are about to move all declarations out of the source file(s). Do you want to delete empty files?", RefactoringBundle.message("move.title"), Messages.getQuestionIcon());
            if (ret == Messages.CANCEL)
                return;
            deleteSourceFile = ret == Messages.YES;
        }
        MoveDeclarationsDescriptor options = new MoveDeclarationsDescriptor(elementsToMove, target, MoveDeclarationsDelegate.TopLevel.INSTANCE, isSearchInComments(), isSearchInNonJavaFiles(), true, deleteSourceFile, moveCallback, false);
        invokeRefactoring(new MoveKotlinDeclarationsProcessor(myProject, options, Mover.Default.INSTANCE));
    } catch (IncorrectOperationException e) {
        CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.getMessage(), null, myProject);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MoveDestination(com.intellij.refactoring.MoveDestination) AutocreatingSingleSourceRootMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination) MultipleRootsMoveDestination(com.intellij.refactoring.move.moveClassesOrPackages.MultipleRootsMoveDestination) KtNamedDeclaration(org.jetbrains.kotlin.psi.KtNamedDeclaration) KtFile(org.jetbrains.kotlin.psi.KtFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 2 with KtNamedDeclaration

use of org.jetbrains.kotlin.psi.KtNamedDeclaration in project kotlin by JetBrains.

the class CFGraphToDotFilePrinter method dumpDot.

public static void dumpDot(File file, Collection<Pseudocode> pseudocodes) throws FileNotFoundException {
    File target = KotlinTestUtils.replaceExtension(file, "dot");
    PrintStream out = new PrintStream(target);
    out.println("digraph " + FileUtil.getNameWithoutExtension(file) + " {");
    int[] count = new int[1];
    Map<Instruction, String> nodeToName = new HashMap<Instruction, String>();
    for (Pseudocode pseudocode : pseudocodes) {
        dumpNodes(pseudocode.getInstructionsIncludingDeadCode(), out, count, nodeToName, Sets.newHashSet(pseudocode.getInstructions()));
    }
    int i = 0;
    for (Pseudocode pseudocode : pseudocodes) {
        String label;
        KtElement correspondingElement = pseudocode.getCorrespondingElement();
        if (correspondingElement instanceof KtNamedDeclaration) {
            KtNamedDeclaration namedDeclaration = (KtNamedDeclaration) correspondingElement;
            label = namedDeclaration.getName();
        } else {
            label = "anonymous_" + i;
        }
        out.println("subgraph cluster_" + i + " {\n" + "label=\"" + label + "\";\n" + "color=blue;\n");
        dumpEdges(pseudocode.getInstructionsIncludingDeadCode(), out, count, nodeToName);
        out.println("}");
        i++;
    }
    out.println("}");
    out.close();
}
Also used : PrintStream(java.io.PrintStream) KtElement(org.jetbrains.kotlin.psi.KtElement) KtNamedDeclaration(org.jetbrains.kotlin.psi.KtNamedDeclaration) Pseudocode(org.jetbrains.kotlin.cfg.pseudocode.Pseudocode) Instruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction) SubroutineSinkInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineSinkInstruction) SubroutineExitInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction) SubroutineEnterInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineEnterInstruction) MagicInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MagicInstruction) LocalFunctionDeclarationInstruction(org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction) File(java.io.File)

Example 3 with KtNamedDeclaration

use of org.jetbrains.kotlin.psi.KtNamedDeclaration in project kotlin by JetBrains.

the class MoveKotlinTopLevelDeclarationsDialog method initMemberInfo.

private void initMemberInfo(@NotNull final Set<KtNamedDeclaration> elementsToMove, @NotNull List<KtFile> sourceFiles) {
    final List<KotlinMemberInfo> memberInfos = CollectionsKt.map(getAllDeclarations(sourceFiles), new Function1<KtNamedDeclaration, KotlinMemberInfo>() {

        @Override
        public KotlinMemberInfo invoke(KtNamedDeclaration declaration) {
            KotlinMemberInfo memberInfo = new KotlinMemberInfo(declaration, false);
            memberInfo.setChecked(elementsToMove.contains(declaration));
            return memberInfo;
        }
    });
    KotlinMemberSelectionPanel selectionPanel = new KotlinMemberSelectionPanel(getTitle(), memberInfos, null);
    memberTable = selectionPanel.getTable();
    MemberInfoModelImpl memberInfoModel = new MemberInfoModelImpl();
    memberInfoModel.memberInfoChanged(new MemberInfoChange<KtNamedDeclaration, KotlinMemberInfo>(memberInfos));
    selectionPanel.getTable().setMemberInfoModel(memberInfoModel);
    selectionPanel.getTable().addMemberInfoChangeListener(memberInfoModel);
    selectionPanel.getTable().addMemberInfoChangeListener(new MemberInfoChangeListener<KtNamedDeclaration, KotlinMemberInfo>() {

        private boolean shouldUpdateFileNameField(final Collection<KotlinMemberInfo> changedMembers) {
            if (!tfFileNameInPackage.isEnabled())
                return true;
            Collection<KtNamedDeclaration> previousDeclarations = CollectionsKt.filterNotNull(CollectionsKt.map(memberInfos, new Function1<KotlinMemberInfo, KtNamedDeclaration>() {

                @Override
                public KtNamedDeclaration invoke(KotlinMemberInfo info) {
                    return changedMembers.contains(info) != info.isChecked() ? info.getMember() : null;
                }
            }));
            String suggestedText = previousDeclarations.isEmpty() ? "" : MoveUtilsKt.guessNewFileName(previousDeclarations);
            return tfFileNameInPackage.getText().equals(suggestedText);
        }

        @Override
        public void memberInfoChanged(MemberInfoChange<KtNamedDeclaration, KotlinMemberInfo> event) {
            updatePackageDirectiveCheckBox();
            updateFileNameInPackageField();
            // Update file name field only if it user hasn't changed it to some non-default value
            if (shouldUpdateFileNameField(event.getChangedMembers())) {
                updateSuggestedFileName();
            }
        }
    });
    memberInfoPanel.add(selectionPanel, BorderLayout.CENTER);
}
Also used : KotlinMemberInfo(org.jetbrains.kotlin.idea.refactoring.memberInfo.KotlinMemberInfo) KotlinMemberSelectionPanel(org.jetbrains.kotlin.idea.refactoring.memberInfo.KotlinMemberSelectionPanel) KtNamedDeclaration(org.jetbrains.kotlin.psi.KtNamedDeclaration)

Example 4 with KtNamedDeclaration

use of org.jetbrains.kotlin.psi.KtNamedDeclaration in project kotlin by JetBrains.

the class KotlinFindPropertyUsagesDialog method addUsagesOptions.

@Override
protected void addUsagesOptions(JPanel optionsPanel) {
    super.addUsagesOptions(optionsPanel);
    KtNamedDeclaration property = (KtNamedDeclaration) getPsiElement();
    boolean isAbstract = property.hasModifier(KtTokens.ABSTRACT_KEYWORD);
    boolean isOpen = property.hasModifier(KtTokens.OPEN_KEYWORD);
    if (isOpen || isAbstract) {
        cbOverrides = addCheckboxToPanel(isAbstract ? KotlinBundle.message("find.what.implementing.properties.checkbox") : KotlinBundle.message("find.what.overriding.properties.checkbox"), FindSettings.getInstance().isSearchOverloadedMethods(), optionsPanel, false);
    }
}
Also used : KtNamedDeclaration(org.jetbrains.kotlin.psi.KtNamedDeclaration)

Aggregations

KtNamedDeclaration (org.jetbrains.kotlin.psi.KtNamedDeclaration)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 MoveDestination (com.intellij.refactoring.MoveDestination)1 AutocreatingSingleSourceRootMoveDestination (com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination)1 MultipleRootsMoveDestination (com.intellij.refactoring.move.moveClassesOrPackages.MultipleRootsMoveDestination)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 File (java.io.File)1 PrintStream (java.io.PrintStream)1 Pseudocode (org.jetbrains.kotlin.cfg.pseudocode.Pseudocode)1 Instruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction)1 MagicInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MagicInstruction)1 LocalFunctionDeclarationInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.LocalFunctionDeclarationInstruction)1 SubroutineEnterInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineEnterInstruction)1 SubroutineExitInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction)1 SubroutineSinkInstruction (org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineSinkInstruction)1 KotlinMemberInfo (org.jetbrains.kotlin.idea.refactoring.memberInfo.KotlinMemberInfo)1 KotlinMemberSelectionPanel (org.jetbrains.kotlin.idea.refactoring.memberInfo.KotlinMemberSelectionPanel)1 KtElement (org.jetbrains.kotlin.psi.KtElement)1 KtFile (org.jetbrains.kotlin.psi.KtFile)1