|
| 1 | +/* |
| 2 | + * Copyright MapStruct Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +package org.mapstruct.intellij.inspection; |
| 7 | + |
| 8 | +import com.intellij.codeInspection.LocalQuickFixOnPsiElement; |
| 9 | +import com.intellij.codeInspection.ProblemHighlightType; |
| 10 | +import com.intellij.codeInspection.ProblemsHolder; |
| 11 | +import com.intellij.codeInspection.util.IntentionFamilyName; |
| 12 | +import com.intellij.codeInspection.util.IntentionName; |
| 13 | +import com.intellij.openapi.project.Project; |
| 14 | +import com.intellij.psi.PsiAnnotation; |
| 15 | +import com.intellij.psi.PsiAnnotationMemberValue; |
| 16 | +import com.intellij.psi.PsiElement; |
| 17 | +import com.intellij.psi.PsiFile; |
| 18 | +import com.intellij.psi.PsiNameValuePair; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | +import org.mapstruct.intellij.MapStructBundle; |
| 21 | + |
| 22 | +import static com.intellij.psi.PsiElementFactory.getInstance; |
| 23 | +import static org.mapstruct.intellij.expression.JavaExpressionInjector.JAVA_EXPRESSION; |
| 24 | + |
| 25 | +public class JavaExpressionUnnecessaryWhitespacesInspector extends MappingAnnotationInspectionBase { |
| 26 | + |
| 27 | + @Override |
| 28 | + void visitMappingAnnotation(@NotNull ProblemsHolder problemsHolder, @NotNull PsiAnnotation psiAnnotation, |
| 29 | + @NotNull MappingAnnotation mappingAnnotation) { |
| 30 | + inspectUnnecessaryWhitespaces( problemsHolder, mappingAnnotation.getExpressionProperty() ); |
| 31 | + inspectUnnecessaryWhitespaces( problemsHolder, mappingAnnotation.getDefaultExpressionProperty() ); |
| 32 | + inspectUnnecessaryWhitespaces( problemsHolder, mappingAnnotation.getConditionExpression() ); |
| 33 | + } |
| 34 | + |
| 35 | + private void inspectUnnecessaryWhitespaces(@NotNull ProblemsHolder problemsHolder, PsiNameValuePair property) { |
| 36 | + if ( property == null ) { |
| 37 | + return; |
| 38 | + } |
| 39 | + PsiAnnotationMemberValue value = property.getValue(); |
| 40 | + if ( value == null ) { |
| 41 | + return; |
| 42 | + } |
| 43 | + String text = value.getText(); |
| 44 | + if ( !JAVA_EXPRESSION.matcher( text ).matches() ) { |
| 45 | + return; |
| 46 | + } |
| 47 | + if ( text.indexOf( "java(" ) > 1 ) { |
| 48 | + problemsHolder.registerProblem( property, |
| 49 | + MapStructBundle.message( "inspection.java.expression.unnecessary.whitespace", |
| 50 | + "before", property.getAttributeName() ), |
| 51 | + ProblemHighlightType.WEAK_WARNING, new RemoveWhitespacesBefore(property) ); |
| 52 | + } |
| 53 | + if ( text.lastIndexOf( ')' ) < text.length() - 2) { |
| 54 | + problemsHolder.registerProblem( property, |
| 55 | + MapStructBundle.message( "inspection.java.expression.unnecessary.whitespace", |
| 56 | + "after", property.getAttributeName() ), |
| 57 | + ProblemHighlightType.WEAK_WARNING, new RemoveWhitespacesAfter(property) ); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static class RemoveWhitespacesBefore extends LocalQuickFixOnPsiElement { |
| 62 | + |
| 63 | + private final String name; |
| 64 | + |
| 65 | + private RemoveWhitespacesBefore(@NotNull PsiNameValuePair element) { |
| 66 | + super( element ); |
| 67 | + this.name = element.getName(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public @IntentionName @NotNull String getText() { |
| 72 | + return MapStructBundle.message( "inspection.java.expression.remove.unnecessary.whitespace", |
| 73 | + "before", name ); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement psiElement, |
| 78 | + @NotNull PsiElement psiElement1) { |
| 79 | + if (psiElement instanceof PsiNameValuePair) { |
| 80 | + PsiNameValuePair psiNameValuePair = (PsiNameValuePair) psiElement; |
| 81 | + PsiAnnotationMemberValue value = psiNameValuePair.getValue(); |
| 82 | + if (value != null) { |
| 83 | + String text = value.getText(); |
| 84 | + psiNameValuePair.setValue( getInstance( project ) |
| 85 | + .createExpressionFromText( "\"" + text.substring( text.indexOf( "java(" ) ), value ) ); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public @IntentionFamilyName @NotNull String getFamilyName() { |
| 92 | + return MapStructBundle.message( "intention.java.expression.remove.unnecessary.whitespace" ); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, |
| 97 | + @NotNull PsiElement endElement) { |
| 98 | + if ( !super.isAvailable( project, file, startElement, endElement ) ) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + if ( !(startElement instanceof PsiNameValuePair ) ) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + return ((PsiNameValuePair) startElement).getValue() != null; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static class RemoveWhitespacesAfter extends LocalQuickFixOnPsiElement { |
| 109 | + |
| 110 | + private final String name; |
| 111 | + |
| 112 | + private RemoveWhitespacesAfter(@NotNull PsiNameValuePair element) { |
| 113 | + super( element ); |
| 114 | + this.name = element.getName(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public @IntentionName @NotNull String getText() { |
| 119 | + return MapStructBundle.message( "inspection.java.expression.remove.unnecessary.whitespace", "after", name ); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement psiElement, |
| 124 | + @NotNull PsiElement psiElement1) { |
| 125 | + if (psiElement instanceof PsiNameValuePair) { |
| 126 | + PsiNameValuePair psiNameValuePair = (PsiNameValuePair) psiElement; |
| 127 | + PsiAnnotationMemberValue value = psiNameValuePair.getValue(); |
| 128 | + if (value != null) { |
| 129 | + String text = value.getText(); |
| 130 | + psiNameValuePair.setValue( getInstance( project ).createExpressionFromText( |
| 131 | + text.substring( 0, text.lastIndexOf( ')' ) + 1 ) + "\"", value ) ); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public @IntentionFamilyName @NotNull String getFamilyName() { |
| 138 | + return MapStructBundle.message( "intention.java.expression.remove.unnecessary.whitespace" ); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, |
| 143 | + @NotNull PsiElement endElement) { |
| 144 | + if ( !super.isAvailable( project, file, startElement, endElement ) ) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + if ( !(startElement instanceof PsiNameValuePair ) ) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + return ((PsiNameValuePair) startElement).getValue() != null; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments