Search in sources :

Example 6 with NonCodeUsageInfo

use of com.intellij.refactoring.util.NonCodeUsageInfo in project intellij-community by JetBrains.

the class RenameProcessor method performRefactoring.

@Override
public void performRefactoring(@NotNull UsageInfo[] usages) {
    List<Runnable> postRenameCallbacks = new ArrayList<>();
    final MultiMap<PsiElement, UsageInfo> classified = classifyUsages(myAllRenames.keySet(), usages);
    for (final PsiElement element : myAllRenames.keySet()) {
        String newName = myAllRenames.get(element);
        final RefactoringElementListener elementListener = getTransaction().getElementListener(element);
        final RenamePsiElementProcessor renamePsiElementProcessor = RenamePsiElementProcessor.forElement(element);
        Runnable postRenameCallback = renamePsiElementProcessor.getPostRenameCallback(element, newName, elementListener);
        final Collection<UsageInfo> infos = classified.get(element);
        try {
            RenameUtil.doRename(element, newName, infos.toArray(new UsageInfo[infos.size()]), myProject, elementListener);
        } catch (final IncorrectOperationException e) {
            RenameUtil.showErrorMessage(e, element, myProject);
            return;
        }
        if (postRenameCallback != null) {
            postRenameCallbacks.add(postRenameCallback);
        }
    }
    for (Runnable runnable : postRenameCallbacks) {
        runnable.run();
    }
    List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
    for (UsageInfo usage : usages) {
        if (usage instanceof NonCodeUsageInfo) {
            nonCodeUsages.add((NonCodeUsageInfo) usage);
        }
    }
    myNonCodeUsages = nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
    if (!mySkippedUsages.isEmpty()) {
        if (!ApplicationManager.getApplication().isUnitTestMode() && !ApplicationManager.getApplication().isHeadlessEnvironment()) {
            ApplicationManager.getApplication().invokeLater(() -> {
                final IdeFrame ideFrame = WindowManager.getInstance().getIdeFrame(myProject);
                if (ideFrame != null) {
                    StatusBarEx statusBar = (StatusBarEx) ideFrame.getStatusBar();
                    HyperlinkListener listener = new HyperlinkListener() {

                        @Override
                        public void hyperlinkUpdate(HyperlinkEvent e) {
                            if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
                                return;
                            Messages.showMessageDialog("<html>Following usages were safely skipped:<br>" + StringUtil.join(mySkippedUsages, unresolvableCollisionUsageInfo -> unresolvableCollisionUsageInfo.getDescription(), "<br>") + "</html>", "Not All Usages Were Renamed", null);
                        }
                    };
                    statusBar.notifyProgressByBalloon(MessageType.WARNING, "<html><body>Unable to rename certain usages. <a href=\"\">Browse</a></body></html>", null, listener);
                }
            }, ModalityState.NON_MODAL);
        }
    }
}
Also used : DescriptiveNameUtil(com.intellij.lang.findUsages.DescriptiveNameUtil) IdeFrame(com.intellij.openapi.wm.IdeFrame) java.util(java.util) MessageType(com.intellij.openapi.ui.MessageType) HyperlinkEvent(javax.swing.event.HyperlinkEvent) ModalityState(com.intellij.openapi.application.ModalityState) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) NonNls(org.jetbrains.annotations.NonNls) Computable(com.intellij.openapi.util.Computable) UsageInfo(com.intellij.usageView.UsageInfo) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) RefactoringEventListener(com.intellij.refactoring.listeners.RefactoringEventListener) AutomaticRenamer(com.intellij.refactoring.rename.naming.AutomaticRenamer) ContainerUtil(com.intellij.util.containers.ContainerUtil) CopyFilesOrDirectoriesHandler(com.intellij.refactoring.copy.CopyFilesOrDirectoriesHandler) Project(com.intellij.openapi.project.Project) Messages(com.intellij.openapi.ui.Messages) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) Extensions(com.intellij.openapi.extensions.Extensions) ProgressManager(com.intellij.openapi.progress.ProgressManager) HyperlinkListener(javax.swing.event.HyperlinkListener) IncorrectOperationException(com.intellij.util.IncorrectOperationException) StatusBarEx(com.intellij.openapi.wm.ex.StatusBarEx) StringUtil(com.intellij.openapi.util.text.StringUtil) WindowManager(com.intellij.openapi.wm.WindowManager) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) LightElement(com.intellij.psi.impl.light.LightElement) AutomaticRenamerFactory(com.intellij.refactoring.rename.naming.AutomaticRenamerFactory) UsageViewUtil(com.intellij.usageView.UsageViewUtil) UsageViewDescriptor(com.intellij.usageView.UsageViewDescriptor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) Nullable(org.jetbrains.annotations.Nullable) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) ApplicationManager(com.intellij.openapi.application.ApplicationManager) RelatedUsageInfo(com.intellij.refactoring.util.RelatedUsageInfo) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) HyperlinkEvent(javax.swing.event.HyperlinkEvent) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) IdeFrame(com.intellij.openapi.wm.IdeFrame) HyperlinkListener(javax.swing.event.HyperlinkListener) IncorrectOperationException(com.intellij.util.IncorrectOperationException) StatusBarEx(com.intellij.openapi.wm.ex.StatusBarEx) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) RelatedUsageInfo(com.intellij.refactoring.util.RelatedUsageInfo)

Example 7 with NonCodeUsageInfo

use of com.intellij.refactoring.util.NonCodeUsageInfo in project intellij-plugins by JetBrains.

