Search in sources :

Example 1 with RefactoringElementListenerComposite

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

the class RefactoringScopeElementListenerProvider method getListener.

@Override
public RefactoringElementListener getListener(PsiElement element) {
    if (!(element instanceof PsiQualifiedNamedElement) && !(element instanceof PsiDirectory))
        return null;
    final PsiFile containingFile = element.getContainingFile();
    RefactoringElementListenerComposite composite = null;
    String oldName = getQualifiedName(element, false);
    if (oldName != null) {
        composite = getComposite(element, containingFile, null, oldName);
    }
    if (element instanceof PsiQualifiedNamedElement) {
        oldName = getQualifiedName(element, true);
        if (oldName != null) {
            composite = getComposite(element, containingFile, composite, oldName);
        }
    }
    return composite;
}
Also used : PsiQualifiedNamedElement(com.intellij.psi.PsiQualifiedNamedElement) PsiDirectory(com.intellij.psi.PsiDirectory) RefactoringElementListenerComposite(com.intellij.refactoring.listeners.RefactoringElementListenerComposite) PsiFile(com.intellij.psi.PsiFile)

Example 2 with RefactoringElementListenerComposite

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

the class RunConfigurationRefactoringElementListenerProvider method getListener.

@Override
public RefactoringElementListener getListener(final PsiElement element) {
    RefactoringElementListenerComposite composite = null;
    final List<RunConfiguration> configurations = RunManager.getInstance(element.getProject()).getAllConfigurationsList();
    for (RunConfiguration configuration : configurations) {
        if (configuration instanceof RefactoringListenerProvider) {
            // todo: perhaps better way to handle listeners?
            RefactoringElementListener listener;
            try {
                listener = ((RefactoringListenerProvider) configuration).getRefactoringElementListener(element);
            } catch (Exception e) {
                LOG.error(e);
                continue;
            }
            if (listener != null) {
                if (configuration instanceof LocatableConfiguration) {
                    listener = new NameGeneratingListenerDecorator((LocatableConfiguration) configuration, listener);
                }
                if (composite == null) {
                    composite = new RefactoringElementListenerComposite();
                }
                composite.addListener(listener);
            }
        }
    }
    return composite;
}
Also used : RunConfiguration(com.intellij.execution.configurations.RunConfiguration) LocatableConfiguration(com.intellij.execution.configurations.LocatableConfiguration) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) RefactoringElementListenerComposite(com.intellij.refactoring.listeners.RefactoringElementListenerComposite) RefactoringListenerProvider(com.intellij.execution.configurations.RefactoringListenerProvider)

Example 3 with RefactoringElementListenerComposite

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

the class TestsPattern method getListener.

@Nullable
@Override
public RefactoringElementListener getListener(PsiElement element, JUnitConfiguration configuration) {
    final RefactoringElementListenerComposite composite = new RefactoringElementListenerComposite();
    final JUnitConfiguration.Data data = configuration.getPersistentData();
    final Set<String> patterns = data.getPatterns();
    for (final String pattern : patterns) {
        final PsiClass testClass = getTestClass(configuration.getProject(), pattern.trim());
        if (testClass != null && testClass.equals(element)) {
            final RefactoringElementListener listeners = RefactoringListeners.getListeners(testClass, new RefactoringListeners.Accessor<PsiClass>() {

                private String myOldName = testClass.getQualifiedName();

                @Override
                public void setName(String qualifiedName) {
                    final Set<String> replaced = new LinkedHashSet<>();
                    for (String currentPattern : patterns) {
                        if (myOldName.equals(currentPattern)) {
                            replaced.add(qualifiedName);
                            myOldName = qualifiedName;
                        } else {
                            replaced.add(currentPattern);
                        }
                    }
                    patterns.clear();
                    patterns.addAll(replaced);
                }

                @Override
                public PsiClass getPsiElement() {
                    return testClass;
                }

                @Override
                public void setPsiElement(PsiClass psiElement) {
                    if (psiElement == testClass) {
                        setName(psiElement.getQualifiedName());
                    }
                }
            });
            if (listeners != null) {
                composite.addListener(listeners);
            }
        }
    }
    return composite;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) RefactoringElementListenerComposite(com.intellij.refactoring.listeners.RefactoringElementListenerComposite) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with RefactoringElementListenerComposite

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

the class PyExtractMethodUtil method processParameters.

private static void processParameters(@NotNull final Project project, @NotNull final PyFunction generatedMethod, @NotNull final AbstractVariableData[] variableData, final boolean isMethod, final boolean isClassMethod, final boolean isStaticMethod) {
    final Map<String, String> map = createMap(variableData);
    // Rename parameters
    for (PyParameter parameter : generatedMethod.getParameterList().getParameters()) {
        final String name = parameter.getName();
        final String newName = map.get(name);
        if (name != null && newName != null && !name.equals(newName)) {
            final Map<PsiElement, String> allRenames = new java.util.HashMap<>();
            allRenames.put(parameter, newName);
            final UsageInfo[] usages = RenameUtil.findUsages(parameter, newName, false, false, allRenames);
            try {
                RenameUtil.doRename(parameter, newName, usages, project, new RefactoringElementListenerComposite());
            } catch (IncorrectOperationException e) {
                RenameUtil.showErrorMessage(e, parameter, project);
                return;
            }
        }
    }
    // Change signature according to pass settings and
    final PyFunctionBuilder builder = new PyFunctionBuilder("foo", generatedMethod);
    if (isClassMethod) {
        builder.parameter("cls");
    } else if (isMethod && !isStaticMethod) {
        builder.parameter("self");
    }
    for (AbstractVariableData data : variableData) {
        if (data.isPassAsParameter()) {
            builder.parameter(data.getName());
        }
    }
    final PyParameterList pyParameterList = builder.buildFunction(project, LanguageLevel.forElement(generatedMethod)).getParameterList();
    generatedMethod.getParameterList().replace(pyParameterList);
}
Also used : HashMap(com.intellij.util.containers.hash.HashMap) RefactoringElementListenerComposite(com.intellij.refactoring.listeners.RefactoringElementListenerComposite) AbstractVariableData(com.intellij.refactoring.util.AbstractVariableData) PyFunctionBuilder(com.jetbrains.python.psi.impl.PyFunctionBuilder) IncorrectOperationException(com.intellij.util.IncorrectOperationException) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

RefactoringElementListenerComposite (com.intellij.refactoring.listeners.RefactoringElementListenerComposite)4 RefactoringElementListener (com.intellij.refactoring.listeners.RefactoringElementListener)2 LocatableConfiguration (com.intellij.execution.configurations.LocatableConfiguration)1 RefactoringListenerProvider (com.intellij.execution.configurations.RefactoringListenerProvider)1 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiFile (com.intellij.psi.PsiFile)1 PsiQualifiedNamedElement (com.intellij.psi.PsiQualifiedNamedElement)1 AbstractVariableData (com.intellij.refactoring.util.AbstractVariableData)1 UsageInfo (com.intellij.usageView.UsageInfo)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 HashMap (com.intellij.util.containers.hash.HashMap)1 PyFunctionBuilder (com.jetbrains.python.psi.impl.PyFunctionBuilder)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 Nullable (org.jetbrains.annotations.Nullable)1