elementsToInvert.add(refElement);
}
}
-
- if (namedElement instanceof PsiVariable) {
- final PsiExpression initializer = ((PsiVariable)namedElement).getInitializer();
- if (initializer != null) {
- elementsToInvert.add(initializer);
- }
- }
}
public PsiElement getElementToInvert(PsiElement namedElement, PsiElement element) {
}
@Override
- public void invertDefaultElementInitializer(final PsiElement element) {
+ public void invertElementInitializer(final PsiElement element) {
if (element instanceof PsiField && ((PsiField)element).getInitializer() == null) {
((PsiField)element).setInitializer(JavaPsiFacade.getElementFactory(element.getProject()).createExpressionFromText("true", element));
+ } else if (element instanceof PsiVariable) {
+ final PsiExpression initializer = ((PsiVariable)element).getInitializer();
+ if (initializer != null) {
+ replaceWithNegatedExpression(initializer);
+ }
}
}
public void collectRefElements(final PsiElement element,
- final Collection<PsiElement> elementsToInvert,
final RenameProcessor renameProcessor,
- @NotNull final String newName) {
+ @NotNull final String newName,
+ final Collection<PsiElement> elementsToInvert) {
collectRefsToInvert(element, elementsToInvert);
if (element instanceof PsiMethod) {
}
@Override
- public void findConflicts(MultiMap<PsiElement, String> conflicts, UsageInfo[] usageInfos) {
+ public void findConflicts(UsageInfo[] usageInfos, MultiMap<PsiElement, String> conflicts) {
for (UsageInfo info : usageInfos) {
final PsiElement element = info.getElement();
if (element instanceof PsiMethodReferenceExpression) {
/**
* Eventually collect additional elements to rename, e.g. override methods
- * and find expressions which need to be inverted
+ * and find expressions which need to be inverted, e.g. return method statements inside the method itself, etc
*
* @param renameProcessor null if element is not named or name was not changed
*/
public abstract void collectRefElements(PsiElement element,
- Collection<PsiElement> elementsToInvert,
@Nullable RenameProcessor renameProcessor,
- @NotNull String newName);
+ @NotNull String newName,
+ Collection<PsiElement> elementsToInvert);
/**
- * Invoked from {@link #getForeignElementToInvert(com.intellij.psi.PsiElement, com.intellij.psi.PsiElement, com.intellij.lang.Language)};
+ * Invoked from {@link #getForeignElementToInvert(PsiElement, PsiElement, Language)};
* should be used to reject usages for elements from foreign language to be refactored
* @return null, if reference should not be reverted
*/
public abstract PsiElement getElementToInvert(PsiElement namedElement, PsiElement expression);
/**
- * Should be called from {@link #collectRefElements(PsiElement, Collection, RenameProcessor, String)}
+ * Should be called from {@link #collectRefElements(PsiElement, RenameProcessor, String, Collection)}
* to process found usages in foreign languages
*/
protected static PsiElement getForeignElementToInvert(PsiElement namedElement,
/**
* Replace expression with created negation
- * @param expression to be inverted, found in {@link #collectRefElements(PsiElement, Collection, RenameProcessor, String)}
+ * @param expression to be inverted, found in {@link #collectRefElements(PsiElement, RenameProcessor, String, Collection)}
*/
public abstract void replaceWithNegatedExpression(PsiElement expression);
/**
- * Initialize variable with negated default initializer when default was initially omitted
+ * Initialize variable with negated default initializer when default was initially omitted,
+ * or invert variable initializer
*/
- public void invertDefaultElementInitializer(PsiElement var) {}
+ public abstract void invertElementInitializer(PsiElement var);
/**
* Detect usages which can't be inverted
*/
- public void findConflicts(MultiMap<PsiElement, String> conflicts,
- UsageInfo[] usageInfos) {}
+ public void findConflicts(UsageInfo[] usageInfos, MultiMap<PsiElement, String> conflicts) {}
}
@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
final MultiMap<PsiElement, String> conflicts = new MultiMap<PsiElement, String>();
- myDelegate.findConflicts(conflicts, refUsages.get());
+ myDelegate.findConflicts(refUsages.get(), conflicts);
if (!conflicts.isEmpty()) {
return showConflicts(conflicts, null);
final List<SmartPsiElementPointer> toInvert = new ArrayList<SmartPsiElementPointer>();
final LinkedHashSet<PsiElement> elementsToInvert = new LinkedHashSet<PsiElement>();
- myDelegate.collectRefElements(myElement, elementsToInvert, myRenameProcessor, myNewName);
+ myDelegate.collectRefElements(myElement, myRenameProcessor, myNewName, elementsToInvert);
for (PsiElement element : elementsToInvert) {
toInvert.add(mySmartPointerManager.createSmartPsiElementPointer(element));
}
}
}
- myDelegate.invertDefaultElementInitializer(myElement);
+ myDelegate.invertElementInitializer(myElement);
}
@Override
@Override
public void collectRefElements(PsiElement psiElement,
- Collection<PsiElement> elementsToInvert,
@Nullable RenameProcessor renameProcessor,
- @NotNull String newName) {
+ @NotNull String newName,
+ Collection<PsiElement> elementsToInvert) {
final Collection<PsiReference> refs = ReferencesSearch.search(psiElement).findAll();
for (PsiReference ref : refs) {
ContainerUtil.addIfNotNull(elementsToInvert, getForeignElementToInvert(psiElement, refElement, PythonLanguage.getInstance()));
}
}
-
- if (psiElement instanceof PyNamedParameter) {
- PyExpression defaultValue = ((PyNamedParameter)psiElement).getDefaultValue();
- if (defaultValue != null) {
- elementsToInvert.add(defaultValue);
- }
- }
}
public PsiElement getElementToInvert(PsiElement namedElement, PsiElement element) {
return null;
}
+ @Override
+ public void invertElementInitializer(PsiElement psiElement) {
+ if (psiElement instanceof PyNamedParameter) {
+ final PyExpression defaultValue = ((PyNamedParameter)psiElement).getDefaultValue();
+ if (defaultValue != null) {
+ replaceWithNegatedExpression(defaultValue);
+ }
+ }
+ }
+
@Override
public void replaceWithNegatedExpression(PsiElement expression) {
if (expression != null && PsiTreeUtil.getParentOfType(expression, PyImportStatementBase.class, false) == null) {