Search in sources :

Example 1 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class InlinePropertyHandler method inlineElement.

public void inlineElement(final Project project, Editor editor, PsiElement psiElement) {
    if (!(psiElement instanceof IProperty))
        return;
    IProperty property = (IProperty) psiElement;
    final String propertyValue = property.getValue();
    if (propertyValue == null)
        return;
    final List<PsiElement> occurrences = Collections.synchronizedList(ContainerUtil.<PsiElement>newArrayList());
    final Collection<PsiFile> containingFiles = Collections.synchronizedSet(new HashSet<PsiFile>());
    containingFiles.add(psiElement.getContainingFile());
    boolean result = ReferencesSearch.search(psiElement).forEach(psiReference -> {
        PsiElement element = psiReference.getElement();
        PsiElement parent = element.getParent();
        if (parent instanceof PsiExpressionList && parent.getParent() instanceof PsiMethodCallExpression) {
            if (((PsiExpressionList) parent).getExpressions().length == 1) {
                occurrences.add(parent.getParent());
                containingFiles.add(element.getContainingFile());
                return true;
            }
        }
        return false;
    });
    if (!result) {
        CommonRefactoringUtil.showErrorHint(project, editor, "Property has non-method usages", REFACTORING_NAME, null);
    }
    if (occurrences.isEmpty()) {
        CommonRefactoringUtil.showErrorHint(project, editor, "Property has no usages", REFACTORING_NAME, null);
        return;
    }
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
        String occurrencesString = RefactoringBundle.message("occurrences.string", occurrences.size());
        String question = PropertiesBundle.message("inline.property.confirmation", property.getName(), propertyValue) + " " + occurrencesString;
        RefactoringMessageDialog dialog = new RefactoringMessageDialog(REFACTORING_NAME, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
        if (!dialog.showAndGet()) {
            return;
        }
    }
    final RefactoringEventData data = new RefactoringEventData();
    data.addElement(psiElement.copy());
    new WriteCommandAction.Simple(project, REFACTORING_NAME, containingFiles.toArray(new PsiFile[containingFiles.size()])) {

        @Override
        protected void run() throws Throwable {
            project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_ID, data);
            PsiLiteral stringLiteral = (PsiLiteral) JavaPsiFacade.getInstance(getProject()).getElementFactory().createExpressionFromText("\"" + StringUtil.escapeStringCharacters(propertyValue) + "\"", null);
            for (PsiElement occurrence : occurrences) {
                occurrence.replace(stringLiteral.copy());
            }
            project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(REFACTORING_ID, null);
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) IProperty(com.intellij.lang.properties.IProperty) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData)

Example 2 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class PyMembersRefactoringBaseProcessor method getBeforeData.

@Nullable
@Override
protected RefactoringEventData getBeforeData() {
    RefactoringEventData data = new RefactoringEventData();
    data.addElement(myFrom);
    data.addMembers(myMembersToMove.toArray(new PyMemberInfo[myMembersToMove.size()]), info -> info.getMember());
    return data;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class IntroduceParameterProcessor method getBeforeData.

@Nullable
@Override
protected RefactoringEventData getBeforeData() {
    RefactoringEventData data = new RefactoringEventData();
    data.addElements(new PsiElement[] { myLocalVariable, myExpressionToSearch });
    return data;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class PullUpProcessor method getBeforeData.

@Nullable
@Override
protected RefactoringEventData getBeforeData() {
    RefactoringEventData data = new RefactoringEventData();
    data.addElement(mySourceClass);
    data.addMembers(myMembersToMove, info -> info.getMember());
    return data;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class InlineConstantFieldProcessor method getBeforeData.

@Nullable
@Override
protected RefactoringEventData getBeforeData() {
    RefactoringEventData data = new RefactoringEventData();
    data.addElement(myField);
    return data;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

RefactoringEventData (com.intellij.refactoring.listeners.RefactoringEventData)51 Nullable (org.jetbrains.annotations.Nullable)36 NotNull (org.jetbrains.annotations.NotNull)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Project (com.intellij.openapi.project.Project)5 UsageInfo (com.intellij.usageView.UsageInfo)5 ArrayList (java.util.ArrayList)5 Result (com.intellij.openapi.application.Result)4 Logger (com.intellij.openapi.diagnostic.Logger)4 com.intellij.psi (com.intellij.psi)4 RefactoringEventListener (com.intellij.refactoring.listeners.RefactoringEventListener)4 MultiMap (com.intellij.util.containers.MultiMap)4 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)3 CommandProcessor (com.intellij.openapi.command.CommandProcessor)3 EditorColors (com.intellij.openapi.editor.colors.EditorColors)3 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)3 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)3 WindowManager (com.intellij.openapi.wm.WindowManager)3 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)3