the class FlexMoveInnerClassProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    try {
        ActionScriptCreateClassOrInterfaceFix.createClass(myClassName, myPackageName, myTargetDirectory, false);
    } catch (Exception e) {
        Messages.showErrorDialog(myProject, e.getMessage(), getCommandName());
        return;
    }
    final PsiFile sourceFile = myElement.getContainingFile();
    Collection<String> importsInTargetFile = new HashSet<>();
    Collection<String> namespacesInTargetFile = new HashSet<>();
    List<FormatFixer> formatters = new ArrayList<>();
    //JSRefactoringUtil.addRemovalFormatters(mySourceClass, myMembersToMove, Condition.TRUE, Condition.TRUE, postponedFormatters);
    JSClass targetClass = myElement instanceof JSClass ? (JSClass) myElement : null;
    JSRefactoringUtil.fixOutgoingReferences(myElement, importsInTargetFile, namespacesInTargetFile, Collections.singletonList(((JSAttributeListOwner) myElement)), targetClass, false, false);
    myElement.setName(myClassName);
    Collection<UsageInfo> usagesToProcess = new ArrayList<>(Arrays.asList(usages));
    for (Iterator<UsageInfo> i = usagesToProcess.iterator(); i.hasNext(); ) {
        UsageInfo usage = i.next();
        PsiElement element;
        if (usage instanceof NonCodeUsageInfo || (element = usage.getElement()) == null || !PsiTreeUtil.isAncestor(myElement, element, false)) {
            continue;
        }
        if (element instanceof JSReferenceExpression) {
            ((JSReferenceExpression) element).bindToElement(myElement);
        } else if (element instanceof PsiNamedElement) {
            ((PsiNamedElement) element).setName(myClassName);
        } else {
            continue;
        }
        i.remove();
    }
    final PsiElement clazz = ActionScriptClassResolver.findClassByQNameStatic(StringUtil.getQualifiedName(myPackageName, myClassName), GlobalSearchScope.projectScope(myProject));
    PsiElement toInsert = myElement instanceof JSVariable ? JSRefactoringUtil.getVarStatementCopy((JSVariable) myElement) : myElement.copy();
    final PsiElement inserted = clazz.replace(toInsert);
    PsiFile insertedContainingFile = inserted.getContainingFile();
    JSQualifiedNamedElement newClass = inserted instanceof JSVarStatement ? ((JSVarStatement) inserted).getVariables()[0] : (JSQualifiedNamedElement) inserted;
    SmartPsiElementPointer<JSQualifiedNamedElement> newClassPointer = SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(newClass);
    JSRefactoringUtil.handleDocCommentAndFormat(inserted, formatters);
    JSRefactoringUtil.deleteWithNoPostponedFormatting(myElement);
    if (myPackageName.length() > 0) {
        for (UsageInfo usage : usagesToProcess) {
            if (usage instanceof NonCodeUsageInfo || usage.getFile() != sourceFile)
                continue;
            final PsiElement element = usage.getElement();
            if (element == null)
                continue;
            ImportUtils.doImport(element, StringUtil.getQualifiedName(myPackageName, myClassName), true);
        }
    }
    JSRefactoringUtil.postProcess(sourceFile, newClass, Collections.singletonList(sourceFile), importsInTargetFile, namespacesInTargetFile, formatters, true, false);
    boolean makePublic = false;
    newClass = newClassPointer.getElement();
    List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
    for (UsageInfo usage : usagesToProcess) {
        if (usage instanceof NonCodeUsageInfo) {
            nonCodeUsages.add((NonCodeUsageInfo) usage);
        } else {
            JSReferenceExpression refExpr = (JSReferenceExpression) usage.getElement();
            if (refExpr == null) {
                continue;
            }
            makePublic |= JSPsiImplUtils.getQNameForMove(refExpr, newClass) != null;
            refExpr.bindToElement(newClass);
        }
    }
    JSChangeVisibilityUtil.setVisibility((JSAttributeListOwner) newClass, makePublic ? JSAttributeList.AccessType.PUBLIC : JSAttributeList.AccessType.PACKAGE_LOCAL);
    myNonCodeUsages = nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
    if (myMoveCallback != null) {
        myMoveCallback.refactoringCompleted();
    }
    OpenFileDescriptor descriptor = new OpenFileDescriptor(myProject, insertedContainingFile.getVirtualFile(), newClass.getTextOffset());
    FileEditorManager.getInstance(myProject).openTextEditor(descriptor, true);
}
Also used : NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) JSVariable(com.intellij.lang.javascript.psi.JSVariable) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) JSVarStatement(com.intellij.lang.javascript.psi.JSVarStatement) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) FormatFixer(com.intellij.lang.javascript.refactoring.FormatFixer) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo)

Aggregations

NonCodeUsageInfo (com.intellij.refactoring.util.NonCodeUsageInfo)7 PsiElement (com.intellij.psi.PsiElement)4 UsageInfo (com.intellij.usageView.UsageInfo)4 PsiFile (com.intellij.psi.PsiFile)3 RefactoringElementListener (com.intellij.refactoring.listeners.RefactoringElementListener)3 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 PsiReference (com.intellij.psi.PsiReference)2 DescriptiveNameUtil (com.intellij.lang.findUsages.DescriptiveNameUtil)1 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)1 JSVarStatement (com.intellij.lang.javascript.psi.JSVarStatement)1 JSVariable (com.intellij.lang.javascript.psi.JSVariable)1 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)1 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)1 FormatFixer (com.intellij.lang.javascript.refactoring.FormatFixer)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ModalityState (com.intellij.openapi.application.ModalityState)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Extensions (com.intellij.openapi.extensions.Extensions)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1