Search in sources :

Example 1 with RefactoringElementAdapter

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

the class TestNGConfiguration method getRefactoringElementListener.

@Nullable
public RefactoringElementListener getRefactoringElementListener(final PsiElement element) {
    if (data.TEST_OBJECT.equals(TestType.PACKAGE.getType())) {
        if (!(element instanceof PsiPackage))
            return null;
        final RefactoringElementListener listener = RefactoringListeners.getListener((PsiPackage) element, myPackage);
        return RunConfigurationExtension.wrapRefactoringElementListener(element, this, listener);
    } else if (data.TEST_OBJECT.equals(TestType.CLASS.getType())) {
        if (!(element instanceof PsiClass) && !(element instanceof PsiPackage))
            return null;
        final RefactoringElementListener listener = RefactoringListeners.getClassOrPackageListener(element, myClass);
        return RunConfigurationExtension.wrapRefactoringElementListener(element, this, listener);
    } else if (data.TEST_OBJECT.equals(TestType.METHOD.getType())) {
        if (!(element instanceof PsiMethod)) {
            final RefactoringElementListener listener = RefactoringListeners.getClassOrPackageListener(element, myClass);
            return RunConfigurationExtension.wrapRefactoringElementListener(element, this, listener);
        }
        final PsiMethod method = (PsiMethod) element;
        if (!method.getName().equals(data.getMethodName()))
            return null;
        if (!method.getContainingClass().equals(myClass.getPsiElement()))
            return null;
        class Listener extends RefactoringElementAdapter implements UndoRefactoringElementListener {

            public void elementRenamedOrMoved(@NotNull final PsiElement newElement) {
                data.setTestMethod(PsiLocation.fromPsiElement((PsiMethod) newElement));
            }

            @Override
            public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                final int methodIdx = oldQualifiedName.indexOf("#") + 1;
                if (methodIdx <= 0 || methodIdx >= oldQualifiedName.length())
                    return;
                data.METHOD_NAME = oldQualifiedName.substring(methodIdx);
            }
        }
        return RunConfigurationExtension.wrapRefactoringElementListener(element, this, new Listener());
    }
    return null;
}
Also used : RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) UndoRefactoringElementListener(com.intellij.refactoring.listeners.UndoRefactoringElementListener) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) UndoRefactoringElementListener(com.intellij.refactoring.listeners.UndoRefactoringElementListener) UndoRefactoringElementListener(com.intellij.refactoring.listeners.UndoRefactoringElementListener) NotNull(org.jetbrains.annotations.NotNull) RefactoringElementAdapter(com.intellij.refactoring.listeners.RefactoringElementAdapter) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with RefactoringElementAdapter

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

the class GroovyScriptRunConfiguration method getRefactoringElementListener.

@Override
public RefactoringElementListener getRefactoringElementListener(PsiElement element) {
    if (scriptPath == null || !scriptPath.equals(getPathByElement(element))) {
        return null;
    }
    final PsiClass classToRun = GroovyRunnerPsiUtil.getRunningClass(element);
    if (element instanceof GroovyFile) {
        return new RefactoringElementAdapter() {

            @Override
            protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                if (newElement instanceof GroovyFile) {
                    GroovyFile file = (GroovyFile) newElement;
                    setScriptPath(ScriptFileUtil.getScriptFilePath(file.getVirtualFile()));
                }
            }

            @Override
            public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                elementRenamedOrMoved(newElement);
            }
        };
    } else if (element instanceof PsiClass && element.getManager().areElementsEquivalent(element, classToRun)) {
        return new RefactoringElementAdapter() {

            @Override
            protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                setName(((PsiClass) newElement).getName());
            }

            @Override
            public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                elementRenamedOrMoved(newElement);
            }
        };
    }
    return null;
}
Also used : PsiClass(com.intellij.psi.PsiClass) NotNull(org.jetbrains.annotations.NotNull) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement) RefactoringElementAdapter(com.intellij.refactoring.listeners.RefactoringElementAdapter)

Example 3 with RefactoringElementAdapter

use of com.intellij.refactoring.listeners.RefactoringElementAdapter in project android by JetBrains.

