-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quickfixes for removing unnecessary whitespace before and after Java …
…expressions (#164)
- Loading branch information
Showing
12 changed files
with
497 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
...java/org/mapstruct/intellij/inspection/JavaExpressionUnnecessaryWhitespacesInspector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.inspection; | ||
|
||
import com.intellij.codeInspection.LocalQuickFixOnPsiElement; | ||
import com.intellij.codeInspection.ProblemHighlightType; | ||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.codeInspection.util.IntentionFamilyName; | ||
import com.intellij.codeInspection.util.IntentionName; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiAnnotation; | ||
import com.intellij.psi.PsiAnnotationMemberValue; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.PsiNameValuePair; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.mapstruct.intellij.MapStructBundle; | ||
|
||
import static com.intellij.psi.PsiElementFactory.getInstance; | ||
import static org.mapstruct.intellij.expression.JavaExpressionInjector.JAVA_EXPRESSION; | ||
|
||
public class JavaExpressionUnnecessaryWhitespacesInspector extends MappingAnnotationInspectionBase { | ||
|
||
@Override | ||
void visitMappingAnnotation(@NotNull ProblemsHolder problemsHolder, @NotNull PsiAnnotation psiAnnotation, | ||
@NotNull MappingAnnotation mappingAnnotation) { | ||
inspectUnnecessaryWhitespaces( problemsHolder, mappingAnnotation.getExpressionProperty() ); | ||
inspectUnnecessaryWhitespaces( problemsHolder, mappingAnnotation.getDefaultExpressionProperty() ); | ||
inspectUnnecessaryWhitespaces( problemsHolder, mappingAnnotation.getConditionExpression() ); | ||
} | ||
|
||
private void inspectUnnecessaryWhitespaces(@NotNull ProblemsHolder problemsHolder, PsiNameValuePair property) { | ||
if ( property == null ) { | ||
return; | ||
} | ||
PsiAnnotationMemberValue value = property.getValue(); | ||
if ( value == null ) { | ||
return; | ||
} | ||
String text = value.getText(); | ||
if ( !JAVA_EXPRESSION.matcher( text ).matches() ) { | ||
return; | ||
} | ||
if ( text.indexOf( "java(" ) > 1 ) { | ||
problemsHolder.registerProblem( property, | ||
MapStructBundle.message( "inspection.java.expression.unnecessary.whitespace", | ||
"before", property.getAttributeName() ), | ||
ProblemHighlightType.WEAK_WARNING, new RemoveWhitespacesBefore(property) ); | ||
} | ||
if ( text.lastIndexOf( ')' ) < text.length() - 2) { | ||
problemsHolder.registerProblem( property, | ||
MapStructBundle.message( "inspection.java.expression.unnecessary.whitespace", | ||
"after", property.getAttributeName() ), | ||
ProblemHighlightType.WEAK_WARNING, new RemoveWhitespacesAfter(property) ); | ||
} | ||
} | ||
|
||
private static class RemoveWhitespacesBefore extends LocalQuickFixOnPsiElement { | ||
|
||
private final String name; | ||
|
||
private RemoveWhitespacesBefore(@NotNull PsiNameValuePair element) { | ||
super( element ); | ||
this.name = element.getName(); | ||
} | ||
|
||
@Override | ||
public @IntentionName @NotNull String getText() { | ||
return MapStructBundle.message( "inspection.java.expression.remove.unnecessary.whitespace", | ||
"before", name ); | ||
} | ||
|
||
@Override | ||
public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement psiElement, | ||
@NotNull PsiElement psiElement1) { | ||
if (psiElement instanceof PsiNameValuePair) { | ||
PsiNameValuePair psiNameValuePair = (PsiNameValuePair) psiElement; | ||
PsiAnnotationMemberValue value = psiNameValuePair.getValue(); | ||
if (value != null) { | ||
String text = value.getText(); | ||
psiNameValuePair.setValue( getInstance( project ) | ||
.createExpressionFromText( "\"" + text.substring( text.indexOf( "java(" ) ), value ) ); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public @IntentionFamilyName @NotNull String getFamilyName() { | ||
return MapStructBundle.message( "intention.java.expression.remove.unnecessary.whitespace" ); | ||
} | ||
|
||
@Override | ||
public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, | ||
@NotNull PsiElement endElement) { | ||
if ( !super.isAvailable( project, file, startElement, endElement ) ) { | ||
return false; | ||
} | ||
if ( !(startElement instanceof PsiNameValuePair ) ) { | ||
return false; | ||
} | ||
return ((PsiNameValuePair) startElement).getValue() != null; | ||
} | ||
} | ||
|
||
private static class RemoveWhitespacesAfter extends LocalQuickFixOnPsiElement { | ||
|
||
private final String name; | ||
|
||
private RemoveWhitespacesAfter(@NotNull PsiNameValuePair element) { | ||
super( element ); | ||
this.name = element.getName(); | ||
} | ||
|
||
@Override | ||
public @IntentionName @NotNull String getText() { | ||
return MapStructBundle.message( "inspection.java.expression.remove.unnecessary.whitespace", "after", name ); | ||
} | ||
|
||
@Override | ||
public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement psiElement, | ||
@NotNull PsiElement psiElement1) { | ||
if (psiElement instanceof PsiNameValuePair) { | ||
PsiNameValuePair psiNameValuePair = (PsiNameValuePair) psiElement; | ||
PsiAnnotationMemberValue value = psiNameValuePair.getValue(); | ||
if (value != null) { | ||
String text = value.getText(); | ||
psiNameValuePair.setValue( getInstance( project ).createExpressionFromText( | ||
text.substring( 0, text.lastIndexOf( ')' ) + 1 ) + "\"", value ) ); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public @IntentionFamilyName @NotNull String getFamilyName() { | ||
return MapStructBundle.message( "intention.java.expression.remove.unnecessary.whitespace" ); | ||
} | ||
|
||
@Override | ||
public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, | ||
@NotNull PsiElement endElement) { | ||
if ( !super.isAvailable( project, file, startElement, endElement ) ) { | ||
return false; | ||
} | ||
if ( !(startElement instanceof PsiNameValuePair ) ) { | ||
return false; | ||
} | ||
return ((PsiNameValuePair) startElement).getValue() != null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/resources/inspectionDescriptions/JavaExpressionUnnecessaryWhitespaces.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<html> | ||
<body> | ||
<p> | ||
This inspection reports when a java expression has whitespaces before or after the <code>java()</code> block. | ||
</p> | ||
<pre><code> | ||
//wrong | ||
@Mapper | ||
public interface EmployeeMapper { | ||
@Mapping(source = "employeeName", expression = " java(\"Name\")") | ||
Employee toEmployee(EmployeeDto employeeDto, @Context CycleAvoidingMappingContext context); | ||
} | ||
</code></pre> | ||
</p> | ||
<p> | ||
<pre><code> | ||
//correct | ||
@Mapper | ||
public interface EmployeeMapper { | ||
@Mapping(source = "employeeName", expression = "java(\"Name\")") | ||
Employee toEmployee(EmployeeDto employeeDto, @Context CycleAvoidingMappingContext context); | ||
} | ||
</code></pre> | ||
</p> | ||
<!-- tooltip end --> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
.../org/mapstruct/intellij/inspection/JavaExpressionUnnecessaryWhitespacesInspectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.inspection; | ||
|
||
import com.intellij.codeInsight.intention.IntentionAction; | ||
import com.intellij.codeInspection.LocalInspectionTool; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class JavaExpressionUnnecessaryWhitespacesInspectorTest extends BaseInspectionTest { | ||
@Override | ||
protected @NotNull Class<? extends LocalInspectionTool> getInspection() { | ||
return JavaExpressionUnnecessaryWhitespacesInspector.class; | ||
} | ||
|
||
public void testJavaExpressionUnnecessaryWhitespacesInspectorWhitespaceBefore() { | ||
doTest(); | ||
String testName = getTestName( false ); | ||
List<IntentionAction> allQuickFixes = myFixture.getAllQuickFixes(); | ||
|
||
assertThat( allQuickFixes ) | ||
.extracting( IntentionAction::getText ) | ||
.as( "Intent Text" ) | ||
.containsExactly( | ||
"Remove unnecessary whitespaces before conditionExpression", | ||
"Remove unnecessary whitespaces before defaultExpression", | ||
"Remove unnecessary whitespaces before expression" | ||
); | ||
|
||
allQuickFixes.forEach( myFixture::launchAction ); | ||
myFixture.checkResultByFile( testName + "_after.java" ); | ||
} | ||
|
||
public void testJavaExpressionUnnecessaryWhitespacesInspectorWhitespaceAfter() { | ||
doTest(); | ||
String testName = getTestName( false ); | ||
List<IntentionAction> allQuickFixes = myFixture.getAllQuickFixes(); | ||
|
||
assertThat( allQuickFixes ) | ||
.extracting( IntentionAction::getText ) | ||
.as( "Intent Text" ) | ||
.containsExactly( | ||
"Remove unnecessary whitespaces after conditionExpression", | ||
"Remove unnecessary whitespaces after defaultExpression", | ||
"Remove unnecessary whitespaces after expression" | ||
); | ||
|
||
allQuickFixes.forEach( myFixture::launchAction ); | ||
myFixture.checkResultByFile( testName + "_after.java" ); | ||
} | ||
} |
Oops, something went wrong.