Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
backported some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mplushnikov committed Aug 30, 2020
1 parent 97177b0 commit 7505dc5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,4 @@ private void findRedundantDefinition(PsiVariable field, PsiClass containingClass
}
}
}

@Override
public @Nls(capitalization = Nls.Capitalization.Sentence)
@NotNull
String getDisplayName() {
return "@Slf4j";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import java.util.Arrays;
import java.util.Optional;

import static de.plushnikov.intellij.plugin.inspection.modifiers.RedundantModifiersInfoType.INNER_CLASS;

public abstract class LombokRedundantModifierInspection extends AbstractBaseJavaLocalInspectionTool {

private final Class<?> supportedAnnotation;
Expand Down Expand Up @@ -86,7 +84,7 @@ private void visit(PsiModifierListOwner psiModifierListOwner) {
|| (infoType != RedundantModifiersInfoType.VARIABLE && !(parentModifierListOwner instanceof PsiClass))) {
continue;
}
if ((supportedAnnotation == null || parentModifierListOwner.hasAnnotation(supportedAnnotation.getName())) &&
if ((supportedAnnotation == null || PsiAnnotationSearchUtil.isAnnotatedWith(parentModifierListOwner, supportedAnnotation.getName())) &&
redundantModifiersInfo.getType().getSupportedClass().isAssignableFrom(psiModifierListOwner.getClass())) {
PsiModifierList psiModifierList = psiModifierListOwner.getModifierList();
if (psiModifierList == null ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package de.plushnikov.intellij.plugin.intention.valvar;

import com.intellij.codeInsight.intention.LowPriorityAction;
import com.intellij.ide.scratch.ScratchFileService;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiDiamondTypeUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.siyeh.ig.psiutils.CommentTracker;
import de.plushnikov.intellij.plugin.intention.AbstractLombokIntentionAction;
import de.plushnikov.intellij.plugin.settings.ProjectSettings;
import org.jetbrains.annotations.NotNull;
Expand All @@ -20,7 +24,7 @@ public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull Psi
if (!ProjectSettings.isEnabled(project, ProjectSettings.IS_VAL_ENABLED)) {
return false;
}
if (element instanceof PsiCompiledElement || !canModify(element) || !element.getLanguage().is(JavaLanguage.INSTANCE)) {
if (element instanceof PsiCompiledElement || !canModifyBP(element) || !element.getLanguage().is(JavaLanguage.INSTANCE)) {
return false;
}

Expand All @@ -34,6 +38,10 @@ public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull Psi
return context != null && isAvailableOnDeclarationStatement(context);
}

public static boolean canModifyBP(PsiElement element) {
return element.getManager().isInProject(element) || ScratchFileService.isInScratchRoot(PsiUtilCore.getVirtualFile(element));
}

@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(element, PsiDeclarationStatement.class);
Expand All @@ -56,4 +64,51 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement
public abstract void invokeOnVariable(PsiVariable psiVariable);

public abstract void invokeOnDeclarationStatement(PsiDeclarationStatement psiDeclarationStatement);

// from RemoveRedundantTypeArgumentsUtil
private PsiElement replaceExplicitWithDiamond(PsiElement psiElement) {
PsiElement replacement = createExplicitReplacementBP(psiElement);
return replacement == null ? psiElement : psiElement.replace(replacement);
}

//from PsiDiamondTypeUtil.createExplicitReplacement
private PsiElement createExplicitReplacementBP(PsiElement psiElement) {
if (psiElement instanceof PsiReferenceParameterList) {
final PsiNewExpression expression =
(PsiNewExpression) JavaPsiFacade.getElementFactory(psiElement.getProject()).createExpressionFromText("new a<>()", psiElement);
final PsiJavaCodeReferenceElement classReference = expression.getClassReference();
final PsiReferenceParameterList parameterList = classReference.getParameterList();
return parameterList;
}
return null;
}

/**
* Removes redundant type arguments which appear in any descendants of the supplied element.
*
* @param element element to start the replacement from
* <p>
* from RemoveRedundantTypeArgumentsUtil
*/
public void removeRedundantTypeArguments(PsiElement element) {
for (PsiNewExpression newExpression : PsiTreeUtil.collectElementsOfType(element, PsiNewExpression.class)) {
PsiJavaCodeReferenceElement classReference = newExpression.getClassOrAnonymousClassReference();
if (classReference != null && PsiDiamondTypeUtil.canCollapseToDiamond(newExpression, newExpression, null)) {
replaceExplicitWithDiamond(classReference.getParameterList());
}
}
PsiElementFactory factory = JavaPsiFacade.getElementFactory(element.getProject());
for (PsiMethodCallExpression call : PsiTreeUtil.collectElementsOfType(element, PsiMethodCallExpression.class)) {
PsiType[] arguments = call.getTypeArguments();
if (arguments.length == 0) continue;
PsiMethod method = call.resolveMethod();
if (method != null) {
PsiTypeParameter[] parameters = method.getTypeParameters();
if (arguments.length == parameters.length && PsiDiamondTypeUtil.areTypeArgumentsRedundant(arguments, call, false, method, parameters)) {
PsiMethodCallExpression expr = (PsiMethodCallExpression) factory.createExpressionFromText("foo()", null);
new CommentTracker().replaceAndRestoreComments(call.getTypeArgumentList(), expr.getTypeArgumentList());
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package de.plushnikov.intellij.plugin.intention.valvar.from;

import com.intellij.codeInspection.RemoveRedundantTypeArgumentsUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.util.IncorrectOperationException;
import de.plushnikov.intellij.plugin.intention.valvar.AbstractValVarIntentionAction;
import de.plushnikov.intellij.plugin.processor.ValProcessor;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class AbstractReplaceVariableWithExplicitTypeIntentionAction extends AbstractValVarIntentionAction {

Expand Down Expand Up @@ -63,11 +65,36 @@ public void invokeOnVariable(PsiVariable psiVariable) {
if (psiTypeElement == null) {
return;
}
PsiTypesUtil.replaceWithExplicitType(psiTypeElement);
RemoveRedundantTypeArgumentsUtil.removeRedundantTypeArguments(psiVariable);
replaceWithExplicitTypeBP(psiTypeElement);
removeRedundantTypeArguments(psiVariable);
executeAfterReplacing(psiVariable);
CodeStyleManager.getInstance(psiVariable.getProject()).reformat(psiVariable);
}

protected abstract void executeAfterReplacing(PsiVariable psiVariable);

@Nullable
// from PsiTypesUtil.replaceWithExplicitType
private PsiTypeElement replaceWithExplicitTypeBP(PsiTypeElement typeElement) {
PsiType type = typeElement.getType();
if (!isDenotableType(type, typeElement)) {
return null;
}
Project project = typeElement.getProject();
PsiTypeElement typeElementByExplicitType = JavaPsiFacade.getElementFactory(project).createTypeElement(type);
PsiElement explicitTypeElement = typeElement.replace(typeElementByExplicitType);
explicitTypeElement = JavaCodeStyleManager.getInstance(project).shortenClassReferences(explicitTypeElement);
return (PsiTypeElement) CodeStyleManager.getInstance(project).reformat(explicitTypeElement);
}

private boolean isDenotableType(@Nullable PsiType type, @NotNull PsiElement context) {
if (type == null || type instanceof PsiWildcardType) return false;
PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(context.getProject());
try {
PsiType typeAfterReplacement = elementFactory.createTypeElementFromText(type.getCanonicalText(), context).getType();
return type.equals(typeAfterReplacement);
} catch (IncorrectOperationException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package de.plushnikov.intellij.plugin.intention.valvar.to;

import com.intellij.codeInspection.RemoveRedundantTypeArgumentsUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase;
import com.intellij.psi.impl.PsiDiamondTypeUtil;
import de.plushnikov.intellij.plugin.intention.valvar.AbstractValVarIntentionAction;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -73,13 +72,33 @@ public void invokeOnVariable(PsiVariable psiVariable) {
return;
}
PsiJavaCodeReferenceElement referenceElementByFQClassName = elementFactory.createReferenceElementByFQClassName(variableClass.getName(), psiVariable.getResolveScope());
typeElement = (PsiTypeElement) IntroduceVariableBase.expandDiamondsAndReplaceExplicitTypeWithVar(typeElement, typeElement);
typeElement = (PsiTypeElement) expandDiamondsAndReplaceExplicitTypeWithVar(typeElement, typeElement);
typeElement.deleteChildRange(typeElement.getFirstChild(), typeElement.getLastChild());
typeElement.add(referenceElementByFQClassName);
RemoveRedundantTypeArgumentsUtil.removeRedundantTypeArguments(psiVariable);
removeRedundantTypeArguments(psiVariable);
executeAfterReplacing(psiVariable);
CodeStyleManager.getInstance(project).reformat(psiVariable);
}

protected abstract void executeAfterReplacing(PsiVariable psiVariable);

/**
* Ensure that diamond inside initializer is expanded, then replace variable type with var
* From IntroduceVariableBase
*/
private PsiElement expandDiamondsAndReplaceExplicitTypeWithVar(PsiTypeElement typeElement, PsiElement context) {
PsiElement parent = typeElement.getParent();
if (parent instanceof PsiVariable) {
PsiExpression copyVariableInitializer = ((PsiVariable) parent).getInitializer();
if (copyVariableInitializer instanceof PsiNewExpression) {
final PsiDiamondType.DiamondInferenceResult diamondResolveResult =
PsiDiamondTypeImpl.resolveInferredTypesNoCheck((PsiNewExpression) copyVariableInitializer, copyVariableInitializer);
if (!diamondResolveResult.getInferredTypes().isEmpty()) {
PsiDiamondTypeUtil.expandTopLevelDiamondsInside(copyVariableInitializer);
}
}
}

return typeElement.replace(JavaPsiFacade.getElementFactory(context.getProject()).createTypeElementFromText("var", context));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.plushnikov.superbuilder;

import lombok.experimental.SuperBuilder;
import lombok.val;

import java.util.List;

Expand Down

0 comments on commit 7505dc5

Please sign in to comment.