the class AndroidTestRunConfiguration method getRefactoringElementListener.

/**
   * Returns a refactoring listener that listens to changes in either the package, class or method names
   * depending on the current {@link #TESTING_TYPE}.
   */
@Nullable
@Override
public RefactoringElementListener getRefactoringElementListener(PsiElement element) {
    if (element instanceof PsiPackage) {
        String pkgName = ((PsiPackage) element).getQualifiedName();
        if (TESTING_TYPE == TEST_ALL_IN_PACKAGE && !StringUtil.equals(pkgName, PACKAGE_NAME)) {
            // testing package, but the refactored package does not match our package
            return null;
        } else if (TESTING_TYPE != TEST_ALL_IN_PACKAGE && !StringUtil.equals(pkgName, StringUtil.getPackageName(CLASS_NAME))) {
            // testing a class or a method, but the refactored package doesn't match our containing package
            return null;
        }
        return new RefactoringElementAdapter() {

            @Override
            protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                if (newElement instanceof PsiPackage) {
                    String newPkgName = ((PsiPackage) newElement).getQualifiedName();
                    if (TESTING_TYPE == TEST_ALL_IN_PACKAGE) {
                        PACKAGE_NAME = newPkgName;
                    } else {
                        CLASS_NAME = CLASS_NAME.replace(StringUtil.getPackageName(CLASS_NAME), newPkgName);
                    }
                }
            }

            @Override
            public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                if (newElement instanceof PsiPackage) {
                    if (TESTING_TYPE == TEST_ALL_IN_PACKAGE) {
                        PACKAGE_NAME = oldQualifiedName;
                    } else {
                        CLASS_NAME = CLASS_NAME.replace(StringUtil.getPackageName(CLASS_NAME), oldQualifiedName);
                    }
                }
            }
        };
    } else if ((TESTING_TYPE == TEST_CLASS || TESTING_TYPE == TEST_METHOD) && element instanceof PsiClass) {
        if (!StringUtil.equals(JavaExecutionUtil.getRuntimeQualifiedName((PsiClass) element), CLASS_NAME)) {
            return null;
        }
        return new RefactoringElementAdapter() {

            @Override
            protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                if (newElement instanceof PsiClass) {
                    CLASS_NAME = JavaExecutionUtil.getRuntimeQualifiedName((PsiClass) newElement);
                }
            }

            @Override
            public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                if (newElement instanceof PsiClass) {
                    CLASS_NAME = oldQualifiedName;
                }
            }
        };
    } else if (TESTING_TYPE == TEST_METHOD && element instanceof PsiMethod) {
        PsiMethod psiMethod = (PsiMethod) element;
        if (!StringUtil.equals(psiMethod.getName(), METHOD_NAME)) {
            return null;
        }
        PsiClass psiClass = psiMethod.getContainingClass();
        if (psiClass == null) {
            return null;
        }
        String fqName = psiClass.getQualifiedName();
        if (fqName != null && !StringUtil.equals(fqName, CLASS_NAME)) {
            return null;
        }
        return new RefactoringElementAdapter() {

            @Override
            protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                if (newElement instanceof PsiMethod) {
                    METHOD_NAME = ((PsiMethod) newElement).getName();
                }
            }

            @Override
            public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                if (newElement instanceof PsiMethod) {
                    METHOD_NAME = oldQualifiedName;
                }
            }
        };
    }
    return null;
}
Also used : NotNull(org.jetbrains.annotations.NotNull) RefactoringElementAdapter(com.intellij.refactoring.listeners.RefactoringElementAdapter) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with RefactoringElementAdapter

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

the class AbstractPythonLegacyTestRunConfiguration method getRefactoringElementListener.

