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;
}
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;
}
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;
}
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;
}
Aggregations