Search in sources :

Example 1 with InlineHandler

use of com.intellij.lang.refactoring.InlineHandler in project intellij-community by JetBrains.

the class GenericInlineHandler method initializeInliners.

public static Map<Language, InlineHandler.Inliner> initializeInliners(PsiElement element, InlineHandler.Settings settings, Collection<? extends PsiReference> allReferences) {
    final Map<Language, InlineHandler.Inliner> inliners = new HashMap<>();
    for (PsiReference ref : allReferences) {
        if (ref == null) {
            LOG.error("element: " + element.getClass() + ", allReferences contains null!");
            continue;
        }
        PsiElement refElement = ref.getElement();
        LOG.assertTrue(refElement != null, ref.getClass().getName());
        final Language language = refElement.getLanguage();
        if (inliners.containsKey(language))
            continue;
        final List<InlineHandler> handlers = InlineHandlers.getInlineHandlers(language);
        for (InlineHandler handler : handlers) {
            InlineHandler.Inliner inliner = handler.createInliner(element, settings);
            if (inliner != null) {
                inliners.put(language, inliner);
                break;
            }
        }
    }
    return inliners;
}
Also used : Language(com.intellij.lang.Language) HashMap(com.intellij.util.containers.HashMap) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) InlineHandler(com.intellij.lang.refactoring.InlineHandler)

Example 2 with InlineHandler

use of com.intellij.lang.refactoring.InlineHandler in project intellij-community by JetBrains.

the class GenericInlineHandler method invoke.

public static boolean invoke(final PsiElement element, @Nullable Editor editor, final InlineHandler languageSpecific) {
    final PsiReference invocationReference = editor != null ? TargetElementUtil.findReference(editor) : null;
    final InlineHandler.Settings settings = languageSpecific.prepareInlineElement(element, editor, invocationReference != null);
    if (settings == null || settings == InlineHandler.Settings.CANNOT_INLINE_SETTINGS) {
        return settings != null;
    }
    final Collection<? extends PsiReference> allReferences;
    if (settings.isOnlyOneReferenceToInline()) {
        allReferences = Collections.singleton(invocationReference);
    } else {
        final Ref<Collection<? extends PsiReference>> usagesRef = new Ref<>();
        ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> usagesRef.set(ReferencesSearch.search(element).findAll()), "Find Usages", false, element.getProject());
        allReferences = usagesRef.get();
    }
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final Map<Language, InlineHandler.Inliner> inliners = initializeInliners(element, settings, allReferences);
    for (PsiReference reference : allReferences) {
        collectConflicts(reference, element, inliners, conflicts);
    }
    final Project project = element.getProject();
    if (!conflicts.isEmpty()) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
        } else {
            final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts);
            if (!conflictsDialog.showAndGet()) {
                return true;
            }
        }
    }
    HashSet<PsiElement> elements = new HashSet<>();
    for (PsiReference reference : allReferences) {
        PsiElement refElement = reference.getElement();
        if (refElement != null) {
            elements.add(refElement);
        }
    }
    if (!settings.isOnlyOneReferenceToInline()) {
        elements.add(element);
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatusRecursively(project, elements, true)) {
        return true;
    }
    ApplicationManager.getApplication().runWriteAction(() -> {
        final String subj = element instanceof PsiNamedElement ? ((PsiNamedElement) element).getName() : "element";
        CommandProcessor.getInstance().executeCommand(project, () -> {
            final PsiReference[] references = sortDepthFirstRightLeftOrder(allReferences);
            final UsageInfo[] usages = new UsageInfo[references.length];
            for (int i = 0; i < references.length; i++) {
                usages[i] = new UsageInfo(references[i]);
            }
            for (UsageInfo usage : usages) {
                inlineReference(usage, element, inliners);
            }
            if (!settings.isOnlyOneReferenceToInline()) {
                languageSpecific.removeDefinition(element, settings);
            }
        }, RefactoringBundle.message("inline.command", StringUtil.notNullize(subj, "<nameless>")), null);
    });
    return true;
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PsiReference(com.intellij.psi.PsiReference) MultiMap(com.intellij.util.containers.MultiMap) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) Language(com.intellij.lang.Language) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) InlineHandler(com.intellij.lang.refactoring.InlineHandler) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet)

Example 3 with InlineHandler

use of com.intellij.lang.refactoring.InlineHandler in project intellij-community by JetBrains.

the class InlineAction method getHandler.

@Override
protected RefactoringActionHandler getHandler(@NotNull Language language, PsiElement element) {
    RefactoringActionHandler handler = super.getHandler(language, element);
    if (handler != null)
        return handler;
    List<InlineHandler> handlers = InlineHandlers.getInlineHandlers(language);
    return handlers.isEmpty() ? null : new InlineRefactoringActionHandler();
}
Also used : InlineRefactoringActionHandler(com.intellij.refactoring.inline.InlineRefactoringActionHandler) RefactoringActionHandler(com.intellij.refactoring.RefactoringActionHandler) InlineRefactoringActionHandler(com.intellij.refactoring.inline.InlineRefactoringActionHandler) InlineHandler(com.intellij.lang.refactoring.InlineHandler)

Aggregations

InlineHandler (com.intellij.lang.refactoring.InlineHandler)3 Language (com.intellij.lang.Language)2 PsiElement (com.intellij.psi.PsiElement)2 PsiReference (com.intellij.psi.PsiReference)2 Project (com.intellij.openapi.project.Project)1 Ref (com.intellij.openapi.util.Ref)1 PsiNamedElement (com.intellij.psi.PsiNamedElement)1 RefactoringActionHandler (com.intellij.refactoring.RefactoringActionHandler)1 InlineRefactoringActionHandler (com.intellij.refactoring.inline.InlineRefactoringActionHandler)1 ConflictsDialog (com.intellij.refactoring.ui.ConflictsDialog)1 UsageInfo (com.intellij.usageView.UsageInfo)1 HashMap (com.intellij.util.containers.HashMap)1 HashSet (com.intellij.util.containers.HashSet)1 MultiMap (com.intellij.util.containers.MultiMap)1