@Override
public RefactoringElementListener getRefactoringElementListener(PsiElement element) {
    if (element instanceof PsiDirectory) {
        VirtualFile vFile = ((PsiDirectory) element).getVirtualFile();
        if ((myTestType == TestType.TEST_FOLDER && pathsEqual(vFile, myFolderName)) || pathsEqual(vFile, getWorkingDirectory())) {
            return new RefactoringElementAdapter() {

                @Override
                protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                    String newPath = FileUtil.toSystemDependentName(((PsiDirectory) newElement).getVirtualFile().getPath());
                    setWorkingDirectory(newPath);
                    if (myTestType == TestType.TEST_FOLDER) {
                        myFolderName = newPath;
                    }
                }

                @Override
                public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                    final String systemDependant = FileUtil.toSystemDependentName(oldQualifiedName);
                    setWorkingDirectory(systemDependant);
                    if (myTestType == TestType.TEST_FOLDER) {
                        myFolderName = systemDependant;
                    }
                }
            };
        }
        return null;
    }
    if (myTestType == TestType.TEST_FOLDER) {
        return null;
    }
    File scriptFile = new File(myScriptName);
    if (!scriptFile.isAbsolute()) {
        scriptFile = new File(getWorkingDirectory(), myScriptName);
    }
    PsiFile containingFile = element.getContainingFile();
    VirtualFile vFile = containingFile == null ? null : containingFile.getVirtualFile();
    if (vFile != null && Comparing.equal(new File(vFile.getPath()).getAbsolutePath(), scriptFile.getAbsolutePath())) {
        if (element instanceof PsiFile) {
            return new RefactoringElementAdapter() {

                @Override
                protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                    VirtualFile virtualFile = ((PsiFile) newElement).getVirtualFile();
                    if (virtualFile != null) {
                        myScriptName = FileUtil.toSystemDependentName(virtualFile.getPath());
                    }
                }

                @Override
                public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                    myScriptName = FileUtil.toSystemDependentName(oldQualifiedName);
                }
            };
        }
        if (element instanceof PyClass && (myTestType == TestType.TEST_CLASS || myTestType == TestType.TEST_METHOD) && Comparing.equal(((PyClass) element).getName(), myClassName)) {
            return new RefactoringElementAdapter() {

                @Override
                protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                    myClassName = ((PyClass) newElement).getName();
                }

                @Override
                public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                    myClassName = oldQualifiedName;
                }
            };
        }
        if (element instanceof PyFunction && Comparing.equal(((PyFunction) element).getName(), myMethodName)) {
            ScopeOwner scopeOwner = PsiTreeUtil.getParentOfType(element, ScopeOwner.class);
            if ((myTestType == TestType.TEST_FUNCTION && scopeOwner instanceof PyFile) || (myTestType == TestType.TEST_METHOD && scopeOwner instanceof PyClass && Comparing.equal(scopeOwner.getName(), myClassName))) {
                return new RefactoringElementAdapter() {

                    @Override
                    protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                        myMethodName = ((PyFunction) newElement).getName();
                    }

                    @Override
                    public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                        final int methodIdx = oldQualifiedName.indexOf("#") + 1;
                        if (methodIdx > 0 && methodIdx < oldQualifiedName.length()) {
                            myMethodName = oldQualifiedName.substring(methodIdx);
                        }
                    }
                };
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyClass(com.jetbrains.python.psi.PyClass) PyFile(com.jetbrains.python.psi.PyFile) NotNull(org.jetbrains.annotations.NotNull) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PyFunction(com.jetbrains.python.psi.PyFunction) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PyFile(com.jetbrains.python.psi.PyFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) PsiElement(com.intellij.psi.PsiElement) RefactoringElementAdapter(com.intellij.refactoring.listeners.RefactoringElementAdapter)

Aggregations

RefactoringElementAdapter (com.intellij.refactoring.listeners.RefactoringElementAdapter)4 NotNull (org.jetbrains.annotations.NotNull)4 PsiElement (com.intellij.psi.PsiElement)2 Nullable (org.jetbrains.annotations.Nullable)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiClass (com.intellij.psi.PsiClass)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiFile (com.intellij.psi.PsiFile)1 RefactoringElementListener (com.intellij.refactoring.listeners.RefactoringElementListener)1 UndoRefactoringElementListener (com.intellij.refactoring.listeners.UndoRefactoringElementListener)1 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)1 PyClass (com.jetbrains.python.psi.PyClass)1 PyFile (com.jetbrains.python.psi.PyFile)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 File (java.io.File)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1