Skip to content

Commit 1100022

Browse files
authored
Add support for java text-blocks for expression injection (#188)
1 parent 27c6cfc commit 1100022

File tree

5 files changed

+122
-1
lines changed

5 files changed

+122
-1
lines changed

src/main/java/org/mapstruct/intellij/expression/JavaExpressionInjector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
*/
5050
public class JavaExpressionInjector implements MultiHostInjector {
5151

52-
public static final Pattern JAVA_EXPRESSION = Pattern.compile( "^\"\\s*java\\((.*)\\)\\s*\"$", Pattern.DOTALL );
52+
public static final Pattern JAVA_EXPRESSION = Pattern.compile( "^(\"|\"{3})\\s*java\\((.*)\\)\\s*\\1$",
53+
Pattern.DOTALL );
5354

5455
private static final ElementPattern<PsiElement> PATTERN =
5556
StandardPatterns.or(

src/main/java/org/mapstruct/intellij/inspection/JavaExpressionUnnecessaryWhitespacesInspector.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ private void inspectUnnecessaryWhitespaces(@NotNull ProblemsHolder problemsHolde
4444
if ( !JAVA_EXPRESSION.matcher( text ).matches() ) {
4545
return;
4646
}
47+
if ( text.charAt( 1 ) == '"') {
48+
// Text-Block
49+
return;
50+
}
4751
if ( text.indexOf( "java(" ) > 1 ) {
4852
problemsHolder.registerProblem( property,
4953
MapStructBundle.message( "inspection.java.expression.unnecessary.whitespace",

src/test/java/org/mapstruct/intellij/expression/JavaExpressionInjectionTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,43 @@ protected void withMapperToDtoWithoutAccessors(String attribute) {
706706
assertThat( elementAt.getText() ).isEqualTo( ";" );
707707
}
708708

709+
public void testWithTextBlockAndTargetDefined() {
710+
withTextBlockAndTargetDefined( "expression" );
711+
withTextBlockAndTargetDefined( "defaultExpression" );
712+
withTextBlockAndTargetDefined( "conditionExpression" );
713+
}
714+
715+
protected void withTextBlockAndTargetDefined(String attribute) {
716+
String mapping = "@Mapping(target = \"manufacturingYear\", " + attribute
717+
+ " = \"\"\"\njava(car.<caret>)\"\"\")\n";
718+
@Language("java")
719+
String mapper = formatMapper( CAR_MAPPER, mapping );
720+
PsiFile file = configureMapperByText( mapper );
721+
722+
assertThat( myFixture.completeBasic() )
723+
.extracting( LookupElementPresentation::renderElement )
724+
.extracting( LookupElementPresentation::getItemText )
725+
.contains(
726+
"getMake",
727+
"setMake",
728+
"getManufacturingDate",
729+
"setManufacturingDate",
730+
"getNumberOfSeats",
731+
"setNumberOfSeats"
732+
);
733+
734+
assertThat( myFixture.complete( CompletionType.SMART ) )
735+
.extracting( LookupElementPresentation::renderElement )
736+
.extracting( LookupElementPresentation::getItemText )
737+
.containsExactlyInAnyOrder( "getMake", "toString" );
738+
739+
PsiElement elementAt = file.findElementAt( myFixture.getCaretOffset() );
740+
assertThat( elementAt )
741+
.isNotNull()
742+
.isInstanceOf( PsiJavaToken.class );
743+
assertThat( elementAt.getText() ).isEqualTo( ";" );
744+
}
745+
709746
private PsiFile configureMapperByText(@Language("java") String text) {
710747
return myFixture.configureByText( JavaFileType.INSTANCE, text );
711748
}

src/test/java/org/mapstruct/intellij/inspection/JavaExpressionUnnecessaryWhitespacesInspectorTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.intellij.codeInsight.intention.IntentionAction;
99
import com.intellij.codeInspection.LocalInspectionTool;
10+
import com.intellij.pom.java.LanguageLevel;
1011
import org.jetbrains.annotations.NotNull;
1112

1213
import java.util.List;
@@ -54,4 +55,17 @@ public void testJavaExpressionUnnecessaryWhitespacesInspectorWhitespaceAfter() {
5455
allQuickFixes.forEach( myFixture::launchAction );
5556
myFixture.checkResultByFile( testName + "_after.java" );
5657
}
58+
59+
//Tests if inspection ignores Textblocks
60+
public void testJavaExpressionUnnecessaryWhitespacesTextBlock() {
61+
doTest();
62+
String testName = getTestName( false );
63+
List<IntentionAction> allQuickFixes = myFixture.getAllQuickFixes();
64+
assertThat( allQuickFixes ).isEmpty();
65+
}
66+
67+
@Override
68+
protected LanguageLevel getLanguageLevel() {
69+
return LanguageLevel.JDK_15;
70+
}
5771
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
7+
import org.mapstruct.BeanMapping;
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.Mapping;
10+
11+
class Source {
12+
13+
private String name;
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
}
23+
24+
class Target {
25+
26+
private String name;
27+
private String lastName;
28+
private String city;
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public String getLastName() {
39+
return lastName;
40+
}
41+
42+
public void setLastName(String lastName) {
43+
this.lastName = lastName;
44+
}
45+
46+
public String getCity() {
47+
return city;
48+
}
49+
50+
public void setCity(String city) {
51+
this.city = city;
52+
}
53+
}
54+
55+
@Mapper
56+
interface SingleMappingMapper {
57+
58+
@Mapping(target = "name", source = "name", conditionExpression = """
59+
java( !source.getName().length > 0 ) """)
60+
@Mapping(target = "lastName", source = "name",defaultExpression = """
61+
java( \" \" ) """)
62+
@Mapping(target = "city", expression = """
63+
java( \" \" ) """)
64+
Target map(Source source);
65+
}

0 commit comments

Comments
 (0)